# Notebook from biolins/frivolous_dnns
Path: ipy_notebooks/plot_ablation_thresholds.ipynb

<code>
import numpy as np
import pandas as pd
import sys
import seaborn
import matplotlib
import matplotlib.ticker
from matplotlib import pyplot as plt

matplotlib.rcParams['pdf.fonttype'] = 42
matplotlib.rcParams['ps.fonttype'] = 42
seaborn.set()
seaborn.set_style("white")
seaborn.set_context("poster")
_____no_output_____def get_threshold(ab_data, th=0.8):
    # ab_data is a crosses x ablation proportions array, th is threshold
    crosses, ranges = ab_data.shape
    for c in range(crosses):
        for r in range(ranges-1):
            if ab_data[c,r] >= th and ab_data[c,r+1] < th:
                x1 = r / (ranges-1)
                x2 = (r+1) / (ranges-1)
                y1 = ab_data[c,r]
                y2 = ab_data[c,r+1]
                m_inv = (x2 - x1) / (y2 - y1)
                ab_data[c,0] = (m_inv * (th - y1)) + x1  # store in the 0 column
                break
    return ab_data[:, 0]  # return 0 column

def update_results(ablation_results, factor, curve_idx, size_idx, factor_multiplier):

    ### CHANGED
    ablation_results = get_threshold(ablation_results) * (1/factor) * factor_multiplier
    results[seed, :, curve_idx, size_idx] =  ablation_results
    results_no[seed,: , curve_idx, size_idx] = 1/2**(4-size_idx) - ablation_results

def get_mean_std(res, maximum=True):

    results_tmp=np.zeros((curves,sizes,2))
    if maximum:
        results_tmp[:,:,0] = np.mean( np.max(res, axis=1), axis=(0))
        results_tmp[:,:,1] = np.std( np.max(res, axis=1), axis=(0))
    else:
        results_tmp[:,:,0] = np.mean( np.min(res, axis=1), axis=(0))
        results_tmp[:,:,1] = np.std( np.min(res, axis=1), axis=(0))
    return results_tmp

def get_slope(res):

    res[res == 0]  = np.nan

    res_tmp = np.zeros((np.shape(res)[0],np.shape(res)[1]-1))
    for i in range(0,np.shape(res)[1]-1):
        res_tmp[:, i] = (res[:,i+1,0] / res[:,i,0] )
    x = np.nanmean(res_tmp, axis=1)
    #x = res_tmp
    x  = x [~np.isnan(x )]
    return x

def print_ablation(title, data, marker_styles, curve_colors, curve_styles,
                   curve_labels, yticks, leg_title, rand_base, leg_loc, leg_flag,
                   name, log_y, non, slopes, ncol=1, include_xlabel=True, include_ylabel=True):
    fig, ax = plt.subplots(figsize=(3, 3))
    ax.xaxis.tick_bottom() ### CHANGED
    ax.yaxis.tick_left() ### CHANGED
    plt.title(title , fontsize=25)
    if include_xlabel:
        plt.xlabel(r'Model Size Factor', fontsize=25)
    plt.xlim((0.245, 4.07))
    if log_y:
        plt.yscale('log', basey=2)
        plt.ylim((np.min(yticks), np.max(yticks)))
    else:
        from matplotlib.ticker import FormatStrFormatter
        plt.ylim((0, np.max(yticks)))
        ax.yaxis.set_major_formatter(FormatStrFormatter('%.2f'))

    if include_ylabel:
        if non:
            plt.ylabel('Non-Prunability', fontsize=25)
        else:
            plt.ylabel('Prunability', fontsize=25)

    #plt.grid(True, which='both')
    plt.xscale('log', basex=2)
    plt.xticks(size_factors, size_labels)
    plt.yticks(yticks)

    for curve_idx in range(np.shape(data)[0]):
        non_0_idx = (data[curve_idx,:,0] != 0)
        plt.errorbar(size_factors[non_0_idx], data[curve_idx,non_0_idx,0], yerr=data[curve_idx,non_0_idx,1].T,
                 marker=marker_styles[curve_idx], markersize=7, color=curve_colors[curve_idx],
                 linestyle=curve_styles[curve_idx], linewidth=3)

    if leg_flag:
        leg = plt.legend(curve_labels, loc=leg_loc, framealpha=0.8, fontsize=10, title=leg_title,ncol=ncol)
        leg.get_title().set_fontsize('10')

    import math
    if not np.any(np.isnan(data[:,3,0])):
        plt.annotate(r"$g_{max}=$" + "{:.2f}".format(np.max(slopes)), xy= (math.log(3,2), np.max(data[:,3,0])),\
                 horizontalalignment='right', fontsize=14)
    else:
        if not slopes.size==0:

            plt.annotate(r"$g_{max}=$" + "{:.2f}".format(np.max(slopes)), xy= (math.log(2,2), np.max(data[:,2,0])),\
                 horizontalalignment='right', verticalalignment='bottom', fontsize=14)
    if not slopes.size==0:
        plt.annotate(r"$g_{min}=$" + "{:.2f}".format(np.min(slopes)), xy= (math.log(2,2), np.min(data[:,1,0])),\
                 horizontalalignment='left', fontsize=14)

    for item in ([ax.title, ax.xaxis.label, ax.yaxis.label]):
        item.set_fontsize(15)
    for item in (ax.get_xticklabels() + ax.get_yticklabels()):
        item.set_fontsize(14)

    plt.rc('xtick',labelsize=8)

    plt.show()
    fig.savefig(name+".pdf",format='pdf', bbox_inches='tight',dpi=1000)

def set_results(ID, experiments, size_idx, knockout_range, layer=None):

    csv_file = experiments.opt[ID].csv_dir + experiments.opt[ID].name + '_robustness.csv'
    csv_file = csv_file.replace("seed=0", "seed=" + str(seed))
    df = pd.read_csv(csv_file)

    ablation_results = np.zeros((crosses, range_len))
    for cross in range(crosses):
        for amount in range(range_len):
            if layer == None:
                ablation_results[cross, amount] = df[df['cross_validation'] == cross] \
                    [df['evaluation_set'] == 'test'][df['perturbation_layer'] == 'all'] \
                    [df['perturbation_name'] == 'Activation Knockout'][df['perturbation_amount'] == knockout_range[amount]] \
                    ['performance'].iloc[0]
            else:
                ablation_results[cross, amount] = df[df['cross_validation'] == cross] \
                    [df['evaluation_set'] == 'test'][df['perturbation_layer'] == str(layer)] \
                    [df['perturbation_name'] == 'Activation Knockout'][df['perturbation_amount'] == knockout_range[amount]] \
                    ['performance'].iloc[0]

    return ablation_results

    _____no_output_____sys.path.append('../')
import experiments

range_len = 7
curves = 4
curve_labels = ['AlexNet-CIFAR', 'AlexNet-Random', 'ResNet56-CIFAR', 'ResNet56-Random']
curve_colors = ['darkblue', 'darkblue', 'indianred', 'indianred']
curve_styles = ['-', ':', '-', ':']
marker_styles = ['o', 'v', 'o', 'v']

sizes = 5
size_factors = np.array([0.25, 0.5, 1, 2, 4])
size_idx_map = {0.25: 0, 0.5: 1, 1: 2, 2: 3, 4: 4}
size_labels = ['1/4x', '1/2x', '1x', '2x', '4x']
knockout_range = np.linspace(0.0, 1.0, num=range_len)
crosses = 3

seeds = 2
results = np.zeros((seeds, crosses, curves, sizes))
results_no = np.zeros((seeds, crosses, curves, sizes))
# all are indexed [size_factor][nn_type][avg/lowererr/uppererr]
# we have to make sure to not be sketchy and state that these are averages within a net instead of between nets

for seed in range(seeds):
    for ID in range(62, 92):
        opt = experiments.opt[ID]

        if opt.dataset.random_labels:
            curve_idx = 1  # random labels
        elif opt.hyper.augmentation and opt.hyper.drop_train < 1:
            curve_idx = 0  # all regularizers
        else:
            continue

        ablation_results = set_results(ID, experiments,
                            size_idx_map[opt.dnn.neuron_multiplier[0]], knockout_range)
        update_results(ablation_results, 4, curve_idx, \
                       size_idx_map[opt.dnn.neuron_multiplier[0]], opt.dnn.neuron_multiplier[0])

    for ID in range(217, 231):
        opt = experiments.opt[ID]

        if opt.dataset.random_labels:
            curve_idx = 3  # random labels
        elif opt.hyper.lr_bs_factor == 1:
            curve_idx = 2  # all regularizers
        else:
            continue

        ablation_results = set_results(ID, experiments,
                        size_idx_map[opt.dnn.neuron_multiplier[0]], knockout_range)
        update_results(ablation_results, 4, curve_idx, \
                       size_idx_map[opt.dnn.neuron_multiplier[0]], opt.dnn.neuron_multiplier[0])

results = get_mean_std(results)
results_no = get_mean_std(results_no, False)

slopes = get_slope(results)
slopes_no = get_slope(results_no)

/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:124: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:125: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
leg_loc = (0.05, -0.9)
yticks = [0, .20, .4, 0.6]  ### CHANGED
yticks_log = [1/512,1/128,  1/32,  1/8, 1/2,1]  ### CHANGED
yticks_log2 =  [0, 1/4,1/2,1] ### CHANGED

#                 "robust_norm_cifar_train", False)
print_ablation('CIFAR-10', results, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Network-Labels}$", 0.1,leg_loc, False,
                 "prune_cifar_rand", True, False, slopes, include_xlabel=False)

print_ablation('CIFAR-10', results_no, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Network-Labels}$", 0.1,leg_loc, False,
                 "nonprune_cifar_rand", True, True, slopes_no, include_xlabel=False)

_____no_output_____sys.path.append('../')
import experiments

range_len = 7
curves = 5
curve_labels = ['None', 'All', 'DataAug.', 'Dropout', 'W. Decay']
curve_colors = ['slateblue', 'darkblue', 'purple', 'violet', 'pink']
curve_styles = ['-', '-', '--', '--', '--']
marker_styles = ['o', 'o', 'v', 'v', 'v']

sizes = 5
size_factors = np.array([0.25, 0.5, 1, 2, 4])
size_idx_map = {0.25: 0, 0.5: 1, 1: 2, 2: 3, 4: 4}
size_labels = ['1/4x', '1/2x', '1x', '2x', '4x']
knockout_range = np.linspace(0.0, 1.0, num=range_len)
crosses = 3

seeds = 3
results = np.zeros((seeds, crosses, curves, sizes))
results_no = np.zeros((seeds, crosses, curves, sizes))

# all are indexed [size_factor][nn_type][avg/lowererr/uppererr]
# we have to make sure to not be sketchy and state that these are averages within a net instead of between nets

for seed in range(seeds):
    for ID in range(62, 92):
        opt = experiments.opt[ID]

        curve_idx = 0  # unregularized
        if opt.dataset.random_labels:
            continue  # random labels
        elif opt.hyper.augmentation and opt.hyper.drop_train < 1:
            curve_idx = 1  # all regularizers
        elif opt.hyper.augmentation:
            curve_idx = 2
        elif opt.hyper.drop_train < 1:
            curve_idx = 3
        elif opt.hyper.weight_decay:
            curve_idx = 4

        ablation_results = set_results(ID, experiments,
                        size_idx_map[opt.dnn.neuron_multiplier[0]], knockout_range)
        update_results(ablation_results, 4, curve_idx, \
                       size_idx_map[opt.dnn.neuron_multiplier[0]], opt.dnn.neuron_multiplier[0])

results = get_mean_std(results)
results_no = get_mean_std(results_no, False)

slopes = get_slope(results)
slopes_no = get_slope(results_no)
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:124: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:125: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
leg_loc = (-0.22, -0.8)

yticks = [0, 20, 40, 60]  ### CHANGED
yticks_log = [1/512,1/128,  1/32,  1/8, 1/2,1]  ### CHANGED
yticks_log2 =  [0, 1/4,1/2,1] ### CHANGED

print_ablation('CIFAR-10', results, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Regularizer}$", 0.1,leg_loc, False,
                 "prune_cifar_regularizers", True, False, slopes, ncol=2, include_xlabel=False)

print_ablation('CIFAR-10', results_no, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Regularizer}$", 0.1,leg_loc, False,
                 "nonprune_cifar_regularizers", True, True, slopes_no, include_xlabel=False)

_____no_output_____#Batch - CIFAR
sys.path.append('../')
import experiments

range_len = 7
curves = 6
curve_labels = ['AlexNet-k=1', 'AlexNet-k=0.25', 'AlexNet-k=4', 'ResNet-k=1', 'ResNet-k=0.25', 'ResNet-k=4']
curve_colors = ['slateblue', 'lightblue', 'lightblue', 'indianred', 'mistyrose', 'mistyrose']
curve_styles = ['-', '--', '--', '-', '--', '--']
marker_styles = ['o', 'v', 's', 'o', 'v', 's']
lr_factor = [1, 0.25, 4]

sizes = 5
size_factors = np.array([0.25, 0.5, 1, 2, 4])
size_idx_map = {0.25: 0, 0.5: 1, 1: 2, 2: 3, 4: 4}
size_labels = ['1/4x', '1/2x', '1x', '2x', '4x']
knockout_range = np.linspace(0.0, 1.0, num=range_len)
crosses = 3

seeds = 2
results = np.zeros((seeds, crosses, curves, sizes))
results_no = np.zeros((seeds, crosses, curves, sizes))

# all are indexed [size_factor][nn_type][avg/lowererr/uppererr]
# we have to make sure to not be sketchy and state that these are averages within a net instead of between nets

for seed in range(seeds):
    for ID in list(range(62, 92)) + list(range(157,217)) :

        #print('Processing ID:', ID)
        opt = experiments.opt[ID]

        if opt.dataset.random_labels:
            continue
        if not (opt.hyper.augmentation or opt.hyper.drop_train < 1 or opt.hyper.weight_decay):
            curve_idx = np.where(np.array(opt.hyper.lr_bs_factor) == lr_factor)[0][0]
        else:
            continue

        if ID == 166: #Not possible to calculate
            continue

        ablation_results = set_results(ID, experiments,
                        size_idx_map[opt.dnn.neuron_multiplier[0]], knockout_range)
        update_results(ablation_results, 4, curve_idx, \
                       size_idx_map[opt.dnn.neuron_multiplier[0]], opt.dnn.neuron_multiplier[0])

    for ID in list(range(217,232)) + list(range(234,239)):

        #print('Processing ID:', ID)
        opt = experiments.opt[ID]

        if opt.dataset.random_labels:
            continue
        else:
            curve_idx = np.where(np.array(opt.hyper.lr_bs_factor) == lr_factor)[0][0] + 3

        ablation_results = set_results(ID, experiments,
                        size_idx_map[opt.dnn.neuron_multiplier[0]], knockout_range)
        update_results(ablation_results, 4, curve_idx, \
                       size_idx_map[opt.dnn.neuron_multiplier[0]], opt.dnn.neuron_multiplier[0])

results = get_mean_std(results)
results_no = get_mean_std(results_no, False)

slopes = get_slope(results)
slopes_no = get_slope(results_no)

        /usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:124: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:125: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
leg_loc = (0.0, 0.42)
yticks = [0, 20, 40, 60]  ### CHANGED
yticks_log = [1/512,1/128,  1/32,  1/8, 1/2, 1]  ### CHANGED
yticks_log2 =  [0, 1/4,1/2,1] ### CHANGED

print_ablation('CIFAR-10', results, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Network-k}$", 0.1,leg_loc, False,
                 "prune_cifar_batch", True, False, slopes)

print_ablation('CIFAR-10', results_no, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Network-k}$", 0.1,leg_loc, False,
                 "nonprune_cifar_batch", True, True, slopes_no)

_____no_output_____#Layer - CIFAR
sys.path.append('../')
import experiments

range_len = 7
curves = 4
curve_labels = ['c1',  'c2', 'fc1', 'fc2']
cm = seaborn.light_palette("indianred", as_cmap=True)
curve_colors = [cm(i) for i in np.linspace(0.0, 1.0, num=curves+1)][1:]
curve_styles = ['-', '-', '-', '-', '-']
marker_styles = ['o', 'o', 'o', 'o', 'o']

sizes = 5
yticks = [0, 0.2, 0.4, 0.6, 0.8, 1]
size_factors = np.array([0.25, 0.5, 1, 2, 4])
size_idx_map = {0.25: 0, 0.5: 1, 1: 2, 2: 3, 4: 4}
size_labels = ['1/4x', '1/2x', '1x', '2x', '4x']
knockout_range = np.linspace(0.0, 1.0, num=range_len)
crosses = 3

seeds = 2
results = np.zeros((seeds, crosses, curves, sizes))
results_no = np.zeros((seeds, crosses, curves, sizes))
# all are indexed [size_factor][nn_type][avg/lowererr/uppererr]
# we have to make sure to not be sketchy and state that these are averages within a net instead of between nets

for seed in range(seeds):
    for ID in list(range(217,232)) + list(range(234,239)):
        for curve_idx, layer in enumerate([0, 1, 2, 3]):

            #print('Processing ID:', ID)
            opt = experiments.opt[ID]

            if opt.dataset.random_labels or not opt.hyper.lr_bs_factor == 1:
                continue

            size_idx = size_idx_map[opt.dnn.neuron_multiplier[0]]

            ablation_results = set_results(ID, experiments,
                            size_idx_map[opt.dnn.neuron_multiplier[0]], knockout_range, layer)
            update_results(ablation_results, 4, curve_idx, \
                           size_idx_map[opt.dnn.neuron_multiplier[0]], opt.dnn.neuron_multiplier[0])

results = get_mean_std(results)
results_no = get_mean_std(results_no, False)

slopes = get_slope(results)
slopes_no = get_slope(results_no)

leg_loc = (0.0, 0.42)

print_ablation('CIFAR-10', results, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Layer}$", 0.1,leg_loc, False,
                 "prune_cifar_layer", True, False, slopes)

print_ablation('CIFAR-10', results_no, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Layer}$", 0.1,leg_loc, False,
                 "nonprune_cifar_layer", True, True, slopes_no)

/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:129: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:130: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
#Init - CIFAR
sys.path.append('../')
import experiments

range_len = 7
curves = 3
curve_labels = ['Glorot Norm', 'He Norm', 'Lecun Norm']
curve_colors = ['slateblue', 'skyblue', 'steelblue']
curve_styles = ['-',  '-',  '-']
marker_styles = ['o', 'o','o']

sizes = 5
size_factors = np.array([0.25, 0.5, 1, 2, 4])
size_idx_map = {0.25: 0, 0.5: 1, 1: 2, 2: 3, 4: 4}
size_labels = ['1/4x', '1/2x', '1x', '2x', '4x']
knockout_range = np.linspace(0.0, 1.0, num=range_len)
crosses = 3

seeds = 3
results = np.zeros((seeds, crosses, curves, sizes))
results_no = np.zeros((seeds, crosses, curves, sizes))

# all are indexed [size_factor][nn_type][avg/lowererr/uppererr]
# we have to make sure to not be sketchy and state that these are averages within a net instead of between nets

for seed in range(seeds):
    for ID in range(2,32):

        opt = experiments.opt[ID]

        curve_idx = opt.init_type // 2
        if opt.init_type in [1, 3, 5]:
            continue

        ablation_results = set_results(ID, experiments,
                        size_idx_map[opt.dnn.neuron_multiplier[0]], knockout_range)
        update_results(ablation_results, 4, curve_idx, \
                       size_idx_map[opt.dnn.neuron_multiplier[0]], opt.dnn.neuron_multiplier[0])

results = get_mean_std(results)
results_no = get_mean_std(results_no, False)

slopes = get_slope(results)
slopes_no = get_slope(results_no)
        /usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:124: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:125: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
leg_loc = (0.15, -0.8)
leg_loc_log = (0.48, 0.01)
yticks = [0, 20, 40, 60]  ### CHANGED
yticks_log = [1/512,1/128,  1/32,  1/8, 1/2, 1]  ### CHANGED
yticks_log2 = [0, 1/4, 1/2, 1]  ### CHANGED

print_ablation('CIFAR-10', results, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Initializer}$", 0.1,leg_loc, False,
                 "prune_cifar_initializers", True, False, slopes, include_xlabel=False)

print_ablation('CIFAR-10', results_no, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Initializer}$", 0.1,leg_loc_log, False,
                 "nonprune_cifar_initializers", True, True, slopes_no, include_xlabel=False)
slopes
_____no_output_____#Init - CIFAR

range_len = 7
curves = 3
curve_labels = ['Glorot Norm', 'He Norm', 'Lecun Norm']
curve_colors = ['slateblue', 'skyblue', 'steelblue']
curve_styles = ['-',  '-',  '-']
marker_styles = ['o', 'o','o']

sizes = 5
size_factors = np.array([0.25, 0.5, 1, 2, 4])
size_idx_map = {0.25: 0, 0.5: 1, 1: 2, 2: 3, 4: 4}
size_labels = ['1/4x', '1/2x', '1x', '2x', '4x']
knockout_range = np.linspace(0.0, 1.0, num=range_len)
crosses = 3

seeds = 3
results = np.zeros((seeds, crosses, curves, sizes))
results_no = np.zeros((seeds, crosses, curves, sizes))

for seed in range(seeds):
    for ID in range(2,32):

        opt = experiments.opt[ID]

        curve_idx = opt.init_type // 2
        if opt.init_type in [1, 3, 5]:
            continue

        ablation_results = set_results(ID, experiments,
                        size_idx_map[opt.dnn.neuron_multiplier[0]], knockout_range, layer=1)
        update_results(ablation_results, 4, curve_idx, \
                       size_idx_map[opt.dnn.neuron_multiplier[0]], opt.dnn.neuron_multiplier[0])

results = get_mean_std(results)
results_no = get_mean_std(results_no, False)

slopes = get_slope(results)
slopes_no = get_slope(results_no)

leg_loc = (0.15, -0.8)
yticks = [0, 20, 40, 60]  ### CHANGED
yticks_log = [1/512,1/128,  1/32,  1/8, 1/2, 1]  ### CHANGED
yticks_log2 = [0, 1/4, 1/2, 1]  ### CHANGED

print_ablation('CIFAR-10 (Layer Conv2)', results, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Initializer}$", 0.1,leg_loc, False,
                 "prune_cifar_initializers_conv2", True, False, slopes)

print_ablation('CIFAR-10 (Layer Conv2)', results_no, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Initializer}$", 0.1,leg_loc, False,
                 "nonprune_cifar_initializers_conv2", True, True, slopes_no)
slopes/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:129: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:130: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
#Init - CIFAR
sys.path.append('../')
import experiments

range_len = 7
curves = 3
curve_labels = ['Glorot Unif.', 'He Unif.', 'Lecun Unif.']
curve_colors = ['slateblue', 'skyblue',  'steelblue']
curve_styles = ['--', '--', '--']
marker_styles = ['v', 'v', 'v']

sizes = 5
size_factors = np.array([0.25, 0.5, 1, 2, 4])
size_idx_map = {0.25: 0, 0.5: 1, 1: 2, 2: 3, 4: 4}
size_labels = ['1/4x', '1/2x', '1x', '2x', '4x']
knockout_range = np.linspace(0.0, 1.0, num=range_len)
crosses = 2

seeds = 3
results = np.zeros((seeds, crosses, curves, sizes))
results_no = np.zeros((seeds, crosses, curves, sizes))

# all are indexed [size_factor][nn_type][avg/lowererr/uppererr]
# we have to make sure to not be sketchy and state that these are averages within a net instead of between nets

for seed in range(seeds):
    for ID in range(2,32):

        opt = experiments.opt[ID]

        curve_idx = opt.init_type // 2
        if opt.init_type in [0, 2, 4]:
            continue

        ablation_results = set_results(ID, experiments,
                        size_idx_map[opt.dnn.neuron_multiplier[0]], knockout_range)
        update_results(ablation_results, 4, curve_idx, \
                       size_idx_map[opt.dnn.neuron_multiplier[0]], opt.dnn.neuron_multiplier[0])

results = get_mean_std(results)
results_no = get_mean_std(results_no, False)

slopes = get_slope(results)
slopes_no = get_slope(results_no)
        /usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:124: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:125: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
leg_loc = (0.01, 0.49)
yticks = [0, 20, 40, 60]  ### CHANGED
yticks_log = [1/512,1/128,  1/32,  1/8, 1/2, 1]  ### CHANGED
yticks_log2 =  [0, 1/4,1/2,1] ### CHANGED

print_ablation('CIFAR-10', results, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Initializer}$", 0.1,leg_loc, False,
                 "prune_cifar_initializers_uni", True, False, slopes)

print_ablation('CIFAR-10', results_no, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Initializer}$", 0.1,leg_loc, False,
                 "nonprune_cifar_initializers_uni", True, True, slopes_no)
slopes_____no_output_____import pickle

with open('ids_mlps.p', 'rb') as handle:
    IDs = pickle.load(handle)

dataset_idx_map = {"rand10": 0, "rand10000": 1}
data_idx_map = {1e3: 0, 1e4: 1, 1e5: 2}
optimizer_idx_map = {0: 0, 1: 1, 2: 2, 5: 3}
init_idx_map = {1e-2: 0, 1e-1:1, 1:2, 1e1:3, 1e2:4, 1e3:5, 1e4:6, 1e5:7, 1e6:8, 1e7:9}
_____no_output_____#Base - MLPs

sys.path.append('../')
import experiments

range_len = 7
sizes = 5
curves = 3
curve_labels = ['1K', '10K', '100K']
curve_colors = ['orange', 'orange', 'orange']
curve_styles = ['-', '--',':' ]
marker_styles = ['o', 'o', 'o']

size_factors = np.array([0.25, 0.5, 1, 2, 4])
size_idx_map = {0.25: 0, 0.5: 1, 1: 2, 2: 3, 4: 4}
size_labels = ['1/4x', '1/2x', '1x', '2x', '4x']
knockout_range = np.linspace(0.0, 1.0, num=range_len)
crosses = 3
factors = [1, 2, 4, 8, 16]
#select_lr = [1e-1, 1e-2, 1e-3, 1e-3]

seeds = 3

#inits_fact = [1e-2, 1, 1e2, 1e4, 1e6]  ### SC

results = np.zeros((seeds, crosses, curves, sizes))
results_no = np.zeros((seeds, crosses, curves, sizes))

# both are indexed [nn_type][size_factor]
# both are indexed [nn_type][size_factor]
for seed in range(seeds):
    for curve_idx in range(curves):

        optimizer_idx = 0

        if curve_idx == 0:
            dataset_idx = dataset_idx_map["rand10"]
            data_idx = data_idx_map[1e3]
        elif  curve_idx == 1:
            dataset_idx = dataset_idx_map["rand10"]
            data_idx = data_idx_map[1e4]
        elif  curve_idx == 2:
            dataset_idx = dataset_idx_map["rand10"]
            data_idx = data_idx_map[1e5]

        for size_idx in range(sizes):
            id_best = int(IDs[1][seed, dataset_idx, data_idx, optimizer_idx, size_idx])
            opt = experiments.opt[id_best]

            ablation_results = set_results(id_best, experiments, \
                        size_idx, knockout_range)
            update_results(ablation_results, 16, curve_idx, size_idx, opt.dnn.neuron_multiplier[0])

results = get_mean_std(results)
results_no = get_mean_std(results_no, False)

slopes = get_slope(results)
slopes_no = get_slope(results_no)
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:124: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:125: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
yticks = [0, 20, 40, 60, 80, 100]  ### CHANGED
yticks_log = [1/64,1/16, 1/4,1 ]  ### CHANGED
yticks_log2 = [0, 1/8, 1/4,1/2 ]  ### CHANGED
leg_loc1 = (0.02, 0.39)
leg_loc2 = (0.38, 0.01)

print_ablation('Uncorr. Data 10 dim', results, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"#$\bf{Data}$", 0.1,leg_loc1, False,
                 "prune_synth_10_data", True, False, slopes)

print_ablation('Uncorr. Data 10 dim', results_no, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"#$\bf{Data}$", 0.1,leg_loc2, False,
                 "nonprune_synth_10_data", True, True, slopes_no)

_____no_output_____#Base - MLPs

sys.path.append('../')
import experiments

range_len = 7
sizes = 5
curves = 3
curve_labels = ['1K', '10K', '100K']
curve_colors = ['purple','purple','purple']
curve_styles = [ '-', '--', ':']
marker_styles = [ 'v',  'v','v']

size_factors = np.array([0.25, 0.5, 1, 2, 4])
size_idx_map = {0.25: 0, 0.5: 1, 1: 2, 2: 3, 4: 4}
size_labels = ['1/4x', '1/2x', '1x', '2x', '4x']
knockout_range = np.linspace(0.0, 1.0, num=range_len)
crosses = 3
factors = [1, 2, 4, 8, 16]
#select_lr = [1e-1, 1e-2, 1e-3, 1e-3]

seeds = 3

#inits_fact = [1e-2, 1, 1e2, 1e4, 1e6]  ### SC

results = np.zeros((seeds, crosses, curves, sizes))
results_no = np.zeros((seeds, crosses, curves, sizes))

# both are indexed [nn_type][size_factor]
# both are indexed [nn_type][size_factor]
for seed in range(seeds):
    for curve_idx in range(curves):

        optimizer_idx = 0

        if curve_idx == 0:
            dataset_idx = dataset_idx_map["rand10000"]
            data_idx = data_idx_map[1e3]
        elif  curve_idx == 1:
            dataset_idx = dataset_idx_map["rand10000"]
            data_idx = data_idx_map[1e4]
        elif  curve_idx == 2:
            dataset_idx = dataset_idx_map["rand10000"]
            data_idx = data_idx_map[1e5]

        for size_idx in range(sizes):
            id_best = int(IDs[1][seed, dataset_idx, data_idx, optimizer_idx, size_idx])
            opt = experiments.opt[id_best]

            ablation_results = set_results(id_best, experiments, \
                        size_idx, knockout_range)
            update_results(ablation_results, 16, curve_idx, size_idx, opt.dnn.neuron_multiplier[0])

results = get_mean_std(results)
results_no = get_mean_std(results_no, False)

slopes = get_slope(results)
slopes_no = get_slope(results_no)
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:124: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:125: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
yticks = [0, 20, 40, 60, 80, 100]  ### CHANGED
yticks_log = [1/64,1/16, 1/4,1 ]  ### CHANGED
yticks_log2 = [0, 1/8, 1/4,1/2 ]  ### CHANGED
leg_loc1 = (0.02, 0.39)
leg_loc2 = (0.38, 0.01)

print_ablation('Uncorr. Data 10K dim', results, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"#$\bf{Data}$", 0.1,leg_loc1, False,
                 "prune_synth_10k_data", True, False, slopes)

print_ablation('Uncorr. Data 10K dim', results_no, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"#$\bf{Data}$", 0.1,leg_loc2, False,
                 "nonprune_synth_10k_data", True, True, slopes_no)
_____no_output_____#Base - MLPs

sys.path.append('../')
import experiments

range_len = 7
sizes = 5
curves = 4
curve_labels = ['Momentum', 'SGD', 'Adam', 'RMSprop']
curve_colors = ['orange','olive', 'olive', 'olive']
curve_styles = ['-','-', '--',':']
marker_styles = ['o', 'o', 'o', 'o']
size_factors = np.array([0.25, 0.5, 1, 2, 4])
size_idx_map = {0.25: 0, 0.5: 1, 1: 2, 2: 3, 4: 4}
size_labels = ['1/4x', '1/2x', '1x', '2x', '4x']
knockout_range = np.linspace(0.0, 1.0, num=range_len)
crosses = 3
factors = [1, 2, 4, 8, 16]
#select_lr = [1e-1, 1e-2, 1e-3, 1e-3]

seeds = 3

#inits_fact = [1e-2, 1, 1e2, 1e4, 1e6]  ### SC

results = np.zeros((seeds, crosses, curves, sizes))
results_no = np.zeros((seeds, crosses, curves, sizes))

# both are indexed [nn_type][size_factor]
for seed in range(seeds):
    for curve_idx in range(curves):

        optimizer_idx = 0
        data_idx = data_idx_map[1e3]
        if curve_idx == 0:
            dataset_idx = dataset_idx_map["rand10"]
            optimizer_idx = optimizer_idx_map[0]
        elif curve_idx == 1:
            dataset_idx = dataset_idx_map["rand10"]
            optimizer_idx = optimizer_idx_map[1]
        elif  curve_idx == 2:
            dataset_idx = dataset_idx_map["rand10"]
            optimizer_idx = optimizer_idx_map[2]
        elif  curve_idx == 3:
            dataset_idx = dataset_idx_map["rand10"]
            optimizer_idx = optimizer_idx_map[5]

        for size_idx in range(sizes):
            id_best = int(IDs[1][seed, dataset_idx, data_idx, optimizer_idx, size_idx])
            opt = experiments.opt[id_best]

            ablation_results = set_results(id_best, experiments, \
                        size_idx, knockout_range)
            update_results(ablation_results, 16, curve_idx, size_idx, opt.dnn.neuron_multiplier[0])

results = get_mean_std(results)
results_no = get_mean_std(results_no, False)

slopes = get_slope(results)
slopes_no = get_slope(results_no)

/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:124: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:125: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
yticks = [0, 20, 40, 60, 80, 100]  ### CHANGED
yticks_log = [1/64,1/16, 1/4,1 ]  ### CHANGED
yticks_log2 = [1/8, 1/4,1/2 ]  ### CHANGED
leg_loc1 = (0.02, 0.39)
leg_loc2 = (0.38, 0.01)

print_ablation('Uncorr. Data 10 dim', results, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Optimizer}$", 0.1,leg_loc1, False,
                 "prune_synth_10_optimizer", True, False, slopes)

print_ablation('Uncorr. Data 10 dim', results_no, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Optimizer}$", 0.1,leg_loc2, False,
                 "nonprune_synth_10_optimizer", True, True, slopes_no)

_____no_output_____#Base - MLPs

sys.path.append('../')
import experiments

range_len = 7
sizes = 5
curves = 4
curve_labels = ['Momentum', 'SGD', 'Adam', 'RMSprop']
curve_colors = ['purple', 'peru','peru','peru']
curve_styles = ['-', '-', '--', ':']
marker_styles = ['v','v',  'v','v']
size_factors = np.array([0.25, 0.5, 1, 2, 4])
size_idx_map = {0.25: 0, 0.5: 1, 1: 2, 2: 3, 4: 4}
size_labels = ['1/4x', '1/2x', '1x', '2x', '4x']
knockout_range = np.linspace(0.0, 1.0, num=range_len)
crosses = 3
factors = [1, 2, 4, 8, 16]
#select_lr = [1e-1, 1e-2, 1e-3, 1e-3]

seeds = 3

#inits_fact = [1e-2, 1, 1e2, 1e4, 1e6]  ### SC

results = np.zeros((seeds, crosses, curves, sizes))
results_no = np.zeros((seeds, crosses, curves, sizes))

# both are indexed [nn_type][size_factor]
for seed in range(seeds):
    for curve_idx in range(curves):

        optimizer_idx = 0
        data_idx = data_idx_map[1e3]
        if curve_idx == 0:
            dataset_idx = dataset_idx_map["rand10000"]
            optimizer_idx = optimizer_idx_map[0]
        elif curve_idx == 1:
            dataset_idx = dataset_idx_map["rand10000"]
            optimizer_idx = optimizer_idx_map[1]
        elif  curve_idx == 2:
            dataset_idx = dataset_idx_map["rand10000"]
            optimizer_idx = optimizer_idx_map[2]
        elif  curve_idx == 3:
            dataset_idx = dataset_idx_map["rand10000"]
            optimizer_idx = optimizer_idx_map[5]

        for size_idx in range(sizes):
            id_best = int(IDs[1][seed, dataset_idx, data_idx, optimizer_idx, size_idx])
            opt = experiments.opt[id_best]

            ablation_results = set_results(id_best, experiments, \
                        size_idx, knockout_range)
            update_results(ablation_results, 16, curve_idx, size_idx, opt.dnn.neuron_multiplier[0])

results = get_mean_std(results)
results_no = get_mean_std(results_no, False)

slopes = get_slope(results)
slopes_no = get_slope(results_no)
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:124: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:125: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
yticks = [0, 20, 40, 60, 80, 100]  ### CHANGED
yticks_log = [1/256, 1/64,1/16, 1/4, 1]  ### CHANGED
yticks_log2 = [1/8, 1/4,1/2 ]  ### CHANGED
leg_loc1 = (0.02, 0.39)
leg_loc2 = (0.38, 0.01)

print_ablation('Uncorr. Data 10K dim', results, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Optimizer}$", 0.1,leg_loc1, False,
                 "prune_synth_10k_optimizer", True, False, slopes)

print_ablation('Uncorr. Data 10K dim', results_no, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Optimizer}$", 0.1,leg_loc2, False,
                 "nonprune_synth_10k_optimizer", True, True, slopes_no)

_____no_output_____#Init - MLPs

sys.path.append('../')
import experiments

range_len = 7
sizes = 5
curve_labels = ['1e-4', '1e-2', '1', '1e2', '1e4']
cm = seaborn.light_palette("purple", as_cmap=True)
curve_colors = [cm(i) for i in np.linspace(0.0, 1.0, num=5)]
curve_styles = ['-', '-', '-', '-', '-']
marker_styles = ['o', 'o', 'o', 'o',  'o']
inits_fact = [1e-2, 1, 1e2, 1e4, 1e6]
curves = 5

yticks = [0, 0.2, 0.4, 0.6, 0.8, 1]
size_factors = np.array([0.25, 0.5, 1, 2, 4])
size_idx_map = {0.25: 0, 0.5: 1, 1: 2, 2: 3, 4: 4}
size_labels = ['1/4x', '1/2x', '1x', '2x', '4x']
knockout_range = np.linspace(0.0, 1.0, num=range_len)
crosses = 3
lr = [1, 1e-1, 1e-2, 1e-3, 1e-4]
factors = [1, 2, 4, 8, 16]
select_lr = [1e-1, 1e-2, 1e-3, 1e-3]

seeds = 3

inits_fact = [1e-2, 1, 1e2, 1e4, 1e6]  ### SC

results = np.zeros((seeds, crosses, curves, sizes))
results_no = np.zeros((seeds, crosses, curves, sizes))

# both are indexed [nn_type][size_factor]
for seed in range(seeds):
    for curve_idx in range(curves):

        optimizer_idx = 0
        dataset_idx = dataset_idx_map["rand10000"]
        data_idx = data_idx_map[1e3]
        init_idx = init_idx_map[inits_fact[curve_idx]]

        for size_idx in range(sizes):
            id_best = int(IDs[0][seed, dataset_idx, data_idx, optimizer_idx, init_idx, size_idx])
            opt = experiments.opt[id_best]

            ablation_results = set_results(id_best, experiments, \
                        size_idx, knockout_range)
            update_results(ablation_results, 16, curve_idx, size_idx, opt.dnn.neuron_multiplier[0])

results = get_mean_std(results)
results_no = get_mean_std(results_no, False)

slopes = get_slope(results)
slopes_no = get_slope(results_no)
        /usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:124: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:125: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
yticks = [0, 20, 40, 60, 80, 100]  ### CHANGED
yticks_log = [1/64,1/16, 1/4,1 ]  ### CHANGED
yticks_log2 = [0, 1/8, 1/4,1/2 ]  ### CHANGED
leg_loc1 = (0.02, 0.39)
leg_loc2 = (0.38, 0.01)

print_ablation('Uncorr. Data 10K dim', results, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Initializer}$", 0.1,leg_loc1, False,
                 "prune_synth_10k_initializer", True, False, slopes)

print_ablation('Uncorr. Data 10K dim', results_no, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Initializer}$", 0.1,leg_loc2, False,
                 "nonprune_synth_10k_initializer", True, True, slopes_no)

_____no_output_____#Init - MLPs

sys.path.append('../')
import experiments

range_len = 7
sizes = 5
curve_labels = ['1e-4', '1e-2', '1', '1e2', '1e4']
cm = seaborn.light_palette("orange", as_cmap=True)
curve_colors = [cm(i) for i in np.linspace(0.0, 1.0, num=5)]
curve_styles = ['-', '-', '-', '-', '-']
marker_styles = ['o', 'o', 'o', 'o',  'o']
inits_fact = [1e-2, 1, 1e2, 1e4, 1e6]
curves = 5

yticks = [0, 0.2, 0.4, 0.6, 0.8, 1]
size_factors = np.array([0.25, 0.5, 1, 2, 4])
size_idx_map = {0.25: 0, 0.5: 1, 1: 2, 2: 3, 4: 4}
size_labels = ['1/4x', '1/2x', '1x', '2x', '4x']
knockout_range = np.linspace(0.0, 1.0, num=range_len)
crosses = 3
lr = [1, 1e-1, 1e-2, 1e-3, 1e-4]
factors = [1, 2, 4, 8, 16]
select_lr = [1e-1, 1e-2, 1e-3, 1e-3]

seeds = 3

inits_fact = [1e-2, 1, 1e2, 1e4, 1e6]  ### SC

results = np.zeros((seeds, crosses, curves, sizes))
results_no = np.zeros((seeds, crosses, curves, sizes))

# both are indexed [nn_type][size_factor]
for seed in range(seeds):
    for curve_idx in range(curves):

        optimizer_idx = 0
        dataset_idx = dataset_idx_map["rand10"]
        data_idx = data_idx_map[1e3]
        init_idx = init_idx_map[inits_fact[curve_idx]]

        for size_idx in range(sizes):
            id_best = int(IDs[0][seed, dataset_idx, data_idx, optimizer_idx, init_idx, size_idx])
            opt = experiments.opt[id_best]

            ablation_results = set_results(id_best, experiments, \
                        size_idx, knockout_range)
            update_results(ablation_results, 16, curve_idx, size_idx, opt.dnn.neuron_multiplier[0])

results = get_mean_std(results)
results_no = get_mean_std(results_no, False)

slopes = get_slope(results)
slopes_no = get_slope(results_no)/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:124: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:125: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
yticks = [0, 20, 40, 60, 80, 100]  ### CHANGED
yticks_log = [1/64,1/16, 1/4,1 ]  ### CHANGED
yticks_log2 = [0, 1/8, 1/4,1/2 ]  ### CHANGED
leg_loc1 = (0.02, 0.39)
leg_loc2 = (0.38, 0.01)

print_ablation('Uncorr. Data 10 dim', results, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Initializer}$", 0.1,leg_loc1, False,
                 "prune_synth_10_initializer", True, False, slopes)

print_ablation('Uncorr. Data 10 dim', results_no, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Initializer}$", 0.1,leg_loc2, False,
                 "nonprune_synth_10_initializer", True, True, slopes_no)

_____no_output_____#Init - MLPs

sys.path.append('../')
import experiments

range_len = 7
sizes = 5
curve_labels = ['10dim - 1e-2', '10dim - 1', '10Kdim - 1e-2', '10Kdim - 1']
cm_orange = seaborn.light_palette("orange", as_cmap=True)
cm_purple = seaborn.light_palette("purple", as_cmap=True)

curve_colors = [cm_orange(0.45), cm_orange(0.75),cm_purple(0.45), cm_purple(0.75)]
curve_styles = ['-', '-',  '-', '-']
marker_styles = ['o', 'o',  'o',  'o']

curves = 4

yticks = [0, 0.2, 0.4, 0.6, 0.8, 1]
size_factors = np.array([0.25, 0.5, 1, 2, 4])
size_idx_map = {0.25: 0, 0.5: 1, 1: 2, 2: 3, 4: 4}
size_labels = ['1/4x', '1/2x', '1x', '2x', '4x']
knockout_range = np.linspace(0.0, 1.0, num=range_len)
crosses = 3
factors = [1, 2, 4, 8, 16]

seeds = 3

results = np.zeros((seeds, crosses, curves, sizes))
results_no = np.zeros((seeds, crosses, curves, sizes))

# both are indexed [nn_type][size_factor]
for seed in range(seeds):
    for curve_idx in range(curves):

        optimizer_idx = 0
        if curve_idx < 2:
            dataset_idx = dataset_idx_map["rand10"]
        else:
            dataset_idx = dataset_idx_map["rand10000"]

        data_idx = data_idx_map[1e3]
        if curve_idx == 0 or curve_idx == 2:
            init_idx = init_idx_map[1e-2]
        else:
            init_idx = init_idx_map[1e0]

        for size_idx in range(sizes):
            id_best = int(IDs[0][seed, dataset_idx, data_idx, optimizer_idx, init_idx, size_idx])
            opt = experiments.opt[id_best]

            ablation_results = set_results(id_best, experiments, \
                        size_idx, knockout_range)
            update_results(ablation_results, 16, curve_idx, size_idx, opt.dnn.neuron_multiplier[0])

results = get_mean_std(results)
results_no = get_mean_std(results_no, False)

slopes = get_slope(results)
slopes_no = get_slope(results_no)/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:124: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
/usr/local/lib/python3.6/dist-packages/ipykernel_launcher.py:125: UserWarning: Boolean Series key will be reindexed to match DataFrame index.
yticks = [0, 20, 40, 60, 80, 100]  ### CHANGED
yticks_log = [1/64,1/16, 1/4,1 ]  ### CHANGED
yticks_log2 = [0, 1/8, 1/4,1/2 ]  ### CHANGED

leg_loc = (0.1, -.85)

print_ablation('Uncorr. Data', results, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Dataset-Init.}$", 0.1,leg_loc, False,
                 "prune_synth_summary", True, False, slopes, include_xlabel=False)

print_ablation('Uncorr. Data', results_no, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Initializer}$", 0.1,leg_loc, False,
                 "nonprune_synth_summary", True, True, slopes_no, include_xlabel=False)

_____no_output_____#Batch - ImageNet

sys.path.append('../')
sys.path.append('../ImageNet/')
from ImageNet.experiments import experiments

opts = experiments.get_experiments('','')

range_len = 7
sizes = 5
levels = np.array(np.linspace(0.0, 1.0, num=range_len))
knockout_range = np.linspace(0.0, 1.0, num=range_len)
level_labels = ["{:.0f}".format(a*100) for a in levels]

curves = 4
curve_labels = ['max.',  '4096', '2048', '1024']
curve_colors = ['indianred', 'salmon', 'salmon', 'salmon']
curve_styles = ['-', '-', '--', '--']
marker_styles = ['o', 'v', 's', 'D']
sizes = 5
size_factors = np.array([0.25, 0.5, 1, 2, 4])
size_idx_map = {0.25: 0, 0.5: 1, 1: 2, 2: 3, 4: 4}
size_labels = ['1/4x', '1/2x', '1x', '2x', '4x']
crosses = 1

ablation = np.zeros((curves, sizes, 2))
ablation_test = np.zeros((curves, sizes, 2))

for ID in range(5):
    #print('Processing ID:', ID)
    opt = opts[ID]
    csv_file = opt.csv_dir + opt.name + '_robustness.csv'
    df = pd.read_csv(csv_file)

    size_idx = size_idx_map[opt.dnn.factor]

    rand_ablation_results = np.zeros((range_len))
    for amount in range(range_len):
        rand_ablation_results[amount] = \
            np.array(df[df['evaluation_set'] == 'validation'][df['perturbation_layer'] == 'all'] \
                [df['perturbation_type'] == 2] \
                    [df['perturbation_amount'] == knockout_range[amount]]['unchanged labels'].iloc[0])

    # ablation[0, size_idx, 0] = np.mean(rand_ablation_results)
    ablation[0, size_idx, 0] = get_threshold(np.expand_dims(rand_ablation_results, 0)) * (1/4) * opt.dnn.factor
    ablation_test[0, size_idx, 0] = 1/2**(4-size_idx) - ablation[0, size_idx, 0]

batch_plot = [4096, 2048, 1024]
for ID in range(13):

    #print('Processing ID:', ID)
    opt = opts[ID]
    csv_file = opt.csv_dir + opt.name + '_robustness.csv'
    df = pd.read_csv(csv_file)

    size_idx = size_idx_map[opt.dnn.factor]
    batch_idx = np.where(np.asarray(batch_plot) == opt.hyper.batch_size)[0]
    if len(batch_idx):
        rand_ablation_results = np.zeros((range_len))
        for amount in range(range_len):
            rand_ablation_results[amount] = \
                np.array(df[df['evaluation_set'] == 'validation'][df['perturbation_layer'] == 'all'] \
                    [df['perturbation_type'] == 2] \
                        [df['perturbation_amount'] == knockout_range[amount]]['unchanged labels'].iloc[0])

        ablation[int(batch_idx)+1, size_idx, 0] = get_threshold(np.expand_dims(rand_ablation_results, 0)) * (1/4) * opt.dnn.factor
        ablation_test[int(batch_idx)+1, size_idx, 0] = 1/2**(4-size_idx) - ablation[int(batch_idx)+1, size_idx, 0]

slopes = get_slope(ablation)
slopes_no = get_slope(ablation_test)

OPTS LOOKUP:
ID: 0, resnet, factor: 0.25, batch_size:8192
ID: 1, resnet, factor: 0.5, batch_size:4096
ID: 2, resnet, factor: 1, batch_size:3072
ID: 3, resnet, factor: 2, batch_size:1024
ID: 4, resnet, factor: 4, batch_size:512
ID: 5, resnet, factor: 0.25, batch_size:4096
ID: 6, resnet, factor: 0.25, batch_size:2048
ID: 7, resnet, factor: 0.5, batch_size:2048
ID: 8, resnet, factor: 1, batch_size:2048
ID: 9, resnet, factor: 0.25, batch_size:1024
ID: 10, resnet, factor: 0.5, batch_size:1024
ID: 11, resnet, factor: 1, batch_size:1024
ID: 12, inception, factor: 1, batch_size:512
ID: 13, inception, factor: 0.25, batch_size:512
ID: 14, inception, factor: 0.5, batch_size:512
ID: 15, inception, factor: 1, batch_size:512
ID: 16, inception, factor: 1, batch_size:512
ID: 17, inception, factor: 1, batch_size:512
ID: 18, inception, factor: 1, batch_size:512
ID: 19, resnet, factor: 0.25, batch_size:8192
ID: 20, resnet, factor: 0.5, batch_size:4096
ID: 21, resnet, factor: 1, batch_size:3072
ID: 22, resnet, factor: 2, batch_size:1024
ID: 23, resnet, factor: 4, batch_size:512
ID: 24, resnet, factor: 0.25, batch_size:8192
ID: 25, resnet, factor: 0.5, batch_size:4096
ID: 26, resnet, factor: 1, batch_size:3072
ID: 27, resnet, factor: 2, batch_size:1024
ID: 28, resnet, factor: 4, batch_size:512
ID: 29, resnet, factor: 0.25, batch_size:8192
ID: 30, resnet, factor: 0.5, batch_size:4096
ID: 31, resnet, factor: 1, batch_size:3072
ID: 32, resnet, factor: 2, batch_size:1024
ID: 33, resnet, factor: 4, batch_size:512
ID: 34, resnet, factor: 0.25, batch_size:8192
ID: 35, resnet, factor: 0.5, batch_size:4096
ID: 36, resnet, factor: 1, batch_size:3072
ID: 37, resnet, factor: 2, batch_size:1024
ID: 38, resnet, factor: 4, batch_size:512
leg_loc = (0.0, 0.42)

yticks = [0, 20, 40, 60]  ### CHANGED
yticks_log = [ 1/512, 1/128,  1/32, 1/8, 1/2, 1]  ### CHANGED
yticks_log2 = [0,  1/4, 1/2, 1]  ### CHANGED

print_ablation('ImageNet', ablation, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Batch} \bf{Size}$", 0.1,leg_loc, False,
                 "prune_imagenet_batch", True, False, slopes)

print_ablation('ImageNet', ablation_test, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Batch} \bf{Size}$", 0.1,leg_loc, False,
                 "nonprune_imagenet_batch", True, True, slopes_no)

slopes

# yticks = [0, 0.05, 0.1, 0.15, 0.2, 0.25]
# print_auc(r'ImageNet - Validation', ablation-targeted_ablation, marker_styles,
#                curve_colors, curve_styles, curve_labels, yticks, r"$\bf{Batch} \bf{Size}$", 0.1,leg_loc, False,
#                  "target_imagenet_batch_test")

_____no_output_____#ImageNet

sys.path.append('../')
sys.path.append('../ImageNet/')
from ImageNet.experiments import experiments

opts = experiments.get_experiments('','')
range_len = 7

sizes = 5
levels = np.array(np.linspace(0.0, 1.0, num=range_len))
knockout_range = np.linspace(0.0, 1.0, num=range_len)
level_labels = ["{:.0f}".format(a*100) for a in levels]

range_len = 7
curves = 3
curve_labels = ['ResNet18',  'Inceptionv3', 'Inceptionv3-layer']
curve_colors = ['indianred', 'green', 'lightgreen']
curve_styles = ['-', '-', ':']
marker_styles = ['o', 'v', 's']
sizes = 5
size_factors = np.array([0.25, 0.5, 1, 2, 4])
size_idx_map = {0.25: 0, 0.5: 1, 1: 2, 2: 3, 4: 4}
size_labels = ['1/4x', '1/2x', '1x', '2x', '4x']
crosses = 1

ablation = np.zeros((curves, sizes, 2))
targeted_ablation = np.zeros((curves, sizes, 2))
for ID in range(5):
    #print('Processing ID:', ID)
    opt = opts[ID]
    csv_file = opt.csv_dir + opt.name + '_robustness.csv'
    df = pd.read_csv(csv_file)

    size_idx = size_idx_map[opt.dnn.factor]

    rand_ablation_results = np.zeros((range_len))
    targeted_ablation_results = np.zeros((range_len))
    for amount in range(range_len):
        rand_ablation_results[amount] = \
            np.array(df[df['evaluation_set'] == 'validation'][df['perturbation_layer'] == 'all'] \
                [df['perturbation_type'] == 2] \
                    [df['perturbation_amount'] == knockout_range[amount]]['unchanged labels'].iloc[0])

    ablation[0, size_idx, 0] = get_threshold(np.expand_dims(rand_ablation_results, 0)) * (1/4) * opt.dnn.factor
    targeted_ablation[0, size_idx, 0] = 1/2**(4-size_idx) -  ablation[0, size_idx, 0]

for ID in range(12,15):
    #print('Processing ID:', ID)
    opt = opts[ID]
    csv_file = opt.csv_dir + opt.name + '_robustness.csv'
    df = pd.read_csv(csv_file)

    size_idx = size_idx_map[opt.dnn.factor]

    rand_ablation_results = np.zeros((range_len))
    targeted_ablation_results = np.zeros((range_len))
    for amount in range(range_len):
        rand_ablation_results[amount] = \
            np.array(df[df['evaluation_set'] == 'validation'][df['perturbation_layer'] == '13'] \
                [df['perturbation_type'] == 2] \
                    [df['perturbation_amount'] == knockout_range[amount]]['unchanged labels'].iloc[0])

    ablation[1, size_idx, 0] = get_threshold(np.expand_dims(rand_ablation_results, 0)) * (1/4) * opt.dnn.factor
    targeted_ablation[1, size_idx, 0] = 1/2**(4-size_idx) -  ablation[1, size_idx, 0]

for ID in list(range(12,13)) + list(range(15,19)):
    #print('Processing ID:', ID)
    opt = opts[ID]
    csv_file = opt.csv_dir + opt.name + '_robustness.csv'
    df = pd.read_csv(csv_file)

    size_idx = size_idx_map[opt.dnn.factor_end]

    rand_ablation_results = np.zeros((range_len))
    targeted_ablation_results = np.zeros((range_len))
    for amount in range(range_len):
        rand_ablation_results[amount] = \
            np.array(df[df['evaluation_set'] == 'validation'][df['perturbation_layer'] == '7'] \
                [df['perturbation_type'] == 2] \
                    [df['perturbation_amount'] == knockout_range[amount]]['unchanged labels'].iloc[0])

    factor_map = {12: 1, 15: 0.25, 16: 0.5, 17: 2, 18: 4}
    ablation[2, size_idx, 0] = get_threshold(np.expand_dims(rand_ablation_results, 0)) * (1/4) * factor_map[ID]
    targeted_ablation[2, size_idx, 0] = 1/2**(4-size_idx) - ablation[2, size_idx, 0]

slopes = get_slope(ablation)
slopes_no = get_slope(targeted_ablation)
OPTS LOOKUP:
ID: 0, resnet, factor: 0.25, batch_size:8192
ID: 1, resnet, factor: 0.5, batch_size:4096
ID: 2, resnet, factor: 1, batch_size:3072
ID: 3, resnet, factor: 2, batch_size:1024
ID: 4, resnet, factor: 4, batch_size:512
ID: 5, resnet, factor: 0.25, batch_size:4096
ID: 6, resnet, factor: 0.25, batch_size:2048
ID: 7, resnet, factor: 0.5, batch_size:2048
ID: 8, resnet, factor: 1, batch_size:2048
ID: 9, resnet, factor: 0.25, batch_size:1024
ID: 10, resnet, factor: 0.5, batch_size:1024
ID: 11, resnet, factor: 1, batch_size:1024
ID: 12, inception, factor: 1, batch_size:512
ID: 13, inception, factor: 0.25, batch_size:512
ID: 14, inception, factor: 0.5, batch_size:512
ID: 15, inception, factor: 1, batch_size:512
ID: 16, inception, factor: 1, batch_size:512
ID: 17, inception, factor: 1, batch_size:512
ID: 18, inception, factor: 1, batch_size:512
ID: 19, resnet, factor: 0.25, batch_size:8192
ID: 20, resnet, factor: 0.5, batch_size:4096
ID: 21, resnet, factor: 1, batch_size:3072
ID: 22, resnet, factor: 2, batch_size:1024
ID: 23, resnet, factor: 4, batch_size:512
ID: 24, resnet, factor: 0.25, batch_size:8192
ID: 25, resnet, factor: 0.5, batch_size:4096
ID: 26, resnet, factor: 1, batch_size:3072
ID: 27, resnet, factor: 2, batch_size:1024
ID: 28, resnet, factor: 4, batch_size:512
ID: 29, resnet, factor: 0.25, batch_size:8192
ID: 30, resnet, factor: 0.5, batch_size:4096
ID: 31, resnet, factor: 1, batch_size:3072
ID: 32, resnet, factor: 2, batch_size:1024
ID: 33, resnet, factor: 4, batch_size:512
ID: 34, resnet, factor: 0.25, batch_size:8192
ID: 35, resnet, factor: 0.5, batch_size:4096
ID: 36, resnet, factor: 1, batch_size:3072
ID: 37, resnet, factor: 2, batch_size:1024
ID: 38, resnet, factor: 4, batch_size:512
leg_loc = (0.05, 1.3)

yticks = [0, 20, 40, 60]  ### CHANGED
yticks_log = [ 1/512, 1/128,  1/32, 1/8, 1/2, 1]  ### CHANGED

yticks_log2 = [0, 1/4, 1/2, 1]  ### CHANGED

print_ablation('ImageNet', ablation, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Network}$", 0.1,leg_loc, False,
                 "prune_imagenet", True, False, slopes)

leg_loc = (0.33, 0.01)
print_ablation('ImageNet', targeted_ablation, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Network}$", 0.1,leg_loc, False,
                 "nonprune_imagenet", True, True, slopes_no)
slopes_____no_output_____#Layersc=cImageNet

sys.path.append('../')
sys.path.append('../ImageNet/')
from ImageNet.experiments import experiments

opts = experiments.get_experiments('','')
yticks = [0, 0.2, 0.4, 0.6, 0.8, 1]
range_len = 7

sizes = 5
levels = np.array(np.linspace(0.0, 1.0, num=range_len))
knockout_range = np.linspace(0.0, 1.0, num=range_len)
level_labels = ["{:.0f}".format(a*100) for a in levels]

range_len = 7
curves = 5
curve_labels = ['conv',  'blk.1', 'blk.2', 'blk.3','blk.4']
cm = seaborn.light_palette("indianred", as_cmap=True)
curve_colors = [cm(i) for i in np.linspace(0.0, 1.0, num=curves+1)][1:]
curve_styles = ['-', '-', '-', '-', '-']
marker_styles = ['o', 'o', 'o', 'o', 'o']

sizes = 5
size_factors = np.array([0.25, 0.5, 1, 2, 4])
size_idx_map = {0.25: 0, 0.5: 1, 1: 2, 2: 3, 4: 4}
size_labels = ['1/4x', '1/2x', '1x', '2x', '4x']
crosses = 3

ablation = np.zeros((curves, sizes, 2))
ablation_test = np.zeros((curves, sizes, 2))

for ID in range(5):
    for idx_layer, layer in enumerate([0,1,2,3,4]):

        #print('Processing ID:', ID)
        opt = opts[ID]
        csv_file = opt.csv_dir + opt.name + '_robustness.csv'
        df = pd.read_csv(csv_file)

        size_idx = size_idx_map[opt.dnn.factor]

        rand_ablation_results = np.zeros((range_len))
        targeted_ablation_results = np.zeros(( range_len))
        for amount in range(range_len):
            rand_ablation_results[amount] = \
                np.array(df[df['cross'] == 0][df['evaluation_set'] == 'validation'][df['perturbation_layer'] == str(layer)] \
                    [df['perturbation_type'] == 2] \
                        [df['perturbation_amount'] == knockout_range[amount]]['unchanged labels'].iloc[0])

        ablation[idx_layer, size_idx, 0] = get_threshold(np.expand_dims(rand_ablation_results, 0)) * (1/4) * opt.dnn.factor
        ablation_test[idx_layer, size_idx, 0] = 1/2**(4-size_idx) - ablation[idx_layer, size_idx, 0]

slopes = get_slope(ablation)
slopes_no = get_slope(ablation_test)

leg_loc = (0.01, 0.01)

yticks = [0, 20, 40, 60]  ### CHANGED
yticks_log = [ 1/512, 1/128,  1/32, 1/8, 1/2, 1]  ### CHANGED

yticks_log2 = [0, 1/4, 1/2, 1]  ### CHANGED

print_ablation('ImageNet-ResNet18', ablation, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"", 0.1,leg_loc, True,
                 "prune_imagenet_resnet_all_layer", True, False, slopes)

print_ablation('ImageNet - ResNet18', ablation_test, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"", 0.1,leg_loc, True,
                 "nonprune_imagenet_resnet_all_layer", True, True, slopes_no, ncol=1)

curves = 3
curve_labels = ['7',  '12', '13']
cm = seaborn.light_palette("darkgreen", as_cmap=True)
curve_colors = [cm(i) for i in np.linspace(0.0, 1.0, num=4)][1:]
curve_styles = ['-', '-', '-']
marker_styles = ['v', 'v', 'v']

ablation = np.zeros((curves, sizes, 2))
ablation_test = np.zeros((curves, sizes, 2))
for ID in range(12,15):
    for idx_layer, layer in enumerate([7, 13, 15]):
        #print('Processing ID:', ID)
        opt = opts[ID]
        csv_file = opt.csv_dir + opt.name + '_robustness.csv'
        df = pd.read_csv(csv_file)

        size_idx = size_idx_map[opt.dnn.factor]

        rand_ablation_results = np.zeros((range_len))
        for amount in range(range_len):
            rand_ablation_results[amount] = \
                np.array(df[df['cross'] == 0][df['evaluation_set'] == 'validation'][df['perturbation_layer'] == str(layer)] \
                    [df['perturbation_type'] == 2] \
                        [df['perturbation_amount'] == knockout_range[amount]]['unchanged labels'].iloc[0])

        ablation[idx_layer, size_idx, 0] = get_threshold(np.expand_dims(rand_ablation_results, 0)) * (1/4) * opt.dnn.factor
        ablation_test[idx_layer, size_idx, 0] = 1/2**(4-size_idx) - ablation[idx_layer, size_idx, 0]

slopes = get_slope(ablation)
slopes_no = get_slope(ablation_test)

leg_loc = (0.7, 0.57)
yticks_log = [ 1/512, 1/128,  1/32, 1/8, 1/2, 1]  ### CHANGED

yticks_log2 = [0, 1/8, 1/4, 1/2]  ### CHANGED
print_ablation('ImageNet - Inception-v3', ablation, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Layer}$", 0.1,leg_loc, True,
                 "prune_imagenet_inception_all_layer", True, False, slopes)

leg_loc = (0.69, 0.02)
print_ablation('ImageNet - Inception-v3', ablation_test, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Layer}$", 0.1,leg_loc, True,
                 "nonprune_imagenet_inception_all_layer", True, True, slopes_no)

curves = 3
curve_labels = ['7',  '12', '13']
cm = seaborn.light_palette("green", as_cmap=True)
curve_colors = [cm(i) for i in np.linspace(0.0, 1.0, num=4)][1:]
curve_styles = ['--', '--', '--']
marker_styles = ['s', 's', 's']
ablation = np.zeros((curves, sizes, 2))
targeted_ablation = np.zeros((curves, sizes, 2))
for ID in list(range(12,13)) + list(range(15,19)):
    for idx_layer, layer in enumerate([7, 13, 15]):
        #print('Processing ID:', ID)
        opt = opts[ID]
        csv_file = opt.csv_dir + opt.name + '_robustness.csv'
        df = pd.read_csv(csv_file)

        size_idx = size_idx_map[opt.dnn.factor_end]

        rand_ablation_results = np.zeros((range_len))
        for amount in range(range_len):
            rand_ablation_results[amount] = \
                np.array(df[df['cross'] == 0][df['evaluation_set'] == 'validation'][df['perturbation_layer'] == str(layer)] \
                    [df['perturbation_type'] == 2] \
                        [df['perturbation_amount'] == knockout_range[amount]]['unchanged labels'].iloc[0])

        if layer == 7:
            ablation[idx_layer, size_idx, 0] = get_threshold(np.expand_dims(rand_ablation_results, 0)) * (1/4) * factor_map[ID]
            ablation_test[idx_layer, size_idx, 0] = 1/2**(4-size_idx) - ablation[idx_layer, size_idx, 0]
        else:
            ablation[idx_layer, size_idx, 0] = get_threshold(np.expand_dims(rand_ablation_results, 0))
            ablation_test[idx_layer, size_idx, 0] = 1 - ablation[idx_layer, size_idx, 0]

slopes = get_slope(ablation)
slopes_no = get_slope(ablation_test)

leg_loc = (0.02, 0.28)
yticks_log = [ 1/512, 1/128,  1/32, 1/8, 1/2, 1]  ### CHANGED

yticks_log2 = [0, 1/4, 1/2,1]  ### CHANGED
print_ablation('Inception-v3-layer', ablation, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Layer}$", 0.1,leg_loc, True,
                 "prune_imagenet_inception_layer", True, False, slopes)

leg_loc = (0.02, 0.02)
print_ablation('Inception-v3-layer', ablation_test, marker_styles,
               curve_colors, curve_styles, curve_labels, yticks_log, r"$\bf{Layer}$", 0.1,leg_loc, True,
                 "nonprune_imagenet_inception_layer", True, True, slopes_no)
OPTS LOOKUP:
ID: 0, resnet, factor: 0.25, batch_size:8192
ID: 1, resnet, factor: 0.5, batch_size:4096
ID: 2, resnet, factor: 1, batch_size:3072
ID: 3, resnet, factor: 2, batch_size:1024
ID: 4, resnet, factor: 4, batch_size:512
ID: 5, resnet, factor: 0.25, batch_size:4096
ID: 6, resnet, factor: 0.25, batch_size:2048
ID: 7, resnet, factor: 0.5, batch_size:2048
ID: 8, resnet, factor: 1, batch_size:2048
ID: 9, resnet, factor: 0.25, batch_size:1024
ID: 10, resnet, factor: 0.5, batch_size:1024
ID: 11, resnet, factor: 1, batch_size:1024
ID: 12, inception, factor: 1, batch_size:512
ID: 13, inception, factor: 0.25, batch_size:512
ID: 14, inception, factor: 0.5, batch_size:512
ID: 15, inception, factor: 1, batch_size:512
ID: 16, inception, factor: 1, batch_size:512
ID: 17, inception, factor: 1, batch_size:512
ID: 18, inception, factor: 1, batch_size:512
ID: 19, resnet, factor: 0.25, batch_size:8192
ID: 20, resnet, factor: 0.5, batch_size:4096
ID: 21, resnet, factor: 1, batch_size:3072
ID: 22, resnet, factor: 2, batch_size:1024
ID: 23, resnet, factor: 4, batch_size:512
ID: 24, resnet, factor: 0.25, batch_size:8192
ID: 25, resnet, factor: 0.5, batch_size:4096
ID: 26, resnet, factor: 1, batch_size:3072
ID: 27, resnet, factor: 2, batch_size:1024
ID: 28, resnet, factor: 4, batch_size:512
ID: 29, resnet, factor: 0.25, batch_size:8192
ID: 30, resnet, factor: 0.5, batch_size:4096
ID: 31, resnet, factor: 1, batch_size:3072
ID: 32, resnet, factor: 2, batch_size:1024
ID: 33, resnet, factor: 4, batch_size:512
ID: 34, resnet, factor: 0.25, batch_size:8192
ID: 35, resnet, factor: 0.5, batch_size:4096
ID: 36, resnet, factor: 1, batch_size:3072
ID: 37, resnet, factor: 2, batch_size:1024
ID: 38, resnet, factor: 4, batch_size:512
#Time - ImageNet

sys.path.append('../')
sys.path.append('../ImageNet/')
from ImageNet.experiments import experiments

opts = experiments.get_experiments('','')
range_len = 7
curves = 5
curve_labels = ['x1/4',  'x1/2', 'x1', 'x2', 'x4']
cm = seaborn.light_palette("indianred", as_cmap=True)
curve_colors = [cm(i) for i in np.linspace(0.0, 1.0, num=5)]
curve_styles = ['-', '-', '-', '-', '-']
marker_styles = ['o', 'o', 'o', 'o', 'o']
sizes = 5
size_factors = np.array([-4, -3, -2, -1, 0])
size_idx_map = {0.25: 0, 0.5: 1, 1: 2, 2: 3, 4: 4}
size_labels = ['-8', '-6', '-4', '-2', '0']
crosses=3

ablation = np.zeros((curves, sizes, 2))
targeted_ablation = np.zeros((curves, sizes, 2))

# both are indexed [nn_type][size_factor]

for ID in range(19, 39):

    #print('Processing ID:', ID)
    opt = opts[ID]

    curve_idx = 0  # unregularized

    size_idx = size_idx_map[opt.dnn.factor]

    csv_file = opt.csv_dir + opt.name + '_robustness.csv'
    df = pd.read_csv(csv_file)

    rand_ablation_results = np.zeros((range_len))
    targeted_ablation_results = np.zeros((range_len))
    for amount in range(range_len):
        rand_ablation_results[amount] = \
            np.array(df[df['cross'] == 0][df['evaluation_set'] == 'validation'][df['perturbation_layer'] == 'all'] \
                [df['perturbation_type'] == 2] \
                    [df['perturbation_amount'] == knockout_range[amount]]['unchanged labels'].iloc[0])

        targeted_ablation_results[amount] = \
            np.array(df[df['cross'] == 0][df['evaluation_set'] == 'validation'][df['perturbation_layer'] == 'all'] \
                [df['perturbation_type'] == 4] \
                    [df['perturbation_amount'] == knockout_range[amount]]['unchanged labels'].iloc[0])

    ablation[size_idx][opt.time_step][0] = get_threshold(np.expand_dims(rand_ablation_results, 0)) * (1/4) * opt.dnn.factor
    ablation[size_idx][opt.time_step][1] = 0
    targeted_ablation[size_idx][opt.time_step][0] = get_threshold(np.expand_dims(rand_ablation_results, 0)) * (1/4) * opt.dnn.factor
    targeted_ablation[size_idx][opt.time_step][1] = 0

for ID in range(5):

    #print('Processing ID:', ID)
    opt = opts[ID]

    curve_idx = 0  # unregularized

    size_idx = size_idx_map[opt.dnn.factor]

    csv_file = opt.csv_dir + opt.name + '_robustness.csv'
    df = pd.read_csv(csv_file)
    rand_ablation_results = np.zeros((range_len))
    targeted_ablation_results = np.zeros((range_len))
    for amount in range(range_len):
        rand_ablation_results[amount] = \
            np.array(df[df['cross'] == 0][df['evaluation_set'] == 'validation'][df['perturbation_layer'] == 'all'] \
                [df['perturbation_type'] == 2] \
                    [df['perturbation_amount'] == knockout_range[amount]]['unchanged labels'].iloc[0])

        targeted_ablation_results[amount] = \
            np.array(df[df['cross'] == 0][df['evaluation_set'] == 'validation'][df['perturbation_layer'] == 'all'] \
                [df['perturbation_type'] == 4] \
                    [df['perturbation_amount'] == knockout_range[amount]]['unchanged labels'].iloc[0])

    ablation[size_idx][-1][0] = get_threshold(np.expand_dims(rand_ablation_results, 0)) * (1/4) * opt.dnn.factor
    ablation[size_idx][-1][1] = 0
    targeted_ablation[size_idx][-1][0] = get_threshold(np.expand_dims(rand_ablation_results, 0)) * (1/4) * opt.dnn.factor
    targeted_ablation[size_idx][-1][1] = 0

data = ablation[:, :, 0]
stds = ablation[:, :, 1]
title = r'ImageNet - ResNet18'
leg_title = r"$\bf{Model} \bf{Size}$"
leg_flag = True
rand_base = 0.1

leg_loc = (0.7, 0.040)
yticks = [ 1/512, 1/128,  1/32, 1/8, 1/2, 1]  ### CHANGED

fig, ax = plt.subplots(figsize=(4, 3))
plt.title(title , fontsize=25)
plt.xlabel(r'Epochs before Completion', fontsize=25)
plt.ylabel('Prunability', fontsize=25)
plt.xlim((-4, 0))
plt.xticks(size_factors, size_labels)
plt.yticks(yticks)
ax.xaxis.tick_bottom() ### CHANGED
ax.yaxis.tick_left() ### CHANGED
plt.yscale('log', basey=2)
plt.ylim((np.min(yticks), np.max(yticks)))

for curve_idx in range(np.shape(data)[0]):
    non_0_idx = (data[curve_idx] != 0)
    plt.errorbar(size_factors[non_0_idx], data[curve_idx][non_0_idx], yerr=stds[curve_idx][non_0_idx], marker=marker_styles[curve_idx], color=curve_colors[curve_idx],
             linestyle=curve_styles[curve_idx], linewidth=4, markersize=10)

for item in ([ax.title, ax.xaxis.label, ax.yaxis.label] +
             ax.get_xticklabels() + ax.get_yticklabels()):
    item.set_fontsize(15)

if False:
    leg = plt.legend(curve_labels, loc=leg_loc, framealpha=0.8, fontsize=10, title=leg_title)
    leg.get_title().set_fontsize('12')
plt.rc('xtick',labelsize=8)

plt.show()
fig.savefig("prune_imagenet_dynamics.pdf",format='pdf', bbox_inches='tight',dpi=1000)
    OPTS LOOKUP:
ID: 0, resnet, factor: 0.25, batch_size:8192
ID: 1, resnet, factor: 0.5, batch_size:4096
ID: 2, resnet, factor: 1, batch_size:3072
ID: 3, resnet, factor: 2, batch_size:1024
ID: 4, resnet, factor: 4, batch_size:512
ID: 5, resnet, factor: 0.25, batch_size:4096
ID: 6, resnet, factor: 0.25, batch_size:2048
ID: 7, resnet, factor: 0.5, batch_size:2048
ID: 8, resnet, factor: 1, batch_size:2048
ID: 9, resnet, factor: 0.25, batch_size:1024
ID: 10, resnet, factor: 0.5, batch_size:1024
ID: 11, resnet, factor: 1, batch_size:1024
ID: 12, inception, factor: 1, batch_size:512
ID: 13, inception, factor: 0.25, batch_size:512
ID: 14, inception, factor: 0.5, batch_size:512
ID: 15, inception, factor: 1, batch_size:512
ID: 16, inception, factor: 1, batch_size:512
ID: 17, inception, factor: 1, batch_size:512
ID: 18, inception, factor: 1, batch_size:512
ID: 19, resnet, factor: 0.25, batch_size:8192
ID: 20, resnet, factor: 0.5, batch_size:4096
ID: 21, resnet, factor: 1, batch_size:3072
ID: 22, resnet, factor: 2, batch_size:1024
ID: 23, resnet, factor: 4, batch_size:512
ID: 24, resnet, factor: 0.25, batch_size:8192
ID: 25, resnet, factor: 0.5, batch_size:4096
ID: 26, resnet, factor: 1, batch_size:3072
ID: 27, resnet, factor: 2, batch_size:1024
ID: 28, resnet, factor: 4, batch_size:512
ID: 29, resnet, factor: 0.25, batch_size:8192
ID: 30, resnet, factor: 0.5, batch_size:4096
ID: 31, resnet, factor: 1, batch_size:3072
ID: 32, resnet, factor: 2, batch_size:1024
ID: 33, resnet, factor: 4, batch_size:512
ID: 34, resnet, factor: 0.25, batch_size:8192
ID: 35, resnet, factor: 0.5, batch_size:4096
ID: 36, resnet, factor: 1, batch_size:3072
ID: 37, resnet, factor: 2, batch_size:1024
ID: 38, resnet, factor: 4, batch_size:512

</code>


# Notebook from sebastiandres/workshop_unsa_ar
Path: 06_pandas/M2L03_data_manipulation.ipynb

# Manipulación de Datos_____no_output_____El principal objetivo de esta clase es introducir la biblioteca de análisis de datos __pandas__, la cual agrega mayor flexibilidad y opciones que Numpy, sin embargo el costo de esto se traduce en pérdida de rendimiento y eficiencia. Conocer los elemnos básicos pandas permite manejar desde pocos dados en un arhivo _excel_ hasta miles y millones de registros de una base de datos._____no_output_____## Introducción a pandas_____no_output_____Desde de la [página oficial](https://pandas.pydata.org/) Pandas se introduce de la siguiente forma:

_pandas is a fast, powerful, flexible and easy to use open source data analysis and manipulation tool,
built on top of the Python programming language._

Mientras que su [misión](https://pandas.pydata.org/about/):

_pandas aims to be the fundamental high-level building block for doing practical, real world data analysis in Python. Additionally, it has the broader goal of becoming the most powerful and flexible open source data analysis / manipulation tool available in any language.______no_output_____### Principales Características

* A fast and efficient __DataFrame__ object for data manipulation with integrated indexing;
* Tools for __reading and writing data__ between in-memory data structures and different formats: CSV and text files, Microsoft Excel, SQL databases, and the fast HDF5 format;
* Intelligent __data alignment__ and integrated handling of __missing data__: gain automatic label-based alignment in computations and easily manipulate messy data into an orderly form;
* Flexible __reshaping__ and pivoting of data sets;
* Intelligent label-based __slicing__, __fancy indexing__, and __subsetting__ of large data sets;
* Columns can be inserted and deleted from data structures for __size mutability__;
* Aggregating or transforming data with a powerful __group by__ engine allowing split-apply-combine operations on data sets;
* High performance __merging and joining__ of data sets;
* __Hierarchical axis indexing__ provides an intuitive way of working with high-dimensional data in a lower-dimensional data structure;
* __Time series__-functionality: date range generation and frequency conversion, moving window statistics, date shifting and lagging. Even create domain-specific time offsets and join time series without losing data;
* Highly __optimized for performance__, with critical code paths written in Cython or C.
* Python with pandas is in use in a wide variety of __academic and commercial__ domains, including Finance, Neuroscience, Economics, * Statistics, Advertising, Web Analytics, and more._____no_output_____

import pandas as pd_____no_output_____pd.__version_______no_output_____

## Series_____no_output_____Arreglos unidimensionales con etiquetas. Se puede pensar como una generalización de los diccionarios de Python._____no_output_____

# Descomenta y ejecuta para ver la documentación sobre pd.Series
# pd.Series?_____no_output_____

Para crear una instancia de una serie existen muchas opciones, las más comunes son:

* A partir de una lista.
* A partir de un _numpy.array_.
* A partir de un diccionario.
* A partir de un archivo (por ejemplo un csv)._____no_output_____

my_serie = pd.Series(range(3, 33, 3))
my_serie_____no_output_____type(my_serie)_____no_output_____# Presiona TAB y sorpréndete con la cantidad de métodos y atributos que poseen!
# my_serie._____no_output_____

Las series son arreglos unidemensionales que constan de _data_ e _index_._____no_output_____

# Data
my_serie.values_____no_output_____type(my_serie.values)_____no_output_____# Index
my_serie.index_____no_output_____type(my_serie.index)_____no_output_____

¿Te fijaste que el index es de otra clase?_____no_output_____A diferencia de Numpy, pandas ofrece más flexibilidad para los valores e índices._____no_output_____

my_serie_2 = pd.Series(range(3, 33, 3), index=list('abcdefghij'))
my_serie_2_____no_output_____

### Acceder a los valores de una_____no_output_____

my_serie_2['b']_____no_output_____my_serie_2.loc['b']_____no_output_____my_serie_2.iloc[1]_____no_output_____

```loc```?? ```iloc```?? ¿Qué es eso?_____no_output_____A modo de resumen:

* ```loc``` es un método que hace referencia a las etiquetas (*labels*) del objeto .
* ```iloc``` es un método que hace referencia posicional del objeto._____no_output_____**Consejo**: Si quieres editar valores siempre utiliza ```loc``` y/o ```iloc```._____no_output_____

my_serie_2.loc['d'] = 1000_____no_output_____my_serie_2_____no_output_____

¿Y si quiero escoger más de un valor?_____no_output_____

my_serie_2.loc["b":"e"]  # Incluso retorna el último valor!_____no_output_____my_serie_2.iloc[1:5]  # Incluso retorna el último valor!_____no_output_____

Sorpresa! También puedes filtrar según condiciones!_____no_output_____En la mayoría de los tutoriales en internet encontrarás algo como lo siguiente:_____no_output_____

my_serie_2[my_serie_2 % 2 == 0]_____no_output_____

Lo siguiente se conoce como _mask_, y se basa en el siguiente hecho:_____no_output_____

my_serie_2 % 2 == 0  # Retorna una serie con valores booleanos pero los mismos index!_____no_output_____

Si es una serie resultante de otra operación, tendrás que guardarla en una variable para así tener el nombre y luego acceder a ella. La siguiente manera puede qeu sea un poco más verboso, pero te otorga más flexibilidad._____no_output_____

my_serie_2.loc[lambda s: s % 2 == 0]_____no_output_____

Una función lambda es una función pequeña y anónima. Pueden tomar cualquer número de argumentos pero solo tienen una expresión._____no_output_____### Trabajar con fechas_____no_output_____Pandas incluso permite que los index sean fechas! Por ejemplo, a continuación se crea una serie con fechas y valores de temperatura._____no_output_____

temperature = pd.read_csv(
    "https://raw.githubusercontent.com/jbrownlee/Datasets/master/daily-min-temperatures.csv",
    index_col="Date",
    squeeze=True
)_____no_output_____temperature.head(10)_____no_output_____temperature.tail(10)_____no_output_____temperature.dtype_____no_output_____temperature.index_____no_output_____

**OJO!** Los valores del Index son _strings_ (_object_ es una generalización). _____no_output_____**Solución:** _Parsear_ a elementos de fecha con la función ```pd.to_datetime()```._____no_output_____

# pd.to_datetime?_____no_output_____temperature.index = pd.to_datetime(temperature.index, format='%Y-%m-%d')_____no_output_____temperature.index_____no_output_____

Para otros tipos de _parse_ puedes visitar la documentación [aquí](https://docs.python.org/3/library/datetime.html#strftime-and-strptime-behavior).
_____no_output_____La idea de los elementos de fecha es poder realizar operaciones que resulten naturales para el ser humano. Por ejemplo:_____no_output_____

temperature.index.min()_____no_output_____temperature.index.max()_____no_output_____temperature.index.max() - temperature.index.min()_____no_output_____

Volviendo a la Serie, podemos trabajar con todos sus elementos, por ejemplo, determinar rápidamente la máxima tendencia._____no_output_____

max_temperature = temperature.max()
max_temperature_____no_output_____

Para determinar el _index_ correspondiente al valor máximo usualmente se utilizan dos formas:

* Utilizar una máscara (*mask*)
* Utilizar métodos ya implementados_____no_output_____

# Mask
temperature[temperature == max_temperature]_____no_output_____# Built-in method
temperature.idxmax()_____no_output_____

## DataFrames_____no_output_____Arreglo bidimensional y extensión natural de una serie. Podemos pensarlo como la generalización de un numpy.array._____no_output_____Para motivar utilizaremos los datos de COVID-19 disponibilizados por el Ministerio de Ciencias, Tecnología, Conocimiento e Innovación de Chile. En particular, los casos totales por comuna, puedes leer más detalles en el [repositorio oficial](https://github.com/MinCiencia/Datos-COVID19/tree/master/output/producto1)._____no_output_____

covid = pd.read_csv("https://raw.githubusercontent.com/MinCiencia/Datos-COVID19/master/output/producto1/Covid-19_std.csv")
covid.head()_____no_output_____covid.tail()_____no_output_____covid.query("Region == 'Valparaíso'")_____no_output_____type(covid)_____no_output_____covid.info(memory_usage=True)<class 'pandas.core.frame.DataFrame'>
RangeIndex: 19548 entries, 0 to 19547
Data columns (total 7 columns):
 #   Column             Non-Null Count  Dtype
---  ------             --------------  -----
 0   Region             19548 non-null  object
 1   Codigo region      19548 non-null  int64
 2   Comuna             19548 non-null  object
 3   Codigo comuna      18684 non-null  float64
 4   Poblacion          18684 non-null  float64
 5   Fecha              19548 non-null  object
 6   Casos confirmados  19129 non-null  float64
dtypes: float64(3), int64(1), object(3)
memory usage: 1.0+ MB
covid.dtypes_____no_output_____

Puedes pensar que un dataframe es una colección de series_____no_output_____

covid['Casos confirmados'].head()_____no_output_____type(covid['Casos confirmados'])_____no_output_____

### Exploración _____no_output_____

covid.describe().T_____no_output_____covid.describe(include='all').T_____no_output_____covid.max()_____no_output_____

Para extraer elementos lo más recomendable es el método loc._____no_output_____

covid.loc[19271, 'Region']_____no_output_____

Evita acceder con doble corchete_____no_output_____

covid[19271]['Region']_____no_output_____

Aunque en ocasiones funcione, no se asegura que sea siempre así. [Más info aquí.](https://pandas.pydata.org/pandas-docs/stable/indexing.html#why-does-assignment-fail-when-using-chained-indexing)_____no_output_____

covid['Region'].value_counts()_____no_output_____

### Valores perdidos/nulos_____no_output_____Pandas ofrece herramientas para trabajar con valors nulos, pero es necesario conocerlas y saber aplicarlas. Por ejemplo, el método ```isnull()``` entrega un booleano si algún valor es nulo._____no_output_______Ejemplo:__ ¿Qué registros tienen casos confirmados nulos?_____no_output_____

covid.index.shape_____no_output_____covid.loc[lambda x: x['Casos confirmados'].isnull()]_____no_output_____

Si deseamos encontrar todas las filas que contengan por lo menos un valor nulo._____no_output_____

covid.isnull()_____no_output_____rows_null_mask = covid.isnull().any(axis=1)  # axis=1 hace referencia a las filas.
rows_null_mask.head()_____no_output_____covid[rows_null_mask].head()_____no_output_____covid[rows_null_mask].shape_____no_output_____

Para determinar aquellos que no tienen valors nulos el procedimiento es similar._____no_output_____

covid.loc[lambda x: x.notnull().all(axis=1)].head()_____no_output_____

Pandas incluso ofrece opciones para eliminar elementos nulos!_____no_output_____

# Cualquier registro con null
print(covid.dropna().shape)
# Filas con elementos nulos
print(covid.dropna(axis=0).shape)
# Columnas con elementos nulos
print(covid.dropna(axis=1).shape)(18681, 7)
(18681, 7)
(19548, 4)

## Resumen
* Pandas posee una infinidad de herramientas para trabajar con datos, incluyendo la carga, manipulación, operaciones y filtrado de datos.
* La documentación oficial (y StackOverflow) son tus mejores amigos.
* La importancia está en darle sentido a los datos, no solo a coleccionarlos._____no_output_____


# Notebook from quantumjot/segment-classify-track
Path: multiclass_pipeline_notebooks/stardist_multiclass_segmentation.ipynb

# Segmentation for multiclass population of objects

This notebook shows how to use Stardist (Object Detection with Star-convex Shapes) as a part of a segmentation-classification-tracking analysis pipeline.

The sections of this notebook are as follows:

1. Load images
2. Load model of choice and segment an initial image to test Stardist parameters
3. Batch segment a sequence of images

The data used in this notebook is timelapse microscopy data with h2b-gfp/rfp markers that show the spatial extent of the nucleus and it's mitotic state.

This notebook uses the dask octopuslite image loader from the CellX/Lowe lab project._____no_output_____

import matplotlib.pyplot as plt
import numpy as np
import os
import re
from octopuslite import DaskOctopusLiteLoader
from stardist.models import StarDist2D
from stardist.plot import render_label
from csbdeep.utils import normalize
from tqdm.auto import tqdm
from skimage.io import imsave
import json
from scipy import ndimage as nd_____no_output_____%matplotlib inline
plt.rcParams['figure.figsize'] = [18,8]_____no_output_____

## 1. Load images_____no_output_____

# define experiment ID and select a position
expt = 'ND0011'
pos = 'Pos6'
# point to where the data is
root_dir = '/home/nathan/data'
image_path = f'{root_dir}/{expt}/{pos}/{pos}_images'
# lazily load images
images = DaskOctopusLiteLoader(image_path,
                               remove_background = True)
images.channelsUsing cropping: (1100, 1500)

Set segmentation channel and load test image_____no_output_____

# set test image index
frame = 0
# load 2 single-channel test images
gfp = images['gfp'][frame]
rfp = images['rfp'][frame]
# create 2-channel image
img = np.zeros((gfp.shape[0], gfp.shape[1] ,2), dtype = 'uint8')
img[:,:,0] = gfp
img[:,:,1] = rfp
img.shape_____no_output_____

## 2. Load model and test segment single image _____no_output_____

model = StarDist2D(None, name='stardist_multiclass_MDCK', basedir='../models')
modelLoading network weights from 'weights_best.h5'.
Loading thresholds from 'thresholds.json'.
Using default values: prob_thresh=0.552369, nms_thresh=0.3.

### 2.1 Test run and display initial results_____no_output_____

# initialise test segmentation
labels, details = model.predict_instances(normalize(img))

# plot input image and prediction
plt.clf()
plt.subplot(1,2,1)
plt.imshow(normalize(img[:,:,0]), cmap="PiYG")
plt.axis("off")
plt.title("input image")
plt.subplot(1,2,2)
plt.imshow(render_label(labels, img = img))
plt.axis("off")
plt.title("prediction + input overlay")
plt.show()WARNING:tensorflow:6 out of the last 6 calls to <function Model.make_predict_function.<locals>.predict_function at 0x7f661eda6940> triggered tf.function retracing. Tracing is expensive and the excessive number of tracings could be due to (1) creating @tf.function repeatedly in a loop, (2) passing tensors with different shapes, (3) passing Python objects instead of tensors. For (1), please define your @tf.function outside of the loop. For (2), @tf.function has experimental_relax_shapes=True option that relaxes argument shapes that can avoid unnecessary retracing. For (3), please refer to https://www.tensorflow.org/guide/function#controlling_retracing and https://www.tensorflow.org/api_docs/python/tf/function for  more details.

### 2.2 Format mask so that it contains multiclass information
(i.e. pixel value = class identity)_____no_output_____

# format 2channel mask image
mask = np.zeros(labels.shape)
# make multiclass mask from details dict
for cell_ID, class_label in enumerate(details['class_id'], 1):
    #needs erosion step to stop merging of labels
    segment = nd.binary_erosion(labels==cell_ID)
    mask[segment] = class_label
    # set background to zero
    mask[labels == 0] = 0
plt.imshow(mask)_____no_output_____

## 3. Batch segment a whole stack of images_____no_output_____When you segment a whole data set you do not want to apply any image transformation. This is so that when you load images and masks later on you can apply the same transformation. You can apply a crop but note that you need to be consistent with your use of the crop from this point on, otherwise you'll get a shift. _____no_output_____

pos = 'Pos5'
expt = 'ND0010'
root_dir = '/home/nathan/data/kraken/ras'
reference_channel = 'irfp'
overwrite = True

model = StarDist2D(None, name='stardist_multiclass_MDCK', basedir='../models')Loading network weights from 'weights_best.h5'.
Loading thresholds from 'thresholds.json'.
Using default values: prob_thresh=0.552369, nms_thresh=0.3.
%%time
for expt in tqdm(['ND0010', 'ND0011', 'ND0012']):
    pos_list = [pos for pos in os.listdir(f'{root_dir}/{expt}')
                    if 'Pos' in pos
                    and os.path.isdir(f'{root_dir}/{expt}/{pos}')]
    for pos in tqdm(pos_list):
        if not overwrite and glob.glob(f'{root_dir}/{expt}/{pos}/{pos}_images/*channel099*.tif'):
            print(glob.glob(f'{root_dir}/{expt}/{pos}/{pos}_images/*channel099*.tif'), f'file found, skipping {expt}/{pos}')
            continue
        print('Starting experiment position:', expt, pos)
        # load images
        image_path = f'{root_dir}/{expt}/{pos}/{pos}_images'
        images = DaskOctopusLiteLoader(image_path,
                                       remove_background = True)
        # iterate over images filenames
        for frame, fn in tqdm(enumerate(images.files(reference_channel)),
                              position = 0, leave = True, total = len(images.files(reference_channel))):
            # compile 2-channels into XYC array
            img = np.zeros((images[reference_channel][frame].shape[0],
                            images[reference_channel][frame].shape[1], 2))
            img[:,:,0] = images['irfp'][frame]
            img[:,:,1] = images['rfp'][frame]
            # predict labels using 2ch image
            labels, details = model.predict_instances(normalize(img))
            # format 2channel mask image
            mask = np.zeros(labels.shape)
            # make multiclass mask from details dict
            for cell_ID, class_label in enumerate(details['class_id'], 1):
                #needs erosion step to stop merging of labels
                segment = nd.binary_erosion(labels==cell_ID)
                mask[segment] = class_label
                # set background to zero
                mask[labels == 0] = 0
            # set filename as mask format (channel099)
            fn = ((images.files('irfp')[frame])).replace('channel003', 'channel099')
            # save out labelled image
            imsave(fn, mask.astype(np.uint8), check_contrast=False)
          0%|          | 0/12 [00:00<?, ?it/s]