text:
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import LSTM, Dense, Dropout, Bidirectional, BatchNormalization, Embedding, SimpleRNN, GRU,SpatialDropout1D
from tensorflow.keras.layers import GlobalMaxPooling1D, Flatten, Masking, GaussianNoise, concatenate, Embedding, TimeDistributed
from tensorflow.keras.utils import to_categorical,plot_model
from tensorflow.keras.backend import clear_session
from tensorflow.keras.callbacks import ModelCheckpoint
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import load_model
from tensorflow.keras import Input, Model
from sklearn.model_selection import KFold
from sklearn import metrics
import numpy as np
import pickle
import matplotlib.pyplot as plt
import copy
import random
import tensorflow as tf
from tensorflow.keras.preprocessing import sequence
import kerastuner as kt
from kerastuner import HyperModel
# from tensorflow.keras.callbacks import TensorBoard
# from tensorflow.compat.v1 import InteractiveSession
# config = tf.compat.v1.ConfigProto()
#config.gpu_options.per_process_gpu_memory_fraction = 0.9
# config.gpu_options.allow_growth = True
# session = InteractiveSession(config=config)
# fix random seed for reproducibility
np.random.seed(21)
from time import process_time
# from livelossplot.tf_keras import PlotLossesCallback
checkpoint = ModelCheckpoint("best_model.h5", monitor='val_accuracy', verbose=1, save_best_only=True, mode='max')
# callbacks_list = [PlotLossesCallback(), checkpoint]

callbacks_list = [checkpoint]
loc_val_2dpickle="../input/endgamepickles3/val_set_2d_pickle"
loc_val_3dpickle="../input/endgamepickles3/val_set_3d_pickle"
loc_val_labels="../input/endgamepickles3/val_set_label_pickle"

gamma=2
alpha=.25
def focal_loss_fixed(y_true, y_pred):
    yc = tf.clip_by_value(y_pred,1e-15,1-1e-15)
    pt_1 = tf.where(tf.equal(y_true, 1), yc, tf.ones_like(yc))
    pt_0 = tf.where(tf.equal(y_true, 0), yc, tf.zeros_like(yc))
    return (-tf.keras.backend.sum(alpha * tf.keras.backend.pow(1. - pt_1, gamma) * tf.keras.backend.log(pt_1))-tf.keras.backend.sum((1-alpha) * tf.keras.backend.pow( pt_0, gamma) * tf.keras.backend.log(1. - pt_0)))

model_dense1 = load_model("best_2dsubm_1.h5")
model_dense2 = load_model("best_2dsubm_2.h5")
model_gru1 = load_model("best_3dsubm_1.h5",custom_objects={'focal_loss_fixed': focal_loss_fixed})
model_gru2 = load_model("best_3dsubm_2.h5")

def define_stacked_model(members):
	# update all layers in all models to not be trainable
	for i in range(len(members)):
		model = members[i]
		for layer in model.layers:
			# make not trainable
			layer.trainable = False
			# rename to avoid 'unique layer name' issue
			layer._name = 'ensemble_' + str(i+1) + '_' + layer.name
	# define multi-headed input
	ensemble_visible = [model.input for model in members]
	# concatenate merge output from each model
	ensemble_outputs = [model.output for model in members]
	merge = concatenate(ensemble_outputs)
	hidden = Dense(10, activation='relu')(merge)
	output = Dense(15, activation='softmax')(hidden)
	model = Model(inputs=ensemble_visible, outputs=output)
	# plot graph of ensemble
	plot_model(model, show_shapes=True, to_file='model_graph.png')
	# compile
	model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
	return model

# define ensemble model
members = [model_dense1,model_dense2,model_gru1,model_gru2]            #CHANGE
stacked_model = define_stacked_model(members)

with open(loc_val_3dpickle, "rb") as fp:   # Unpickling
    val_arr_3d=pickle.load(fp)

with open(loc_val_2dpickle, "rb") as fp:   # Unpickling
    val_arr_2d=pickle.load(fp)

with open(loc_val_labels, "rb") as fp:   # Unpickling
    val_labels=pickle.load(fp)

max_length = 162
num_classes = 15

val_inputs3d = sequence.pad_sequences(val_arr_3d, maxlen=max_length,padding='post',dtype='float32')
del(val_arr_3d)
val_inputs2d = np.array(val_arr_2d)
del(val_arr_2d)
val_targets = to_categorical(val_labels, num_classes=15)
del(val_labels)
val_inputs3d = val_inputs3d.astype(np.float32)
val_inputs2d = val_inputs2d.astype(np.float32)
val_targets = val_targets.astype(np.int32)

timesteps,data_dim = (162, 13)
dim_2d_feats=156
tot_samples = 7848

history = stacked_model.fit([val_inputs2d,val_inputs2d,val_inputs3d,val_inputs3d], val_targets, epochs=50, verbose=1)                  #CHANGE

stacked_model.save("fourstacked_model.h5")

def plot_model_change(history):
    # summarize history for accuracy
    plt.plot(history.history['accuracy'])
    plt.title('model accuracy')
    plt.ylabel('accuracy')
    plt.xlabel('epoch')
    plt.show()
    # summarize history for loss
    plt.plot(history.history['loss'])
    plt.title('model loss')
    plt.ylabel('loss')
    plt.xlabel('epoch')
    plt.show()

plot_model_change(history)

preds = stacked_model.predict([test_inputs2d,test_inputs2d,test_inputs3d,test_inputs3d],verbose=True)                 #CHANGE
y_preds = preds.argmax(axis=1)
y_preds_2 = preds
y_true = test_targets.argmax(axis=1)

print(metrics.accuracy_score(y_true, y_preds))

def multi_weighted_logloss(y_true, y_preds):
    """
    @author olivier https://www.kaggle.com/ogrellier
    multi logloss for PLAsTiCC challenge
    """
    # class_weights taken from Giba's topic : https://www.kaggle.com/titericz
    # https://www.kaggle.com/c/PLAsTiCC-2018/discussion/67194
    # with Kyle Boone's post https://www.kaggle.com/kyleboone
    classes = [6, 15, 16, 42, 52, 53, 62, 64, 65, 67, 88, 90, 92, 95]
    class_weight = {6: 1, 15: 2, 16: 1, 42: 1, 52: 1, 53: 1, 62: 1, 64: 2, 65: 1, 67: 1, 88: 1, 90: 1, 92: 1, 95: 1}
    if len(np.unique(y_true)) > 14:
        classes.append(99)
        class_weight[99] = 2
    y_p = y_preds
    # Trasform y_true in dummies
    y_ohe = pd.get_dummies(y_true)
    # Normalize rows and limit y_preds to 1e-15, 1-1e-15
    y_p = np.clip(a=y_p, a_min=1e-15, a_max=1 - 1e-15)
    # Transform to log
    y_p_log = np.log(y_p)
    # Get the log for ones, .values is used to drop the index of DataFrames
    # Exclude class 99 for now, since there is no class99 in the training set
    # we gave a special process for that class
    y_log_ones = np.sum(y_ohe.values * y_p_log, axis=0)
    # Get the number of positives for each class
    nb_pos = y_ohe.sum(axis=0).values.astype(float)
    # Weight average and divide by the number of positives
    class_arr = np.array([class_weight[k] for k in sorted(class_weight.keys())])
    y_w = y_log_ones * class_arr / nb_pos

    loss = - np.sum(y_w) / np.sum(class_arr)
    return loss

import pandas as pd
print('MULTI WEIGHTED LOG LOSS : %.5f ' % multi_weighted_logloss(y_true, y_preds_2))


text:
import numpy as np
import torch
from torch import nn as nn
from torch.nn import functional as F

from rlkit.pythonplusplus import identity
from rlkit.torch import pytorch_util as ptu
from rlkit.torch.core import PyTorchModule

class FeatPointMlp(PyTorchModule):
    def __init__(
        self,
        downsample_size,
        input_channels,
        num_feat_points,
        temperature=1.0,
        init_w=1e-3,
        input_size=32,
        hidden_init=ptu.fanin_init,
        output_activation=identity,
    ):
        super().__init__()

        self.downsample_size = downsample_size
        self.temperature = temperature
        self.num_feat_points = num_feat_points
        self.hidden_init = hidden_init
        self.output_activation = output_activation
        self.input_channels = input_channels
        self.input_size = input_size

        #        self.bn1 = nn.BatchNorm2d(1)
        self.conv1 = nn.Conv2d(input_channels, 48, kernel_size=5, stride=2)
        #        self.bn1 = nn.BatchNorm2d(16)
        self.conv2 = nn.Conv2d(48, 48, kernel_size=5, stride=1)
        self.conv3 = nn.Conv2d(48, self.num_feat_points, kernel_size=5, stride=1)

        test_mat = ptu.zeros(1, self.input_channels, self.input_size, self.input_size)
        test_mat = self.conv1(test_mat)
        test_mat = self.conv2(test_mat)
        test_mat = self.conv3(test_mat)
        self.out_size = int(np.prod(test_mat.shape))
        self.fc1 = nn.Linear(2 * self.num_feat_points, 400)
        self.fc2 = nn.Linear(400, 300)
        self.last_fc = nn.Linear(
            300, self.input_channels * self.downsample_size * self.downsample_size
        )

        self.init_weights(init_w)
        self.i = 0

    def init_weights(self, init_w):
        self.hidden_init(self.conv1.weight)
        self.conv1.bias.data.fill_(0)
        self.hidden_init(self.conv2.weight)
        self.conv2.bias.data.fill_(0)

    def forward(self, input):
        h = self.encoder(input)
        out = self.decoder(h)
        return out

    def encoder(self, input):
        x = input.contiguous().view(
            -1, self.input_channels, self.input_size, self.input_size
        )
        x = F.relu(self.conv1(x))
        x = F.relu(self.conv2(x))
        x = self.conv3(x)
        d = int((self.out_size // self.num_feat_points) ** (1 / 2))
        x = x.view(-1, self.num_feat_points, d * d)
        x = F.softmax(x / self.temperature, 2)
        x = x.view(-1, self.num_feat_points, d, d)

        maps_x = torch.sum(x, 2)
        maps_y = torch.sum(x, 3)

        weights = ptu.from_numpy(np.arange(d) / (d + 1))

        fp_x = torch.sum(maps_x * weights, 2)
        fp_y = torch.sum(maps_y * weights, 2)

        x = torch.cat([fp_x, fp_y], 1)
        #        h = x.view(-1, 2, self.num_feat_points).transpose(1, 2).contiguous().view(-1, self.num_feat_points * 2)
        h = x.view(-1, self.num_feat_points * 2)
        return h

    def decoder(self, input):
        h = input
        h = F.relu(self.fc1(h))
        h = F.relu(self.fc2(h))
        h = self.last_fc(h)
        return h

    def history_encoder(self, input, history_length):
        input = input.contiguous().view(
            -1, self.input_channels, self.input_size, self.input_size
        )
        latent = self.encoder(input)

        assert latent.shape[0] % history_length == 0
        n_samples = latent.shape[0] // history_length
        latent = latent.view(n_samples, -1)
        return latent

ext: py

max_stars_repo_name: mihdalal/raps

max_issues_repo_name: mihdalal/raps

max_forks_repo_name: mihdalal/raps

reason: import numpy


text:
import sys, os, time
import numpy as np
import networkx as nx
import json
# Add the parent folder path to the sys.path list for importing
sys.path.insert(1, os.path.join(sys.path[0], '..'))
# Import tools
from util import timestamp, memory_usage, sparse_to_dense

def calc_degree( G, normalized = True ):
  # Calculates the degree centrality, non-normalized
  degree = nx.degree_centrality( G )
  if not normalized:
    degree = { node:value * ( G.number_of_nodes() - 1 ) for node, value in degree.items() }
  #end
  return degree
#end calc_degree

def calc_betweenness( G, normalized = True ):
  # Calculates the betweenness centrality, non-normalized
  return nx.betweenness_centrality( G, normalized = normalized )
#end calc_betweenness

def calc_closeness( G, normalized = True ):
  # Calculates the closeness centrality
  closeness = { node:nx.closeness_centrality( G, node ) for node in G }
  if not normalized:
    g_n = len( G )
    closeness = { node: value * ( G.number_of_nodes() - 1 ) / ( len( nx.node_connected_component( G, node ) ) - 1 ) if 0 < len( nx.node_connected_component( G, node ) ) else 0 for node,value in closeness.items() }
  #end if
  return closeness
#end calc_closeness

def calc_eigenvector( G ):
  # Calculates the eigenvector centrality
  return nx.eigenvector_centrality( G )
#end calc_eigenvector

def create_graph( g_n, erdos_renyi_p = 0.25, powerlaw_gamma = 3, smallworld_k = 4, smallworld_p = 0.25, powerlaw_cluster_m = 3, powerlaw_cluster_p = 0.1 ):
  # Tries to create a graph until all of its centralities can be computed
  Gs = None
  while Gs is None:
    Gs = _create_graph( g_n, erdos_renyi_p, powerlaw_gamma, smallworld_k, smallworld_p, powerlaw_cluster_m, powerlaw_cluster_p )
  #end while
  return Gs
#end create_graph

def _create_graph( g_n, erdos_renyi_p = 0.25, powerlaw_gamma = 3, smallworld_k = 4, smallworld_p = 0.25, powerlaw_cluster_m = 3, powerlaw_cluster_p = 0.1 ):
  # Tries to create a graph and returns None if it fails at any point.
  # Create a graph from a random distribution
  graph_type = np.random.randint( 0, 4 )
  if graph_type == 0:
    G = nx.fast_gnp_random_graph( g_n, erdos_renyi_p )
  elif graph_type == 1:
    try:
      G = nx.random_graphs.random_powerlaw_tree( g_n, powerlaw_gamma )
    except nx.NetworkXError as e:
      print( e, file = sys.stderr, flush = True )
      return None
    #end try
  elif graph_type == 2:
    try:
      G = nx.random_graphs.connected_watts_strogatz_graph( g_n, smallworld_k, smallworld_p )
    except nx.NetworkXError as e:
      print( e, file = sys.stderr, flush = True )
      return None
    #end try
  elif graph_type == 3:
    try:
      G = nx.random_graphs.powerlaw_cluster_graph( g_n, powerlaw_cluster_m, powerlaw_cluster_p )
    except nx.NetworkXError as e:
      print( e, file = sys.stderr, flush = True )
      return None
    #end try
  #end if
  for s, t in G.edges:
    G[s][t]["weight"] = np.random.rand()
  #end for
  # Define a random target
  T = np.random.randint( 0, g_n )
  # Calculate centrality measures
  try:
    eigenvector = calc_eigenvector( G, T, g_n )
  except nx.exception.PowerIterationFailedConvergence as e:
    print( e, file = sys.stderr, flush = True )
    return None
  #end try
  degree = calc_degree( G, T, g_n )
  betweenness = calc_betweenness( G, T, g_n )
  closeness = calc_closeness( G, T, g_n )
  # Build matrices
  # TODO: Get values from the edges
  M_index, M_values = build_M_from_graph( G, key = "weight" )
  M = [M_index,M_values,(g_n,g_n)]
  return M,T,degree,betweenness,closeness,eigenvector
#end _create_graph

def create_dataset(
  instances,
  min_n        = 32,
  max_n        = 128,
  path         = "instances/erdos",
  graph_type   = "erdos_renyi",
  graph_args   = list(),
  graph_kwargs = dict()
):
  i = 0
  while i < instances:
    g_n = np.random.randint( min_n, max_n )

    if graph_type == "erdos_renyi":
      G = nx.fast_gnp_random_graph( g_n, *graph_args, **graph_kwargs )
    elif graph_type == "powerlaw_tree":
      try:
        G = nx.random_graphs.random_powerlaw_tree( g_n, *graph_args, **graph_kwargs )
      except nx.NetworkXError as e:
        print( e, file = sys.stderr, flush = True )
        continue
      #end try
    elif graph_type == "watts_strogatz":
      try:
        G = nx.random_graphs.connected_watts_strogatz_graph( g_n, *graph_args, **graph_kwargs )
      except nx.NetworkXError as e:
        print( e, file = sys.stderr, flush = True )
        continue
      #end try
    elif graph_type == "powerlaw_cluster":
      try:
        G = nx.random_graphs.powerlaw_cluster_graph( g_n, *graph_args, **graph_kwargs )
      except nx.NetworkXError as e:
        print( e, file = sys.stderr, flush = True )
        continue
      #end try
    else:
      raise InvalidArgumentError( "Graph type not supported" )
    #end if

    if len( nx.node_connected_component( G, 0 ) ) != g_n:
      # No disjoint subgraphs allowed
      continue
    #end

    degree_normalized = calc_degree( G, normalized = True )
    degree_not_normalized = calc_degree( G, normalized = False )
    betweenness_normalized = calc_betweenness( G, normalized = True )
    betweenness_not_normalized = calc_betweenness( G, normalized = False )
    closeness_normalized = calc_closeness( G, normalized = True )
    closeness_not_normalized = calc_closeness( G, normalized = False )
    try:
      eigenvector = calc_eigenvector( G )
    except nx.exception.PowerIterationFailedConvergence as e:
      continue
    #end

    for node in G:
      G.nodes[node]["d_n"] = degree_normalized[ node ]
      G.nodes[node]["d_p"] = degree_not_normalized[ node ]
      G.nodes[node]["b_n"] = betweenness_normalized[ node ]
      G.nodes[node]["b_p"] = betweenness_not_normalized[ node ]
      G.nodes[node]["c_n"] = closeness_normalized[ node ]
      G.nodes[node]["c_p"] = closeness_not_normalized[ node ]
      G.nodes[node]["e_n"] = eigenvector[ node ]
    #end for

    degree_rank = sorted( [ (centrality,node) for node,centrality in degree_normalized.items() ], key = lambda x: x[0], reverse = True )
    betweenness_rank = sorted( [ (centrality,node) for node,centrality in betweenness_normalized.items() ], key = lambda x: x[0], reverse = True )
    closeness_rank = sorted( [ (centrality,node) for node,centrality in closeness_normalized.items() ], key = lambda x: x[0], reverse = True )
    eigenvector_rank = sorted( [ (centrality,node) for node,centrality in eigenvector.items() ], key = lambda x: x[0], reverse = True )

    rank = 0
    while rank < G.number_of_nodes():
      _, node = degree_rank[ rank ]
      G.nodes[node]["d_r"] = rank
      _, node = betweenness_rank[ rank ]
      G.nodes[node]["b_r"] = rank
      _, node = closeness_rank[ rank ]
      G.nodes[node]["c_r"] = rank
      _, node = eigenvector_rank[ rank ]
      G.nodes[node]["e_r"] = rank
      rank += 1
    #end while

    data = nx.readwrite.json_graph.node_link_data( G )
    s = json.dumps( data )
    with open(
      "{path}/{i}.g".format(
        path=path,
        i=i
      ),
      mode = "w"

    ) as f:
      f.write(s)
    #end with open f

    i += 1
  #end while
#end create_dataset

if __name__ == "__main__":
  n_instances = int( sys.argv[1] )
  # Erdos Renyi dataset
  os.makedirs( "instances/erdos", exist_ok = True )
  create_dataset(
    instances = n_instances,
    min_n        = 32,
    max_n        = 128,
    path         = "instances/erdos",
    graph_type   = "erdos_renyi",
    graph_args   = [0.25],
    graph_kwargs = dict()
  )
  # Random Powerlaw Tree dataset
  os.makedirs( "instances/pltree", exist_ok = True )
  create_dataset(
    instances = n_instances,
    min_n        = 32,
    max_n        = 128,
    path         = "instances/pltree",
    graph_type   = "powerlaw_tree",
    graph_args   = [3],
    graph_kwargs = dict()
  )
  # Random Watts Strogatz Smallworld dataset
  os.makedirs( "instances/smallworld", exist_ok = True )
  create_dataset(
    instances = n_instances,
    min_n        = 32,
    max_n        = 128,
    path         = "instances/smallworld",
    graph_type   = "watts_strogatz",
    graph_args   = [4,0.25],
    graph_kwargs = dict()
  )
  # Random Powerlaw Tree dataset
  os.makedirs( "instances/plcluster", exist_ok = True )
  create_dataset(
    instances = n_instances,
    min_n        = 32,
    max_n        = 128,
    path         = "instances/plcluster",
    graph_type   = "powerlaw_cluster",
    graph_args   = [3,0.1],
    graph_kwargs = dict()
  )

max_stars_repo_name: machine-reasoning-ufrgs/graph-nn

max_issues_repo_name: machine-reasoning-ufrgs/graph-nn

max_forks_repo_name: machine-reasoning-ufrgs/graph-nn

reason: import numpy,import networkx


text:
import datetime
from numbers import Number
from typing import List, Tuple

import numpy as np
import pyproj

# OSGB is also called "OSGB 1936 / British National Grid -- United
# Kingdom Ordnance Survey".  OSGB is used in many UK electricity
# system maps, and is used by the UK Met Office UKV model.  OSGB is a
# Transverse Mercator projection, using 'easting' and 'northing'
# coordinates which are in meters.  See https://epsg.io/27700
OSGB = 27700

# WGS84 is short for "World Geodetic System 1984", used in GPS. Uses
# latitude and longitude.
WGS84 = 4326
WGS84_CRS = f"EPSG:{WGS84}"

# Geographic bounds for various regions of interest, in order of min_lon, max_lon, min_lat, max_lat
GEOGRAPHIC_BOUNDS = {"UK": (-16, 45, 10, 62),
                    "RSS": (-64, 83, 16, 69)}

class Transformers:
    """
    Class to store transformation from one Grid to another.

    Its good to make this only once, but need the
    option of updating them, due to out of data grids.
    """

    def __init__(self):
        """Init"""
        self._lat_lon_to_osgb = None
        self.make_transformers()

    def make_transformers(self):
        """
        Make transformers

         Nice to only make these once, as it makes calling the functions below quicker
        """
        self._lat_lon_to_osgb = pyproj.Transformer.from_crs(crs_from=WGS84, crs_to=OSGB)

    @property
    def lat_lon_to_osgb(self):
        """lat-lon to OSGB property"""
        return self._lat_lon_to_osgb

# make the transformers
transformers = Transformers()

def download_grids():
    """The transformer grid sometimes need updating"""
    pyproj.transformer.TransformerGroup(crs_from=WGS84, crs_to=OSGB).download_grids(verbose=True)

    transformers.make_transformers()

def lat_lon_to_osgb(lat: List[Number], lon: List[Number]) -> Tuple[np.ndarray, np.ndarray]:
    """
    Change lat, lon to a OSGB coordinates

    Args:
        lat: latitude
        lon: longitude

    Return: 2-tuple of x (east-west), y (north-south).

    """
    return transformers.lat_lon_to_osgb.transform(lat, lon)

max_stars_repo_name: openclimatefix/Satip

max_issues_repo_name: openclimatefix/Satip

max_forks_repo_name: openclimatefix/Satip


text:
import pygame
import numpy as np

def calcula_angulo(ms_pos,pl_pos,last_angle=0):
    pl_ms_dx = ms_pos[0]-pl_pos[0]
    pl_ms_dy = -ms_pos[1]+pl_pos[1]

    if pl_ms_dy == 0:
        if pl_ms_dx == 0:
            return last_angle
        elif pl_ms_dx > 0:
            return 0
        else:
            return 180
    elif pl_ms_dy > 0:
        if pl_ms_dx == 0:
            return 90
        elif pl_ms_dx > 0:
            return np.rad2deg(np.arctan(pl_ms_dy/pl_ms_dx))
        else:
            return 180+np.rad2deg(np.arctan(pl_ms_dy/pl_ms_dx))
    else:
        if pl_ms_dx == 0:
            return 270
        elif pl_ms_dx > 0:
            return 360+np.rad2deg(np.arctan(pl_ms_dy/pl_ms_dx))
        else:
            return 180+np.rad2deg(np.arctan(pl_ms_dy/pl_ms_dx))

#ref = https://stackoverflow.com/questions/4183208/how-do-i-rotate-an-image-around-its-center-using-pygame
def rotacionar(imagem,angulo):
    centro = imagem.get_rect().center
    n_imagem = pygame.transform.rotate(imagem,angulo)
    n_rect = n_imagem.get_rect(center = centro)
    return(n_imagem,n_rect)


text:
# Local Polynomial Approximation using Numba JIT Acceleration
#
# Calculates the coefficients of a 3D 2nd order polynomial approximation to a small window around
# each data sample using gaussian weighted least squares. This implementation uses the Numba JIT to
# accelerate the convolution.
#
# The approximation has the following form:
# r0 + r1*x + r2*y + r3*z + r4*x^2 + r5*y^2 +r6*z^2 + r7*x*y + r8*xz + r9*yz
# where x,y and z are relative to the analysis location, ie the analysis location has x=y=z=0.
#
import sys,os
import numpy as np
#
# Import the module with the I/O scaffolding of the External Attribute
#
sys.path.insert(0, os.path.join(sys.path[0], '..', '..'))
import extattrib as xa
import extlib as xl
#
# These are the attribute parameters
#
xa.params = {
	'Input': 'Input',
	'Output': ['r0', 'r1', 'r2', 'r3', 'r4', 'r5', 'r6', 'r7', 'r8', 'r9'],
	'ZSampMargin' : {'Value':[-1,1], 'Symmetric': True},
	'StepOut' : {'Value': [1,1]},
	'Par_0': {'Name': 'Weight Factor', 'Value': 0.2},
	'Help': 'http://waynegm.github.io/OpendTect-Plugin-Docs/External_Attributes/LPA_Attributes/'
}
#
# Define the compute function
#
def doCompute():
	zs = xa.params['ZSampMargin']['Value'][1] - xa.params['ZSampMargin']['Value'][0] + 1
	kernel = lpa3D_init(xa.SI['nrinl'], xa.SI['nrcrl'], zs, xa.params['Par_0']['Value'])
	while True:
		xa.doInput()
		for i in range(0,10):
			xa.Output['r'+str(i)] = xl.sconvolve(xa.Input,kernel[i])
		xa.doOutput()

#
# Find the LPA solution for a 2nd order polynomial in 3D
#
def lpa3D_init( xs, ys, zs, sigma=0.2 ):
	sx = sigma * (xs-1)
	sy = sigma * (ys-1)
	sz = sigma * (zs-1)
	hxs = (xs-1)/2
	hys = (ys-1)/2
	hzs = (zs-1)/2
	xtmp = np.linspace(-hxs,hxs,xs)
	ytmp = np.linspace(-hys,hys,ys)
	ztmp = np.linspace(-hzs,hzs,zs)
	xyz = np.meshgrid(xtmp,ytmp,ztmp, indexing='ij')
	x = xyz[0].flatten()
	y = xyz[1].flatten()
	z = xyz[2].flatten()
	w = np.exp(-(x**2/(2*sx**2) + y**2/(2*sy**2) + z**2/(2*sz**2)))
	W = np.diagflat(w)
	A = np.dstack((np.ones(x.size), x, y, z, x*x, y*y, z*z, x*y, x*z, y*z)).reshape((x.size,10))
	DB = np.linalg.inv(A.T.dot(W).dot(A)).dot(A.T).dot(W)
	return DB.reshape((10,xs,ys,zs))

#
# Assign the compute function to the attribute
#
xa.doCompute = doCompute
#
# Do it
#
xa.run(sys.argv[1:])

max_stars_repo_name: waynegm/OpendTect-External-Attributes

max_issues_repo_name: waynegm/OpendTect-External-Attributes

max_forks_repo_name: waynegm/OpendTect-External-Attributes


text:
import os
from functools import partial
import jax
from jax import random
import jax.numpy as jnp
import numpy as np
from PIL import Image

from nerf import clip_utils
from nerf import utils
from demo.src.config import NerfConfig
from demo.src.models import init_model

model, _ = init_model()

def render_predict_from_pose(state, theta, phi, radius):
    rng = random.PRNGKey(0)
    partial_render_fn = partial(render_pfn, state.optimizer.target)
    rays = _render_rays_from_pose(theta, phi, radius)
    pred_color, pred_disp, _ = render_image_for_inference(partial_render_fn, rays,
                                                          rng, False, chunk=NerfConfig.CHUNK)
    return pred_color, pred_disp

def render_image_for_inference(render_fn, rays, rng, normalize_disp, chunk=8192):
    """Render all the pixels of an image (in test mode).

    Args:
        render_fn: function, jit-ed render function.
        rays: a `Rays` namedtuple, the rays to be rendered.
        rng: jnp.ndarray, random number generator (used in training mode only).
        normalize_disp: bool, if true then normalize `disp` to [0, 1].
        chunk: int, the size of chunks to render sequentially.

    Returns:
        rgb: jnp.ndarray, rendered color image.
        disp: jnp.ndarray, rendered disparity image.
        acc: jnp.ndarray, rendered accumulated weights per pixel.
    """
    height, width = rays[0].shape[:2]
    num_rays = height * width
    rays = utils.namedtuple_map(lambda r: r.reshape((num_rays, -1)), rays)
    unused_rng, key_0, key_1 = jax.random.split(rng, 3)
    host_id = jax.host_id()
    results = []
    for i in range(0, num_rays, chunk):
        # pylint: disable=cell-var-from-loop
        chunk_rays = utils.namedtuple_map(lambda r: r[i:i + chunk], rays)
        chunk_size = chunk_rays[0].shape[0]
        rays_remaining = chunk_size % jax.device_count()
        if rays_remaining != 0:
            padding = jax.device_count() - rays_remaining
            chunk_rays = utils.namedtuple_map(
                lambda r: jnp.pad(r, ((0, padding), (0, 0)), mode="edge"), chunk_rays)
        else:
            padding = 0
        # After padding the number of chunk_rays is always divisible by
        # host_count.
        rays_per_host = chunk_rays[0].shape[0] // jax.process_count()
        start, stop = host_id * rays_per_host, (host_id + 1) * rays_per_host
        chunk_rays = utils.namedtuple_map(lambda r: utils.shard(r[start:stop]), chunk_rays)
        chunk_results = render_fn(key_0, key_1, chunk_rays)[-1]
        results.append([utils.unshard(x[0], padding) for x in chunk_results])
        # pylint: enable=cell-var-from-loop
    rgb, disp, acc = [jnp.concatenate(r, axis=0) for r in zip(*results)]
    # Normalize disp for visualization for ndc_rays in llff front-facing scenes.
    if normalize_disp:
        disp = (disp - disp.min()) / (disp.max() - disp.min())
    return (rgb.reshape((height, width, -1)), disp.reshape(
        (height, width, -1)), acc.reshape((height, width, -1)))

def predict_to_image(pred_out) -> Image:
    image_arr = np.array(np.clip(pred_out, 0., 1.) * 255.).astype(np.uint8)
    return Image.fromarray(image_arr)

def _render_rays_from_pose(theta, phi, radius):
    camtoworld = np.array(clip_utils.pose_spherical(radius, theta, phi))
    rays = _camtoworld_matrix_to_rays(camtoworld)
    return rays

def _camtoworld_matrix_to_rays(camtoworld):
    """ render one instance of rays given a camera to world matrix (4, 4) """
    pixel_center = 0.
    w, h = NerfConfig.W, NerfConfig.H
    focal, downsample = NerfConfig.FOCAL, NerfConfig.DOWNSAMPLE
    x, y = np.meshgrid(  # pylint: disable=unbalanced-tuple-unpacking
        np.arange(0, w, downsample, dtype=np.float32) + pixel_center,  # X-Axis (columns)
        np.arange(0, h, downsample, dtype=np.float32) + pixel_center,  # Y-Axis (rows)
        indexing="xy")
    camera_dirs = np.stack([(x - w * 0.5) / focal,
                            -(y - h * 0.5) / focal,
                            -np.ones_like(x)],
                           axis=-1)
    directions = (camera_dirs[..., None, :] * camtoworld[None, None, :3, :3]).sum(axis=-1)
    origins = np.broadcast_to(camtoworld[None, None, :3, -1], directions.shape)
    viewdirs = directions / np.linalg.norm(directions, axis=-1, keepdims=True)
    return utils.Rays(origins=origins, directions=directions, viewdirs=viewdirs)

def _render_fn(variables, key_0, key_1, rays):
    return jax.lax.all_gather(model.apply(
        variables, key_0, key_1, rays, False),
        axis_name="batch")

render_pfn = jax.pmap(_render_fn, in_axes=(None, None, None, 0),
                      donate_argnums=3, axis_name="batch")

reason: import numpy,import jax,from jax


text:
from __future__ import division
from builtins import object
from past.utils import old_div

import pytest
import numpy as np
try:
    import cupy as cp
    try:
        cp.cuda.Device(0).compute_capability
    except cp.cuda.runtime.CUDARuntimeError:
        pytest.skip("GPU device inaccessible", allow_module_level=True)
except ImportError:
    pytest.skip("cupy not installed", allow_module_level=True)

from sporco.cupy.admm import tvl2
import sporco.cupy.metric as sm

class TestSet01(object):

    def setup_method(self, method):
        cp.random.seed(12345)
        self.D = cp.random.randn(16, 15)
        self.Dc = cp.random.randn(16, 15) + 1j * cp.random.randn(16, 15)

    def test_01(self):
        lmbda = 3
        try:
            b = tvl2.TVL2Denoise(self.D, lmbda)
            b.solve()
        except Exception as e:
            print(e)
            assert 0

    def test_01cplx(self):
        lmbda = 3
        try:
            b = tvl2.TVL2Denoise(self.Dc, lmbda)
            b.solve()
        except Exception as e:
            print(e)
            assert 0

    def test_02(self):
        lmbda = 3
        try:
            b = tvl2.TVL2Deconv(cp.ones((1, 1)), self.D, lmbda)
            b.solve()
        except Exception as e:
            print(e)
            assert 0

    def test_02cplx(self):
        lmbda = 3
        try:
            b = tvl2.TVL2Deconv(cp.ones((1, 1)), self.Dc, lmbda)
            b.solve()
        except Exception as e:
            print(e)
            assert 0

    def test_03(self):
        lmbda = 3
        dt = cp.float16
        opt = tvl2.TVL2Denoise.Options(
            {'Verbose': False, 'MaxMainIter': 20, 'AutoRho':
             {'Enabled': True}, 'DataType': dt})
        b = tvl2.TVL2Denoise(self.D, lmbda, opt=opt)
        b.solve()
        assert b.X.dtype == dt
        assert b.Y.dtype == dt
        assert b.U.dtype == dt

    def test_04(self):
        lmbda = 3
        dt = cp.float32
        opt = tvl2.TVL2Denoise.Options(
            {'Verbose': False, 'MaxMainIter': 20, 'AutoRho':
             {'Enabled': True}, 'DataType': dt})
        b = tvl2.TVL2Denoise(self.D, lmbda, opt=opt)
        b.solve()
        assert b.X.dtype == dt
        assert b.Y.dtype == dt
        assert b.U.dtype == dt

    def test_05(self):
        lmbda = 3
        dt = cp.float64
        opt = tvl2.TVL2Denoise.Options(
            {'Verbose': False, 'MaxMainIter': 20, 'AutoRho':
             {'Enabled': True}, 'DataType': dt})
        b = tvl2.TVL2Denoise(self.D, lmbda, opt=opt)
        b.solve()
        assert b.X.dtype == dt
        assert b.Y.dtype == dt
        assert b.U.dtype == dt

    def test_06(self):
        lmbda = 3
        dt = cp.float32
        opt = tvl2.TVL2Deconv.Options(
            {'Verbose': False, 'MaxMainIter': 20, 'AutoRho':
             {'Enabled': True}, 'DataType': dt})
        b = tvl2.TVL2Deconv(cp.ones((1, )), self.D, lmbda, opt=opt)
        b.solve()
        assert b.X.dtype == dt
        assert b.Y.dtype == dt
        assert b.U.dtype == dt

    def test_07(self):
        lmbda = 3
        dt = cp.float64
        opt = tvl2.TVL2Deconv.Options(
            {'Verbose': False, 'MaxMainIter': 20, 'AutoRho':
             {'Enabled': True}, 'DataType': dt})
        b = tvl2.TVL2Deconv(cp.ones((1, )), self.D, lmbda, opt=opt)
        b.solve()
        assert b.X.dtype == dt
        assert b.Y.dtype == dt
        assert b.U.dtype == dt

class TestSet02(object):

    def setup_method(self, method):
        np.random.seed(12345)
        N = 64
        self.U = cp.ones((N, N))
        self.U[:, 0:(old_div(N, 2))] = -1
        self.V = 1e-1 * cp.asarray(np.random.randn(N, N))
        self.D = self.U + self.V

    def test_01(self):
        lmbda = 1e-1
        opt = tvl2.TVL2Denoise.Options(
            {'Verbose': False, 'gEvalY': False, 'MaxMainIter': 300,
             'rho': 75 * lmbda})
        b = tvl2.TVL2Denoise(self.D, lmbda, opt)
        X = b.solve()
        assert cp.abs(b.itstat[-1].ObjFun - 32.875710674129564) < 1e-3
        assert sm.mse(self.U, X) < 1e-3

    def test_02(self):
        lmbda = 1e-1
        opt = tvl2.TVL2Deconv.Options(
            {'Verbose': False, 'gEvalY': False, 'MaxMainIter': 250})
        b = tvl2.TVL2Deconv(cp.ones((1)), self.D, lmbda, opt)
        X = b.solve()
        assert cp.abs(b.itstat[-1].ObjFun - 45.45958573088) < 1e-3
        assert sm.mse(self.U, X) < 1e-3

class TestSet03(object):

    def setup_method(self, method):
        np.random.seed(12345)
        N = 32
        self.U = cp.ones((N, N, N))
        self.U[:, 0:(old_div(N, 2)), :] = -1
        self.V = 1e-1 * cp.asarray(np.random.randn(N, N, N))
        self.D = self.U + self.V

    def test_01(self):
        lmbda = 1e-1
        opt = tvl2.TVL2Denoise.Options(
            {'Verbose': False, 'gEvalY': False, 'MaxMainIter': 250,
             'rho': 10 * lmbda})
        b = tvl2.TVL2Denoise(self.D, lmbda, opt, axes=(0, 1))
        X = b.solve()
        assert cp.abs(b.itstat[-1].ObjFun - 363.0802047) < 1e-3
        assert sm.mse(self.U, X) < 1e-3

    def test_02(self):
        lmbda = 1e-1
        opt = tvl2.TVL2Deconv.Options(
            {'Verbose': False, 'gEvalY': False, 'MaxMainIter': 250})
        b = tvl2.TVL2Deconv(cp.ones((1)), self.D, lmbda, opt, axes=(0, 1))
        X = b.solve()
        assert cp.abs(b.itstat[-1].ObjFun - 564.1586542) < 1e-3
        assert sm.mse(self.U, X) < 1e-3

class TestSet04(object):

    def setup_method(self, method):
        np.random.seed(12345)
        N = 32
        self.U = cp.ones((N, N, N))
        self.U[:, 0:(old_div(N, 2)), :] = -1
        self.V = 1e-1 * cp.asarray(np.random.randn(N, N, N))
        self.D = self.U + self.V

    def test_01(self):
        lmbda = 1e-1
        opt = tvl2.TVL2Denoise.Options(
            {'Verbose': False, 'gEvalY': False, 'MaxMainIter': 250,
             'rho': 10 * lmbda})
        b = tvl2.TVL2Denoise(self.D, lmbda, opt, axes=(0, 1, 2))
        X = b.solve()
        assert cp.abs(b.itstat[-1].ObjFun - 366.04267554965134) < 1e-3
        assert sm.mse(self.U, X) < 1e-3

    def test_02(self):
        lmbda = 1e-1
        opt = tvl2.TVL2Deconv.Options(
            {'Verbose': False, 'gEvalY': False, 'MaxMainIter': 250})
        b = tvl2.TVL2Deconv(cp.ones((1)), self.D, lmbda, opt, axes=(0, 1, 2))
        X = b.solve()
        assert cp.abs(b.itstat[-1].ObjFun - 567.72425227) < 1e-3
        assert sm.mse(self.U, X) < 1e-3

max_stars_repo_name: vishalbelsare/sporco

max_issues_repo_name: vishalbelsare/sporco

max_forks_repo_name: vishalbelsare/sporco

reason: import numpy,import cupy


text:
import pickle
import numpy as np
from utils import *
from sklearn.svm import SVC
from sklearn.metrics import classification_report, confusion_matrix, accuracy_score

import optunity
import optunity.metrics
"""
1/19/2018   using 40 features, default SVC (w/ RBF kernel) can generate D about 4
            after adding D tuning, when choose prob_cutoff as 0.28134, D can be 5.15

"""

label_back = lambda x: 'correct' if x==1 else 'incorrect'

def get_D_based_thresholds(LANG_T, lang_prob, y, print=False):
    scores = init_scores()
    for i, p_lang in enumerate(lang_prob):
        if p_lang[1] >= LANG_T:
            decision = 'accept'
        else:
            decision = 'reject'
        score_decision(decision, label_back(get_langauge_y(y)[i]), label_back(get_meaning_y(y)[i]), scores)
    if print:
        print_scores(scores)
    return get_D(scores)

MEANING_DIM = 24
def get_meaning_X(X):
    return X[:,0:MEANING_DIM]

def get_meaning_y(y):
    return y[:,0]

def get_langauge_X(X):
    return X[:, MEANING_DIM:]  # using -1 will lose the last column.

def get_langauge_y(y):
    return y[:,1]

if __name__ == '__main__':

 # year17 data
    with open('../data/processed/numpy/year17_withHuy.pkl', 'rb') as pf:
        objs = pickle.load(pf)
        year17_lang_train_X = objs[0]
        year17_lang_train_y = objs[1]
        year17_lang_test_X = objs[2]
        year17_lang_test_y = objs[3]

        year17_meaning_train_X = objs[4]
        year17_meaning_train_y = objs[5]
        year17_meaning_test_X = objs[6]
        year17_meaning_test_y = objs[7]

        year17_train_X = np.concatenate((year17_meaning_train_X, year17_lang_train_X), axis=1)
        year17_train_y = np.column_stack((year17_meaning_train_y, year17_lang_train_y))
        year17_test_X = np.concatenate((year17_meaning_test_X, year17_lang_test_X), axis=1)
        year17_test_y = np.column_stack((year17_meaning_test_y, year17_lang_test_y))

    logger = get_logger(__name__, simple=True)

    @optunity.cross_validated(x=year17_train_X, y=year17_train_y, num_folds=5)
    def tune_Dscore(x_train, y_train, x_test, y_test, L_thres=0.5):
        model_lang = SVC(probability=True)
        model_lang.fit(get_langauge_X(x_train), get_langauge_y(y_train))
        D_score = get_D_based_thresholds(L_thres,
                                         model_lang.predict_proba(get_langauge_X(x_test)),
                                         y_test)
        return D_score

    @timeit
    # this can use optunity's multi-thread and smarter solver
    def tune_D_smart():
        thres_grid = {'L_thres': [0.25, 0.35]}
        optm_thres, info, _ = optunity.maximize_structured(tune_Dscore,
                                                           num_evals=20,
                                                           pmap=optunity.pmap,
                                                           search_space=thres_grid)
        print("Optimal parameters" + str(optm_thres))
        print("best D after CV running: %2.3f" % info.optimum)
        return (optm_thres['L_thres'])

    best_L = tune_D_smart()
    #
    # 1/19/2018
    #
    # best_L 0.5  D = 4.04
    # best_L 0.3  D = 4.58
    # It looks that we need tune best_L
    # best_L = 0.3
    #
    # when tuning D on L_thres [0.25, 0.75] for 50 times, we get L_thres = 0.251273
    # D can reach 5.76, however, cRj is 0.04
    # After focusing on [0.25, 0.35] for 20 times, get D 5.15 with a cRj 0.05
    #

    model_lang = SVC(probability=True)
    model_lang.fit(year17_lang_train_X, year17_lang_train_y)
    probs_l = model_lang.predict_proba(year17_lang_test_X)

    y_true, y_pred = year17_lang_test_y, model_lang.predict(year17_lang_test_X)

    print(accuracy_score(y_true, y_pred))
    print(classification_report(y_true, y_pred))
    print(confusion_matrix(y_true, y_pred))

    D_score = get_D_based_thresholds(best_L,
                                     probs_l,
                                     year17_test_y, True)


text:
import numpy as np
from mesohops.dynamics.bath_corr_functions import bcf_exp
from mesohops.dynamics.noise_fft import FFTFilterNoise
from mesohops.dynamics.noise_trajectories import NumericNoiseTrajectory

__title__ = "Test of noiseFunctions"
__author__ = "D. I. G. Bennett"
__version__ = "0.1"
__date__ = "Feb. 10, 2019"

# Test Noise Classes
# ------------------
t_axis = np.array([0, 1, 2, 3, 4], dtype=np.float64)
noise = np.array([[1, 2, 2, 1, 3], [2, 3, 3, 2, 4]], dtype=np.float64)

def test_NumericNoise():
    """
    this test checks numeric noise trajectories
    """
    numNoise = NumericNoiseTrajectory(noise, t_axis)
    assert (
        numNoise.get_noise(np.array([0.0, 1.0]))[0, 1]
        == np.array([[1, 2], [2, 3]], dtype=np.float64)[0, 1]
    )
    assert list(numNoise.get_taxis()) == list(t_axis)

# Test Noise Model
# ----------------
noise_param = {
    "SEED": 0,
    "MODEL": "FFT_FILTER",
    "TLEN": 10.0,  # Units: fs
    "TAU": 1.0,  # Units: fs
}

loperator = np.zeros([2, 2, 2], dtype=np.float64)
loperator[0, 0, 0] = 1.0
loperator[1, 1, 1] = 1.0
sys_param = {
    "HAMILTONIAN": np.array([[0, 10.0], [10.0, 0]], dtype=np.float64),
    "GW_SYSBATH": [[10.0, 10.0], [5.0, 5.0]],
    "L_HIER": loperator,
    "ALPHA_NOISE1": bcf_exp,
    "PARAM_NOISE1": [[10.0, 10.0], [5.0, 5.0]],
    "L_NOISE1": loperator,
}

sys_param["NSITE"] = len(sys_param["HAMILTONIAN"][0])
sys_param["NMODES"] = len(sys_param["GW_SYSBATH"][0])
sys_param["N_L2"] = 2
sys_param["L_IND_BY_NMODE1"] = [0, 1]
sys_param["LIND_DICT"] = {0: loperator[0, :, :], 1: loperator[1, :, :]}

def test_noiseModel():
    noise_corr = {
        "CORR_FUNCTION": sys_param["ALPHA_NOISE1"],
        "N_L2": sys_param["N_L2"],
        "LIND_BY_NMODE": sys_param["L_IND_BY_NMODE1"],
        "CORR_PARAM": sys_param["PARAM_NOISE1"],
    }
    noiseModel = FFTFilterNoise(noise_param, noise_corr)
    noiseModel.prepare_noise()
    assert noiseModel.param["T_AXIS"][-1] == 10.0
    testnoise = noiseModel.get_noise(np.arange(10.0))[0, :]
    testnoise_answer = np.array(
        [
            3.82810911 + 0.9397952j,
            3.75969812 + 0.91927177j,
            3.73492567 + 1.04445045j,
            3.62975737 + 0.96089954j,
            3.45986897 + 0.81667851j,
            3.57567558 + 0.75886821j,
            3.91953564 + 0.6094234j,
            3.84677822 + 0.45471175j,
            3.92130167 + 0.45716379j,
            3.94064793 + 0.52815227j,
        ]
    )
    assert np.allclose(testnoise, testnoise_answer, rtol=1e-7)

def test_fft_filter_noise_diagonal():
    noise_param = {
        "SEED": 0,
        "MODEL": "FFT_FILTER",
        "TLEN": 10000.0,  # Units: fs
        "TAU": 1.0,  # Units: fs
    }
    noise_corr = {
        "CORR_FUNCTION": sys_param["ALPHA_NOISE1"],
        "N_L2": sys_param["N_L2"],
        "LIND_BY_NMODE": sys_param["L_IND_BY_NMODE1"],
        "CORR_PARAM": sys_param["PARAM_NOISE1"],
    }

    noiseModel = FFTFilterNoise(noise_param, noise_corr)
    t_axis = np.arange(0, 5000.0, 1.0)
    alpha = noiseModel._corr_func_by_lop_taxis(t_axis)
    alpha_test = np.array([10.0 + 0.0j, 9.98118165 + 0.0j, 9.96239871 + 0.0j])
    assert np.allclose(alpha[0][0:3], alpha_test)

max_stars_repo_name: MesoscienceLab/mesohops

max_issues_repo_name: MesoscienceLab/mesohops

max_forks_repo_name: MesoscienceLab/mesohops


text:
import numpy as np
import pygame


text:
import numpy as np
import pickle as pkl
import scipy.sparse as sp
import sys
import os
import networkx as nx
from utils import *
import json
from networkx.readwrite import json_graph

 # 'cora', 'citeseer', 'pubmed'

if __name__=="__main__":
    data_name = 'cora'
    adj, features, y_train, y_val, y_test, train_mask, val_mask, test_mask = load_data(data_name)

    G = nx.from_scipy_sparse_matrix(adj)

    val_index = np.where(val_mask)[0]
    test_index = np.where(test_mask)[0]
    y = y_train+y_val+y_test
    y = np.argmax(y,axis=1)

    for i in range(len(y)):
        if i in val_index:
            G.node[i]['val']=True
            G.node[i]['test']=False
        elif i in test_index:
            G.node[i]['test']=True
            G.node[i]['val']=False
        else:
            G.node[i]['test'] = False
            G.node[i]['val'] = False

    data = json_graph.node_link_data(G)
    with open("cora/cora-G.json","wb") as f:
        json.dump(data,f)
    classMap = {}
    idMap = {}
    for i in range(len(y)):
        classMap[i]=y[i]
        idMap[i] = i
    with open("cora/cora-id_map.json","wb") as f:
        json.dump(idMap,f)
    with open("cora/cora-class_map.json","wb") as f:
        json.dump(classMap,f)
    np.save(open("cora/cora-feats.npy","wb"), features.todense())

max_stars_repo_name: CEfanmin/DataMiningProjects

max_issues_repo_name: CEfanmin/DataMiningProjects

max_forks_repo_name: CEfanmin/DataMiningProjects

reason: import numpy,import scipy,import networkx,from networkx


text:
# -*- coding: utf-8 -*-
"""Classes (Python) to compute the Bandit UCB1 arm allocation and choosing the arm to pull next.

See :class:`moe.bandit.ucb.ucb_interface.UCBInterface` for further details on this bandit.

"""
import math

import numpy

from moe.bandit.constant import UCB_SUBTYPE_1
from moe.bandit.ucb.ucb_interface import UCBInterface

class UCB1(UCBInterface):

    r"""Implementation of UCB1.

    A class to encapsulate the computation of bandit UCB1.
    See :func:`moe.bandit.ucb.ucb_interface.UCBInterface.allocate_arms` for more details on how UCB allocates arms.

    See superclass :class:`moe.bandit.ucb.ucb_interface.UCBInterface` for further details.

    """

    def __init__(
            self,
            historical_info,
    ):
        """Construct an UCB1 object. See superclass :class:`moe.bandit.ucb.ucb_interface.UCBInterface` for details."""
        super(UCB1, self).__init__(
            historical_info=historical_info,
            subtype=UCB_SUBTYPE_1,
            )

    def get_ucb_payoff(self, sampled_arm, number_sampled):
        r"""Compute the expected upper confidence bound payoff using the UCB1 formula.

        The expected upper confidence bound payoff (expected UCB payoff) is computed as follows:

        .. math:: r_j = \mu + \sqrt{\frac{2 \ln n}{n_j}}

        where :math:`\mu` is the average payoff obtained from arm *j* (the given ``sampled_arm``),
        :math:`n_j` is the number of times arm *j* has been pulled (``sampled_arm.total``),
        and *n* is overall the number of pulls so far (``number_sampled``). ``number_sampled`` (number sampled)
        is calculated by summing up total from each arm sampled.

        :param sampled_arm: a sampled arm
        :type sampled_arm: :class:`moe.bandit.data_containers.SampleArm`
        :param number_sampled: the overall number of pulls so far
        :type number_sampled: int
        :return: ucb payoff
        :rtype: float64
        :raise: ValueError when ``sampled_arm`` is empty.

        """
        if not sampled_arm:
            raise ValueError('sampled_arm is empty!')

        # If number_sampled is zero, we haven't sampled any arm at all. All arms are equally likely to be the best arm.
        if number_sampled == 0:
            return 0.0

        avg_payoff = numpy.float64(sampled_arm.win - sampled_arm.loss) / sampled_arm.total if sampled_arm.total > 0 else 0
        return avg_payoff + math.sqrt(2.0 * math.log(number_sampled) / sampled_arm.total)

max_stars_repo_name: dstoeckel/MOE

max_issues_repo_name: dstoeckel/MOE

max_forks_repo_name: dstoeckel/MOE


text:
import glob
import json
import os
from subprocess import call

import numpy as np
import pandas as pd

def gen_label_stubs(ontology_path='ontologies/class-tree_dbpedia_2016-10.json'):

    with open('{0}'.format(ontology_path), 'r') as f:
        tree = json.load(f)

    classes = list(tree.keys())
    df = pd.DataFrame(classes, columns=['class'])
    df['label'] = ''

    for file_name in glob.glob('*.csv'):
        print('creating stub for', file_name)
        file_name = file_name.split('.')[0]  # remove file extension
        df.to_csv('{0}_labels.csv'.format(file_name), index=False)

def labels_to_positive_list():

    paths = glob.glob('data/*_labels.csv')

    for label_file in paths:
        label_df = pd.read_csv(label_file)

        dataset_name = label_file[:-11]
        pos_examples = label_df['class'][label_df['label'] == 1].tolist()

        # if labels have been provided, write pos examples to file
        if pos_examples:
            with open('{0}_positive_examples.json'.format(dataset_name), 'w') as json_file:
                json.dump(pos_examples, json_file, indent=2)

def flatten_data_directories():
    data_files = glob.glob('data/data/*/*_dataset/tables/learningData.csv')

    for file_path in data_files:
        dataset_name = os.path.dirname(file_path).split('/')[1]
        dataset_name = '_'.join(dataset_name.split('_')[2:])  # remove "LL0_number_" prefix
        print('dataset name: ', dataset_name)
        call(['cp', file_path, dataset_name + '.csv'])

if __name__ == '__main__':
    # flatten_data_directories()
    # gen_label_stubs()
    labels_to_positive_list()


text:
from oscar.env import envs
# from oscar.agent.scripted.minigame.deepmindAgents import CollectMineralShards
# from oscar.agent.scripted.minigame.bruno_mineralshard import CollectMineralShards
from oscar.agent.scripted.minigame.nicolas_mineralshard import CollectMineralShardsP
from learning_tools.A3C_learner.neuralmodel import get_neural_network, save_neural_network
from learning_tools.A3C_learner.constants import ENV
import gym
import numpy as np
import time

# ENV = 'pysc2-mineralshard-v1' set into the constants file (of A3C learner)
TRAINING_STEPS = 240 * 2
TEST_RUN = 1

RUN_NN_ACTION = False

env = gym.make(ENV)
agent = CollectMineralShardsP()

output_shape = env.action_space.n
input_shape = env.observation_space.shape

model = get_neural_network(input_shape=input_shape,
                           output_shape_list=[output_shape, 1])
model.summary()

action_batch = []
obs_batch = []

episode_reward = 0
agent.reset()
obs = env.reset()

print("learning for", TRAINING_STEPS, "steps")
try:
    for i in range(TRAINING_STEPS):
        # time.sleep(0.1)
        action = agent.step(env.last_obs)

        action_id = env.get_action_id_from_action(sc2_action=action.function,
                                                  sc2_args=action.arguments)

        learning_action = np.zeros(shape=output_shape, dtype=float)
        learning_action[action_id] = 1.0
        if RUN_NN_ACTION:
            # train NN
            model.fit(x=np.reshape(obs, (1, len(obs),) + obs[0].shape),
                      y=[np.reshape(learning_action, (1,) + learning_action.shape), np.zeros(1)],
                      verbose=0,
                      epochs=3
                      )
            # get his action
            p = model.predict(x=np.reshape(obs, (1, len(obs),) + obs[0].shape))[0][0]
            # get mask for unavailable action
            action_mask = env.get_action_mask()
            # apply mask to action probability
            p *= action_mask
            # normalize (set sum back to 1.0)
            p_sum = np.sum(p)
            if p_sum != 0:
                p /= p_sum
                # played_action_id = np.random.choice(len(p), p=p)
                played_action_id = np.argmax(p)
            else:
                played_action_id = 0
        else:
            # generate batch for training (on episode end only)
            action_batch.append(learning_action)
            obs_batch.append(obs)
            played_action_id = action_id

        # print("action: train", action_id, "selected", played_action_id)
        # if played_action_id < 256:
        #     action = env.get_select_action(played_action_id)
        # else:
        #     action = env.get_move_action(played_action_id - 256)
        # print("played action:", action)
        # if action_id < 256:
        #     action = env.get_select_action(action_id)
        # else:
        #     action = env.get_move_action(action_id - 256)
        # print("trained action:", action)

        obs, reward, done, _ = env.step(played_action_id)

        episode_reward += reward

        if done:
            print("step:", i, "- episode reward:", episode_reward)
            episode_reward = 0
            if not RUN_NN_ACTION:
                # learn from current batch
                model.fit(x=np.array(obs_batch),
                          y=[np.array(action_batch), np.zeros(len(action_batch))],
                          batch_size=len(action_batch),
                          verbose=0,
                          epochs=10
                          )
                action_batch = []
                obs_batch = []
            # reset env and agent
            agent.reset()
            obs = env.reset()
except KeyboardInterrupt:
    print("Keyboard interrupt, save NN and exit")
else:
    print("Test for", TEST_RUN, "minigames")
    obs = env.reset()
    minigame_reward = 0
    for i in range(TEST_RUN * 240 + 1):
        # time.sleep(0.2)
        p = model.predict(x=np.array([obs]),
                          batch_size=1)[0][0]
        # get mask for unavailable action
        action_mask = env.get_action_mask()
        # apply mask to action probability
        p *= action_mask
        # normalize (set sum back to 1.0)
        p_sum = np.sum(p)
        if p_sum != 0:
            p /= p_sum
            action_id = np.argmax(p)
            # action_id = np.random.choice(len(p), p=p)
        else:
            action_id = 0
        # if action_id < 256:
        #     action = env.get_select_action(action_id)
        # else:
        #     action = env.get_move_action(action_id - 256)
        # print("played action:", action)
        obs, reward, done, _ = env.step(action_id)
        minigame_reward += reward
        if done:
            obs = env.reset()
            print("reward", minigame_reward)
            minigame_reward = 0

save_neural_network(model=model)

agent.reset()
env.reset()
env.close()

del env

max_stars_repo_name: Xaxetrov/OSCAR

max_issues_repo_name: Xaxetrov/OSCAR

max_forks_repo_name: Xaxetrov/OSCAR


text:
import os
import sys
import random
import shutil
import tempfile

import cv2
import numpy as np
import torch
import torch.nn as nn
import matplotlib.pyplot as plt

from .configuration import SystemConfig, TrainerConfig, DataloaderConfig

def init_semantic_segmentation_dataset(data_path, imgs_folder="train", masks_folder="trainannot"):
    """ Prepare Semantic Segmentation dataset

    Arguments:
        data_path (string): path to dataset.
        imgs_folder (string): path to images (related of the data_path).
        masks_folder (string): path to masks (related of the data_path).

    Initialise dataset as a list of pairs {"image": <path_to_image>, "mask": <path_to_mask>}.
    """
    names = os.listdir(os.path.join(data_path, imgs_folder))
    dataset = []
    for name in names:
        dataset.append({
            "image": os.path.join(data_path, imgs_folder, name),
            "mask": os.path.join(data_path, masks_folder, name)
        })
    return dataset

def draw_semantic_segmentation_samples(dataset, n_samples=3):
    """ Draw samples from semantic segmentation dataset.

    Arguments:
        dataset (iterator): dataset class.
        plt (matplotlib.pyplot): canvas to show samples.
        n_samples (int): number of samples to visualize.
    """
    fig, ax = plt.subplots(nrows=n_samples, ncols=2, sharey=True, figsize=(10, 10))
    for i, sample in enumerate(dataset):
        if i >= n_samples:
            break
        ax[i][0].imshow(sample["image"])
        ax[i][0].set_xlabel("image")
        ax[i][0].set_xticks([])
        ax[i][0].set_yticks([])

        ax[i][1].imshow(sample["mask"])
        ax[i][1].set_xlabel("mask")
        ax[i][1].set_xticks([])
        ax[i][1].set_yticks([])

    plt.tight_layout()
    plt.gcf().canvas.draw()
    plt.show()
    plt.close(fig)

def draw_semantic_segmentation_batch(images, masks_gt, masks_pred=None, n_samples=3):
    """ Draw batch from semantic segmentation dataset.

    Arguments:
        images (torch.Tensor): batch of images.
        masks_gt (torch.LongTensor): batch of ground-truth masks.
        plt (matplotlib.pyplot): canvas to show samples.
        masks_pred (torch.LongTensor, optional): batch of predicted masks.
        n_samples (int): number of samples to visualize.
    """
    nrows = min(images.size(0), n_samples)
    ncols = 2 if masks_pred is None else 3
    fig, ax = plt.subplots(nrows=nrows, ncols=ncols, sharey=True, figsize=(10, 10))
    for i in range(nrows):
        img = images[i].permute(1, 2, 0).detach().cpu().numpy()
        img = np.clip(img, 0, 1)
        ax[i][0].imshow(img)
        ax[i][0].set_xlabel("image")
        ax[i][0].set_xticks([])
        ax[i][0].set_yticks([])
        gt_mask = masks_gt[i].detach().cpu().numpy()
        ax[i][1].imshow(gt_mask)
        ax[i][1].set_xlabel("ground-truth mask")
        ax[i][1].set_xticks([])
        ax[i][1].set_yticks([])
        if masks_pred is not None:
            pred = masks_pred[i].detach().cpu().numpy()
            ax[i][2].imshow(pred)
            ax[i][2].set_xlabel("predicted mask")
            ax[i][2].set_xticks([])
            ax[i][2].set_yticks([])

    plt.tight_layout()
    plt.gcf().canvas.draw()
    plt.show()
    plt.close(fig)

def get_camvid_dataset_parameters(data_path, dataset_type="train", transforms=None):
    """ Get CamVid parameters compatible with DanseData dataset class.

    Arguments:
        data_path (string): path to dataset folder.
        dataset_type (string): dataset type (train, test or val).
        transforms (callable, optional): A function/transform that takes in a sample
            and returns a transformed version.

    Returns:
        dictionary with parameters of CamVid dataset.
    """
    return {
        "data_path":
            os.path.join(data_path, "CamVid"),
        "images_folder":
            dataset_type,
        "masks_folder":
            dataset_type + "annot",
        "num_classes":
            11,
        "transforms":
            transforms,
        "dataset_url":
            "https://github.com/alexgkendall/SegNet-Tutorial.git",
        "dataset_folder":
            "CamVid",
        "class_names": [
            'sky', 'building', 'pole', 'road', 'pavement', 'tree', 'signsymbol', 'fence', 'car', 'pedestrian',
            'bicyclist', 'unlabelled'
        ]
    }

class AverageMeter:
    """Computes and stores the average and current value"""
    def __init__(self):
        self.reset()

    def reset(self):
        self.val = 0
        self.avg = 0
        self.sum = 0
        self.count = 0

    def update(self, val, count=1):
        self.val = val
        self.sum += val * count
        self.count += count
        self.avg = self.sum / self.count

class DeviceManager:
    """ Helper class to auto upload variables to CUDA.

    Arguments:
        use_cuda (bool): if True then variables will be upload to GPU.
    """
    def __init__(self, use_cuda=True):
        self.use_cuda = use_cuda

    def __call__(self, var):
        """ Upload variable to default device.

        Arguments:
            var (torch.Module or torch.Tensor): input variable.

        Returns:
            Returns input variable uploaded to select default device.
        """
        return var.cuda() if self.use_cuda else var

def patch_configs(epoch_num_to_set=TrainerConfig.epoch_num, batch_size_to_set=DataloaderConfig.batch_size):
    """ Patches configs if cuda is not available

    Returns:
        returns patched dataloader_config and trainer_config

    """
    # default experiment params
    num_workers_to_set = DataloaderConfig.num_workers

    if torch.cuda.is_available():
        device = "cuda"
    else:
        device = "cpu"
        batch_size_to_set = 16
        num_workers_to_set = 2
        epoch_num_to_set = 1

    dataloader_config = DataloaderConfig(batch_size=batch_size_to_set, num_workers=num_workers_to_set)
    trainer_config = TrainerConfig(device=device, epoch_num=epoch_num_to_set, progress_bar=True)
    return dataloader_config, trainer_config

def setup_system(system_config: SystemConfig) -> None:
    torch.manual_seed(system_config.seed)
    np.random.seed(system_config.seed)
    random.seed(system_config.seed)
    torch.set_printoptions(precision=10)
    if torch.cuda.is_available():
        torch.cuda.manual_seed_all(system_config.seed)
        torch.backends.cudnn_benchmark_enabled = system_config.cudnn_benchmark_enabled
        torch.backends.cudnn.deterministic = system_config.cudnn_deterministic

def fit_class_number(model: nn.Module, num_classes: int) -> nn.Module:
    filters_num = model.fc.in_features
    model.fc = nn.Linear(filters_num, num_classes)
    return model

def draw_samples(data, rows, cols):
    assert len(data) >= rows * cols
    fig, ax = plt.subplots(nrows=rows, ncols=cols, sharey=True, figsize=(10, 10))
    iterator = iter(data)
    for idx in range(rows * cols):
        img, _ = next(iterator)
        i = idx // cols
        j = idx % cols
        ax[i][j].axis("off")
        ax[i][j].imshow(img, interpolation="nearest")
    plt.tight_layout()
    fig.canvas.draw()
    plt.show()

def resize(img, boxes, size, max_size=1000):
    '''Resize the input cv2 image to the given size.

    Args:
      img: (cv2) image to be resized.
      boxes: (tensor) object boxes, sized [#ojb,4].
      size: (tuple or int)
        - if is tuple, resize image to the size.
        - if is int, resize the shorter side to the size while maintaining the aspect ratio.
      max_size: (int) when size is int, limit the image longer size to max_size.
                This is essential to limit the usage of GPU memory.
    Returns:
      img: (cv2) resized image.
      boxes: (tensor) resized boxes.
    '''
    height, width, _ = img.shape
    if isinstance(size, int):
        size_min = min(width, height)
        size_max = max(width, height)
        scale_w = scale_h = float(size) / size_min
        if scale_w * size_max > max_size:
            scale_w = scale_h = float(max_size) / size_max
        new_width = int(width * scale_w + 0.5)
        new_height = int(height * scale_h + 0.5)
    else:
        new_width, new_height = size
        scale_w = float(new_width) / width
        scale_h = float(new_height) / height

    return cv2.resize(img, (new_height, new_width)), \
           boxes * torch.Tensor([scale_w, scale_h, scale_w, scale_h])

def random_flip(img, boxes):
    '''Randomly flip the given cv2 Image.

    Args:
        img: (cv2) image to be flipped.
        boxes: (tensor) object boxes, sized [#ojb,4].

    Returns:
        img: (cv2) randomly flipped image.
        boxes: (tensor) randomly flipped boxes.
    '''
    if random.random() < 0.5:
        img = cv2.flip(img, 1)
        width = img.shape[1]
        xmin = width - boxes[:, 2]
        xmax = width - boxes[:, 0]
        boxes[:, 0] = xmin
        boxes[:, 2] = xmax
    return img, boxes


text:
import numpy as np
import pandas as pd

# Test the GC function
from PiSlice.popstatistics import gc
test = "ATCGTAGCTGGGCTAGCTGATGCGCGCG" # 18 GC on 28 nucleotides
gc(test) == 18/28
test = "ATCGTNGCNGGGCTAGCTGATGCGCGCG" # 18 GC on 26 nucleotides + 2 Ns not to consider
gc(test) == 18/26
gc("ATC") is np.nan
gc("") is np.nan

# Test the CpG function
from PiSlice.popstatistics import cpg
test = "ATCGTAGCTGGGCTAGCTGATGCGCGCG" # 4 CpG sites on 14 sites
cpg(test) == 4/14
test = "ATCGTAGCNNGGCTAGCTGATGCGCGCG" # 4 CpG sites on 13 sites + 1 site NN
cpg(test) == 4/13
import PiSlice.input as input
# Test short sequence (< 6bp)
cpg("ATCG") is np.nan
# Test on real data
fasta_file = "data/Oryza_sativa_GCA_001433935.1/GCA_001433935.1_IRGSP-1.0_genomic.fna.gz"
genome = input.fasta(fasta_file)
nb_chromosome = 5
windows = pd.DataFrame({
    'Chromosome': genome.references[0:nb_chromosome],
    'Start': [1] * nb_chromosome,
    'End': list(map(lambda x: len(genome.fetch(x)), genome.references[0:nb_chromosome]))
})
from PiSlice.popstatistics import piSlice
piSlice(windows, statistics=["cpg"], fasta=genome)

windows = pd.DataFrame({
    'Chromosome': genome.references[0:2:1],
    'Start': [1000, 1000],
    'End': [2000, 1005]
})
piSlice(windows, statistics=["cpg"], fasta=genome)

max_stars_repo_name: ThomasBrazier/PiSlice

max_issues_repo_name: ThomasBrazier/PiSlice

max_forks_repo_name: ThomasBrazier/PiSlice


text:
"""DeBruijnGenerator.py: Defines a simple class for generating
deBruijn sequences one symbol at a time.
It is based on a class I originally wrote some years ago in c++.
A function that generates the entire sequence is also provided
for testing purposes.
Developed and tested using python v2.7.10 and numpy v1.15.4
Brendan Harding, March 2019
"""

__author__    = "Brendan Harding"
__copyright__ = "Copyright 2019"
__license__   = "MIT"
__version__   = "0.1.0"

import numpy as np

class DeBruijnGenerator:
    """
    Class for producing symbols from a debruijn sequence one at a time.
    This has the advantage of not generating the whole sequence in memory.
    The sequence produced is the lexicographic smallest.
    Functions are included to enable a checkpoint/restart like functionality.
    """

    def __init__(self,b,n):
        """Initialise the sequence given a base b and sub-sequence length n."""
        self.b = b                        # base
        self.n = n                        # length of subsequences
        self.i = n                        # internal working index
        self.m = 1                        # internal cache index
        self.a = np.zeros(n+1,np.int)     # working array
        self.cache = np.zeros(n+1,np.int) # internal cache
        self.completed = False            # a completion flag

    def reset(self):
        """Reset the sequence to start from the beginning."""
        self.a *= 0
        self.cache *= 0
        self.i = self.n
        self.m = 1

    def __call__(self):
        """Return the next element in the sequence."""
        if self.m>0:
            self.m -= 1
            return self.cache[self.m+1]
        else:
            while self.i>0:
                self.a[self.i] += 1
                for j in range(1,self.n+1-self.i):
                    self.a[j+self.i] = self.a[j]
                #self.a[self.i+1:] = self.a[1:self.n+1-self.i]
                # Should not use slicing here ^^^ (sequential only)
                if self.n%self.i==0:
                    self.cache[self.i:0:-1] = self.a[1:self.i+1]
                    self.m = self.i-1
                    self.i = self.n-np.argmin(self.a[::-1]==self.b-1)
                    return self.cache[self.m+1]
                self.i = self.n-np.argmin(self.a[::-1]==self.b-1)
            self.completed = True
            self.reset()
            self.m -= 1
            return self.cache[self.m+1]

    def is_complete(self):
        """Return a flag specifying if sequence has run to completion."""
        return self.completed

    def reset_completion_flag(self):
        """Resets the completion flag for the sequence."""
        self.completed = False

    def length(self):
        """Returns the sequence length, but beware of overflow!"""
        return self.b**self.n

    def dump_state(self):
        """Dump the current state of the generator (i.e. a checkpoint)."""
        return [self.b,self.n,self.i,self.m,self.a,self.cache,self.completed]

    def set_state(self,data):
        """Set the state of the generator (i.e. a restart from checkpoint)."""
        self.b = data[0]
        self.n = data[1]
        self.i = data[2]
        self.m = data[3]
        self.a = data[4]
        self.cache = data[5]
        self.completed = data[6]
        # There should probably be some checkes that self.a
        # and self.cache are the correct length...

    # end class

# We don't really use this, other than to test the above.
def deBruijn(k, n):
    """
    Generate a de Bruijn sequence for alphabet k and subsequences of length n.
    This is from the Wikipedia article (based on that in Frank Ruskey's book).
    """
    try:
        _ = int(k) # check if k can be cast to an integer
        alphabet = list(map(str, range(k))) # if so, make our alphabet a list
    except (ValueError, TypeError):
        alphabet = k
        k = len(k)
    a = [0] * k * n
    sequence = []
    def db(t, p):
        if t > n:
            if n % p == 0:
                sequence.extend(a[1:p + 1])
        else:
            a[t] = a[t - p]
            db(t + 1, p)
            for j in range(a[t - p] + 1, k):
                a[t] = j
                db(t + 1, t)
    db(1, 1)
    return "".join(alphabet[i] for i in sequence)

if __name__ == "__main__":
	# If ran as a script, perform a few short tests
	db_2_12 = DeBruijnGenerator(2,12)
	seq1 = np.array([db_2_12() for _ in range(2**12)])
	seq2 = np.array([int(symbol) for symbol in deBruijn(2,12)])
	assert (seq1==seq2).all()
	db_3_8 = DeBruijnGenerator(3,8)
	seq1 = np.array([db_3_8() for _ in range(3**8)])
	seq2 = np.array([int(symbol) for symbol in deBruijn(3,8)])
	assert (seq1==seq2).all()
	db_4_6 = DeBruijnGenerator(4,6)
	seq1 = np.array([db_4_6() for _ in range(4**6)])
	seq2 = np.array([int(symbol) for symbol in deBruijn(4,6)])
	assert (seq1==seq2).all()
	db_5_5 = DeBruijnGenerator(5,5)
	seq1 = np.array([db_5_5() for _ in range(5**5)])
	seq2 = np.array([int(symbol) for symbol in deBruijn(5,5)])
	assert (seq1==seq2).all()
	print("DeBruijnGenerator.py: all tests passed!")

max_stars_repo_name: brendanharding/TwoMapDynSys

max_issues_repo_name: brendanharding/TwoMapDynSys

max_forks_repo_name: brendanharding/TwoMapDynSys


text:
# -*- coding: UTF-8 -*-
import numpy as np

from gym.spaces import Space
from gym import logger

class OneHot(Space):
    '''
    An n-dimensional onehot space.

    '''

    def __init__(self, n):
        self.n = n
        super(OneHot, self).__init__((self.n,), np.int8)

    def sample(self):
        vec = [0]*self.n
        vec[np.random.randint(n)] = 1
        return vec

    def contains(self, x):
        if isinstance(x, list):
            x = np.array(x)  # Promote list to array for contains check
        return (sum(x == 0) == self.n-1 & sum(x == 1) == 1)

    def __repr__(self):
        return "OneHot({})".format(self.n)

    def __eq__(self, other):
        return isinstance(other, OneHot) and self.n == other.n

max_stars_repo_name: Shi-Tianyi/EdgeFed-MARL-MEC

max_issues_repo_name: Shi-Tianyi/EdgeFed-MARL-MEC

max_forks_repo_name: Zhuzzq/Edge-Federated-MARL-MEC


text:
from __future__ import print_function

import argparse
import keras
from keras import backend as K
from keras.preprocessing import image
from keras.datasets import fashion_mnist
from keras_contrib.applications.wide_resnet import WideResidualNetwork
import numpy as np
import tensorflow as tf
import horovod.keras as hvd
import os

parser = argparse.ArgumentParser(description='Keras Fashion MNIST Example',
                                 formatter_class=argparse.ArgumentDefaultsHelpFormatter)
parser.add_argument('--log-dir', default='./logs',
                    help='tensorboard log directory')
parser.add_argument('--batch-size', type=int, default=32,
                    help='input batch size for training')
parser.add_argument('--val-batch-size', type=int, default=32,
                    help='input batch size for validation')
parser.add_argument('--epochs', type=int, default=40,
                    help='number of epochs to train')
parser.add_argument('--base-lr', type=float, default=0.01,
                    help='learning rate for a single GPU')
parser.add_argument('--momentum', type=float, default=0.9,
                    help='SGD momentum')
parser.add_argument('--wd', type=float, default=0.000005,
                    help='weight decay')

args = parser.parse_args()

# Checkpoints will be written in the log directory.
args.checkpoint_format = os.path.join(args.log_dir, 'checkpoint-{epoch}.h5')

# Horovod: initialize Horovod.
hvd.init()

# Horovod: pin GPU to be used to process local rank (one GPU per process)
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.gpu_options.visible_device_list = str(hvd.local_rank())
K.set_session(tf.Session(config=config))

# If set > 0, will resume training from a given checkpoint.
resume_from_epoch = 0
for try_epoch in range(args.epochs, 0, -1):
    if os.path.exists(args.checkpoint_format.format(epoch=try_epoch)):
        resume_from_epoch = try_epoch
        break

# Horovod: broadcast resume_from_epoch from rank 0 (which will have
# checkpoints) to other ranks.
resume_from_epoch = hvd.broadcast(resume_from_epoch, 0, name='resume_from_epoch')

# Horovod: print logs on the first worker.
verbose = 1 if hvd.rank() == 0 else 0

# Input image dimensions
img_rows, img_cols = 28, 28
num_classes = 10

# Load Fashion MNIST data.
(x_train, y_train), (x_test, y_test) = fashion_mnist.load_data()

if K.image_data_format() == 'channels_first':
    x_train = x_train.reshape(x_train.shape[0], 1, img_rows, img_cols)
    x_test = x_test.reshape(x_test.shape[0], 1, img_rows, img_cols)
    input_shape = (1, img_rows, img_cols)
else:
    x_train = x_train.reshape(x_train.shape[0], img_rows, img_cols, 1)
    x_test = x_test.reshape(x_test.shape[0], img_rows, img_cols, 1)
    input_shape = (img_rows, img_cols, 1)

# Convert class vectors to binary class matrices
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes)

# Training data iterator.
train_gen = image.ImageDataGenerator(featurewise_center=True, featurewise_std_normalization=True,
                                     horizontal_flip=True, width_shift_range=0.2, height_shift_range=0.2)
train_gen.fit(x_train)
train_iter = train_gen.flow(x_train, y_train, batch_size=args.batch_size)

# Validation data iterator.
test_gen = image.ImageDataGenerator(featurewise_center=True, featurewise_std_normalization=True)
test_gen.mean = train_gen.mean
test_gen.std = train_gen.std
test_iter = test_gen.flow(x_test, y_test, batch_size=args.val_batch_size)

# Restore from a previous checkpoint, if initial_epoch is specified.
if resume_from_epoch > 0:
    model = keras.models.load_model(args.checkpoint_format.format(epoch=resume_from_epoch))
else:
    # Set up standard WideResNet-16-10 model.
    model = WideResidualNetwork(depth=16, width=10, weights=None, input_shape=input_shape,
                                classes=num_classes, dropout_rate=0.01)

    # WideResNet model that is included with Keras is optimized for inference.
    # Add L2 weight decay & adjust BN settings.
    model_config = model.get_config()
    for layer, layer_config in zip(model.layers, model_config['layers']):
        if hasattr(layer, 'kernel_regularizer'):
            regularizer = keras.regularizers.l2(args.wd)
            layer_config['config']['kernel_regularizer'] = \
                {'class_name': regularizer.__class__.__name__,
                 'config': regularizer.get_config()}
        if type(layer) == keras.layers.BatchNormalization:
            layer_config['config']['momentum'] = 0.9
            layer_config['config']['epsilon'] = 1e-5

    model = keras.models.Model.from_config(model_config)

    opt = keras.optimizers.SGD(lr=args.base_lr,
                               momentum=args.momentum)

    model.compile(loss=keras.losses.categorical_crossentropy,
                  optimizer=opt,
                  metrics=['accuracy'])

def lr_schedule(epoch):
    if epoch < 15:
        return args.base_lr
    if epoch < 25:
        return 1e-1 * args.base_lr
    if epoch < 35:
        return 1e-2 * args.base_lr
    return 1e-3 * args.base_lr

callbacks = [
    keras.callbacks.LearningRateScheduler(lr_schedule),
    keras.callbacks.ModelCheckpoint(args.checkpoint_format),
    keras.callbacks.TensorBoard(args.log_dir)
]

# Train the model.
model.fit_generator(train_iter,
                    steps_per_epoch=len(train_iter),
                    callbacks=callbacks,
                    epochs=args.epochs,
                    verbose=verbose,
                    workers=4,
                    initial_epoch=resume_from_epoch,
                    validation_data=test_iter,
                    validation_steps=len(test_iter))

# Evaluate the model on the full data set.
score = model.evaluate_generator(test_iter, len(test_iter), workers=4)
if verbose:
    print('Test loss:', score[0])
    print('Test accuracy:', score[1])

max_stars_repo_name: tappinganalytics/tutorials

max_issues_repo_name: tappinganalytics/tutorials

max_forks_repo_name: tappinganalytics/tutorials


text:
import unittest
from setup.settings import *
from numpy.testing import *
from pandas.util.testing import *
import numpy as np
import dolphindb_numpy as dnp
import pandas as pd
import orca

class FunctionLeftshiftTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        # connect to a DolphinDB server
        orca.connect(HOST, PORT, "admin", "123456")

    def test_function_math_binary_left_shift_scalar(self):
        self.assertEqual(dnp.left_shift(1, 4), np.left_shift(1, 4))
        self.assertEqual(dnp.left_shift(1, -5), np.left_shift(1, -5))
        self.assertEqual(dnp.left_shift(0, 9), np.left_shift(0, 9))

    def test_function_math_binary_left_shift_list(self):
        lst1 = [1, 2, 3]
        lst2 = [4, 6, 9]
        assert_array_equal(dnp.left_shift(lst1, lst2), np.left_shift(lst1, lst2))

    def test_function_math_binary_left_shift_array_with_scalar(self):
        npa = np.array([1, 2, 3])
        dnpa = dnp.array([1, 2, 3])
        assert_array_equal(dnp.left_shift(dnpa, 1), np.left_shift(npa, 1))
        assert_array_equal(dnp.left_shift(1, dnpa), np.left_shift(1, npa))

    def test_function_math_binary_left_shift_array_with_array(self):
        npa1 = np.array([1, 2, 3])
        npa2 = np.array([4, 6, 9])

        dnpa1 = dnp.array([1, 2, 3])
        dnpa2 = dnp.array([4, 6, 9])
        assert_array_equal(dnp.left_shift(dnpa1, dnpa2), np.left_shift(npa1, npa2))

    def test_function_math_binary_left_shift_array_with_array_param_out(self):
        npa1 = np.array([1, 2, 3])
        npa2 = np.array([4, 6, 9])
        npa = np.zeros(shape=(1, 3))

        dnpa1 = dnp.array([1, 2, 3])
        dnpa2 = dnp.array([4, 6, 9])
        dnpa = dnp.zeros(shape=(1, 3))

        np.left_shift(npa1, npa2, out=npa)
        dnp.left_shift(dnpa1, dnpa2, out=dnpa)
        assert_array_equal(dnpa, npa)

    def test_function_math_binary_left_shift_array_with_series(self):
        npa = np.array([1, 2, 3])
        dnpa = dnp.array([1, 2, 3])
        ps = pd.Series([4, 6, 9])
        os = orca.Series([4, 6, 9])
        assert_series_equal(dnp.left_shift(dnpa, os).to_pandas(), np.left_shift(npa, ps), check_dtype=False)
        assert_series_equal(dnp.left_shift(os, dnpa).to_pandas(), np.left_shift(ps, npa), check_dtype=False)

        pser = pd.Series([1, 2, 3])
        oser = orca.Series([1, 2, 3])
        assert_series_equal(dnp.left_shift(os, oser).to_pandas(), np.left_shift(ps, pser))

if __name__ == '__main__':
    unittest.main()

reason: import numpy,from numpy


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import argparse
import torch
import torch.nn.parallel
from torch.autograd import Variable
import torch.optim as optim
import random
import sys
import os
import numpy as np
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
sys.path.append(BASE_DIR)
sys.path.append('../..')
sys.path.append(os.path.abspath(os.path.join(BASE_DIR, 'models')))
sys.path.append(os.path.abspath(os.path.join(BASE_DIR, 'dataloaders')))
import shapenet_part_loader
from utils import PointLoss,distance_squre,farthest_point_sample,index_points
from PCN_model import Autoencoder
import data_utils as d_utils

parser = argparse.ArgumentParser()
parser.add_argument('--batch_size', type=int, default=24, help='input batch size')
parser.add_argument('--n_epochs', type=int, default=101, help='number of epochs to train for')
parser.add_argument('--workers', type=int,default=2, help='number of data loading workers')
parser.add_argument('--num_inputs', type=int, default=1024, help='parital point numbers')
parser.add_argument('--num_coarses', type=int, default=1024, help='coarse point numbers')
parser.add_argument('--num_fines', type=int, default=2048, help='fine point numbers')
parser.add_argument('--grid_size', type=int, default=4, help='2d grid size')

parser.add_argument('--crop_point_num',type=int,default=512,help='number of crop points ')

parser.add_argument('--model', type=str, default='', help='model path')
parser.add_argument('--dataset', type=str, default='shapenet_part', help='dataset: shapenet_part, shapenet_core13, shapenet_core55')
opt = parser.parse_args()

resume_epoch=0
USE_CUDA = True
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
PCN = Autoencoder(opt.num_inputs,opt.num_coarses,opt.num_fines,opt.grid_size)

if opt.model != '':
    PCN.load_state_dict(torch.load(opt.model,map_location=lambda storage, location: storage)['state_dict'])
    resume_epoch = torch.load(opt.model)['epoch']

if USE_CUDA:
    print("Let's use", torch.cuda.device_count(), "GPUs!")
    PCN.to(device)
    PCN = torch.nn.DataParallel(PCN)

dset = shapenet_part_loader.PartDataset( root='../dataset/shapenetcore_partanno_segmentation_benchmark_v0/',classification=True, class_choice=None, npoints=opt.num_fines, split='train')
assert dset
dataloader = torch.utils.data.DataLoader(dset, batch_size=opt.batch_size,
                                         shuffle=True,num_workers = int(opt.workers))

test_dset = shapenet_part_loader.PartDataset( root='../dataset/shapenetcore_partanno_segmentation_benchmark_v0/',classification=True, class_choice=None, npoints=opt.num_fines, split='test')
test_dataloader = torch.utils.data.DataLoader(test_dset, batch_size=opt.batch_size,
                                         shuffle=True,num_workers = int(opt.workers))

print(PCN)
criterion_PointLoss = PointLoss().to(device)

for epoch in range(resume_epoch,opt.n_epochs):
    if epoch < 50:
        optimizer = optim.Adam(PCN.parameters(), lr=0.0001)
    elif epoch<150:
        optimizer = optim.Adam(PCN.parameters(), lr=0.00001)
    else:
        optimizer = optim.Adam(PCN.parameters(), lr=0.000001)

    if epoch<15:
        alpha = 0.01
    elif epoch<35:
        alpha = 0.1
    elif epoch<70:
        alpha = 0.5
    else:
        alpha = 1.0

    for batch_id, data in enumerate(dataloader):
        PCN.train()
        real_point, target = data
        real_point = torch.unsqueeze(real_point,1)
        p_origin = [0,0,0]
        batch_size = real_point.size()[0]
        input_cropped =torch.FloatTensor(batch_size, 1, opt.num_fines-opt.crop_point_num, 3)
#        choice =[torch.Tensor([1,0,0]),torch.Tensor([0,0,1]),torch.Tensor([1,0,1])]
        choice = [torch.Tensor([1,0,0]),torch.Tensor([0,0,1]),torch.Tensor([1,0,1]),torch.Tensor([-1,0,0]),torch.Tensor([-1,1,0])]
        for m in range(batch_size):
            index = random.sample(choice,1)
            distance_list = []
            p_center = index[0]
            for n in range(opt.num_fines):
                distance_list.append(distance_squre(real_point[m,0,n],p_center))
            distance_order = sorted(enumerate(distance_list), key  = lambda x:x[1])

            crop_num_list = []
            for num in range(opt.num_fines-opt.crop_point_num):
                crop_num_list.append(distance_order[num+opt.crop_point_num][0])
            indices = torch.LongTensor(crop_num_list)
            input_cropped[m,0]=torch.index_select(real_point[m,0],0,indices)

        real_point = torch.squeeze(real_point,1) #BX2048X3
        input_cropped = torch.squeeze(input_cropped,1) #BX(2048-512)X3

        real_key_point_index =farthest_point_sample(real_point,opt.num_coarses,False)
        real_key_point = index_points(real_point,real_key_point_index) #BX1024X3

        input_key_cropped_index = farthest_point_sample(input_cropped,opt.num_inputs,False)
        input_key_cropped = index_points(input_cropped,input_key_cropped_index) #BX1024X3

        real_point = Variable(real_point,requires_grad=True)
        real_key_point = Variable(real_key_point,requires_grad=True)
        input_key_cropped = Variable(input_key_cropped,requires_grad=True)

        real_point = real_point.to(device)
        real_key_point = real_key_point.to(device)
        input_key_cropped = input_key_cropped.to(device)
        optimizer.zero_grad()

        coarses,fine=PCN(input_key_cropped)

        errG = criterion_PointLoss(fine,real_point)+alpha*criterion_PointLoss(coarses,real_key_point)
        errG.backward()
        optimizer.step()
        print('[%d/%d][%d/%d]  Loss_G: %.4f '
              % (epoch, opt.n_epochs, batch_id, len(dataloader), errG))
        f=open('loss_PCN.txt','a')
        f.write('\n'+'[%d/%d][%d/%d]  Loss_G: %.4f '
              % (epoch, opt.n_epochs, batch_id, len(dataloader), errG))

        f.close()
    if epoch% 10 == 0:
        torch.save({'epoch':epoch+1,
                'state_dict':PCN.module.state_dict()},
                'Trained_Recon_Model_PCN/pcn'+str(epoch)+'.pth' )

max_stars_repo_name: ldda/PF-Net-Point-Fractal-Network

max_issues_repo_name: ldda/PF-Net-Point-Fractal-Network

max_forks_repo_name: ldda/PF-Net-Point-Fractal-Network


text:
##############################################################################
# This file is a part of PFFDTD.
#
# PFFTD is released under the MIT License.
# For details see the LICENSE file.
#
# Copyright 2021 Brian Hamilton.
#
# File name: myfuncs.py
#
# Description: Miscellaneous python/numpy functions.  Not all used or useful.
#
##############################################################################

import numpy as np
from numpy import array as npa
import numpy.linalg as npl
from numpy import cos,sin,pi
from typing import Any,Optional
from common.myasserts import assert_np_array_float, assert_np_array_complex
from pathlib import Path
import struct
import os
import shutil
import hashlib
import multiprocessing as mp
import scipy.io.wavfile

EPS = np.finfo(np.float).eps

def rotmatrix_ax_ang(Rax:Any, Rang:float):
    assert isinstance(Rax,np.ndarray)
    assert Rax.shape == (3,)
    assert type(Rang) is float

    Rax = Rax/npl.norm(Rax) #to be sure

    theta = Rang/180.0*pi #in rad
    #see https://en.wikipedia.org/wiki/Rotation_matrix
    R = np.array([[cos(theta) + Rax[0]*Rax[0]*(1-cos(theta)), Rax[0]*Rax[1]*(1-cos(theta)) - Rax[2]*sin(theta), Rax[0]*Rax[2]*(1-cos(theta)) + Rax[1]*sin(theta)],\
                  [Rax[1]*Rax[0]*(1-cos(theta)) + Rax[2]*sin(theta), cos(theta) + Rax[1]*Rax[1]*(1-cos(theta)), Rax[1]*Rax[2]*(1-cos(theta)) - Rax[0]*sin(theta)],\
                  [Rax[2]*Rax[0]*(1-cos(theta)) - Rax[1]*sin(theta), Rax[2]*Rax[1]*(1-cos(theta)) + Rax[0]*sin(theta), cos(theta) + Rax[2]*Rax[2]*(1-cos(theta))]])
    assert npl.norm(npl.inv(R)-R.T) < 1e-8
    return R

def rotate_xyz_deg(thx_d,thy_d,thz_d):
    #R applies Rz then Ry then Rx (opposite to wikipedia)
    #rotations about x,y,z axes, right hand rule

    thx = np.deg2rad(thx_d)
    thy = np.deg2rad(thy_d)
    thz = np.deg2rad(thz_d)

    Rx = npa([[1,0,0],\
              [0,np.cos(thx),-np.sin(thx)],\
              [0,np.sin(thx),np.cos(thx)]])

    Ry = npa([[np.cos(thy),0,np.sin(thy)],\
             [0,1,0],\
             [-np.sin(thy),0,np.cos(thy)]])

    Rz = npa([[np.cos(thz),-np.sin(thz),0],\
             [np.sin(thz),np.cos(thz),0],\
             [0,0,1]])

    R = Rx @ Ry @ Rz

    return R,Rx,Ry,Rz

def rotate_az_el_deg(az_d,el_d):
    #R applies Rel then Raz
    #Rel is rotation about negative y-axis, right hand rule
    #Raz is rotation z-axis, right hand rule
    #this uses matlab convention
    #note, this isn't the most general rotation
    _,_,Ry,Rz = rotate_xyz_deg(0,-el_d,az_d)
    Rel = Ry
    Raz = Rz
    R = Raz @ Rel

    return R,Raz,Rel

#only used in box
def mydefault(a:Any, default:Any):
    if a is None:
        a = default
    return a

#seconds to day/hour/min/s
def s2dhms(time:int):
    #give time in seconds
    day = time // (24 * 3600)
    time = time % (24 * 3600)
    hour = time // 3600
    time %= 3600
    minutes = time // 60
    time %= 60
    seconds = time
    return (day, hour, minutes, seconds)

#misc numpy functions
def clamp(x:Any, xmin:Any, xmax:Any):
    assert isinstance(x,(np.ndarray,int,np.integer,float))
    assert isinstance(xmin,(np.ndarray,int,np.integer,float))
    assert isinstance(xmax,(np.ndarray,int,np.integer,float))
    return np.where(x<xmin,xmin,np.where(x>xmax,xmax,x))

def vclamp(x, xmin, xmax):
    return np.where(x<xmin,xmin,np.where(x>xmax,xmax,x)) #only this works for vectorized xmin,xmax

def dot2(v):
    return dotv(v,v)

def dotv(v1,v2):
    return np.sum(v1*v2,axis=-1)

def vecnorm(v1):
    return np.sqrt(dot2(v1))

def vecnorm2(v1):
    return np.sum(v1**2,axis=-1)

def normalise(v1,eps=EPS):
    return (v1.T/(vecnorm(v1)+eps)).T

def ceilint(x:float):
    assert isinstance(x,float)
    return np.int_(np.ceil(x))

def roundint(x:float):
    assert isinstance(x,float)
    return np.int_(np.round(x))

def floorint(x:float):
    assert isinstance(x,float)
    return np.int_(np.floor(x))

def maxabs(x:Any):
    assert_np_array_complex(x)
    if x.ndim==2:
        return np.amax(np.abs(x),axis=-1)[:,None]
    else:
        return np.amax(np.abs(x))

def to_col_2d(x:Any):
    assert x.ndim<3
    x = x.reshape(x.shape[-1],-1)
    assert x.shape[0]>x.shape[1]
    return x

def to_row_2d(x:Any):
    assert x.ndim<3
    x = x.reshape(-1,x.shape[-1])
    assert x.shape[1]>x.shape[0]
    return x

def ind2sub3d(ii,Nx,Ny,Nz):
    iz = ii % Nz
    iy = (ii - iz)//Nz % Ny
    ix = ((ii - iz)//Nz-iy)//Ny
    return ix,iy,iz

def rel_diff(x0,x1): #relative difference at machine epsilon level
    return (x0-x1)/(2.0**np.floor(np.log2(x0)))

def get_default_nprocs():
    return max(1,int(0.8*mp.cpu_count()))

#taken from SO, for argparse bool problem
def str2bool(v:str):
    assert isinstance(v, str)
    if v.lower() in ('true'):
        return True
    elif v.lower() in ('false'):
        return False
    else:
        raise argparse.ArgumentTypeError('Boolean value expected.')

def clear_dat_folder(dat_folder_str=None):
    #clear dat folder
    dat_path = Path(dat_folder_str)
    if not dat_path.exists():
        dat_path.mkdir(parents=True)
    else:
        assert dat_path.is_dir()

    assert dat_path.exists()
    assert dat_path.is_dir()
    for f in dat_path.glob('*'):
        try:
            f.unlink()
        except OSError as e:
            print("Error: %s : %s" % (f, e.strerror))

def clear_folder(folder_str):
    print(f'clearing {folder_str}...')
    os.system('rm -rf ' + str(Path(folder_str)) + '/*')
    print(f'{folder_str} cleared')

def double_to_hex(d):
    return hex(struct.unpack('<Q', struct.pack('<d', d))[0])

def hex_to_double(s):
    s_list = list(s)
    assert s_list[0]=='0'
    assert s_list[1]=='x'
    return struct.unpack('!d', bytes.fromhex(s.split('0x')[1]))[0]

def clear_console(lines):
    _, columns = os.popen('stty size', 'r').read().split()
    for n in range(lines):
        print(' ' * int(columns))
    for n in range(lines):
        print("\033[F",end='')

def read_txt_values(filepath_str):
    assert Path(filepath_str).exists()
    rdict={}
    print(f'reading {filepath_str}...')
    with open(Path(filepath_str)) as tfile:
        lines = tfile.readlines()
        keyvals = [line.rstrip('\n').split(' ') for line in lines]
        for keyval in keyvals:
            assert len(keyval)==2
            key = keyval[0]
            val = keyval[1] #still a string
            rdict[key] = val
            print(f'key = {key}, val = {val}')
    return rdict

def gen_md5hash(*args):
    hash_list = []
    out_hash = ''
    for arg in args:
        if isinstance(arg,np.ndarray):
            tmp_hash = hashlib.md5(arg.tobytes()).hexdigest()
        elif isinstance(arg,str):
            tmp_hash = hashlib.md5(arg.encode()).hexdigest()
        else:
            tmp_hash = hashlib.md5(str(arg).encode()).hexdigest()
        out_hash = hashlib.md5((tmp_hash + out_hash).encode()).hexdigest()

    return out_hash

def yes_or_no(question):
    while "the answer is invalid":
        reply = str(input(question+' (y/n): ')).lower().strip()
        if reply[:1] == 'y':
            return True
        if reply[:1] == 'n':
            return False

def iceil(x):
    return np.int_(np.ceil(x))

def iround(x):
    return np.int_(np.round(x))

def wavread(fname):
    SR,data = scipy.io.wavfile.read(fname) #reads in (Nsamples,Nchannels)
    if data.dtype == np.int16:
        data = data/32768.0
        SR = np.float_(SR)
    return SR,np.float_(data.T)

def wavwrite(fname,SR,data):
    data = np.atleast_2d(data) #expects (Nchannels,Nsamples), this will also assert that
    scipy.io.wavfile.write(fname,int(SR),np.float32(data.T)) #reads in (Nsamples,Nchannels)
    print(f'wrote {fname} at SR={SR/1000:.2f} kHz')

max_stars_repo_name: bsxfun/pffdtd

max_issues_repo_name: bsxfun/pffdtd

max_forks_repo_name: bsxfun/pffdtd

reason: import numpy,from numpy,import scipy


text:
import numpy as np
from .cyfisher import cy_fisher

def fisher(x, means, covariances, priors, normalized=False, square_root=False,
           improved=False, fast=False, verbose=False):
    r"""
    Computes the Fisher vector encoding of the vectors ``x`` relative to
    the diagonal covariance Gaussian mixture model with ``means``,
    ``covariances``, and prior mode probabilities ``priors``.

    By default, the standard Fisher vector is computed.

    Parameters
    ----------
    x : [D, N]  `float32` `ndarray`
        One column per data vector (e.g. a SIFT descriptor)
    means :  [F, N]  `float32` `ndarray`
        One column per GMM component.
    covariances :  [F, N]  `float32` `ndarray`
        One column per GMM component (covariance matrices are assumed diagonal,
        hence these are simply the variance of each data dimension).
    priors :  [F, N]  `float32` `ndarray`
        Equal to the number of GMM components.
    normalized : `bool`, optional
        If ``True``, L2 normalize the Fisher vector.
    square_root : `bool`, optional
        If ``True``, the signed square root function is applied to the return
        vector before normalization.
    improved : `bool`, optional
        If ``True``, compute the improved variant of the Fisher Vector. This is
        equivalent to specifying the ``normalized`` and ``square_root` options.
    fast : `bool`, optional
        If ``True``, uses slightly less accurate computations but significantly
        increase the speed in some cases (particularly with a large number of
        Gaussian modes).
    verbose: `bool`, optional
        If ``True``, print information.

    Returns
    -------
    enc : [k, 1] `float32` `ndarray`
        A vector of size equal to the product of
        ``k = 2 * the n_data_dimensions * n_components``.
    """
    # check for None
    if x is None or means is None or covariances is None or priors is None:
        raise ValueError('A required input is None')

    # validate the gmm parameters
    D = means.shape[0]  # the feature dimensionality
    K = means.shape[1]  # the number of GMM modes
    # N = x.shape[1] is the number of samples
    if covariances.shape[0] != D:
        raise ValueError('covariances and means do not have the same '
                         'dimensionality')

    if priors.ndim != 1:
        raise ValueError('priors has an unexpected shape')

    if covariances.shape[1] != K or priors.shape[0] != K:
        raise ValueError('covariances or priors does not have the correct '
                         'number of modes')

    if x.shape[0] != D:
        raise ValueError('x and means do not have the same dimensionality')

    return cy_fisher(x, means, covariances, priors,
                     np.int32(normalized), np.int32(square_root),
                     np.int32(improved), np.int32(fast), np.int32(verbose))

max_stars_repo_name: tanayag/cyvlfeat

max_issues_repo_name: simmimourya/cyvlfeat

max_forks_repo_name: simmimourya/cyvlfeat