diff --git a/matsim/calibration/__init__.py b/matsim/calibration/__init__.py index 77a4f47..d999ae6 100644 --- a/matsim/calibration/__init__.py +++ b/matsim/calibration/__init__.py @@ -222,7 +222,12 @@ def f(trial): sleep(1) if p.returncode != 0: - raise Exception("Process returned with error code: %s" % p.returncode) + print("The scenario could not be run properly and returned with an error code.", file=sys.stderr) + if not debug: + print("Set debug=True and check the output for any errors.", file=sys.stderr) + print("Alternatively run the cmd from the log above manually and check for errors.", file=sys.stderr) + + raise Exception("Process returned with error code: %s." % p.returncode) finally: p.terminate() diff --git a/matsim/scenariogen/data/__init__.py b/matsim/scenariogen/data/__init__.py index 230d914..5f36c0f 100644 --- a/matsim/scenariogen/data/__init__.py +++ b/matsim/scenariogen/data/__init__.py @@ -288,6 +288,8 @@ class Household: type: HouseholdType region_type: int location: str + zone: str = None + """ A detailed zone, which can be more accurate than location. """ income: float = None geom: object = None @@ -328,6 +330,8 @@ class Trip: purpose: Purpose sd_group: SourceDestinationGroup valid: bool + from_zone: str = None + to_zone: str = None @dataclass diff --git a/matsim/scenariogen/data/formats/srv.py b/matsim/scenariogen/data/formats/srv.py index bd02d1a..a500888 100644 --- a/matsim/scenariogen/data/formats/srv.py +++ b/matsim/scenariogen/data/formats/srv.py @@ -2,14 +2,15 @@ import os -import pandas as pd import numpy as np +import pandas as pd from .. import * # Has households, persons and trips INPUT_FILES = 3 + def is_format(f: os.DirEntry): fp = f.name if not f.path.endswith(".csv"): @@ -92,6 +93,7 @@ def convert(data: tuple, regio=None): SrV2018.household_type(h.E_HHTYP), SrV2018.region_type(h, regio, random_state), h.ST_CODE_NAME, + zone=SrV2018.parse_zone(h), income=SrV2018.income(h.V_EINK), ) ) @@ -116,17 +118,33 @@ def convert(data: tuple, regio=None): SrV2018.trip_purpose(t.V_ZWECK), SrV2018.sd_group(int(t.E_QZG_17)), # Trip is valid if length and duration are present - 0 <= t.GIS_LAENGE and t.E_DAUER > 0 + 0 <= t.GIS_LAENGE and t.E_DAUER > 0, + from_zone=SrV2018.parse_zone(t, "V_START_"), + to_zone=SrV2018.parse_zone(t, "V_ZIEL_") ) ) return pd.DataFrame(hhs).set_index("hh_id"), ps, pd.DataFrame(ts).set_index("t_id") + def pint(x): """ Convert to positive integer""" return max(0, int(x)) +def parse_int_str(x): + """ Return parsed int or string """ + try: + i = int(x) + if i >= 0: + return str(i) + return None + except ValueError: + if not x: + return None + return x + + class SrV2018: """ Maps SrV data to standard format""" @@ -420,3 +438,19 @@ def income(x): return 5600 return -1 + + @staticmethod + def parse_zone(h, prefix=""): + ob = parse_int_str(getattr(h, prefix + "OBERBEZIRK")) + + zone = pd.NA + if ob: + zone = ob + ub = parse_int_str(getattr(h, prefix + "UNTERBEZIRK")) + if ub: + zone += "-" + ub + tb = parse_int_str(getattr(h, prefix + "TEILBEZIRK")) + if tb: + zone += "-" + tb + + return zone