Exemplo n.º 1
0
def unit(u_, Data0, cc, blockID):
	
	# number of time bins to include in the LFP array
	nlfpsamp = 0
	for tt, trial in enumerate(Data0['trial'][0][0][0]):

		thislfpsamp = trial['LFP'].shape[1]
		if thislfpsamp>nlfpsamp:
			nlfpsamp = thislfpsamp
	
	
	ntrials = Data0['trial'][0][0][0].size # find number of trials
	nstimID = Data0['trial'][0][0][0][0]['Epoch_Value'][0].size
	
	# initialze LFP, spike times, spike trials, spike waveform
	lfp = np.ndarray((0, nlfpsamp), dtype = 'float32')
	spktimes = np.ndarray(0)
	spktrials = np.ndarray(0)
	spkwaveform = np.ndarray((0, 22))

	# initialize frequency and attenuation IDs
	stimID = np.ndarray((0, nstimID), dtype = 'float32')

	ttt = 0 # valid trial counter
	for tt in range(ntrials):
		trial = Data0['trial'][0][0][0][tt]

		thisstimID = np.float32(trial['Epoch_Value'][0])

		# if not ((blockID.startswith('b')) and (thisstimID[0] < 2)):

		# get the LFP for this trial and pad it with nans so it can fit in a matrix (since
		# some of the trials have +/-1 data point for LFP)
		lfpchannel = trial['LFP'][cc]
		lfpchannel = np.concatenate((lfpchannel, np.zeros(nlfpsamp - len(lfpchannel)) * np.nan))
		lfp = np.vstack((lfp, lfpchannel))

		spktime = trial['CH'][0][cc]['latency']
		if np.prod(spktime.shape) > 0:
			spktimes = np.append(spktimes, spktime)
			spktrials = np.append(spktrials, np.ones(spktime.size) * ttt)
			spkwaveform = np.concatenate((spkwaveform, trial['CH'][0][cc]['spkwaveform'].T), 0)
			
		# add to Epoch_Value
		stimID = np.vstack((stimID, thisstimID))

		ttt += 1 # increment valid trial counter

			# end if valid ID
	
	# end trial loop
	
	if spktimes.size == 0: # if no spikes
		print 'No spikes detected for this unit.'
		spktimes = np.array([np.nan])
		spktrials = np.array([np.nan])
		spkwaveform = np.array([np.nan])
		rast = np.array([np.nan])
		
	else:
		# filter out unwanted trials
		remID = np.array([np.nan, np.nan])
		if blockID.startswith('b'):
			remID = np.array([1., 70.])
		elif blockID.startswith('r'):
			remID = np.array([0., 0.])
	
		spktimes, spktrials, spkwaveform, lfp, stimID = \
			remove_trials(spktimes, spktrials, spkwaveform, lfp, stimID, remID)
	
		# create raster
		ntrials = stimID.shape[0]
		nbins = np.ceil(1000 * spktimes.max())+1
		rast = Spikes.calc_rast(spktimes, spktrials, ntrials, nbins)

	
	# save out to file
	u_.create_dataset('chan', data = cc)
	u_.create_dataset('blockID', data = blockID)
	# add stimulus ID datasets to this stimset on this unit
	u_.create_dataset('stimID', data = stimID)
	u_.create_dataset('lfp', data = lfp, compression = 'gzip')
	u_.create_dataset('spktimes', data = spktimes, compression = 'gzip')
	u_.create_dataset('spktrials', data = spktrials, compression = 'gzip')
	u_.create_dataset('spkwaveform', data = spkwaveform, compression = 'gzip')
	u_.create_dataset('rast', data = rast, compression = 'gzip')
		
	if blockID.startswith('b'):
		rf = RF.calc_rf(rast, stimID)
		u_.create_dataset('rf', data = rf, compression = 'gzip')