Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions flodym/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
DynamicStockModel as DynamicStockModel,
InflowDrivenDSM as InflowDrivenDSM,
StockDrivenDSM as StockDrivenDSM,
FlexibleDSM as FlexibleDSM,
)
from flodym.lifetime_models import (
LifetimeModel as LifetimeModel,
Expand Down
34 changes: 32 additions & 2 deletions flodym/flodym_arrays.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
from collections import defaultdict
import numpy as np
import pandas as pd
from pydantic import BaseModel as PydanticBaseModel, ConfigDict, model_validator
from typing import Optional, Union, Callable
from pydantic import (
BaseModel as PydanticBaseModel,
ConfigDict,
model_validator,
ModelWrapValidatorHandler,
)
from typing import Optional, Union, Callable, Any, Self
from copy import copy

from .processes import Process
Expand Down Expand Up @@ -61,6 +66,8 @@ class FlodymArray(PydanticBaseModel):
"""Values of the FlodymArray. Must have the same shape as the dimensions of the FlodymArray. If None, an array of zeros is created."""
name: Optional[str] = "unnamed"
"""Name of the FlodymArray."""
_is_set: bool = False
"""Flag indicating whether the flow has been set or not."""

@model_validator(mode="after")
def validate_values(self):
Expand All @@ -84,6 +91,14 @@ def _check_value_format(self):
f"Values shape: {self.values.shape}\n"
)

@model_validator(mode="wrap")
@classmethod
def mark_set_or_unset(cls, data: Any, handler: ModelWrapValidatorHandler[Self]) -> Self:
obj = handler(data)
if isinstance(data, dict):
obj._is_set = "values" in data
return obj

@classmethod
def from_dims_superset(
cls, dims_superset: DimensionSet, dim_letters: tuple = None, **kwargs
Expand Down Expand Up @@ -171,6 +186,7 @@ def set_values(self, values: np.ndarray):
self._check_value_format()
else:
self.values[...] = values
self._is_set = True

def sum_values(self):
"""Return the sum of all values in the FlodymArray."""
Expand Down Expand Up @@ -447,6 +463,7 @@ def __setitem__(self, keys, item):
if isinstance(item, FlodymArray):
slice_obj = self._sub_array_handler(keys)
self.values[slice_obj.ids] = item.sum_values_to(slice_obj.dim_letters)
self._is_set = True
else:
self.set_values(copy(item))
return
Expand Down Expand Up @@ -566,6 +583,19 @@ def items_where(self, condition: Callable) -> np.array:
]
return np.array(items).transpose()

@property
def is_set(self) -> bool:
"""A boolean to indicate whether the flow's values are known or not."""
return self._is_set

def mark_set(self):
"""Mark the flow as having values."""
self._is_set = True

def mark_unset(self):
"""Mark the flow as not having values"""
self._is_set = False

def __str__(self):
base = f"{self.__class__.__name__} '{self.name}'"
dims = f" with dims ({','.join(self.dims.letters)}) and shape {self.shape};"
Expand Down
9 changes: 9 additions & 0 deletions flodym/mfa_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,15 @@ def check_flows(
if all_good:
logging.info(f"Success - No negative flows or NaN values in {self.__class__.__name__}")

def mark_all_unset(self):
"""Mark all parameters as unset."""
for flow in self.flows.values():
flow.mark_unset()
for stock in self.stocks.values():
stock.inflow.mark_unset()
stock.outflow.mark_unset()
stock.stock.mark_unset()

@staticmethod
def _error_or_warning(message: str, raise_error: bool) -> bool:
if raise_error:
Expand Down
Loading