Exemple #1
0
def calc_rast_by_stim(rast, stimparams, bins = None):

	
	usp1 = np.unique(stimparams[:, 0])
	usp2 = np.unique(stimparams[:, 1])
		
	nstimparams = stimparams.shape[1]
	ustimparams = [np.unique(stimparams[:, 0]), np.unique(stimparams[:, 1])]
	nparamlevels = np.array([usp1.size, usp2.size])

	nbins = rast.shape[1]
	
	ntrials_per_stim = np.zeros((nparamlevels[0], nparamlevels[1]))
	for i in range(nparamlevels[0]):
		for j in range(nparamlevels[1]):
			ntrials_per_stim[i, j] = RF.get_trials(stimparams, np.array([usp1[i], usp2[j]])).size
	
	if np.unique(ntrials_per_stim).size > 1:
		print 'Different numbers of trials per stimulus!'
	elif np.unique(ntrials_per_stim).size == 1:
		ntrials = np.int32(ntrials_per_stim[0])
	
	rast2 = np.zeros((ntrials, nparamlevels[0], nparamlevels[1], nbins))
	for i in range(nparamlevels[0]):
		for j_ in range(nparamlevels[1]):
			
			ix = RF.get_trials(stimparams, (usp1[i], usp2[j_]))
			rast2[:, i, j_, :] = rast[ix, :]
				
	
	return rast2, ustimparams
Exemple #2
0
def make_spk_mask(spktrials, stimparams, param):
	
	ix = RF.get_trials(stimparams, param)
	spk_mask = np.zeros(spktrials.size, dtype = np.bool)
	for ix_ in ix:
		spk_mask[spktrials == ix_] = True
		
	return spk_mask
Exemple #3
0
def make_trial_mask(stimparams, param):
		
	ntrials = stimparams.shape[0]
	ix = RF.get_trials(stimparams, param)
	trial_mask = np.zeros(ntrials, dtype = np.bool)
	for ix_ in ix:
		trial_mask[ix_] = True
		
	return trial_mask
Exemple #4
0
def make_spk_and_trial_masks(spktrials, stimparams, param):
	ntrials = stimparams.shape[0]
	ix = RF.get_trials(stimparams, param)
	trial_mask = np.zeros(ntrials, dtype = np.bool)
	spk_mask = np.zeros(spktrials.size, dtype = np.bool)
	for ix_ in ix:
		spk_mask[spktrials == ix_] = True
		trial_mask[ix_] = True
		
	return spk_mask, trial_mask
Exemple #5
0
def calc_rf_psth(rast, stimparams):
	
	ufreqs = np.unique(stimparams[:, 0])
	uattens = np.unique(stimparams[:, 1])
	nfreqs, nattens = ufreqs.size, uattens.size
	rf_psth = np.empty((nattens, nfreqs, rast.shape[1]))
	for i in range(nfreqs):
		for j in range(nattens):
			ix = RF.get_trials(stimparams, np.array([ufreqs[i], uattens[j]]))
			rf_psth[j, i, :] = rast[ix, :].mean(0)
	
	return rf_psth
Exemple #6
0
def circ_psth_all(rast, stimparams, freq, npips, onset = 0.05, bins = 20, color = 'b', remove_first = False, axs = None):
	'''
	Input:
	Output:
		r :  the mean vector length for each repetition rate
		V : the summed vector length for each repetition rate
		theta : the mean vector angle for each repetition rate
	'''
	urrs = np.unique(stimparams[:, 1])
	nrrs = urrs.size
	ix = RF.get_trials(stimparams, (freq, np.nan))
	rast_ = rast[ix, :]
	stimparams_ = stimparams[ix, :]
	r = []; V = []; theta = []
	
	for i in xrange(nrrs):
		ix = RF.get_trials(stimparams_, (np.nan, urrs[i]))
		r_, V_, theta_ = circ_psth(rast_[ix, :], urrs[i], npips[i], onset = onset, bins = bins, color = color, remove_first = remove_first, ax = axs[i])
		r.append(r_); V.append(V_); theta.append(theta_)
	
	misc.sameyaxis(axs)

	return np.array(r), np.array(V), np.array(theta)
Exemple #7
0
def calc_vs_all(rast, stimparams, ufreqs, urrs, npips = 6, onset = 0.05):
	
	ufreqs = np.asarray(ufreqs)
	urrs = np.asarray(urrs)
	nfreqs = ufreqs.size
	nrrs = urrs.size
	vs = np.empty((nfreqs, nrrs))*np.nan
	vs_p = np.empty((nfreqs, nrrs))*np.nan
	for f in range(nfreqs):
		for r in range(nrrs):
			ix = RF.get_trials(stimparams, [ufreqs[f], urrs[r]])
			rast_ = rast[ix, :]
			vs[f, r], vs_p[f, r] = calc_vs(rast_, urrs[r], npips, onset)
		
	return vs, vs_p
Exemple #8
0
def calc_rrtf_lfp_all(lfp, lfp_t, stimparams, freq, rrs, onset = 0.05):

	nrrs = rrs.size
	rrtf_lfp = np.empty(nrrs) * np.nan
	trial_ix = stimparams[:, 0] == freq
	pip_start = 0.005 + onset
	pip_end = pip_start + 0.02
	time_ix = np.vstack((pip_start<lfp_t, lfp_t<pip_end)).all(0)

	lfp_mag_1st = (lfp[trial_ix, :][:, time_ix]).mean(0).min()
	print lfp_mag_1st
	for i, rr in enumerate(rrs):
		lfp_ = lfp[RF.get_trials(stimparams, np.array([freq, rr])), :]
		rrtf_lfp[i] = calc_rrtf_lfp(lfp_, lfp_t, rr, lfp_mag_1st)
	
	return rrtf_lfp
Exemple #9
0
def calc_lfp_by_stim(rast, stimparams):

	nstimparams = stimparams.shape[1]
	
	usp = []
	for i in range(nstimparams):	
		usp.append(list(np.unique(stimparams[:, i])))

	nparamlevels = np.empty(nstimparams, dtype = np.int32)
	for i in range(nstimparams):
		nparamlevels[i] = len(usp[i])

	ntrials_per_stim = np.zeros(nparamlevels)

	'''
	compute
	nbins	:	the number of bins
	'''
	dur_ms = rast.shape[1] # number of milliseconds
	t_ms = np.arange(dur_ms) # time indices in ms
	nbins = rast.shape[-1]
	assert np.unique(ntrials_per_stim).size == 1

	ntrials = np.int32(ntrials_per_stim[0])
	
	psth_shape = np.hstack((nparamlevels, nbins))
	psth = np.zeros(psth_shape)
	
	combinations = []
	combinations_ix = []
	for i in itertools.product(*usp):
		combinations.append(i)
		combinations_ix_ = []
		for j, i_ in enumerate(i):
			q = (np.array(usp[j])==i_).nonzero()[0][0]
			combinations_ix_.append(q)
		combinations_ix.append(combinations_ix_)
		
	for m, n in zip(combinations, combinations_ix):
		ix = RF.get_trials(stimparams, m)
		ntrials = ix.size
		lfp_ = rast[ix, :]
		psth[tuple(n)] = lfp_.sum(0)
				
	return psth, usp
Exemple #10
0
def calc_rrtf_all(rast, stimparams, freq, urrs, npips = 6, onset = 0.05, norm = True):
	'''
	Takes the full block raster and stimparams, finds the response to the first tone by filtering the rast to include only responses to the given frequency (but for all repetition rates), then it filters the rast to only the given freq/rr pair and passes that	to calc_rrtf, along with the first tone response for all repetition rates

	Input:
		rast : full block raster
		stimparams : full block stimulus parameters
		freq : in Hz, the frequency played to this unit
		urrs : sorted list (lo-hi) of repetition rates played
		npips : list of the number of pips at each rate, or scalar if each rate had the same number of pips
		onset : in seconds, the onset time for the first pip
		norm : for the response to each pip, subtract the pre-pip response

	Output:
		rrtf : n-length vector, where n is the number of repetition rates
	'''
	if type(npips) is int:
		npips = [npips]*len(urrs)

	nrrs = urrs.size
	rrtf = np.empty(nrrs) * np.nan

	# get raster subset for this frequency
	ix = stimparams[:, 0] == freq

	# response onset and offset (5 - 25 ms after stimulus onset)
	resp_start = np.int32((0.005 + onset) * 1000)
	resp_end = resp_start + 20
	nspks_1st = (rast[ix, resp_start:resp_end]).mean() # spikes per millisecond

	# normalize by pre-pip baseline (-20 - 0 ms before stimulus onset)
	if norm:
		pip_end_pre = resp_start - 5
		pip_start_pre = pip_end_pre - 20
		nspks_1st = nspks_1st - (rast[ix, pip_start_pre: pip_end_pre]).mean()

	# loop through repetition rates, get raster subset, and calculate RRTF
	for i, (rr, npip) in enumerate(zip(urrs, npips)):
		rast_ = rast[RF.get_trials(stimparams, np.array([freq, rr])), :]
		rrtf[i] = calc_rrtf(rast_, rr, nspks_1st, npips = npip, norm = norm)
	
	return rrtf
Exemple #11
0
def aligned_psth_separate_all(rast, stimparams, freq, npips, onset = 0.05, axs = None):
	'''
	Input:
		rast : full block raster
		stimparams : full block stimulus parameters
		freq : in Hz, the frequency played to this neuron
		npips : number of pips for each repetition rate
		onset : in ms, onset time of first pip
		axs : a list of axis to which the output will be displayed
	'''
	urrs = np.unique(stimparams[:, 1])
	nrrs = urrs.size
	if axs is None:
		axs = [None]*nrrs

	aligned_psths = []
	for rr, npip, ax in zip(urrs, npips, axs):
		ix = RF.get_trials(stimparams, (freq, rr))
		psth = 1000*rast[ix, :].mean(0)
		aligned_psths.append(aligned_psth_separate(psth, rr, npip, onset = onset, ax = ax))

	return aligned_psths
Exemple #12
0
		rast = frr['rast'].value
		stimparams = frr['stimID'].value
		frr.close()

		# if not np.isnan(spktimes[0]):
		cf = cfs[cfs[:, 0]==unitnum, 1][0]
		cf_hz = ix2freq[20:][int(cf)]
		freqs = stimparams[:, 0]
		rrs = stimparams[:, 1]
		ufreqs = np.unique(freqs)
		urrs = np.unique(rrs)
		nrrs = urrs.size
	
		# now we determine which of the frequencies we played is closest to this neuron's CF
		thisfreq, thisfreq_ix, thisfreq_err = misc.closest(ufreqs, cf_hz, log = True)
		if np.abs(thisfreq_err) > 0.2:
			print 'No close frequency found!'
		thisfreq = ufreqs[thisfreq_ix]
	
		# isolate the parts of the raster for this frequency and build a psth for each RR
		ix = RF.get_trials(stimparams, np.array([thisfreq, np.nan]))
		thisrast = rast[ix, :1050]
		thisstims = stimparams[ix, :]
		psths, ustims = Spikes.calc_psth_by_stim(thisrast, thisstims)
	
		rrtf = RR.calc_rrtf_all(thisrast, thisstims, thisfreq, urrs)
	
		db.resize(db.size+1)
		db[-1] = np.array((gen, exp, sess, unitnum, cf_hz, rrtf, urrs), dtype = dtype)

np.savez(savepath, db)
Exemple #13
0
def calc_psth_by_stim(rast, stimparams, bins = 0.001):

	nstimparams = stimparams.shape[1]
	
	usp = []
	for i in range(nstimparams):	
		usp.append(list(np.unique(stimparams[:, i])))

	nparamlevels = np.empty(nstimparams, dtype = np.int32)
	for i in range(nstimparams):
		nparamlevels[i] = len(usp[i])

	ntrials_per_stim = np.zeros(nparamlevels)

	'''
	compute
	nbins	:	the number of bins
	bins	:	times (seconds) for each bin
	'''
	dur_ms = rast.shape[1] # number of milliseconds
	t_ms = np.arange(dur_ms) # time indices in ms
	if type(bins) in (int, np.int32):
		nbins = bins
		bins = np.linspace(0, dur_ms, nbins) / 1000
		bindur = bins[1]-bins[0]
	elif type(bins) in (float, np.float):
		bindur = bins
		bins = np.arange(0, (dur_ms/1000.)+bindur, bindur)
		nbins = bins.size-1
	elif type(bins) is np.ndarray:
		nbins = bins.size-1
		bindur = bins[1]-bins[0]
	

	assert np.unique(ntrials_per_stim).size == 1

	ntrials = np.int32(ntrials_per_stim[0])
	
	psth_shape = np.hstack((nparamlevels, nbins))
	psth = np.zeros(psth_shape)
	
	combinations = []
	combinations_ix = []
	for i in itertools.product(*usp):
		combinations.append(i)
		combinations_ix_ = []
		for j, i_ in enumerate(i):
			q = (np.array(usp[j])==i_).nonzero()[0][0]
			combinations_ix_.append(q)
		combinations_ix.append(combinations_ix_)
		
	for m, n in zip(combinations, combinations_ix):
		ix = RF.get_trials(stimparams, m)
		ntrials = ix.size
		spktimes_ = rast2spktimes(rast[ix, :])
		psth_, edges_ = np.histogram(spktimes_, bins = bins)
		psth_ = (1./bindur) * (psth_.astype(float)/ntrials)
		psth[tuple(n)] = psth_
	
	psth = psth.squeeze()		
	return psth, usp