aegis_sim.recording.summaryrecorder

 1import json
 2import time
 3import psutil
 4import numpy as np
 5import pathlib
 6import datetime
 7
 8from aegis_sim.utilities.get_folder_size import get_folder_size_with_du
 9
10from aegis_sim import variables
11from .recorder import Recorder
12
13
14class SummaryRecorder(Recorder):
15    """
16
17    Records once.
18    """
19
20    def __init__(self, odir: pathlib.Path):
21        self.odir = odir
22        self.init_odir()
23        self.time_start = time.time()
24        self.extinct = False
25
26        self.memuse = []
27        self.pp = psutil.Process()
28
29    def write_output_summary(self):
30        """
31
32        # OUTPUT SPECIFICATION
33        path: /output_summary.json
34        filetype: json
35        category: log
36        description: A json dictionary documenting a number of simulation traits at the end of the simulation; e.g. extinction status, random seed, time at start, total runtime, median memory use, total storage use.
37        trait granularity: N/A
38        time granularity: N/A
39        frequency parameter: once
40        structure: A json dictionary.
41        header: None
42        """
43        try:
44            storage_use = get_folder_size_with_du(self.odir)
45        except:
46            storage_use = ""
47
48        summary = {
49            "extinct": self.extinct,
50            "random_seed": variables.random_seed,
51            "time_start": self.time_start,
52            "runtime": time.time() - self.time_start,
53            "memory_use": self.get_median_memuse(),
54            "storage_use": storage_use,
55        }
56        with open(self.odir / "output_summary.json", "w") as f:
57            json.dump(summary, f, indent=4)
58
59    def write_input_summary(self, ticker_pid):
60        """
61
62        # OUTPUT SPECIFICATION
63        path: /input_summary.json
64        filetype: json
65        category: log
66        description: A json dictionary documenting a number of simulation traits at the start of the simulation; e.g. random seed, time at start.
67        trait granularity:
68        time granularity:
69        frequency parameter: once
70        structure: A json dictionary.
71        header: None
72        """
73        summary = {
74            "random_seed": variables.random_seed,
75            "time_start": self.time_start,
76            "pickle_path": str(variables.pickle_path),
77            "time_start_formatted": datetime.datetime.fromtimestamp(self.time_start).strftime("%Y-%m-%d %H:%M:%S"),
78            "pid": self.pp.pid,
79            "ticker_pid": ticker_pid,
80        }
81        with open(self.odir / "input_summary.json", "w") as f:
82            json.dump(summary, f, indent=4)
83
84    def record_memuse(self):
85        # TODO refine
86        memuse_ = self.pp.memory_info()[0] / float(2**20)
87        self.memuse.append(memuse_)
88        if len(self.memuse) > 1000:
89            self.memuse.pop(0)
90
91    def get_median_memuse(self):
92        return np.median(self.memuse)
class SummaryRecorder(aegis_sim.recording.recorder.Recorder):
15class SummaryRecorder(Recorder):
16    """
17
18    Records once.
19    """
20
21    def __init__(self, odir: pathlib.Path):
22        self.odir = odir
23        self.init_odir()
24        self.time_start = time.time()
25        self.extinct = False
26
27        self.memuse = []
28        self.pp = psutil.Process()
29
30    def write_output_summary(self):
31        """
32
33        # OUTPUT SPECIFICATION
34        path: /output_summary.json
35        filetype: json
36        category: log
37        description: A json dictionary documenting a number of simulation traits at the end of the simulation; e.g. extinction status, random seed, time at start, total runtime, median memory use, total storage use.
38        trait granularity: N/A
39        time granularity: N/A
40        frequency parameter: once
41        structure: A json dictionary.
42        header: None
43        """
44        try:
45            storage_use = get_folder_size_with_du(self.odir)
46        except:
47            storage_use = ""
48
49        summary = {
50            "extinct": self.extinct,
51            "random_seed": variables.random_seed,
52            "time_start": self.time_start,
53            "runtime": time.time() - self.time_start,
54            "memory_use": self.get_median_memuse(),
55            "storage_use": storage_use,
56        }
57        with open(self.odir / "output_summary.json", "w") as f:
58            json.dump(summary, f, indent=4)
59
60    def write_input_summary(self, ticker_pid):
61        """
62
63        # OUTPUT SPECIFICATION
64        path: /input_summary.json
65        filetype: json
66        category: log
67        description: A json dictionary documenting a number of simulation traits at the start of the simulation; e.g. random seed, time at start.
68        trait granularity:
69        time granularity:
70        frequency parameter: once
71        structure: A json dictionary.
72        header: None
73        """
74        summary = {
75            "random_seed": variables.random_seed,
76            "time_start": self.time_start,
77            "pickle_path": str(variables.pickle_path),
78            "time_start_formatted": datetime.datetime.fromtimestamp(self.time_start).strftime("%Y-%m-%d %H:%M:%S"),
79            "pid": self.pp.pid,
80            "ticker_pid": ticker_pid,
81        }
82        with open(self.odir / "input_summary.json", "w") as f:
83            json.dump(summary, f, indent=4)
84
85    def record_memuse(self):
86        # TODO refine
87        memuse_ = self.pp.memory_info()[0] / float(2**20)
88        self.memuse.append(memuse_)
89        if len(self.memuse) > 1000:
90            self.memuse.pop(0)
91
92    def get_median_memuse(self):
93        return np.median(self.memuse)

Records once.

SummaryRecorder(odir: pathlib.Path)
21    def __init__(self, odir: pathlib.Path):
22        self.odir = odir
23        self.init_odir()
24        self.time_start = time.time()
25        self.extinct = False
26
27        self.memuse = []
28        self.pp = psutil.Process()
odir
time_start
extinct
memuse
pp
def write_output_summary(self):
30    def write_output_summary(self):
31        """
32
33        # OUTPUT SPECIFICATION
34        path: /output_summary.json
35        filetype: json
36        category: log
37        description: A json dictionary documenting a number of simulation traits at the end of the simulation; e.g. extinction status, random seed, time at start, total runtime, median memory use, total storage use.
38        trait granularity: N/A
39        time granularity: N/A
40        frequency parameter: once
41        structure: A json dictionary.
42        header: None
43        """
44        try:
45            storage_use = get_folder_size_with_du(self.odir)
46        except:
47            storage_use = ""
48
49        summary = {
50            "extinct": self.extinct,
51            "random_seed": variables.random_seed,
52            "time_start": self.time_start,
53            "runtime": time.time() - self.time_start,
54            "memory_use": self.get_median_memuse(),
55            "storage_use": storage_use,
56        }
57        with open(self.odir / "output_summary.json", "w") as f:
58            json.dump(summary, f, indent=4)

OUTPUT SPECIFICATION

path: /output_summary.json filetype: json category: log description: A json dictionary documenting a number of simulation traits at the end of the simulation; e.g. extinction status, random seed, time at start, total runtime, median memory use, total storage use. trait granularity: N/A time granularity: N/A frequency parameter: once structure: A json dictionary. header: None

def write_input_summary(self, ticker_pid):
60    def write_input_summary(self, ticker_pid):
61        """
62
63        # OUTPUT SPECIFICATION
64        path: /input_summary.json
65        filetype: json
66        category: log
67        description: A json dictionary documenting a number of simulation traits at the start of the simulation; e.g. random seed, time at start.
68        trait granularity:
69        time granularity:
70        frequency parameter: once
71        structure: A json dictionary.
72        header: None
73        """
74        summary = {
75            "random_seed": variables.random_seed,
76            "time_start": self.time_start,
77            "pickle_path": str(variables.pickle_path),
78            "time_start_formatted": datetime.datetime.fromtimestamp(self.time_start).strftime("%Y-%m-%d %H:%M:%S"),
79            "pid": self.pp.pid,
80            "ticker_pid": ticker_pid,
81        }
82        with open(self.odir / "input_summary.json", "w") as f:
83            json.dump(summary, f, indent=4)

OUTPUT SPECIFICATION

path: /input_summary.json filetype: json category: log description: A json dictionary documenting a number of simulation traits at the start of the simulation; e.g. random seed, time at start. trait granularity: time granularity: frequency parameter: once structure: A json dictionary. header: None

def record_memuse(self):
85    def record_memuse(self):
86        # TODO refine
87        memuse_ = self.pp.memory_info()[0] / float(2**20)
88        self.memuse.append(memuse_)
89        if len(self.memuse) > 1000:
90            self.memuse.pop(0)
def get_median_memuse(self):
92    def get_median_memuse(self):
93        return np.median(self.memuse)