Skip to content

Commit db91bb1

Browse files
Reduce code duplication
1 parent dc94648 commit db91bb1

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

policyengine/simulation.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,8 @@ def _initialise_simulations(self):
289289
situation=self.data if not macro else None,
290290
reform=self._parsed_baseline,
291291
)
292+
if self.time_period is not None:
293+
self.baseline_sim.default_calculation_period = self.time_period
292294

293295
if "region" in self.options and isinstance(
294296
self.baseline_sim, CountryMicrosimulation
@@ -299,8 +301,6 @@ def _initialise_simulations(self):
299301
self.options["region"],
300302
reform=self.baseline_sim.reform,
301303
)
302-
if self.time_period is not None:
303-
self.baseline_sim.default_calculation_period = self.time_period
304304

305305
if "subsample" in self.options:
306306
self.baseline_sim = self.baseline_sim.subsample(
@@ -315,6 +315,9 @@ def _initialise_simulations(self):
315315
situation=self.data if not macro else None,
316316
reform=self._parsed_reform,
317317
)
318+
319+
if self.time_period is not None:
320+
self.reformed_sim.default_calculation_period = self.time_period
318321
if "region" in self.options and isinstance(
319322
self.reformed_sim, CountryMicrosimulation
320323
):
@@ -330,9 +333,6 @@ def _initialise_simulations(self):
330333
self.options["subsample"]
331334
)
332335

333-
if self.time_period is not None:
334-
self.reformed_sim.default_calculation_period = self.time_period
335-
336336
# Set the 'baseline tax-benefit system' to be the actual baseline. For example, when working out an individual's
337337
# baseline MTR, it should use the actual policy baseline, not always current law.
338338

@@ -404,6 +404,11 @@ def _apply_region_to_simulation(
404404
with h5py.File(weights_file_path, "r") as f:
405405
weights = f[str(self.time_period)][...]
406406

407+
print(
408+
weights[constituency_id],
409+
simulation.default_calculation_period,
410+
)
411+
407412
simulation.calculate("household_net_income")
408413

409414
simulation.set_input(
@@ -419,7 +424,7 @@ def _apply_region_to_simulation(
419424
local_folder=None,
420425
version=None,
421426
)
422-
la_names_file_path = Path(constituency_names_file_path)
427+
la_names_file_path = Path(la_names_file_path)
423428
la_names = pd.read_csv(la_names_file_path)
424429
if la in la_names.code.values:
425430
la_id = la_names[la_names.code == la].index[0]

policyengine/utils/maps.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,36 @@
77
from policyengine.utils.charts import *
88

99

10-
def plot_hex_map(value_by_area_name: dict, location_type: str) -> dict:
10+
def get_location_options_table(location_type: str) -> pd.DataFrame:
1111
if location_type == "parliamentary_constituencies":
1212
area_names_file_path = download(
1313
repo="policyengine/policyengine-uk-data",
1414
repo_filename="constituencies_2024.csv",
1515
local_folder=None,
1616
version=None,
1717
)
18-
x_bounds = [30, 85]
19-
y_bounds = [-50, 2]
2018
elif location_type == "local_authorities":
2119
area_names_file_path = download(
2220
repo="policyengine/policyengine-uk-data",
2321
repo_filename="local_authorities_2021.csv",
2422
local_folder=None,
2523
version=None,
2624
)
25+
df = pd.read_csv(area_names_file_path)
26+
return df
27+
28+
29+
def plot_hex_map(value_by_area_name: dict, location_type: str) -> dict:
30+
if location_type == "parliamentary_constituencies":
31+
x_bounds = [30, 85]
32+
y_bounds = [-50, 2]
33+
elif location_type == "local_authorities":
2734
x_bounds = [-20, 25]
2835
y_bounds = [-10, 35]
2936
else:
3037
raise ValueError("Invalid location_type: " + location_type)
31-
area_names = pd.read_csv(area_names_file_path)
38+
39+
area_names = get_location_options_table(location_type)
3240

3341
x_range = area_names["x"].max() - area_names["x"].min()
3442
y_range = area_names["y"].max() - area_names["y"].min()

0 commit comments

Comments
 (0)