text:
import pandas as pd
from tqdm import tqdm
from torchvision import transforms
from torch.utils.data import DataLoader, Dataset
from torchvision.datasets.folder import pil_loader
from common import *
import numpy as np

class MURA_Dataset(Dataset):
    def _select_study(self, study_name):
        studydf = self._fulldf[
            list(map(lambda x: self._fulldf.path.at[x].split('/')[2] == study_name, range(self._fulldf.path.size)))
        ]
        return studydf

    def _gen_tot_cnt(self):
        self.nt = {}
        self.at = {}
        self.wt1 = {}
        self.wt0 = {}
        self.sizes = {}
        for name in study_names:
            studydf = self._select_study(name)
            nt = studydf.label.value_counts().iloc[0]
            at = studydf.label.value_counts().iloc[1]
            self.sizes[name] = nt + at
            self.nt[name] = nt
            self.at[name] = at
            self.wt1[name] = (nt / (nt + at)).astype(np.float32)
            self.wt0[name] = (at / (nt + at)).astype(np.float32)

    def __init__(self, phase, study_name=None, data_dir='MURA-v1.0', transform=None):
        assert phase in phases
        if study_name:
            assert study_name in study_names

        self._fulldf = pd.read_csv(data_dir + '/' + phase + '.csv', names=['path', 'label'])
        self._fulldf.transform({'path': lambda x: x.str.replace(r'^[\w\-.\d_]+(?=/)', data_dir), 'label': lambda x: x})
        if study_name:
            self.df = self._select_study(study_name)
        else:
            self.df = self._fulldf
        self._gen_tot_cnt()
        self.transform = transform

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

    def __getitem__(self, item):
        imgPath = self.df.iloc[item, 0]
        image = pil_loader(imgPath)
        label = self.df.iloc[item, 1]
        if self.transform:
            image = self.transform(image)
        splitedPath = imgPath.split('/')
        study_name = splitedPath[2]
        imgnum = int(splitedPath[5][5:-4])
        jointDirPath = '/'.join(splitedPath[:-1])
        more = 0
        while item + more + 1 < len(self.df) and self.df.iloc[item + more + 1, 0].startswith(jointDirPath):
            more += 1

        metadata = {
            'study_name': study_name,
            'patient/study': splitedPath[3] + '/' + splitedPath[4],
            'total_img_num': imgnum + more,
            'nt': self.nt[study_name],
            'at': self.at[study_name],
            'dataset_size': self.sizes[study_name],
            'wt1': self.wt1[study_name],
            'wt0': self.wt0[study_name]
        }
        if label == 1:
            metadata['wt'] = metadata['wt1']
        else:
            metadata['wt'] = metadata['wt0']
        sample = {'image': image, 'label': label, 'metadata': metadata}
        return sample

class MURA_Study_Dataset(Dataset):

    def _firsts(self, df):
        def closure(s):
            return '/'.join(df.loc[s].iloc[0].split('/')[:-1])
        return closure

    def _select_study(self, study_name):
        studydf = self._fulldf[
            list(map(lambda x: self._fulldf.path.at[x].split('/')[2] == study_name, range(self._fulldf.path.size)))
        ]
        return studydf

    def _gen_tot_cnt(self):
        self.sizes = {}
        for name in study_names:
            studydf = self._select_study(name)
            self.sizes[name] = len(studydf.groupby(self._firsts(studydf)))

    def __init__(self, phase, study_name=None, data_dir='MURA-v1.0', transform=None):
        assert phase in phases
        if study_name:
            assert study_name in study_names

        self._fulldf = pd.read_csv(data_dir + '/' + phase + '.csv', names=['path', 'label'])
        self._fulldf.transform({'path': lambda x: x.str.replace(r'^[\w\-.\d_]+(?=/)', data_dir), 'label': lambda x: x})
        if study_name:
            self.df = self._select_study(study_name)
        else:
            self.df = self._fulldf

        self._gen_tot_cnt()

        self.gb = list(self.df.groupby(self._firsts(self.df)))
        self.transform = transform

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

    def __getitem__(self, item):
        _, df = self.gb[item]
        label = df.iloc[0,1]
        images = []

        splitedPath = None

        for idx in range(len(df)):
            imgPath = df.iloc[idx, 0]
            image = pil_loader(imgPath)
            if self.transform:
                image = self.transform(image)
            images.append(image)
            splitedPath = imgPath.split('/')

        assert splitedPath
        study_name =  splitedPath[2]

        # -1 means no data

        metadata = {
            'study_name': study_name,
            'patient/study': splitedPath[3] + '/' + splitedPath[4],
            'total_img_num': -1,
            'nt': -1,
            'at': -1,
            'dataset_size': self.sizes[study_name],
            'wt1': -1,
            'wt0': -1
        }

        sample = {'images': {imglabel: torch.stack([img[imglabel] for img in images]) for imglabel in ['orig','norm']}, 'label': label, 'metadata': metadata}
        return sample

def get_dataloaders(study_name=None, data_dir='MURA-v1.0', batch_size=8, batch_eval_ten=12, shuffle=True):
    data_transforms = {
        'train': transforms.Compose([
            transforms.Resize((320, 320)),
            transforms.RandomResizedCrop(224),
            transforms.RandomHorizontalFlip(),
            transforms.RandomRotation(20),
            transforms.ToTensor(),
            transforms.Lambda(lambda t:{
                'orig':t,
                'norm':transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])(t)}
            )
        ]),
        'valid': transforms.Compose([
            transforms.Resize((320, 320)),
            transforms.CenterCrop(224),
            #transforms.Resize((224,224)),
            transforms.ToTensor(),
            transforms.Lambda(lambda t: {
                'orig': t,
                'norm': transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])(t)}
            )
        ]),
        'valid_tencrop': transforms.Compose([
            transforms.Resize((320, 320)),
            transforms.TenCrop(224),
            # transforms.Resize((224,224)),
            transforms.Lambda(
                lambda crops: {
                    'orig': torch.stack([transforms.ToTensor()(crop) for crop in crops]),
                    'norm': torch.stack([
                    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])(
                        transforms.ToTensor()(crop)
                    )
                    for crop in crops
                ])}
            )
            #transforms.ToTensor(),
            #transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
        ])
    }
    image_datasets = {x: MURA_Dataset(x, study_name, data_dir, data_transforms[x]) for x in phases}
    dataloader = \
        {x: DataLoader(image_datasets[x], batch_size=batch_size, shuffle=shuffle, num_workers=32) for x in phases}
    dataset_sizes = {x: len(image_datasets[x]) for x in phases}

    # can only be trained on Titan Xp or GPUs with enough graph memory
    image_datasets_valid_tencrop = MURA_Dataset('valid', study_name, data_dir, data_transforms['valid_tencrop'])
    dataloader['valid_tencrop'] = DataLoader(image_datasets_valid_tencrop, batch_size=batch_eval_ten, shuffle=shuffle, num_workers=32)
    dataset_sizes['valid_tencrop'] = dataset_sizes['valid']

    image_study_datasets = MURA_Study_Dataset('valid', study_name, data_dir, data_transforms['valid'])
    dataloader['valid_study'] = DataLoader(image_study_datasets, batch_size=1, shuffle=False, num_workers=32)
    dataset_sizes['valid_study'] = len(image_study_datasets)

    return dataloader, dataset_sizes

def main():
    dataloaders, dataset_sizes = get_dataloaders(None, batch_size=8)
    for i, data in enumerate(tqdm(dataloaders['train'])):
        j = 0

if __name__ == '__main__':
    main()

max_stars_repo_name: aDobeZ/MURA-Model

max_issues_repo_name: aDobeZ/MURA-Model

max_forks_repo_name: aDobeZ/MURA-Model


text:
# Copyright © 2019 National Institute of Advanced Industrial Science and Technology （AIST）. All rights reserved.
# !/usr/bin/env python3.6
# coding=utf-8

import keras.backend as K
import numpy as np

class ACCCalculator:
    """
    精度を計算する

    accuracy calculator
    """

    def normalize_y_pred(self, y_pred):
        return K.one_hot(K.argmax(y_pred), y_pred.shape[-1])

    def class_true_positive(self, class_label, y_true, y_pred):
        y_pred = self.normalize_y_pred(y_pred)
        return K.cast(K.equal(y_true[:, class_label] + y_pred[:, class_label], 2),
                    K.floatx())

    def true_positive(self, y_true, y_pred):
        y_pred = self.normalize_y_pred(y_pred)
        return K.cast(K.equal(y_true + y_pred, 2),
                    K.floatx())

    def class_precision(self, class_label, y_true, y_pred):
        y_pred = self.normalize_y_pred(y_pred)
        return K.sum(self.class_true_positive(class_label, y_true, y_pred)) / (K.sum(y_pred[:, class_label]) + K.epsilon())

    def micro_precision(self, y_true, y_pred):
        y_pred = self.normalize_y_pred(y_pred)
        return K.sum(self.true_positive(y_true, y_pred)) / (K.sum(y_pred) + K.epsilon())

    def all_class_precision(self, y_true, y_pred):
        return [self.class_precision(i, y_true, y_pred) for i in range(y_pred.shape[-1])]

    def macro_precision(self, y_true, y_pred):
        class_count = y_pred.shape[-1]
        return K.sum([self.class_precision(i, y_true, y_pred) for i in range(class_count)]) \
            / K.cast(class_count, K.floatx())

    def class_recall(self, class_label, y_true, y_pred):
        return K.sum(self.class_true_positive(class_label, y_true, y_pred)) / (K.sum(y_true[:, class_label]) + K.epsilon())

    def micro_recall(self, y_true, y_pred):
        return K.sum(self.true_positive(y_true, y_pred)) / (K.sum(y_true) + K.epsilon())

    def all_class_recall(self, y_true, y_pred):
        return [self.class_recall(i, y_true, y_pred) for i in range(y_pred.shape[-1])]

    def macro_recall(self, y_true, y_pred):
        class_count = y_pred.shape[-1]
        return K.sum([self.class_recall(i, y_true, y_pred) for i in range(class_count)]) \
            / K.cast(class_count, K.floatx())

    def class_accuracy(self, class_label, y_true, y_pred):
        y_pred = self.normalize_y_pred(y_pred)
        return K.cast(K.equal(y_true[:, class_label], y_pred[:, class_label]),
                    K.floatx())

    def all_class_accuracy(self, y_true, y_pred):
        return [np.mean(self.class_accuracy(i, y_true, y_pred)) for i in range(y_pred.shape[-1])]

    def average_accuracy(self, y_true, y_pred):
        class_count = y_pred.shape[-1]
        class_acc_list = [self.class_accuracy(i, y_true, y_pred) for i in range(class_count)]
        class_acc_matrix = K.concatenate(class_acc_list, axis=0)
        return K.mean(class_acc_matrix, axis=0)

    def class_f_measure(self, class_label, y_true, y_pred):
        precision = self.class_precision(class_label, y_true, y_pred)
        recall = self.class_recall(class_label, y_true, y_pred)
        return (2 * precision * recall) / (precision + recall + K.epsilon())

    def all_class_f_measure(self, y_true, y_pred):
        return [self.class_f_measure(i, y_true, y_pred) for i in range(y_pred.shape[-1])]

    def micro_f_measure(self, y_true, y_pred):
        precision = self.micro_precision(y_true, y_pred)
        recall = self.micro_recall(y_true, y_pred)
        return (2 * precision * recall) / (precision + recall + K.epsilon())

    def macro_f_measure(self, y_true, y_pred):
        precision = self.macro_precision(y_true, y_pred)
        recall = self.macro_recall(y_true, y_pred)
        return (2 * precision * recall) / (precision + recall + K.epsilon())

ext: py

max_issues_repo_name: aistairc/qunomon

reason: import numpy


text:
# Import the required files

import os, random, argparse
from PIL import Image
import imghdr
import numpy as np

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

def Average(image):  # function to get the image width, heidht and dimension.

    im = np.array(image) # to get array of the image
    width,height,dimension = im.shape #shape of the image

    return tuple(np.average(im.reshape(width*height, dimension), axis=0)) # get average value

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

def Split(image, size): # function to get row*column list of image

    Wid, Hei = image.size[0],image.size[1]
    row, column = size
    w, h =int(Wid/column), int(Hei/row)

    img = [] # list of images

    for j in range(row): # generate list dimension
        for i in range(column):
            img.append(image.crop((i*w, j*h, (i+1)*w, (j+1)*h))) #append the croped image
    return img

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

def Get(image_dir): # function to get list of image

    file = os.listdir(image_dir)
    images = [] # image list

    for i in file: # path to the files
        filepath = os.path.abspath(os.path.join(image_dir, i))

        try:
            f = open(filepath, 'rb') # opening the file
            im = Image.open(f) # opening hte image
            images.append(im)
            im.load()  #loading the image from file
            f.close  # closing the file
        except:
            print("Invalid Image" , filepath)

    return images

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

def Filename(image_dir): # function to get list of image file names

    file = os.listdir(image_dir)
    f_names = [] # ilst of file names

    for i in file: # path to the file
        filepath = os.path.abspath(os.path.join(image_dir, i))

        try:
            img_Type = imghdr.what(filepath)
            if img_Type:
                f_names.append(filepath)
        except:
            print("Invalid Image", filepath)

    return f_names

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

def Match(i_avg, avgs): # function to get the image index to place the image

    avg = i_avg # average input

    ind = 0
    min_ind = 0
    min_dist = float("inf")

    for i in avgs:
        dist = ((i[0] - avg[0])*(i[0] - avg[0])+(i[1] - avg[1])*(i[1] - avg[1]) +
                            (i[2] - avg[2])*(i[2] - avg[2]))

        if dist < min_dist: #caluclate min distance
            min_dist = dist;
            min_ind = ind
            ind+=1

    return min_ind

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

def Create(images, dims): # function to create grid for easy fitting images

    row, column = dims

    assert row*column == len(images)

    width = max([img.size[0] for img in images]) # maimum width
    height = max([img.size[1] for img in images]) # maimum height

    grid_img = Image.new('RGB', (column*width, row*height)) # formating output image

    for i in range(len(images)): # Fitting the images in the grid
            row = int(i/column)
            col = i - column*row
            grid_img.paste(images[i], (col*width, row*height))

    return grid_img

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

def Mosaic(t_img, inp_img, grid_size, reuse=True): #Functtion to create photomosaic

    t_img = Split(t_img, grid_size) # Split function is used
    out_img = [] #output images

    c = 0
    b_size = int(len(t_img)/10)

    avgs = []
    for img in inp_img:
        avgs.append(Average(img))

    for img in t_img:
        avg = Average(img)

        match_index = Match(avg, avgs) # to find the matching index
        out_img.append(inp_img[match_index])

        if c > 0 and b_size > 10 and count % b_size is 0:
            c += 1
        if not reuse: #if flag set remove selected image
            inp_img.remove(match)

    mosaic_image = Create(out_img, grid_size)
    return mosaic_image

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

def main():

    # parser arguments
    parser = argparse.ArgumentParser(description='Creates a photomosaic from input images')
    parser.add_argument('--target-image', dest='target_image', required=True)
    parser.add_argument('--input-folder', dest='input_folder', required=True)
    parser.add_argument('--grid-size', nargs=2, dest='grid_size', required=True)
    parser.add_argument('--output-file', dest='outfile', required=False)

    args = parser.parse_args()

    target_image = Image.open(args.target_image) # target the image

    input_image = Get(args.input_folder) # input image

    if input_image == []:
        print('No input images found in %s. Exiting.' % (args.input_folder, ))
        exit()

    random.shuffle(input_image)

    grid_size = (int(args.grid_size[0]), int(args.grid_size[1])) #Grid size

    output_filename = 'mosaic.png' # output image
    if args.outfile:
        output_filename = args.outfile

    reuse_images = True
    resize_input = True

    if not reuse_images:
        if grid_size[0]*grid_size[1] > len(input_image):
            exit()

    if resize_input:
        dims = (int(target_image.size[0]/grid_size[1]),int(target_image.size[1]/grid_size[0]))

    for img in input_image:
        img.thumbnail(dims)

    # creating mosaic images
    mosaic_image = Mosaic(target_image, input_image, grid_size, reuse_images)

    mosaic_image.save(output_filename, 'PNG')

    # user understanding message
    print("saved output to %s" % (output_filename,))
    print('done.')

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

if __name__ == '__main__':
    main()  # call the main program


text:
"""
Plots for numerous reasons
"""
import os

import numpy as np

import pickle
import pandas

import matplotlib.pyplot as plt
from matplotlib.colors import ListedColormap

from pyrcn.util import tud_colors

directory = 'plots'

def f_x(x, sigma=1, mu=0.):
    return np.multiply(np.divide(1/np.sqrt(2*np.pi), sigma), np.exp(-.5 * np.power(np.divide((x - mu), sigma), 2)))

def f_y(y, sigma=1, mu=0.):
    return np.divide(f_x(np.arctanh(y), sigma, mu), (1 - np.power(y, 2)))

def save_line2d_data(lines: [plt.Line2D], filepath: str):
    data = []
    header = []
    fmt = []
    for line in lines:
        header.append('x({0})'.format(line.get_label()))
        data.append(line.get_xdata())
        fmt.append('%f')
        header.append('y({0})'.format(line.get_label()))
        data.append(line.get_ydata())
        fmt.append('%f')

    # noinspection PyTypeChecker
    np.savetxt(
        fname=filepath,
        X=np.array(data).T,
        fmt=','.join(fmt),
        header=','.join(header),
        comments=''
    )
    return

def plot_activation_variance():
    x = np.linspace(-3, 3, 1000)
    y = np.linspace(-.9999, .9999, 1000)

    sigma = np.array([.5, .75, 1.])
    mu = np.array([0.])

    fx = np.zeros((len(sigma), len(x)))
    fy = np.zeros((len(sigma), len(y)))

    fig = plt.figure(figsize=(4, 2))
    ax = plt.axes()
    ax.set_xlim((-2, 2))
    ax.set_ylim((0, 1))
    lines = []

    for s in sigma:
        lines += ax.plot(x, f_x(x, sigma=s, mu=mu[0]), color=tud_colors['gray'], linewidth=1.2, alpha=.5, label='fx;sigma={0}'.format(s))
        lines += ax.plot(y, f_y(y, sigma=s, mu=mu[0]), color=tud_colors['lightblue'], linewidth=1.2, label='fy;sigma={0}'.format(s))

    lines[0].set_linestyle('--')
    lines[1].set_linestyle('--')
    lines[2].set_linestyle('-.')
    lines[3].set_linestyle('-.')
    lines[4].set_linestyle(':')
    lines[5].set_linestyle(':')

    ax.legend(lines, ('$f_x, \sigma = .5$', '$f_y, \sigma = .5$', '$f_x, \sigma = .75$', '$f_y, \sigma = .75$','$f_x, \sigma = 1.$', '$f_y, \sigma = 1.$'), bbox_to_anchor=(1, .5), loc="center left")
    fig.tight_layout()
    fig.savefig(os.path.join(os.environ['PGFPATH'], 'plot-distribution-sigma.pgf'), format='pgf')
    fig.savefig(os.path.join(directory, 'plot-distribution-sigma.pdf'), format='pdf')

    save_line2d_data(lines, os.path.join(directory, 'plot-distribution-sigma.csv'))
    return

def plot_activation_mean():
    x = np.linspace(-3, 3, 1000)
    y = np.linspace(-.9999, .9999, 1000)

    sigma = np.array([.5])
    mu = np.array([0., -.25, -.5])

    fx = np.zeros((len(sigma), len(x)))
    fy = np.zeros((len(sigma), len(y)))

    fig = plt.figure(figsize=(4, 2))
    ax = plt.axes()
    ax.set_xlim((-2, 2))
    ax.set_ylim((0, 1.25))
    lines = []

    for m in mu:
        lines += ax.plot(x, f_x(x, sigma=sigma[0], mu=m), color=tud_colors['gray'], linewidth=1.2, alpha=.5, label='fx;mean={0}'.format(m))
        lines += ax.plot(y, f_y(y, sigma=sigma[0], mu=m), color=tud_colors['lightblue'], linewidth=1.2, label='fy;mean={0}'.format(m))

    lines[0].set_linestyle('--')
    lines[1].set_linestyle('--')
    lines[2].set_linestyle('-.')
    lines[3].set_linestyle('-.')
    lines[4].set_linestyle(':')
    lines[5].set_linestyle(':')

    ax.legend(lines, ('$f_x, \mu = 0$', '$f_y, \mu = 0$', '$f_x, \mu = -.25$', '$f_y, \mu = -.25$','$f_x, \mu = -.5$', '$f_y, \mu = -.5$'), bbox_to_anchor=(1, .5), loc="center left")
    fig.tight_layout()
    fig.savefig(os.path.join(os.environ['PGFPATH'], 'plot-distribution-mean.pgf'), format='pgf')
    fig.savefig(os.path.join(directory, 'plot-distribution-mean.pdf'), format='pdf')

    save_line2d_data(lines, os.path.join(directory, 'plot-distribution-mean.csv'))
    return

def plot_ridge():
    rs = np.random.RandomState(1824)

    x = np.linspace(0, 2*np.pi, 1000)
    y = np.sin(x)
    y_noise = y + rs.normal(0., .2, size=x.shape)
    choice = rs.choice(x.size, size=20)
    # choice = np.arange(0, x.size, 75)
    x_sampled = x[choice]
    y_sampled = y[choice]
    y_sampled_noise = y_noise[choice]

    alpha = [0, 1., 100.]

    h = np.array([np.power(x, exponent).T for exponent in range(10)]).T

    fig = plt.figure(figsize=(4, 2))
    ax = plt.axes()
    ax.set_xlim((np.min(x), np.max(x)))
    ax.set_ylim((-1.4, 1.4))
    lines = []

    lines += ax.plot(x, y, label='sin', linestyle='-', color=tud_colors['lightblue'], alpha=.5)

    colors = [tud_colors['gray'], tud_colors['lightgreen'], tud_colors['gray']]
    alphas = [.4, 1., .4]
    linestyles = [':', '-.', '--']

    for a in alpha:
        coefs = np.dot(np.linalg.inv(np.dot(h[choice, :].T, h[choice, :]) + a * np.eye(h.shape[1])), np.dot(h[choice].T, y_noise[choice]))
        y_regression = np.dot(h, coefs)
        lines += ax.plot(x, y_regression, label='lda={0}'.format(a), color=colors.pop(0), alpha=alphas.pop(0), linestyle=linestyles.pop(0))

    lines += ax.plot(x_sampled, y_sampled_noise, label='samples', marker='x', linestyle='None', color=tud_colors['lightblue'], alpha=.3)
    ax.set_xticks([0, np.pi, 2*np.pi])
    ax.set_xticklabels(['$0$', '$\pi$', '$2\pi$'])

    ax.legend(bbox_to_anchor=(1, .5), loc='center left')
    fig.tight_layout()
    fig.savefig(os.path.join(os.environ['PGFPATH'], 'plot-ridge.pgf'), format='pgf')  # pgf
    fig.savefig(os.path.join(directory, 'plot-ridge.pdf'), format='pdf')  # pgf

    # save_line2d_data(lines, os.path.join(directory, 'plot-ridge.csv'))
    return

def plot_pca():
    filepath = os.path.abspath('./experiments/elm_pca.csv')
    df = pandas.read_csv(filepath)
    df.sort_values(by='pca_n_components', inplace=True)

    xticks = [10, 20, 50, 100, 200, 500, 784]
    error_yticks = [2e-2, 3e-2, 4e-2, 5e-2]
    ratio_yticks = [.50, .60, .70, .80, .90, 1.00]

    fig = plt.figure(figsize=(5, 3))

    # add grid axis
    ax_error = fig.add_subplot(
        111,
        xscale='log',
        xticks=xticks,
        xticklabels=['{0:.0f}'.format(xtick) for xtick in xticks],
        xlim=(10, 784),
        xlabel='\#components'
    )

    # disable patch
    ax_error.patch.set_visible(False)

    # clone x-axis (the retarded matplotlib-way)
    ax_ratio = ax_error.twinx()

    # plot
    lines = []

    lines += ax_error.plot(
        df['pca_n_components'],
        1 - df['score'],
        color=tud_colors['lightblue'],
        label='test set error rate'
    )

    ax_error.set_yticks(error_yticks)
    ax_error.set_yticklabels(['{0:.1f}%'.format(ytick * 100) for ytick in error_yticks])
    ax_error.set_ylabel('error rate [%]')
    ax_error.set_ylim([2e-2, 5e-2])

    lines += ax_ratio.plot(
        df['pca_n_components'],
        df['pca_explained_variance_ratio'],
        color=tud_colors['gray'],
        label='explained variance ratio',
        linestyle='dashed'
    )

    ax_ratio.set_yticks(ratio_yticks)
    ax_ratio.set_yticklabels(['{0:.1f}%'.format(ytick * 100) for ytick in ratio_yticks])
    ax_ratio.set_ylabel('explained variance ratio [%]')
    ax_ratio.set_ylim([.50, 1.00])

    # grid in background
    ax_error.grid(which='both', axis='both', alpha=.7)

    legend = ax_error.legend(lines, [line._label for line in lines], loc='lower right')  # , bbox_to_anchor=[1., .5]
    fig.tight_layout()

    filename = 'plot_pca'
    fig.savefig(os.path.join('./plots/', '{0}.pdf'.format(filename)), format='pdf')
    fig.savefig(os.path.join(os.environ['PGFPATH'], '{0}.pgf'.format(filename)), format='pgf')
    return

def plot_hyperparameters():
    filepath = os.path.join('./mnist-elm', 'elm_basic.csv')
    df = pandas.read_csv(filepath, sep=',')
    df_tanh2000 = df[
        (df['param_input_to_nodes__activation'] == 'tanh') & (df['param_input_to_nodes__hidden_layer_size'] == 2000)
    ].sort_values(by=['param_input_to_nodes__bias_scaling', 'param_input_to_nodes__input_scaling'], axis=0, ascending=[False, True])
    df_relu500 = df[
        (df['param_input_to_nodes__activation'] == 'relu') & (df['param_input_to_nodes__hidden_layer_size'] == 500)
    ].sort_values(by=['param_input_to_nodes__bias_scaling', 'param_input_to_nodes__input_scaling'], axis=0, ascending=[False, True])
    df_relu2000 = df[
        (df['param_input_to_nodes__activation'] == 'relu') & (df['param_input_to_nodes__hidden_layer_size'] == 2000)
    ].sort_values(by=['param_input_to_nodes__bias_scaling', 'param_input_to_nodes__input_scaling'], axis=0, ascending=[False, True])

    filepath = os.path.join('./mnist-elm', 'elm_preprocessed.csv')
    df = pandas.read_csv(filepath, sep=',')
    df_tanh2000pca = df[
        (df['param_input_to_nodes__activation'] == 'tanh') & (df['param_input_to_nodes__hidden_layer_size'] == 2000)
    ].sort_values(by=['param_input_to_nodes__bias_scaling', 'param_input_to_nodes__input_scaling'], axis=0, ascending=[False, True])
    df_relu500pca = df[
        (df['param_input_to_nodes__activation'] == 'relu') & (df['param_input_to_nodes__hidden_layer_size'] == 500)
    ].sort_values(by=['param_input_to_nodes__bias_scaling', 'param_input_to_nodes__input_scaling'], axis=0, ascending=[False, True])
    df_relu2000pca = df[
        (df['param_input_to_nodes__activation'] == 'relu') & (df['param_input_to_nodes__hidden_layer_size'] == 2000)
    ].sort_values(by=['param_input_to_nodes__bias_scaling', 'param_input_to_nodes__input_scaling'], axis=0, ascending=[False, True])

    n_rows = df_tanh2000.shape[0]

    fig, axs = plt.subplots(nrows=2, ncols=3, figsize=(6., 4.), gridspec_kw={'hspace': 1.1, 'wspace': 1.1, 'left': .1, 'right': .95, 'bottom': .18, 'top': .92})  # , subplot_kw={'projection': '3d'})

    df_tanh2000.attrs.update({'titlestr': 'tanh\n$m=2000$'})
    df_relu500.attrs.update({'titlestr': 'ReLU\n$m=500$'})
    df_relu2000.attrs.update({'titlestr': 'ReLU\n$m=2000$'})
    df_tanh2000pca.attrs.update({'titlestr': 'PCA50, tanh\n$m=2000$'})
    df_relu500pca.attrs.update({'titlestr': 'PCA50, ReLU\n$m=500$'})
    df_relu2000pca.attrs.update({'titlestr': 'PCA50, ReLU\n$m=2000$'})

    df_dict = {
        0: df_tanh2000,
        1: df_relu500,
        2: df_relu2000,
        3: df_tanh2000pca,
        4: df_relu500pca,
        5: df_relu2000pca
    }

    # colormap
    # cm = ListedColormap(np.linspace(start=tud_colors['red'], stop=tud_colors['lightgreen'], num=255))
    n_upper = 20
    color_array = np.zeros((255, 4))
    color_array[: 255 - n_upper, :] += np.linspace(start=tud_colors['red'], stop=(1., 1., 1., 1.), num=255 - n_upper)
    color_array[255 - n_upper:, :] += np.linspace(start=(1., 1., 1., 1.), stop=tud_colors['darkgreen'], num=n_upper)
    cm = ListedColormap(color_array)

    for row in range(axs.shape[0]):
        for col in range(axs.shape[1]):
            df_loop = df_dict[row*3 + col]
            ax = axs[row][col]
            # ax = axs[col][row]

            X_ticks = np.sort(df_loop['param_input_to_nodes__input_scaling'].unique())  # ascending
            Y_ticks = np.sort(df_loop['param_input_to_nodes__bias_scaling'].unique())[::-1]  # descending

            mesh_shape = (len(X_ticks), len(Y_ticks))

            Z_value = df_loop['mean_test_score'].values.reshape(mesh_shape)*100
            # norm = Normalize(vmin=np.mean(Z_value), clip=True) # -np.std(Z_value)

            # surf = ax.plot_surface(
            im = ax.imshow(
                # np.log10(df_loop['param_input_to_nodes__bias_scaling'].values.reshape(mesh_shape)),
                # np.log10(df_loop['param_input_to_nodes__input_scaling'].values.reshape(mesh_shape)),
                Z_value,
                cmap=cm,  # matplotlib.cm.coolwarm
                # norm=norm
                interpolation='none'
            )

            fig.colorbar(im, ax=ax, use_gridspec=True, shrink=.9)  #spacing='proportional')

            # ax.set_xticks(np.log10(X_ticks))
            ax.set_xticks(range(mesh_shape[0]))
            ax.set_xticklabels(['{0:3.3f}'.format(x) for x in X_ticks])
            ax.tick_params(axis='x', labelrotation=90)
            ax.set_xlabel('input scaling')

            # ax.set_yticks(np.log10(Y_ticks))
            ax.set_yticks(range(mesh_shape[1]))
            ax.set_yticklabels(['{0:0.3f}'.format(y) for y in Y_ticks])
            ax.set_ylabel('bias scaling')

            # annotate
            y = np.argmax(Z_value) // len(Z_value)
            x = np.argmax(Z_value) % len(Z_value)
            ax.annotate('{0:0.1f}%'.format(np.max(Z_value)), xy=(x, y), c=(0., 0., 0., .7), horizontalalignment='center', verticalalignment='center', fontsize='xx-small', fontstretch='ultra-condensed')

            ax.set_title(df_loop.attrs['titlestr'])

    # fig.colorbar(surf, shrink=0.5, aspect=5)
    # fig.tight_layout()
    fig.savefig(os.path.join(directory, 'hyperparameter-relu-tanh.pdf'), format='pdf')
    fig.savefig(os.path.join(os.environ['PGFPATH'], 'hyperparameter-relu-tanh.pgf'), format='pgf')  # pgf
    #plt.show()

def plot_hyperparameters_poster():
    filepath = os.path.join('./mnist-elm', 'elm_basic.csv')
    df = pandas.read_csv(filepath, sep=',')
    df_tanh2000 = df[
        (df['param_input_to_nodes__activation'] == 'tanh') & (df['param_input_to_nodes__hidden_layer_size'] == 2000)
    ].sort_values(by=['param_input_to_nodes__bias_scaling', 'param_input_to_nodes__input_scaling'], axis=0, ascending=[False, True])
    df_relu500 = df[
        (df['param_input_to_nodes__activation'] == 'relu') & (df['param_input_to_nodes__hidden_layer_size'] == 500)
    ].sort_values(by=['param_input_to_nodes__bias_scaling', 'param_input_to_nodes__input_scaling'], axis=0, ascending=[False, True])
    df_relu2000 = df[
        (df['param_input_to_nodes__activation'] == 'relu') & (df['param_input_to_nodes__hidden_layer_size'] == 2000)
    ].sort_values(by=['param_input_to_nodes__bias_scaling', 'param_input_to_nodes__input_scaling'], axis=0, ascending=[False, True])

    filepath = os.path.join('./mnist-elm', 'elm_preprocessed.csv')
    df = pandas.read_csv(filepath, sep=',')
    df_tanh2000pca = df[
        (df['param_input_to_nodes__activation'] == 'tanh') & (df['param_input_to_nodes__hidden_layer_size'] == 2000)
    ].sort_values(by=['param_input_to_nodes__bias_scaling', 'param_input_to_nodes__input_scaling'], axis=0, ascending=[False, True])
    df_relu500pca = df[
        (df['param_input_to_nodes__activation'] == 'relu') & (df['param_input_to_nodes__hidden_layer_size'] == 500)
    ].sort_values(by=['param_input_to_nodes__bias_scaling', 'param_input_to_nodes__input_scaling'], axis=0, ascending=[False, True])
    df_relu2000pca = df[
        (df['param_input_to_nodes__activation'] == 'relu') & (df['param_input_to_nodes__hidden_layer_size'] == 2000)
    ].sort_values(by=['param_input_to_nodes__bias_scaling', 'param_input_to_nodes__input_scaling'], axis=0, ascending=[False, True])

    n_rows = df_tanh2000.shape[0]

    fig, axs = plt.subplots(nrows=1, ncols=2, figsize=(4., 2.2), gridspec_kw={'hspace': 1.1, 'wspace': 1.1, 'left': .15, 'right': .95, 'bottom': .25, 'top': .92})  # , subplot_kw={'projection': '3d'})
    axs.resize((1, 2))

    df_tanh2000.attrs.update({'titlestr': 'tanh\n$m=2000$'})
    df_relu500.attrs.update({'titlestr': 'ReLU\n$m=500$'})
    df_relu2000.attrs.update({'titlestr': 'ReLU\n$m=2000$'})
    df_tanh2000pca.attrs.update({'titlestr': 'PCA50, tanh\n$m=2000$'})
    df_relu500pca.attrs.update({'titlestr': 'PCA50, ReLU\n$m=500$'})
    df_relu2000pca.attrs.update({'titlestr': 'PCA50, ReLU\n$m=2000$'})

    df_dict = {
        0: df_tanh2000pca,
        1: df_relu2000pca
    }

    # colormap
    # cm = ListedColormap(np.linspace(start=tud_colors['red'], stop=tud_colors['lightgreen'], num=255))
    n_upper = 20
    color_array = np.zeros((255, 4))
    color_array[: 255 - n_upper, :] += np.linspace(start=tud_colors['red'], stop=(1., 1., 1., 1.), num=255 - n_upper)
    color_array[255 - n_upper:, :] += np.linspace(start=(1., 1., 1., 1.), stop=tud_colors['darkgreen'], num=n_upper)
    cm = ListedColormap(color_array)

    for row in range(axs.shape[0]):
        for col in range(axs.shape[1]):
            df_loop = df_dict[col]
            ax = axs[row][col]
            # ax = axs[col][row]

            X_ticks = np.sort(df_loop['param_input_to_nodes__input_scaling'].unique())  # ascending
            Y_ticks = np.sort(df_loop['param_input_to_nodes__bias_scaling'].unique())[::-1]  # descending

            mesh_shape = (len(X_ticks), len(Y_ticks))

            Z_value = df_loop['mean_test_score'].values.reshape(mesh_shape)*100
            # norm = Normalize(vmin=np.mean(Z_value), clip=True) # -np.std(Z_value)

            # surf = ax.plot_surface(
            im = ax.imshow(
                # np.log10(df_loop['param_input_to_nodes__bias_scaling'].values.reshape(mesh_shape)),
                # np.log10(df_loop['param_input_to_nodes__input_scaling'].values.reshape(mesh_shape)),
                Z_value,
                cmap=cm,  # matplotlib.cm.coolwarm
                # norm=norm
                interpolation='none'
            )

            fig.colorbar(im, ax=ax, use_gridspec=True, shrink=.5)  #spacing='proportional')

            # ax.set_xticks(np.log10(X_ticks))
            ax.set_xticks(range(mesh_shape[0]))
            ax.set_xticklabels(['{0:3.3f}'.format(x) for x in X_ticks])
            ax.tick_params(axis='x', labelrotation=90)
            ax.set_xlabel('input scaling')

            # ax.set_yticks(np.log10(Y_ticks))
            ax.set_yticks(range(mesh_shape[1]))
            ax.set_yticklabels(['{0:0.3f}'.format(y) for y in Y_ticks])
            ax.set_ylabel('bias scaling')

            # annotate
            y = np.argmax(Z_value) // len(Z_value)
            x = np.argmax(Z_value) % len(Z_value)
            ax.annotate('{0:0.1f}%'.format(np.max(Z_value)), xy=(x, y), c=(0., 0., 0., .7), horizontalalignment='center', verticalalignment='center', fontsize='xx-small', fontstretch='ultra-condensed')

            ax.set_title(df_loop.attrs['titlestr'])

    # fig.colorbar(surf, shrink=0.5, aspect=5)
    # fig.tight_layout()
    fig.savefig(os.path.join(directory, 'hyperparameter-poster.pdf'), format='pdf')
    fig.savefig(os.path.join(os.environ['PGFPATH'], 'hyperparameter-poster.pgf'), format='pgf')  # pgf
    #plt.show()

def plot_preprocessed():
    filepath = os.path.join('./mnist-elm', 'elm_preprocessed_relu.csv')
    df = pandas.read_csv(filepath, sep=',')
    df_tanh2000 = df[
        (df['param_input_to_nodes__activation'] == 'relu') & (df['param_input_to_nodes__hidden_layer_size'] == 2000)
    ].sort_values(by=['param_input_to_nodes__bias_scaling', 'param_input_to_nodes__input_scaling'], axis=0, ascending=[False, True])

    fig, axs = plt.subplots(nrows=1, ncols=1, figsize=(3., 3.))  # , subplot_kw={'projection': '3d'})

    df_dict = {
        0: df_tanh2000,
    }

    # colormap
    # cm = ListedColormap(np.linspace(start=tud_colors['red'], stop=tud_colors['lightgreen'], num=255))
    n_upper = 20
    color_array = np.zeros((255, 4))
    color_array[: 255 - n_upper, :] += np.linspace(start=tud_colors['red'], stop=(1., 1., 1., 1.), num=255 - n_upper)
    color_array[255 - n_upper:, :] += np.linspace(start=(1., 1., 1., 1.), stop=tud_colors['darkgreen'], num=n_upper)
    cm = ListedColormap(color_array)

    df_loop = df_tanh2000
    ax = axs

    X_ticks = np.sort(df_loop['param_input_to_nodes__input_scaling'].unique())  # ascending
    Y_ticks = np.sort(df_loop['param_input_to_nodes__bias_scaling'].unique())[::-1]  # descending

    mesh_shape = (len(X_ticks), len(Y_ticks))

    Z_value = df_loop['mean_test_score'].values.reshape(mesh_shape)*100

    im = ax.imshow(
        Z_value,
        cmap=cm
    )

    fig.colorbar(im, ax=ax, use_gridspec=True, spacing='proportional')

    ax.set_xticks(range(mesh_shape[0]))
    ax.set_xticklabels(['{0:0.3f}'.format(x) for x in X_ticks])
    ax.tick_params(axis='x', labelrotation=90)
    ax.set_xlabel('input scaling')

    ax.set_yticks(range(mesh_shape[1]))
    ax.set_yticklabels(['{0:0.3f}'.format(y) for y in Y_ticks])
    ax.set_ylabel('bias scaling')

    # annotate
    y = np.argmax(Z_value) // len(Z_value)
    x = np.argmax(Z_value) % len(Z_value)
    ax.annotate('{0:0.1f}%'.format(np.max(Z_value)), xy=(x, y), c=(1., 1., 1., 1.), horizontalalignment='center', fontsize='small', fontstretch='ultra-condensed')

    ax.set_title('relu, $m=2000$\n450 features')

    # fig.colorbar(surf, shrink=0.5, aspect=5)
    fig.tight_layout()
    fig.savefig('./plots/elm_preprocessed_relu-compare.pdf', format='pdf')
    plt.show()

def plot_hidden_layer_size():
    df = pandas.read_excel(os.path.join(os.environ['DATAPATH'], 'elm_hidden_layer_size_pca.xlsx'), sheet_name='pca-kmeans-results')

    list_data = [
        {
            'name': 'BP (Chazal et. al.)',
            'x': df.iloc[df[df.type == 'BP ref'].index].hls.values,
            'y': df.iloc[df[df.type == 'BP ref'].index].error_rate.values,
            'color': tud_colors['gray'],
            'linestyle': 'dashed',
        },
        {
            'name': 'ELM (Chazal et. al.)',
            'x': df.iloc[df[df.type == 'ELM ref'].index].hls.values,
            'y': df.iloc[df[df.type == 'ELM ref'].index].error_rate.values,
            'color': tud_colors['gray'],
            'linestyle': 'dashdot',
        },
        {
            'name': 'ELM (PyRCN)',
            'x': df.iloc[df[df.type == 'elm'].index].hls.values,
            'y': df.iloc[df[df.type == 'elm'].index].error_rate.values,
            'color': tud_colors['lightblue'],
            'linestyle': 'solid',
        },
    ]

    """
    pca-elm
    elm
    minibatch
    original
    ELM ref
    BP ref
    stacked
    """

    # plots
    fig, ax = plt.subplots(figsize=(5., 3.))

    for dict_data in list_data:
        ax.plot(dict_data['x'], dict_data['y'], label=dict_data['name'], color=dict_data['color'], linewidth=1., linestyle=dict_data['linestyle'])

    ax.set_xlim([784, 15680])
    ax.legend()
    plt.xscale('log')
    plt.yscale('log')

    yticks = [.02, .03, .04, .05, .06]
    ax.set_yticks(yticks)
    # ax.set_yticklabels([''])
    ax.tick_params(axis='x', which='minor', bottom=False, top=False)
    xticks = 784 * np.array([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15, 20])
    ax.set_xticks(xticks)
    ax.set_xticklabels(['{0:.0f}'.format(xtick/784) for xtick in xticks])

    plt.xlabel('Fan-out')
    plt.ylabel('Error rate [%]')

    plt.tight_layout()
    plt.grid(b=True, which='major', axis='both')
    # plt.show()
    plt.savefig(os.path.join(directory, 'hidden_layer_size.pdf'), format='pdf')
    plt.savefig(os.path.join(os.environ['PGFPATH'], 'hidden_layer_size.pgf'), format='pgf')

def plot_significance():
    directory = './plots'
    filepath = './mnist-elm/significance.xlsx'
    df = pandas.read_excel(filepath, sheet_name='data')

    print(df['type'].unique())
    list_type = [
        'pca+random+activation',
        'pca+kmeans+cosine',
        'pca+kmeans+cosine+norm',
        'pca+avg+kmeans+cosine',
        'pca+kmeans+euclidean',
        'pca+kmeans+euclidean+activation'
    ]
    list_labels = [
        'Random',
        'Cosine',
        'Cosine\n(normed)',
        'Cosine\n(average)',
        'Euclidean',
        'Euclidean\n(activation)'
    ]

    list_error_rates = [df[df.type == i_type].mean_error_rate.values for i_type in list_type]

    fig, ax = plt.subplots(figsize=(4, 3), gridspec_kw={'bottom': .3})
    dict_bplot = ax.boxplot(
        [error_rate * 100 for error_rate in list_error_rates],
        labels=list_labels,
        patch_artist=True,
        boxprops={'facecolor': tud_colors['lightblue'], 'alpha': 1., 'color': tud_colors['gray']},
        medianprops={'color': tud_colors['darkblue']},
        flierprops={'markeredgecolor': tud_colors['orange'], 'marker': 'x', 'alpha': .7},
        whiskerprops={'color': tud_colors['gray'], 'alpha': 1.},
        capprops={'color': tud_colors['gray'], 'alpha': 1.},
    )
    ax.xaxis.set_tick_params(rotation=45)
    ax.set_ylabel('error rate [%]')
    ax.grid(axis='y')
    # plt.show()
    fig.savefig(os.path.join('./plots/', 'boxplot.pdf'), format='pdf')
    fig.savefig(os.path.join(os.environ['PGFPATH'], 'boxplot.pgf'), format='pgf')

def plot_hls_error_rate():
    directory = os.path.abspath('./plots/')
    filepath = os.path.abspath('./plots/hls.csv')
    df = pandas.read_csv(filepath)

    df.sort_values(by='hls', ascending=True, inplace=True)

    # concatenate name, hls, pca
    df['identifier'] = df['name'].map(str) + df['pca'].map(lambda pca: '{0:.0f}'.format(pca) if not np.isnan(pca) else '')

    list_identifier = list(df['identifier'].unique())
    print(list_identifier)

    # remove labels
    list_identifier.remove('stacked')
    list_identifier.remove('reference-ELM')
    list_identifier.remove('reference-BP')
    list_identifier.remove('original100')
    list_identifier.remove('elm_pca100')
    list_identifier.remove('minibatch100')

    list_identifier = ['elm_basic', 'elm_pca50', 'original50', 'minibatch50']
    dict_linespecs = {
        'elm_basic': {
            'label': 'random ELM',
            'linestyle': '--',
            'color': tud_colors['red'],
            'alpha': .7,
        },
        'elm_pca50': {
            'label': 'random ELM (PCA50)',
            'linestyle': '-',
            'color': tud_colors['lightgreen'],
            'alpha': .7,
        },
        'original50': {
            'label': 'K-Means ELM (PCA50)',
            'linestyle': (0, (5, 5)),
            'color': tud_colors['lightblue'],
            'alpha': .7,
            'cluster_color': tud_colors['darkblue'],
            'cluster_linestyle': (0, (3, 4)),
            'cluster_label': 'K-Means fit time',
        },
        'minibatch50': {
            'label': 'Minibatch ELM (PCA50)',
            'linestyle': (0, (1, 1)),
            'color': tud_colors['lightpurple'],
            'alpha': .7,
            'cluster_color': tud_colors['darkpurple'],
            'cluster_linestyle': (0, (1, 3)),
            'cluster_label': 'Minibatch fit time',
        }
    }

    fig, ax = plt.subplots(figsize=(5, 4), gridspec_kw={'bottom': .15, 'left': .15})

    lines = []
    for identifier in list_identifier:
        lines += ax.plot(
            df[df.identifier == identifier].hls,
            100 * df[df.identifier == identifier].error_rate,
            label=dict_linespecs[identifier]['label'],
            linestyle=dict_linespecs[identifier]['linestyle'],
            color=dict_linespecs[identifier]['color'],
            alpha=dict_linespecs[identifier]['alpha'],
        )

    ax.set_xlim([20, 16000])
    ax.set_xscale('log')
    ax.set_xticks([1e2, 1e3, 1e4])
    ax.set_xlabel('hidden layer size')

    ax.set_yscale('log')
    ax.set_yticks([2., 5., 10.])
    ax.set_yticklabels(['{0:.0f}%'.format(ytick) for ytick in [2., 5., 10.]])
    ax.set_ylabel('error rate [%]')

    ax.grid(which='both', axis='both')
    ax.legend()
    # plt.show()
    fig.savefig(os.path.join(directory, 'hls_error_rate.pdf'), format='pdf')
    fig.savefig(os.path.join(os.environ['PGFPATH'], 'hls_error_rate.pgf'), format='pgf')

def plot_hls_error_rate_poster():
    directory = os.path.abspath('./plots/')
    filepath = os.path.abspath('./plots/hls.csv')
    df = pandas.read_csv(filepath)

    df.sort_values(by='hls', ascending=True, inplace=True)

    # concatenate name, hls, pca
    df['identifier'] = df['name'].map(str) + df['pca'].map(lambda pca: '{0:.0f}'.format(pca) if not np.isnan(pca) else '')

    list_identifier = list(df['identifier'].unique())
    print(list_identifier)

    # remove labels
    list_identifier.remove('stacked')
    list_identifier.remove('reference-ELM')
    list_identifier.remove('reference-BP')
    list_identifier.remove('original100')
    list_identifier.remove('elm_pca100')
    list_identifier.remove('minibatch100')

    list_identifier = ['reference-ELM', 'elm_basic', 'elm_pca50', 'original50', 'minibatch50']
    dict_linespecs = {
        'reference-ELM': {
            'label': 'ELM (Chazal et. al.)',
            'linestyle': '--',
            'color': tud_colors['red'],
            'alpha': .7,
        },
        'elm_basic': {
            'label': 'random ELM',
            'linestyle': '--',
            'color': tud_colors['orange'],
            'alpha': .7,
        },
        'elm_pca50': {
            'label': 'random ELM (PCA50)',
            'linestyle': '-',
            'color': tud_colors['lightgreen'],
            'alpha': .7,
        },
        'original50': {
            'label': 'K-Means ELM (PCA50)',
            'linestyle': (0, (5, 5)),
            'color': tud_colors['lightblue'],
            'alpha': .7,
            'cluster_color': tud_colors['darkblue'],
            'cluster_linestyle': (0, (3, 4)),
            'cluster_label': 'K-Means fit time',
        },
        'minibatch50': {
            'label': 'Minibatch ELM (PCA50)',
            'linestyle': (0, (1, 1)),
            'color': tud_colors['lightpurple'],
            'alpha': .7,
            'cluster_color': tud_colors['darkpurple'],
            'cluster_linestyle': (0, (1, 3)),
            'cluster_label': 'Minibatch fit time',
        }
    }

    fig, ax = plt.subplots(figsize=(4, 3), gridspec_kw={'bottom': .18, 'left': .16, 'top': .7})

    lines = []
    for identifier in list_identifier:
        lines += ax.plot(
            df[df.identifier == identifier].hls,
            100 * df[df.identifier == identifier].error_rate,
            label=dict_linespecs[identifier]['label'],
            linestyle=dict_linespecs[identifier]['linestyle'],
            color=dict_linespecs[identifier]['color'],
            alpha=dict_linespecs[identifier]['alpha'],
        )

    ax.set_xlim([100, 16000])
    ax.set_xscale('log')
    ax.set_xticks([1e2, 1e3, 1e4])
    ax.set_xlabel('hidden layer size')

    ax.set_yscale('log')
    ax.set_ylim([1.0, 10.])
    ax.set_yticks([1., 2., 5., 10.], minor=False)
    ax.set_yticklabels(['{0:.0f}%'.format(ytick) for ytick in [1., 2., 5., 10.]])
    ax.set_yticklabels(['' for tick in ax.get_yticks(minor=True)], minor=True)
    ax.set_ylabel('error rate [%]')

    ax.grid(which='both', axis='both')
    ax.legend()
    ax.legend(bbox_to_anchor=(-0.23, 1.05, 1.38, 0), loc="lower left", mode="expand", ncol=2)
    # plt.show()
    fig.savefig(os.path.join(directory, 'hls_error_rate_poster.pdf'), format='pdf')
    fig.savefig(os.path.join(os.environ['PGFPATH'], 'hls_error_rate_poster.pgf'), format='pgf')

def plot_hls_error_rate_pcacompare():
    directory = os.path.abspath('./plots/')
    filepath = os.path.abspath('./plots/hls.csv')
    df = pandas.read_csv(filepath)

    df.sort_values(by='hls', ascending=True, inplace=True)

    # concatenate name, hls, pca
    df['identifier'] = df['name'].map(str) + df['pca'].map(lambda pca: '{0:.0f}'.format(pca) if not np.isnan(pca) else '')

    list_identifier = list(df['identifier'].unique())
    print(list_identifier)

    # remove labels
    list_identifier.remove('stacked')
    list_identifier.remove('reference-ELM')
    list_identifier.remove('reference-BP')
    list_identifier.remove('original100')
    list_identifier.remove('elm_pca100')
    list_identifier.remove('minibatch100')

    list_identifier = ['minibatch50', 'original100', 'minibatch100', 'original50', 'elm_pca100', 'elm_pca50']
    dicts_identifier = {
        'elm_pca50': {
            'label': 'Random ELM (PCA50)',
            'linestyle': (0, (3, 5, 1, 5, 1, 5)),
            'color': tud_colors['lightgreen'],
            'alpha': .7,
        },
        'elm_pca100': {
            'label': 'Random ELM (PCA100)',
            'linestyle': (0, (3, 1, 1, 1, 1, 1)),
            'color': tud_colors['darkgreen'],
            'alpha': .7,
        },
        'original50': {
            'label': 'K-Means ELM (PCA50)',
            'linestyle': (0, (5, 5)),
            'color': tud_colors['lightblue'],
            'alpha': .7,
        },
        'original100': {
            'label': 'K-Means ELM (PCA100)',
            'linestyle': (0, (3, 4)),
            'color': tud_colors['darkblue'],
            'alpha': .7,
        },
        'minibatch50': {
            'label': 'Minibatch ELM (PCA50)',
            'linestyle': (0, (1, 1)),
            'color': tud_colors['lightpurple'],
            'alpha': .7,
        },
        'minibatch100': {
            'label': 'Minibatch ELM (PCA50)',
            'linestyle': (0, (1, 3)),
            'color': tud_colors['darkpurple'],
            'alpha': .7,
        }
    }

    fig, ax = plt.subplots(figsize=(5, 4), gridspec_kw={'bottom': .15, 'left': .15})

    lines = []
    for identifier in dicts_identifier:
        lines += ax.plot(
            df[df.identifier == identifier].hls,
            100 * df[df.identifier == identifier].error_rate,
            label=dicts_identifier[identifier]['label'],
            linestyle=dicts_identifier[identifier]['linestyle'],
            color=dicts_identifier[identifier]['color'],
            alpha=dicts_identifier[identifier]['alpha'],
        )

    ax.set_xlim([20, 16000])
    ax.set_xscale('log')
    ax.set_xticks([1e2, 1e3, 1e4])
    ax.set_xlabel('hidden layer size')

    ax.set_yscale('log')
    ax.set_yticks([2., 5., 10.])
    ax.set_yticklabels(['{0:.0f}%'.format(ytick) for ytick in [2., 5., 10.]])
    ax.set_ylabel('error rate [%]')

    ax.grid(which='both', axis='both')
    ax.legend()
    # plt.show()
    fig.savefig(os.path.join(directory, 'hls_error_rate_pca.pdf'), format='pdf')
    fig.savefig(os.path.join(os.environ['PGFPATH'], 'hls_error_rate_pca.pgf'), format='pgf')

def plot_hls_fittime():
    directory = os.path.abspath('./plots/')
    filepath = os.path.abspath('./plots/hls.csv')
    df = pandas.read_csv(filepath)

    df.sort_values(by='hls', ascending=True, inplace=True)

    # concatenate name, hls, pca
    df['identifier'] = df['name'].map(str) + df['pca'].map(lambda pca: '{0:.0f}'.format(pca) if not np.isnan(pca) else '')

    list_identifier = list(df['identifier'].unique())
    print(list_identifier)

    # remove labels
    list_identifier.remove('stacked')
    list_identifier.remove('reference-ELM')
    list_identifier.remove('reference-BP')
    list_identifier.remove('original100')
    list_identifier.remove('elm_pca100')
    list_identifier.remove('minibatch100')

    list_identifier = ['elm_basic', 'elm_pca50', 'original50', 'minibatch50']
    dict_linespecs = {
        'elm_basic': {
            'label': 'random ELM',
            'linestyle': '--',
            'color': tud_colors['red'],
            'alpha': .7,
        },
        'elm_pca50': {
            'label': 'random ELM (PCA50)',
            'linestyle': '-',
            'color': tud_colors['lightgreen'],
            'alpha': .7,
        },
        'original50': {
            'label': 'K-Means ELM (PCA50)',
            'linestyle': (0, (5, 5)),
            'color': tud_colors['lightblue'],
            'alpha': .7,
            'cluster_color': tud_colors['darkblue'],
            'cluster_linestyle': (0, (3, 4)),
            'cluster_label': 'K-Means fit time',
        },
        'minibatch50': {
            'label': 'Minibatch ELM (PCA50)',
            'linestyle': (0, (1, 1)),
            'color': tud_colors['lightpurple'],
            'alpha': .7,
            'cluster_color': tud_colors['darkpurple'],
            'cluster_linestyle': (0, (1, 3)),
            'cluster_label': 'Minibatch fit time',
        }
    }

    fig, ax = plt.subplots(figsize=(5, 4), gridspec_kw={'bottom': .15, 'left': .15})

    lines = []
    for identifier in list_identifier:
        lines += ax.plot(
            df[df.identifier == identifier].hls,
            df[df.identifier == identifier].fit_time,
            label=dict_linespecs[identifier]['label'],
            linestyle=dict_linespecs[identifier]['linestyle'],
            color=dict_linespecs[identifier]['color'],
            alpha=dict_linespecs[identifier]['alpha'],
        )

        if identifier in ['minibatch50', 'original100', 'minibatch100', 'original50']:
            lines += ax.plot(
                df[df.identifier == identifier].hls,
                1. * df[df.identifier == identifier].kmeans_fit_time,
                label=dict_linespecs[identifier]['cluster_label'],
                linestyle=dict_linespecs[identifier]['cluster_linestyle'],
                color=dict_linespecs[identifier]['cluster_color'],
                alpha=dict_linespecs[identifier]['alpha'],
            )

    ax.set_xlim([20, 16000])
    ax.set_xscale('log')
    ax.set_xticks([1e2, 1e3, 1e4])
    ax.set_xlabel('hidden layer size')

    ax.set_ylim([3e-1, 1e3])
    ax.set_yscale('log')
    # ax.set_yticks([2., 5., 10.])
    # ax.set_yticklabels(['{0:.0f}%'.format(ytick) for ytick in [2., 5., 10.]])
    ax.set_ylabel('fit time [s]')

    ax.grid(which='both', axis='both')
    ax.legend()
    # plt.show()
    fig.savefig(os.path.join(directory, 'hls_fittime.pdf'), format='pdf')
    fig.savefig(os.path.join(os.environ['PGFPATH'], 'hls_fittime.pgf'), format='pgf')

def plot_silhouette_n_clusters():
    filepath = os.path.abspath('./plots/silhouette_n_clusters.csv')
    df = pandas.read_csv(filepath)

    df.sort_values('n_clusters', ascending=True)
    print(df.keys())
    keys = [
        'n_clusters',
        'n_init',
        'variance_threshold',
        'pca_n_components',
        'pca_explained_variance',
        'pca_explained_variance_ratio',
        'silhouette_original',
        'silhouette_variance_threshold',
        'silhouette_pca',
        'fittime_original',
        'fittime_variance_threshold',
        'fittime_pca',
        'inertia_original',
        'inertia_variance_threshold',
        'inertia_pca',
        'n_iter_original',
        'n_iter_variance_threshold',
        'n_iter_pca'
    ]

    fig, ax_score = plt.subplots(figsize=(5., 3.), gridspec_kw={'left': .15, 'right': .85, 'bottom': .2, 'top': .8})

    ax_score.set_xlim([np.min(df['n_clusters'].values), np.max(df['n_clusters'].values)])
    ax_score.set_xlabel(r'\#centroids')
    ax_score.set_xscale('log')
    ax_score.set_xticks([5, 10, 20, 50, 100, 200, 500, 1000, 2000, 4000])
    ax_score.set_xticklabels(['{0:.0f}'.format(xtick) for xtick in [5, 10, 20, 50, 100, 200, 500, 1000, 2000, 4000]])
    ax_score.grid(axis='x', which='both')

    lines = []

    lines += ax_score.plot(df['n_clusters'].values, 100*df['silhouette_original'].values, color=tud_colors['lightpurple'], alpha=.7)
    lines += ax_score.plot(df['n_clusters'].values, 100*df['silhouette_variance_threshold'].values, color=tud_colors['lightgreen'], alpha=.7)
    lines += ax_score.plot(df['n_clusters'].values, 100*df['silhouette_pca'].values, color=tud_colors['lightblue'], alpha=.7)

    ax_time = ax_score.twinx()

    lines += ax_time.plot(df['n_clusters'].values, df['fittime_original'].values, color=tud_colors['lightpurple'], alpha=.7, linestyle='--')
    lines += ax_time.plot(df['n_clusters'].values, df['fittime_variance_threshold'].values, color=tud_colors['lightgreen'], alpha=.7, linestyle='--')
    lines += ax_time.plot(df['n_clusters'].values, df['fittime_pca'].values, color=tud_colors['lightblue'], alpha=.7, linestyle='--')

    ax_score.set_ylabel('silhouette score [%]')
    # ax_score.set_yscale('log')
    ax_score.grid(axis='y', which='major')
    ax_score.set_ylim([0, 8])
    ax_score.set_yticks([0, 2, 4, 6, 8])
    ax_score.set_yticks([], minor=True)
    ax_score.set_yticklabels(['{0:.0f}%'.format(ytick) for ytick in [0, 2, 4, 6, 8]])
    ax_time.set_ylabel('fit time [s]')
    ax_time.set_yscale('log')

    ax_score.legend(lines[:3], [
        'original',
        'variance\nthreshold = {0:.1f}'.format(df['variance_threshold'][0]),
        'PCA, expl.\nvar ratio = {0:.1f}%'.format(100 * df['pca_explained_variance_ratio'][0])
    ], bbox_to_anchor=(-0.2, 1.05, 1.4, 0), loc="lower left", mode="expand", ncol=3)

    # plt.show()
    fig.savefig('./plots/plot_silhouette_n_clusters.pdf', format='pdf')
    fig.savefig(os.path.join(os.environ['PGFPATH'], 'plot_silhouette_n_clusters.pgf'), format='pgf')

def silhouette_kmeans_features():
    filepath = os.path.abspath('./mnist-elm/silhouette_kmeans20_features.csv')
    df = pandas.read_csv(filepath)

    print(df.keys())
    keys = [
        'nfeatures',
        'fittime_random',
        'fittime_maxvar',
        'fittime_pca',
        'silhouette_random',
        'silhouette_maxvar',
        'silhouette_pca',
        'explainvar_random',
        'explainvar_maxvar',
        'explainvar_pca',
        'explvarrat_random',
        'explvarrat_maxvar',
        'explvarrat_pca',
        'n_clusters'
    ]
    print(np.unique(df['n_clusters'].values))

    df.sort_values('nfeatures', ascending=True)

    fig, ax_score = plt.subplots(figsize=(5., 3.), gridspec_kw={'left': .15, 'right': .85, 'top': .8, 'bottom': .2})

    ax_score.set_xlim([np.min(df['nfeatures'].values), np.max(df['nfeatures'].values)])
    ax_score.set_xscale('log')
    ax_score.set_xticks([1, 2, 5, 10, 20, 50, 100, 200, 500, 784])
    ax_score.set_xticklabels(['{0:.0f}'.format(xtick) for xtick in [1, 2, 5, 10, 20, 50, 100, 200, 500, 784]])
    ax_score.grid(axis='x', which='both')
    ax_score.set_xlabel(r'\#features')

    ax_variance = ax_score.twinx()

    lines = []
    lines += ax_score.plot(df['nfeatures'].values, 100*df['silhouette_random'].values, color=tud_colors['lightpurple'], linestyle='-', alpha=.7)
    lines += ax_score.plot(df['nfeatures'].values, 100*df['silhouette_maxvar'].values, color=tud_colors['lightgreen'], linestyle='-', alpha=.7)
    lines += ax_score.plot(df['nfeatures'].values, 100*df['silhouette_pca'].values, color=tud_colors['lightblue'], linestyle='-', alpha=.7)
    lines += ax_variance.plot(df['nfeatures'].values, 100*df['explvarrat_random'].values, color=tud_colors['lightpurple'], linestyle='--', alpha=.7)
    lines += ax_variance.plot(df['nfeatures'].values, 100*df['explvarrat_maxvar'].values, color=tud_colors['lightgreen'], linestyle='--', alpha=.7)
    lines += ax_variance.plot(df['nfeatures'].values, 100*df['explvarrat_pca'].values, color=tud_colors['lightblue'], linestyle='--', alpha=.7)

    # ax_score.set_yscale('log')
    ax_score.set_ylim([0, 10])
    ax_score.set_yticks([0, 2, 4, 6, 8, 10])
    # ax_score.set_yticks([], minor=True)
    ax_score.set_yticklabels(['{0:.0f}%'.format(ytick) for ytick in [0, 2, 4, 6, 8, 10]])
    ax_score.set_ylabel('silhouette score [%]')

    ax_variance.set_yscale('log')
    ax_variance.set_ylim([1, 100])
    ax_variance.set_yticks([1, 2, 5, 10, 20, 50, 100])
    ax_variance.set_yticks([], minor=True)
    ax_variance.set_yticklabels(['{0:.0f}%'.format(ytick) for ytick in [1, 2, 5, 10, 20, 50, 100]])
    ax_variance.grid(axis='y', which='both')
    ax_variance.set_ylabel('explained variance ratio [%]')

    ax_score.legend(lines[:3], ['random', 'maximal\nvariance', 'principal\ncomponents'], bbox_to_anchor=(-0.2, 1.05, 1.4, 0), loc="lower left", mode="expand", ncol=3)
    # plt.show()
    fig.savefig('./plots/silhouette_kmeans_features.pdf', format='pdf')
    fig.savefig(os.path.join(os.environ['PGFPATH'], 'silhouette_kmeans_features.pgf'), format='pgf')

def plot_silhouette_subset():
    filepath = os.path.abspath('./plots/silhouette_kmeans_subset_size.csv')
    df = pandas.read_csv(filepath)

    print(df.keys())
    keys = [
        'subset_size',
        'k',
        'n_init',
        'silhouette_raninit',
        'silhouette_preinit',
        'fittime_raninit',
        'fittime_preinit',
        'scoretime_raninit',
        'scoretime_preinit'
    ]
    print(np.unique(df['k'].values))

    df.sort_values('subset_size', ascending=True)

    fig, ax_score = plt.subplots(figsize=(5., 3.), gridspec_kw={'left': .15, 'right': .85, 'top': .8, 'bottom': .2})

    ax_score.set_xlim([np.min(df['subset_size'].values), np.max(df['subset_size'].values)])
    ax_score.set_xscale('log')
    ax_score.set_xticks([250, 500, 1000, 2000, 4000, 8000, 16000, 32000, 60000])
    ax_score.set_xticklabels(['{0:.0f}'.format(xtick) for xtick in [250, 500, 1000, 2000, 4000, 8000, 16000, 32000, 60000]])
    ax_score.grid(axis='x', which='both')
    ax_score.set_xlabel(r'subset size')

    ax_time = ax_score.twinx()

    lines = []
    lines += ax_score.plot(df['subset_size'].values, 100*df['silhouette_raninit'].values, color=tud_colors['lightpurple'], linestyle='-', alpha=.7)
    lines += ax_score.plot(df['subset_size'].values, 100*df['silhouette_preinit'].values, color=tud_colors['lightblue'], linestyle='-', alpha=.7)
    lines += ax_time.plot(df['subset_size'].values, df['fittime_raninit'].values, color=tud_colors['lightpurple'], linestyle='--', alpha=.7)
    lines += ax_time.plot(df['subset_size'].values, df['fittime_preinit'].values, color=tud_colors['lightblue'], linestyle='--', alpha=.7)
    lines += ax_time.plot(df['subset_size'].values, df['scoretime_preinit'].values, color=tud_colors['gray'], linestyle='-.', alpha=.5)

    # ax_score.set_yscale('log')
    ax_score.set_ylim([8, 11])
    ax_score.set_yticks([8, 9, 10, 11])
    ax_score.set_yticks([], minor=True)
    ax_score.set_yticklabels(['{0:.0f}%'.format(ytick) for ytick in [8, 9, 10, 11]])
    ax_score.grid(axis='y', which='both')
    ax_score.set_ylabel('silhouette score [%]')

    ax_time.set_yscale('log')
    ax_time.set_ylabel('time [s]')

    ax_score.legend([lines[idx] for idx in [0, 1, 4]], [
        '{0:.0f} random inits'.format(int(np.unique(df['n_init'].values))),
        'preinit',
        'score time'
    ], bbox_to_anchor=(-0.2, 1.05, 1.4, 0), loc="lower left", mode="expand", ncol=3)
    # plt.show()
    fig.savefig('./plots/plot_silhouette_subset.pdf', format='pdf')
    fig.savefig(os.path.join(os.environ['PGFPATH'], 'plot_silhouette_subset.pgf'), format='pgf')

def plot_silhouette_kcluster():
    filepath = os.path.abspath('./plots/silhouette_kcluster.csv')
    df = pandas.read_csv(filepath)

    print(df.keys())
    keys = [
        'n_clusters',
        'pca_n_components',
        'pca_expl_var',
        'pca_expl_var_ratio',
        'silhouette_kcosine',
        'silhouette_kmeans',
        'fittime_kcosine',
        'fittime_kmeans'
    ]
    print(np.unique(df['pca_n_components'].values))

    df.sort_values('n_clusters', ascending=True)

    fig, ax_score = plt.subplots(figsize=(5., 3.), gridspec_kw={'left': .15, 'right': .85, 'top': .8, 'bottom': .2})

    ax_score.set_xlim([np.min(df['n_clusters'].values), np.max(df['n_clusters'].values)])
    ax_score.set_xscale('log')
    ax_score.set_xticks([10, 20, 50, 100, 200])
    ax_score.set_xticklabels(['{0:.0f}'.format(xtick) for xtick in [10, 20, 50, 100, 200]])
    ax_score.grid(axis='x', which='both')
    ax_score.set_xlabel(r'\#centroids')

    ax_time = ax_score.twinx()

    lines = []
    lines += ax_score.plot(df['n_clusters'].values, 100*df['silhouette_kcosine'].values, color=tud_colors['lightpurple'], linestyle='-', alpha=.7)
    lines += ax_score.plot(df['n_clusters'].values, 100*df['silhouette_kmeans'].values, color=tud_colors['lightblue'], linestyle='-', alpha=.7)
    lines += ax_time.plot(df['n_clusters'].values, df['fittime_kcosine'].values, color=tud_colors['lightpurple'], linestyle='--', alpha=.7)
    lines += ax_time.plot(df['n_clusters'].values, df['fittime_kmeans'].values, color=tud_colors['lightblue'], linestyle='--', alpha=.7)

    # ax_score.set_yscale('log')
    ax_score.set_ylim([7, 17])
    ax_score.set_yticks([10, 15])
    ax_score.set_yticks([], minor=True)
    ax_score.set_yticklabels(['{0:.0f}%'.format(ytick) for ytick in [10, 15]])
    ax_score.grid(axis='y', which='both')
    ax_score.set_ylabel('silhouette score [%]')

    ax_time.set_yscale('log')
    ax_time.set_ylabel('time [s]')

    ax_score.legend(lines[:2], [
        'kcosine',
        'kmeans'
    ], bbox_to_anchor=(.1, 1.05, .8, 0), loc="lower left", mode="expand", ncol=3)
    # plt.show()
    fig.savefig('./plots/plot_silhouette_kcluster.pdf', format='pdf')
    # fig.savefig(os.path.join(os.environ['PGFPATH'], 'plot_silhouette_kcluster.pgf'), format='pgf')

def plot_kmeans_subset_phenomen():
    keys = ['n_clusters', 'silhouette_original', 'n_iter_original']

    df_2k = pandas.read_csv('./mnist-elm/silhouette_n_clusters_subset2k.csv').set_index('n_clusters')
    df_5k = pandas.read_csv('./mnist-elm/silhouette_n_clusters_subset5k.csv').set_index('n_clusters')
    df_10k = pandas.read_csv('./mnist-elm/silhouette_n_clusters_subset10k.csv').set_index('n_clusters')

    # force suffix
    df_2k.columns = df_2k.columns.map(lambda x: str(x) + '_2k')

    df = df_10k.join(df_5k, lsuffix='_10k', rsuffix='_5k').join(df_2k)

    print(df.keys())

    df.sort_index(ascending=True, inplace=True)

    fig, ax_score = plt.subplots(figsize=(5., 3.), gridspec_kw={'left': .15, 'right': .85, 'top': .8, 'bottom': .2})

    # df.index = df['n_clusters_2k']
    ax_score.set_xlim([np.min(df.index.values), np.max(df.index.values)])
    ax_score.set_xscale('log')
    ax_score.set_xlim([50, 2000])
    ax_score.set_xticks([50, 100, 200, 500, 1000, 2000])
    ax_score.set_xticklabels(['{0:.0f}'.format(xtick) for xtick in [50, 100, 200, 500, 1000, 2000]])
    ax_score.grid(axis='x', which='both')
    ax_score.set_xlabel(r'\#centroids')

    ax_niter = ax_score.twinx()

    lines = []
    lines += ax_score.plot(df.index.values, 100 * df['silhouette_original_2k'].values, color=tud_colors['lightpurple'], linestyle='-', alpha=.7)
    lines += ax_score.plot(df.index.values, 100 * df['silhouette_original_5k'].values, color=tud_colors['lightblue'], linestyle='-', alpha=.7)
    lines += ax_score.plot(df.index.values, 100 * df['silhouette_original_10k'].values, color=tud_colors['lightgreen'], linestyle='-', alpha=.7)

    lines += ax_niter.plot(df.index.values, df['n_iter_original_2k'].values, color=tud_colors['lightpurple'], linestyle='--', alpha=.7)
    lines += ax_niter.plot(df.index.values, df['n_iter_original_5k'].values, color=tud_colors['lightblue'], linestyle='--', alpha=.7)
    lines += ax_niter.plot(df.index.values, df['n_iter_original_10k'].values, color=tud_colors['lightgreen'], linestyle='--', alpha=.7)

    ax_score.grid(axis='y', which='both')
    ax_score.set_ylabel('silhouette score [%]')
    ax_score.set_yticks([0, 2, 4, 6, 8, 10])
    ax_score.set_yticks([], minor=True)
    ax_score.set_yticklabels(['{0:.0f}%'.format(ytick) for ytick in [0, 2, 4, 6, 8, 10]])

    ax_niter.set_yscale('log')
    ax_niter.set_ylabel(r'\#iterations')

    ax_score.legend(lines[:3], [
        '2000',
        '5000',
        '10000'
    ], bbox_to_anchor=(.1, 1.05, .8, 0), loc="lower left", mode="expand", ncol=3)
    # plt.show()
    fig.savefig('./plots/plot_kmeans_subset_phenomen.pdf', format='pdf')
    fig.savefig(os.path.join(os.environ['PGFPATH'], 'plot_kmeans_subset_phenomen.pgf'), format='pgf')

def plot_cluster_critical():
    cluster_centers = np.load('./cluster_critical.npy', allow_pickle=True)

    plt.imsave('./centroid4.png', cluster_centers[99, ...].reshape(28, 28), cmap=plt.cm.gray_r)
    return

def main():
    if not os.path.exists(directory):
        os.makedirs(directory)

    # plot_activation_variance()
    # plot_activation_mean()
    # plot_hyperparameters()
    # plot_hyperparameters_poster()
    # plot_preprocessed()
    # plot_hidden_layer_size()
    # plot_ridge()
    # plot_pca()
    # plot_significance()
    # plot_hls_error_rate()
    plot_hls_error_rate_poster()
    # plot_hls_error_rate_pcacompare()
    # plot_hls_fittime()
    # plot_silhouette_n_clusters()
    # silhouette_kmeans_features()
    # plot_silhouette_subset()
    # plot_silhouette_kcluster()
    # plot_kmeans_subset_phenomen()
    # plot_cluster_critical()
    return

if __name__ == "__main__":
    main()

max_stars_repo_name: TUD-STKS/PyRCN

max_issues_repo_name: TUD-STKS/PyRCN

max_forks_repo_name: TUD-STKS/PyRCN


text:
import numpy as np
from numpy import testing as nptest

from geneEcomparison import Utils

def test_zscore():
  result = Utils.z_score(np.asarray([1, 3 , 5]))

  # https://stackoverflow.com/questions/3302949/best-way-to-assert-for-numpy-array-equality
  assert nptest.assert_allclose(result, np.asarray([-1.22474487,  0,  1.22474487])) is None

max_stars_repo_name: JureFabjan/aba-analysis

max_issues_repo_name: JureFabjan/aba-analysis

max_forks_repo_name: JureFabjan/aba-analysis

reason: import numpy,from numpy


text:
import numpy as np
from torch import nn

from src.trainers import hooks as hooks_
from .likelihood_trainer import LikelihoodTrainer
from .moment_matching_trainer import MomentMatchingTrainer
from .mse_trainer import MSETrainer
from .student_t_trainer import StudentTTrainer
from .variational_variance_trainer import VariationalVarianceTrainer
from src.models import variational_variance
from src.models.networks import (
    GaussianLikelihoodHead,
    NormalGammaHead,
    GaussianLikelihoodFixedVarianceHead,
)

def filter_dictionary(info_dict):
    non_metric = [
        "hgrad_norm",
        "egrad_norm",
        "ehgrad_angle",
        "ehgrad_norm_sim",
        "ehgrad_conflict",
        "wanalysis",
    ]

    return dict(filter(lambda x: x[0] not in non_metric, info_dict.items()))

def get_head_and_trainer(opts, hooks=None):
    if "likelihood" in opts.training:
        if "fixed_unit_var" in opts.training:
            head = lambda inp, outp: GaussianLikelihoodFixedVarianceHead(
                inp, outp, fixed_var=1
            )
        else:
            head = lambda inp, outp: GaussianLikelihoodHead(
                inp, outp, initial_var=1, max_var=100
            )

        nll_weight_beta = 0.0 if opts.loss_weight is None else opts.loss_weight

        trainer = LikelihoodTrainer(
            collect_likelihoods=opts.collect_likelihoods,
            optimizer_name=opts.optimizer_name,
            mse_warmup=opts.mse_warmup,
            lr=opts.lr,
            n_epochs=opts.n_epochs,
            betas=opts.betas,
            batch_size=opts.batch_size,
            log_every=opts.log_every,
            checkpoint=opts.checkpoint,
            device=opts.device,
            hooks=hooks,
            momentum=opts.momentum,
            rms_alpha=opts.rms_alpha,
            detach_variance=opts.detach_variance,
            dataloader_drop_last=not opts.dataloader_keep_last,
            nll_weight_beta=nll_weight_beta,
            early_stop_metric=opts.early_stop_metric,
            early_stop_iters=opts.early_stop_iters,
            track_best_metrics=opts.track_best_metrics,
            log_intermediate_results=opts.log_intermediate_results,
        )
    elif opts.training == "mse":
        head = nn.Linear

        trainer = MSETrainer(
            optimizer_name=opts.optimizer_name,
            lr=opts.lr,
            n_epochs=opts.n_epochs,
            betas=opts.betas,
            batch_size=opts.batch_size,
            log_every=opts.log_every,
            checkpoint=opts.checkpoint,
            device=opts.device,
            hooks=hooks,
            momentum=opts.momentum,
            dataloader_drop_last=not opts.dataloader_keep_last,
            early_stop_metric=opts.early_stop_metric,
            early_stop_iters=opts.early_stop_iters,
            track_best_metrics=opts.track_best_metrics,
            log_intermediate_results=opts.log_intermediate_results,
        )
    elif opts.training.startswith("moment_matching"):
        head = lambda inp, outp: GaussianLikelihoodHead(
            inp, outp, initial_var=1, max_var=100
        )

        gamma_loss = "gamma" in opts.training

        trainer = MomentMatchingTrainer(
            collect_likelihoods=opts.collect_likelihoods,
            optimizer_name=opts.optimizer_name,
            lr=opts.lr,
            n_epochs=opts.n_epochs,
            betas=opts.betas,
            batch_size=opts.batch_size,
            log_every=opts.log_every,
            checkpoint=opts.checkpoint,
            device=opts.device,
            hooks=hooks,
            momentum=opts.momentum,
            gamma_loss=gamma_loss,
            dataloader_drop_last=not opts.dataloader_keep_last,
            early_stop_metric=opts.early_stop_metric,
            early_stop_iters=opts.early_stop_iters,
            track_best_metrics=opts.track_best_metrics,
            log_intermediate_results=opts.log_intermediate_results,
        )
    elif opts.training == "student_t":
        head = lambda inp, outp: NormalGammaHead(inp, outp)

        trainer = StudentTTrainer(
            collect_likelihoods=opts.collect_likelihoods,
            optimizer_name=opts.optimizer_name,
            lr=opts.lr,
            n_epochs=opts.n_epochs,
            betas=opts.betas,
            batch_size=opts.batch_size,
            log_every=opts.log_every,
            checkpoint=opts.checkpoint,
            device=opts.device,
            hooks=hooks,
            momentum=opts.momentum,
            dataloader_drop_last=not opts.dataloader_keep_last,
            early_stop_metric=opts.early_stop_metric,
            early_stop_iters=opts.early_stop_iters,
            track_best_metrics=opts.track_best_metrics,
            log_intermediate_results=opts.log_intermediate_results,
        )
    elif "vari_var" in opts.training:
        n_mc_samples = 20
        n_components = 100
        if "vbem" in opts.training:
            if "star" not in opts.training:
                n_components = 144

            def head(inp_dim, outp_dim):
                return variational_variance.NormalGammaPiHead(
                    inp_dim, outp_dim, n_components=n_components
                )

            def reg_init_fn(model, inp_dim, outp_dim, batch):
                if "star" in opts.training:
                    alphas, betas = variational_variance.get_vbem_star_standard_init(
                        outp_dim, n_components
                    )
                else:
                    alphas, betas = variational_variance.get_vbem_standard_init(
                        outp_dim
                    )
                return variational_variance.VBEMRegularizer(
                    alphas,
                    betas,
                    n_mc_samples=n_mc_samples,
                    prior_trainable="star" in opts.training,
                )

        elif "xvamp" in opts.training:

            def head(inp_dim, outp_dim):
                return variational_variance.NormalGammaPiHead(
                    inp_dim, outp_dim, n_components=n_components
                )

            def reg_init_fn(model, inp_dim, outp_dim, batch):
                inputs = batch[0].float()
                if len(inputs) < n_components:
                    raise ValueError(
                        "Not enough samples for xVAMP regularizer in batch"
                    )
                return variational_variance.xVAMPRegularizer(
                    model,
                    inputs[:n_components],
                    n_mc_samples=n_mc_samples,
                    prior_trainable="star" in opts.training,
                )

        else:
            raise ValueError("Unknown regularizer")

        trainer = VariationalVarianceTrainer(
            reg_init_fn,
            collect_likelihoods=opts.collect_likelihoods,
            optimizer_name=opts.optimizer_name,
            lr=opts.lr,
            n_epochs=opts.n_epochs,
            betas=opts.betas,
            batch_size=opts.batch_size,
            log_every=opts.log_every,
            checkpoint=opts.checkpoint,
            device=opts.device,
            hooks=hooks,
            momentum=opts.momentum,
            dataloader_drop_last=not opts.dataloader_keep_last,
            early_stop_metric=opts.early_stop_metric,
            early_stop_iters=opts.early_stop_iters,
            track_best_metrics=opts.track_best_metrics,
            log_intermediate_results=opts.log_intermediate_results,
        )
    else:
        raise ValueError(f"Unknown training method {opts.training}.")

    return head, trainer

def get_hooks(opts, train_dataset, eval_dataset):
    hooks = []
    if (
        not opts.eval_hook
        and not opts.jac_var_hook
        and not opts.vae_vis_hook
        and not opts.test_posterior_metrics_hook
    ):
        return hooks

    run_every = opts.hooks_every if opts.hooks_every else [opts.log_every]
    if not isinstance(run_every, (list, tuple)):
        run_every = [run_every]

    if opts.eval_hook or opts.jac_var_hook:
        example = train_dataset[0][0]
        if example.shape[0] == 1:  # Toy dataset
            inputs = [x[0] for x in train_dataset]
            eval_points = np.linspace(
                np.min(inputs), np.max(inputs), opts.num_hook_points
            )[:, None]
            targets = None
        else:
            rng = np.random.RandomState(44)
            idxs = rng.permutation(len(train_dataset))[: opts.num_hook_points]
            eval_points = train_dataset[idxs][0]
            targets = train_dataset[idxs][1]

        assert eval_points.ndim == 2

    if opts.eval_hook:
        hooks.append(
            hooks_.EvalHook(
                eval_points=eval_points,
                eval_every=run_every,
                last_epoch=opts.n_epochs,
                stop_after=opts.stop_hooks_after,
                targets=targets,
            )
        )
    if opts.jac_var_hook:
        assert opts.feature_layer is not None
        assert opts.granularities is not None
        hook = hooks_.JacobianVarianceHook(
            eval_points=eval_points,
            granularities=opts.granularities,
            target_layer=opts.feature_layer,
            eval_every=run_every,
            last_epoch=opts.n_epochs,
            stop_after=opts.stop_hooks_after,
        )
        hooks.append(hook)
    if opts.vae_vis_hook:
        hook = hooks_.VAEVisualizationHook(
            eval_every=run_every,
            last_epoch=opts.n_epochs,
            image_size=train_dataset.shape,
        )
        hooks.append(hook)
    if opts.test_posterior_metrics_hook:
        hooks.append(hooks_.TestPosteriorPredictiveMetricsHook())

    return hooks

max_stars_repo_name: martius-lab/beta-nll

max_issues_repo_name: martius-lab/beta-nll

max_forks_repo_name: martius-lab/beta-nll


text:
import numpy as np

class Perceptron():

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

  def test_match(self, test_data, theta, theta0):
    return test_data[1] * (np.matmul(theta, test_data[0]) + theta0)

  def all_good(self, theta, theta0):
    results = []
    any_wrong_label = False
    for test_data in self.test_set:
      results += [self.test_match(test_data, theta, theta0)]
    for result in results:
      any_wrong_label = any_wrong_label or result <= 0
    return any_wrong_label == False

  def optimal_theta(self):
    theta = np.zeros(self.test_set[0][0].shape)
    theta0 = 0
    while (self.all_good(theta, theta0) == False):
      for i in range(len(self.test_set)):
        test_data = self.test_set[i]
        if self.test_match(test_data, theta, theta0) <= 0:
          theta = theta + test_data[1] * test_data[0]
          theta0 = theta0 + test_data[1]
          print("temporary theta : " + str(theta) + " and theta0 : " + str(theta0))
    return [theta, theta0]

test_set = [
  np.array([np.array([0,0,0]), np.array(+1)]),
  np.array([np.array([0,0,1]), np.array(-1)]),
  np.array([np.array([0,1,0]), np.array(-1)]),
  np.array([np.array([0,1,1]), np.array(-1)]),
  np.array([np.array([1,0,0]), np.array(-1)]),
  np.array([np.array([1,0,1]), np.array(-1)]),
  np.array([np.array([1,1,0]), np.array(-1)]),
  np.array([np.array([1,1,1]), np.array(-1)])
]

perceptron = Perceptron(test_set)
optimal_theta = perceptron.optimal_theta()
print("final theta : " + str(optimal_theta[0]) + " and theta0 : " + str(optimal_theta[1]))

max_stars_repo_name: tritus/ml-courses

max_issues_repo_name: tritus/ml-courses

max_forks_repo_name: tritus/ml-courses


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

import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ..test_feature_extraction_common import FeatureExtractionSavingTestMixin, prepare_image_inputs

if is_torch_available():
    import torch

if is_vision_available():
    from PIL import Image

    from transformers import ConvNextFeatureExtractor

class ConvNextFeatureExtractionTester(unittest.TestCase):
    def __init__(
        self,
        parent,
        batch_size=7,
        num_channels=3,
        image_size=18,
        min_resolution=30,
        max_resolution=400,
        do_resize=True,
        size=20,
        crop_pct=0.875,
        do_normalize=True,
        image_mean=[0.5, 0.5, 0.5],
        image_std=[0.5, 0.5, 0.5],
    ):
        self.parent = parent
        self.batch_size = batch_size
        self.num_channels = num_channels
        self.image_size = image_size
        self.min_resolution = min_resolution
        self.max_resolution = max_resolution
        self.do_resize = do_resize
        self.size = size
        self.crop_pct = crop_pct
        self.do_normalize = do_normalize
        self.image_mean = image_mean
        self.image_std = image_std

    def prepare_feat_extract_dict(self):
        return {
            "image_mean": self.image_mean,
            "image_std": self.image_std,
            "do_normalize": self.do_normalize,
            "do_resize": self.do_resize,
            "size": self.size,
            "crop_pct": self.crop_pct,
        }

@require_torch
@require_vision
class ConvNextFeatureExtractionTest(FeatureExtractionSavingTestMixin, unittest.TestCase):

    feature_extraction_class = ConvNextFeatureExtractor if is_vision_available() else None

    def setUp(self):
        self.feature_extract_tester = ConvNextFeatureExtractionTester(self)

    @property
    def feat_extract_dict(self):
        return self.feature_extract_tester.prepare_feat_extract_dict()

    def test_feat_extract_properties(self):
        feature_extractor = self.feature_extraction_class(**self.feat_extract_dict)
        self.assertTrue(hasattr(feature_extractor, "do_resize"))
        self.assertTrue(hasattr(feature_extractor, "size"))
        self.assertTrue(hasattr(feature_extractor, "crop_pct"))
        self.assertTrue(hasattr(feature_extractor, "do_normalize"))
        self.assertTrue(hasattr(feature_extractor, "image_mean"))
        self.assertTrue(hasattr(feature_extractor, "image_std"))

    def test_batch_feature(self):
        pass

    def test_call_pil(self):
        # Initialize feature_extractor
        feature_extractor = self.feature_extraction_class(**self.feat_extract_dict)
        # create random PIL images
        image_inputs = prepare_image_inputs(self.feature_extract_tester, equal_resolution=False)
        for image in image_inputs:
            self.assertIsInstance(image, Image.Image)

        # Test not batched input
        encoded_images = feature_extractor(image_inputs[0], return_tensors="pt").pixel_values
        self.assertEqual(
            encoded_images.shape,
            (
                1,
                self.feature_extract_tester.num_channels,
                self.feature_extract_tester.size,
                self.feature_extract_tester.size,
            ),
        )

        # Test batched
        encoded_images = feature_extractor(image_inputs, return_tensors="pt").pixel_values
        self.assertEqual(
            encoded_images.shape,
            (
                self.feature_extract_tester.batch_size,
                self.feature_extract_tester.num_channels,
                self.feature_extract_tester.size,
                self.feature_extract_tester.size,
            ),
        )

    def test_call_numpy(self):
        # Initialize feature_extractor
        feature_extractor = self.feature_extraction_class(**self.feat_extract_dict)
        # create random numpy tensors
        image_inputs = prepare_image_inputs(self.feature_extract_tester, equal_resolution=False, numpify=True)
        for image in image_inputs:
            self.assertIsInstance(image, np.ndarray)

        # Test not batched input
        encoded_images = feature_extractor(image_inputs[0], return_tensors="pt").pixel_values
        self.assertEqual(
            encoded_images.shape,
            (
                1,
                self.feature_extract_tester.num_channels,
                self.feature_extract_tester.size,
                self.feature_extract_tester.size,
            ),
        )

        # Test batched
        encoded_images = feature_extractor(image_inputs, return_tensors="pt").pixel_values
        self.assertEqual(
            encoded_images.shape,
            (
                self.feature_extract_tester.batch_size,
                self.feature_extract_tester.num_channels,
                self.feature_extract_tester.size,
                self.feature_extract_tester.size,
            ),
        )

    def test_call_pytorch(self):
        # Initialize feature_extractor
        feature_extractor = self.feature_extraction_class(**self.feat_extract_dict)
        # create random PyTorch tensors
        image_inputs = prepare_image_inputs(self.feature_extract_tester, equal_resolution=False, torchify=True)
        for image in image_inputs:
            self.assertIsInstance(image, torch.Tensor)

        # Test not batched input
        encoded_images = feature_extractor(image_inputs[0], return_tensors="pt").pixel_values
        self.assertEqual(
            encoded_images.shape,
            (
                1,
                self.feature_extract_tester.num_channels,
                self.feature_extract_tester.size,
                self.feature_extract_tester.size,
            ),
        )

        # Test batched
        encoded_images = feature_extractor(image_inputs, return_tensors="pt").pixel_values
        self.assertEqual(
            encoded_images.shape,
            (
                self.feature_extract_tester.batch_size,
                self.feature_extract_tester.num_channels,
                self.feature_extract_tester.size,
                self.feature_extract_tester.size,
            ),
        )

max_stars_repo_name: dctelus/transformers


text:
import keras
import os
from AbstractRNNClassifier import AbstractRNNClassifier
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Input, Lambda, LSTM, Dense
from keras.models import load_model
from keras.models import Model
import numpy as np

class MnistLSTMClassifier(AbstractRNNClassifier):
    def __init__(self):
        # Classifier
        self.time_steps = 28  # timesteps to unroll
        self.n_units = 128  # hidden LSTM units
        self.n_inputs = 28  # rows of 28 pixels (an mnist img is 28x28)
        self.n_classes = 10  # mnist classes/labels (0-9)
        self.batch_size = 128  # Size of each batch
        self.n_epochs = 20

    def create_model(self):
        self.model = Sequential()
        self.model.add(LSTM(self.n_units, input_shape=(self.time_steps, self.n_inputs)))
        self.model.add(Dense(self.n_classes, activation='softmax'))

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

    def load_hidden_state_model(self, model_path):
        """
        return the rnn model with return_sequence enabled.
        """
        input = Input(shape=(self.time_steps, self.n_inputs))
        lstm = LSTM(self.n_units, input_shape=(self.time_steps, self.n_inputs), return_sequences=True)(input)
        last_timestep = Lambda(lambda x: x[:, -1, :])(lstm)
        dense = Dense(10, activation='softmax')(last_timestep)
        model = Model(inputs=input, outputs=[dense, lstm])
        model.load_weights(model_path)
        self.model = model

    def train(self, save_path):
        self.create_model()
        (x_train, y_train), (x_test, y_test) = mnist.load_data()

        x_train = self.input_preprocess(x_train)
        x_test = self.input_preprocess(x_test)

        y_test = keras.utils.to_categorical(y_test, num_classes=10)
        y_train = keras.utils.to_categorical(y_train, num_classes=10)

        self.model.fit(x_train, y_train, validation_data=(x_test, y_test),
                       batch_size=self.batch_size, epochs=self.n_epochs, shuffle=False)

        os.makedirs(save_path, exist_ok=True)
        self.model.save(os.path.join(save_path, "model.h5"))

    def evaluate(self, model=None):
        (x_train, y_train), (x_test, y_test) = mnist.load_data()

        x_test = self.input_preprocess(x_test)
        y_test = keras.utils.to_categorical(y_test, num_classes=10)

        model = load_model(model) if model else self.model
        test_loss = model.evaluate(x_test, y_test)
        print(test_loss)

    def input_preprocess(self, data):
        data = data.reshape(data.shape[0], self.n_inputs, self.n_inputs)
        data = data.astype('float32')
        data /= 255
        return data

    def profile_train_data(self, profile_save_path):
        (x_train, y_train), (x_test, y_test) = mnist.load_data()
        x_train = self.input_preprocess(x_train)
        output = self.model.predict(x_train)
        cls = np.argmax(output[0], axis=1)
        correct_idx = np.where(cls == y_train)[0]
        os.makedirs(profile_save_path, exist_ok=True)
        states_correct = output[1][correct_idx]
        np.save(os.path.join(profile_save_path, "states_profile.npy"), states_correct)

    def get_state_profile(self, inputs):
        inputs = self.input_preprocess(inputs)
        output = self.model.predict(inputs)
        return output[1]

if __name__ == "__main__":
    save_path = "test/rnn_model"

    lstm_classifier = MnistLSTMClassifier()
    # train an rnn model
    lstm_classifier.create_model()
    lstm_classifier.train(save_path)
    lstm_classifier.evaluate()

    # Load a trained model with return_sequence enabled.
    # profile_path = "test/output/profile_save"
    # lstm_classifier.load_hidden_state_model(os.path.join(save_path, "model.h5"))
    # lstm_classifier.profile_train_data(profile_path)

max_stars_repo_name: xiaoningdu/deepstellar

max_issues_repo_name: xiaoningdu/deepstellar

max_forks_repo_name: xiaoningdu/deepstellar


text:
from tensorflow.keras.models import load_model
from PIL import Image, ImageOps
import numpy as np

class TeachableMachine(object):
    '''
    Create your TeachableMachine object to run your exported AI models.
    '''
    __supported_types = ('keras', 'Keras', 'h5')

    def __init__(self, model_path='keras_model.h5', labels_file_path='labels.txt', model_type='h5') -> None:
        self.model_type = model_type.lower()
        self.labels_file_path = labels_file_path
        try:
            self.model = load_model(model_path)
        except IOError as e:
            print('*****Wrong model path*****')
            raise IOError from e
        except:
            raise '*****Error while loading model.*****'
        self.object_creation_status = self.model_type in self.__supported_types
        if self.object_creation_status:
            print('Teachable Machine Object is created successfully.')
        else:
            raise '*****Not supported model type: Select your model like "keras" or "h5".*****'

    def classify_image(self, img_path: str):
        '''To deploy your Teachable Machine Model on a computer/PC
            with .h5 extension using TensorFlow.

            Parameters:
            * img: Provide your img before classification process.

            Returns:
            * highest_prediciton: The highest prediction from the classes.
            * all_predictions: All values for all classes.
        '''
        try:
            image = Image.open(img_path)
        except FileNotFoundError as e:
            raise '*****Error in image path.*****' from e
        try:
            if self.object_creation_status:
                data = np.ndarray(shape=(1, 224, 224, 3), dtype=np.float32)
                size = (224, 224)
                image = ImageOps.fit(image, size, Image.ANTIALIAS)
                image_array = np.asarray(image)
                normalized_image_array = (
                    image_array.astype(np.float32) / 127.0) - 1
                data[0] = normalized_image_array
                all_predictions = self.model.predict(data)
                highest_class_id = np.argmax(all_predictions)
                return {
                    'highest_class_id': highest_class_id,
                    'all_predictions': all_predictions,
                }
        except:
            raise 'Error in classification process, retrain your model.'

max_stars_repo_name: MeqdadDev/teachable-machine

max_issues_repo_name: MeqdadDev/teachable-machine

max_forks_repo_name: MeqdadDev/teachable-machine


text:
import numpy as np
import tensorflow as tf

def discrete_attention(inp, fovea_x, fovea_y, delta, N):
    top_left_x = (fovea_x - delta + 1) / 2.0
    top_left_y = (fovea_y - delta + 1) / 2.0

    bottom_right_x = (fovea_x + delta + 1) / 2.0
    bottom_right_y = (fovea_y + delta + 1) / 2.0

    boxes = tf.concat([top_left_y, top_left_x, bottom_right_y, bottom_right_x], axis=1)

    inp = tf.expand_dims(inp, 3)

    result = tf.image.crop_and_resize(
        image=inp,
        boxes=boxes,
        box_ind=tf.range(tf.shape(inp)[0]),
        crop_size=(N, N))
    result = tf.squeeze(result, 3)
    return result

def DRAW_attention_2D(inp, fovea_x, fovea_y, delta, std, N, normalize=False):
    """
    Parameters
    ----------
    inp: Tensor (batch_size, B, A)
    fovea_x: Tensor (batch_size, 1)
    fovea_y: Tensor (batch_size, 1)
    delta: Tensor (batch_size, 1)
    std: Tensor (batch_size, 1)
    N: int
    normalize: bool
        Whether to normalize the filter before applying it.

    """
    B = int(inp.shape[1])
    A = int(inp.shape[2])

    fovea_x = (fovea_x + 1) * A/2
    fovea_y = (fovea_y + 1) * B/2

    mu_x = fovea_x + np.linspace(-A/2, A/2, N).reshape(1, -1) * delta
    mu_y = fovea_y + np.linspace(-B/2, B/2, N).reshape(1, -1) * delta
    std = std + mu_x - mu_x

    loc_x = tf.constant(np.arange(A) + 0.5, dtype=tf.float32)
    loc_y = tf.constant(np.arange(B) + 0.5, dtype=tf.float32)

    X_filt = gaussian_filter(mu_x, std, loc_x, normalize=normalize)
    Y_filt = gaussian_filter(mu_y, std, loc_y, normalize=normalize)
    return tf.matmul(Y_filt, tf.matmul(inp, X_filt, adjoint_b=True))

def gaussian_filter(mu, std, locations, normalize=False):
    """ Make a 1-D Gaussian filter.

    Parameters
    ----------
    mu: Tensor (batch_size, n_filters)
    std: Tensor (batch_size, n_filters)
    locations: Tensor (n_points)

    Returns
    -------
    filt: Tensor (batch_size, n_filters, n_points)

    """
    mu = mu[..., None]
    std = std[..., None]
    locations = tf.reshape(locations, (1, 1, -1))
    filt = tf.exp(-0.5 * ((mu - locations)**2) / std)
    if normalize:
        filt /= tf.sqrt(2 * np.pi) * std
    return filt

def apply_gaussian_filter(mu, std, locations, values, normalize=False):
    """ Create and apply a single 1-D gaussian filter. """
    filt = gaussian_filter(mu, std, locations, normalize)
    filt = tf.squeeze(filt, axis=[1])
    vision = tf.reduce_sum(values * filt, axis=-1, keepdims=True)
    return vision

class DRAW(object):
    def __init__(self, N):
        self.N = N

    def __call__(self, inp, fovea_x=None, fovea_y=None, delta=None, sigma=None):
        reshape = len(inp.shape) == 2
        if reshape:
            s = int(np.sqrt(int(inp.shape[1])))
            inp = tf.reshape(inp, (-1, s, s))

        batch_size = tf.shape(inp)[0]
        if fovea_x is None:
            fovea_x = tf.zeros((batch_size, 1))
        if fovea_y is None:
            fovea_y = tf.zeros((batch_size, 1))
        if delta is None:
            delta = tf.ones((batch_size, 1))
        if sigma is None:
            sigma = tf.ones((batch_size, 1))

        glimpse = DRAW_attention_2D(
            inp, fovea_x=fovea_x, fovea_y=fovea_y, delta=delta, std=sigma, N=self.N)

        if reshape:
            glimpse = tf.reshape(glimpse, (-1, self.N*self.N))

        return glimpse

class DiscreteAttn(object):
    def __init__(self, N):
        self.N = N

    def __call__(self, inp, fovea_x=None, fovea_y=None, delta=None):
        reshape = len(inp.shape) == 2
        if reshape:
            s = int(np.sqrt(int(inp.shape[1])))
            inp = tf.reshape(inp, (-1, s, s))

        batch_size = tf.shape(inp)[0]
        if fovea_x is None:
            fovea_x = tf.zeros((batch_size, 1))
        if fovea_y is None:
            fovea_y = tf.zeros((batch_size, 1))
        if delta is None:
            delta = tf.ones((batch_size, 1))

        glimpse = discrete_attention(
            inp, fovea_x=fovea_x, fovea_y=fovea_y, delta=delta, N=self.N)

        if reshape:
            glimpse = tf.reshape(glimpse, (-1, self.N*self.N))

        return glimpse

max_stars_repo_name: alcinos/dps

max_issues_repo_name: alcinos/dps

max_forks_repo_name: alcinos/dps