text:
import keras
from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D, Flatten, Activation
from keras.callbacks import TensorBoard
import os
import random
import numpy as np
import time
from tensorflow.keras.callbacks import TensorBoard

os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

#NAME = 'terran-ai-bot-{}'.format(int(time.time()))

#dense_layers = [0,1,2]
#layer_sizes = [32, 64, 128]
#conv_layers = [1, 2, 3]

#for dense_layer in dense_layers:
#   for layer_size in layer_sizes:
#      for conv_layer in conv_layers:
#         NAME = '{}-conv-{}-nodes-{}-dense-{}-time'.format(conv_layer_size, layer_size, dense_layer, int(time.time()))

#         model = Sequential()
#         model.add(Conv2D(32, (3,3), padding='same',
#                          input_shape=(184, 208, 3),
#                          activation='relu'))

#         model.add(Conv2D(32, (3,3), padding='same',
#                          input_shape=(184, 208, 3),
#                          activation='relu'))
#         model.add(Conv2D(32, (3, 3), activation='relu'))
#         model.add(MaxPooling2D(pool_size=(2, 2)))
#         model.add(Dropout(0.2))

#         model.add(Conv2D(64, (3, 3), padding='same',
#                          activation='relu'))
#         model.add(Conv2D(32, (3, 3), activation='relu'))
#         model.add(MaxPooling2D(pool_size=(2, 2)))
#         model.add(Dropout(0.2))

#         model.add(Conv2D(64, (3, 3), padding='same',
#                          activation='relu'))
#         model.add(Conv2D(128, (3, 3), activation='relu'))
#         model.add(MaxPooling2D(pool_size=(2, 2)))
#         model.add(Dropout(0.2))

#         model.add(Flatten())
#         model.add(Dense(128, activation='relu'))
#         model.add(Dropout(0.5))
#         model.add(Dense(6, activation='softmax'))
#         learning_rate = 0.001
#         opt = keras.optimizers.adam(lr=learning_rate, decay = 1e-6)
#         model.compile(loss='categorical_crossentropy',
#                       optimizer=opt,
#                       metrics=['accuracy'])
#tensorboard = TensorBoard(log_dir='logs/{}'.format(NAME))

#model = Sequential()
#model.add(Conv2D(32, (3,3), padding='same',
#                 input_shape=(184, 208, 3),
#                 activation='relu'))

#model.add(Conv2D(32, (3,3), padding='same',
#                 input_shape=(184, 208, 3),
#                 activation='relu'))
#model.add(Conv2D(32, (3, 3), activation='relu'))
#model.add(MaxPooling2D(pool_size=(2, 2)))
#model.add(Dropout(0.2))

#model.add(Conv2D(64, (3, 3), padding='same',
#                 activation='relu'))
#model.add(Conv2D(32, (3, 3), activation='relu'))
#model.add(MaxPooling2D(pool_size=(2, 2)))
#model.add(Dropout(0.2))

#model.add(Conv2D(64, (3, 3), padding='same',
#                 activation='relu'))
#model.add(Conv2D(128, (3, 3), activation='relu'))
#model.add(MaxPooling2D(pool_size=(2, 2)))
#model.add(Dropout(0.2))

#model.add(Flatten())
#model.add(Dense(128, activation='relu'))
#model.add(Dropout(0.5))
#model.add(Dense(6, activation='softmax'))
#learning_rate = 0.001
#opt = keras.optimizers.adam(lr=learning_rate, decay = 1e-6)
#model.compile(loss='categorical_crossentropy',
#              optimizer=opt,
#              metrics=['accuracy'])
#tensorboard = TensorBoard(log_dir='logs/stage1')

model = Sequential()
model.add(Conv2D(32, (7, 7), padding='same',
                 input_shape=(184, 208, 3),
                 activation='relu'))
model.add(Conv2D(32, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))

model.add(Conv2D(64, (3, 3), padding='same',
                 activation='relu'))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))

model.add(Conv2D(128, (3, 3), padding='same',
                 activation='relu'))
model.add(Conv2D(128, (3, 3), activation='relu'))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.2))

model.add(Dense(256, activation='relu'))
model.add(Dropout(0.5))

model.add(Flatten())
model.add(Dense(1024, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(11, activation='softmax'))

learning_rate = 0.0001
opt = keras.optimizers.adam(lr=learning_rate)#, decay=1e-6)

model.compile(loss='categorical_crossentropy',
              optimizer=opt,
              metrics=['accuracy'])
#model = Sequential()
#model.add(Conv2D(32, (3, 3), padding='same',
#                 input_shape=(184, 208, 3),
#                 activation='relu'))

#model.add(Conv2D(8, (3, 3), activation='relu'))
#model.add(Conv2D(32, (3, 3), activation='relu'))
#model.add(MaxPooling2D(pool_size=(3, 3)))
#model.add(Dropout(0.5))

#model.add(Conv2D(32, (3, 3), activation='relu'))
#model.add(Conv2D(64, (3, 3), activation='relu'))
#model.add(MaxPooling2D(pool_size=(2,2)))
#model.add(Dropout(0.2))

#model.add(Conv2D(256, (3, 3), activation='relu'))
#model.add(MaxPooling2D(pool_size=(3, 3)))
#model.add(Dropout(0.7))

#model.add(Flatten())
#model.add(Dense(1024, activation='relu'))
#model.add(Dropout(0.5))
#model.add(Dense(11, activation='softmax'))
#learning_rate = 0.0001
#opt = keras.optimizers.adam(lr=learning_rate, decay = 1e-6)
#model.compile(loss='categorical_crossentropy',
#              optimizer=opt,
#              metrics=['accuracy'])
tensorboard = TensorBoard(log_dir='logs/stage6')

def check_data(choices):
    total_data = 0

    lengths = []
    for choice in choices:
        print("Length of {} is: {}".format(choice, len(choices[choice])))
        total_data += len(choices[choice])
        lengths.append(len(choices[choice]))

    print("Total data length now is:", total_data)
    return lengths

train_data_dir = 'train_data/data'
#train_data_dir = 'train_data'
hm_epochs = 5

for i in range(hm_epochs):
   current = 0
   increment = 5
   not_maximum = True
   all_files = os.listdir(train_data_dir)
   maximum = len(all_files)
   random.shuffle(all_files)

   while not_maximum:
      print("WORKING ON {}:{}, EPOCH:{}".format(current, current+increment, i))
      #no_attacks = []
      #attack_closest_to_nexus = []
      #attack_enemy_structures = []
      #attack_enemy_start = []
      #harass_attack = []
      choices = {0: [],
                 1: [],
                 2: [],
                 3: [],
                 4: [],
                 5: [],
                 6: [],
                 7: [],
                 8: [],
                 9: [],
                 10: []
                 }

      for file in all_files[current:current+increment]:
         full_path = os.path.join(train_data_dir, file)
         data = np.load(full_path)
         data = list(data)
         #for d in data:
         #      choice = np.argmax(d[0])
         #      if choice == 0:
         #         no_attacks.append(d)
         #      elif choice == 1:
         #         attack_closest_to_nexus.append(d)
         #      elif choice == 2:
         #         attack_enemy_structures.append(d)
         #      elif choice == 3:
         #         attack_enemy_start.append(d)
         #      elif choice == 4:
         #         harass_attack.append(d)
         for d in data:
            choice = np.argmax(d[0])
            choices[choice].append([d[0], d[1]])

      lengths = check_data(choices)
      lowest_data = min(lengths)
      print('lengths: {}'.format(lengths))
      print('lowest data: {}'.format(lowest_data))

      for choice in choices:
         random.shuffle(choices[choice])
         choices[choice] = choices[choice][:lowest_data]

      check_data(choices)

      #train_data = no_attacks + attack_closest_to_nexus + attack_enemy_structures + attack_enemy_start + harass_attack
      train_data = []
      for choice in choices:
         for d in choices[choice]:
            train_data.append(d)

      random.shuffle(train_data)
      print(len(train_data))

      #test_size = 30
      #batch_size = 60
      test_size = 10
      batch_size = 20

      x_train = np.array([i[1] for i in train_data[:-test_size]]).reshape(-1, 184, 208, 3)
      y_train = np.array([i[0] for i in train_data[:-test_size]])

      x_test = np.array([i[1] for i in train_data[-test_size:]]).reshape(-1, 184, 208, 3)
      y_test = np.array([i[0] for i in train_data[-test_size:]])

      model.fit(x_train, y_train,
               batch_size=batch_size,
               validation_data=(x_test, y_test),
               shuffle=True,
               verbose=1,
               epochs=hm_epochs,
              callbacks=[tensorboard])

      model.save("models/december-12-2018/BasicCNN-{}-epochs-{}-LR-STAGE2".format(hm_epochs, learning_rate))
      current += increment
      if current > maximum:
         not_maximum = False

#model = Sequential()
#model.add(Conv2D(32, (7, 7), padding='same',
#                 input_shape=(176, 200, 1),
#                 activation='relu'))
#model.add(Conv2D(32, (3, 3), activation='relu'))
#model.add(MaxPooling2D(pool_size=(2, 2)))
#model.add(Dropout(0.2))

#model.add(Conv2D(64, (3, 3), padding='same',
#                 activation='relu'))
#model.add(Conv2D(64, (3, 3), activation='relu'))
#model.add(MaxPooling2D(pool_size=(2, 2)))
#model.add(Dropout(0.2))

#model.add(Conv2D(128, (3, 3), padding='same',
#                 activation='relu'))
#model.add(Conv2D(128, (3, 3), activation='relu'))
#model.add(MaxPooling2D(pool_size=(2, 2)))
#model.add(Dropout(0.2))

#model.add(Flatten())
#model.add(Dense(1024, activation='relu'))
#model.add(Dropout(0.5))
#model.add(Dense(11, activation='softmax'))

#learning_rate = 0.001
#opt = keras.optimizers.adam(lr=learning_rate)#, decay=1e-6)

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

#tensorboard = TensorBoard(log_dir="logs/STAGE2-{}-{}".format(int(time.time()), learning_rate))

#train_data_dir = "train_data/data"

##model = keras.models.load_model('BasicCNN-5000-epochs-0.001-LR-STAGE2')

#def check_data(choices):
#    total_data = 0

#    lengths = []
#    for choice in choices:
#        print("Length of {} is: {}".format(choice, len(choices[choice])))
#        total_data += len(choices[choice])
#        lengths.append(len(choices[choice]))

#    print("Total data length now is:", total_data)
#    return lengths

#hm_epochs = 5000

#for i in range(hm_epochs):
#    current = 0
#    increment = 50
#    not_maximum = True
#    all_files = os.listdir(train_data_dir)
#    maximum = len(all_files)
#    random.shuffle(all_files)

#    while not_maximum:
#        try:
#            print("WORKING ON {}:{}, EPOCH:{}".format(current, current+increment, i))

#            choices = {0: [],
#                       1: [],
#                       2: [],
#                       3: [],
#                       4: [],
#                       5: [],
#                       6: [],
#                       7: [],
#                       8: [],
#                       9: [],
#                       10: [],
#                       }

#            for file in all_files[current:current+increment]:
#                try:
#                    full_path = os.path.join(train_data_dir, file)
#                    data = np.load(full_path)
#                    data = list(data)
#                    for d in data:
#                        choice = np.argmax(d[0])
#                        choices[choice].append([d[0], d[1]])
#                except Exception as e:
#                    print(str(e))

#            lengths = check_data(choices)

#            lowest_data = min(lengths)

#            for choice in choices:
#                random.shuffle(choices[choice])
#                choices[choice] = choices[choice][:lowest_data]

#            check_data(choices)

#            train_data = []

#            for choice in choices:
#                for d in choices[choice]:
#                    train_data.append(d)

#            random.shuffle(train_data)
#            print(len(train_data))

#            test_size = 100
#            batch_size = 128  # 128 best so far.

#            x_train = np.array([i[1] for i in train_data[:-test_size]]).reshape(-1, 184, 208, 3)
#            y_train = np.array([i[0] for i in train_data[:-test_size]])

#            x_test = np.array([i[1] for i in train_data[-test_size:]]).reshape(-1, 184, 208, 3)
#            y_test = np.array([i[0] for i in train_data[-test_size:]])

#            model.fit(x_train, y_train,
#                      batch_size=batch_size,
#                      validation_data=(x_test, y_test),
#                      shuffle=True,
#                      epochs=1,
#                      verbose=1, callbacks=[tensorboard])

#            model.save("BasicCNN-5000-epochs-0.001-LR-STAGE2")
#        except Exception as e:
#            print(str(e))
#        current += increment
#        if current > maximum:
#            not_maximum = False

max_stars_repo_name: Vilchis-Joshua/SeniorProject

max_issues_repo_name: Vilchis-Joshua/SeniorProject

max_forks_repo_name: Vilchis-Joshua/SeniorProject


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jul 23 08:56:44 2019

Creates a unit normalized field of Rayleigh damping (picture frame)
Works the old fashioned way with lots of nested loops... so sue me!

@author: -
"""

import math as mt
import numpy as np
import scipy.sparse as sps
from matplotlib import cm
import matplotlib.pyplot as plt

def computeRayleighField(DIMS, REFS, height, width, applyTop, applyLateral):

       # Get DIMS data
       L1 = DIMS[0]
       L2 = DIMS[1]
       ZH = DIMS[2]
       NX = DIMS[3] + 1
       NZ = DIMS[4]

       RP = 4
       GP = 2

       # Get REFS data
       X = REFS[4]
       Z = REFS[5]

       # Set the layer bounds
       dLayerZ = height
       dLayerR = L2 - width
       dLayerL = L1 + width
       depth = ZH - height

       # Assemble the Rayleigh field
       RL = np.zeros((NZ, NX))
       RLX = np.zeros((NZ, NX))
       RLZ = np.zeros((NZ, NX))
       SBR = np.ones((NZ, NX))

       for ii in range(0,NZ):
              for jj in range(0,NX):
                     # Get this X location
                     XRL = X[ii,jj]
                     ZRL = Z[ii,jj]
                     if applyLateral:
                            # Left layer or right layer or not? [1 0]
                            if XRL >= dLayerR:
                                   dNormX = (L2 - XRL) / width
                            elif XRL <= dLayerL:
                                   dNormX = (XRL - L1) / width
                            else:
                                   dNormX = 1.0
                            # Evaluate the Rayleigh factor
                            RFX = (mt.cos(0.5 * mt.pi * dNormX))**RP
                     else:
                            RFX = 0.0
                     if applyTop:
                            # In the top layer?
                            if ZRL >= dLayerZ[jj]:
                                   # This maps [depth ZH] to [1 0]
                                   dNormZ = (ZH - ZRL) / depth[jj]
                            else:
                                   dNormZ = 1.0
                            # Evaluate the strength of the field
                            RFZ = (mt.cos(0.5 * mt.pi * dNormZ))**RP
                     else:
                            RFZ = 0.0

                     # Set the field to max(lateral, top) to handle corners
                     RLX[ii,jj] = RFX
                     RLZ[ii,jj] = RFZ
                     RL[ii,jj] = np.amax([RFX, RFZ])
                     # Set the binary matrix
                     if RL[ii,jj] != 0.0:
                            SBR[ii,jj] = 0.0
       '''
       plt.figure()
       plt.contourf(X, Z, RL, 101, cmap=cm.seismic)
       plt.colorbar()
       plt.show()
       input()
       '''
       # Assemble the Grid Matching Layer field X and Z directions
       GML = np.ones((NZ, NX))
       GMLX = np.ones((NZ, NX))
       GMLZ = np.ones((NZ, NX))
       C1 = 0.02
       C2 = 10.0
       isStretchGML = True # True: trig GML to RHS, False, direct GML to state
       for ii in range(0,NZ):
              for jj in range(0,NX):
                     # Get this X location
                     XRL = X[ii,jj]
                     ZRL = Z[ii,jj]
                     if applyLateral:
                            # Left layer or right layer or not? [0 1]
                            if XRL >= dLayerR:
                                   dNormX = (XRL - dLayerR) / width
                            elif XRL <= dLayerL:
                                   dNormX = (dLayerL - XRL) / width
                            else:
                                   dNormX = 0.0

                            if isStretchGML:
                                   # Evaluate the GML factor
                                   #RFX = (mt.tan(0.5 * mt.pi * dNormX))**GP
                                   RFX = 2.0 * dNormX**2
                            else:
                                   # Evaluate buffer layer factor
                                   RFX = (1.0 - C1 * dNormX**2) * \
                                          (1.0 - (1.0 - mt.exp(C2 * dNormX**2)) / (1.0 - mt.exp(C2)))
                     else:
                            RFX = 0.0
                     if applyTop:
                            # In the top layer?
                            if ZRL >= dLayerZ[jj]:
                                   dNormZ = (ZRL - dLayerZ[jj]) / (ZH - height[jj])
                            else:
                                   dNormZ = 0.0

                            if isStretchGML:
                                   # Evaluate the strength of the field
                                   #RFZ = (mt.tan(0.5 * mt.pi * dNormZ))**GP
                                   RFZ = 2.0 * dNormZ**2
                            else:
                                   # Evaluate buffer layer factor
                                   RFZ = (1.0 - C1 * dNormZ**2) * \
                                          (1.0 - (1.0 - mt.exp(C2 * dNormZ**2)) / (1.0 - mt.exp(C2)))
                     else:
                            RFZ = 0.0

                     if isStretchGML:
                            GMLX[ii,jj] = 1.0 / (1.0 + RFX)
                            GMLZ[ii,jj] = 1.0 / (1.0 + RFZ)
                            # Set the field to max(lateral, top) to handle corners
                            RFM = np.amax([RFX, RFZ])
                            GML[ii,jj] = 1.0 / (1.0 + RFM)
                     else:
                            GMLX[ii,jj] = RFX
                            GMLZ[ii,jj] = RFZ
                            # Set the field to max(lateral, top) to handle corners
                            GML[ii,jj] = np.amin([RFX, RFZ])
       '''
       plt.figure()
       plt.contourf(X, Z, GMLX, 101, cmap=cm.seismic)
       plt.colorbar()
       plt.show()
       input()
       '''
       return (GML, GMLX, GMLZ), RL, RLX, RLZ, SBR

def computeRayleighEquations(DIMS, REFS, depth, RLOPT, topdex, botdex):
       # Get options data
       width = RLOPT[1]
       applyTop = RLOPT[2]
       applyLateral = RLOPT[3]
       mu = RLOPT[4]

       # Get DIMS data
       NX = DIMS[3] + 1
       NZ = DIMS[4]
       OPS = NX * NZ

       # Set up the Rayleigh field
       GML, RL, RLX, RLZ, SBR = computeRayleighField(DIMS, REFS, depth, width, \
                                                     applyTop, applyLateral)

       # Compute the diagonal for full Rayleigh field
       tempDiagonal = np.reshape(RL, (OPS,), order='F')
       # Compute the matrix operator
       RLM = sps.spdiags(tempDiagonal, 0, OPS, OPS)
       '''
       # Compute the diagonal for full Rayleigh field
       tempDiagonal = np.reshape(RLX, (OPS,), order='F')
       # Compute the matrix operator
       RLXM = sps.spdiags(tempDiagonal, 0, OPS, OPS)
       # Compute the diagonal for full Rayleigh field
       tempDiagonal = np.reshape(RLZ, (OPS,), order='F')
       # Compute the matrix operator
       RLZM = sps.spdiags(tempDiagonal, 0, OPS, OPS)
       '''
       # Store the diagonal blocks corresponding to Rayleigh damping terms
       ROPS = mu * np.array([RLM, RLM, RLM, RLM])

       return ROPS, RLM, GML, SBR

ext: py

max_stars_repo_name: jeguerra/nonlinearMtnWavesSolver

max_issues_repo_name: jeguerra/nonlinearMtnWavesSolver

max_forks_repo_name: jeguerra/nonlinearMtnWavesSolver

reason: import numpy,import scipy


text:
import pandas as pd
import numpy as np
from sklearn import metrics
from functools import partial

from commons import *
from classifier import *
from sklearn.model_selection import KFold, StratifiedKFold
import numpy as np

def crossvalidation_transformed( X, Y, Y_pred, K=10):
    score = np.zeros(K)
    kf = StratifiedKFold(n_splits=K, shuffle=True)
    i = 0
    for train, test in kf.split(X, Y):
        print(i)
        X_train, X_test, y_train, y_test = X.iloc[train], X.iloc[test], Y[train], Y[test]

        score[i] = accuracy(y_test, Y_pred[test])
        i += 1
    return score

train = pd.DataFrame.from_csv(trainFile,sep=";",index_col=None)
test = pd.DataFrame.from_csv(testFile,sep=";",index_col=None)

train_t = pd.DataFrame.from_csv(trainTransformed,sep=";",index_col=None)
test_t = pd.DataFrame.from_csv(testTransformed,sep=";",index_col=None)

x_train = train
y_train = train[col_decision]

groupped = train_t.groupby(["TransID",'Prediction'])['ExpertID'].agg('count')

dict = defaultdict(dict)

for name in groupped.index:
    trans_id, prediction = name
    dict[trans_id][prediction] =  groupped.loc[name]

y_pred = {}
for trans_id, predictions in dict.items():
    y_pred[trans_id] = vmap_rev[max(predictions, key=predictions.get)]

y_pred_labels = np.array(list(y_pred.values()))

scores = crossvalidation_transformed(x_train,y_train,y_pred_labels,3)

print('Final - Accuracy: %0.3f (+/- %0.3f)'%(np.mean(scores),scores.std()*2))

reason: import numpy


text:
import wisardpkg as wp
import random
import numpy as np
import time
from astropy.stats import bootstrap
from astropy.utils import NumpyRNGContext

LOW_N = 5
HIGH_N = 31
MIN_SCORE = 0.1
GROW_INTERVAL = 100
MAX_DISCRIMINATOR_LIMIT = 10

class BordaBagging(object):

    def __init__(self, train_dataset, learners, partitions = "undefined", voting = "borda0"):
        self.train_dataset = train_dataset
        self.learners = learners
        self.nets = []
        self.partitions = partitions
        if(partitions == "undefined"):
            self.partitions = int(len(train_dataset)/75)
        if(self.partitions == 0):
            self.partitions = 1
        self.entry_size = len(train_dataset.get(0))
        self.voting = voting
        self.training_time = 0
        self.ensemble()

    def random_wisard(self):
        return wp.ClusWisard(np.random.randint(LOW_N, HIGH_N), 0.1, 10, 1)

    def generate_dataset(self):
        boot = []
        for i in range(len(self.train_dataset)):
            boot.append(i)
        with NumpyRNGContext(1):
            bootresult = bootstrap(np.array(boot), self.learners, int(len(self.train_dataset)*self.partitions))

        dataset = []
        for samples in bootresult:
            d = wp.DataSet()
            for sample in samples:
                d.add(self.train_dataset.get(int(sample)), self.train_dataset.getLabel(int(sample)))
            dataset.append(d)

        return dataset

    def ensemble(self):
        dataset = self.generate_dataset()
        for i in range(0, self.learners):
            net = self.random_wisard()
            training_time = time.time()
            net.train(dataset[i])
            self.training_time = self.training_time + time.time() - training_time
            self.nets.append(net)

    def get_training_time(self):
        return self.training_time

    @staticmethod
    def get_labels(out):
        labels = []
        for label in out[0]:
            labels.append(label)
        return labels

    @staticmethod
    def borda_count_0(scores, labels):
        score_labels = [0] * len(labels)
        for i in range(len(scores)):
            for j in range(len(labels)):
                if(scores[i] == labels[j]):
                    score_labels[j] += 1

        scores_template = sorted(set(score_labels))
        new_scores = []
        for i in range(len(score_labels)):
            vote = scores_template.index(score_labels[i])
            new_scores.append(vote/(len(labels)-1))
        return labels[new_scores.index(max(new_scores))]

    @staticmethod
    def borda_count_1(scores, labels):
        score_labels = [0] * len(labels)
        for i in range(len(scores)):
            for j in range(len(labels)):
                if(scores[i] == labels[j]):
                    score_labels[j] += 1

        scores_template = sorted(set(score_labels))
        new_scores = []
        for i in range(len(score_labels)):
            vote = scores_template.index(score_labels[i])
            new_scores.append((vote+1)/len(labels))
        return labels[new_scores.index(max(new_scores))]

    @staticmethod
    def dowdall(scores, labels):
        score_labels = [0] * len(labels)
        for i in range(len(scores)):
            for j in range(len(labels)):
                if(scores[i] == labels[j]):
                    score_labels[j] += 1

        scores_template = sorted(set(score_labels), reverse = True)
        new_scores = []
        for i in range(len(score_labels)):
            vote = scores_template.index(score_labels[i])
            new_scores.append(1/(vote+1))
        return labels[new_scores.index(max(new_scores))]

    def classify(self, test_dataset):
        results = []
        for i in range(0, len(test_dataset)):
            scores = []
            test = wp.DataSet()
            bi = wp.BinInput(test_dataset.get(i))
            test.add(bi, test_dataset.getLabel(i))
            for j in range(0, len(self.nets)):
                scores.append(self.nets[j].classify(test)[0])

            out = self.nets[0].getAllScores(test)
            labels = self.get_labels(out)

            result = 0
            if(self.voting == "borda0"):
                result = self.borda_count_0(scores, labels)
            else:
                if(self.voting == "borda1"):
                    result = self.borda_count_1(scores, labels)
                else:
                    result = self.dowdall(scores, labels)
            results.append(result)

        return results

reason: import numpy,from astropy


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

"""Hyperopt plotting utilities.
"""
import os
import csv
import ipdb
import json
import pandas as pd
import pickle as pkl
from collections import defaultdict

import numpy as np
import scipy.ndimage

from matplotlib import ticker
import matplotlib.pyplot as plt
import matplotlib.ticker as tick
import matplotlib.colors as colors
from matplotlib.ticker import MultipleLocator

import seaborn as sns
sns.set_style('white')
sns.set_palette('bright')

def reformat_large_tick_values(tick_val, pos):
    """
    Turns large tick values (in the billions, millions and thousands) such as 4500 into 4.5K and also appropriately turns 4000 into 4K (no zero after the decimal).
    """
    if tick_val >= 1000000000:
        val = round(tick_val/1000000000, 1)
        new_tick_format = '{:}B'.format(val)
    elif tick_val >= 1000000:
        val = round(tick_val/1000000, 1)
        new_tick_format = '{:}M'.format(val)
    elif tick_val >= 1000:
        val = round(tick_val/1000, 1)
        new_tick_format = '{:}K'.format(val)
    elif tick_val < 1000:
        new_tick_format = round(tick_val, 1)
    else:
        new_tick_format = tick_val

    # make new_tick_format into a string value
    new_tick_format = str(new_tick_format)

    # code below will keep 4.5M as is but change values such as 4.0M to 4M since that zero after the decimal isn't needed
    index_of_decimal = new_tick_format.find(".")

    if index_of_decimal != -1:
        value_after_decimal = new_tick_format[index_of_decimal+1]
        if value_after_decimal == "0":
            # remove the 0 after the decimal point since it's not needed
            new_tick_format = new_tick_format[0:index_of_decimal] + new_tick_format[index_of_decimal+2:]

    return new_tick_format

def load_log(exp_dir, fname='train_log.csv'):
    result_dict = defaultdict(list)
    with open(os.path.join(exp_dir, fname), newline='') as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            for key in row:
                try:
                    if key in ['global_iteration', 'iteration', 'epoch']:
                        result_dict[key].append(int(row[key]))
                    else:
                        result_dict[key].append(float(row[key]))
                except:
                    pass
    return result_dict

def print_best_thetas(log,
                      key='train_loss',
                      print_keys=['train_sum_loss', 'train_loss', 'val_loss', 'train_acc', 'val_acc'],
                      top_k=1,
                      reverse=False,
                      constrained=True):
    sorted_idxs = np.argsort(log[key])

    if reverse:
        sorted_idxs = list(reversed(sorted_idxs))

    if constrained:
        theta_key = 'thetas_constrained'
    else:
        theta_key = 'thetas'

    for k, idx in enumerate(sorted_idxs[:top_k]):
        print('{} Top {}: {}'.format(key, k, log[key][idx]))
        for print_key in print_keys:
            print('{}: {:5.3f}'.format(print_key, log[print_key][idx]))
        print(' ')

    print('\n')

def print_best_thetas_for_keys(log,
                               keys=['train_sum_loss', 'train_loss', 'val_loss', 'train_acc', 'val_acc'],
                               top_k=1,
                               constrained=True):
    for key in keys:
        print_best_thetas(log, key=key, print_keys=keys,
                          top_k=top_k, reverse=True if 'acc' in key else False, constrained=constrained)

def plot_hist(directory, key, ax=None, bins=60, min_value=None, max_value=None, yscale='linear'):
    rs_values = get_all_random_search_values(directory, key=key)
    if not max_value:
        max_value = np.nanmax(rs_values)
    if not min_value:
        min_value = np.nanmin(rs_values)

    if ax:
        plt.sca(ax)
    else:
        plt.figure()

    plt.hist([value for value in np.sort(rs_values) if (value >= min_value) and (value <= max_value)], bins=bins)
    plt.xticks(fontsize=18)
    plt.yticks(fontsize=18)
    plt.yscale(yscale)
    plt.xlabel('Objective Value', fontsize=20)
    plt.ylabel('Count', fontsize=20)
    plt.title(key, fontsize=20)

def get_best_theta(log, key='train_losses', top_k=1, reverse=False, constrained=True):
    sorted_idxs = np.argsort(log[key])

    if reverse:
        sorted_idxs = list(reversed(sorted_idxs))

    if constrained:
        theta_key = 'thetas_constrained'
    else:
        theta_key = 'thetas'

    # for idx in sorted_idxs[:top_k]:
    #     for name, value in zip(log['hparam_fieldnames'], log[theta_key][idx]):
    #         print('\t{}: {:6.3e}'.format(name, value))

    best_theta = log[theta_key][sorted_idxs[0]]
    value_at_best_theta = log[key][sorted_idxs[0]]
    # print('Best theta: {} | Value at theta: {}'.format(best_theta, value_at_best_theta))
    return best_theta, value_at_best_theta, sorted_idxs

def make_dataframe(result):
    subset_result = {}
    subset_result['thetas'] = [', '.join(['{:5.2f}'.format(item) for item in theta]) for theta in result['thetas']]
    subset_result['train_accs'] = result['train_accs']
    subset_result['val_accs'] = result['val_accs']
    subset_result['train_losses'] = result['train_losses']
    subset_result['val_losses'] = result['val_losses']
    subset_result['trajectory_sums'] = result['trajectory_sums']
    # subset_result['train_cost'] = result['train_cost']
    df = pd.DataFrame.from_dict(subset_result, orient='index').transpose()
    return df

def make_theta_dicts(result):
    best_theta_F_sum, value_at_best_theta_F_sum = get_best_theta(result, key='trajectory_sums', reverse=False)
    best_theta_train_loss, value_at_best_theta_train_loss = get_best_theta(result, key='train_losses', reverse=False)
    best_theta_val_loss, value_at_best_theta_val_loss = get_best_theta(result, key='val_losses', reverse=False)
    best_theta_train_acc, value_at_best_theta_train_acc = get_best_theta(result, key='train_accs', reverse=True)
    best_theta_val_acc, value_at_best_theta_val_acc = get_best_theta(result, key='val_accs', reverse=True)

    best_theta_dict = {'F_sum': best_theta_F_sum,
                       'full_train_loss': best_theta_train_loss,
                       'full_val_loss': best_theta_val_loss,
                       'full_train_acc': best_theta_train_acc,
                       'full_val_acc': best_theta_val_acc,
                      }

    value_at_best_theta_dict = {'F_sum': value_at_best_theta_F_sum,
                                'full_train_loss': value_at_best_theta_train_loss,
                                'full_val_loss': value_at_best_theta_val_loss,
                                'full_train_acc': value_at_best_theta_train_acc,
                                'full_val_acc': value_at_best_theta_val_acc,
                               }

    return best_theta_dict, value_at_best_theta_dict

def plot_heatmap(pkl_path, xlabel, ylabel, title='', key='train_sum_loss', cmap=plt.cm.gray, levels=10, figsize=(10,8)):
    with open(pkl_path, 'rb') as f:
        result = pkl.load(f)

    side_length = int(np.sqrt(len(result['thetas'])))

    # grid_data = result[key]
    grid_data = result[key].reshape(side_length, side_length)
    vmin = np.nanmin(grid_data)
    vmax = np.nanmax(grid_data)

    xv, yv = result['xv'], result['yv']

    if key in ['F_grid_train_loss', 'F_grid_val_loss'] and (vmax > 3):
        vmax = 3
    elif key == 'train_sum_loss':
        vmax = 1e4

    grid_data[np.isnan(grid_data)] = vmax
    grid_data[grid_data > vmax] = vmax

    smoothed_F_grid = scipy.ndimage.gaussian_filter(grid_data, sigma=1.0)

    plt.figure(figsize=figsize)
    if key in ['F_grid_train_loss', 'F_grid_val_loss', 'train_sum_loss']:
        smoothed_F_grid = np.log(smoothed_F_grid)

    if 'acc' in key:
        # levels = [0, 0.5, 0.6, 0.7, 0.8, 0.85, 0.9, 0.95, 0.97, 0.98, 0.99]
        levels = [0.9, 0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99, 1.0]
        contour_cmap = plt.cm.get_cmap(cmap, len(levels)+1)
        CS = plt.contourf(yv, xv, smoothed_F_grid.T, levels=levels, cmap=contour_cmap)
        print(CS.levels)
        cbar = plt.colorbar(CS, boundaries=levels)
        cbar.ax.tick_params(labelsize=16)
    else:
        contour_cmap = plt.cm.get_cmap(cmap, levels+1)
        CS = plt.contourf(yv, xv, smoothed_F_grid.T, levels=levels, cmap=contour_cmap)
        cbar = plt.colorbar(CS)
        cbar.ax.tick_params(labelsize=16)
    plt.xticks(fontsize=18)
    plt.yticks(fontsize=18)
    plt.xlabel(xlabel, fontsize=20)
    plt.ylabel(ylabel, fontsize=20)
    plt.title(title, fontsize=24)

def plot_hparams(log,
                 keys,
                 inner_problem_len=200,
                 xkey='inner_problem_steps',
                 plot_inner_problem_ticks=False,
                 xlim=None,
                 xtick_locs=None,
                 xtick_labels=None,
                 xscale='linear',
                 yscale='linear',
                 xlabel='Inner Iteration',
                 ylabel='Hyperparameter Value',
                 show_legend=True,
                 legend_outside=True):
    plt.figure(figsize=(8,5))

    colors = plt.rcParams['axes.prop_cycle'].by_key()['color']

    for i, key in enumerate(keys):
        # plt.plot(log['result_dict'][xkey], np.log10(np.exp(np.array(log['result_dict'][key]))),
        #          label='{}'.format(key), color=colors[i % len(colors)], marker='o', linewidth=2, linestyle='-')
        plt.plot(log[xkey], np.log10(np.exp(np.array(log[key]))),
                 label='{}'.format(key), color=colors[i % len(colors)], linewidth=2, linestyle='-')

    plt.xlabel(xlabel, fontsize=18)
    plt.ylabel(ylabel, fontsize=18)

    if xlim:
        plt.xlim(xlim)
    plt.xscale(xscale)
    plt.yscale(yscale)

    if plot_inner_problem_ticks:
        ml = MultipleLocator(inner_problem_len)
        plt.gca().xaxis.set_minor_locator(ml)
        plt.gca().xaxis.set_major_locator(ml)
        plt.gca().xaxis.grid(which='both', color='k', linestyle='-.', linewidth=0.5, alpha=0.5)

    if xtick_locs:
        plt.xticks(xtick_locs, xtick_labels, fontsize=18)
    else:
        plt.xticks(fontsize=18)
    plt.yticks(fontsize=18)

    if show_legend:
        if legend_outside:
            plt.legend(fontsize=16, fancybox=True, framealpha=0.3, bbox_to_anchor=(1.04,1), loc='upper left')
        else:
            plt.legend(fontsize=16, fancybox=True, framealpha=0.3)

    plt.gca().xaxis.set_major_formatter(tick.FuncFormatter(reformat_large_tick_values))
    sns.despine()

def plot_es_pes_hparams(es_log, pes_log, keys, inner_problem_len=200, xlim=None,
                        xtick_locs=None, xtick_labels=None, yscale='linear',
                        xlabel='Inner Iteration', ylabel='Hyperparameter Value',
                        legend_outside=False):
    plt.figure(figsize=(8,5))
    # plt.axhline(y=best_theta_dict['F_sum'], color='k')

    colors = plt.rcParams['axes.prop_cycle'].by_key()['color']

    for i, key in enumerate(keys):
        plt.plot(es_log['inner_problem_steps'], es_log[key],
                 label='ES {}'.format(key), color=colors[i % len(colors)], linewidth=2, linestyle='-', alpha=0.4)

    for i, key in enumerate(keys):
        plt.plot(pes_log['inner_problem_steps'], pes_log[key],
                 label='PES {}'.format(key), color=colors[i % len(colors)], linewidth=2, alpha=1)

    ml = MultipleLocator(inner_problem_len)
    plt.gca().xaxis.set_minor_locator(ml)
    plt.gca().xaxis.set_major_locator(ml)
    plt.gca().xaxis.grid(which='both', color='k', linestyle='-.', linewidth=0.5, alpha=0.5)

    plt.xlabel(xlabel, fontsize=18)
    plt.ylabel(ylabel, fontsize=18)
    if xtick_locs:
        plt.xticks(xtick_locs, xtick_labels, fontsize=18)
    else:
        plt.xticks(fontsize=18)
    plt.yticks(fontsize=18)
    if xlim:
        plt.xlim(xlim)
    plt.yscale(yscale)
    if legend_outside:
        plt.legend(fontsize=16, fancybox=True, framealpha=0.3, bbox_to_anchor=(1.04,1), loc='upper left')
    else:
        plt.legend(fontsize=16, fancybox=True, framealpha=0.3)

    plt.gca().xaxis.set_major_formatter(tick.FuncFormatter(reformat_large_tick_values))
    sns.despine()

def plot_performance(log, show_key,
                     xkey='inner_problem_steps',
                     inner_problem_len=200,
                     plot_inner_problem_ticks=False,
                     xlim=None,
                     xtick_locs=None, xtick_labels=None,
                     xscale='linear', yscale='linear'):
    plt.figure(figsize=(8,5))

    plt.plot(log[xkey], log[show_key], linewidth=2, marker='o')

    plt.xlabel('Inner Iteration', fontsize=20)
    plt.ylabel(y_label_dict[show_key], fontsize=20)

    if plot_inner_problem_ticks:
        ml = MultipleLocator(inner_problem_len)
        plt.gca().xaxis.set_minor_locator(ml)
        plt.gca().xaxis.set_major_locator(ml)
        plt.gca().xaxis.grid(which='both', color='k', linestyle='-.', linewidth=0.5, alpha=0.5)

    plt.xticks(xtick_locs, xtick_labels, fontsize=18)
    plt.yticks(fontsize=18)
    plt.xlim(xlim)
#     plt.xscale(xscale)
    plt.yscale(yscale)

    plt.gca().xaxis.set_major_formatter(tick.FuncFormatter(reformat_large_tick_values))
    sns.despine()

def plot_es_pes_performance(es_log, pes_log, show_key, inner_problem_len=200, xlim=None,
                            xtick_locs=None, xtick_labels=None, yscale='linear'):
    plt.figure(figsize=(10,5))
    plt.figure(figsize=(8,5))

    # Plot value at the best theta found by the random search
    # plt.axhline(y=value_at_best_theta_dict[show_key], color='k', linestyle='-')

    plt.plot(es_log['inner_problem_steps'], es_log[show_key], label='ES', linewidth=2)
    plt.plot(pes_log['inner_problem_steps'], pes_log[show_key], label='PES', linewidth=2)

    ml = MultipleLocator(inner_problem_len)
    plt.gca().xaxis.set_minor_locator(ml)
    plt.gca().xaxis.set_major_locator(ml)
    plt.gca().xaxis.grid(which='both', color='k', linestyle='-.', linewidth=0.5, alpha=0.5)

    plt.xlabel('Inner Iteration', fontsize=20)
    plt.ylabel(y_label_dict[show_key], fontsize=20)
    plt.xticks(xtick_locs, xtick_labels, fontsize=18)
    plt.yticks(fontsize=18)
    plt.xlim(xlim)
    plt.yscale(yscale)
    plt.legend(fontsize=16, fancybox=True, framealpha=0.3)
    plt.gca().xaxis.set_major_formatter(tick.FuncFormatter(reformat_large_tick_values))
    sns.despine()

def plot_piecewise_schedule_knots_vals(knots, values, T=2500, color=None, alpha=1.0, create_figure=True):
    ts = jax.nn.softmax(jnp.array(knots))
    ts = jnp.cumsum(ts)
    ts = jnp.concatenate([jnp.array([0.0]), ts])  # Explicitly add the 0 mark --> [0, 0.25, 0.5, 0.75, 1]
    ts = ts * T

    if create_figure:
        plt.figure()

    if color:
        plt.plot(ts, values, marker='o', linewidth=2, color=color, alpha=alpha)
    else:
        plt.plot(ts, values, marker='o', linewidth=2, alpha=alpha)

    plt.xticks(fontsize=18)
    plt.yticks(fontsize=18)
    plt.xlabel('Iteration', fontsize=20)
    plt.ylabel('Hparam Schedule', fontsize=20)

def plot_piecewise_schedule(log, T=2500):
    # log = log['result_dict']

    knots = [log[key][-1] for key in log.keys() if 'knot' in key and 'grad' not in key]
    values = [log[key][-1] for key in log.keys() if 'value' in key and 'grad' not in key]

    plot_piecewise_schedule_knots_vals(knots, values, T)

def plot_individual_layer_grid(log_dir, xkey='inner_problem_steps', key_identifier='lr'):
    log = load_log(log_dir, fname='frequent.csv')
    keys = [key for key in log.keys() if key_identifier in key and 'grad' not in key]

    param_name_hparam_dict = defaultdict(list)
    xvalues = log[xkey]

    ymin = 10
    ymax = -10

    for key in keys:
        if '/' in key:
            values = log[key]
            param_name, hparam_name = key.rsplit('/', 1)
            param_name_hparam_dict[param_name].append((hparam_name, values))

            if min(values) < ymin:
                ymin = min(values)
            if max(values) > ymax:
                ymax = max(values)

    nrows = 5
    ncols = 5
    fig, axs = plt.subplots(nrows=nrows, ncols=ncols, figsize=(14, 14))

    i = 0
    for param_name in param_name_hparam_dict:
        ax_row, ax_col = i // nrows, i % ncols
        for (hparam_name, values) in param_name_hparam_dict[param_name]:
            axs[ax_row, ax_col].plot(xvalues, values, label=hparam_name)
        axs[ax_row, ax_col].set_ylim(ymin, ymax)
        axs[ax_row, ax_col].set_title(param_name[6:], fontsize=10)
        axs[ax_row, ax_col].legend(fontsize=14, fancybox=True, framealpha=0.3)
        i += 1

    plt.tight_layout()

def plot_stuff(log_dir,
               xkey='inner_problem_steps',
               key_identifiers=['net'],
               perf_keys=['F_sum', 'full_val_acc'],
               what_to_show=['uncons_hparams', 'cons_hparams', 'performance', 'online'],
               plot_grads=False,
               inner_problem_len=200,
               plot_inner_problem_ticks=False,
               show_legend=True,
               xlim=None,
               xtick_locs=None,
               xtick_labels=None,
               xscale='linear',
               yscale='log',
               optimal_hparams=None,
               optimal_obj=None):
    log = load_log(log_dir, fname='frequent.csv')

    keys = [key for key in log.keys() if any(key_id in key for key_id in key_identifiers) and ('grad' not in key) and ('cons' not in key)]
    cons_keys = [key for key in log.keys() if any(key_id in key for key_id in key_identifiers) and ('grad' not in key) and ('cons' in key)]
    grad_keys = [key for key in log.keys() if any(key_id in key for key_id in key_identifiers) and 'grad' in key]

    if 'uncons_hparams' in what_to_show:
        plot_hparams(log,
                     keys=keys,
                     xkey=xkey,
                     xlim=xlim,
                     xtick_locs=xtick_locs,
                     xtick_labels=xtick_labels,
                     xscale=xscale,
                     inner_problem_len=inner_problem_len,
                     plot_inner_problem_ticks=plot_inner_problem_ticks,
                     show_legend=show_legend)

    if 'cons_hparams' in what_to_show:
        plot_hparams(log,
                     keys=cons_keys,
                     xkey=xkey,
                     xlim=xlim,
                     xtick_locs=xtick_locs,
                     xtick_labels=xtick_labels,
                     xscale=xscale,
                     yscale='log',
                     inner_problem_len=inner_problem_len,
                     plot_inner_problem_ticks=plot_inner_problem_ticks,
                     show_legend=show_legend)

        if optimal_hparams:
            colors = plt.rcParams['axes.prop_cycle'].by_key()['color']
            for i in range(len(optimal_hparams)):
                plt.axhline(y=optimal_hparams[i], color=colors[i % len(colors)], linestyle='--')

    if plot_grads:
        plot_hparams(log, keys=grad_keys,
                     xkey=xkey,
                     xlim=xlim,
                     xtick_locs=xtick_locs,
                     xtick_labels=xtick_labels,
                     xscale=xscale,
                     inner_problem_len=inner_problem_len,
                     plot_inner_problem_ticks=plot_inner_problem_ticks,
                     show_legend=show_legend,
                     yscale='symlog',
                     ylabel='Hyperparameter Gradient')

    # ---------------------------------------------------

    if 'performance' in what_to_show:
        log = load_log(log_dir, fname='iteration.csv')

        for key in perf_keys:
            plot_performance(log, key,
                             xkey=xkey,
                             xlim=xlim,
                             xtick_locs=xtick_locs,
                             xtick_labels=xtick_labels,
                             xscale=xscale,
                             yscale=yscale,
                             inner_problem_len=inner_problem_len,
                             plot_inner_problem_ticks=plot_inner_problem_ticks)

        if optimal_obj:
            plt.axhline(y=optimal_obj, color='k', linestyle='--')

    if 'print_performance' in what_to_show:
        for metric in ['train_sum_loss', 'val_sum_loss', 'train_mean_loss', 'val_mean_loss']:
            if 'acc' in metric:
                best_value = max(log[metric])
                best_idx = np.argmax(log[metric])
            else:
                best_value = min(log[metric])
                best_idx = np.argmin(log[metric])

            print('Best {}: {}'.format(metric, best_value))
            print('Final {}: {}'.format(metric, log[metric][-1]))

            best_params = {curr_key: log[curr_key][best_idx] for curr_key in cons_keys}
            for curr_key in best_params:
                print('\t{}: {:6.3e}'.format(curr_key, best_params[curr_key]))

            print('\n')

    # ---------------------------------------------------

    if 'online' in what_to_show:
        try:
            log = load_log(log_dir, fname='online.csv')
        except:
            return

        for key in ['full_train_loss', 'full_val_loss']:
        # for key in ['full_train_loss']:
            plot_performance(log, key,
                             xkey=xkey,
                             xlim=xlim,
                             xtick_locs=xtick_locs,
                             xtick_labels=xtick_labels,
                             xscale=xscale,
                             yscale=yscale,
                             inner_problem_len=inner_problem_len,
                             plot_inner_problem_ticks=plot_inner_problem_ticks)

y_label_dict = { 'F_sum': 'Sum of Train Losses',
                 'train_sum_loss': 'Sum of Train Losses',
                 'train_mean_loss': 'Mean of Train Losses',
                 'val_sum_loss': 'Sum of Val Losses',
                 'val_mean_loss': 'Mean of Val Losses',
                 'train_sum_acc': 'Sum of Train Accs',
                 'val_sum_acc': 'Sum of Val Accs',
                 'full_train_loss': 'Train Loss',
                 'full_val_loss': 'Val Loss',
                 'full_train_acc': 'Train Accuracy',
                 'full_val_acc': 'Val Accuracy' }

def get_all_random_search_values(rs_dir, key='train_sum_loss', return_thetas=False, constrained=True):
    all_values = []
    all_thetas = []

    for subdir in os.listdir(rs_dir):
        pkl_path = os.path.join(rs_dir, subdir, 'result.pkl')
        with open(pkl_path, 'rb') as f:
            result = pkl.load(f)
        values = result[key].reshape(-1)
        if constrained:
            thetas = result['thetas_constrained']
        else:
            thetas = result['thetas']
        all_values.append(values)
        all_thetas.append(thetas)

    values = np.concatenate(all_values)
    thetas = np.concatenate(all_thetas)

    if return_thetas:
        return values, thetas
    else:
        return values

def sim_rs_runs(values, num_parallel=2, inner_problem_len=2500, max_loss=1e8, seeds=[5,7,11]):
    all_best_value_lists = []

    # for seed in [5, 7, 11, 13, 17, 23]:
    # for seed in [5, 7, 11, 13, 17]:
    for seed in seeds:
        np.random.seed(seed)
        best_value = max_loss
        total_compute = 0
        total_compute_list = []
        best_value_list = []

        for i in range(100000):
            random_indexes = np.random.randint(low=0, high=len(values), size=num_parallel)
            values_in_current_run = values[random_indexes]
            best_value_in_run = np.nanmin(values_in_current_run)
            if best_value_in_run < best_value:
                best_value = best_value_in_run
            total_compute += inner_problem_len * num_parallel
            total_compute_list.append(total_compute)
            best_value_list.append(best_value)

        all_best_value_lists.append(best_value_list)

    best_values_np = np.array(all_best_value_lists)
    best_values_min = np.nanmin(best_values_np, axis=0)
    best_values_max = np.nanmax(best_values_np, axis=0)
    best_values_mean = np.nanmean(best_values_np, axis=0)

    return total_compute_list, best_values_mean, best_values_min, best_values_max

def get_mean_min_max(exp_dir, N):
    running_value_list = []
    total_compute_list = []

    for log_fname in os.listdir(exp_dir):
        log_path = os.path.join(exp_dir, log_fname)
        log = load_log(log_path, fname='iteration.csv')
        total_compute = np.array(log['inner_problem_steps']) * N  # N is the number of particles
        running_values = np.minimum.accumulate(log['F_sum'])
        running_value_list.append(running_values)
        total_compute_list.append(total_compute)

    result_array = np.array(running_value_list)
    result_mean = np.mean(result_array, axis=0)
    result_min = np.min(result_array, axis=0)
    result_max = np.max(result_array, axis=0)

    return total_compute_list[0], result_mean, result_min, result_max

def get_mean_min_max_bo(log_paths, inner_problem_len, min_or_max='min'):
    running_value_list = []
    total_compute_list = []

    shortest_list_len = 1e9

    for log_path in log_paths:
        # with tf.io.gfile.GFile(os.path.join(log_path, 'logs.json'), 'r') as f:
        with open(os.path.join(log_path, 'logs.json'), 'r') as f:
            text = f.read()

        all_values = []
        total_compute = []

        for (i,line) in enumerate(text.splitlines()):
            result = json.loads(line)
            all_values.append(-result['target'])
            total_compute.append(i * inner_problem_len)

        if min_or_max == 'min':
            running_values = np.minimum.accumulate(all_values)
        elif min_or_max == 'max':
            running_values = np.maximum.accumulate(all_values)

        running_value_list.append(running_values)
        total_compute_list.append(total_compute)
        if len(total_compute) < shortest_list_len:
            shortest_list_len = len(total_compute)

    running_value_list = [lst[:shortest_list_len] for lst in running_value_list]
    result_array = np.array(running_value_list)
    result_mean = np.mean(result_array, axis=0)
    result_min = np.min(result_array, axis=0)
    result_max = np.max(result_array, axis=0)

    return total_compute_list[0][:shortest_list_len], result_mean, result_min, result_max

max_stars_repo_name: dshirron/google-research


text:
# Copyright (c) Facebook, Inc. and its affiliates.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
#
import models
import torch
import torch.nn.functional as F
import numpy as np
import math, numbers

def get_in_map(locs):
  assert locs.dim() == 4, 'locs : {:}'.format(locs.shape)
  return torch.sum((locs > -1) + (locs < 1), dim=-1) == 4

def calculate_temporal_loss(criterion, locs, past2now, future2now, FBcheck, mask, config):
  # return the calculate target from the first frame to the whole sequence.
  batch, frames, num_pts, _ = locs.size()
  assert batch == past2now.size(0) == future2now.size(0) == FBcheck.size(0), '{:} vs {:} vs {:} vs {:}'.format(locs.size(), past2now.size(), future2now.size(), FBcheck.size())
  assert num_pts == past2now.size(2) == future2now.size(2) == FBcheck.size(1), '{:} vs {:} vs {:} vs {:}'.format(locs.size(), past2now.size(), future2now.size(), FBcheck.size())
  assert frames-1 == past2now.size(1) == future2now.size(1), '{:} vs {:} vs {:} vs {:}'.format(locs.size(), past2now.size(), future2now.size(), FBcheck.size())
  assert mask.dim() == 4 and mask.size(0) == batch and mask.size(1) == num_pts, 'mask : {:}'.format(mask.size())

  locs, past2now, future2now = locs.contiguous(), past2now.contiguous(), future2now.contiguous()
  FBcheck, mask = FBcheck.contiguous(), mask.view(batch, num_pts).contiguous()
  if config.sbr_loss_type == 'L1':
    past_loss, future_loss = criterion.loss_l1_func(locs[:,1:], past2now, reduction='none'), criterion.func(locs[:,:-1], future2now, reduction='none')
  elif config.sbr_loss_type == 'MSE':
    past_loss, future_loss = criterion.loss_mse_func(locs[:,1:], past2now, reduction='none'), criterion.func(locs[:,:-1], future2now, reduction='none')
  else:
    raise ValueError('invalid sbr loss type : {:}'.format(config.sbr_loss_type))

  temporal_loss = past_loss + future_loss

  # check oks
  with torch.no_grad():
    inmap_ok = get_in_map( locs ).sum(1) == frames
    check_ok = torch.sqrt(FBcheck[:,:,0]**2 + FBcheck[:,:,1]**2) < config.fb_thresh
    distc_ok = (past_loss.sum(-1) + future_loss.sum(-1))/4 < config.dis_thresh
    distc_ok = distc_ok.sum(1) == frames-1
    data_ok  = (inmap_ok.view(batch, 1, num_pts, 1) + check_ok.view(batch, 1, num_pts, 1) + distc_ok.view(batch, 1, num_pts, 1) + mask.view(batch, 1, num_pts, 1)) == 4

  nums     = torch.sum(data_ok).item()
  if nums == 0: return 0, nums
  else:
    selected_loss = torch.masked_select(temporal_loss, data_ok)
    return torch.mean(selected_loss), nums


text:
import torch
import numpy as np
from torch import Tensor
from torch.utils.data import Dataset, DataLoader
from torchvision import io
from pathlib import Path
from typing import Tuple

class Wound(Dataset):
    """
    num_classes: 18
    """
    # explain the purpose of the model
    # where is it, how big it is,
    # give examples of what each of segments are
    # people who are familiar: segmentation
    # medical background: application site, trying to identify different areas in a an image
    # in the wound we are looking for different types of tissues
    # get the story
    CLASSES = ['Boundary','PeriWoundPerimeter','WoundPerimeter','Epithellialization','Granulation','Hypergranulation','NecroticSlough','Eschar','OtherWound','DamagedToeNail','HealthyToeNail','Oedematous','Erythematous','OtherSkinUnbroken','Maceration','Excoriation','OtherSkinBroken','HealthySkin']

    PALETTE = torch.tensor([[192, 192, 192],[0, 183, 235],[0, 255, 255],[255, 255, 0],[212, 175, 55],[127, 255, 212],[138, 43, 226],[204, 255, 0],[220, 208, 255],[0, 250, 154],[255, 69, 0],[255, 165, 0],[30, 144, 255],[221, 160, 221],[0, 255, 0],[0, 128, 128],[252, 15, 192],[220, 20, 60]])

    ID2TRAINID = {0: 255, 1: 255, 2: 255, 3: 255, 4: 255, 5: 255, 6: 255, 7: 0, 8: 1, 9: 255, 10: 255, 11: 2, 12: 3, 13: 4, 14: 255, 15: 255, 16: 255,
                  17: 5, 18: 255, 19: 6, 20: 7, 21: 8, 22: 9, 23: 10, 24: 11, 25: 12, 26: 13, 27: 14, 28: 15, 29: 255, 30: 255, 31: 16, 32: 17, 33: 18, -1: -1}

    def __init__(self, root: str, split: str = 'train', transform = None) -> None:
        super().__init__()
        assert split in ['train', 'val', 'test']
        self.transform = transform
        self.n_classes = len(self.CLASSES)
        self.ignore_label = 255

        self.label_map = np.arange(256)
        for id, trainid in self.ID2TRAINID.items():
            self.label_map[id] = trainid

        img_path = Path(root) / 'leftImg8bit' / split
        self.files = list(img_path.rglob('*.png'))

        if not self.files:
            raise Exception(f"No images found in {img_path}")

        print(f"Found {len(self.files)} {split} images.")

    def __len__(self) -> int:
        return len(self.files)

    def __getitem__(self, index: int) -> Tuple[Tensor, Tensor]:
        img_path = str(self.files[index])
        lbl_path = str(self.files[index]).replace('leftImg8bit', 'gtFine').replace('.png', '_labelIds.png')

        image = io.read_image(img_path)
        label = io.read_image(lbl_path)

        if self.transform:
            image, label = self.transform(image, label)
        return image, self.encode(label.squeeze().numpy()).long()

    def encode(self, label: Tensor) -> Tensor:
        label = self.label_map[label]
        return torch.from_numpy(label)
        # for id, trainid in self.ID2TRAINID.items():
        #     label[label == id] = trainid
        # return label

    def decode(self, label: Tensor) -> Tensor:
        return self.PALETTE[label.to(int)]

if __name__ == '__main__':
    import matplotlib.pyplot as plt
    from torchvision import transforms as T
    from torchvision.utils import make_grid
    from transforms import Compose, RandomResizedCrop, Normalize

    root = 'C:\\Users\\sithu\\Documents\\Datasets\\CityScapes'
    transform = Compose([RandomResizedCrop((1024, 1024)), Normalize()])

    dataset = CityScapes(root, split="train", transform=transform)
    dataloader = DataLoader(dataset, shuffle=True, batch_size=4)
    image, label = next(iter(dataloader))
    print('=========================')
    print(image.shape, label.shape)
    print(label.unique())
    label[label==255] = 0
    labels = [dataset.decode(lbl).permute(2, 0, 1) for lbl in label]
    labels = torch.stack(labels)

    inv_normalize = T.Normalize(
        mean=(-0.485/0.229, -0.456/0.224, -0.406/0.225),
        std=(1/0.229, 1/0.224, 1/0.225)
    )
    image = inv_normalize(image)
    image *= 255
    images = torch.vstack([image, labels])

    plt.imshow(make_grid(images, nrow=4).to(torch.uint8).numpy().transpose((1, 2, 0)))
    plt.show()

max_stars_repo_name: Genevievekim/segformer

max_issues_repo_name: Genevievekim/segformer

max_forks_repo_name: Genevievekim/segformer