Beispiel #1
0
def test_time_to_allopatric_speciation(zone_example_grid):
    mg, z = zone_example_grid

    # Create a zone for time 0.

    z[[9, 10, 11, 12]] = 1

    se = SpeciesEvolver(mg)
    sc = ZoneController(mg, zone_func)
    taxa = sc.populate_zones_uniformly(1, time_to_allopatric_speciation=20)
    se.track_taxa(taxa)

    z[[11]] = 0

    while len(se.taxa_data_frame) == 1:
        sc.run_one_step(10)
        se.run_one_step(10)

    expected_df = pd.DataFrame(
        {
            "pid": [np.nan, 0],
            "type": 2 * [ZoneTaxon.__name__],
            "t_first": [0, 30],
            "t_final": 2 * [np.nan],
        },
        index=[0, 1],
    )
    expected_df.index.name = "tid"
    expected_df["pid"] = expected_df["pid"].astype("Int64")
    expected_df["t_final"] = expected_df["t_final"].astype("Int64")

    pd.testing.assert_frame_equal(se.taxa_data_frame, expected_df, check_like=True)
Beispiel #2
0
def test_allopatric_wait_time(zone_example_grid):
    mg, z = zone_example_grid

    # Create a zone for time 0.

    z[[9, 10, 11, 12]] = 1

    se = SpeciesEvolver(mg)
    sc = ZoneController(mg, zone_func)
    taxa = sc.populate_zones_uniformly(1, allopatric_wait_time=20)
    se.track_taxa(taxa)

    z[[11]] = 0

    while len(se.taxa_data_frame) == 1:
        sc.run_one_step(10)
        se.run_one_step(10)

    expected_df = pd.DataFrame(
        {
            'appeared': [0, 30, 30],
            'latest_time': [30, 30, 30],
            'extant': [False, True, True]
        },
        index=[0, 1, 2])
    pd.testing.assert_frame_equal(se.taxa_data_frame,
                                  expected_df,
                                  check_like=True)
Beispiel #3
0
def test_zone_taxon_range_mask(zone_example_grid):
    mg, z = zone_example_grid

    # Create a zone for time 0.

    ids = [9, 10, 11, 12]
    z[ids] = 1

    se = SpeciesEvolver(mg)
    sc = ZoneController(mg, zone_func)
    taxa = sc.populate_zones_uniformly(1)
    se.track_taxa(taxa)

    expected_mask = np.zeros(mg.number_of_nodes, bool)
    expected_mask[ids] = True
    np.testing.assert_array_equal(taxa[0].range_mask, expected_mask)

    # Remove extent so taxa range mask is all False.

    z[ids] = 0
    sc.run_one_step(1)
    se.run_one_step(1)

    expected_mask = np.zeros(mg.number_of_nodes, bool)
    np.testing.assert_array_equal(taxa[0].range_mask, expected_mask)
Beispiel #4
0
def test_get_extant_taxon_objects(zone_example_grid):
    se = SpeciesEvolver(zone_example_grid)

    introduced_taxa = [TestTaxon(), TestTaxon()]
    se.track_taxa(introduced_taxa)
    se.run_one_step(10)
    se.run_one_step(10)

    # Test no parameters.

    queried_taxa = se.get_extant_taxon_objects()
    np.testing.assert_equal(Counter(queried_taxa), Counter(se._taxon_objs))

    # Test `tids` parameter.

    queried_taxa = se.get_extant_taxon_objects(tids=[0])
    ids = [t.tid for t in queried_taxa]
    expected_ids = []
    np.testing.assert_equal(Counter(ids), Counter(expected_ids))

    queried_taxa = se.get_extant_taxon_objects(tids=[4, 5])
    ids = [t.tid for t in queried_taxa]
    expected_ids = [4, 5]
    np.testing.assert_equal(Counter(ids), Counter(expected_ids))

    # Test `time` parameter.

    queried_taxa = se.get_extant_taxon_objects(time=20)
    ids = [t.tid for t in queried_taxa]
    expected_ids = [4, 5]
    np.testing.assert_equal(Counter(ids), Counter(expected_ids))

    queried_taxa = se.get_extant_taxon_objects(time=10)
    ids = [t.tid for t in queried_taxa]
    expected_ids = []
    np.testing.assert_equal(Counter(ids), Counter(expected_ids))

    queried_taxa = se.get_extant_taxon_objects(time=30)
    ids = [t.tid for t in queried_taxa]
    expected_ids = []
    np.testing.assert_equal(Counter(ids), Counter(expected_ids))

    # Test `ancestor` parameter.

    queried_taxa = se.get_extant_taxon_objects(ancestor=1)
    ids = [t.tid for t in queried_taxa]
    expected_ids = [5]
    np.testing.assert_equal(Counter(ids), Counter(expected_ids))

    queried_taxa = se.get_extant_taxon_objects(ancestor=5)
    np.testing.assert_equal(queried_taxa, [])

    queried_taxa = se.get_extant_taxon_objects(ancestor=6)
    np.testing.assert_equal(queried_taxa, [])

    # Test multiple parameters.

    queried_taxa = se.get_extant_taxon_objects(ancestor=1, time=10)
    np.testing.assert_equal(queried_taxa, [])
def test_get_taxon_objects(zone_example_grid):
    se = SpeciesEvolver(zone_example_grid)

    introduced_taxa = [TestTaxon(), TestTaxon()]
    se.track_taxa(introduced_taxa)
    se.run_one_step(10)
    se.run_one_step(10)

    # Test no parameters.

    queried_taxa = se.get_taxon_objects()
    np.testing.assert_equal(
        Counter(queried_taxa), Counter(se._taxa['object'])
    )

    # Test `time` parameter.

    queried_taxa = se.get_taxon_objects(time=0)
    np.testing.assert_equal(
        Counter(queried_taxa), Counter(introduced_taxa)
    )

    queried_taxa = se.get_taxon_objects(time=10)
    ids = [s.uid for s in queried_taxa]
    expected_ids = [0, 1, 2, 3]
    np.testing.assert_equal(Counter(ids), Counter(expected_ids))

    np.testing.assert_raises(ValueError, se.get_taxon_objects, time=5)
    np.testing.assert_raises(ValueError, se.get_taxon_objects, time=11)

    # Test `extant_at_latest_time` parameter.

    queried_taxa = se.get_taxon_objects(extant_at_latest_time=True)
    ids = [s.uid for s in queried_taxa]
    expected_ids = [4, 5]
    np.testing.assert_equal(Counter(ids), Counter(expected_ids))

    # Test `ancestor` parameter.

    queried_taxa = se.get_taxon_objects(ancestor=1)
    ids = [s.uid for s in queried_taxa]
    expected_ids = [3, 5]
    np.testing.assert_equal(Counter(ids), Counter(expected_ids))

    queried_taxa = se.get_taxon_objects(ancestor=5)
    np.testing.assert_equal(queried_taxa, [])

    queried_taxa = se.get_taxon_objects(ancestor=6)
    np.testing.assert_equal(queried_taxa, [])

    # Test multiple parameters.

    queried_taxa = se.get_taxon_objects(ancestor=1, time=10)
    ids = [s.uid for s in queried_taxa]
    expected_ids = [3]
    np.testing.assert_equal(Counter(ids), Counter(expected_ids))
def test_track_taxa_and_component_attributes(zone_example_grid):
    se = SpeciesEvolver(zone_example_grid)

    # Introduce multiple taxa.
    taxa = [TestTaxon(), TestTaxon()]
    se.track_taxa(taxa)

    # Introduce a single taxon.
    taxon = TestTaxon()
    se.track_taxa(taxon)

    # Test attributes at initial time step.

    expected_df = pd.DataFrame({
        'appeared': [0, 0, 0],
        'latest_time': [0, 0, 0],
        'extant': [True, True, True]},
        index=[0, 1, 2]
    )
    expected_df.index.name = 'uid'
    pd.testing.assert_frame_equal(
        se.taxa_data_frame, expected_df, check_like=True
    )

    expected_df = pd.DataFrame({
        'time': [0],
        'taxa': [3]
    })
    pd.testing.assert_frame_equal(
        se.record_data_frame, expected_df, check_like=True
    )

    # Test attributes at a later time.

    se.run_one_step(10)

    expected_df = pd.DataFrame({
        'appeared': [0, 0, 0, 10, 10, 10],
        'latest_time': [10, 10, 10, 10, 10, 10],
        'extant': [False, False, False, True, True, True]},
        index=[0, 1, 2, 3, 4, 5]
    )
    pd.testing.assert_frame_equal(
        se.taxa_data_frame, expected_df, check_like=True
    )

    expected_df = pd.DataFrame({
        'time': [0, 10],
        'taxa': [3, 3]
    })
    pd.testing.assert_frame_equal(
        se.record_data_frame, expected_df, check_like=True
    )
Beispiel #7
0
def test_many_to_many(zone_example_grid):
    mg, z = zone_example_grid

    # Create two zones for time 0.

    z[[10, 12, 17, 19, 24, 26]] = 1

    se = SpeciesEvolver(mg)
    sc = ZoneController(mg, zone_func)
    taxa = sc.populate_zones_uniformly(1)
    se.track_taxa(taxa)

    expected_df = pd.DataFrame({
        'time': [0],
        'zones': [2],
        'fragmentations': [np.nan],
        'captures': [np.nan],
        'area_captured_sum': [np.nan],
        'area_captured_max': [np.nan]
    })
    pd.testing.assert_frame_equal(sc.record_data_frame,
                                  expected_df,
                                  check_like=True)

    np.testing.assert_equal(len(se.get_taxon_objects(time=0)), 2)

    # Modify elevation such that two zones each overlap the original two zones.

    z[[17, 19]] = 0
    z[[11, 25]] = 1

    sc.run_one_step(1)
    se.run_one_step(1)

    np.testing.assert_equal(len(sc.zones), 2)
    for z in sc.zones:
        np.testing.assert_equal(z._conn_type, zn.Connection.MANY_TO_MANY)

    expected_df = pd.DataFrame({
        'time': [0, 1],
        'zones': [2, 2],
        'fragmentations': [np.nan, 0],
        'captures': [np.nan, 2],
        'area_captured_sum': [np.nan, 24],
        'area_captured_max': [np.nan, 12]
    })
    pd.testing.assert_frame_equal(sc.record_data_frame,
                                  expected_df,
                                  check_like=True)

    np.testing.assert_equal(
        len(se.get_taxon_objects(extant_at_latest_time=True)), 4)
Beispiel #8
0
def test_one_to_many(zone_example_grid):
    mg, z = zone_example_grid

    # Create a zone for time 0.

    z[[9, 10, 11, 12]] = 1

    se = SpeciesEvolver(mg)
    sc = ZoneController(mg, zone_func)
    taxa = sc.populate_zones_uniformly(1)
    se.track_taxa(taxa)

    expected_df = pd.DataFrame(
        {
            "time": [0],
            "zones": [1],
            "fragmentations": [np.nan],
            "captures": [np.nan],
            "area_captured_sum": [np.nan],
            "area_captured_max": [np.nan],
        }
    )
    pd.testing.assert_frame_equal(sc.record_data_frame, expected_df, check_like=True)

    np.testing.assert_equal(len(se.get_extant_taxon_objects(time=0)), 1)

    # Break the zone in two for time 1.

    z[11] = 0

    sc.run_one_step(1)
    se.run_one_step(1)

    np.testing.assert_equal(len(sc.zones), 2)
    np.testing.assert_equal(
        set([z._conn_type for z in sc.zones]), set([None, zn.Connection.ONE_TO_MANY])
    )

    expected_df = pd.DataFrame(
        {
            "time": [0, 1],
            "zones": [1, 2],
            "fragmentations": [np.nan, 2],
            "captures": [np.nan, 0],
            "area_captured_sum": [np.nan, 0],
            "area_captured_max": [np.nan, 0],
        }
    )
    pd.testing.assert_frame_equal(sc.record_data_frame, expected_df, check_like=True)

    np.testing.assert_equal(len(se.get_extant_taxon_objects()), 2)
Beispiel #9
0
def test_one_to_one(zone_example_grid):
    mg, z = zone_example_grid

    # Create a zone for time 0.

    z[[9, 10, 11, 12]] = 1

    se = SpeciesEvolver(mg)
    sc = ZoneController(mg, zone_func)
    taxa = sc.populate_zones_uniformly(1)
    se.track_taxa(taxa)

    np.testing.assert_equal(len(sc.zones), 1)
    zone = sc.zones[0]

    expected_df = pd.DataFrame(
        {
            "time": [0],
            "zones": [1],
            "fragmentations": [np.nan],
            "captures": [np.nan],
            "area_captured_sum": [np.nan],
            "area_captured_max": [np.nan],
        }
    )
    pd.testing.assert_frame_equal(sc.record_data_frame, expected_df, check_like=True)

    # Modify elevation, although  there is still one zone in time 1.

    z[[11, 12]] = 0

    sc.run_one_step(1)
    se.run_one_step(1)

    np.testing.assert_equal(len(sc.zones), 1)
    np.testing.assert_equal(zone, sc.zones[0])
    np.testing.assert_equal(sc.zones[0]._conn_type, zn.Connection.ONE_TO_ONE)

    expected_df = pd.DataFrame(
        {
            "time": [0, 1],
            "zones": [1, 1],
            "fragmentations": [np.nan, 0],
            "captures": [np.nan, 0],
            "area_captured_sum": [np.nan, 0],
            "area_captured_max": [np.nan, 0],
        }
    )
    pd.testing.assert_frame_equal(sc.record_data_frame, expected_df, check_like=True)

    np.testing.assert_equal(len(se.get_extant_taxon_objects(time=1)), 1)
Beispiel #10
0
def test_one_to_none(zone_example_grid):
    mg, z = zone_example_grid

    # Create a zone for time 0.

    z[[9, 10, 11, 12]] = 1

    se = SpeciesEvolver(mg)
    sc = ZoneController(mg, zone_func)
    taxa = sc.populate_zones_uniformly(1)
    se.track_taxa(taxa)

    np.testing.assert_equal(len(sc.zones), 1)
    zone = sc.zones[0]

    expected_df = pd.DataFrame(
        {
            "time": [0],
            "zones": [1],
            "fragmentations": [np.nan],
            "captures": [np.nan],
            "area_captured_sum": [np.nan],
            "area_captured_max": [np.nan],
        }
    )
    pd.testing.assert_frame_equal(sc.record_data_frame, expected_df, check_like=True)

    # No zones for time 1.

    z[:] = 0

    sc.run_one_step(1)
    se.run_one_step(1)

    np.testing.assert_equal(len(sc.zones), 0)
    np.testing.assert_equal(zone._conn_type, zn.Connection.ONE_TO_NONE)

    expected_df = pd.DataFrame(
        {
            "time": [0, 1],
            "zones": [1, 0],
            "fragmentations": [np.nan, 0],
            "captures": [np.nan, 0],
            "area_captured_sum": [np.nan, 0],
            "area_captured_max": [np.nan, 0],
        }
    )
    pd.testing.assert_frame_equal(sc.record_data_frame, expected_df, check_like=True)

    np.testing.assert_equal(se.record_data_frame.taxa.sum(), 1)
Beispiel #11
0
def test_track_taxa_and_component_attributes(zone_example_grid):
    se = SpeciesEvolver(zone_example_grid)

    # Introduce multiple taxa.
    taxa = [TestTaxon(), TestTaxon()]
    se.track_taxa(taxa)

    # Introduce a single taxon.
    taxon = TestTaxon()
    se.track_taxa(taxon)

    # Test attributes at initial time step.

    expected_df = pd.DataFrame(
        {
            "pid": 3 * [np.nan],
            "type": 3 * [TestTaxon.__name__],
            "t_first": [0, 0, 0],
            "t_final": 3 * [np.nan],
        },
        index=[0, 1, 2],
    )
    expected_df.index.name = "tid"
    expected_df["pid"] = expected_df["pid"].astype("Int64")
    expected_df["t_final"] = expected_df["t_final"].astype("Int64")
    pd.testing.assert_frame_equal(se.taxa_data_frame, expected_df, check_like=True)

    expected_df = pd.DataFrame({"time": [0], "taxa": [3]})
    pd.testing.assert_frame_equal(se.record_data_frame, expected_df, check_like=True)

    # Test attributes at a later time.

    se.run_one_step(10)

    expected_df = pd.DataFrame(
        {
            "pid": 3 * [np.nan] + [0, 1, 2],
            "type": 6 * [TestTaxon.__name__],
            "t_first": [0, 0, 0, 10, 10, 10],
            "t_final": [10, 10, 10] + 3 * [np.nan],
        },
        index=[0, 1, 2, 3, 4, 5],
    )
    expected_df.index.name = "tid"
    expected_df["pid"] = expected_df["pid"].astype("Int64")
    expected_df["t_final"] = expected_df["t_final"].astype("Int64")
    pd.testing.assert_frame_equal(se.taxa_data_frame, expected_df, check_like=True)

    expected_df = pd.DataFrame({"time": [0, 10], "taxa": [3, 3]})
    pd.testing.assert_frame_equal(se.record_data_frame, expected_df, check_like=True)
Beispiel #12
0
def test_taxa_richness_field(zone_example_grid):
    mg = zone_example_grid

    se = SpeciesEvolver(mg)

    expected_field = np.zeros(mg.number_of_nodes)
    np.testing.assert_array_equal(mg.at_node["taxa__richness"], expected_field)

    introduced_taxa = [TestTaxon(), TestTaxon()]
    se.track_taxa(introduced_taxa)
    se.run_one_step(10)

    expected_field = np.array([0, 0, 0, 2, 2, 2, 0, 0, 0])
    np.testing.assert_array_equal(mg.at_node["taxa__richness"], expected_field)
Beispiel #13
0
def test_many_to_one(zone_example_grid):
    mg, z = zone_example_grid

    # Create two zones for time 0.

    z[[8, 9, 10, 12]] = 1

    se = SpeciesEvolver(mg)
    sc = ZoneController(mg, zone_func)
    taxa = sc.populate_zones_uniformly(1)
    se.track_taxa(taxa)

    expected_df = pd.DataFrame(
        {
            "time": [0],
            "zones": [2],
            "fragmentations": [np.nan],
            "captures": [np.nan],
            "area_captured_sum": [np.nan],
            "area_captured_max": [np.nan],
        }
    )
    pd.testing.assert_frame_equal(sc.record_data_frame, expected_df, check_like=True)

    np.testing.assert_equal(len(se.get_extant_taxon_objects(time=0)), 2)

    # Modify elevation such that two zones each overlap the original two zones.

    z[11] = 1

    sc.run_one_step(1)
    se.run_one_step(1)

    np.testing.assert_equal(len(sc.zones), 1)
    np.testing.assert_equal(sc.zones[0]._conn_type, zn.Connection.MANY_TO_ONE)

    expected_df = pd.DataFrame(
        {
            "time": [0, 1],
            "zones": [2, 1],
            "fragmentations": [np.nan, 0],
            "captures": [np.nan, 1],
            "area_captured_sum": [np.nan, 12],
            "area_captured_max": [np.nan, 12],
        }
    )
    pd.testing.assert_frame_equal(sc.record_data_frame, expected_df, check_like=True)

    np.testing.assert_equal(len(se.get_extant_taxon_objects(time=1)), 2)
Beispiel #14
0
def test_none_to_one(zone_example_grid):
    mg, z = zone_example_grid

    # No zones exist at time 0.

    se = SpeciesEvolver(mg)
    sc = ZoneController(mg, zone_func)
    taxa = sc.populate_zones_uniformly(1)
    se.track_taxa(taxa)

    np.testing.assert_equal(len(sc.zones), 0)

    expected_df = pd.DataFrame({
        'time': [0],
        'zones': [0],
        'fragmentations': [np.nan],
        'captures': [np.nan],
        'area_captured_sum': [np.nan],
        'area_captured_max': [np.nan]
    })
    pd.testing.assert_frame_equal(sc.record_data_frame,
                                  expected_df,
                                  check_like=True)

    # Create a zone for time 1.

    z[[9, 10, 11, 12]] = 1

    sc.run_one_step(1)
    se.run_one_step(1)

    np.testing.assert_equal(len(sc.zones), 1)
    np.testing.assert_equal(sc.zones[0]._conn_type, zn.Connection.NONE_TO_ONE)

    expected_df = pd.DataFrame({
        'time': [0, 1],
        'zones': [0, 1],
        'fragmentations': [np.nan, 0],
        'captures': [np.nan, 0],
        'area_captured_sum': [np.nan, 0],
        'area_captured_max': [np.nan, 0]
    })
    pd.testing.assert_frame_equal(sc.record_data_frame,
                                  expected_df,
                                  check_like=True)

    np.testing.assert_equal(se.record_data_frame.taxa.sum(), 0)
Beispiel #15
0
def test_one_to_many_to_one(zone_example_grid):
    mg, z = zone_example_grid

    z[[9, 10, 11, 12]] = 1

    se = SpeciesEvolver(mg)
    sc = ZoneController(mg, zone_func)
    taxa = sc.populate_zones_uniformly(1, time_to_allopatric_speciation=1)
    se.track_taxa(taxa)

    z[11] = 0
    sc.run_one_step(1)
    se.run_one_step(1)

    z[11] = 1
    sc.run_one_step(1)
    se.run_one_step(1)

    np.testing.assert_equal(len(se.get_extant_taxon_objects()), 1)
Beispiel #16
0
def test_pseudoextinction(zone_example_grid):
    mg, z = zone_example_grid

    z[[9, 10, 11, 12]] = 1

    se = SpeciesEvolver(mg)
    sc = ZoneController(mg, zone_func)
    taxa = sc.populate_zones_uniformly(1, persists_post_speciation=False)
    se.track_taxa(taxa)

    z[11] = 0
    sc.run_one_step(1)
    se.run_one_step(1)

    expected_df = pd.DataFrame(
        {
            "pid": [np.nan, 0, 0],
            "type": 3 * [ZoneTaxon.__name__],
            "t_first": [0, 1, 1],
            "t_final": [1, np.nan, np.nan],
        },
        index=[0, 1, 2],
    )
    expected_df.index.name = "tid"
    expected_df["pid"] = expected_df["pid"].astype("Int64")
    expected_df["t_final"] = expected_df["t_final"].astype("Int64")

    pd.testing.assert_frame_equal(se.taxa_data_frame, expected_df, check_like=True)

    expected_df = pd.DataFrame(
        {
            "time": [0, 1],
            "taxa": [1, 2],
            "speciations": [np.nan, 2],
            "extinctions": [np.nan, 0],
            "pseudoextinctions": [np.nan, 1],
        }
    )
    pd.testing.assert_frame_equal(se.record_data_frame, expected_df, check_like=True)