Skip to content

Commit

Permalink
Possibly working EventArbitraryProd (remains to be tested)
Browse files Browse the repository at this point in the history
  • Loading branch information
spjuhel committed Jul 12, 2024
1 parent caf457c commit 9ef415c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 9 deletions.
12 changes: 10 additions & 2 deletions boario/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,14 @@ def from_series(
recovery_function=recovery_function,
)
elif event_type == "arbitrary":
raise NotImplementedError("This type of event is not implemented yet.")
return EventArbitraryProd._from_series(
impact=impact,
occurrence=occurrence,
duration=duration,
name=name,
recovery_tau=recovery_tau,
recovery_function=recovery_function,
)

else:
raise ValueError(f"Wrong event type: {event_type}")
Expand Down Expand Up @@ -530,7 +537,7 @@ def _from_scalar_regions_sectors(
**kwargs,
) -> Event:
affected_industries = cls._build_industries_idx(
affected_regions, affected_sectors
regions=affected_regions, sectors=affected_sectors
)
regional_distrib = cls._level_distrib(
affected_industries.levels[0], impact_regional_distrib
Expand Down Expand Up @@ -615,6 +622,7 @@ def from_scalar_regions_sectors(
if impact <= 0:
raise ValueError("Impact is null.")

@classmethod
def _build_industries_idx(cls, regions: RegionsList, sectors: SectorsList):
# TODO: Move this in utils?
if isinstance(regions, str):
Expand Down
14 changes: 10 additions & 4 deletions boario/simulation.py
Original file line number Diff line number Diff line change
Expand Up @@ -1361,6 +1361,12 @@ def __init__(
source_event.impact.copy(), self.sim.model.industries
)
self._indus_dmg = self._indus_dmg_0.copy()
if source_event.impact_households is not None:
self._house_dmg_0 = _thin_to_wide(
source_event.impact_households.copy(), self.sim.model.regions
)
self._house_dmg = self._house_dmg_0.copy()

if isinstance(source_event, EventKapitalRebuild):
self._init_distrib("indus", indus_rebuild_distribution, source_event)
assert self._indus_dmg_0 is not None
Expand All @@ -1379,10 +1385,6 @@ def __init__(
self.rebuild_dem_indus_distribution = self._reb_dem_indus_distribution

if source_event.impact_households is not None:
self._house_dmg_0 = _thin_to_wide(
source_event.impact_households.copy(), self.sim.model.regions
)
self._house_dmg = self._house_dmg_0.copy()
self._init_distrib("house", house_rebuild_distribution, source_event)
self._rebuild_demand_house_0 = pd.DataFrame(
source_event.rebuilding_sectors.values[:, None]
Expand Down Expand Up @@ -1411,6 +1413,10 @@ def __init__(
)

if isinstance(self.event, EventArbitraryProd):
self._prod_delta_from_arb_0 = _thin_to_wide(
source_event.impact.copy(), self.sim.model.industries
)
self._prod_delta_from_arb = self._prod_delta_from_arb_0.copy()
self._recovery_function_arb_delta = partial(
self.event.recovery_function,
init_impact_stock=self._prod_delta_from_arb_0,
Expand Down
6 changes: 3 additions & 3 deletions tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ def test_event_from_series_rebuilding(self, sample_series):
)

def test_event_from_series_arbitrary(self, sample_series):
with pytest.raises(NotImplementedError):
with pytest.raises(ValueError):
event.from_series(sample_series, event_type="arbitrary")

def test_event_from_series_wrongtype(self, sample_series):
Expand Down Expand Up @@ -626,7 +626,7 @@ def r_fun2(init_impact_stock, recovery_time):
)
def test_event_from_series_arbitrary(self, impact, expected):
if isinstance(expected, str) and expected == "error":
with pytest.raises(ValueError):
with pytest.raises((ValueError, AttributeError)):
event.from_series(
impact,
event_type="arbitrary",
Expand All @@ -639,5 +639,5 @@ def test_event_from_series_arbitrary(self, impact, expected):
recovery_tau=1,
)
pd.testing.assert_series_equal(
event_instance.prod_cap_delta_arbitrary, expected
event_instance.prod_cap_delta_arbitrary, expected, check_names=False
)

0 comments on commit 9ef415c

Please sign in to comment.