def test_Simulation_set_optic_fiber_layer(): sim = p2p.Simulation(p2p.implants.ArgusI(), engine='serial') # Invalid grid ranges with pytest.raises(ValueError): sim.set_optic_fiber_layer(x_range=[10, 0]) with pytest.raises(ValueError): sim.set_optic_fiber_layer(x_range=[1, 2, 3]) with pytest.raises(ValueError): sim.set_optic_fiber_layer(x_range='invalid') with pytest.raises(ValueError): sim.set_optic_fiber_layer(y_range=[10, 0]) with pytest.raises(ValueError): sim.set_optic_fiber_layer(y_range=[1, 2, 3]) with pytest.raises(ValueError): sim.set_optic_fiber_layer(y_range='invalid') x_range = [-100, 100] y_range = [0, 200] sim.set_optic_fiber_layer(x_range=x_range, y_range=y_range, save_data=False) npt.assert_equal(sim.ofl.gridx.min(), x_range[0]) npt.assert_equal(sim.ofl.gridx.max(), x_range[1]) npt.assert_equal(sim.ofl.gridy.min(), y_range[0]) npt.assert_equal(sim.ofl.gridy.max(), y_range[1]) npt.assert_equal(sim.ofl.range_x, np.diff(x_range)) npt.assert_equal(sim.ofl.range_x, np.diff(x_range)) # Smoke test implant = p2p.implants.ElectrodeArray('epiretinal', 10, 0, 0, 0) sim = p2p.Simulation(implant, engine='serial') sim.set_optic_fiber_layer(x_range=0, y_range=0) sim.set_optic_fiber_layer(x_range=[0, 0], y_range=[0, 0]) sim.set_optic_fiber_layer()
def test_brightness_movie(): logging.getLogger(__name__).info("test_brightness_movie()") tsample = 0.075 / 1000 tsample_out = 1.0 / 30.0 s1 = p2p.stimuli.PulseTrain(freq=20, dur=0.5, pulse_dur=.075 / 1000., interphase_dur=.075 / 1000., delay=0., tsample=tsample, amp=20, pulsetype='cathodicfirst') implant = p2p.implants.ElectrodeArray('epiretinal', [1, 1], [0, 1], [0, 1], [0, 1]) # Smoke testing, feed the same stimulus through both electrodes: sim = p2p.Simulation(implant, engine='serial') sim.set_optic_fiber_layer(x_range=[-2, 2], y_range=[-3, 3], sampling=1, save_data=False) sim.set_ganglion_cell_layer('latest', tsample=tsample) logging.getLogger(__name__).info(" - PulseTrain") sim.pulse2percept([s1, s1], t_percept=tsample_out)
def test_Simulation_pulse2percept(): implant = p2p.implants.ElectrodeArray("epiretinal", 10, 0, 0, 0) sim = p2p.Simulation(implant, engine='serial') sim.set_optic_fiber_layer(x_range=[0, 0], y_range=[0, 0]) pt = p2p.stimuli.BiphasicPulse('cathodicfirst', 0.45 / 1000, 0.005 / 1000) sim.pulse2percept(pt) sim.pulse2percept(pt, layers=['GCL']) sim.pulse2percept(pt, layers=['INL']) # PulseTrain must have the same tsample as (implicitly set up) GCL pt = p2p.stimuli.BiphasicPulse("cathodicfirst", 0.1, 0.001) with pytest.raises(ValueError): sim.pulse2percept(pt) pt = p2p.stimuli.BiphasicPulse("cathodicfirst", 0.1, 0.005 / 1000) with pytest.raises(ValueError): sim.pulse2percept(pt, layers=['GCL', 'invalid'])
def test_Simulation_set_ganglion_cell_layer(): # A valid ganglion cell model class Valid(p2p.retina.BaseModel): def model_cascade(self, inval): return inval # Smoke test custom model implant = p2p.implants.ElectrodeArray('epiretinal', 10, 0, 0, 0) sim = p2p.Simulation(implant, engine='serial') sim.set_optic_fiber_layer(x_range=0, y_range=0) valid_model = Valid(tsample=0.2) sim.set_ganglion_cell_layer(valid_model) npt.assert_equal(isinstance(sim.gcl, p2p.retina.BaseModel), True) npt.assert_equal(sim.gcl.tsample, 0.2) # Smoke test latest model for modelstr in ['latest', 'Latest', 'LATEST']: sim.set_ganglion_cell_layer(modelstr, lweight=42) npt.assert_equal(isinstance(sim.gcl, p2p.retina.TemporalModel), True) npt.assert_equal(sim.gcl.lweight, 42) sim.set_ganglion_cell_layer(modelstr, unknown_param=2) # smoke # Smoke test Nanduri model for modelstr in ['Nanduri2012', 'nanduri2012', 'NANDURI2012']: sim.set_ganglion_cell_layer(modelstr, tau3=42) npt.assert_equal(isinstance(sim.gcl, p2p.retina.Nanduri2012), True) npt.assert_equal(sim.gcl.tau3, 42) sim.set_ganglion_cell_layer(modelstr, unknown_param=2) # smoke # Smoke test Horsager model for modelstr in ['Horsager2009', 'horsager', 'HORSAGER2009']: sim.set_ganglion_cell_layer(modelstr, tau3=42) npt.assert_equal(isinstance(sim.gcl, p2p.retina.Horsager2009), True) npt.assert_equal(sim.gcl.tau3, 42) sim.set_ganglion_cell_layer(modelstr, unknown_param=2) # smoke # Model unknown with pytest.raises(ValueError): sim.set_ganglion_cell_layer('unknown-model') with pytest.raises(ValueError): sim.set_ganglion_cell_layer(p2p.implants.ArgusII())
def img2percept( infile, implant_type='AlphaIMS', engine_string='serial', s_sample=25, t_sample=0.005 / 1000, dconst=0.01, retinamodel='Nanduri2012', naxons=501, downsampling_input=None, # tuple of desired resolution, e.g. (104,104) invert_input=False, videobool=False, tolerance=0.0): """ Run pulse2percept on an input image. Parameters ---------- infile : str path to input image file implant_type : str type of implant used for the simulation. Can be from ['AlphaIMS', 'ArgusI', 'ArgusII'] engine_string : str, default='serial' parallelization engine used by p2p s_sample t_sample dconst retinamodel naxons downsampling_input invert_input videobool tolerance Returns ------- """ # TODO: Finish docstring # choose implant type implant = _import_implant(implant_type) # configure simulation sim = p2p.Simulation(implant, engine=engine_string, n_jobs=-1, use_jit=False) sim.set_optic_fiber_layer(sampling=s_sample, sensitivity_rule='decay', decay_const=dconst, n_axons=naxons) sim.set_ganglion_cell_layer(retinamodel, tsample=t_sample) # load input image if not videobool: img_in = sio.imread(infile, as_gray=True) if downsampling_input: img_in = sit.resize(img_in, (104, 104)) if invert_input: img_in = invert(img_in) # generate pulsetrain if videobool: stim = p2p.stimuli.video2pulsetrain(infile, implant, tsample=t_sample) else: stim = p2p.stimuli.image2pulsetrain(img_in, implant, tsample=t_sample) # choose retina layers to be included in simulation simlayers = _choose_layers(retinamodel) # generate percept from pulsetrain percept = sim.pulse2percept(stim, layers=simlayers, t_percept=t_sample, tol=tolerance) return percept
import numpy as np import pickle import pulse2percept as p2p videofile = '../data/kid-pool.avi' stimfile = '../examples/notebooks/stim-kid-pool-amplitude.dat' perceptfile = 'percept-kid-pool-amplitude' # Place an Argus I array on the retina argus = p2p.implants.ArgusII(x_center=0, y_center=0, h=100) sim = p2p.Simulation(argus, engine='joblib', num_jobs=5) # Set parameters of the optic fiber layer (OFL) # In previous versions of the model, this used to be called the `Retina` # object, which created a spatial grid and generated the axtron streak map. sampling = 250 # spatial sampling of the retina (microns) axon_lambda = 2 # constant that determines fall-off with axonal distance sim.set_optic_fiber_layer(sampling=sampling, axon_lambda=axon_lambda, x_range=[-2800, 2800], y_range=[-1700, 1700]) # Set parameters of the ganglion cell layer (GCL) # In previous versions of the model, this used to be called `TemporalModel`. t_gcl = 0.005 / 1000 # Sampling step (s) for the GCL computation t_percept = 1.0 / 30.0 # Sampling step (s) for the perceptual output sim.set_ganglion_cell_layer(tsample=t_gcl) try: stim = pickle.load(open(stimfile, "rb"))
def test_Simulation___init__(): implant = p2p.implants.Electrode("epiretinal", 10, 0, 0, 0) with pytest.raises(TypeError): p2p.Simulation(implant)
import numpy as np import pickle import pulse2percept as p2p # Place an Argus I array on the retina argus = p2p.implants.ArgusII(x_center=0, y_center=0, h=100) sim = p2p.Simulation(argus) # Set parameters of the optic fiber layer (OFL) # In previous versions of the model, this used to be called the `Retina` # object, which created a spatial grid and generated the axtron streak map. sampling = 250 # spatial sampling of the retina (microns) axon_lambda = 2 # constant that determines fall-off with axonal distance sim.set_optic_fiber_layer(sampling=sampling, axon_lambda=axon_lambda, x_range=[-2800, 2800], y_range=[-1700, 1700]) # Set parameters of the ganglion cell layer (GCL) # In previous versions of the model, this used to be called `TemporalModel`. t_gcl = 0.005 / 1000 # Sampling step (s) for the GCL computation t_percept = 1.0 / 30.0 # Sampling step (s) for the perceptual output sim.set_ganglion_cell_layer(tsample=t_gcl) stim = p2p.stimuli.video2pulsetrain('../../data/boston-train.mp4', argus, framerate=30, coding='amplitude', valrange=[0, 60], max_contrast=False,