aegis_sim.recording.featherrecorder

  1import logging
  2import pandas as pd
  3import numpy as np
  4
  5import pathlib
  6
  7from aegis_sim.dataclasses.population import Population
  8from .recorder import Recorder
  9from aegis_sim import variables
 10
 11from aegis_sim.parameterization import parametermanager
 12from aegis_sim.utilities.funcs import steps_to_end, skip
 13
 14
 15class FeatherRecorder(Recorder):
 16    def __init__(self, odir: pathlib.Path):
 17        self.odir_genotypes = odir / "snapshots" / "genotypes"
 18        self.odir_phenotypes = odir / "snapshots" / "phenotypes"
 19        self.odir_demography = odir / "snapshots" / "demography"
 20        self.init_dir(self.odir_genotypes)
 21        self.init_dir(self.odir_phenotypes)
 22        self.init_dir(self.odir_demography)
 23
 24    def write(self, population: Population):
 25        """Record demographic, genetic and phenotypic data from the current population."""
 26
 27        # If not final snapshots to be taken, and about to skip or the population is extinct, do not write.
 28        final_snapshots = parametermanager.parameters.SNAPSHOT_FINAL_COUNT > steps_to_end()
 29        if not final_snapshots and (skip("SNAPSHOT_RATE") or len(population) == 0):
 30            return
 31
 32        step = variables.steps
 33
 34        logging.debug(f"Snapshots recorded at step {step}.")
 35
 36        self.write_genotypes(step=step, population=population)
 37        self.write_phenotypes(step=step, population=population)
 38        self.write_demography(step=step, population=population)
 39
 40    def write_genotypes(self, step: int, population: Population):
 41        """
 42
 43        # OUTPUT SPECIFICATION
 44        path: /snapshots/genotypes/{step}.feather
 45        filetype: feather
 46        category: genotype
 47        description: A snapshot of complete binary genomes of all individuals at a certain simulation step.
 48        trait granularity: individual
 49        time granularity: snapshot
 50        frequency parameter: SNAPSHOT_RATE
 51        structure: A bool matrix; rows: individuals, columns: genome positions, values: bit states
 52        header: genome positions
 53        """
 54        df_gen = pd.DataFrame(np.array(population.genomes.flatten()))
 55        df_gen.reset_index(drop=True, inplace=True)
 56        df_gen.columns = [str(c) for c in df_gen.columns]
 57        df_gen.to_feather(self.odir_genotypes / f"{step}.feather")
 58
 59    def write_phenotypes(self, step: int, population: Population):
 60        # TODO add more info to columns and rows
 61        """
 62
 63        # OUTPUT SPECIFICATION
 64        path: /snapshots/phenotypes/{step}.feather
 65        filetype: feather
 66        category: phenotype
 67        description: A snapshot of complete intrinsic phenotypes of all individuals at a certain simulation step.
 68        trait granularity: individual
 69        time granularity: snapshot
 70        frequency parameter: SNAPSHOT_RATE
 71        structure: A float matrix; rows: individuals, columns: individual phenotypic traits (depending on which traits are evolvable and what is max lifespan), values: trait values
 72        """
 73        # TODO bugged, wrong header
 74        df_phe = pd.DataFrame(population.phenotypes.get())
 75        df_phe.reset_index(drop=True, inplace=True)
 76        df_phe.columns = [str(c) for c in df_phe.columns]
 77        df_phe.to_feather(self.odir_phenotypes / f"{step}.feather")
 78
 79    def write_demography(self, step: int, population: Population):
 80        """
 81
 82        # OUTPUT SPECIFICATION
 83        path: /snapshots/demography/{step}.feather
 84        filetype: feather
 85        category: demography
 86        description: A recording of life history metrics (age, number of births given, step at which born, current size, sex) of all individuals until a certain simulation step.
 87        trait granularity: individual
 88        time granularity: snapshot
 89        frequency parameter: SNAPSHOT_RATE
 90        structure: A matrix of ints and floats
 91        header: ['ages', 'births', 'birthdays', 'sizes', 'sexes']
 92        """
 93        dem_attrs = [
 94            "ages",
 95            "births",
 96            "birthdays",
 97            # "generations",
 98            "sizes",
 99            "sexes",
100        ]
101        demo = {attr: getattr(population, attr) for attr in dem_attrs}
102        df_dem = pd.DataFrame(demo, columns=dem_attrs)
103        df_dem.reset_index(drop=True, inplace=True)
104        df_dem.to_feather(self.odir_demography / f"{step}.feather")
class FeatherRecorder(aegis_sim.recording.recorder.Recorder):
 16class FeatherRecorder(Recorder):
 17    def __init__(self, odir: pathlib.Path):
 18        self.odir_genotypes = odir / "snapshots" / "genotypes"
 19        self.odir_phenotypes = odir / "snapshots" / "phenotypes"
 20        self.odir_demography = odir / "snapshots" / "demography"
 21        self.init_dir(self.odir_genotypes)
 22        self.init_dir(self.odir_phenotypes)
 23        self.init_dir(self.odir_demography)
 24
 25    def write(self, population: Population):
 26        """Record demographic, genetic and phenotypic data from the current population."""
 27
 28        # If not final snapshots to be taken, and about to skip or the population is extinct, do not write.
 29        final_snapshots = parametermanager.parameters.SNAPSHOT_FINAL_COUNT > steps_to_end()
 30        if not final_snapshots and (skip("SNAPSHOT_RATE") or len(population) == 0):
 31            return
 32
 33        step = variables.steps
 34
 35        logging.debug(f"Snapshots recorded at step {step}.")
 36
 37        self.write_genotypes(step=step, population=population)
 38        self.write_phenotypes(step=step, population=population)
 39        self.write_demography(step=step, population=population)
 40
 41    def write_genotypes(self, step: int, population: Population):
 42        """
 43
 44        # OUTPUT SPECIFICATION
 45        path: /snapshots/genotypes/{step}.feather
 46        filetype: feather
 47        category: genotype
 48        description: A snapshot of complete binary genomes of all individuals at a certain simulation step.
 49        trait granularity: individual
 50        time granularity: snapshot
 51        frequency parameter: SNAPSHOT_RATE
 52        structure: A bool matrix; rows: individuals, columns: genome positions, values: bit states
 53        header: genome positions
 54        """
 55        df_gen = pd.DataFrame(np.array(population.genomes.flatten()))
 56        df_gen.reset_index(drop=True, inplace=True)
 57        df_gen.columns = [str(c) for c in df_gen.columns]
 58        df_gen.to_feather(self.odir_genotypes / f"{step}.feather")
 59
 60    def write_phenotypes(self, step: int, population: Population):
 61        # TODO add more info to columns and rows
 62        """
 63
 64        # OUTPUT SPECIFICATION
 65        path: /snapshots/phenotypes/{step}.feather
 66        filetype: feather
 67        category: phenotype
 68        description: A snapshot of complete intrinsic phenotypes of all individuals at a certain simulation step.
 69        trait granularity: individual
 70        time granularity: snapshot
 71        frequency parameter: SNAPSHOT_RATE
 72        structure: A float matrix; rows: individuals, columns: individual phenotypic traits (depending on which traits are evolvable and what is max lifespan), values: trait values
 73        """
 74        # TODO bugged, wrong header
 75        df_phe = pd.DataFrame(population.phenotypes.get())
 76        df_phe.reset_index(drop=True, inplace=True)
 77        df_phe.columns = [str(c) for c in df_phe.columns]
 78        df_phe.to_feather(self.odir_phenotypes / f"{step}.feather")
 79
 80    def write_demography(self, step: int, population: Population):
 81        """
 82
 83        # OUTPUT SPECIFICATION
 84        path: /snapshots/demography/{step}.feather
 85        filetype: feather
 86        category: demography
 87        description: A recording of life history metrics (age, number of births given, step at which born, current size, sex) of all individuals until a certain simulation step.
 88        trait granularity: individual
 89        time granularity: snapshot
 90        frequency parameter: SNAPSHOT_RATE
 91        structure: A matrix of ints and floats
 92        header: ['ages', 'births', 'birthdays', 'sizes', 'sexes']
 93        """
 94        dem_attrs = [
 95            "ages",
 96            "births",
 97            "birthdays",
 98            # "generations",
 99            "sizes",
100            "sexes",
101        ]
102        demo = {attr: getattr(population, attr) for attr in dem_attrs}
103        df_dem = pd.DataFrame(demo, columns=dem_attrs)
104        df_dem.reset_index(drop=True, inplace=True)
105        df_dem.to_feather(self.odir_demography / f"{step}.feather")
FeatherRecorder(odir: pathlib.Path)
17    def __init__(self, odir: pathlib.Path):
18        self.odir_genotypes = odir / "snapshots" / "genotypes"
19        self.odir_phenotypes = odir / "snapshots" / "phenotypes"
20        self.odir_demography = odir / "snapshots" / "demography"
21        self.init_dir(self.odir_genotypes)
22        self.init_dir(self.odir_phenotypes)
23        self.init_dir(self.odir_demography)
odir_genotypes
odir_phenotypes
odir_demography
def write(self, population: aegis_sim.dataclasses.population.Population):
25    def write(self, population: Population):
26        """Record demographic, genetic and phenotypic data from the current population."""
27
28        # If not final snapshots to be taken, and about to skip or the population is extinct, do not write.
29        final_snapshots = parametermanager.parameters.SNAPSHOT_FINAL_COUNT > steps_to_end()
30        if not final_snapshots and (skip("SNAPSHOT_RATE") or len(population) == 0):
31            return
32
33        step = variables.steps
34
35        logging.debug(f"Snapshots recorded at step {step}.")
36
37        self.write_genotypes(step=step, population=population)
38        self.write_phenotypes(step=step, population=population)
39        self.write_demography(step=step, population=population)

Record demographic, genetic and phenotypic data from the current population.

def write_genotypes( self, step: int, population: aegis_sim.dataclasses.population.Population):
41    def write_genotypes(self, step: int, population: Population):
42        """
43
44        # OUTPUT SPECIFICATION
45        path: /snapshots/genotypes/{step}.feather
46        filetype: feather
47        category: genotype
48        description: A snapshot of complete binary genomes of all individuals at a certain simulation step.
49        trait granularity: individual
50        time granularity: snapshot
51        frequency parameter: SNAPSHOT_RATE
52        structure: A bool matrix; rows: individuals, columns: genome positions, values: bit states
53        header: genome positions
54        """
55        df_gen = pd.DataFrame(np.array(population.genomes.flatten()))
56        df_gen.reset_index(drop=True, inplace=True)
57        df_gen.columns = [str(c) for c in df_gen.columns]
58        df_gen.to_feather(self.odir_genotypes / f"{step}.feather")

OUTPUT SPECIFICATION

path: /snapshots/genotypes/{step}.feather filetype: feather category: genotype description: A snapshot of complete binary genomes of all individuals at a certain simulation step. trait granularity: individual time granularity: snapshot frequency parameter: SNAPSHOT_RATE structure: A bool matrix; rows: individuals, columns: genome positions, values: bit states header: genome positions

def write_phenotypes( self, step: int, population: aegis_sim.dataclasses.population.Population):
60    def write_phenotypes(self, step: int, population: Population):
61        # TODO add more info to columns and rows
62        """
63
64        # OUTPUT SPECIFICATION
65        path: /snapshots/phenotypes/{step}.feather
66        filetype: feather
67        category: phenotype
68        description: A snapshot of complete intrinsic phenotypes of all individuals at a certain simulation step.
69        trait granularity: individual
70        time granularity: snapshot
71        frequency parameter: SNAPSHOT_RATE
72        structure: A float matrix; rows: individuals, columns: individual phenotypic traits (depending on which traits are evolvable and what is max lifespan), values: trait values
73        """
74        # TODO bugged, wrong header
75        df_phe = pd.DataFrame(population.phenotypes.get())
76        df_phe.reset_index(drop=True, inplace=True)
77        df_phe.columns = [str(c) for c in df_phe.columns]
78        df_phe.to_feather(self.odir_phenotypes / f"{step}.feather")

OUTPUT SPECIFICATION

path: /snapshots/phenotypes/{step}.feather filetype: feather category: phenotype description: A snapshot of complete intrinsic phenotypes of all individuals at a certain simulation step. trait granularity: individual time granularity: snapshot frequency parameter: SNAPSHOT_RATE structure: A float matrix; rows: individuals, columns: individual phenotypic traits (depending on which traits are evolvable and what is max lifespan), values: trait values

def write_demography( self, step: int, population: aegis_sim.dataclasses.population.Population):
 80    def write_demography(self, step: int, population: Population):
 81        """
 82
 83        # OUTPUT SPECIFICATION
 84        path: /snapshots/demography/{step}.feather
 85        filetype: feather
 86        category: demography
 87        description: A recording of life history metrics (age, number of births given, step at which born, current size, sex) of all individuals until a certain simulation step.
 88        trait granularity: individual
 89        time granularity: snapshot
 90        frequency parameter: SNAPSHOT_RATE
 91        structure: A matrix of ints and floats
 92        header: ['ages', 'births', 'birthdays', 'sizes', 'sexes']
 93        """
 94        dem_attrs = [
 95            "ages",
 96            "births",
 97            "birthdays",
 98            # "generations",
 99            "sizes",
100            "sexes",
101        ]
102        demo = {attr: getattr(population, attr) for attr in dem_attrs}
103        df_dem = pd.DataFrame(demo, columns=dem_attrs)
104        df_dem.reset_index(drop=True, inplace=True)
105        df_dem.to_feather(self.odir_demography / f"{step}.feather")

OUTPUT SPECIFICATION

path: /snapshots/demography/{step}.feather filetype: feather category: demography description: A recording of life history metrics (age, number of births given, step at which born, current size, sex) of all individuals until a certain simulation step. trait granularity: individual time granularity: snapshot frequency parameter: SNAPSHOT_RATE structure: A matrix of ints and floats header: ['ages', 'births', 'birthdays', 'sizes', 'sexes']