def test_log_for_times(self): # Test the method Protocol.log_for_times() # Relies on PacingSystem p = myokit.Protocol() p.schedule(2, 10, 100, 1000, 2) t = [ 0, 9.999, 10, 10.001, 109.999, 110, 110.001, 1000, 1009.99, 1010, 1110, 2000, 2020 ] v = [0, 0, 2, 2, 2, 0, 0, 0, 0, 2, 0, 0, 0] d = p.log_for_times(t) self.assertEqual(len(d), 2) self.assertIn('time', d) self.assertIn('pace', d) self.assertEqual(d.time(), t) self.assertEqual(d['pace'], v) # Empty times d = p.log_for_times([]) self.assertEqual(len(d.time()), 0) self.assertEqual(len(d['pace']), 0) # Deprecated alias with WarningCollector() as w: p.create_log_for_times([]) self.assertIn('deprecated', w.text())
def test_strfloat(self): # Deprecated alias of myokit.float.str args = ['-1.234', True, myokit.SINGLE_PRECISION] with WarningCollector() as c: x = myokit.strfloat(*args) self.assertIn('`myokit.strfloat` is deprecated', c.text()) self.assertEqual(x, myokit.float.str(*args))
def test_iterdir(self): # Test the iterdir() method that iterators over model, protocol tuples. with WarningCollector(): import myokit.lib.multi as multi # Get all found tuples (model, protocol) tuples = [x for x in multi.iterdir(DIR_MULTI)] self.assertEqual(len(tuples), 2) # Should contain (ordered by filename) # (Beeler (no name), None) # (Lr1991, protocol) # self.assertEqual(type(tuples[0][0]), myokit.Model) self.assertEqual(tuples[0][0].name(), 'beeler-no-name') self.assertIsNone(tuples[0][1], None) self.assertEqual(type(tuples[1][0]), myokit.Model) self.assertEqual(tuples[1][0].name(), 'Luo-Rudy model (1991)') self.assertEqual(type(tuples[1][1]), myokit.Protocol) # Test without name setting tuples = [x for x in multi.iterdir(DIR_MULTI, False)] self.assertEqual(len(tuples), 2) self.assertEqual(type(tuples[0][0]), myokit.Model) self.assertIsNone(tuples[0][0].name()) self.assertIsNone(tuples[0][1], None) self.assertEqual(type(tuples[1][0]), myokit.Model) self.assertEqual(tuples[1][0].name(), 'Luo-Rudy model (1991)') self.assertEqual(type(tuples[1][1]), myokit.Protocol) # Path must be a directory path = os.path.join(DIR_MULTI, 'lr-1991.mmt') i = multi.iterdir(path) self.assertRaisesRegex(ValueError, 'not a directory', next, i)
def test_activation(self): # Test the activation experiment class. with WarningCollector(): import myokit.lib.common as common # Load model m = os.path.join(DIR_DATA, 'lr-1991.mmt') m = myokit.load_model(m) # Create experiment c = m.get('ina') g = c.add_variable('g') g.set_rhs('m^3 * h * j') m.validate() a = common.Activation(m, 'ina.g') # Test a.times() a.traces() a.peaks(normalize=True) a.peaks(normalize=False) a.fit_boltzmann() a.convert_g2i() a.times() a.traces() a.peaks(normalize=True) a.peaks(normalize=False) a.fit_boltzmann()
def test_variable(self): # Tests writing of variables m1 = cellml.Model('m') c = m1.add_component('c') p = c.add_variable('p', 'mole') q = c.add_variable('q', 'kelvin', interface='public') r = c.add_variable('r', 'ampere', interface='private') p.set_initial_value(1) with WarningCollector(): xml = cellml.write_string(m1) m2 = cellml.parse_string(xml) p, q, r = m2['c']['p'], m2['c']['q'], m2['c']['r'] self.assertEqual(p.units().name(), 'mole') self.assertEqual(q.units().name(), 'kelvin') self.assertEqual(r.units().name(), 'ampere') self.assertEqual(p.interface(), 'none') self.assertEqual(q.interface(), 'public') self.assertEqual(r.interface(), 'private') self.assertEqual(p.initial_value(), myokit.Number(1, myokit.units.mole)) self.assertEqual(q.initial_value(), None) self.assertEqual(r.initial_value(), None)
def test_progress_reporter(self): # Test running with a progress reporter. m, p, x = myokit.load(os.path.join(DIR_DATA, 'lr-1991.mmt')) with WarningCollector() as c: s = myokit.PSimulation(m, p, variables=['membrane.V'], parameters=['ina.gNa']) with myokit.tools.capture() as c: s.run(2, progress=myokit.ProgressPrinter()) c = c.text().splitlines() self.assertEqual(len(c), 2) self.assertEqual( c[0], '[0.0 minutes] 50.0 % done, estimated 0 seconds remaining') self.assertEqual( c[1], '[0.0 minutes] 100.0 % done, estimated 0 seconds remaining') # Not a progress reporter self.assertRaisesRegex(ValueError, 'ProgressReporter', s.run, 1, progress=12) # Cancel from reporter self.assertRaises(myokit.SimulationCancelledError, s.run, 1, progress=CancellingReporter(0))
def test_set_constant(self): # Test :meth:`PSimulation.set_constant()` and # :meth:`PSimulation.set_parameters()` m, p, x = myokit.load(os.path.join(DIR_DATA, 'lr-1991.mmt')) with WarningCollector() as c: s = myokit.PSimulation(m, p, variables=['membrane.V'], parameters=['ina.gNa']) s.set_constant('ica.gCa', 1) s.set_constant(m.get('ica.gCa'), 1) # Variable is not a literal self.assertRaisesRegex(ValueError, 'literal', s.set_constant, 'membrane.V', 1) # Variable is in parameters list self.assertRaisesRegex(ValueError, 'parameter', s.set_constant, 'ina.gNa', 1) # Set parameter values s.set_parameters([1]) self.assertRaisesRegex(ValueError, '1 values', s.set_parameters, [1, 2]) s.set_parameters({'ina.gNa': 1}) s.set_parameters({m.get('ina.gNa'): 1}) self.assertRaisesRegex(ValueError, 'Unknown', s.set_parameters, {'bert': 2}) self.assertRaisesRegex(ValueError, 'parameter', s.set_parameters, {'ica.gCa': 2})
def test_recovery(self): # Test the recovery experiment class. with WarningCollector(): import myokit.lib.common as common # Load model m = os.path.join(DIR_DATA, 'lr-1991.mmt') m = myokit.load_model(m) # Create experiment c = m.get('ina') g = c.add_variable('g') g.set_rhs('m^3 * h * j') m.validate() r = common.Recovery(m, 'ina.g') # Test n = 20 r.set_pause_duration(0.1, 10, n) d = r.ratio() self.assertEqual(len(d), 2) self.assertIn('engine.time', d) self.assertIn('ina.g', d) self.assertEqual(n, len(d['engine.time'])) self.assertEqual(n, len(d['ina.g'])) x = d['engine.time'] self.assertTrue(np.all(x[1:] > x[:-1])) x = d['ina.g'] # This is a monotonically increasing function self.assertTrue(np.all(x[1:] > x[:-1]))
def test_time(self): # Test the time() method that returns the time variable. with WarningCollector(): import myokit.lib.multi as multi m = myokit.Model() c = m.add_component('c') x = c.add_variable('x') x.set_rhs(0) with WarningCollector() as w: self.assertRaises(myokit.IncompatibleModelError, multi.time, m) x.set_binding('time') self.assertEqual(x, multi.time(m)) self.assertIn('deprecated', w.text())
def test_variable(self): # Tests parsing variables # Valid has already been tested # Must have a name x = '<component name="a"><variable units="volt"/></component>' self.assertBad(x, 'must have a name attribute') # Must have units x = '<component name="a"><variable name="a" /></component>' self.assertBad(x, 'must have a units attribute') # CellML errors are converted to parsing errors x = '<component name="a"><variable name="1" units="volt"/></component>' self.assertBad(x, 'valid CellML identifier') # Unsupported units are ignored x = '<variable name="x" units="celsius" initial_value="3" />' x = '<component name="a">' + x + '</component>' with WarningCollector() as w: x = self.parse(x)['a']['x'] self.assertIn('celsius', w.text()) self.assertEqual(x.units().myokit_unit(), myokit.units.dimensionless)
def test_label(self): # Test the label() method that returns a labelled variable. with WarningCollector(): import myokit.lib.multi as multi m = myokit.Model() c = m.add_component('c') x = c.add_variable('x') x.set_rhs(0) with WarningCollector() as w: self.assertRaises(myokit.IncompatibleModelError, multi.label, m, 'x') x.set_label('x') self.assertEqual(x, multi.label(m, 'x')) self.assertIn('deprecated', w.text())
def test_from_data_log(self): # Test some edge cases of `DataBlock2d.from_log`. # Test valid construction d = myokit.DataLog() d.set_time_key('time') d['time'] = [1, 2, 3] d['x', 0, 0] = [0, 0, 0] d['x', 1, 0] = [1, 1, 2] d['x', 0, 1] = [1, 2, 2] d['x', 1, 1] = [3, 4, 5] d['x', 0, 2] = [6, 6, 6] d['x', 1, 2] = [7, 7, 2] myokit.DataBlock2d.from_log(d) # Deprecated alias with WarningCollector() as wc: myokit.DataBlock2d.from_DataLog(d) self.assertIn('deprecated', wc.text()) # No time key d.set_time_key(None) self.assertRaises(ValueError, myokit.DataBlock2d.from_log, d) # Bad time key d.set_time_key('z') # Multi-dimensional time key d.set_time_key('0.0.x') self.assertRaises(ValueError, myokit.DataBlock2d.from_log, d) # 1-dimensional stuff d.set_time_key('time') myokit.DataBlock2d.from_log(d) d['y', 0] = [10, 10, 10] d['y', 1] = [20, 20, 20] self.assertRaises(ValueError, myokit.DataBlock2d.from_log, d) # Mismatched dimensions d = myokit.DataLog() d.set_time_key('time') d['time'] = [1, 2, 3] d['x', 0, 0] = [0, 0, 0] d['x', 1, 0] = [1, 1, 2] d['x', 0, 1] = [1, 2, 2] d['x', 1, 1] = [3, 4, 5] d['x', 0, 2] = [6, 6, 6] d['x', 1, 2] = [7, 7, 2] d['y', 0, 0] = [0, 0, 0] d['y', 1, 0] = [1, 1, 2] d['y', 0, 1] = [1, 2, 2] d['y', 1, 1] = [3, 4, 5] d['y', 0, 2] = [6, 6, 6] d['y', 1, 2] = [7, 7, 2] myokit.DataBlock2d.from_log(d) del (d['0.2.y']) del (d['1.2.y']) self.assertRaises(ValueError, myokit.DataBlock2d.from_log, d)
def test_linear_model_from_component(self): # Load model fname = os.path.join(DIR_DATA, 'clancy-1999-fitting.mmt') model = myokit.load_model(fname) # Create a markov model markov.LinearModel.from_component(model.get('ina')) # Test deprecated MarkovModel class with WarningCollector() as w: m = markov.MarkovModel.from_component(model.get('ina')) self.assertEqual(type(m), markov.AnalyticalSimulation) self.assertIn('deprecated', w.text()) # Test partially automatic creation states = [ 'ina.C3', 'ina.C2', 'ina.C1', 'ina.IF', 'ina.IS', model.get('ina.O'), ] markov.LinearModel.from_component(model.get('ina'), states=states) parameters = ['ina.p1', 'ina.p2', 'ina.p3', model.get('ina.p4', )] markov.LinearModel.from_component(model.get('ina'), parameters=parameters) current = 'ina.i' markov.LinearModel.from_component(model.get('ina'), current=current) markov.LinearModel.from_component(model.get('ina'), current=model.get(current)) # No current --> This is allowed m2 = model.clone() m2.get('ina').remove_variable(m2.get('ina.i')) m = markov.LinearModel.from_component(m2.get('ina')) # Two currents m2 = model.clone() v = m2.get('ina').add_variable('i2') v.set_rhs(m2.get('ina.i').rhs().clone()) self.assertRaisesRegex( markov.LinearModelError, 'more than one variable that could be a current', markov.LinearModel.from_component, m2.get('ina')) # Explict vm m2 = model.clone() m2.get('membrane.V').set_label(None) markov.LinearModel.from_component(model.get('ina'), vm='membrane.V') self.assertRaisesRegex(markov.LinearModelError, 'labeled as "membrane_potential"', markov.LinearModel.from_component, m2.get('ina'))
def test_format_path(self): # Deprecated alias of myokit.tools.format_path root = os.path.join(os.path.abspath('.'), 'a') a = os.path.join(root, 'b', 'c') b = os.path.join(os.path.abspath('.'), 'a') with WarningCollector() as c: x = myokit.format_path(a, b) self.assertIn('`myokit.format_path` is deprecated', c.text()) self.assertEqual(x, myokit.tools.format_path(a, b))
def test_model(self): # Tests importing the Hodgkin-Huxley model i = myokit.formats.importer('sbml') with WarningCollector() as w: model = i.model( os.path.join(DIR_FORMATS, 'sbml', 'HodgkinHuxley.xml')) self.assertIn('Unknown SBML namespace', w.text()) self.assertIsInstance(model, myokit.Model)
def test_periodic(self): # Test periodic logging. m, p, x = myokit.load(os.path.join(DIR_DATA, 'lr-1991.mmt')) with WarningCollector(): s = myokit.PSimulation( m, p, variables=['membrane.V'], parameters=['ina.gNa']) # Set tolerance for equality testing emax = 1e-2 # Time steps for logging are approximate # Test 1: Simple 5 ms simulation, log_interval 0.5 ms d, e = s.run(5, log=['engine.time'], log_interval=0.5) d = d.npview() t = d['engine.time'] q = np.arange(0, 5, 0.5) if debug: print(t) print(q) print('- ' * 10) self.assertEqual(len(t), len(q)) self.assertTrue(np.max(np.abs(t - q)) < emax) # Test 2: Very short simulation s.reset() d, e = s.run(1, log=['engine.time'], log_interval=0.5) d = d.npview() t = d['engine.time'] q = np.arange(0, 1, 0.5) if debug: print(t) print(q) print('- ' * 10) self.assertEqual(len(t), len(q)) self.assertTrue(np.max(np.abs(t - q)) < emax) # Test 3: Stop and start a simulation s.reset() d, e = s.run(1, log=['engine.time'], log_interval=0.5) d, e = s.run(2, log=d, log_interval=0.5) d, e = s.run(2, log=d, log_interval=0.5) d = d.npview() t = d['engine.time'] q = np.arange(0, 5, 0.5) if debug: print(t) print(q) print('- ' * 10) self.assertEqual(len(t), len(q)) self.assertTrue(np.max(np.abs(t - q)) < emax) # Negative or 0 log interval --> Log every step s.set_step_size(0.01) d, dp = s.run(1, log_interval=0) self.assertEqual(len(d.time()), 101) d, dp = s.run(1, log_interval=-1) self.assertEqual(len(d.time()), 101)
def test_format_float_dict(self): # Test myokit.format_float_dict, which is deprecated d = {'one': 1, 'Definitely two': 2, 'Three-ish': 3.1234567} with WarningCollector() as c: x = myokit.format_float_dict(d).splitlines() self.assertIn('`myokit.format_float_dict` is deprecated', c.text()) self.assertEqual(len(x), 3) self.assertEqual(x[0], 'Definitely two = 2') self.assertEqual(x[1], 'Three-ish = 3.1234567') self.assertEqual(x[2], 'one = 1')
def test_simulation_error_2(self): # Test for simulation error detection: failure occurred too often. # Cvode error (test failure occurred too many times) m = self.model.clone() v = m.get('membrane.V') v.set_rhs(myokit.Multiply(v.rhs(), myokit.Number(1e18))) s = myokit.LegacySimulation(m, self.protocol) with WarningCollector(): self.assertRaisesRegex( myokit.SimulationError, 'numerical error', s.run, 5000)
def test_neg_stimulus(self): # Tests if NaNs are detected in the tissue, after a negative stimulus try: self.s1.set_protocol(self.p3) with WarningCollector(): self.assertRaisesRegex( myokit.SimulationError, 'in tissue simulation at t=1.7', self.s1.run, 5) finally: self.s1.set_protocol(self.p1) self.s1.reset()
def test_restitution(self): # Test the restitution experiment class. with WarningCollector(): import myokit.lib.common as common # Load model m = os.path.join(DIR_DATA, 'lr-1991.mmt') m = myokit.load_model(m) # Create experiment r = common.Restitution(m) # Test r.set_times(300, 800, 200) r.run()
def test_strength_duration(self): # Test the strength-duration experiment class. with WarningCollector(): import myokit.lib.common as common # Load model m = os.path.join(DIR_DATA, 'lr-1991.mmt') m = myokit.load_model(m) # Create experiment s = common.StrengthDuration(m, 'membrane.i_stim') # Test s.set_currents(-200, -10) s.set_times(0.5, 1.0, 0.2) s.run()
def test_units_predefined(self): # Tests parsing all the predefined units def u(units): m = self.parse( '<component name="c">' ' <variable name="x" units="' + units + '"' ' initial_value="1" />' '</component>' ) return m['c']['x'].units().myokit_unit() self.assertEqual(u('ampere'), myokit.units.ampere) self.assertEqual(u('becquerel'), myokit.units.becquerel) self.assertEqual(u('candela'), myokit.units.candela) self.assertEqual(u('coulomb'), myokit.units.coulomb) self.assertEqual(u('dimensionless'), myokit.units.dimensionless) self.assertEqual(u('farad'), myokit.units.farad) self.assertEqual(u('gram'), myokit.units.g) self.assertEqual(u('gray'), myokit.units.gray) self.assertEqual(u('henry'), myokit.units.henry) self.assertEqual(u('hertz'), myokit.units.hertz) self.assertEqual(u('joule'), myokit.units.joule) self.assertEqual(u('katal'), myokit.units.katal) self.assertEqual(u('kelvin'), myokit.units.kelvin) self.assertEqual(u('kilogram'), myokit.units.kg) self.assertEqual(u('liter'), myokit.units.liter) self.assertEqual(u('litre'), myokit.units.liter) self.assertEqual(u('lumen'), myokit.units.lumen) self.assertEqual(u('lux'), myokit.units.lux) self.assertEqual(u('meter'), myokit.units.meter) self.assertEqual(u('metre'), myokit.units.meter) self.assertEqual(u('mole'), myokit.units.mole) self.assertEqual(u('newton'), myokit.units.newton) self.assertEqual(u('ohm'), myokit.units.ohm) self.assertEqual(u('pascal'), myokit.units.pascal) self.assertEqual(u('radian'), myokit.units.radian) self.assertEqual(u('second'), myokit.units.second) self.assertEqual(u('siemens'), myokit.units.siemens) self.assertEqual(u('sievert'), myokit.units.sievert) self.assertEqual(u('steradian'), myokit.units.steradian) self.assertEqual(u('tesla'), myokit.units.tesla) self.assertEqual(u('volt'), myokit.units.volt) self.assertEqual(u('watt'), myokit.units.watt) self.assertEqual(u('weber'), myokit.units.weber) # Celsius is not supported with WarningCollector() as w: self.assertEqual(u('celsius'), myokit.units.dimensionless) self.assertIn('celsius', w.text())
def test_unit_conversion(self): # Tests exporting a model that requires unit conversion # Export model m = myokit.parse_model(units_model) e = myokit.formats.easyml.EasyMLExporter() with TemporaryDirectory() as d: path = d.path('easy.model') e.model(path, m) with open(path, 'r') as f: observed = f.read().strip().splitlines() # Get expected output expected = units_output.strip().splitlines() # Compare (line by line, for readable output) for ob, ex in zip(observed, expected): self.assertEqual(ob, ex) self.assertEqual(len(observed), len(expected)) # Test warnings are raised if conversion fails m.get('membrane.V').set_rhs('hh.I1 + mm.I2') m.get('membrane').remove_variable(m.get('membrane.C')) with TemporaryDirectory() as d: path = d.path('easy.model') with WarningCollector() as c: e.model(path, m) self.assertIn('Unable to convert hh.I1', c.text()) self.assertIn('Unable to convert mm.I2', c.text()) m.get('engine.time').set_unit(myokit.units.cm) with TemporaryDirectory() as d: path = d.path('easy.model') with WarningCollector() as c: e.model(path, m) self.assertIn('Unable to convert time units [cm]', c.text())
def test_basic(self): # Test basic usage. # Load model m, p, _ = myokit.load(os.path.join(DIR_DATA, 'lr-1991.mmt')) n = m.count_states() # Run a simulation with WarningCollector() as c: s = myokit.ICSimulation(m, p) self.assertIn('`ICSimulation` is deprecated', c.text()) self.assertEqual(s.time(), 0) self.assertEqual(s.state(), m.state()) self.assertEqual(s.default_state(), m.state()) self.assertTrue(np.all(s.derivatives() == np.eye(n))) d, e = s.run(20, log_interval=5) self.assertEqual(s.time(), 20) self.assertNotEqual(s.state(), m.state()) self.assertEqual(s.default_state(), m.state()) self.assertFalse(np.all(s.derivatives() == np.eye(n))) # Create a datablock from the simulation log b = s.block(d, e) # Calculate eigenvalues b.eigenvalues('derivatives') # Log with missing time value d2 = d.clone() del(d2['engine.time']) self.assertRaisesRegex(ValueError, 'time', s.block, d2, e) # Wrong size derivatives array self.assertRaisesRegex(ValueError, 'shape', s.block, d, e[:-1]) # Time can't be negative self.assertRaises(ValueError, s.run, -1) # Test running without a protocol s.set_protocol(None) s.run(1) # Test step size is > 0 self.assertRaises(ValueError, s.set_step_size, 0) # Test negative log interval is ignored s.run(1, log_interval=-1)
def test_unit(self): # Test the unit() method that returns a unit conversion factor. with WarningCollector(): import myokit.lib.multi as multi m = myokit.Model() c = m.add_component('c') x = c.add_variable('x') x.set_rhs(0) x.set_unit(myokit.parse_unit('mV')) self.assertRaises(myokit.IncompatibleModelError, multi.unit, x, myokit.parse_unit('kg')) self.assertEqual(multi.unit(x, myokit.parse_unit('V')), 0.001) self.assertEqual(multi.unit(x, myokit.parse_unit('mV')), 1) self.assertEqual(multi.unit(x, myokit.parse_unit('uV')), 1000)
def test_periodic(self): # Test periodic logging. m, p, x = myokit.load(os.path.join(DIR_DATA, 'lr-1991.mmt')) with WarningCollector(): s = myokit.ICSimulation(m, p) # Set tolerance for equality testing emax = 1e-2 # Time steps for logging are approximate # Test 1: Simple 5 ms simulation, log_interval 0.5 ms d, e = s.run(5, log=['engine.time'], log_interval=0.5) d = d.npview() t = d['engine.time'] q = np.arange(0, 5, 0.5) if debug: print(t) print(q) print('- ' * 10) self.assertEqual(len(t), len(q)) self.assertTrue(np.max(np.abs(t - q)) < emax) # Test 2: Very short simulation s.reset() d, e = s.run(1, log=['engine.time'], log_interval=0.5) d = d.npview() t = d['engine.time'] q = np.arange(0, 1, 0.5) if debug: print(t) print(q) print('- ' * 10) self.assertEqual(len(t), len(q)) self.assertTrue(np.max(np.abs(t - q)) < emax) # Test 3: Stop and start a simulation s.reset() d, e = s.run(1, log=['engine.time'], log_interval=0.5) d, e = s.run(2, log=d, log_interval=0.5) d, e = s.run(2, log=d, log_interval=0.5) d = d.npview() t = d['engine.time'] q = np.arange(0, 5, 0.5) if debug: print(t) print(q) print('- ' * 10) self.assertEqual(len(t), len(q)) self.assertTrue(np.max(np.abs(t - q)) < emax)
def test_from_log(self): # Test some edge cases of `DataBlock1d.from_log`. # Test valid construction d = myokit.DataLog() d.set_time_key('time') d['time'] = [1, 2, 3] d['x', 0] = [0, 0, 0] d['x', 1] = [1, 1, 2] myokit.DataBlock1d.from_log(d) # Deprecated alias with WarningCollector() as wc: myokit.DataBlock1d.from_DataLog(d) self.assertIn('deprecated', wc.text()) # No time key d = myokit.DataLog() d['time'] = [1, 2, 3] d['x', 0] = [0, 0, 0] d['x', 1] = [1, 1, 2] self.assertRaises(ValueError, myokit.DataBlock1d.from_log, d) # Multi-dimensional time key d.set_time_key('0.x') self.assertRaises(ValueError, myokit.DataBlock1d.from_log, d) # 2-dimensional stuff d.set_time_key('time') myokit.DataBlock1d.from_log(d) d['y', 0, 0] = [10, 10, 10] self.assertRaises(ValueError, myokit.DataBlock1d.from_log, d) # Mismatched dimensions d = myokit.DataLog() d.set_time_key('time') d['time'] = [1, 2, 3] d['x', 0] = [0, 0, 0] d['x', 1] = [1, 1, 2] d['y', 0] = [2, 0, 0] d['y', 1] = [3, 1, 2] d['y', 2] = [0, 4, 5] self.assertRaises(ValueError, myokit.DataBlock1d.from_log, d)
def test_progress_reporter(self): # Test running with a progress reporter. m, p, x = myokit.load(os.path.join(DIR_DATA, 'lr-1991.mmt')) # Test using a progress reporter with WarningCollector() as c: s = myokit.ICSimulation(m, p) with myokit.tools.capture() as c: s.run(110, progress=myokit.ProgressPrinter()) c = c.text().splitlines() self.assertTrue(len(c) > 0) # Not a progress reporter self.assertRaisesRegex( ValueError, 'ProgressReporter', s.run, 5, progress=12) # Cancel from reporter self.assertRaises( myokit.SimulationCancelledError, s.run, 1, progress=CancellingReporter(0))
def test_scandir(self): # Test the scandir() method that returns a models list and a protocols # list. with WarningCollector(): import myokit.lib.multi as multi # Get list of models and protocols models, protocols = multi.scandir(DIR_MULTI) self.assertEqual(len(models), len(protocols), 2) # Should contain (ordered by model name) # (Lr1991, protocol) --> Upper case goes first # (Beeler (no name), None) # self.assertEqual(type(models[0]), myokit.Model) self.assertEqual(type(models[1]), myokit.Model) self.assertEqual(models[0].name(), 'Luo-Rudy model (1991)') self.assertEqual(models[1].name(), 'beeler-no-name') self.assertEqual(type(protocols[0]), myokit.Protocol) self.assertIsNone(protocols[1])
def test_log_for_interval(self): # Tests the method Protocol.log_for_interval() # Relies on PacingSystem debug = False # Test basic use + log creation methods p = myokit.Protocol() p.schedule(1, 10, 1, 1000, 0) d = p.log_for_interval(0, 3000) self.assertEqual(d.time(), [0, 10, 11, 1010, 1011, 2010, 2011, 3000]) d = p.log_for_interval(0, 2000, for_drawing=True) if debug: import matplotlib.pyplot as plt plt.figure() plt.plot(d.time(), d['pace']) plt.show() self.assertEqual(d.time(), [0, 10, 10, 11, 11, 1010, 1010, 1011, 1011, 2000]) p = myokit.Protocol() p.schedule(1, 0, 1, 1000, 0) d = p.log_for_interval(0, 3000) self.assertEqual(d.time(), [0, 1, 1000, 1001, 2000, 2001, 3000]) d = p.log_for_interval(0, 2000, for_drawing=True) if debug: import matplotlib.pyplot as plt plt.figure() plt.plot(d.time(), d['pace']) plt.show() self.assertEqual(d.time(), [0, 1, 1, 1000, 1000, 1001, 1001, 2000, 2000]) # Test bad interval call self.assertRaises(ValueError, p.log_for_interval, 100, 0) # Test deprecated alias with WarningCollector() as w: p.create_log_for_interval(0, 2000, for_drawing=True) self.assertIn('deprecated', w.text())