Ejemplo n.º 1
0
def test_append_parameters_bad_inputs3():
    """ Test that if we provide append_parameters with an input of wrong type that we get a warning """
    try:
        si.append_parameters('x', ['y'], [1, 2, 3])
        raise Exception('This should have failed!')
    except TypeError:
        pass
Ejemplo n.º 2
0
def test_append_parameters_bad_inputs2():
    """ Test that if we provide append_parameters with an reasonable input (a list of multiple dicts) but bad inputs
     (two lists of different lengths) that it raises the right errors """
    try:
        si.append_parameters({'x': 1}, ['y'], [1, 2, 3])
        raise Exception('This should have failed!')
    except ValueError:
        pass
Ejemplo n.º 3
0
def test_append_parameters_bad_inputs1():
    """ Test that if we provide append_parameters with an reasonable input (a list of multiple dicts) but bad inputs
     ( one list and one not list) that it raises the right errors """
    try:
        si.append_parameters({'x': 1}, ['y'], 2)
        raise Exception('This should have failed!')
    except ValueError:
        return
Ejemplo n.º 4
0
def calculate_fdl_vs_dur(dur_low, dur_high, n_dur):
    """
    Calculates ideal observer FDL vs duration

    Arguments:
        dur_low (float): lowest duration to test in seconds

        dur_high (float): highest duration to test in seconds

        n_dur (int): number of durations to test... frequencies between dur_low and dur_high will be distributed
            logarithmically

    Returns:
        tuple of ndarrays, the first containing all-information thresholds, the second containing
            rate-place thresholds, and the third containing the durations at which they were estimated

    """
    # Initialize simulator object
    sim = anf.AuditoryNerveHeinz2001()

    # Define stimulus parameters
    tone_level = 40
    tone_freq = 970
    tone_ramp_dur = 0.004
    tone_durs = 10**np.linspace(np.log10(dur_low), np.log10(dur_high), n_dur)

    # Encode stimulus parameters
    params = {
        'level': tone_level,
        'freq': tone_freq,
        'dur_ramp': tone_ramp_dur
    }
    params = si.wiggle_parameters(params, 'dur', tone_durs)

    # Encode model parameters
    params = si.append_parameters(params, [
        'cf_low', 'cf_high', 'n_cf', 'fs', 'n_fiber_per_chan', 'delta_theta',
        'API'
    ], [100, 10000, 60, int(500e3), 200, [0.001],
        np.zeros(1)])

    # Encode increment and synthesize
    params = si.increment_parameters(params, {'freq': 0.001})
    synth = PureToneHeinz2001()
    stimuli = synth.synthesize_sequence(params)
    params = si.stitch_parameters(params, '_input', stimuli)

    # Run model
    output = sim.run(params,
                     parallel=True,
                     runfunc=dc.decode_ideal_observer(sim.simulate))
    t_AI = [
        x[0] for x in output
    ]  # extract AI thresholds, which are always the first element of each tuple in results
    t_RP = [
        x[1] for x in output
    ]  # extract RP thresholds, which are always the second element of each tuple in results

    # Return
    return np.array(t_AI), np.array(t_RP), tone_durs
Ejemplo n.º 5
0
def test_ideal_observer_real_simulation_with_level_roving():
    """ Test that ideal observer analysis on simple pure tone FDLs shows increasing FDLs with increasing frequency in
    the context of a mild level rove on the pure tone """
    # Initialize simulator object
    sim = anf.AuditoryNerveHeinz2001()

    # Define stimulus parameters
    fs = int(200e3)

    def tone_level():
        return np.random.uniform(25, 35, 1)

    tone_dur = 0.1
    tone_ramp_dur = 0.01
    tone_freqs = [1000, 2000, 4000, 8000]

    # Encode stimulus parameters
    params = {
        'level': tone_level,
        'dur': tone_dur,
        'dur_ramp': tone_ramp_dur,
        'fs': fs
    }
    params = si.wiggle_parameters(params, 'freq', tone_freqs)

    # Encode model parameters
    params = si.stitch_parameters(params, 'cf_low', [1000, 2000, 4000, 8000])
    params = si.stitch_parameters(params, 'cf_high', [1000, 2000, 4000, 8000])
    params = si.append_parameters(
        params, ['fs', 'n_cf', 'n_fiber_per_chan', 'delta_theta', 'API'],
        [int(200e3), 1, 5, [0.001, 0.001],
         np.array([[0, 0], [0, 1 / 6**2]])])

    # Encode repeats and increments
    params = si.repeat_parameters(params, 10)
    params = si.increment_parameters(params, {'freq': 0.001, 'level': 0.001})

    # Synthesize stimuli and encode in params
    synth = sy.PureTone()
    stimuli = synth.synthesize_sequence(params)
    params = si.stitch_parameters(params, '_input', stimuli)

    # Run model
    output = sim.run(params,
                     parallel=True,
                     runfunc=decode_ideal_observer(sim.simulate))

    # Extract AI thresholds
    output = [
        out[0] for out in output
    ]  # AI thresholds are always the first element of each tuple in output

    # Check to make sure that thresholds grow with frequency
    assert np.all(np.diff(output) > 0)
Ejemplo n.º 6
0
def test_ideal_observer_single_input():
    """ Test that if we provide a single stimulus to a ratefunc wrapped in decode_ideal_observer that some sort of
     error is raised to indicate that an ideal observer can't be calculated based on a single simulation! """
    # Initialize simulator object
    sim = anf.AuditoryNerveHeinz2001()

    # Define stimulus parameters
    fs = int(200e3)
    tone_level = 30
    tone_dur = 0.1
    tone_ramp_dur = 0.01
    tone_freq = 1000

    # Synthesize stimuli
    synth = sy.PureTone()
    params = {
        'level': tone_level,
        'dur': tone_dur,
        'dur_ramp': tone_ramp_dur,
        'freq': tone_freq,
        'fs': fs
    }
    stimuli = synth.synthesize_sequence([params])

    # Add stimuli and model params
    params = si.append_parameters(params,
                                  ['_input', 'cf_low', 'cf_high', 'n_cf'],
                                  [stimuli[0], 1000, 1000, 1])
    params = si.append_parameters(
        params, ['n_fiber_per_chan', 'fs', 'delta_theta', 'API'],
        [5, int(200e3), [0.001], np.zeros(1)])

    # Run model
    try:
        sim.run([params], runfunc=decode_ideal_observer(sim.simulate))
        raise Exception('This should have failed!')
    except ValueError:
        return
Ejemplo n.º 7
0
def test_ideal_observer_FDL_vs_frequency():
    """ Test that ideal observer analysis on simple pure tone FDLs shows increasing FDLs with increasing frequency """
    # Initialize simulator object
    sim = anf.AuditoryNerveHeinz2001()

    # Define stimulus parameters
    fs = int(200e3)
    tone_level = 30
    tone_dur = 0.1
    tone_ramp_dur = 0.01
    tone_freqs = [1000, 2000, 4000, 8000]

    # Encode stimulus information
    params = {
        'level': tone_level,
        'dur': tone_dur,
        'dur_ramp': tone_ramp_dur,
        'fs': fs
    }
    params = si.wiggle_parameters(params, 'freq', tone_freqs)

    # Encode model information
    params = si.stitch_parameters(params, 'cf_low', [1000, 2000, 4000, 8000])
    params = si.stitch_parameters(params, 'cf_high', [1000, 2000, 4000, 8000])
    params = si.append_parameters(
        params, ['n_cf', 'fs', 'n_fiber_per_chan', 'delta_theta', 'API'],
        [1, int(200e3), 5, [0.001], np.zeros((1))])

    # Flatten and increment frequency
    params = si.flatten_parameters(params)
    params = si.increment_parameters(params, {'freq': 0.001})
    synth = sy.PureTone()
    stimuli = synth.synthesize_sequence(params)
    params = si.stitch_parameters(params, '_input', stimuli)

    # Run model
    output = sim.run(params,
                     parallel=True,
                     runfunc=decode_ideal_observer(sim.simulate))

    # Extract AI thresholds
    output = [
        out[0] for out in output
    ]  # AI thresholds are always the first element of each tuple in output

    # Check to make sure that thresholds grow with frequency
    assert np.all(np.diff(output) > 0)
Ejemplo n.º 8
0
def test_append_parameters_multiple_items_to_append_array():
    """ Test that if we provide append_parameters with an reasonable input (an array of multiple dicts) that it
     correctly appends multiple parameters to each dict """
    params = np.array([{'x': 1}, {'x': 2}, {'x': 3}])
    params = si.append_parameters(params, ['y', 'z'], [2, 3])
    assert params[0]['y'] == 2 and params[1]['y'] == 2 and params[0]['z'] == 3 and params[1]['z'] == 3
Ejemplo n.º 9
0
def test_append_parameters_single_item_to_append():
    """ Test that if we provide append_parameters with an reasonable input (a list of multiple dicts) that it correctly
     appends a parameter to each dict """
    params = [{'x': 1}, {'x': 2}, {'x': 3}]
    params = si.append_parameters(params, 'y', 2)
    assert params[0]['y'] == 2 and params[1]['y'] == 2
Ejemplo n.º 10
0
def test_append_parameters_single_element_input():
    """ Test that if we provide append_parameters with an input with just one element that it still returns a list """
    assert type(si.append_parameters([{'hello': 'world'}], 'foo', 'bar')) == list