Example #1
0
def main():

    fs = 48e3
    cf = cochlea.get_nearest_cf_holmberg2007(1e3)

    ### Make sound
    sound = wv.ramped_tone(
        fs=fs,
        freq=cf,
        duration=150e-3,
        pad=10e-3,
        dbspl=70,
    )

    ### Run model
    anf_trains = sg.run_holmberg2007_sg(
        sound,
        fs,
        cf=cf,
        anf_num=(10, 0, 0),
        seed=0,
    )

    print(th.firing_rate(anf_trains))

    ### Plot auditory nerve response
    fig, ax = plt.subplots(2, 1)
    th.plot_signal(signal=sound, fs=fs, ax=ax[0])

    th.plot_raster(anf_trains, ax=ax[1])

    plt.show()
Example #2
0
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 main():

    fs = 48e3
    tmax = 0.1

    ### Make sound
    t = np.arange(0, tmax, 1/fs)
    s = np.zeros_like(t)


    ### Run model
    vesicle_trains = cochlea.run_holmberg2007_vesicles(
        s,
        fs,
        anf_num=(1,0,0),
        seed=0,
    )



    print(vesicle_trains)

    ### Need to rename a column: vesicles -> spikes, because that's
    ### the expected name by many functions in thorns
    trains = vesicle_trains.rename(columns={'vesicles': 'spikes'})

    ### Calculate average rate
    rate = th.firing_rate(trains)
    print()
    print("Spontanious rate:", rate)


    ### Raster plot
    th.plot_raster(trains)
    th.show()
def main():

    fs = 48e3
    tmax = 0.1

    ### Make sound
    t = np.arange(0, tmax, 1/fs)
    s = np.zeros_like(t)


    ### Run model
    vesicle_trains = cochlea.run_holmberg2007_vesicles(
        s,
        fs,
        anf_num=(1,0,0),
        seed=0,
    )



    print(vesicle_trains)

    ### Need to rename a column: vesicles -> spikes, because that's
    ### the expected name by many functions in thorns
    trains = vesicle_trains.rename(columns={'vesicles': 'spikes'})

    ### Calculate average rate
    rate = th.firing_rate(trains)
    print()
    print("Spontanious rate:", rate)


    ### Raster plot
    th.plot_raster(trains)
    th.show()
Example #5
0
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
Example #6
0
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
Example #7
0
def test_firing_rate():

    trains = th.make_trains(
        [[0.1, 0.4],
         [0.4, 0.5, 0.6]],
        duration=1
    )

    rate = th.firing_rate(
        trains
    )

    assert_equal(rate, 2.5)
Example #8
0
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
Example #9
0
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
Example #10
0
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
'''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')