Skip to content

Commit

Permalink
Properly handle the case of no losses when aggregating.
Browse files Browse the repository at this point in the history
Loss model: make `aggregate_losses` return a DataFrame with zeros.
  • Loading branch information
ioannis-vm committed May 18, 2024
1 parent b50c3d5 commit 9bbe1e3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
19 changes: 10 additions & 9 deletions pelicun/model/loss_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -537,25 +537,26 @@ def aggregate_losses(self):
for model in self._loss_models
if model.sample is not None
]
if not samples:
self.log.msg("There are no losses.")
return None

sample = pd.concat(samples, axis=1)

# group results by DV type and location
aggregated = sample.groupby(level=['dv', 'loc'], axis=1).sum()

# Note: The `Time` DV receives special treatment.

# create the summary DF
columns = [
f'repair_{x.lower()}' for x in self.decision_variables if x != 'Time'
]
if 'Time' in self.decision_variables:
columns.extend(('repair_time-sequential', 'repair_time-parallel'))

if not samples:
self.log.msg("There are no losses.")
df_agg = pd.DataFrame(0.00, index=[0], columns=columns)
return df_agg

sample = pd.concat(samples, axis=1)
df_agg = pd.DataFrame(index=sample.index, columns=columns)

# group results by DV type and location
aggregated = sample.groupby(level=['dv', 'loc'], axis=1).sum()

for decision_variable in self.decision_variables:

# Time ..
Expand Down
19 changes: 19 additions & 0 deletions pelicun/tests/model/test_loss_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,25 @@ def test__ensure_loss_parameter_availability(self, assessment_instance):
"[('consequence_E', 'DecisionVariableXYZ')]"
) in str(record[0].message)

def test_aggregate_losses_when_no_loss(self, assessment_instance):

# tests that aggregate losses works when there is no loss.
loss_model = LossModel(assessment_instance)
df_agg = loss_model.aggregate_losses()
pd.testing.assert_frame_equal(
df_agg,
pd.DataFrame(
{
'repair_carbon': 0.0,
'repair_cost': 0.00,
'repair_energy': 0.00,
'repair_time-sequential': 0.00,
'repair_time-parallel': 0.00,
},
index=[0],
),
)


class TestRepairModel_Base(TestPelicunModel):
def test___init__(self, assessment_instance):
Expand Down

0 comments on commit 9bbe1e3

Please sign in to comment.