Skip to content

Commit f71e980

Browse files
committed
More general way to specify model name
1 parent 3166678 commit f71e980

File tree

2 files changed

+27
-22
lines changed

2 files changed

+27
-22
lines changed

.pre-commit-config.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
repos:
22
- repo: https://github.com/psf/black
3-
rev: 23.3.0
3+
rev: 24.10.0
44
hooks:
55
- id: black
6-
language_version: python3.10
6+
language_version: python3.11
77

88
- repo: https://github.com/pycqa/isort
9-
rev: 5.12.0
9+
rev: 5.13.2
1010
hooks:
1111
- id: isort
1212
name: isort (python)
13-
language_version: python3.10
13+
language_version: python3.11

xija/get_model_spec.py

+23-18
Original file line numberDiff line numberDiff line change
@@ -51,21 +51,22 @@ def get_xija_model_spec(
5151
"""
5252
Get Xija model specification for the specified ``model_name``.
5353
54-
Supported model names include (but are not limited to): ``'aca'``,
55-
``'acisfp'``, ``'dea'``, ``'dpa'``, ``'psmc'``, ``'minusyz'``, and
56-
``'pftank2t'``.
54+
This gets the model specification from the Ska chandra_models repository, looking
55+
for ``*.json`` files in the ``chandra_models/xija`` directory of the repository.
5756
58-
Use ``get_xija_model_names()`` for the full list.
57+
The ``model_name`` can be provided in two ways:
58+
- Short like ``'acisfp'`` which looks for ``acisfp_spec.json`` (tried first).
59+
- Full like ``'acisfp_spec_matlab'`` which looks for ``acisfp_spec_matlab.json``.
5960
6061
Examples
6162
--------
62-
Get the latest version of the ``acisfp`` model spec from the local Ska data
63+
Get the latest version of the ``acisfp_spec`` model spec from the local Ska data
6364
directory ``$SKA/data/chandra_models``, checking that the version matches
6465
the latest release tag on GitHub.
6566
6667
>>> import xija
6768
>>> from xija.get_model_spec import get_xija_model_spec
68-
>>> model_spec, version = get_xija_model_spec('acisfp', check_version=True)
69+
>>> model_spec, version = get_xija_model_spec('acisfp_spec', check_version=True)
6970
>>> model = xija.XijaModel('acisfp', model_spec=model_spec,
7071
... start='2020:001', stop='2020:010')
7172
>>> model.make()
@@ -81,7 +82,7 @@ def get_xija_model_spec(
8182
Parameters
8283
----------
8384
model_name : str
84-
Name of model
85+
Name of model or model spec file (e.g. 'acisfp' or 'acisfp_spec_matlab')
8586
version : str, None
8687
Tag, branch or commit of chandra_models to use (default=latest tag from
8788
repo)
@@ -120,17 +121,21 @@ def _get_xija_model_spec(
120121
if not models_path.exists():
121122
raise FileNotFoundError(f"xija models directory {models_path} does not exist")
122123

123-
file_glob = str(models_path / "*" / f"{model_name.lower()}_spec.json")
124-
try:
125-
# get_globfiles() default requires exactly one file match and returns a list
126-
file_name = get_globfiles(file_glob)[0]
127-
except ValueError:
128-
names = get_xija_model_names(repo_path)
129-
raise ValueError(
130-
f'no models matched {model_name}. Available models are: {", ".join(names)}'
131-
)
132-
133-
model_spec = json.load(open(file_name, "r"))
124+
file_path = None
125+
for suffix in ["_spec.json", ".json"]:
126+
file_paths = list(models_path.glob(f"*/{model_name}{suffix}"))
127+
if len(file_paths) == 1:
128+
file_path = file_paths[0]
129+
break
130+
elif len(file_paths) > 1:
131+
raise ValueError(
132+
f"Multiple files found for {model_name} in {models_path}: {file_paths}"
133+
)
134+
135+
if file_path is None:
136+
raise ValueError(f"no model spec files matched {model_name}")
137+
138+
model_spec = json.load(open(file_path, "r"))
134139

135140
# Get version and ensure that repo is clean and tip is at latest tag
136141
if version is None:

0 commit comments

Comments
 (0)