def test_concept_equality_network(s, simple_subsys_all_off): phi = 1.0 concept = models.Concept(mechanism=(), cause=None, effect=None, subsystem=simple_subsys_all_off, phi=phi) another = models.Concept(mechanism=(), cause=None, effect=None, subsystem=s, phi=phi) assert concept != another
def test_concept_equality_mechanism(s): phi = 1.0 concept = models.Concept(mechanism=(1,), cause=None, effect=None, subsystem=s, phi=phi) another = models.Concept(mechanism=(), cause=None, effect=None, subsystem=s, phi=phi) assert concept != another
def test_constellation_distance_uses_simple_vs_emd(mock_emd_distance, mock_simple_distance, s): """Quick check that we use the correct constellation distance function. If the two constellations differ only in that some concepts have moved to the null concept and all other concepts are the same then we use the simple constellation distance. Otherwise, use the EMD. """ make_mice = lambda: models.Mice(models.Mip( phi=None, direction=None, mechanism=None, purview=None, partition=None, unpartitioned_repertoire=None, partitioned_repertoire=None)) lone_concept = models.Concept(cause=make_mice(), effect=make_mice(), mechanism=(0, 1)) # lone concept -> null concept compute.constellation_distance((lone_concept,), ()) assert mock_emd_distance.called is False assert mock_simple_distance.called is True mock_simple_distance.reset_mock() other_concept = models.Concept(cause=make_mice(), effect=make_mice(), mechanism=(0, 1, 2)) # different concepts in constellation compute.constellation_distance((lone_concept,), (other_concept,)) assert mock_emd_distance.called is True assert mock_simple_distance.called is False
def test_concept_equality_repertoires(s): phi = 1.0 mice1 = mice(phi=phi, repertoire=np.array([1, 2]), partitioned_repertoire=()) mice2 = mice(phi=phi, repertoire=np.array([0, 0]), partitioned_repertoire=None) concept = models.Concept(mechanism=(), cause=mice1, effect=mice2, subsystem=s) another = models.Concept(mechanism=(), cause=mice2, effect=mice1, subsystem=s) assert concept != another
def test_concept_equality_one_subsystem_is_subset_of_another(s, subsys_n1n2): phi = 1.0 mice = models.Mice(models.Mip( direction=None, mechanism=(), purview=(1, 2), unpartitioned_repertoire=(), partitioned_repertoire=(), phi=0.0, partition=(models.Part((), ()), models.Part((), ())))) concept = models.Concept(mechanism=(2,), cause=mice, effect=mice, subsystem=s, phi=phi) another = models.Concept(mechanism=(2,), cause=mice, effect=mice, subsystem=subsys_n1n2, phi=phi) assert concept == another
def test_concept_odering_by_mechanism(s): small = models.Concept( mechanism=(0, 1), cause=None, effect=None, subsystem=s, phi=1.0) big = models.Concept( mechanism=(0, 1, 3), cause=None, effect=None, subsystem=s, phi=1.0) assert small < big assert small <= big assert big > small assert big >= small assert big != small
def test_concept_hashing_one_subsystem_is_subset_of_another(s, subsys_n1n2): phi = 1.0 mice = models.Mice(models.Mip( direction=None, mechanism=(), purview=(1, 2), unpartitioned_repertoire=(), partitioned_repertoire=(), phi=0.0, partition=(models.Part((), ()), models.Part((), ())))) concept = models.Concept(mechanism=(2,), cause=mice, effect=mice, subsystem=s, phi=phi) another = models.Concept(mechanism=(2,), cause=mice, effect=mice, subsystem=subsys_n1n2, phi=phi) assert hash(concept) == hash(another) assert(len(set([concept, another])) == 1)
def test_concept_equality_repertoires(s): phi = 1.0 mice1 = models.Mice(models.Mip( direction=None, mechanism=(), purview=(), unpartitioned_repertoire=np.array([1, 2]), partitioned_repertoire=(), phi=0.0, partition=(models.Part((), ()), models.Part((), ())))) mice2 = models.Mice(models.Mip( direction=None, mechanism=(), purview=(), unpartitioned_repertoire=np.array([0, 0]), partitioned_repertoire=None, phi=0.0, partition=(models.Part((), ()), models.Part((), ())))) concept = models.Concept(mechanism=(), cause=mice1, effect=mice2, subsystem=s, phi=phi) another = models.Concept(mechanism=(), cause=mice2, effect=mice1, subsystem=s, phi=phi) assert concept != another
def test_concept_hashing(s): mice = models.Mice(models.Mip( direction=None, mechanism=(0, 1, 2), purview=(0, 1, 2), unpartitioned_repertoire=None, partitioned_repertoire=None, phi=0.0, partition=(models.Part((), ()), models.Part((), ())))) concept = models.Concept( mechanism=(0, 1, 2), cause=mice, effect=mice, subsystem=s, phi=0.0) hash(concept)
def test_concept_repr_str(): r = namedtuple('object_with_repertoire', ['repertoire']) concept = models.Concept(mechanism=(), cause=r('a_repertoire'), effect=r('a_repertoire'), subsystem=None, phi=0.0) print(repr(concept)) print(str(concept))
def test_concept_repr_str(): mice = models.Mice(models.Mip( direction=None, mechanism=(), purview=(), unpartitioned_repertoire=None, partitioned_repertoire=None, phi=0.0, partition=(models.Part((), ()), models.Part((), ())))) concept = models.Concept( mechanism=(), cause=mice, effect=mice, subsystem=None, phi=0.0) print(repr(concept)) print(str(concept))
def concept(mechanism=(0, 1), cause_purview=(1,), effect_purview=(1,), phi=1.0, subsystem=None): """Build a ``Concept``.""" return models.Concept( mechanism=mechanism, cause=mic(mechanism=mechanism, purview=cause_purview, phi=phi, direction=Direction.CAUSE), effect=mie(mechanism=mechanism, purview=effect_purview, phi=phi, direction=Direction.EFFECT), subsystem=subsystem)
def test_concept_ordering(s, micro_s): phi1 = models.Concept( mechanism=(0, 1), cause=1, effect=None, subsystem=s, phi=1.0) different_phi1 = models.Concept( mechanism=(0, 1), cause='different', effect=None, subsystem=micro_s, phi=1.0) phi2 = models.Concept( mechanism=(0,), cause='stilldifferent', effect=None, subsystem=s, phi=1.0 + constants.EPSILON*2) assert phi1 < phi2 assert phi2 > phi1 assert phi1 <= phi2 assert phi2 >= phi1 with pytest.raises(TypeError): phi1 <= different_phi1 with pytest.raises(TypeError): phi1 > different_phi1
def test_concept_equality(): phi = 1.0 concept = models.Concept(mechanism=(), cause=None, effect=None, subsystem=None, phi=phi) close_enough = models.Concept(mechanism=(), cause=None, effect=None, subsystem=None, phi=(phi - constants.EPSILON / 2)) not_quite = models.Concept(mechanism=(), cause=None, effect=None, subsystem=None, phi=(phi - constants.EPSILON * 2)) assert concept == close_enough assert concept != not_quite
def test_ces_distance_uses_simple_vs_emd(mock_emd_distance, mock_simple_distance, s): """Quick check that we use the correct CES distance function. If the two CESs differ only in that some concepts have moved to the null concept and all other concepts are the same then we use the simple CES distance. Otherwise, use the EMD. """ mock_emd_distance.return_value = float() mock_simple_distance.return_value = float() make_mice = lambda: models.MaximallyIrreducibleCauseOrEffect( models.RepertoireIrreducibilityAnalysis( phi=0, direction=None, mechanism=None, purview=None, partition=None, repertoire=None, partitioned_repertoire=None, )) lone_concept = models.Concept(cause=make_mice(), effect=make_mice(), mechanism=(0, 1), subsystem=s) # lone concept -> null concept compute.ces_distance((lone_concept, ), ()) assert mock_emd_distance.called is False assert mock_simple_distance.called is True mock_simple_distance.reset_mock() other_concept = models.Concept(cause=make_mice(), effect=make_mice(), mechanism=(0, 1, 2), subsystem=s) # different concepts in CES compute.ces_distance((lone_concept, ), (other_concept, )) assert mock_emd_distance.called is True assert mock_simple_distance.called is False
def test_null_concept(s, flushcache, restore_fs_cache): flushcache() cause = models.Mice(models.Mip( unpartitioned_repertoire=s.unconstrained_cause_repertoire(()), phi=0, direction=DIRECTIONS[PAST], mechanism=(), purview=(), partition=None, partitioned_repertoire=None)) effect = models.Mice(models.Mip( unpartitioned_repertoire=s.unconstrained_effect_repertoire(()), phi=0, direction=DIRECTIONS[FUTURE], mechanism=(), purview=(), partition=None, partitioned_repertoire=None)) assert (s.null_concept == models.Concept(mechanism=(), phi=0, cause=cause, effect=effect, subsystem=s))
def test_concept_ordering(): phi1 = models.Concept(mechanism=(0, 1), cause=1, effect=None, subsystem=None, phi=1.0) different_phi1 = models.Concept(mechanism=(), cause='different', effect=None, subsystem=None, phi=1.0) phi2 = models.Concept(mechanism=0, cause='stilldifferent', effect=None, subsystem=None, phi=1.0 + constants.EPSILON * 2) assert phi1 < phi2 assert phi2 > phi1 assert phi1 <= phi2 assert phi2 >= phi1 assert phi1 <= different_phi1 assert phi1 >= different_phi1
def test_constellation_is_still_a_tuple(): c = models.Constellation([models.Concept()]) assert len(c) == 1
def test_ces_are_always_normalized(): c1 = models.Concept(mechanism=(1, )) c2 = models.Concept(mechanism=(2, )) c3 = models.Concept(mechanism=(1, 3)) c4 = models.Concept(mechanism=(1, 2, 3)) assert (c1, c2, c3, c4) == models.CauseEffectStructure((c3, c4, c2, c1))
def test_concept_equality_phi(s): concept = models.Concept(mechanism=(), cause=None, effect=None, subsystem=s, phi=1.0) another = models.Concept(mechanism=(), cause=None, effect=None, subsystem=s, phi=0.0) assert concept != another