Skip to content

Commit

Permalink
try to parse json profiles as sum or enum profiles, and skip them if …
Browse files Browse the repository at this point in the history
…that does not work
  • Loading branch information
DavidNeuroth committed Oct 31, 2024
1 parent f9c2802 commit 0f5f156
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
42 changes: 26 additions & 16 deletions pylpg/lpg_execution.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setuptools.setup(
name="pyloadprofilegenerator",
version="0.1.0",
version="0.1.1",
author="Noah Pflugradt",
author_email="[email protected]",
description="Python Bindings for the LoadProfileGenerator",
Expand Down
2 changes: 1 addition & 1 deletion test/test_pylpg.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand Down

0 comments on commit 0f5f156

Please sign in to comment.