From 0f5f156a711344872518f6cf5a2911576f33e741 Mon Sep 17 00:00:00 2001 From: David Neuroth Date: Thu, 31 Oct 2024 17:36:22 +0100 Subject: [PATCH] try to parse json profiles as sum or enum profiles, and skip them if that does not work --- pylpg/lpg_execution.py | 42 ++++++++++++++++++++++++++---------------- setup.py | 2 +- test/test_pylpg.py | 2 +- 3 files changed, 28 insertions(+), 18 deletions(-) diff --git a/pylpg/lpg_execution.py b/pylpg/lpg_execution.py index 6394883..e031918 100644 --- a/pylpg/lpg_execution.py +++ b/pylpg/lpg_execution.py @@ -544,6 +544,23 @@ def make_default_lpg_settings(self, year: int) -> HouseCreationAndCalculationJob ) return hj + @staticmethod + def parse_json_profile(filepath: str) -> JsonSumProfile | JsonEnumProfile: + """parses a single json profile file""" + print("Reading json file " + str(filepath)) + with open(str(filepath)) as json_file: + filecontent: str = json_file.read() # type: ignore + try: + # try to load as a sum profile + profile: JsonSumProfile = JsonSumProfile.from_json(filecontent) # type: ignore + except Exception: + try: + # try to load as enum profile instead + profile = JsonEnumProfile.from_json(filecontent) + except Exception as e: + print(f"Could not parse Json file {filepath} - skipping it") + return profile + def read_all_json_results_in_directory(self) -> Optional[pd.DataFrame]: df: pd.DataFrame = pd.DataFrame() results_directory = Path(self.calculation_directory, "results", "Results") @@ -569,27 +586,20 @@ def read_all_json_results_in_directory(self) -> Optional[pd.DataFrame]: potential_sum_files.extend(soc) isFirst = True for file in potential_sum_files: - print("Reading file " + str(file)) - with open(str(file)) as json_file: - filecontent: str = json_file.read() # type: ignore - sumProfile: JsonSumProfile = JsonSumProfile.from_json(filecontent) # type: ignore - if sumProfile.LoadTypeName is None: + profile = self.parse_json_profile(file) + if profile.LoadTypeName is None: raise Exception("Empty load type name on " + str(file)) if ( - sumProfile is None - or sumProfile.HouseKey is None - or sumProfile.HouseKey.HHKey is None + profile is None + or profile.HouseKey is None + or profile.HouseKey.HHKey is None ): raise Exception("empty housekey") - key: str = ( - sumProfile.LoadTypeName + "_" + str(sumProfile.HouseKey.HHKey.Key) - ) - df[key] = sumProfile.Values + key: str = profile.LoadTypeName + "_" + str(profile.HouseKey.HHKey.Key) + df[key] = profile.Values if isFirst: isFirst = False - ts = sumProfile.StartTime - timestamps = pd.date_range( - ts, periods=len(sumProfile.Values), freq="min" - ) + ts = profile.StartTime + timestamps = pd.date_range(ts, periods=len(profile.Values), freq="min") df.index = timestamps return df diff --git a/setup.py b/setup.py index bfb4608..5cf882f 100644 --- a/setup.py +++ b/setup.py @@ -10,7 +10,7 @@ setuptools.setup( name="pyloadprofilegenerator", - version="0.1.0", + version="0.1.1", author="Noah Pflugradt", author_email="n.pflugradt@fz-juelich.de", description="Python Bindings for the LoadProfileGenerator", diff --git a/test/test_pylpg.py b/test/test_pylpg.py index 7859a54..504c55b 100644 --- a/test/test_pylpg.py +++ b/test/test_pylpg.py @@ -151,7 +151,7 @@ def test_grid_profiles_function() -> None: travel_route_set=TravelRouteSets.Travel_Route_Set_for_15km_Commuting_Distance, ) df.to_csv(r"lpgexport_grid.csv", index=True, sep=";") - sumcol = df.sum(axis=0) + sumcol = df.sum(axis=0, numeric_only=True) print(sumcol) print("successfully exportet dataframe to lpgexport.csv")