text:
import logging
import socket
import numpy
import time
from cPickle import loads
from scipy import linalg
from matplotlib import pyplot
from multiprocessing import Array
from src.logic import helpers
from src.logic.parallel_process import ProcessParallel
from scipy import *
from numpy import *

class Server:
    def __init__(self,
                 server_address,
                 server_port,
                 true_positions,
                 estimated_positions,
                 sensor_positions,
                 microphone_amount,
                 trials,
                 coordinates,
                 cores_amount):
        self.__x, self.__y, self.__z = coordinates
        self.__server_address = server_address
        self.__microphone_amount = microphone_amount
        self.__server_port = server_port
        self.__true_positions = true_positions
        self.__estimated_positions = estimated_positions
        self.__trials = trials
        self.__sensor_positions = sensor_positions
        self.__distances = []
        self.__time_delays = []
        self.__padding = []
        self.__cores_amount = cores_amount
        self.__microphone_data = None
        self.__raw_microphone_data = []

    def generate_data(self):
        self.generate_source_positions()
        self.generate_distances()
        self.prepare()

    def run(self, received_data):

        # Create a TCP/IP socket
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

        # Bind the socket to the port
        server_address = (self.__server_address, self.__server_port)
        logging.info('Starting up on %s port %s', self.__server_address, self.__server_port)

        sock.bind(server_address)

        microphones_data = {}

        received_data_count = 0
        while received_data_count < self.__microphone_amount:
            logging.info('Waiting to receive message...')

            data, address = sock.recvfrom(65535 - 28)
            logging.info("Received %s", len(data))
            if len(data) == 36:
                received_data[received_data_count] = microphones_data[data]
                received_data_count += 1
                logging.info("Received data from %s microphones", received_data_count)
            else:
                microphone_id = data[0:36]

            if not microphone_id in microphones_data:
                microphones_data[microphone_id] = data[36:]
            else:
                microphones_data[microphone_id] += data[36:]

        logging.info("Received data from all microphones")

    def generate_source_positions(self):
        logging.info('Generating sources positions.')

        for i in range(self.__trials):
            #r = numpy.random.rand(1) * 50
            #t = numpy.random.rand(1) * 2 * math.pi
            r = 0.1 * 50
            t = 0.2 * 50
            z = 0.3 * 20
            x = r * math.cos(t)
            y = r * math.sin(t)
            #z = numpy.random.rand(1) * 20
            self.__true_positions[i, 0] = x
            self.__true_positions[i, 1] = y
            self.__true_positions[i, 2] = z

        logging.info('Generated sources positions.')

    def generate_distances(self):
        logging.info('Generating distances.')

        self.__distances = numpy.zeros((self.__trials, self.__microphone_amount))
        for i in range(self.__trials):
            for j in range(self.__microphone_amount):
                x1 = self.__true_positions[i, 0]
                y1 = self.__true_positions[i, 1]
                z1 = self.__true_positions[i, 2]
                x2 = self.__sensor_positions[j, 0]
                y2 = self.__sensor_positions[j, 1]
                z2 = self.__sensor_positions[j, 2]
                self.__distances[i, j] = math.sqrt((x1 - x2) ** 2 + (y1 - y2) ** 2 + (z1 - z2) ** 2)

        logging.info('Generated distances.')

    def log_results(self):
        for trial_number in range(self.__trials):
            logging.info('Trial number: %d', trial_number + 1)

            logging.info('Estimated X = %.15f, Estimated Y = %.15f, Estimated Z = %.15f',
                         float(self.__estimated_positions[trial_number][0]),
                         float(self.__estimated_positions[trial_number][1]),
                         float(self.__estimated_positions[trial_number][2]))

            logging.info('True X = %.15f, True Y = %.15f, True Z = %.15f',
                         float(self.__true_positions[trial_number][0]),
                         float(self.__true_positions[trial_number][1]),
                         float(self.__true_positions[trial_number][2]))

    def draw_plot(self):
        pyplot.plot(self.__true_positions[:, 0], self.__true_positions[:, 1], 'bd', label='True position')
        pyplot.plot(self.__estimated_positions[:, 0], self.__estimated_positions[:, 1], 'r+',
                    label='Estimated position')
        pyplot.legend(loc='upper right', numpoints=1)
        pyplot.xlabel('X coordinate of target')
        pyplot.ylabel('Y coordinate of target')
        pyplot.title('TDOA Hyperbolic Localization')
        pyplot.axis([-50, 50, -50, 50])
        pyplot.show()

    def prepare(self):
        logging.info('Preparing stage started.')

        self.__time_delays = numpy.divide(self.__distances, 340.29)
        self.__padding = numpy.multiply(self.__time_delays, 44100)

        logging.info('Preparing stage ended.')

    def handle_retrieved_data(self, received_data):
        for i in range(self.__trials):
            x = self.__true_positions[i, 0]
            y = self.__true_positions[i, 1]
            z = self.__true_positions[i, 2]

            data = []
            for j in range(self.__microphone_amount):
                data.append(received_data[j])

            multi_track = numpy.array([loads(raw) for raw in data])
            logging.info('Prepared all data.')
            logging.info('Started source localization.')

            x, y, z = self.locate(self.__sensor_positions, multi_track)

            logging.info('Localized source.')

            self.__estimated_positions[i, 0] = x
            self.__estimated_positions[i, 1] = y
            self.__estimated_positions[i, 2] = z

    def locate(self, sensor_positions, multi_track):
        s = sensor_positions.shape
        len = s[0]

        time_delays = numpy.zeros((len, 1))

        starts = time.time()

        if self.__cores_amount == 1:
            for p in range(len):
                time_delays[p] = helpers.time_delay_function(multi_track[0,], multi_track[p,])
        else:
            pp = ProcessParallel()

            outs = Array('d', range(len))

            ranges = []

            for result in helpers.per_delta(0, len, len / self.__cores_amount):
                ranges.append(result)

            for start, end in ranges:
                pp.add_task(helpers.time_delay_function_optimized, (start, end, outs, multi_track))

            pp.start_all()
            pp.join_all()

            for idx, res in enumerate(outs):
                time_delays[idx] = res

        ends = time.time()

        logging.info('%.15f passed for localization computation trial.', ends - starts)

        Amat = numpy.zeros((len, 1))
        Bmat = numpy.zeros((len, 1))
        Cmat = numpy.zeros((len, 1))
        Dmat = numpy.zeros((len, 1))

        for i in range(2, len):
            x1 = sensor_positions[0, 0]
            y1 = sensor_positions[0, 1]
            z1 = sensor_positions[0, 2]
            x2 = sensor_positions[1, 0]
            y2 = sensor_positions[1, 1]
            z2 = sensor_positions[1, 2]
            xi = sensor_positions[i, 0]
            yi = sensor_positions[i, 1]
            zi = sensor_positions[i, 2]
            if time_delays[i] == 0 and time_delays[1] == 0:
                Amat[i] = 0
                Bmat[i] = 0
                Cmat[i] = 0
                Dmat[i] = 0
                continue

            if time_delays[i] == 0:
                ti_value = 0
            else:
                ti_value = 1 / (340.29 * time_delays[i])

            if time_delays[1] == 0:
                t1_value = 0
            else:
                t1_value = 1 / (340.29 * time_delays[1])

            Amat[i] = ti_value * (-2 * x1 + 2 * xi) - t1_value * (
                    -2 * x1 + 2 * x2)
            Bmat[i] = ti_value * (-2 * y1 + 2 * yi) - t1_value * (
                    -2 * y1 + 2 * y2)
            Cmat[i] = ti_value * (-2 * z1 + 2 * zi) - t1_value * (
                    -2 * z1 + 2 * z2)
            Sum1 = (x1 ** 2) + (y1 ** 2) + (z1 ** 2) - (xi ** 2) - (yi ** 2) - (zi ** 2)
            Sum2 = (x1 ** 2) + (y1 ** 2) + (z1 ** 2) - (x2 ** 2) - (y2 ** 2) - (z2 ** 2)
            Dmat[i] = 340.29 * (time_delays[i] - time_delays[1]) + ti_value * Sum1 - t1_value * Sum2

        M = numpy.zeros((len + 1, 3))
        D = numpy.zeros((len + 1, 1))
        for i in range(len):
            M[i, 0] = Amat[i]
            M[i, 1] = Bmat[i]
            M[i, 2] = Cmat[i]
            D[i] = Dmat[i]

        M = numpy.array(M[2:len, :])
        D = numpy.array(D[2:len])

        D = numpy.multiply(-1, D)

        Minv = linalg.pinv(M)

        T = numpy.dot(Minv, D)
        x = T[0]
        y = T[1]
        z = T[2]

        return x, y, z

    @property
    def padding(self):
        return self.__padding

    @property
    def distances(self):
        return self.__distances

reason: import numpy,from numpy,from scipy


text:
#Documentation at https://matplotlib.org/users/pyplot_tutorial.html
import numpy as np
import matplotlib.pyplot as plt
plt.plot([1,2,3,4])
plt.ylabel('some numbers')

plt.plot([1,2,3,4], [1,4,9,16], 'ro')
plt.axis([0, 6, 0, 20])

# evenly sampled time at 200ms intervals
t = np.arange(0., 5., 0.2)

# red dashes, blue squares and green triangles
plt.plot(t, t, 'r--', t, t**2, 'bs', t, t**3, 'g^')
plt.show()

ext: py

reason: import numpy


text:
""" A collection of timeseries pipelines """

import numpy as np
import pandas as pd

from sklearn.base import BaseEstimator, TransformerMixin

class AlignPandas(BaseEstimator, TransformerMixin):

    def __init__(self, LAGS, HORIZIONS):
        self.max_lag = max(LAGS)
        self.max_horizion = max(HORIZIONS)

    def fit(self, x, y=None):
        return self

    def transform(self, x):
        #  catching the edge case with a hack!
        if self.max_horizion == 0:
            self.max_horizion = 1

        if isinstance(x, pd.Series):
            return x.iloc[self.max_lag:-self.max_horizion]
        elif isinstance(x, pd.DataFrame):
            return x.iloc[self.max_lag:-self.max_horizion, :]
        else:
            raise ValueError('The AlignPandas pipeline uses pandas objects')

class Align(BaseEstimator, TransformerMixin):

    def __init__(self, LAGS, HORIZIONS):
        self.max_lag = max(LAGS)
        self.max_horizion = max(HORIZIONS)

    def fit(self, x, y=None):
        return self

    def transform(self, x):
        #  catching the edge case with a hack!
        if self.max_horizion == 0:
            self.max_horizion = 1

        return x[self.max_lag:-self.max_horizion]

class HourlyCyclicalFeatures(BaseEstimator, TransformerMixin):
    """
    args
    """
    hours_in_day = 24

    def __init__(self):
        pass

    def fit(self, x, y=None):
        return self

    def transform(self, x):
        h = x.index.hour

        sin = np.sin(2 * np.pi * h / self.hours_in_day)
        cos = np.cos(2 * np.pi * h / self.hours_in_day)

        out = pd.DataFrame(index=x.index)
        out.loc[:, 'sin_h'] = sin
        out.loc[:, 'cos_h'] = cos

        return out

class HalfHourlyCyclicalFeatures(BaseEstimator, TransformerMixin):
    """
    args
    """
    hours_in_day = 24

    def __init__(self):
        pass

    def fit(self, x, y=None):
        return self

    def transform(self, x):
        hh = x.index.hour + (x.index.minute / 60)

        sin = np.sin(2 * np.pi * hh / self.hours_in_day)
        cos = np.cos(2 * np.pi * hh / self.hours_in_day)

        out = pd.DataFrame(index=x.index)
        out.loc[:, 'sin_hh'] = sin
        out.loc[:, 'cos_hh'] = cos

        return out

class CyclicalFeatures(BaseEstimator, TransformerMixin):

    def __init__(self, max_value):
        self.max_value = max_value

    def fit(self, x, y=None):
        return self

    def transform(self, x):

        sin = np.sin(2 * np.pi * x / self.max_value)
        cos = np.cos(2 * np.pi * x / self.max_value)

        out = pd.DataFrame(index=x.index)
        out.loc[:, 'sin_hh'] = sin
        out.loc[:, 'cos_hh'] = cos

        return out

class RollingSum(BaseEstimator, TransformerMixin):
    """
    A rolling sum excluding the current observation

    nan values that occur at the top of the transformation are
    filled with 0
    """
    def __init__(self, window):
        self.window = window

    def fit(self, x, y=None):
        return self

    def transform(self, x):
        return x.shift(1).rolling(
            window=self.window,
            min_periods=1
        ).sum().fillna(0)

class WeekendDummy(BaseEstimator, TransformerMixin):
    """
    Creates a dummy column indicating weekday or weekend
    """
    #  the threshold for the .weekday method on a pandas dt index
    thresh = 5

    def __init__(self):
        pass

    def fit(self, x, y=None):
        return self

    def transform(self, x):
        """
        create a column of zeros, fill in the weekends using boolean indexing
        """
        dummies = pd.DataFrame(np.zeros(x.shape[0]),
                               index=x.index,
                               columns=['Weekday/Weekend Dummy'])
        dummies.loc[x.index.weekday >= self.thresh, 'Weekday/Weekend Dummy'] = 1
        return dummies

max_stars_repo_name: ADGEfficiency/forecast

max_issues_repo_name: l-leo/forecast

max_forks_repo_name: l-leo/forecast


text:
import time
import pandas as pd
import numpy as np
from sklearn.preprocessing import StandardScaler, normalize
from sklearn.mixture import GaussianMixture as GMM
from scipy.stats import multivariate_normal, norm
from matplotlib import pyplot as plt, cm as cm, mlab as mlab
from matplotlib.patches import Patch
from sklearn.preprocessing import StandardScaler
# import seaborn as sns; sns.set()
from collections import defaultdict
import os
import math
import time
import pickle
from sklearn.decomposition import PCA, FastICA
from sklearn import linear_model
import scipy.spatial as spatial
from IPython.core.debugger import Pdb

# Define some functions
def createFolder(directory):
    try:
        if not os.path.exists(directory):
            os.makedirs(directory)
    except OSError:
        print ('Error: Creating directory. ' + directory)

def tailored_svd(data):
    # perform singular value decomposition on the given data matrix
    #center the data
    mean = np.mean(data, axis=0)
    data -= mean
    P, D, Q = np.linalg.svd(data, full_matrices=False)
    return P, D, Q

# Create the grid for PC1 and PC2 in a standalone file to avoid replicating code
def createGridForPC12(dGrid,flowArray):
    # Remove endpoints
    ind = [True if not np.isnan(flowArray[i,0,1]) else False for i in range(flowArray.shape[0])]
    fa = flowArray[ind,:,:]
    points2D = fa[:,range(0,2),0]

    u0Min = np.floor(np.min(points2D[:,0] - dGrid) / dGrid) * dGrid # PC1 min
    u0Max = np.ceil(np.max(points2D[:,0] + dGrid) / dGrid) * dGrid # PC1 max
    v0Min = np.floor(np.min(points2D[:,1] - dGrid) / dGrid) * dGrid # PC1 min
    v0Max = np.ceil(np.max(points2D[:,1] + dGrid) / dGrid) * dGrid # PC1 max
    u0Vect = np.arange(u0Min,u0Max,dGrid)
    v0Vect = np.arange(v0Min,v0Max,dGrid)
    return u0Vect,v0Vect

#Vectorize samples
def Ev_given_x_ygrid2(x_grid, y_samples,sigma2):
    n_samples = y_samples.shape[0]
    n_obs = len(pos_v_not_nan)
    x_grid_ex = np.repeat(x_grid.reshape([1,-1]),n_samples*n_obs ,axis=0)
    y_samples_ex = np.tile(y_samples,(n_obs,1))
    xy_ex = np.concatenate((x_grid_ex,y_samples_ex),axis=1)
    d_xy_ex = np.repeat(d_xy[pos_v_not_nan,:],n_samples,axis=0)
    d_v_ex = np.repeat(d_v[pos_v_not_nan,:],n_samples,axis=0)
    phi = np.prod( norm.pdf(d_xy_ex,loc=xy_ex,scale=np.power(sigma2,.5)),axis=1)
    phi2 = np.repeat( phi.reshape([-1,1]), 2, axis=1 )

    v_weighted = d_v_ex * phi2

    num = np.sum(v_weighted,axis=0)
    denom = np.sum(phi2,axis=0)

    velocity_est = num/denom

    return velocity_est

#TF version
def Ev_given_xy_tf(x_grid, y_sample,sigma2):
    m = tf.concat([x_grid,y_sample],axis=0)
    mvn = tfd.MultivariateNormalDiag(    loc=m,    scale_diag=sigma2) #sigma2 is (9,)
    phi_seq = mvn.prob(d_xy_tf)
    phi_seq2 = tf.tile(tf.reshape(phi_seq,[-1,1]),[1,2])
    s = d_v_tf*phi_seq2
    num = tf.reduce_sum(s ,axis=0)
    denom = tf.reduce_sum(phi_seq)
    velocity = num/denom

    return velocity

def Ev_given_x_ygrid_tf(x_grid, y_samples_tf,n_samples,sigma2):

    velocity_each_sample = np.zeros( [n_samples, 2] )

    for i,y_sample in enumerate(y_samples_tf):
        velocity_each_sample[i,:] = Ev_given_xy_tf(x_grid, y_sample,sigma2)
    velocity_est = np.mean(velocity_each_sample,axis=0)

    vel_sum = tf.zeros(shape=[2])
    for i in range(n_samples):
        y_sample = y_samples_tf[i,:]
        vel = Ev_given_xy_tf(x_grid, y_sample,sigma2)
        vel_sum = vel_sum+vel
    velocity_est = vel_sum/n_samples

    return velocity_est

def Ev_given_x_ygrid_tf2(x_grid, y_samples_tf,n_samples,sigma2):

    x_ex = tf.tile(tf.reshape(x_grid,[1,-1]), [n_obs,1])
    x_ex2 = tf.concat( (x_ex,  tf.zeros([n_obes,7])), axis=1 )
    d_xy_temp = d_xy_tf - x_ex2
    d_xy_temp2 = tf.tile( d_xy_temp,[n_samples,1]  )
    y_ex = tf.tile( y_samples_tf, [n_obs,1] )

def calcMeanVectorsSlice(u0Vect,v0Vect,r0,flowArray,minPoints=10):
    # Remove endpoints
    ind = [True if not np.isnan(flowArray[i,0,1]) else False for i in range(flowArray.shape[0])]
    fa = flowArray[ind,:,:]

    points9D = flowArray[:,:,0]
    points9D = points9D[ind,:]
    tree9D = spatial.cKDTree(points9D)

    # Calculate the mean for the remaining seven PCs
    otherMeans = np.empty(7)
    for ii in range(0,flowArray.shape[1]-2):
        otherMeans[ii] = np.mean(flowArray[:,ii+2,0])

    # Initialize the matrices
    du0Mat = np.empty((len(u0Vect),len(v0Vect)))
    dv0Mat = np.empty((len(u0Vect),len(v0Vect)))
    mm0Mat = np.empty((len(u0Vect),len(v0Vect)))

    for i,u0 in enumerate(u0Vect):
        for j,v0 in enumerate(v0Vect):
            z0 = np.hstack((u0,v0,otherMeans))
            neighb = tree9D.query_ball_point(z0,r0)
            if len(neighb) >= minPoints:
                du0Mat[i,j] = np.mean(fa[neighb,0,1])
                dv0Mat[i,j] = np.mean(fa[neighb,1,1])
                #Pdb().set_trace()
                mm0Mat[i,j] = np.sqrt(np.sum(np.power(fa[neighb,0,1] - du0Mat[i,j],2) + np.power(fa[neighb,1,1]-dv0Mat[i,j],2))/len(neighb))
            else:
                du0Mat[i,j] = np.nan
                dv0Mat[i,j] = np.nan
                mm0Mat[i,j] = np.nan
    return du0Mat,dv0Mat,mm0Mat

def calcMeanVectorsWeighted(u0Vect,v0Vect,r0,flowArray,minPoints=10):
    # Using the weighting procedure describe in Supplementary Information
    # dGrid is grid spacing (same for both dimensions, PC1 and PC2)
    # r0 is radius for choosing points

    # Remove endpoints
    ind = [True if not np.isnan(flowArray[i,0,1]) else False for i in range(flowArray.shape[0])]
    fa = flowArray[ind,:,:]
    points2D = fa[:,range(0,2),0]
    tree2D = spatial.cKDTree(points2D)

    du0Mat = np.empty((len(u0Vect),len(v0Vect)))
    dv0Mat = np.empty((len(u0Vect),len(v0Vect)))
    mm0Mat = np.empty((len(u0Vect),len(v0Vect)))

    for i,u0 in enumerate(u0Vect):
        for j,v0 in enumerate(v0Vect):
            neighb = tree2D.query_ball_point([u0,v0],r0)
            if len(neighb) >= minPoints:
                weights = np.empty(len(neighb))
                for k,n in enumerate(neighb):
                    dx = np.sqrt(np.power(points2D[n,0] - u0,2) + np.power(points2D[n,1] - v0,2))
                    d = np.sqrt(np.power(r0,2) - np.power(dx,2))
                    weights[k] = np.power(d,7)
                weights = weights / np.sum(weights)
                #weights = np.ones(len(neighb)) # Uncomment to compare with old approach
                #weights = weights / len(weights)
                du0Mat[i,j] = np.sum(fa[neighb,0,1] * weights)
                #Pdb().set_trace()
                dv0Mat[i,j] = np.sum(fa[neighb,1,1] * weights)
                mm0Mat[i,j] = np.sqrt(np.sum(weights*np.power(fa[neighb,0,1] - du0Mat[i,j],2) + weights*np.power(fa[neighb,1,1]-dv0Mat[i,j],2)))
            else:
                du0Mat[i,j] = np.nan
                dv0Mat[i,j] = np.nan
                mm0Mat[i,j] = np.nan
    return du0Mat,dv0Mat,mm0Mat

def generate_core_data():
    # Read csv data files
    CC_file = "data1.csv" #20 imputed sets
    PC1_file = "data2.csv" #Turchin's PC1s
    #polity_file = os.path.abspath(os.path.join("./..","data","scraped_seshat.csv")) #Info on polities spans and gaps
    CC_df = pd.read_csv(CC_file) # A pandas dataframe
    PC1_df = pd.read_csv(PC1_file) # A pandas dataframe
#polity_df = pd.read_csv(polity_file) # A pandas dataframe

    # Create a dictionary that maps from World Region to Late, Intermediate, and Early NGAs
    regionDict = {"Africa":["Ghanaian Coast","Niger Inland Delta","Upper Egypt"]}
    regionDict["Europe"] = ["Iceland","Paris Basin","Latium"]
    regionDict["Central Eurasia"] = ["Lena River Valley","Orkhon Valley","Sogdiana"]
    regionDict["Southwest Asia"] = ["Yemeni Coastal Plain","Konya Plain","Susiana"]
    regionDict["South Asia"] = ["Garo Hills","Deccan","Kachi Plain"]
    regionDict["Southeast Asia"] = ["Kapuasi Basin","Central Java","Cambodian Basin"]
    regionDict["East Asia"] = ["Southern China Hills","Kansai","Middle Yellow River Valley"]
    regionDict["North America"] = ["Finger Lakes","Cahokia","Valley of Oaxaca"]
    regionDict["South America"] = ["Lowland Andes","North Colombia","Cuzco"]
    regionDict["Oceania-Australia"] = ["Oro PNG","Chuuk Islands","Big Island Hawaii"]

    worldRegions = list(regionDict.keys()) # List of world regions

    # Define some plotting parameters
    t_min = -10000
    t_max = 2000
    pc1_min = -7
    pc1_max = 7
    pc2_min = -7
    pc2_max = 7

    pd.set_option('display.max_columns', None)
    pd.set_option('display.max_rows', None)

    # Do the singular value decomposition
    # Subset only the 9 CCs and convert to a numpy array
    CC_names = ['PolPop', 'PolTerr', 'CapPop', 'levels', 'government','infrastr', 'writing', 'texts', 'money']
    CC_array = CC_df.loc[:, CC_names].values

    # Normalize the data (across 20 imputations, not within each imputation)
    CC_scaled = StandardScaler().fit_transform(CC_array)
    CC_times = CC_df.loc[:, ['Time']].values

    # Do a singular value decomposition
    P, D, Q = tailored_svd(CC_scaled)

    # For each polity, project onto the principle components
    # PC_matrix is 8280 x 9 = (414*20) x 9
    PC_matrix = np.matmul(CC_scaled, Q.T)

    NGAs = CC_df.NGA.unique().tolist() # list of unique NGAs from the dataset

    # Create the data for the flow analysis. The inputs for this data creation are
    # the complexity characteristic dataframe, CC_df [8280 x 13], and the matrix of
    # principal component projections, PC_matrix [8280 x 9]. Each row is an imputed
    # observation for 8280 / 20 = 414 unique polity configurations. CC_df provides
    # key information for each observation, such as NGA and Time.
    #
    # Four arrays are created: movArrayOut, velArrayIn, movArrayIn, and velArrayIn.
    # All four arrays have the dimensions 414 x 9 x 2. mov stands for movements and
    # vel for velocity. 414 is the numbers of observations, 9 is the number of PCs,
    # and the final axis has two elements: (a) the PC value and (b) the change in
    # the PC value going to the next point in the NGA's time sequence (or, for vel,
    # the change divided by the time difference). The "Out" arrays give the
    # movement (or velocity) away from a point and the "In" arrays give the
    # movement (or velocity) towards a point. The difference is set to NA for the
    # last point in each "Out" sequence and the first point in each "In" sequence.
    # In addition, NGA name and time are stored in the dataframe flowInfo (the needed
    # "supporting" info for each  observation).

    # Generate the "Out" datasets
    movArrayOut = np.empty(shape=(0,9,2)) # Initialize the movement array "Out"
    velArrayOut = np.empty(shape=(0,9,3)) # Initialize the velocity array "Out" [location, movement / duration, duration]
    flowInfo = pd.DataFrame(columns=['NGA','Time']) # Initialize the info dataframe

    # Iterate over NGAs to populate movArrayOut, velArrayOut, and flowInfo
    for nga in NGAs:
        indNga = CC_df["NGA"] == nga # boolean vector for slicing by NGA
        times = sorted(np.unique(CC_df.loc[indNga,'Time'])) # Vector of unique times
        for i_t,t in enumerate(times):
            ind = indNga & (CC_df['Time']==t) # boolean vector for slicing also by time
            newInfoRow = pd.DataFrame(data={'NGA': [nga], 'Time': [t]})
            flowInfo = flowInfo.append(newInfoRow,ignore_index=True)
            newArrayEntryMov = np.empty(shape=(1,9,2))
            newArrayEntryVel = np.empty(shape=(1,9,3))
            for p in range(movArrayOut.shape[1]):
                newArrayEntryMov[0,p,0] = np.mean(PC_matrix[ind,p]) # Average across imputations
                newArrayEntryVel[0,p,0] = np.mean(PC_matrix[ind,p]) # Average across imputations
                if i_t < len(times) - 1:
                    nextTime = times[i_t + 1]
                    nextInd = indNga & (CC_df['Time']==nextTime) # boolean vector for slicing also by time
                    nextVal = np.mean(PC_matrix[nextInd,p])
                    newArrayEntryMov[0,p,1] = nextVal - newArrayEntryMov[0,p,0]
                    newArrayEntryVel[0,p,1] = newArrayEntryMov[0,p,1]/(nextTime-t)
                    newArrayEntryVel[0,p,2] = (nextTime-t)
                else:
                    newArrayEntryMov[0,p,1] = np.nan
                    newArrayEntryVel[0,p,1] = np.nan
                    newArrayEntryVel[0,p,2] = np.nan
            movArrayOut = np.append(movArrayOut,newArrayEntryMov,axis=0)
            velArrayOut = np.append(velArrayOut,newArrayEntryVel,axis=0)

    # Modify movement and velocity arrays to be for movements in rather than movements out
    movArrayIn = np.copy(movArrayOut)
    velArrayIn = np.copy(velArrayOut)
    movArrayIn[:,:,1] = np.nan
    velArrayIn[:,:,1] = np.nan

    ind = np.where([True if np.isnan(movArrayOut[i,0,1]) else False for i in range(movArrayOut.shape[0])])[0]
    loVect = np.insert(ind[0:(-1)],0,0)
    hiVect = ind - 1
    for lo,hi in zip(loVect,hiVect):
        for k in range(lo,hi+1):
            movArrayIn[k+1,:,1] = movArrayOut[k,:,1]
            velArrayIn[k+1,:,1] = velArrayOut[k,:,1]
            velArrayIn[k+1,:,2] = velArrayOut[k,:,2]

    # Next, create interpolated arrays by iterating over NGAs
    movArrayOutInterp = np.empty(shape=(0,9,2)) # Initialize the flow array
    flowInfoInterp = pd.DataFrame(columns=['NGA','Time']) # Initialize the info dataframe
    interpTimes = np.arange(-9600,1901,100)
    for nga in NGAs:
        indNga = CC_df["NGA"] == nga # boolean vector for slicing by NGA
        times = sorted(np.unique(CC_df.loc[indNga,'Time'])) # Vector of unique times
        for i_t,t in enumerate(interpTimes):
            if t >= min(times) and t <= max(times): # Is the time in the NGAs range?
                newInfoRow = pd.DataFrame(data={'NGA': [nga], 'Time': [t]})
                flowInfoInterp = flowInfoInterp.append(newInfoRow,ignore_index=True)
                newArrayEntry = np.empty(shape=(1,9,2))
                for p in range(movArrayOutInterp.shape[1]):
                    # Interpolate using flowArray
                    indFlow = flowInfo['NGA'] == nga
                    tForInterp = np.array(flowInfo['Time'][indFlow],dtype='float64')
                    pcForInterp = movArrayOut[indFlow,p,0]
                    currVal = np.interp(t,tForInterp,pcForInterp)
                    newArrayEntry[0,p,0] = currVal
                    if i_t < len(interpTimes) - 1:
                        nextTime = interpTimes[i_t + 1]
                        nextVal = np.interp(nextTime,tForInterp,pcForInterp)
                        newArrayEntry[0,p,1] = nextVal - currVal
                    else:
                        newArrayEntry[0,p,1] = np.nan
                movArrayOutInterp = np.append(movArrayOutInterp,newArrayEntry,axis=0)

    r0 = 1.5
    minPoints = 20
    dGrid = .2
    u0Vect,v0Vect = createGridForPC12(dGrid,velArrayOut)
    velScaling = 100
    return worldRegions,NGAs,PC_matrix,CC_df,CC_times, CC_scaled, PC1_df,regionDict,t_min,t_max,pc1_min,pc1_max,flowInfo,movArrayOut,velArrayOut,movArrayIn,velArrayIn,flowInfoInterp,movArrayOutInterp,r0,minPoints,dGrid,u0Vect,v0Vect,velScaling

def identifSeq(v):
    # v is a vector-like object
    # identify sequences along with their ranges

    if(len(v) == 1):
      return([(v,0,0)])

    currentVal = v[0]
    currentIndex = 0
    seq = []
    for i,x in enumerate(v[1:]):
        if x != currentVal:
            seq.append((currentVal,currentIndex,i))
            currentVal = x
            currentIndex = i+1
    # Add the last entry
    seq.append((currentVal,currentIndex,i+1))
    return(seq)

max_stars_repo_name: jaewshin/Holocene

max_issues_repo_name: jaewshin/Holocene

max_forks_repo_name: jaewshin/Holocene

reason: import numpy,import scipy,from scipy


text:
# -*- coding: utf-8 -*-
''' Created on Wed Apr  8 19:21:58 2020
@author: ag4915 '''

# -*- coding: utf-8 -*-
''' Created on Wed Apr  3 09:43:26 2019
@author: ag4915 '''

# -*- coding: utf-8 -*-
''' Edited on Wed March 17 18:12:10 2021
@author: jmatailov11 '''

import torch
import torch.nn as nn
import tifffile                         # Python library to store numpy arrays in tiff (Tagged Image File Format) files
import torch.nn.parallel                # Classes and modules to implement and train the nn
import torch.utils.data
import os                               # This module allows us to access functionalities dependent
import numpy as np                      # Python library used for working with arrays
# import torch.distributions as td
from PorousMediaGenerationExample.dcgan_test import Generator        # import class 'Generator' and function 'weight_init'
import torch.backends.cudnn as cudnn    # torch.backends controls the behavior of various backends that Pytorch supports.

def alpha_func(alpha_value):

    params = {
            'imsize' : 64,              # Spatial size of training images. All images will be resized to this size during preprocessing.
            'nc' : 3,                   # Number of channles in the training images. For coloured images this is 3.
            'nz' : 100,                 # Size of the Z latent vector
            'ngf' : 64,                 # Size of feature maps in the generator. The filtes will be multiples of this.
            'ndf' : 16,                 # Size of features maps in the discriminator. The filters will be multiples of this.
            'ngpu': 0,                  # Number of GPUs to be used
            'nepochs' : 15,             # Number of training epochs.
            'lr' : 0.0002,              # Learning rate for optimizers
            'beta1' : 0.5,              # Beta1 hyperparam for Adam optimizer
            'alpha' : alpha_value,      # Size of z space
            'stride' : 16,              # Stride on image to crop
            'num_samples' : 1   }       # Save step.

    cudnn.benchmark = True
    # Enable the inbuilt cudnn auto-tunner to find the best algorithm to use for your hardware

    # Use GPU is available else use CPU:
    # device = torch.device("cuda:0" if(torch.cuda.is_available()) else "cpu")
    device = torch.device('cpu')
    print(device, "will be used.\n")

    size = 64 + (params['alpha']-1)*16
    out_dir = 'output_generated'
    os.makedirs( str(out_dir), exist_ok=True )
    checkpoint = torch.load('PorousMediaGenerationExample/SOFC_mod_out/netG_epoch_42.pth', map_location=device)

    for i in range(0, params['num_samples']):
        if('cuda' in str(device)):
            # Create the generator.
            netG = Generator(params['nz'], params['nc'], params['ngf'], params['ngpu']).to(device)
            netG.load_state_dict(checkpoint)
            netG = nn.DataParallel(netG)
            noise = torch.FloatTensor(1, params['nz'], params['alpha'], params['alpha'], params['alpha']).normal_(0, 1)
            noise = noise.to(device)
        else:
            # Create the generator.
            netG = Generator(params['nz'], params['nc'], params['ngf'], params['ngpu']).to(device)
            netG.load_state_dict(checkpoint)
            netG = nn.DataParallel(netG)
            noise = torch.FloatTensor(1, params['nz'], params['alpha'], params['alpha'], params['alpha']).normal_(0, 1)
            noise = noise.to(device)

        fake = netG(noise)
        print(fake.shape)
        img = fake.cpu()
        img = img.detach().numpy()

        W = img.shape[2]
        H = img.shape[3]
        L = img.shape[4]

        edge = params['stride']/2
        edge = int(edge)

        phase2 = np.zeros([W, H, L])
        phase3 = np.zeros([W, H, L])

        p1 = np.array(img[0][0])
        p2 = np.array(img[0][1])
        p3 = np.array(img[0][2])

        phase2[(p2 > p1) & (p2 > p3)] = 128     # Nickel -> Grey
        phase3[(p3 > p2) & (p3 > p1)] = 255     #  YSZ   -> White

        output_img = np.int_(phase2+phase3)

        ### Crop edges ###
        nW = W-params['stride']
        nH = H-params['stride']
        nL = L-params['stride']
        output_image = np.zeros([1, 1, nW, nH, nL])
        output_image = output_img[0+edge:W-edge, 0+edge:H-edge, 0+edge:L-edge]

        matrix_pores  = (np.equal(output_image,0))   * 1            # Generate pores matrix
        matrix_Nickel = (np.equal(output_image,128)) * 1            # Generate nickel matrix
        matrix_YSZ    = (np.equal(output_image,255)) * 1            # Generate YSZ matrix
        media = matrix_Nickel + matrix_YSZ                          # Generate media matrix by adding pores and nickel matrices

        ### Save cropped image as tiff ###
        new_output = output_image
        new_output = new_output.astype(np.uint8)
        tifffile.imsave(str(out_dir)+'/test_'+str(nW)+'_'+str(nH)+'__{0}.tif'.format(i), new_output)

        return matrix_pores, matrix_Nickel, matrix_YSZ, media


text:
import numpy as np
from typing import Type

from latent_with_splitseqs.memory.replay_buffer_for_latent import LatentReplayBuffer

def get_fixed_seqlen_latent_replay_buffer_class(
        latent_replay_buffer_splitseq_cls: Type[LatentReplayBuffer],
) -> Type[LatentReplayBuffer]:

    class LatentReplayBufferSplitSeqSamplingRandomSeqLen(
            latent_replay_buffer_splitseq_cls):

        def __init__(self,
                     *args,
                     sample_seqlen,
                     **kwargs
                     ):
            super().__init__(
                *args,
                **kwargs
            )
            self.sample_seqlen = sample_seqlen

        def _get_sample_seqlen(self) -> int:
            return self.sample_seqlen

    return LatentReplayBufferSplitSeqSamplingRandomSeqLen

max_stars_repo_name: fgitmichael/SelfSupevisedSkillDiscovery

max_issues_repo_name: fgitmichael/SelfSupevisedSkillDiscovery

max_forks_repo_name: fgitmichael/SelfSupevisedSkillDiscovery


text:
import numpy as np #imported numpy and Assigned as np
import pandas as pd #imported pandas and Assigned as pd
import pickle
df = pd.read_csv('mtsamples.csv')

col = ['medical_specialty','keywords']
df = df[col]
df.columns = ['medical_specialty','keywords']

df['category_id'] = df['medical_specialty'].factorize()[0]
from io import StringIO
category_id_df = df[['medical_specialty', 'category_id']].drop_duplicates().sort_values('category_id')
category_to_id = dict(category_id_df.values)
id_to_category = dict(category_id_df[['category_id', 'medical_specialty']].values)

df.head()

#import matplotlib.pyplot as plt
#fig = plt.figure(figsize=(8,6))
#df.groupby('medical_specialty').description.count().plot.bar(ylim=0)
#plt.show()

from sklearn.feature_extraction.text import TfidfVectorizer
tfidf = TfidfVectorizer(sublinear_tf=True, min_df=5, norm='l2', encoding='latin-1', ngram_range=(1, 2), stop_words='english')
features = tfidf.fit_transform(df.keywords).toarray()
labels = df.category_id
features.shape

pickle.dump(tfidf,open('tfidf.pkl','wb'))

from sklearn.feature_selection import chi2
import numpy as np

#N = 2

from sklearn.model_selection import train_test_split
from sklearn.feature_extraction.text import CountVectorizer
from sklearn.feature_extraction.text import TfidfTransformer
from sklearn.naive_bayes import MultinomialNB
from sklearn.linear_model import LogisticRegression
from sklearn.ensemble import RandomForestClassifier
from sklearn.svm import LinearSVC

####MultinomialNB MODEL PREDICTION
print("Running Multinomial NB Model Training & Testing")
X_train, X_test, y_train, y_test = train_test_split(df['keywords'], df['medical_specialty'], random_state = 42)
count_vect = CountVectorizer()
X_train_counts = count_vect.fit_transform(X_train)
tfidf_transformer = TfidfTransformer()
X_train_tfidf = tfidf_transformer.fit_transform(X_train_counts)

multinb = MultinomialNB().fit(X_train_tfidf,y_train)
print("Creating Multinomial NB Model Pickle Object Code")
pickle.dump(multinb,open('multinb.pkl','wb'))

pickle.dump(count_vect,open('vectorizer.pkl', 'wb'))

####LINEAR SVC MODEL PREDICTION
print("Running LINEAR SVC Model Training & Testing")
from sklearn.model_selection import train_test_split

linear = LinearSVC()

X_train, X_test, y_train, y_test, indices_train, indices_test = train_test_split(features, labels, df.index, test_size=0.22, random_state=42)
linear.fit(X_train, y_train)
y_pred = linear.predict(X_test)

print("Creating LINEAR SVC Model Pickle Object Code")
pickle.dump(linear,open('linearsvc.pkl', 'wb'))

####Random Forest Classifier MODEL PREDICTION
print("Running Random Forest Classifier Model Training & Testing")
from sklearn.model_selection import train_test_split

random = RandomForestClassifier()
X_train, X_test, y_train, y_test, indices_train, indices_test = train_test_split(features, labels, df.index, test_size=0.22, random_state=42)
random.fit(X_train, y_train)
y_pred = random.predict(X_test)

print("Creating Random Forest Classifier Model Pickle Object Code")
pickle.dump(random,open('random.pkl', 'wb'))
pickle.dump(id_to_category,open('id_to_category.pkl','wb'))

###LogisticRegression MODEL PREDICTION
print("Running  LogisticRegression Model Training & Testing")
from sklearn.model_selection import train_test_split

logic = LogisticRegression()
X_train, X_test, y_train, y_test, indices_train, indices_test = train_test_split(features, labels, df.index, test_size=0.22, random_state=42)
logic.fit(X_train, y_train)
y_pred = logic.predict(X_test)

print("Creating LogisticRegression Model Pickle Object Code")
pickle.dump(logic,open('logic.pkl', 'wb'))

print("Model.py Executed and Created Respective Pickle Files")
print("Now Run App")
print("TESTING ACCURACY")


text:
import numpy as np # linear algebra
import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)
import matplotlib.pyplot as plt
import seaborn as sns
import torch
import sys
import traceback

# prep
from sklearn.model_selection import train_test_split
from sklearn import preprocessing
from sklearn.datasets import make_classification
from sklearn.preprocessing import binarize, LabelEncoder, MinMaxScaler
from sklearn import metrics

# models
from torch import nn
from torch import optim
from torch.autograd import Variable
import os
import sys

sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from demo.runners.support.utils import log_msg

async def validate_model(model_path):
    log_msg("COORDINATOR IS CLEANING THE VALIDATION SET")

    #Read in Data
    train_df = pd.read_csv('data/data.csv')

    log_msg("COORDINATOR DATA", train_df)

    ########## START DATA CLEANING ###############
    #Let’s get rid of the variables "Timestamp",“comments”, “state” just to make our lives easier.
    train_df = train_df.drop(['comments'], axis= 1)
    train_df = train_df.drop(['state'], axis= 1)
    train_df = train_df.drop(['Timestamp'], axis= 1)

    # Assign default values for each data type
    defaultInt = 0
    defaultString = 'NaN'
    defaultFloat = 0.0

    # Create lists by data tpe
    intFeatures = ['Age']
    stringFeatures = ['Gender', 'Country', 'self_employed', 'family_history', 'treatment', 'work_interfere',
                     'no_employees', 'remote_work', 'tech_company', 'anonymity', 'leave', 'mental_health_consequence',
                     'phys_health_consequence', 'coworkers', 'supervisor', 'mental_health_interview', 'phys_health_interview',
                     'mental_vs_physical', 'obs_consequence', 'benefits', 'care_options', 'wellness_program',
                     'seek_help']
    floatFeatures = []

    # Clean the NaN's
    for feature in train_df:
        if feature in intFeatures:
            train_df[feature] = train_df[feature].fillna(defaultInt)
        elif feature in stringFeatures:
            train_df[feature] = train_df[feature].fillna(defaultString)
        elif feature in floatFeatures:
            train_df[feature] = train_df[feature].fillna(defaultFloat)
        else:
            log_msg('Error: Feature %s not recognized.' % feature)

    #clean 'Gender'
    #Slower case all columm's elements
    gender = train_df['Gender'].str.lower()
    #log_msg(gender)

    #Select unique elements
    gender = train_df['Gender'].unique()

    #Made gender groups
    male_str = ["male", "m", "male-ish", "maile", "mal", "male (cis)", "make", "male ", "man","msle", "mail", "malr","cis man", "Cis Male", "cis male"]
    trans_str = ["trans-female", "something kinda male?", "queer/she/they", "non-binary","nah", "all", "enby", "fluid", "genderqueer", "androgyne", "agender", "male leaning androgynous", "guy (-ish) ^_^", "trans woman", "neuter", "female (trans)", "queer", "ostensibly male, unsure what that really means"]
    female_str = ["cis female", "f", "female", "woman",  "femake", "female ","cis-female/femme", "female (cis)", "femail"]

    for (row, col) in train_df.iterrows():

        if str.lower(col.Gender) in male_str:
            train_df['Gender'].replace(to_replace=col.Gender, value='male', inplace=True)

        if str.lower(col.Gender) in female_str:
            train_df['Gender'].replace(to_replace=col.Gender, value='female', inplace=True)

        if str.lower(col.Gender) in trans_str:
            train_df['Gender'].replace(to_replace=col.Gender, value='trans', inplace=True)

    #Get rid of bullshit
    stk_list = ['A little about you', 'p']
    train_df = train_df[~train_df['Gender'].isin(stk_list)]

    #complete missing age with mean
    train_df['Age'].fillna(train_df['Age'].median(), inplace = True)

    # Fill with media() values < 18 and > 120
    s = pd.Series(train_df['Age'])
    s[s<18] = train_df['Age'].median()
    train_df['Age'] = s
    s = pd.Series(train_df['Age'])
    s[s>120] = train_df['Age'].median()
    train_df['Age'] = s

    #Ranges of Age
    train_df['age_range'] = pd.cut(train_df['Age'], [0,20,30,65,100], labels=["0-20", "21-30", "31-65", "66-100"], include_lowest=True)

    #There are only 0.20% of self work_interfere so let's change NaN to "Don't know
    #Replace "NaN" string from defaultString

    train_df['work_interfere'] = train_df['work_interfere'].replace([defaultString], 'Don\'t know' )

    #Encoding data
    labelDict = {}
    for feature in train_df:
        le = preprocessing.LabelEncoder()
        le.fit(train_df[feature])
        le_name_mapping = dict(zip(le.classes_, le.transform(le.classes_)))
        train_df[feature] = le.transform(train_df[feature])
        # Get labels
        labelKey = 'label_' + feature
        labelValue = [*le_name_mapping]
        labelDict[labelKey] =labelValue

    #Get rid of 'Country'
    train_df = train_df.drop(['Country'], axis= 1)

    # Scaling Age
    scaler = MinMaxScaler()
    train_df['Age'] = scaler.fit_transform(train_df[['Age']])

    # define X and y
    feature_cols = ['Age', 'Gender', 'family_history', 'benefits', 'care_options', 'anonymity', 'leave', 'work_interfere']
    X = train_df[feature_cols]
    y = train_df.treatment

    # split X and y into training and testing sets
    X_test, y_test = X, y

    # Transform pandas dataframe to torch tensor for DL

    x_test_data = torch.from_numpy(X_test.values)
    x_test_data = x_test_data.float()

    y_test_data = []
    for data in y_test.values:
        y_test_data.append([data])
    y_test_data = torch.tensor(y_test_data).float()

    log_msg("VALIDATION SET HAS BEEN CLEANED")

    ########## END DATA CLEANING ###############

    log_msg(model_path)
    # Pull in model

    model = torch.load(model_path)

    log_msg("HOSPITAL MODEL LOADED")
    log_msg("\nPRINTING PARAMETERS:\n\n")

    log_msg(model)

    log_msg('\n')

    for name, param in model.named_parameters():
        if param.requires_grad:
            log_msg(name, param.data)

    # Validation Logic
    log_msg("\n\n\nHOSPITAL IS VALIDATING")

    #BINARIZE PREDICTION FOR CONFUSION MATRIX

    pred = []

    for data in  model(x_test_data):
        if data > .5:
            pred.append(1)
        else:
            pred.append(0)

    confusion = metrics.confusion_matrix(pred, y_test_data)

    log_msg("Model loss on validation set: ", (model(x_test_data) - y_test_data).sum())
    log_msg("Confusion Matrix:\n                Actual_True, Actual_False \n Predicted_True    ",confusion[1][1],"   |     ",confusion[1][0],"    \n Predicted_False   ",confusion[0][1],"     |      ",confusion[0][0],"    \n")

    return True

max_stars_repo_name: harshkasyap/PyAriesFL

max_issues_repo_name: totemprotocol/aries-fl

max_forks_repo_name: totemprotocol/aries-fl


text:
local_machine = False

import numpy as np
if local_machine:
    import numpy as K
    import numpy as tf
    K.atan = np.arctan
    np.pow = np.power
else:
    import tensorflow.keras.backend as K
    import tensorflow as tf

def mean_squared_error(y_true, y_predicted):
    err = K.square(y_true - y_predicted)
    return K.mean(err, axis=-1)

def sivia_skilling_loss(y_true, y_predicted):
    epsilon = 10 ** -6
    gamma = 0.2
    r = abs(y_true - y_predicted)/gamma
    factor = - K.log((1 - K.exp(-(r ** 2 + epsilon) / 2)) / (r ** 2 + epsilon))
    return K.mean(factor, axis=-1)

def cauchy_selection_loss_fixed_boundary_trainable_gamma(layer, y_max=1, y_min=-1):
    L = ConditionalCauchySelectionLoss(gamma=layer.gamma, y_max=y_max, y_min=y_min)

    def loss(y_true, y_predicted):
        return L.loss(y_true, y_predicted)
    return loss

def cauchy_selection_loss_fixed_boundary(gamma=0.2, y_max=1, y_min=-1):
    L = ConditionalCauchySelectionLoss(gamma=gamma, y_max=y_max, y_min=y_min)

    def loss(y_true, y_predicted):
        return L.loss(y_true, y_predicted)
    return loss

def cauchy_selection_loss_trainable_gamma(layer, y_max=1, y_min=-1):
    def loss(y_true, y_pred):
        gamma = layer.gamma
        r = (y_true - y_pred)/gamma
        tail_term = K.log(1 + K.square(r))
        selection_term = K.log(tf.atan((y_max - y_pred)/gamma) - tf.atan((y_min - y_pred)/gamma))
        loss = tail_term + selection_term + K.log(gamma)
        return K.mean(loss, axis=-1)
    return loss

def cauchy_selection_loss(gamma=0.2, y_max=1, y_min=-1):
    def loss(y_true, y_predicted):
        r = (y_true - y_predicted) / gamma
        tail_term = K.log(1 + K.square(r))
        selection_term = K.log(tf.atan((y_max - y_predicted) / gamma) - tf.atan((y_min - y_predicted) / gamma))
        loss = tail_term + selection_term
        return K.mean(loss, axis=-1)
    return loss

# Numpy equivalents

def cauchy_selection_loss_numpy(y_true, y_predicted, y_max=1, y_min=-1, gamma=0.2):
    r = (y_true - y_predicted)/gamma

    tail_term = np.log(1 + np.square(r))
    selection_term = np.log(np.arctan((y_max - y_predicted)/gamma) - np.arctan((y_min - y_predicted)/gamma))

    loss = tail_term + selection_term
    return np.mean(loss, axis=-1)

def dc_dx(y_true, y_predicted, y_max=1, y_min=-1, gamma=0.2):
    d1 = - 2 * (y_true - y_predicted)/(gamma**2 + (y_true - y_predicted)**2)

    term1 = 1/(np.arctan((y_max - y_predicted)/gamma) - np.arctan((y_min - y_predicted)/gamma))
    term2 = (- gamma / (gamma**2 + (y_max - y_predicted)**2)) - (- gamma / (gamma**2 + (y_min - y_predicted)**2))
    d2 = term1 * term2
    return np.mean(d1 + d2, axis=-1)

def sivia_skilling_loss_numpy(y_true, y_predicted, gamma=1):
    r = abs(y_true - y_predicted)/gamma
    factor = - np.log((1 - np.exp(-r**2 / 2)) / r ** 2)
    norm = np.log(gamma * np.sqrt(2*np.pi))
    return factor + norm

def squared_error_numpy(y_true, y_predicted, derivative=False):
    if derivative is True:
        return np.mean(- 2 * (y_true - y_predicted), axis=-1)
    else:
        return np.mean((y_true - y_predicted)**2, axis=-1)

class ConditionalCauchySelectionLoss:
    # Class for loss Cauchy + selection loss function with fixed boundary
    def __init__(self, gamma=0.2, y_max=1, y_min=-1):
        self.g = gamma
        self.y_maximum = y_max
        self.y_minimum = y_min
        if local_machine:
            self.e = np.e
        else:
            self.e = K.constant(np.e, dtype="float32")

    def loss(self, y_true, y_pred):
        return self._loss(y_true, y_pred)

    def _loss(self, y_true, y_pred):
        zeros = K.zeros_like(y_pred)

        mask_range = K.less_equal(K.abs(y_pred), K.ones_like(y_pred))
        range_term = tf.where(mask_range, self.loss_range(y_true, y_pred), zeros)

        mask_neg = K.less(y_pred, -1 * K.ones_like(y_pred))
        negative_term = tf.where(mask_neg, self.loss_neg(y_true, y_pred), zeros)

        mask_pos = K.less(K.ones_like(y_pred), y_pred)
        positive_term = tf.where(mask_pos, self.loss_pos(y_true, y_pred), zeros)

        loss = negative_term + range_term + positive_term
        return K.mean(loss, axis=-1)

    def dloss(self, y_true, y_pred):
        zeros = K.zeros_like(y_pred)

        mask_range = K.less_equal(K.abs(y_pred), K.ones_like(y_pred))
        range_term = tf.where(mask_range, self.deriv_loss_range(y_true, y_pred), zeros)

        mask_neg = K.less(y_pred, -1 * K.ones_like(y_pred))
        negative_term = tf.where(mask_neg, self.deriv_loss_neg(y_true, y_pred), zeros)

        mask_pos = K.less(K.ones_like(y_pred), y_pred)
        positive_term = tf.where(mask_pos, self.deriv_loss_pos(y_true, y_pred), zeros)

        loss = negative_term + range_term + positive_term
        return K.mean(loss, axis=-1)

    def loss_pos(self, y_true, y_pred):
        g, y_maximum, e = self.g, self.y_maximum, self.e
        alpha_pos = self.alpha(y_maximum, y_true, e, g)
        beta_pos = self.beta(y_maximum, y_true, e, g)
        return self.function_outside_boundary(y_pred, y_maximum, alpha_pos, beta_pos)

    def deriv_loss_pos(self, y_true, y_pred):
        g, y_maximum, e = self.g, self.y_maximum, self.e
        alpha_pos = self.alpha(y_maximum, y_true, e, g)
        return K.exp(K.exp(y_pred) + y_pred) + 2 * alpha_pos * y_pred

    def loss_neg(self, y_true, y_pred):
        g, y_minimum, e = self.g, self.y_minimum, self.e
        alpha_neg = self.alpha(y_minimum, y_true, e, g)
        beta_neg = self.beta(y_minimum, y_true, e, g)
        return self.function_outside_boundary(y_pred, y_minimum, alpha_neg, beta_neg)

    def deriv_loss_neg(self, y_true, y_pred):
        g, y_minimum, e = self.g, self.y_minimum, self.e
        alpha_neg = self.alpha(y_minimum, y_true, e, g)
        return - K.exp(K.exp(- y_pred) - y_pred) + 2 * alpha_neg * y_pred

    def loss_range(self, y_true, y_pred):
        return self.function_inside_boundary(y_true, y_pred, self.g, self.y_maximum, self.y_minimum)

    def deriv_loss_range(self, y_true, y_pred):
        y_max = self.y_maximum
        y_min = self.y_minimum

        d1 = - 2 * (y_true - y_pred) / (self.g ** 2 + (y_true - y_pred) ** 2)

        term1 = 1 / (np.arctan((y_max - y_pred) / self.g) - np.arctan((y_min - y_pred) / self.g))
        term2 = (- self.g / (self.g ** 2 + (y_max - y_pred) ** 2)) + ( self.g / (self.g ** 2 + (y_min - y_pred) ** 2))
        d2 = term1 * term2
        return d1 + d2

    def function_outside_boundary(self, y_pred, y_boundary, alpha, beta):
        return K.exp(K.exp(y_boundary * y_pred)) + alpha * K.square(y_pred) + beta

    def function_inside_boundary(self, y_true, y_pred, gamma, y_max, y_min):
        r = (y_true - y_pred) / gamma
        f = K.log(gamma) + K.log(1 + K.square(r)) + \
            K.log(tf.atan((y_max - y_pred) / gamma) - tf.atan((y_min - y_pred) / gamma))
        return f

    def alpha(self, y_boundary, y_true, e_const, gamma):
        term1 = - K.exp(1 + e_const) / 2
        term2 = - y_boundary * (y_true - y_boundary) / (K.pow(-y_boundary + y_true, 2) + K.pow(gamma, 2))
        term3 = - 2 / ((4 * gamma + K.pow(gamma, 3)) * tf.atan(2 / gamma))
        return term1 + term2 + term3

    def beta(self, y_boundary, y_true, e_const, gamma):
        term1 = 0.5 * (-2 + e_const) * K.exp(e_const)
        term2 = y_boundary * (y_true - y_boundary) / (K.pow(-y_boundary + y_true, 2) + K.pow(gamma, 2))
        term3 = 2 / ((4 * gamma + K.pow(gamma, 3)) * tf.atan(2 / gamma))
        term4 = K.log(1 + (K.pow(- y_boundary + y_true, 2) / K.pow(gamma, 2)))
        term5 = K.log(gamma)
        term6 = K.log(tf.atan(2 / gamma))
        return term1 + term2 + term3 + term4 + term5 + term6

max_stars_repo_name: finn-dodgson/DeepHalos

max_issues_repo_name: finn-dodgson/DeepHalos

max_forks_repo_name: finn-dodgson/DeepHalos


text:
import gym
import numpy as np

from rltf.agents      import LoggingAgent
from rltf.memory      import PGBuffer
from rltf.monitoring  import Monitor

class AgentPG(LoggingAgent):

  def __init__(self,
               env_maker,
               model,
               gamma,
               lam,
               rollout_len,
               stop_step,
               vf_iters=1,
               stack_frames=3,
               **agent_kwargs
              ):
    """
    Args:
      env_maker: callable. Function that takes the mode of an env and retruns a new environment instance
      gamma: float. Discount factor for GAE(gamma, lambda)
      lam: float. Lambda value for GAE(gamma, lambda)
      rollout_len: int. Number of agent steps before taking a policy gradient step
      stop_step: int. Total number of agent steps
      vf_iters: int. Number of value function training steps in a single epoch
    """

    super().__init__(**agent_kwargs)

    assert self.log_period % rollout_len == 0, "Log period must be divisible by rollout length"
    if self.eval_len > 0:
      assert self.eval_period % rollout_len == 0, "Eval period must be divisible by rollout length"
    if self.save_period > np.inf:
      assert self.save_period % rollout_len == 0, "Save period must be divisible by rollout length"

    self.env_train = Monitor(
                      env=env_maker('t'),
                      log_dir=self.model_dir,
                      mode='t',
                      log_period=None,
                      video_spec=self.video_period,
                    )

    self.env_eval  = Monitor(
                      env=env_maker('e'),
                      log_dir=self.model_dir,
                      mode='e',
                      log_period=self.eval_len,
                      video_spec=self.video_period,
                      eval_period=self.eval_period,
                    )

    self.rollout_len  = rollout_len
    self.stop_step    = stop_step
    self.epochs       = self.stop_step // self.rollout_len
    self.vf_iters     = vf_iters

    self.gamma  = gamma
    self.lam    = lam

    # Get environment specs
    obs_shape, obs_dtype, act_shape, act_dtype, obs_len = self._state_action_spec(stack_frames)

    # Initialize the model and the experience buffer
    self.model  = model(obs_shape=obs_shape, act_space=self.env_train.action_space, **self.model_kwargs)
    self.buffer = PGBuffer(self.rollout_len, obs_shape, obs_dtype, act_shape, act_dtype, obs_len)

  def _train(self):
    # Get the function that generates trajectories
    run_policy = self._trajectory_generator(self.rollout_len)

    for t in range(self.agent_step+1, self.epochs+1):
      if self._terminate:
        break

      step = t * self.rollout_len

      # Collect experience in the environment
      run_policy()

      # Train the model
      self._run_train_step(t)

      if step % self.log_period == 0:
        self.env_train.monitor.log_stats()

      # Stop and run evaluation procedure
      if self.eval_len > 0 and step % self.eval_period == 0:
        self._eval_agent()

      # Update the agent step - corresponds to number of epochs
      self.agent_step = step

      # Save **after** agent step is correct and completed
      if step % self.save_period == 0:
        self.save()

  def _trajectory_generator(self, horizon):
    """
    Args:
      horizon: int. Number of steps to run before yielding the trajectories
    Returns:
      A function which generates trajectories
    """

    obs = self.reset()

    def run_env():
      nonlocal obs

      # Clear the buffer to avoid using old data
      self.buffer.reset()

      for _ in range(horizon):
        if self._terminate:
          return

        # Get an action to run and the value function estimate of this state
        action, vf, logp = self._action_train(obs)

        # Run action
        next_obs, reward, done, info = self.env_train.step(action)

        # Store the effect of the action taken upon obs
        self.buffer.store(obs, action, reward, done, vf, logp)

        # Reset the environment if end of episode
        if done:
          next_obs = self.reset()
        obs = next_obs

      # Store the value function for the next state. Needed to compute GAE(lambda)
      if not done:
        _, next_vf, _ = self._action_train(obs)
      else:
        next_vf = 0

      # Compute GAE(gamma, lambda) and TD(lambda)
      self.buffer.compute_estimates(self.gamma, self.lam, next_vf)

    return run_env

  def _get_feed_dict(self, batch, t):
    feed_dict = {
      self.model.obs_ph:              batch["obs"],
      self.model.act_ph:              batch["act"],
      self.model.adv_ph:              batch["adv"],
      self.model.ret_ph:              batch["ret"],
      self.model.old_logp_ph:         batch["logp"],
      self.model.pi_opt_conf.lr_ph:   self.model.pi_opt_conf.lr_value(t),
      self.model.vf_opt_conf.lr_ph:   self.model.vf_opt_conf.lr_value(t),
    }

    return feed_dict

  def _run_summary_op(self, t, feed_dict):
    if t * self.rollout_len % self.log_period == 0:
      self.summary = self.sess.run(self.summary_op, feed_dict=feed_dict)

  def _run_train_step(self, t):
    batch     = self.buffer.get_data()
    feed_dict = self._get_feed_dict(batch, t)

    train_pi  = self.model.ops_dict["train_pi"]
    train_vf  = self.model.ops_dict["train_vf"]

    # Run a policy gradient step and a value function training step
    self.sess.run([train_pi, train_vf], feed_dict=feed_dict)
    # self.sess.run([self.model.train_op], feed_dict=feed_dict)

    # Run a policy gradient step
    # self.sess.run(train_pi, feed_dict=feed_dict)

    # Train the value function additionally
    for _ in range(self.vf_iters-1):
      if self._terminate:
        break
      self.sess.run(train_vf, feed_dict=feed_dict)

    # Run the summary op to log the changes from the update if necessary
    self._run_summary_op(t, feed_dict)

  def _action_train(self, state):
    data   = self.model.action_train_ops(self.sess, state)
    action = data["action"][0]
    vf     = data["vf"][0]
    logp   = data["logp"][0]
    return action, vf, logp

  def _action_eval(self, state):
    data   = self.model.action_eval_ops(self.sess, state)
    action = data["action"][0]
    return action

  def _save_allowed(self):
    # Prevent saving if the process was terminated - state is most likely inconsistent
    return not self._terminate

  def _state_action_spec(self, stack_frames):
    assert isinstance(self.env_train.observation_space, gym.spaces.Box)

    # Get environment specs
    act_shape = list(self.env_train.action_space.shape)
    obs_shape = list(self.env_train.observation_space.shape)

    # Get obs_shape and obs_dtype
    if len(obs_shape) == 3:
      assert stack_frames > 1
      obs_dtype = np.uint8
      obs_len   = stack_frames
    else:
      obs_dtype = np.float32
      obs_len   = 1

    # Get act_shape and act_dtype
    if isinstance(self.env_train.action_space, gym.spaces.Box):
      act_shape = list(self.env_train.action_space.shape)
      act_dtype = np.float32
    elif isinstance(self.env_train.action_space, gym.spaces.Discrete):
      act_shape = []
      act_dtype = np.uint8
    else:
      raise ValueError("Unsupported action space")

    return obs_shape, obs_dtype, act_shape, act_dtype, obs_len

  def _reset(self):
    pass

  def _append_log_spec(self):
    return [ ( "pi_learn_rate", "f", self.model.pi_opt_conf.lr_value), ]

max_stars_repo_name: psFournier/rltf


text:
import pytest
import numpy as np

from gibbs.mixture import Mixture

def test_invalid_input_composition():
    z = np.array([0.5, 0.42, 0.05])
    omegas = np.array([0.0115, 0.1928, 0.4902])
    Tcs = np.array([190.556, 425.16667, 617.666667])
    Pcs = np.array([4604318.9, 3796942.8, 2.096e6])

    with pytest.raises(ValueError) as message:
        Mixture(z=z, Tc=Tcs, Pc=Pcs, acentric_factor=omegas)

    assert str(message.value) == 'Overall composition must has summation equal 1.'

def test_invalid_input_omegas():
    z = np.array([0.5, 0.42, 0.08])
    omegas = np.array([0.0115, 0.1928, 0.4902, 0.1000])
    Tcs = np.array([190.556, 425.16667, 617.666667])
    Pcs = np.array([4604318.9, 3796942.8, 2.096e6])

    with pytest.raises(ValueError) as message:
        Mixture(z=z, Tc=Tcs, Pc=Pcs, acentric_factor=omegas)

    assert str(message.value) == 'Input values have incompatible dimensions.'

def test_invalid_input_Tc():
    z = np.array([0.5, 0.42, 0.08])
    omegas = np.array([0.0115, 0.1928, 0.4902])
    Tcs = np.array([190.556, 425.16667])
    Pcs = np.array([4604318.9, 3796942.8, 2.096e6])

    with pytest.raises(ValueError) as message:
        Mixture(z=z, Tc=Tcs, Pc=Pcs, acentric_factor=omegas)

    assert str(message.value) == 'Input values have incompatible dimensions.'

def test_invalid_input_Pc():
    z = np.array([0.5, 0.42, 0.08])
    omegas = np.array([0.0115, 0.1928, 0.4902])
    Tcs = np.array([190.556, 425.16667, 617.666667])
    Pcs = np.array([4604318.9, 3796942.8])

    with pytest.raises(ValueError) as message:
        Mixture(z=z, Tc=Tcs, Pc=Pcs, acentric_factor=omegas)

    assert str(message.value) == 'Input values have incompatible dimensions.'

def test_non_physical_input_Tc():
    z = np.array([0.5, 0.42, 0.08])
    omegas = np.array([0.0115, 0.1928, 0.4902])
    Tcs = np.array([190.556, -425.16667, 617.666667])
    Pcs = np.array([4604318.9, 3796942.8, 2.096e6])

    with pytest.raises(ValueError) as message:
        Mixture(z=z, Tc=Tcs, Pc=Pcs, acentric_factor=omegas)

    assert str(message.value) == 'Temperature must be greater than zero.'

def test_non_physical_input_Pc():
    z = np.array([0.5, 0.42, 0.08])
    omegas = np.array([0.0115, 0.1928, 0.4902])
    Tcs = np.array([190.556, 425.16667, 617.666667])
    Pcs = np.array([-4604318.9, 3796942.8, 2.096e6])

    with pytest.raises(ValueError) as message:
        Mixture(z=z, Tc=Tcs, Pc=Pcs, acentric_factor=omegas)

    assert str(message.value) == 'Pressure must be greater than zero.'

def test_mixture_whitson_ex18():
    z = np.array([0.5, 0.42, 0.08])
    omegas = np.array([0.0115, 0.1928, 0.4902])
    Tcs = np.array([190.556, 425.16667, 617.666667])
    Pcs = np.array([4604318.9, 3796942.8, 2.096e6])
    whiston_mixture = Mixture(z=z, Tc=Tcs, Pc=Pcs, acentric_factor=omegas)

    assert whiston_mixture.z.all() == z.all()
    assert whiston_mixture.acentric_factor.all() == omegas.all()
    assert whiston_mixture.Tc.all() == Tcs.all()
    assert whiston_mixture.Pc.all() == Pcs.all()

max_stars_repo_name: volpatto/gibbs

max_issues_repo_name: volpatto/gibbs

max_forks_repo_name: volpatto/gibbs


text:
#!/usr/bin/env python3
""" Server/API class.

Class for the classifier server/API.

MIT License

Copyright (c) 2021 Asociación de Investigacion en Inteligencia Artificial
Para la Leucemia Peter Moss

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

Contributors:
- Adam Milton-Barker

"""

import cv2
import json
import jsonpickle
import os
import requests
import time

import numpy as np

from io import BytesIO
from PIL import Image
from flask import Flask, request, Response

from modules.AbstractServer import AbstractServer

class server(AbstractServer):
    """ Server/API class.

    Class for the classifier server/API.
    """

    def predict(self, req):
        """ Classifies an image sent via HTTP. """

        img = np.fromstring(req.data, np.uint8)
        img = cv2.imdecode(img, cv2.IMREAD_COLOR)

        img = cv2.resize(img, (self.model.data.dim,
                               self.model.data.dim))
        img = self.model.reshape(img)

        return self.model.predict(img)

    def start(self):
        """ Starts the server. """

        app = Flask("AllJetsonNano")

        @app.route('/Inference', methods=['POST'])
        def Inference():
            """ Responds to HTTP POST requests. """

            prediction = self.predict(request)

            if prediction == 1:
                message = "Acute Lymphoblastic Leukemia detected!"
                diagnosis = "Positive"
            elif prediction == 0:
                message = "Acute Lymphoblastic Leukemia not detected!"
                diagnosis = "Negative"

            resp = jsonpickle.encode({
                'Response': 'OK',
                'Message': message,
                'Diagnosis': diagnosis
            })

            return Response(response=resp, status=200, mimetype="application/json")

        app.run(host=self.helpers.get_ip_addr(), port=self.helpers.confs["agent"]["port"])


text:
import torch
import numpy as np

from .grid import make_grid
from .imshow import MultiViewer

from ..module import BaseModuleHook

class Conv2DViewer(BaseModuleHook):
    """Monitor the intermediate outputs of a module.

    Parameters
    ----------
    module : torch.nn.Module
        The module, intermediate outputs of which to monitor. The viewer
        attaches itself to the structure of the module using torch's API,
        which notifies the former of the new intermediate tensors and which
        submodules produced it.

    activation : callable, or dict of callable
        The extra nonlinearity applied to the monitored intermediate outputs.

    normalize : bool, default=True
        Specify whether the feature/channels of the tracked tensors are to
        be normalized individually or altogether.

    tap : torch.nn.Module, an str, or a sequence
        The layers, the outputs of which are tapped by the viewer.

    aspect : pair of numbers
        The width-height aspect ratio.

    viewer : None, or MultiViewer
        The multi-image viewer with which to display the monitored outputs.
        A new one is created if `None`.
    """
    def __init__(self, module, activation=None, *, normalize=True,
                 tap=torch.nn.Conv2d, aspect=(16, 9), viewer=None):
        super().__init__()

        # create own multi-image viewer instance
        if viewer is None:
            viewer = MultiViewer()
        assert isinstance(viewer, MultiViewer)

        self.feature_maps, self.viewers = {}, viewer
        self.aspect, self.normalize = aspect, normalize

        # immediately register self with the module
        if not isinstance(tap, tuple):
            tap = tap,

        *types, = map(lambda T: T if isinstance(T, type) else type(T), tap)
        if all(issubclass(t, torch.nn.Module) for t in types):
            # filter by layer class
            for name, mod in module.named_modules():
                if isinstance(mod, tap):
                    self.register(name, mod)

        elif all(issubclass(t, str) for t in types):
            # use string identifiers: match prefix
            for name, mod in module.named_modules():
                if name.startswith(tap):
                    self.register(name, mod)

        else:
            raise TypeError('`tap` tuple must be either all prefix strings'
                            f' or all `Module` subclasses. Got `{types}`.')

        # sort out the activations
        if callable(activation):
            activation = dict.fromkeys(self.labels, activation)

        elif activation is None:
            activation = {}

        assert isinstance(activation, dict)
        assert all(callable(act) for act in activation.values())

        # ensure mod-keyed dict, in case of string keys in `activation` dict
        mod_activation = {}
        label_to_module = dict(zip(self.labels.values(), self.labels.keys()))
        for mod, act in activation.items():
            if not isinstance(mod, torch.nn.Module):
                mod = label_to_module[mod]
            mod_activation[mod] = act

        assert mod_activation.keys() <= self.labels.keys()
        self.activation = mod_activation

    def exit(self):
        super().exit()
        self.feature_maps.clear()
        self.close()

    @torch.no_grad()
    def on_forward(self, label, mod, inputs, output):
        """Make a grid of grayscale 2d conv outputs."""
        assert isinstance(output, torch.Tensor)
        batch, channels, *spatial = output.shape
        assert batch == 1 and len(spatial) == 2

        # apply the specified activation
        activation = self.activation.get(mod)
        if callable(activation):
            output = activation(output)

        # flatten the channel and batch dims and apply normalization
        tensor = output.cpu().reshape(-1, *spatial)
        if self.normalize:
            # flatten and 0-1 normalize each image independently
            flat = tensor.flatten(1, -1).clone()
            flat = flat.sub_(flat.min(dim=-1, keepdim=True).values)
            upper = flat.max(dim=-1, keepdim=True).values
            flat = flat.div_(upper.clamp_(min=1e-4))

            # rebuild images
            tensor = flat.reshape(-1, *spatial)

        # make a grid of images with a checkerboard placeholder
        self.feature_maps[label] = make_grid(
            tensor, aspect=self.aspect,
            normalize=not self.normalize).numpy()

    def __iter__(self):
        yield from self.feature_maps.items()

    @property
    def isopen(self):
        """Check if any viewers are open."""
        return self.viewers.isopen

    def close(self):
        """Close all viewers."""
        self.viewers.close()

    def draw(self, *, vsync=False):
        """Draw the most recently collected outputs."""
        for label, image in self:
            if label not in self.viewers:
                # create a viewer with a proper caption
                self.viewers.get(label, f'Feature maps of `{label}`')

            # update our own viewer only
            self.viewers.imshow(label, (image * 255).astype(np.uint8))

    def __exit__(self, exc_type, exc_value, traceback):
        """Draw on exit."""
        super().__exit__(exc_type, exc_value, traceback)
        self.draw()

if __name__ == '__main__':
    """A simple illustration of how the Conv2dViewer can be used."""

    from collections import OrderedDict

    module = torch.nn.Sequential(OrderedDict([
        ('block_1', torch.nn.Sequential(OrderedDict([
            ('conv_1', torch.nn.Conv2d(32, 64, 5, 2)),
            ('relu_1', torch.nn.ReLU()),
        ]))),
        ('conv_2', torch.nn.Conv2d(64, 128, 3, 1)),
        ('relu_2', torch.nn.ReLU()),
    ]))

    print('Tapping layers with names strating with `conv_` or `block_`')
    viewer = Conv2DViewer(module, tap=('conv_', 'block_'))

    # do not track the intermediates outside `with` contexts
    viewer.toggle(False)

    single = torch.randn(1, 32, 28, 28)
    while True:
        with viewer:  # track this forward pass
            module(single)  # open the viewers on the first in-context call

        # the forward pass of this batch is not tracked:
        #  the viewer is hard coded to accept single element batches only.
        module(torch.randn(3, 32, 28, 28))
        if not viewer.isopen:
            break

    print('Tapping layers of type `torch.nn.Conv2d`')
    viewer = Conv2DViewer(module, tap=torch.nn.Conv2d)
    while True:
        with viewer:
            module(torch.randn(1, 32, 28, 28))

        if not viewer.isopen:
            break

max_stars_repo_name: ivannz/rlplay

max_issues_repo_name: ivannz/rlplay

max_forks_repo_name: ivannz/rlplay


text:
import argparse
from collections import deque
import copy
from functools import partial
import gc
import logging
from multiprocessing.pool import ThreadPool
import os
import pickle
import random
import sys
import time

# from evostra import EvolutionStrategy
from pytorch_es import EvolutionModule
from pytorch_es.utils.helpers import weights_init
import gym
from gym import logger as gym_logger
import numpy as np
import torch
from torch.autograd import Variable
import torch.nn as nn

gym_logger.setLevel(logging.CRITICAL)

parser = argparse.ArgumentParser()
parser.add_argument('-w', '--weights_path', type=str, required=True, help='Path to save final weights')
parser.add_argument('-c', '--cuda', action='store_true', help='Whether or not to use CUDA')
parser.set_defaults(cuda=False)

args = parser.parse_args()

cuda = args.cuda and torch.cuda.is_available()

# add the model on top of the convolutional base
model = nn.Sequential(
    nn.Linear(4, 100),
    nn.ReLU(True),
    nn.Linear(100, 2),
    nn.Softmax()
)

# model.apply(weights_init)

if cuda:
    model = model.cuda()

def get_reward(weights, model, render=False):
    cloned_model = copy.deepcopy(model)
    for i, param in enumerate(cloned_model.parameters()):
        try:
            param.data.copy_(weights[i])
        except:
            param.data.copy_(weights[i].data)

    env = gym.make("CartPole-v0")
    ob = env.reset()
    done = False
    total_reward = 0
    while not done:
        if render:
            env.render()
            time.sleep(0.05)
        batch = torch.from_numpy(ob[np.newaxis,...]).float()
        if cuda:
            batch = batch.cuda()
        prediction = cloned_model(Variable(batch))
        action = prediction.data.numpy().argmax()
        ob, reward, done, _ = env.step(action)

        total_reward += reward

    env.close()
    return total_reward

partial_func = partial(get_reward, model=model)
mother_parameters = list(model.parameters())

es = EvolutionModule(
    mother_parameters, partial_func, population_size=5, sigma=0.1,
    learning_rate=0.001, threadcount=15, cuda=cuda, reward_goal=200,
    consecutive_goal_stopping=10
)
start = time.time()
final_weights = es.run(400)
end = time.time() - start

pickle.dump(final_weights, open(os.path.abspath(args.weights_path), 'wb'))

reward = partial_func(final_weights, render=True)
print(f"Reward from final weights: {reward}")
print(f"Time to completion: {end}")

max_stars_repo_name: bryanlincoln/gvgai-rl-models

max_issues_repo_name: bryanlincoln/gvgai-rl-models

max_forks_repo_name: bryanlincoln/gvgai-rl-models


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

def p_ss_atma(p_res_atma=250,
              q_liq_sm3day=50,
              mu_cP=1,
              B_m3m3=1.2,
              k_mD=40,
              h_m=10,
              r_e=240,
              r=0.1):
    """
    функция расчета давления в произвольной точке пласта для стационарного решения
    уравнения фильтрации
    p_res_atma - пластовое давление, давление на контуре питания
    q_liq_sm3day - дебит жидкости на поверхности в стандартных условиях
    mu_cP - вязкость нефти (в пластовых условиях)
    B_m3m3 - объемный коэффициент нефти
    k_mD - проницаемость пласта
    h_m - мощность пласта
    r_e - радиус контрура питания
    r - расстояние на котором проводится расчет
    """
    return p_res_atma - 18.41 * q_liq_sm3day * mu_cP * B_m3m3 / k_mD / h_m * np.log(r_e / r)

# r = np.arange(0.1, 100, 1)
# plt.plot(r, p_ss_atma(r=r))
# plt.show()

# first task: two-zones reservoir

def p_ss_atma_two_zones(p_res_atma=250,
                        q_liq_sm3day=50,
                        mu_cP=1,
                        B_m3m3=1.2,
                        h_m=10,
                        r_e=240,
                        r=0.1,
                        r_bound=100,
                        k1_mD=30,
                        k2_mD=10):
    """
    функция, использующая функцию p_ss_atma для расчета композитного двухслойного пласта.
    r_bound - расстояние от скважины до следующей зоны
    k1_mD, k2_mD - проницаемости зон от скважины до контура соответственно
    """
    if r < r_bound:
        k_mD = k1_mD
    else:
        k_mD = k2_mD
    return p_res_atma - 18.41 * q_liq_sm3day * mu_cP * B_m3m3 / k_mD / h_m * np.log(r_e / r)

r = np.arange(0.1, 240, 1)
two_zones = np.array([p_ss_atma_two_zones(r=rr) for rr in r])
# plt.plot(r, two_zones)
# plt.grid()
# plt.show()

# comparison. One-zone reservoir has k = 40 mD meantime two-zone has

one_zone = np.array([p_ss_atma(r=rr) for rr in r])
plt.plot(r, one_zone, r, two_zones)
plt.show()

max_stars_repo_path: hw10/two-zones reservoir.py

max_issues_repo_path: hw10/two-zones reservoir.py

max_forks_repo_path: hw10/two-zones reservoir.py


text:
import numpy as np

class FuzzyPy():
    # Defines TRiangular membership finction f:x->y, with 'a' and 'c' the base of triangle and 'b' is peak
    def trimf(self, x, a, b, c):
        X1 = (x - a) / (b - a)
        X2 = (c - x) / (c - b)
        X3 = np.minimum(X1, X2)
        X4 = np.zeros(x.size)
        y = np.maximum(X3, X4)
        return y

    # Defines Trapezoidal membership finction f:x->y, with 'a' and 'd' the base of trpezoid and 'b' and 'c' the shoulder
    def trapmf(self, x, a, b, c, d):
        X1 = (x - a) / (b - a)
        X2 = np.ones(x.size)
        X3 = (d - x) / (d - c)
        X4 = np.minimum(np.minimum(X1, X2), X3)
        X5 = np.zeros(x.size)
        y = np.maximum(X4, X5)
        return y

    def gaussmf(self, x, c, v):
        """Compute Gaussian Membership function. """
        y = [np.exp(-np.power((i - c) , 2) / (2 * v ** 2.0)) for i in x]
        return y

    def softmax(x):
       """Compute softmax values for each sets of scores in x."""
       return np.exp(x)/np.sum(np.exp(x), axis=0)

class Triangle(FuzzyPy):
    def __init__(self, x, low, middle, high):
        self.low = self.trimf(x, low[0], low[1], low[2])
        self.middle = self.trimf(x, middle[0], middle[1], middle[2])
        self.high = self.trimf(x, high[0], high[1], high[2])

class Triangle5(FuzzyPy):
    def __init__(self, x, low2,low1, middle, high1,high2):
        self.low2 = self.trimf(x, low2[0], low2[1], low2[2])
        self.low1 = self.trimf(x, low1[0], low1[1], low1[2])
        self.middle = self.trimf(x, middle[0], middle[1], middle[2])
        self.high1 = self.trimf(x, high1[0], high1[1], high1[2])
        self.high2 = self.trimf(x, high2[0], high2[1], high2[2])

class Trapezoid(FuzzyPy):
    def __init__(self, x, low, middle, high):
        self.low = self.trapmf(x, low[0], low[1], low[2], low[3])
        self.middle = self.trapmf(x, middle[0], middle[1], middle[2], middle[3])
        self.high = self.trapmf(x, high[0], high[1], high[2], high[3])

class Trapezoid5(FuzzyPy):
    def __init__(self, x, low2,low1, middle, high1,high2):
        self.low2 = self.trapmf(x, low2[0], low2[1], low2[2], low2[3])
        self.low1 = self.trapmf(x, low1[0], low1[1], low1[2], low1[3])
        self.middle = self.trapmf(x, middle[0], middle[1], middle[2], middle[3])
        self.high1 = self.trapmf(x, high1[0], high1[1], high1[2], high1[3])
        self.high2 = self.trapmf(x, high2[0], high2[1], high2[2], high2[3])

class Gauss(FuzzyPy):
    def __init__(self, x, low, middle, high):
        self.low = self.gaussmf(x, low[0], low[1])
        self.middle = self.gaussmf(x, middle[0], middle[1])
        self.high = self.gaussmf(x, high[0], high[1])

class Gauss5(FuzzyPy):
    def __init__(self, x, low2,low1, middle, high1,high2):
        self.low2 = self.gaussmf(x, low2[0], low2[1])
        self.low1 = self.gaussmf(x, low1[0], low1[1])
        self.middle = self.gaussmf(x, middle[0], middle[1])
        self.high1 = self.gaussmf(x, high1[0], high1[1])
        self.high2 = self.gaussmf(x, high2[0], high2[1])

  ## Operators Defined

def intersect(A,B):
     """Intersect two membership functions"""
    return np.minimum(A,B)

def union(A, B):
    """Union of two membership functions"""
    return np.maximum(A, B)

def complement(A):
    """complement of membership"""
    return 1-A

def alphaCut(A,a):
    """Alpha cut on membership function"""
    from copy import deepcopy
    B = deepcopy(A)
    B[(B < a)]= 0
    return B

def add(A,B):
    """Adds two fuzzy membership functions/sets"""
    return np.minimum(A+B,1)

def sub(A,B):
    """Subtracts two fuzzy membership functions/sets"""
    return np.maximum(A-B,0)

max_stars_repo_name: amita-kapoor/Python-Fuzzy

max_issues_repo_name: amita-kapoor/Python-Fuzzy

max_forks_repo_name: amita-kapoor/Python-Fuzzy


text:
import math
import pandas as pd
pd.set_option('precision', 3)

import numpy as np

# minus infinity value
m_inf = -math.inf

def knapsack_optimizer(table):
    """
    Given a budgets/sub-campaigns matrix, resolve the Knapsack problem optimization.
    :param table: budgets/sub-campaigns matrix, each cell contains the number of clicks

        EXAMPLE
        table = [
            [m_inf, 90, 100, 105, 110, m_inf, m_inf, m_inf],
            [0, 82, 90, 92, m_inf, m_inf, m_inf, m_inf],
            [0, 80, 83, 85, 86, m_inf, m_inf, m_inf],
            [m_inf, 90, 110, 115, 118, 120, m_inf, m_inf],
            [m_inf, 111, 130, 138, 142, 148, 155, m_inf]
        ]

    :return: list of budget-indexes correspondent to each sub-campaign
    """

    rows = len(table)
    cols = len(table[0])

    # set negative values to -inf
    for row in range(rows):
        for col in range(cols):
            if table[row][col] < 0:
                table[row][col] = m_inf

    # optimization table
    opt_table = [[] for row in range(rows)]

    # pointer matrix
    opt_indexes = [[] for row in range(rows - 1)]

    # copy the value of the first sub-campaign in the optimization table
    for col in range(cols):
        opt_table[0].append(table[0][col])

    # optimization algorithm
    for row in range(1, rows):
        for col in range(cols):
            temp = []

            for col2 in range(col + 1):
                temp.append(table[row][col2] + opt_table[row - 1][col - col2])

            max_value = max(temp)

            # update tables of values and pointers
            opt_table[row].append(max_value)
            opt_indexes[row - 1].append(temp.index(max_value))

    # optimal cumulative number of clicks
    opt_value = max(opt_table[rows - 1])

    # pointer to the optimal budget column
    opt_col = opt_table[rows - 1].index(opt_value)

    # list of budget-pointers for each sub-campaign
    assignments = [0 for r in range(rows)]

    for row in range(rows - 1, 0, -1):
        # index of the optimal sub-campaign budget
        subc_col = opt_indexes[row - 1][opt_col]

        assignments[row] = subc_col
        opt_col -= subc_col

    # assign the index of the first sub-campaign
    assignments[0] = opt_col

    # return list(enumerate(assignments))
    return assignments

def get_knapsack_values(table, assignments):
    values = []
    for row in range(len(assignments)):
        values.append(table[row][assignments[row]])
    return values

def get_dataframe(table, assignments, columns):
    def highlight(s, map):
        a = next(map)
        attr = 'background-color: yellow'
        return [attr if v == a else '' for v in range(len(s))]

    df = pd.DataFrame(data=table,
                        index=pd.Index(["C" + str(i) for i in range(len(table))]),
                        columns=columns
                        )
    df.insert(len(table[0]),'Budget',assignments)
    df = df.style.apply(highlight, map=iter(assignments), axis=1)
    return df


text:
'''
Generates a summary of a model's layers and dimensionality
'''

import torch
from torch import nn
from torch.autograd import Variable
import numpy as np
import pandas as pd

class Summary(object):

    def __init__(self, model, input_size=(1,1,256,256)):
        '''
        Generates summaries of model layers and dimensions.
        '''
        self.model = model
        self.input_size = input_size

        self.summarize()
        print(self.summary)

    def get_variable_sizes(self):
        '''Run sample input through each layer to get output sizes'''
        input_ = Variable(torch.FloatTensor(*self.input_size), volatile=True)
        mods = list(self.model.modules())
        in_sizes = []
        out_sizes = []
        for i in range(1, len(mods)):
            m = mods[i]
            out = m(input_)
            in_sizes.append(np.array(input_.size()))
            out_sizes.append(np.array(out.size()))
            input_ = out

        self.in_sizes = in_sizes
        self.out_sizes = out_sizes
        return

    def get_layer_names(self):
        '''Collect Layer Names'''
        mods = list(self.model.named_modules())
        names = []
        layers = []
        for m in mods[1:]:
            names += [m[0]]
            layers += [str(m[1].__class__)]

        layer_types = [x.split('.')[-1][:-2] for x in layers]

        self.layer_names = names
        self.layer_types = layer_types
        return

    def get_parameter_sizes(self):
        '''Get sizes of all parameters in `model`'''
        mods = list(self.model.modules())
        sizes = []

        for i in range(1,len(mods)):
            m = mods[i]
            p = list(m.parameters())
            modsz = []
            for j in range(len(p)):
                modsz.append(np.array(p[j].size()))
            sizes.append(modsz)

        self.param_sizes = sizes
        return

    def get_parameter_nums(self):
        '''Get number of parameters in each layer'''
        param_nums = []
        for mod in self.param_sizes:
            all_params = 0
            for p in mod:
                all_params += np.prod(p)
            param_nums.append(all_params)
        self.param_nums = param_nums
        return

    def summary(self):
        '''
        Makes a summary listing with:

        Layer Name, Layer Type, Input Size, Output Size, Number of Parameters
        '''

        df = pd.DataFrame( np.zeros( (len(self.layer_names), 5) ) )
        df.columns = ['Name', 'Type', 'InSz', 'OutSz', 'Params']

        df['Name'] = self.layer_names
        df['Type'] = self.layer_types
        df['InSz'] = self.in_sizes
        df['OutSz'] = self.out_sizes
        df['Params'] = self.param_nums

        self.summary = df
        return

    def summarize(self):
        self.get_variable_sizes()
        self.get_layer_names()
        self.get_parameter_sizes()
        self.get_parameter_nums()
        self.summary()

        return


text:
import OpenGL.GL as gl
import visual
import numpy as np
#-------------------------------------------------------------------------------
# Visual representation of an agent
#-------------------------------------------------------------------------------
class Avatar(object):
    def __init__(self):
        self.agent = None
        self.visCar = None
        self.visFLWheel = None
        self.visFRWheel = None
        self.visRLWheel = None
        self.visRRWheel = None
        self.visSensor = None

    def configure(self, cfg):
        pass

    def setAgent(self, agent):
        self.agent = agent

    def calculateVertices(self):
        self.visCar = visual.DynObj(primitive=gl.GL_TRIANGLE_FAN)
        self.visCar.setBody(self.agent.body)
        self.visCar.color = [0.25,0.55,0.8,1]
        # Calculate vertices manually, since we have an
        # interesting shape for the car
        vList = self.visCar.body.fixtures[0].shape.vertices
        vCount = len(vList)
        verts = np.zeros((vCount+1, 3), 'f')
        for i in xrange(vCount):
            verts[i+1,:2] = vList[i]
        self.visCar.setVertices(verts)

        wheelBody = self.agent.frontLeftWheel.body
        self.visFLWheel = visual.DynObj()
        self.visFLWheel.setBody(wheelBody)
        self.visFLWheel.color = [1,0,0,1]
        self.visFLWheel.verticesFromFixtures(wheelBody.fixtures)

        wheelBody = self.agent.frontRightWheel.body
        self.visFRWheel = visual.DynObj()
        self.visFRWheel.setBody(wheelBody)
        self.visFRWheel.color = [1,0,0,1]
        self.visFRWheel.verticesFromFixtures(wheelBody.fixtures)

        wheelBody = self.agent.rearLeftWheel.body
        self.visRLWheel = visual.DynObj()
        self.visRLWheel.setBody(wheelBody)
        self.visRLWheel.color = [.5,0,0,1]
        self.visRLWheel.verticesFromFixtures(wheelBody.fixtures)

        wheelBody = self.agent.rearRightWheel.body
        self.visRRWheel = visual.DynObj()
        self.visRRWheel.setBody(wheelBody)
        self.visRRWheel.color = [.5,0,0,1]
        self.visRRWheel.verticesFromFixtures(wheelBody.fixtures)

        self.visSensor = visual.SensorObj()
        self.visSensor.setBody(self.agent.body)
        self.visSensor.color = [1,1,1,1]
        self.visSensor.verticesFromFixtures([self.agent.sensor])
        self.visSensor.setVertices(self.agent.sensor)

    def draw(self, prog):
        """ Overrides the default function
        """
        self.visCar.draw(prog)
        self.visFLWheel.draw(prog)
        self.visFRWheel.draw(prog)
        self.visRLWheel.draw(prog)
        self.visRRWheel.draw(prog)
        self.visSensor.draw(prog)


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

# In[1]:

################################
########### Imports ############
################################
from sympy.utilities.lambdify import lambdify
import numpy as np
import matplotlib.pyplot as plt
from ipywidgets import interactive, fixed, FloatSlider, HBox, Layout, Button, Label, Output, VBox
from IPython.display import display
import code7814 as wi
import matplotlib.image as mpimg
import sys
sys.path.append('../../python/')          # Defining path
import dataPython as dp
#getting so that the halo component/total RC curve updates as bh widgets update

def h(r,Mbh,arraysize):
    Me=wi.scale*Mbh*arraysize #units: [bh/dot]*[Msun/bh] * [dot] * [Msun] = Msun --> changes as sliders are moved
    MMM=Me*wi.viso #units: unitless * [Msun] = Msun
    return np.sqrt(MMM*wi.GG/wi.rcut) #is it rc or just r? the model looks better with rcut
    #units: ( [Msun]* [(km/s)^2*(kpc/Msun)] / kpc )^.5 = km/s

def t(r,Mbh,arraysize):
    return np.sqrt((wi.d)**2
               +(wi.b)**2
               +(h(r,Mbh,arraysize))**2
               +(wi.g)**2)
img = mpimg.imread('../python/thing.jpg') #import special snowflake ngc 6814, which has visual diameter about 27.6kpc\

# Define plotting function
def f(arraysize,Mbh):

    #fractions: now changing the number of dots plottd within each bracket, which changes as the slider
    #variable ("arraysize") changes

    #and trim to the first x elements of the pre-caclated radius arrays for each bracket
    arraysize=int(arraysize) #units: dot
    re = wi.rrr[:arraysize]
    anglee=wi.angle[:arraysize]

    #plot for each bracket, two equations for each bracket
    y=wi.c_y+re*np.sin(anglee) #y coordinates
    x=wi.c_x+re*np.cos(anglee) #x coordinates

    #below: changing the physical size of the plotted dots as the bh mass slider changes. the if else statement is fancy
    #but not necessary

    Mbh=1.5*Mbh #judging by eye
    if .1<=Mbh<1:
        Mbh=Mbh**1.5
    elif 1<=Mbh<2:
        Mbh=Mbh**2
    else:
        Mbh=Mbh**3 #display size of each marker

    f, (ax1, ax2) = plt.subplots(1, 2, sharey=False)
    f.set_figheight(15)
    f.set_figwidth(35)

     # Define r, for some reason this isn't working in code7814.py, so put directly here:
    data = dp.getXYdata_wXYerr('../testing/7814/ngc7814data')
    r = np.asarray(data['xx'])
    z=r[0]
    rr=[0,z]
    ax1.scatter(x,y,color="r",marker='o',s=Mbh)
    ax1.imshow(img)
    ax1.axis('off')
    ax2.tick_params(axis='both', which='major', labelsize=30)
    ax2.plot(r,h(r,Mbh,arraysize),label=("Dark Matter Halo"),color='green')
    ax2.plot(rr,[0,h(z,Mbh,arraysize)[0]],color='green') #straight lining connecting halo curve to origin, for visual aesthetic
    ax2.errorbar(r,wi.v_dat,yerr=wi.v_err1,fmt='bo',label='Data')
    ax2.plot(r,wi.b,label=("Bulge"),color='orange')
    ax2.plot(rr,[0,wi.b[0]],color='orange') #straight line for bulge
    ax2.plot(r,wi.d,label=("Disk"),color='purple')
    ax2.plot(rr,[0,wi.d[0]],color='purple') #straight line for disk
    ax2.plot(r,wi.g,label=("Gas"),color='blue')
    ax2.plot(r,t(r,Mbh,arraysize),label=("Total Curve"),color='red')
    ax2.plot(rr,[0,t(z,Mbh,arraysize)[0]],color='red') #straight line for total curve
    ax2.set_xlim([wi.minkpc, 19])
    ax2.set_ylim([0, 400])
    ax2.set_title('NGC 7814',fontsize = 40)
    ax2.set_ylabel('Velocity [km/s]',fontsize = 30)
    ax2.set_xlabel('radius [kpc]',fontsize = 30)
    ax2.legend(loc='upper right',prop={'size': 20},ncol=3)
    ax2.text(.1, 310, 'How close can you get? \nA reduced $\chi^2$ less than 1 is considered a good fit',fontsize=30)
    ax = plt.gca()
    xticks = ax.xaxis.get_major_ticks()
    xticks[0].label1.set_visible(False)
    residuals = wi.v_dat - t(wi.r,Mbh,arraysize)
    # Determining errors
    errors = wi.v_err1**2 #second term is inclination uncertainty
    # Chi squared
    chisquared = np.sum(residuals**2/errors**2)
    #chisquared = stats.chisquare(v_dat,totalcurve(r,M,bpref,dpref,rc,rho00,gpref))
    reducedchisquared = chisquared * (1/(len(wi.r)-6))

    props = dict(boxstyle='round', facecolor='white', alpha=0.5)
    ax2.text(0.4,360,r"Reduced $\chi^2$:"+'\n'+"{:.2f}".format(reducedchisquared),bbox=props,size=30)

style = {'description_width': 'initial'}
layout = {'width':'600px'}

################################
######## Define Sliders ########
################################

#mass of each black hole
Mbh = FloatSlider(min=wi.minmass, max=wi.maxmass, step=wi.minmass,
                value=wi.start,
                description='Mass of each lil black holes [Msun]',
                readout= True,
                readout_format='.1f',
                orientation='horizontal',
                style=style, layout=layout)

#number of projected black dots slider
arraysize = FloatSlider(min=wi.Min, max=wi.Max, step=wi.stepN,
                value=wi.best_A,
                description='Number of lil black holes multiplied by scale %.1e'%wi.scale,
                readout= True,
                readout_format='.2d',
                orientation='horizontal',
                style=style, layout=layout)

def interactive_plot(f):
    interact = interactive(f, Mbh = Mbh, arraysize=arraysize,continuous_update=False)
    return interact

################################
########### Button #############
################################

# Button to revert back to Best Fit
button = Button(
    description="Best Fit",
    button_style='warning', # 'success', 'info', 'warning', 'danger' or ''
    icon='check')
out = Output()

def on_button_clicked(_):
    #display(Javascript('IPython.notebook.execute_cells_below()'))
    Mbh.value = wi.start
    arraysize.value=wi.best_A
button.on_click(on_button_clicked)

# displaying button and its output together
#VBox([button,out,interactive_plot(f)])

# In[ ]:

max_stars_repo_name: villano-lab/galactic-spin

max_issues_repo_name: villano-lab/galactic-spin

max_forks_repo_name: villano-lab/galactic-spin

reason: import numpy,from sympy