text:
import numpy as np
import matplotlib
matplotlib.rc('text', usetex=True)
import matplotlib.pyplot as plt
plt.rc('text', usetex=True)
plt.rc('font', family='serif')

# indices for axis 1 in data
SNR = 0
IYT = 1
ACC = 2

w_p = 3.
w_s = 2.5
font_primary = 24
font_second = 20
font_legend = 16
SNR_XLIM = 120

def fault_tolerance_unique_obj_ADef(data_list, legend=False, save=False, labels=None, plot_name=None):

    XLIM = 3

    fig, ax1 = plt.subplots(1, 1, figsize=(6, 4))

    pcolor_list = ['navy', 'maroon', 'forestgreen', 'black']
    if legend:
        assert len(data_list) == len(labels)

    for i, data in enumerate(data_list):
        ax1.plot(data[:, SNR], data[:, IYT], label=labels[i], c=pcolor_list[i], linewidth=w_p)

    ax1.set_xlim(0,XLIM)
    ax1.set_ylim(0., 3.3)
    ax1.set_ylabel(r'$I(T; Y)$', fontsize=font_primary, labelpad=8)#, color='navy')
    ax1.set_xlabel(r'Max. Norm', fontsize=font_primary)

    # Major ticks every 10, minor ticks every 5
    #major_ticks_x = np.arange(5, XLIM+1, 5)
    major_ticks_x = np.arange(1, XLIM+1, 1)
    minor_ticks_x = np.arange(0.5, XLIM+1, 0.5)
    ax1.set_xticks(major_ticks_x)
    ax1.set_xticks(minor_ticks_x, minor=True)
    ax1.set_yticks([0., 0.5, 1.0, 1.5, 2.0, 2.5, 3.0])
    ax1.set_yticks([.25, .75, 1.25, 1.75, 2.25, 2.75, 3.0], minor=True)

    # And a corresponding grid
    ax1.grid(which='both')
    if legend:
        plt.legend(fontsize=font_legend, shadow='true', edgecolor='k')

    '''
    ax2 = ax1.twinx()
    #scolor_list = ['lightsteelblue', 'indianred', 'lightsalmon']
    for i, data in enumerate(data_list):
        ax2.plot(data[:, SNR], data[:, ACC] * 100, linewidth=w_s, c=pcolor_list[i], linestyle='--')
    ax2.set_ylabel(r'Accuracy (\%)', fontsize=font_primary, labelpad=10)
    ax2.tick_params('y', colors='k', direction='in', labelsize=font_second, length=0)
    ax2.set_ylim(0, 100)
    ax2.set_yticks([10, 30, 50, 70, 90])
    '''

    # Or if you want different settings for the grids:
    ax1.grid(which='minor', linestyle='-', linewidth=0.5, c='lightgray')
    ax1.grid(which='major', linestyle='-', c='darkgray')
    ax1.tick_params(which='major', direction='in', labelsize=font_second, length=8)
    ax1.tick_params(which='minor', direction='in', labelsize=font_second, length=5)
    ax1.set_facecolor('white')
    plt.tight_layout()
    plt.show()
    if save:
        fig.savefig(plot_name, bbox_inches='tight', format='eps')

def fault_tolerance_unique_obj(data_list, legend=False, axis_labels='both', save=False, min_snr=0, max_snr=SNR_XLIM, labels=None, modelname=None):
    """
    Figure 1. fault tolerance plot for unique attack objectives for NIPS SECML.

    @param data_list: takes up to four different data series
                     (if more are needed, add more colours to pcolor_list.
    @param legend: boolean, if legend should be used
    @param axis_labels: either 'both', 'mi', or 'acc''
    @save boolean: boolean, if plot should be saved to file as *.eps
    @label: list of series labels, to be used if save=True
    @modelname: descriptive name to be supplied if save=True
    """
    fig, ax1 = plt.subplots(1, 1, figsize=(6, 4))

    pcolor_list = ['lightgray', 'navy', 'maroon', 'forestgreen']
    if legend:
        assert len(data_list) == len(labels)
    for i, data in enumerate(data_list):
        ax1.plot(data[:, SNR], data[:, IYT], label=labels[i], c=pcolor_list[i], linewidth=w_p)
    ax1.set_xlim(max_snr, min_snr + 1)
    ax1.set_xlabel(r'SNR', fontsize=font_primary)
    ax1.set_ylim(0., 3.3)
    if axis_labels == 'both' or axis_labels == 'mi':
        ax1.set_ylabel(r'$I(T; Y)$', fontsize=font_primary, labelpad=8)#, color='navy')
    else:
        ax1.yaxis.set_ticklabels([])
    # Major ticks every 10, minor ticks every 5
    major_ticks_x = np.arange(10, max_snr + 1, 10)[::-1]
    minor_ticks_x = np.arange(5, max_snr + 1, 5)[::-1]
    ax1.set_xticks(major_ticks_x)
    ax1.set_xticks(minor_ticks_x, minor=True)
    ax1.set_yticks([.25, .75, 1.25, 1.75, 2.25, 2.75, 3.0], minor=True)
    ax1.set_yticks([0, 0.5, 1.0, 1.5, 2.0, 2.5, 3.0])

    # And a corresponding grid
    ax1.grid(which='both')
    if legend:
        plt.legend(fontsize=font_legend, edgecolor='k', loc='lower left') # borderaxespad=.75, )

    ax2 = ax1.twinx()
    #scolor_list = ['lightsteelblue', 'indianred', 'lightsalmon']
    for i, data in enumerate(data_list):
        ax2.plot(data[:, SNR], data[:, ACC] * 100, linewidth=w_s, c=pcolor_list[i], linestyle='--')
    ax2.tick_params('y', colors='k', direction='in', labelsize=font_second, length=0)
    ax2.set_ylim(0, 100)
    if axis_labels == 'both' or axis_labels == 'acc':
        ax2.set_ylabel(r'Accuracy (\%)', fontsize=font_primary, labelpad=10)
        ax2.set_yticks([10, 30, 50, 70, 90])
    else:
        ax2.set_yticks([])
    #ax2.axvline(x=5, ymin=0, ymax=1, c='pink') # rose SNR criterion

    # Or if you want different settings for the grids:
    ax1.grid(which='minor', linestyle='-', linewidth=0.5, c='lightgray')
    ax1.grid(which='major', linestyle='-', c='darkgray')
    ax1.tick_params(which='major', direction='in', labelsize=font_second, length=5)
    ax1.tick_params(which='minor', direction='in', labelsize=font_second, length=3)
    ax1.set_facecolor('white')
    plt.tight_layout()
    plt.show()
    if save:
        PLT_NAME = modelname + 'fault.eps'
        fig.savefig(PLT_NAME, bbox_inches='tight', pad_inches=0.1, format='eps')

def fault_tolerance_plot_rot_from_list_30(rot, data_list, legend=False, save=False, labels=None, modelname=None):
    """
    Figure 2. b) fault tolerance plot for rotation for NIPS SECML.

    @param rot: x-axis steps in radians
    @param data_list:
    @param legend: boolean, if legend should be used
    @save boolean: boolean, if plot should be saved to file as *.eps
    @label: list of series labels, to be used if save=True
    @modelname: descriptive name to be supplied if save=True
    """
    fig, ax1 = plt.subplots(1, 1, figsize=(6, 4))

    pcolor_list = ['navy', 'maroon', 'forestgreen', 'lightgray']
    if legend:
        assert len(data_list) == len(labels)
    for i, data in enumerate(data_list):
        ax1.plot(rot * (180 / np.pi), data[:, IYT], label=labels[i],c=pcolor_list[i], linewidth=w_p)

    ax1.set_xlim(0, (np.pi / 6) * (180 / np.pi))
    ax1.set_ylim(0., 3.3)
    ax1.set_ylabel(r'$I(T; Y)$', fontsize=font_primary, labelpad=10)
    ax1.set_xlabel(r'Rotation (Deg.)', fontsize=font_primary)

    # Major ticks every 10, minor ticks every 5
    major_ticks_x = np.arange(0, 31, 10)[::-1]
    minor_ticks_x = np.arange(0, 31, 5)[::-1]
    ax1.set_xticks(major_ticks_x)
    ax1.set_xticks(minor_ticks_x, minor=True)
    ax1.set_yticks([0., 0.5, 1.0, 1.5, 2.0, 2.5, 3.0])
    ax1.set_yticks([.25, .75, 1.25, 1.75, 2.25, 2.75, 3.0], minor=True)

    # And a corresponding grid
    ax1.grid(which='both')
    if legend:
        plt.legend(fontsize=font_legend, shadow='true', edgecolor='k')
        #plt.legend(fontsize=font_legend, shadow='true', edgecolor='k', bbox_to_anchor=(0.94, 0.55))

    ax2 = ax1.twinx()
    for i, data in enumerate(data_list):
        ax2.plot(rot * (180 / np.pi), data[:, ACC] * 100, linewidth=w_s, c=pcolor_list[i], linestyle='--')
    ax2.set_ylabel(r'Accuracy (\%)', fontsize=font_primary, labelpad=10)
    ax2.tick_params('y', colors='k', direction='in', labelsize=font_second, length=0)
    ax2.set_ylim(0, 100)
    ax2.set_yticks([10, 30, 50, 70, 90])

    # Or if you want different settings for the grids:
    ax1.grid(which='minor', linestyle='-', linewidth=0.5, c='lightgray')
    ax1.grid(which='major', linestyle='-', c='darkgray')
    ax1.tick_params(which='major', direction='in', labelsize=font_second, length=8)
    ax1.tick_params(which='minor', direction='in', labelsize=font_second, length=5)
    ax1.set_facecolor('white')
    plt.tight_layout()
    plt.show()
    if save:
        PLT_NAME = modelname + '_fault_rot30.eps'
        fig.savefig(PLT_NAME, bbox_inches='tight', pad_inches=0.1, format='eps')

def fault_tolerance_plot_rot_from_list(rot, data_list, legend=False, save=False, max_rot=180, labels=None, modelname=None):
    """
    @param rot: x-axis steps in radians
    @param data_list:
    @param legend: boolean, if legend should be used
    @save boolean: boolean, if plot should be saved to file as *.eps
    @label: list of series labels, to be used if save=True
    @modelname: descriptive name to be supplied if save=True
    """
    fig, ax1 = plt.subplots(1, 1, figsize=(6, 4))

    pcolor_list = ['navy', 'maroon', 'forestgreen', 'lightgray']
    if legend:
        assert len(data_list) == len(labels)
    for i, data in enumerate(data_list):
        ax1.plot(rot * (180 / np.pi), data[:, IYT], label=labels[i],c=pcolor_list[i], linewidth=w_p)

    ax1.set_xlim(0, max_rot)
    ax1.set_ylim(0., 3.3)
    ax1.set_ylabel(r'$I(T; Y)$', fontsize=font_primary, labelpad=10)
    ax1.set_xlabel(r'Rotation (Degrees)', fontsize=font_primary)
    # Major ticks every 10, minor ticks every 5
    major_ticks_x = np.arange(0, max_rot + 1, 60)[::-1]
    minor_ticks_x = np.arange(0, max_rot + 1, 20)[::-1]
    ax1.set_xticks(major_ticks_x)
    ax1.set_xticks(minor_ticks_x, minor=True)
    ax1.set_yticks([0., 0.5, 1.0, 1.5, 2.0, 2.5, 3.0])
    ax1.set_yticks([.75, 1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0], minor=True)

    # And a corresponding grid
    ax1.grid(which='both')
    if legend:
        plt.legend(fontsize=font_legend, shadow='true', edgecolor='k', bbox_to_anchor=(0.94, 0.55))
    ax2 = ax1.twinx()
    for i, data in enumerate(data_list):
        ax2.plot(rot * (180 / np.pi), data[:, ACC] * 100, linewidth=w_s, c=pcolor_list[i], linestyle='--')
    ax2.set_ylabel(r'Accuracy (\%)', fontsize=font_primary, labelpad=15)
    ax2.tick_params('y', colors='k', direction='in', labelsize=font_primary, length=0)
    ax2.set_ylim(0, 100)
    ax2.set_yticks([10, 30, 50, 70, 90])

    # Or if you want different settings for the grids:
    ax1.grid(which='minor', linestyle='-', linewidth=0.5, c='lightgray')
    ax1.grid(which='major', linestyle='-', c='darkgray')
    ax1.tick_params(which='major', direction='in', labelsize=font_primary, length=8)
    ax1.tick_params(which='minor', direction='in', labelsize=font_primary, length=5)
    ax1.set_facecolor('white')
    plt.show()
    if save:
        PLT_NAME = modelname + '_fault_rot360.eps'
        fig.savefig(PLT_NAME, bbox_inches='tight', pad_inches=0.1, format='eps')

def fault_tolerance_plot_from_list(data_list, legend=False, save=False, max_snr=SNR_XLIM, labels=None, modelname=None):
    """
    For reproducing Fig. 5 in "A Rate-Distortion Theory of Adversarial Examples"
    @param data_list:
    @param legend: boolean, if legend should be used
    @save boolean: boolean, if plot should be saved to file as *.eps
    @label: list of series labels, to be used if save=True
    @modelname: descriptive name to be supplied if save=True
    """
    fig, ax1 = plt.subplots(1, 1, figsize=(6, 4))

    line_sty = ['-', '--', ':']
    if legend:
        assert len(data_list) == len(labels)
    for i, data in enumerate(data_list):
        ax1.plot(data[:, SNR], data[:, IYT], label=labels[i],
                 c='navy', linewidth=w_p, linestyle=line_sty[i])

    ax1.set_xlim(max_snr, 1)
    ax1.set_ylim(0.5, 3.0)
    ax1.set_ylabel(r'$I(T; Y)$', fontsize=font_primary, color='navy')
    ax1.set_xlabel(r'SNR', fontsize=font_primary)
    # Major ticks every 10, minor ticks every 5
    major_ticks_x = np.arange(10, max_snr + 1, 20)[::-1]
    minor_ticks_x = np.arange(5, max_snr + 1, 10)[::-1]
    ax1.set_xticks(major_ticks_x)
    ax1.set_xticks(minor_ticks_x, minor=True)
    ax1.set_yticks([1.0, 1.5, 2.0, 2.5, 3.0])
    ax1.set_yticks([.75, 1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0], minor=True)

    # And a corresponding grid
    ax1.grid(which='both')
    if legend:
        plt.legend(fontsize=font_legend, shadow='true', edgecolor='k')
    ax2 = ax1.twinx()
    for i, data in enumerate(data_list):
        ax2.plot(data[:, SNR], data[:, ACC] * 100, linewidth=w_s,
                 c='lightsteelblue', linestyle=line_sty[i])
    ax2.set_ylabel('Test Accuracy (\%)', color='lightsteelblue', fontsize=font_second, labelpad=8)
    ax2.tick_params('y', colors='k', direction='in', labelsize=font_second, length=5)
    ax2.set_ylim(0, 100)
    ax2.set_yticks([10, 30, 50, 70, 90])

    # Or if you want different settings for the grids:
    ax1.grid(which='minor', linestyle='-', linewidth=0.5, c='lightgray')
    ax1.grid(which='major', linestyle='-', c='darkgray')
    ax1.tick_params(which='major', direction='in', labelsize=font_primary, length=8)
    ax1.tick_params(which='minor', direction='in', labelsize=font_primary, length=5)
    ax1.set_facecolor('white')
    plt.show()
    if save:
        PLT_NAME = modelname + '_fault.eps'
        fig.savefig(PLT_NAME, bbox_inches='tight', pad_inches=0.1, format='eps')

def fault_tolerance_plot(data, legend=False, save=False, max_snr=SNR_XLIM, label=None, modelname=None):

    fig, ax1 = plt.subplots(1, 1, figsize=(6, 4))

    ax1.plot(data[:, SNR], data[:, IYT], label=label, c='navy', linewidth=w_p)
    ax1.set_xlim(max_snr, 1)
    ax1.set_ylim(0., 3.33)

    ax1.set_ylabel(r'$I(T; Y)$', fontsize=font_primary, color='navy')
    ax1.set_xlabel(r'SNR', fontsize=font_primary)

    # Major ticks every 10, minor ticks every 5
    major_ticks_x = np.arange(10, max_snr + 1, 10)[::-1]
    minor_ticks_x = np.arange(5, max_snr  + 1, 5)[::-1]

    ax1.set_xticks(major_ticks_x)
    ax1.set_xticks(minor_ticks_x, minor=True)
    ax1.set_yticks([0., 0.5, 1.0, 1.5, 2.0, 2.5, 3.0, 3.33])
    ax1.set_yticks([.75, 1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0, 3.33], minor=True)

    # And a corresponding grid
    ax1.grid(which='both')

    #plt.legend(fontsize=font_legend, facecolor='antiquewhite', shadow='true', edgecolor='k')
    if legend:
        plt.legend(fontsize=font_legend, shadow='true', edgecolor='k')

    ax2 = ax1.twinx()

    ax2.plot(data[:, SNR], data[:, ACC] * 100, linewidth=w_s, c='lightsteelblue')
    #ax2.plot(ft_inf[:, SNR], ft_inf[:, ACC]*100, linewidth=w_s, c='lightsteelblue', linestyle='--')
    #ax2.plot(ft_noise[:, SNR], ft_noise[:, ACC]*100, linewidth=w_s, c='lightsteelblue', linestyle=':')

    ax2.set_ylabel('Test Accuracy (\%)', color='lightsteelblue', fontsize=font_second, labelpad=8)
    ax2.tick_params('y', colors='k', direction='in', labelsize=font_second, length=5)
    ax2.set_ylim(0, 100)
    ax2.set_yticks([10, 30, 50, 70, 90])

    # Or if you want different settings for the grids:
    ax1.grid(which='minor', linestyle='-', linewidth=0.5, c='lightgray')
    ax1.grid(which='major', linestyle='-', c='darkgray')
    ax1.tick_params(which='major', direction='in', labelsize=font_primary, length=8)
    ax1.tick_params(which='minor', direction='in', labelsize=font_primary, length=5)

    #ax1.set_facecolor('oldlace')
    ax1.set_facecolor('white')

    plt.show()

    if save:
        PLT_NAME = modelname + '_fault.eps'
        fig.savefig(PLT_NAME, bbox_inches='tight', pad_inches=0.1, format='eps')

def fault_tolerance_rotation_plot(rot, data, legend=False, save=False, label=None, modelname=None):

    fig, ax1 = plt.subplots(1, 1, figsize=(6, 4))

    ax1.plot(rot * (180 / np.pi), data[:, IYT], label=label, c='navy', linewidth=w_p)

    #ax1.set_xlim(65, 1)
    ax1.set_xlim(0, np.pi/6 * (180 / np.pi))
    ax1.set_ylim(0.5, 3.0)

    ax1.set_ylabel(r'$I(T; Y)$', fontsize=font_primary, color='navy')
    ax1.set_xlabel(r'Rotation (Deg.)', fontsize=font_primary)

    # Major ticks every 10, minor ticks every 5
    major_ticks_x = np.arange(0, 31, 10)[::-1]
    minor_ticks_x = np.arange(0, 31, 5)[::-1]

    ax1.set_xticks(major_ticks_x)
    ax1.set_xticks(minor_ticks_x, minor=True)
    ax1.set_yticks([1.0, 1.5, 2.0, 2.5, 3.0])
    ax1.set_yticks([.75, 1.0, 1.25, 1.5, 1.75, 2.0, 2.25, 2.5, 2.75, 3.0], minor=True)

    # And a corresponding grid
    ax1.grid(which='both')

    #plt.legend(fontsize=font_legend, facecolor='antiquewhite', shadow='true', edgecolor='k')
    if legend:
        plt.legend(fontsize=font_legend, shadow='true', edgecolor='k')

    ax2 = ax1.twinx()

    ax2.plot(rot * (180 / np.pi), data[:, ACC] * 100, linewidth=w_s, c='lightsteelblue')

    ax2.set_ylabel('Test Accuracy (\%)', color='lightsteelblue', fontsize=font_second, labelpad=8)
    ax2.tick_params('y', colors='k', direction='in', labelsize=font_second, length=5)
    ax2.set_ylim(0, 100)
    ax2.set_yticks([10, 30, 50, 70, 90])

    # Or if you want different settings for the grids:
    ax1.grid(which='minor', linestyle='-', linewidth=0.5, c='lightgray')
    ax1.grid(which='major', linestyle='-', c='darkgray')
    ax1.tick_params(which='major', direction='in', labelsize=font_primary, length=8)
    ax1.tick_params(which='minor', direction='in', labelsize=font_primary, length=5)

    #ax1.set_facecolor('oldlace')
    ax1.set_facecolor('white')

    plt.show()

    if save:
        PLT_NAME = modelname + '_fault.eps'
        fig.savefig(PLT_NAME, bbox_inches='tight', pad_inches=0.1, format='eps')

def grid_visual(data):
    """
    This function displays a grid of images to show full misclassification
    :param data: grid data of the form;
        [nb_classes : nb_classes : img_rows : img_cols : nb_channels]
    :return: if necessary, the matplot figure to reuse
    """
    # build a rectangle in axes coords
    left, width = .25, .5
    bottom, height = .25, .5
    right = left + width
    top = bottom + height
    fontsize = 18
    import matplotlib.pyplot as plt

    # Ensure interactive mode is disabled and initialize our graph
    plt.ioff()
    figure = plt.figure(figsize=(8, 8))
    figure.canvas.set_window_title('Cleverhans: Grid Visualization')

    # Add the images to the plot
    num_cols = data.shape[0]
    num_rows = data.shape[1]
    #num_channels = data.shape[4]
    current_row = 0
    for y in range(num_rows):
        for x in range(num_cols):
            figure.add_subplot(num_rows, num_cols, (x + 1) + (y * num_cols))
            #plt.axis('off')
            ax = plt.gca()
            ax.set_yticklabels([])
            ax.set_xticklabels([])
            ax.tick_params(axis='both', length=0)
            plt.imshow(data[x, y, :, :], cmap='gray')
            if y == 0:
                ax.set_title('%d' % x, fontsize=fontsize)
            if x == 0:
                ax.set_ylabel('%d' % y, rotation='horizontal', va='center',
                              fontsize=fontsize, labelpad=10)

    #plt.tight_layout()
    #figure.text(0.5, 1.01, r'\textbf{Target Class}', ha='center', fontsize=24)
    #figure.text(-0.02, 0.5*(bottom+top), r'\textbf{Source Class}',
    #         ha='center', va='center', fontsize=24, rotation='vertical')
    # Draw the plot and return
    plt.show()
    return figure


text:
import logging
import os
import sys
import itertools
import json
import re
import numpy as np
from collections import defaultdict

import torch

from misc import util

class ExecutorTrainer(object):

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

    def do_rollout(self, batch, student, teacher, is_eval, bc=False):

        init_poses = []
        goal_viewpoints = []
        instructions = []

        batch_size = len(batch)

        for item in batch:
            pose = (item['scan'], item['path'][0], item['heading'], 0)
            init_poses.append(pose)
            goal_viewpoints.append(item['path'][-1])
            instructions.append(item['instruction'])

        states = student.init(init_poses, instructions, is_eval)

        while not student.has_terminated():
            teacher_actions = teacher(states, goal_viewpoints)
            student_actions = student.act(
                states, teacher_actions=teacher_actions, bc=bc)
            states = states.step(student_actions)

    def train(self, datasets, student, teacher):

        max_iters = self.config.trainer.max_iters
        log_every = self.config.trainer.log_every
        bc = self.config.trainer.bc
        metric_name = self.config.trainer.main_metric_name

        i_iter = 0
        total_loss = 0

        best_metric = {
                'val'  : teacher.init_metric_value(metric_name),
            }

        for batch in itertools.cycle(datasets['train'].iterate_batches()):

            i_iter += 1

            self.do_rollout(batch, student, teacher, False, bc=bc)

            loss = student.learn()
            total_loss += loss

            if i_iter % log_every == 0:

                avg_loss = total_loss / log_every
                total_loss = 0

                log_str = 'Train iter %d (%d%%): ' % \
                    (i_iter, i_iter / max_iters * 100)
                log_str += 'loss = %.4f' % avg_loss

                logging.info('')
                logging.info(log_str)

                # Save last model
                student.save('last')

                # Save best model
                for split in best_metric.keys():
                    eval_info = self.evaluate(datasets[split], student, teacher)
                    eval_preds = eval_info['pred']
                    self.save_preds('last_%s' % split, eval_preds)

                    eval_metric = eval_info['metric'][metric_name]
                    if teacher.is_better(metric_name, eval_metric, best_metric[split]):
                        logging.info('New best %s: %.1f' % (split, eval_metric))
                        best_metric[split] = eval_metric
                        save_name = 'best_%s' % split
                        student.save(save_name)
                        self.save_preds(save_name, eval_preds)

            if i_iter >= max_iters:
                break

    def evaluate(self, dataset, student, teacher, save_pred=False):

        all_preds = {}

        with torch.no_grad():
            for i, batch in enumerate(dataset.iterate_batches()):
                # Make predictions
                scans = []
                init_poses = []
                gold_paths = []
                goal_viewpoints = []
                instructions = []

                batch_size = len(batch)
                for item in batch:
                    pose = (item['scan'], item['path'][0], item['heading'], 0)
                    init_poses.append(pose)
                    scans.append(item['scan'])
                    gold_paths.append(item['path'])
                    goal_viewpoints.append(item['path'][-1])
                    instructions.append(item['instruction'])

                pred_paths, _ = student.predict(init_poses, instructions)
                pred_goal_viewpoints = [path[-1] for path in pred_paths]

                metrics = []
                zipped_info = zip(scans, pred_paths, gold_paths)
                for scan, pred_path, gold_path in zipped_info:
                    metric = teacher.eval(scan, pred_path, gold_path)
                    metrics.append(metric)

                for item, path, metric in zip(batch, pred_paths, metrics):
                    new_item = { 'pred_path' : path }
                    new_item.update(metric)
                    new_item.update(item)
                    if 'new_instructions' in new_item:
                        del new_item['new_instructions']
                    if 'chunk_view' in new_item:
                        del new_item['chunk_view']
                    instr_id = new_item['instr_id']
                    all_preds[instr_id] = new_item

        all_metrics = defaultdict(list)
        for item in all_preds.values():
            for metric_name in teacher.get_metrics():
                all_metrics[metric_name].append(item[metric_name])

        avg_metric = {}
        for metric_name in teacher.get_metrics():
            avg_metric[metric_name] = np.average(all_metrics[metric_name])

        log_str = 'Evaluation on %s: ' % dataset.split
        log_str += teacher.format_metrics(avg_metric)
        logging.info(log_str)

        if save_pred:
            self.save_preds(dataset.split, all_preds)

        eval_info = {
                'metric': avg_metric,
                'pred'  : all_preds,
            }

        return eval_info

    def save_preds(self, filename, all_preds):
        file_path = '%s/%s' % (self.config.experiment_dir, filename + '.pred')
        with open(file_path, 'w') as f:
            json.dump(all_preds, f, indent=2)
        logging.info('Saved eval info to %s' % file_path)

ext: py

max_stars_repo_name: khanhptnk/iliad

max_issues_repo_name: khanhptnk/iliad

max_forks_repo_name: khanhptnk/iliad

reason: import numpy


text:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 25 09:46:47 2019
@author: yujijun
Description: This is a script for CistromeDB metadata statistic and analysis:
we use the six index from http://cistrome.org/db/#/about to do filter.
Input:
DC_haveProcessed_20190506_filepath.xls_compiled_database.xls
and DC_haveProcessed_20190506_filepath.xls

Output:
01-Homo_sapiens_ca_DNase.csv
02-Homo_sapiens_ca_DNase_QC(906).txt
"""
import pandas as pd
import numpy as np
import warnings
from collections import Counter
warnings.filterwarnings('ignore')

Cutoff_FastQC = 25
Cutoff_UniquelyMappedRatio = 0.6
Cutoff_PBC = 0.8
Cutoff_FRiP = 0.01
Cutoff_MergedPeaksUnionDHSRatio = 0.7
Cutoff_PeaksFoldChange10 = 500

Input_path = '/Users/yujijun/Documents/01-Work/02-HiglassProject/01-DataFilter/Original_data/'
metadata_name = '%s/DC_haveProcessed_20190506_filepath.xls' %Input_path
QC_name = '%s/DC_haveProcessed_20190506_filepath.xls_compiled_database.xls' %Input_path
Output_path = '/Users/yujijun/Documents/01-Work/02-HiglassProject/01-DataFilter/Output_data/'

#######step one: data selected #########
#Data import
metadata = pd.read_csv(metadata_name, sep="\t")
QCdata = pd.read_csv(QC_name,sep="\t")

#merge the data（This is the all data）
data_merge = pd.merge(metadata, QCdata, on="DatsetId")

#all homo sapiens data
data_merge_Homo_sapiens = data_merge[(data_merge.Species == "Homo sapiens")]

#all chromosome accessiblility data in homo sapiens
data_merge_Homo_sapiens_ca = data_merge_Homo_sapiens[(data_merge_Homo_sapiens.Factor_type == "ca")]
len(data_merge_Homo_sapiens[(data_merge_Homo_sapiens.Factor == "ATAC-seq")])
len(data_merge_Homo_sapiens[(data_merge_Homo_sapiens.Factor == "DNase")])

#output of the DNase in ca
data_merge_Homo_sapiens_ca_DNase = data_merge_Homo_sapiens_ca[(data_merge_Homo_sapiens_ca.Factor == "DNase")]
data_merge_Homo_sapiens_ca_DNase.to_csv('%s/01-Homo_sapiens_ca_DNase.csv' %Output_path, index=False)

#############Step two:QC#################
data_merge_Homo_sapiens_ca_DNase_afterQC = data_merge_Homo_sapiens_ca_DNase[(data_merge_Homo_sapiens_ca_DNase["FastQC"] >= Cutoff_FastQC)
                                                  & (data_merge_Homo_sapiens_ca_DNase["UniquelyMappedRatio"]>=Cutoff_UniquelyMappedRatio)
                                                  & (data_merge_Homo_sapiens_ca_DNase["PBC"]>=Cutoff_PBC)
                                                  & (data_merge_Homo_sapiens_ca_DNase["FRiP"]>=Cutoff_FRiP)
                                                  & (data_merge_Homo_sapiens_ca_DNase["MergedPeaksUnionDHSRatio"]>=Cutoff_MergedPeaksUnionDHSRatio)
                                                  & (data_merge_Homo_sapiens_ca_DNase["PeaksFoldChange10"] >=Cutoff_PeaksFoldChange10)]

data_merge_Homo_sapiens_ca_DNase_afterQC.to_csv('%s/02-Homo_sapiens_ca_DNase_QC(906).txt' %Output_path, index=False)

max_stars_repo_name: yujijun/Higlass

max_issues_repo_name: yujijun/Higlass

max_forks_repo_name: yujijun/Higlass


text:
import pandas
import pyomo
import pyomo.opt
import pyomo.environ as pe
import logging

class MultiCommodityInterdiction:
    """A class to compute multicommodity flow interdictions."""

    def __init__(self, nodefile, node_commodity_file, arcfile, arc_commodity_file, attacks=0):
        """
        All the files are CSVs with columns described below.  Attacks is the number of attacks.

        - nodefile:
            Node

        Every node must appear as a line in the nodefile.  You can have additional columns as well.

        - node_commodity_file:
            Node,Commodity,SupplyDemand

        Every commodity node imbalance that is not zero must appear in the node_commodity_file

        - arcfile:
            StartNode,EndNode,Capacity,Attackable

        Every arc must appear in the arcfile.  Also the arcs total capacity and whether we can attack this arc.

        - arc_commodity_file:
            StartNode,EndNode,Commodity,Cost,Capacity

        This file specifies the costs and capacities of moving each commodity across each arc.  If an (node, node, commodity) tuple does not appear in this file, then it means the commodity cannot flow across that edge.
        """
        # Read in the node_data
        self.node_data = pandas.read_csv(nodefile)
        self.node_data.set_index(['Node'], inplace=True)
        self.node_data.sort_index(inplace=True)
        # Read in the node_commodity_data
        self.node_commodity_data = pandas.read_csv(node_commodity_file)
        self.node_commodity_data.set_index(['Node','Commodity'], inplace=True)
        self.node_commodity_data.sort_index(inplace=True)
        # Read in the arc_data
        self.arc_data = pandas.read_csv(arcfile)
        self.arc_data.set_index(['StartNode','EndNode'], inplace=True)
        self.arc_data.sort_index(inplace=True)
        # Read in the arc_commodity_data
        self.arc_commodity_data = pandas.read_csv(arc_commodity_file)
        self.arc_commodity_data['xbar'] = 0
        self.arc_commodity_data.set_index(['StartNode','EndNode','Commodity'], inplace=True)
        self.arc_commodity_data.sort_index(inplace=True)
        # Can df.reset_index() to go back

        self.attacks = attacks

        self.node_set = self.node_data.index.unique()
        self.commodity_set = self.node_commodity_data.index.levels[1].unique()
        self.arc_set = self.arc_data.index.unique()

        # Compute nCmax
        self.nCmax = len(self.node_set) * self.arc_commodity_data['Cost'].max()

        self.createPrimal()
        self.createInterdictionDual()

    def createPrimal(self):
        """Create the primal pyomo model.

        This is used to compute flows after interdiction.  The interdiction is stored in arc_commodity_data.xbar."""

        model = pe.ConcreteModel()
        # Tell pyomo to read in dual-variable information from the solver
        model.dual = pe.Suffix(direction=pe.Suffix.IMPORT)

        # Add the sets
        model.node_set = pe.Set( initialize=self.node_set )
        model.edge_set = pe.Set( initialize=self.arc_set, dimen=2)
        model.commodity_set = pe.Set( initialize=self.commodity_set )

        # Create the variables
        model.y = pe.Var(model.edge_set*model.commodity_set, domain=pe.NonNegativeReals)
        model.UnsatSupply = pe.Var(model.node_set*model.commodity_set, domain=pe.NonNegativeReals)
        model.UnsatDemand = pe.Var(model.node_set*model.commodity_set, domain=pe.NonNegativeReals)

        # Create the objective
        def obj_rule(model):
            return  sum( (data['Cost']+data['xbar']*(2*self.nCmax+1))*model.y[e] for e,data in self.arc_commodity_data.iterrows()) + sum(self.nCmax*(model.UnsatSupply[n] + model.UnsatDemand[n]) for n,data in self.node_commodity_data.iterrows())
        model.OBJ = pe.Objective(rule=obj_rule, sense=pe.minimize)

        # Create the constraints, one for each node
        def flow_bal_rule(model, n,k):
            tmp = self.arc_data.reset_index()
            successors = tmp.ix[ tmp.StartNode == n, 'EndNode'].values
            predecessors = tmp.ix[ tmp.EndNode == n, 'StartNode'].values
            lhs = sum(model.y[(i,n,k)] for i in predecessors) - sum(model.y[(n,i,k)] for i in successors)
            imbalance = self.node_commodity_data['SupplyDemand'].get((n,k),0)
            supply_node = int(imbalance < 0)
            demand_node = int(imbalance > 0)
            rhs = (imbalance + model.UnsatSupply[n,k]*(supply_node) - model.UnsatDemand[n,k]*(demand_node))
            constr = (lhs == rhs)
            if isinstance(constr, bool):
                return pe.Constraint.Skip
            return constr

        model.FlowBalance = pe.Constraint(model.node_set*model.commodity_set, rule=flow_bal_rule)

        # Capacity constraints, one for each edge and commodity
        def capacity_rule(model, i, j, k):
            capacity = self.arc_commodity_data['Capacity'].get((i,j,k),-1)
            if capacity < 0:
                return pe.Constraint.Skip
            return model.y[(i,j,k)] <= capacity

        model.Capacity = pe.Constraint(model.edge_set*model.commodity_set, rule=capacity_rule)

        # Joint capacity constraints, one for each edge
        def joint_capacity_rule(model, i, j):
            capacity = self.arc_data['Capacity'].get((i,j), -1)
            if capacity < 0:
                return pe.Constraint.Skip
            return sum(model.y[(i,j,k)] for k in self.commodity_set) <= capacity

        model.JointCapacity = pe.Constraint(model.edge_set, rule=joint_capacity_rule)

        # Store the model
        self.primal = model

    def createInterdictionDual(self):
        # Create the model
        model = pe.ConcreteModel()

        # Add the sets
        model.node_set = pe.Set( initialize=self.node_set )
        model.edge_set = pe.Set( initialize=self.arc_set, dimen=2)
        model.commodity_set = pe.Set( initialize=self.commodity_set )

        # Create the variables
        model.rho = pe.Var(model.node_set*model.commodity_set, domain=pe.Reals)
        model.piSingle = pe.Var(model.edge_set*model.commodity_set, domain=pe.NonPositiveReals)
        model.piJoint = pe.Var(model.edge_set, domain=pe.NonPositiveReals)

        model.x = pe.Var(model.edge_set, domain=pe.Binary)

        # Create the objective
        def obj_rule(model):
            return  sum(data['Capacity']*model.piJoint[e] for e,data in self.arc_data.iterrows() if data['Capacity']>=0) +\
                    sum(data['Capacity']*model.piSingle[e] for e,data in self.arc_commodity_data.iterrows() if data['Capacity']>=0)+\
                    sum(data['SupplyDemand']*model.rho[n] for n,data in self.node_commodity_data.iterrows())

        model.OBJ = pe.Objective(rule=obj_rule, sense=pe.maximize)

        # Create the constraints for y_ijk
        def edge_constraint_rule(model, i, j, k):
            if (i,j,k) not in self.arc_commodity_data.index:
                return pe.Constraint.Skip
            attackable = int(self.arc_data['Attackable'].get((i,j),0))
            hasSingleCap = int(self.arc_commodity_data['Capacity'].get((i,j,k),-1)>=0)
            hasJointCap = int(self.arc_data['Capacity'].get((i,j),-1)>=0)
            return model.rho[(j,k)] - model.rho[(i,k)] + model.piSingle[(i,j,k)]*hasSingleCap + model.piJoint[(i,j)]*hasJointCap <=  self.arc_commodity_data['Cost'].get((i,j,k)) + (2*self.nCmax+1)*model.x[(i,j)]*attackable

        model.DualEdgeConstraint = pe.Constraint(model.edge_set*model.commodity_set, rule=edge_constraint_rule)

        # Create constraints for the UnsatDemand variables
        def unsat_constraint_rule(model, n, k):
            if (n,k) not in self.node_commodity_data.index:
                return pe.Constraint.Skip
            imbalance = self.node_commodity_data['SupplyDemand'].get((n,k),0)
            supply_node = int(imbalance < 0)
            demand_node = int(imbalance > 0)
            if (supply_node):
                return -model.rho[(n,k)] <= self.nCmax
            if (demand_node):
                return model.rho[(n,k)] <= self.nCmax
            return pe.Constraint.Skip

        model.UnsatConstraint = pe.Constraint(model.node_set*model.commodity_set, rule=unsat_constraint_rule)

        # Create the interdiction budget constraint
        def block_limit_rule(model):
            model.attacks = self.attacks
            return pe.summation(model.x) <= model.attacks

        model.BlockLimit = pe.Constraint(rule=block_limit_rule)

        # Create, save the model
        self.Idual = model

    def solve(self, tee=False):
        solver = pyomo.opt.SolverFactory('cplex')

        # Solve the dual first
        self.Idual.BlockLimit.construct()
        self.Idual.BlockLimit._constructed = False
        del self.Idual.BlockLimit._data[None]
        self.Idual.BlockLimit.reconstruct()
        self.Idual.preprocess()
        results = solver.solve(self.Idual, tee=tee, keepfiles=False, options_string="mip_tolerances_integrality=1e-9 mip_tolerances_mipgap=0")

        # Check that we actually computed an optimal solution, load results
        if (results.solver.status != pyomo.opt.SolverStatus.ok):
            logging.warning('Check solver not ok?')
        if (results.solver.termination_condition != pyomo.opt.TerminationCondition.optimal):
            logging.warning('Check solver optimality?')

        self.Idual.solutions.load_from(results)
        # Now put interdictions into xbar and solve primal

        for e in self.arc_data.index:
            self.arc_commodity_data.ix[e,'xbar'] = self.Idual.x[e].value

        self.primal.OBJ.construct()
        self.primal.OBJ._constructed = False
        self.primal.OBJ._init_sense = pe.minimize
        del self.primal.OBJ._data[None]
        self.primal.OBJ.reconstruct()
        self.primal.preprocess()
        results = solver.solve(self.primal, tee=tee, keepfiles=False, options_string="mip_tolerances_integrality=1e-9 mip_tolerances_mipgap=0")

        # Check that we actually computed an optimal solution, load results
        if (results.solver.status != pyomo.opt.SolverStatus.ok):
            logging.warning('Check solver not ok?')
        if (results.solver.termination_condition != pyomo.opt.TerminationCondition.optimal):
            logging.warning('Check solver optimality?')

        self.primal.solutions.load_from(results)

    def printSolution(self):
        print()
        print('Using %d attacks:'%self.attacks)
        print()
        edges = sorted(self.arc_set)
        for e in edges:
            if self.Idual.x[e].value > 0:
                print('Interdict arc %s -> %s'%(str(e[0]), str(e[1])))
        print()

        nodes = sorted(self.node_commodity_data.index)
        for n in nodes:
            remaining_supply = self.primal.UnsatSupply[n].value
            if remaining_supply > 0:
                print('Remaining supply of %s on node %s: %.2f'%(str(n[1]), str(n[0]), remaining_supply))
        for n in nodes:
            remaining_demand = self.primal.UnsatDemand[n].value
            if remaining_demand > 0:
                print('Remaining demand of %s on node %s: %.2f'%(str(n[1]), str(n[0]), remaining_demand))
        print()

        for e0,e1 in self.arc_set:
            for k in self.commodity_set:
                flow = self.primal.y[(e0,e1,k)].value
                if flow > 0:
                    print('Flow on arc %s -> %s: %.2f %s'%(str(e0), str(e1), flow, str(k)))
        print()

        print('----------')
        print('Total cost = %.2f (primal) %.2f (dual)'%(self.primal.OBJ(), self.Idual.OBJ()))

########################
# Now lets do something
########################

if __name__ == '__main__':
    m = MultiCommodityInterdiction('sample_nodes_data.csv', 'sample_nodes_commodity_data.csv', 'sample_arcs_data.csv', 'sample_arcs_commodity_data.csv')
    m.solve()
    m.printSolution()
    m.attacks = 1
    m.solve()
    m.printSolution()
    m.attacks = 2
    m.solve()
    m.printSolution()

max_stars_repo_name: wogandavid/PyomoGallery

max_issues_repo_name: wogandavid/PyomoGallery

max_forks_repo_name: wogandavid/PyomoGallery

reason: import pyomo


text:
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
import os, glob
from tempfile import NamedTemporaryFile

dataframe = pd.read_csv('source/nat1900-2017.tsv', sep='\t')
dataframe.rename(columns={"sexe":"Gender", "preusuel":"Name","annais":"Years","nombre":"Number of newborns"}, inplace=True)
dataframe.set_index('Years', inplace=True)
dataframe.drop('XXXX', inplace=True)
dataframe.Gender = dataframe.Gender.map({1:'male', 2:'female'})
dataframe.index = pd.to_numeric(dataframe.index,errors='coerce')

def find_name(name, year_beginning, year_end):
    diagram_png = None
    statistics = os.path.join(os.path.dirname(__file__), 'static')

    my_filter = lambda name: dataframe.Name == name
    results = dataframe[my_filter(name.upper())]

    filter_years = (results.index >= int(year_beginning)) & (results.index <= int(year_end))
    results = results[filter_years]

    for statistic in glob.glob(f'{statistics}/*.png'):
        if statistic == f'{statistics}/{name}_{year_beginning}_{year_end}.png':
            diagram_png = f'{name}_{year_beginning}_{year_end}.png'

    if diagram_png == None:

        popularity = results['Number of newborns']
        list_of_values = []
        for key, value in popularity.items():
            for x in range(value):
                list_of_values.append(key)
        my_bins = [x for x in range(int(year_beginning), int(year_end)+10, 10)]

        plt.xkcd()
        plt.style.use('seaborn')
        plt.hist(list_of_values,bins=my_bins, edgecolor='#595959', color='#ff7b64')
        plt.xticks(np.arange(int(year_beginning), int(year_end)+10, 10),labels=my_bins)
        plt.gcf().autofmt_xdate(rotation = 30)
        plt.xlabel('Years')
        plt.title(f'Popularity of the name "{name}" in France')

        plt.savefig(f'{statistics}/{name}_{year_beginning}_{year_end}.png')
        diagram_png = f'{name}_{year_beginning}_{year_end}.png'

    # customize DataFRame
    results.Gender = results.Gender.map({1:'male', 2:'female'})
    results = results.loc[:, ['Gender','Number of newborns']]

    results = results.groupby(pd.cut(results.index, np.arange(int(year_beginning), int(year_end)+10, 10))).sum()

    indexes = [''.join((str(x), '-', str(x+10))) for x in range(int(year_beginning), int(year_end), 10)]
    results.index = indexes

    return diagram_png, results

def compare_names(name1, name2):
    diagram_png = None
    statistics = os.path.join(os.path.dirname(__file__), 'static')

    for statistic in glob.glob(f'{statistics}/*.png'):
        if statistic == f'{statistics}/compare_{name1}_{name2}.png':
            diagram_png = f'compare_{name1}_{name2}.png'

    if not diagram_png:
        my_filter = lambda name: dataframe.Name == name

        name_first = dataframe[my_filter(name1.upper())]
        name_second = dataframe[my_filter(name2.upper())]

        values_first_name = []
        values_second_name = []

        results_first = name_first['Number of newborns']
        results_second = name_second['Number of newborns']

        for key, value in results_first.items():
            for x in range(value):
                values_first_name.append(key)

        for key, value in results_second.items():
            for x in range(value):
                values_second_name.append(key)

        my_bins = [x for x in range(1900, 2030, 10)]
        width = 10
        plt.xkcd()
        plt.style.use('seaborn')
        plt.hist(values_first_name,bins=my_bins, alpha=0.5, width=width, edgecolor='#595959', color='#ff7b64', label=f'{name1}', log=True)
        plt.hist(values_second_name,bins=my_bins, alpha=0.5, width=width, edgecolor='#595959', color='#6985a0', label=f'{name2}', log=True)
        plt.xticks(np.arange(1900, 2030, 10),labels=my_bins)
        plt.gcf().autofmt_xdate(rotation = 30)
        plt.xlabel('Years')
        plt.title(f'Comparision of the names: "{name1}" and "{name2}" in France')
        plt.legend([f'{name1}', f'{name2}'])

        plt.savefig(f'{statistics}/compare_{name1}_{name2}.png')
        diagram_png = f'compare_{name1}_{name2}.png'
        plt.clf()

    # # customize DataFRame

    result_first = name_first['Number of newborns'].groupby(pd.cut(name_first.index, np.arange(1900,2030, 10))).sum()
    indexes = [''.join((str(x), '-', str(x+10))) for x in range(1900,2020, 10)]
    result_first.index = indexes
    result_first = pd.concat([result_first, pd.Series([f'{name1}' for x in range(12)], index=indexes, name='Name')], axis=1)

    result_second = name_second['Number of newborns'].groupby(pd.cut(name_second.index, np.arange(1900,2030, 10))).sum()
    indexes = [''.join((str(x), '-', str(x+10))) for x in range(1900,2020, 10)]
    result_second.index = indexes
    result_second = pd.concat([result_second, pd.Series([f'{name2}' for x in range(12)], index=indexes, name='Name')], axis=1)

    results = pd.concat([result_first, result_second], axis=1)

    return diagram_png, results

def general_statistics():
    pass


text:
from __future__ import annotations
import numpy as np
import numpy.typing as npt
from ..batched_unit import BatchedUnit

class Variance1D(BatchedUnit):
    def __init__(self, values: npt.ArrayLike, is_sample_variance=True) -> None:
        super().__init__()
        self.is_sample_variance = is_sample_variance
        values = np.array(values)
        values = values.squeeze()

        if values.ndim > 1:
            raise Exception("Should be 1D array like")

        self.num_samples = len(values)
        self.mean = np.mean(values)
        # Sum of squares of differences from the current mean
        self.ssd = np.sum((values - self.mean)**2)

    def update(self, values: npt.ArrayLike) -> None:
        values = np.array(values)
        values = values.squeeze()

        if values.ndim > 1:
            raise Exception("Should be 1D array like")

        self.merge(Variance1D(values))

    def merge(self, batched_unit: Variance1D) -> None:
        new_num_samples = self.num_samples + batched_unit.num_samples
        delta = batched_unit.mean - self.mean
        self.ssd += batched_unit.ssd + delta**2 * (self.num_samples * batched_unit.num_samples)/new_num_samples
        self.mean += delta*batched_unit.num_samples/new_num_samples
        self.num_samples = new_num_samples

    def compute(self) -> float:
        if self.is_sample_variance:
            return self.ssd/(self.num_samples-1)
        return self.ssd/self.num_samples

max_stars_repo_name: braindeadpool/dmath

max_issues_repo_name: braindeadpool/dmath

max_forks_repo_name: braindeadpool/dmath


text:
import os
import torch
import numpy as np
import pytest

import schnetpack as spk
import schnetpack.data
from tests.assertions import assert_dataset_equal
from numpy.testing import assert_array_almost_equal

from tests.fixtures import *

def test_add_and_read(empty_dataset, example_data):
    """
    Test if data can be added to existing dataset.
    """
    # add data
    for ats, props in example_data:
        empty_dataset.add_system(ats, **props)

    assert len(empty_dataset) == len(example_data)
    assert os.path.exists(empty_dataset.dbpath)

def test_empty_subset_of_subset(example_subset):
    """
    Test if empty subsubset can be created.
    """
    subsubset = spk.data.create_subset(example_subset, [])
    assert len(example_subset) == 2
    assert len(subsubset) == 0

def test_merging(tmpdir, example_dataset, partition_names):
    # create merged dataset by repeating original three times
    merged_dbpath = os.path.join(str(tmpdir), "merged.db")

    parts = [example_dataset.dbpath, example_dataset.dbpath, example_dataset.dbpath]
    if partition_names is not None:
        parts = {k: v for k, v in zip(partition_names, parts)}

    merged_data = schnetpack.data.merge_datasets(merged_dbpath, parts)

    # check merged
    assert len(merged_data) == 3 * len(example_dataset)

    partitions = merged_data.get_metadata("partitions")
    partition_meta = merged_data.get_metadata("partition_meta")

    assert len(partitions) == 3

    for p in partitions.values():
        assert len(p) == 2

    if partition_names is not None:
        assert "example1" in partitions.keys()
        assert "example2" in partitions.keys()
        assert "ex3" in partitions.keys()

def test_loader(example_loader, batch_size):
    """
    Test dataloader iteration and batch shapes.
    """
    for batch in example_loader:
        for entry in batch.values():
            assert entry.shape[0] == min(batch_size, len(example_loader.dataset))

def test_statistics_calculation(example_loader, dataset_stats, main_properties):
    """
    Test statistics calculation of dataloader.
    """
    means, stds = example_loader.get_statistics(main_properties)
    for pname in main_properties:
        assert_array_almost_equal(means[pname].numpy(), dataset_stats[0][pname])
        assert_array_almost_equal(stds[pname].numpy(), dataset_stats[1][pname])

def test_dataset(example_dataset, tmp_dbpath, available_properties, num_data):
    assert example_dataset.available_properties == available_properties
    assert example_dataset.__len__() == num_data

    # test valid path exists but wrong properties
    too_many = available_properties + ["invalid"]
    not_all = available_properties[:-1]
    wrong_prop = available_properties[:-1] + ["invalid"]
    with pytest.raises(spk.data.AtomsDataError):
        dataset = spk.data.AtomsData(tmp_dbpath, available_properties=too_many)
    with pytest.raises(spk.data.AtomsDataError):
        dataset = spk.data.AtomsData(tmp_dbpath, available_properties=not_all)
    with pytest.raises(spk.data.AtomsDataError):
        dataset = spk.data.AtomsData(tmp_dbpath, available_properties=wrong_prop)

    # test valid path, but no properties
    dataset = spk.data.AtomsData(tmp_dbpath)
    assert set(dataset.available_properties) == set(available_properties)

def test_extension_check():
    """
    Test if dataset raises error if .db is missing  in dbpath.
    """
    with pytest.raises(spk.data.AtomsDataError):
        dataset = spk.data.atoms.AtomsData("test/path")

def test_get_center(h2o, o2):
    """
    Test calculation of molecular centers.
    """
    # test if centers are equal for symmetric molecule
    com = spk.data.get_center_of_mass(o2)
    cog = spk.data.get_center_of_geometry(o2)

    assert list(com.shape) == [3]
    assert list(cog.shape) == [3]
    np.testing.assert_array_almost_equal(com, cog)

    # test if centers are different for asymmetric molecule
    com = spk.data.get_center_of_mass(h2o)
    cog = spk.data.get_center_of_geometry(h2o)

    np.testing.assert_raises(AssertionError, np.testing.assert_array_equal, com, cog)

def test_concatenation(
    example_dataset, example_subset, example_concat_dataset, example_concat_dataset2
):
    """
    Test ids of concatenated datasets.
    """
    len_e = len(example_dataset)
    # test lengths
    assert len(example_concat_dataset) == len(example_subset) + len(example_dataset)
    assert len(example_subset) == 2
    assert len(example_concat_dataset2) == len(example_concat_dataset) + len(
        example_subset
    )

    for i in range(len_e):
        c, e = example_concat_dataset[i], example_dataset[i]
        for key in c.keys():
            if key == "_idx":
                continue
            cv, ev = c[key], e[key]
            assert torch.equal(cv, ev)

    for i in range(2):
        c, e = example_concat_dataset[len_e + i], example_subset[i]
        for key in c.keys():
            if key == "_idx":
                continue
            cv, ev = c[key], e[key]
            assert torch.equal(cv, ev), "key {} does not match!".format(key)

def test_save_concatenated(tmp_data_dir, example_concat_dataset):
    """
    Test if a concatenated dataset can be saved.

    Args:
        tmp_data_dir:
        example_concat_dataset:

    Returns:

    """
    # save dataset two times
    tmp_dbpath1 = os.path.join(tmp_data_dir, "concat.db")
    spk.data.save_dataset(tmp_dbpath1, example_concat_dataset)
    tmp_dbpath2 = os.path.join(tmp_data_dir, "concat2.db")
    spk.data.save_dataset(tmp_dbpath2, example_concat_dataset)

    # check if paths exist
    assert os.path.exists(tmp_dbpath1)
    assert os.path.exists(tmp_dbpath2)

    # assert if saved datasets are equal
    dataset1 = spk.data.AtomsData(tmp_dbpath1)
    dataset2 = spk.data.AtomsData(tmp_dbpath2)

    assert_dataset_equal(dataset1, dataset2)

reason: import numpy,from numpy


text:
import os
import unittest

from scipy.io import loadmat

from Atlas.aero import Aero, Aero2

class AeroTestCase(unittest.TestCase):
    def setup(self):
        pass

    def test_aero(self):
        """ test Aero
            (with simple actuator disk method for induced velocity calculation)
        """
        comp = Aero()

        # populate inputs
        path = os.path.join(os.path.dirname(__file__), 'aero.mat')
        data = loadmat(path, struct_as_record=True, mat_dtype=True)

        comp.b        = int(data['b'][0][0])
        comp.yN       = data['yN'].flatten()
        comp.Ns       = max(comp.yN.shape) - 1
        comp.R        = 10.0
        comp.dr       = [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]
        comp.r        = [ 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5]
        comp.h        = data['h'][0][0]
        comp.ycmax    = data['ycmax'][0][0]

        comp.rho      = data['rho'][0][0]
        comp.visc     = data['visc'][0][0]
        comp.vw       = data['vw'][0][0]
        comp.vc       = data['vc'][0][0]
        comp.Omega    = data['Omega'][0][0]

        comp.c        = data['cE']
        comp.Cl       = data['Cl']
        comp.d        = data['d']

        comp.yWire    = data['yWire'][0]
        comp.zWire    = data['zWire'][0][0]
        comp.tWire    = data['tWire'][0][0]

        comp.Cm       = data['Cm']
        comp.xtL      = data['xtL']
        comp.xtU      = data['xtU']

        # run
        comp.run()

        # check outputs
        for i, val in enumerate(data['vi']):
            self.assertAlmostEquals(comp.vi[i], val, 4,
                msg='vi[%d] is %f, should be %f' % (i, comp.vi[i], val))

        for i, val in enumerate(data['phi']):
            self.assertAlmostEquals(comp.phi[i], val, 4,
                msg='phi[%d] is %f, should be %f' % (i, comp.phi[i], val))

        for i, val in enumerate(data['Re']):
            self.assertAlmostEquals(comp.Re[i], val, 4,
                msg='Re[%d] is %f, should be %f' % (i, comp.Re[i], val))

        for i, val in enumerate(data['Cd']):
            self.assertAlmostEquals(comp.Cd[i], val, 4,
                msg='Cd[%d] is %f, should be %f' % (i, comp.Cd[i], val))

        for i, val in enumerate(data['Fblade']['Fx'][0][0]):
            self.assertAlmostEquals(comp.Fblade.Fx[i], val, 4,
                msg='Fx[%d] is %f, should be %f' % (i, comp.Fblade.Fx[i], val))

        for i, val in enumerate(data['Fblade']['Fz'][0][0]):
            self.assertAlmostEquals(comp.Fblade.Fz[i], val, 4,
                msg='Fz[%d] is %f, should be %f' % (i, comp.Fblade.Fz[i], val))

        for i, val in enumerate(data['Fblade']['My'][0][0]):
            self.assertAlmostEquals(comp.Fblade.My[i], val, 4,
                msg='My[%d] is %f, should be %f' % (i, comp.Fblade.My[i], val))

        for i, val in enumerate(data['Fblade']['Q'][0][0]):
            self.assertAlmostEquals(comp.Fblade.Q[i], val, 4,
                msg='Q[%d] is %f, should be %f' % (i, comp.Fblade.Q[i], val))

        for i, val in enumerate(data['Fblade']['P'][0][0]):
            self.assertAlmostEquals(comp.Fblade.P[i], val, 4,
                msg='P[%d] is %f, should be %f' % (i, comp.Fblade.P[i], val))

        for i, val in enumerate(data['Fblade']['Pi'][0][0]):
            self.assertAlmostEquals(comp.Fblade.Pi[i], val, 4,
                msg='Pi[%d] is %f, should be %f' % (i, comp.Fblade.Pi[i], val))

        for i, val in enumerate(data['Fblade']['Pp'][0][0]):
            self.assertAlmostEquals(comp.Fblade.Pp[i], val, 4,
                msg='Pp[%d] is %f, should be %f' % (i, comp.Fblade.Pp[i], val))

    def test_aero2(self):
        """ test Aero2
            (with vortex method for induced velocity calculation)
        """
        comp = Aero2()

        # populate inputs
        path = os.path.join(os.path.dirname(__file__), 'aero2.mat')
        data = loadmat(path, struct_as_record=True, mat_dtype=True)

        comp.b        = int(data['b'][0][0])
        comp.yN       = data['yN'].flatten()
        comp.Ns       = max(comp.yN.shape) - 1
        comp.R        = 10.0
        comp.dr       = [1., 1., 1., 1., 1., 1., 1., 1., 1., 1.]
        comp.r        = [ 0.5, 1.5, 2.5, 3.5, 4.5, 5.5, 6.5, 7.5, 8.5, 9.5]
        comp.h        = data['h'][0][0]
        comp.ycmax    = data['ycmax'][0][0]

        comp.rho      = data['rho'][0][0]
        comp.visc     = data['visc'][0][0]
        comp.vw       = data['vw'][0][0]
        comp.vc       = data['vc'][0][0]
        comp.Omega    = data['Omega'][0][0]

        comp.c        = data['cE']
        comp.Cl       = data['Cl']
        comp.d        = data['d']

        comp.yWire    = data['yWire'][0]
        comp.zWire    = data['zWire'][0][0]
        comp.tWire    = data['tWire'][0][0]

        comp.Cm       = data['Cm']
        comp.xtL      = data['xtL']
        comp.xtU      = data['xtU']

        comp.q       = data['q']

        comp.anhedral = data['anhedral'][0][0]

        # run
        comp.run()

        # check outputs
        for i, val in enumerate(data['vi']):
            self.assertAlmostEquals(comp.vi[i], val, 4,
                msg='vi[%d] is %f, should be %f' % (i, comp.vi[i], val))

        for i, val in enumerate(data['phi']):
            self.assertAlmostEquals(comp.phi[i], val, 4,
                msg='phi[%d] is %f, should be %f' % (i, comp.phi[i], val))

        for i, val in enumerate(data['Re']):
            self.assertAlmostEquals(comp.Re[i], val, 4,
                msg='Re[%d] is %f, should be %f' % (i, comp.Re[i], val))

        for i, val in enumerate(data['Cd']):
            self.assertAlmostEquals(comp.Cd[i], val, 4,
                msg='Cd[%d] is %f, should be %f' % (i, comp.Cd[i], val))

        for i, val in enumerate(data['Fblade']['Fx'][0][0]):
            self.assertAlmostEquals(comp.Fblade.Fx[i], val, 4,
                msg='Fx[%d] is %f, should be %f' % (i, comp.Fblade.Fx[i], val))

        for i, val in enumerate(data['Fblade']['Fz'][0][0]):
            self.assertAlmostEquals(comp.Fblade.Fz[i], val, 4,
                msg='Fz[%d] is %f, should be %f' % (i, comp.Fblade.Fz[i], val))

        for i, val in enumerate(data['Fblade']['My'][0][0]):
            self.assertAlmostEquals(comp.Fblade.My[i], val, 4,
                msg='My[%d] is %f, should be %f' % (i, comp.Fblade.My[i], val))

        for i, val in enumerate(data['Fblade']['Q'][0][0]):
            self.assertAlmostEquals(comp.Fblade.Q[i], val, 4,
                msg='Q[%d] is %f, should be %f' % (i, comp.Fblade.Q[i], val))

        for i, val in enumerate(data['Fblade']['P'][0][0]):
            self.assertAlmostEquals(comp.Fblade.P[i], val, 4,
                msg='P[%d] is %f, should be %f' % (i, comp.Fblade.P[i], val))

        for i, val in enumerate(data['Fblade']['Pi'][0][0]):
            self.assertAlmostEquals(comp.Fblade.Pi[i], val, 4,
                msg='Pi[%d] is %f, should be %f' % (i, comp.Fblade.Pi[i], val))

        for i, val in enumerate(data['Fblade']['Pp'][0][0]):
            self.assertAlmostEquals(comp.Fblade.Pp[i], val, 4,
                msg='Pp[%d] is %f, should be %f' % (i, comp.Fblade.Pp[i], val))

if __name__ == "__main__":
    unittest.main()

max_stars_repo_name: swryan/Atlas

max_issues_repo_name: swryan/Atlas

max_forks_repo_name: swryan/Atlas

reason: from scipy


text:
import pickle
import numpy as np

def predict_single(customer, dv, model):
    X = dv.transform([customer])
    y_pred = model.predict_proba(X)[:, 1]
    return y_pred[0]

with open("./model1.bin", "rb") as f_in:
    model = pickle.load(f_in)

with open("./dv.bin", "rb") as f_in:
    dv = pickle.load(f_in)

customer = {"contract": "two_year", "tenure": 12, "monthlycharges": 19.7}

prediction = predict_single(customer, dv, model)

print("\nprediction: %.3f" % prediction)

if prediction >= 0.5:
    print("verdict: Churn")
else:
    print("verdict: Not churn")

max_stars_repo_name: ashok-arora/ML-Zoomcamp

max_issues_repo_name: ashok-arora/ML-Zoomcamp

max_forks_repo_name: ashok-arora/ML-Zoomcamp


text:
import numpy as np

# Number of training iterations
NUM_ITERATIONS = 50
# Threshold for 0/1 classification
THRESHOLD = 0.0
# Learning rate
LEARNING_RATE = 0.01

# Data (both training and testing)
input_data = np.linspace(0, 5, 1000)

def output(n):
    return train_value_function(n) + 0.5 * train_value_function(n - 1) - 1.5 * train_value_function(n - 2)

def train_value_function(t):
    return np.sin(10 * np.sin(t) * t)

def sigmoid(x):
    return np.power((1 + np.exp(-x)), -1)

def tanh(x):
    return 2 / (1 + np.exp(-2 * x)) - 1

# Create perceptron weights (random weights)
weights = np.random.rand(4)

# Train perceptron
for i in range(2, len(input_data)):
    # Calculate predictions with current weights
    # print(np.concatenate((input_data[i - 2:i+1], [1])))
    current_input = np.concatenate((input_data[i - 2:i + 1], [1]))
    predictions = np.dot(current_input, weights)

    output_value = tanh(train_value_function(i))
    # # Turn into {-1,1}
    # predictions = (predictions * 2) - 1
    #
    # # Calculate accuracy (not needed for training, but to track the learning progress)
    accuracy = np.mean(predictions == output_value)
    # # Print the accuracy
    print("Iteration %d: Acc %f \t %s %s" % (i, accuracy, str(predictions), str(output_value)))
    #
    # # Update weights according to update rule
    weights = weights + LEARNING_RATE * (output_value - predictions) * current_input
    # print(weights)

# Print weights for inspection
print(weights)

max_stars_repo_name: envil/deep-learning-playground

max_issues_repo_name: envil/deep-learning-playground

max_forks_repo_name: envil/deep-learning-playground


text:
"""
Created on Wed Oct 21 12:03:38 2020
@author: ISH KAPOOR
"""
import cv2
import numpy as np
import glob
img_array = []
for filename in glob.glob('C:/Users/ISH KAPOOR\Desktop/Cartoon Git Project/Cartoon_Babbar_Frame/*.png'):
    img = cv2.imread(filename)
    height, width, layers = img.shape
    size = (width, height)
    img_array.append(img)

out = cv2.VideoWriter('Babbar_Cartoon.mp4', cv2.VideoWriter_fourcc(*'DIVX'), int(60), size)

for i in range(len(img_array)):
    out.write(img_array[i])
    print(i)

out.release()


text:
# Steps to constructions shapes, rendering png and forming stl
from objects.components import (
    backbone_flat,
    backbone_weak_curve,
    backbone_strong_curve,
    backbone_sharp_bend,
    backbone_hook_f,
    backbone_hook_r,
    backbone_s,
    cp_round,
    cp_concave,
    cp_plane,
    cp_convex,
    cp_elliptical,
    sd_cylinder,
    sd_cylinder_spherical_top,
    sd_curved_cylinder,
    sd_sphere,
    sd_ellipsoid,
    sd_cone,
    sd_curved_elliptical_cylinder,
    sd_elliptical_cylinder,
)
from objects.cross_section import CrossSection
from objects.axial_component import AxialComponent
from objects.shape import Shape
from objects.parameters import BACKBONE_LENGTH, ORDER
from objects.utilities import transform_sd_mesh
from splipy import BSplineBasis, Curve
import numpy as np
import pickle
from datetime import datetime
from pathlib import Path
import matplotlib.pyplot as plt
from matplotlib import gridspec
import copy

##################
### Parameters ###
##################
set_version = "Z"  # Typically a single letter
base_dir = Path(r"C:\Users\William\Files\OConnor\Code\Projects\objects\stimulus_set", set_version)

# # Backbones used
# backbone_list = [
#     backbone_flat,
#     backbone_weak_curve,
#     backbone_strong_curve,
#     backbone_sharp_bend,
#     backbone_hook_f,
#     backbone_hook_r,
#     backbone_s,
# ]

# # Controlpoints used for cross sections
# cp_list = [
#     cp_round,
#     cp_concave,
#     cp_plane,
#     cp_convex,
#     cp_elliptical,
# ]

# # Surface deformations used
# sd_list = [
#     sd_cylinder,
#     sd_cylinder_spherical_top,
#     sd_curved_cylinder,
#     sd_sphere,
#     sd_ellipsoid,
#     sd_cone,
# ]

# Dictionary linking names and arrays (helps with constructing description of each shape)
d = {
    "b_flat": (backbone_flat),
    "b_weak_curve": (backbone_weak_curve),
    "b_strong_curve": (backbone_strong_curve),
    "b_sharp_bend": (backbone_sharp_bend),
    "b_hook_f": (backbone_hook_f),
    "b_hook_r": (backbone_hook_r),
    "b_s": (backbone_s),
    "cs_round": (cp_round),
    "cs_concave": (cp_concave),
    "cs_plane": (cp_plane),
    "cs_convex": (cp_convex),
    "cs_elliptical": (cp_elliptical),
    "sd_cylinder": (sd_cylinder),
    "sd_cylinder_spherical_top": (sd_cylinder_spherical_top),
    "sd_curved_cylinder": (sd_curved_cylinder),
    "sd_sphere": (sd_sphere),
    "sd_ellipsoid": (sd_ellipsoid),
    "sd_cone": (sd_cone),
}
##########################
### Shape Construction ###
##########################
shapes = []
transitions_dict = {}

start = len(shapes)
# Varying backbones
for b_name in ["b_flat", "b_weak_curve", "b_strong_curve", "b_sharp_bend", "b_hook_f", "b_hook_r", "b_s"]:
    cs_name = "cs_round"
    backbone = d[b_name]
    cp = d[cs_name]
    rotation = 0
    cs_list = [CrossSection(cp, i, rotation=rotation) for i in np.linspace(0.1, 0.9, 5)]
    ac = AxialComponent(backbone=backbone, cross_sections=cs_list)
    s = Shape([ac])
    s.description = "{0}-{1}".format(b_name, cs_name)
    s.cs_name = [cs_name]
    s.sd_name = None
    shapes.append(s)
stop = len(shapes)
transitions_dict["varying_backbones"] = [start, stop]

# Varying cross sections
# #TODO: rotations?
start = len(shapes)
for b_name in ["b_flat", "b_weak_curve"]:
    for cs_name in ["cs_round", "cs_concave", "cs_plane", "cs_convex", "cs_elliptical"]:

        if b_name == "b_flat" and cs_name == "cs_round":
            continue

        backbone = d[b_name]
        cp = d[cs_name]
        rotation = 0
        cs_list = [CrossSection(cp, i, rotation=rotation) for i in np.linspace(0.1, 0.9, 10)]
        ac = AxialComponent(backbone=backbone, cross_sections=cs_list)
        s = Shape([ac])
        s.description = "{0}-{1}".format(b_name, cs_name)
        s.cs_name = [cs_name]
        s.sd_name = None
        shapes.append(s)
stop = len(shapes)
transitions_dict["varying_cross_sections"] = [start, stop]

# Blending cross sections
# #TODO: rotations?
start = len(shapes)
for cs_name_a in ["cs_round", "cs_elliptical"]:
    for cs_name_b in ["cs_round", "cs_concave", "cs_plane", "cs_convex", "cs_elliptical"]:

        # Skip when cp_a and cp_b are the same cross section, as its already formed above
        if cs_name_a == cs_name_b:
            continue

        # Do not duplicate cs_elliptical + cs_round
        if cs_name_a == "cs_elliptical" and cs_name_b == "cs_round":
            continue

        b_name = "b_flat"
        cp_a = d[cs_name_a]
        cp_b = d[cs_name_b]

        # Blend in both directions (i.e. shape AB)
        backbone = d[b_name]
        rotation = 0
        cs_list = [CrossSection(cp_a, 0.1, rotation=rotation), CrossSection(cp_b, 0.9, rotation=rotation)]
        ac = AxialComponent(backbone=backbone, cross_sections=cs_list)
        s = Shape([ac])
        s.description = "{0}-{1}-{2}".format(b_name, cs_name_a, cs_name_b)
        s.cs_name = [cs_name_a, cs_name_b]
        s.sd_name = None
        shapes.append(s)

        # Blend in both directions (i.e. shape BA)
        backbone = backbone_flat
        rotation = 0
        cs_list = [CrossSection(cp_b, 0.1, rotation=rotation), CrossSection(cp_a, 0.9, rotation=rotation)]
        ac = AxialComponent(backbone=backbone, cross_sections=cs_list)
        s = Shape([ac])
        s.description = "{0}-{1}-{2}".format(b_name, cs_name_b, cs_name_a)
        s.cs_name = [cs_name_b, cs_name_a]
        s.sd_name = None
        shapes.append(s)
stop = len(shapes)
transitions_dict["blending_cross_sections"] = [start, stop]

# Varying cross section relative sizes
start = len(shapes)
sizes = np.array(
    [
        [0.5, 1, 1.5],  # linear increase (cone)
        [1.5, 1, 0.5],  # linear decrease (cone)
        [0.5, 1, 2],  # quadratic increase (flare)
        [2, 1, 0.5],  # quadratic decrease (flare)
        [0.5, 1.5, 0.5],  # spindle
        [1.5, 0.5, 1.5],  # hourglass
    ]
)
for size in sizes:
    for cs_name in ["cs_round"]:
        b_name = "b_flat"
        backbone = d[b_name]
        cp = d[cs_name]
        rotation = 0
        cs_list = [
            CrossSection(cp * size[0], 0.1, rotation=rotation),
            CrossSection(cp * size[1], 0.5, rotation=rotation),
            CrossSection(cp * size[2], 0.9, rotation=rotation),
        ]
        ac = AxialComponent(backbone=backbone, cross_sections=cs_list)
        s = Shape([ac])
        size_as_str = [str(int(s * 100)) + "%" for s in size]
        s.description = "{0}-{1}-{2}".format(b_name, cs_name, "-".join(size_as_str))
        s.cs_name = [cs_name]
        s.sd_name = None
        shapes.append(s)
stop = len(shapes)
transitions_dict["varying_size"] = [start, stop]

# Varying surface deformations
start = len(shapes)
for b_name in ["b_flat"]:
    backbone = d[b_name]
    for cs_name in ["cs_round"]:

        cp = d[cs_name]

        # Generate base shape
        rotation = 0
        cs_list = [CrossSection(cp, i, rotation=rotation) for i in np.linspace(0.1, 0.9, 9)]
        ac = AxialComponent(backbone=backbone, cross_sections=cs_list)
        base_shape = Shape([ac])

        # Apply surface deformations
        for sd_name in [
            "sd_sphere",
            "sd_ellipsoid",
            "sd_cylinder",
            "sd_cylinder_spherical_top",
            "sd_curved_cylinder",
            "sd_cone",
        ]:

            sd = d[sd_name]

            # Rotations around round axis of axial component
            for theta_backbone in np.linspace(0, 2 * np.pi, 4, endpoint=False):

                if theta_backbone != 0 and backbone == backbone_flat:
                    continue

                # Rotations about vector from backbone to surface point where surface deformation is attached
                for theta_linear_segment in np.linspace(0, 2 * np.pi, 4, endpoint=False):

                    if ~np.isclose(theta_linear_segment, 0) and sd not in [
                        sd_curved_cylinder,
                        sd_ellipsoid,
                    ]:
                        continue

                    # Don't duplicate the ellipsoid with rotation
                    if sd == sd_ellipsoid:
                        pass
                    if np.any(np.isclose(theta_linear_segment, [np.pi, 3 * np.pi / 2])) and sd in [
                        sd_ellipsoid,
                    ]:
                        continue

                    # Apply "difference" operation to make concavity
                    for operation in ["union", "difference"]:
                        if operation == "difference" and sd not in [sd_sphere, sd_ellipsoid]:
                            continue

                        # Allow surface deformation to be in 1-2 positions
                        for pos_list in [[0.33], [0.66], [0.33, 0.66]]:
                            s = copy.deepcopy(base_shape)
                            for pos in pos_list:
                                sd_mesh, origin = sd
                                sd_mesh_transformed = transform_sd_mesh(
                                    sd_mesh, origin, ac, pos, theta_backbone, theta_linear_segment
                                )
                                # sd_mesh_rotations = [transform_sd_mesh(sd_mesh, origin, ac, pos, theta_backbone, theta_linear_segment) for theta_linear_segment in np.linspace(0, 2*np.pi, 4, endpoint=False)]
                                s.combine_meshes([s.mesh, sd_mesh_transformed], operation=operation)
                            # s.mesh.show()

                            # b_flat-cs_round-sd_sphere-rot_b_90-rot_ls_90-pos_0.33_0.66
                            if operation == "union":
                                curv = "convex"
                            elif operation == "difference":
                                curv = "concave"
                            s.description = "{0}-{1}-{2}-rot_b_{3}-rot_ls_{4}-pos_{5}_{6}".format(
                                b_name,
                                cs_name_a,
                                sd_name,
                                str(np.round(theta_backbone / np.pi * 180).astype("int")),
                                str(np.round(theta_linear_segment / np.pi * 180).astype("int")),
                                "_".join([str(p) for p in pos_list]),
                                curv,
                            )
                            s.cs_name = [cs_name]
                            s.sd_name = sd_name
                            shapes.append(s)
stop = len(shapes)
transitions_dict["surface_deformations"] = [start, stop]

# Labeling the shapes
for i, s in enumerate(shapes):
    s.label = set_version + "_" + str(i).zfill(4)

# Attach interface
for i, s in enumerate(shapes):
    print(i)
    s.create_interface()
    s.fuse_mesh_to_interface()
    # s.mesh_with_interface.show()

###########################
### Pickling the shapes ###
###########################

# Make base_dir directory
if base_dir.is_dir() is False:
    base_dir.mkdir(parents=True)

# Save with timestamp
now = datetime.now()
timestamp = now.strftime("%d_%m_%y__%H_%M_%S")
pickle_name = Path(base_dir, "stimulus_set_ " + timestamp + ".pickle")
with open(pickle_name, "wb") as f:
    pickle.dump((shapes, transitions_dict), f)

# Load from pickled file
with open(pickle_name, "rb") as f:
    (shapes, transitions_dict) = pickle.load(f)

# ###################################
# ### Save shapes as .stl meshses ###
# ###################################
# for s in shapes:

#     # Make save_dir
#     save_dir = Path(base_dir, "stl")
#     if save_dir.is_dir() is False:
#         save_dir.mkdir(parents=True)

#     # Export mesh
#     filename = Path(save_dir, s.label).with_suffix(".stl")
#     s.mesh.export(filename)

# #####################################
# ### Save shapes as png renderings ###
# #####################################
# for s in shapes:

#     backbone = s.ac_list[0].backbone
#     ac = s.ac_list[0]
#     shape = s

#     # Create figure
#     fig = plt.figure(figsize=(12, 4))
#     gs = gridspec.GridSpec(1, 3, width_ratios=[1, 1, 2])
#     ax0 = plt.subplot(gs[0])
#     ax1 = plt.subplot(gs[1])
#     ax2 = plt.subplot(gs[2])
#     ax0.set_title(backbone.name)

#     # Plot backbone
#     x, y = backbone.controlpoints[:, :2].T
#     y_mean = y.mean()
#     padding = 10
#     t = np.linspace(0, 1, 100)
#     x, y, _ = backbone.r(t).T
#     ax0.plot(x, y, "b-", linewidth=10)
#     ax0.set_ylim([y_mean - BACKBONE_LENGTH / 2 - padding, y_mean + BACKBONE_LENGTH / 2 + padding])
#     ax0.set_aspect("equal")
#     ax0.tick_params(
#         axis="both", which="both", bottom=False, top=False, left=False, right=False, labelbottom=False, labelleft=False
#     )

#     # Plot cross sections
#     plot_name = "\n".join(s.cs_name)
#     ax1.set_title(plot_name)
#     for i, cs_name in enumerate(s.cs_name):
#         cp = d[cs_name]

#         # Make curve
#         degree = ORDER - 1
#         num_cp_per_cross_section = cp.shape[0]
#         num_knots = num_cp_per_cross_section + ORDER + degree
#         knot = np.linspace(0, 1, num_knots)
#         basis1 = BSplineBasis(order=ORDER, knots=knot, periodic=1)
#         curve = Curve(basis1, controlpoints=cp, rational=False)

#         # Sample curve
#         curve.reparam()
#         t = np.linspace(0, 1, 1000)
#         x, y = curve(t).T
#         x += i * 60  # Shift to right
#         ax1.set_ylim([-40, 40])
#         ax1.plot(x, y, "b-", linewidth=10)
#         ax1.set_aspect("equal")
#         ax1.tick_params(
#             axis="both",
#             which="both",
#             bottom=False,
#             top=False,
#             left=False,
#             right=False,
#             labelbottom=False,
#             labelleft=False,
#         )

#     # Plot shape
#     # rotation about x-axis
#     r = rotation - np.pi / 2
#     R = np.array(
#         [
#             [1, 0, 0, 0],
#             [0, np.cos(r), -np.sin(r), 0],
#             [0, np.sin(r), np.cos(r), 0],
#             [0, 0, 0, 1],
#         ]
#     )
#     color = shape.save_mesh_as_png(save_dir="", return_img=True, rotation=R)
#     color = np.flip(color, axis=1)  # Reverse y axis so that the render aligns with the above plots
#     ax2.imshow(color / 2**16)  # Convert to range (0,1)
#     ax2.tick_params(
#         axis="both", which="both", bottom=False, top=False, left=False, right=False, labelbottom=False, labelleft=False
#     )
#     if s.sd_name != None:
#         ax2.set_title(s.sd_name)

#     # Make save_dir
#     save_dir = Path(base_dir, "png")
#     if save_dir.is_dir() is False:
#         save_dir.mkdir(parents=True)

#     filename = Path(save_dir, s.label).with_suffix(".png")
#     fig.suptitle(s.label + "_" + s.description)
#     fig.savefig(filename)
#     # plt.show()
#     plt.close(fig)

max_stars_repo_name: williamsnider/objects

max_issues_repo_name: williamsnider/objects

max_forks_repo_name: williamsnider/objects


text:
"""
Copyright (c) 2019 Yevheniia Soroka
Licensed under the MIT License
Author: Yevheniia Soroka
Email: ysoroka@cs.stonybrook.edu
Last modified: 18/12/2019

Usage:
Run this script to plot cosine similarity matrix of word embeddings of emotion words.
"""

import os
import gluonnlp as nlp
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sb
from sklearn.metrics.pairwise import cosine_similarity
#from glove import Corpus, Glove
import glob
import pickle
from gensim.models import Word2Vec
from gensim.models import KeyedVectors

img_folder = "/nfs/bigfovea/add_disk0/eugenia/Emotion/Adobe/images/"
adobe_folder = "/nfs/bigfovea/add_disk0/eugenia/Emotion/Adobe/"
kw_folder = "/nfs/bigfovea/add_disk0/eugenia/Emotion/Adobe/keyword_lists/"
model_folder = "/nfs/bigfovea/add_disk0/eugenia/Emotion/wordembedding_models/"

def plot_sim_matrix(sm, classes, full=False):
	'''	saves a heatmap plot of a matrix of similarities 'sm' of classes 'classes' '''
    if not full: # include annotations and class names in the plot
        plt.figure(figsize=(6, 4))
        fntsz = 8
        cls = range(len(classes))
        save_folder = "/nfs/bigfovea/add_disk0/eugenia/Emotion/Adobe/matrices/small"
    else: # exclude annotations and class names from the plot
        plt.figure(figsize=(7, 5))
        fntsz = 9
        cls = classes
        save_folder = "/nfs/bigfovea/add_disk0/eugenia/Emotion/Adobe/matrices"
    plt.xticks(fontsize=fntsz)
    plt.yticks(fontsize=fntsz)
    mask = np.zeros_like(sm)
    mask[np.triu_indices_from(mask)] = True
    with sb.axes_style("white"):
        ax = sb.heatmap(sm, mask=mask, vmin=-0.08, vmax=0.83, annot=full, annot_kws={"size": fntsz},
                        cmap="YlGnBu", xticklabels=cls, yticklabels=cls)
    plt.tight_layout()
    b, t = plt.ylim()  # discover the values for bottom and top
    b += 0.5  # Add 0.5 to the bottom
    t -= 0.5  # Subtract 0.5 from the top
    plt.ylim(b, t)
    plt.savefig(os.path.join(save_folder, 'ft_w2v.png'), bbox_inches='tight')

#########  WORD2VEC MODEL  #########

# load word2vec model
model = Word2Vec.load(os.path.join(model_folder, "w2v_finetuned.model"))
#model = KeyedVectors.load_word2vec_format(os.path.join(model_folder, "finetuned_Adobe_emotions.model.wv.vectors.npy"), binary=False)
print("Model loaded")

# emotion lists
emotion_dict = {
    6: sorted(['love', 'anger', 'surprise', 'joy', 'sadness', 'fear']),
    8: sorted(['amusement', 'awe', 'contentment', 'excitement', 'anger', 'disgust', 'fear', 'sadness'])
}

# get emotion vectors
emotion_list = sorted([emotion for emotion in set(emotion_dict[6] + emotion_dict[8])])

# compute similarity matrix
pairwise_sims = np.array([model.wv.similarity(emo0, emo1) for emo0 in emotion_list for emo1 in emotion_list]).reshape((len(emotion_list), len(emotion_list)))
plot_sim_matrix(pairwise_sims, emotion_list)
plot_sim_matrix(pairwise_sims, emotion_list, full=True)

"""

#########  DEFAULT GLOVE MODEL  #########

# load word2vec model
glove = nlp.embedding.create('glove', source='glove.6B.100d')
vocab = nlp.Vocab(nlp.data.Counter(glove.idx_to_token))
vocab.set_embedding(glove)

# emotion lists
emotion_dict = {
    6: sorted(['love', 'anger', 'surprise', 'joy', 'sadness', 'fear']),
    8: sorted(['amusement', 'awe', 'contentment', 'excitement', 'anger', 'disgust', 'fear', 'sadness'])
}

# get emotion vectors
emotion_list = sorted([emotion for emotion in set(emotion_dict[6] + emotion_dict[8])])

# compute similarity matrix
pairwise_sims = np.array([cosine_similarity(vocab.embedding[emo0].asnumpy().reshape((1, -1)), vocab.embedding[emo1].asnumpy().reshape((1, -1)))
                          for emo0 in emotion_list for emo1 in emotion_list]).reshape((len(emotion_list), len(emotion_list)))

plot_sim_matrix(pairwise_sims, emotion_list)
plot_sim_matrix(pairwise_sims, emotion_list, full=True)
"""

"""

#########  ADOBE GLOVE MODEL  #########

#from gensim.scripts.glove2word2vec import glove2word2vec
out_file = "/nfs/bigfovea/add_disk0/eugenia/Emotion/wordembedding_models/glove_adobe/gensim_glove_vectors.txt"
#glove2word2vec(glove_input_file="/nfs/bigfovea/add_disk0/eugenia/Emotion/wordembedding_models/glove_adobe/vectors.txt",
#               word2vec_output_file=out_file)

# load word2vec model
from gensim.models.keyedvectors import KeyedVectors
glove = KeyedVectors.load_word2vec_format(out_file, binary=False)

# emotion lists
emotion_dict = {
    6: sorted(['love', 'anger', 'surprise', 'joy', 'sadness', 'fear']),
    8: sorted(['amusement', 'awe', 'contentment', 'excitement', 'anger', 'disgust', 'fear', 'sadness'])
}

# get emotion vectors
emotion_list = sorted([emotion for emotion in set(emotion_dict[6] + emotion_dict[8])])

# compute similarity matrix
pairwise_sims = np.array([glove.similarity(emo0, emo1) for emo0 in emotion_list for emo1 in emotion_list]).reshape((len(emotion_list), len(emotion_list)))
plot_sim_matrix(pairwise_sims, emotion_list)
plot_sim_matrix(pairwise_sims, emotion_list, full=True)
"""

max_stars_repo_name: cvlab-stonybrook/Emotion-Prediction

max_issues_repo_name: cvlab-stonybrook/Emotion-Prediction

max_forks_repo_name: cvlab-stonybrook/Emotion-Prediction


text:
import pathlib
import os
import sys
import unittest
import numpy as np
from .tools import debug_on
from numpy.polynomial.legendre import Legendre
import matplotlib.pyplot as plt
import time
try:
    from gsplines.basis import BasisLegendre
    from gsplines.basis import BasisLagrange
    from gsplines import Interpolator
except ImportError:
    MOD_PATH = pathlib.Path(__file__).parent.absolute()
    MOD_PATH_PYGSPLINES = pathlib.Path(MOD_PATH, '..', 'build')
    sys.path.append(str(MOD_PATH_PYGSPLINES))
    from gsplines.basis import BasisLegendre
    from gsplines.basis import BasisLagrange
    from gsplines import Interpolator

def show_piecewisefunction(_q, _up_to_deriv=3, _dt=0.1, _wp=None, _title=''):
    dim = _q.get_codom_dim()
    fig, ax = plt.subplots(_up_to_deriv + 1, dim)
    if dim == 1:
        ax = np.array([[ax[i]] for i in range(_up_to_deriv + 1)])
    if _title:
        fig.suptitle(_title)
    t = np.arange(0.0, _q.get_exec_time(), _dt)

    for i in range(0, _up_to_deriv + 1):
        q = _q.deriv(i)
        qt = q(t)
        for j in range(0, dim):
            ax[i, j].plot(t, qt[:, j])
            ax[i, j].grid()
            if i == 0:
                ax[i, j].set_title('coordinate {:d}'.format(j + 1), fontsize=8)

            if hasattr(_q, 'get_domain_breakpoints'):
                t_bp = _q.get_domain_breakpoints()
                for ti in t_bp:
                    ax[i, j].axvline(ti, alpha=0.1, color='red')
                if _wp is not None and i == 0:
                    ax[i, j].plot(t_bp, _wp[:, j], 'b*')

    plt.subplots_adjust(
        left=0.025,
        bottom=0.05,
        right=0.975,
        top=0.95,
        wspace=0.25,
        hspace=0.15)

class MyTest(unittest.TestCase):
    # @debug_on()
    def __init__(self, *args, **kwargs):
        super(MyTest, self).__init__(*args, **kwargs)
        np.random.seed()

    # @debug_on()
    def compute_gspline_test(self):
        """ Test the computation of an interpolating gpline """
        basis_dim = 8
        basis = BasisLegendre(basis_dim)
        dim = 6  # np.random.randint(1, 10)
        intervals = np.random.randint(3, 6)
        waypoints = np.random.rand(intervals+1, dim)
        interval_lengths = 1.0 + np.random.rand(intervals) * 2.0
        inter = Interpolator(dim, intervals, basis)
        res = inter.interpolate(interval_lengths, waypoints)

        show_piecewisefunction(res, 5, 0.10, waypoints)

        nodes = np.array([np.cos((2*k-1)/2/basis_dim*np.pi)
                          for k in range(1, basis_dim+1)])

        basis = BasisLagrange(nodes)
        inter = Interpolator(dim, intervals, basis)
        res = inter.interpolate(interval_lengths, waypoints)
        show_piecewisefunction(res, 5, 0.10, waypoints)
        plt.show()

#    #@debug_on()
#    def interpolation_test_computation_time(self):
#        """ copmutes the time it takes to interpolate """
#        basis = BasisLegendre(6)
#        dim = 7
#        intervals = 10
#        waypoints = np.random.rand(intervals+1, dim)
#        intervals_length = np.array(intervals*[1])
#
#        iters = 100
#        tmean = 0
#        for _ in range(iters):
#            time_0 = time.time()
#            inter = Interpolator(dim, intervals, basis)
#            _ = inter.interpolate(intervals_length, waypoints)
#            time_1 = time.time()
#            tmean += (time_1 - time_0)
#
#        print('mean time = ', tmean/iters)

    # @debug_on()
    def derivative_y(self):
        ''' Compare the numerical derivate of y w.r.t tau with the nominal one
        '''
        for _ in range(40):
            print('--- begin --')
            b_dim = 2*np.random.randint(1, 10)
            basis = BasisLegendre(b_dim)
            np.random.seed()
            dim = np.random.randint(1, 6)
            intervals = np.random.randint(2, 10)
            waypoints = (np.random.rand(intervals + 1, dim) - 0.5) * 2 * np.pi
            interval_lengths = 1.0 + np.random.rand(intervals) * 2.0
            print('interplator constrcutor codo_dim, N, bdim',
                  dim, intervals, b_dim)
            inter = Interpolator(dim, intervals, basis)
            y_nom = inter.solve_interpolation(
                interval_lengths, waypoints)

            dtau = 1.0e-5
            err = 0.0
            errp = 0.0
            for iinter in range(0, intervals):
                dydtauNom = inter.get_coeff_derivative_wrt_tau(
                    y_nom, interval_lengths, iinter)

                tauv_aux = interval_lengths.copy()
                tauv_aux[iinter] += -2 * dtau
                y0 = inter.solve_interpolation(
                    tauv_aux, waypoints).copy() * (1.0 / 12.0)
                tauv_aux[iinter] += dtau
                y1 = inter.solve_interpolation(
                    tauv_aux, waypoints).copy() * (-2.0 / 3.0)
                tauv_aux[iinter] += 2 * dtau
                y2 = inter.solve_interpolation(
                    tauv_aux, waypoints).copy() * (2.0 / 3.0)
                tauv_aux[iinter] += dtau
                y3 = inter.solve_interpolation(
                    tauv_aux, waypoints).copy() * (-1.0 / 12.0)
                dydtauiTest = (y0 + y1 + y2 + y3) / dtau
                ev = np.abs(dydtauiTest - dydtauNom)
                e = np.max(ev)
                eidx = np.argmax(ev)

                if(abs(dydtauiTest[eidx]) > 1.0e-5):
                    ep = e / abs(dydtauiTest[eidx])
                else:
                    ep = e

                if e > err:
                    err = e
                if ep > errp:
                    errp = ep

                self.assertTrue(ep < 5.0e-2, '''
                error on dydtau = {:10.7e}
                relative error  = {:10.7e}
                value of dydtau test = {:10.7e}
                value of dydtau nom = {:10.7e}
                '''.format(e, ep, dydtauiTest[eidx], dydtauNom[eidx]))

    def test(self):

        self.compute_gspline_test()

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

max_stars_repo_name: rafaelrojasmiliani/gsplinespp

max_issues_repo_name: rafaelrojasmiliani/gsplinespp

max_forks_repo_name: rafaelrojasmiliani/gsplinespp


text:
#!/usr/bin/env python3
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
#  Author: Jeremy Maurer, Raymond Hogenson & David Bekaert
#  Copyright 2019, by the California Institute of Technology. ALL RIGHTS
#  RESERVED. United States Government Sponsorship acknowledged.
#
# ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
import contextlib
import os
import sys

import numpy as np

from datetime import datetime, date

from RAiDER.logger import *
from RAiDER.utilFcns import getTimeFromFile

def getWMFilename(weather_model_name, time, outLoc):
    '''
    Check whether the output weather model exists, and
    if not, download it.
    '''
    with contextlib.suppress(FileExistsError):
        os.mkdir('weather_files')

    download_flag = True
    f = os.path.join(
        outLoc,
        '{}_{}.nc'.format(
            weather_model_name,
            datetime.strftime(time, '%Y_%m_%d_T%H_%M_%S')
        )
    )
    if weather_model_name in ['GMAO', 'MERRA2']:
        if time.date() < date(2017, 12, 1) and weather_model_name == 'GMAO':
            ext = 'nc'
        else:
            ext = 'h5'
        f = f[:-2]+ ext

    logger.debug('Storing weather model at: %s', f)

    if os.path.exists(f):
        logger.warning('Weather model already exists, skipping download')
        download_flag = False

    return download_flag, f

def prepareWeatherModel(
        weatherDict,
        wmFileLoc,
        lats=None,
        lons=None,
        los=None,
        zref=None,
        time=None,
        download_only=False,
        makePlots=False
    ):
    '''
    Parse inputs to download and prepare a weather model grid for interpolation
    '''
    weather_model, weather_files, weather_model_name = \
        weatherDict['type'], weatherDict['files'], weatherDict['name']

    # check whether weather model files are supplied
    if weather_files is None:
        if time is None:
            raise RuntimeError('prepareWeatherModel: Either a file or a time must be specified')
        download_flag,f = getWMFilename(weather_model.Model(), time, wmFileLoc)
        weather_model.files = [f]
    else:
        download_flag = False
        time = getTimeFromFile(weather_files[0])

    if (time < datetime(2013, 6, 26, 0, 0, 0)) and (weather_model._Name is 'HRES'):
        weather_model.update_a_b()

    # if no weather model files supplied, check the standard location
    if download_flag:
        weather_model.fetch(*weather_model.files, lats, lons, time)

        # exit on download if download_only requested
        if download_only:
            logger.warning(
                'download_only flag selected. No further processing will happen.'
            )
            return None, None, None

    # Load the weather model data
    if weather_model.files is not None:
        weather_model.load(*weather_model.files, outLats=lats, outLons=lons, los=los, zref=zref)
        download_flag = False
    else:
        weather_model.load(f, outLats=lats, outLons=lons, los=los, zref=zref)

    logger.debug('Number of weather model nodes: %d', np.prod(weather_model.getWetRefractivity().shape))
    logger.debug('Shape of weather model: %s', weather_model.getWetRefractivity().shape)
    logger.debug(
        'Bounds of the weather model: %.2f/%.2f/%.2f/%.2f (SNWE)',
        np.nanmin(weather_model._ys), np.nanmax(weather_model._ys),
        np.nanmin(weather_model._xs), np.nanmax(weather_model._xs)
    )
    logger.debug('Weather model: %s', weather_model.Model())
    logger.debug(
        'Mean value of the wet refractivity: %f',
        np.nanmean(weather_model.getWetRefractivity())
    )
    logger.debug(
        'Mean value of the hydrostatic refractivity: %f',
        np.nanmean(weather_model.getHydroRefractivity())
    )
    logger.debug(weather_model)

    if makePlots:
        p = weather_model.plot('wh', True)
        p = weather_model.plot('pqt', True)

    return weather_model, lats, lons


text:
import iSeaborn as isn
from bokeh.plotting import output_file, save
from numpy import median

tips = isn.load_dataset("tips")
fig = isn.barplot(x="day", y="tip", data=tips, estimator=median)

output_file("setDesiredEstimator.html")
save(fig)

max_stars_repo_name: pseudoPixels/bokehBot

max_issues_repo_name: pseudoPixels/bokehBot

max_forks_repo_name: pseudoPixels/bokehBot

reason: from numpy