Skip to content

Commit

Permalink
rework save/export
Browse files Browse the repository at this point in the history
  • Loading branch information
jensdebruijn committed May 2, 2024
1 parent 044c470 commit 955b116
Showing 1 changed file with 39 additions and 29 deletions.
68 changes: 39 additions & 29 deletions honeybees/reporter.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ def export_value(self, name: str, value: np.ndarray, conf: dict) -> None:
value: The array itself.
conf: Configuration for saving the file. Contains options such a file format, and whether to export the array in this timestep at all.
"""
if value is None:
return None
folder = os.path.join(self.export_folder, name)
try:
os.makedirs(folder)
Expand All @@ -101,8 +103,6 @@ def export_value(self, name: str, value: np.ndarray, conf: dict) -> None:
elif conf["format"] == "csv":
fn += ".csv"
fp = os.path.join(folder, fn)
if isinstance(value, (np.ndarray, cp.ndarray)):
value = value.tolist()
if len(value) > 100_000:
self.model.logger.info(
f"Exporting {len(value)} items to csv. This might take a long time and take a lot of space. Consider using NumPy (compressed) binary format (npy/npz)."
Expand Down Expand Up @@ -139,16 +139,44 @@ def report_value(
for v in value:
self.check_value(v)

if "save" not in conf:
raise ValueError(
f"Save type must be specified for {name} in config file (save/save+export/export)."
)
if conf["save"] not in ("save", "export", "save+export"):
raise ValueError(
f"Save type for {name} in config file must be 'save', 'save+export' or 'export')."
if "save" in conf:
if conf["save"] not in ("save", "export"):
raise ValueError(
f"Save type for {name} in config file must be 'save', 'save+export' or 'export')."
)
import warnings

warnings.warn(
"The `save` option is deprecated and will be removed in future versions. If you use 'save: export' the option can simply be removed (new default). If you use 'save: save', please replace with 'single_file: true'",
DeprecationWarning,
)
if conf["save"] == "save":
conf["single_file"] = True
del conf["save"]

if conf["save"] == "export":
if (
"single_file" in conf
and conf["single_file"] is True
and conf["format"] != "netcdf"
):
try:
if isinstance(name, tuple):
name, ID = name
if name not in self.variables:
self.variables[name] = {}
if ID not in self.variables[name]:
self.variables[name][ID] = []
self.variables[name][ID].append(value)
else:
if name not in self.variables:
self.variables[name] = []
self.variables[name].append(value)
except KeyError:
raise KeyError(
f"Variable {name} not initialized. This likely means that an agent is reporting for a group that was not is not the reporter"
)

else:
if "frequency" in conf and conf["frequency"] is not None:
if conf["frequency"] == "initial":
if self.model.current_timestep == 0:
Expand Down Expand Up @@ -181,24 +209,6 @@ def report_value(
else:
self.export_value(name, value, conf)

if conf["save"] == "save":
try:
if isinstance(name, tuple):
name, ID = name
if name not in self.variables:
self.variables[name] = {}
if ID not in self.variables[name]:
self.variables[name][ID] = []
self.variables[name][ID].append(value)
else:
if name not in self.variables:
self.variables[name] = []
self.variables[name].append(value)
except KeyError:
raise KeyError(
f"Variable {name} not initialized. This likely means that an agent is reporting for a group that was not is not the reporter"
)

@staticmethod
@njit
def mean_per_ID(
Expand Down Expand Up @@ -259,7 +269,7 @@ def parse_agent_data(self, name: str, values: Any, agents, conf: dict) -> None:
conf: Dictionary with report configuration for values.
"""
function = conf["function"]
if function is None:
if function is None or values is None:
values = deepcopy(
values
) # need to copy item, because values are passed without applying any a function.
Expand Down

0 comments on commit 955b116

Please sign in to comment.