def test_progress_printer(self): # Test basic functionality. # Zero call with myokit.tools.capture() as c: p = myokit.ProgressPrinter() self.assertTrue(p.update(0)) pattern1 = re.compile( r'\[[0-9]{1}\.[0-9]{1} minutes\] [0-9]+(.[0-9])? % done') lines = c.text().splitlines() self.assertTrue(len(lines) > 0) for line in lines: self.assertTrue(pattern1.match(line)) # Normal call with myokit.tools.capture() as c: p = myokit.ProgressPrinter() self.assertTrue(p.update(0.49)) pattern2 = re.compile( r'\[[0-9]{1}\.[0-9]{1} minutes] [0-9]+(.[0-9])? % done,' r' estimated [0-9]{1} seconds remaining') lines = c.text().splitlines() self.assertTrue(len(lines) > 0) for line in lines: self.assertTrue(pattern2.match(line)) # Call that will take minutes with myokit.tools.capture() as c: p = myokit.ProgressPrinter() time.sleep(0.1) self.assertTrue(p.update(1e-3)) self.assertIn('minutes remaining', c.text()) # Note: Printer must be created withing capture, otherwise it # will print to stdout (which won't have been redirected yet). # Status every ten percent with myokit.tools.capture() as c: p = myokit.ProgressPrinter(digits=-1) self.assertTrue(p.update(0)) self.assertTrue(p.update(0.08)) self.assertTrue(p.update(0.18)) self.assertTrue(p.update(0.19)) self.assertTrue(p.update(0.199)) self.assertTrue(p.update(1)) lines = c.text().splitlines() self.assertTrue(len(lines), 3) self.assertTrue(pattern1.match(lines[0])) self.assertTrue(pattern2.match(lines[1])) self.assertTrue(pattern2.match(lines[2]))
def test_progress_reporter(self): # Test running with a progress reporter. # Test if it works sim = myokit.Simulation(self.model, self.protocol) with myokit.tools.capture() as c: sim.run(110, progress=myokit.ProgressPrinter()) c = c.text().splitlines() self.assertEqual(len(c), 2) p = re.compile( re.escape('[0.0 minutes] 1.9 % done, estimated ') + '[0-9]+' + re.escape(' seconds remaining')) self.assertIsNotNone(p.match(c[0])) p = re.compile( re.escape('[0.0 minutes] 100.0 % done, estimated ') + '[0-9]+' + re.escape(' seconds remaining')) self.assertIsNotNone(p.match(c[1])) # Not a progress reporter self.assertRaisesRegex(ValueError, 'ProgressReporter', self.sim.run, 5, progress=12) # Cancel from reporter self.assertRaises(myokit.SimulationCancelledError, self.sim.run, 1, progress=CancellingReporter(0))
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_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_with_progress_reporter(self): # Test running with a progress reporter. m, p, _ = myokit.load(os.path.join(DIR_DATA, 'lr-1991.mmt')) # Test using a progress reporter s = myokit.Simulation1d(m, p, ncells=5) s.set_step_size(0.05) with myokit.PyCapture() 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_run_progress(self): # Tests running with a progress reporter. pass # Run with a progress reporter with myokit.tools.capture() as c: self.s1.run(3, progress=myokit.ProgressPrinter()) self.assertTrue(c.text() != '') # Cancel using a progress reporter self.assertRaises( myokit.SimulationCancelledError, self.s1.run, 20, progress=CancellingReporter(0)) # Run with invalid progress reporter try: self.assertRaisesRegex(ValueError, 'subclass of myokit.ProgressR', self.s1.run, 1, progress=12) finally: self.s1.reset()
def simulate_ohara_ap(drug=None, concentration=0, beats=2, cl=1): """ Generates APs with the O'Hara model. """ # Convert CL to milliseconds cl *= 1000 # Load model model = myokit.load_model('ohara-2011.mmt') # Load protocol protocol = myokit.pacing.blocktrain(period=cl, duration=0.5, offset=50) # Add drug block scalings scales = {} def hill(x, ic50, h=1): return 1 / (1 + (x / ic50)**h) def add(current, x, ic50): f = hill(x, ic50) if current in scales: scales[current] *= f else: scales[current] = f # Add drug scales if drug is None or drug == 'paracetamol': pass elif drug == 'dofetilide': add('ikr.IKr', concentration, 5.2e-3) add('ina.INa', concentration, 147.9) add('ical.ICaL', concentration, 26.7) add('iks.IKs', concentration, 415.8) elif drug == 'quinidine': add('ikr.IKr', concentration, 0.3) add('ina.INa', concentration, 16.6) add('ical.ICaL', concentration, 15.6) elif drug == 'sotalol': add('ikr.IKr', concentration, 111.4) add('ina.INa', concentration, 7013.9) add('ical.ICaL', concentration, 193.3) elif drug == 'sotalol-okada': add('ikr.IKr', concentration, 356.4) #add('ina.INa', concentration, 7013.9) #add('ical.ICaL', concentration, 193.3) elif drug == 'verapamil': add('ikr.IKr', concentration, 0.25) add('ina.INa', concentration, 32.5) add('ical.ICaL', concentration, 0.2) else: raise ValueError('Unknown drug: ' + str(drug)) # Apply scalings for var, scale in scales.iteritems(): v = model.get(var) v.set_rhs(myokit.Multiply(myokit.Number(scale), v.rhs())) # Simulate with modified model sim = myokit.Simulation(model, protocol) # Pre-pace for some beats p = myokit.ProgressPrinter() sim.pre(100 * cl, progress=p) # Log some beats p = myokit.ProgressPrinter() log = sim.run(beats * cl, log=['engine.time', 'membrane.V'], log_interval=DT * 1e3, progress=p).npview() # Convert to seconds and volts log['engine.time'] *= 1e-3 log['membrane.V'] *= 1e-3 # Return return log