예제 #1
0
def test_io_round_trip():

    with path.tempdir() as p:
        full_path = os.path.join(p, "relationship.csv")
        four_to_plenty.save_to(full_path)

        retrieved = Relationship.load_from(full_path)

        assert four_to_plenty.seed == retrieved.seed
        assert four_to_plenty.unique_tos() == retrieved.unique_tos()
        assert four_to_plenty.grouped.keys() == retrieved.grouped.keys()

        expected_relations = four_to_plenty.get_relations().sort_values(
            ["from", "to"]).reset_index()
        actual_relations = retrieved.get_relations().sort_values(
            ["from", "to"]).reset_index()

        assert expected_relations["from"].equals(actual_relations["from"])
        assert expected_relations["to"].equals(actual_relations["to"])
        assert expected_relations["weight"].equals(actual_relations["weight"])
예제 #2
0
    def load_from(folder, circus):
        """
        Reads all persistent data of this population and loads it

        :param folder: folder containing all CSV files of this population
        :param circus: parent circus containing this population
        :return:
        """

        ids_path = os.path.join(folder, "ids.csv")
        ids = pd.read_csv(ids_path, index_col=0, names=[]).index

        attribute_dir = os.path.join(folder, "attributes")
        if os.path.exists(attribute_dir):
            attributes = {
                filename[:-4]:
                Attribute.load_from(os.path.join(attribute_dir, filename))
                for filename in os.listdir(attribute_dir)
            }
        else:
            attributes = {}

        relationships_dir = os.path.join(folder, "relationships")
        if os.path.exists(relationships_dir):
            relationships = {
                filename[:-4]: Relationship.load_from(
                    os.path.join(relationships_dir, filename))
                for filename in os.listdir(relationships_dir)
            }
        else:
            relationships = {}

        population = Population(circus=circus, size=0)
        population.attributes = attributes
        population.relationships = relationships
        population.ids = ids
        population.size = len(ids)

        return population