def run_stim(self, model: sciunit.Model, stim: float): stim_start = 50.0 # ms stim_dur = 500.0 # ms stim_amp = stim # nA model.inject_step_current_soma(current={ 'delay': stim_start, 'duration': stim_dur, 'amplitude': stim_amp }) start = timeit.default_timer() trace = model.get_membrane_potential_soma_eFEL_format( tstop=stim_start + stim_dur, start=stim_start, stop=stim_start + stim_dur) stop = timeit.default_timer() self.run_times[str(stim)] = stop - start self.traces.append({ "stim": stim_amp, "t": trace["T"], "v": trace["V"] }) try: result = efel.getFeatureValues([trace], [ "Spikecount_stimint" ])[0]["Spikecount_stimint"][0] / (stim_dur * 1e-3) # (Hz) except: result = float("nan") return result
def test_error_types(self): from sciunit.errors import CapabilityError, BadParameterValueError,\ PredictionError, InvalidScoreError from sciunit import Model, Capability CapabilityError(Model(), Capability) PredictionError(Model(), 'foo') InvalidScoreError() BadParameterValueError('x', 3)
def test_ProtocolToFeaturesTest(self): t = ProtocolToFeaturesTest([1, 2, 3]) m = Model() m.run = lambda: 0 self.assertIsInstance(t.generate_prediction(m), NotImplementedError) self.assertIsInstance(t.setup_protocol(m), NotImplementedError) self.assertIsInstance(t.get_result(m), NotImplementedError) self.assertIsInstance(t.extract_features(m, list()), NotImplementedError)
def test_fake_test(self): from neuronunit.tests import FakeTest from sciunit import Model observation = {'a' : 1, 'b' : 1} ft = FakeTest(observation, name="a_b_test") m = Model(name="a_b_model") m.attrs = {'a' : 1, 'b' : 1} prediction = ft.generate_prediction(m) score = ft.compute_score([0.9, 1.1], prediction).score self.assertAlmostEqual(score, 0.09, 2)
def __init__(self, project_path, name=None, **kwargs): """file_path is the full path to an .ncx file.""" print("Instantiating a neuroConstruct model from %s." % project_path) self.project_path = project_path self.ran = False self.population_name = self.get_cell_group()+'_0' Model.__init__(self) Runnable_NC.__init__(self) ReceivesCurrent_NC.__init__(self) for key,value in kwargs.items(): setattr(self,key,value) super().__init__(name=name, **kwargs)
def test_error_types(self): from sciunit.errors import ( CapabilityError, BadParameterValueError, PredictionError, InvalidScoreError, ) from sciunit import Model, Capability CapabilityError(Model(), Capability) CapabilityError(Model(), Capability, "this is a test detail") PredictionError(Model(), "foo") InvalidScoreError() BadParameterValueError("x", 3)
def generate_prediction(self, model: sciunit.Model, verbose: bool = False) -> float: efel.reset() stim_start = 10.0 # ms stim_dur = 50.0 # ms stim_amp = -1.0 # nA efel.setDoubleSetting('stimulus_current', stim_amp) model.inject_soma_square_current(current={'delay':stim_start, 'duration':stim_dur, 'amplitude':stim_amp}) trace = model.get_soma_membrane_potential_eFEL_format(tstop=stim_start+stim_dur+stim_start, start=stim_start, stop =stim_start+stim_dur) prediction = efel.getFeatureValues([trace], ['ohmic_input_resistance_vb_ssse'])[0]["ohmic_input_resistance_vb_ssse"][0] return prediction
def generate_prediction(self, model: sciunit.Model, verbose: bool = False) -> Optional[float]: efel.reset() stim_start = 10.0 # ms stim_dur = 5.0 # ms stim_amp = 15.0 # nA efel.setDoubleSetting('stimulus_current', stim_amp) model.inject_soma_square_current(current={'delay':stim_start, 'duration':stim_dur, 'amplitude':stim_amp}) trace = model.get_soma_membrane_potential_eFEL_format(tstop=stim_start+stim_dur+stim_start, start=stim_start, stop =stim_start+stim_dur) output = efel.getFeatureValues([trace], ['AP_duration_half_width'])[0]["AP_duration_half_width"] prediction = output[0] if output else None return prediction
def test_Test(self): pv = config["PREVALIDATE"] config["PREVALIDATE"] = 1 with self.assertRaises(ObservationError): t = Test(None) with self.assertRaises(ObservationError): class Test2(Test): observation_schema = None score_type = ZScore units = pq.pA def generate_prediction(self): return 1 t = Test2({"mean": 5 * pq.pA}) t = Test({}) self.assertRaises(ObservationError, t.validate_observation, "I am not an observation") t.observation_schema = {} t.validate_observation({0: 0, 1: 1}) Test.observation_schema = [{}, {}] self.assertListEqual(t.observation_schema_names(), ["Schema 1", "Schema 2"]) config["PREVALIDATE"] = pv self.assertRaises(ParametersError, t.validate_params, None) self.assertRaises(ParametersError, t.validate_params, "I am not an observation") t.params_schema = {} t.validate_params({0: 1, 1: 2}) self.assertRaises(Error, t.check_capabilities, "I am not a model") t.condition_model(Model()) self.assertRaises(NotImplementedError, t.generate_prediction, Model()) self.assertRaises(NotImplementedError, t.optimize, Model()) self.assertTrue(t.compute_score({0: 2, 1: 2}, {0: 2, 1: 2}).score) self.assertFalse(t.compute_score({0: -2, 1: 2}, {0: 2, 1: -2}).score) t.score_type = None self.assertRaises(NotImplementedError, t.compute_score, {}, {}) t.score_type = BooleanScore self.assertRaises(InvalidScoreError, t.check_score_type, FloatScore(0.5))
def test_backends_init_caches(self): myModel = Model() backend = Backend() backend.model = myModel backend.init_backend(use_disk_cache=True, use_memory_cache=True) backend.init_backend(use_disk_cache=False, use_memory_cache=True) backend.init_backend(use_disk_cache=True, use_memory_cache=False) backend.init_backend(use_disk_cache=False, use_memory_cache=False) backend.init_cache()
def test_backends_set_caches(self): myModel = Model() backend = Backend() backend.model = myModel # backend.init_memory_cache() self.assertIsNone(backend.get_disk_cache("key1")) self.assertIsNone(backend.get_disk_cache("key2")) self.assertIsNone(backend.get_memory_cache("key1")) self.assertIsNone(backend.get_memory_cache("key2")) backend.set_disk_cache("value1", "key1") backend.set_memory_cache("value1", "key1") self.assertEqual(backend.get_memory_cache("key1"), "value1") self.assertEqual(backend.get_disk_cache("key1"), "value1") backend.set_disk_cache("value2") backend.set_memory_cache("value2") self.assertEqual(backend.get_memory_cache(myModel.hash()), "value2") self.assertEqual(backend.get_disk_cache(myModel.hash()), "value2") backend.load_model() backend.set_attrs(test_attribute="test attribute") backend.set_run_params(test_param="test parameter") backend.init_backend(use_disk_cache=True, use_memory_cache=True)
def run_stim(self, model: sciunit.Model, stim: float): stim_start = 50.0 # ms stim_dur = (60 * 1000) - 50.0 # ms stim_amp = stim # nA model.inject_step_current_soma(current={ 'delay': stim_start, 'duration': stim_dur, 'amplitude': stim_amp }) start = timeit.default_timer() trace = model.get_membrane_potential_soma_eFEL_format( tstop=stim_start + stim_dur, start=stim_start, stop=stim_start + stim_dur) stop = timeit.default_timer() run_time = stop - start self.traces.append({ "stim": stim_amp, "t": trace["T"], "v": trace["V"] }) return run_time
def test_Test(self): config_set("PREVALIDATE", True) with self.assertRaises(ObservationError): t = Test(None) config_set("PREVALIDATE", False) t = Test(None) self.assertRaises(ObservationError, t.validate_observation, None) self.assertRaises(ObservationError, t.validate_observation, "I am not an observation") self.assertRaises(ObservationError, t.validate_observation, {"mean": None}) t = Test([0, 1]) t.observation_schema = {} t.validate_observation({0: 0, 1: 1}) Test.observation_schema = [{}, {}] self.assertListEqual(t.observation_schema_names(), ["Schema 1", "Schema 2"]) self.assertRaises(ParametersError, t.validate_params, None) self.assertRaises(ParametersError, t.validate_params, "I am not an observation") t.params_schema = {} t.validate_params({0: 1, 1: 2}) self.assertRaises(Error, t.check_capabilities, "I am not a model") t.condition_model(Model()) self.assertRaises(NotImplementedError, t.generate_prediction, Model()) self.assertRaises(NotImplementedError, t.optimize, Model()) self.assertTrue(t.compute_score({0: 2, 1: 2}, {0: 2, 1: 2}).score) self.assertFalse(t.compute_score({0: -2, 1: 2}, {0: 2, 1: -2}).score) t.score_type = None self.assertRaises(NotImplementedError, t.compute_score, {}, {}) t.score_type = BooleanScore self.assertRaises(InvalidScoreError, t.check_score_type, FloatScore(0.5)) self.assertRaises(ObservationError, t.judge, [Model(), Model()])
def test_is_match(self): from sciunit import Model m = Model() m2 = Model() self.assertFalse(m.is_match(m2)) self.assertTrue(m.is_match(m)) self.assertTrue(m.is_match("Model"))
def test_get_model_state(self): from sciunit import Model m = Model() state = m.__getstate__() self.assertEqual(m.__dict__, state)
def generate_prediction(self, model: sciunit.Model, verbose: bool = False) -> float: trace = model.get_soma_membrane_potential(tstop=50.0) prediction = numpy.median(trace[1]) return prediction
def test_getattr(self): from sciunit import Model m = Model() self.assertEqual(m.name, m.__getattr__("name"))
def test_get_model_state(self): from sciunit import Model m = Model() state = m.__getstate__() self.assertEqual(m.__dict__,state)
class MyBackend(Backend): model = Model() def _backend_run(self) -> str: return "test result"