Esempio n. 1
0
def test_sia(transition):
    sia = actual.sia(transition)
    assert sia.alpha == 0.415037
    assert sia.cut == ac_cut(Direction.CAUSE, Part((), (1, )),
                             Part((0, ), (2, )))
    assert len(sia.account) == 3
    assert len(sia.partitioned_account) == 2
Esempio n. 2
0
def test_partitioned_repertoire_with_tripartition(s):
    tripartition = Tripartition(Part((), (1, )), Part((0, ), ()),
                                Part((), (2, )))

    assert np.array_equal(
        s.partitioned_repertoire(Direction.CAUSE, tripartition),
        np.array([[[0.25, 0.25], [0.25, 0.25]]]))
Esempio n. 3
0
def test_transition_apply_cut(transition):
    cut = ac_cut(Direction.CAUSE, Part((1, ), (2, )), Part((), (0, )))
    cut_transition = transition.apply_cut(cut)
    assert cut_transition.before_state == transition.before_state
    assert cut_transition.after_state == transition.after_state
    assert cut_transition.cause_indices == transition.cause_indices
    assert cut_transition.effect_indices == transition.effect_indices
    assert cut_transition.cut == cut
    assert cut_transition != transition
Esempio n. 4
0
def test_kcut_equality(kcut_cause, kcut_effect):
    other = KCut(Direction.CAUSE, KPartition(
        Part((0, 2), (0,)), Part((), (2,)), Part((3,), (3,))))
    assert kcut_cause == other
    assert hash(kcut_cause) == hash(other)
    assert hash(kcut_cause) != hash(kcut_cause.partition)

    assert kcut_cause != kcut_effect
    assert hash(kcut_cause) != hash(kcut_effect)
Esempio n. 5
0
def test_actual_cut_matrix():
    cut = ac_cut(Direction.CAUSE, Part((0,), (0, 2)), Part((2,), ()))
    # fmt: off
    answer = np.array([
        [0, 0, 1],
        [0, 0, 0],
        [0, 0, 1]
    ])
    # fmt: on
    assert np.array_equal(cut.cut_matrix(3), answer)
Esempio n. 6
0
def test_actual_apply_cut():
    cut = ac_cut(Direction.CAUSE, Part((0,), (0, 2)), Part((2,), ()))
    cm = np.ones((3, 3))
    # fmt: off
    answer = np.array([
        [1, 1, 0],
        [1, 1, 1],
        [1, 1, 0],
    ])
    # fmt: on
    assert np.array_equal(cut.apply_cut(cm), answer)
Esempio n. 7
0
def test_system_accessors(s):
    cut_cause = KCut(Direction.CAUSE,
                     KPartition(Part((0, 2), (0, 1)), Part((1, ), (2, ))))
    cs_cause = ConceptStyleSystem(s, Direction.CAUSE, cut_cause)
    assert cs_cause.cause_system.cut == cut_cause
    assert not cs_cause.effect_system.is_cut

    cut_effect = KCut(Direction.EFFECT,
                      KPartition(Part((0, 2), (0, 1)), Part((1, ), (2, ))))
    cs_effect = ConceptStyleSystem(s, Direction.EFFECT, cut_effect)
    assert not cs_effect.cause_system.is_cut
    assert cs_effect.effect_system.cut == cut_effect
def test_find_mip(direction, subsystem, cut, mechanism, purview, expected):
    # Set up testing parameters from scenario
    mechanism = tuple(subsystem.nodes[index] for index in mechanism)
    purview = tuple(subsystem.nodes[index] for index in purview)

    result = subsystem.find_mip(direction, mechanism, purview)

    # IMPORTANT: Since several different ways of partitioning the system can
    # yield the same phi value, the partition used in finding the MIP is not
    # unique. Thus, ``expected['partitions']`` is a dictionary that maps all the
    # ways of partitioning the system that yeild the minimal phi value to their
    # expected partitioned repertoires.

    if expected:
        # Construct expected list of possible MIPs
        expected = [
            Mip(direction=direction,
                partition=(Part(
                    mechanism=tuple(subsystem.nodes[i]
                                    for i in expected_partition[0].mechanism),
                    purview=tuple(subsystem.nodes[i]
                                  for i in expected_partition[0].purview)),
                           Part(mechanism=tuple(
                               subsystem.nodes[i]
                               for i in expected_partition[1].mechanism),
                                purview=tuple(
                                    subsystem.nodes[i]
                                    for i in expected_partition[1].purview))),
                mechanism=mechanism,
                purview=purview,
                unpartitioned_repertoire=expected['unpartitioned_repertoire'],
                partitioned_repertoire=expected_partitioned_repertoire,
                phi=expected['phi']) for expected_partition,
            expected_partitioned_repertoire in expected['partitions'].items()
        ]

    print('Result:', '---------', '', result, '', sep='\n')
    print('Expected:', '---------', '', sep='\n')
    if expected:
        print(*[mip for mip in expected], sep='\n')
    else:
        print(expected)
    print('\n')

    if expected:
        assert result in expected
    else:
        assert result == expected
Esempio n. 9
0
def test_concept_style_cuts():
    assert list(concept_cuts(Direction.CAUSE, (0,))) == [
        KCut(Direction.CAUSE, KPartition(
            Part((), ()), Part((), (0,)), Part((0,), ())))]
    assert list(concept_cuts(Direction.EFFECT, (0,))) == [
        KCut(Direction.EFFECT, KPartition(
            Part((), ()), Part((), (0,)), Part((0,), ())))]
Esempio n. 10
0
def test_mip_bipartitions():
    mechanism, purview = (0, ), (1, 2)
    answer = set([
        Bipartition(Part((), (2, )), Part((0, ), (1, ))),
        Bipartition(Part((), (1, )), Part((0, ), (2, ))),
        Bipartition(Part((), (1, 2)), Part((0, ), ())),
    ])
    assert set(mip_bipartitions(mechanism, purview)) == answer
Esempio n. 11
0
def test_mip_bipartition():
    mechanism, purview = (0, ), (1, 2)
    answer = [
        (Part((), (2, )), Part((0, ), (1, ))),
        (Part((), (1, )), Part((0, ), (2, ))),
        (Part((), (1, 2)), Part((0, ), ())),
    ]
    assert set(mip_bipartitions(mechanism, purview)) == set(answer)
Esempio n. 12
0
def test_actual_cut_indices():
    cut = ac_cut(Direction.CAUSE, Part((0, ), (2, )), Part((4, ), (5, )))
    assert cut.indices == (0, 2, 4, 5)
    cut = ac_cut(Direction.EFFECT, Part((0, 2), (0, 2)), Part((), ()))
    assert cut.indices == (0, 2)
Esempio n. 13
0
def kcut_cause():
    partition = KPartition(Part((0, 2), (0, )), Part((), (2, )),
                           Part((3, ), (3, )))
    return KCut(Direction.CAUSE, partition)
Esempio n. 14
0
def kcut_effect():
    partition = KPartition(Part((0, 2), (0, )), Part((), (2, )),
                           Part((3, ), (3, )))
    return KCut(Direction.EFFECT, partition)
Esempio n. 15
0
    # fmt: on
    assert np.array_equal(cut.cut_matrix(3), answer)


def ac_cut(direction, *parts):
    return models.ActualCut(direction, KPartition(*parts))


@config.override(PARTITION_TYPE="TRI")
@pytest.mark.parametrize(
    "direction,answer",
    [
        (
            Direction.BIDIRECTIONAL,
            [
                ac_cut(Direction.CAUSE, Part((), ()), Part((), (1, 2)), Part((0,), ())),
                ac_cut(
                    Direction.EFFECT, Part((), ()), Part((1,), (0,)), Part((2,), ())
                ),
                ac_cut(
                    Direction.EFFECT, Part((), ()), Part((1,), ()), Part((2,), (0,))
                ),
            ],
        ),
        (
            Direction.CAUSE,
            [ac_cut(Direction.CAUSE, Part((), ()), Part((), (1, 2)), Part((0,), ()))],
        ),
        (
            Direction.EFFECT,
            [
#
# {'partitions':
#     {
#         (first part, second part): expected partitioned repertoire
#     },
#  'phi': expected phi value
# }

scenarios = [
    # Past {{{
    # ~~~~~~~~
    # No cut {{{
    # ----------
    ('past', s, None, (0, ), (0, ), {
        'partitions': {
            (Part(mechanism=(), purview=(0, )),
             Part(mechanism=(0, ), purview=())):
            np.array([0.5, 0.5]).reshape(2, 1, 1, order="F")
        },
        'unpartitioned_repertoire':
        np.array([0.5, 0.5]).reshape(2, 1, 1, order="F"),
        'phi':
        0.0
    }),
    # }}}
    # With cut {{{
    # ------------
    ('past', s, (0, (1, 2)), (1, ), (2, ), {
        'partitions': {
            (Part(mechanism=(), purview=(2, )),
             Part(mechanism=(1, ), purview=())):
Esempio n. 17
0
# }

scenarios = [
    # Previous {{{
    # ~~~~~~~~
    # No cut {{{
    # ----------
    (
        Direction.CAUSE,
        s,
        None,
        (0, ),
        (0, ),
        {
            "partitions": {
                Bipartition(Part(mechanism=(), purview=(0, )),
                            Part(mechanism=(0, ), purview=())):
                np.array([0.5, 0.5]).reshape(2, 1, 1, order="F")
            },
            "repertoire": np.array([0.5, 0.5]).reshape(2, 1, 1, order="F"),
            "phi": 0.0,
        },
    ),
    # }}}
    # With cut {{{
    # ------------
    (
        Direction.CAUSE,
        s,
        (0, (1, 2)),
        (1, ),
Esempio n. 18
0

def test_actual_cut_matrix():
    cut = ac_cut(Direction.CAUSE, Part((0, ), (0, 2)), Part((2, ), ()))
    assert np.array_equal(cut.cut_matrix(3),
                          np.array([[0, 0, 1], [0, 0, 0], [0, 0, 1]]))


def ac_cut(direction, *parts):
    return models.ActualCut(direction, KPartition(*parts))


@config.override(PARTITION_TYPE='TRI')
@pytest.mark.parametrize('direction,answer',
                         [(Direction.BIDIRECTIONAL, [
                             ac_cut(Direction.CAUSE, Part(
                                 (), ()), Part((), (1, 2)), Part((0, ), ())),
                             ac_cut(Direction.EFFECT, Part(
                                 (), ()), Part((1, ), (0, )), Part((2, ), ())),
                             ac_cut(Direction.EFFECT, Part(
                                 (), ()), Part((1, ), ()), Part((2, ), (0, )))
                         ]),
                          (Direction.CAUSE, [
                              ac_cut(Direction.CAUSE, Part(
                                  (), ()), Part((), (1, 2)), Part((0, ), ()))
                          ]),
                          (Direction.EFFECT, [
                              ac_cut(Direction.EFFECT, Part(
                                  (), ()), Part((), (0, )), Part((1, 2), ())),
                              ac_cut(Direction.EFFECT, Part(
                                  (), ()), Part((1, ), (0, )), Part((2, ),
                                                                    ())),
Esempio n. 19
0
def test_wedge_partitions():
    mechanism, purview = (0, ), (1, 2)
    assert set(wedge_partitions(mechanism, purview)) == set([
        Tripartition(Part((), ()), Part((), (1, 2)), Part((0, ), ())),
    ])

    mechanism, purview = (3, 4), (5, 6)
    assert set(wedge_partitions(mechanism, purview)) == set([
        Tripartition(Part((), ()), Part((), (5, 6)), Part((3, 4), ())),
        Tripartition(Part((), ()), Part((3, ), ()), Part((4, ), (5, 6))),
        Tripartition(Part((), ()), Part((3, ), (5, )), Part((4, ), (6, ))),
        Tripartition(Part((), ()), Part((3, ), (5, 6)), Part((4, ), ())),
        Tripartition(Part((), ()), Part((3, ), (6, )), Part((4, ), (5, ))),
        Tripartition(Part((), (5, )), Part((3, ), ()), Part((4, ), (6, ))),
        Tripartition(Part((), (5, )), Part((3, ), (6, )), Part((4, ), ())),
        Tripartition(Part((), (6, )), Part((3, ), ()), Part((4, ), (5, ))),
        Tripartition(Part((), (6, )), Part((3, ), (5, )), Part((4, ), ())),
    ])
Esempio n. 20
0
def test_all_partitions():
    mechanism, purview = (0, 1), (2, )
    assert set(all_partitions(mechanism, purview)) == set([
        KPartition(Part((0, 1), ()), Part((), (2, ))),
        KPartition(Part((0, ), ()), Part((1, ), ()), Part((), (2, ))),
        KPartition(Part((0, ), (2, )), Part((1, ), ()), Part((), ())),
        KPartition(Part((0, ), ()), Part((1, ), (2, )), Part((), ())),
    ])

    mechanism, purview = (0, 1), (2, 3)
    assert set(all_partitions(mechanism, purview)) == set([
        KPartition(Part((0, 1), ()), Part((), (2, 3))),
        KPartition(Part((0, ), ()), Part((1, ), (2, 3)), Part((), ())),
        KPartition(Part((0, ), (2, 3)), Part((1, ), ()), Part((), ())),
        KPartition(Part((0, ), ()), Part((1, ), ()), Part((), (2, 3))),
        KPartition(Part((0, ), ()), Part((1, ), (3, )), Part((), (2, ))),
        KPartition(Part((0, ), (2, )), Part((1, ), ()), Part((), (3, ))),
        KPartition(Part((0, ), ()), Part((1, ), (2, )), Part((), (3, ))),
        KPartition(Part((0, ), (3, )), Part((1, ), (2, )), Part((), ())),
        KPartition(Part((0, ), (3, )), Part((1, ), ()), Part((), (2, ))),
        KPartition(Part((0, ), (2, )), Part((1, ), (3, )), Part((), ())),
    ])
Esempio n. 21
0
def test_actual_cut_matrix():
    cut = ac_cut(Direction.CAUSE, Part((0, ), (0, 2)), Part((2, ), ()))
    assert np.array_equal(cut.cut_matrix(3),
                          np.array([[0, 0, 1], [0, 0, 0], [0, 0, 1]]))