text:
import numpy as np
from tqdm import tqdm
import torch
from torch.nn.functional import conv2d
import time
import torch.nn as nn

def transform_filter(dir_filter):
    dir_filter = dir_filter.squeeze(0).squeeze(0)
    FColLength, FRowLength = dir_filter.shape
    new_filters = torch.zeros(2 * FColLength - 1, 2 * FRowLength - 1).float().cuda()
    index_mapping = torch.zeros(FColLength, FRowLength, 2).int().cuda()
    for i in range(FRowLength):
        index_mapping[:, i, 0] = torch.arange(new_filters.shape[0] - 1 - i, new_filters.shape[0] - 1 - i - FColLength, -1).int()
        index_mapping[:, i, 1] = torch.arange(FRowLength - 1 - i, FRowLength - 1 - i + FColLength).int()

    new_filters[index_mapping.reshape(-1, 2)[:, 0].long(), index_mapping.reshape(-1, 2)[:, 1].long()] = dir_filter.reshape(-1, )
    return new_filters.unsqueeze(0).unsqueeze(0)

def zconv2(signal, dir_filter, mmatrix, gpu_mode=False):

    SColLength, SRowLength = signal.shape[-2:]
    FColLength, FRowLength = dir_filter.shape[-2:]

    FArray = dir_filter
    SArray = signal
    M = np.asarray(mmatrix)

    M0 = int(M.flatten('F')[0])
    M1 = int(M.flatten('F')[1])
    M2 = int(M.flatten('F')[2])
    M3 = int(M.flatten('F')[3])

    NewFRowLength = int((M.flatten('F')[0] - 1) * (FRowLength - 1)) + int((M.flatten('F')[2]) * (FColLength - 1)) + FRowLength - 1
    NewFColLength = int((M.flatten('F')[3] - 1) * (FColLength - 1)) + int((M.flatten('F')[1]) * (FRowLength - 1)) + FColLength - 1

    outArray = np.zeros((SColLength, SRowLength))

    sum = 0
    Start1 = int(NewFRowLength / 2)
    Start2 = int(NewFColLength / 2)
    mn1 = Start1 % SRowLength
    mn2 = mn2save = Start2 % SColLength

    # index = np.zeros((FColLength * FRowLength * SColLength * SRowLength, 2)).astype(np.int)

    # filter_size = FRowLength * FColLength

    if gpu_mode is not True:
        for n1 in tqdm(range(SRowLength), desc='n1'):
            for n2 in range(SColLength):
                outindexx = mn1
                outindexy = mn2
                # print('Sshape:{}, Fshape:{}'.format(SArray.shape, FArray.shape))

                for l1 in range(FRowLength):
                    indexx = outindexx
                    indexy = outindexy
                    for l2 in range(FColLength):
                        sum += SArray[indexy, indexx] * FArray[l2, l1]
                        # index[(n1 * SColLength + n2) * filter_size + (l1 * FColLength + l2), 0] = indexy
                        # index[(n1 * SColLength + n2) * filter_size + (l1 * FColLength + l2), 1] = indexx
                        # sum = 0
                        # print('S:[{}, {}], F[{}, {}]'.format(indexy, indexx, l2, l1))
                        indexx -= M2
                        if indexx < 0:
                            indexx += SRowLength
                        if indexx > SRowLength - 1:
                            indexx -= SRowLength
                        indexy -= M3
                        if indexy < 0:
                            indexy += SColLength
                    # print('-'*20)
                    outindexx -= M0
                    if outindexx < 0:
                        outindexx += SRowLength
                    outindexy -= M1
                    if outindexy < 0:
                        outindexy += SColLength
                    if outindexy > SColLength-1:
                        outindexy -= SColLength
                outArray[n2, n1] = sum
                # print('*' * 20)
                sum = 0
                mn2 += 1
                if mn2 > SColLength - 1:
                    mn2 -= SColLength
            mn2 = mn2save
            mn1 += 1
            if mn1 > SRowLength - 1:
                mn1 -= SRowLength

    else:
        dir_filter = transform_filter(dir_filter)

        pad = nn.ReflectionPad2d(padding=(FColLength - 1, FColLength - 1, FColLength - 1, FColLength - 1))
        signal = pad(signal)
        outArray = torch.nn.functional.conv2d(signal, dir_filter)
    return outArray

max_stars_repo_name: yilinshao/CoT-Contourlet-Transformer

max_issues_repo_name: yilinshao/CoT-Contourlet-Transformer

max_forks_repo_name: yilinshao/CoT-Contourlet-Transformer


text:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
from scipy.stats import gaussian_kde
import torch
import torch.nn.functional as f
from collections import defaultdict
from sklearn import metrics
import networkx as nx
import community as community_louvain
import matplotlib.cm as cm
import random
import matplotlib.colors as colors

####################
## Synthetic data ##
####################

def random_points():
    '''
    Randomly drawing points from the circumference
    '''
    #center
    center_x = 0
    center_y = 0
    #Radius
    r = 10
    angle = (np.random.random())*2*np.pi #np.cos and sin assumes radians not degrees
    #angle = random.random()*2*np.pi
    return np.array([center_x + np.cos(angle) * r, center_y + np.sin(angle)* r])

def synthetic_data(k, dim, alpha, nsamples):
    '''
    Randomly uniformly sample of data within a predefined polytope ((k-1)-simplex) using the Dirichlet distribution
        k: number of archetypes
        dim: The dimensions of the final latent space
        alpha: parameter in dirichlet distribution
        nsamples: number of samples
    '''

    alpha = [alpha for i in range(k)]
    A = np.zeros((dim, k))
    #A =  np.array([[12., 13.,  9.],
    #    [18.,  6., 12.],
    #    [14.,  7., 16.]])
    for i in range(k):
        A[:,i] = random_points() #np.random.randint(20, size=dim).reshape(dim,)

    Z = np.zeros((k, nsamples))
    for i in range(nsamples):
        Z[:,i] = np.random.dirichlet(alpha)

    A = torch.from_numpy(A).float() #Should this sum to one?
    #A = f.softmax(A, dim=1)
    Z = torch.from_numpy(Z).float()
    return np.matmul(A, Z).T, A, Z

def logit2prob(logit):
    '''
    utils function //
    Convert logit to probability
    '''
    odds = torch.exp(logit)
    probs = (odds) / (1+odds)
    return probs

def convert(a):
    '''
    Utils function //
    Convert adjacency matrix to edgelist
    '''
    edge_list = np.zeros((2, int(sum(sum(a)))))
    for i in range(a.shape[0]):
        for j in range(a.shape[1]):
                       if a[i][j]== 1:
                           edge_list[0, i] = i
                           edge_list[1, j] = j
    edge_list = edge_list[edge_list[0, :].argsort(), :]
    return edge_list

def generate_network_bias(A, Z, k, d, nsamples, rand = False):
    ''' Generate adj matrix, Undirected case & without dimensionality reduction
            Z: samples drawn from dirichlet distribution
            A: Archetypes
            rand: Random effects
    '''
    #A = torch.from_numpy(A).float() #Should this sum to one?
    #A = f.softmax(A, dim=1)
    #Z = torch.from_numpy(Z).float() #Already sums to one
    if rand:
        #Trying to replicate natural sparcity
        r1=-5
        r2=5
        a = 1
        b = nsamples
        beta = torch.FloatTensor(a, b).uniform_(r1, r2).reshape(-1)
    else:
        beta = torch.ones(nsamples)
        #dim_matrix = torch.rand(d, k)
        #dim_matrix = torch.diag(torch.tensor([40,40,40])).float()

    beta_matrix = beta.unsqueeze(1) + beta
    beta_matrix = beta_matrix/2
    #M = torch.matmul(dim_matrix, torch.matmul(A, Z)).T # (N x K)
    M = torch.matmul(A, Z).T
    z_dist = ((M.unsqueeze(1) - M + 1e-06)**2).sum(-1)**0.5 # (N x N)
    theta = beta_matrix - z_dist #beta_matrix - z_dist # (N x N) - log_odds
    #theta = 1 - z_dist
    probs = logit2prob(theta)
    adj_m = torch.bernoulli(probs) # bernoullig distribution to get links
    #Making adjacency matrix symmetric using upper triangle
    triu = torch.triu(adj_m)
    adj_m = triu + triu.T - torch.diag(torch.diagonal(adj_m))
    adj_m = adj_m - torch.diag(torch.diagonal(adj_m))
    return adj_m, beta

def ideal_prediction(adj_m, G, A, Z, beta, test_size = 0.3, seed_split = False):
        '''
        A: Arcetypes
        Z: sampled datapoints
    '''
        if seed_split != False:
            np.random.seed(seed_split)
            torch.manual_seed(seed_split)
        N = adj_m.shape[0]
        num_samples = round(test_size * 0.5* (N * (N - 1)))
        idx_i_test = torch.multinomial(input=torch.arange(0, float(N)), num_samples=num_samples,
                                replacement=True)
        #Only sample upper corner
        G = G.copy()
        idx_j_test = torch.zeros(num_samples).long()
        for i in range(len(idx_i_test)):
            idx_j_test[i] = torch.arange(idx_i_test[i].item(), float(N))[
                torch.multinomial(input=torch.arange(idx_i_test[i].item(), float(N)), num_samples=1,
                                  replacement=True).item()].item()  # Temp solution to sample from upper corner
            target_nodes = G.neighbors(int(idx_i_test[i]))
            if int(idx_j_test[i]) in target_nodes:  # Loop through neighbors (super fast instead of self.edge_list)
                G.remove_edge(int(idx_i_test[i]), int(idx_j_test[i]))
                if nx.number_connected_components(G) == 1:
                    continue
                else:
                    G.add_edge(int(idx_i_test[i]),
                               int(idx_j_test[i]))  # skip the draw if the link splits network into two components
                    continue

        adj_m = adj_m.clone().detach()

        value_test = adj_m[idx_i_test, idx_j_test].numpy()

        M_i = torch.matmul(A, Z[:, idx_i_test]).T
        M_j = torch.matmul(A, Z[:, idx_j_test]).T
        z_pdist_test = ((M_i - M_j + 1e-06) ** 2).sum(-1) ** 0.5
        theta = beta[idx_i_test] + beta[idx_j_test] - z_pdist_test
        #rate = torch.exp(theta)
        prob = logit2prob(theta)
        fpr, tpr, threshold = metrics.roc_curve(value_test, prob.cpu().data.numpy())
        auc_score = metrics.roc_auc_score(value_test, prob.cpu().data.numpy())
        return auc_score, fpr, tpr

def get_clusters(adj_m):
    G = nx.from_numpy_matrix(adj_m.numpy())
    partition = community_louvain.best_partition(G)
    return partition

def get_sparsity(adj_m):
    return 0.5 * (sum(sum(adj_m)/(adj_m.shape[0]*(adj_m.shape[0]-1))))

def truncate_colormap(cmap, minval=0.0, maxval=1.0, n=100):
    '''
    This function takes part of a matplotlib colormap and uses it. (we didnt like too white values)
    thanks to: https://stackoverflow.com/questions/18926031/how-to-extract-a-subset-of-a-colormap-as-a-new-colormap-in-matplotlib
    '''
    new_cmap = colors.LinearSegmentedColormap.from_list(
        'trunc({n},{a:.2f},{b:.2f})'.format(n=cmap.name, a=minval, b=maxval),
        cmap(np.linspace(minval, maxval, n)))
    return new_cmap

def main(alpha, k, dim, nsamples, rand):
    synth_data, A, Z = synthetic_data(k, dim, alpha, nsamples)
    adj_m, beta = generate_network_bias(A, Z, k, dim, nsamples, rand)

    #Removing disconnected components
    G = nx.from_numpy_matrix(adj_m.numpy())

    if nx.number_connected_components(G) > 1:
        Gcc = sorted(nx.connected_components(G), key=len, reverse=True)
        G = G.subgraph(Gcc[0])
        delete_Z = []
        for i in range(len(Gcc)):
            if Gcc[i] == Gcc[0]:
                continue
            else:
                for j in range(len(Gcc[i])):
                   delete_Z.append(list(Gcc[i])[j])
        mask = torch.ones((Z.shape[0],Z.shape[1]), dtype=torch.bool)
        mask[:, delete_Z] = False
        Z = Z[mask].reshape(k, mask.shape[1]-len(delete_Z))

        mask_adj = torch.ones((adj_m.shape[0],adj_m.shape[1]), dtype=torch.bool)
        mask_adj[:, delete_Z] = False
        mask_adj[delete_Z,:] = False
        adj_m = adj_m[mask_adj].reshape(adj_m.shape[0] - len(delete_Z),adj_m.shape[1] - len(delete_Z))

        synth_data = torch.matmul(A, Z).T

    #label_map = {x: i for i, x in enumerate(G.nodes)}
    #G = nx.relabel_nodes(G, label_map)
    #Louvain partition
    partition = get_clusters(adj_m)

    #Calculating density
    xy = np.vstack((synth_data[:,0].numpy(), synth_data[:,1].numpy()))
    z = gaussian_kde(xy)(xy)
    mpl.rcParams['font.family'] = 'Times New Roman'
    cmap = plt.get_cmap('RdPu')
    cmap = truncate_colormap(cmap, 0.2, 1)
    if dim == 3:
        fig = plt.figure(dpi=100)
        ax = fig.add_subplot(projection='3d')
        sc = ax.scatter(synth_data[:, 0], synth_data[:, 1], synth_data[:, 2], c=z, cmap="viridis")
        ax.scatter(A[0, :], A[1, :], A[2, :], marker='^', c='black', label="Archetypes")
        ax.set_title(f"True Latent Space (alpha={alpha})")
        fig.colorbar(sc, label="Density")
    else:

        fig, ax = plt.subplots(dpi=100)
        sc = ax.scatter(synth_data[:, 0], synth_data[:, 1], c=z, cmap=cmap)

        #ax.scatter(synth_data[:, 0], synth_data[:, 1], c=list(partition.values()), cmap='Set2')
        ax.scatter(A[0, :], A[1, :], marker='^', c='black', label="Archetypes")
        #ax.set_title(f"True Latent Space (alpha={alpha})")
        fig.colorbar(sc, label="Density")
    ax.legend()

    plt.savefig(f'true_latent_space_test.png',dpi=100)
    #plt.show()
    print(f"fraction of links: {get_sparsity(adj_m):.3f}")
    fig, ax = plt.subplots(dpi=100)
    ax.imshow(adj_m,cmap="Greys", interpolation='none')
    #fig.set_facecolor("white")
    #ax.plot(0,0, "o", c="black", label=f"fraction of links: {get_sparsity(adj_m):.3f}")
    ax.set_title(f"Adjacency matrix ({alpha})")
    #ax.legend()
    #ax.savefig(f'synt_adjacency_test.png', dpi=500)
    #plt.show()

    fig, ax = plt.subplots(dpi=100)
    ax.scatter(synth_data[:, 0], synth_data[:, 1], c=list(partition.values()), cmap='tab10')
    ax.scatter(A[0, :], A[1, :], marker='^', c='black', label="Archetypes")
    #ax.set_title(f"True_latent_space_louvain.png", dpi=500)
    #plt.savefig(f"True_latent_space_louvain_test.png", dpi=500)
    #plt.show()
    return adj_m, z, A, Z, beta, partition

if __name__ == "__main__":

    main(alpha=0.2, k=3, dim=2, nsamples=100, rand=False)

ext: py

max_stars_repo_name: ChristianDjurhuus/RAA

max_issues_repo_name: ChristianDjurhuus/RAA

max_forks_repo_name: ChristianDjurhuus/RAA

reason: import numpy,from scipy,import networkx


text:
import os

import numpy as np
import visualize as artist
import matplotlib.pyplot as plt

from matplotlib import rcParams

rcParams['text.usetex'] = True

def jaccard_index(abstract,topic):
	abstract = set(abstract)
	topic = set(topic)

	return len(abstract & topic)/float(len(abstract | topic))

def parse(data):
	ans = {}
	for line in data:
		topic,values = line.split(':')
		values = dict([item.split('*')[::-1] for item in values.split(' + ')])
		ans[topic] = values
	return ans

#--load topics
topics = parse(open('topics','rb').read().splitlines())
all_words = list(set([item for sublist in topics.values() for item in sublist.keys()]))

#--load abstracts
abstracts = [line.split() for line in open('cleansed_abstracts','rb').read().splitlines()]

overlap = np.array([[jaccard_index(abstract,topics[topic].keys()) for abstract in abstracts] for topic in topics])
#-Each row is a topic, each column is an abstract

SAVENAME = 'jaccard_overlap'
if not os.path.isfile(SAVENAME):
	np.savetxt(SAVENAME,overlap,fmt='%.04f',delimiter='\t',header='Each row is a topic, each column an abstract')

idx = np.argsort(overlap,axis=1)
print idx
fig = plt.figure()
ax = fig.add_subplot(111)
cax = ax.imshow(np.sort(overlap,axis=1),interpolation='nearest',aspect='auto')

plt.colorbar(cax)
plt.tight_layout()
plt.show()

reason: import numpy


text:
import os
import pickle

import numpy as np

from configs import ROOT_DIR

def expand_W(W, kw_nums):
    """Transforms W from containing bids for keywords to contain bids for queries. This is done using
    np.take(https://numpy.org/doc/stable/reference/generated/numpy.take.html). Now we can directly apply the weights
    like we did in the greedy setting.
    Args:
        W:
        kw_nums:
    Returns:

    """
    W_new = np.take(W, indices=kw_nums, axis=1)
    return W_new

def construct_data_file_from_alias(data_alias='ds0'):
    path = os.path.join(ROOT_DIR, 'data', data_alias+'.pkl')
    return path

def create_data_vars(data_alias='ds0'):
    """Use aliases like ds0, ds1, ds2, ds3 to initialize the required matrices.
    NOTE: The returned W contains expanded dims with bids of dimension n*m inplace of the original n*r,
    Args:
        data_alias:ds0|ds1|ds2|ds3

    Returns: Dictionary of data variables.
    """
    filepath = construct_data_file_from_alias(data_alias)
    assert os.path.exists(filepath), f"Pickle filepath: {filepath} doesn't exist"
    B, W, kw_nums = pickle.load(open(filepath, 'rb'))
    n = len(B)
    m = len(kw_nums)
    r = len(W[0])
    # print(f'W: {len(W), len(W[0])} | n: {n} | m: {m} | r: {r}')

    data = {
        'n': n,
        'm': m,
        'r': r,
        'B': B,
        'W': W,
        'kw_nums': kw_nums
    }
    return data

# print(create_data_vars('ds0'))


text:
import concurrent
import sys
from .mdso.spectral_ordering_ import SpectralBaseline, SpectralOrdering
from .cycle_phasing import dis_counts
import numpy as np
import pandas as pd
from sklearn import preprocessing
from sklearn.decomposition import PCA
from sklearn.metrics.pairwise import euclidean_distances, rbf_kernel
from concurrent import futures
def euclid_kernel(cdps,n_components):
    # affinity based on PCA-euclid-distance
    # Input:
    #    n_components: number of PCA components used to calculate distance
    # Output:
    #    similarity matrix
    scaler = preprocessing.StandardScaler()
    scaled = scaler.fit_transform(cdps)
    pca = PCA()
    pca_res = pca.fit_transform(scaled)
    dm = euclidean_distances(pca_res[:,:n_components])
    sm = np.exp(-dm * 1/n_components)
    return sm
def euclid(cdps,n_components):
    scaler = preprocessing.StandardScaler()
    scaled = scaler.fit_transform(cdps)
    pca = PCA()
    pca_res = pca.fit_transform(scaled)
    dm = euclidean_distances(pca_res[:,:n_components])
    return dm
def c_rbf_kernel(cdps,n_components):
    scaler = preprocessing.StandardScaler()
    scaled = scaler.fit_transform(cdps)
    return rbf_kernel(scaled)
def _mdso(cdps:pd.DataFrame, annote:pd.DataFrame, n_components:int=6):
    """
    Input:
        cdps: contact decay profile, index must be sample name
        annote: annotaions, index must be sample name
    Output:
        list of sample_name as order
    """
    # calculate similarity matrix
    sm =pd.DataFrame(
        euclid_kernel(cdps.values,n_components),
        index = cdps.index,
        columns = cdps.index
        )
    so = SpectralOrdering()
    so_res = so.fit_transform(sm.values)
    return list(annote.iloc[so_res].index)
def spectral_ordering(filesp, threads=24):
    """
    Input:
        filesp : DataFrame, must have pairs col
    Output:
        DataFrame with additional col "order_index"
    """
    with futures.ProcessPoolExecutor(threads) as pool:
        res = pool.map(dis_counts,filesp["pairs"])
    ares = list(res)
    cdps = pd.DataFrame(ares)
    cdps.columns = cdps.columns.astype("string")
    cdps.index = filesp.index
    order = _mdso(cdps,filesp)
    order_index = pd.Series(list(range(len(order))),index=order,name="order_index")
    new_filesp = pd.concat([filesp,order_index],axis=1)
    return new_filesp


text:
#
#    Module: String2TF
#
#    Author: Dimitiros Pritsos
#
#    License: BSD Style
#
#    Last update: Please refer to the GIT tracking
#

""" html2vect.base.vectortypes.termslist2tfavgspeed: submodule of `html2tf` module defines the class BaseString2TPL """

import numpy as np
from termslist2tf import *

def trms2tpl_dict(terms_l):

    #In case None is returned then return None again. The outer code layer should handle this if caused due to error.
    if terms_l == None:
        return None

    #Count Terms and Build the Terms-PositionsList (TP) dictionary
    TPL_d = dict()
    for i, trm in enumerate(terms_l):
        if trm in TPL_d: #if the dictionary of terms has the 'terms' as a key
            TPL_d[ trm ].append(i + 1)
        else:
            TPL_d[ trm ] = [i + 1]

    return TPL_d

def trms2tfspd_dict(terms_l, vocabulary=None):

    #Getting the terms positions lists dictionary.
    trms_pos = trms2tpl_dict(terms_l)

    #Gettring the terms frequencies dictionary.
    trms_tf = trms2tf_dict(terms_l, vocabulary)

    #Creating the list of average-frequency-speeds of terms found in terms_lst.
    tfs_list = [ (t, f / (float(max(trms_pos[t]) - min(trms_pos[t])) + 1.0) ) for t, f in trms_tf.items() ]

    #Retunring the terms dictonary of average-frequency-speeds.
    return dict(tfs_list)


text:
import time
import numpy as np
import networkx as nx
import matplotlib.pyplot as plt
from tqdm.contrib.concurrent import process_map

import algorithm
import graph_utils
import gen_circuits

file = open("result.csv", "w+")

n_runs = 20

def dispatcher(args):
    return single_run(args[0], args[1])

def opt_1(G):
    time_1 = time.time()
    W, D = algorithm.WD(G)
    Gr_1 = algorithm.OPT_1(G, W, D)
    return time.time() - time_1

def opt_2(G):
    time_2 = time.time()
    W, D = algorithm.WD(G)
    Gr_2 = algorithm.OPT_2(G, D)
    return time.time() - time_2

def single_run(G, run_name):
    # Using OPT 1
    time_1 = [opt_1(G) for _ in range(n_runs)]
    m_time_1 = np.mean(time_1)
    d_time_1 = np.std(time_1)
    # Using OPT 2
    time_2 = [opt_2(G) for _ in range(n_runs)]
    m_time_2 = np.mean(time_2)
    d_time_2 = np.std(time_2)

    # Check
    nodes, edges, clock = graph_utils.graph_stats(G)

    file.write(f"{run_name}, {nodes}, {edges}, {m_time_1}, {d_time_1}, {m_time_2}, {d_time_2}\n")
    file.flush()

runs = []

# Test the correlator
for i in range(2, 5):
    for j in range(1, 5):
        G = gen_circuits.gen_tree(n_branch=i, depth=j)
        G = graph_utils.preprocess(G)
        runs += [(G, f"Tree n_branch {i} depth {j}")]

# Test the correlator
for i in range(2, 5):
    for j in range(1, 5):
        G = gen_circuits.gen_tree(n_branch=i, depth=j, delay=100, random_delays=True)
        G = graph_utils.preprocess(G)
        runs += [(G, f"Tree n_branch {i} depth {j} random_delays")]

# Test the correlator
for j in range(20, 200, 20):
    G = gen_circuits.gen_tree(n_branch=1, depth=j)
    G = graph_utils.preprocess(G)
    runs += [(G, f"Tree n_branch {1} depth {j}")]

# Test the correlator
for j in range(20, 200, 20):
    G = gen_circuits.gen_tree(n_branch=1, depth=j, delay=100, random_delays=True)
    G = graph_utils.preprocess(G)
    runs += [(G, f"Tree n_branch {1} depth {j} random_delays")]

# Test the correlator
for i in range(5, 40, 5):
    G = gen_circuits.gen_correlator(i)
    G = graph_utils.preprocess(G)
    runs += [(G, f"Correlator {i} bit")]

# Test the correlator
for i in range(20, 200, 20):
    G = gen_circuits.gen_full_graph(i)
    G = graph_utils.preprocess(G)
    runs += [(G, f"Full graph {i} nodes")]

# Test the correlator
for i in range(20, 200, 20):
    G = gen_circuits.gen_full_graph(i, delay=100, random_delays=True)
    G = graph_utils.preprocess(G)
    runs += [(G, f"Full graph {i} nodes random_delays")]

process_map(dispatcher, runs, max_workers=20)

reason: import numpy,import networkx


text:
import numpy as np
import logging
from FixedBase import FixedBase
from seldon_core.proto import prediction_pb2
import tensorflow as tf

class FixedPredictRaw(FixedBase):

    def __init__(self, iterations=1):
        super().__init__(iterations)

    def predict_raw(self, X):
        is_proto = isinstance(X, prediction_pb2.SeldonMessage)
        if is_proto:
            self.work()
            Y = np.array(self.work())
            datadef = prediction_pb2.DefaultData(
                tftensor = tf.make_tensor_proto(Y)
            )
            request = prediction_pb2.SeldonMessage(data = datadef)
            return request
        else:
            return self.work()

max_stars_repo_name: juldou/seldon-core

max_issues_repo_name: juldou/seldon-core

max_forks_repo_name: juldou/seldon-core


text:
import netCDF4 as nc
import numpy as np
import matplotlib.pylab as plt
from scipy.interpolate import interp1d

nc_file = nc.Dataset(r"T:\sync_to_data\MODIS_snow\NSDI_SI_cloudfilled\DSC_MOD10A1_2000_v0_nosparse_interp.nc")
nc_dt = nc.num2date(nc_file.variables['time'][:], nc_file.variables['time'].units)
nc_time = nc_file.variables['time'][:]
y = nc_file.variables['y'][:]
x = nc_file.variables['x'][:]

ndsi_filled = np.zeros(nc_file.variables['NDSI_Snow_Cover'].shape, dtype=np.uint8)

for point_y in range(len(y)):
    for point_x in range(len(x)):
        # point_x = 611
        # point_y = 937
        ndsi_raw = nc_file.variables['NDSI_Snow_Cover'][:, point_y, point_x]
        if ndsi_raw[0] == 237: # inland water
            ndsi_filled[:, point_y, point_x] = 237
        elif ndsi_raw[0] == 239: # ocean
            ndsi_filled[:, point_y, point_x] = 239
        else:
            clean_ndsi = ndsi_raw.data.copy()
            clean_ndsi[(ndsi_raw.mask)] = 255  # set masked values to 255 (max for uint8)
            clean_time = nc_time[(clean_ndsi <= 100)]
            clean_ndsi = clean_ndsi[(clean_ndsi <= 100)]
            if len(clean_time) == 0:
                ndsi_filled[:, point_y, point_x] = 200 # no good points
                pass
            else:
                try:
                    f = interp1d(clean_time, clean_ndsi, kind='cubic', bounds_error=False, fill_value=np.mean(clean_ndsi))

                    ndsi_fill = f(nc_time)
                    ndsi_filled[:, point_y, point_x] = ndsi_fill
                except:
                    ndsi_filled[:, point_y, point_x] = 200 # bad interpolation

plt.imshow(np.mean(ndsi_filled,axis=0),origin=0)
plt.show()

# point_x = 611
# point_y = 937
# nc2 = nc.Dataset(r"Z:\MODIS_snow\MODIS_NetCDF\fsca_2001hy_comp9.nc")
# fsca_old = nc2.variables['fsca'][:,point_y,point_x-460]
# nc2_dt = nc.num2date(nc2.variables['time'][:], nc2.variables['time'].units)
# nc2_e = nc2.variables['easting'][:]
# nc2_n = nc2.variables['northing'][:]
#
# fig1 = plt.figure()
# plt.plot(nc_dt,ndsi_filled)
# plt.plot(nc_dt,ndsi_raw,'o')
# plt.plot(nc2_dt,fsca_old)
# plt.legend(['cloud filled ndsi', 'ndsi','fsca(Todd)'],loc=7)
# plt.title('x= {}, y= {}  '.format(x[point_x],y[point_y]) + 'e2= {}, n2= {}'.format(nc2_e[point_x-460],nc2_n[point_y]))
#

reason: import numpy,from scipy


text:
# -*- coding: utf-8 -*-
"""Contains collection of error based deleters"""
from typing import Sequence

import numpy as np

from ..base import Property
from .base import Deleter

class CovarianceBasedDeleter(Deleter):
    """ Track deleter based on covariance matrix size.

    Deletes tracks whose state covariance matrix (more specifically its trace)
    exceeds a given threshold.
    """

    covar_trace_thresh: float = Property(doc="Covariance matrix trace threshold")
    mapping: Sequence[int] = Property(default=None,
                                      doc="Track state vector indices whose corresponding "
                                          "covariances' sum is to be considered. Defaults to"
                                          "None, whereby the entire track covariance trace is "
                                          "considered.")

    def check_for_deletion(self, track, **kwargs):
        """Check if a given track should be deleted

        A track is flagged for deletion if the trace of its state covariance
        matrix is higher than :py:attr:`~covar_trace_thresh`.

        Parameters
        ----------
        track : Track
            A track object to be checked for deletion.

        Returns
        -------
        bool
            `True` if track should be deleted, `False` otherwise.
        """

        diagonals = np.diag(track.state.covar)
        if self.mapping:
            track_covar_trace = np.sum(diagonals[self.mapping])
        else:
            track_covar_trace = np.sum(diagonals)

        if track_covar_trace > self.covar_trace_thresh:
            return True
        return False


text:
# Licensed under a 3-clause BSD style license - see LICENSE.rst
"""
This module defines interpolator classes for Background2D.
"""

import numpy as np

from ..utils import ShepardIDWInterpolator

__all__ = ['BkgZoomInterpolator', 'BkgIDWInterpolator']

__doctest_requires__ = {('BkgZoomInterpolator'): ['scipy']}

class BkgZoomInterpolator:
    """
    This class generates full-sized background and background RMS images
    from lower-resolution mesh images using the `~scipy.ndimage.zoom`
    (spline) interpolator.

    This class must be used in concert with the `Background2D` class.

    Parameters
    ----------
    order : int, optional
        The order of the spline interpolation used to resize the
        low-resolution background and background RMS mesh images.  The
        value must be an integer in the range 0-5.  The default is 3
        (bicubic interpolation).

    mode : {'reflect', 'constant', 'nearest', 'wrap'}, optional
        Points outside the boundaries of the input are filled according
        to the given mode.  Default is 'reflect'.

    cval : float, optional
        The value used for points outside the boundaries of the input if
        ``mode='constant'``. Default is 0.0

    grid_mode : bool, optional
        If `True` (default), the samples are considered as the centers
        of regularly-spaced grid elements. If `False`, the samples
        are treated as isolated points. For zooming 2D images,
        this keyword should be set to `True`, which makes zoom's
        behavior consistent with `scipy.ndimage.map_coordinates` and
        `skimage.transform.resize`. The `False` option is provided only
        for backwards-compatibility.
    """

    def __init__(self, *, order=3, mode='reflect', cval=0.0, grid_mode=True):
        self.order = order
        self.mode = mode
        self.cval = cval
        self.grid_mode = grid_mode

    def __call__(self, mesh, bkg2d_obj):
        """
        Resize the 2D mesh array.

        Parameters
        ----------
        mesh : 2D `~numpy.ndarray`
            The low-resolution 2D mesh array.

        bkg2d_obj : `Background2D` object
            The `Background2D` object that prepared the ``mesh`` array.

        Returns
        -------
        result : 2D `~numpy.ndarray`
            The resized background or background RMS image.
        """
        mesh = np.asanyarray(mesh)
        if np.ptp(mesh) == 0:
            return np.zeros_like(bkg2d_obj.data) + np.min(mesh)

        from scipy.ndimage import zoom

        if bkg2d_obj.edge_method == 'pad':
            # The mesh is first resized to the larger padded-data size
            # (i.e., zoom_factor should be an integer) and then cropped
            # back to the final data size.
            zoom_factor = bkg2d_obj.box_size
            result = zoom(mesh, zoom_factor, order=self.order, mode=self.mode,
                          cval=self.cval, grid_mode=self.grid_mode)
            return result[0:bkg2d_obj.data.shape[0],
                          0:bkg2d_obj.data.shape[1]]
        else:
            # The mesh is resized directly to the final data size.
            zoom_factor = np.array(bkg2d_obj.data.shape) / mesh.shape
            return zoom(mesh, zoom_factor, order=self.order, mode=self.mode,
                        cval=self.cval)

class BkgIDWInterpolator:
    """
    This class generates full-sized background and background RMS images
    from lower-resolution mesh images using inverse-distance weighting
    (IDW) interpolation (`~photutils.utils.ShepardIDWInterpolator`).

    This class must be used in concert with the `Background2D` class.

    Parameters
    ----------
    leafsize : float, optional
        The number of points at which the k-d tree algorithm switches
        over to brute-force. ``leafsize`` must be positive.  See
        `scipy.spatial.cKDTree` for further information.

    n_neighbors : int, optional
        The maximum number of nearest neighbors to use during the
        interpolation.

    power : float, optional
        The power of the inverse distance used for the interpolation
        weights.

    reg : float, optional
        The regularization parameter. It may be used to control the
        smoothness of the interpolator.
    """

    def __init__(self, *, leafsize=10, n_neighbors=10, power=1.0, reg=0.0):
        self.leafsize = leafsize
        self.n_neighbors = n_neighbors
        self.power = power
        self.reg = reg

    def __call__(self, mesh, bkg2d_obj):
        """
        Resize the 2D mesh array.

        Parameters
        ----------
        mesh : 2D `~numpy.ndarray`
            The low-resolution 2D mesh array.

        bkg2d_obj : `Background2D` object
            The `Background2D` object that prepared the ``mesh`` array.

        Returns
        -------
        result : 2D `~numpy.ndarray`
            The resized background or background RMS image.
        """
        mesh = np.asanyarray(mesh)
        if np.ptp(mesh) == 0:
            return np.zeros_like(bkg2d_obj.data) + np.min(mesh)

        yxpos = np.column_stack(bkg2d_obj._mesh_yxpos)
        mesh1d = mesh[bkg2d_obj._mesh_idx]
        interp_func = ShepardIDWInterpolator(yxpos, mesh1d,
                                             leafsize=self.leafsize)

        # the position coordinates used when calling the interpolator
        ny, nx = bkg2d_obj.data.shape
        yi, xi = np.mgrid[0:ny, 0:nx]
        yx_indices = np.column_stack((yi.ravel(), xi.ravel()))
        data = interp_func(yx_indices,
                           n_neighbors=self.n_neighbors, power=self.power,
                           reg=self.reg)

        return data.reshape(bkg2d_obj.data.shape)

max_stars_repo_name: rosteen/photutils

max_issues_repo_name: rosteen/photutils

max_forks_repo_name: rosteen/photutils


text:
import numpy as np
import pytest

import pandas._testing as tm
from pandas.core.arrays.sparse import SparseArray

@pytest.mark.parametrize(
    "kwargs",
    [
        dict(),  # Default is check_exact=False
        dict(check_exact=False),
        dict(check_exact=True),
    ],
)
def test_assert_extension_array_equal_not_exact(kwargs):
    # see gh-23709
    arr1 = SparseArray([-0.17387645482451206, 0.3414148016424936])
    arr2 = SparseArray([-0.17387645482451206, 0.3414148016424937])

    if kwargs.get("check_exact", False):
        msg = """\
ExtensionArray are different

ExtensionArray values are different \\(50\\.0 %\\)
\\[left\\]:  \\[-0\\.17387645482.*, 0\\.341414801642.*\\]
\\[right\\]: \\[-0\\.17387645482.*, 0\\.341414801642.*\\]"""

        with pytest.raises(AssertionError, match=msg):
            tm.assert_extension_array_equal(arr1, arr2, **kwargs)
    else:
        tm.assert_extension_array_equal(arr1, arr2, **kwargs)

@pytest.mark.parametrize("decimals", range(10))
def test_assert_extension_array_equal_less_precise(decimals):
    rtol = 0.5 * 10 ** -decimals
    arr1 = SparseArray([0.5, 0.123456])
    arr2 = SparseArray([0.5, 0.123457])

    if decimals >= 5:
        msg = """\
ExtensionArray are different

ExtensionArray values are different \\(50\\.0 %\\)
\\[left\\]:  \\[0\\.5, 0\\.123456\\]
\\[right\\]: \\[0\\.5, 0\\.123457\\]"""

        with pytest.raises(AssertionError, match=msg):
            tm.assert_extension_array_equal(arr1, arr2, rtol=rtol)
    else:
        tm.assert_extension_array_equal(arr1, arr2, rtol=rtol)

def test_assert_extension_array_equal_dtype_mismatch(check_dtype):
    end = 5
    kwargs = dict(check_dtype=check_dtype)

    arr1 = SparseArray(np.arange(end, dtype="int64"))
    arr2 = SparseArray(np.arange(end, dtype="int32"))

    if check_dtype:
        msg = """\
ExtensionArray are different

Attribute "dtype" are different
\\[left\\]:  Sparse\\[int64, 0\\]
\\[right\\]: Sparse\\[int32, 0\\]"""

        with pytest.raises(AssertionError, match=msg):
            tm.assert_extension_array_equal(arr1, arr2, **kwargs)
    else:
        tm.assert_extension_array_equal(arr1, arr2, **kwargs)

def test_assert_extension_array_equal_missing_values():
    arr1 = SparseArray([np.nan, 1, 2, np.nan])
    arr2 = SparseArray([np.nan, 1, 2, 3])

    msg = """\
ExtensionArray NA mask are different

ExtensionArray NA mask values are different \\(25\\.0 %\\)
\\[left\\]:  \\[True, False, False, True\\]
\\[right\\]: \\[True, False, False, False\\]"""

    with pytest.raises(AssertionError, match=msg):
        tm.assert_extension_array_equal(arr1, arr2)

@pytest.mark.parametrize("side", ["left", "right"])
def test_assert_extension_array_equal_non_extension_array(side):
    numpy_array = np.arange(5)
    extension_array = SparseArray(numpy_array)

    msg = f"{side} is not an ExtensionArray"
    args = (
        (numpy_array, extension_array)
        if side == "left"
        else (extension_array, numpy_array)
    )

    with pytest.raises(AssertionError, match=msg):
        tm.assert_extension_array_equal(*args)

max_stars_repo_licenses: MIT Unlicense

max_issues_repo_licenses: MIT Unlicense

max_forks_repo_licenses: MIT Unlicense


text:
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import os
a=os.path.join(os.path.dirname(__file__),"train.csv")

#Cost Function
def cost(h,y,m):
    return(sum((h-y)**2)/(2*m))

#Feature Scaling
def fea_scale(n):
    mea=n.mean()
    sd=n.std()
    return((n-mea)/sd)

#Reading file and removing missing data for all columns
data=pd.read_csv(os.path.join(os.path.dirname(__file__),"train.csv"))
data.dropna(axis=0)

y_data=data.SalePrice
y=np.array(y_data,dtype=np.float64)

x1_data=data["LotArea"]
x2_data=data["OverallQual"]
x3_data=data["OverallCond"]
x4_data=data["YearBuilt"]

m=len(y)
x0=np.ones(m)
x=np.array([x0,x1_data,x2_data,x3_data,x4_data],dtype=np.float64).T

#Applying Feature Scaling for x
for i in range(1,5):
    x[:,i]=fea_scale(x[:,i])

r,c=x.shape
theta=np.zeros(c)
alpha=0.01
epochs=6000

j_value=[]
ite=[]

#Gradient Descent
for k in range(1,epochs+1):
    h=x.dot(theta)          #calculating hypothesis function
    for i in range(0,5):
        theta[i]-=alpha*(sum((h-y)*x[:,i])/(2*m))    #Updating theta
    if(k%1000==0):
        print("epoch",k)
        h=x.dot(theta)
        j_value.append(cost(h,y,m))
        ite.append(k)

for i in range(0,5):
    print("theta",i,theta[i])

#Testing data
print("give input as: 9878 5 7 2006")
ls=list(map(int,input("Enter the 4 features: ").split()))
am=np.array(ls)
print(sum(np.insert(am,0,1)*theta))

#Visualizing cost function
plt.plot(ite,j_value)
plt.xlabel("No of Iterations")
plt.ylabel("Cost Function")
plt.title("Theta Values"+str(theta))
plt.show()

'''I suppose the initialising the theta value is the problem. As we were setting it to 1 it converge to a local minimum
which is inefficient. So the theta should be initialised properly.But I don't know how to do it.'''

max_stars_repo_name: iSriBalaji/MachineLearning

max_issues_repo_name: iSriBalaji/MachineLearning

max_forks_repo_name: iSriBalaji/MachineLearning


text:
import matplotlib.pyplot as plt
import numpy as np
from tensorflow.keras.datasets import mnist

def conv2D(image: np.ndarray, kernel: np.ndarray) -> np.ndarray:
    rows, cols = image.shape  # 28x28
    k_size, _ = kernel.shape  # 2x2
    conv_image = np.zeros(shape=(rows, cols), dtype=np.float32)  # 28x28

    for i in range(rows - k_size):
        for j in range(cols - k_size):
            conv_image[i][j] = np.sum(kernel * image[i: i + k_size, j: j + k_size])

    return conv_image

if __name__ == "__main__":
    (x_train, y_train), (x_test, y_test) = mnist.load_data()

    image = x_train[0]
    image = image.reshape((28, 28))
    kernel = np.random.uniform(low=0.0, high=1.0, size=(2, 2))

    conv_image = conv2D(image, kernel)

    plt.imshow(image, cmap="gray")
    plt.show()

    plt.imshow(conv_image, cmap="gray")
    plt.show()

max_stars_repo_name: thisisjako/UdemyTF

max_issues_repo_name: thisisjako/UdemyTF

max_forks_repo_name: thisisjako/UdemyTF


text:
import os
import math
import yaml
import lmdb
import numpy as np
import torch
import wandb
import carla
import random
import string

from torch.distributions.categorical import Categorical
from srunner.scenariomanager.carla_data_provider import CarlaDataProvider
from leaderboard.autoagents.autonomous_agent import AutonomousAgent, Track
from utils import visualize_obs, _numpy
from pathlib import Path

from rails.bellman import BellmanUpdater
from rails.models import EgoModel, CameraModel
from autoagents.waypointer import Waypointer

def get_entry_point():
    return 'QCollectorImage'

FPS = 20.
STOP_THRESH = 0.1
MAX_STOP = 500

class OrnsteinUhlenbeckActionNoise:
    def __init__(self, mu=0, sigma=0.1, theta=0.1, dt=0.1, x0=None):
        self.theta = theta
        self.mu = mu
        self.sigma = sigma
        self.dt = dt
        self.x0 = x0
        self.reset()

    def __call__(self):
        x = self.x_prev + self.theta * (self.mu - self.x_prev) * self.dt + self.sigma * np.sqrt(self.dt) * np.random.normal()
        self.x_prev = x
        return x

    def reset(self):
        self.x_prev = self.x0 if self.x0 is not None else np.zeros_like(self.mu)

class QCollectorImage(AutonomousAgent):

    """
    action value agent but assumes a static world
    """

    def setup(self, path_to_conf_file):
        """
        Setup the agent parameters
        """

        self.track = Track.MAP
        self.num_frames = 0

        with open(path_to_conf_file, 'r') as f:
            config = yaml.safe_load(f)

        for key, value in config.items():
            setattr(self, key, value)

        device = torch.device('cuda')
        ego_model = EgoModel(1./FPS*(self.num_repeat+1)).to(device)
        ego_model.load_state_dict(torch.load(self.ego_model_dir))
        ego_model.eval()
        BellmanUpdater.setup(config, ego_model, device=device)

        self.vizs      = []
        self.wide_rgbs = []
        self.narr_rgbs = []
        self.wide_sems = []
        self.narr_sems = []

        self.lbls = []
        self.locs = []
        self.rots = []
        self.spds = []
        self.cmds = []
        self.infs = []

        self.waypointer = None

        #if self.log_wandb:
        #    wandb.init(project='carla_data_phase1')

        self.noiser = OrnsteinUhlenbeckActionNoise(dt=1/FPS)
        self.prev_steer = 0

        self.stop_count = 0

        # for hybrid mode
        self.device = device
        self.image_model = CameraModel(config).to(self.device)
        self.image_model.load_state_dict(torch.load(self.main_model_dir))
        self.image_model.eval()

        self.steers = torch.tensor(np.linspace(-self.max_steers,self.max_steers,self.num_steers)).float().to(self.device)
        self.throts = torch.tensor(np.linspace(0,self.max_throts,self.num_throts)).float().to(self.device)

        self.lane_change_counter = 0
        self.lane_changed=None

        self.rstring = _random_string()
        self.num_infractions = 0
        #os.environ['SAVE_ROOT'] = os.path.join(self.main_data_dir, self.rstring)
        os.environ['SAVE_ROOT'] = self.main_data_dir

        self.num_samples = 0
        self.initialized = False

    def destroy(self):
        if len(self.lbls) == 0:
            return
        self.cleanup()

    def cleanup(self):
        inf_list = CarlaDataProvider.get_infraction_list()
        if len(inf_list) > 0:
            inf_obj = inf_list[-1]
            if len(self.infs) == 0:
                self.infs.append(inf_obj.get_type().value)
            else:
                self.infs[-1] = inf_obj.get_type().value

        self.flush_data(self.num_samples-1)
        with self.lmdb_env.begin(write=True) as txn:
            txn.put('len'.encode(), str(self.num_samples).encode())

        self.vizs.clear()
        self.wide_rgbs.clear()
        self.narr_rgbs.clear()
        self.wide_sems.clear()
        self.narr_sems.clear()
        self.lbls.clear()
        self.locs.clear()
        self.rots.clear()
        self.spds.clear()
        self.cmds.clear()
        self.infs.clear()

        self.lmdb_env.close()

    def flush_data(self, i):
        with self.lmdb_env.begin(write=True) as txn:
            for idx in range(len(self.camera_yaws)):
                txn.put(
                    f'wide_rgb_{idx}_{i:05d}'.encode(),
                    np.ascontiguousarray(self.wide_rgbs[i][idx]).astype(np.uint8),
                )

                txn.put(
                    f'narr_rgb_{idx}_{i:05d}'.encode(),
                    np.ascontiguousarray(self.narr_rgbs[i][idx]).astype(np.uint8),
                )

                txn.put(
                    f'wide_sem_{idx}_{i:05d}'.encode(),
                    np.ascontiguousarray(self.wide_sems[i][idx]).astype(np.uint8),
                )

                txn.put(
                    f'narr_sem_{idx}_{i:05d}'.encode(),
                    np.ascontiguousarray(self.narr_sems[i][idx]).astype(np.uint8),
                )

            txn.put(
                f'lbl_{i:05d}'.encode(),
                np.ascontiguousarray(self.lbls[i]).astype(np.uint8),
            )

            txn.put(
                f'loc_{i:05d}'.encode(),
                np.ascontiguousarray(self.locs[i]).astype(np.float32)
            )

            txn.put(
                f'rot_{i:05d}'.encode(),
                np.ascontiguousarray(self.rots[i]).astype(np.float32)
            )

            txn.put(
                f'spd_{i:05d}'.encode(),
                np.ascontiguousarray(self.spds[i]).astype(np.float32)
            )

            txn.put(
                f'cmd_{i:05d}'.encode(),
                np.ascontiguousarray(self.cmds[i]).astype(np.float32)
            )

            txn.put(
                f'inf_{i:05d}'.encode(),
                np.ascontiguousarray(self.infs[i]).astype(np.float32)
            )

    def sensors(self):
        sensors = [
            {'type': 'sensor.collision', 'id': 'COLLISION'},
            {'type': 'sensor.map', 'id': 'MAP'},
            {'type': 'sensor.speedometer', 'id': 'EGO'},
            {'type': 'sensor.other.gnss', 'x': 0., 'y': 0.0, 'z': self.camera_z, 'id': 'GPS'},
        ]

        # Add sensors
        for i, yaw in enumerate(self.camera_yaws):
            x = self.camera_x*math.cos(yaw*math.pi/180)
            y = self.camera_x*math.sin(yaw*math.pi/180)
            sensors.append({'type': 'sensor.stitch_camera.rgb', 'x': x, 'y': y, 'z': self.camera_z, 'roll': 0.0, 'pitch': 0.0, 'yaw': yaw,
            'width': 160, 'height': 240, 'fov': 60, 'id': f'Wide_RGB_{i}'})
            sensors.append({'type': 'sensor.stitch_camera.semantic_segmentation', 'x': x, 'y': y, 'z': self.camera_z, 'roll': 0.0, 'pitch': 0.0, 'yaw': yaw,
            'width': 160, 'height': 240, 'fov': 60, 'id': f'Wide_SEG_{i}'})
            sensors.append({'type': 'sensor.camera.rgb', 'x': x, 'y': y, 'z': self.camera_z, 'roll': 0.0, 'pitch': 0.0, 'yaw': yaw,
            'width': 384, 'height': 240, 'fov': 50, 'id': f'Narrow_RGB_{i}'})
            sensors.append({'type': 'sensor.camera.semantic_segmentation', 'x': x, 'y': y, 'z': self.camera_z, 'roll': 0.0, 'pitch': 0.0, 'yaw': yaw,
            'width': 384, 'height': 240, 'fov': 50, 'id': f'Narrow_SEG_{i}'})

        return sensors

    def _init(self):
        data_path = Path(self.main_data_dir) / 'data' / os.environ['ROUTE_NAME'] / os.environ['REPETITION']
        data_path.mkdir(parents=True,exist_ok=True)
        data_path = str(data_path)
        self.lmdb_env = lmdb.open(data_path, map_size=int(1e10))
        self.initialized = True

    def run_step(self, input_data, timestamp):
        if not self.initialized:
            self._init()

        wide_rgbs = []
        narr_rgbs = []
        wide_sems = []
        narr_sems = []

        for i in range(len(self.camera_yaws)):

            _, wide_rgb = input_data.get(f'Wide_RGB_{i}')
            _, narr_rgb = input_data.get(f'Narrow_RGB_{i}')
            _, wide_sem = input_data.get(f'Wide_SEG_{i}')
            _, narr_sem = input_data.get(f'Narrow_SEG_{i}')

            wide_rgbs.append(wide_rgb[...,:3])
            narr_rgbs.append(narr_rgb[...,:3])
            wide_sems.append(wide_sem)
            narr_sems.append(narr_sem)

        _, lbl = input_data.get('MAP')
        _, col = input_data.get('COLLISION')
        _, ego = input_data.get('EGO')
        _, gps = input_data.get('GPS')

        if self.waypointer is None:
            self.waypointer = Waypointer(self._global_plan, gps)
            _, _, cmd = self.waypointer.tick(gps)
        else:
            _, _, cmd = self.waypointer.tick(gps)

        yaw = ego.get('rot')[-1]
        spd = ego.get('spd')
        loc = ego.get('loc')

        #delta_locs, delta_yaws, next_spds = BellmanUpdater.compute_table(yaw/180*math.pi)

        # Convert lbl to rew maps
        lbl_copy = lbl.copy()
        #waypoint_rews, stop_rews, brak_rews, free = BellmanUpdater.get_reward(lbl_copy, [0,0], ref_yaw=yaw/180*math.pi)

        #waypoint_rews = waypoint_rews[None].expand(self.num_plan, *waypoint_rews.shape)
        #brak_rews = brak_rews[None].expand(self.num_plan, *brak_rews.shape)
        #stop_rews = stop_rews[None].expand(self.num_plan, *stop_rews.shape)
        #free = free[None].expand(self.num_plan, *free.shape)

        # If it is idle, make it LANE_FOLLOW
        cmd_value = cmd.value-1
        cmd_value = 3 if cmd_value < 0 else cmd_value

        #action_values, _ = BellmanUpdater.get_action(
        #    delta_locs, delta_yaws, next_spds,
        #    waypoint_rews[...,cmd_value], brak_rews, stop_rews, free,
        #    torch.zeros((self.num_plan,2)).float().to(BellmanUpdater._device),
        #    extract=(
        #        torch.tensor([[0.,0.]]),  # location
        #        torch.tensor([0.]),       # yaw
        #        torch.tensor([spd]),      # spd
        #    )
        #)
        #action_values = action_values.squeeze(0)

        #action = int(Categorical(logits=action_values/self.temperature).sample())
        # action = int(action_values.argmax())

        #steer, throt, brake = map(float, BellmanUpdater._actions[action])
        steer, throt, brake = self.run_step_image_policy(input_data, timestamp, cmd)

        if self.noise_collect:
            steer += self.noiser()

        #if len(self.vizs) > self.num_per_flush:
        #    self.flush_data()

        rgb = np.concatenate([wide_rgbs[0], narr_rgbs[0]], axis=1)
        spd = ego.get('spd')
        self.vizs.append(visualize_obs(rgb, yaw/180*math.pi, (steer, throt, brake), spd, cmd=cmd.value, lbl=lbl_copy))

        if col:
            self.cleanup()
            raise Exception('Collector has collided!! Heading out :P')

        if spd < STOP_THRESH:
            self.stop_count += 1
        else:
            self.stop_count = 0

        if cmd_value in [4,5]:
            actual_steer = steer
        else:
            actual_steer = steer * 1.2

        self.prev_steer = actual_steer

        # Save data
        if self.num_frames % (self.num_repeat+1) == 0 and self.stop_count < MAX_STOP:
            # Note: basically, this is like fast-forwarding. should be okay tho as spd is 0.
            self.wide_rgbs.append(wide_rgbs)
            self.narr_rgbs.append(narr_rgbs)
            self.wide_sems.append(wide_sems)
            self.narr_sems.append(narr_sems)
            self.lbls.append(lbl)
            self.locs.append(loc)
            self.rots.append(yaw)
            self.spds.append(spd)
            self.cmds.append(cmd_value)

            inf = -1
            infs = CarlaDataProvider.get_infraction_list()
            if self.num_infractions < len(infs):
                inf_obj = infs[-1]
                inf = inf_obj.get_type().value
                self.num_infractions = len(infs)

            self.infs.append(inf)
            self.flush_data(self.num_samples)
            self.num_samples += 1

        self.num_frames += 1

        return carla.VehicleControl(steer=actual_steer, throttle=throt, brake=brake)

    def run_step_image_policy(self, input_data, timestamp, cmd):
        _, wide_rgb = input_data.get(f'Wide_RGB_0')
        _, narr_rgb = input_data.get(f'Narrow_RGB_0')

        # Crop images
        _wide_rgb = wide_rgb[self.wide_crop_top:,:,:3]
        _narr_rgb = narr_rgb[:-self.narr_crop_bottom,:,:3]

        _wide_rgb = _wide_rgb[...,::-1].copy()
        _narr_rgb = _narr_rgb[...,::-1].copy()

        _, ego = input_data.get('EGO')
        _, gps = input_data.get('GPS')

        #_, _, cmd = self.waypointer.tick(gps)

        spd = ego.get('spd')

        cmd_value = cmd.value-1
        cmd_value = 3 if cmd_value < 0 else cmd_value

        if cmd_value in [4,5]:
            if self.lane_changed is not None and cmd_value != self.lane_changed:
                self.lane_change_counter = 0

            self.lane_change_counter += 1
            self.lane_changed = cmd_value if self.lane_change_counter > {4:200,5:200}.get(cmd_value) else None
        else:
            self.lane_change_counter = 0
            self.lane_changed = None

        if cmd_value == self.lane_changed:
            cmd_value = 3

        _wide_rgb = torch.tensor(_wide_rgb[None]).float().permute(0,3,1,2).to(self.device)
        _narr_rgb = torch.tensor(_narr_rgb[None]).float().permute(0,3,1,2).to(self.device)

        if self.all_speeds:
            steer_logits, throt_logits, brake_logits = self.image_model.policy(_wide_rgb, _narr_rgb, cmd_value)
            # Interpolate logits
            steer_logit = self._lerp(steer_logits, spd)
            throt_logit = self._lerp(throt_logits, spd)
            brake_logit = self._lerp(brake_logits, spd)
        else:
            steer_logit, throt_logit, brake_logit = self.image_model.policy(_wide_rgb, _narr_rgb, cmd_value, spd=torch.tensor([spd]).float().to(self.device))

        action_prob = self.action_prob(steer_logit, throt_logit, brake_logit)

        brake_prob = float(action_prob[-1])

        steer = float(self.steers @ torch.softmax(steer_logit, dim=0))
        throt = float(self.throts @ torch.softmax(throt_logit, dim=0))

        steer, throt, brake = self.post_process(steer, throt, brake_prob, spd, cmd_value)

        #steer, throt, brake = (2*np.random.random()-1, 0.75, 0)
        return steer, throt, brake
        #return carla.VehicleControl(steer=steer, throttle=throt, brake=brake)

    def post_process(self, steer, throt, brake_prob, spd, cmd):

        if brake_prob > 0.5:
            steer, throt, brake = 0, 0, 1
        else:
            brake = 0
            throt = max(0.4, throt)

        # # To compensate for non-linearity of throttle<->acceleration
        # if throt > 0.1 and throt < 0.4:
        #     throt = 0.4
        # elif throt < 0.1 and brake_prob > 0.3:
        #     brake = 1

        if spd > {0:10,1:10}.get(cmd, 20)/3.6: # 10 km/h for turning, 15km/h elsewhere
            throt = 0

        # if cmd == 2:
        #     steer = min(max(steer, -0.2), 0.2)

        # if cmd in [4,5]:
        #     steer = min(max(steer, -0.4), 0.4) # no crazy steerings when lane changing

        return steer, throt, brake

    def _lerp(self, v, x):
        D = v.shape[0]

        min_val = self.min_speeds
        max_val = self.max_speeds

        x = (x - min_val)/(max_val - min_val)*(D-1)

        x0, x1 = max(min(math.floor(x), D-1),0), max(min(math.ceil(x), D-1),0)
        w = x - x0

        return (1-w) * v[x0] + w * v[x1]

    def action_prob(self, steer_logit, throt_logit, brake_logit):

        steer_logit = steer_logit.repeat(self.num_throts)
        throt_logit = throt_logit.repeat_interleave(self.num_steers)

        action_logit = torch.cat([steer_logit, throt_logit, brake_logit[None]])

        return torch.softmax(action_logit, dim=0)

def _random_string(length=10):
    return ''.join(random.choice(string.ascii_lowercase) for i in range(length))


text:
import numpy as np

# List of states
# - forward
# - stop
# - recovery
# - steering_to_rock
# - slow_forward_to_rock
# - pick_up_rock

# Implemention of left wall following method

# This is where you can build a decision tree for determining throttle, brake and steer
# commands based on the output of the perception_step() function
def decision_step(Rover):

    # Implement conditionals to decide what to do given perception data
    # Here you're all set up with some basic functionality but you'll need to
    # improve on this decision tree to do a good job of navigating autonomously!

    print (Rover.mode) # Print robot action state

    # Example:
    # Check if we have vision data to make decisions with
    if Rover.nav_angles is not None:
        # Check for Rover.mode status
        if Rover.mode == 'forward':
            # Check the extent of navigable terrain
            if len(Rover.nav_angles) >= Rover.stop_forward:

                # It only stores the angle between 50 to -30 degree
                # So as to restrict the robot field of view, to create a left bias direction
                fov_angles = list()
                for angle in Rover.nav_angles:
                    if angle <= 0.698132 and angle >= -0.523599: # 50 to -30 degrees
                        fov_angles.append(angle * 180/np.pi)

                # If mode is forward, navigable terrain looks good
                # and velocity is below max, then throttle
                if Rover.vel < Rover.max_vel:
                    # Set throttle value to throttle setting
                    Rover.throttle = Rover.throttle_set
                else: # Else coast
                    Rover.throttle = 0

                # Release brake
                Rover.brake = 0

                mean_fov_angle = np.mean(fov_angles)
                if np.isnan(mean_fov_angle):
                    mean_fov_angle = 0.0

                # steering adjustment
                # Set steering to average angle clipped to the range +/- 15
                Rover.steer = np.clip(mean_fov_angle, -15, 15)

                print(Rover.struck_counter)

                # struck_counter to check whether the robot is struck
                if Rover.vel < 0.2:
                    Rover.struck_counter = Rover.struck_counter + 1
                else:
                    Rover.struck_counter = 0

                # if the roock is detected, go to next state 'stop'
                if Rover.rock_found == True:
                    Rover.mode = 'stop'

                # if the robot is struck, go to next state 'stop'
                if Rover.struck_counter >= 54: # Struck 3 seconds , 18fps, 3 * 16 = 54
                    Rover.struck_counter = 0
                    Rover.mode = 'stop'
            # If there's a lack of navigable terrain pixels then go to next state 'stop'
            elif len(Rover.nav_angles) < Rover.stop_forward:
                    # Set mode to "stop" and hit the brakes!
                    Rover.throttle = 0
                    # Set brake to stored brake value
                    Rover.brake = Rover.brake_set
                    Rover.steer = 0
                    Rover.mode = 'stop'

        # If we're already in "stop" mode then make different decisions
        elif Rover.mode == 'stop':
            # If we're in stop mode but still moving keep braking
            if Rover.vel > 0.2:
                Rover.throttle = 0
                Rover.brake = Rover.brake_set
                Rover.steer = 0

            # start next state 'recovery'
            elif Rover.vel <= 0.2 and Rover.rock_found == False:
                Rover.steer = 0
                Rover.mode = 'recovery'

            # if the rock is found and robot is stationary, go to next state 'steering_to_rock'
            if Rover.vel == 0.0 and Rover.rock_found == True:
                Rover.steer = 0
                Rover.mode = 'steering_to_rock'
        # Perform recovery when the robot is struck
        elif Rover.mode == 'recovery':
            left_dists = list()
            center_dists = list()
            right_dists = list()
            # Robot vision is separated into 3 views, left, right and center
            for angle, dist in zip(Rover.nav_angles, Rover.nav_dists):
                if angle <= 0.0349066 and angle >= -0.0349066: # -2 to 2 deg
                    center_dists.append(dist)
                if angle <= -0.436332 and angle >= -0.523599: # -25 to -30 deg
                    right_dists.append(dist)
                if angle <= 0.52359 and angle >= 0.436332: # 25 to 30 deg
                    left_dists.append(dist)

            # compute the mean value
            mean_center_dist = np.mean(center_dists)
            mean_left_dist = np.mean(left_dists)
            mean_right_dist = np.mean(right_dists)

            if np.isnan(mean_center_dist):
                mean_center_dist = 0.0
            if np.isnan(mean_left_dist):
                mean_left_dist = 0.0
            if np.isnan(mean_right_dist):
                mean_right_dist = 0.0

            print(mean_center_dist)
            print(mean_left_dist)
            print(mean_right_dist)

            if Rover.vel <= 0.1:
                Rover.vel_counter = Rover.vel_counter + 1
                Rover.throttle = 0
                # Release the brake to allow turning
                Rover.brake = 0
                # Turn range is +/- 15 degrees, when stopped the next line will induce 4-wheel turning
                Rover.steer = -15
                print('vel = 0, turning')
                if Rover.vel_counter >= 36:
                    Rover.vel_counter = 0
                    Rover.mode = 'forward'
            elif (len(Rover.nav_angles) < Rover.go_forward) or (mean_center_dist <= 15.0) or (mean_left_dist <= 15.0) or (mean_right_dist <= 15.0):
                Rover.throttle = 0
                # Release the brake to allow turning
                Rover.brake = 0
                # Turn range is +/- 15 degrees, when stopped the next line will induce 4-wheel turning
                if mean_right_dist <= 15.0:
                    Rover.steer = 15 # Turn left
                else:
                    Rover.steer = -15 #Turn right
            # If we're stopped but see sufficient navigable terrain in front then go!
            # Return back to 'forward'
            elif len(Rover.nav_angles) >= Rover.go_forward:
                Rover.steer = 0
                # Set throttle back to stored value
                Rover.throttle = 0
                # Release the brake
                Rover.brake = 0
                Rover.mode = 'forward'
        # Steer the robot direction to face the rock
        elif Rover.mode == 'steering_to_rock':

            Rover.throttle = 0
            Rover.brake = 0

            mean_rock_dist = np.mean(Rover.rock_dists)
            mean_rock_angle = np.mean(Rover.rock_angles) * 180/np.pi

            if np.isnan(mean_rock_dist):
                mean_rock_dist = 0.0
            if np.isnan(mean_rock_angle):
                mean_rock_angle = 0.0

            print(mean_rock_dist)
            print(mean_rock_angle)

            Rover.slow_steering_counter = Rover.slow_steering_counter + 1
            # the robot must face the rock between -15 to 15 deg,
            # if true , go to next state 'slow_forward_to_rock'
            if mean_rock_angle <= 15.0 and mean_rock_angle >= -15.0 or Rover.slow_steering_counter >= 100:
                Rover.slow_steering_counter = 0
                Rover.mode = 'slow_forward_to_rock'
                Rover.brake = Rover.brake_set
                Rover.steer = 0
            elif mean_rock_angle >= 0.0:
                Rover.steer = 4.0
                print("left")
            else:
                Rover.steer = -4.0
                print("right")

        elif Rover.mode == 'slow_forward_to_rock':
            Rover.throttle = 0.2
            Rover.brake = 0
            Rover.steer = 0
            Rover.slow_forward_counter = Rover.slow_forward_counter + 1

            # if the robot still unable to reach the rock, go to next state 'stop'
            if Rover.slow_forward_counter >= 100:
                Rover.slow_forward_counter = 0
                Rover.mode = 'stop'
            # the robot is near the rock, set send_pickup and go to next state 'pick_up_rock'
            if Rover.near_sample == 1:
                Rover.throttle = 0.0
                Rover.brake = Rover.brake_set
                Rover.steer = 0
                Rover.send_pickup = True
                Rover.mode = 'pick_up_rock'
        # Waiting for the picking of rock process to be completed
        elif Rover.mode == 'pick_up_rock':
            Rover.pickup_counter = Rover.pickup_counter + 1
            # When the pick_counter timeout, resume back to 'forward'
            if Rover.pickup_counter >= 500:
                print("Done")
                Rover.pickup_counter = 0
                Rover.rock_found = False
                Rover.mode = 'forward'

    # Just to make the rover do something
    # even if no modifications have been made to the code
    else:
        Rover.throttle = Rover.throttle_set
        Rover.steer = 0
        Rover.brake = 0

    return Rover

max_stars_repo_name: tkxiong/RoboND-Rover-Project

max_issues_repo_name: tkxiong/RoboND-Rover-Project

max_forks_repo_name: tkxiong/RoboND-Rover-Project


text:
import numpy as np
import matplotlib.pyplot as plt
from pandas import read_csv
from os import system
import sys

FILE = str(sys.argv[1])
Cmax = 1.66E-16

try:
	CLEAN = "clean" in str(sys.argv[2])
except IndexError:
	CLEAN = False

if CLEAN:
  system("sed -i 's/^  //g' " + FILE)
  system("sed -i 's/  /,/g' " + FILE)
  system("head -3 " + FILE)
  system("rm *.out")

ObservationPoint = np.loadtxt(FILE,delimiter=",",skiprows=1)

Cnorm = ObservationPoint[:,3]/Cmax
Time = ObservationPoint[:,0]

Legend=["$\\dfrac{[V_{(aq)}]}{[V_{(aq)}]_0}$"]

fig = plt.figure(figsize=(5,4),facecolor="white")
ax1 = plt.subplot(1,1,1)
ax1.plot(Time,Cnorm,c="violet",lw=3,label=Legend[0])

ax1.set_yscale("symlog",\
	linthreshy=1.0E-4,subsy=[1,2,3,4,5,6,7,8,9])
ax1.set_ylim([-1.0E-5,1.15])
ax1.set_xlim([0,10])

#ax1.yaxis.grid(True, which='minor')
#ax1.yaxis.set_minor_locator(MultipleLocator(5))

#ax1.set_ylabel(Legend[0],fontsize="large",rotation=0)
ax1.set_xlabel("Time [$a$]",fontsize="large")
ax1.legend(fontsize="large")
plt.savefig("./breakthrough.png",transparent=True)

max_stars_repo_name: edsaac/bioparticle

max_issues_repo_name: edsaac/bioparticle


text:
from numpy import *
from geo import *

def prop2array(prop,x,y,z, undefined_value):
	if(prop.size() != (x*y*z)):
		print "Error! Property size is: ", prop.size(), "but given x*y*z size is: ",x*y*z
		return
	if type(prop) is hpgl.cont_property_array:
		values = zeros(prop.size(), dtype=float64)
		values_right = zeros( (x,y,z), dtype=float64)
	else:
		values = zeros(prop.size(), dtype=int32)
		values_right = zeros( (x,y,z), dtype=int32)

	for i in xrange(prop.size()):
		if(prop.is_informed(i) == True):
			values[i]=prop.get_at(i)
		else:
			values[i] = undefined_value

	values = array(values).reshape(z,y,x)

	for i in range(x):
		for j in range(y):
			for k in range(z):
				values_right[i,j,k] = values[k,j,i]

	return values_right

def array2prop(array_prop, undefined_value):
	x = size(array_prop,0)
	y = size(array_prop,1)
	z = size(array_prop,2)

	if array_prop.dtype is not dtype('int32'):
		print "Array is float, creating continious property..."
		prop = hpgl.cont_property_array(array_prop.size)
		print "Done"
	else:
		print "Array is int, creating indicator property..."
		indicators = unique(array_prop)
		indicators_t = []
		for k in xrange(indicators.size):
			if(indicators[k] != undefined_value):
				indicators_t.append(indicators[k])
		print "Indicators is: ", indicators_t
		prop = hpgl.byte_property_array(array_prop.size, indicators_t )
		print "Done"

	values_for_prop = zeros( (z,y,x), dtype=array_prop.dtype)

	for i in range(x):
		for j in range(y):
			for k in range(z):
				values_for_prop[k,j,i] = array_prop[i,j,k]

	size_p = x*y*z
	values_for_prop = values_for_prop.reshape(size_p)

	for k in xrange(size_p):
		if(values_for_prop[k] != undefined_value):
			prop.set_at(k,values_for_prop[k])
	return prop

max_stars_repo_name: hpgl/hpgl

max_issues_repo_name: hpgl/hpgl

max_forks_repo_name: hpgl/hpgl

reason: from numpy


text:
# Copyright 2018 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Model script to test TF-TensorRT integration."""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import numpy as np

from tensorflow.contrib.tensorrt.test import tf_trt_integration_test_base as trt_test
from tensorflow.python.framework import constant_op
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import ops
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import gen_array_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import nn
from tensorflow.python.platform import test

class BiasaddMatMulTest(trt_test.TfTrtIntegrationTestBase):

  def GetParams(self):
    """Testing conversion of BiasAdd MatMul in TF-TRT conversion."""
    dtype = dtypes.float32
    input_name = "input"
    input_dims = [48, 12]
    output_name = "output"
    g = ops.Graph()
    with g.as_default():
      x = array_ops.placeholder(dtype=dtype, shape=input_dims, name=input_name)

      b = constant_op.constant(np.random.randn(12, 4), dtype=dtype)
      x1 = math_ops.matmul(x, b)
      b = constant_op.constant(np.random.randn(1, 4), dtype=dtype)
      x1 = x1 + b

      b = constant_op.constant(np.random.randn(48, 4), dtype=dtype)
      x2 = math_ops.matmul(x, b, transpose_a=True)
      x2 = gen_array_ops.reshape(x2, [48, 1])

      b = constant_op.constant(np.random.randn(4, 12), dtype=dtype)
      x3 = math_ops.matmul(x, b, transpose_b=True)

      b = constant_op.constant(np.random.randn(16, 48), dtype=dtype)
      x4 = math_ops.matmul(x, b, transpose_b=True, transpose_a=True)
      x4 = gen_array_ops.reshape(x4, [48, 4])

      x5 = gen_array_ops.reshape(x, [4, 144])
      b = constant_op.constant(np.random.randn(144, 48), dtype=dtype)
      x5 = math_ops.matmul(x5, b)
      b = constant_op.constant(np.random.randn(48), dtype=dtype)
      x5 = nn.bias_add(x5, b)
      x5 = gen_array_ops.reshape(x5, [48, 4])

      x6 = gen_array_ops.reshape(x, [4, 12, 12])
      b = constant_op.constant(np.random.randn(12), dtype=dtype)
      x6 = nn.bias_add(x6, b, data_format="NHWC")
      x6 = gen_array_ops.reshape(x6, [48, -1])

      x7 = gen_array_ops.reshape(x, [4, 12, 3, 4])
      b = constant_op.constant(np.random.randn(4), dtype=dtype)
      x7 = nn.bias_add(x7, b, data_format="NHWC")
      x7 = gen_array_ops.reshape(x7, [48, -1])

      x8 = gen_array_ops.reshape(x, [4, 12, 3, 2, 2])
      b = constant_op.constant(np.random.randn(2), dtype=dtype)
      x8 = nn.bias_add(x8, b, data_format="NHWC")
      x8 = gen_array_ops.reshape(x8, [48, -1])

      x9 = gen_array_ops.reshape(x, [4, 12, 3, 2, 2])
      b = constant_op.constant(np.random.randn(3), dtype=dtype)
      x9 = nn.bias_add(x9, b, data_format="NCHW")
      x9 = gen_array_ops.reshape(x9, [48, -1])

      x10 = gen_array_ops.reshape(x, [4, 12, 3, 4])
      b = constant_op.constant(np.random.randn(12), dtype=dtype)
      x10 = nn.bias_add(x10, b, data_format="NCHW")
      x10 = gen_array_ops.reshape(x10, [48, -1])

      x11 = gen_array_ops.reshape(x, [4, 12, 12])
      b = constant_op.constant(np.random.randn(4), dtype=dtype)
      x11 = nn.bias_add(x11, b, data_format="NCHW")
      x11 = gen_array_ops.reshape(x11, [48, -1])

      out = array_ops.concat(
          [x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11], axis=-1)
      out = array_ops.squeeze(out, name=output_name)
    return trt_test.TfTrtIntegrationTestParams(
        gdef=g.as_graph_def(),
        input_names=[input_name],
        input_dims=[input_dims],
        output_names=[output_name],
        expected_output_dims=[(48, 89)])

  def GetConversionParams(self, run_params):
    """Return a ConversionParams for test."""
    return super(BiasaddMatMulTest,
                 self).GetConversionParams(run_params)._replace(
                     max_batch_size=48, maximum_cached_engines=2)

  def _ValidEngines(self):
    """Engines expected to build and run."""
    return [
        "my_trt_op_0", "my_trt_op_1", "my_trt_op_2", "my_trt_op_6",
        "my_trt_op_7", "my_trt_op_8", "my_trt_op_9"
    ]

  def _InvalidEngines(self):
    """Engines that will cause conversion error at building time."""
    return ["my_trt_op_3", "my_trt_op_4", "my_trt_op_5"]

  def ExpectedEnginesToBuild(self, run_params):
    """Return the expected engines to build."""
    # In dynamic engine mode the engines are built in execution time, not in
    # conversion time, so build errors occurs later. Here three of the engines
    # will be failed to built but the corresponding engine op are still created.
    # TODO(aaroey, jjsjann123): fix this.
    if (run_params.dynamic_engine and
        not trt_test.IsQuantizationMode(run_params.precision_mode)):
      return self._ValidEngines() + self._InvalidEngines()
    return self._ValidEngines()

  def ExpectedEnginesToRun(self, run_params):
    """Return the expected engines to run."""
    return self._ValidEngines()

  def ShouldRunTest(self, run_params):
    """Whether to run the test."""
    # TODO(aaroey): Trt 4.0 forbids conversion for tensors with rank <3 in int8
    # mode, which is a bug. Re-enable this when trt library is fixed.
    return not trt_test.IsQuantizationMode(run_params.precision_mode)

if __name__ == "__main__":
  test.main()

max_stars_repo_name: knightvishal/tensorflow

max_issues_repo_name: knightvishal/tensorflow

max_forks_repo_name: knightvishal/tensorflow


text:
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function
import collections
import copy
import six
import numpy as np
from ..framework import Block, Variable, in_dygraph_mode
from ..data_feeder import convert_dtype, check_variable_and_dtype, check_type, check_dtype
from ..layer_helper import LayerHelper
from sys import version_info

def convert_to_list(value, n, name, dtype=int):
    """
    Converts a single numerical type or iterable of numerical
    types into an numerical type list.

    Arguments:
      value: The value to validate and convert. Could an int, or any iterable
        of ints.
      n: The size of the list to be returned.
      name: The name of the argument being validated, e.g. "stride" or
        "filter_size". This is only used to format error messages.
      dtype: the numerical type of the element of the list to be returned.

    Returns:
      A list of n dtypes.

    Raises:
      ValueError: If something else than an int/long or iterable thereof was
        passed.
    """
    if isinstance(value, dtype):
        return [value, ] * n
    else:
        try:
            value_list = list(value)
        except TypeError:
            raise ValueError("The " + name +
                             "'s type must be list or tuple. Received: " + str(
                                 value))
        if len(value_list) != n:
            raise ValueError("The " + name + "'s length must be " + str(n) +
                             ". Received: " + str(value))
        for single_value in value_list:
            try:
                dtype(single_value)
            except (ValueError, TypeError):
                raise ValueError(
                    "The " + name + "'s type must be a list or tuple of " + str(
                        n) + " " + str(dtype) + " . Received: " + str(
                            value) + " "
                    "including element " + str(single_value) + " of type" + " "
                    + str(type(single_value)))
        return value_list

def is_sequence(seq):
    """
    Whether `seq` is an entry or nested structure
    """
    if isinstance(seq, dict):
        return True
    return (isinstance(seq, collections.Sequence) and
            not isinstance(seq, six.string_types))

def _sorted(dict_):
    """
    Returns a sorted list of the dict keys, with error if keys not sortable.
    """
    try:
        return sorted(six.iterkeys(dict_))
    except TypeError:
        raise TypeError("nest only supports dicts with sortable keys.")

def _yield_value(iterable):
    if isinstance(iterable, dict):
        # Iterate through dictionaries in a deterministic order by sorting the
        # keys. Notice this means that we ignore the original order of `OrderedDict`
        # instances. This is intentional, to avoid potential bugs caused by mixing
        # ordered and plain dicts (e.g., flattening a dict but using a
        # corresponding `OrderedDict` to pack it back).
        for key in _sorted(iterable):
            yield iterable[key]
    else:
        for value in iterable:
            yield value

def _yield_flat_nest(nest):
    for n in _yield_value(nest):
        if is_sequence(n):
            for ni in _yield_flat_nest(n):
                yield ni
        else:
            yield n

def flatten(nest):
    """
	:alias_main: paddle.flatten
	:alias: paddle.flatten,paddle.tensor.flatten,paddle.tensor.manipulation.flatten
	:old_api: paddle.fluid.layers.flatten

    Traverse all entries in the nested structure and put them into an list.
    """
    if is_sequence(nest):
        return list(_yield_flat_nest(nest))
    else:
        return [nest]

def _sequence_like(instance, args):
    """
    Convert the sequence `args` to the same type as `instance`.
    """
    if isinstance(instance, dict):
        # Pack dictionaries in a deterministic order by sorting the keys.
        # Notice this means that we ignore the original order of `OrderedDict`
        # instances. This is intentional, to avoid potential bugs caused by mixing
        # ordered and plain dicts (e.g., flattening a dict but using a
        # corresponding `OrderedDict` to pack it back).
        result = dict(zip(_sorted(instance), args))
        return type(instance)((key, result[key])
                              for key in six.iterkeys(instance))
    elif (isinstance(instance, tuple) and hasattr(instance, "_fields") and
          isinstance(instance._fields, collections.Sequence) and
          all(isinstance(f, six.string_types) for f in instance._fields)):
        # This is a namedtuple
        return type(instance)(*args)
    else:
        # Not a namedtuple
        return type(instance)(args)

def _packed_nest_with_indices(structure, flat, index):
    """
    Helper function for pack_sequence_as.
    """
    packed = []
    for s in _yield_value(structure):
        if is_sequence(s):
            new_index, child = _packed_nest_with_indices(s, flat, index)
            packed.append(_sequence_like(s, child))
            index = new_index
        else:
            packed.append(flat[index])
            index += 1
    return index, packed

def pack_sequence_as(structure, flat_sequence):
    """
    Pack a given flattened sequence into a given structure.
    """
    if not is_sequence(flat_sequence):
        raise TypeError("flat_sequence must be a sequence")
    if not is_sequence(structure):
        if len(flat_sequence) != 1:
            raise ValueError(
                "Structure is a scalar but len(flat_sequence) == %d > 1" %
                len(flat_sequence))
        return flat_sequence[0]
    flat_structure = flatten(structure)
    if len(flat_structure) != len(flat_sequence):
        raise ValueError(
            "Could not pack sequence. Structure had %d elements, but flat_sequence "
            "had %d elements.  Structure: %s, flat_sequence: %s." %
            (len(flat_structure), len(flat_sequence), structure, flat_sequence))
    _, packed = _packed_nest_with_indices(structure, flat_sequence, 0)
    return _sequence_like(structure, packed)

def map_structure(func, *structure):
    """
    Apply `func` to each entry in `structure` and return a new structure.
    """
    flat_structure = [flatten(s) for s in structure]
    entries = zip(*flat_structure)
    return pack_sequence_as(structure[0], [func(*x) for x in entries])

def hold_mutable_vars(structure):
    """
    Returns whether structure holds sequence like `list/dict`.
    """
    for s in structure:
        if is_sequence(s):
            return True
    return False

def copy_mutable_vars(structure):
    """
    Returns vars copied from sequence without mutable property.
    """
    flat_structure = copy.copy(flatten(structure))
    return pack_sequence_as(structure, flat_structure)

def _recursive_assert_same_structure(nest1, nest2, check_types):
    """
    Helper function for `assert_same_structure`.
    """
    is_sequence_nest1 = is_sequence(nest1)
    if is_sequence_nest1 != is_sequence(nest2):
        raise ValueError(
            "The two structures don't have the same nested structure.\n\n"
            "First structure: %s\n\nSecond structure: %s." % (nest1, nest2))
    if not is_sequence_nest1:
        return  # finished checking
    if check_types:
        type_nest1 = type(nest1)
        type_nest2 = type(nest2)
        if type_nest1 != type_nest2:
            raise TypeError(
                "The two structures don't have the same sequence type. First "
                "structure has type %s, while second structure has type %s." %
                (type_nest1, type_nest2))
        if isinstance(nest1, dict):
            keys1 = set(six.iterkeys(nest1))
            keys2 = set(six.iterkeys(nest2))
            if keys1 != keys2:
                raise ValueError(
                    "The two dictionaries don't have the same set of keys. First "
                    "structure has keys {}, while second structure has keys {}."
                    .format(keys1, keys2))
    nest1_as_sequence = [n for n in _yield_value(nest1)]
    nest2_as_sequence = [n for n in _yield_value(nest2)]
    for n1, n2 in zip(nest1_as_sequence, nest2_as_sequence):
        _recursive_assert_same_structure(n1, n2, check_types)

def assert_same_structure(nest1, nest2, check_types=True):
    """
    Confirm two nested structures with the same structure.
    """
    len_nest1 = len(flatten(nest1)) if is_sequence(nest1) else 1
    len_nest2 = len(flatten(nest2)) if is_sequence(nest2) else 1
    if len_nest1 != len_nest2:
        raise ValueError("The two structures don't have the same number of "
                         "elements.\n\nFirst structure (%i elements): %s\n\n"
                         "Second structure (%i elements): %s" %
                         (len_nest1, nest1, len_nest2, nest2))
    _recursive_assert_same_structure(nest1, nest2, check_types)

def _is_symmetric_padding(padding, data_dim):
    """
    Check whether padding is symmetrical.
    """
    assert len(padding) == data_dim * 2 or len(padding) == data_dim
    is_sys = True
    if len(padding) == data_dim * 2:
        for i in range(data_dim):
            if padding[i * 2] != padding[i * 2 + 1]:
                is_sys = False
    return is_sys

def _contain_var(list_or_tuple):
    """
    Check whether list or tuple contains variable.
    """
    for item in list_or_tuple:
        if isinstance(item, Variable):
            return True
    return False

def get_shape_tensor_inputs(inputs, attrs, shape, op_type):
    from .tensor import fill_constant, cast

    def _get_attr_shape(list_shape):
        attr_shape = []
        for idx, dim in enumerate(list_shape):
            if isinstance(dim, Variable):
                attr_shape.append(-1)
            else:
                attr_shape.append(dim)
        return attr_shape

    def _get_shape_tensor(list_shape):
        shape_tensor_list = []
        for idx, dim in enumerate(list_shape):
            if isinstance(dim, Variable):
                dim.stop_gradient = True
                check_dtype(
                    dim.dtype, 'shape[' + str(idx) + ']', ['int32', 'int64'],
                    op_type,
                    '(When type of shape in' + op_type + 'is list or tuple.)')
                if convert_dtype(dim.dtype) == 'int64':
                    dim = cast(x=dim, dtype='int32')
                shape_tensor_list.append(dim)
            else:
                temp_out = fill_constant([1], 'int32', dim, force_cpu=True)
                shape_tensor_list.append(temp_out)
        return shape_tensor_list

    if isinstance(shape, Variable):
        shape.stop_gradient = True
        check_dtype(shape.dtype, 'shape', ['int32', 'int64'], 'fill_constant',
                    '(When type of shape in' + op_type + ' is Variable.)')
        if (convert_dtype(shape.dtype) == 'int64'):
            shape = cast(shape, 'int32')
        inputs["ShapeTensor"] = shape
    elif isinstance(shape, (list, tuple)):
        assert len(shape) > 0, (
            "The size of 'shape' in" + op_type + " can't be zero, "
            "but received %s." % len(shape))
        attrs["shape"] = _get_attr_shape(shape)
        if _contain_var(shape):
            inputs['ShapeTensorList'] = _get_shape_tensor(shape)
    else:
        raise TypeError("Shape only supports Variable, or list, or tuple.")

def _convert_to_tensor_list(old_list, dtype="int32"):
    """
    Converts all elements of a list to Variable.
    """
    from .tensor import fill_constant
    new_list_tensor = []
    for ele in old_list:

        if isinstance(ele, Variable):
            ele.stop_gradient = True
            new_list_tensor.append(ele)
        else:
            assert isinstance(ele, six.integer_types)
            temp_out = fill_constant([1], dtype, ele, force_cpu=True)
            new_list_tensor.append(temp_out)
    return new_list_tensor

def convert_shape_to_list(shape):
    """
    Convert shape(list, tuple, variable) to list in imperative mode
    """
    if isinstance(shape, (list, tuple)):
        shape = list(
            map(lambda x: x.numpy()[0] if isinstance(x, Variable) else x,
                shape))
    else:
        shape = shape.numpy().astype(int).tolist()
    return shape

def check_shape(shape):
    """
    Check shape type and shape elements type before passing it to fill_constant
    """
    if isinstance(shape, Variable):
        check_dtype(shape.dtype, 'shape', ['int32', 'int64'], 'fill_constant')
    else:
        for ele in shape:
            if not isinstance(ele, Variable):
                if ele < 0:
                    raise ValueError(
                        "All elements in ``shape`` must be positive when it's a list or tuple"
                    )
                if not isinstance(ele, six.integer_types):
                    raise TypeError(
                        "All elements in ``shape`` must be integers when it's a list or tuple"
                    )

def try_set_static_shape_tensor(tensor, shape):
    """Try to set static shape of tensor from a shape tensor.

    For example,

    import paddle
    paddle.enable_static()
    data = paddle.static.data(name="x", shape=[-1, 2], dtype='float32')
    shape = paddle.shape(data)  # shape should be [-1, 2] instead of [-1, -1]
    x = paddle.uniform(shape)
    print(x.shape)
    # (-1, 2)

    """
    if not in_dygraph_mode():
        # static mode, and shape is not all inferred (contains -1)
        if -1 in tensor.shape:
            if isinstance(shape, Variable):
                shape = try_get_constant_shape_from_tensor(shape)
                if shape:
                    tensor.desc.set_shape(shape)

def try_get_constant_shape_from_tensor(shape_tensor):
    """Try to get shape from a tensor with constant value.

    For example,

    import paddle
    paddle.enable_static()
    data = paddle.static.data(name="x", shape=[-1, 2], dtype='float32')
    shape = paddle.shape(data)  # shape should be [-1, 2] instead of [-1, -1]
    x = paddle.uniform(shape)
    print(x.shape)
    # (-1, 2)

    """
    if not in_dygraph_mode():
        try:
            if shape_tensor.op is not None:
                generate_op = shape_tensor.op
                if generate_op.type == 'shape':
                    var = shape_tensor.block.vars[generate_op.input_arg_names[
                        0]]
                    return var.shape
        except:
            return None

        return None

def get_inputs_outputs_in_block(block):
    """
    Returns the inputs and outputs variable used in this block but not
    created in this block.
    """
    assert isinstance(
        block,
        Block), "input non-Block argument for get_inputs_outputs_in_block."
    assert block.parent_idx != -1, "input block should be a sub-block, not main block."

    # Find input/output var names of all ops in block
    inner_inputs = set()
    inner_outputs = set()
    for op in block.ops:
        for iname in op.input_names:
            for in_var_name in op.input(iname):
                if not block.has_var(in_var_name):
                    # variable not created in this block
                    inner_inputs.add(in_var_name)
        for oname in op.output_names:
            for out_var_name in op.output(oname):
                if not block.has_var(out_var_name):
                    # variable not created in this block
                    inner_outputs.add(out_var_name)

    return inner_inputs, inner_outputs

max_stars_repo_name: xxworkspace/Paddle

max_issues_repo_name: xxworkspace/Paddle

max_forks_repo_name: xxworkspace/Paddle


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

plt.style.use("dark_background")
f = lambda t,x : np.sin(x*t)

t,x = np.linspace(-5,5,10),np.linspace(-5,5,10)
T,X = np.meshgrid(t,x)
f_i = 1/np.sqrt(1+f(T,X)**2)
f_j = f(t,x)/np.sqrt(1+f(T,X)**2)
fig,ax = plt.subplots(1,1,figsize=(5,5))
plt.quiver(T,X,T/(T**2+X**2+1),X/(T**2+X**2+1),color = "#f23333")
plt.savefig("/home/planck/Home2.png")
plt.show()