Skip to content

Commit

Permalink
Add fill_new_category() to LeverNode
Browse files Browse the repository at this point in the history
  • Loading branch information
jtuomist committed Mar 4, 2025
1 parent 9d3a782 commit 8638d50
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 18 deletions.
4 changes: 2 additions & 2 deletions configs/dinspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ supported_languages: [de]
site_url: https://dinspec.paths.kausal.tech
dataset_repo:
url: https://github.com/kausaltech/dvctest.git
commit: c27477d6486bcecbd9053abb360d1e00f2b7d431
commit: 4a4cd140f9022c9ab5ac0e75307945e22aa832e3
dvc_remote: kausal-s3
name: Greenhouse Gas Inventory according to DINSpec 91637
owner: Kausal
Expand All @@ -15,7 +15,7 @@ minimum_historical_year: 2020
maximum_historical_year: 2023
emission_unit: kt/a
emission_forecast_from: 2024
emission_dimensions: [causal_step, sector]
emission_dimensions: [transport_mode, causal_step, sector]
features:
baseline_visible_in_graphs: true
show_significant_digits: 3
Expand Down
43 changes: 28 additions & 15 deletions configs/modules/dinspec/car_to_bike_shift.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ actions:
unit: dimensionless
color: '#2ca25f' # FIXME Does not change color
group: lever
input_dimensions: [causal_step, sector]
output_dimensions: [causal_step, sector]
input_dimensions: [causal_step, sector, transport_mode]
output_dimensions: [causal_step, sector, transport_mode]
input_datasets:
- id: gpc/dinspec
filters:
Expand All @@ -51,6 +51,7 @@ actions:
- id: sector
- id: causal_step
categories: [activity_shift]
- id: transport_mode

# Actions

Expand Down Expand Up @@ -508,12 +509,19 @@ nodes:
name_en: Shift from cars to bikes
type: simple.LeverNode
quantity: emissions # TODO Use fraction after fixing colors
unit: dimensionless
unit: '%'
color: '#ccebc5'
input_dimensions: [sector, causal_step]
output_dimensions: [sector, causal_step]
input_dimensions: [transport_mode, sector, causal_step]
output_dimensions: [transport_mode, sector, causal_step]
input_nodes:
- id: fraction_of_car_trips
to_dimensions:
- id: sector
- id: causal_step
- id: transport_mode
categories: [cars]
params:
new_category: transport_mode:bicycles

# Effect

Expand All @@ -524,8 +532,8 @@ nodes:
quantity: emissions
unit: kt/a
color: '#decbe4'
input_dimensions: [causal_step, sector]
output_dimensions: [causal_step, sector]
input_dimensions: [transport_mode, sector, causal_step]
output_dimensions: [transport_mode, sector, causal_step]
input_nodes:
- id: shift_from_cars_to_bikes
from_dimensions:
Expand All @@ -537,6 +545,7 @@ nodes:
- id: sector
- id: causal_step
categories: [effect]
- id: transport_mode
input_datasets:
- id: gpc/dinspec
filters:
Expand All @@ -552,8 +561,8 @@ nodes:
quantity: emissions
unit: t/a
color: '#decbe4'
input_dimensions: [causal_step, sector]
output_dimensions: [causal_step, sector]
input_dimensions: [transport_mode, sector, causal_step]
output_dimensions: [transport_mode, sector, causal_step]
input_nodes:
- id: shift_from_cars_to_bikes
from_dimensions:
Expand All @@ -565,6 +574,7 @@ nodes:
- id: sector
- id: causal_step
categories: [effect]
- id: transport_mode
input_datasets:
- id: gpc/dinspec
filters:
Expand All @@ -578,8 +588,8 @@ nodes:
quantity: currency
unit: MEUR/a
color: '#decbe4'
input_dimensions: [causal_step, sector]
output_dimensions: [causal_step, sector]
input_dimensions: [transport_mode, sector, causal_step]
output_dimensions: [transport_mode, sector, causal_step]
input_nodes:
- id: shift_from_cars_to_bikes
from_dimensions:
Expand All @@ -591,6 +601,7 @@ nodes:
- id: sector
- id: causal_step
categories: [effect]
- id: transport_mode
input_datasets:
- id: gpc/dinspec
filters:
Expand All @@ -604,8 +615,8 @@ nodes:
quantity: emissions
unit: t/a
color: '#decbe4'
input_dimensions: [causal_step, sector]
output_dimensions: [causal_step, sector]
input_dimensions: [transport_mode, sector, causal_step]
output_dimensions: [transport_mode, sector, causal_step]
input_nodes:
- id: shift_from_cars_to_bikes
from_dimensions:
Expand All @@ -617,6 +628,7 @@ nodes:
- id: sector
- id: causal_step
categories: [effect]
- id: transport_mode
input_datasets:
- id: gpc/dinspec
filters:
Expand All @@ -630,8 +642,8 @@ nodes:
quantity: emissions # TODO Use exposure after fixing colors
unit: Lden
color: '#decbe4'
input_dimensions: [causal_step, sector]
output_dimensions: [causal_step, sector]
input_dimensions: [transport_mode, sector, causal_step]
output_dimensions: [transport_mode, sector, causal_step]
input_nodes:
- id: shift_from_cars_to_bikes
from_dimensions:
Expand All @@ -643,6 +655,7 @@ nodes:
- id: sector
- id: causal_step
categories: [effect]
- id: transport_mode
input_datasets:
- id: gpc/dinspec
filters:
Expand Down
28 changes: 27 additions & 1 deletion nodes/simple.py
Original file line number Diff line number Diff line change
Expand Up @@ -986,11 +986,17 @@ class LeverNode(GenericNode):
explanation = _(
"""LeverNode replaces the upstream computation completely, if the lever is enabled."""
)
allowed_parameters = [
*GenericNode.allowed_parameters,
StringParameter(local_id='new_category'),
]

def compute(self) -> ppl.PathsDataFrame:
df = super().compute()
lever = self.get_input_node(tag='other_node', required=True)
return self.override_with_lever(df, lever)
df = self.override_with_lever(df, lever)
df = self.fill_new_category(df)
return df

def override_with_lever(self, df: ppl.PathsDataFrame, lever: Node) -> ppl.PathsDataFrame:
if not isinstance(lever, ActionNode):
Expand All @@ -1010,6 +1016,26 @@ def override_with_lever(self, df: ppl.PathsDataFrame, lever: Node) -> ppl.PathsD
raise NodeError(self, f"Lever {lever.id} must result in the same structure {s}")
return out

def fill_new_category(self, df: ppl.PathsDataFrame) -> ppl.PathsDataFrame:
category = self.get_parameter_value_str('new_category', required=True)
dim, cat = category.split(':')

df = df.ensure_unit(VALUE_COLUMN, 'dimensionless')

df2 = df.paths.sum_over_dims(dim)
df2 = df2.with_columns((pl.lit(1.0) - pl.col(VALUE_COLUMN)).alias(VALUE_COLUMN))
df2 = df2.with_columns(pl.lit(cat).cast(pl.Categorical).alias(dim))
df2 = df2.select(df.columns)

df = df.paths.concat_vertical(df2)
df = df.ensure_unit(VALUE_COLUMN, self.unit)
if self.get_parameter_value('drop_nans', required=False): # FIXME Not consistent with the parameter name!
df = df.paths.to_wide()
for col in df.metric_cols:
df = df.filter(~pl.col(col).is_null())
df = df.paths.to_narrow()
return df


class WeightedSumNode(GenericNode):
explanation = _(
Expand Down

0 comments on commit 8638d50

Please sign in to comment.