def test_keys(self): # Test the keys0d() and keys1d() methods. w = 2 time = [1, 2, 3] b1 = myokit.DataBlock1d(w, time) # Test keys0d self.assertEqual(len(list(b1.keys0d())), 0) pace = np.array([0, 0, 2]) b1.set0d('pace', pace) self.assertEqual(len(list(b1.keys0d())), 1) self.assertIn('pace', b1.keys0d()) b1.set0d('poos', pace) self.assertEqual(len(list(b1.keys0d())), 2) self.assertIn('poos', b1.keys0d()) # Test keys1d self.assertEqual(len(list(b1.keys1d())), 0) x = np.array([[1, 1], [2, 2], [3, 3]]) b1.set1d('x', x) self.assertEqual(len(list(b1.keys1d())), 1) y = np.array([[2, 1], [3, 2], [4, 3]]) b1.set1d('y', y) self.assertEqual(len(list(b1.keys1d())), 2)
def test_trace(self): w = 2 time = [1, 2, 3] b = myokit.DataBlock1d(w, time) x = np.array([[1, 4], [2, 5], [3, 6]]) b.set1d('x', x) self.assertTrue(np.all(b.trace('x', 0) == [1, 2, 3])) self.assertTrue(np.all(b.trace('x', 1) == [4, 5, 6]))
def test_remove1d(self): # Test remove1d(). # Add new 0d field and remove again b = myokit.DataBlock1d(2, [1, 2, 3]) self.assertEqual(b.len1d(), 0) b.set1d('z', np.array([[0, 0], [2, 3], [2, 1]])) self.assertEqual(b.len1d(), 1) b.remove1d('z') self.assertEqual(b.len1d(), 0)
def test_remove0d(self): # Test remove0d(). # Add new 0d field and remove again b = myokit.DataBlock1d(2, [1, 2, 3]) self.assertEqual(b.len0d(), 0) b.set0d('pace', np.array([0, 0, 2])) self.assertEqual(b.len0d(), 1) b.remove0d('pace') self.assertEqual(b.len0d(), 0) # Time can't be removed self.assertRaises(KeyError, b.remove0d, 'time')
def test_block2d(self): # Test conversion to 2d. w = 2 time = [1, 2, 3] pace = [0, 0, 2] x = np.array([[1, 1], [2, 2], [3, 3]]) b1 = myokit.DataBlock1d(w, time) b1.set0d('pace', pace) b1.set1d('x', x) b2 = b1.block2d() self.assertTrue(np.all(b2.get0d('pace') == pace)) self.assertTrue(np.all(b2.get2d('x') == x.reshape((3, 1, 2))))
def test_bad_constructor(self): # Test bad arguments to a DataBlock1d raise ValueErrors. # Test valid constructor w = 2 time = [1, 2, 3] myokit.DataBlock1d(w, time) # Test w < 1 self.assertRaises(ValueError, myokit.DataBlock1d, 0, time) # Test time matrix self.assertRaises(ValueError, myokit.DataBlock1d, w, [[1, 2, 3]]) # Decreasing times self.assertRaises(ValueError, myokit.DataBlock1d, w, [3, 2, 1])
def test_to_log(self): # Test the `DataBlock1d.to_log()` method. # Create test block b = myokit.DataBlock1d(3, [1, 2, 3, 4]) b.set0d('pace', [0, 1, 0, 0]) b.set1d('voltage', [[2, 1, 0], [1, 2, 1], [0, 1, 2], [0, 0, 1]]) # Convert and inspect d = b.to_log() self.assertIn('time', d) self.assertIn('pace', d) self.assertIn('0.voltage', d) self.assertIn('1.voltage', d) self.assertIn('2.voltage', d) self.assertEqual(len(d), 5) self.assertEqual(list(d.time()), [1, 2, 3, 4]) self.assertEqual(list(d['pace']), [0, 1, 0, 0]) self.assertEqual(list(d['0.voltage']), [2, 1, 0, 0]) self.assertEqual(list(d['1.voltage']), [1, 2, 1, 0]) self.assertEqual(list(d['2.voltage']), [0, 1, 2, 1])
def test_set1d(self): # Test set1d(). w = 2 time = [1, 2, 3] b1 = myokit.DataBlock1d(w, time) # Test valid call x = np.array([[1, 1], [2, 2], [3, 3]]) b1.set1d('x', x) self.assertTrue(np.all(b1.get1d('x') == x)) # Test copying b1.set1d('x', x, copy=False) self.assertTrue(np.all(b1.get1d('x') == x)) self.assertIs(b1.get1d('x'), x) b1.set1d('x', x, copy=True) self.assertTrue(np.all(b1.get1d('x') == x)) self.assertIsNot(b1.get1d('x'), x) # Test bad calls self.assertRaises(ValueError, b1.set1d, '', x) x = np.array([[1, 1], [2, 2], [3, 3], [4, 4]]) self.assertRaises(ValueError, b1.set1d, 'x', x)
def test_set0d(self): # Test set0d(). w = 2 time = [1, 2, 3] b1 = myokit.DataBlock1d(w, time) # Test valid call pace = np.array([0, 0, 2]) b1.set0d('pace', pace) self.assertTrue(np.all(b1.get0d('pace') == pace)) # Test copying b1.set0d('pace', pace, copy=False) self.assertTrue(np.all(b1.get0d('pace') == pace)) self.assertIs(b1.get0d('pace'), pace) b1.set0d('pace', pace, copy=True) self.assertTrue(np.all(b1.get0d('pace') == pace)) self.assertIsNot(b1.get0d('pace'), pace) # Test bad calls self.assertRaises(ValueError, b1.set0d, '', pace) self.assertRaises(ValueError, b1.set0d, 'pace', [1, 2, 3, 4]) self.assertRaises(ValueError, b1.set0d, 'pace', np.array([1, 2, 3, 4]))