From 29bb733b7f9a962912ba880cb4979bc598a43c87 Mon Sep 17 00:00:00 2001 From: simei94 Date: Mon, 19 Feb 2024 00:29:44 -0600 Subject: [PATCH 1/2] handling of delimiters different from ";" --- matsim/calibration/analysis.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/matsim/calibration/analysis.py b/matsim/calibration/analysis.py index dc82a4a..28a1833 100644 --- a/matsim/calibration/analysis.py +++ b/matsim/calibration/analysis.py @@ -74,6 +74,9 @@ def read_trips_and_persons(run, transform_persons=None, transform_trips=None) -> df = pd.read_csv(trips, sep=";", dtype={"person": "str"}) dfp = pd.read_csv(persons, sep=";", dtype={"person": "str"}) + df = get_correct_delimiter(df, trips, persons) + dfp = get_correct_delimiter(dfp, trips, persons) + gdf = geopandas.GeoDataFrame(dfp, geometry=geopandas.points_from_xy(dfp.first_act_x, dfp.first_act_y) ) @@ -93,6 +96,26 @@ def read_trips_and_persons(run, transform_persons=None, transform_trips=None) -> return df, gdf +def get_correct_delimiter(df : pd.DataFrame, trips : str, persons : str): + """ Determine correct delimiter """ + + delimiter = "" + + if len(df.columns) == 1: + if str(df.columns).find(",") != -1: + delimiter = "," + + if str(df.columns).find("\t") != -1: + delimiter = "\t" + + if str(df.columns).find("trip") != -1: + df = pd.read_csv(trips, sep=delimiter, dtype={"person": "str"}) + else: + df = pd.read_csv(persons, sep=delimiter, dtype={"person": "str"}) + + return df + + def read_leg_stats(run : str, transform_persons=None, transform_legs=None): """ Reads leg statistic from run directory """ From a5754ff3d4e68082cee858f7235f3b3056b65eb5 Mon Sep 17 00:00:00 2001 From: simei94 Date: Tue, 20 Feb 2024 03:04:41 -0600 Subject: [PATCH 2/2] simplify --- matsim/calibration/analysis.py | 27 ++------------------------- 1 file changed, 2 insertions(+), 25 deletions(-) diff --git a/matsim/calibration/analysis.py b/matsim/calibration/analysis.py index 28a1833..5f25c5f 100644 --- a/matsim/calibration/analysis.py +++ b/matsim/calibration/analysis.py @@ -71,11 +71,8 @@ def read_trips_and_persons(run, transform_persons=None, transform_trips=None) -> trips = glob.glob(run.rstrip("/") + "/*.output_trips.csv.gz")[0] persons = glob.glob(run.rstrip("/") + "/*.output_persons.csv.gz")[0] - df = pd.read_csv(trips, sep=";", dtype={"person": "str"}) - dfp = pd.read_csv(persons, sep=";", dtype={"person": "str"}) - - df = get_correct_delimiter(df, trips, persons) - dfp = get_correct_delimiter(dfp, trips, persons) + df = pd.read_csv(trips, dtype={"person": "str"}) + dfp = pd.read_csv(persons, dtype={"person": "str"}) gdf = geopandas.GeoDataFrame(dfp, geometry=geopandas.points_from_xy(dfp.first_act_x, dfp.first_act_y) @@ -96,26 +93,6 @@ def read_trips_and_persons(run, transform_persons=None, transform_trips=None) -> return df, gdf -def get_correct_delimiter(df : pd.DataFrame, trips : str, persons : str): - """ Determine correct delimiter """ - - delimiter = "" - - if len(df.columns) == 1: - if str(df.columns).find(",") != -1: - delimiter = "," - - if str(df.columns).find("\t") != -1: - delimiter = "\t" - - if str(df.columns).find("trip") != -1: - df = pd.read_csv(trips, sep=delimiter, dtype={"person": "str"}) - else: - df = pd.read_csv(persons, sep=delimiter, dtype={"person": "str"}) - - return df - - def read_leg_stats(run : str, transform_persons=None, transform_legs=None): """ Reads leg statistic from run directory """