Skip to content

made caching optional with config flag #35

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 12 additions & 0 deletions docs/whats_new.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,17 @@
# What's New

## v0.9.6 (November 15, 2024)

* made caching directory creation and saving to cache optional with input option `use_cache`.

## v0.9.5 (November 14, 2024)

* fixed error in output file

## v0.9.4 (November 14, 2024)

* Updated locations for local model output.

## v0.9.3 (November 13, 2024)

* Moved `output_format` parameter to manager config from model config
Expand Down
15 changes: 1 addition & 14 deletions particle_tracking_manager/models/opendrift/opendrift.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
from pathlib import Path
from typing import Optional, Union

import appdirs

# using my own version of ROMS reader
# from .reader_ROMS_native import Reader
import pandas as pd
Expand Down Expand Up @@ -858,22 +856,11 @@ def run_add_reader(
"start_time and end_time must be set to narrow model output to simulation time"
)

# save interpolators to save time
cache_dir = Path(
appdirs.user_cache_dir(
appname="particle-tracking-manager", appauthor="axiom-data-science"
)
)
cache_dir.mkdir(parents=True, exist_ok=True)
self.cache_dir = cache_dir
self.interpolator_filename = cache_dir / Path(
f"{self.ocean_model}_interpolator"
)
reader = reader_ROMS_native.Reader(
filename=ds,
name=self.ocean_model,
standard_name_mapping=standard_name_mapping,
save_interpolator=True,
save_interpolator=self.save_interpolator,
interpolator_filename=self.interpolator_filename,
)

Expand Down
30 changes: 29 additions & 1 deletion particle_tracking_manager/the_manager.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
"""Contains logic for configuring particle tracking simulations."""


# from docstring_inheritance import NumpyDocstringInheritanceMeta
import datetime
import json
import logging
import pathlib

from typing import Optional, Union

# from docstring_inheritance import NumpyDocstringInheritanceMeta
import appdirs
import pandas as pd

from .cli import is_None
Expand Down Expand Up @@ -139,6 +140,8 @@ class ParticleTrackingManager:
Name of output file to save, by default None. If None, default is set in the model. Without any suffix.
output_format : str, default "netcdf"
Name of input/output module type to use for writing Lagrangian model output. Default is "netcdf".
use_cache : bool
Set to True to use cache for saving interpolators, by default True.

Notes
-----
Expand All @@ -165,6 +168,7 @@ class ParticleTrackingManager:
output_file: str
output_format: str
output_file_initial: Optional[str]
interpolator_filename: Optional[pathlib.Path]

def __init__(
self,
Expand Down Expand Up @@ -193,6 +197,7 @@ def __init__(
use_static_masks: bool = config_ptm["use_static_masks"]["default"],
output_file: Optional[str] = config_ptm["output_file"]["default"],
output_format: str = config_ptm["output_format"]["default"],
use_cache: bool = config_ptm["use_cache"]["default"],
**kw,
) -> None:
"""Inputs necessary for any particle tracking."""
Expand Down Expand Up @@ -466,6 +471,29 @@ def __setattr__(self, name: str, value) -> None:
)
self.seed_seafloor = False

# save interpolators to save time
if name == "use_cache":
if value:
cache_dir = pathlib.Path(
appdirs.user_cache_dir(
appname="particle-tracking-manager",
appauthor="axiom-data-science",
)
)
cache_dir.mkdir(parents=True, exist_ok=True)
self.cache_dir = cache_dir
self.interpolator_filename = cache_dir / pathlib.Path(
f"{self.ocean_model}_interpolator"
)
self.save_interpolator = True
self.logger.info(
f"Interpolators will be saved to {self.interpolator_filename}."
)
else:
self.save_interpolator = False
self.interpolator_filename = None
self.logger.info("Interpolators will not be saved.")

# if reader, lon, and lat set, check inputs
if (
name == "has_added_reader"
Expand Down
6 changes: 6 additions & 0 deletions particle_tracking_manager/the_manager_config.json
Original file line number Diff line number Diff line change
Expand Up @@ -187,5 +187,11 @@
"default": "netcdf",
"description": "Output file format. Options are \"netcdf\" or \"parquet\".",
"ptm_level": 2
},
"use_cache": {
"type": "bool",
"default": true,
"description": "Set to True to use cache for storing interpolators. This saves time on repeat simulations, may be used for other items in the future.",
"ptm_level": 3
}
}
7 changes: 7 additions & 0 deletions tests/test_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,13 @@ def test_input_too_many_end_of_simulation():
)


def test_no_cache():
"""Test having no cache"""

m = ptm.OpenDriftModel(use_cache=False)
assert m.interpolator_filename is None


def test_changing_end_of_simulation():
"""change end_time, steps, and duration

Expand Down
Loading