Skip to content
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

Add support for two new env vars for chandra_models defaults + get_data info #33

Merged
merged 5 commits into from
Jun 1, 2023

Conversation

taldcroft
Copy link
Member

@taldcroft taldcroft commented May 30, 2023

Description

This adds two new environment variables that impact the behavior:

  • CHANDRA_MODELS_REPO_DIR as an alias for THERMAL_MODELS_DIR_FOR_MATLAB_TOOLS_SW:
    override the default root for the chandra_models repository. The name defined by FOT Matlab tools is impossible
    to remember. 😄
  • CHANDRA_MODELS_DEFAULT_VERSION: override the default repo version.

Adding, CHANDRA_MODELS_DEFAULT_VERSION environment variable might be controversial but this greatly facilitates two important use cases:

  • Setting version to a fixed version in unit tests (e.g. with monkeypatch)
  • Set to a developement branch to test a model file update with applications like yoshi where
    specifying a version would require a long chain of API updates.

This also adds a return dict of provenance information regarding the get_data call.

Interface impacts

Changes return of get_data from just the data to a tuple (data, provenance information).

Testing

Unit tests

  • Mac

Independent check of unit tests by Jean

  • Linux

Functional tests

@jeanconn extracted the FOT Matlab tools archive of chandra_models. This looks like below, where each directory is a shallow git repo with just the latest commit:

ska3-dev) ➜  chandra_aca git:(model-globs) ls /Users/aldcroft/Downloads/Thermal_Models
Archive/               chandra_models-3.43/   chandra_models-3.44/   chandra_models-3.46/   chandra_models-3.48/
chandra_models-3.41.1/ chandra_models-3.43.1/ chandra_models-3.45/   chandra_models-3.47/

Then I did the following in a dev ska with this repo installed:

In [11]: os.environ[
    ...:     "CHANDRA_MODELS_REPO_DIR"
    ...: ] = "/Users/aldcroft/Downloads/Thermal_Models/chandra_models-3.47"

In [12]: data, info = chandra_models.get_data("chandra_models/xija/aca/aca_spec.json")

In [13]: import json

In [14]: spec = json.loads(data)

In [15]: spec["name"]
Out[15]: 'aacccdpt'

In [16]: info
Out[16]: 
{'call_args': {'file_path': 'chandra_models/xija/aca/aca_spec.json',
  'version': None,
  'repo_path': 'None',
  'require_latest_version': False,
  'timeout': 5,
  'read_func': 'None',
  'read_func_kwargs': None},
 'version': '3.47',
 'commit': '5307bf8ad3a8588de3417b44f8eddf2a5a26a9e0',
 'data_file_path': '/Users/aldcroft/Downloads/Thermal_Models/chandra_models-3.47/chandra_models/xija/aca/aca_spec.json',
 'repo_path': '/Users/aldcroft/Downloads/Thermal_Models/chandra_models-3.47',
 'CHANDRA_MODELS_DEFAULT_VERSION': None,
 'CHANDRA_MODELS_REPO_DIR': '/Users/aldcroft/Downloads/Thermal_Models/chandra_models-3.47',
 'md5': '71cda7e5c2c5f72e5007f5d79014bd14'}

In [17]: del os.environ["CHANDRA_MODELS_REPO_DIR"]

In [18]: data, info = chandra_models.get_data("chandra_models/xija/aca/aca_spec.json")

In [19]: info
Out[19]: 
{'call_args': {'file_path': 'chandra_models/xija/aca/aca_spec.json',
  'version': None,
  'repo_path': 'None',
  'require_latest_version': False,
  'timeout': 5,
  'read_func': 'None',
  'read_func_kwargs': None},
 'version': '3.48',
 'commit': '68a58099a9b51bef52ef14fbd0f1971f950e6ba3',
 'data_file_path': '/Users/aldcroft/ska/data/chandra_models/chandra_models/xija/aca/aca_spec.json',
 'repo_path': '/Users/aldcroft/ska/data/chandra_models',
 'CHANDRA_MODELS_DEFAULT_VERSION': None,
 'CHANDRA_MODELS_REPO_DIR': None,
 'md5': '71cda7e5c2c5f72e5007f5d79014bd14'}

@taldcroft taldcroft changed the title WIP: add support for two new environment variables for chandra_models defaults Add support for two new environment variables for chandra_models defaults May 31, 2023
@taldcroft taldcroft requested a review from jeanconn May 31, 2023 19:19

:returns: Path object
Path to ``chandra_models`` repository
"""
for env_var in CHANDRA_MODELS_ROOT_ENV_VARS:
if env_var in os.environ:
return Path(os.environ[env_var])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feels a little odd to handle this via an "early" return but it is a short function.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From what I've read early returns are generally a good thing and encouraged to reduce code complexity. Here is an opinion from the internet that must therefore be true. http://www.itamarweiss.com/personal/2018/02/28/return-early-pattern.html

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's worth saying that this is something new-ish to me and might not be reflected in earlier code / patterns.

Copy link
Contributor

@jeanconn jeanconn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changes make sense to me and unit and functional testing looks good.

@jeanconn
Copy link
Contributor

I note that I think sot/ska_sun#31 needs to go at the same time.

@taldcroft taldcroft merged commit c32cd96 into master Jun 1, 2023
@taldcroft taldcroft deleted the chandra-models-env-vars branch June 1, 2023 09:57
@taldcroft taldcroft changed the title Add support for two new environment variables for chandra_models defaults Add support for two new env vars for chandra_models defaults + get_data info Jul 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants