From ed2e526dcd171cd96c79e687b1ad99b47e8292b1 Mon Sep 17 00:00:00 2001 From: rakow Date: Sun, 18 Feb 2024 12:00:51 +0100 Subject: [PATCH 1/3] add zone information to data model --- matsim/scenariogen/data/__init__.py | 4 +++ matsim/scenariogen/data/formats/srv.py | 38 ++++++++++++++++++++++++-- 2 files changed, 40 insertions(+), 2 deletions(-) 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..79bb094 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 = None + 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 From ca3e24ec5a58c123489f3c86ad507cc6d118d141 Mon Sep 17 00:00:00 2001 From: rakow Date: Sun, 18 Feb 2024 12:08:01 +0100 Subject: [PATCH 2/3] add error messages in case the scenario returns an error --- matsim/calibration/__init__.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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() From b8ccf201f18d47e1a957137d6b5b54923c0c4444 Mon Sep 17 00:00:00 2001 From: rakow Date: Mon, 19 Feb 2024 10:20:31 +0100 Subject: [PATCH 3/3] use pd.NA instead of None --- matsim/scenariogen/data/formats/srv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/matsim/scenariogen/data/formats/srv.py b/matsim/scenariogen/data/formats/srv.py index 79bb094..a500888 100644 --- a/matsim/scenariogen/data/formats/srv.py +++ b/matsim/scenariogen/data/formats/srv.py @@ -443,7 +443,7 @@ def income(x): def parse_zone(h, prefix=""): ob = parse_int_str(getattr(h, prefix + "OBERBEZIRK")) - zone = None + zone = pd.NA if ob: zone = ob ub = parse_int_str(getattr(h, prefix + "UNTERBEZIRK"))