From 6326528940ad0c6ed99605f4ff163b9d44eaf191 Mon Sep 17 00:00:00 2001 From: Sijia Wang Date: Thu, 2 Apr 2026 00:19:11 -0400 Subject: [PATCH 1/2] remove `sample_rate` rounding --- activitysim/abm/tables/households.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/activitysim/abm/tables/households.py b/activitysim/abm/tables/households.py index 80972ff74a..ba3d69d859 100644 --- a/activitysim/abm/tables/households.py +++ b/activitysim/abm/tables/households.py @@ -99,10 +99,7 @@ def households(state: workflow.State) -> pd.DataFrame: if households_sample_size == 0: sample_rate = 1 else: - # TODO: do not round, keep full precision to avoid creating 0 sample_rate for small samples - # Existing CI tests will fail when performing bitwise comparisons with the unrounded sample_rate - # We should update the CI tests - sample_rate = round(households_sample_size / tot_households, 3) + sample_rate = households_sample_size / tot_households df["sample_rate"] = sample_rate From 1c6b2e5a6f00d9aa5aafaba003703b60ddc593f8 Mon Sep 17 00:00:00 2001 From: Sijia Wang Date: Thu, 30 Apr 2026 18:17:04 -0400 Subject: [PATCH 2/2] create a unit test for sample rate calculation --- .../abm/test/test_misc/test_sample_rate.py | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 activitysim/abm/test/test_misc/test_sample_rate.py diff --git a/activitysim/abm/test/test_misc/test_sample_rate.py b/activitysim/abm/test/test_misc/test_sample_rate.py new file mode 100644 index 0000000000..8f4f3e8fbc --- /dev/null +++ b/activitysim/abm/test/test_misc/test_sample_rate.py @@ -0,0 +1,55 @@ +# ActivitySim +# See full license in LICENSE.txt. +import pandas as pd + +import pytest + +from activitysim.core import workflow + + +@pytest.fixture(scope="session") +def example_root(tmp_path_factory): + root = tmp_path_factory.mktemp("example") + config_dir = root / "configs" + config_dir.mkdir() + + data_dir = root / "data" + data_dir.mkdir() + + return root + + +@pytest.fixture(scope="module") +def state(example_root) -> workflow.State: + + settings = """ + input_table_list: + - tablename: households + filename: households.csv + index_col: household_id + + households_sample_size: 2 + """ + + settings_file = example_root / "configs" / "settings.yaml" + settings_file.write_text(settings) + + households = pd.DataFrame( + { + "household_id": range(1, 100001), + "home_zone_id": [1, 2] * 50000, + } + ) + households.to_csv(example_root / "data" / "households.csv", index=False) + + state = workflow.State.make_default(example_root) + + return state + + +def test_sample_rate_calculation(state): + households_df = state.get_dataframe("households") + sample_rate = households_df["sample_rate"].iloc[0] + assert ( + sample_rate == 0.00002 + ), f"Expected sample rate of 0.00002, but got {sample_rate}"