Convert pandas dataframe to 4d binned np array

Viewed 27

I have the following dataframe of trials, images, neurons and spikes: spikes

and I'd like to convert it to a numpy array of shape neurons x images x trials x spikes where spikes is binned to 10 ms intervals from 0-250ms. Essentially the end shape will be something like neurons x 118 x 50 x 25. I'm able to do it in for loops the following way

ims = []
for i in range(118):
    trials = []
    im = spikes[spikes['image_index']==i]
    im = im.replace(np.unique(im['trial_id']),range(50))
    for j in range(50):
        im_trial = im[im['trial_id']==j]
        groups = im_trial.groupby(['unit_id', pd.cut(im_trial.time_since_stimulus_presentation_onset, range(0,260,10))])
        rates = groups.size().unstack().apply(lambda x:x/10) 
        print(np.array(rates).shape)
        trials.append(np.array(rates))
    ims.append(trials)

but this results in a ragged array of shape (118 x 50) which isnt what I want

0 Answers
Related