Skip to content

Commit

Permalink
Refactor data model imports and add YAML configuration method
Browse files Browse the repository at this point in the history
- Moved `init_df_state` from `state.py` to `type.py`
- Added `from_yaml` class method to `SUEWSConfig` in `core.py`
- Updated imports in multiple data model modules
- Added new utility function `init_config_from_yaml` in `_config.py`
- Installed new data model files `hydro.py` and `ohm.py`
  • Loading branch information
sunt05 committed Feb 6, 2025
1 parent baaabe9 commit b68caf7
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 8 deletions.
14 changes: 14 additions & 0 deletions src/supy/data_model/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,20 @@ class SUEWSConfig(BaseModel):
class Config:
extra = "allow"

@classmethod
def from_yaml(cls, path: str) -> "SUEWSConfig":
"""Initialize SUEWSConfig from YAML file.
Args:
path (str): Path to YAML configuration file
Returns:
SUEWSConfig: Instance of SUEWSConfig initialized from YAML
"""
with open(path, "r") as file:
config = yaml.load(file, Loader=yaml.FullLoader)
return cls(**config)

def create_multi_index_columns(self, columns_file: str) -> pd.MultiIndex:
"""Create MultiIndex from df_state_columns.txt"""
with open(columns_file, "r") as f:
Expand Down
4 changes: 2 additions & 2 deletions src/supy/data_model/human_activity.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pandas as pd
from .type import ValueWithDOI, Reference
from .profile import HourlyProfile, WeeklyProfile, DayProfile
from .state import init_df_state
from .type import init_df_state


class IrrigationParams(
Expand Down Expand Up @@ -481,7 +481,7 @@ def from_df_state(cls, df: pd.DataFrame, grid_id: int) -> "AnthropogenicEmission
# Reconstruct CO2 parameters
co2 = CO2Params.from_df_state(df, grid_id)

return cls(startdls=startdls, enddls=enddls, heat=heat, co2=co2)
return cls(startdls=startdls, enddls=enddls, heat=heat.model_dump(), co2=co2.model_dump())

class AnthropogenicHeat(
BaseModel
Expand Down
3 changes: 1 addition & 2 deletions src/supy/data_model/hydro.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
from pydantic import BaseModel, Field, PrivateAttr
from typing import Optional
import pandas as pd
from .type import ValueWithDOI, Reference
from .site import SurfaceType
from .type import ValueWithDOI, Reference, SurfaceType
import math


Expand Down
2 changes: 1 addition & 1 deletion src/supy/data_model/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from enum import Enum

from .type import ValueWithDOI, Reference
from .state import init_df_state
from .type import init_df_state


class EmissionsMethod(Enum):
Expand Down
5 changes: 2 additions & 3 deletions src/supy/data_model/state.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from dataclasses import Field
from typing import Optional, Union, List, Literal, Type
import pandas as pd
from pydantic import BaseModel, field_validator, model_validator, PrivateAttr
from pydantic import BaseModel, Field, field_validator, model_validator, PrivateAttr

from .type import ValueWithDOI, Reference
from .type import ValueWithDOI, Reference, init_df_state
from .site import SurfaceType


Expand Down
2 changes: 2 additions & 0 deletions src/supy/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,10 @@ py.install_sources(
[
'data_model/__init__.py',
'data_model/core.py',
'data_model/hydro.py',
'data_model/human_activity.py',
'data_model/model.py',
'data_model/ohm.py',
'data_model/profile.py',
'data_model/site.py',
'data_model/state.py',
Expand Down
6 changes: 6 additions & 0 deletions src/supy/util/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -5138,3 +5138,9 @@ def to_yaml(self, path: str = "./config-suews.yml"):
sort_keys=False,
allow_unicode=True,
)

def init_config_from_yaml(path: str = "./config-suews.yml") -> SUEWSConfig:
"""Initialize SUEWSConfig from YAML file"""
with open(path, "r") as file:
config = yaml.load(file, Loader=yaml.FullLoader)
return SUEWSConfig(**config)

0 comments on commit b68caf7

Please sign in to comment.