Пример #1
0
    def test_init_weights(self):
        net = WTNetwork([[1]])
        self.assertEqual(1, net.size)
        self.assertTrue(np.array_equal([[1]], net.weights))
        self.assertTrue(np.array_equal([0], net.thresholds))
        self.assertIsNone(net.names)

        net = WTNetwork([[1, 0], [0, 1]])
        self.assertEqual(2, net.size)
        self.assertTrue(np.array_equal([[1, 0], [0, 1]], net.weights))
        self.assertTrue(np.array_equal([0, 0], net.thresholds))
        self.assertIsNone(net.names)

        net = WTNetwork([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
        self.assertEqual(3, net.size)
        self.assertTrue(np.array_equal(
            [[1, 0, 0], [0, 1, 0], [0, 0, 1]], net.weights))
        self.assertTrue(np.array_equal([0, 0, 0], net.thresholds))
        self.assertIsNone(net.names)

        net = WTNetwork(1)
        self.assertEqual(1, net.size)
        self.assertTrue(np.array_equal([[0]], net.weights))
        self.assertTrue(np.array_equal([0], net.thresholds))
        self.assertIsNone(net.names)

        net = WTNetwork(2)
        self.assertEqual(2, net.size)
        self.assertTrue(np.array_equal([[0, 0], [0, 0]], net.weights))
        self.assertTrue(np.array_equal([0, 0], net.thresholds))
        self.assertIsNone(net.names)
Пример #2
0
    def test_fission_yeast_numpy(self):
        net = WTNetwork(
            [[-1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
             [0.0, 0.0, -1.0, -1.0, -1.0, 0.0, 0.0, 0.0, 0.0],
             [-1.0, -1.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 1.0],
             [-1.0, -1.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 1.0],
             [0.0, 0.0, 0.0, 0.0, -1.0, 1.0, 0.0, 0.0, 0.0],
             [0.0, 0.0, -1.0, -1.0, -1.0, 0.0, -1.0, 1.0, 0.0],
             [0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0],
             [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0],
             [0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, -1.0]],
            [0.0, -0.5, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0])

        self.assertEqual(9, net.size)
        self.assertEqual(512, len(list(net)))

        init = np.asarray([1, 0, 1, 1, 0, 0, 1, 0, 0])
        bio_sequence = np.asarray([[0, 0, 0, 0, 0, 0, 1, 0, 0],
                                   [0, 1, 0, 0, 0, 0, 1, 0, 0],
                                   [0, 1, 0, 0, 0, 0, 0, 1, 0],
                                   [0, 1, 0, 0, 0, 1, 0, 1, 0],
                                   [0, 1, 0, 0, 1, 1, 0, 1, 0],
                                   [0, 0, 0, 0, 1, 0, 0, 1, 1],
                                   [0, 0, 1, 1, 0, 0, 1, 0, 1],
                                   [0, 0, 1, 1, 0, 0, 1, 0, 0]])

        for expected in bio_sequence:
            self.assertTrue(np.array_equal(expected, net.update(init)))
Пример #3
0
 def test_different_matrix_without_trans(self):
     net = WTNetwork([[1, -1], [0, 1]], [0.5, 0])
     trans = list(map(net.decode, net.transitions))
     for state in net:
         with_trans = net.difference_matrix(state[:], transitions=trans)
         without_trans = net.difference_matrix(state[:])
         self.assertTrue(np.allclose(with_trans, without_trans, atol=1e-6))
Пример #4
0
 def test_update_values_none(self):
     net = WTNetwork([[1, 0], [-1, 1]], [0.5, 0.0],
                     theta=WTNetwork.positive_threshold)
     xs = [0, 0]
     self.assertEqual([0, 1], net.update(xs, values=None))
     xs = [0, 0]
     self.assertEqual([0, 1], net.update(xs, values={}))
Пример #5
0
    def test_fission_yeast(self):
        net = WTNetwork(
            [[-1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
             [0.0, 0.0, -1.0, -1.0, -1.0, 0.0, 0.0, 0.0, 0.0],
             [-1.0, -1.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 1.0],
             [-1.0, -1.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 1.0],
             [0.0, 0.0, 0.0, 0.0, -1.0, 1.0, 0.0, 0.0, 0.0],
             [0.0, 0.0, -1.0, -1.0, -1.0, 0.0, -1.0, 1.0, 0.0],
             [0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0],
             [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0],
             [0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, -1.0]],
            [0.0, -0.5, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0])

        self.assertEqual(9, net.size)
        self.assertEqual(512, len(list(net)))

        init = [1, 0, 1, 1, 0, 0, 1, 0, 0]
        bio_sequence = [[0, 0, 0, 0, 0, 0, 1, 0, 0],
                        [0, 1, 0, 0, 0, 0, 1, 0, 0],
                        [0, 1, 0, 0, 0, 0, 0, 1, 0],
                        [0, 1, 0, 0, 0, 1, 0, 1, 0],
                        [0, 1, 0, 0, 1, 1, 0, 1, 0],
                        [0, 0, 0, 0, 1, 0, 0, 1, 1],
                        [0, 0, 1, 1, 0, 0, 1, 0, 1],
                        [0, 0, 1, 1, 0, 0, 1, 0, 0]]

        for expected in bio_sequence:
            self.assertEqual(expected, net.update(init))
Пример #6
0
 def test_state_space(self):
     net = WTNetwork([[1]])
     self.assertEqual(2, len(list(net)))
     net = WTNetwork([[1, 0], [1, 1]])
     self.assertEqual(4, len(list(net)))
     net = WTNetwork([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
     self.assertEqual(8, len(list(net)))
Пример #7
0
 def test_average_difference_matrix_without_calc(self):
     net = WTNetwork([[1, -1], [0, 1]], [0.5, 0])
     states = list(net)
     with_calc = net.average_sensitivity(states=deepcopy(states),
                                         calc_trans=True)
     without_calc = net.average_sensitivity(states=deepcopy(states),
                                            calc_trans=False)
     self.assertTrue(np.allclose(with_calc, without_calc, atol=1e-6))
Пример #8
0
    def test_split_threshold(self):
        xs = [0, 0, 0]
        self.assertEqual(
            [1, 0, 0], WTNetwork.split_threshold([1, -1, 0], xs))
        self.assertEqual([1, 0, 0], xs)

        xs = [1, 1, 1]
        self.assertEqual(
            [1, 0, 1], WTNetwork.split_threshold([1, -1, 0], xs))
        self.assertEqual([1, 0, 1], xs)
Пример #9
0
    def test_positive_threshold(self):
        xs = [0, 0, 0]
        self.assertEqual(
            [1, 0, 1], WTNetwork.positive_threshold([1, -1, 0], xs))
        self.assertEqual([1, 0, 1], xs)

        xs = [1, 1, 1]
        self.assertEqual(
            [1, 0, 1], WTNetwork.positive_threshold([1, -1, 0], xs))
        self.assertEqual([1, 0, 1], xs)
Пример #10
0
    def test_update_values(self):
        net = WTNetwork([[1, 0], [-1, 1]], [0.5, 0.0],
                        theta=WTNetwork.negative_threshold)

        xs = [1, 1]
        self.assertEqual([1, 1], net.update(xs, values={1: 1}))

        net.theta = WTNetwork.positive_threshold
        xs = [0, 0]
        self.assertEqual([1, 1], net.update(xs, values={0: 1}))
Пример #11
0
 def test_update_values_pin_clash(self):
     net = WTNetwork([[1, 0], [-1, 1]], [0.5, 0.0],
                     theta=WTNetwork.positive_threshold)
     with self.assertRaises(ValueError):
         net.update([0, 0], pin=[0], values={0: 1})
     with self.assertRaises(ValueError):
         net.update([0, 0], pin=[1], values={1: 0})
     with self.assertRaises(ValueError):
         net.update([0, 0], pin=[1], values={0: 0, 1: 0})
     with self.assertRaises(ValueError):
         net.update([0, 0], pin=[1, 0], values={0: 0})
Пример #12
0
    def test_init_thresholds(self):
        with self.assertRaises(TypeError):
            WTNetwork([[1]], theta=5)

        net = WTNetwork([[1]])
        self.assertEqual(WTNetwork.split_threshold, net.theta)

        net = WTNetwork([[1]], theta=WTNetwork.negative_threshold)
        self.assertEqual(WTNetwork.negative_threshold, net.theta)

        net = WTNetwork([[1]], theta=WTNetwork.positive_threshold)
        self.assertEqual(WTNetwork.positive_threshold, net.theta)
Пример #13
0
    def test_neighbors_both_split_threshold(self):

        net = WTNetwork(
            [[-1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
             [0.0, 0.0, -1.0, -1.0, -1.0, 0.0, 0.0, 0.0, 0.0],
             [-1.0, -1.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 1.0],
             [-1.0, -1.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 1.0],
             [0.0, 0.0, 0.0, 0.0, -1.0, 1.0, 0.0, 0.0, 0.0],
             [0.0, 0.0, -1.0, -1.0, -1.0, 0.0, -1.0, 1.0, 0.0],
             [0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0],
             [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0],
             [0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, -1.0]],
            [0.0, -0.5, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0])

        self.assertEqual(net.neighbors(2), set([0, 1, 2, 5, 8]))
Пример #14
0
    def test_neighbors_out_negative_threshold(self):

        net = WTNetwork(
            [[-1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
             [0.0, 0.0, -1.0, -1.0, -1.0, 0.0, 0.0, 0.0, 0.0],
             [-1.0, -1.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 1.0],
             [-1.0, -1.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 1.0],
             [0.0, 0.0, 0.0, 0.0, -1.0, 1.0, 0.0, 0.0, 0.0],
             [0.0, 0.0, -1.0, -1.0, -1.0, 0.0, -1.0, 1.0, 0.0],
             [0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0],
             [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0],
             [0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, -1.0]],
            [0.0, -0.5, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0],
            theta=WTNetwork.negative_threshold)

        self.assertEqual(net.neighbors_out(2), set([1, 5]))
Пример #15
0
 def test_update_pin_index_clash(self):
     net = WTNetwork([[1, 0], [-1, 1]], [0.5, 0.0],
                     theta=WTNetwork.positive_threshold)
     with self.assertRaises(ValueError):
         net.update([0, 0], index=0, pin=[1])
     with self.assertRaises(ValueError):
         net.update([0, 0], index=1, pin=[1])
     with self.assertRaises(ValueError):
         net.update([0, 0], index=1, pin=[0, 1])
Пример #16
0
    def test_transitions_wtnetwork(self):
        net = WTNetwork(
            weights=[[1, 0], [-1, 1]],
            thresholds=[0.5, 0.0],
            theta=WTNetwork.positive_threshold
        )

        self.assertEqual([2, 1, 2, 3], list(net.transitions))
Пример #17
0
    def test_transitions_wtnetwork(self):
        net = WTNetwork(weights=[[1, 0], [-1, 1]],
                        thresholds=[0.5, 0.0],
                        theta=WTNetwork.positive_threshold)

        landscape = Landscape(net)
        self.assertEqual(net, landscape.network)
        self.assertEqual(2, landscape.size)
        self.assertEqual([2, 1, 2, 3], list(landscape.transitions))
Пример #18
0
    def test_transitions_wtnetwork(self):
        """
        test ``transitions`` on WTNetworks
        """
        net = WTNetwork(weights=[[1, 0], [-1, 1]],
                        thresholds=[0.5, 0.0],
                        theta=WTNetwork.positive_threshold)

        got = transitions(net)
        self.assertEqual([[0, 1], [1, 0], [0, 1], [1, 1]], got)
Пример #19
0
    def test_transitions_wtnetwork_encoded(self):
        """
        test ``transitions`` on WTNetworks; encoding the states
        """
        net = WTNetwork(weights=[[1, 0], [-1, 1]],
                        thresholds=[0.5, 0.0],
                        theta=WTNetwork.positive_threshold)

        got = transitions(net, encode=True)
        self.assertEqual([2, 1, 2, 3], got)
Пример #20
0
 def test_positive_threshold_scalar(self):
     test = {
         (1, 0): 1,
         (0, 0): 1,
         (-1, 0): 0,
         (1, 1): 1,
         (0, 1): 1,
         (-1, 1): 0,
     }
     for x, s in test:
         self.assertEqual(
             test[(x, s)], WTNetwork.positive_threshold(x, s))
Пример #21
0
    def test_neighbors_in_split_threshold(self):

        net = WTNetwork(
            [[-1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
             [0.0, 0.0, -1.0, -1.0, -1.0, 0.0, 0.0, 0.0, 0.0],
             [-1.0, -1.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 1.0],
             [-1.0, -1.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 1.0],
             [0.0, 0.0, 0.0, 0.0, -1.0, 1.0, 0.0, 0.0, 0.0],
             [0.0, 0.0, -1.0, -1.0, -1.0, 0.0, -1.0, 1.0, 0.0],
             [0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0],
             [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0],
             [0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, -1.0]],
            [0.0, -0.5, 0.0, 0.0, 0.0, 0.5, 0.0, 0.0, 0.0])

        self.assertEqual(net.neighbors_in(2), set([0, 1, 2, 5, 8]))

        with self.assertRaises(IndexError):
            self.assertEqual(net.neighbors_in(2.0))

        with self.assertRaises(IndexError):
            self.assertEqual(net.neighbors_in('2'))
Пример #22
0
 def test_update_pin_invalid_indicies(self):
     net = WTNetwork([[1, 0], [-1, 1]], [0.5, 0.0],
                     theta=WTNetwork.positive_threshold)
     with self.assertRaises(IndexError):
         net.update([0, 0], values={-3: 0})
     with self.assertRaises(IndexError):
         net.update([0, 0], values={2: 0})
Пример #23
0
 def test_update_invalid_values(self):
     net = WTNetwork([[1, 0], [-1, 1]], [0.5, 0.0],
                     theta=WTNetwork.positive_threshold)
     with self.assertRaises(ValueError):
         net.update([0, 0], values={0: 2})
     with self.assertRaises(ValueError):
         net.update([0, 0], values={0: -1})
Пример #24
0
    def test_transitions_wtnetwork_pin(self):
        net = WTNetwork(
            weights=[[1, 0], [-1, 1]],
            thresholds=[0.5, 0.0],
            theta=WTNetwork.positive_threshold
        )

        net.landscape(pin=[1])
        self.assertEqual([0, 1, 2, 3], list(net.transitions))

        net.landscape(pin=[0])
        self.assertEqual([2, 1, 2, 3], list(net.transitions))

        net.landscape(pin=None)
        self.assertEqual([2, 1, 2, 3], list(net.transitions))
Пример #25
0
    def test_transitions_wtnetwork_values(self):
        net = WTNetwork(
            weights=[[1, 0], [-1, 1]],
            thresholds=[0.5, 0.0],
            theta=WTNetwork.positive_threshold
        )

        net.landscape(values={0: 1})
        self.assertEqual([3, 1, 3, 3], list(net.transitions))

        net.landscape(values={1: 0})
        self.assertEqual([0, 1, 0, 1], list(net.transitions))

        net.landscape(values={})
        self.assertEqual([2, 1, 2, 3], list(net.transitions))
Пример #26
0
    def test_trajectory_wtnetwork(self):
        net = WTNetwork(weights=[[1, 0], [-1, 0]],
                        thresholds=[0.5, 0.0],
                        theta=WTNetwork.positive_threshold)

        landscape = Landscape(net)

        state = [0, 0]
        got = landscape.trajectory(state)
        self.assertEqual([0, 0], state)
        self.assertEqual([[0, 0], [0, 1]], got)

        got = landscape.trajectory(state, timesteps=3)
        self.assertEqual([0, 0], state)
        self.assertEqual([[0, 0], [0, 1], [0, 1], [0, 1]], got)

        got = landscape.trajectory(state, encode=False)
        self.assertEqual([0, 0], state)
        self.assertEqual([[0, 0], [0, 1]], got)

        got = landscape.trajectory(state, timesteps=3, encode=False)
        self.assertEqual([0, 0], state)
        self.assertEqual([[0, 0], [0, 1], [0, 1], [0, 1]], got)

        got = landscape.trajectory(state, encode=True)
        self.assertEqual([0, 0], state)
        self.assertEqual([0, 2], got)

        got = landscape.trajectory(state, timesteps=3, encode=True)
        self.assertEqual([0, 0], state)
        self.assertEqual([0, 2, 2, 2], got)

        state = 0
        got = landscape.trajectory(state)
        self.assertEqual([0, 2], got)

        got = landscape.trajectory(state, timesteps=3)
        self.assertEqual([0, 2, 2, 2], got)

        got = landscape.trajectory(state, encode=True)
        self.assertEqual([0, 2], got)

        got = landscape.trajectory(state, timesteps=3, encode=True)
        self.assertEqual([0, 2, 2, 2], got)

        got = landscape.trajectory(state, encode=False)
        self.assertEqual([[0, 0], [0, 1]], got)

        got = landscape.trajectory(state, timesteps=3, encode=False)
        self.assertEqual([[0, 0], [0, 1], [0, 1], [0, 1]], got)
Пример #27
0
    def test_update_pin(self):
        net = WTNetwork([[1, 0], [-1, 1]], [0.5, 0.0])

        net.theta = WTNetwork.negative_threshold
        xs = [1, 1]
        self.assertEqual([1, 0], net.update(xs, pin=[0]))

        net.theta = WTNetwork.positive_threshold
        xs = [0, 0]
        self.assertEqual([0, 0], net.update(xs, pin=[1]))
Пример #28
0
    def test_trajectory_wtnetwork(self):
        """
        test ``trajectory`` on WTNetworks
        """
        net = WTNetwork(weights=[[1, 0], [-1, 0]],
                        thresholds=[0.5, 0.0],
                        theta=WTNetwork.positive_threshold)

        state = [0, 0]
        got = trajectory(net, state)
        self.assertEqual([0, 0], state)
        self.assertEqual([[0, 0], [0, 1]], got)

        got = trajectory(net, state, timesteps=3)
        self.assertEqual([0, 0], state)
        self.assertEqual([[0, 0], [0, 1], [0, 1], [0, 1]], got)
Пример #29
0
    def test_trajectory_wtnetwork_encoded(self):
        """
        test ``trajectory`` on WTNetworks; encoding the states
        """
        net = WTNetwork(weights=[[1, 0], [-1, 0]],
                        thresholds=[0.5, 0.0],
                        theta=WTNetwork.positive_threshold)

        state = [0, 0]
        got = trajectory(net, state, encode=True)
        self.assertEqual([0, 0], state)
        self.assertEqual([0, 2], got)

        got = trajectory(net, state, timesteps=3, encode=True)
        self.assertEqual([0, 0], state)
        self.assertEqual([0, 2, 2, 2], got)
Пример #30
0
    def test_init_names(self):
        with self.assertRaises(TypeError):
            WTNetwork([[1]], names=5)

        with self.assertRaises(ValueError):
            WTNetwork([[1]], names=["A", "B"])

        with self.assertRaises(ValueError):
            WTNetwork([[1]], names=["A", "B"])

        net = WTNetwork([[1]], names=["A"])
        self.assertEqual(['A'], net.names)

        net = WTNetwork([[1, 0], [1, 1]], names=['A', 'B'])
        self.assertEqual(['A', 'B'], net.names)

        net = WTNetwork([[1]], names="A")
        self.assertEqual(['A'], net.names)

        net = WTNetwork([[1, 0], [1, 1]], names="AB")
        self.assertEqual(['A', 'B'], net.names)