Skip to content

Commit

Permalink
Load local simulations directly from sxs.load
Browse files Browse the repository at this point in the history
  • Loading branch information
moble committed Oct 9, 2024
1 parent 2f349a6 commit 870476d
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 24 deletions.
15 changes: 10 additions & 5 deletions sxs/handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,18 @@ def load(location, download=None, cache=None, progress=None, truepath=None, **kw
return Catalog.load(download=download)

elif location == "simulations":
return Simulations.load(download=download)

elif location == "local_simulations":
return Simulations.local(download=download)
return Simulations.load(
download=download,
local=kwargs.get("local", False),
annex_dir=kwargs.get("annex_dir", None)
)

elif location == "dataframe":
return Simulations.load(download=download).dataframe
return Simulations.load(
download=download,
local=kwargs.get("local", False),
annex_dir=kwargs.get("annex_dir", None)
).dataframe

elif sxs_id_version_lev_exact_re.match(location):
return Simulation(location, download=download, cache=cache, progress=progress, **kwargs)
Expand Down
47 changes: 28 additions & 19 deletions sxs/simulations/simulations.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,36 +174,45 @@ def local(cls, directory=None, *, download=None):
return simulations

@classmethod
def load(cls, download=None, *, local=False):
def load(cls, download=None, *, local=False, annex_dir=None):
"""Load the catalog of SXS simulations
Note that — unlike most SXS data files — the simulations file is updated
frequently. As a result, this function — unlike the loading functions for most
SXS data files — will download the simulations by default each time it is called.
However, also note that this function is itself cached, meaning that the same
dict will be returned on each call in a given python session. If you want to
avoid that behavior, use `Simulations.reload`.
Note that — unlike most SXS data files — the simulations file
is updated frequently. As a result, this function — unlike
the loading functions for most SXS data files — will download
the simulations by default each time it is called. However,
also note that this function is itself cached, meaning that
the same dict will be returned on each call in a given python
session. If you want to avoid that behavior, use
`Simulations.reload`.
Parameters
----------
download : {None, bool}, optional
If False, this function will look for the simulations in the sxs cache and
raise an error if it is not found. If True, this function will download
the simulations and raise an error if the download fails. If None (the
default), it will try to download the file, warn but fall back to the cache
if that fails, and only raise an error if the simulations is not found in the
cache. Note that this ignores the sxs configuration file entirely.
If False, this function will look for the simulations in
the sxs cache and raise an error if it is not found. If
True, this function will download the simulations and
raise an error if the download fails. If None (the
default), it will try to download the file, warn but fall
back to the cache if that fails, and only raise an error
if the simulations is not found in the cache. Note that
this ignores the sxs configuration file entirely.
Keyword-only Parameters
-----------------------
local : {None, bool}, optional
If True, this function will load local simulations from the sxs cache. To
prepare the cache, you may wish to call `sxs.write_local_simulations`.
If True, this function will load local simulations from
the sxs cache. To prepare the cache, you may wish to call
`sxs.write_local_simulations`.
annex_dir : {None, str, Path}, optional
If provided and `local=True`, this function will load
local simulations from the given directory. This is
equivalent to calling `Simulations.local(directory)`.
See Also
--------
sxs.sxs_directory : Locate cache directory
Simulations.reload : Avoid caching the result of this function
sxs.sxs_directory : Locate cache directory Simulations.reload
: Avoid caching the result of this function
"""
from datetime import datetime, timezone
Expand All @@ -215,8 +224,8 @@ def load(cls, download=None, *, local=False):
if hasattr(cls, "_simulations"):
return cls._simulations

if local:
cls._simulations = cls.local(download=download)
if local or annex_dir is not None:
cls._simulations = cls.local(annex_dir, download=download)
return cls._simulations

progress = read_config("download_progress", True)
Expand Down

0 comments on commit 870476d

Please sign in to comment.