Search
Fig 5. Results of knockout analysis

This code example is a Jupyter notebook with a Script of Scripts (SoS) workflow.

The calculations are written using Python 2.7 (from author's repo), and the interactive figures are written in Python 3.6 with Plotly.

Figure 5:

The firing rate maps of the most (a-e) and least (f) influential neurons according to the knockout analysis. Colour bar to the right of each plot indicates the firing rate in Hz. With the complete dataset the mean error was 12.50±0.28 cm. When knocking out neurons 55, 26, 41, 17, 23 (the five most influential) and 9 (the least influential), the mean error increased to 14.72 cm, 13.80 cm, 13.66 cm, 13.50 cm, 13.49 cm and 12.58 cm respectively.

https://doi.org/10.1371/journal.pcbi.1006822.g005

%use Python2

from ratdata import load_data
import numpy as np
import pickle 

data = np.load('R2192_models/grads_R2192_1x1400_v1.npz')
#grads = data['grads'][:,:,:63]
grads = data['grads'][:,:,:63]
#onehot_grads = data['grads'][:,:,63:]
targets = data['targets']

new_tgts=0
all_grads=[]

# For the article, this was actually done for 10 CV-runs, 
# but to limit repo size we have included only one set of grad to the repository

for i in range(1,2):
    data = np.load("R2192_models/grads_R2192_1x1400_v"+str(i)+".npz")
    grads = data['grads'][:,:,:63]
    if i>1:
        assert(np.all(new_tgts==data["targets"]))
    new_tgts = data["targets"]
    all_grads.append(grads)

mean_grads = np.mean(np.abs(all_grads), axis=0)

neuron_grads = np.mean(mean_grads, axis=(0,1))


feature_importance = neuron_grads

sorted_idx = np.argsort(feature_importance)

X, y = load_data('data/R2192_1x1400_at35_step200_bin100-RAW_feat.dat', 'data/R2192_1x1400_at35_step200_bin100-RAW_pos.dat')

list_neurons = {"1":53, 
                "2":14, 
                "3":2, 
                "4":1, 
                "5":18, 
                "6":48}

labels_titles = {"1": '(a)',
                 "2": '(b)', 
                 "3": '(c)',
                 "4": '(d)',
                 "5": '(e)',
                 "6": '(f)'} 

with open('train.pickle', 'wb') as f:
    pickle.dump([X, y, sorted_idx], f)

Figure 5 (a-f)

%use Python3

import plotly.graph_objects as go
import plotly.tools as tls
from plotly.offline import plot, iplot, init_notebook_mode
from IPython.core.display import display, HTML
init_notebook_mode(connected = True)
config={'showLink': False, 'displayModeBar': False}
import numpy as np
from hexplot import  get_hexbin_attributes, pl_cell_color, make_hexagon, mpl_to_plotly
import matplotlib.pyplot as plt
import matplotlib.cm as cm
import pickle

with open('train.pickle', 'rb') as f:
    X, y,sorted_idx = pickle.load(f, encoding='bytes')

with open ('outfile', 'rb') as fp:
    centers_main = pickle.load(fp)
 
with open ('outfile2', 'rb') as fp:
    shapes_main = pickle.load(fp)
    
with open ('HB_map.pickle', 'rb') as fp:
    HB_map = pickle.load(fp)
    
list_neurons = {"1":53, 
                "2":14, 
                "3":2, 
                "4":1, 
                "5":18, 
                "6":48}

labels_titles = {"1": '(a)',
                 "2": '(b)', 
                 "3": '(c)',
                 "4": '(d)',
                 "5": '(e)',
                 "6": '(f)'}     
    
Xx = X
yy = y
    
fig5 = go.Figure()


for i in range(1,7):
    pl_algae = mpl_to_plotly(HB_map[0][i-1].cmap,11)
    X, Y = zip(*centers_main[i-1])
    # define  text to be  displayed on hovering the mouse over the cells
    _, _, _,  counts = get_hexbin_attributes(HB_map[0][i-1])
    text = [f'x: {round(X[k],2)}<br>y: {round(Y[k],2)}<br>Counts: {int(counts[k])}' for k in range(len(X))]
    fig5.add_trace(go.Scatter(x=list(X), 
                              y=list(Y), 
                              mode='markers',
                              marker=dict(size=0.5, 
                                          color=counts, 
                                          colorscale=pl_algae, 
                                          showscale=True,
                                          colorbar=dict(thickness=20,  
                                                        ticklen=4)
                                         ), 
                              showlegend=False,
                              visible = False, 
                              text=text,
                              hoverinfo='text',
                              ))

    fig5.data[0].visible = True
    
fig5.update_layout(
    updatemenus=[
        dict(
            active = 0, 
            x=0.1,
            y=1.2,
            yanchor="top",
            buttons=list([
                dict(label=labels_titles["1"] + "\n Neuron #%d" % sorted_idx[-list_neurons["1"]],
                             method="update",
                             args=[{"visible": [True,False,False,False,False,False]},
                                   {"title": labels_titles["1"],
                                   "shapes": shapes_main[0] }
                                  ]),
                
                dict(label=labels_titles["2"] + "\n Neuron #%d" % sorted_idx[-list_neurons["2"]],
                             method="update",
                             args=[{"visible": [False,True,False,False,False,False]},
                                   {"title": labels_titles["2"],
                                   "shapes": shapes_main[1]}
                                  ]),
                
                dict(label= labels_titles["3"] + "\n Neuron #%d" % sorted_idx[-list_neurons["3"]],
                             method="update",
                             args=[{"visible": [False,False,True,False,False,False]},
                                   {"title": labels_titles["3"],
                                   "shapes": shapes_main[2] }
                                  ]),
                
                dict(label=labels_titles["4"] + "\n Neuron #%d" % sorted_idx[-list_neurons["4"]],
                             method="update",
                             args=[{"visible":  [False,False,False,True,False,False]},
                                   {"title": labels_titles["4"],
                                   "shapes": shapes_main[3] }
                                  ]),
                
                dict(label=labels_titles["5"] + "\n Neuron #%d" % sorted_idx[-list_neurons["5"]],
                             method="update",
                             args=[{"visible":  [False,False,False,False,True,False]},
                                   {"title": labels_titles["5"],
                                   "shapes": shapes_main[4] }
                                  ]),
                
                dict(label=labels_titles["6"]+"\n Neuron #%d" % sorted_idx[-list_neurons["6"]],
                             method="update",
                             args=[{"visible": [False,False,False,False,False,True]},
                                   {"title": labels_titles["6"],
                                   "shapes": shapes_main[5] }
                                  ]) 
                ])
            )], 
    title = labels_titles["1"],
    title_x = 0.55, 
    xaxis_title='X-coordinate (cm)',
    xaxis=dict(mirror=True,
               ticks='outside',
               showline=True,
               linecolor='#000',
               tickvals = [0,20,40,60,80,100], 
               tickfont = dict(size=15)), 
    yaxis_title='Y-coordinate (cm)',
    yaxis=dict(mirror=True,
               ticks='outside', 
               showline=True,
               linecolor='#000',
               tickfont = dict(size=15)),
    plot_bgcolor='#fff', 
    width = 550, 
    height = 460, 
    shapes = shapes_main[0],
    hovermode='closest',
    margin=go.layout.Margin(l=50,
                            r=50,
                            b=60,
                            t=35),
    font = dict(size = 14))


plot(fig5, filename = 'fig5.html', config = config)
# THEBELAB
display(HTML('fig5.html'))
# BINDER
# iplot(fig5,config=config)