class Test0TaggedSet(unittest.TestCase): def test_init(self): ls = tl.TaggedSet() ls = tl.TaggedSet(zip([1, 2, 3], [["a", "b"], "a", ["b", "c"]])) self.assertListEqual(ls._data, [1, 2, 3]) self.assertListEqual(ls._tags, [{"a", "b"}, {"a"}, {"b", "c"}])
return cnt def setUp(self): ds = tl.TaggedSet() ds.add(tl.Trajectory.fromArray([0, 0.75, 0.5, 0.3, 5.4, 5.5, 5.3, -2.0, 5.4])) ds.add(tl.Trajectory.fromArray([1.2, 1.4, np.nan, np.nan, 10.0, 10.2]))
def setUp(self): def traj(): return tl.Trajectory.fromArray( np.cumsum(np.random.normal(size=(10, 3)), axis=0)) self.data = tl.TaggedSet((traj() for _ in range(10)), hasTags=False) self.fit = msdfit.lib.SplineFit(self.data, ss_order=1, n=2)
class TestUtilClean(myTestCase): def setUp(self): # Split with threshold = 2 => trajectories of lengths [2, 3, 3] self.traj = tl.Trajectory.fromArray([1., 2, 4.1, 4.5, 3, -0.5, -1, -0.7]) # Split with threshold = 3 => 3 trajectories with lengths [4, 5, 6] self.ds = tl.TaggedSet()
self.assertListEqual(self.ls._data, [2, 3, 4]) def test_process(self): # need a TaggedSet of mutable objects mutls = tl.TaggedSet(zip(['hello', 'World', '!'], [["a", "b"], "a", ["b", "c"]])) mutls.makeSelection(tags="a") newls = mutls.process(lambda word : word+'_moo')
self.assertListEqual(newls._data, ['hello_moo', 'World_moo']) def test_map_unique(self): def funTrue(x): return True def fun2(x): return 2*x self.assertTrue(self.ls.map_unique(funTrue)) with self.assertRaises(RuntimeError):
self.assertEqual(self.ls[1], 2) def test_mergein(self): newls = tl.TaggedSet(zip([4, 5, 6], [["d"], [], "e"])) self.ls.mergein(newls, additionalTags='new') self.assertListEqual(self.ls._data, [1, 2, 3, 4, 5, 6]) self.assertSetEqual(self.ls.tagset(), {'a', 'b', 'c', 'd', 'e', 'new'}) self.ls |= newls self.assertEqual(len(self.ls), 9)
self.ds.add(tl.Trajectory.fromArray([1.2, 1.4, np.nan, np.nan, 10.0, 10.2])) def test_csv(self): filename = "testdata/test_write.csv" tl.io.write.csv(self.ds, filename) with open(filename, 'r') as f: self.assertTrue(f.read() == 'id\tframe\tx\n0\t0\t0.0\n0\t1\t0.75\n0\t2\t0.5\n0\t3\t0.3\n0\t4\t5.4\n0\t5\t5.5\n0\t6\t5.3\n0\t7\t-2.0\n0\t8\t5.4\n1\t0\t1.2\n1\t1\t1.4\n1\t4\t10.0\n1\t5\t10.2\n') ds = tl.TaggedSet() ds.add(tl.Trajectory.fromArray(np.arange(20).reshape((2, 5, 2)))) tl.io.write.csv(ds, filename)
def setUp(self): model = tl.models.rouse.Model(10) tracked = [2, 7] def traj(): conf = model.conf_ss() traj = [] for _ in range(10): conf = model.evolve(conf) traj.append(conf[tracked[0] - tracked[1]]) return tl.Trajectory.fromArray(traj) self.data = tl.TaggedSet((traj() for _ in range(10)), hasTags=False)
def test_fitting_rouse(self): modelfam = neda.ParametricFamily((0.1, ), [(1e-10, None)]) modelfam.get = lambda k: neda.models.RouseModel(20, 1, k) true_params = (1, ) data = tl.TaggedSet() mod_gen = modelfam.get(*true_params) for state in 3 * [0, 1]: lt = neda.Loopingtrace.fromStates(100 * [state]) data.add( mod_gen.trajectory_from_loopingtrace(lt, localization_error=0.1)) fitres = neda.models.fit(data, modelfam, maxfun=200) self.assertTrue(fitres.success) for true_param, est_param in zip(true_params, fitres.x): self.assertAlmostEqual(true_param, est_param, delta=0.5)
def test_fitting_factorized(self): modelfam = neda.ParametricFamily((0.1, 10), [(1e-10, None), (1e-10, None)]) modelfam.get = lambda s0, s1: neda.models.FactorizedModel([ scipy.stats.maxwell(scale=s0), scipy.stats.maxwell(scale=s1), ]) true_params = (1, 4) data = tl.TaggedSet() mod_gen = modelfam.get(*true_params) for state in 3 * [0, 1]: lt = neda.Loopingtrace.fromStates(100 * [state]) data.add(mod_gen.trajectory_from_loopingtrace(lt)) fitres = neda.models.fit(data, modelfam, maxfun=200) self.assertTrue(fitres.success) for true_param, est_param in zip(true_params, fitres.x): self.assertAlmostEqual(true_param, est_param, delta=0.5)
class TestIOWrite(myTestCase): def setUp(self): self.ds = tl.TaggedSet()
class Test1TaggedSet(unittest.TestCase):
ls = tl.TaggedSet([1, 2, 3], hasTags=False) def test_add(self): ls = tl.TaggedSet() ls.add(1)
def setUp(self): def traj(): return tl.Trajectory.fromArray( np.cumsum(np.random.normal(size=(10, 3)), axis=0)) self.data = tl.TaggedSet((traj() for _ in range(10)), hasTags=False)