Skip to content

Commit bdf51ec

Browse files
committed
pass income percentiles in tests
1 parent 6372878 commit bdf51ec

2 files changed

Lines changed: 26 additions & 24 deletions

File tree

ogcore/demographics.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -932,27 +932,13 @@ def expand_pop_obj_J(
932932
dict: demographic objects with the same keys needed by get_pop_objs.
933933
"""
934934
omega_SS = omega_SSfx[-S:] / omega_SSfx[-S:].sum()
935-
base_objs = {
936-
"omega_path_S": omega_path_S,
937-
"omega_SS": omega_SS,
938-
"mort_rates_S": mort_rates_S,
939-
"imm_rates_mat": imm_rates_mat,
940-
}
941-
942935
income_inputs = (
943936
fert_gradient,
944937
mort_gradient,
945938
infmort_gradient,
946939
imm_pctiles,
947940
)
948-
if income_percentiles is None:
949-
if any(x is not None for x in income_inputs):
950-
raise ValueError(
951-
"income_percentiles must be provided when using income-specific "
952-
"fertility, mortality, infant mortality, or immigration inputs."
953-
)
954-
return base_objs
955-
941+
assert income_percentiles is not None, "income_percentiles must be provided when using income-specific inputs."
956942
income_shares, pct_midpoints = _income_shares_and_midpoints(
957943
income_percentiles
958944
)
@@ -961,13 +947,14 @@ def expand_pop_obj_J(
961947
assert totpers == E + S
962948

963949
all_income_inputs_none = all(x is None for x in income_inputs)
950+
964951
if all_income_inputs_none:
952+
print("In the all are none case.")
965953
return {
966-
"omega_path_S": omega_path_S[:, :, None]
967-
* income_shares.reshape(1, 1, J),
968-
"omega_SS": omega_SS[:, None] * income_shares.reshape(1, J),
969-
"mort_rates_S": np.tile(mort_rates_S[:, :, None], (1, 1, J)),
970-
"imm_rates_mat": np.tile(imm_rates_mat[:, :, None], (1, 1, J)),
954+
"omega_path_S": omega_path_S.reshape(omega_path_S.shape[0], omega_path_S.shape[1], 1) * income_shares.reshape(1, 1, J),
955+
"omega_SS": omega_SS.reshape(omega_SS.shape[0], 1) * income_shares.reshape(1, J),
956+
"mort_rates_S": np.tile(mort_rates_S.reshape(mort_rates_S.shape[0], mort_rates_S.shape[1], 1), (1, 1, J)),
957+
"imm_rates_mat": np.tile(imm_rates_mat.reshape(imm_rates_mat.shape[0], imm_rates_mat.shape[1], 1), (1, 1, J)),
971958
}
972959

973960
fert_slopes = _format_age_gradient(

tests/test_demographics.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def test_get_pop_objs_read_UN_data():
4242
T,
4343
0,
4444
99,
45+
income_percentiles=[0.25, 0.25, 0.2, 0.2, 0.05, 0.04, 0.01],
4546
initial_data_year=start_year - 1,
4647
final_data_year=start_year,
4748
GraphDiag=False,
@@ -71,13 +72,17 @@ def test_get_pop_objs():
7172
infmort_rates=infmort_rates,
7273
imm_rates=imm_rates,
7374
infer_pop=True,
75+
income_percentiles=[0.25, 0.25, 0.2, 0.2, 0.05, 0.04, 0.01],
7476
pop_dist=pop_dist[0, :].reshape(1, E + S),
7577
initial_data_year=start_year - 1,
7678
final_data_year=start_year,
7779
GraphDiag=False,
7880
)
79-
80-
assert np.allclose(pop_dict["omega_SS"], pop_dict["omega"][-1, :])
81+
print("Omega_SS shape:", pop_dict["omega_SS"].shape)
82+
print("Omega last period shape:", pop_dict["omega"][-1, :, :].shape)
83+
print("Some values from Omega_SS:", pop_dict["omega_SS"][:5, :5])
84+
print("Some values from Omega last period:", pop_dict["omega"][-1, :5, :5])
85+
assert np.allclose(pop_dict["omega_SS"], pop_dict["omega"][-1, :, :])
8186

8287

8388
def test_pop_smooth():
@@ -100,6 +105,7 @@ def test_pop_smooth():
100105
infmort_rates=infmort_rates,
101106
imm_rates=imm_rates,
102107
infer_pop=True,
108+
income_percentiles=[0.25, 0.25, 0.2, 0.2, 0.05, 0.04, 0.01],
103109
pop_dist=pop_dist[0, :].reshape(1, E + S),
104110
initial_data_year=start_year - 1,
105111
final_data_year=start_year,
@@ -147,6 +153,7 @@ def test_pop_growth_smooth():
147153
infmort_rates=infmort_rates,
148154
imm_rates=imm_rates,
149155
infer_pop=True,
156+
income_percentiles=[0.25, 0.25, 0.2, 0.2, 0.05, 0.04, 0.01],
150157
pop_dist=pop_dist[0, :].reshape(1, E + S),
151158
initial_data_year=start_year - 1,
152159
final_data_year=start_year,
@@ -187,6 +194,7 @@ def test_imm_smooth():
187194
99,
188195
initial_data_year=start_year - 1,
189196
final_data_year=start_year,
197+
income_percentiles=[0.25, 0.25, 0.2, 0.2, 0.05, 0.04, 0.01],
190198
GraphDiag=False,
191199
)
192200
# assert diffs are small
@@ -338,13 +346,14 @@ def test_custom_series():
338346
T,
339347
0,
340348
99,
349+
income_percentiles=[0.25, 0.25, 0.2, 0.2, 0.05, 0.04, 0.01],
341350
initial_data_year=start_year,
342351
final_data_year=start_year + 1,
343352
GraphDiag=False,
344353
imm_rates=imm_rates,
345354
infer_pop=True,
346355
)
347-
assert np.allclose(pop_dict["imm_rates"][0, :], imm_rates[0, E:])
356+
assert np.allclose(pop_dict["imm_rates"][0, :, 0], imm_rates[0, E:])
348357

349358

350359
def test_custom_series_fail():
@@ -398,6 +407,7 @@ def test_custom_series_fail():
398407
mort_rates=mort_rates,
399408
infmort_rates=infmort_rates,
400409
imm_rates=imm_rates,
410+
income_percentiles=[0.25, 0.25, 0.2, 0.2, 0.05, 0.04, 0.01],
401411
pop_dist=pop_dist,
402412
initial_data_year=start_year,
403413
final_data_year=start_year + 1,
@@ -428,6 +438,7 @@ def test_SS_dist():
428438
imm_rates=imm_rates,
429439
infer_pop=True,
430440
pop_dist=pop_dist[0, :].reshape(1, E + S),
441+
income_percentiles=[0.25, 0.25, 0.2, 0.2, 0.05, 0.04, 0.01],
431442
initial_data_year=start_year - 1,
432443
final_data_year=start_year,
433444
GraphDiag=False,
@@ -459,6 +470,7 @@ def test_time_path_length():
459470
infmort_rates=infmort_rates,
460471
imm_rates=imm_rates,
461472
infer_pop=True,
473+
income_percentiles=[0.25, 0.25, 0.2, 0.2, 0.05, 0.04, 0.01],
462474
pop_dist=pop_dist[0, :].reshape(1, E + S),
463475
initial_data_year=start_year - 1,
464476
final_data_year=start_year,
@@ -521,6 +533,7 @@ def test_infer_pop_nones():
521533
mort_rates=mort_rates,
522534
infmort_rates=infmort_rates,
523535
imm_rates=imm_rates,
536+
income_percentiles=[0.25, 0.25, 0.2, 0.2, 0.05, 0.04, 0.01],
524537
infer_pop=True,
525538
pop_dist=None,
526539
initial_data_year=start_year,
@@ -548,12 +561,13 @@ def test_data_download(tmpdir):
548561
T,
549562
0,
550563
99,
564+
income_percentiles=[0.25, 0.25, 0.2, 0.2, 0.05, 0.04, 0.01],
551565
initial_data_year=start_year,
552566
final_data_year=start_year + 1,
553567
download_path=tmpdir,
554568
)
555569

556-
# No read in each file and call get_pop_objs again with the data
570+
# Now read in each file and call get_pop_objs again with the data
557571
fert_rates = np.loadtxt(
558572
os.path.join(tmpdir, "fert_rates.csv"), delimiter=","
559573
)
@@ -580,6 +594,7 @@ def test_data_download(tmpdir):
580594
infmort_rates=infmort_rates[:],
581595
imm_rates=imm_rates[:, :],
582596
infer_pop=True,
597+
income_percentiles=[0.25, 0.25, 0.2, 0.2, 0.05, 0.04, 0.01],
583598
pop_dist=pop_dist[0, :].reshape(1, E + S),
584599
initial_data_year=start_year,
585600
final_data_year=start_year + 2,

0 commit comments

Comments
 (0)