def test_Model_predict_percept(): # A None Model has nothing to build, nothing to perceive: model = Model() npt.assert_equal(model.predict_percept(ArgusI()), None) npt.assert_equal(model.predict_percept(ArgusI(stim={'A1': 1})), None) npt.assert_equal( model.predict_percept(ArgusI(stim={'A1': 1}), t_percept=[0, 1]), None) # Just the spatial model: model = Model(spatial=ValidSpatialModel()).build() npt.assert_equal(model.predict_percept(ArgusI()), None) # Just the temporal model: model = Model(temporal=ValidTemporalModel()).build() npt.assert_equal(model.predict_percept(ArgusI()), None) # Invalid calls: model = Model(spatial=ValidSpatialModel(), temporal=ValidTemporalModel()) with pytest.raises(NotBuiltError): # Must call build first: model.predict_percept(ArgusI()) model.build() with pytest.raises(ValueError): # Cannot request t_percepts that are not multiples of dt: model.predict_percept(ArgusI(stim=np.ones(16)), t_percept=[0.1, 0.11]) with pytest.raises(ValueError): # stim.time==None but requesting t_percept != None model.predict_percept(ArgusI(stim=np.ones(16)), t_percept=[0, 1, 2]) with pytest.raises(TypeError): # Must pass an implant: model.predict_percept(Stimulus(3))
def test_Model_build(): # A None model: model = Model() # Nothing to build, so `is_built` is always True (we want to be able to # call `predict_percept`): npt.assert_equal(model.is_built, True) model.build() npt.assert_equal(model.is_built, True) # SpatialModel, but no TemporalModel: model = Model(spatial=ValidSpatialModel()) npt.assert_equal(model.is_built, False) model.build() npt.assert_equal(model.is_built, True) # TemporalModel, but no SpatialModel: model = Model(temporal=ValidTemporalModel()) npt.assert_equal(model.is_built, False) model.build() npt.assert_equal(model.is_built, True) # SpatialModel and TemporalModel: model = Model(spatial=ValidSpatialModel(), temporal=ValidTemporalModel()) npt.assert_equal(model.is_built, False) model.build() npt.assert_equal(model.is_built, True)
def test_Model(): # A None Model: model = Model() npt.assert_equal(model.has_space, False) npt.assert_equal(model.has_time, False) npt.assert_equal(str(model), "Model(spatial=None, temporal=None)") # Cannot add attributes outside the constructor: with pytest.raises(AttributeError): model.a with pytest.raises(FreezeError): model.a = 1 # Wrong model type: with pytest.raises(TypeError): Model(spatial=ValidTemporalModel()) with pytest.raises(TypeError): Model(temporal=ValidSpatialModel()) # SpatialModel, but no TemporalModel: model = Model(spatial=ValidSpatialModel()) npt.assert_equal(model.has_space, True) npt.assert_equal(model.has_time, False) npt.assert_almost_equal(model.xystep, 0.25) npt.assert_almost_equal(model.spatial.xystep, 0.25) model.xystep = 2 npt.assert_almost_equal(model.xystep, 2) npt.assert_almost_equal(model.spatial.xystep, 2) # Cannot add more attributes: with pytest.raises(AttributeError): model.a with pytest.raises(FreezeError): model.a = 1 # TemporalModel, but no SpatialModel: model = Model(temporal=ValidTemporalModel()) npt.assert_equal(model.has_space, False) npt.assert_equal(model.has_time, True) npt.assert_almost_equal(model.dt, 5e-3) npt.assert_almost_equal(model.temporal.dt, 5e-3) model.dt = 1 npt.assert_almost_equal(model.dt, 1) npt.assert_almost_equal(model.temporal.dt, 1) # Cannot add more attributes: with pytest.raises(AttributeError): model.a with pytest.raises(FreezeError): model.a = 1 # SpatialModel and TemporalModel: model = Model(spatial=ValidSpatialModel(), temporal=ValidTemporalModel()) npt.assert_equal(model.has_space, True) npt.assert_equal(model.has_time, True) npt.assert_almost_equal(model.xystep, 0.25) npt.assert_almost_equal(model.spatial.xystep, 0.25) npt.assert_almost_equal(model.dt, 5e-3) npt.assert_almost_equal(model.temporal.dt, 5e-3) # Setting a new spatial parameter: model.xystep = 2 npt.assert_almost_equal(model.xystep, 2) npt.assert_almost_equal(model.spatial.xystep, 2) # Setting a new temporal parameter: model.dt = 1 npt.assert_almost_equal(model.dt, 1) npt.assert_almost_equal(model.temporal.dt, 1) # Setting a parameter that's part of both spatial/temporal: npt.assert_equal(model.thresh_percept, {'spatial': 0, 'temporal': 0}) model.thresh_percept = 1.234 npt.assert_almost_equal(model.spatial.thresh_percept, 1.234) npt.assert_almost_equal(model.temporal.thresh_percept, 1.234) # Cannot add more attributes: with pytest.raises(AttributeError): model.a with pytest.raises(FreezeError): model.a = 1
def test_Model_set_params(): # SpatialModel, but no TemporalModel: model = Model(spatial=ValidSpatialModel()) model.set_params({'xystep': 2.33}) npt.assert_almost_equal(model.xystep, 2.33) npt.assert_almost_equal(model.spatial.xystep, 2.33) # TemporalModel, but no SpatialModel: model = Model(temporal=ValidTemporalModel()) model.set_params({'dt': 2.33}) npt.assert_almost_equal(model.dt, 2.33) npt.assert_almost_equal(model.temporal.dt, 2.33) # SpatialModel and TemporalModel: model = Model(spatial=ValidSpatialModel(), temporal=ValidTemporalModel()) # Setting both using the convenience function: model.set_params({'xystep': 5, 'dt': 2.33}) npt.assert_almost_equal(model.xystep, 5) npt.assert_almost_equal(model.spatial.xystep, 5) npt.assert_equal(hasattr(model.temporal, 'xystep'), False) npt.assert_almost_equal(model.dt, 2.33) npt.assert_almost_equal(model.temporal.dt, 2.33) npt.assert_equal(hasattr(model.spatial, 'dt'), False)
def test_Model_predict_percept_correctly_parallelizes(): # setup and time spatial model with 1 thread one_thread_spatial = Model(spatial=ValidSpatialModel(n_threads=1)).build() start_time_one_thread_spatial = time.perf_counter() one_thread_spatial.predict_percept(ArgusI()) one_thread_spatial_predict_time = time.perf_counter( ) - start_time_one_thread_spatial # setup and time spatial model with 2 threads two_thread_spatial = Model(spatial=ValidSpatialModel(n_threads=2)).build() start_time_two_thread_spatial = time.perf_counter() two_thread_spatial.predict_percept(ArgusI()) two_threaded_spatial_predict_time = time.perf_counter( ) - start_time_two_thread_spatial # we expect roughly a linear decrease in time as thread count increases npt.assert_almost_equal(actual=two_threaded_spatial_predict_time, desired=one_thread_spatial_predict_time / 2, decimal=1e-5) # setup and time temporal model with 1 thread one_thread_temporal = Model(temporal=ValidTemporalModel( n_threads=1)).build() start_time_one_thread_temporal = time.perf_counter() one_thread_temporal.predict_percept(ArgusI()) one_thread_temporal_predict_time = time.perf_counter( ) - start_time_one_thread_temporal # setup and time temporal model with 2 threads two_thread_temporal = Model(temporal=ValidTemporalModel( n_threads=2)).build() start_time_two_thread_temporal = time.perf_counter() two_thread_temporal.predict_percept(ArgusI()) two_thread_temporal_predict_time = time.perf_counter( ) - start_time_two_thread_temporal # we expect roughly a linear decrease in time as thread count increases npt.assert_almost_equal(actual=two_thread_temporal_predict_time, desired=one_thread_temporal_predict_time / 2, decimal=1e-5)