Exemple #1
0
def test_possible_complexes(s):
    assert list(compute.possible_complexes(s.network, s.state)) == [
        Subsystem(s.network, s.state, (0, 1, 2)),
        Subsystem(s.network, s.state, (1, 2)),
        Subsystem(s.network, s.state, (0, 2)),
        Subsystem(s.network, s.state, (0, 1)),
        Subsystem(s.network, s.state, (1,)),
    ]
def test_different_states(big):
    all_off = [0] * 5
    all_on = [1] * 5
    s1 = Subsystem(big, all_off, range(2, 5))
    s2 = Subsystem(big, all_on, range(2, 5))
    a = s1.nodes[2:]
    b = s2.nodes[2:]
    x = cc.NormalizedMechanism(a, s1)
    y = cc.NormalizedMechanism(b, s2)
    assert x != y
def test_different_states(big):
    all_off = Network(big.tpm, tuple([0]*5), tuple([0]*5),
                      connectivity_matrix=big.connectivity_matrix)
    all_on = Network(big.tpm, tuple([1]*5), tuple([1]*5),
                     connectivity_matrix=big.connectivity_matrix)
    s1 = Subsystem(range(2, 5), all_off)
    s2 = Subsystem(range(2, 5), all_on)
    a = s1.nodes[2:]
    b = s2.nodes[2:]
    x = cc.NormalizedMechanism(a, s1)
    y = cc.NormalizedMechanism(b, s2)
    assert x != y
def test_validate_state_no_error_2():
    tpm = np.array([
        [1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1],
        [1, 1, 1, 1],
    ])
    net = Network(tpm)
    # Globally impossible state.
    state = (1, 1, 0, 0)
    # But locally possible for first two nodes.
    subsystem = Subsystem(net, state, (0, 1))
    validate.state_reachable(subsystem)
Exemple #5
0
def test_reconstitute_tpm(standard, s_complete, rule152, noised):
    # Check subsystem and network TPM are the same when the subsystem is the
    # whole network
    assert np.array_equal(reconstitute_tpm(s_complete), standard.tpm)

    # Regression tests
    answer = np.array([[[[0., 0., 0.], [0., 0., 0.]],
                        [[0., 0., 1.], [0., 1., 0.]]],
                       [[[0., 1., 0.], [0., 0., 0.]],
                        [[1., 0., 1.], [1., 1., 0.]]]])
    subsystem = Subsystem(rule152, (0, ) * 5, (0, 1, 2))
    assert np.array_equal(answer, reconstitute_tpm(subsystem))

    subsystem = Subsystem(noised, (0, 0, 0), (0, 1))
    answer = np.array([[[0., 0.], [0.7, 0.]], [[0., 0.], [1., 0.]]])
    assert np.array_equal(answer, reconstitute_tpm(subsystem))
Exemple #6
0
def test_only_cache_uncut_subsystem_mices(redis_cache, flush_redis, s):
    s = Subsystem(s.network, (1, 0, 0),
                  s.node_indices,
                  cut=models.Cut((1, ), (0, 2)))
    mechanism = (1, )  # has a core cause
    s.find_mice('past', mechanism)
    # don't cache anything because subsystem is cut
    assert s._mice_cache.size() == 0
def test_inherited_cache_must_come_from_uncut_subsystem(redis_cache):
    s = examples.basic_subsystem()
    cut_s = Subsystem(s.network,
                      s.state,
                      s.node_indices,
                      cut=models.Cut((0, 2), (1, )))
    with pytest.raises(ValueError):
        cache.MICECache(s, cut_s._mice_cache)
Exemple #8
0
    def setup(self, mode, network):
        if network == 'basic':
            self.subsys = examples.basic_subsystem()
        elif network == 'rule154':
            network = examples.rule154_network()
            state = (1,) * 5
            self.subsys = Subsystem(network, state, network.node_indices)
        elif network == 'fig16':
            network = examples.fig16()
            state = (0,) * 7
            self.subsys = Subsystem(network, state, network.node_indices)
        else:
            raise ValueError(network)

        if mode == 'parallel':
            config.PARALLEL_CONCEPT_EVALUATION = True
        elif mode == 'sequential':
            config.PARALLEL_CONCEPT_EVALUATION = False
        else:
            raise ValueError(mode)
Exemple #9
0
def test_damaged(s):
    # Build cut subsystem from s
    cut = models.Cut((0, ), (1, 2))
    cut_s = Subsystem(s.network, s.state, s.node_indices, cut=cut)

    # Cut splits mechanism:
    m1 = mice(mechanism=(0, 1), purview=(1, 2), direction=Direction.EFFECT)
    assert m1.damaged_by_cut(cut_s)
    assert not m1.damaged_by_cut(s)

    # Cut splits mechanism & purview (but not *only* mechanism)
    m2 = mice(mechanism=(0, ), purview=(1, 2), direction=Direction.EFFECT)
    assert m2.damaged_by_cut(cut_s)
    assert not m2.damaged_by_cut(s)
Exemple #10
0
def test_damaged(s):
    # Build cut subsystem from s
    cut = models.Cut((0,), (1, 2))
    subsys = Subsystem(s.network, s.state, s.node_indices, cut=cut)

    # Cut splits mechanism:
    mip = mock.MagicMock(mechanism=(0, 1), purview=(1, 2), direction='future')
    mice = models.Mice(mip)
    assert mice.damaged_by_cut(subsys)
    assert not mice.damaged_by_cut(s)

    # Cut splits mechanism & purview (but not *only* mechanism)
    mip = mock.MagicMock(mechanism=(0,), purview=(1, 2), direction='future')
    mice = models.Mice(mip)
    assert mice.damaged_by_cut(subsys)
    assert not mice.damaged_by_cut(s)
Exemple #11
0
def test_true_ces(standard):
    previous_state = (1, 0, 0)
    current_state = (0, 0, 1)
    next_state = (1, 1, 0)
    subsystem = Subsystem(standard, current_state, standard.node_indices)

    ces = actual.true_ces(subsystem, previous_state, next_state)

    assert len(ces) == 2
    actual_cause, actual_effect = ces

    assert actual_cause.purview == (0, 1)
    assert actual_cause.mechanism == (2, )

    assert actual_effect.purview == (1, )
    assert actual_effect.mechanism == (2, )
Exemple #12
0
def test_split_mechanism_mice_is_not_reusable(redis_cache, flush_redis):
    """If mechanism is split, then cached mice are not usable
    when a cache is built from a parent cache."""
    s = examples.basic_subsystem()
    mechanism = (0, 1)
    mice = s.find_mice('past', mechanism)
    assert s._mice_cache.size() == 1  # cached
    assert mice.purview == (1, 2)

    # Splits mechanism, but not relevant connections:
    cut = models.Cut((0, ), (1, 2))
    cut_s = Subsystem(s.network,
                      s.state,
                      s.node_indices,
                      cut=cut,
                      mice_cache=s._mice_cache)
    key = cut_s._mice_cache.key('past', mechanism)
    assert cut_s._mice_cache.get(key) is None
def test_cut_relevant_connections_mice_is_not_reusable(redis_cache):
    """If relevant connections are cut, cached mice are not usable
    when a cache is built from a parent cache."""
    s = examples.basic_subsystem()
    mechanism = (1, )
    mice = s.find_mice(Direction.CAUSE, mechanism)
    assert s._mice_cache.size() == 1  # cached
    assert mice.purview == (2, )

    # Cuts connections from 2 -> 1
    cut = models.Cut((0, 2), (1, ))
    cut_s = Subsystem(s.network,
                      s.state,
                      s.node_indices,
                      cut=cut,
                      mice_cache=s._mice_cache)
    key = cut_s._mice_cache.key(Direction.CAUSE, mechanism)
    assert cut_s._mice_cache.get(key) is None
Exemple #14
0
def test_inherited_mice_cache_keeps_unaffected_mice(redis_cache, flush_redis):
    """Cached Mice are saved from the parent cache if both
    the mechanism and the relevant connections are not cut."""
    s = examples.basic_subsystem()
    mechanism = (1, )
    mice = s.find_mice('past', mechanism)
    assert s._mice_cache.size() == 1  # cached
    assert mice.purview == (2, )

    # Does not cut from 0 -> 1 or split mechanism
    cut = models.Cut((0, 1), (2, ))
    cut_s = Subsystem(s.network,
                      s.state,
                      s.node_indices,
                      cut=cut,
                      mice_cache=s._mice_cache)
    key = cut_s._mice_cache.key('past', mechanism)
    assert cut_s._mice_cache.get(key) == mice
from itertools import chain

from pyphi import Subsystem
from pyphi.models import Mice, Cut
from pyphi.utils import phi_eq

import example_networks

# Expected results {{{
# ====================

s = example_networks.s()
directions = ('past', 'future')
cuts = (None, Cut((1, 2), (0, )))
subsystem = {
    cut: Subsystem(s.node_indices, s.network, cut=cut)
    for cut in cuts
}

expected_purview_indices = {
    cuts[0]: {
        'past': {
            (1, ): (2, ),
            (2, ): (0, 1),
            (0, 1): (1, 2),
            (0, 1, 2): (0, 1, 2)
        },
        'future': {
            (1, ): (0, ),
            (2, ): (1, ),
            (0, 1): (2, ),
from itertools import chain

from pyphi import Subsystem
from pyphi.models import Mice, Cut, _null_mip
from pyphi.utils import phi_eq

import example_networks

# Expected results {{{
# ====================

s = example_networks.s()
directions = ('past', 'future')
cuts = (None, Cut((1, 2), (0, )))
subsystem = {
    cut: Subsystem(s.network, s.state, s.node_indices, cut=cut)
    for cut in cuts
}

expected_purview_indices = {
    cuts[0]: {
        'past': {
            (1, ): (2, ),
            (2, ): (0, 1),
            (0, 1): (1, 2),
            (0, 1, 2): (0, 1, 2)
        },
        'future': {
            (1, ): (0, ),
            (2, ): (1, ),
            (0, 1): (2, ),
Exemple #17
0
 def setup(self):
     # 7-node network
     self.network = examples.fig16()
     self.state = (0, ) * 7
     self.idxs = self.network.node_indices
     self.subsys = Subsystem(self.network, self.state, self.idxs)
  np.array([0.5, 0.5]).reshape(2, 1, 1, order="F")),
 ('cause_repertoire', standard_subsystem, [0], [0],
  np.array([0.5, 0.5]).reshape(2, 1, 1, order="F")),
 ('cause_repertoire', standard_subsystem, [0, 1], [0, 2],
  np.array([0.5, 0.5, 0.0, 0.0]).reshape(2, 1, 2, order="F")),
 ('cause_repertoire', standard_subsystem, [1], [2],
  np.array([1.0, 0.0]).reshape(1, 1, 2, order="F")),
 ('cause_repertoire', standard_subsystem, [], [2],
  np.array([0.5, 0.5]).reshape(1, 1, 2, order="F")),
 ('cause_repertoire', standard_subsystem, [1], [], np.array([1])),
 # }}}
 # Full network, with cut {{{
 # --------------------------
 ('cause_repertoire',
  Subsystem(standard,
            standard_subsystem.state,
            full,
            cut=Cut((2, ), (0, 1))), [0], [1],
  np.array([1 / 3, 2 / 3]).reshape(1, 2, 1, order="F")),
 # }}}
 # Subset, with cut {{{
 # --------------------
 ('cause_repertoire',
  Subsystem(standard,
            standard_subsystem.state, (1, 2),
            cut=Cut((1, ), (2, ))), [2], [1, 2],
  np.array([0.25, 0.25, 0.25, 0.25]).reshape(1, 2, 2, order="F")),
 ('cause_repertoire',
  Subsystem(standard,
            standard_subsystem.state, (1, 2),
            cut=Cut((1, ),
                    (2, ))), [2], [2], np.array([0.5,
Exemple #19
0
def test_validate_state_error(s):
    with pytest.raises(exceptions.StateUnreachableError):
        state = (0, 1, 0)
        Subsystem(s.network, state, s.node_indices)
     [],
     [2],
     np.array([0.5, 0.5]).reshape(1, 1, 2, order="F")
 ), (
     'cause_repertoire',
     standard_subsystem,
     [1],
     [],
     np.array([1])
 ),
     # }}}
     # Full network, with cut {{{
     # --------------------------
 (
     'cause_repertoire',
     Subsystem(standard, standard_subsystem.state, full,
               cut=Cut((2,), (0, 1))),
     [0],
     [1],
     np.array([1 / 3, 2 / 3]).reshape(1, 2, 1, order="F")
 ),
     # }}}
     # Subset, with cut {{{
     # --------------------
 (
     'cause_repertoire',
     Subsystem(standard, standard_subsystem.state, (1, 2),
               cut=Cut((1,), (2,))),
     [2],
     [1, 2],
     np.array([0.25, 0.25, 0.25, 0.25]).reshape(1, 2, 2, order="F")
 ), (
Exemple #21
0
# Scenario structure:
# (
#     function to test,
#     subsystem,
#     mechanism,
#     purview,
#     expected result
# )
scenarios = [
    # Cause repertoire {{{
    # ====================
    # Default Matlab network {{{
    # ~~~~~~~~~~~~~~~~~~~~~~~~~~
    # Full network, no cut {{{
    # ------------------------
    ('cause_repertoire', Subsystem(full, standard, cut=None), [0], [0],
     np.array([0.5, 0.5]).reshape(2, 1, 1, order="F")),
    ('cause_repertoire', Subsystem(full, standard, cut=None), [0], [0],
     np.array([0.5, 0.5]).reshape(2, 1, 1, order="F")),
    ('cause_repertoire', Subsystem(full, standard, cut=None), [0, 1], [0, 2],
     np.array([0.5, 0.5, 0.0, 0.0]).reshape(2, 1, 2, order="F")),
    ('cause_repertoire', Subsystem(full, standard, cut=None), [1], [2],
     np.array([1.0, 0.0]).reshape(1, 1, 2, order="F")),
    ('cause_repertoire', Subsystem(full, standard, cut=None), [], [2],
     np.array([0.5, 0.5]).reshape(1, 1, 2, order="F")),
    ('cause_repertoire', Subsystem(full, standard,
                                   cut=None), [1], [], np.array([1])),
    # }}}
    # Full network, with cut {{{
    # --------------------------
    ('cause_repertoire', Subsystem(full, standard, cut=Cut(
Exemple #22
0
#!/usr/bin/env python3
# -*- coding: utf-8 -*-

from pyphi.compute import big_mip
from pyphi import Subsystem
import numpy as np
import example_networks
from pyphi.constants import EPSILON

micro = example_networks.micro()
micro.current_state = (0, 0, 0, 0)
micro.past_state = (0, 0, 0, 0)
micro_subsystem = Subsystem(range(micro.size), micro)
mip = big_mip(micro_subsystem)

CD = micro_subsystem.nodes[2:4]
BCD = micro_subsystem.nodes[1:4]
ABCD = micro_subsystem.nodes[0:4]

A = mip.unpartitioned_constellation[0]

cause = A.cause.mip.unpartitioned_repertoire
effect = A.effect.mip.unpartitioned_repertoire


def test_expand_cause_repertoire():
    assert np.all(abs(A.expand_cause_repertoire(CD) - cause) < EPSILON)
    assert np.all(
        abs(
            A.expand_cause_repertoire(BCD).flatten(order='F') -
            np.array([1 / 6 if i < 6 else 0 for i in range(8)])) < EPSILON)