def test_creating_an_empty_population_and_adding_attributes_later_should_be_possible(
):

    # empty population
    a = Population(circus=None, size=0)
    assert a.ids.shape[0] == 0

    # empty attributes
    a.create_attribute("att1")
    a.create_attribute("att2")

    dynamically_created = pd.DataFrame(
        {
            "att1": [1, 2, 3],
            "att2": [11, 12, 13],
        },
        index=["ac1", "ac2", "ac3"])

    a.update(dynamically_created)

    assert a.ids.tolist() == ["ac1", "ac2", "ac3"]
    assert a.get_attribute_values("att1",
                                  ["ac1", "ac2", "ac3"]).tolist() == [1, 2, 3]
    assert a.get_attribute_values(
        "att2", ["ac1", "ac2", "ac3"]).tolist() == [11, 12, 13]
def test_insert_poppulation_value_for_existing_populations_should_update_all_values(
):

    # copy of dummy population that will be updated
    tested_population = Population(circus=None,
                                   size=10,
                                   ids_gen=SequencialGenerator(max_length=1,
                                                               prefix="a_"))
    ages = [10, 20, 40, 10, 100, 98, 12, 39, 76, 23]
    tested_population.create_attribute("age", init_values=ages)
    city = ["a", "b", "b", "a", "d", "e", "r", "a", "z", "c"]
    tested_population.create_attribute("city", init_values=city)

    current = tested_population.get_attribute_values("age",
                                                     ["a_0", "a_7", "a_9"])
    assert current.tolist() == [10, 39, 23]

    update = pd.DataFrame({
        "age": [139, 123],
        "city": ["city_7", "city_9"]
    },
                          index=["a_7", "a_9"])

    tested_population.update(update)

    # we should have the same number of populations
    assert tested_population.ids.shape[0] == 10

    updated_age = tested_population.get_attribute_values(
        "age", ["a_0", "a_7", "a_9"])
    updated_city = tested_population.get_attribute_values(
        "city", ["a_0", "a_7", "a_9"])

    assert updated_age.tolist() == [10, 139, 123]
    assert updated_city.tolist() == ["a", "city_7", "city_9"]
def test_insert_op_population_value_for_existing_populations_should_update_all_values(
):
    # same as test above but triggered as an Operation on story data

    # copy of dummy population that will be updated
    tested_population = Population(circus=None,
                                   size=10,
                                   ids_gen=SequencialGenerator(max_length=1,
                                                               prefix="a_"))
    ages = [10, 20, 40, 10, 100, 98, 12, 39, 76, 23]
    tested_population.create_attribute("age", init_values=ages)
    city = ["a", "b", "b", "a", "d", "e", "r", "a", "z", "c"]
    tested_population.create_attribute("city", init_values=city)

    story_data = pd.DataFrame(
        {
            "the_new_age": [139, 123, 1, 2],
            "location": ["city_7", "city_9", "city_11", "city_10"],
            "updated_populations": ["a_7", "a_9", "a_11", "a_10"]
        },
        index=["d_1", "d_2", "d_4", "d_3"])

    update_op = tested_population.ops.update(id_field="updated_populations",
                                             copy_attributes_from_fields={
                                                 "age": "the_new_age",
                                                 "city": "location"
                                             })

    story_data_2, logs = update_op(story_data)

    # there should be no impact on the story data
    assert story_data_2.shape == (4, 3)
    assert sorted(story_data_2.columns.tolist()) == [
        "location", "the_new_age", "updated_populations"
    ]

    # we should have 2 new populations
    assert tested_population.ids.shape[0] == 12

    updated_age = tested_population.get_attribute_values(
        "age", ["a_0", "a_7", "a_9", "a_10", "a_11"])
    updated_city = tested_population.get_attribute_values(
        "city", ["a_0", "a_7", "a_9", "a_10", "a_11"])

    assert updated_age.tolist() == [10, 139, 123, 2, 1]
    assert updated_city.tolist() == [
        "a", "city_7", "city_9", "city_10", "city_11"
    ]
Exemple #4
0
    def create_random_cells(self, n_cells):
        """
        Creation of a basic population for cells, with latitude and longitude
        """

        cells = Population(size=n_cells)

        latitude_generator = FakerGenerator(method="latitude",
                                            seed=next(self.seeder))
        longitude_generator = FakerGenerator(method="longitude",
                                             seed=next(self.seeder))

        cells.create_attribute("latitude", init_gen=latitude_generator)
        cells.create_attribute("longitude", init_gen=longitude_generator)

        return cells
def test_overwrite_attribute():

    population = Population(circus=tc, size=10,
                            ids_gen=SequencialGenerator(prefix="u_", max_length=1))

    ages = [10, 20, 40, 10, 100, 98, 12, 39, 76, 23]
    age_attr = population.create_attribute("age", init_values=ages)

    # before modification
    ages = age_attr.get_values(["u_0", "u_4", "u_9"]).tolist()
    assert ages == [10, 100, 23]

    story_data = pd.DataFrame({
        # id of the populations to update
        "A_ID": ["u_4", "u_0"],

        # new values to copy
        "new_ages": [34, 30]},

        # index of the story data has, in general, nothing to do with the
        # updated population
        index=["cust_1", "cust_2"]
    )

    update = age_attr.ops.update(
        member_id_field="A_ID",
        copy_from_field="new_ages"
    )

    _, logs = update(story_data)

    assert logs == {}
    # before modification
    ages = age_attr.get_values(["u_0", "u_4", "u_9"]).tolist()
    assert ages == [30, 34, 23]
import path
import pandas as pd
import os
import pytest

from trumania.core.random_generators import SequencialGenerator
from trumania.core.population import Population

dummy_population = Population(circus=None,
                              size=10,
                              ids_gen=SequencialGenerator(max_length=1,
                                                          prefix="id_"))

ages = [10, 20, 40, 10, 100, 98, 12, 39, 76, 23]
dummy_population.create_attribute("age", init_values=ages)

city = ["a", "b", "b", "a", "d", "e", "r", "a", "z", "c"]
dummy_population.create_attribute("city", init_values=city)

# some fake story data with an index corresponding to another population
# => simulates an story triggered by that other population
# the column "NEIGHBOUR" contains value that point to the dummy population, with
# a duplication (id2)
story_data = pd.DataFrame(
    {
        "A": ["a1", "a2", "a3", "a4"],
        "B": ["b1", "b2", "b3", "b4"],
        "NEIGHBOUR": ["id_2", "id_4", "id_7", "id_2"],
        "COUSINS": [
            ["id_2", "id_4", "id_7", "id_2"],
            ["id_3"],