def test_triplets_timing(): tc = OrderedDict( [ (43.0, 0.1353352832366127), (44.0, 0.24935220877729614), (45.0, 0.41111229050718745), (46.0, 0.6065306597126333), (47.0, 0.8007374029168081), (48.0, 0.9459594689067655), (49.0, 1.0), (50.0, 0.9459594689067655), (51.0, 0.8007374029168081), (52.0, 0.6065306597126333), (53.0, 0.41111229050718745), (54.0, 0.24935220877729614) ] ) #df, iti, tone_length, dt = 4, .04, .033, .001 #actual = aba_triplet(tc, df, iti=iti, tone_length=tone_length, dt=dt) stim = ABAStimulus() actual = stim.generate_triplet_tones() # we wanna check the tone length... of all 3... tone_time = int(stim.tone_length/stim.dt) iti_time = int(stim.iti/stim.dt) gap_time = iti_time - tone_time # for u_ind in xrange(actual.shape[0]): for t_ind in xrange(3): start_ind = iti_time * t_ind # check the tone length assert list(actual[start_ind:]).index(0) == tone_time # check the gap length if t_ind is not 2: assert list(actual[start_ind + tone_time:] > 0).index(True) == gap_time # check the period assert len(actual) == iti_time * 4 assert not any([np.isnan(x) for x in actual])
def test_build_stimulus_currents(): stim = ABAStimulus() # center, spread for each unit, starting at 440 Hz A and 3 semitones up from there s_units = [ Selectivity(music.key_to_frequency(49), 1, 0.6), Selectivity(music.key_to_frequency(46), 1, 0.6) ] stim = ABAStimulus(a_semitone=49, df=3) network = SynapticNetwork(selectivities=s_units, stimulus=stim) out = network.build_stimulus_currents() assert out[0][0] > out[1][0] assert out[0][50] < out[1][50]
def test_tones_init(): stim = ABAStimulus() # print(stim.__dict__) assert len(stim.tax) == len(stim.tones) == stim.ttot assert stim.tones[0] == 49 assert stim.tax[-1] == stim.T # tax[0] is not 0... assert not any([np.isnan(x) for x in stim.tones])
def test_map_tones_to_cell(): u1 = SensoryWCUnit() stimulus = ABAStimulus() u1.add_stim_current(stimulus, .5) # print(u1.currents) # assert False assert u1.stim[0] == 0 u1.currents["stim"].update() assert u1.currents["stim"].value == 1
def test_selectivity(): Selectivity = namedtuple("Selectivity", ("best_frequency", "spread", "gain")) # center, spread for each unit, starting at 440 Hz A and 3 semitones up from there s_units = [ Selectivity(music.key_to_frequency(49), 1, 0.8), Selectivity(music.key_to_frequency(52), 1, 0.8) ] stim = ABAStimulus() network = TonotopicNetwork(s_units, stim) network.update_all(10) assert network.units[0].r > network.units[1].r
def test_pars_list(): Selectivity = namedtuple("Selectivity", ("best_frequency", "spread", "gain")) pars_list = [{"tau": 100., "the": .4}, None] # center, spread for each unit, starting at 440 Hz A and 3 semitones up from there s_units = [ Selectivity(music.key_to_frequency(49), 1.1, 0.8), Selectivity(music.key_to_frequency(49), 1, 0.8) ] stim = ABAStimulus() network = TonotopicNetwork(s_units, stim, pars_list=pars_list) network.update_all(10) # print(network.units[0].__dict__) # print(network.units[1].__dict__) assert network.units[0].r < network.units[1].r
# update response self.unit.update() # update traces for trace in self.traces.values(): trace.update_trace() self.t_i += 1 @staticmethod def main(): pass if __name__ == '__main__': tic = datetime.datetime.now() u1 = SensoryWCUnit(name="u1", tauA=5000, gSFA=0.8) stim = ABAStimulus(a_semitone=44, df=9) u1.add_stim_current(stim, weight=0.5) sim = SensoryTripletsSimulation(sensory_unit=u1, T=5) sim.run() toc = datetime.datetime.now() # let's try it with seaborn and a dataframe trace_dict = {} trace_dict.update((k, v.trace) for k, v in sim.traces.items()) trace_dict['tax'] = sim.tax data = pd.DataFrame(trace_dict, index=sim.tax) \ [['tax', 'FR', 'SFA', 'stim']] plot_sensory_traces(data=data, unit=u1) # data1 = data.add(pd.Series(np.ones(len(sim.tax)), index=sim.tax))
df["tax"] = sim.tax ulst = [df] for k, v in units.items()[1:]: ndf = pd.DataFrame(v) # units.items()[0][1], index=sim.tax) ndf["unit"] = k # units.items()[0][0] ndf["tax"] = sim.tax ulst.append(ndf) df_out = pd.concat(ulst) return df_out if __name__ == '__main__': tic = datetime.datetime.now() stim = ABAStimulus() Selectivity = namedtuple("Selectivity", ("best_frequency", "spread", "gain")) # center, spread for each unit, starting at 440 Hz A and 3 semitones up from there s_units = [ Selectivity(music.key_to_frequency(49), 1, 0.6), Selectivity(music.key_to_frequency(52), 1, 0.6) ] stim = ABAStimulus(a_semitone=49, df=3) network = TonotopicNetwork(s_units, stim) sim = TonotopicTripletsSimulation(network=network) sim.run() data = sim.traces_to_df() g = sns.FacetGrid(data, col='unit', col_wrap=1) g.map_dataframe(plot_generic_traces)
""" Add a unit to the network. Args: selectivity: tuple of (best_frequency, spread, gain) best_frequency (float): Hz, e. g., 440.0 spread (float): by default, standard deviation of norm response curve in semitones away from BF gain (float): strength of the stimulus on the cell. """ best_frequency, spread, gain = selectivity name = name = 's({st}:{sp}:{g})'.format(st=int( music.frequency_to_key(best_frequency)), sp=spread, g=gain) unit = SensoryWCUnit(best_frequency=best_frequency, spread=spread, name=name, **kwargs) unit.add_stim_current(self.stimulus, weight=gain) self.units.append(unit) if __name__ == "__main__": tic = time.time() stim = ABAStimulus() network = TonotopicNetwork(s_units, stim) network.update_all(10) toc = time.time() print(toc - tic) print(network.units[0].r, network.units[1].r) print(stim.value)
def __init__(self, **kwargs): print("AB") ABAStimulus.__init__(self, **kwargs) self.tones = self.repeating_tones(self.generate_ab_interval())