3 PaI@sldZddlZddlZddlZejjdddlm Z ddl m Z ddl mZddlmZGdd d eZdS) zP This file defines the Sim class for simulating the Drosophila AL December 2019 Nz..)load_door_data)params)make_PSC) set_paramsc@seZdZdZd+ddZddZd d Zd d Zd dZd,ddZ ddZ ddZ d-ddZ d.ddZ d/ddZd0ddZdd ggfd!d"Zd#d$Zd%d&Zd1d(d)Zd*S)2SimzY Class used for storing information and executing actions for AL simulation -C6?c Cs||_i|_||_||_||_t|j|j|_tjd|j|j|_ t |\|_ |_ |_ |_||_||_||ddkdjjj}||ddkdjjj} tj|| |_t|j|_|j||j |j jj|j|j jj|jj@jjdjdj|j|_ |j j!j"dj|_#|j d jj"dj$d d d d j|_%tj&|j'|jf|_(g|_)g|_*tj&|j'|jf|_+tj,|j(|_-dS)aZ Initialize a simulation with a model name (model_name), glomerulus + neuron physiological parameters (params), connectivity strength (connect_params), end time in seconds (end_time), timestep in seconds (sim_dt), path to home directory (where ALModel/ is) raltypeORNglomuPNcode model_glom?receptorcSsdj|ddddddS)Nz{} ({})rr)format)xr..\utils\simulation_class.pyRszSim.__init__..r)axisN).rZconnect_params model_nameend_timedtintNstepsnparangetimerZdf_door_mappingsZ df_door_odorZdf_door_response_matrixdf_odor_response_matrix df_neur_ids al_block_df value_countsindexvalues intersect1d glom_nameslenZngloms _set_paramsrisincolumns reset_index set_index rename_axisreindexglom_dfrfillnaZglom_receptorsapplyZ glom_labelszerosnORNs odor_stimulus odor_listZall_odor_door_responses Iin_ratescopyIin_PSCs) selfrr"Zal_blockrrZsim_dthome_dirZ orn_glomsZpn_glomsrrr__init__s8   &z Sim.__init__cCsdj|jdj|j|jS)z. Print some model information z Model: {} zSimulation time: {} s, dt: {} s)rrrr)r;rrr__str__[s z Sim.__str__cCst|S)N)str)r;rrr__repr__bsz Sim.__repr__cs~tjfddtj|jD}|j||_|j|_t|j|_t|j|_ d|j |j<d|j |j<d|j j |jdf<dS) z Given an array of positions of the eLN set in the LNs (i.e. values from 0 to self.nLNs), stores the positions of iLNs/eLNs csg|]}|kr|qSrr).0r)eln_setrr lsz Sim.set_eLNs..iLNeLNrpolarityNr) rarrayrnLNsLNposiLNposeLNposr)niLNsneLNs neur_typesr"loc)r;rBZiln_setr)rBrset_eLNsfs       z Sim.set_eLNscCsxt|||j|_tjg|_t|j|_t|j|_tjdg|j |_ d|j |j <d|j |j<d|j j |jdf<dS)zG Simply calls set_params(self, params) from data_utils r PNrDrrFNr)rrIrJrrGrKr)rLrMnALrNPNposr"rO)r;rrrrr*ws      zSim._set_paramsTcCstj|j|jdkr6tdj|j|j|jfdS|r|j|jjdkjj }|j|jjdkjj }tj ||ddf||ddf<tj ||ddf ||ddf<||_ dS)z Given a connectivity matrix of size nAL * nAL, first checks that dimensions are correct, then checks that signs are correct (ORNs/eLNs/PNs excitatory, iLNs inhibitory), then stores it as self.connect z/shape of connect is {}x{}, but should be {}x{}!rNrrr) rprodshaperRprintrr"rFr%r&absconnect)r;rYZ check_signsZneurons_pos_polarityZneurons_neg_polarityrrr set_connects"$zSim.set_connectcCsdd|jDS)zG Returns list of odors (each added through add_odor()) cSsg|] }|dqS)rr)rArrrrrCsz&Sim.get_odor_names..)r7)r;rrrget_odor_namesszSim.get_odor_namescCs`g}xV|jD]L}|\}}}tj|j|kdd}tj|j|kdd}|j||gq W|S)zE For each odor in odor_list, return the time indices r)r7rwherer append)r;tsrow odor_name odor_startodor_endodor_start_pos odor_end_posrrrget_odor_positionss  zSim.get_odor_positionsrc Csj|j|}|\}}}tj|j|kdd}tj|j|kdd}||} |dd||fjd| S)z Given a (num_neurons x num_timepoints) Spike array, and an odor of interest (odor_index), finds the relevant time points in the Spike array to give firing rate under that odor for all neurons rNr)r7rr]r sum) r;Spikes odor_indexZ odor_inforarbrcrdreodor_durrrrget_odor_firing_ratess   zSim.get_odor_firing_ratesc Cs|j|\}}}|dkrd}n|j|dd}tj|j|kdd}tj|j|kdd}||} |dd||fjd| S)z Given a (num_neurons x num_timepoints) Spike array, and an odor of interest (odor_index), finds the relevant time points in the Spike array to give firing rate BEFORE that odor for all neurons rrrTN)r7rr]r rg) r;rhrirarbrcZ pause_startZpause_start_posrd odor_pauserrrget_pre_odor_firing_ratesszSim.get_pre_odor_firing_ratescCs |j||}||j||jfS)z Given a (num_neurons x num_timepoints) Spike array, and an odor of interest (odor_index), finds the relevant time points in the Spike array to give firing rate under that odor for ORNs and PNs only )rkORNposrS)r;rhriZodor_firing_ratesrrrget_ORN_PN_firing_ratess zSim.get_ORN_PN_firing_ratescCs:|j||}tj||jtj||jtj||jfS)z Given a (num_neurons x num_timepoints) Spike array, and an odor of interest (odor_index), finds the relevant time points in the Spike array to give firing rate under that odor for ORNs and PNs only )rkrmeanrnrIrS)r;rhriZratesrrr"get_ORN_PN_firing_rates_mean_classs z&Sim.get_ORN_PN_firing_rates_mean_classg _Bg?cCs|dkrtdd S||jkr4tdj||jdS||jkrTtdj||jdSt|dkr|jj|jdj}|jjj |ddj d |j d} t|dkr|j |j |fjd } n|} |j|jjd kj tj| d | jjdd |j} tj||j||jjt} xpt|jD]b} | | } | | d|j}| dkrj|| d|| tj| |}n| }||j| | f<qW|j|jj |||g| | | fS)z Given common name of odor (odor)name), looks it up in the DoOR response table (already stored in this class), finds the glomerular responses, adds it to odor_stimulus rz"odor start ({:.2f} s) is negative!rz:odor start ({:.2f} s) is after simulation time ({:.2f} s)!z8odor end ({:.2f} s) is after simulation time ({:.2f} s)!rleft)howrr r )left_onright_onrsrrr)!rWrrr)r!Tr/r-r1merger.r2rOr(r"r pd DataFramer%namer&rrrastyperranger5expr6 set_Iin_ratesr7r^)r;rarbrctcZfadaptZmanual_odor_glom_responsesZimputed_glom_responsesZ or_responseZodor_glom_responsesZodor_orn_responsesZ odor_timesimax_valZt_arrZ exp_decayrrradd_odorsT           &z Sim.add_odorcCs,|j|j}tjjdd|j}||kjtS)z Once rates are established with self.set_Iin_rates(), actually draw instances of spikes, for use in make_Iin() rr)r8rrrandomuniformrVr{r)r;ZIin_rates_per_dtZran_valsrrr draw_Iin_PSCs,s zSim.draw_Iin_PSCscCs>|jd|jd|j|j|jddf<d|j|jdk<dS)z Given baseline ORN rate and the odor stimulus, sets the input current firing rates Should be run at least after the last odor. Z spon_fr_ORNZ odor_rate_maxNr)rr6r8rn)r;rrrr~6s "zSim.set_Iin_ratesFc s8fddtjD}tdd|D}fddtjD}tjjj|f}j}xtjD]}||}t|} t ||ddfdkrtj ||ddfdkd} n||ddf} xJtt| D]:} | | tj | } |j || f|j d7<qWqnWtj dj|j_|S) zp Generates input current array for the ORNs based on the rates set by the odor stimulus csg|]}t|qSr)r)rAr)r;rrrCMsz Sim.make_Iin..cSsg|] }t|qSr)r))rAZPSCrrrrCPscsg|]}t|qSr)r)rAr) is_input_PSCr;rrrCUsNrrZ PSCweight_ORN)r|rRmaxr5rr4rrr)minr]rrnrrr ) r;rZall_PSCsZ max_tBuffZorn_input_PSCsZIinr:rZPSCiZtBuffiZ pscFallTimesjZPSCranger)rr;rmake_IinFs  ,z Sim.make_IinN)rrr r)T)r)r)r)r)F)__name__ __module__ __qualname____doc__r=r>r@rPr*rZr\rfrkrmrorqrrr~rrrrrrs* A     B r)rnumpyrsyspandasrxpathr^Zutils.odor_utilsrutils.model_paramsrdefault_paramsutils.LIF_funcs_cythonrutils.data_utilsrobjectrrrrrs     k