Skip to content

Commit 1ed0396

Browse files
committed
add timestamping to expression profile logs
1 parent 29d535a commit 1ed0396

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

activitysim/core/timing.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ def write_log(self, state: State) -> None:
9999
filename = self.log_file
100100
else:
101101
filename = state.get_log_file_path(
102-
str(Path("expr-performance") / self.log_file)
102+
str(Path("expr-performance") / self.log_file), timestamped=True
103103
)
104104

105105
# if the log file already exists and overwrite is false, create a new file
@@ -191,7 +191,9 @@ def _read_log(self, log_file: Path) -> pd.DataFrame:
191191
return df
192192

193193
def __init__(self, state: State, collect_mp: bool = True) -> None:
194-
self.log_dir = state.get_log_file_path(str(Path("expr-performance")))
194+
self.log_dir = state.get_log_file_path(
195+
str(Path("expr-performance")), timestamped=True
196+
)
195197
self.default_cutoff = state.settings.expression_profile_cutoff
196198
raw_data = {}
197199
for f in self.log_dir.glob("*.log"):
@@ -205,7 +207,9 @@ def __init__(self, state: State, collect_mp: bool = True) -> None:
205207

206208
if collect_mp:
207209
raw_data = {}
208-
mp_log_dirs = state.get_log_file_path(".").glob("*-expr-performance")
210+
mp_log_dirs = state.get_log_file_path(".", timestamped=True).glob(
211+
"*-expr-performance"
212+
)
209213
for mp_log_dir in mp_log_dirs:
210214
subproc_name = "-".join(mp_log_dir.stem.split("-")[:-2])
211215
for f in mp_log_dir.glob("*.log"):

activitysim/core/workflow/state.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,9 @@ def get_output_file_path(self, file_name: str, prefix: str | bool = None) -> Pat
11421142
file_name = f"{prefix}-{file_name}"
11431143
return self.filesystem.get_output_dir().joinpath(file_name)
11441144

1145-
def get_log_file_path(self, file_name: str, prefix: bool = True) -> Path:
1145+
def get_log_file_path(
1146+
self, file_name: str, prefix: bool = True, timestamped: bool = False
1147+
) -> Path:
11461148
"""
11471149
Get the log file path for this process.
11481150
@@ -1159,6 +1161,10 @@ def get_log_file_path(self, file_name: str, prefix: bool = True) -> Path:
11591161
value of the prefix id drawn from the "log_file_prefix" key within
11601162
this state. If that key is not set, no prefix is added regardless
11611163
of the value of this argument.
1164+
timestamped : bool, default False
1165+
Whether to add a timestamp to the log file name. If True, a
1166+
timestamp is added as a directory prefix to the log file name,
1167+
and the directory is created if it does not already exist.
11621168
11631169
Returns
11641170
-------
@@ -1167,6 +1173,19 @@ def get_log_file_path(self, file_name: str, prefix: bool = True) -> Path:
11671173
prefix = prefix and self.get_injectable("log_file_prefix", None)
11681174
if prefix:
11691175
file_name = f"{prefix}-{file_name}"
1176+
if timestamped:
1177+
from datetime import datetime
1178+
1179+
timestamp = self.get("run_timestamp", None)
1180+
if timestamp is None:
1181+
# if no run timestamp, use current time, and store it so
1182+
# it can be used later in the same run
1183+
timestamp = datetime.now().strftime("%Y%m%d-%H%M%S")
1184+
self.set("run_timestamp", timestamp)
1185+
self.filesystem.get_log_file_path(timestamp).mkdir(
1186+
parents=True, exist_ok=True
1187+
)
1188+
file_name = os.path.join(timestamp, file_name)
11701189
return self.filesystem.get_log_file_path(file_name)
11711190

11721191
def set_step_args(self, args=None):

0 commit comments

Comments
 (0)