Skip to content

Commit

Permalink
Test furness/no furness in jacobian
Browse files Browse the repository at this point in the history
  • Loading branch information
isaac-tfn committed Dec 12, 2023
1 parent d3b3cb3 commit 3278725
Showing 1 changed file with 45 additions and 6 deletions.
51 changes: 45 additions & 6 deletions tests/gravity_model/test_multi_area.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ def fixture_dists(data_dir, distributions):
)
return sorted

@pytest.fixture(name="multi_conf", scope="session")
@pytest.fixture(name="no_furness_jac_conf", scope="session")
def fixture_conf(data_dir, mock_dir):
conf = gm.MultiDistInput(TLDFile=data_dir / 'distributions.csv',
TldLookupFile=data_dir / 'distributions_lookup.csv',
Expand All @@ -91,20 +91,55 @@ def fixture_conf(data_dir, mock_dir):
init_params={'mu': 1, 'sigma': 2},
log_path=mock_dir / 'log.csv',
furness_tolerance=0.1,
furness_jac=False
)
return conf

@pytest.fixture(name="furness_jac_conf", scope="session")
def fixture_jac_furn(data_dir, mock_dir):
conf = gm.MultiDistInput(TLDFile=data_dir / 'distributions.csv',
TldLookupFile=data_dir / 'distributions_lookup.csv',
cat_col='cat',
min_col='lower',
max_col='upper',
ave_col='avg',
trips_col='trips',
lookup_cat_col='cat',
lookup_zone_col='zone',
init_params={'mu': 1, 'sigma': 2},
log_path=mock_dir / 'log.csv',
furness_tolerance=0.1,
furness_jac=True,
)
return conf


@pytest.fixture(name="cal_no_furness", scope="session")
def fixture_cal_no_furness(data_dir, infilled, no_furness_jac_conf, trip_ends, mock_dir):
row_targets = trip_ends["origin"].values
col_targets = trip_ends["destination"].values
model = gm.MultiAreaGravityModelCalibrator(
row_targets=row_targets,
col_targets=col_targets,
cost_matrix=infilled,
cost_function=cost_functions.BuiltInCostFunction.LOG_NORMAL.get_cost_function(),
params=no_furness_jac_conf
)
results = model.calibrate(
running_log_path=mock_dir / "temp_log.csv"
)
return results

@pytest.fixture(name="cal_results", scope="session")
def fixture_cal_res(data_dir, infilled, multi_conf, trip_ends, mock_dir):
@pytest.fixture(name="cal_furness", scope="session")
def fixture_cal_furness(data_dir, infilled, furness_jac_conf, trip_ends, mock_dir):
row_targets = trip_ends["origin"].values
col_targets = trip_ends["destination"].values
model = gm.MultiAreaGravityModelCalibrator(
row_targets=row_targets,
col_targets=col_targets,
cost_matrix=infilled,
cost_function=cost_functions.BuiltInCostFunction.LOG_NORMAL.get_cost_function(),
params=multi_conf
params=furness_jac_conf
)
results = model.calibrate(
running_log_path=mock_dir / "temp_log.csv"
Expand All @@ -125,12 +160,16 @@ def test_tld_processing_type(self, distributions_sorted):

class TestDist:
@pytest.mark.parametrize("area", ['City', 'Town', 'External', 'Village'])
def test_convergence(self, cal_results, area):
@pytest.mark.parametrize("cal_results", ['cal_furness', 'cal_no_furness'])
def test_convergence(self, cal_results, area, request):
cal_results = request.getfixturevalue(cal_results)
dist = cal_results[area]
assert dist.cost_convergence > 0.85

@pytest.mark.parametrize("area", ['City', 'Town', 'External', 'Village'])
def test_params(self, cal_results, area):
@pytest.mark.parametrize("cal_results", ['cal_furness', 'cal_no_furness'])
def test_params(self, cal_results, area, request):
cal_results=request.getfixturevalue(cal_results)
dist = cal_results[area]
mu = dist.cost_params['mu']
sigma = dist.cost_params['sigma']
Expand Down

0 comments on commit 3278725

Please sign in to comment.