Skip to content

Commit

Permalink
fix usage of multi groups
Browse files Browse the repository at this point in the history
  • Loading branch information
rakow committed Nov 30, 2023
1 parent 5f53f4b commit ed738d0
Showing 1 changed file with 16 additions and 5 deletions.
21 changes: 16 additions & 5 deletions matsim/calibration/calib_group_asc.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

from .base import CalibratorBase, CalibrationInput
from .analysis import read_trips_and_persons
from .utils import completed_trials


def get_sbb_params(config, group, value, mode):
Expand Down Expand Up @@ -88,7 +87,7 @@ def __init__(self,
super().__init__(modes, initial, target, lr, constraints)

self.fixed_mode = fixed_mode
self.multi_groups = multi_groups
self.multi_groups = set(multi_groups)
self.corr_correction = corr_correction
self.config_format = config_format

Expand Down Expand Up @@ -119,14 +118,26 @@ def __init__(self,
self.base = None
self.alternate_base = False

def get_group(self, df, groups: dict = None):
def get_group(self, df, groups: dict = None, use_multi=False):
""" Get data of one group"""
# return result for empty groups
if groups is None:
idx = reduce(lambda x, y: x & y, [pd.isna(df[g]) for g in self.groups])
return df[idx]

idx = reduce(lambda x, y: x & y, [df[g] == v for g, v in groups.items()])
if use_multi:
idx = []
for g, v in groups.items():
if g in self.multi_groups:
vs = v.split(",")
idx.append(df[g].isin(vs))
else:
idx.append(df[g] == v)

idx = reduce(lambda x, y: x & y, idx)
else:
idx = reduce(lambda x, y: x & y, [df[g] == v for g, v in groups.items()])

return df[idx]

@property
Expand Down Expand Up @@ -258,7 +269,7 @@ def calc_stats(self, trial: optuna.Trial, run_dir: str,
if pd.isna(v):
continue

sub_trips = self.get_group(trips, {g: v})
sub_trips = self.get_group(trips, {g: v}, g in self.multi_groups)
sub_share = sub_trips.groupby("main_mode").count()["trip_number"] / len(sub_trips)
sub_share.rename("share", inplace=True)

Expand Down

0 comments on commit ed738d0

Please sign in to comment.