aegis_sim.recording.flushrecorder
1import copy 2import numpy as np 3from aegis_sim.constants import VALID_CAUSES_OF_DEATH 4import pathlib 5from aegis_sim.utilities.funcs import skip 6 7from aegis_sim.parameterization import parametermanager 8from .recorder import Recorder 9 10 11class FlushRecorder(Recorder): 12 """ 13 14 Records collections. 15 """ 16 17 def __init__(self, odir: pathlib.Path, resuming=False): 18 19 self.odir = odir / "gui" / "spectra" 20 self.init_odir() 21 22 self.n_ages = parametermanager.parameters.AGE_LIMIT + 1 23 24 self._collection = { 25 "age_at_birth": [0] * self.n_ages, 26 "additive_age_structure": [0] * self.n_ages, 27 } 28 29 self._collection.update({f"age_at_{causeofdeath}": [0] * self.n_ages for causeofdeath in VALID_CAUSES_OF_DEATH}) 30 31 self.collection = copy.deepcopy(self._collection) 32 33 if not resuming: 34 self.init_headers() 35 36 def collect(self, key, ages): 37 """Add data into memory which will be recorded later.""" 38 self.collection[key] += np.bincount(ages, minlength=self.n_ages) 39 40 def flush(self): 41 """Record data that has been collected over time.""" 42 # spectra/*.csv | Age distribution of various subpopulations (e.g. population that died of genetic causes) 43 44 if skip("INTERVAL_RATE"): 45 return 46 47 for key, val in self.collection.items(): 48 self.write_age_at(filename=key, collected_values=val) 49 # with open(self.odir / f"{key}.csv", "ab") as f: 50 # array = np.array(val) 51 # np.savetxt(f, [array], delimiter=",", fmt="%i") 52 53 # Reinitialize the collection 54 self.collection = copy.deepcopy(self._collection) 55 56 def write_age_at(self, filename, collected_values): 57 """ 58 59 # OUTPUT SPECIFICATION 60 path: /gui/spectra/age_at_{cause}.csv 61 filetype: csv 62 category: demography 63 description: Total number of deaths by age and cause of death, within a simulation interval. 64 trait granularity: population count 65 time granularity: interval 66 frequency parameter: INTERVAL_RATE 67 structure: An int matrix. 68 header: list of ages from 0 to AGE_LIMIT (inclusive) 69 """ 70 with open(self.odir / f"{filename}.csv", "ab") as f: 71 array = np.array(collected_values) 72 np.savetxt(f, [array], delimiter=",", fmt="%i") 73 74 def init_headers(self): 75 for key in self._collection.keys(): 76 with open(self.odir / f"{key}.csv", "ab") as f: 77 array = np.arange(self.n_ages) 78 np.savetxt(f, [array], delimiter=",", fmt="%i")
12class FlushRecorder(Recorder): 13 """ 14 15 Records collections. 16 """ 17 18 def __init__(self, odir: pathlib.Path, resuming=False): 19 20 self.odir = odir / "gui" / "spectra" 21 self.init_odir() 22 23 self.n_ages = parametermanager.parameters.AGE_LIMIT + 1 24 25 self._collection = { 26 "age_at_birth": [0] * self.n_ages, 27 "additive_age_structure": [0] * self.n_ages, 28 } 29 30 self._collection.update({f"age_at_{causeofdeath}": [0] * self.n_ages for causeofdeath in VALID_CAUSES_OF_DEATH}) 31 32 self.collection = copy.deepcopy(self._collection) 33 34 if not resuming: 35 self.init_headers() 36 37 def collect(self, key, ages): 38 """Add data into memory which will be recorded later.""" 39 self.collection[key] += np.bincount(ages, minlength=self.n_ages) 40 41 def flush(self): 42 """Record data that has been collected over time.""" 43 # spectra/*.csv | Age distribution of various subpopulations (e.g. population that died of genetic causes) 44 45 if skip("INTERVAL_RATE"): 46 return 47 48 for key, val in self.collection.items(): 49 self.write_age_at(filename=key, collected_values=val) 50 # with open(self.odir / f"{key}.csv", "ab") as f: 51 # array = np.array(val) 52 # np.savetxt(f, [array], delimiter=",", fmt="%i") 53 54 # Reinitialize the collection 55 self.collection = copy.deepcopy(self._collection) 56 57 def write_age_at(self, filename, collected_values): 58 """ 59 60 # OUTPUT SPECIFICATION 61 path: /gui/spectra/age_at_{cause}.csv 62 filetype: csv 63 category: demography 64 description: Total number of deaths by age and cause of death, within a simulation interval. 65 trait granularity: population count 66 time granularity: interval 67 frequency parameter: INTERVAL_RATE 68 structure: An int matrix. 69 header: list of ages from 0 to AGE_LIMIT (inclusive) 70 """ 71 with open(self.odir / f"{filename}.csv", "ab") as f: 72 array = np.array(collected_values) 73 np.savetxt(f, [array], delimiter=",", fmt="%i") 74 75 def init_headers(self): 76 for key in self._collection.keys(): 77 with open(self.odir / f"{key}.csv", "ab") as f: 78 array = np.arange(self.n_ages) 79 np.savetxt(f, [array], delimiter=",", fmt="%i")
Records collections.
FlushRecorder(odir: pathlib.Path, resuming=False)
18 def __init__(self, odir: pathlib.Path, resuming=False): 19 20 self.odir = odir / "gui" / "spectra" 21 self.init_odir() 22 23 self.n_ages = parametermanager.parameters.AGE_LIMIT + 1 24 25 self._collection = { 26 "age_at_birth": [0] * self.n_ages, 27 "additive_age_structure": [0] * self.n_ages, 28 } 29 30 self._collection.update({f"age_at_{causeofdeath}": [0] * self.n_ages for causeofdeath in VALID_CAUSES_OF_DEATH}) 31 32 self.collection = copy.deepcopy(self._collection) 33 34 if not resuming: 35 self.init_headers()
def
collect(self, key, ages):
37 def collect(self, key, ages): 38 """Add data into memory which will be recorded later.""" 39 self.collection[key] += np.bincount(ages, minlength=self.n_ages)
Add data into memory which will be recorded later.
def
flush(self):
41 def flush(self): 42 """Record data that has been collected over time.""" 43 # spectra/*.csv | Age distribution of various subpopulations (e.g. population that died of genetic causes) 44 45 if skip("INTERVAL_RATE"): 46 return 47 48 for key, val in self.collection.items(): 49 self.write_age_at(filename=key, collected_values=val) 50 # with open(self.odir / f"{key}.csv", "ab") as f: 51 # array = np.array(val) 52 # np.savetxt(f, [array], delimiter=",", fmt="%i") 53 54 # Reinitialize the collection 55 self.collection = copy.deepcopy(self._collection)
Record data that has been collected over time.
def
write_age_at(self, filename, collected_values):
57 def write_age_at(self, filename, collected_values): 58 """ 59 60 # OUTPUT SPECIFICATION 61 path: /gui/spectra/age_at_{cause}.csv 62 filetype: csv 63 category: demography 64 description: Total number of deaths by age and cause of death, within a simulation interval. 65 trait granularity: population count 66 time granularity: interval 67 frequency parameter: INTERVAL_RATE 68 structure: An int matrix. 69 header: list of ages from 0 to AGE_LIMIT (inclusive) 70 """ 71 with open(self.odir / f"{filename}.csv", "ab") as f: 72 array = np.array(collected_values) 73 np.savetxt(f, [array], delimiter=",", fmt="%i")
OUTPUT SPECIFICATION
path: /gui/spectra/age_at_{cause}.csv filetype: csv category: demography description: Total number of deaths by age and cause of death, within a simulation interval. trait granularity: population count time granularity: interval frequency parameter: INTERVAL_RATE structure: An int matrix. header: list of ages from 0 to AGE_LIMIT (inclusive)