def test_cutout_peak(): """Test that the `filtertools.cutout()` method correctly uses the filter peak.""" chunk = np.zeros((4, 2, 2)) chunk[2, 1, 1] = 1 arr = np.pad(chunk, ((0, 0), (1, 1), (1, 1)), 'constant', constant_values=0) cutout = flt.cutout(arr, width=1) assert np.allclose(cutout, chunk)
def test_cutout(): """Test cutting out a small tube through a 3D spatiotemporal filter""" np.random.seed(0) chunk = np.random.randn(4, 2, 2) arr = np.pad(chunk, ((0, 0), (1, 1), (1, 1)), 'constant', constant_values=0) cutout = flt.cutout(arr, (2, 2), width=1) assert np.allclose(cutout, chunk)
def stimcut(data, expt, ci, width=11): """Cuts out a stimulus around the whitenoise receptive field""" # get the white noise STA for this cell wn = _loadexpt_h5(expt, 'whitenoise') sta = np.array(wn[f'train/stas/cell{ci+1:02d}']).copy() # find the peak of the sta xc, yc = ft.filterpeak(sta)[1] # cutout stimulus X, y = data Xc = ft.cutout(X, idx=(yc, xc), width=width) yc = y[:, ci].reshape(-1, 1) return Exptdata(Xc, yc)
def loadexpt(expt, cells, filename, train_or_test, history, nskip, cutout_width=None): """Loads an experiment from an h5 file on disk Parameters ---------- expt : str The date of the experiment to load in YY-MM-DD format (e.g. '15-10-07') cells : list of ints Indices of the cells to load from the experiment filename : string Name of the hdf5 file to load (e.g. 'whitenoise' or 'naturalscene') train_or_test : string The key in the hdf5 file to load ('train' or 'test') history : int Number of samples of history to include in the toeplitz stimulus nskip : float, optional Number of samples to skip at the beginning of each repeat cutout_width : int, optional If not None, cuts out the stimulus around the STA (assumes cells is a scalar) """ assert history > 0 and type(history) is int, "Temporal history must be a positive integer" assert train_or_test in ('train', 'test'), "train_or_test must be 'train' or 'test'" with notify('Loading {}ing data for {}/{}'.format(train_or_test, expt, filename)): # get whitenoise STA for cutout stimulus if cutout_width is not None: assert len(cells) == 1, "cutout must be used with single cells" wn = _loadexpt_h5(expt, 'whitenoise') sta = np.array(wn[f'train/stas/cell{cells[0]+1:02d}']).copy() py, px = ft.filterpeak(sta)[1] # load the hdf5 file with _loadexpt_h5(expt, filename) as f: expt_length = f[train_or_test]['time'].size # load the stimulus into memory as a numpy array, and z-score it if cutout_width is None: stim = zscore(np.array(f[train_or_test]['stimulus']).astype('float32')) else: stim = zscore(ft.cutout(np.array(f[train_or_test]['stimulus']), idx=(px, py), width=cutout_width).astype('float32')) # apply clipping to remove the stimulus just after transitions num_blocks = NUM_BLOCKS[expt] if train_or_test == 'train' and nskip > 0 else 1 valid_indices = np.arange(expt_length).reshape(num_blocks, -1)[:, nskip:].ravel() # reshape into the Toeplitz matrix (nsamples, history, *stim_dims) stim_reshaped = rolling_window(stim[valid_indices], history, time_axis=0) # get the response for this cell (nsamples, ncells) resp = np.array(f[train_or_test]['response/firing_rate_10ms'][cells]).T[valid_indices] resp = resp[history:] # get the spike history counts for this cell (nsamples, ncells) binned = np.array(f[train_or_test]['response/binned'][cells]).T[valid_indices] spk_hist = rolling_window(binned, history, time_axis=0) return Exptdata(stim_reshaped, resp, spk_hist)
def test_cutout_raises(): """Test cutout() raises an exception when the index argument does not have two elements.""" with pytest.raises(ValueError): flt.cutout(np.zeros((10, 10, 10)), (1, ))
# In[6]: spk = f['spikes/cell01'] # In[8]: time = np.array(f['train/time']) sta, tax = ft.getsta(time, whitenoise_train.X, spk, 35) # In[11]: Xcut = ft.cutout(whitenoise_train.X, idx=np.flipud(ft.filterpeak(sta)[1]), width=5) # In[113]: stc = np.zeros((35*11*11, 35*11*11)) for idx, s in enumerate(ft.getste(time, Xcut, spk, 35)): sr = s.astype('float').ravel() if sr.size == (35*11*11): stc += np.outer(sr, sr) if idx % 500 == 0: print('{}'.format(100.*idx/len(spk))) # In[ ]:
def test_cutout_raises(): """Test cutout() raises an exception when the index argument does not have two elements.""" with pytest.raises(ValueError): flt.cutout(np.zeros((10, 10, 10)), (1,))