|
| 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