text:
"""Created on Wed Sep 07 2016 12:09.

@author: Nathan Budd
"""
import unittest
import numpy as np
import matplotlib.pyplot as plt
from ..model_coe import ModelCOE
from ..perturb_zero import PerturbZero
from ..reference_coe import ReferenceCOE
from ..warm_start_constant import WarmStartConstant
from ...mcpi.mcpi import MCPI
from ...mcpi.mcpi_approx import MCPIapprox
from ...orbital_mech.element_sets.orb_coe import OrbCOE

class TestModelCOE(unittest.TestCase):
    """Test class for ModelCOE."""

    def setUp(self):
        """."""
        mu = 1.
        self.mcoe = ModelCOE(mu)

    def test_instantiation(self):
        """."""
        self.assertIsInstance(self.mcoe, ModelCOE)

    def test_getattr(self):
        """."""
        self.assertEqual(self.mcoe.mu, 1)

    def test_setattr(self):
        """."""
        self.mcoe.mu = 2.
        self.assertEqual(self.mcoe.mu, 2)

    def test_dynamics(self):
        x = np.array([[2., .5, 1., .1, .1, 0.],
                      [4., .5, 1., .1, .1, 0.],
                      [8., .5, 1., .1, .1, 0.]])
        t = np.array([[0.], [1.], [2.]])

        xdot = self.mcoe(t, x)
        print(self.mcoe.Xdot)
        self.assertEqual(xdot.shape, (3, 6))

    def test_dynamics_integration(self):
        domains = (0., 6.)
        N = 20,
        X0 = [2., .1, .1, 0., 0., 0.]
        tol = 1e-10

        mcpi = MCPI(self.mcoe, domains, N, WarmStartConstant(), X0, tol)
        X_approx = mcpi.solve_serial()
        print(mcpi.iterations)

        T = np.linspace(domains[0], domains[1], 100).reshape((100, 1))
        x_approx = X_approx(T)
        plt_p, = plt.plot(T, [row[0] for row in x_approx], label='p')
        plt_e, = plt.plot(T, [row[1] for row in x_approx], label='e')
        plt_i, = plt.plot(T, [row[2] for row in x_approx], label='i')
        plt_W, = plt.plot(T, [row[3] for row in x_approx], label='W')
        plt_w, = plt.plot(T, [row[4] for row in x_approx], label='w')
        plt_f, = plt.plot(T, [row[5] for row in x_approx], label='f')
        plt.legend(handles=[plt_p, plt_e, plt_i, plt_W, plt_w, plt_f])
        plt.show()
        self.assertIsInstance(X_approx, MCPIapprox)

    def test_reference(self):
        domains = (0., 1.)
        T = np.linspace(domains[0], domains[1], 100).reshape((100, 1))
        X0 = np.array([[2., .1, .1, 0., 0., 0.]])
        X = ReferenceCOE(X0, 1.)(T).tolist()

        plt_p, = plt.plot(T, [row[0] for row in X], label='p')
        plt_e, = plt.plot(T, [row[1] for row in X], label='e')
        plt_i, = plt.plot(T, [row[2] for row in X], label='i')
        plt_W, = plt.plot(T, [row[3] for row in X], label='W')
        plt_w, = plt.plot(T, [row[4] for row in X], label='w')
        plt_f, = plt.plot(T, [row[5] for row in X], label='f')
        plt.legend(handles=[plt_p, plt_e, plt_i, plt_W, plt_w, plt_f])
        plt.show()
        self.assertEqual(X[-1][0], X0[0,0])

max_stars_repo_name: lasr/orbit

max_issues_repo_name: lasr/orbit

max_forks_repo_name: lasr/orbit


text:
import logging
import unittest
from functools import partial

import numpy as np
from sklearn import datasets
from sklearn.model_selection import train_test_split

from openTSNE import affinity

affinity.log.setLevel(logging.ERROR)

Multiscale = partial(affinity.Multiscale, method="exact")
MultiscaleMixture = partial(affinity.MultiscaleMixture, method="exact")
PerplexityBasedNN = partial(affinity.PerplexityBasedNN, method="exact")
FixedSigmaNN = partial(affinity.FixedSigmaNN, method="exact")

class TestPerplexityBased(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.x = np.random.normal(100, 50, (91, 4))

    def test_properly_reduces_large_perplexity(self):
        aff = PerplexityBasedNN(self.x, perplexity=40)
        self.assertEqual(aff.perplexity, 30)

    def test_handles_reducing_perplexity_value(self):
        perplexity = 20
        k_neighbors = perplexity * 3
        aff = PerplexityBasedNN(self.x, perplexity=perplexity)

        self.assertEqual(aff.perplexity, perplexity)

        # Check that the initial `P` matrix is allright
        n_samples = self.x.shape[0]
        original_P = aff.P.copy()
        # Can't check for equality because the matrix is symmetrized therefore
        # each point may have non-zero values in more than just the k neighbors
        self.assertTrue(original_P.nnz >= n_samples * k_neighbors)

        # Check that lowering the perplexity properly changes affinity matrix
        perplexity = 10
        k_neighbors = perplexity * 3

        aff.set_perplexity(perplexity)
        self.assertEqual(aff.perplexity, perplexity)
        reduced_P = aff.P.copy()
        self.assertTrue(reduced_P.nnz >= n_samples * k_neighbors)
        self.assertTrue(reduced_P.nnz < original_P.nnz,
                        "Lower perplexities should consider less neighbors, "
                        "resulting in a sparser affinity matrix")

        # Raising the perplexity above the initial value would need to recompute
        # the nearest neighbors, so it should raise an error
        with self.assertRaises(RuntimeError):
            aff.set_perplexity(30)

class TestMultiscale(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.x = np.random.normal(100, 50, (91, 4))

    def test_handles_too_large_perplexities(self):
        # x has 91 samples, this means that the max perplexity that we allow is
        # (91 - 1) / 3 = 30. -1 because we don't consider ith point. Anything
        # above that should be ignored or corrected

        ms = Multiscale(self.x, perplexities=[20])
        np.testing.assert_array_equal(
            ms.perplexities, [20],
            "Incorrectly changed perplexity that was within a valid range",
        )

        ms = Multiscale(self.x, perplexities=[20, 40])
        np.testing.assert_array_equal(
            ms.perplexities, [20, 30],
            "Did not lower large perplexity."
        )

        ms = Multiscale(self.x, perplexities=[20, 40, 60])
        np.testing.assert_array_equal(
            ms.perplexities, [20, 30],
            "Did not drop large perplexities when more than one was too large."
        )

        ms = Multiscale(self.x, perplexities=[20, 30, 40, 60])
        np.testing.assert_array_equal(
            ms.perplexities, [20, 30],
            "Did not drop duplicate corrected perplexity."
        )

    def test_handles_changing_perplexities(self):
        perplexities = [15, 25]
        k_neighbors = perplexities[-1] * 3

        ms = Multiscale(self.x, perplexities=perplexities)
        np.testing.assert_equal(ms.perplexities, perplexities)

        # Check that the initial `P` matrix is allright
        n_samples = self.x.shape[0]
        original_P = ms.P.copy()
        # Can't check for equality because the matrix is symmetrized therefore
        # each point may have non-zero values in more than just the k neighbors
        self.assertTrue(original_P.nnz >= n_samples * k_neighbors)

        # Check that lowering the perplexity properly changes affinity matrix
        new_perplexities = [10, 20]
        k_neighbors = new_perplexities[-1] * 3
        ms.set_perplexities(new_perplexities)
        np.testing.assert_equal(ms.perplexities, new_perplexities)

        reduced_P = ms.P.copy()
        self.assertTrue(reduced_P.nnz >= n_samples * k_neighbors)
        self.assertTrue(reduced_P.nnz < original_P.nnz,
                        "Lower perplexities should consider less neighbors, "
                        "resulting in a sparser affinity matrix")

        # Raising the perplexity above the initial value would need to recompute
        # the nearest neighbors, so it should raise an error
        with self.assertRaises(RuntimeError):
            ms.set_perplexities([20, 30])

class TestAffinityMatrixCorrectness(unittest.TestCase):
    affinity_classes = [
        ("PerplexityBasedNN", PerplexityBasedNN),
        ("FixedSigmaNN", partial(FixedSigmaNN, sigma=1)),
        ("MultiscaleMixture", partial(MultiscaleMixture, perplexities=[10, 20])),
        ("Multiscale", partial(Multiscale, perplexities=[10, 20])),
    ]

    @classmethod
    def setUpClass(cls):
        cls.iris = datasets.load_iris().data

    def test_that_regular_matrix_sums_to_one(self):
        for method_name, cls in self.affinity_classes:
            aff: affinity.Affinities = cls(self.iris)
            self.assertAlmostEqual(np.sum(aff.P), 1, msg=method_name)

    def test_that_to_new_transform_matrix_treats_each_datapoint_separately(self):
        x_train, x_test = train_test_split(self.iris, test_size=0.33, random_state=42)

        for method_name, cls in self.affinity_classes:
            aff: affinity.Affinities = cls(x_train)
            P = aff.to_new(x_test)
            np.testing.assert_allclose(
                np.asarray(np.sum(P, axis=1)).ravel(),
                np.ones(len(x_test)),
                err_msg=method_name,
            )

ext: py

max_stars_repo_name: acopar/openTSNE

max_issues_repo_name: acopar/openTSNE

max_forks_repo_name: acopar/openTSNE

reason: import numpy


text:
from __future__ import division
import numpy as np
import pandas as pd
from logomaker.src.error_handling import check, handle_errors

@handle_errors
def validate_matrix(df, matrix_type=None, allow_nan=False):
    """
    Checks to make sure that the input dataframe, df, represents a valid
    matrix, i.e., an object that can be displayed as a logo.

    parameters
    ----------

    df: (dataframe)
        A pandas dataframe where each row represents an (integer) position
        and each column represents to a (single) character.

    matrix_type: (None or str)
        If 'probability', validates df as a probability matrix, i.e., all
        elements are in [0,1] and rows are normalized). If 'information',
        validates df as an information matrix, i.e., all elements >= 0.

    allow_nan: (bool)
        Whether to allow NaN entries in the matrix.

    returns
    -------
    out_df: (dataframe)
        A cleaned-up version of df (if possible).
    """

    # check that df is a dataframe
    check(isinstance(df, pd.DataFrame),
          'out_df needs to be a valid pandas out_df, '
          'out_df entered: %s' % type(df))

    # create copy of df so we don't overwrite the user's data
    out_df = df.copy()

    # check that type is valid
    check(matrix_type in {None, 'probability', 'information'},
          'matrix_type = %s; must be None, "probability", or "information"' %
          matrix_type)

    # check that allow_nan is boolean
    check(isinstance(allow_nan, bool),
          'allow_nan must be of type bool; is type %s.' % type(allow_nan))

    if not allow_nan:
        # make sure all entries are finite numbers
        check(np.isfinite(out_df.values).all(),
              'some matrix elements are not finite. '
              'Set allow_nan=True to allow this.')

    # make sure the matrix has a finite number of rows and columns
    check(out_df.shape[0] >= 1, 'df has zero rows. Needs multiple rows.')
    check(out_df.shape[1] >= 1, 'df has zero columns. Needs multiple columns.')

    # check that all column names are strings and have length 1
    for i, col in enumerate(out_df.columns):

        # convert from unicode to string for python 2
        col = str(col)
        check(isinstance(col, str),
              'column number %d is of type %s; must be a str' % (i, col))
        check(len(col) == 1,
              'column %d is %s and has length %d; ' % (i, repr(col), len(col))
              + 'must have length 1.')

    # sort columns alphabetically
    char_cols = list(out_df.columns)
    char_cols.sort()
    out_df = out_df[char_cols]

    # name out_df.index as 'pos'
    out_df.index.name = 'pos'

    # try casting df.index as type int
    try:
        int_index = out_df.index.astype(int)
    except TypeError:
        check(False,
              'could not convert df.index to type int. Check that '
              'all positions have integer numerical values.')

    # make sure that df.index values have not changed
    check(all(int_index == out_df.index),
          'could not convert df.index values to int without changing'
          'some values. Make sure that df.index values are integers.')

    # check that all index values are unique
    check(len(set(out_df.index)) == len(out_df.index),
          'not all values of df.index are unique. Make sure all are unique.')

    # if type is 'information', make sure elements are nonnegative
    if matrix_type is 'information':

        # make sure all elements are nonnegative
        check(all(df.values.ravel() >= 0), 'not all values in df are >=0.')

    # if type is 'probability', make sure elements are valid probabilities
    elif matrix_type is 'probability':

        # make sure all values are non-negative
        check(all(df.values.ravel() >= 0),
              'not all values in df are >=0.')

        # check to see if values sum to one
        sums = df.sum(axis=1).values

        # if any sums are close to zero, abort
        check(not any(np.isclose(sums, 0.0)),
              'some columns in df sum to nearly zero.')

        # if any sums are not close to one, renormalize all sums
        if not all(np.isclose(sums, 1.0)):
            print('in validate_matrix(): Row sums in df are not close to 1. '
                  'Reormalizing rows...')
            df.loc[:, :] = df.values / sums[:, np.newaxis]
            out_df = df.copy()

    # nothing more to check if type is None
    elif matrix_type is None:
        pass

    # return cleaned-up out_df
    return out_df

@handle_errors
def validate_probability_mat(df):
    """
    Verifies that the input dataframe df indeed represents a
    probability matrix. Renormalizes df with a text warning if it is not
    already normalized. Throws an error if df cannot be reliably normalized.

    parameters
    ----------

    df: (dataframe)
        A pandas dataframe where each row represents an (integer) position
        and each column represents to a (single) character.

    returns
    -------
    prob_df: (dataframe)
        A cleaned-up and normalized version of df (if possible).
    """

    # Validate as a matrix. Make sure this contains no NaN values
    prob_df = validate_matrix(df, allow_nan=False)

    # Make sure all values are non-negative
    check(all(prob_df.values.ravel() >= 0),
          'not all values in df are >=0.')

    # Check to see if values sum to one
    sums = prob_df.sum(axis=1).values

    # If any sums are close to zero, abort
    check(not any(np.isclose(sums, 0.0)),
          'some columns in prob_df sum to nearly zero.')

    # If any sums are not close to one, renormalize all sums
    if not all(np.isclose(sums, 1.0)):
        print('in validate_probability_mat(): '
              'Row sums in df are not close to 1. '
              'Reormalizing rows...')
        prob_df.loc[:, :] = prob_df.values / sums[:, np.newaxis]

    # Return validated probability matrix to user
    return prob_df

max_stars_repo_name: ruxi/logomaker

max_issues_repo_name: ruxi/logomaker

max_forks_repo_name: ruxi/logomaker


text:
import numpy as np
import torch.nn.functional as F
from torch import nn
import torch
import params
import math
from model.encoder import DenseEncoder

class BaseModel(nn.Module):
  def load(self, path):
    if use_cuda:
      params = torch.load(path)
    else:
      params = torch.load(path, map_location=lambda storage, loc: storage)

    state = self.state_dict()
    for name, val in params.items():
      if name in state:
        assert state[name].shape == val.shape, "%s size has changed from %s to %s" % \
                             (name, state[name].shape, val.shape)
        state[name].copy_(val)
      else:
        print("WARNING: %s not in model during model loading!" % name)

  def save(self, path):
    torch.save(self.state_dict(), path)

class ScaledDotProductAttention(nn.Module):
  ''' Scaled Dot-Product Attention '''

  def __init__(self, temperature, attn_dropout=0.0): #either apply dropout after adding sc scores or don't apply anything
    super().__init__()
    self.temperature = temperature
    self.dropout = nn.Dropout(attn_dropout)

  def forward(self, q, k, mask=None, PE_solution_scores=None):

    attn = torch.matmul(q / self.temperature, k.transpose(2, 3)) #(bs, num_heads, max_len_q, max_len_k)
    #print("attn:", attn.shape)

    if PE_solution_scores is not None:
      sc = PE_solution_scores
      attn = (attn+sc)/2.0

    if mask is not None:
      attn = attn.masked_fill(mask == 0, -1e9)

    attn = self.dropout(F.softmax(attn, dim=-1))#(bs, num_heads, max_len_q, max_len_k)
    return attn

class MultiHeadAttention(nn.Module):
    def __init__(self, n_head=4, d_model=params.state_dim, d_k=32, d_v=32,dropout=0.0,
           include_res_ln=True, return_att_weights=False, query_mode='global'):
      super(MultiHeadAttention, self).__init__()

      self.include_res_ln = include_res_ln
      self.query_mode = query_mode
      self.return_att_weights = return_att_weights
      self.n_head = n_head
      self.d_k = d_k
      self.d_v = d_v
      self.d_model = d_model
      self.w_qs = nn.Linear(d_model, n_head * d_k, bias=False)
      self.w_ks = nn.Linear(d_model, n_head * d_k, bias=False)
      self.w_vs = nn.Linear(d_model, n_head * d_v, bias=False)
      self.fc = nn.Linear(n_head * d_v, d_model, bias=False)
      self.attention = ScaledDotProductAttention(temperature=d_k ** 0.5, attn_dropout=dropout)
      self.dropout = nn.Dropout(dropout)
      self.layer_norm = nn.LayerNorm(d_model, eps=1e-6)

    def forward(self, PE_states, global_state, PE_statements, PE_operators, PE_solution_scores,
          att_type, mask):

      d_k, d_v, n_head = self.d_k, self.d_v, self.n_head
      sz_b = global_state.size(0) #batch_size
      q = global_state #(bs, max_len_g, state_dim)
      if self.query_mode == 'PE':
        q = q.view(sz_b, -1, self.d_model)
      k = PE_states.view(sz_b, -1, self.d_model) #(bs, num_examples* max_len_PE, state_dim)
      v_s, v_o = PE_statements.view(sz_b, -1, self.d_model), PE_operators.view(sz_b, -1, self.d_model)#(bs, num_examples* max_len_PE, state_dim)
      len_q, len_k, len_v = q.size(1), k.size(1), v_s.size(1) #(max_len_g, max_len_PE*num_examples, max_len_PE*num_examples)

      residual = q

      # Pass through the pre-attention projection: b x lq x (n*dv)
      # Separate different heads: b x lq x n x dv
      q = self.w_qs(q).view(sz_b, len_q, n_head, d_k)
      k = self.w_ks(k).view(sz_b, len_k, n_head, d_k)
      v_s = self.w_vs(v_s).view(sz_b, len_v, n_head, d_v)
      v_o = self.w_vs(v_o).view(sz_b, len_v, n_head, d_v)

      # Transpose for attention dot product: b x n x lq x dv
      q, k, v_s, v_o = q.transpose(1, 2), k.transpose(1, 2), v_s.transpose(1, 2), v_o.transpose(1, 2)

      if mask is not None:
        mask = mask.unsqueeze(1)   # For head axis broadcasting.

      if att_type =='ca_sc':
        PE_solution_scores = torch.repeat_interleave(PE_solution_scores.view(sz_b, -1).unsqueeze(1), \
                    repeats=len_q, dim=-2).unsqueeze(1)
      else:
        PE_solution_scores = None

      # calculate attention scores
      attn = self.attention(q, k, mask, PE_solution_scores)#(bs, num_heads, max_len_g, max_len_PE)

      statement_pred = torch.matmul(attn, v_s)
      statement_pred = statement_pred.transpose(1, 2).contiguous().view(sz_b, len_q, -1) #(bs, max_len_g, num_heads*state_dim)
      statement_pred = self.dropout(self.fc(statement_pred)) #(bs, max_len_g, state_dim)
      if self.include_res_ln:
        statement_pred += residual
        statement_pred = self.layer_norm(statement_pred)

      operator_pred = torch.matmul(attn, v_o)
      operator_pred = operator_pred.transpose(1, 2).contiguous().view(sz_b, len_q, -1)#(bs, max_len_g, num_heads*state_dim)
      operator_pred = self.dropout(self.fc(operator_pred)) #(bs, max_len_g, state_dim)
      if self.include_res_ln:
        operator_pred += residual
        operator_pred = self.layer_norm(operator_pred)

      if self.return_att_weights:
        return statement_pred, operator_pred, attn
      else:
        return statement_pred, operator_pred, None

    def predict(self, PE_states, global_state, PE_statements, PE_solution_scores, att_type, mask):
      # only statements are predicted here

      d_k, d_v, n_head = self.d_k, self.d_v, self.n_head
      sz_b = global_state.size(0) #batch_size
      q = global_state #(bs, max_len_g, state_dim)
      if self.query_mode == 'PE':
        q = q.view(sz_b, -1, self.d_model)
      k = PE_states.view(sz_b, -1, self.d_model) #(bs, num_examples* max_len_PE, state_dim)
      v_s = PE_statements.view(sz_b, -1, self.d_model)#(bs, num_examples* max_len_PE, state_dim)

      len_q, len_k, len_v = q.size(1), k.size(1), v_s.size(1) #(max_len_g, max_len_PE*num_examples, max_len_PE*num_examples)

      residual = q

      # Pass through the pre-attention projection: b x lq x (n*dv)
      # Separate different heads: b x lq x n x dv
      q = self.w_qs(q).view(sz_b, len_q, n_head, d_k)
      k = self.w_ks(k).view(sz_b, len_k, n_head, d_k)
      v_s = self.w_vs(v_s).view(sz_b, len_v, n_head, d_v)

      # Transpose for attention dot product: b x n x lq x dv
      q, k, v_s = q.transpose(1, 2), k.transpose(1, 2), v_s.transpose(1, 2)

      if mask is not None:
        mask = mask.unsqueeze(1)   # For head axis broadcasting.

      if att_type =='ca_sc':
        PE_solution_scores = torch.repeat_interleave(PE_solution_scores.view(sz_b, -1).unsqueeze(1), \
                    repeats=len_q, dim=-2).unsqueeze(1)
      else:
        PE_solution_scores = None

      attn = self.attention(q, k, mask, PE_solution_scores)#(bs, num_heads, max_len_g, max_len_PE)
      statement_pred = torch.matmul(attn, v_s)
      statement_pred = statement_pred.transpose(1, 2).contiguous().view(sz_b, len_q, -1) #(bs, max_len_g, num_heads*state_dim)
      statement_pred = self.dropout(self.fc(statement_pred)) #(bs, max_len_g, state_dim)
      if self.include_res_ln:
        statement_pred += residual
        statement_pred = self.layer_norm(statement_pred)

      if self.return_att_weights:
        return statement_pred, attn
      else:
        return statement_pred, None

class PositionalEncoding(nn.Module):

  def __init__(self, d_hid, n_position=200):
    super(PositionalEncoding, self).__init__()

    # Not a parameter
    self.register_buffer('pos_table', self._get_sinusoid_encoding_table(n_position, d_hid))

  def _get_sinusoid_encoding_table(self, n_position, d_hid):
    ''' Sinusoid position encoding table '''

    def get_position_angle_vec(position):
      return [position / np.power(10000, 2 * (hid_j // 2) / d_hid) for hid_j in range(d_hid)]

    sinusoid_table = np.array([get_position_angle_vec(pos_i) for pos_i in range(n_position)])
    sinusoid_table[:, 0::2] = np.sin(sinusoid_table[:, 0::2])  # dim 2i
    sinusoid_table[:, 1::2] = np.cos(sinusoid_table[:, 1::2])  # dim 2i+1

    return torch.FloatTensor(sinusoid_table).unsqueeze(0)

  def forward(self, x):
    return x + self.pos_table[:, :x.size(1)].clone().detach()

class PositionwiseFeedForward(nn.Module):
  ''' A two-feed-forward-layer module '''

  def __init__(self, d_in, d_hid, dropout=0.0):
    super().__init__()
    self.w_1 = nn.Linear(d_in, d_hid) # position-wise
    self.w_2 = nn.Linear(d_hid, d_in) # position-wise
    self.layer_norm = nn.LayerNorm(d_in, eps=1e-6)
    self.dropout = nn.Dropout(dropout)

  def forward(self, x):
    residual = x
    x = self.w_2(F.relu(self.w_1(x)))
    x = self.dropout(x)
    x += residual
    x = self.layer_norm(x)
    return x

class BasicAggModel(nn.Module):

  def __init__(self, include_value_emb=True, include_pos_emb=True, include_ff=True, include_res_ln=True,
        dropout=0.0, d_inner=2048, d_model=params.state_dim, return_att_weights=False, n_head=8,
        d_k=64, head_params=None, state_emb_params=None, include_state_emb=False, include_dense_proj=True,
        query_mode='global'):
    super(BasicAggModel, self).__init__()

    self.include_value_emb = include_value_emb
    self.query_mode = query_mode
    self.include_pos_emb = include_pos_emb
    self.include_ff = include_ff
    self.include_res_ln = include_res_ln
    self.include_dense_proj = include_dense_proj
    self.include_state_emb = include_state_emb
    self.d_inner = d_inner
    self.d_model = d_model
    self.return_att_weights = return_att_weights
    self.mha = MultiHeadAttention(n_head=n_head, d_k=d_k, d_v=d_k, dropout=dropout,\
                    return_att_weights=return_att_weights, include_res_ln=include_res_ln,
                    query_mode=query_mode)
    self.statement_ff = PositionwiseFeedForward(self.d_model, self.d_inner, dropout=dropout)
    self.operator_ff = PositionwiseFeedForward(self.d_model, self.d_inner, dropout=dropout)
    self.position_enc = PositionalEncoding(d_model)
    self.statement_layer_norm = nn.LayerNorm(params.num_statements, eps=1e-6)
    self.operator_layer_norm = nn.LayerNorm(params.num_operators, eps=1e-6)
    self.layer_norm = nn.LayerNorm(d_model, eps=1e-6)
    self.dropout = nn.Dropout(p=dropout)

    if self.include_dense_proj:
      self.statement_dense_proj = nn.Linear(d_model, params.num_statements, bias=False)
      self.operator_dense_proj = nn.Linear(d_model, params.num_operators, bias=False)
    if self.include_value_emb:
      self.statement_emb = nn.Embedding(params.num_statements+1, d_model, padding_idx=params.num_statements)
      self.operator_emb = nn.Embedding(params.num_operators+1, d_model, padding_idx=params.num_operators)

    if self.include_state_emb:
      self.state_encoder = DenseEncoder()

    # parameter initializations
    if head_params!=None and self.include_dense_proj:
      self.statement_dense_proj.weight.data = head_params[0]
      self.operator_dense_proj.weight.data = head_params[1]

    if state_emb_params!=None and self.include_state_emb:
      model_dict = self.state_encoder.state_dict()
      model_dict.update(state_emb_params)
      self.state_encoder.load_state_dict(model_dict)

    for name, p in self.named_parameters():
      if name!='statement_emb.weight' and name!='operator_emb.weight' and name!='statement_dense_proj.weight' \
      and name!='operator_dense_proj.weight' and p.dim() > 1:
        nn.init.xavier_uniform_(p)

  def get_pad_mask(self, seq, pad_idx):
    return (seq != pad_idx)

  def forward(self, PE_states, global_state, PE_statements, PE_operators, PE_solution_scores, att_type):

    if self.include_state_emb:
      max_set_size_PE = PE_states.shape[-3]
      bs = global_state.shape[0]
      max_examples_PE = PE_states.shape[1]

      global_state = self.state_encoder(global_state.view(-1, 5, 12, 22)).view(bs, -1, params.state_dim)
      PE_states = self.state_encoder(PE_states.view(-1, max_set_size_PE, 12, 22)).view(bs, max_examples_PE, -1, params.state_dim)

    #Learn embedding of statements and operators
    if self.include_value_emb:
      PE_statements = self.statement_emb(PE_statements)
      PE_operators = self.operator_emb(PE_operators)

    # calculate mask to be used for attention later
    batch_size = PE_states.size(0)
    q = global_state #(bs, max_len_g, params.state_dim=embedding dim for seq)
    if self.query_mode == 'PE':
      q = q.view(batch_size, -1, self.d_model)
    k = PE_states.view(batch_size, -1, self.d_model) #(bs, max_len_k, params.state_dim)
    attn = torch.matmul(q , k.transpose(1, 2)) #(bs, max_len_g, max_len_k=num_examples*max_len_PE)
    mask = self.get_pad_mask(attn, 0.0) #(bs, max_len_g, max_len_k=num_examples*max_len_PE)

    if self.include_pos_emb:
      num_examples, max_len_PE = PE_states.size(1), PE_states.size(2)
      if self.query_mode == 'PE':
        global_state = global_state.view(-1, max_len_PE, self.d_model)
        global_state = self.dropout(self.position_enc(global_state))#(bs, max_len_g, params.state_dim)
        global_state = global_state.view(-1, num_examples, max_len_PE, self.d_model)
      if self.query_mode =='global':
        global_state = self.dropout(self.position_enc(global_state))#(bs, max_len_g, params.state_dim)
      PE_states = self.dropout(self.position_enc(PE_states.view(-1, max_len_PE, self.d_model)))#(bs*num_examples, max_len_PE, params.state_dim)
      PE_states = PE_states.view(-1, num_examples, max_len_PE, self.d_model)#(bs, num_examples, max_len_PE, params.state_dim)
      PE_statements = self.dropout(self.position_enc(PE_statements.view(-1, max_len_PE, self.d_model)))#(bs*num_examples, max_len_PE, params.state_dim)
      PE_statements = PE_statements.view(-1, num_examples, max_len_PE, self.d_model)#(bs, num_examples, max_len_PE, params.state_dim)
      PE_operators = self.dropout(self.position_enc(PE_operators.view(-1, max_len_PE, self.d_model)))#(bs*num_examples, max_len_PE, params.state_dim)
      PE_operators = PE_operators.view(-1, num_examples, max_len_PE, self.d_model)#(bs, num_examples, max_len_PE, params.state_dim)

    global_state = self.layer_norm(global_state)
    PE_states = self.layer_norm(PE_states)
    PE_statements = self.layer_norm(PE_statements)
    PE_operators = self.layer_norm(PE_operators)

    #Multi-Head Attention
    statement_pred, operator_pred, att_weights = self.mha(PE_states, global_state, PE_statements, PE_operators,
                  PE_solution_scores, att_type, mask) #(bs, max_len_g, state_dim), (bs, max_len_g, state_dim),
                                    #(bs, num_heads, max_len_g, max_len_PE)
    #Positionwise FeedForward
    if self.include_ff:
      statement_pred = self.statement_ff(statement_pred)#(bs, max_len_g, state_dim)
      operator_pred = self.operator_ff(operator_pred)#(bs, max_len_g, state_dim)

    #Final projection to get logits
    if self.include_dense_proj:
      statement_pred = self.statement_dense_proj(statement_pred)#(bs, max_len_g, num_statements)
      operator_pred = self.operator_dense_proj(operator_pred)#(bs, max_len_g, num_operators)

    if self.return_att_weights:
      return statement_pred, operator_pred, att_weights
    else:
      return statement_pred, operator_pred, None

  def predict(self, PE_states, global_state, PE_statements, PE_solution_scores, att_type):

    #Learn embedding of statements and operators
    if self.include_value_emb:
      PE_statements = self.statement_emb(PE_statements)

    # calculate mask to be used for attention later
    batch_size = PE_states.size(0)
    q = global_state #(bs, max_len_g, params.state_dim=embedding dim for seq)
    if self.query_mode == 'PE':
      q = q.view(batch_size, -1, self.d_model)
    k = PE_states.view(batch_size, -1, self.d_model) #(bs, max_len_k=num_examples*max_len_PE, params.state_dim)
    attn = torch.matmul(q , k.transpose(1, 2)) #(bs, max_len_g, max_len_k=num_examples*max_len_PE)
    mask = self.get_pad_mask(attn, 0.0) #(bs, max_len_g, max_len_k=num_examples*max_len_PE)

    if self.include_pos_emb:
      num_examples, max_len_PE = PE_states.size(1), PE_states.size(2)
      if self.query_mode == 'PE':
        global_state = global_state.view(-1, max_len_PE, self.d_model)
        global_state = self.dropout(self.position_enc(global_state))#(bs, max_len_g, params.state_dim)
        global_state = global_state.view(-1, num_examples, max_len_PE, self.d_model)
      if self.query_mode =='global':
        global_state = self.dropout(self.position_enc(global_state))#(bs, max_len_g, params.state_dim)
      PE_states = self.dropout(self.position_enc(PE_states.view(-1, max_len_PE, self.d_model)))#(bs*num_examples, max_len_PE, params.state_dim)
      PE_states = PE_states.view(-1, num_examples, max_len_PE, self.d_model)#(bs, num_examples, max_len_PE, params.state_dim)
      PE_statements = self.dropout(self.position_enc(PE_statements.view(-1, max_len_PE, self.d_model)))#(bs*num_examples, max_len_PE, params.state_dim)
      PE_statements = PE_statements.view(-1, num_examples, max_len_PE, self.d_model)#(bs, num_examples, max_len_PE, params.state_dim)

    global_state = self.layer_norm(global_state)
    PE_states = self.layer_norm(PE_states)
    PE_statements = self.layer_norm(PE_statements)

    statement_pred, att_weights = self.mha.predict(PE_states, global_state, PE_statements, PE_solution_scores,
                    att_type, mask) #(bs, max_len_g, state_dim), (bs, max_len_g, state_dim), (bs, num_heads, max_len_g, max_len_PE)
    #Positionwise FeedForward
    if self.include_ff:
      statement_pred = self.statement_ff(statement_pred)#(bs, max_len_g, state_dim)

    #Final projection to get logits
    if self.include_dense_proj:
      statement_pred = self.statement_dense_proj(statement_pred)#(bs, max_len_g, num_statements)
      statement_pred = F.softmax(statement_pred, dim=-1).data.squeeze(1)

    if self.return_att_weights:
      return statement_pred, att_weights
    else:
      return statement_pred, None

class AttModel(nn.Module):

  def __init__(self, include_pos_emb=True, include_ff=True, include_res_ln=True,
        dropout=0.0, d_inner=2048, d_model=params.state_dim, return_att_weights=False, n_head=8,
        d_k=64, head_params=None, state_emb_params=None, include_self_attention=False, self_attention_type='val',
        include_state_emb=False):
    super(AttModel, self).__init__()

    self.include_self_attention = include_self_attention
    self.self_attention_type = self_attention_type

    if not self.include_self_attention or (self.include_self_attention and self.self_attention_type == 'key'):
      include_value_emb_ca = True
    else:
      include_value_emb_ca = False

    self.return_att_weights = return_att_weights
    self.d_model = d_model

    if self.include_self_attention:
      if self.self_attention_type =='val' or self.self_attention_type=='both':
        self.sa_val = BasicAggModel(include_value_emb=True, include_pos_emb=include_pos_emb, include_ff=include_ff,
          include_res_ln=include_res_ln, dropout=dropout, d_inner=d_inner, d_model=d_model,
          return_att_weights=return_att_weights, n_head=n_head, d_k=d_k, head_params=head_params,
          state_emb_params=state_emb_params, include_state_emb=include_state_emb, include_dense_proj=False,
          query_mode='PE')

      if self.self_attention_type =='key' or self.self_attention_type=='both':
          self.sa_key = BasicAggModel(include_value_emb=False, include_pos_emb=include_pos_emb, include_ff=include_ff,
          include_res_ln=include_res_ln, dropout=dropout, d_inner=d_inner, d_model=d_model,
          return_att_weights=return_att_weights, n_head=n_head, d_k=d_k, head_params=head_params,
          state_emb_params=state_emb_params, include_state_emb=include_state_emb, include_dense_proj=False,
          query_mode='PE')

    self.ca = BasicAggModel(include_value_emb=include_value_emb_ca, include_pos_emb=include_pos_emb,
        include_ff=include_ff, include_res_ln=include_res_ln, dropout=dropout, d_inner=d_inner,
        d_model=d_model, return_att_weights=return_att_weights, n_head=n_head, d_k=d_k, head_params=head_params,
        state_emb_params=state_emb_params, include_state_emb=include_state_emb, include_dense_proj=True,
        query_mode='global')

  def forward(self, PE_states, global_state, PE_statements, PE_operators, PE_solution_scores, att_type):
    #global_state = (bs, max_len_g, params.state_dim=embedding dim for seq)
    #PE_states = (bs, num_of_solutions, max_len_PE, params.state_dim=embedding dim for seq)
    #PE_statements = (bs, num_of_solutions, max_len_PE)

    if self.include_self_attention:
      if self.self_attention_type == 'val' or self.self_attention_type == 'both':
        PE_statements, PE_operators, sa_val_att_weights = self.sa_val(PE_states, PE_states, PE_statements, PE_operators, \
                              PE_solution_scores, att_type)

      if self.self_attention_type == 'key' or self.self_attention_type == 'both':
        num_examples, max_len_PE = PE_states.size(1), PE_states.size(2)
        PE_states, _, sa_key_att_weights = self.sa_key(PE_states, PE_states, PE_states, PE_states, \
                              PE_solution_scores, att_type)
        PE_states = PE_states.view(-1, num_examples, max_len_PE, self.d_model)

    statement_pred, operator_pred, ca_att_weights = self.ca(PE_states, global_state, PE_statements, PE_operators, \
                            PE_solution_scores, att_type)

    if self.return_att_weights:
      return statement_pred, operator_pred, (sa_val_att_weights, sa_key_att_weights, ca_att_weights)
    else:
      return statement_pred, operator_pred, None

  def predict(self, PE_states, global_state, PE_statements, PE_solution_scores, att_type):

    if self.include_self_attention:
      if self.self_attention_type == 'val' or self.self_attention_type == 'both':
        PE_statements, sa_val_att_weights = self.sa_val.predict(PE_states, PE_states, PE_statements, \
                             PE_solution_scores, att_type)

      if self.self_attention_type == 'key' or self.self_attention_type == 'both':
        num_examples, max_len_PE = PE_states.size(1), PE_states.size(2)

        PE_states, sa_key_att_weights = self.sa_key.predict(PE_states, PE_states, PE_states, \
                              PE_solution_scores, att_type)
        PE_states = PE_states.view(-1, num_examples, max_len_PE, self.d_model)

    statement_pred, ca_att_weights = self.ca.predict(PE_states, global_state, PE_statements, \
                            PE_solution_scores, att_type)

    if self.return_att_weights:
      return statement_pred, ca_att_weights
    else:
      return statement_pred, None

max_stars_repo_name: shrivastavadisha/N-PEPS

max_issues_repo_name: shrivastavadisha/N-PEPS

max_forks_repo_name: shrivastavadisha/N-PEPS


text:
import sim
import numpy as np
import time
import matplotlib.pyplot as plt

import gym
from gym import error, spaces

class UR5Env(gym.Env):

    def __init__(self): # n_actions:3 (target pos), n_states:6 (3pos+3force)
        self.metadata = {'render.modes': ['human']}
        super().__init__()
        sim.simxFinish(-1)
        for _ in range(5):
            self.clientID = sim.simxStart('127.0.0.1',19997,True,True,5000,5)
            if self.clientID != -1:
                # print('[INFO] Connected to CoppeliaSim.')
                break
        if self.clientID == -1:
            raise IOError('[ERROR] Could not connect to CoppeliaSim.')

        sim.simxSynchronous(self.clientID, True)
        # sim.simxStartSimulation(self.clientID, sim.simx_opmode_oneshot)
        sim.simxGetPingTime(self.clientID)
        self.stepCount = 0
        self.reward = 0
        self.n_substeps = 10
        # self.sim_timestep = 0.5      # set in coppeliaSim (not implemented)
        self.n_actions = 3
        self.n_states = 6
        self.action_space = spaces.Box(-1., 1, shape = (self.n_actions,), dtype = 'float32')
        self.observation_space = spaces.Box(-np.inf, np.inf, shape=(self.n_states,), dtype='float32')
        self._getHandles()
        sim.simxGetPingTime(self.clientID)

    def getHandle(self, name, ignoreError = False, attempts = 5):
        for _ in range(attempts):
            res, out_handle = sim.simxGetObjectHandle(self.clientID, name, sim.simx_opmode_blocking)
            if res == sim.simx_return_ok or ignoreError:
                # print('[INFO] {} handle obtained.'.format(name))
                break
            sim.simxGetPingTime(self.clientID)
        # if res!=sim.simx_return_ok and not ignoreError:
            # print('[WARNING] Failed to find {} with error {}.'.format(name, res))
        return out_handle

    def __del__(self):
        self.close()

    def close(self):
        sim.simxStopSimulation(self.clientID, sim.simx_opmode_oneshot)
        sim.simxGetPingTime(self.clientID)
        sim.simxFinish(self.clientID)

    def step(self, action):
        # action = np.clip(action, self.action_space.low, self.action_space.high)
        obs = self._get_obs()
        action = np.clip(action, obs[0:3]-0.1*np.ones(([1,3])), obs[0:3]+0.1*np.ones(([1,3])))[0]
        self._set_action(action)
        self._simulation_step()
        obs = self._get_obs()
        done = self._is_done()
        info = {}
        reward = self._getReward()
        return obs, reward, done, info

    def _set_action(self, action):
        actionX = action[0]
        actionY = action[1]
        actionZ = action[2]
        # print('[DATA] X:{:.4f}, Y:{:.4f}, Z:{:.4f}'.format(actionX,actionY,actionZ))
        sim.simxSetFloatSignal(self.clientID, 'actionX', actionX, sim.simx_opmode_oneshot)
        sim.simxSetFloatSignal(self.clientID, 'actionY', actionY, sim.simx_opmode_oneshot)
        sim.simxSetFloatSignal(self.clientID, 'actionZ', actionZ, sim.simx_opmode_oneshot)
        sim.simxGetPingTime(self.clientID)

    def _simulation_step(self):
        #for _ in range(self.n_substeps):
        sim.simxSynchronousTrigger(self.clientID)
        sim.simxGetPingTime(self.clientID)
        # while self._getTargetPos(False) is not self._getFinalPos(False): # keep triggering until movement is done
        #     sim.simxSynchronousTrigger(self.clientID)
        # sim.simxGetPingTime(self.clientID)

    def _get_obs(self):
        X, Y, Z = self.getKinectXYZ(False)
        # X, Y, Z = self._getTargetPosFromKinect(False) # changed observation with exact target pos
        forces = self.getForce(False)
        return [X, Y, Z, forces[0][2], forces[1][2], forces[2][2]]

    def _is_done(self):
        pathIsDone = sim.simxGetFloatSignal(self.clientID,'movePathDone',sim.simx_opmode_blocking)[1]
        sim.simxGetPingTime(self.clientID)
        return True if pathIsDone==1 else False

    def reset(self):
        sim.simxSetFloatSignal(self.clientID, 'renderEnv', 0, sim.simx_opmode_oneshot)
        sim.simxGetPingTime(self.clientID)
        self.resetSim()
        self.prepareSim()
        obs = self._get_obs()
        return obs

    def render(self, mode='human'):
        sim.simxSetFloatSignal(self.clientID, 'renderEnv', 1, sim.simx_opmode_oneshot)
        sim.simxGetPingTime(self.clientID)

    def _getHandles(self):
        '''
        print('getting tip handle...')
        self.tipHandle = self.getHandle('UR5_link7_visible')
        print('tip handle obtained successfully.')
        '''
        self.jointHandles = []
        # print('[INFO] getting joint handles...')
        for i in range(1,7):
            self.jointHandles.append(self.getHandle('UR5_joint{}'.format(i)))
        # print('[INFO] joint handles obtained successfully.')
        # print('[INFO] getting dummy handles...')
        self.tipDummyHandle     = self.getHandle('Tip')
        self.targetDummyHandle  = self.getHandle('TargetDummy')
        self.testDummyHandle    = self.getHandle('TestDummy')
        # print('[INFO] dummy handles obtained successfully.')
        self.targetObjectHandle = self.getHandle('TargetObject')
        # print('[INFO] target object handle obtained successfully.')
        # print('[INFO] getting force sensor handles...')
        self.kinectHandle = self.getHandle('kinect_rgb')
        self.fsHandles = []
        for i in range(1,4):
            self.fsHandles.append(self.getHandle('JacoHand_forceSens2_finger{}'.format(i)))
        # print('[INFO] force sensor handles obtained successfully.')

    def _getTipPos(self, initialize = True):
        if initialize:
            ret, pos = sim.simxGetObjectPosition(self.clientID, self.tipDummyHandle, -1, sim.simx_opmode_streaming)
        else:
            ret, pos = sim.simxGetObjectPosition(self.clientID, self.tipDummyHandle, -1, sim.simx_opmode_buffer)
        if ret == sim.simx_return_ok:
            return pos
        else:
            # print('[WARNING] problem in getting tip position.')
            return -1

    def _getTargetPos(self, initialize = True):
        if initialize:
            ret, pos = sim.simxGetObjectPosition(self.clientID, self.targetObjectHandle, -1, sim.simx_opmode_streaming)
        else:
            ret, pos = sim.simxGetObjectPosition(self.clientID, self.targetObjectHandle, -1, sim.simx_opmode_buffer)
        if ret == sim.simx_return_ok:
            return pos
        else:
            # print('[WARNING] problem in getting target position.')
            return -1

    def _getTargetPosFromKinect(self, initialize = True):
        if initialize:
            ret, pos = sim.simxGetObjectPosition(self.clientID, self.targetObjectHandle, self.kinectHandle, sim.simx_opmode_streaming)
        else:
            ret, pos = sim.simxGetObjectPosition(self.clientID, self.targetObjectHandle, self.kinectHandle, sim.simx_opmode_buffer)
        if ret == sim.simx_return_ok:
            return pos
        else:
            # print('[WARNING] problem in getting target position.')
            return -1

    def _getFinalPos(self, initialize = True):
        if initialize:
            ret, pos = sim.simxGetObjectPosition(self.clientID, self.testDummyHandle, -1, sim.simx_opmode_streaming)
        else:
            ret, pos = sim.simxGetObjectPosition(self.clientID, self.testDummyHandle, -1, sim.simx_opmode_buffer)
        if ret == sim.simx_return_ok:
            return pos
        else:
            # print('[WARNING] problem in getting tip position.')
            return -1

    def getKinectXYZ(self, initialize = True):
        if initialize:
            X = sim.simxGetFloatSignal(self.clientID,'actX',sim.simx_opmode_streaming)[1]
            Y = sim.simxGetFloatSignal(self.clientID,'actY',sim.simx_opmode_streaming)[1]
            Z = sim.simxGetFloatSignal(self.clientID,'actZ',sim.simx_opmode_streaming)[1]
        else:
            X = sim.simxGetFloatSignal(self.clientID,'actX',sim.simx_opmode_buffer)[1]
            Y = sim.simxGetFloatSignal(self.clientID,'actY',sim.simx_opmode_buffer)[1]
            Z = sim.simxGetFloatSignal(self.clientID,'actZ',sim.simx_opmode_buffer)[1]
        sim.simxGetPingTime(self.clientID)
        return X,Y,Z

    def getForce(self, initialize = True):
        if initialize:
            F1 = sim.simxReadForceSensor(self.clientID,self.fsHandles[0],sim.simx_opmode_streaming)[2]
            F2 = sim.simxReadForceSensor(self.clientID,self.fsHandles[1],sim.simx_opmode_streaming)[2]
            F3 = sim.simxReadForceSensor(self.clientID,self.fsHandles[2],sim.simx_opmode_streaming)[2]
        else:
            F1 = sim.simxReadForceSensor(self.clientID,self.fsHandles[0],sim.simx_opmode_buffer)[2]
            F2 = sim.simxReadForceSensor(self.clientID,self.fsHandles[1],sim.simx_opmode_buffer)[2]
            F3 = sim.simxReadForceSensor(self.clientID,self.fsHandles[2],sim.simx_opmode_buffer)[2]
        sim.simxGetPingTime(self.clientID)
        return [F1,F2,F3]

    def getForceMagnitude(self, initialize = True):
        if initialize:
            F1 = sim.simxReadForceSensor(self.clientID,self.fsHandles[0],sim.simx_opmode_streaming)[2]
            F2 = sim.simxReadForceSensor(self.clientID,self.fsHandles[1],sim.simx_opmode_streaming)[2]
            F3 = sim.simxReadForceSensor(self.clientID,self.fsHandles[2],sim.simx_opmode_streaming)[2]
        else:
            F1 = sim.simxReadForceSensor(self.clientID,self.fsHandles[0],sim.simx_opmode_buffer)[2]
            F2 = sim.simxReadForceSensor(self.clientID,self.fsHandles[1],sim.simx_opmode_buffer)[2]
            F3 = sim.simxReadForceSensor(self.clientID,self.fsHandles[2],sim.simx_opmode_buffer)[2]
        sim.simxGetPingTime(self.clientID)
        return [np.linalg.norm(F1),np.linalg.norm(F2),np.linalg.norm(F3)]

    def initializeFunctions(self):
        self.getKinectXYZ(True)
        self.getForce(True)
        self.getForceMagnitude(True)
        self._getTipPos(True)
        self._getTargetPos(True)
        self._getTargetPosFromKinect(True)
        self._getFinalPos(True)

    def _getReward(self):
        tipPos    = self._getTipPos(False)
        targetPos = self._getTargetPos(False)
        dist      = np.linalg.norm(np.array(tipPos)-np.array(targetPos))
        self.stepCount += 1
        # self.reward = self.reward + (1/self.stepCount)*((-dist*dist)-self.reward)
        self.reward = self.reward - (dist*dist)
        return self.reward

    def prepareSim(self):
        self.initializeFunctions()
        isTracking = sim.simxGetFloatSignal(self.clientID,'isTracking',sim.simx_opmode_streaming)[1]
        while isTracking != 1:
            sim.simxSynchronousTrigger(self.clientID)
            isTracking = sim.simxGetFloatSignal(self.clientID,'isTracking',sim.simx_opmode_buffer)[1]
            sim.simxGetPingTime(self.clientID)
            X,Y,Z = self.getKinectXYZ(True)
            actionX = X
            actionY = Y
            actionZ = Z
            # print(actionX,actionY,actionZ)
            # print('[DATA] X:{:.4f}, Y:{:.4f}, Z:{:.4f}'.format(actionX,actionY,actionZ))
            sim.simxSetFloatSignal(self.clientID, 'actionX', actionX, sim.simx_opmode_oneshot)
            sim.simxSetFloatSignal(self.clientID, 'actionY', actionY, sim.simx_opmode_oneshot)
            sim.simxSetFloatSignal(self.clientID, 'actionZ', actionZ, sim.simx_opmode_oneshot)
            sim.simxGetPingTime(self.clientID)
        # print('[INFO] simulation is ready for tracking.')

    def resetSim(self):
        # print('[INFO] reseting sim...')
        ret = sim.simxStopSimulation(self.clientID,sim.simx_opmode_blocking)
        if ret == sim.simx_return_ok:
            # print('[INFO] sim reset successfully.')
            time.sleep(1)
        else:
            raise IOError('[ERROR] problem in reseting sim...')
        sim.simxGetPingTime(self.clientID)
        # print('[INFO] starting sim in synchronous mode...')
        sim.simxSynchronous(self.clientID, True)
        sim.simxGetPingTime(self.clientID)
        ret = sim.simxStartSimulation(self.clientID,sim.simx_opmode_oneshot)
        # if ret == sim.simx_return_ok:
            # print('[INFO] sim started successfully.')
        sim.simxGetPingTime(self.clientID)
        sim.simxClearFloatSignal(self.clientID, 'actionX', sim.simx_opmode_blocking)
        sim.simxClearFloatSignal(self.clientID, 'actionY', sim.simx_opmode_blocking)
        sim.simxClearFloatSignal(self.clientID, 'actionZ', sim.simx_opmode_blocking)
        sim.simxGetPingTime(self.clientID)
        self.stepCount = 0
        self.reward = 0

    def doTracking(self):
        self.resetSim()
        self.prepareSim()

        pathIsDone = sim.simxGetFloatSignal(self.clientID,'movePathDone',sim.simx_opmode_streaming)[1]
        posX = []
        posY = []
        posZ = []
        tactile1 = []
        reward = []

        while pathIsDone == 0:
            X,Y,Z = self.getKinectXYZ(False)
            # forces = self.getForce(False)
            # diffForce = (forces[0][0] + ((forces[1][0] + forces[2][0]) / 2))/2
            # print('[DATA] diff force: {:.4f}'.format(diffForce))
            actionX = X # + 0.005*diffForce
            actionY = Y
            actionZ = Z
            # print('[DATA] X:{:.4f}, Y:{:.4f}, Z:{:.4f}'.format(actionX,actionY,actionZ))
            sim.simxSetFloatSignal(self.clientID, 'actionX', actionX, sim.simx_opmode_oneshot)
            sim.simxSetFloatSignal(self.clientID, 'actionY', actionY, sim.simx_opmode_oneshot)
            sim.simxSetFloatSignal(self.clientID, 'actionZ', actionZ, sim.simx_opmode_oneshot)
            sim.simxGetPingTime(self.clientID)
            sim.simxSynchronousTrigger(self.clientID)
            forces = self.getForceMagnitude(False)
            posX.append(X)
            posY.append(Y)
            posZ.append(Z)
            tactile1.append(forces[0])
            reward.append(self._getReward())
            sim.simxGetPingTime(self.clientID)
            pathIsDone = sim.simxGetFloatSignal(self.clientID,'movePathDone',sim.simx_opmode_buffer)[1]
            # time.sleep(0.5)
        # print('[INFO] tracking is done.')
        # print('[DATA] accumulated reward: {}'.format(np.sum(np.array(reward))))
        sim.simxStopSimulation(self.clientID,sim.simx_opmode_blocking)


text:
import math

import numpy as np
from bresenham import bresenham

from algorithm.potential_field.PotentialField import PotentialField
from data import obstacles
from data.Map import Map
from data.plotting import plot_path

class PotentialFieldWithRegression(PotentialField):
    def __init__(self, env, start, goal, k_att, k_rep, rr, max_iter, coefficient, dis_obs_goal, dis_goal_current):
        super().__init__(env, start, goal, k_att, k_rep, rr, max_iter)

        self.coefficient = coefficient
        self.dis_goal_current = dis_goal_current
        self.dis_obs_goal = dis_obs_goal
        self.FLAG = 'Potential Field with Regression '

    def get_attractive_force_rs(self, start, goal):
        """
        改进之后引力大小的计算
        :return: 引力对应矢量
        """
        dis_goal = math.hypot(start[0] - goal[0], start[1] - goal[1])
        force = self.k_att * np.subtract(goal, start)
        if dis_goal <= self.coefficient:
            return force
        else:
            return self.coefficient / dis_goal * force

    def get_repulsion_force_rs(self, goal):
        force = np.zeros(2)
        for obs in self.env.obs:
            dis_obs = math.hypot(self.current_pos[0] - obs[0], self.current_pos[1] - obs[1])
            dis_goal = math.hypot(self.current_pos[0] - goal[0], self.current_pos[1] - goal[1])
            if dis_obs <= self.rr:
                rep_1 = np.subtract(self.current_pos, np.asarray(obs)) * self.k_rep \
                        * (1.0 / dis_obs - 1.0 / self.rr) / (dis_obs ** 2) * (dis_goal ** 2)
                rep_2 = np.subtract(goal, np.asarray(obs)) * self.k_rep * (
                            (1.0 / dis_obs - 1.0 / self.rr) ** 2) * dis_goal
                force = np.add(rep_1, rep_2, force)

        return force

    def get_total_force_rs(self, goal):
        """
        改进计算合力方式
        :return:
        """
        min_dis = float('inf')
        min_obs = 0
        for obs in self.env.obs:
            if math.hypot(self.current_pos[0] - obs[0], self.current_pos[1] - obs[1]) < min_dis:
                min_obs = obs
        dis_qc = math.hypot(min_obs[0] - goal[0], min_obs[1] - goal[1])
        dis_gr = math.hypot(self.current_pos[0] - goal[0], self.current_pos[1] - goal[1])
        # if dis_qc <= self.dis_obs_goal and dis_gr <= self.dis_goal_current:
        #     return self.get_attractive_force_rs(self.current_pos, goal)
        # else:
        return np.add(self.get_attractive_force_rs(self.current_pos, goal), self.get_repulsion_force_rs(goal))

    def check_obs_pos(self, goal):
        """
        查找垂于与当前点与goal连线的线上的障碍物
        :param goal:
        :return:
        """
        # obs_list[0] y值大于等于当前点y值， obs_list[1] y值小于等于当前点y值
        obs_list = [(float('inf'), float('inf')), (float('-inf'), float('-inf'))]
        length = 2
        if self.current_pos[1] - goal[1] == 0:
            x = self.current_pos[0]
            for y in range(self.env.y_range):
                node = (round(x), y)
                if node in self.env.obs:
                    if self.current_pos[1] < y < obs_list[0][1]:
                        obs_list[0] = node
                    if obs_list[1][1] < y < self.current_pos[1]:
                        obs_list[1] = node
        else:
            k = - (self.current_pos[0] - goal[0]) / (self.current_pos[1] - goal[1])
            b = self.current_pos[1] - k * self.current_pos[0]
            bias = abs(k / 2)
            # 找到路径上障碍物
            for x in range(self.env.x_range):
                nodes = set()
                nodes.add((x, round(k * x + b - bias)))
                nodes.add((x, round(k * x + b + bias)))
                nodes.add((x, math.ceil(k * x + b)))
                nodes.add((x, math.floor(k * x + b)))
                for node in nodes:
                    if node in self.env.obs:
                        if k >= 0:
                            if self.current_pos[0] < node[0] < obs_list[0][0]:
                                obs_list[0] = node
                            if obs_list[1][0] < node[0] < self.current_pos[0]:
                                obs_list[1] = node
                        else:
                            if self.current_pos[1] < node[1] < obs_list[0][1]:
                                obs_list[0] = node
                            if obs_list[1][1] < node[1] < self.current_pos[1]:
                                obs_list[1] = node
        if obs_list[0] == (float('inf'), float('inf')):
            length -= 1
        if obs_list[1] == (float('-inf'), float('-inf')):
            length -= 1
        return length

    def dfs_obs(self, obs_pos):
        stack = [obs_pos]
        seen = [obs_pos]
        directions = {obs_pos: [obs_pos, obs_pos]}
        results = []

        while stack:
            v_node = stack.pop()
            is_colon = True
            for motion in Map.MOTIONS["manhattan"]:
                s_next = tuple([v_node[i] + motion[i] for i in range(2)])
                if s_next in self.env.obs and s_next not in seen:
                    stack.append(s_next)
                    seen.append(s_next)
                    directions[s_next] = [motion, v_node]
                    is_colon = False
            if is_colon:  # 如果找到了一个尽头
                cur_node = v_node
                cur_motion = directions[cur_node][0]
                pre_node = directions[cur_node][1]
                pre_motion = directions[pre_node][0]
                line = []
                node = (cur_node[0] + cur_motion[0], cur_node[1] + cur_motion[1])
                if node not in self.env.obs and (0 < node[0] < self.env.x_range) and (0 < node[1] < self.env.y_range):
                    line.append(node)
                while pre_node is not obs_pos:
                    if cur_motion != pre_motion:
                        node = (pre_node[0] + pre_motion[0], pre_node[1] + pre_motion[1])
                        if node not in self.env.obs and (0 < node[0] < self.env.x_range) \
                                and (0 < node[1] < self.env.y_range):
                            line.append(node)
                    cur_node = pre_node
                    cur_motion = directions[cur_node][0]
                    pre_node = directions[cur_node][1]
                    pre_motion = directions[pre_node][0]
                if line:
                    results.append(line)

        return results

    def get_point_dis_line(self, point, goal):
        vec1 = np.subtract(goal, np.asarray(point))
        vec2 = np.subtract(self.current_pos, np.asarray(point))
        distance = np.abs(np.cross(vec1, vec2) / np.linalg.norm(np.subtract(goal, self.current_pos)))
        return distance

    def deal_minima_problem(self, obs_pos, goal):
        """
        处理局部最小值问题
        :obs_pos: 陷入障碍物的坐标
        :return: 返回虚拟目标点序列
        """
        result = []
        size = self.check_obs_pos(goal)
        paths = self.dfs_obs(obs_pos)
        if size == 0:
            dis = []
            for path in paths:
                dis.append(self.get_point_dis_line(path[0], goal))
            idx = dis.index(min(dis))
            result.append(paths[idx][0])
        elif size == 1:
            dis = []
            for path in paths:
                dis_path = 0
                for idx, node in enumerate(path):
                    if idx == 0:
                        dis_path += math.hypot(self.current_pos[0] - node[0], self.current_pos[1] - node[1])
                    if idx == len(path) - 1:
                        dis_path += math.hypot(goal[0] - node[0], goal[1] - node[1])
                    if idx < len(path) - 1:
                        dis_path += math.hypot(node[0] - path[idx + 1][0], node[1] - path[idx + 1][1])
                dis.append(dis_path)
            idx = dis.index(min(dis))
            result.extend(paths[idx])
        elif size == 2:
            dis = []
            for path in paths:
                dis.append(self.get_point_dis_line(path[0], goal))
            idx = dis.index(min(dis))
            result.extend(paths[idx])
        return result

    def has_obs_in_path(self, start, goal):
        result = False
        nodes = list(bresenham(int(start[0]), int(start[1]), int(goal[0]), int(goal[1])))
        for node in nodes:
            if node in self.env.obs:
                result = True
        return result, nodes

    def optimize_path_by_regression(self):
        """
        基于回归的方法进行路径优化
        :return:
        """
        cur_end = 1
        cur_start = cur_end - 1
        path = [self.path[0]]
        pre_sub_path = []
        while cur_start < len(self.path):
            start = self.path[cur_start]
            end = self.path[cur_end]
            has_obs, sub_path = self.has_obs_in_path(start, end)
            if has_obs:
                path.extend(pre_sub_path[1:])
                cur_start = cur_end - 1
            else:
                if cur_end == len(self.path) - 1:
                    path.extend(sub_path[1:])
                    cur_start = len(self.path)
                else:
                    pre_sub_path = sub_path
                    cur_end += 1
        return path

    def plan_path_rs(self, goal):
        self.is_success = True
        count = 0
        while not np.array_equal(self.current_pos, goal):
            if count > self.max_iter:
                self.is_success = False
                break
            count += 1
            force = self.get_total_force_rs(goal)
            pos = self.get_next_pos(self.current_pos, force)
            # 触发局部最小值处理
            if (pos[0], pos[1]) in self.env.obs and (pos[0], pos[1]) not in self.path:
                targets = self.deal_minima_problem((pos[0], pos[1]), goal)
                for target in targets:
                    self.plan_path_rs(np.asarray(target))
            else:
                self.current_pos = pos
                self.path.append((self.current_pos[0], self.current_pos[1]))

    def plan_path(self):
        """
        改进之后的运行流程
        :return:
        """
        self.plan_path_rs(self.goal)
        # plot_path(self.path, self.start, self.goal, 'b')
        self.path = self.optimize_path_by_regression()

def main():
    # basic
    # rough rr 1 coefficient 10/15
    # rug rr 3 coefficient 30
    start = (1, 1)
    goal = (20, 20)
    k_att = 1.0
    k_rep = 0.8
    rr = 3
    max_iter = 1000
    # 主函数
    heuristic_type = "euclidean"
    env = Map(21, 21, heuristic_type=heuristic_type)
    obs, free = obstacles.get_rug_obs(env.x_range, env.y_range)
    env.update_obs(obs, free)
    # improved
    coefficient = 30
    dis_goal_obs = 4
    dis_goal_current = 6
    demo = PotentialFieldWithRegression(env, start, goal, k_att, k_rep, rr, max_iter,
                                        coefficient, dis_goal_obs, dis_goal_current)
    demo.run()

if __name__ == "__main__":
    main()

max_stars_repo_name: G-H-Li/PathNavigation

max_issues_repo_name: G-H-Li/PathNavigation

max_forks_repo_name: G-H-Li/PathNavigation


text:
from unittest import TestCase

from dlmPython import dlm_uni, lego, param, dlm_mod
import numpy as np
from scipy.linalg import block_diag

class Test_dlm_uni(TestCase):

    ### tests for concatenating delta and dim ###
    def test_join(self):
        np.testing.assert_equal(lego.join(1,1), np.array([1,1]))
        np.testing.assert_equal(lego.join(1.,1), np.array([1,1]))
        np.testing.assert_equal(lego.join(1.,1.), np.array([1,1]))

        np.testing.assert_equal(
            lego.join(lego.E(3), lego.E(3)),
                      np.array([1, 0, 0, 1, 0, 0]))

        np.testing.assert_equal(
            lego.join(lego.E(3), 1),
                      np.array([1, 0, 0, 1]))

        np.testing.assert_equal(
            lego.join(1, lego.E(3)),
                      np.array([1, 1, 0, 0]))

    def test_mod_poly(self):
        dlm1 = dlm_mod.poly(1,V=3)
        dlm2 = dlm_mod.poly(2, V=3, discount=1)
        dlm3 = dlm_uni(lego.E(4), lego.J(4)*2, V=2)
        dlm = dlm1 + dlm2 + dlm3

        # Test get_block
        np.testing.assert_equal(
                dlm.__get_block__(dlm.G, 0),
                lego.J(2))
        np.testing.assert_equal(
                dlm.__get_block__(dlm.G, 2),
                2*lego.J(4))

    def test_dlm_add(self):
        dlm1 = dlm_mod.poly(2, discount=1)
        dlm2 = dlm_mod.arma(ar=[3,2,4], ma=[1,2,3], tau2=3)
        dlm3 = dlm_uni(lego.E(2), lego.J(2))
        dlm = dlm1 + dlm2 + dlm3
        print dlm

        ## Test F
        #np.testing.assert_equal(
        #        dlm.F,
        #        np.array([1,0,0,1,0,0,0,1,0]))

        ## Test G
        #np.testing.assert_equal(
        #        dlm3.G,
        #        block_diag(lego.J(3), lego.J(4)))

        ## Test dim
        #np.testing.assert_equal(
        #        dlm3.dim,
        #        np.array([3,3]))

        ## Test delta
        #np.testing.assert_equal(
        #        dlm3.delta,
        #        np.array([1,1]))

        ##print dlm3

max_stars_repo_name: luiarthur/dlmPython

max_issues_repo_name: luiarthur/dlmPython

max_forks_repo_name: luiarthur/dlmPython

reason: import numpy,from scipy


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Nov  2 16:09:39 2020

@author: grat05
"""

import numpy as np
import pymc3 as pm
import theano.tensor as tt
from scipy import stats
from pymc3.distributions.dist_math import bound

class MyMvNorm(pm.Continuous):
    def __init__(self, mean, chol_cov, *args, **kwargs):

        self.mean = mean
        self.chol_cov = chol_cov
        self.solve_lower = tt.slinalg.Solve(A_structure="lower_triangular")

        super().__init__(*args, **kwargs)

    def logp(self, value):
        delta = value - self.mean

        chol_cov = self.chol_cov
        diag = tt.nlinalg.diag(chol_cov)
        # Check if the covariance matrix is positive definite.
        ok = tt.all(diag > 0)
        # If not, replace the diagonal. We return -inf later, but
        # need to prevent solve_lower from throwing an exception.
        chol_cov = tt.switch(ok, chol_cov, 1)

        delta_trans = self.solve_lower(chol_cov, delta.T).T
        quaddist = (delta_trans ** 2).sum(axis=-1)
        logdet = tt.sum(tt.log(diag))

        k = pm.floatX(value.shape[-1])
        norm = -0.5 * k * pm.floatX(np.log(2 * np.pi))
        return bound(norm - 0.5 * quaddist - logdet, ok)

    # def logp(self, value):
    #     mean = np.atleast_2d(self.mean)
    #     value = np.atleast_2d(value)
    #     logp = 0
    #     for val, mu in zip(value, mean):
    #         logp += stats.multivariate_normal.logpdf(val, mean=mu, cov=self.cov)
    #     return logp

    def random(self, point=None, size=None):
        mean_1d = len(self.mean.shape) == 1
        mean = np.atleast_2d(self.mean)
        cov = np.dot(self.chol_cov, self.chol_cov.T)
        rng = np.random.default_rng()
        def draw_func(mean):
            return rng.multivariate_normal(mean, cov, size=size)
        draws = np.apply_along_axis(draw_func, 1, mean)
        return np.squeeze(draws) if mean_1d else draws

reason: import numpy,from scipy,import theano,import pymc3,from pymc3


text:
"""
To construct data iterator from imdb, batch sampling procedure are defined here
training minibatch =
    {'data': [num_images, c, h, w],
    'rois': [num_rois, 5],
    'labels': [num_rois],
    'bbox_targets': [num_rois, 4 * num_classes],
    'bbox_inside_weights': [num_rois, 4 * num_classes],
    'bbox_outside_weights': [num_rois, 4 * num_classes]}
    num_images should divide config['TRAIN_BATCH_SIZE'] and num_rois = config['TRAIN_BATCH_SIZE'] / num_images
validation minibatch is similar except num_images = 1 and num_rois = all rois
testing minibatch =
    {'data': [num_images, c, h, w],
    'rois': [num_rois, 5]}
    num_images = 1 and num_rois = all rois
"""

import cv2
import numpy as np
import numpy.random as npr

from helper.processing import image_processing
from helper.processing.bbox_regression import expand_bbox_regression_targets
from rcnn.config import config

def get_minibatch(roidb, num_classes):
    """
    return minibatch of images in roidb
    :param roidb: subset of main database
    :param num_classes: number of classes is used in bbox regression targets
    :return: minibatch: {'data', 'rois', 'labels', 'bbox_targets', 'bbox_inside_weights', 'bbox_outside_weights'}
    """
    num_images = len(roidb)
    random_scale_indexes = npr.randint(0, high=len(config.TRAIN.SCALES), size=num_images)
    assert config.TRAIN.BATCH_SIZE % num_images == 0, \
        'num_images {} must devide BATCHSIZE {}'.format(num_images, config.TRAIN.BATCH_SIZE)
    rois_per_image = config.TRAIN.BATCH_SIZE / num_images
    fg_rois_per_image = np.round(config.TRAIN.FG_FRACTION * rois_per_image).astype(int)

    # im_array: [num_images, c, h, w]
    im_array, im_scales = get_image_array(roidb, config.TRAIN.SCALES, random_scale_indexes)

    rois_array = list()
    labels_array = list()
    bbox_targets_array = list()
    bbox_inside_array = list()

    for im_i in range(num_images):
        im_rois, labels, bbox_targets, bbox_inside_weights, overlaps = \
            sample_rois(roidb[im_i], fg_rois_per_image, rois_per_image, num_classes)

        # project im_rois
        # do not round roi
        rois = im_rois * im_scales[im_i]
        batch_index = im_i * np.ones((rois.shape[0], 1))
        rois_array_this_image = np.hstack((batch_index, rois))
        rois_array.append(rois_array_this_image)

        # add labels
        labels_array.append(labels)
        bbox_targets_array.append(bbox_targets)
        bbox_inside_array.append(bbox_inside_weights)

    rois_array = np.vstack(rois_array)
    labels_array = np.hstack(labels_array)
    bbox_targets_array = np.vstack(bbox_targets_array)
    bbox_inside_array = np.vstack(bbox_inside_array)
    bbox_outside_array = np.array(bbox_inside_array > 0).astype(np.float32)

    minibatch = {'data': im_array,
                 'rois': rois_array,
                 'labels': labels_array,
                 'bbox_targets': bbox_targets_array,
                 'bbox_inside_weights': bbox_inside_array,
                 'bbox_outside_weights': bbox_outside_array}
    return minibatch

def get_testbatch(roidb, num_classes):
    """
    return test batch of given roidb
    actually, there is only one testing scale and len(roidb) is 1
    :param roidb: subset of main database
    :param num_classes: number of classes is used in bbox regression targets
    :return: minibatch: {'data', 'rois'}
    """
    num_images = len(roidb)
    random_scale_indexes = npr.randint(0, high=len(config.TEST.SCALES), size=num_images)
    im_array, im_scales = get_image_array(roidb, config.TEST.SCALES, random_scale_indexes)

    rois_array = list()
    for im_i in range(num_images):
        im_rois = roidb[im_i]['boxes']
        rois = im_rois * im_scales[im_i]
        batch_index = im_i * np.ones((rois.shape[0], 1))
        rois_array_this_image = np.hstack((batch_index, rois))
        rois_array.append(rois_array_this_image)

    rois_array = np.vstack(rois_array)

    testbatch = {'data': im_array,
                 'rois': rois_array}
    return testbatch

def get_image_array(roidb, scales, scale_indexes):
    """
    build image array from specific roidb
    :param roidb: images to be processed
    :param scales: scale list
    :param scale_indexes: indexes
    :return: array [b, c, h, w], list of scales
    """
    num_images = len(roidb)
    processed_ims = []
    im_scales = []
    for i in range(num_images):
        im = cv2.imread(roidb[i]['image'])
        if roidb[i]['flipped']:
            im = im[:, ::-1, :]
        target_size = scales[scale_indexes[i]]
        im, im_scale = image_processing.resize(im, target_size, config.TRAIN.MAX_SIZE)
        im_tensor = image_processing.transform(im, config.PIXEL_MEANS)
        processed_ims.append(im_tensor)
        im_scales.append(im_scale)
    array = image_processing.tensor_vstack(processed_ims)
    return array, im_scales

def sample_rois(roidb, fg_rois_per_image, rois_per_image, num_classes):
    """
    generate random sample of ROIs comprising foreground and background examples
    :param roidb: database of selected rois
    :param fg_rois_per_image: foreground roi number
    :param rois_per_image: total roi number
    :param num_classes: number of classes
    :return: (labels, rois, bbox_targets, bbox_inside_weights, overlaps)
    """
    # label = class RoI has max overlap with
    labels = roidb['max_classes']
    overlaps = roidb['max_overlaps']
    rois = roidb['boxes']

    # foreground RoI with FG_THRESH overlap
    fg_indexes = np.where(overlaps >= config.TRAIN.FG_THRESH)[0]
    # guard against the case when an image has fewer than fg_rois_per_image foreground RoIs
    fg_rois_per_this_image = np.minimum(fg_rois_per_image, fg_indexes.size)
    # Sample foreground regions without replacement
    if fg_indexes.size > 0:
        fg_indexes = npr.choice(fg_indexes, size=fg_rois_per_this_image, replace=False)

    # Select background RoIs as those within [BG_THRESH_LO, BG_THRESH_HI)
    bg_indexes = np.where((overlaps < config.TRAIN.BG_THRESH_HI) & (overlaps >= config.TRAIN.BG_THRESH_LO))[0]
    # Compute number of background RoIs to take from this image (guarding against there being fewer than desired)
    bg_rois_per_this_image = rois_per_image - fg_rois_per_this_image
    bg_rois_per_this_image = np.minimum(bg_rois_per_this_image, bg_indexes.size)
    # Sample foreground regions without replacement
    if bg_indexes.size > 0:
        bg_indexes = npr.choice(bg_indexes, size=bg_rois_per_this_image, replace=False)

    # indexes selected
    keep_indexes = np.append(fg_indexes, bg_indexes)

    # pad more to ensure a fixed minibatch size
    if keep_indexes.shape[0] < rois_per_image:
        gap = rois_per_image - keep_indexes.shape[0]
        gap_indexes = npr.choice(range(len(rois)), size=gap, replace=False)
        keep_indexes = np.append(keep_indexes, gap_indexes)

    # select labels
    labels = labels[keep_indexes]
    # set labels of bg_rois to be 0
    labels[fg_rois_per_this_image:] = 0
    overlaps = overlaps[keep_indexes]
    rois = rois[keep_indexes]

    bbox_targets, bbox_inside_weights = \
        expand_bbox_regression_targets(roidb['bbox_targets'][keep_indexes, :], num_classes)

    return rois, labels, bbox_targets, bbox_inside_weights, overlaps


text:
import jax.numpy as jnp
from jax.ops import index_add
from jax.random import split, permutation
from jax.nn import one_hot

import pandas as pd

import requests
import io

from environment import BanditEnvironment

def get_ads16(key, ntrain, intercept):
    url = "https://raw.githubusercontent.com/probml/probml-data/main/data/ads16_preprocessed.csv"
    download = requests.get(url).content

    dataset = pd.read_csv(io.StringIO(download.decode('utf-8')))
    dataset.drop(columns=['Unnamed: 0'], inplace=True)
    dataset = dataset.sample(frac=1).reset_index(drop=True).to_numpy()

    ntrain = ntrain if ntrain > 0 and ntrain < len(dataset) else len(dataset)
    nusers, nads = 120, 300
    users = jnp.arange(nusers)

    n_ads_per_user, rem = divmod(ntrain, nusers)

    mykey, key = split(key)
    indices = permutation(mykey, users)[:rem]
    n_ads_per_user = jnp.ones((nusers,)) * int(n_ads_per_user)
    n_ads_per_user = index_add(n_ads_per_user, indices, 1).astype(jnp.int32)

    indices = jnp.array([])

    for user, nrow in enumerate(n_ads_per_user):
        mykey, key = split(key)
        df_indices = jnp.arange(user * nads, (user + 1) * nads)
        indices = jnp.append(indices, permutation(mykey, df_indices)[:nrow]).astype(jnp.int32)

    narms = 2
    dataset = dataset[indices]

    X = dataset[:, :-1]
    Y = one_hot(dataset[:, -1], narms)

    if intercept:
        X = jnp.concatenate([jnp.ones_like(X[:, :1]), X])

    opt_rewards = jnp.ones((len(X),))

    return X, Y, opt_rewards

def ADS16Environment(key, ntrain, intercept=False):
    mykey, key = split(key)
    X, Y, opt_rewards = get_ads16(mykey, ntrain, intercept)
    return BanditEnvironment(key, X, Y, opt_rewards)

max_stars_repo_name: probml/bandits

max_issues_repo_name: probml/bandits

max_forks_repo_name: probml/bandits

reason: import jax,from jax


text:
# tutorial_utils.py utility functions for the tutorial
# Author:Itzik Ben Sabat sitzikbs[at]gmail.com
# If you use this code,see LICENSE.txt file and cite our work

import numpy as np
import torch
import scipy.interpolate
import scipy.spatial as spatial

class SinglePointCloudDataset():
    def __init__(self, point_filename, points_per_patch):
        self.points_per_patch = points_per_patch
        self.points = np.loadtxt(point_filename).astype('float32')
        self.bbdiag = float(np.linalg.norm(self.points.max(0) - self.points.min(0), 2))
        self.points = (self.points - self.points.mean(0)) / (0.5*self.bbdiag)  # shrink shape to unit sphere
        self.kdtree = spatial.cKDTree(self.points, 10)
        self.pc_plot = None

    def __getitem__(self, index):

        point_distances, patch_point_inds = self.kdtree.query(self.points[index, :], k=self.points_per_patch)
        rad = max(point_distances)
        patch_points = torch.from_numpy(self.points[patch_point_inds, :])

        # center the points around the query point and scale patch to unit sphere
        patch_points = patch_points - torch.from_numpy(self.points[index, :])
        patch_points = patch_points / rad

        patch_points, trans = self.pca_points(patch_points)
        return torch.transpose(patch_points, 0, 1), trans, rad

    def __len__(self):
        return self.points.shape[0]

    def pca_points(self, patch_points):
        '''

        Args:
            patch_points: xyz points

        Returns:
            patch_points: xyz points after aligning using pca
        '''
        # compute pca of points in the patch:
        # center the patch around the mean:
        pts_mean = patch_points.mean(0)
        patch_points = patch_points - pts_mean

        trans, _, _ = torch.svd(torch.t(patch_points))
        patch_points = torch.mm(patch_points, trans)

        cp_new = -pts_mean  # since the patch was originally centered, the original cp was at (0,0,0)
        cp_new = torch.matmul(cp_new, trans)

        # re-center on original center point
        patch_points = patch_points - cp_new
        return patch_points, trans

class SyntheticPointCloudDataset():
    def __init__(self, n_points, jet_order, points_per_patch=128):
        self.points_per_patch = points_per_patch
        self.beta = self.generate_random_beta(jet_order)
        self.points = self.generate_synthetic_example(n_points, jet_order, self.beta)
        self.gt_normals = self.get_gt_normals(jet_order, self.beta, self.points)
        self.bbdiag = float(np.linalg.norm(self.points.max(0) - self.points.min(0), 2))
        self.points = (self.points - self.points.mean(0)) / (0.5*self.bbdiag)  #
        self.kdtree = spatial.cKDTree(self.points, 10)

    def __getitem__(self, index):
        point_distances, patch_point_inds = self.kdtree.query(self.points[index, :], k=self.points_per_patch)
        rad = max(point_distances)
        patch_points = torch.tensor(self.points[patch_point_inds, :], dtype=torch.float32)

        # center the points around the query point and scale patch to unit sphere
        patch_points = patch_points - torch.tensor(self.points[index, :], dtype=torch.float32)
        patch_points = patch_points / rad

        # patch_points, trans = self.pca_points(patch_points) # the data is generated aligned to z
        return torch.transpose(patch_points, 0, 1), rad

    def __len__(self):
        return self.points.shape[0]

    def generate_random_beta(self, jet_order):
        """
        generate a random set of jet coefficients
        Args:
            jet_order:

        Returns:

        """
        n_coefficients = int((jet_order +1) * (jet_order + 2) / 2)
        beta = np.expand_dims(np.random.uniform(-1, 1, n_coefficients), -1)
        return beta

    def generate_synthetic_example(self, n_points, jet_order, beta):
        """
        Generate sample points for a random n-jet of order jet_order
        Args:
            n_points: number of output points
            jet_order: het order

        Returns:
            points: xyz coordinates
        """

        x = np.expand_dims(np.random.uniform(-1.0, 1.0, size=n_points), -1)
        y = np.expand_dims(np.random.uniform(-1.0, 1.0, size=n_points), -1)
        M = self.get_vandermonde(x, y, jet_order)
        z = np.dot(M, beta)
        points = np.concatenate([x, y, z], axis=1)
        return points

    def get_gt_normals(self, jet_order, beta, points):
        """
        computer the surface ground truth normals given the surface coefficients
        Args:
            beta: surface coefficients

        Returns:
            normals: surface normals at every point
        """
        x = np.expand_dims(points[:, 0], -1)
        y = np.expand_dims(points[:, 1], -1)

        if jet_order == 1:
            normals = np.concatenate([-beta[0] * np.ones_like(x), -beta[1] * np.ones_like(x), np.ones_like(x)])
        elif jet_order == 2:
            normals = np.concatenate([-(beta[0] + 2 * beta[2] * x + beta[4] * y),
                           -(beta[1] + 2 * beta[3] * y + beta[4] * x),
                           np.ones_like(x)], axis=1)
        elif jet_order == 3:
            y_2 = y * y
            x_2 = x * x
            xy = x * y
            normals = np.concatenate([-(beta[0] + 2 * beta[2] * x + beta[4] * y + 3 * beta[5] * x_2 +
                             2 *beta[7] * xy + beta[8] * y_2),
                           -(beta[1] + 2 * beta[3] * y + beta[ 4] * x + 3 * beta[6] * y_2 +
                             beta[7] * x_2 + 2 * beta[ 8] * xy),
                           np.ones_like(x)], axis=1)
        elif jet_order == 4:
            # [x, y, x_2, y_2, xy, x_3, y_3, x_2 * y, y_2 * x, x_3 * x, y_3 * y, x_3 * y, y_3 * x, y_2 * x_2
            y_2 = y * y
            x_2 = x * x
            x_3 = x_2 * x
            y_3 = y_2 * y
            xy = x * y
            normals = np.concatenate([-(beta[0] + 2 * beta[2] * x + beta[4] * y + 3 * beta[5] * x_2 +
                             2 * beta[7] * xy + beta[8] * y_2 + 4 * beta[9] * x_3 + 3 * beta[11] * x_2 * y
                             + beta[12] * y_3 + 2 * beta[13] * y_2 * x),
                           -(beta[1] + 2 * beta[3] * y + beta[4] * x + 3 * beta[6] * y_2 +
                             beta[7] * x_2 + 2 * beta[8] * xy + 4 * beta[10] * y_3 + beta[11] * x_3 +
                             3 * beta[12] * x * y_2 + 2 * beta[13] * y * x_2),
                           np.ones_like(x)], axis=1)

        normals = normals / np.linalg.norm(normals, ord=2, axis=1, keepdims=True)
        return normals

    def get_vandermonde(self, x, y, jet_order):
        """
        Generate Vandermonde matrix
        Args:
            x: x coordinate vector
            y: y coordinate vector
            jet_order: jet order

        Returns:
            M: The Vandermonde matrix
        """

        if jet_order == 1:
            M = np.concatenate([x, y, np.ones_like(x)], axis=1)
        elif jet_order == 2:
            M = np.concatenate([x, y, x * x, y * y, x * y, np.ones_like(x)], axis=1)
        elif jet_order == 3:
            y_2 = y * y
            x_2 = x * x
            xy = x * y
            M = np.concatenate([x, y, x_2, y_2, xy, x_2 * x, y_2 * y, x_2 * y, y_2 * x, np.ones_like(x)], axis=1)
        elif jet_order == 4:
            y_2 = y * y
            x_2 = x * x
            x_3 = x_2 * x
            y_3 = y_2 * y
            xy = x * y
            M = np.concatenate(
                [x, y, x_2, y_2, xy, x_3, y_3, x_2 * y, y_2 * x, x_3 * x, y_3 * y, x_3 * y, y_3 * x, y_2 * x_2,
                 np.ones_like(x)], axis=1)
        else:
            raise ValueError("Unsupported jet order")

        return M

def compute_principal_curvatures(beta):
    """
    given the jet coefficients, compute the principal curvatures and principal directions:
    the eigenvalues and eigenvectors of the weingarten matrix
    :param beta: batch of Jet coefficients vector
    :return: k1, k2, dir1, dir2: batch of principal curvatures and principal directions
    """
    with torch.no_grad():
        if beta.shape[1] < 5:
            raise ValueError("Can't compute curvatures for jet with order less than 2")
        else:
            b1_2 = torch.pow(beta[:, 0], 2)
            b2_2 = torch.pow(beta[:, 1], 2)
            #first fundemental form
            E = (1 + b1_2).view(-1, 1, 1)
            G = (1 + b2_2).view(-1, 1, 1)
            F = (beta[:, 1] * beta[:, 0]).view(-1, 1, 1)
            I = torch.cat([torch.cat([E, F], dim=2), torch.cat([F, G], dim=2)], dim=1)
            # second fundemental form
            norm_N0 = torch.sqrt(b1_2 + b2_2 + 1)
            e = (2*beta[:, 2] / norm_N0).view(-1, 1, 1)
            f = (beta[:, 4] / norm_N0).view(-1, 1, 1)
            g = (2*beta[:, 3] / norm_N0).view(-1, 1, 1)
            II = torch.cat([torch.cat([e, f], dim=2), torch.cat([f, g], dim=2)], dim=1)

            M_weingarten = -torch.bmm(torch.inverse(I), II)

            curvatures, dirs = torch.symeig(M_weingarten, eigenvectors=True)
            dirs = torch.cat([dirs, torch.zeros(dirs.shape[0], 2, 1, device=dirs.device)], dim=2) # pad zero in the normal direction

    return curvatures, dirs

def normal2rgb(normals):
    '''
    map normal vectors to RGB cube
    Args:
        normals : normal vectors

    Returns:
        mapped normals to rgb
    '''
    r = np.clip(np.expand_dims((127.5 + 127.5 * normals[:, 0]) / 255, -1), 0, 1)
    g = np.clip(np.expand_dims((127.5 + 127.5 * normals[:, 1]) / 255, -1), 0, 1)
    b = np.clip(np.expand_dims((127.5 + 127.5 * normals[:, 2]) / 255, -1), 0, 1)
    return np.concatenate([r, g, b], 1)

def curvatures2rgb(curvatures,  k1_range=[-1, 1], k2_range=[-1, 1]):
    '''
    map principal curvature to RGB values
    Args:
        curvatures : principal vectors
        k1_range : range of maximal principal curvatures
        k2_range : range of minimal principal curvatures
    Returns:
        mapped normals to rgb
    '''
    k1min = k1_range[0]
    k1max = k1_range[1]
    k2min = k2_range[0]
    k2max = k2_range[1]
    mapped_curvatures = curvatures
    mapped_curvatures[mapped_curvatures[:, 0] > k1max, 0] = k1max
    mapped_curvatures[mapped_curvatures[:, 0] < k1min, 0] = k1min
    mapped_curvatures[mapped_curvatures[:, 1] > k2max, 1] = k2max
    mapped_curvatures[mapped_curvatures[:, 1] < k2min, 1] = k2min

    red_dist = np.array([[0, 0.5, 0],  [0.5, 1, 1], [0, 1, 1]])
    green_dist = np.array([[0,  1, 1], [1, 1, 1],   [1, 1, 0]])
    blue_dist = np.array([[1, 1, 0],   [1, 1, 0.5], [0, 0.5, 0]])

    Xq, Yq = mapped_curvatures[:, 0],  mapped_curvatures[:, 1]

    kx, ky = 1, 1 #interpulation params

    red_mapper = scipy.interpolate.RectBivariateSpline(np.array([k1min, 0, k1max]), np.array([k2min, 0, k2max]),
                                                       red_dist, kx=kx, ky=ky)
    green_mapper = scipy.interpolate.RectBivariateSpline(np.array([k1min, 0, k1max]), np.array([k2min, 0, k2max]),
                                                       green_dist, kx=kx, ky=ky)
    blue_mapper = scipy.interpolate.RectBivariateSpline(np.array([k1min, 0, k1max]), np.array([k2min, 0, k2max]),
                                                       blue_dist, kx=kx, ky=ky)

    r = np.around(red_mapper.ev(Xq, Yq), 4)
    g = np.around(green_mapper.ev(Xq, Yq), 4)
    b = np.around(blue_mapper.ev(Xq, Yq), 4)

    return np.concatenate([np.expand_dims(r, -1), np.expand_dims(g, -1), np.expand_dims(b, -1)], 1)

if __name__ == "__main__":
    dataset = SyntheticPointCloudDataset(n_points=128, jet_order=1, points_per_patch=128)

max_stars_repo_name: sitzikbs/DeepF

max_issues_repo_name: sitzikbs/DeepF

reason: import numpy,import scipy


text:
# !/usr/bin/python27
# coding: utf8

# ****************for bc stim con to sac
import hocmidp
import warnings
import numpy as np
from countcon import segname
warnings.filterwarnings("ignore")
lst1 = []
lst2 = []
lst3 = []
seg1 = hocmidp.hocmidp('sac1.hoc')
for node in seg1:
    if node[0] > 70 and node[0] < 75:
        lst1.append(seg1.index(node))
    elif node[0] < 70:
        lst2.append(seg1.index(node))
    elif node[0] < 170:
        lst3.append(seg1.index(node))

print segname(172)
# for i in lst1:
#     print "\tSAC[1].%s syn = new Exp2Syn(0.5)" % segname(i)
#     print "\tsyn.e = 0"
#     print "\tsoma netcon = new NetCon (&v(.5), syn)"
#     print "\tnetcon.weight = 23e-5"
#     print "\tbslist.append(netcon)"

# for i in lst2:
#     print "\tSAC[1].%s syn = new Exp2Syn(0.5)" % segname(i)
#     print "\tsyn.e = 0"
#     print "\tsoma1 netcon = new NetCon (&v(.5), syn)"
#     print "\tnetcon.weight = 23e-5"
#     print "\tbslist1.append(netcon)"
#
# for i in lst3:
#     print "\tSAC[1].%s syn = new Exp2Syn(0.5)" % segname(i)
#     print "\tsyn.e = 0"
#     print "\tsoma2 netcon = new NetCon (&v(.5), syn)"
#     print "\tnetcon.weight = 23e-5"
#     print "\tbslist2.append(netcon)"

# origin = np.array([20, 0, 0])
# num = 0
# lst1 = []
# lst2 = []
# for node in seg1:
#     i = seg1.index(node)
#     node = np.array(node)
#     if np.sqrt(np.sum((node - origin)**2)) < 50 :
#         lst1.append(segname(i))
#     elif np.sqrt(np.sum((node - origin)**2)) < 100 :
#         lst2.append(segname(i))
    # print node - origin
    # print (node - origin)**2
    # print np.sum((node - origin)**2)
    # print np.sqrt(np.sum((node - origin)**2))

# print len(lst1), len(lst2)
    #     print segname(i)
    #     print "\tSAC[1].%s syn = new Exp2Syn(0.5)" % segname(i)
    #     print "\tsyn.e = 0"
    #     print "\tsoma netcon = new NetCon (&v(.5), syn)"
    #     print "\tbslist.append(netcon)"
# ****************************************************

max_stars_repo_name: GodfatherPacino/SAC-network

max_issues_repo_name: GodfatherPacino/SAC-network

max_forks_repo_name: GodfatherPacino/SAC-network


text:
import time,os
import torch
import shutil
import argparse
import torch.optim as optim
import torch.nn.init as init
import torch.utils.data as data
import torch.backends.cudnn as cudnn
from layers.functions import PriorBox
from layers.modules import MultiBoxLoss
from data import mk_anchors
from data import COCODetection, VOCDetection, detection_collate, preproc
from configs.CC import Config
from termcolor import cprint
from utils.nms_wrapper import nms
import numpy as np

def set_logger(status):
    if status:
        from logger import Logger
        date = time.strftime("%m_%d_%H_%M") + '_log'
        log_path = './logs/'+ date
        if os.path.exists(log_path):
            shutil.rmtree(log_path)
        os.makedirs(log_path)
        logger = Logger(log_path)
        return logger
    else:
        pass

def anchors(cfg):
    return mk_anchors(cfg.model.input_size,
                               cfg.model.input_size,
                               cfg.model.anchor_config.size_pattern,
                               cfg.model.anchor_config.step_pattern)

def init_net(net, cfg, resume_net):
    if cfg.model.init_net and not resume_net:
        net.init_model(cfg.model.pretrained)
    else:
        print('Loading resume network...')
        state_dict = torch.load(resume_net)

        from collections import OrderedDict
        new_state_dict = OrderedDict()
        for k, v in state_dict.items():
            head = k[:7]
            if head == 'module.':
                name = k[7:]
            else:
                name = k
            new_state_dict[name] = v
        net.load_state_dict(new_state_dict,strict=False)

def set_optimizer(net, cfg):
    return optim.SGD(net.parameters(),
                     lr = cfg.train_cfg.lr[0],
                     momentum = cfg.optimizer.momentum,
                     weight_decay = cfg.optimizer.weight_decay)

def set_criterion(cfg):
    return MultiBoxLoss(cfg.model.m2det_config.num_classes,
                        overlap_thresh = cfg.loss.overlap_thresh,
                        prior_for_matching = cfg.loss.prior_for_matching,
                        bkg_label = cfg.loss.bkg_label,
                        neg_mining = cfg.loss.neg_mining,
                        neg_pos = cfg.loss.neg_pos,
                        neg_overlap = cfg.loss.neg_overlap,
                        encode_target = cfg.loss.encode_target)

def adjust_learning_rate(optimizer, gamma, epoch, step_index, iteration, epoch_size, cfg):
    global lr
    if epoch <= 5:
        lr = cfg.train_cfg.end_lr + (cfg.train_cfg.lr[0]-cfg.train_cfg.end_lr)\
         * iteration / (epoch_size * cfg.train_cfg.warmup)
    else:
        for i in range(len(cfg.train_cfg.step_lr.COCO)):
            if cfg.train_cfg.step_lr.COCO[i]>=epoch:
                lr = cfg.train_cfg.lr[i]
                break
        # lr = cfg.train_cfg.init_lr * (gamma ** (step_index))
    for param_group in optimizer.param_groups:
        param_group['lr'] = lr
    return lr

def get_dataloader(cfg, dataset, setname='train_sets'):
    _preproc = preproc(cfg.model.input_size, cfg.model.rgb_means, cfg.model.p)
    Dataloader_function = {'VOC': VOCDetection, 'COCO':COCODetection}
    _Dataloader_function = Dataloader_function[dataset]
    if setname == 'train_sets':
        dataset = _Dataloader_function(cfg.COCOroot if dataset == 'COCO' else cfg.VOCroot,
                                   getattr(cfg.dataset, dataset)[setname], _preproc)
    else:
        dataset = _Dataloader_function(cfg.COCOroot if dataset == 'COCO' else cfg.VOCroot,
                                   getattr(cfg.dataset, dataset)[setname], None)
    return dataset

def print_train_log(iteration, print_epochs, info_list):
    if iteration % print_epochs == 0:
        cprint('Time:{}||Epoch:{}||EpochIter:{}/{}||Iter:{}||Loss_L:{:.4f}||Loss_C:{:.4f}||Batch_Time:{:.4f}||LR:{:.7f}'.format(*info_list), 'green')

def print_info(info, _type=None):
    if _type is not None:
        if isinstance(info,str):
            cprint(info, _type[0], attrs=[_type[1]])
        elif isinstance(info,list):
            for i in range(info):
                cprint(i, _type[0], attrs=[_type[1]])
    else:
        print(info)

def save_checkpoint(net, cfg, final=True, datasetname='COCO',epoch=10):
    if final:
        torch.save(net.state_dict(), cfg.model.weights_save + \
                'Final_M2Det_{}_size{}_net{}.pth'.format(datasetname, cfg.model.input_size, cfg.model.m2det_config.backbone))
    else:
        torch.save(net.state_dict(), cfg.model.weights_save + \
                'M2Det_{}_size{}_net{}_epoch{}.pth'.format(datasetname, cfg.model.input_size, cfg.model.m2det_config.backbone,epoch))

def write_logger(info_dict,logger,iteration,status):
    if status:
        for tag,value in info_dict.items():
            logger.scalar_summary(tag, value, iteration)
    else:
        pass

def image_forward(img, net, cuda, priors, detector, transform):
    w,h = img.shape[1],img.shape[0]
    scale = torch.Tensor([w,h,w,h])
    with torch.no_grad():
        x = transform(img).unsqueeze(0)
        if cuda:
            x = x.cuda()
            scale = scale.cuda()
    out = net(x)
    boxes, scores = detector.forward(out, priors)
    boxes = (boxes[0] * scale).cpu().numpy()
    scores = scores[0].cpu().numpy()
    return boxes, scores

def nms_process(num_classes, i, scores, boxes, cfg, min_thresh, all_boxes, max_per_image):
    for j in range(1, num_classes): # ignore the bg(category_id=0)
        inds = np.where(scores[:,j] > min_thresh)[0]
        if len(inds) == 0:
            all_boxes[j][i] = np.empty([0,5], dtype=np.float32)
            continue
        c_bboxes = boxes[inds]
        c_scores = scores[inds, j]
        c_dets = np.hstack((c_bboxes, c_scores[:, np.newaxis])).astype(np.float32, copy=False)

        soft_nms = cfg.test_cfg.soft_nms
        keep = nms(c_dets, cfg.test_cfg.iou, force_cpu=soft_nms)
        keep = keep[:cfg.test_cfg.keep_per_class] # keep only the highest boxes
        c_dets = c_dets[keep, :]
        all_boxes[j][i] = c_dets
    if max_per_image > 0:
        image_scores = np.hstack([all_boxes[j][i][:, -1] for j in range(1, num_classes)])
        if len(image_scores) > max_per_image:
            image_thresh = np.sort(image_scores)[-max_per_image]
            for j in range(1, num_classes):
                keep = np.where(all_boxes[j][i][:, -1] >= image_thresh)[0]
                all_boxes[j][i] = all_boxes[j][i][keep, :]


text:
#!/usr/bin/env python
import matplotlib
matplotlib.rcParams['legend.fancybox'] = True
import matplotlib.pyplot as plt
import numpy as np

#data
x = [1, 2, 3]
cached = [1.257, 1.211, 1.175]
no_cached = [15.125, 15.115, 15.094]
adaptui = [1.899, 1.544, 1.213]

#error data
cached_err = [0.248, 0.180, 0.240]
no_cached_err = [0.445, 0.492, 0.501]

ind = np.arange(3)  # the x locations for the groups
width = 0.25       # the width of the bars

fig, ax = plt.subplots()
#plot data
rects1 = ax.bar(ind, cached, width, color='r', yerr=cached_err)
rects2 = ax.bar(ind+width, no_cached, width, color='y', yerr=no_cached_err)
rects3 = ax.bar(ind+width+width, adaptui, width, color='g')

#configure  X axes
plt.xlim(0,3)
plt.xticks(ind+width)

#configure  Y axes
plt.ylim(0.0, 20.0)
plt.yticks([0.0, 5.0, 10.0, 15.0, 20.0])

#labels
plt.ylabel('Time in seconds' + '\n')

#title
plt.title('Comparing Imhotep in mobile devices' + '\n')

ax.set_xticklabels( ('Galaxy SIII Mini', 'Galaxy S3', 'Nexus 10') )
ax.legend( (rects1[0], rects2[0], rects3[0]), ('Imhotep with cache', 'Imhotep without cache', 'AdaptUI') )

def autolabel(rects):
    # attach some text labels
    for rect in rects:
        height = rect.get_height()
        ax.text(rect.get_x()+rect.get_width()/2., 1.05*height, '%d'%int(height),
                ha='center', va='bottom')

autolabel(rects1)
autolabel(rects2)
autolabel(rects3)

for t, a in zip(x, cached):
    plt.plot([t], [a], 'b',)
    plt.annotate(round(a, 3), xy=(t, a), xytext=(t + 0.01, a + 0.01), color='black')

for t, a in zip(x, no_cached):
    plt.plot([t], [a], 'b',)
    plt.annotate(round(a, 3), xy=(t, a), xytext=(t + 0.01, a + 0.01), color='black')

for t, a in zip(x, adaptui):
    plt.plot([t], [a], 'b',)
    plt.annotate(round(a, 3), xy=(t, a), xytext=(t + 0.01, a + 0.01), color='black')

#save plot
plt.savefig('imhotep_comparison.pdf')

plt.show()

max_stars_repo_name: edlectrico/dissertation

max_issues_repo_name: edlectrico/dissertation

max_forks_repo_name: edlectrico/dissertation