Skip to content
1 change: 1 addition & 0 deletions .github/workflows/core_tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ jobs:

- run: uv run pytest test/test_skim_name_conflicts.py
- run: uv run pytest test/random_seed/test_random_seed.py
- run: uv run pytest test/trace_id/test_trace_id.py

builtin_regional_models:
needs: foundation
Expand Down
7 changes: 7 additions & 0 deletions activitysim/cli/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@
import sys
import warnings
from datetime import datetime
import struct
import time

import numpy as np

from activitysim.core import chunk, config, mem, timing, tracing, workflow
from activitysim.core.configuration import FileSystem, Settings
from activitysim.core.run_id import RunId

from activitysim.abm.models.settings_checker import check_model_settings

Expand All @@ -29,6 +32,7 @@
"settings_file_name",
"imported_extensions",
"run_timestamp",
"run_id",
]


Expand Down Expand Up @@ -161,6 +165,8 @@ def inject_arg(name, value):
# 'configs', 'data', and 'output' folders by default
os.chdir(args.working_dir)

inject_arg("run_id", state.tracing.run_id)

if args.ext:
for e in args.ext:
basepath, extpath = os.path.split(e)
Expand Down Expand Up @@ -268,6 +274,7 @@ def run(args):
"""

state = workflow.State()
_init_run_id = state.tracing.run_id

# register abm steps and other abm-specific injectables
# by default, assume we are running activitysim.abm
Expand Down
2 changes: 2 additions & 0 deletions activitysim/core/configuration/top.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

from pathlib import Path
from typing import Any, Literal
import struct
import time

from pydantic import model_validator, validator

Expand Down
5 changes: 5 additions & 0 deletions activitysim/core/mp_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

from activitysim.core import config, mem, tracing, util, workflow
from activitysim.core.configuration import FileSystem, Settings
from activitysim.core.run_id import RunId
from activitysim.core.workflow.checkpoint import (
CHECKPOINT_NAME,
CHECKPOINT_TABLE_NAME,
Expand Down Expand Up @@ -890,6 +891,10 @@ def setup_injectables_and_logging(injectables, locutor: bool = True) -> workflow
injects injectables
"""
state = workflow.State()
_run_id = injectables.get("run_id", None)
if _run_id:
state.tracing.run_id = RunId(_run_id)

state = state.initialize_filesystem(**injectables)
state.settings = injectables.get("settings", Settings())
state.filesystem.parse_settings(state.settings)
Expand Down
11 changes: 11 additions & 0 deletions activitysim/core/run_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import struct
import time


class RunId(str):
def __new__(cls, x=None):
if x is None:
return cls(
hex(struct.unpack("<Q", struct.pack("<d", time.time()))[0])[-6:].lower()
)
return super().__new__(cls, x)
12 changes: 1 addition & 11 deletions activitysim/core/workflow/tracing.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
import logging
import logging.config
import os
import struct
import sys
import tarfile
import tempfile
import time
from collections.abc import Mapping, MutableMapping, Sequence
from pathlib import Path
from typing import Any, Optional
Expand All @@ -22,6 +20,7 @@
from activitysim.core.test import assert_equal, assert_frame_substantively_equal
from activitysim.core.workflow.accessor import FromState, StateAccessor
from activitysim.core.exceptions import TableSlicingError
from activitysim.core.run_id import RunId

logger = logging.getLogger(__name__)

Expand All @@ -37,15 +36,6 @@
]


class RunId(str):
def __new__(cls, x=None):
if x is None:
return cls(
hex(struct.unpack("<Q", struct.pack("<d", time.time()))[0])[-6:].lower()
)
return super().__new__(cls, x)


class Tracing(StateAccessor):
"""
Methods to provide the tracing capabilities of ActivitySim.
Expand Down
2 changes: 2 additions & 0 deletions test/trace_id/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
configs*/
output/
16 changes: 16 additions & 0 deletions test/trace_id/simulation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# ActivitySim
# See full license in LICENSE.txt.

from __future__ import annotations

import argparse
import sys

from activitysim.cli.run import add_run_args, run

if __name__ == "__main__":
parser = argparse.ArgumentParser()
add_run_args(parser)
args = parser.parse_args()

sys.exit(run(args))
85 changes: 85 additions & 0 deletions test/trace_id/test_trace_id.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
from __future__ import annotations

# ActivitySim
# See full license in LICENSE.txt.
import importlib.resources
import os
import subprocess
from shutil import copytree

import pandas as pd
import pandas.testing as pdt
import yaml


def update_settings(settings_file, key, value):
with open(settings_file, "r") as f:
settings = yaml.safe_load(f)
f.close()

settings[key] = value

with open(settings_file, "w") as f:
yaml.safe_dump(settings, f)
f.close()


def test_trace_ids_have_same_hash():
def example_path(dirname):
resource = os.path.join("examples", "prototype_mtc", dirname)
return str(importlib.resources.files("activitysim").joinpath(resource))

def test_path(dirname):
return os.path.join(os.path.dirname(__file__), dirname)

new_configs_dir = test_path("configs")
new_mp_configs_dir = test_path("configs_mp")
new_settings_file = os.path.join(new_configs_dir, "settings.yaml")
copytree(example_path("configs"), new_configs_dir)
copytree(example_path("configs_mp"), new_mp_configs_dir)

update_settings(
new_settings_file, "trace_hh_id", 1932009
) # Household in the prototype_mtc example with 11 people

def check_csv_suffix(directory):
suffix = None
mismatched_files = []
for root, dirs, files in os.walk(directory):
for filename in files:
if filename.lower().endswith(".csv"):
file_suffix = filename[-10:]
if suffix is None:
suffix = file_suffix
elif file_suffix != suffix:
mismatched_files.append(os.path.join(root, filename))
if mismatched_files:
raise AssertionError(
f"CSV files with mismatched suffixes: {mismatched_files}"
)

file_path = os.path.join(os.path.dirname(__file__), "simulation.py")

run_args = [
"-c",
test_path("configs_mp"),
"-c",
test_path("configs"),
"-d",
example_path("data"),
"-o",
test_path("output"),
]

try:
os.mkdir(test_path("output"))
except FileExistsError:
pass

subprocess.run(["coverage", "run", "-a", file_path] + run_args, check=True)

check_csv_suffix(os.path.join(test_path("output"), "trace"))


if __name__ == "__main__":
test_trace_ids_have_same_hash()