def _run_model((model, cf, fs, freq, dbspl, kwargs)): print os.getpid(), freq, dbspl tmax = 250 onset = 30 ear = model((1000, 1000, 1000), cf=cf, **kwargs) # ear = model((250, 0, 0), cf=cf, # powerlaw_implnt='approx', # with_ffGn=False) s = wv.generate_ramped_tone(fs, freq, tone_duration=tmax, pad_duration=0, dbspl=dbspl) anf = ear.run(fs, s) hsr = anf.where(typ='hsr') hsr = th.trim(hsr, onset) hsr_rate = th.calc_rate(hsr, stimulus_duration=(tmax - onset)) msr = anf.where(typ='msr') msr = th.trim(msr, onset) msr_rate = th.calc_rate(msr, stimulus_duration=(tmax - onset)) lsr = anf.where(typ='lsr') lsr = th.trim(lsr, onset) lsr_rate = th.calc_rate(lsr, stimulus_duration=(tmax - onset)) return freq, dbspl, hsr_rate, msr_rate, lsr_rate
def _run_model( (model, cf, fs, freq, dbspl, kwargs) ): print os.getpid(), freq, dbspl tmax = 250 onset = 30 ear = model((1000, 1000, 1000), cf=cf, **kwargs) # ear = model((250, 0, 0), cf=cf, # powerlaw_implnt='approx', # with_ffGn=False) s = wv.generate_ramped_tone(fs, freq, tone_duration=tmax, pad_duration=0, dbspl=dbspl) anf = ear.run(fs, s) hsr = anf.where(typ='hsr') hsr = th.trim(hsr, onset) hsr_rate = th.calc_rate(hsr, stimulus_duration=(tmax-onset)) msr = anf.where(typ='msr') msr = th.trim(msr, onset) msr_rate = th.calc_rate(msr, stimulus_duration=(tmax-onset)) lsr = anf.where(typ='lsr') lsr = th.trim(lsr, onset) lsr_rate = th.calc_rate(lsr, stimulus_duration=(tmax-onset)) return freq, dbspl, hsr_rate, msr_rate, lsr_rate
def _run_model(model, dbspl, cf, model_pars): duration = 100e-3 onset = 10e-3 fs = model_pars.setdefault('fs', 100e3) model_pars.setdefault('anf_num', (250,250,250)) model_pars.setdefault('seed', 0) sound = wv.ramped_tone( fs=fs, freq=cf, duration=duration, pad=0, dbspl=dbspl ) anf = model( sound=sound, cf=cf, **model_pars ) ### We want to make sure the the output CF is equal to the desired ### CF. real_cf, = np.unique(anf['cf']) assert real_cf == cf hsr = anf[anf['type']=='hsr'] hsr = th.trim(hsr, onset, None) si_hsr = th.vector_strength(hsr, cf) msr = anf[anf['type']=='msr'] msr = th.trim(msr, onset, None) si_msr = th.vector_strength(msr, cf) lsr = anf[anf['type']=='lsr'] lsr = th.trim(lsr, onset, None) si_lsr = th.vector_strength(lsr, cf) # print(si_hsr) # th.plot_raster(anf) # th.show() vss = { 'hsr': si_hsr, 'msr': si_msr, 'lsr': si_lsr, } return vss
def _run_model(model, fm, cf, dbspl, model_pars, m): duration = 0.6 onset = 10e-3 fs = model_pars.setdefault('fs', 100e3) model_pars.setdefault('anf_num', (250, 0, 0)) model_pars.setdefault('seed', 0) sound = wv.amplitude_modulated_tone( fs=fs, fm=fm, fc=cf, m=m, duration=duration, dbspl=dbspl, ) anf = model(sound=sound, cf=cf, **model_pars) trimmed = th.trim(anf, onset) vs = th.vector_strength(trimmed, fm) gain = 20 * np.log10(2 * vs / m) return gain
def _run_model(model, dbspl, cf, model_pars, tone_duration): onset = 10e-3 assert tone_duration > onset fs = model_pars.setdefault('fs', 100e3) model_pars.setdefault('anf_num', (250,250,250)) model_pars.setdefault('seed', 0) sound = wv.ramped_tone( fs=fs, freq=cf, duration=tone_duration, pad=0, dbspl=dbspl ) anf = model( sound=sound, cf=cf, **model_pars ) rates = {} for typ,trains in anf.groupby('type'): trimmed = th.trim(trains, onset, None) rate = th.firing_rate(trimmed) rates[typ] = rate return rates
def error_func(dbspl, model, cf, model_pars, spont_si): if model_pars is None: model_pars = {} pars = dict(model_pars) fs = pars.setdefault('fs', 100e3) tone_duration = 250e-3 onset = 15e-3 s = wv.make_ramped_tone(fs, cf, duration=tone_duration, ramp=2.5e-3, pad=0, dbspl=dbspl) anf = model(sound=s, anf_num=(1000, 0, 0), cf=cf, seed=0, **pars) trains = th.trim(anf, onset, None) si = th.calc_si(trains, cf) error = si - spont_si logging.debug("{} {} {}".format(dbspl, si, error)) return error
def _run_model(model, dbspl, cf, model_pars): duration = 100e-3 onset = 10e-3 fs = model_pars.setdefault('fs', 100e3) model_pars.setdefault('anf_num', (250, 250, 250)) model_pars.setdefault('seed', 0) sound = wv.ramped_tone(fs=fs, freq=cf, duration=duration, pad=0, dbspl=dbspl) anf = model(sound=sound, cf=cf, **model_pars) # th.plot_raster(anf) # th.show() # We want to make sure the the output CF is equal to the desired # CF. real_cf, = np.unique(anf['cf']) assert real_cf == cf vss = {} for typ, group in anf.groupby('type'): trimmed = th.trim(group, onset, None) vs = th.vector_strength(trimmed, cf) vss[typ] = vs return vss
def _run_model(model, fm, cf, dbspl, model_pars, m): duration = 0.6 onset = 10e-3 fs = model_pars.setdefault('fs', 100e3) model_pars.setdefault('anf_num', (250, 0, 0)) model_pars.setdefault('seed', 0) sound = wv.amplitude_modulated_tone( fs=fs, fm=fm, fc=cf, m=m, duration=duration, dbspl=dbspl, ) anf = model( sound=sound, cf=cf, **model_pars ) trimmed = th.trim(anf, onset) vs = th.vector_strength(trimmed, fm) gain = 20 * np.log10(2*vs / m) return gain
def main(): ### Load spike trains spike_trains = load_anf_zilany2014() print(spike_trains.head()) ### Calculate vector strength cf, = spike_trains.cf.unique() onset = 10e-3 # ms trimmed = th.trim(spike_trains, onset, None) vs = th.vector_strength(trimmed, freq=cf) print() print("Vector strength: {}".format(vs)) ### Plot raster plot th.plot_raster(spike_trains) ### Show the plot th.show() # Equivalent to plt.show()
def test_trim_without_stop(): trains = th.make_trains([[1, 2], [2, 3]], duration=10, cf=1e3) trimmed = th.trim(trains, 2) expected = th.make_trains([[0], [0, 1]], duration=8, cf=1e3) assert_frame_equal(trimmed, expected)
def test_trim(): trains = th.make_trains([[1, 2], [2, 3]], duration=10, cf=1e3) trimmed = th.trim(trains, 1.5, 2.5) expected = th.make_trains([[0.5], [0.5]], duration=1, cf=1e3) assert_frame_equal(trimmed, expected)
def _run_model(model, dbspl, cf, model_pars, tone_duration): onset = 10e-3 assert tone_duration > onset fs = model_pars.setdefault('fs', 100e3) model_pars.setdefault('anf_num', (250,250,250)) model_pars.setdefault('seed', 0) sound = wv.ramped_tone( fs=fs, freq=cf, duration=tone_duration, pad=0, dbspl=dbspl ) anf = model( sound=sound, cf=cf, **model_pars ) ### TODO: try to use DataFrame.groupby instead hsr = anf.query("type == 'hsr'") hsr = th.trim(hsr, onset, None) rate_hsr = th.firing_rate(hsr) msr = anf.query("type == 'msr'") msr = th.trim(msr, onset, None) rate_msr = th.firing_rate(msr) lsr = anf.query("type == 'lsr'") lsr = th.trim(lsr, onset, None) rate_lsr = th.firing_rate(lsr) rates = { 'hsr': rate_hsr, 'msr': rate_msr, 'lsr': rate_lsr, } return rates
def _run_model(model, dbspl, cf, model_pars): duration = 100e-3 onset = 10e-3 fs = model_pars.setdefault('fs', 100e3) model_pars.setdefault('anf_num', (250,250,250)) model_pars.setdefault('seed', 0) sound = wv.ramped_tone( fs=fs, freq=cf, duration=duration, pad=0, dbspl=dbspl ) anf = model( sound=sound, cf=cf, **model_pars ) hsr = anf[anf['type']=='hsr'] hsr = th.trim(hsr, onset, None) rate_hsr = th.firing_rate(hsr) msr = anf[anf['type']=='msr'] msr = th.trim(msr, onset, None) rate_msr = th.firing_rate(msr) lsr = anf[anf['type']=='lsr'] lsr = th.trim(lsr, onset, None) rate_lsr = th.firing_rate(lsr) rates = { 'hsr': rate_hsr, 'msr': rate_msr, 'lsr': rate_lsr, } return rates
def test_trim(): trains = th.make_trains([[1, 2, 3, 4], [3, 4, 5, 6]], duration=8, type='hsr') trimmed = th.trim(trains, 2, 4) expected = th.make_trains([[0, 1, 2], [1, 2]], duration=2, type='hsr') assert_frame_equal(trimmed, expected)
def error_func( dbspl, model, cf, spont_rate, model_pars, asr_filter=False, freq=None ): pars = dict(model_pars) fs = pars.setdefault('fs', 100e3) pars.setdefault('seed', 0) pars.setdefault('anf_num', (1000, 0, 0)) if freq is None: freq = cf tone_duration = 250e-3 onset = 15e-3 sound = wv.ramped_tone( fs, freq, duration=tone_duration, ramp=2.5e-3, pad=0, dbspl=dbspl ) if asr_filter: sound = adjust_to_human_thresholds(sound, fs, model) anf = model( sound=sound, cf=cf, **pars ) trains = th.trim(anf, onset, None) rate = th.firing_rate(trains) error = rate - spont_rate logging.debug("{} {} {}".format(dbspl, rate, error)) return error
def test_trim_without_stop(): trains = th.make_trains( [[1, 2], [2, 3]], duration=10, cf=1e3 ) trimmed = th.trim(trains, 2) expected = th.make_trains( [[0], [0, 1]], duration=8, cf=1e3 ) assert_frame_equal(trimmed, expected)
def test_trim(): trains = th.make_trains( [[1, 2], [2, 3]], duration=10, cf=1e3 ) trimmed = th.trim(trains, 1.5, 2.5) expected = th.make_trains( [[0.5], [0.5]], duration=1, cf=1e3 ) assert_frame_equal(trimmed, expected)
def error_func(dbspl, model, cf, spont_rate, model_pars, asr_filter=False, freq=None): pars = dict(model_pars) fs = pars.setdefault('fs', 100e3) pars.setdefault('seed', 0) pars.setdefault('anf_num', (1000, 0, 0)) if freq is None: freq = cf tone_duration = 250e-3 onset = 15e-3 sound = wv.ramped_tone(fs, freq, duration=tone_duration, ramp=2.5e-3, pad=0, dbspl=dbspl) if asr_filter: sound = adjust_to_human_thresholds(sound, fs, model) anf = model(sound=sound, cf=cf, **pars) trains = th.trim(anf, onset, None) rate = th.firing_rate(trains) error = rate - spont_rate logging.debug("{} {} {}".format(dbspl, rate, error)) return error
def _run_model(model, dbspl, cf, model_pars): duration = 100e-3 onset = 10e-3 fs = model_pars.setdefault('fs', 100e3) model_pars.setdefault('anf_num', (250,250,250)) model_pars.setdefault('seed', 0) sound = wv.ramped_tone( fs=fs, freq=cf, duration=duration, pad=0, dbspl=dbspl ) anf = model( sound=sound, cf=cf, **model_pars ) # th.plot_raster(anf) # th.show() ### We want to make sure the the output CF is equal to the desired ### CF. real_cf, = np.unique(anf['cf']) assert real_cf == cf vss = {} for typ,group in anf.groupby('type'): trimmed = th.trim(group, onset, None) vs = th.vector_strength(trimmed, cf) vss[typ] = vs return vss
def main(): # Load spike trains spike_trains = load_anf_zilany2014() print(spike_trains.head()) # Calculate vector strength cf, = spike_trains.cf.unique() onset = 10e-3 trimmed = th.trim(spike_trains, onset, None) vs = th.vector_strength(trimmed, freq=cf) print() print("Vector strength: {}".format(vs)) # Plot raster plot th.plot_raster(spike_trains) # Show the plot th.show() # Equivalent to plt.show()
def error_func(dbspl, model, cf, model_pars, spont_si): if model_pars is None: model_pars = {} pars = dict(model_pars) fs = pars.setdefault('fs', 100e3) tone_duration = 250e-3 onset = 15e-3 s = wv.make_ramped_tone( fs, cf, duration=tone_duration, ramp=2.5e-3, pad=0, dbspl=dbspl ) anf = model( sound=s, anf_num=(1000, 0, 0), cf=cf, seed=0, **pars ) trains = th.trim(anf, onset, None) si = th.calc_si(trains, cf) error = si - spont_si logging.debug("{} {} {}".format(dbspl, si, error)) return error
def _run_model(model, dbspl, cf, model_pars, tone_duration): onset = 10e-3 assert tone_duration > onset fs = model_pars.setdefault('fs', 100e3) model_pars.setdefault('anf_num', (250, 250, 250)) model_pars.setdefault('seed', 0) sound = wv.ramped_tone(fs=fs, freq=cf, duration=tone_duration, pad=0, dbspl=dbspl) anf = model(sound=sound, cf=cf, **model_pars) rates = {} for typ, trains in anf.groupby('type'): trimmed = th.trim(trains, onset, None) rate = th.firing_rate(trimmed) rates[typ] = rate return rates
def test_trim(): trains = th.make_trains( [[1,2,3,4], [3,4,5,6]], duration=8, type='hsr' ) trimmed = th.trim(trains, 2, 4) expected = th.make_trains( [[0,1,2], [1,2]], duration=2, type='hsr' ) assert_frame_equal( trimmed, expected )
'''Plot the results of the inhibitory variation script You need to previously run the inhibitory_variation.py script so that the inhibitory_variation.h5 result file is generated in the work subfolder ''' import numpy as np import matplotlib.pyplot as plt import thorns import scipy.optimize as opt res = thorns.util.loaddb('inhibitory_variation') res = res.reset_index() calc_fr = lambda x: thorns.firing_rate(thorns.trim(x, start=0.045, stop=0.145)) def gaus(x, a, x0, sigma, b): return a * np.exp(-(x - x0)**2 / (sigma**2)) + b # res['rate_left'] = res.spikes_left.apply(calc_fr) res['rate_right'] = res.spikes_right.apply(calc_fr) sel_curve = res[res.str_i == res.str_i.unique()[-2]] fig, ax = plt.subplots(1, 1) for strength, sg in res.groupby('str_i'): ax.plot(sg.itd * 1e3, sg.rate_left) ax.set_ylim(0, 90) ax.set_ylabel('Firing rate / sps')
run(duration, network) mso_train_left = thorns.make_trains(sp_mon_left) mso_train_left.duration = duration mso_train_right = thorns.make_trains(sp_mon_right) mso_train_right.duration = duration return { 'spikes_left': mso_train_left, 'spikes_right': mso_train_right, 'anf_train_left': cochlea_train_left, 'n_itd': n_itd } if __name__ == '__main__': res = run_exp(c_freq=400, sound_freq=400, itd=200e-6) trim = lambda x: thorns.trim(x, start=0.125, stop=2.125) spikes_left = trim(res['spikes_left']) spikes_right = trim(res['spikes_right']) psth_l, bins = thorns.psth(spikes_left, 30e-3) psth_r, bins = thorns.psth(spikes_right, 30e-3) fig, ax = plt.subplots(1, 1) ax.plot(bins[:-1], psth_r - psth_l) ax.set_xlabel('time / s') ax.set_ylabel('$\Delta R$ / sps')