text:
from sklearn.svm import SVC
from sklearn.model_selection import KFold
import numpy as np
import pandas as pd
from sklearn.metrics import f1_score
from sklearn.linear_model import LogisticRegression
import argparse
import xgboost as xgb

parser = argparse.ArgumentParser()
parser.add_argument('-t', '--task_num', type=int,default=0) # task_list=['MaterialType-2','Sex-2','DiseaseState-16','BioSourceType-7']
parser.add_argument('-rf', '--raw_flag', type=bool, default=False) # `True` use raw data, `False` use pca data
parser.add_argument('-dm', '--dimension', type=int,default=22283)

#  {'newton-cg', 'lbfgs', 'sag', 'saga'},

# save args
args = parser.parse_args()

# configuration
task_list=['MaterialType-2','Sex-2','DiseaseState-16','BioSourceType-7']
num_class_list=[2,2,16,7]

param = {
         # 'reg_alpha': 0.0001,
         # 'colsample_bytree': 0.8,
         # 'scale_pos_weight': 1,
         'learning_rate': 0.07,
         # 'min_child_weight': 11,
         'subsample': 0.8,
         # 'reg_lambda': 0.0049,
         'seed': 1,
         'objective': 'multi:softmax',
         'max_depth': 1,
         'gamma': 0.0,
         'silent': 1,
         'num_class': num_class_list[args.task_num]
         }

task_name=task_list[args.task_num]
raw_flag=False
print 'raw_flag',raw_flag
dimension=args.dimension

# load data
if raw_flag:
    X = np.load("./../data/all_raw_data.npy")
    X = X[:,0:dimension]

else:
    X = np.load("./../data/all_PCA_"+str(dimension)+".npy")

# todo another way to decrease dimension
# transformer = random_projection.SparseRandomProjection()
# X = transformer.fit_transform(X)

# load label
label_df = pd.read_csv('./../data/all_label.csv')
y = np.array(label_df[task_name].tolist())
y_mask=np.zeros([y.shape[0],])
y_mask=(y!=y_mask)

# mask data
y=y[y_mask]

y=y-1
print y

X = X[y_mask]

print 'data shape:',X.shape

# split data random_state=1
kf = KFold(n_splits=5,random_state=1,shuffle=True)
kf.get_n_splits(X)
print(kf)

# store f1
macro_f1=[]
micro_f1=[]

for train_index, test_index in kf.split(X):
    # print("TRAIN:", train_index, "TEST:", test_index)

    y_test=y[test_index]

    dtrain = xgb.DMatrix(data=X[train_index], label=y[train_index])
    dtest = xgb.DMatrix(data=X[test_index], label=y[test_index])

    # evallist = [(dtrain, 'train'), (dtest, 'eval')]
    bst = xgb.train(param, dtrain, 100,
                    # evallist
                    )

    y_pred = bst.predict(dtest)

    macro_f1.append(f1_score(y_test, y_pred, average='macro'))
    micro_f1.append(f1_score(y_test, y_pred, average='micro'))
    print 'macro f1', f1_score(y_test, y_pred, average='macro')
    print 'micro f1', f1_score(y_test, y_pred, average='micro')

print 'avg macro f1', sum(macro_f1)/len(macro_f1)
print 'avg micro f1', sum(micro_f1)/len(micro_f1)

with open('./../log/xgb.csv', 'a') as fout:
    fout.write(task_name+',')
    if raw_flag:
        fout.write('use raw data,')
    else:
        fout.write('use PCA data,')
    for key in param:
        fout.write(key+':'+str(param[key])+',')
    fout.write(str(dimension) + ',')
    fout.write(str(sum(macro_f1) / len(macro_f1)) + ',')
    fout.write(str(sum(micro_f1) / len(micro_f1)) + ',\n')


text:
#!/usr/bin/env python3
'''
Copyright (c) Microsoft Corporation. All rights reserved.
Licensed under the MIT License.
'''

""" binary_classification.py: build binary classification models
"""

import glob
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.optim import lr_scheduler
from torch.utils.data import Dataset, DataLoader, SubsetRandomSampler

import torchvision
from torchvision import utils
import torchvision.transforms as transforms
from torchvision import datasets, models

from copy import deepcopy
import numpy as np
import pandas as pd
import seaborn as sns
from PIL import Image
import matplotlib.pyplot as plt
from matplotlib import image
from pylab import *

from sklearn.metrics import classification_report, confusion_matrix

class BaseDataset(Dataset):
    def __init__(self, df, split, transform = None):
        super().__init__()
        self.split = df.loc[df['split'] == split].reset_index(drop=True)
        self.transform = transform

    def __len__(self):
        return len(self.split)

    def __getitem__(self,index):
        filename = self.split.loc[index, 'filename']
        image = Image.open(filename).convert('RGB')
        label = self.split.loc[index, 'label']

        if self.transform is not None:
            image = self.transform(image)
        return filename, image, label

def load_dataset(data, batch_size):
    mean = [0.485, 0.456, 0.406]
    std = [0.229, 0.224, 0.225]

    data_transforms = {
        'train': transforms.Compose([transforms.RandomGrayscale(p=0.1),
                                      transforms.RandomRotation(10),
                                      #transforms.RandomResizedCrop((224, 224), scale=(0.05, 0.95), ratio=(0.9, 1.1)),
                                      #transforms.ColorJitter(brightness=0.2, contrast=0.2, saturation=0.2, hue=0.2),
                                      #transforms.GaussianBlur(kernel_size=(5, 9), sigma=(0.1, 2.0)),
                                      transforms.Resize((224, 224)),
                                      transforms.ToTensor(),
                                      transforms.Normalize(mean, std),
                                      ]),
        'validation': transforms.Compose([transforms.Resize((224, 224)),
                                    transforms.ToTensor(),
                                    transforms.Normalize(mean, std),
                                   ]),
        'test': transforms.Compose([transforms.Resize((224, 224)),
                                     transforms.ToTensor(),
                                     transforms.Normalize(mean, std),
                                    ])
    }

    train_data = BaseDataset(data, 'train', data_transforms['train'])
    val_data = BaseDataset(data, 'val', data_transforms['validation'])
    test_data = BaseDataset(data, 'test', data_transforms['test'])

    train_loader = DataLoader(dataset=train_data, batch_size=batch_size, shuffle=True, drop_last=False)
    val_loader = DataLoader(dataset=val_data, batch_size=batch_size, shuffle=True, drop_last=False)
    test_loader = DataLoader(dataset=test_data, batch_size=batch_size, shuffle=False, drop_last=False)

    classes = sorted(data.label.unique().tolist())
    num_classes = len(classes)

    return train_data, val_data, test_data, train_loader, val_loader, test_loader, classes, num_classes

class Customized_CNN(nn.Module):
    def __init__(self, num_classes):
        super(Customized_CNN, self).__init__()
        self.block1 = self.conv_block(c_in=3, c_out=256, dropout=0.1, kernel_size=5, stride=1, padding=2)
        self.block2 = self.conv_block(c_in=256, c_out=128, dropout=0.1, kernel_size=3, stride=1, padding=1)
        self.block3 = self.conv_block(c_in=128, c_out=64, dropout=0.1, kernel_size=3, stride=1, padding=1)
        self.maxpool = nn.MaxPool2d(kernel_size=2, stride=2)
        self.lastcnn = nn.Conv2d(in_channels=64, out_channels=num_classes, kernel_size=56, stride=1, padding=0)

    def forward(self, x):
        x = self.block1(x)
        x = self.maxpool(x)
        x = self.block2(x)
        x = self.block3(x)
        x = self.maxpool(x)
        x = self.lastcnn(x)
        return x

    def conv_block(self, c_in, c_out, dropout,  **kwargs):
        seq_block = nn.Sequential(
            nn.Conv2d(in_channels=c_in, out_channels=c_out, **kwargs),
            nn.BatchNorm2d(num_features=c_out),
            nn.ReLU(),
            nn.Dropout2d(p=dropout)
        )
        return seq_block

class Multiclass_Classification_Models():

    def train_model(data, model_name, model_version, model_dir, batch_size = 32, pretrained = True, optimizer = 'Adam', learning_rate = 0.0001, lr_decay = True, num_epochs = 25):

        train_data, val_data, test_data, train_loader, val_loader, test_loader, classes, num_classes = load_dataset(data, batch_size = batch_size)

        print('Please choose one model from the available models: Customized_CNN, Resnet18, Resnet34, Resnet50, Resnet101, Resnet152, Alexnet, VGG11, VGG13, VGG16, VGG19, Densenet121, Densenet169, Densenet201, Squeezenet1_0.')

        dic_label_to_index = dict()
        dic_index_to_label = dict()
        for index, label in enumerate(classes):
            dic_label_to_index[label] = index
            dic_index_to_label[index] = label

        if model_name == 'Customized_CNN':
            model = Customized_CNN(num_classes)
        elif model_name == 'Resnet18':
            model = models.resnet18(pretrained = pretrained)
            model.classifier = nn.Linear(1024, num_classes)
        elif model_name == 'Resnet34':
            model = models.resnet34(pretrained = pretrained)
            model.classifier = nn.Linear(1024, num_classes)
        elif model_name == 'Resnet50':
            model = models.resnet50(pretrained = pretrained)
            model.classifier = nn.Linear(1024, num_classes)
        elif model_name == 'Resnet101':
            model = models.resnet101(pretrained = pretrained)
            model.classifier = nn.Linear(1024, num_classes)
        elif model_name == 'Resnet152':
            model = models.resnet152(pretrained = pretrained)
            model.classifier = nn.Linear(1024, num_classes)
        elif model_name == 'Alexnet':
            model = models.alexnet(pretrained = pretrained)
            model.classifier[6] = nn.Linear(4096, num_classes)
        elif model_name == 'VGG11':
            model = models.vgg11(pretrained = pretrained)
            model.classifier[6] = nn.Linear(4096, num_classes)
        elif model_name == 'VGG13':
            model = models.vgg13(pretrained = pretrained)
            model.classifier[6] = nn.Linear(4096, num_classes)
        elif model_name == 'VGG16':
            model = models.vgg16(pretrained = pretrained)
            model.classifier[6] = nn.Linear(4096, num_classes)
        elif model_name == 'VGG19':
            model = models.vgg19(pretrained = pretrained)
            model.classifier[6] = nn.Linear(4096, num_classes)
        elif model_name == 'Densenet121':
            model = models.densenet121(pretrained = pretrained)
            model.classifier = nn.Linear(1024, num_classes)
        elif model_name == 'Densenet169':
            model = models.densenet121(pretrained = pretrained)
            model.classifier = nn.Linear(1024, num_classes)
        elif model_name == 'Densenet201':
            model = models.densenet121(pretrained = pretrained)
            model.classifier = nn.Linear(1024, num_classes)
        elif model_name == 'Squeezenet1_0':
            model = models.squeezenet1_0(pretrained = pretrained)
            model.classifier[1] = nn.Conv2d(512, num_classes, kernel_size=(1,1), stride=(1,1))

        device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
        model = model.to(device)
        criterion = nn.CrossEntropyLoss()

        if optimizer == 'SGD':
            optimizer_ft = optim.SGD(model.parameters(), lr=learning_rate, momentum=0.9)
        elif optimizer == 'Adam':
            optimizer_ft = optim.Adam(model.parameters(), lr=learning_rate)

        exp_lr_scheduler = lr_scheduler.StepLR(optimizer_ft, step_size=10, gamma=0.1)

        best_model_wts = deepcopy(model.state_dict())
        val_best_acc = 0.0

        accuracy_stats = {
            'train': [],
            'val': []
        }
        loss_stats = {
            'train': [],
            'val': []
        }

        print("Begin training.")
        for epoch in range(1, num_epochs+1):
            ###### Model training
            model.train()  # Set model to training mode
            running_loss = 0.0
            running_corrects = 0
            for filename, inputs, labels in train_loader:
                inputs = inputs.to(device)
                labels = torch.as_tensor([dic_label_to_index[label] for label in labels])  ## from original labels to label-index
                labels = labels.to(device)
                optimizer_ft.zero_grad()

                if model_name != 'Customized_CNN':
                    outputs = model(inputs)
                elif model_name == 'Customized_CNN':
                    outputs = model(inputs).squeeze()

                _, preds = torch.max(outputs, 1)
                loss = criterion(outputs, labels)
                loss.backward()
                optimizer_ft.step()
                running_loss += loss.item() * inputs.size(0)
                running_corrects += torch.sum(preds == labels.data).cpu().numpy()
                if lr_decay == True:
                        exp_lr_scheduler.step()
            train_epoch_loss = running_loss / len(train_data)
            train_epoch_acc = running_corrects / len(train_data)

            ###### Model evaluation
            model.eval()   # Set model to evaluate mode
            running_loss = 0.0
            running_corrects = 0
            for filename, inputs, labels in val_loader:
                inputs = inputs.to(device)
                labels = torch.as_tensor([dic_label_to_index[label] for label in labels])  ## from original labels to label-index
                labels = labels.to(device)
                optimizer_ft.zero_grad()

                if model_name != 'Customized_CNN':
                    outputs = model(inputs)
                elif model_name == 'Customized_CNN':
                    outputs = model(inputs).squeeze()

                _, preds = torch.max(outputs, 1)
                loss = criterion(outputs, labels)
                running_loss += loss.item() * inputs.size(0)
                running_corrects += torch.sum(preds == labels.data).cpu().numpy()
            val_epoch_loss = running_loss / len(val_data)
            val_epoch_acc = running_corrects / len(val_data)
            if val_epoch_acc > val_best_acc:
                val_best_acc = val_epoch_acc
                best_model_wts = deepcopy(model.state_dict())

            ###### results summary for each epoch
            loss_stats['train'].append(train_epoch_loss)
            loss_stats['val'].append(val_epoch_loss)
            accuracy_stats['train'].append(train_epoch_acc)
            accuracy_stats['val'].append(val_epoch_acc)
            print(f'Epoch {epoch+0:02}: | Train Loss: {train_epoch_loss:.5f} | Val Loss: {val_epoch_loss:.5f} | Train Acc: {train_epoch_acc:.3f}| Val Acc: {val_epoch_acc:.3f}')

        print('Best validation accuracy: {:4f}'.format(val_best_acc))
        # load best model weights
        model.load_state_dict(best_model_wts)

        train_val_acc_df = pd.DataFrame.from_dict(accuracy_stats).reset_index().melt(id_vars=['index']).rename(columns={"index":"epochs"})
        train_val_loss_df = pd.DataFrame.from_dict(loss_stats).reset_index().melt(id_vars=['index']).rename(columns={"index":"epochs"})
        fig, axes = plt.subplots(nrows=1, ncols=2, figsize=(15,5))
        sns.lineplot(data=train_val_acc_df, x = "epochs", y="value", hue="variable",  ax=axes[0]).set_title('Train-Val Accuracy/Epoch')
        sns.lineplot(data=train_val_loss_df, x = "epochs", y="value", hue="variable", ax=axes[1]).set_title('Train-Val Loss/Epoch')
        plt.show()

        ###### Model scoring on testset
        filenames = []
        y_pred = []
        y_true = []
        for filename, inputs, labels in test_loader:
            filenames.append(filename)
            inputs = inputs.to(device)
            labels = torch.as_tensor([dic_label_to_index[label] for label in labels])  ## from original labels to label-index
            labels = labels.to(device)
            if model_name != 'Customized_CNN':
                outputs = model(inputs)
            elif model_name == 'Customized_CNN':
                outputs = model(inputs).squeeze()
            _, preds = torch.max(outputs, 1)
            y_pred += preds.cpu().tolist()
            y_true  += labels.cpu().tolist()

        print('Scoring results on testset:')
        print(classification_report(y_true, y_pred))
        print('Confusion matrix on testset:')
        cm = confusion_matrix([dic_index_to_label[x] for x in y_true], [dic_index_to_label[x] for x in y_pred], classes)
        print(pd.DataFrame(cm, index=['true: {:}'.format(x) for x in classes], columns=['pred: {:}'.format(x) for x in classes]))

        ax= plt.subplot()
        sns.heatmap(cm, annot=True, fmt='g', ax=ax, cmap='Blues')
        ax.set_xlabel('Predicted labels');
        ax.set_ylabel('True labels');
        ax.set_title('Confusion Matrix');
        ax.xaxis.set_ticklabels(classes);
        ax.yaxis.set_ticklabels(classes);

        ###### plot examples of correct & incorrect classifications for each category
        from collections import defaultdict
        label_true_pred_mapping = defaultdict(list)
        for i in range(len(y_true)):
            label_true_pred_mapping[y_true[i]].append((i, y_pred[i]))

        def DrawPics(category, sample_size):

            category_correct_classification = [i for (i, y_pred) in label_true_pred_mapping[category] if y_pred == category][:sample_size]
            category_incorrect_classification = [i for (i, y_pred) in label_true_pred_mapping[category] if y_pred != category][:sample_size]
            fig=plt.figure(figsize=(20, 5))
            for i in range(len(category_correct_classification)):
                index = category_correct_classification[i]
                subplot = fig.add_subplot(1, sample_size, i+1)
                axis("off")
                img = image.imread(test_data[index][0])
                plt.imshow(img)
                subplot.title.set_text('correct classification: ' + dic_index_to_label[category])
            fig=plt.figure(figsize=(20, 5))
            for i in range(len(category_incorrect_classification)):
                index = category_incorrect_classification[i]
                subplot = fig.add_subplot(1, sample_size, i+1)
                axis("off")
                img = image.imread(test_data[index][0])
                plt.imshow(img)
                subplot.title.set_text('incorrect classification: ' + dic_index_to_label[category])

        for category in list(dic_index_to_label):
            DrawPics(category, sample_size = 5)

        #TODO: Check for name availability

        if model_version:
            torch.save(model.state_dict(), model_dir + model_version + '-multiclass_classification_model-' + model_name + '.pth')
        else:
            torch.save(model.state_dict(), model_dir + 'multiclass_classification_model-' + model_name + '.pth')

        return model

############################################################################
######################### score for new dataset ############################
############################################################################
class Dataset_to_Score(Dataset):
    def __init__(self, df, transform = None):
        super().__init__()
        self.df = df.reset_index(drop=True)
        self.transform = transform

    def __len__(self):
        return len(self.df)

    def __getitem__(self,index):
        filename = self.df.loc[index, 'filename']
        image = Image.open(filename).convert('RGB')

        if self.transform is not None:
            image = self.transform(image)
        return filename, image

def load_dataset_to_score(data, batch_size):
    mean = [0.485, 0.456, 0.406]
    std = [0.229, 0.224, 0.225]

    data_transforms = {
        'to_score': transforms.Compose([transforms.Resize((224, 224)),
                                        transforms.ToTensor(),
                                        transforms.Normalize(mean, std),
                                        ])
    }

    data_to_score = Dataset_to_Score(data, data_transforms['to_score'])
    data_to_score_loader = DataLoader(dataset=data_to_score, batch_size=batch_size, shuffle=False, drop_last=False)

    return data_to_score, data_to_score_loader

class Multiclass_Classification_Scoring():

    def score_new_dataset(categories, spectrogram_dir_to_score, model_dir, saved_model):

        categories = categories + ['-9999']
        dic_label_category = {i:v for i,v in enumerate(sorted(categories))}
        num_classes = len(dic_label_category)

        spectrogram_filenames_to_score = glob.glob(spectrogram_dir_to_score + '*')
        spectrogram_filenames_to_score = pd.DataFrame(spectrogram_filenames_to_score, columns =['filename'])

        data_to_score, data_to_score_loader = load_dataset_to_score(spectrogram_filenames_to_score, batch_size = 32)

        output = pd.DataFrame(columns = ['Begin Time (s)', 'End Time (s)', 'Low Freq (Hz)', 'High Freq (Hz)', 'Begin File', 'Spectrogram_filename', 'Predicted_Category', 'Predicted_Probability'])

        device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
        model_name = saved_model.split('-')[-1].split('.')[0]

        if model_name == 'Customized_CNN':
            model = Customized_CNN(num_classes)
        elif model_name == 'Resnet18':
            model = models.resnet18()
            model.classifier = nn.Linear(1024, num_classes)
        elif model_name == 'Resnet34':
            model = models.resnet34()
            model.classifier = nn.Linear(1024, num_classes)
        elif model_name == 'Resnet50':
            model = models.resnet50()
            model.classifier = nn.Linear(1024, num_classes)
        elif model_name == 'Resnet101':
            model = models.resnet101()
            model.classifier = nn.Linear(1024, num_classes)
        elif model_name == 'Resnet152':
            model = models.resnet152()
            model.classifier = nn.Linear(1024, num_classes)
        elif model_name == 'Alexnet':
            model = models.alexnet()
            model.classifier[6] = nn.Linear(4096, num_classes)
        elif model_name == 'VGG11':
            model = models.vgg11()
            model.classifier[6] = nn.Linear(4096, num_classes)
        elif model_name == 'VGG13':
            model = models.vgg13()
            model.classifier[6] = nn.Linear(4096, num_classes)
        elif model_name == 'VGG16':
            model = models.vgg16()
            model.classifier[6] = nn.Linear(4096, num_classes)
        elif model_name == 'VGG19':
            model = models.vgg19()
            model.classifier[6] = nn.Linear(4096, num_classes)
        elif model_name == 'Densenet121':
            model = models.densenet121()
            model.classifier = nn.Linear(1024, num_classes)
        elif model_name == 'Densenet169':
            model = models.densenet121()
            model.classifier = nn.Linear(1024, num_classes)
        elif model_name == 'Densenet201':
            model = models.densenet121()
            model.classifier = nn.Linear(1024, num_classes)
        elif model_name == 'Squeezenet1_0':
            model = models.squeezenet1_0()
            model.classifier[1] = nn.Conv2d(512, num_classes, kernel_size=(1,1), stride=(1,1))

        model.load_state_dict(torch.load(model_dir + saved_model, map_location=device))
        model.to(device)

        filenames = []
        y_pred = []
        pred_probs = []

        for filename, inputs in data_to_score_loader:
            filenames += filename
            inputs = inputs.to(device)
            if model_name != 'Customized_CNN':
                outputs = model(inputs)
            elif model_name == 'Customized_CNN':
                outputs = model(inputs).squeeze()
            _, preds = torch.max(outputs, 1)
            pred_prob = F.softmax(outputs, dim=1).cpu().tolist()
            y_pred += [dic_label_category[pred] for pred in preds.cpu().tolist()]
            pred_probs += [max(prob) for prob in pred_prob]

        audio_filenames = [filename.split('/')[-1].split('-')[0] for filename in filenames]
        begin_times = [filename.split('-')[1] for filename in filenames]
        end_times = [filename.split('-')[2] for filename in filenames]
        output['Begin Time (s)'] = begin_times
        output['End Time (s)'] = end_times
        output['Begin File'] = audio_filenames
        output['Spectrogram_filename'] = filenames
        output['Predicted_Category'] = y_pred
        output['Predicted_Probability'] = pred_probs

        return output

ext: py

max_stars_repo_name: microsoft/Akoustos

max_issues_repo_name: microsoft/Akoustos

max_forks_repo_name: microsoft/Akoustos

reason: import numpy


text:
import numpy as np
import random
import bisect
import environment
import pickle
from collections import deque
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import Adam
from keras.regularizers import l2
from keras import backend as K
from keras.models import load_model
import tensorflow as tf

import time

def sample_from_distribution(distribution):
    total = np.sum(distribution)
    cdf = []
    cumsum = 0
    for w in distribution:
        cumsum += w
        result.append(cumsum / total)
    x = random.random()
    idx = bisect.bisect(cdf, x)
    return idx

def epsilon_greedy_selection(q, actions, epsilon=0.1):
    if np.random.uniform(0, 1) < epsilon:
        # exploration
        return np.random.choice(actions)
    else:
        # exploitation
        arg = np.argsort(q[actions])[::-1]
        n_tied = sum(np.isclose(q[actions], q[actions][arg[0]]))
        return actions[np.random.choice(arg[0:n_tied])]

class Dumby():
    def __init__(self, env, epsilon=0.3, gamma=0.75, algorithm='dqn', schedule={}):
        self.state_size = env.n_states
        self.action_size = env.n_actions
        self.batch_size = 32
        self.gamma = gamma    # discount rate
        self.epsilon = epsilon  # exploration rate
        self.epsilon_min = 0.01
        self.epsilon_decay = 0.995
        self.learning_rate = 0.001
        self.algorithm = algorithm
        self.schedule = schedule
        self.in_between_training_steps = self.batch_size
        if self.algorithm=='dqn':
            self.memory = deque(maxlen=2000)
            self.target_model = self._build_model()
        elif self.algorithm =='sarsa':
            self.alpha = 0.1
            self.q = np.zeros((self.state_size, self.action_size))
            self.q.fill(float('-inf'))
            for s in range(self.state_size):
                actions = env.actions(s)
                for a in actions:
                    self.q[s, a] = 0

    def _huber_loss(self, y_true, y_pred, clip_delta=1.0):
        error = y_true - y_pred
        cond  = K.abs(error) <= clip_delta

        squared_loss = 0.5 * K.square(error)
        quadratic_loss = 0.5 * K.square(clip_delta) + clip_delta * (K.abs(error) - clip_delta)

        return K.mean(tf.where(cond, squared_loss, quadratic_loss))

    def _build_model(self):
        l2_reg = 0.00001
        model = Sequential()
        # model.add(Dense(10, input_dim=self.state_size, activation='relu', kernel_regularizer=l2(l2_reg), bias_regularizer=l2(l2_reg)))
        # model.add(Dropout(0.1))
        # model.add(Dense(16, input_dim=self.state_size, activation='relu', kernel_regularizer=l2(l2_reg), bias_regularizer=l2(l2_reg)))
        # model.add(Dropout(0.1))
        model.add(Dense(24, activation='relu', input_dim=self.state_size)) #, kernel_regularizer=l2(l2_reg), bias_regularizer=l2(l2_reg), activation_regularizer=l2(l2_reg)))
        model.add(Dropout(0.01))
        model.add(Dense(24, activation='relu')) #, kernel_regularizer=l2(l2_reg), bias_regularizer=l2(l2_reg), activation_regularizer=l2(l2_reg)))
        model.add(Dropout(0.01))
        # model.add(Dropout(0.1))
        # model.add(Dense(30, activation='relu', kernel_regularizer=l2(l2_reg), bias_regularizer=l2(l2_reg)))
        # model.add(Dropout(0.3))
        model.add(Dense(self.action_size, activation='linear'))
        model.compile(loss='mse',
                      optimizer=Adam(lr=self.learning_rate))
        # model.compile(loss=self._huber_loss,
        #               optimizer=Adam(lr=self.learning_rate))
        return model

    def remember(self, state, action, reward, next_state, done):
        self.memory.append((state, action, reward, next_state, done))
        if len(self.memory) >= self.batch_size and self.in_between_training_steps >= self.batch_size:
            # print(' replay')
            print('[!] Fitting model with replay')
            loss = self.replay()
            self.in_between_training_steps = 0

        self.in_between_training_steps += 1

    # def forget(self):
    #     del self.memory
    #     self.memory = deque(maxlen=2000)

    def update_target_model(self):
        # copy weights from model to target_model
        self.target_model.set_weights(self.model.get_weights())

    def act(self, state, actions):
        if np.random.rand() <= self.epsilon:
            return np.random.choice(actions)
            # return random.randrange(self.action_size)
        if self.algorithm=='dqn':
            act_values = self.target_model.predict(state)
            # if np.argmax(act_values[0]) not in actions:
            #     act_ = np.random.choice(actions)
            #     print('random action', act_)
            #     return act_
            # else:
            #     # print(['{:.3f}'.format(si) for si in state[0,:]], ['{:.3f}'.format(si) for si in act_values[0,:]])
            #     print('predicted action', np.argmax(act_values[0]))
            return np.argmax(act_values[0])  # returns action

        elif self.algorithm == 'sarsa':

            q_ = self.q[state]
            arg = np.argsort(q_[actions])[::-1]
            n_tied = sum(np.isclose(q_[actions], q_[actions][arg[0]]))

            return actions[np.random.choice(arg[0:n_tied])]

    def replay(self):
        # minibatch = random.sample(self.memory, batch_size)
        # for state, action, reward, next_state, done in minibatch:
        #     target = reward
        #     if not done:
        #         target = (reward + self.gamma * np.amax(self.target_model.predict(next_state)[0]))
        #     target_f = self.target_model.predict(state)
        #     target_f[0][action] = target
        #     self.target_model.fit(state, target_f, epochs=1, verbose=0)
        # if self.epsilon > self.epsilon_min:
        #     self.epsilon *= self.epsilon_decay
        #minibatch = random.sample(self.memory, batch_size)
        # minibatch = self.memory
        losses = []
        #print(len(self.memory), len(self.memory[0]))
        # minibatch = self.memory #random.sample(self.memory, batch_size)
        #print(len(self.memory), self.batch_size)
        minibatch = random.sample(self.memory, self.batch_size)
        counter_ = 1
        for state, action, reward, next_state, done in minibatch:
            target = reward
            if not done:
                target = reward + self.gamma * np.amax(self.target_model.predict(next_state)[0])
            target_f = self.target_model.predict(state)
            target_f[0][action] = target
            # print(state, target_f, reward, self.gamma * np.amax(self.target_model.predict(next_state)[0]), self.target_model.predict(state))
            history = self.target_model.fit(state, target_f, epochs=1, verbose=0)
            # target = self.target_model.predict(state)
            # if done:
            #     target[0][action] = reward
            # else:
            #     # a = self.target_model.predict(next_state)[0]
            #     t = self.target_model.predict(next_state)[0]
            #     target[0][action] = reward + self.gamma * np.argmax(t)
            #     # print('log:', action, reward, np.argmax(t), reward + self.gamma * np.argmax(t))
            #     # target[0][action] = reward + self.gamma * t[np.argmax(a)]
            # #print(state, target)
            # history = self.target_model.fit(state, target, epochs=1, verbose=0)
            # print('loss:', history.history['loss'])
            losses.append(history.history['loss'])
            print('[-] Fitting loss instance #{} in minibatch: {}'.format(counter_, history.history['loss']))
            counter_ += 1

        if self.epsilon > self.epsilon_min:
            self.epsilon *= self.epsilon_decay

        return np.mean(losses)

    # def clear_memory(self):
    #     self.memory = deque(maxlen=2000)

    def get_state_size(self):
        return self.state_size

    def get_epsilon(self):
        return self.epsilon

    def update(self, state, action, next_state, reward, done, time):
        loss = 0.0
        if self.algorithm=='dqn':
            next_state = np.reshape(next_state, [1, self.state_size])
            next_state = np.clip(next_state, 0.0, 1.0)
            self.remember(state, action, reward, next_state, done)
            #print(state, action, reward, next_state, done)
            batch_size = self.batch_size
            # print('here')
            if len(self.memory) >= batch_size:
                # print(' replay')
                loss = self.replay(batch_size)
                self.forget()
        elif self.algorithm=='sarsa':

            self.q[state, action] = self.q[state, action] \
                                            + self.alpha * (reward + self.gamma * max(self.q[next_state]) \
                                            - self.q[state, action]) #- time * 100.0
            np.savetxt('q_matrix.txt', self.q)

        return loss

    def load(self, name):
        self.target_model.load_weights(name)
        # self.target_model = load_model(name)

    def save(self, name=""):
        self.target_model.save_weights(name)
        # self.target_model.save(name)

    def infer(self, state, env):
        action_pred = self.target_model.predict(state)
        # print(action_pred)
        action = np.argmax(action_pred[0])
        next_state = env.next_state(state, action)
        next_state = np.clip(next_state, 0., 1.)
        return next_state

# def sarsa(env, n_episodes, alpha=0.1, gamma=0.9, epsilon=0.1, quickest=0.1):
#     n_states, n_actions = env.n_states, env.n_actions
#     q = np.zeros((n_states, n_actions))
#     q.fill(float('-inf'))
#     for s in range(n_states):
#         actions = env.actions(s)
#         for a in actions:
#             q[s, a] = 0
#     results = []
#     for e in range(n_episodes):
#         print('episode #{:02d}'.format(e))
#         s = env.sample_initial_state()
#         a = epsilon_greedy_selection(q[s], env.actions(s), epsilon)
#         results_e = [s]
#         counting_steps = 0
#         while not env.is_final(s):
#             next_s, r = env.state_reward(s, a)
#             next_a = epsilon_greedy_selection(q[next_s], env.actions(next_s), epsilon)
#             q[s, a] = q[s, a] + alpha * (r + gamma * q[next_s, next_a] - q[s, a]) - quickest * counting_steps
#             counting_steps += 1
#             s = next_s
#             a = next_a
#             results_e.append(next_s)
#         results.append(results_e)
#     pickle.dump(results, open('results.pkl', 'wb'))
#     print(results)
#     pi = q.argmax(axis=1)
#     v = q.max(axis=1)
#     return pi, v

def run():
    env = environment.ToySynths()
    algorithm = 'dqn'
    agent = Dumby(env, epsilon=0.1, algorithm=algorithm)
    n_episodes = 50
    history = []
    for e in range(n_episodes):
        print('episode #{:02d}'.format(e+1))
        # initial state
        state = env.sample_initial_state()

        results_e = [state]

        for time in range(300):
            print(state, state.shape)
            # get current action, compute next_state and reward
            action = agent.act(state,  env.actions(state))
            next_state, reward, done = env.state_reward(state, action)
            results_e.append(next_state)
            # check if final state otherwise update
            if done:
                print("episode: {}/{}, score: {}, state: {}".format(e, n_episodes, time, next_state))
                break
            agent.update(state, action, next_state, reward, done, time)
            state = next_state

        #print(results_e)
        history.append(results_e)
    print(history)

if __name__ == "__main__":
    run()

max_stars_repo_name: bcaramiaux/humane-methods

max_issues_repo_name: bcaramiaux/humane-methods

max_forks_repo_name: bcaramiaux/humane-methods


text:
# quantum_2d.py
# Author: Vinod P. Gehlot
""" 2D Quantum system example. """

import numpy as np
from control_kit import DynamicModel
import matplotlib.pyplot as plt
import matplotlib as mpl

class Schrodinger(DynamicModel):
    """ Dynamics of the state-function of a quantum-mechanical system based
    on Schrodinger's equation.

    This class represents an n-dimensional model of quantum system (x) based
    on the Schrodinger's equation

    partial x/ partial t = -i H x, where H is the hamiltonian of the system.
    """
    def __init__(self, x0, t, h, name=""):
        """
        Constructs a dynamical system based on the Schrodinger's equation
        using a user supplied Hamiltonian.

        Args:
            x0: Initial condition of the state vector (1D numpy array).
            t: Time vector used for the simulation (1D numpy array).
            h: The Hamiltonian matrix for the Quantum system (2D numpy array).
            name: Optional, Name of the system (string).
        """
        self._h = h

        u0 = np.array([0])

        super().__init__(x0, u0, t, dtype=np.complex128, name=name)

    def _model(self, t, x, u, y):
        """ Models the Schrodinger's Equation.
        Args:
            t: Time instant in seconds (scalar).
            x: Instantaneous quantum state ( 1D numpy array).
            u: Instantaneous input vector (1D numpy array / scalar).
            y: Instantaneous output vector (1D numpy array).

        Returns:
            x_dot, which is - i * H * x (1D numpy array).

        """
        return -1j * self._h @ x

    def _output_model(self, t, x, u):
        """ The output of the quantum system.

        y = c * x = [1, 0] * x
        """

        c = np.array([[1+0j, 0+0j]])

        return c @ x

class QuantumEstimator(DynamicModel):
    """ Dynamics of the state-function of a quantum-mechanical system based
    on Schrodinger's equation.

    This class represents an n-dimensional model of quantum estimator system
    (x_hat) based on the Schrodinger's equation, and LTI estimator system
    estimator design.

    partial x/ partial t = -i H x, where H is the hamiltonian of the system.
    """
    def __init__(self, x0, t, h, k, y, name=""):
        """
        Constructs a dynamical system based on the Schrodinger's equation
        using a user supplied Hamiltonian.

        Args:
            x0: Initial condition of the state vector (1D numpy array).
            t: Time vector used for the simulation (1D numpy array).
            h: The Hamiltonian matrix for the Quantum system (2D numpy array).
            k: Filter gain matrix (numpy array).
            y: Quantum system initial output.
            name: Optional, Name of the system (string).
        """
        # #TODO argument check.

        self._h = h

        self._k = k

        # The input to the estimator is the output of the quantum system.
        # Therefore, u0 = y
        u0 = y

        super().__init__(x0, u0, t, dtype=np.complex128, name=name)

    def _model(self, t, x, u, y):
        """ Models the Schrodinger's Equation.
        Args:
            t: Time instant in seconds (scalar).
            x: Instantaneous quantum state ( 1D numpy array).
            u: Instantaneous input vector (1D numpy array / scalar).
            y: Instantaneous output vector (1D numpy array).

        Returns:
            x_dot, which is - i * H * x (1D numpy array).

        """
        return -1j * self._h @ x + self._k @ (u - y)

    def _output_model(self, t, x, u):
        """ The output model from the estimator.

        The output y_hat = [1, 0] * x.
        """
        c = np.array([[1+0j, 0+0j]])

        return c @ x

def basis_vectors():
    """ Orthonormal basis for the 2d system. """
    phi_1 = (1 / np.sqrt(2)) * np.array([1, 1j])
    phi_2 = (1 / np.sqrt(2)) * np.array([1, -1j])

    return phi_1, phi_2

def initial(angle, basis):
    """Quantum state initial conditions on a unit circle"""
    phi_1, phi_2 = basis()

    angle_d = np.rad2deg(angle)
    return np.cos(angle_d) * phi_1 + np.sin(angle_d) * phi_2

def probability(basis, x):
    """ Quantum probability.

    Args:
        basis: function returning basis vector list.
        x: quantum state vector (1D numpy array)

    Returns:
        list (generator) of quantum probabilities.
    """
    for phi_k in basis():
        yield abs(np.vdot(phi_k, x))**2

def main():
    """ Simulation. """

    """ Simulation time vector. """
    n_points = 1000
    t_final = 15
    t = np.linspace(0, t_final, n_points)

    """ Pauli Matrices and the corresponding the Hamiltonian."""
    sigma_0 = np.eye(2)

    sigma_2 = np.array([[0, -1j],
                        [1j, 0]])

    alpha = 0.1
    h = sigma_0 + alpha * sigma_2  # Hamiltonian

    """ Quantum system (plant)."""
    x0 = initial(10, basis_vectors)
    quantum = Schrodinger(x0, t, h, name="2d")

    """ Quantum state estimator. """
    k = np.array([[2 + 2j],
                  [(-2 / alpha) + ((alpha**2 - 1) / alpha)*1j]])  # Estimator gain matrix.
    x0_estimator = initial(20, basis_vectors)
    quantum_estimator = QuantumEstimator(x0_estimator, t, h, k, quantum.y, name="2d_est")

    # Storage for quantum probabilities (plant).
    alpha = [np.zeros((n_points, 1)) for k in range(2)]
    for i, alpha_k in enumerate(probability(basis_vectors, x0)):
        alpha[i][0] = alpha_k

    # Storage for quantum probabilities (estimator).
    alpha_est = [np.zeros((n_points, 1)) for k in range(2)]
    for i, alpha_k in enumerate(probability(basis_vectors, x0_estimator)):
        alpha_est[i][0] = alpha_k

    """ Simulation loop. """
    for k in range(1, n_points):
        ts = t[k-1], t[k]

        quantum_estimator.u = quantum.y

        quantum.integrate(ts)
        quantum_estimator.integrate(ts)

        for i, alpha_k in enumerate(probability(basis_vectors, quantum.x)):
            alpha[i][k] = alpha_k

        for i, alpha_k in enumerate(probability(basis_vectors, quantum_estimator.x)):
            alpha_est[i][k] = alpha_k

    """ Plot results. """

    mpl.rcParams["savefig.facecolor"] = 'black'
    with plt.style.context('dark_background'):
        fig, ax = plt.subplots(2, 2)

        ratio = 16/9
        width = 10
        height = 10 / ratio
        fig.set_figheight(height)
        fig.set_figwidth(width)

        ax[0, 0].plot(t, np.real(quantum.xa[:, 0]), ls=':', color='C3', lw=1, label='quantum plant')
        ax[0, 0].plot(t, np.real(quantum_estimator.xa[:, 0]), color='C4', alpha=0.8, lw=1, label='estimator')
        ax[0, 0].set_xlim([t[0], t[-1]])
        ax[0, 0].set_title('States (Real)')
        ax[0, 0].set_ylabel('$x$ and $\\hat{x}$')
        ax[0, 0].legend(loc='upper right')

        ax[1, 0].plot(t, np.imag(quantum.xa[:, 0]), ls=':', color='C2', lw=1, label='quantum plant')
        ax[1, 0].plot(t, np.imag(quantum_estimator.xa[:, 0]), color='C1', alpha=0.8, lw=1, label='estimator')
        ax[1, 0].set_xlim([t[0], t[-1]])
        ax[1, 0].set_title('States (Imaginary)')
        ax[1, 0].set_ylabel('$x$ and $\\hat{x}$')
        ax[1, 0].legend(loc='upper right')

        e = quantum_estimator.xa - quantum.xa
        ax[0, 1].plot(t, np.real(e[:, 0]), lw=1, label='real($e^p$)', ls=':')
        ax[0, 1].plot(t, np.imag(e[:, 0]), lw=1, label='imag($e^p$)', ls=':')
        ax[0, 1].plot(t, np.real(e[:, 1]), lw=1, label='real($e^m$)', ls=':')
        ax[0, 1].plot(t, np.imag(e[:, 1]), lw=1, label='imag($e^m$)', ls=':')
        ax[0, 1].set_xlim([t[0], t[-1]])
        ax[0, 1].set_title('Estimator Error')
        ax[0, 1].set_ylim([-10, 10])
        ax[0, 1].set_ylabel('$e = \\hat{x} - x$')
        ax_e_norm = ax[0, 1].twinx()
        ax_e_norm.plot(t, np.linalg.norm(e, axis=1), lw=2, label='$\|\|e\|\|$')
        ax_e_norm.set_ylabel('$\|\|e\|\|$')
        ax_e_norm.legend(loc='upper right')
        ax_e_norm.set_ylim([-10, 10])
        ax[0, 1].legend(loc='upper left', ncol=2)

        ax[1, 1].plot(t, alpha[0], label='$p_1(x)$', lw=1, color='C0')
        ax[1, 1].plot(t, alpha[1], label='$p_2(x)$', lw=1, color='C1')
        ax[1, 1].plot(t, alpha[0] + alpha[1], label='$p_1(x) + p_2(x)$', ls=':', lw=1, color='C2')
        ax[1, 1].set_xlim([t[0], t[-1]])
        ax[1, 1].set_ylim([-0.3, 1.3])
        ax[1, 1].set_xlabel('Time [s]')
        ax[1, 1].set_title('Probabilities (Plant)')
        ax[1, 1].set_ylabel('$p_k(x) = | (\\phi_k, x) |^2$')
        ax[1, 1].legend(loc='upper right')

        fig, ax = plt.subplots(1, 1)

        ratio = 16/9
        width = 10
        height = 10 / ratio
        fig.set_figheight(height)
        fig.set_figwidth(width)

        ax.plot(t, alpha[0], lw=1, color='C0', label='$p_1(x)$')
        ax.plot(t, alpha[1], lw=1, color='C1', label='$p_2(x)$')
        ax.plot(t, alpha[0] + alpha[1], lw=1, color='C2', label='$p_1(x)+p_2(x)$')
        ax.plot(t, alpha_est[0], lw=1, color='C0', ls=':', label='$p_1(\\hat{x})$')
        ax.plot(t, alpha_est[1], lw=1, color='C1', ls=':', label='$p_2(\\hat{x})$')
        ax.plot(t, alpha_est[0] + alpha_est[1], lw=1, color='C2', ls=':', label='$p_1(\\hat{x}) + p_2(\\hat{x})$')
        ax.set_xlim([t[0], t[-1]])
        ax.set_xlabel('Time [s]')
        ax.set_ylabel('$p_k(x) = | (\\phi_k, x) |^2$ and $p_k(\\hat{x}) = | (\\phi_k, \\hat{x}) |^2$')
        ax.set_title('Probabilities (Plant + Estimator)')
        ax.legend(ncol=2)
    plt.show()

if __name__ == '__main__':
    main()

max_stars_repo_name: tdgriffith/quixotic-quantum-control

max_issues_repo_name: tdgriffith/quixotic-quantum-control

max_forks_repo_name: tdgriffith/quixotic-quantum-control


text:
import copy

import numpy as np
from tqdm import tqdm

from kaa.timer import Timer
from kaa.bundle import BundleTransformer
from kaa.modes import BundleTransMode
from kaa.flowpipe import FlowPipe
from settings import KaaSettings
from kaa.templates import StaticStrat, MultiStrategy

class ReachError:

    def __init__(self, msg, total_steps):
        self.message = msg
        self.total_steps = total_steps

"""
Object handling all reachable flowpipe computations.
"""
class ReachSet:

    def __init__(self, model, strat=None, label="", trans_mode=BundleTransMode.AFO, restrict_inter=None):
        self.model = model
        self.trans_mode = trans_mode
        self.strat = StaticStrat(self.model) if strat is None else strat
        self.flowpipe = FlowPipe(self.model, strat, label)
        self.restrict_inter = restrict_inter if restrict_inter else [-np.Inf, np.Inf]

    """
    Compute reachable set for the alloted number of time steps.
    @params time_steps: number of time steps to carry out the reachable set computation.
            TempStrat: template n  loading strategy to use during this reachable set computation.
    @returns FlowPipe object containing computed flowpipe.
    """

    def computeReachSet(self, time_steps):
        transformer = BundleTransformer(self.model, self.trans_mode, self.restrict_inter)
        init_box_vol_thres = ((10 * self.model.dim) * self.model.bund.getIntersect().volume
                              if KaaSettings.UseThreshold
                              else -1)

        with tqdm(range(time_steps)) as step_iter:
            for step in step_iter:
                Timer.start('Reachable Set Computation')
                starting_bund = copy.deepcopy(self.flowpipe[step])  # probably a better way of copying objects.

                try:
                    Timer.start("Open Strategy")
                    self.strat.open_strat(starting_bund, step)
                    Timer.stop("Open Strategy")

                    Timer.start("Bundle Transformation")
                    trans_bund = transformer.transform(starting_bund)
                    Timer.stop("Bundle Transformation")

                    Timer.start("Close Strategy")
                    self.strat.close_strat(trans_bund, step)
                    Timer.stop("Close Strategy")

                except:
                    raise

                reach_time = Timer.stop('Reachable Set Computation')
                self.flowpipe.append(trans_bund)
                step_iter.set_description(f"Step Time: {reach_time}")

                'Check volume of enveloping box and stop loop if the volume becomes too large.'
                if KaaSettings.UseThreshold and self.check_reach_size(trans_bund, init_box_vol_thres):
                    print("Bundle volume grown to too large of volume. Ending reachable set computation.")

                    error = ReachError("Volume too large.", step)
                    self.flowpipe.error = error
                    break

        if not isinstance(self.strat, MultiStrategy):
            'Fetches all trajectories used in strategies during the reachable set computation'
            self.flowpipe.traj_data = self.strat.fetch_traj_data()

        'Store timing data recorded over entire reachable set computation into generated Flowpipe object.'
        self.flowpipe.timing_data = Timer.generate_stats()
        Timer.flush_timer_stack()

        return self.flowpipe

    def check_reach_size(self, bund, threshold):
        envelop_box_vol = bund.getIntersect().calc_vol_envelop_box()
        return envelop_box_vol > threshold

max_stars_repo_name: Tarheel-Formal-Methods/kaa-optimize

max_issues_repo_name: Tarheel-Formal-Methods/kaa-optimize

max_forks_repo_name: Tarheel-Formal-Methods/kaa-optimize


text:
"""
Neighborhood algorithm direct-search optimization; Source code can be found in
https://github.com/keithfma/neighborhood
"""

from copy import copy, deepcopy
from random import uniform
import numpy as np
import pandas as pd
import seaborn
from matplotlib import pyplot as plt
import json
import os

class Searcher():

    def __init__(self, objective, limits, num_samp, num_resamp, names=[], maximize=False, verbose=True):
        """
        Neighborhood algorithm direct-search optimization

        Arguments:
            objective: callable accepting a 1D numpy array of parameters, and
                returning a scalar misfit value
            limits: list of tuples defining range for each objective
                function parameter, as [(min_val, max_val), ...]
            num_samp: int, number of random samples taken at each iteration.
            num_resamp: int, number of best Voronoi polygons sampled at
                each iteration.
            names: list of strings, names for objective function parameters,
                used for plotting, and totally optional
            maximize: boolean, set True to maximize the objective function,
                or false to minimize it.
            verbose: set True to print verbose progress messages

        References:
            Sambridge, M. (1999). Geophysical inversion with a neighbourhood
            algorithm - I. Searching a parameter space. Geophysical Journal
            International, 138(2), 479–494.
        """
        # store and validate input args
        self._objective = objective
        self._limits = deepcopy(limits)
        self._num_samp = num_samp
        self._num_resamp = num_resamp
        if names:
            self._names = copy(names)
        else:
            self._names = ['x{}'.format(ii) for ii in range(len(limits))]
        self._maximize = maximize
        self._verbose = verbose
        self._validate_args()

        # init constants and working vars
        self._num_dim = len(limits)
        self._param_min = np.array([x[0] for x in limits])
        self._param_max = np.array([x[1] for x in limits])
        self._param_rng = np.array([x[1]-x[0] for x in limits])
        self._sample = []
        self._queue = []
        self._iter = 0

    @property
    def sample(self):
        return deepcopy(self._sample)

    @property
    def sample_dataframe(self):
        samps = self.sample
        for samp in samps:
            for name, val in zip(self._names, samp['param']):
                samp[name] = val
            del samp['param']
        return pd.DataFrame(samps)

    def update(self, num_iter=10):
        """
        Execute search algorithm for specified number of iterations

        Arguments:
            num_iter: int, number of iterations to run
        """
        for ii in range(num_iter):

            # generate new sample (populates queue)
            if not self._sample:
                self._random_sample()
            else:
                self._neighborhood_sample()

            # execute forward model for all samples in queue
            while self._queue:
                param = self._queue.pop()
                result = self._objective(param)
                self._sample.append({
                    'param': param,
                    'result': result,
                    'iter': self._iter
                    })

            # prepare for next iteration
            self._sample.sort(key=lambda x: x['result'], reverse=self._maximize)
            self._iter += 1
            if self._verbose:
                print(self)

        return self._sample[0]['param'], self._sample[0]['result']

    def to_json(self, filename, clobber=False):
        """
        Write search data to file as JSON
        NOTE: *cannot save the objective function*, converting to JSON is a
            one-way trip
        Arguments:
            filename: string, path to file to write
            clobber: set True to allow overwriting, False to prevent it
        """
        # check that it is safe to write
        if os.path.isfile(filename) and not clobber:
            raise ValueError('file "{}" exists and clobber is off'.format(filename))

        # make sample serializable
        sample_out = self.sample
        for this in sample_out:
            this['param'] = list(this['param'])

        # gather and write data
        data = {
            'limits': self._limits,
            'num_samp': self._num_samp,
            'num_resamp': self._num_resamp,
            'names': self._names,
            'maximize': self._maximize,
            'iter': self._iter,
            'sample': sample_out,
            }
        with open(filename, 'w') as fp:
            json.dump(data, fp, indent=4)

    def plot(self, size=None, filename=None):
        """
        Pair-plots of sample distribution

        Arguments:
            size: figure (width, height) in inches
            filename: string, provide a file name to save the figure to, if
                None the figure is displayed instead
        """
        df = self.sample_dataframe

        # get names of variables to plot
        var_names = set(df.columns)
        var_names.discard('iter')
        var_names.discard('result')
        var_names = list(var_names)
        num_vars = len(var_names)

        # get axis limits for each variable
        var_limits = {}
        for name in var_names:
            idx = self._names.index(name)
            var_limits[name] = self._limits[idx]

        # main loop, populate grid and titles
        fig, axs = plt.subplots(nrows=num_vars, ncols=num_vars)
        if size:
            fig.set_size_inches(size)
        fig.subplots_adjust(hspace=0.2, wspace=0.2)
        for ii in range(num_vars):
            for jj in range(num_vars):
                ax = axs[ii][jj]
                if ii == jj:
                    # diagonal, plot 1D KDE
                    series = df[var_names[ii]]
                    series.plot.kde(ax=ax, xlim=var_limits[var_names[ii]])
                    axs[ii][jj].set_yticklabels([], fontdict=None, minor=False)
                elif ii < jj:
                    # upper triangle, plot scatter colored by result value
                    xx = df[var_names[jj]]
                    yy = df[var_names[ii]]
                    zz = df['result']
                    ax.scatter(xx, yy, c=zz)
                    for tick in ax.get_yticklabels():
                            tick.set_rotation(45)
                elif ii > jj:
                    # lower triangle, plot 2D KDE
                    xx = df[var_names[jj]]
                    yy = df[var_names[ii]]
                    seaborn.kdeplot(xx, yy, ax=ax, cmap="Blues", shade=True, shade_lowest=True)
                    for tick in ax.get_yticklabels():
                            tick.set_rotation(45)
                    if ii != num_vars - 1:
                        axs[ii][jj].set_xlabel('')
                    if jj != 0:
                        axs[ii][jj].set_ylabel('')
                    ax.set_xlim(var_limits[var_names[jj]])
                    ax.set_ylim(var_limits[var_names[ii]])

        ttl = [
            '2D PDFs (lower), 1D PDFs (diag), 2D Scatter (upper)',
            'Iteration: {}, Samples: {}, Best = {:.4f}'.format(
                self._iter, len(self._sample), df['result'][0]),
             ]
        fig.suptitle('\n'.join(ttl))
        fig.subplots_adjust(hspace=0.4, wspace=0.4)
        fig.savefig('new.png')
        if filename:
            fig.savefig(filename)
        else:
            fig.show()

    def _random_sample(self):
        """Generate uniform random sample for initial iteration"""
        for ii in range(self._num_samp):
            pt  = np.random.rand(self._num_dim)*self._param_rng + self._param_min
            self._queue.append(pt)

    def _neighborhood_sample(self):
        """Generate random samples in best Voronoi polygons"""

        vv = np.array([x['param'] for x in self._sample])
        vv = (vv - self._param_min)/self._param_rng # normalize

        for ii in range(self._num_samp):

            # get starting point and all other points as arrays
            kk = ii % self._num_resamp  # index of start point
            vk = vv[kk,:]
            vj = np.delete(vv, kk, 0)
            xx = vk.copy()

            # get initial distance to ith-axis (where i == 0)
            d2ki = 0.0
            d2ji = np.sum(np.square(vj[:,1:] - xx[1:]), axis=1)

            # random step within voronoi polygon in each dimension
            for ii in range(self._num_dim):

                # find limits of voronoi polygon
                xji = 0.5*(vk[ii] + vj[:,ii] + (d2ki - d2ji)/(vk[ii] - vj[:,ii]))
                try:
                    low = max(0.0, np.max(xji[xji <= xx[ii]]))
                except ValueError: # no points <= current point
                    low = 0.0
                try:
                    high = min(1.0, np.min(xji[xji >= xx[ii]]))
                except ValueError: # no points >= current point
                    high = 1.0

                # random move within voronoi polygon
                xx[ii] = uniform(low, high)

                # update distance to next axis
                if ii < (self._num_dim - 1):
                    d2ki += (np.square(vk[ii  ] - xx[ii  ]) -
                             np.square(vk[ii+1] - xx[ii+1]))
                    d2ji += (np.square(vj[:,ii  ] - xx[ii  ]) -
                             np.square(vj[:,ii+1] - xx[ii+1]))

            # update queue
            xx = xx*self._param_rng + self._param_min # un-normalize
            self._queue.append(xx)

    def _validate_args(self):
        """Check argument types, throw informative exceptions"""
        # # objective
        if not callable(self._objective):
            raise TypeError('"objective" must be a callable')
        # # limits
        if not isinstance(self._limits, list):
            raise TypeError('"limits" must be a list')
        for lim in self._limits:
            if not isinstance(lim, tuple) or len(lim) != 2:
                raise TypeError('"limits" elements must be length-2 tuples')
            if lim[1] <= lim[0]:
                raise ValueError('"limits" elements must be increasing')
        # # num_samp
        if int(self._num_samp) != self._num_samp:
            raise TypeError('"num_samp" must be an integer')
        if self._num_samp < 1:
            raise ValueError('"num_samp" must be positive')
        # # num_resamp
        if int(self._num_resamp) != self._num_resamp:
            raise TypeError('"num_resamp" must be an integer')
        if self._num_resamp < 1:
            raise ValueError('"num_resamp" must be positive')
        if self._num_resamp > self._num_samp:
            raise ValueError('"num_resamp must be <= "num_samp"')
        # # maximize
        if not isinstance(self._maximize, bool):
            raise TypeError('maximize must be boolean: True or False')
        # # verbose
        if not isinstance(self._verbose, bool):
            raise TypeError('verbose must be boolean: True or False')

    def __repr__(self):
        try:
            out = '{}(iteration={}, samples={}, best={:.6e})'.format(
                self.__class__.__name__,
                self._iter,
                len(self._sample),
                self._sample[0]['result'])
        except IndexError:
            out = '{}(iteration=0, samples=0, best=None)'.format(self.__class__.__name__)
        return out

max_stars_repo_name: Siglab-code/WaveFrost

max_issues_repo_name: Siglab-code/WaveFrost

max_forks_repo_name: Siglab-code/WaveFrost


text:
import cv2, json, os, sys
from tqdm import tqdm
import numpy as np
import cupy as cp
from data_loading.image_data_loader import ImageDataLoader
from data_loading.image_preprocessor import ImagePreprocessor
from examples.imagenet_dogs_225_resnet_18_depsep import ResNet18

BATCH_SIZE = 60
data_folder = "/home/will/Datasets"
im_dir = "./dog_images"
verify_accuracy = False
preprocessor = ImagePreprocessor(image_size=(225,225), crop_mode="center")

experiment_name = "DogsImageNet225ResNet18DepSep"
num_to_dog_name_map_fname = "./imagenet_dog_class_names/num_to_dog_name_map.json"
with open(num_to_dog_name_map_fname, "r") as f:
    num_to_dog_name_map = json.load(f)
network = ResNet18("", load_layers=False)
network.load_network_from_json_and_h5(os.path.join(experiment_name, experiment_name + ".json"),
                                      os.path.join(experiment_name, "epoch_26_testacc_0.686.h5"))

if verify_accuracy:
    # You'll need the dataset for this...
    val_data_loader = ImageDataLoader(os.path.join(data_folder, "ImageNet2012/ILSVRC2012_dogs/val_img"),
                                    BATCH_SIZE,
                                    preprocessor,
                                    start_thread=True)
    print("Testing...")
    test_acc = network.test(val_data_loader.pull_batch(int(120*50/BATCH_SIZE)), BATCH_SIZE, 120*50)
    val_data_loader.stop_thread()
    print("Test acc: {}".format(test_acc))

if not os.path.isdir(os.path.join(im_dir, "outputs")):
    os.mkdir(os.path.join(im_dir, "outputs"))

for im_path in os.listdir(im_dir):
    if not(os.path.isdir(os.path.join(im_dir, im_path))):
        im = preprocessor.load_image(os.path.join(im_dir, im_path))
        X = im.reshape((1,) + im.shape)
        loss, batch_scores = network.forward(X, y_one_hot=None, test_mode=True)
        scores = batch_scores[0,:]
        best = np.argsort(cp.asnumpy(scores))[::-1]
        print("###########################")
        for i in range(5):
            print(im_path, best[i], scores[best[i]], num_to_dog_name_map[str(best[i])])
        plain_im = cv2.imread(os.path.join(im_dir, im_path))
        cv2.putText(plain_im, num_to_dog_name_map[str(best[0])], (int(plain_im.shape[0]/10),int(plain_im.shape[1]/10)),
                    cv2.FONT_HERSHEY_SIMPLEX, min(plain_im.shape[0], plain_im.shape[1])/1000, (0, 255, 100), 5)
        cv2.imwrite(os.path.join(im_dir, "outputs", im_path), plain_im)

max_stars_repo_name: WJGiles/Dorknet

max_issues_repo_name: WJGiles/Dorknet

max_forks_repo_name: WJGiles/Dorknet

reason: import numpy,import cupy


text:
from loguru import logger
import cv2
import os
import pickle
import typing
import numpy as np
from sklearn.svm import LinearSVC

from stagesepx.classifier.base import BaseModelClassifier
from stagesepx import toolbox
from stagesepx.video import VideoFrame
from stagesepx import constants

class SVMClassifier(BaseModelClassifier):
    FEATURE_DICT = {
        "hog": toolbox.turn_hog_desc,
        "lbp": toolbox.turn_lbp_desc,
        # do not use feature transform
        "raw": lambda x: x,
    }
    UNKNOWN_STAGE_NAME = constants.UNKNOWN_STAGE_FLAG

    def __init__(
        self, feature_type: str = None, score_threshold: float = None, *args, **kwargs
    ):
        """
        init classifier

        :param feature_type:
            before training, classifier will convert pictures into feature, for better classification.
            eg: 'hog', 'lbp' or 'raw'
        :param score_threshold:
            float, 0 - 1.0, under this value, label -> UNKNOWN_STAGE_NAME
            default value is 0 (None)
        """
        super().__init__(*args, **kwargs)

        # feature settings
        if not feature_type:
            feature_type = "hog"
        if feature_type not in self.FEATURE_DICT:
            raise AttributeError(f"no feature func named {feature_type}")
        self.feature_func: typing.Callable = self.FEATURE_DICT[feature_type]
        logger.debug(f"feature function: {feature_type}")

        # model settings
        self._model: typing.Optional[LinearSVC] = None
        self.score_threshold: float = score_threshold or 0.0
        logger.debug(f"score threshold: {self.score_threshold}")

    def clean_model(self):
        self._model = None

    def save_model(self, model_path: str, overwrite: bool = None):
        """
        save trained model

        :param model_path:
        :param overwrite:
        :return:
        """
        logger.debug(f"save model to {model_path}")
        # assert model file
        if os.path.isfile(model_path) and not overwrite:
            raise FileExistsError(
                f"model file {model_path} already existed, you can set `overwrite` True to cover it"
            )
        # assert model data is not empty
        assert self._model, "model is empty"
        with open(model_path, "wb") as f:
            pickle.dump(self._model, f)

    def load_model(self, model_path: str, overwrite: bool = None):
        """
        load trained model

        :param model_path:
        :param overwrite:
        :return:
        """
        logger.debug(f"load model from {model_path}")
        # assert model file
        assert os.path.isfile(model_path), f"model file {model_path} not existed"
        # assert model data is empty
        if self._model and not overwrite:
            raise RuntimeError(
                f"model is not empty, you can set `overwrite` True to cover it"
            )

        # joblib raise an error ( i have no idea about how to fix it ) here, so use pickle instead
        with open(model_path, "rb") as f:
            self._model = pickle.load(f)

    def train(self):
        """
        train your classifier with data. must be called before prediction

        :return:
        """
        if not self._model:
            logger.debug("no model can be used. build a new one.")
            self._model = LinearSVC()
        else:
            logger.debug("already have a trained model. train on this model.")

        train_data = list()
        train_label = list()
        for each_label, each_label_pic_list in self.read():
            for each_pic_object in each_label_pic_list:
                logger.debug(f"training label: {each_label}")
                # apply hook
                each_pic_object = self._apply_hook(
                    VideoFrame(-1, -1.0, each_pic_object)
                )
                each_pic_object = each_pic_object.data

                each_pic_object = self.feature_func(each_pic_object).flatten()
                train_data.append(each_pic_object)
                train_label.append(each_label)
        logger.debug("data ready")

        assert (
            len(train_label) > 1
        ), f"seems only one class in the training dataset, at least two classes are required: {train_label}"
        self._model.fit(train_data, train_label)
        logger.debug("train finished")

    def predict(self, pic_path: str) -> str:
        """
        predict a single picture

        :param pic_path:
        :return:
        """
        pic_object = toolbox.imread(pic_path)
        return self.predict_with_object(pic_object)

    def predict_with_object(self, frame: np.ndarray) -> str:
        """
        predict a single object

        :param frame:
        :return:
        """
        pic_object = self.feature_func(frame)
        pic_object = pic_object.reshape(1, -1)

        # scores for each stages
        # IMPORTANT:
        # these scores are not always precise
        # at the most of time, we used a tiny train data set for training
        # which may causes 'liblinear failed to converge'
        # actually, it can know which one is the target class
        # but the calculated value may becomes weird
        scores = self._model.decision_function(pic_object)[0]
        logger.debug(f"scores: {scores}")

        # in the binary case, return type is different (wtf ...)
        # for more effective i think
        if len(self._model.classes_) == 2:
            # scores is a float
            # confidence score for self.classes_[1] where >0 means this
            # class would be predicted
            return self._model.classes_[1 if scores > 0 else 0]

        # unknown
        if max(scores) < self.score_threshold:
            logger.warning(
                f"max score is lower than {self.score_threshold}, unknown class"
            )
            return self.UNKNOWN_STAGE_NAME

        return self._model.classes_[np.argmax(scores)]

    def _classify_frame(self, frame: VideoFrame, *_, **__) -> str:
        return self.predict_with_object(frame.data)

max_stars_repo_name: IcyW/stagesepx

max_issues_repo_name: IcyW/stagesepx

max_forks_repo_name: IcyW/stagesepx


text:
import gym, yumi_gym
import pybullet as p
import numpy as np
import h5py
import time
import os

file = './checkpoints/2021-3-21/optimize-wo.h5'
print(file)
hf = h5py.File(file, 'r')
group1 = hf.get('group1')
print(group1.keys())
l_joint_angle = group1.get('l_joint_angle_2')
r_joint_angle = group1.get('r_joint_angle_2')
l_hand_angle = group1.get('l_glove_angle_2')
r_hand_angle = group1.get('r_glove_angle_2')
l_hand_pos = group1.get('l_joint_pos_2')[:, 6]
r_hand_pos = group1.get('r_joint_pos_2')[:, 6]
l_joint_default = [-1.341905951499939, -1.7764934301376343, 0.5122540473937988, -0.315954327583313, 1.8956027030944824, 0.47532641887664795, -0.7984092235565186]
r_joint_default = [1.2869668006896973, -1.7874925136566162, -0.581124496459961, -0.5961062908172607, -0.1543283462524414, 0.47532641887664795, -0.7984092235565186]
height_limit = -0.25
total_frames = l_joint_angle.shape[0]
l_joint_angle_processed = []
r_joint_angle_processed = []

env = gym.make('yumi-v0')
env.render()
observation = env.reset()

while True:
    env.render()
    for t in range(total_frames):
        # t = 100
        # if t < 30 or t > 180:
        #     continue
        print(t, l_joint_angle.shape)

        action = []
        if l_hand_pos[t][2] > height_limit:
            action += l_joint_angle[t].tolist()
            l_joint_angle_processed.append(l_joint_angle[t].tolist())
        else:
            action += l_joint_default
            l_joint_angle_processed.append(l_joint_default)
        if r_hand_pos[t][2] > height_limit:
            action += r_joint_angle[t].tolist()
            r_joint_angle_processed.append(r_joint_angle[t].tolist())
        else:
            action += r_joint_default
            r_joint_angle_processed.append(r_joint_default)
        action += l_hand_angle[t].tolist() + r_hand_angle[t].tolist()
        # action = l_joint_angle[t].tolist() + r_joint_angle[t].tolist() + l_hand_angle[t].tolist() + r_hand_angle[t].tolist()
        # action[5], action[12], action[6], action[13] = 0, 0, 0, 0
        # print(action)
        observation, reward, done, info = env.step(action)
        time.sleep(0.02)

    # l_joint_angle_processed = np.stack(l_joint_angle_processed, axis=0)
    # r_joint_angle_processed = np.stack(r_joint_angle_processed, axis=0)
    # hf_processed = h5py.File('processed.h5', 'w')
    # g1 = hf_processed.create_group('group1')
    # g1.create_dataset('l_joint_angle_2', data=l_joint_angle_processed)
    # g1.create_dataset('r_joint_angle_2', data=r_joint_angle_processed)
    # g1.create_dataset('l_glove_angle_2', data=l_hand_angle)
    # g1.create_dataset('r_glove_angle_2', data=r_hand_angle)
    # print('Processing done!!!')
    # break
env.close()


text:
# -*- coding:utf-8 -*-
from __future__ import unicode_literals, print_function, division

import os
from six import string_types

import jieba
import numpy as np

from keras.models import model_from_json

from model.base.document import Document
from model.base.word2vec import train_word2vec, fit_scaler
from model.config import NN_ARCHITECTURE, BATCH_SIZE, EMBEDDING_SIZE, NB_EPOCHS, THRESHOLD
from model.nn.input_data import get_data_for_model
from model.nn.models import get_nn_model
from model.utils import save_to_disk, load_from_disk

class Multi_Label_Model(object):

    def __init__(self, keras_model=None, word2vec_model=None, scaler=None,
                 labels=None):
        self.labels = labels

        if isinstance(keras_model, string_types):
            self.load_model(keras_model)
        else:
            self.keras_model = keras_model

        if isinstance(word2vec_model, string_types):
            self.load_word2vec_model(word2vec_model)
        else:
            self.word2vec_model = word2vec_model

        if isinstance(scaler, string_types):
            self.load_scaler(scaler)
        else:
            self.scaler = scaler

    def train(self, train_dir=None, labels=None, test_dir=None, callbacks=None, nn_model=NN_ARCHITECTURE,
              batch_size=BATCH_SIZE, test_ratio=0.0, nb_epochs=NB_EPOCHS, verbose=1,
              x_train=None, y_train=None, test_data=None, automatic_data=True):
        """
        Train the model on given data.
        Data could be handled by model itself, but the format must be standardized.

        :param train_dir: directory with data files. Text files should end with
        '.txt' and corresponding files containing labels should end with '.lab'
        :param labels: iterable containing all considered labels
        :param test_dir: directory with test files. They will be used to evaluate
        the model after every epoch of training.
        :param callbacks: objects passed to the Keras fit function as callbacks
        :param nn_model: string defining the NN architecture e.g. 'crnn'
        :param batch_size: size of one batch
        :param test_ratio: the ratio of samples that will be withheld from training
        and used for testing. This can be overridden by test_dir.
        :param nb_epochs: number of epochs to train
        :param verbose: 0, 1 or 2. As in Keras.
        :param automatic_data: If True, means that the format of data is standard and
        x_train, y_train, can't be None. The model will get data from data file automatic.
        If False, user must handle data by yourself and transmit the data for training.

        :return: History object
        """

        if not self.word2vec_model:
            raise ValueError('Word2vec model is not trained. Run train_word2vec() first.')

        if not self.scaler:
            raise ValueError('The scaler is not trained. Run fit_scaler() first.')

        if self.keras_model:
            Warning('WARNING! Overwriting already trained Keras model.')

        self.labels = labels
        self.keras_model = get_nn_model(
            nn_model,
            embedding=self.word2vec_model.vector_size,
            output_length=len(labels)
        )

        if automatic_data:
            (x_train, y_train), test_data = get_data_for_model(
                train_dir,
                labels,
                test_dir=test_dir,
                nn_model=self.keras_model,
                as_generator=False,
                batch_size=batch_size,
                word2vec_model=self.word2vec_model,
                scaler=self.scaler,
            )

        return self.keras_model.fit(
            x_train,
            y_train,
            batch_size=batch_size,
            nb_epoch=nb_epochs,
            validation_data=test_data,
            validation_split=test_ratio,
            callbacks=callbacks or [],
            verbose=verbose,
        )

    def predict_from_file(self, filepath, labels=None):
        """
        Predict labels for a txt file

        :param filepath: path to the file

        :return: list of labels with corresponding confidence intervals
        """
        doc = Document(0, filepath)
        return self._predict(doc, labels)

    def predict_from_text(self, text, labels=None):
        """
        Predict labels for a given string of text

        :param text: string or unicode with the text

        :return: list of labels with corresponding confidence intervals
        """
        doc = Document(0, None, text=text)
        return self._predict(doc, labels)

    def predict_with_string(self, string_list, labels=None):
        """ Use string to predict directly """
        return map(lambda x: self._predict(x, labels, use_type="string"), [string for string in string_list])

    def predict_class_with_string(self, string_list, labels=None):
        """ Predict class for data using string directly """
        class_probability = self.predict_with_string(string_list, labels)

        examples = []
        for i, example in enumerate(class_probability):
            text_label = []
            for (label, probability) in example:
                if probability > THRESHOLD:
                    text_label.append(label)
            if len(text_label) == 0:
                text_label.append(class_probability[i][0][0])
            examples.append(text_label)

        return examples

    def _predict(self, doc, labels=None, use_type="text"):
        """
        Predict labels for a given Document object. If 'self.labels' is None, labels can't be None.

        :param doc: Document object
        :param labels: If the model is reset, user should transmit the labels again.
        :param use_type: The type of input for predicting, text or string.

        :return: list of labels with corresponding confidence intervals
        """
        if type(self.keras_model.input) == list:
            _, sample_length, embedding_size = self.keras_model.input_shape[0]
        else:
            _, sample_length, embedding_size = self.keras_model.input_shape

        if use_type == "text":
            words = doc.get_all_words()[:sample_length]
        else:
            words = '/'.join(jieba.cut(doc)).split('/')[:sample_length]
        x_matrix = np.zeros((1, sample_length, embedding_size))

        for i, w in enumerate(words):
            if w in self.word2vec_model:
                word_vector = self.word2vec_model[w].reshape(1, -1)
                scaled_vector = self.scaler.transform(word_vector, copy=True)[0]
                x_matrix[0][i] = scaled_vector

        if type(self.keras_model.input) == list:
            x = [x_matrix] * len(self.keras_model.input)
        else:
            x = [x_matrix]

        y_predicted = self.keras_model.predict(x)

        try:
            zipped = zip(self.labels, y_predicted[0])
        except TypeError:
            zipped = zip(labels, y_predicted[0])

        return sorted(zipped, key=lambda elem: elem[1], reverse=True)

    def init_word_vectors(self, train_dir, vec_dim=EMBEDDING_SIZE):
        """
        Train word2vec model and fit the scaler afterwards

        :param train_dir: directory with '.txt' files
        :param vec_dim: dimensionality of the word vectors

        :return: None
        """
        self.train_word2vec(train_dir, vec_dim=vec_dim)
        self.fit_scaler(train_dir)

    def train_word2vec(self, train_dir, vec_dim=EMBEDDING_SIZE):
        """
        Train the word2vec model on a directory with text files.

        :param train_dir: directory with '.txt' files
        :param vec_dim: dimensionality of the word vectors

        :return: trained gensim model
        """
        if self.word2vec_model:
            print('WARNING! Overwriting already trained word2vec model.')

        self.word2vec_model = train_word2vec(train_dir, vec_dim=vec_dim)

        return self.word2vec_model

    def fit_scaler(self, train_dir):
        """
        Fit a scaler on given data. Word vectors must be trained already.

        :param train_dir: directory with '.txt' files

        :return: fitted scaler object
        """
        if not self.word2vec_model:
            raise ValueError('word2vec model is not trained. Run train_word2vec() first.')

        if self.scaler:
            Warning('WARNING! Overwriting already fitted scaler.')

        self.scaler = fit_scaler(train_dir, word2vec_model=self.word2vec_model)

        return self.scaler

    def save_scaler(self, filepath, overwrite=False):
        """ Save the scaler object to a file """
        save_to_disk(filepath, self.scaler, overwrite=overwrite)

    def load_scaler(self, filepath):
        """ Load the scaler object from a file """
        self.scaler = load_from_disk(filepath)

    def save_word2vec_model(self, filepath, overwrite=False):
        """ Save the word2vec model to a file """
        save_to_disk(filepath, self.word2vec_model, overwrite=overwrite)

    def load_word2vec_model(self, filepath):
        """ Load the word2vec model from a file """
        self.word2vec_model = load_from_disk(filepath)

    def save_model(self, filepath):
        """ Save the keras NN model's architecture to a json file and weights to a HDF5 file """
        arch_file = os.path.join(filepath, 'nn_arch.json')
        weight_file = os.path.join(filepath, 'nn_weights.h5')

        if os.path.exists(arch_file):
            raise ValueError("File " + arch_file + " already exists!")

        if os.path.exists(weight_file):
            raise ValueError("File " + weight_file + " already exists!")

        open(arch_file, 'w').write(self.keras_model.to_json())

        self.keras_model.save_weights(weight_file)

    def load_model(self, filepath):
        """ Load the keras NN model's architecture from a json file and weights from a HDF5 file """
        if not os.path.exists(filepath):
            raise ValueError("File " + filepath + " does not exist!")

        list_dir = os.listdir(filepath)
        arch_file, weights_file = '', ''
        for dir in list_dir:
            if dir.split('.')[-1] == 'json':
                arch_file = os.path.join(filepath, dir)
            elif dir.split('.')[-1] == 'h5':
                weights_file = os.path.join(filepath, dir)

        if arch_file == '':
            raise ValueError("The json file for NN's architecture is not exist!")

        if weights_file == '':
            raise ValueError("The HDF5 file for NN's weights is not exist!")

        self.keras_model = model_from_json(open(arch_file).read())

        self.keras_model.load_weights(weights_file)

max_stars_repo_name: HeadCow/ARPS

max_issues_repo_name: HeadCow/ARPS

max_forks_repo_name: HeadCow/ARPS


text:
import os
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split

import sktime
from sktime.utils.data_io import load_from_tsfile_to_dataframe
import data_loader as data

DATA_PATH = "data/"
# DATASET = 'RR'

def split_data(
    X_train_orig, y_train_orig, X_test_orig, y_test_orig, shuffle=True, seed=0
):
    if shuffle:
        X_all = pd.concat((X_train_orig, X_test_orig))
        y_all = np.concatenate((y_train_orig, y_test_orig))
        X_train, X_eval, y_train, y_eval = train_test_split(
            X_all, y_all, test_size=0.3, random_state=seed
        )
        X_val, X_test, y_val, y_test = train_test_split(
            X_eval, y_eval, test_size=0.5, random_state=seed + 1
        )
    else:
        X_test, y_test = X_test_orig, y_test_orig
        val_size = int(X_train_orig.shape[0] / 7.0)  # .6 / .1 / .3 split
        X_train, y_train = X_train_orig[:-val_size], y_train_orig[:-val_size]
        X_val, y_val = X_train_orig[-val_size:], y_train_orig[-val_size:]
        # X_train, X_val, y_train, y_val = train_test_split(X_train_orig, y_train_orig, test_size=0.20, random_state=seed)
    return X_train, y_train, X_val, y_val, X_test, y_test

def _to_numpy(X):
    """ Convert DataFrame of series into numpy array """
    return np.stack([np.stack(x) for x in X.to_numpy()]).swapaxes(-1, -2)

def process_data(DATASET, shuffle=True, seed=0):
    X_train_orig, y_train_orig = data.load_from_tsfile_to_dataframe(
        os.path.join(f"{DATA_PATH}/{DATASET}/BIDMC32{DATASET}_TRAIN.ts"),
        replace_missing_vals_with="NaN",
    )

    X_test_orig, y_test_orig = data.load_from_tsfile_to_dataframe(
        os.path.join(f"{DATA_PATH}/{DATASET}/BIDMC32{DATASET}_TEST.ts"),
        replace_missing_vals_with="NaN",
    )

    X_train, y_train, X_val, y_val, X_test, y_test = split_data(
        X_train_orig, y_train_orig, X_test_orig, y_test_orig, shuffle=shuffle, seed=seed
    )

    split = "reshuffle" if shuffle else "original"
    data_dir = os.path.join(DATA_PATH, DATASET, split)
    os.makedirs(data_dir, exist_ok=True)
    np.save(os.path.join(data_dir, "trainx.npy"), _to_numpy(X_train))
    np.save(os.path.join(data_dir, "trainy.npy"), y_train)
    np.save(os.path.join(data_dir, "validx.npy"), _to_numpy(X_val))
    np.save(os.path.join(data_dir, "validy.npy"), y_val)
    np.save(os.path.join(data_dir, "testx.npy"), _to_numpy(X_test))
    np.save(os.path.join(data_dir, "testy.npy"), y_test)

    for f in ["trainx", "trainy", "validx", "validy", "testx", "testy"]:
        df = np.load(f"{DATA_PATH}/{DATASET}/{split}/{f}.npy")
        print(f, df.shape, df.dtype)

if __name__ == "__main__":
    for DATASET in ["RR", "HR", "SpO2"]:
        process_data(DATASET, shuffle=True)

max_stars_repo_name: dumpmemory/state-spaces

max_issues_repo_name: dumpmemory/state-spaces

max_forks_repo_name: MikeOwino/state-spaces


text:
import time
import numpy as np
import pyrosim.pyrosim as pyrosim
import platform
import os
import constants as c

class Solution():
    def __init__(self, id):
        self.weights = np.random.rand(c.num_sensor_neurons, c.num_motor_neurons)
        self.weights = self.weights * 2. - 1.
        self.set_id(id)

    def start_simulation(self, headless, show_debug_output=False, save_as_best=False):
        self.generate_body()
        self.generate_brain()
        if platform.system() == "Windows":
            if show_debug_output:
                os.system(f"start /B python simulate.py {'DIRECT' if headless else 'GUI'} --id {self.id} {'--best' if save_as_best else ''}")
            else:
                os.system(f"start /B python simulate.py {'DIRECT' if headless else 'GUI'} --id {self.id} {'--best' if save_as_best else ''} > nul 2> nul")

        else:
            if show_debug_output:
                os.system(f"python simulate.py {'DIRECT' if headless else 'GUI'} --id {self.id} {'--best' if save_as_best else ''}" + " &")
            else:
                os.system(f"python simulate.py {'DIRECT' if headless else 'GUI'} --id {self.id} {'--best' if save_as_best else ''} 2&>1" + " &")

    def wait_for_simulation(self):
        fit_file = f"fitness{self.id}.txt"

        while not os.path.exists(fit_file):
            time.sleep(0.01)

        with open(fit_file) as f:
            self.fitness = float(f.read())

        if platform.system() == "Windows":
            os.system(f"del fitness{self.id}.txt")
        else:
            os.system(f"rm fitness{self.id}.txt")

        f.close()

    def set_id(self, id):
        self.id = id

    def mutate(self):
        mutate_row = np.random.randint(0, 3)
        mutate_col = np.random.randint(0, 2)
        self.weights[mutate_row, mutate_col] = np.random.rand() * 2. - 1.

    def generate_body(self):
        pyrosim.Start_URDF(f"body{self.id}.urdf")
        pyrosim.Send_Cube(name="Torso", pos=[0, 0, 1], size=[1, 1, 1])
        pyrosim.Send_Joint( name = "Torso_BackLeg" , parent= "Torso" , child = "BackLeg" , type = "revolute", position = [0, -0.5, 1.0], jointAxis = "1 0 0")
        pyrosim.Send_Cube(name="BackLeg", pos=[0.0, -0.5, 0.0], size=[.2, 1., .2])
        pyrosim.Send_Joint( name = "Torso_FrontLeg" , parent= "Torso" , child = "FrontLeg" , type = "revolute", position = [0.0, 0.5, 1.0], jointAxis = "1 0 0")
        pyrosim.Send_Cube(name="FrontLeg", pos=[0.0, 0.5, 0], size=[.2, 1., .2])
        pyrosim.Send_Cube(name="LeftLeg", pos=[-0.5, 0.0, 0.0], size=[1.0, 0.2, 0.2])
        pyrosim.Send_Joint( name = "Torso_LeftLeg" , parent= "Torso" , child = "LeftLeg" , type = "revolute", position = [-0.5, 0, 1.], jointAxis = "0 1 0")
        pyrosim.Send_Cube(name="RightLeg", pos=[0.5, 0.0, 0.0], size=[1.0, 0.2, 0.2])
        pyrosim.Send_Joint( name = "Torso_RightLeg" , parent= "Torso" , child = "RightLeg" , type = "revolute", position = [0.5, 0, 1.], jointAxis = "0 1 0")
        pyrosim.Send_Cube(name="FrontLowerLeg", pos=[0.0, 0.0, -.5], size=[.2, .2, 1.])
        pyrosim.Send_Joint( name = "FrontLeg_FrontLowerLeg" , parent= "FrontLeg" , child = "FrontLowerLeg" , type = "revolute", position = [0,1,0], jointAxis = "1 0 0")
        pyrosim.Send_Cube(name="BackLowerLeg", pos=[0.0, 0.0, -.5], size=[.2, .2, 1.])
        pyrosim.Send_Joint( name = "BackLeg_BackLowerLeg" , parent= "BackLeg" , child = "BackLowerLeg" , type = "revolute", position = [0,-1,0], jointAxis = "1 0 0")
        pyrosim.Send_Cube(name="LeftLowerLeg", pos=[0.0, 0.0, -.5], size=[.2, .2, 1.])
        pyrosim.Send_Joint( name = "LeftLeg_LeftLowerLeg" , parent= "LeftLeg" , child = "LeftLowerLeg" , type = "revolute", position = [-1,0,0], jointAxis = "0 1 0")
        pyrosim.Send_Cube(name="RightLowerLeg", pos=[0.0, 0.0, -.5], size=[.2, .2, 1.])
        pyrosim.Send_Joint( name = "RightLeg_RightLowerLeg" , parent= "RightLeg" , child = "RightLowerLeg" , type = "revolute", position = [1,0,0], jointAxis = "0 1 0")
        pyrosim.End()

    def generate_brain(self):
        pyrosim.Start_NeuralNetwork(f"brain{self.id}.nndf")

        # Neurons:
        # -Input
        n = 0
        # pyrosim.Send_Touch_Sensor_Neuron(name = n , linkName = "Torso"); n+=1
        # pyrosim.Send_Touch_Sensor_Neuron(name = n , linkName = "BackLeg"); n+=1
        # pyrosim.Send_Touch_Sensor_Neuron(name = n , linkName = "FrontLeg"); n+=1
        # pyrosim.Send_Touch_Sensor_Neuron(name = n , linkName = "LeftLeg"); n+=1
        # pyrosim.Send_Touch_Sensor_Neuron(name = n , linkName = "RightLeg"); n+=1
        pyrosim.Send_Rotation_Sensor_Neuron(name = n , linkName = "FrontLowerLeg"); n+=1
        pyrosim.Send_Rotation_Sensor_Neuron(name = n , linkName = "BackLowerLeg"); n+=1
        pyrosim.Send_Rotation_Sensor_Neuron(name = n , linkName = "LeftLowerLeg"); n+=1
        pyrosim.Send_Rotation_Sensor_Neuron(name = n , linkName = "RightLowerLeg"); n+=1

        # -Hidden
        ...

        # -Output
        pyrosim.Send_Motor_Neuron( name = n , jointName = "Torso_BackLeg"); n+=1
        pyrosim.Send_Motor_Neuron( name = n , jointName = "Torso_FrontLeg"); n+=1
        pyrosim.Send_Motor_Neuron( name = n , jointName = "Torso_LeftLeg"); n+=1
        pyrosim.Send_Motor_Neuron( name = n , jointName = "Torso_RightLeg"); n+=1
        pyrosim.Send_Motor_Neuron( name = n , jointName = "FrontLeg_FrontLowerLeg"); n+=1
        pyrosim.Send_Motor_Neuron( name = n , jointName = "BackLeg_BackLowerLeg"); n+=1
        pyrosim.Send_Motor_Neuron( name = n , jointName = "LeftLeg_LeftLowerLeg"); n+=1
        pyrosim.Send_Motor_Neuron( name = n , jointName = "RightLeg_RightLowerLeg"); n+=1

        # Synapses:
        # fully connected:
        for row in range(c.num_sensor_neurons):
            for col in range(c.num_motor_neurons):
                pyrosim.Send_Synapse(sourceNeuronName = row, targetNeuronName = col+c.num_sensor_neurons, weight = self.weights[row][col])

        pyrosim.End()

        while not os.path.exists(f"brain{self.id}.nndf"):
            time.sleep(0.01)

max_stars_repo_name: jacksonsdean/evolutionary-robotics-

max_issues_repo_name: jacksonsdean/evolutionary-robotics-

max_forks_repo_name: jacksonsdean/evolutionary-robotics-