def spec_dpl_kernel(fparam, fts, fspec, f_max): dpl = dipolefn.Dipole(fts) dpl.units = 'nAm' # Do the conversion prior to generating these spec # dpl.convert_fAm_to_nAm() # Generate various spec results spec_agg = MorletSpec(dpl.t, dpl.dpl['agg'], fparam, f_max) spec_L2 = MorletSpec(dpl.t, dpl.dpl['L2'], fparam, f_max) spec_L5 = MorletSpec(dpl.t, dpl.dpl['L5'], fparam, f_max) # Get max spectral power data # for now, only doing this for agg max_agg = spec_agg.max() # Generate periodogram resutls p_dict = paramrw.read(fparam)[1] pgram = Welch(dpl.t, dpl.dpl['agg'], p_dict['dt']) # Save spec results np.savez_compressed(fspec, time=spec_agg.t, freq=spec_agg.f, TFR=spec_agg.TFR, max_agg=max_agg, t_L2=spec_L2.t, f_L2=spec_L2.f, TFR_L2=spec_L2.TFR, t_L5=spec_L5.t, f_L5=spec_L5.f, TFR_L5=spec_L5.TFR, pgram_p=pgram.P, pgram_f=pgram.f)
def exec_show_dpl_max(ddata, opts={}): p = { 'layer': 'L5', 'n_sim': 0, 'n_trial': 0, } args_check(p, opts) expmt_group = ddata.expmt_groups[0] n = p['n_sim'] + p['n_sim']*p['n_trial'] fdpl = ddata.file_match(expmt_group, 'rawdpl')[n] fparam = ddata.file_match(expmt_group, 'param')[n] T = paramrw.find_param(fparam, 'tstop') xlim = (50., T) dpl = dipolefn.Dipole(fdpl) dpl.baseline_renormalize(fparam) dpl.convert_fAm_to_nAm() # add this data to the dict for the string output mapping p['dpl_max'] = dpl.lim(p['layer'], xlim)[1] p['units'] = dpl.units print "The maximal value for the dipole is %(dpl_max)4.3f %(units)s for sim=%(n_sim)i, trial=%(n_trial)i in layer %(layer)s" % (p)
def example_analysis_for_simulation(): # from these two directories droot = fio.return_data_dir() dsim = os.path.join(droot, '2016-02-03/beta-sweep-000') # create the SimulationPaths() object ddata and read the simulation ddata = fio.SimulationPaths() ddata.read_sim(droot, dsim) # print dir(ddata) # print type(np.zeros(5)) # print ddata.expmt_groups # print ddata.fparam # for key, val in ddata.dfig['testing'].items(): # print key, val # print dir({}) # p_exp = paramrw.ExpParams(ddata.fparam) # print p_exp.p_all['dt'] # # print p_exp.p_all # iterate through experimental groups and do the analysis on individual files, etc. for expmt_group in ddata.expmt_groups: print "experiment group is: %s" % (expmt_group) # print ddata.dfig[expmt_group] flist_param = ddata.file_match(expmt_group, 'param') flist_dpl = ddata.file_match(expmt_group, 'rawdpl') # flist_spk = ddata.file_match(expmt_group, 'rawspk') # fio.prettyprint(flist_spk) # iterate through files in the lists for fparam, fdpl in zip(flist_param, flist_dpl): # print fparam, fdpl gid_dict, p_tr = paramrw.read(fparam) # for key, val in p_tr.items(): # print key, val # fio.prettyprint(p_tr.keys()) # create and load dipole data structure d = dipolefn.Dipole(fdpl) # more or less analysis goes here. # generate a filename for a dipole plot fname_png = ddata.return_filename_example('figdpl', expmt_group, p_tr['Sim_No'], tr=p_tr['Trial']) # print p_tr['Trial'], p_tr['Sim_No'], fname_png # example figure for this pair of files fig = PT_example.FigExample() # plot dipole fig.ax['dipole'].plot(d.t, d.dpl['agg']) fig.ax['dipole'].plot(d.t, d.dpl['L2']) fig.ax['dipole'].plot(d.t, d.dpl['L5']) fig.savepng(fname_png) fig.close()
def exec_calc_dpl_mean(ddata, opts={}): for expmt_group in ddata.expmt_groups: list_fdpl = ddata.file_match(expmt_group, 'rawdpl') # order of l_dpl is same as list_fdpl l_dpl = [dipolefn.Dipole(f) for f in list_fdpl] for dpl in l_dpl: print dpl.mean_stationary(opts)
def average_dipole(dsim): dproj = fio.return_data_dir() ddata = fio.SimulationPaths() ddata.read_sim(dproj, dsim) # grab the first experimental group expmt_group = ddata.expmt_groups[0] flist = ddata.file_match(expmt_group, 'rawdpl') N_dpl = len(flist) # grab the time and the length dpl_time = dipolefn.Dipole(flist[0]).t length_dpl = dipolefn.Dipole(flist[0]).N # preallocation of the total dipole # dpl_agg = np.zeros((N_dpl, length_dpl)) dpl_sum = np.zeros(length_dpl) # the specific dipole to use dpl_specific = 'agg' for f in flist: dpl_f = dipolefn.Dipole(f) dpl_sum = dpl_sum + dpl_f.dpl[dpl_specific] dpl_scaled = dpl_sum * 1e-6 dpl_mean = dpl_scaled / N_dpl print dpl_sum print ' ' print dpl_scaled print ' ' print dpl_mean figure_create(dpl_time, dpl_mean)
def exec_welch_max(ddata, opts): p = { 'f_min': 0., } args_check(p, opts) # assume first expmt_group for now expmt_group = ddata.expmt_groups[0] # grab list of dipoles list_dpl = ddata.file_match(expmt_group, 'rawdpl') list_param = ddata.file_match(expmt_group, 'param') # iterate through dipoles for fdpl, fparam in zip(list_dpl, list_param): # grab the dt (needed for the Welch) dt = paramrw.find_param(fparam, 'dt') # grab the dipole dpl = dipolefn.Dipole(fdpl) dpl.baseline_renormalize(fparam) dpl.convert_fAm_to_nAm() # create empty pgram pgram = dict.fromkeys(dpl.dpl) pgram_max = dict.fromkeys(dpl.dpl) # perform stationary Welch, since we're not saving this data yet for key in pgram.keys(): pgram[key] = specfn.Welch(dpl.t, dpl.dpl[key], dt) # create a mask based on f min fmask = (pgram[key].f > p['f_min']) P_cut = pgram[key].P[fmask] f_cut = pgram[key].f[fmask] p_max = np.max(P_cut) f_max = f_cut[P_cut == p_max] # p_max = np.max(pgram[key].P) # f_max = pgram[key].f[pgram[key].P == p_max] # not clear why saving for now pgram_max[key] = (f_max, p_max) print "Max power for %s was %.3e at %4.2f Hz, with f min set to %4.2f" % (key, p_max, f_max, p['f_min'])
def pkernel(dfig_dpl, f_dpl, f_spk, f_spec, f_current, f_spec_current, f_param, ax_handles, spec_cmap='jet'): T = paramrw.find_param(f_param, 'tstop') xlim = (50., T) # into the pdipole directory, this will plot dipole, spec, and spikes # create the axis handle f = ac.FigDipoleExp(ax_handles) # create the figure name fprefix = fio.strip_extprefix(f_dpl) + '-dpl' fname = os.path.join(dfig_dpl, fprefix + '.png') # grab the dipole dpl = dipolefn.Dipole(f_dpl) dpl.convert_fAm_to_nAm() # plot the dipole to the agg axes dpl.plot(f.ax['dpl_agg'], xlim) dpl.plot(f.ax['dpl_agg_L5'], xlim) # f.ax['dpl_agg_L5'].hold(True) # dpl.plot(f.ax['dpl_agg_L5'], xlim, 'L5') # plot individual dipoles dpl.plot(f.ax['dpl'], xlim, 'L2') dpl.plot(f.ax['dpl_L5'], xlim, 'L5') # f.ysymmetry(f.ax['dpl']) # print dpl.max('L5', (0., -1)), dpl.max('L5', (50., -1)) # print f.ax['dpl_L5'].get_ylim() # f.ax['dpl_L5'].set_ylim((-1e5, 1e5)) # f.ysymmetry(f.ax['dpl_L5']) # plot the current I_soma = currentfn.SynapticCurrent(f_current) I_soma.plot_to_axis(f.ax['I_soma'], 'L2') I_soma.plot_to_axis(f.ax['I_soma_L5'], 'L5') # plot the dipole-based spec data pc = specfn.pspec_ax(f.ax['spec_dpl'], f_spec, xlim, 'L2') f.f.colorbar(pc, ax=f.ax['spec_dpl']) pc = specfn.pspec_ax(f.ax['spec_dpl_L5'], f_spec, xlim, 'L5') f.f.colorbar(pc, ax=f.ax['spec_dpl_L5']) # grab the current spec and plot them spec_L2, spec_L5 = data_spec_current = specfn.read(f_spec_current, type='current') pc_L2 = f.ax['spec_I'].imshow(spec_L2['TFR'], aspect='auto', origin='upper',cmap=plt.get_cmap(spec_cmap)) pc_L5 = f.ax['spec_I_L5'].imshow(spec_L5['TFR'], aspect='auto', origin='upper',cmap=plt.get_cmap(spec_cmap)) # plot the current-based spec data # pci = specfn.pspec_ax(f.ax['spec_I'], f_spec_current, type='current') f.f.colorbar(pc_L2, ax=f.ax['spec_I']) f.f.colorbar(pc_L5, ax=f.ax['spec_I_L5']) # get all spikes s = spikefn.spikes_from_file(f_param, f_spk) # these work primarily because of how the keys are done # in the spike dict s (consequence of spikefn.spikes_from_file()) s_L2 = spikefn.filter_spike_dict(s, 'L2_') s_L5 = spikefn.filter_spike_dict(s, 'L5_') # resize xlim based on our 50 ms cutoff thingy xlim = (50., xlim[1]) # plot the spikes spikefn.spike_png(f.ax['spk'], s_L2) spikefn.spike_png(f.ax['spk_L5'], s_L5) f.ax['dpl'].set_xlim(xlim) # f.ax['dpl_L5'].set_xlim(xlim) # f.ax['spec_dpl'].set_xlim(xlim) f.ax['spk'].set_xlim(xlim) f.ax['spk_L5'].set_xlim(xlim) f.savepng(fname) f.close() return 0
def pspec_with_hist(f_spec, f_dpl, f_spk, dfig, f_param, key_types, xlim=None, ylim=None): # Generate file prefix # print('f_spec:',f_spec) fprefix = f_spec.split('/')[-1].split('.')[0] # Create the fig name fig_name = os.path.join(dfig, fprefix + '.png') # print('fig_name:',fig_name) # load param dict _, p_dict = paramrw.read(f_param) f = ac.FigSpecWithHist() # load spec data spec = specfn.Spec(f_spec) # Plot TFR data and add colorbar pc = spec.plot_TFR(f.ax['spec'], 'agg', xlim, ylim) f.f.colorbar(pc, ax=f.ax['spec']) # set xlim based on TFR plot xlim_new = f.ax['spec'].get_xlim() # grab the dipole data dpl = dipolefn.Dipole(f_dpl) dpl.baseline_renormalize(f_param) dpl.convert_fAm_to_nAm() # plot routine dpl.plot(f.ax['dipole'], xlim_new, 'agg') # data_dipole = np.loadtxt(open(f_dpl, 'r')) # t_dpl = data_dipole[xmin_ind:xmax_ind+1, 0] # dp_total = data_dipole[xmin_ind:xmax_ind+1, 1] # f.ax['dipole'].plot(t_dpl, dp_total) # x = (xmin, xmax) # # grab alpha feed data. spikes_from_file() from spikefn.py # s_dict = spikefn.spikes_from_file(f_param, f_spk) # # check for existance of alpha feed keys in s_dict. # s_dict = spikefn.alpha_feed_verify(s_dict, p_dict) # # Account for possible delays # s_dict = spikefn.add_delay_times(s_dict, p_dict) # Get extinput data and account for delays extinputs = spikefn.ExtInputs(f_spk, f_param) extinputs.add_delay_times() extinputs.get_envelope(dpl.t, feed='dist') # set number of bins (150 bins per 1000ms) bins = ceil(150. * (xlim_new[1] - xlim_new[0]) / 1000.) # bins should be int # plot histograms hist = {} hist['feed_prox'] = extinputs.plot_hist(f.ax['feed_prox'], 'prox', dpl.t, bins=bins, xlim=xlim_new, color='red') hist['feed_dist'] = extinputs.plot_hist(f.ax['feed_dist'], 'dist', dpl.t, bins=bins, xlim=xlim_new, color='green') f.ax['feed_dist'].invert_yaxis() # for now, set the xlim for the other one, force it! f.ax['dipole'].set_xlim(xlim_new) f.ax['spec'].set_xlim(xlim_new) f.ax['feed_prox'].set_xlim(xlim_new) f.ax['feed_dist'].set_xlim(xlim_new) # set hist axis props f.set_hist_props(hist) # axis labels f.ax['spec'].set_xlabel('Time (ms)') f.ax['spec'].set_ylabel('Frequency (Hz)') # Add legend to histogram for key in f.ax.keys(): if 'feed' in key: f.ax[key].legend() # create title title_str = ac.create_title(p_dict, key_types) f.f.suptitle(title_str) f.savepng(fig_name) f.close()
def pspec_dpl(f_spec, f_dpl, dfig, p_dict, key_types, xlim=None, ylim=None, f_param=None): # Generate file prefix fprefix = f_spec.split('/')[-1].split('.')[0] # using png for now # fig_name = os.path.join(dfig, fprefix+'.eps') fig_name = os.path.join(dfig, fprefix + '.png') # f.f is the figure handle! f = ac.FigSpec() # load spec data spec = specfn.Spec(f_spec) # Plot TFR data and add colorbar pc = spec.plot_TFR(f.ax['spec'], 'agg', xlim, ylim) f.f.colorbar(pc, ax=f.ax['spec']) # grab the dipole data # data_dipole = np.loadtxt(open(f_dpl, 'r')) dpl = dipolefn.Dipole(f_dpl) # If f_param supplied, renormalize dipole data if f_param: dpl.baseline_renormalize(f_param) dpl.convert_fAm_to_nAm() # plot routine dpl.plot(f.ax['dipole'], xlim, 'agg') # Plot Welch data # Use try/except for backwards compatibility try: spec.plot_pgram(f.ax['pgram']) except KeyError: pgram = specfn.Welch(dpl.t, dpl.dpl['agg'], p_dict['dt']) pgram.plot_to_ax(f.ax['pgram'], spec.spec['agg']['f'][-1]) # plot and create an xlim xlim_new = f.ax['spec'].get_xlim() xticks = f.ax['spec'].get_xticks() xticks[0] = xlim_new[0] # for now, set the xlim for the other one, force it! f.ax['dipole'].set_xlim(xlim_new) f.ax['dipole'].set_xticks(xticks) f.ax['spec'].set_xticks(xticks) # axis labels f.ax['spec'].set_xlabel('Time (ms)') f.ax['spec'].set_ylabel('Frequency (Hz)') # create title title_str = ac.create_title(p_dict, key_types) f.f.suptitle(title_str) # use our fig classes to save and close f.savepng(fig_name) # f.saveeps(dfig, fprefix) f.close()
def aggregate_with_hist(f, ax, f_spec, f_dpl, f_spk, f_param): # load param dict _, p_dict = paramrw.read(f_param) # load spec data from file spec = specfn.Spec(f_spec) # data_spec = np.load(f_spec) # timevec = data_spec['time'] # freqvec = data_spec['freq'] # TFR = data_spec['TFR'] xmin = timevec[0] xmax = p_dict['tstop'] x = (xmin, xmax) pc = spec.plot_TFR(ax['spec'], layer='agg', xlim=x) # pc = ax['spec'].imshow(TFR, extent=[timevec[0], timevec[-1], freqvec[-1], freqvec[0]], aspect='auto', origin='upper') f.f.colorbar(pc, ax=ax['spec'], norm=plt.colors.Normalize(vmin=0, vmax=90000), cmap=plt.get_cmap('jet')) # grab the dipole data dpl = dipolefn.Dipole(f_dpl) dpl.plot(ax['dipole'], x, layer='agg') # data_dipole = np.loadtxt(open(f_dpl, 'r')) # t_dpl = data_dipole[xmin/p_dict['dt']:, 0] # dp_total = data_dipole[xmin/p_dict['dt']:, 1] # ax['dipole'].plot(t_dpl, dp_total) # grab alpha feed data. spikes_from_file() from spikefn.py s_dict = spikefn.spikes_from_file(f_param, f_spk) # check for existance of alpha feed keys in s_dict. s_dict = spikefn.alpha_feed_verify(s_dict, p_dict) # Account for possible delays s_dict = spikefn.add_delay_times(s_dict, p_dict) # set number of bins (150 bins/1000ms) bins = 150. * (xmax - xmin) / 1000. hist = {} # Proximal feed hist['feed_prox'] = ax['feed_prox'].hist( s_dict['alpha_feed_prox'].spike_list, bins, range=[xmin, xmax], color='red', label='Proximal feed') # Distal feed hist['feed_dist'] = ax['feed_dist'].hist( s_dict['alpha_feed_dist'].spike_list, bins, range=[xmin, xmax], color='green', label='Distal feed') # for now, set the xlim for the other one, force it! ax['dipole'].set_xlim(x) ax['spec'].set_xlim(x) ax['feed_prox'].set_xlim(x) ax['feed_dist'].set_xlim(x) # set hist axis props f.set_hist_props(ax, hist) # axis labels ax['spec'].set_xlabel('Time (ms)') ax['spec'].set_ylabel('Frequency (Hz)') # Add legend to histogram for key in ax.keys(): if 'feed' in key: ax[key].legend()
def exec_avgtrials(ddata, datatype): # create the relevant key for the data datakey = 'raw' + datatype datakey_avg = 'avg' + datatype # assumes N_Trials are the same in both p_exp = paramrw.ExpParams(ddata.fparam) sim_prefix = p_exp.sim_prefix N_trials = p_exp.N_trials # fix for N_trials=0 if not N_trials: N_trials = 1 # prefix strings exp_prefix_str = p_exp.exp_prefix_str trial_prefix_str = p_exp.trial_prefix_str # Averaging must be done per expmt for expmt_group in ddata.expmt_groups: ddatatype = ddata.dfig[expmt_group][datakey] dparam = ddata.dfig[expmt_group]['param'] param_list = ddata.file_match(expmt_group, 'param') rawdata_list = ddata.file_match(expmt_group, datakey) # if nothing in the raw data list, then generate it for spec if datakey == 'rawspec': if not len(rawdata_list): # generate the data! exec_spec_regenerate(ddata) rawdata_list = ddata.file_match(expmt_group, datakey) # simple length check, but will proceed bluntly anyway. # this will result in truncated lists, per zip function if len(param_list) != len(rawdata_list): print "warning, some weirdness detected in list length in exec_avgtrials. Check yo' lengths!" # number of unique simulations, per trial # this had better be equivalent as an integer or a float! N_unique = len(param_list) / N_trials # go through the unique simulations for i in range(N_unique): # fills in the correct int for the experimental prefix string formatter 'exp_prefix_str' prefix_unique = exp_prefix_str % i fprefix_long = os.path.join(ddatatype, prefix_unique) fprefix_long_param = os.path.join(dparam, prefix_unique) # create the sublist of just these trials unique_list = [rawdatafile for rawdatafile in rawdata_list if rawdatafile.startswith(fprefix_long)] unique_param_list = [pfile for pfile in param_list if pfile.startswith(fprefix_long_param)] # one filename per unique # length of the unique list is the number of trials for this sim, should match N_trials fname_unique = ddata.create_filename(expmt_group, datakey_avg, prefix_unique) # Average data for each trial # average dipole data if datakey == 'rawdpl': for f_dpl, f_param in zip(unique_list, unique_param_list): dpl = dipolefn.Dipole(f_dpl) # dpl = dipolefn.Dipole(f_dpl, f_param) # ah, this is required becaused the dpl *file* still contains the raw, un-normalized data dpl.baseline_renormalize(f_param) # initialize and use x_dpl if f_dpl is unique_list[0]: # assume time vec stays the same throughout t_vec = dpl.t x_dpl_agg = dpl.dpl['agg'] x_dpl_L2 = dpl.dpl['L2'] x_dpl_L5 = dpl.dpl['L5'] else: x_dpl_agg += dpl.dpl['agg'] x_dpl_L2 += dpl.dpl['L2'] x_dpl_L5 += dpl.dpl['L5'] # poor man's mean x_dpl_agg /= len(unique_list) x_dpl_L2 /= len(unique_list) x_dpl_L5 /= len(unique_list) # write this data to the file # np.savetxt(fname_unique, avg_data, '%5.4f') with open(fname_unique, 'w') as f: for t, x_agg, x_L2, x_L5 in zip(t_vec, x_dpl_agg, x_dpl_L2, x_dpl_L5): f.write("%03.3f\t%5.4f\t%5.4f\t%5.4f\n" % (t, x_agg, x_L2, x_L5)) # average spec data elif datakey == 'rawspec': specfn.average(fname_unique, unique_list)
def exec_phaselock(ddata, opts): p = { 't_interval': [50, 1000], 'f_max': 60., } args_check(p, opts) # Do this per expmt group for expmt_group in ddata.expmt_groups: # Get paths to relevant files list_dpl = ddata.file_match(expmt_group, 'rawdpl') list_spk = ddata.file_match(expmt_group, 'rawspk') list_param = ddata.file_match(expmt_group, 'param') avg_spec = ddata.file_match(expmt_group, 'avgspec')[0] tmp_array_dpl = [] tmp_array_spk = [] for f_dpl, f_spk, f_param in zip(list_dpl, list_spk, list_param): # load Dpl data, do stuff, and store it print f_dpl dpl = dipolefn.Dipole(f_dpl) dpl.baseline_renormalize(f_param) dpl.convert_fAm_to_nAm() t, dp = dpl.truncate_ext(p['t_interval'][0], p['t_interval'][1]) dp = dp['agg'] tmp_array_dpl.append(dp) # Load extinput data, do stuff, and store it try: extinput = spikefn.ExtInputs(f_spk, f_param) except ValueError: print("Error: could not load spike timings from %s" % f_spk) return extinput.add_delay_times() extinput.get_envelope(dpl.t, feed='dist', bins=150) inputs, t = extinput.truncate_ext('env', p['t_interval']) tmp_array_spk.append(inputs) # Convert tmp arrays (actually lists) to numpy nd arrays array_dpl = np.array(tmp_array_dpl) array_spk = np.array(tmp_array_spk) # Phase-locking analysis phase = specfn.PhaseLock(array_dpl, array_spk, list_param[0], p['f_max']) fname_d = os.path.join(ddata.dsim, expmt_group, 'phaselock-%iHz.npz' %p['f_max']) np.savez_compressed(fname_d, t=phase.data['t'], f=phase.data['f'], B=phase.data['B']) # Plotting # Should be moved elsewhere avg_dpl = np.mean(array_dpl, axis=0) avg_spk = np.mean(array_spk, axis=0) f = ac.FigPhase() extent_xy = [t[0], t[-1], phase.data['f'][-1], 0] pc1 = f.ax['phase'].imshow(phase.data['B'], extent=extent_xy, aspect='auto', origin='upper',cmap=plt.get_cmap('jet')) pc1.set_clim([0, 1]) cb1 = f.f.colorbar(pc1, ax=f.ax['phase']) # cb1.set_clim([0, 1]) spec = specfn.Spec(avg_spec) pc2 = spec.plot_TFR(f.ax['spec'], xlim=[t[0], t[-1]]) pc2.set_clim([0, 3.8e-7]) cb2 = f.f.colorbar(pc2, ax=f.ax['spec']) # cb2.set_clim([0, 3.6e-7]) f.ax['dipole'].plot(t, avg_dpl) f.ax['dipole'].set_xlim([t[0], t[-1]]) f.ax['dipole'].set_ylim([-0.0015, 0.0015]) f.ax['input'].plot(t, avg_spk) f.ax['input'].set_xlim([t[0], t[-1]]) f.ax['input'].set_ylim([-1, 5]) f.ax['input'].invert_yaxis() f.ax['phase'].set_xlabel('Time (ms)') f.ax['phase'].set_ylabel('Frequency (Hz)') fname = os.path.join(ddata.dsim, expmt_group, 'phaselock-%iHz.png' %p['f_max']) print fname f.savepng(fname)