Exemplo n.º 1
0
def test_only_cache_uncut_subsystem_mices(redis_cache, s):
    s = Subsystem(s.network, (1, 0, 0), s.node_indices,
                  cut=models.Cut((1,), (0, 2)))
    mechanism = (1,)  # has a MIC
    s.find_mice(Direction.CAUSE, mechanism)
    # don't cache anything because subsystem is cut
    assert s._mice_cache.size() == 0
Exemplo n.º 2
0
class BenchmarkSubsystem():

    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)

    def time_cause_repertoire(self):
        clear_subsystem_caches(self.subsys)
        self.subsys.cause_repertoire(self.idxs, self.idxs)

    def time_cause_repertoire_cache(self):
        clear_subsystem_caches(self.subsys)
        for i in range(3):
            self.subsys.cause_repertoire(self.idxs, self.idxs)

    def time_effect_repertoire(self):
        clear_subsystem_caches(self.subsys)
        self.subsys.effect_repertoire(self.idxs, self.idxs)

    def time_effect_repertoire_cache(self):
        clear_subsystem_caches(self.subsys)
        for i in range(3):
            self.subsys.effect_repertoire(self.idxs, self.idxs)

    # Potential purviews benchmark.
    # TODO: this isn't representative of what actually happens.
    # Can we capture a sample run of multiple calls to
    # subsys.potential_purviews?

    def _do_potential_purviews(self):
        for i in range(100):
            self.subsys.potential_purviews(Direction.CAUSE, self.idxs)

    def time_potential_purviews_no_cache(self):
        # Network purview caches disabled
        clear_network_caches(self.subsys.network)
        default = config.CACHE_POTENTIAL_PURVIEWS
        config.CACHE_POTENTIAL_PURVIEWS = False
        self._do_potential_purviews()
        config.CACHE_POTENTIAL_PURVIEWS = default

    def time_potential_purviews_with_cache(self):
        # Network purview caches enabled
        clear_network_caches(self.subsys.network)
        default = config.CACHE_POTENTIAL_PURVIEWS
        config.CACHE_POTENTIAL_PURVIEWS = True
        self._do_potential_purviews()
        config.CACHE_POTENTIAL_PURVIEWS = default
Exemplo n.º 3
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)
Exemplo n.º 4
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)
Exemplo n.º 5
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, )
Exemplo n.º 6
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
Exemplo n.º 7
0
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
Exemplo n.º 8
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
Exemplo n.º 9
0
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, ),
Exemplo n.º 10
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")
 ), (
Exemplo n.º 12
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)
  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,
Exemplo n.º 14
0
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, ),
Exemplo n.º 15
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)
Exemplo n.º 16
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)
Exemplo n.º 17
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(