Skip to content

Commit

Permalink
Merge branch 'master' into delimiter_fix
Browse files Browse the repository at this point in the history
  • Loading branch information
simei94 authored Feb 19, 2024
2 parents 29bb733 + b8ccf20 commit f7df9e9
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 3 deletions.
7 changes: 6 additions & 1 deletion matsim/calibration/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand Down
4 changes: 4 additions & 0 deletions matsim/scenariogen/data/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -328,6 +330,8 @@ class Trip:
purpose: Purpose
sd_group: SourceDestinationGroup
valid: bool
from_zone: str = None
to_zone: str = None


@dataclass
Expand Down
38 changes: 36 additions & 2 deletions matsim/scenariogen/data/formats/srv.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"):
Expand Down Expand Up @@ -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),
)
)
Expand All @@ -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"""

Expand Down Expand Up @@ -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

0 comments on commit f7df9e9

Please sign in to comment.