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

# # Predicting Student Admissions with Neural Networks
# In this notebook, we predict student admissions to graduate school at UCLA based on three pieces of data:
# - GRE Scores (Test)
# - GPA Scores (Grades)
# - Class rank (1-4)
#
# The dataset originally came from here: http://www.ats.ucla.edu/
#
# ## Loading the data
# To load the data and format it nicely, we will use two very useful packages called Pandas and Numpy. You can read on the documentation here:
# - https://pandas.pydata.org/pandas-docs/stable/
# - https://docs.scipy.org/

# In[1]:

# Importing pandas and numpy
import pandas as pd
import numpy as np

# Reading the csv file into a pandas DataFrame
data = pd.read_csv('student_data.csv')

# Printing out the first 10 rows of our data
data[:10]

# ## Plotting the data
#
# First let's make a plot of our data to see how it looks. In order to have a 2D plot, let's ingore the rank.

# In[2]:

# Importing matplotlib
import matplotlib.pyplot as plt

# Function to help us plot
def plot_points(data):
    X = np.array(data[["gre","gpa"]])
    y = np.array(data["admit"])
    admitted = X[np.argwhere(y==1)]
    rejected = X[np.argwhere(y==0)]
    plt.scatter([s[0][0] for s in rejected], [s[0][1] for s in rejected], s = 25, color = 'red', edgecolor = 'k')
    plt.scatter([s[0][0] for s in admitted], [s[0][1] for s in admitted], s = 25, color = 'cyan', edgecolor = 'k')
    plt.xlabel('Test (GRE)')
    plt.ylabel('Grades (GPA)')

# Plotting the points
plot_points(data)
plt.show()

# Roughly, it looks like the students with high scores in the grades and test passed, while the ones with low scores didn't, but the data is not as nicely separable as we hoped it would. Maybe it would help to take the rank into account? Let's make 4 plots, each one for each rank.

# In[3]:

# Separating the ranks
data_rank1 = data[data["rank"]==1]
data_rank2 = data[data["rank"]==2]
data_rank3 = data[data["rank"]==3]
data_rank4 = data[data["rank"]==4]

# Plotting the graphs
plot_points(data_rank1)
plt.title("Rank 1")
plt.show()
plot_points(data_rank2)
plt.title("Rank 2")
plt.show()
plot_points(data_rank3)
plt.title("Rank 3")
plt.show()
plot_points(data_rank4)
plt.title("Rank 4")
plt.show()

# This looks more promising, as it seems that the lower the rank, the higher the acceptance rate. Let's use the rank as one of our inputs. In order to do this, we should one-hot encode it.
#
# ## TODO: One-hot encoding the rank
# Use the `get_dummies` function in numpy in order to one-hot encode the data.

# In[11]:

# TODO:  Make dummy variables for rank
one_hot_data = pd.concat([data,pd.get_dummies(data['rank'],prefix='rank')],axis=1)

# TODO: Drop the previous rank column
one_hot_data = one_hot_data.drop('rank',axis=1)

# Print the first 10 rows of our data
one_hot_data[:10]

# ## TODO: Scaling the data
# The next step is to scale the data. We notice that the range for grades is 1.0-4.0, whereas the range for test scores is roughly 200-800, which is much larger. This means our data is skewed, and that makes it hard for a neural network to handle. Let's fit our two features into a range of 0-1, by dividing the grades by 4.0, and the test score by 800.

# In[13]:

# Making a copy of our data
processed_data = one_hot_data[:]

# TODO: Scale the columns
processed_data[['gpa']],processed_data[['gre']]=processed_data[['gpa']]/4,processed_data[['gre']]/800
# Printing the first 10 rows of our procesed data
processed_data[:10]

# ## Splitting the data into Training and Testing

# In order to test our algorithm, we'll split the data into a Training and a Testing set. The size of the testing set will be 10% of the total data.

# In[33]:

sample = np.random.choice(processed_data.index, size=int(len(processed_data)*0.9), replace=False)
train_data, test_data = processed_data.iloc[sample], processed_data.drop(sample)

print("Number of training samples is", len(train_data))
print("Number of testing samples is", len(test_data))
print(train_data[:10])
print(test_data[:10])

# ## Splitting the data into features and targets (labels)
# Now, as a final step before the training, we'll split the data into features (X) and targets (y).

# In[34]:

features = train_data.drop('admit', axis=1)
targets = train_data['admit']
features_test = test_data.drop('admit', axis=1)
targets_test = test_data['admit']

print(features[:10])
print(targets[:10])

# # Alternatively,Entire process can be encompassed by few line of codes

# In[ ]:

from sklearn.model_selection import train_test_split
feature_train,feature_test,target_train,target_test=train_test_split(processed_data.loc[:,"gre":"rank_4"],processed_data.loc[:,"admit"],test_size=.1)

print(feature_train[:10])
print(target_train[:10])

# ## Training the 2-layer Neural Network
# The following function trains the 2-layer neural network. First, we'll write some helper functions.

# In[35]:

# Activation (sigmoid) function
def sigmoid(x):
    return 1 / (1 + np.exp(-x))
def sigmoid_prime(x):
    return sigmoid(x) * (1-sigmoid(x))
def error_formula(y, output):
    return - y*np.log(output) - (1 - y) * np.log(1-output)

# # TODO: Backpropagate the error
# Now it's your turn to shine. Write the error term. Remember that this is given by the equation $$ -(y-\hat{y}) \sigma'(x) $$

# In[45]:

# TODO: Write the error term formula
def error_term_formula(y, output):
    return (y-output)*output*(1-output)

# In[46]:

# Neural Network hyperparameters
epochs = 1000
learnrate = 0.5

# Training function
def train_nn(features, targets, epochs, learnrate):

    # Use to same seed to make debugging easier
    np.random.seed(42)

    n_records, n_features = features.shape
    last_loss = None

    # Initialize weights
    weights = np.random.normal(scale=1 / n_features**.5, size=n_features)

    for e in range(epochs):
        del_w = np.zeros(weights.shape)
        for x, y in zip(features.values, targets):
            # Loop through all records, x is the input, y is the target

            # Activation of the output unit
            #   Notice we multiply the inputs and the weights here
            #   rather than storing h as a separate variable
            output = sigmoid(np.dot(x, weights))

            # The error, the target minus the network output
            error = error_formula(y, output)

            # The error term
            #   Notice we calulate f'(h) here instead of defining a separate
            #   sigmoid_prime function. This just makes it faster because we
            #   can re-use the result of the sigmoid function stored in
            #   the output variable
            error_term = error_term_formula(y, output)

            # The gradient descent step, the error times the gradient times the inputs
            del_w += error_term * x

        # Update the weights here. The learning rate times the
        # change in weights, divided by the number of records to average
        weights += learnrate * del_w / n_records

        # Printing out the mean square error on the training set
        if e % (epochs / 10) == 0:
            out = sigmoid(np.dot(features, weights))
            loss = np.mean((out - targets) ** 2)
            print("Epoch:", e)
            if last_loss and last_loss < loss:
                print("Train loss: ", loss, "  WARNING - Loss Increasing")
            else:
                print("Train loss: ", loss)
            last_loss = loss
            print("=========")
    print("Finished training!")
    return weights

weights = train_nn(features, targets, epochs, learnrate)

# ## Calculating the Accuracy on the Test Data

# In[61]:

# Calculate accuracy on test data
tes_out = sigmoid(np.dot(features_test, weights))
predictions = tes_out > 0.7
accuracy = np.mean(predictions == targets_test)
print("Prediction accuracy: {:.3f}".format(accuracy))

# In[ ]:

max_stars_repo_path: Course-1_Neural Network and Deep Learning/First Neural Network/StudentAdmissions.py


text:
# -*- coding: utf-8 -*-
"""
Created on Sun Sep  9 14:51:24 2018

@author: Mohammad Doosti Lakhani
"""
"""
In this file, I implemented functions to calculate accuracy like confusion_matrix or CAP.
You can use this functions in any of the implemented models.
"""

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
from scipy import integrate

def capcurve(y_values, y_preds_proba,title_of_chart):
    num_pos_obs = np.sum(y_values)
    num_count = len(y_values)
    rate_pos_obs = float(num_pos_obs) / float(num_count)
    ideal = pd.DataFrame({'x':[0,rate_pos_obs,1],'y':[0,1,1]})
    xx = np.arange(num_count) / float(num_count - 1)

    y_cap = np.c_[y_values,y_preds_proba[:,1]]
    y_cap_df_s = pd.DataFrame(data=y_cap)
    y_cap_df_s = y_cap_df_s.sort_values([1], ascending=False).reset_index(level = y_cap_df_s.index.names, drop=True)

    print(y_cap_df_s.head(20))

    yy = np.cumsum(y_cap_df_s[0]) / float(num_pos_obs)
    yy = np.append([0], yy[0:num_count-1]) #add the first curve point (0,0) : for xx=0 we have yy=0

    percent = 0.5
    row_index = int(np.trunc(num_count * percent))

    val_y1 = yy[row_index]
    val_y2 = yy[row_index+1]
    if val_y1 == val_y2:
        val = val_y1*1.0
    else:
        val_x1 = xx[row_index]
        val_x2 = xx[row_index+1]
        val = val_y1 + ((val_x2 - percent)/(val_x2 - val_x1))*(val_y2 - val_y1)

    sigma_ideal = 1 * xx[num_pos_obs - 1 ] / 2 + (xx[num_count - 1] - xx[num_pos_obs]) * 1
    sigma_model = integrate.simps(yy,xx)
    sigma_random = integrate.simps(xx,xx)

    ar_value = (sigma_model - sigma_random) / (sigma_ideal - sigma_random)

    fig, ax = plt.subplots(nrows = 1, ncols = 1)
    ax.plot(ideal['x'],ideal['y'], color='grey', label='Perfect Model')
    ax.plot(xx,yy, color='red', label='User Model')
    ax.plot(xx,xx, color='blue', label='Random Model')
    ax.plot([percent, percent], [0.0, val], color='green', linestyle='--', linewidth=1)
    ax.plot([0, percent], [val, val], color='green', linestyle='--', linewidth=1, label=str(val*100)+'% of positive obs at '+str(percent*100)+'%')

    plt.xlim(0, 1.02)
    plt.ylim(0, 1.25)
    plt.title("CAP Curve ("+title_of_chart+") - a_r value ="+str(ar_value))
    plt.xlabel('% of the data')
    plt.ylabel('% of positive obs')
    plt.legend()
# ref = https://github.com/APavlides/cap_curve/blob/master/cap_curve.py

def accuracy_on_cm(confusion_matrix):
    t = np.trace(confusion_matrix)
    f = np.sum(confusion_matrix) - t
    ac = t/(t+f)
    return (t,f,ac)

ext: py

max_stars_repo_name: Nikronic/Machine-Learning-Models

max_issues_repo_name: Nikronic/Machine-Learning-Models

max_forks_repo_name: Nikronic/Machine-Learning-Models

reason: import numpy,from scipy


text:
""" Implementation of Cosmic RIM estimator"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import tensorflow as tf
physical_devices = tf.config.experimental.list_physical_devices('GPU')
print(physical_devices)
world_size = len(physical_devices)
assert len(physical_devices) > 0, "Not enough GPU hardware devices available"
config = tf.config.experimental.set_memory_growth(physical_devices[0], True)

import tensorflow_probability as tfp

import numpy as np
import os, sys, argparse, time
from scipy.interpolate import InterpolatedUnivariateSpline as iuspline
import matplotlib
matplotlib.use('Agg')
from matplotlib import pyplot as plt

from rim_utils import build_rim_parallel_single,  myAdam
from recon_models import Recon_Poisson
from modelpoisson import PoissonData, check_2pt, check_im, get_ps

import flowpm
from flowpm import linear_field, lpt_init, nbody, cic_paint
from flowpm.utils import r2c3d, c2r3d
sys.path.append('../../utils/')
import tools

def str2bool(v):
    if isinstance(v, bool):
       return v
    if v.lower() in ('yes', 'true', 't', 'y', '1'):
        return True
    elif v.lower() in ('no', 'false', 'f', 'n', '0'):
        return False
    else:
        raise argparse.ArgumentTypeError('Boolean value expected.')

parser = argparse.ArgumentParser(description='Process some integers.')
parser.add_argument('--nc', type=int, default=32, help='Grid size')
parser.add_argument('--bs', type=float, default=200, help='Box Size')
parser.add_argument('--nsteps', type=int, default=3, help='')
parser.add_argument('--niter', type=int, default=200, help='Number of iterations/Max iterations')
parser.add_argument('--lr', type=float, default=0.001, help='Learning rate')
parser.add_argument('--optimizer', type=str, default='adam', help='Which optimizer to use')
parser.add_argument('--batch_size', type=int, default=8, help='Batch size')
parser.add_argument('--nsims', type=int, default=100, help='Number of simulations')
parser.add_argument('--nbody', type=str2bool, default=False, help='Number of simulationss')
parser.add_argument('--lpt_order', type=int, default=2, help='Order of LPT Initial conditions')
parser.add_argument('--input_size', type=int, default=16, help='Input layer channel size')
parser.add_argument('--cell_size', type=int, default=16, help='Cell channel size')
parser.add_argument('--rim_iter', type=int, default=10, help='Optimization iteration')
parser.add_argument('--epochs', type=int, default=20, help='Number of epochs')
parser.add_argument('--suffix', type=str, default='', help='Suffix for folder pathname')
parser.add_argument('--batch_in_epoch', type=int, default=20, help='Number of batches in epochs')
parser.add_argument('--plambda', type=float, default=0.10, help='Poisson probability')
parser.add_argument('--parallel', type=str2bool, default=True, help='Parallel or Split')
parser.add_argument('--sims_in_loop', type=int, default=1, help='Number of batches in epochs')

args = parser.parse_args()

nc, bs = args.nc, args.bs
niter = args.niter
lr = args.lr
a0, af, nsteps = 0.1, 1.0,  args.nsteps
stages = np.linspace(a0, af, nsteps, endpoint=True)
plambda = args.plambda
args.stages = stages
args.a0, args.af = a0, af
args.world_size = world_size

#
klin = np.loadtxt('../../data/Planck15_a1p00.txt').T[0]
plin = np.loadtxt('../../data//Planck15_a1p00.txt').T[1]
ipklin = iuspline(klin, plin)
# Compute necessary Fourier kernels
kvec = tools.fftk((nc, nc, nc), boxsize=nc, symmetric=False)
kmesh = (sum(k**2 for k in kvec)**0.5).astype(np.float32)
priorwt = ipklin(kmesh)

args.ipklin = ipklin
args.priorwt = priorwt

datamodel = PoissonData(args)

############################
#RIM params
params = {}
params['input_size'] = args.input_size
params['cell_size'] = args.cell_size
params['strides'] = 2
params['middle_size'] = args.input_size // params['strides']  #lets divide by strides
params['cell_kernel_size'] = 5
params['input_kernel_size'] = 5
params['middle_kernel_size'] = 5
params['output_kernel_size'] = 5
params['rim_iter'] = args.rim_iter
params['input_activation'] = 'tanh'
params['output_activation'] = 'linear'
params['nc'] = nc

rim = build_rim_parallel_single(params)
grad_fn = datamodel.recon_grad
adam = myAdam(params['rim_iter'])
adam10 = myAdam(10*params['rim_iter'])
optimizer = tf.keras.optimizers.Adam(learning_rate=args.lr)
#fid_recon = Recon_Poisson(nc, bs, plambda=plambda, a0=a0, af=af, nsteps=nsteps, nbody=args.nbody, lpt_order=args.lpt_order, anneal=True)

#################################################
suffpath = '_p%03d_single'%(100*plambda) + args.suffix
if args.nbody: ofolder = './models/poisson_L%04d_N%03d_T%02d%s/'%(bs, nc, nsteps, suffpath)
else: ofolder = './models/poisson_L%04d_N%03d_LPT%d%s/'%(bs, nc, args.lpt_order, suffpath)
try: os.makedirs(ofolder)
except Exception as e: print(e)

train_dataset = tf.data.Dataset.range(args.batch_in_epoch)
train_dataset = train_dataset.map(datamodel.pm_data)
train_dataset = train_dataset.prefetch(-1)
test_dataset = tf.data.Dataset.range(1).map(datamodel.pm_data_test).prefetch(-1)

###############################################

def train_step(inputs):
    x_true, y = inputs
    x_init = tf.random.normal(x_true.shape)
    if len(rim.trainable_variables) == 0:
        #Hack since sometimes this si the first time RIM is called and so hasn't been inisitalized
        i = 0
        a, b, c = x_init[i:i+1], y[i:i+1],  x_true[i:i+1]
        _ =  rim(tf.constant(a), tf.constant(b), grad_fn, tf.constant(c))[1] / args.batch_size

    #
    gradients = [0.]*len(rim.trainable_variables)
    #n = args.sims_in_loop
    for i in range(args.batch_size // world_size):
        with tf.GradientTape() as tape:
            #a, b, c = x_init[n*i:n*i+n], y[n*i:n*i+n],  x_true[n*i:n*i+n]
            a, b, c = x_init[i:i+1], y[i:i+1],  x_true[i:i+1]
            loss =  rim(tf.constant(a), tf.constant(b), grad_fn, tf.constant(c))[1] / args.batch_size
        grads = tape.gradient(loss, rim.trainable_variables)
        for j in range(len(grads)):
            gradients[j] = gradients[j] + grads[j]
    optimizer.apply_gradients(zip(gradients, rim.trainable_variables))
    return loss

def test_step(inputs):
    x_true, y = inputs
    x_init = tf.random.normal(x_true.shape)
    x_pred = rim(x_init, y, grad_fn, x_true)[0]
    return x_pred, x_init, x_true, y

###########################################
####Train

losses = []
for epoch in range(args.epochs):
    print("\nFor epoch %d\n"%epoch)
    #TRAIN LOOP
    total_loss = 0.0
    num_batches = 0
    starte = time.time()
    for x in train_dataset:
        startb = time.time()
        loss = train_step(x)
        losses.append(loss.numpy())
        total_loss += loss
        print("epoch %d, num batch %d, loss : "%(epoch, num_batches), loss)
        print("Time taken : ", time.time() - startb)
        num_batches += 1
    train_loss = total_loss / num_batches
    print("Train loss for epoch %d "%epoch, train_loss)
    print("Time taken for epoch %d: "%epoch, time.time() - starte)
    plt.plot(losses)
    plt.savefig(ofolder + 'losses.png')

    ##Test Epoch Training
    for x in test_dataset:
        print('Testing')
        a, b, c, d = test_step(x)
        #print(a.values[0].shape, b.values[0].shape, c.values[0].shape, d.values[0].shape)
        try: pred, x_init, xx, yy = a.values[0], b.values[0], c.values[0], d.values[0]
        except: pred, x_init, xx, yy = a, b, c, d
        pred_adam = adam(x_init, yy, grad_fn)
        pred_adam10 = adam10(x_init, yy, grad_fn)
        print(x_init.shape, xx.shape, yy.shape, pred.shape, pred_adam.shape, pred_adam10.shape)

        check_im(xx[0].numpy(), x_init[0].numpy(), pred[0].numpy(), ofolder + 'rim-im-%d.png'%epoch)
        check_2pt(datamodel, xx, yy, x_init, pred, pred_adam, pred_adam10, ofolder + 'rim-2pt-%d.png'%epoch)

        break

    rim.save_weights(ofolder + '/%d'%epoch)

max_stars_repo_name: modichirag/galference

max_issues_repo_name: modichirag/galference

max_forks_repo_name: modichirag/galference


text:
"""Config for colliding springs system.

This is a system of 4 circles which are fully connected by springs and collide.

To demo this task, navigate to the main directory and run the following:
'''
$ python demo.py --config=spriteworld_physics.configs.colliding_springs \
    --hsv_colors=True
'''
"""

# pylint: disable=import-error

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

import numpy as np
import os
from spriteworld import factor_distributions as distribs
from spriteworld import renderers as spriteworld_renderers
from spriteworld_physics import forces
from spriteworld_physics import generate_sprites
from spriteworld_physics import graph_generators

def get_config(mode=None):
    """Generate environment config.

    Args:
        mode: Unused task mode.

    Returns:
        config: Dictionary defining task/environment configuration. Can be fed
            as kwargs to physics_environment.PhysicsEnvironment.
    """

    # Factor distributions for the sprites.
    factors = distribs.Product([
        distribs.Continuous('x', 0.1, 0.9),
        distribs.Continuous('y', 0.1, 0.9),
        distribs.Discrete('shape', ['circle']),
        distribs.Discrete('scale', [0.1]),
        distribs.Continuous('c0', 0, 1),
        distribs.Continuous('c1', 0.5, 1.),
        distribs.Discrete('c2', [1.]),
        distribs.Continuous('x_vel', -0.02, 0.02),
        distribs.Continuous('y_vel', -0.02, 0.02),
        distribs.Discrete('mass', [1.]),
    ])

    sprite_gen = generate_sprites.generate_sprites(
        factors, num_sprites=4)

    spring_force = forces.Spring(spring_constant=0.01, spring_equilibrium=0.25)
    spring_graph_generator = graph_generators.FullyConnected(
        force=spring_force)
    collision_force = forces.SymmetricShellCollision(shell_radius=0.08)
    collision_graph_generator = graph_generators.LowerTriangular(
        force=collision_force)

    renderers = {
        'image':
            spriteworld_renderers.PILRenderer(
                image_size=(64, 64), anti_aliasing=5)
    }

    config = {
        'graph_generators': (spring_graph_generator, collision_graph_generator),
        'renderers': renderers,
        'init_sprites': sprite_gen,
        'episode_length': 30,
        'bounce_off_walls': True,
        'physics_steps_per_env_step': 10,
        'metadata': {
            'name': os.path.basename(__file__),
            'mode': mode
        }
    }
    return config

reason: import numpy


text:
# plotting is one important aspect is any analysis
# python has a very wide selection of plotting modules
# in our tutorials we will use the matplotlib module which is sufficient for a beginner

# as usual import numpy since we will require it
import numpy as np
# next we will import another important library from plotting
import matplotlib.pyplot as plt

# we are going to see how our random number distributions look like (refer tutorial 04-random-number-generatiion)
# generate a series of 1000 random numbers from a gaussian distribution with mean 0, sigma 1
ran = np.random.normal(0,1,1000) #(mean, sigma, number of samples)
print(ran)
# this is how you plot a histogram
plt.hist(ran, bins=1000)
# you need to show it using the following command (not required in Spyder)
plt.show()

# Assignment
# How do you change the binning of the histogram
# Using the bins argument:
# plt.hist(ran, bins=1000)
# Try this.

# How do you change the axis labels and titles?
# plt.xlabel("Random")
# plt.ylabel("Freq")
# plt.title("Gaussian distribution")
# Try this

# How do you change the axis to log scale?
# plt.yscale('log')
# plt.xscale("log")
# Try this

max_stars_repo_name: deepaksamuel/python-tutorials

max_issues_repo_name: deepaksamuel/python-tutorials

max_forks_repo_name: deepaksamuel/python-tutorials


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

# # [MLPerf Inference Results v0.5](https://github.com/mlperf/inference/tree/master/v0.5)
# ## Automatic results table generation (c) [dividiti](http://dividiti.com/)

# ## Includes

# In[ ]:

import os
import re
import json
from sys import exit

# In[ ]:

import IPython as ip
import pandas as pd
import numpy as np

# In[ ]:

# ## Definitions

# ### Path to the repository with results

# In[ ]:

# Clone the results directory:
# git clone https://github.com/mlperf/inference_results_v0.5 <results_path>
# or
# git clone https://github.com/dividiti/inference_results_v0.5 <results_path>
# results_path = '/home/anton/projects/mlperf/inference_results_v0.5_dividiti'
results_path = os.environ.get('CK_MLPERF_SUBMISSION_ROOT', '')
if results_path == '':
    print("Set CK_MLPERF_SUBMISSION_ROOT to the root of a submissions directory!")
    exit(1)

# ### Path to the cache

# In[ ]:

cache_compression = 'zip'
cache_protocol = 2 # Supported since Python 2.3

cache_file = os.environ.get('CK_MLPERF_DASHBOARD_FILE', 'mlperf-inference-results.zip')
cache_dir = os.environ.get('CK_MLPERF_DASHBOARD_DIR', '')

if cache_dir == '':
    import ck.kernel as ck
    repo_uoa = 'ck-mlperf'
    module_uoa = 'module'
    data_uoa = 'mlperf.inference'
    r = ck.access({'action':'find', 'repo_uoa':repo_uoa, 'module_uoa':module_uoa, 'data_uoa':data_uoa})
    if r['return']>0:
        print('Error: %s' % r['error'])
        exit(1)
    cache_dir = r['path']

cache_path = os.path.join(cache_dir, cache_file)

# ### Divisions

# In[ ]:

divisions = [ 'closed', 'open' ]

# ### Maps for DataFrame construction

# In[ ]:

# Lowercase or camelcase or camelcase with space to camelcase.
scenario_to_str = {
    # SingleStream.
    'singlestream'  : 'SingleStream',
    'SingleStream'  : 'SingleStream',
    'Single Stream' : 'SingleStream',
    # MultiStream.
    'multistream'   : 'MultiStream',
    'MultiStream'   : 'MultiStream',
    'Multi Stream'  : 'MultiStream',
    # Server.
    'server'        : 'Server',
    'Server'        : 'Server',
    # Offline.
    'offline'       : 'Offline',
    'Offline'       : 'Offline',
}

# In[ ]:

division_to_str = {
    # Open.
    'open'   : 'Open',
    'Open'   : 'Open',
    # Closed.
    'closed' : 'Closed',
    'Closed' : 'Closed'
}

# In[ ]:

# dividiti-specific.
system_id_to_processor = {
    'firefly'   : 'Rockchip RK3399',
    'hikey960'  : 'HiSilicon Kirin960',
    'mate10pro' : 'HiSilicon Kirin970',
    'rpi4'      : 'Broadcom BCM2711B0',
}

# In[ ]:

accelerator_name_to_accelerator = {
    'NVIDIA Tesla T4': 'NVIDIA Tesla T4',
    'Nvidia Tesla T4': 'NVIDIA Tesla T4',
    'Tesla T4': 'NVIDIA Tesla T4',
    'Nvidia Tesla V100 SXM3': 'NVIDIA Tesla V100 SXM3',
    'tpu-v3.8': 'Google TPU v3-8', # NB: 8 TPU v3?
    'HanGuang 800': 'Alibaba HanGuang 800',
    'Goya': 'Habana Goya',
}

# ### Metrics for DataFrame construction

# In[ ]:

# Performance metrics: Stream in ms; MultiStream in #streams; Server in QPS; Offline in inputs/s).
performance_columns = [
    'P_{}_{}'.format(task, scenario)
    for task in ['IC1','IC2','OD1','OD2','NMT']
    for scenario in ['SS','MS','S','O']
]
# Accuracy metrics: Image Classification in Top1, %; Object Detection in mAP, %; Machine Translation in BLUE.
accuracy_columns = [
    'A_{}_{}'.format(task, scenario)
    for task in ['IC1','IC2','OD1','OD2','NMT']
    for scenario in ['SS','MS','S','O']
]
# Score columns.
score_columns = performance_columns + accuracy_columns

# ### Non-imagenet benchmarks

# In[ ]:

non_imagenet_benchmarks = {
    # Non-ImageNet benchmarks from the closed division.
    'ssd-small': {
        "name"  : "SSD-MobileNet-v1",
        "width" : 300,
        "height": 300,
    },
    'ssd-large': {
        "name"  : "SSD-ResNet34",
        "width" : 1200,
        "height": 1200,
    },
    'gnmt' : {
        "name"  : "GNMT",
        "width" : -1,
        "height": -1,
    },
    # Non-ImageNet benchmarks from the open division.
    'rcnn-nas-lowproposals' : {
        "name" : "Faster-RCNN-NAS lowproposals",
        "url" : "http://download.tensorflow.org/models/object_detection/faster_rcnn_nas_lowproposals_coco_2018_01_28.tar.gz",
        "width" : 1200,
        "height" : 1200,
    },
    'rcnn-resnet50-lowproposals' : {
        "name" : "Faster-RCNN-ResNet50 lowproposals",
        "url" : "http://download.tensorflow.org/models/object_detection/faster_rcnn_resnet50_lowproposals_coco_2018_01_28.tar.gz",
        "width" : 1024,
        "height" : 600,
    },
    'rcnn-resnet101-lowproposals' : {
        "name" : "Faster-RCNN-ResNet101 lowproposals",
        "url" : "http://download.tensorflow.org/models/object_detection/faster_rcnn_resnet101_lowproposals_coco_2018_01_28.tar.gz",
        "width" : 1024,
        "height" : 600,
    },
    'rcnn-inception-resnet-v2-lowproposals' : {
        "name" : "Faster-RCNN-Inception-ResNet-v2 lowproposals",
        "url" : "http://download.tensorflow.org/models/object_detection/faster_rcnn_inception_resnet_v2_atrous_lowproposals_coco_2018_01_28.tar.gz",
        "width" : 1024,
        "height" : 600,
    },
    'rcnn-inception-v2' : {
        "name" : "Faster-RCNN Inception-v2",
        "url" : "http://download.tensorflow.org/models/object_detection/faster_rcnn_inception_v2_coco_2018_01_28.tar.gz",
        "width" : 1024,
        "height" : 600,
    },
    'ssd-inception-v2' : {
        "name" : "SSD-Inception-v2",
        "url" : "http://download.tensorflow.org/models/object_detection/ssd_inception_v2_coco_2018_01_28.tar.gz",
        "width" : 300,
        "height" : 300,
    },
    'ssd-mobilenet-v1-quantized-mlperf' : {
        "name" : "SSD-MobileNet-v1",
        "url" : "https://zenodo.org/record/3361502/files/ssd_mobilenet_v1_coco_2018_01_28.tar.gz",
        "width" : 300,
        "height" : 300,
        "provenance" : "Google",
    },
    'ssd-mobilenet-v1-non-quantized-mlperf' : {
        "name" : "SSD-MobileNet-v1 quantized",
        "url" : "https://zenodo.org/record/3252084/files/mobilenet_v1_ssd_8bit_finetuned.tar.gz",
        "width" : 300,
        "height" : 300,
        "provenance" : "Habana"
    },
    'ssd-mobilenet-v1-fpn' : {
        "name" : "SSD-MobileNet-v1 FPN SBP",
        "url" : "http://download.tensorflow.org/models/object_detection/ssd_mobilenet_v1_fpn_shared_box_predictor_640x640_coco14_sync_2018_07_03.tar.gz",
        "width" : 640,
        "height" : 640,
    },
    'ssd-resnet50-fpn' : {
        "name" : "SSD-ResNet50-v1 FPN SBP",
        "url" : "http://download.tensorflow.org/models/object_detection/ssd_resnet50_v1_fpn_shared_box_predictor_640x640_coco14_sync_2018_07_03.tar.gz",
        "width" : 640,
        "height" : 640,
    },
    'ssdlite-mobilenet-v2' : {
        "name" : "SSDLite-MobileNet-v2",
        "url" : "http://download.tensorflow.org/models/object_detection/ssdlite_mobilenet_v2_coco_2018_05_09.tar.gz",
        "width" : 300,
        "height" : 300,
    },
    'yolo-v3' : {
        "name" : "YOLO-v3",
        "url" : "https://zenodo.org/record/3386327/files/yolo_v3_coco.tar.gz",
        "width" : 416,
        "height" : 416,
        "provenance" : "https://github.com/YunYang1994/tensorflow-yolov3/"
    }
}

# ## Code

# In[ ]:

# We use two modes: the 'spreadsheet' mode tries to mimic the official submission table as much as possible;
# the 'dashboard' mode uses a more appropriate layout for the CK dashboard.
def get_data(results_path=results_path, mode='spreadsheet'):
    dfs = []
    # FOR EACH division.
    for division in divisions:
        #if division == 'open': continue # skip
        # FOR EACH submitter.
        submitters_dir = os.path.join(results_path, division)
        submitters = [ fn for fn in os.listdir(submitters_dir) if os.path.isdir(os.path.join(submitters_dir, fn)) ]
        for submitter in submitters:
            # Selectively filter out submitters.
            #all_submitters_closed = [ 'Alibaba', 'CentaurTechnology', 'DellEMC', 'dividiti', 'FuriosaAI', 'Google', 'Habana', 'Hailo', 'Intel', 'NVIDIA', 'Qualcomm', 'Tencent' ]
            #if division == 'closed' and submitter not in all_submitters_closed: continue
            #all_submitters_open = [ 'dividiti', 'Habana', 'Inspur', 'NVIDIA', 'Qualcomm' ]
            #if division == 'open' and submitter not in all_submitters_open: continue
            # FOR EACH system.
            results_dir = os.path.join(submitters_dir, submitter, 'results')
            measurements_dir = os.path.join(submitters_dir, submitter, 'measurements')
            systems = [ fn for fn in os.listdir(results_dir) if os.path.isdir(os.path.join(results_dir, fn)) ]
            for system in systems:
                system_dir = os.path.join(results_dir, system)
                system_json_name = system + '.json'
                system_json_path = os.path.join(submitters_dir, submitter, 'systems', system_json_name)
                with open(system_json_path) as system_json_file:
                    system_json = json.load(system_json_file)

                # Category.
                if system_json['status'] in [ 'available', 'Available' ]:
                    category = 'Available'
                elif system_json['status'] in [ 'preview', 'Preview' ]:
                    category = 'Preview'
                elif system_json['status'] in [ 'rdi', 'RDI', 'rdo', 'RDO' ]:
                    category = 'Research, Development, Other'
                elif system_json['status'] in [ 'Unofficial', 'unofficial' ]:
                    category = 'Unofficial'
                else:
                    raise Exception("Unsupported category '%s'!" % (system_json['status']))

                # System details.
                system_name = system_json['system_name']
                system_list = system.split('-')
                system_id = system_list[0]

                # Processor (CPU).
                processor = system_id_to_processor.get(system_id, system_json.get('host_processor_model_name', 'N/A'))
                processor_num = int(system_json.get('host_processors_per_node', 0))

                # Accelerator.
                # Tencent: https://github.com/mlperf/submissions_inference_0_5/issues/285
                accelerator_name = system_json.get('accelerator_model_name', 'N/A')
                accelerator_num = int(system_json.get('accelerators_per_node', 0))
                accelerator = accelerator_name_to_accelerator.get(accelerator_name, accelerator_name)

                # Software (framework).
                software = system_json['framework']

                # Default form factors and notes.
                # NB: Using space rather than empty string turns out to be better for dashboard.
                ff_m = ff_d = ff_s = ff_e = ' '
                notes = [' ']

                # Submitter-specific form factors and notes.
                submitter_str = submitter
                if submitter == 'dividiti':
                    # Form factors.
                    if system_id in [ 'hikey960', 'firefly', 'rpi4' ]: ff_e = 'x'
                    if system_id in [ 'mate10pro', 'hikey960' ]: ff_m = 'x'
                    if system_id in [ 'velociti' ]: ff_d = 'x'
                    # Notes.
                    if system_id == 'hikey960':
                        notes = 'Mobile chip in embedded form factor (development board).'
                    if division == 'open':
                        # Object Detection is collaboration between dividiti and Politecnico di Milano.
                        if system_id == 'velociti': submitter_str = 'dividiti + PoliMi'
                        if system == 'velociti-tensorflow-v1.14-cpu':
                            notes = 'In the Other category, since this Intel CPU is no longer available (end-of-life).'
                elif submitter == 'Alibaba':
                    ff_s = 'x'
                    if system_id == 'alibaba_cloud_t4':
                        notes = 'ECC off'
                elif submitter == 'DellEMC':
                    ff_s = 'x'
                    if system_id == 'R740_T4x4_tensorrt':
                        notes = 'ECC off'
                elif submitter == 'Google':
                    ff_s = 'x'
                    system_name = '{:d}x Cloud {:s}'.format(int(accelerator_num/8), accelerator)
                elif submitter == 'Habana':
                    ff_d = ff_s = ff_e = 'x'
                    if division == 'open':
                        if system_id == 'Goya_fast_latency':
                            notes = 'Low latency results ...'
                        if system_id == 'Goya_med_latency':
                            notes = 'Medium latency results ...'
                elif submitter == 'Intel':
                    if system_id == 'ICL':
                        ff_m = 'x'
                    else:
                        ff_s = 'x'
                elif submitter == 'NVIDIA':
                    if system_id == 'Xavier':
                        ff_e = 'x'
                        if division == 'closed':
                            notes = 'GPU and both DLAs are used in Offline and MultiStream'
                    elif system_id == 'TitanRTXx4':
                        ff_e = ff_s = ff_d = 'x'
                    elif system_id == 'T4x8':
                        ff_e = ff_s = 'x'
                    elif system_id == 'T4x20':
                        ff_s = 'x'
                    else:
                        raise Exception("Unsupported NVIDIA system '%s'!" % system_id)
                elif submitter == 'Qualcomm':
                    ff_m = 'x'
                    if division == 'open':
                        notes = 'Median latency. MultiStream: Both Hexagon Vector Extensions (HVX) and Hexagon Tensor Accelerator (HTA).'
                    if division == 'closed':
                        notes = 'Hexagon Vector Extensions being used.'
                elif submitter == 'Tencent':
                    ff_s = 'x'
                # Preview only.
                elif submitter == 'CentaurTechnology':
                    ff_d = ff_s = ff_e = 'x'
                elif submitter == 'Hailo':
                    ff_d = ff_e = 'x'
                # RDO only.
                elif submitter == 'FuriosaAI':
                    ff_d = ff_s = ff_e = 'x'
                # Open only.
                elif submitter == 'Inspur':
                    ff_s = 'x'
                else:
                    raise Exception("Unsupported division/submitter combination '%s'/'%s'!" % (division, submitter))

                details = 'https://github.com/mlperf/inference_results_v0.5/blob/master/{}/{}/systems/{}'.format(division, submitter, system_json_name)
                code    = 'https://github.com/mlperf/inference_results_v0.5/tree/master/{}/{}/code'.format(division, submitter)
                if category in [ 'Unofficial' ]:
                    details = 'N/A'
                    code    = 'N/A'

                # Create DataFrame for each row of the final table based on the division, submitter and system.
                data = [{
                    #
                    'ID'            : '-', # TODO: Fill in later.
                    'Submitter'     : submitter_str,
                    'System'        : system_name,
                    'Benchmark'     : '-', # TODO: Fill in later.
                    # Processor.
                    'Processor'     : processor,
                    'Processor #'   : processor_num,
                    # Accelerator.
                    'Accelerator'   : accelerator,
                    'Accelerator #' : accelerator_num if accelerator_num != '0' else '',
                    # Software.
                    'Software' : software,
                    # Form factor.
                    'FF_M'     : ff_m,
                    'FF_D'     : ff_d,
                    'FF_S'     : ff_s,
                    'FF_E'     : ff_e,
                    # Details. Code. Notes.
                    'Details'  : details,
                    'Code'     : code,
                    'Notes'    : notes,
                    # Misc.
                    'Division' : division_to_str.get(division, division),
                    'Category' : category,
                    'Task'     : '-', # TODO: Fill in later.
                    'Scenario' : '-', # TODO: Fill in later.
                }]
                # NB: 'Accelerator #' is important to sort Google's submissions correctly (not lexicographically).
                index = [
                    'Division', 'Category', 'Submitter', 'Accelerator #', 'System', 'Software', 'Benchmark' #, 'Task', 'Scenario'
                ]
                # Reset all scores.
                if mode == 'spreadsheet':
                    data[0].update({ score : '' for score in score_columns })

                # FOR EACH benchmark.
                benchmarks = [ fn for fn in os.listdir(system_dir) if os.path.isdir(os.path.join(system_dir, fn)) ]
                for (benchmark, benchmark_idx) in zip(benchmarks, range(len(benchmarks))):
                    is_last_benchmark = (benchmark_idx == len(benchmarks) - 1)
                    # Tencent and Inspur use resnet50.
                    benchmark_name = 'resnet' if benchmark == 'resnet50' else benchmark
                    # Benchmark (with notes).
                    benchmark_dict = non_imagenet_benchmarks.get(benchmark_name)
                    if benchmark_dict:
                        width = benchmark_dict['width']
                        height = benchmark_dict['height']
                    else:
                        if benchmark_name.endswith('96'):
                            side = 96
                        elif benchmark_name.endswith('128'):
                            side = 128
                        elif benchmark_name.endswith('160'):
                            side = 160
                        elif benchmark_name.endswith('192'):
                            side = 192
                        else:
                            side = 224
                        width = side
                        height = side
                    if width != -1 and height != -1:
                        # Benchmark (width x height).
                        benchmark_with_notes = '{} ({}x{})'.format(benchmark_name, width, height)
                    else:
                        # GNMT.
                        benchmark_with_notes = benchmark_name
                    # TODO: Rename to 'Model used, if not Closed Division default' for Open.
                    data[0]['Benchmark'] = benchmark_with_notes

                    # FOR EACH scenario.
                    benchmark_dir = os.path.join(system_dir, benchmark)
                    scenarios = [ fn for fn in os.listdir(benchmark_dir) if os.path.isdir(os.path.join(benchmark_dir, fn)) ]
                    for scenario in scenarios:
                        if mode != 'spreadsheet':
                            data[0].update({ score : '' for score in score_columns })
                        scenario_str = scenario_to_str.get(scenario,'')
                        if scenario_str not in [ 'SingleStream', 'MultiStream', 'Server', 'Offline' ]: continue
                        experiment_dir = os.path.join(benchmark_dir, scenario)

                        # Extract accuracy.
                        if submitter == 'Hailo' and benchmark == 'ssd-small':
                            # https://github.com/mlperf/submissions_inference_0_5/issues/287
                            task = 'OD'
                            accuracy = 21.920 # ssd-small/SingleStream/accuracy/results.json
                        else:
                            accuracy_dir = os.path.join(experiment_dir, 'accuracy')
                            with open(os.path.join(accuracy_dir, 'accuracy.txt'), 'r') as accuracy_file:
                                accuracy_txt = accuracy_file.readlines()
                                accuracy_line = accuracy_txt[-1]
                            if accuracy_line.startswith('mAP'):
                                task = 'OD'
                                match = re.match('mAP\=([\d\.]+)\%', accuracy_line)
                                accuracy = float(match.group(1))
                            elif accuracy_line.startswith('accuracy'):
                                task = 'IC'
                                match = re.match('accuracy=(.+)%, good=(\d+), total=(\d+)', accuracy_line)
                                accuracy = float(match.group(1))
                            elif accuracy_line.startswith('BLEU'):
                                task = 'MT'
                                match = re.match('BLEU:\s*(.+)', accuracy_line)
                                accuracy = float(match.group(1))
                            else:
                                pprint(accuracy_txt)
                                raise Exception('Failed to extract accuracy information from "%s"' % accuracy_line)
                        data[0]['Task'] = { 'IC': 'Image Classification', 'OD': 'Object Detection', 'MT': 'Machine Translation' }.get(task)
                        data[0]['Scenario'] = scenario_to_str.get(scenario, scenario)
                        if submitter == 'Tencent' and scenario_str in [ 'SingleStream', 'Offline' ]:
                            # https://github.com/mlperf/submissions_inference_0_5/issues/286
                            performance_dirs = [ os.path.join(experiment_dir, 'performance') ]
                            # FIXME: Code below assumes directory name, not absolute path.
                        else:
                            performance_dirs = [ fn for fn in os.listdir(os.path.join(experiment_dir, 'performance'))
                                                 if os.path.isdir(os.path.join(experiment_dir, 'performance', fn)) and fn.startswith('run_') ]
                        # FOR EACH performance run. (Iterates over 5 runs for Server and other repeated runs.)
                        for performance_dir in performance_dirs:
                            # Reset notes from previous updates.
                            notes = [' ']
                            # If exist, read generic notes for each scenario.
                            notes_path = os.path.join(measurements_dir, system, benchmark, scenario, 'NOTES.txt')
                            if os.path.isfile(notes_path):
                                with open(notes_path, 'r') as notes_file:
                                    notes = notes_file.readlines()
                                data[0].update({'Notes' : notes})
                            # If exist, append performance notes to notes.
                            performance_notes_path = os.path.join(experiment_dir, 'performance', '%s.txt' % performance_dir)
                            if os.path.isfile(performance_notes_path):
                                with open(performance_notes_path, 'r') as performance_notes_file:
                                    performance_notes = performance_notes_file.readlines()
                                notes.append(performance_notes)
                                data[0].update({'Notes' : notes})
                            # Parse performance stats from the summary file.
                            with open(os.path.join(experiment_dir, 'performance', performance_dir, 'mlperf_log_summary.txt'), 'r') as summary_file:
                                summary_txt = summary_file.readlines()
                                for line in summary_txt:
                                    if re.match("Scenario", line):
                                        # NB: LoadGen scenario strings have spaces between 'Single'/'Multi' and 'Stream'.
                                        loadgen_scenario = line.split(": ",1)[1].strip()
                                        loadgen_scenario_str = scenario_to_str[loadgen_scenario]
                                        if loadgen_scenario_str != scenario_str:
                                            raise Exception("Expected '%s', parsed '%s'!" % (scenario_str, loadgen_scenario_str ))
                                        continue
                                    if scenario_str == "SingleStream":
                                        if re.match("90th percentile latency", line):
                                            score = line.split(": ",1)[1].strip()
                                            continue
                                    if scenario_str == "MultiStream":
                                        if re.match("Result is", line):
                                            status = line.split(": ",1)[1].strip()
                                            notes.append('Result = {}'.format(status))
                                            data[0].update({'Notes' : notes})
                                        if re.match("Per-query latency", line):
                                            latency_type = 'per-query'
                                        if re.match("Per-sample latency", line):
                                            latency_type = 'per-sample'
                                        # Match e.g. "Min latency (ns)                : 67438100"
                                        metric_regex = "(?P<metric>[A-Z][a-z]{2,3}) latency \(ns\)(\s)*:(\s)*(?P<ns>\d+)"
                                        metric_match = re.match(metric_regex, line)
                                        if metric_match:
                                            metric = metric_match.group('metric')
                                            ns = int(metric_match.group('ns'))
                                            ms = float(ns) * 1e-6
                                            note = '{:s} {:s} latency = {:.01f} ms'.format(metric, latency_type, ms)
                                            notes.append(note)
                                            data[0].update({'Notes' : notes})
                                            continue
                                        # Match e.g. "50.00 percentile latency (ns)   : 864943800"
                                        pc_regex = "(?P<pc>\d\d).00 percentile latency \(ns\)(\s)*:(\s)*(?P<ns>\d+)"
                                        pc_match = re.match(pc_regex, line)
                                        if pc_match:
                                            pc = int(pc_match.group('pc'))
                                            ns = int(pc_match.group('ns'))
                                            ms = float(ns) * 1e-6
                                            note = '{:d}% {:s} latency = {:.01f} ms'.format(pc, latency_type, ms)
                                            notes.append(note)
                                            data[0].update({'Notes' : notes})
                                            continue
                                        if re.match("Samples per query", line):
                                            score = line.split(": ",1)[1].strip()
                                            continue
                                    if scenario_str == "Server":
                                        if re.match("Scheduled samples per second", line):
                                            score = line.split(": ",1)[1].strip()
                                            continue
                                    if scenario_str == "Offline":
                                        if re.match("Samples per second", line):
                                            score = line.split(": ",1)[1].strip()
                                            continue
                            if scenario_str == 'SingleStream':
                                time_ns = int(score)
                                time_ms = time_ns * 1e-6
                            elif scenario_str == 'MultiStream':
                                num_streams = int(score)
                            elif scenario_str == 'Server':
                                queries_per_second = float(score)
                            elif scenario_str == 'Offline':
                                samples_per_second = float(score)

                            # Tasks.
                            if mode == 'spreadsheet':
                                ic1 = (task=='IC' and benchmark.startswith('mobilenet'))
                                ic2 = (task=='IC' and benchmark.startswith('resnet'))
                                od1 = (task=='OD' and benchmark=='ssd-small')
                                od2 = (task=='OD' and (benchmark=='ssd-large' or system_id=='velociti'))
                                nmt = (task=='MT')
                            else:
                                ic1 = (task=='IC')
                                ic2 = False
                                od1 = (task=='OD')
                                od2 = False
                                nmt = (task=='MT')
                            if scenario_str == 'SingleStream':
                                performance_str = '{:.03f}'.format(time_ms)
                                accuracy_str    = '{:.03f}'.format(accuracy)
                                if ic1:
                                    data[0]['A_IC1_SS'] = accuracy_str
                                    data[0]['P_IC1_SS'] = performance_str
                                elif ic2:
                                    data[0]['A_IC2_SS'] = accuracy_str
                                    data[0]['P_IC2_SS'] = performance_str
                                elif od1:
                                    data[0]['A_OD1_SS'] = accuracy_str
                                    data[0]['P_OD1_SS'] = performance_str
                                elif od2:
                                    data[0]['A_OD2_SS'] = accuracy_str
                                    data[0]['P_OD2_SS'] = performance_str
                                elif nmt:
                                    data[0]['A_NMT_SS'] = accuracy_str
                                    data[0]['P_NMT_SS'] = performance_str
                            elif scenario_str == 'MultiStream':
                                performance_str = '{:d}'.format(num_streams)
                                accuracy_str    = '{:.03f}'.format(accuracy)
                                if ic1:
                                    data[0]['A_IC1_MS'] = accuracy_str
                                    data[0]['P_IC1_MS'] = performance_str
                                elif ic2:
                                    data[0]['A_IC2_MS'] = accuracy_str
                                    data[0]['P_IC2_MS'] = performance_str
                                elif od1:
                                    data[0]['A_OD1_MS'] = accuracy_str
                                    data[0]['P_OD1_MS'] = performance_str
                                elif od2:
                                    data[0]['A_OD2_MS'] = accuracy_str
                                    data[0]['P_OD2_MS'] = performance_str
                                elif nmt:
                                    data[0]['A_NMT_MS'] = accuracy_str
                                    data[0]['P_NMT_MS'] = performance_str
                            elif scenario_str == 'Server':
                                performance_str = '{:.03f}'.format(queries_per_second)
                                accuracy_str    = '{:.03f}'.format(accuracy)
                                if ic1:
                                    data[0]['A_IC1_S'] = accuracy_str
                                    data[0]['P_IC1_S'] = performance_str
                                elif ic2:
                                    data[0]['A_IC2_S'] = accuracy_str
                                    data[0]['P_IC2_S'] = performance_str
                                elif od1:
                                    data[0]['A_OD1_S'] = accuracy_str
                                    data[0]['P_OD1_S'] = performance_str
                                elif od2:
                                    data[0]['A_OD2_S'] = accuracy_str
                                    data[0]['P_OD2_S'] = performance_str
                                elif nmt:
                                    data[0]['A_NMT_S'] = accuracy_str
                                    data[0]['P_NMT_S'] = performance_str
                            elif scenario_str == 'Offline':
                                performance_str = '{:.03f}'.format(samples_per_second)
                                accuracy_str    = '{:.03f}'.format(accuracy)
                                if ic1:
                                    data[0]['A_IC1_O'] = accuracy_str
                                    data[0]['P_IC1_O'] = performance_str
                                elif ic2:
                                    data[0]['A_IC2_O'] = accuracy_str
                                    data[0]['P_IC2_O'] = performance_str
                                elif od1:
                                    data[0]['A_OD1_O'] = accuracy_str
                                    data[0]['P_OD1_O'] = performance_str
                                elif od2:
                                    data[0]['A_OD2_O'] = accuracy_str
                                    data[0]['P_OD2_O'] = performance_str
                                elif nmt:
                                    data[0]['A_NMT_O'] = accuracy_str
                                    data[0]['P_NMT_O'] = performance_str
                            else:
                                print('Skipping unsupported task/scenario combination!')
                                continue
                            if mode != 'spreadsheet':
                                df = pd.DataFrame(data)
                                df = df.set_index(index)
                                dfs.append(df)
                            # END OF FOR EACH performance run
                    # END OF FOR EACH scenario
                    if mode == 'spreadsheet':
                        # For closed, multiple benchmarks can share the same row, so the Benchmark field can be misleading.
                        if division == 'closed': data[0]['Benchmark'] = ''
                        if is_last_benchmark or (division == 'open' and submitter == 'dividiti'):
                            df = pd.DataFrame(data)
                            df = df.set_index(index)
                            dfs.append(df)
                    # For the spreadsheet mode, include multiple benchmarks per row.
                # END OF FOR EACH benchmark
            # END OF FOR EACH system
        # END OF FOR EACH submitter
    # END OF FOR EACH division

    # Concatenate all thus constructed DataFrames (i.e. stack on top of each other).
    df = pd.concat(dfs)
    # Temporarily capitalize the first letter in 'dividiti' for correct sorting and then back.
    df = df         .rename(index={'dividiti':'Dividiti', 'dividiti + PoliMi':'Dividiti + PoliMi'})         .sort_index(ascending=True)         .rename(index={'Dividiti':'dividiti', 'Dividiti + PoliMi':'dividiti + PoliMi'})
    # Reset the index, but keep Division and Category there.
    df = df.reset_index(level=index[2:])
    df['ID'] = [ 'Inf-0.5-{:03d}'.format(ID) for ID in range(1, len(df)+1) ]
    # Mimic the official template.
    columns = [ 'ID', 'Submitter', 'System', 'Benchmark' ]
    columns += score_columns
    columns += [ 'Processor', 'Processor #', 'Accelerator', 'Accelerator #', 'Software',
                'FF_M', 'FF_D', 'FF_S', 'FF_E', 'Details', 'Code', 'Notes' ]
    # Finalize the table.
    if mode == 'spreadsheet':
        df = df[columns]
    else:
        df = df.reset_index().set_index(keys=[ 'ID', 'Division', 'Category', 'Submitter', 'System', 'Benchmark' ], drop=False)
        df[score_columns] = df[score_columns].apply(pd.to_numeric).astype('float32')

    return df

# ## Dump the table for the CK dashboard

if os.path.exists(cache_path):
    print('Skipping existing dashboard file at \'{}\' ...'.format(cache_path))
    exit(1)
else:
    # Store the table in a simplified format.
    print('Storing dashboard file to \'{}\' ...'.format(cache_path))
    df = get_data(results_path=results_path, mode='dashboard')
    df.to_pickle(path=cache_path, protocol=cache_protocol, compression=cache_compression)

max_stars_repo_name: dsavenko/ck-mlperf

max_issues_repo_name: dsavenko/ck-mlperf

max_forks_repo_name: dsavenko/ck-mlperf


text:
# https://medium.com/datadriveninvestor/deep-autoencoder-using-keras-b77cd3e8be95

from keras.datasets import mnist
from keras.layers import Input, Dense
from keras.models import Model

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

(X_train, _), (X_test, _) = mnist.load_data()
X_train = X_train.astype('float32')/255
X_test = X_test.astype('float32')/255
X_train = X_train.reshape(len(X_train), np.prod(X_train.shape[1:]))
X_test = X_test.reshape(len(X_test), np.prod(X_test.shape[1:]))
print(X_train.shape)
print(X_test.shape)

input_img= Input(shape=(784,))

encoded = Dense(units=128, activation='relu')(input_img)
encoded = Dense(units=64, activation='relu')(encoded)
encoded = Dense(units=32, activation='relu')(encoded)
decoded = Dense(units=64, activation='relu')(encoded)
decoded = Dense(units=128, activation='relu')(decoded)
decoded = Dense(units=784, activation='sigmoid')(decoded)

autoencoder=Model(input_img, decoded)

encoder = Model(input_img, encoded)

print('autoencoder')
autoencoder.summary()

print('encoder')
encoder.summary()

autoencoder.compile(optimizer='adam', loss='binary_crossentropy', metrics=['accuracy'])

autoencoder.fit(X_train, X_train,
                epochs=50,
                batch_size=256,
                shuffle=True,
                validation_data=(X_test, X_test))

encoded_imgs = encoder.predict(X_test)
predicted = autoencoder.predict(X_test)

plt.figure(figsize=(40, 4))
for i in range(10):
    # display original images
    ax = plt.subplot(3, 20, i + 1)
    plt.imshow(X_test[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    # display encoded images
    ax = plt.subplot(3, 20, i + 1 + 20)
    plt.imshow(encoded_imgs[i].reshape(8,4))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

    # display reconstructed images
    ax = plt.subplot(3, 20, 2*20 +i+ 1)
    plt.imshow(predicted[i].reshape(28, 28))
    plt.gray()
    ax.get_xaxis().set_visible(False)
    ax.get_yaxis().set_visible(False)

plt.show()

max_stars_repo_name: DanielBerns/keras-effective-adventure

max_issues_repo_name: DanielBerns/keras-effective-adventure

max_forks_repo_name: DanielBerns/keras-effective-adventure


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

"""Tests for `utils/empirical.py`."""

from functools import partial

from absl.testing import absltest
from jax import test_util as jtu
from jax.api import jit
from jax.config import config as jax_config
import jax.numpy as np
import jax.random as random
from neural_tangents import stax
from neural_tangents.utils import empirical
from neural_tangents.utils import test_utils
from neural_tangents.utils import utils

jax_config.parse_flags_with_absl()

TAYLOR_MATRIX_SHAPES = [(3, 3), (4, 4)]
TAYLOR_RANDOM_SAMPLES = 10

FLAT = 'FLAT'
POOLING = 'POOLING'
CONV = 'CONV'

TRAIN_SHAPES = [(4, 4), (4, 8), (8, 8), (6, 4, 4, 3), (4, 4, 4, 3),
                (4, 4, 4, 3)]
TEST_SHAPES = [(2, 4), (6, 8), (16, 8), (2, 4, 4, 3), (2, 4, 4, 3),
               (2, 4, 4, 3)]
NETWORK = [FLAT, FLAT, FLAT, FLAT, POOLING,
           CONV]
OUTPUT_LOGITS = [1, 2, 3]

CONVOLUTION_CHANNELS = 8
test_utils.update_test_tolerance()

def _build_network(input_shape, network, out_logits):
  if len(input_shape) == 1:
    assert network == FLAT
    return stax.Dense(out_logits, W_std=2.0, b_std=0.5)
  elif len(input_shape) == 3:
    if network == POOLING:
      return stax.serial(
          stax.Conv(CONVOLUTION_CHANNELS, (3, 3), W_std=2.0, b_std=0.05),
          stax.GlobalAvgPool(), stax.Dense(out_logits, W_std=2.0, b_std=0.5))
    elif network == CONV:
      return stax.serial(
          stax.Conv(CONVOLUTION_CHANNELS, (1, 2), W_std=1.5, b_std=0.1),
          stax.Relu(),
          stax.Conv(CONVOLUTION_CHANNELS, (3, 2), W_std=2.0, b_std=0.05),
      )
    elif network == FLAT:
      return stax.serial(
          stax.Conv(CONVOLUTION_CHANNELS, (3, 3), W_std=2.0, b_std=0.05),
          stax.Flatten(), stax.Dense(out_logits, W_std=2.0, b_std=0.5))
    else:
      raise ValueError('Unexpected network type found: {}'.format(network))
  else:
    raise ValueError('Expected flat or image test input.')

def _kernel_fns(key,
                input_shape,
                network,
                out_logits,
                diagonal_axes,
                trace_axes):
  init_fn, f, _ = _build_network(input_shape, network, out_logits)
  _, params = init_fn(key, (-1,) + input_shape)
  implicit_kernel_fn = empirical.empirical_implicit_ntk_fn(f, trace_axes,
                                                           diagonal_axes)
  direct_kernel_fn = empirical.empirical_direct_ntk_fn(f, trace_axes,
                                                       diagonal_axes)
  nngp_kernel_fn = empirical.empirical_nngp_fn(f, trace_axes, diagonal_axes)

  implicit_kernel_fn = jit(implicit_kernel_fn)
  direct_kernel_fn = jit(direct_kernel_fn)
  nngp_kernel_fn = jit(nngp_kernel_fn)

  return (partial(implicit_kernel_fn, params=params),
          partial(direct_kernel_fn, params=params),
          partial(nngp_kernel_fn, params=params))

KERNELS = {}
for o in OUTPUT_LOGITS:
  KERNELS['empirical_logits_{}'.format(o)] = partial(_kernel_fns, out_logits=o)

class EmpiricalTest(jtu.JaxTestCase):

  # We use a three layer deep linear network for testing.
  @classmethod
  def f(cls, x, params, do_alter, do_shift_x=True):
    w1, w2, b = params
    if do_alter:
      b *= 2.
      w1 += 5.
      w2 /= 0.9
    if do_shift_x:
      x = x * 2 + 1.
    return 0.5 * np.dot(np.dot(x.T, w1), x) + np.dot(w2, x) + b

  @classmethod
  def f_lin_exact(cls, x0, x, params, do_alter, do_shift_x=True):
    w1, w2, b = params
    f0 = EmpiricalTest.f(x0, params, do_alter, do_shift_x)
    if do_shift_x:
      x0 = x0 * 2 + 1.
      x = x * 2 + 1.
    dx = x - x0
    if do_alter:
      b *= 2.
      w1 += 5.
      w2 /= 0.9
    return f0 + np.dot(np.dot(x0.T, w1) + w2, dx)

  @jtu.parameterized.named_parameters(
      jtu.cases_from_list({
          'testcase_name': '_{}'.format(shape),
          'shape': shape
      } for shape in TAYLOR_MATRIX_SHAPES))
  def testLinearization(self, shape):
    key = random.PRNGKey(0)
    key, s1, s2, s3, = random.split(key, 4)
    w1 = random.normal(s1, shape)
    w1 = 0.5 * (w1 + w1.T)
    w2 = random.normal(s2, shape)
    b = random.normal(s3, (shape[-1],))
    params = (w1, w2, b)

    key, split = random.split(key)
    x0 = random.normal(split, (shape[-1],))

    f_lin = empirical.linearize(EmpiricalTest.f, x0)

    for _ in range(TAYLOR_RANDOM_SAMPLES):
      for do_alter in [True, False]:
        for do_shift_x in [True, False]:
          key, split = random.split(key)
          x = random.normal(split, (shape[-1],))
          self.assertAllClose(EmpiricalTest.f_lin_exact(x0, x, params, do_alter,
                                                        do_shift_x=do_shift_x),
                              f_lin(x, params, do_alter, do_shift_x=do_shift_x))

  @jtu.parameterized.named_parameters(
      jtu.cases_from_list({
          'testcase_name': '_{}'.format(shape),
          'shape': shape
      } for shape in TAYLOR_MATRIX_SHAPES))
  def testTaylorExpansion(self, shape):

    def f_2_exact(x0, x, params, do_alter, do_shift_x=True):
      w1, w2, b = params
      f_lin = EmpiricalTest.f_lin_exact(x0, x, params, do_alter, do_shift_x)
      if do_shift_x:
        x0 = x0 * 2 + 1.
        x = x * 2 + 1.
      if do_alter:
        b *= 2.
        w1 += 5.
        w2 /= 0.9
      dx = x - x0
      return f_lin + 0.5 * np.dot(np.dot(dx.T, w1), dx)

    key = random.PRNGKey(0)
    key, s1, s2, s3, = random.split(key, 4)
    w1 = random.normal(s1, shape)
    w1 = 0.5 * (w1 + w1.T)
    w2 = random.normal(s2, shape)
    b = random.normal(s3, (shape[-1],))
    params = (w1, w2, b)

    key, split = random.split(key)
    x0 = random.normal(split, (shape[-1],))

    f_lin = empirical.taylor_expand(EmpiricalTest.f, x0, 1)
    f_2 = empirical.taylor_expand(EmpiricalTest.f, x0, 2)

    for _ in range(TAYLOR_RANDOM_SAMPLES):
      for do_alter in [True, False]:
        for do_shift_x in [True, False]:
          key, split = random.split(key)
          x = random.normal(split, (shape[-1],))
          self.assertAllClose(EmpiricalTest.f_lin_exact(x0, x, params, do_alter,
                                                        do_shift_x=do_shift_x),
                              f_lin(x, params, do_alter, do_shift_x=do_shift_x))
          self.assertAllClose(f_2_exact(x0, x, params, do_alter,
                                        do_shift_x=do_shift_x),
                              f_2(x, params, do_alter, do_shift_x=do_shift_x))

  @jtu.parameterized.named_parameters(
      jtu.cases_from_list({
          'testcase_name': '_train_shape={}_test_shape={}_network={}_{}'.format(
              train, test, network, name),
          'train_shape': train,
          'test_shape': test,
          'network': network,
          'name': name,
          'kernel_fn': kernel_fn
      } for train, test, network in zip(TRAIN_SHAPES, TEST_SHAPES, NETWORK)
                          for name, kernel_fn in KERNELS.items()))
  def testNTKAgainstDirect(
      self, train_shape, test_shape, network, name, kernel_fn):
    key = random.PRNGKey(0)
    key, self_split, other_split = random.split(key, 3)
    data_self = random.normal(self_split, train_shape)
    data_other = random.normal(other_split, test_shape)

    implicit, direct, _ = kernel_fn(key, train_shape[1:], network,
                                    diagonal_axes=(), trace_axes=())

    g = implicit(data_self, None)
    g_direct = direct(data_self, None)
    self.assertAllClose(g, g_direct)

    g = implicit(data_other, data_self)
    g_direct = direct(data_other, data_self)
    self.assertAllClose(g, g_direct)

  @jtu.parameterized.named_parameters(
      jtu.cases_from_list({
          'testcase_name': '_diagonal_axes={}_trace_axes={}'.format(
              diagonal_axes, trace_axes),
          'diagonal_axes': diagonal_axes,
          'trace_axes': trace_axes,
      }
                          for diagonal_axes in [(),
                                                (0,),
                                                (0, 1),
                                                (0, 1, 2),
                                                (0, 1, 2, 3),
                                                (-1,),
                                                (-2,),
                                                (0, -1),
                                                (1, -2),
                                                (2, 3),
                                                (3, 0, 2)]
                          for trace_axes in [(),
                                             (0,),
                                             (0, 1),
                                             (-1,),
                                             (1,),
                                             (0, -1),
                                             (-1, -2),
                                             (0, 1, 2, 3),
                                             (3, 1, 2, 0),
                                             (1, 2, 3),
                                             (-3, -2),
                                             (-3, -1),
                                             (-2, -4),
                                             (2, 0, -1)]))
  def testAxes(self, diagonal_axes, trace_axes):
    key = random.PRNGKey(0)
    key, self_split, other_split = random.split(key, 3)
    data_self = random.normal(self_split, (4, 5, 6, 3))
    data_other = random.normal(other_split, (2, 5, 6, 3))

    _diagonal_axes = utils.canonicalize_axis(diagonal_axes, data_self)
    _trace_axes = utils.canonicalize_axis(trace_axes, data_self)

    if any(d == c for d in _diagonal_axes for c in _trace_axes):
      raise absltest.SkipTest(
          'diagonal axes must be different from channel axes.')

    implicit, direct, nngp = KERNELS['empirical_logits_3'](
        key,
        (5, 6, 3),
        CONV,
        diagonal_axes=diagonal_axes,
        trace_axes=trace_axes)

    n_marg = len(_diagonal_axes)
    n_chan = len(_trace_axes)

    g_nngp = nngp(data_self, None)
    self.assertEqual(2 * (data_self.ndim - n_chan) - n_marg, g_nngp.ndim)

    g_direct = direct(data_self, None)
    self.assertEqual(g_nngp.shape, g_direct.shape)

    g = implicit(data_self, None)
    self.assertAllClose(g_direct, g)

    if 0 not in _trace_axes and 0 not in _diagonal_axes:
      g_nngp = nngp(data_other, data_self)
      self.assertEqual(2 * (data_self.ndim - n_chan) - n_marg, g_nngp.ndim)

      g_direct = direct(data_other, data_self)
      self.assertEqual(g_nngp.shape, g_direct.shape)

      g = implicit(data_other, data_self)
      self.assertAllClose(g_direct, g)

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

reason: import jax,from jax


text:
#!/usr/bin/python
##!/usr/bin/env python2

# Convert the sqlite3 files output by isolator to a simpler plain text format.

import numpy as np
import argparse
import sqlite3
import struct
import zlib

def unpack_samples_blob(blob):
    """ Unpack compressed samples into an numpy array. """
    data = zlib.decompress(blob)
    fmt = '{0}f'.format(len(data) / 4)
    return np.array(struct.unpack(fmt, data), dtype=np.float32)

def main():
    ap = argparse.ArgumentParser(
        description='Convert output from isolator to plaintext, tab-delimited data.')
    ap.add_argument('db_fn', metavar='samples.db')
    ap.add_argument('--gene_id', default=None)
    args = ap.parse_args()

    db = sqlite3.connect(args.db_fn)
    cur = db.cursor()

    if args.gene_id:
        q = '''
            select gene_id, ids.transcript_id, effective_length, samples
            from samples, ids
            where samples.transcript_id = ids.transcript_id and
                  ids.gene_id = "{0}"
            '''.format(args.gene_id)
    else:
        q = '''
            select gene_id, ids.transcript_id, effective_length, samples
            from samples, ids
            where samples.transcript_id = ids.transcript_id
            '''

    for gene_id, transcript_id, effective_length, samples in cur.execute(q):
        samples = unpack_samples_blob(samples)
        samples /= effective_length
        print('{gene_id}\t{transcript_id}\t{samples}'.format(
            gene_id=gene_id,
            transcript_id=transcript_id,
            samples=','.join(map(str, samples))))

if __name__ == '__main__':
    main()

max_stars_repo_name: dcjones/isolator

max_issues_repo_name: dcjones/isolator

max_forks_repo_name: dcjones/isolator


text:
from subprocess import Popen, PIPE
from math import *
import random
import numpy as np
import random as pr
from copy import deepcopy
from types import IntType, ListType, TupleType, StringTypes
import itertools
import time
import math
import argparse
import subprocess
from load_model import loaded_model
from keras.preprocessing import sequence
from rdkit import Chem
from rdkit.Chem import Draw
from rdkit.Chem import Descriptors
from rdkit.Chem import MolFromSmiles, MolToSmiles
import sys
from make_smile import zinc_data_with_bracket_original, zinc_processed_with_bracket
from add_node_type import chem_kn_simulation, make_input_smile,predict_smile,check_node_type,node_to_add,expanded_node

class chemical:

    def __init__(self):

        self.position=['&']
    def Clone(self):

        st = chemical()
        st.position= self.position[:]
        return st

    def SelectPosition(self,m):

        self.position.append(m)

    def Getatom(self):
        return [i for i in range(self.num_atom)]

class Node:

    def __init__(self, position = None,  parent = None, state = None):
        self.position = position
        self.parentNode = parent
        self.childNodes = []
        self.child=None
        self.wins = 0
        self.visits = 0
        #self.nonvisited_atom=state.Getatom()
        #self.type_node=tp
        self.depth=0

    def Selectnode(self):
        ucb=[]
        for i in range(len(self.childNodes)):
            ucb.append(self.childNodes[i].wins/self.childNodes[i].visits+1.0*sqrt(2*log(self.visits)/self.childNodes[i].visits))
        m = np.amax(ucb)
        indices = np.nonzero(ucb == m)[0]
        ind=pr.choice(indices)
        s=self.childNodes[ind]
        return s

    def Addnode(self, m, s):

        n = Node(position = m, parent = self, state = s)
        self.childNodes.append(n)

    def simulation(self,state):
        predicted_smile=predict_smile(model,state)
        input_smile=make_input_smile(predicted_smile)
        logp,valid_smile,all_smile=logp_calculation(input_smile)

        return logp,valid_smile,all_smile

    def Update(self, result):

        self.visits += 1
        self.wins += result

def MCTS(root, verbose = False):

    """initialization of the chemical trees and grammar trees"""
    run_time=time.time()+600*2
    rootnode = Node(state = root)
    state = root.Clone()
    maxnum=0
    iteration_num=0
    start_time=time.time()
    """----------------------------------------------------------------------"""

    """global variables used for save valid compounds and simulated compounds"""
    valid_compound=[]
    all_simulated_compound=[]
    desired_compound=[]
    max_score=-100.0
    desired_activity=[]
    time_distribution=[]
    num_searched=[]
    current_score=[]
    depth=[]
    all_score=[]

    """----------------------------------------------------------------------"""

    while maxnum<10100:
        print maxnum
        node = rootnode
        state = root.Clone()
        """selection step"""
        node_pool=[]
        print "current found max_score:",max_score

        while node.childNodes!=[]:
            node = node.Selectnode()
            state.SelectPosition(node.position)
        print "state position:,",state.position
        depth.append(len(state.position))
        if len(state.position)>=81:
            re=-1.0
            while node != None:
                node.Update(re)
                node = node.parentNode
        else:
            """------------------------------------------------------------------"""

            """expansion step"""
            """calculate how many nodes will be added under current leaf"""
            expanded=expanded_node(model,state.position,val)
            nodeadded=node_to_add(expanded,val)

            all_posible=chem_kn_simulation(model,state.position,val,nodeadded)
            generate_smile=predict_smile(all_posible,val)
            new_compound=make_input_smile(generate_smile)

            node_index,score,valid_smile,all_smile=check_node_type(new_compound,SA_mean,SA_std,logP_mean,logP_std,cycle_mean,cycle_std)

            print node_index
            valid_compound.extend(valid_smile)
            all_simulated_compound.extend(all_smile)
            all_score.extend(score)
            iteration_num=len(all_simulated_compound)
            if len(node_index)==0:
                re=-1.0
                while node != None:
                    node.Update(re)
                    node = node.parentNode
            else:
                re=[]
                for i in range(len(node_index)):
                    m=node_index[i]
                    maxnum=maxnum+1
                    node.Addnode(nodeadded[m],state)
                    node_pool.append(node.childNodes[i])
                    if score[i]>=max_score:
                        max_score=score[i]
                        current_score.append(max_score)
                    else:
                       current_score.append(max_score)
                    depth.append(len(state.position))
                    """simulation"""
                    re.append((0.8*score[i])/(1.0+abs(0.8*score[i])))
                    if maxnum==100:
                        maxscore100=max_score
                        time100=time.time()-start_time
                    if maxnum==500:
                        maxscore500=max_score
                        time500=time.time()-start_time
                    if maxnum==1000:

                        maxscore1000=max_score
                        time1000=time.time()-start_time
                    if maxnum==5000:
                        maxscore5000=max_score
                        time5000=time.time()-start_time
                    if maxnum==10000:
                        time10000=time.time()-start_time
                        maxscore10000=max_score
                        #valid10000=10000*1.0/len(all_simulated_compound)
                    """backpropation step"""
                #print "node pool length:",len(node.childNodes)

                for i in range(len(node_pool)):

                    node=node_pool[i]
                    while node != None:
                        node.Update(re[i])
                        node = node.parentNode

            #finish_iteration_time=time.time()-iteration_time
            #print "four step time:",finish_iteration_time

        """check if found the desired compound"""

    #print "all valid compounds:",valid_compound

    finished_run_time=time.time()-start_time

    print "logp max found:", current_score
    #print "length of score:",len(current_score)
    #print "time:",time_distribution

    print "valid_com=",valid_compound
    print "num_valid:", len(valid_compound)
    print "all compounds:",len(all_simulated_compound)
    print "score=", all_score
    print "depth=",depth
    print len(depth)
    print "runtime",finished_run_time
    #print "num_searched=",num_searched
    print "100 max:",maxscore100,time100
    print "500 max:",maxscore500,time500
    print "1000 max:",maxscore1000,time1000
    print "5000 max:",maxscore5000,time5000
    print "10000 max:",maxscore10000,time10000
    return valid_compound

def UCTchemical():
    one_search_start_time=time.time()
    time_out=one_search_start_time+60*10
    state = chemical()
    best = MCTS(root = state,verbose = False)

    return best

if __name__ == "__main__":
    smile_old=zinc_data_with_bracket_original()
    val,smile=zinc_processed_with_bracket(smile_old)
    print val
    #val=['\n', '&', 'C', '(', ')', 'c', '1', '2', 'o', '=', 'O', 'N', '3', 'F', '[C@@H]', 'n', '#', 'S', 'Cl', '[O-]', '[C@H]', '[NH+]', '[C@]', 's', 'Br' '[nH]', '[NH3+]', '4', '[NH2+]', '[C@@]', '[N+]', '[nH+]', '[N-]', '[n+]', 'I', '[n-]', '[OH+]', '[NH-]', '[o+]', '[CH2-]', '[CH-]', '[O+]']

    logP_values = np.loadtxt('logP_values.txt')
    SA_scores = np.loadtxt('SA_scores.txt')
    cycle_scores = np.loadtxt('cycle_scores.txt')
    SA_mean =  np.mean(SA_scores)
    print len(SA_scores)

    SA_std=np.std(SA_scores)
    logP_mean = np.mean(logP_values)
    logP_std= np.std(logP_values)
    cycle_mean = np.mean(cycle_scores)
    cycle_std=np.std(cycle_scores)
    #val2=['C', '(',  'c', '1',  'o', '=', 'O', 'N', 'F', '[C@@H]', 'n',  'S', 'Cl', '[O-]']
    #val2=['C', 'c','#', '3', '(', '2', 'n', 'O', '/', 'N', '=', '\\', ')', '1', 'o', '4', 's', '[C@H]', 'F', 'S', 'Cl', '[C@@H]', '[C@@]', '[C@]', '5', '#', '[nH]', 'Br', 'I', '6', '-', '[NH+]', '[N-]', '[N+]', '[n+]', '[nH+]', '[NH2+]','[NH3+]']
    #val=['\n', '&', 'C', '(', ')', 'c', '1', '2', 'o', '=', 'O', 'N', '3', 'F', '[C@@H]', 'n', '#', 'S', 'Cl', '[O-]', '[C@H]>
    val2=['C', '(', ')', 'c', '1', '2', 'o', '=', 'O', 'N', '3', 'F', '[C@@H]', 'n', '-', '#', 'S', 'Cl', '[O-]', '[C@H]', '[NH+]', '[C@]', 's', 'Br', '/', '[nH]', '[NH3+]', '4', '[NH2+]', '[C@@]', '[N+]', '[nH+]', '\\', '[S@]', '5', '[N-]', '[n+]', '[S@@]', '[S-]', '6', '7', 'I', '[n-]', 'P', '[OH+]', '[NH-]', '[P@@H]', '[P@@]', '[PH2]', '[P@]', '[P+]', '[S+]', '[o+]', '[CH2-]', '[CH-]', '[SH+]', '[O+]', '[s+]', '[PH+]', '[PH]', '8', '[S@@+]']

    model=loaded_model()
    #acitivity_model=loaded_activity_model()
    valid_compound=UCTchemical()

max_stars_repo_name: hieubot/Elix

max_issues_repo_name: hieubot/Elix

max_forks_repo_name: hieubot/Elix


text:
# Utility functions for celeba dataset

# Based on
# https://github.com/rasbt/stat453-deep-learning-ss21/blob/main/L17/helper_data.py

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

# https://github.com/rasbt/stat453-deep-learning-ss21/blob/main/L17/helper_data.py#L251
def compute_average_images(feature_idx, image_dim, data_loader, device=None, encoding_fn=None, max_batches=None):
    avg_img_with_feat = torch.zeros(image_dim, dtype=torch.float32)
    avg_img_without_feat = torch.zeros(image_dim, dtype=torch.float32)

    num_img_with_feat = 0
    num_images_without_feat = 0

    num_batches = 0
    for images, labels in data_loader:
        num_batches += 1
        idx_img_with_feat = labels[:, feature_idx].to(torch.bool)

        if encoding_fn is None:
            embeddings = images
        else:
            ####################################
            ### Get latent representation
            with torch.no_grad():
                if device is not None:
                    images = images.to(device)
                embeddings = encoding_fn(images).to('cpu')
            ####################################

        avg_img_with_feat += torch.sum(embeddings[idx_img_with_feat], axis=0)
        avg_img_without_feat += torch.sum(embeddings[~idx_img_with_feat], axis=0)
        num_img_with_feat += idx_img_with_feat.sum(axis=0)
        num_images_without_feat += (~idx_img_with_feat).sum(axis=0)

        if max_batches is not None:
            if num_batches > max_batches: break

    avg_img_with_feat /= num_img_with_feat
    avg_img_without_feat /= num_images_without_feat

    return avg_img_with_feat, avg_img_without_feat

# https://github.com/rasbt/stat453-deep-learning-ss21/blob/main/L17/helper_plotting.py#L172
def plot_modified_images(original, diff,
                         diff_coefficients=(0., 0.5, 1., 1.5, 2., 2.5, 3.),
                         decoding_fn=None,
                         device=None,
                         normalize=True,
                         figsize=(10, 4)):
    fig, axes = plt.subplots(nrows=2, ncols=len(diff_coefficients),
                             sharex=True, sharey=True, figsize=figsize)

    for i, alpha in enumerate(diff_coefficients):
        more = original + alpha * diff
        less = original - alpha * diff

        if decoding_fn is not None:
            ### Latent -> Original space
            with torch.no_grad():
                if device is not None:
                    more = more.to(device).unsqueeze(0)
                    less = less.to(device).unsqueeze(0)

                more = decoding_fn(more).to('cpu').squeeze(0)
                less = decoding_fn(less).to('cpu').squeeze(0)

        if normalize:   # map from -1..1 to 0..1
          more = more / 2 + 0.5
          less = less / 2 + 0.5

        smore = f'+{alpha}'
        sless = f'-{alpha}'

        axes[0][i].set_title(smore)
        axes[0][i].imshow(more.permute(1, 2, 0))

        axes[1][i].set_title(sless)
        axes[1][i].imshow(less.permute(1, 2, 0))

        axes[1][i].axison = False
        axes[0][i].axison = False

max_stars_repo_name: karalleyna/pyprobml