Skip to content

Commit bf80d5a

Browse files
authored
Remove Sample Rate Rounding (#1073)
* remove `sample_rate` rounding * create a unit test for sample rate calculation * apply the modified copilot review changes
1 parent 279e4f9 commit bf80d5a

2 files changed

Lines changed: 56 additions & 4 deletions

File tree

‎activitysim/abm/tables/households.py‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,10 +99,7 @@ def households(state: workflow.State) -> pd.DataFrame:
9999
if households_sample_size == 0:
100100
sample_rate = 1
101101
else:
102-
# TODO: do not round, keep full precision to avoid creating 0 sample_rate for small samples
103-
# Existing CI tests will fail when performing bitwise comparisons with the unrounded sample_rate
104-
# We should update the CI tests
105-
sample_rate = round(households_sample_size / tot_households, 3)
102+
sample_rate = households_sample_size / tot_households
106103

107104
df["sample_rate"] = sample_rate
108105

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# ActivitySim
2+
# See full license in LICENSE.txt.
3+
import pandas as pd
4+
5+
import pytest
6+
7+
from activitysim.core import workflow
8+
9+
10+
@pytest.fixture(scope="session")
11+
def example_root(tmp_path_factory):
12+
root = tmp_path_factory.mktemp("example")
13+
config_dir = root / "configs"
14+
config_dir.mkdir()
15+
16+
data_dir = root / "data"
17+
data_dir.mkdir()
18+
19+
return root
20+
21+
22+
@pytest.fixture(scope="module")
23+
def state(example_root) -> workflow.State:
24+
25+
settings = """
26+
input_table_list:
27+
- tablename: households
28+
filename: households.csv
29+
index_col: household_id
30+
31+
households_sample_size: 2
32+
"""
33+
34+
settings_file = example_root / "configs" / "settings.yaml"
35+
settings_file.write_text(settings)
36+
37+
households = pd.DataFrame(
38+
{
39+
"household_id": range(1, 4002),
40+
"home_zone_id": [1, 2] * 2000 + [1],
41+
}
42+
)
43+
households.to_csv(example_root / "data" / "households.csv", index=False)
44+
45+
state = workflow.State.make_default(example_root)
46+
47+
return state
48+
49+
50+
def test_sample_rate_calculation(state):
51+
households_df = state.get_dataframe("households")
52+
sample_rate = households_df["sample_rate"].iloc[0]
53+
assert sample_rate == pytest.approx(
54+
0.0005, abs=1e-6
55+
), f"Expected sample rate of 0.0005, but got {sample_rate}"

0 commit comments

Comments
 (0)