Skip to content

Commit

Permalink
Lazy load config. (#124)
Browse files Browse the repository at this point in the history
* Use c.to("km/s") for speed of light

and re-standardize astropy constants imports.

* Lazy load configuration using _config.py and testing.

* fix version in __init__ and __all__

* Remove starfish test config ad fix config tests.

* Test lazy load. xfail as not working correctly.

* Fix cache location config.

* Lint
  • Loading branch information
jason-neal committed Dec 15, 2018
1 parent fbef82e commit eeff318
Show file tree
Hide file tree
Showing 25 changed files with 199 additions and 112 deletions.
4 changes: 2 additions & 2 deletions conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def resampled_data(request):
id_string = "{0:s}-{1:s}-{2:.1f}-{3:s}".format(star, band, float(vel), res)

test_data = os.path.join(
eniric.paths["resampled"], resampled_template.format(star, band, vel, res)
eniric.config.paths["resampled"], resampled_template.format(star, band, vel, res)
)
wav, flux = io.pdread_2col(test_data)
return id_string, wav, flux
Expand Down Expand Up @@ -125,7 +125,7 @@ def grad_flag(request):
)
def atm_model(request):
"""Get atmospheric model name to load."""
return os.path.join(eniric.paths["atmmodel"], request.param)
return os.path.join(eniric.config.paths["atmmodel"], request.param)


@pytest.fixture(params=[2.5, 4])
Expand Down
4 changes: 2 additions & 2 deletions docs/Notebooks/Convolution_speeds.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@
"import PyAstronomy.pyasl as pyasl\n",
"\n",
"import eniric\n",
"#eniric.cache[\"location\"] = None # Disable caching for these tests\n",
"eniric.cache[\"location\"] = \".joblib\" # Enable caching\n",
"#eniric.config.cache[\"location\"] = None # Disable caching for these tests\n",
"eniric.config.cache[\"location\"] = \".joblib\" # Enable caching\n",
"\n",
"from eniric.broaden import rotational_convolution, resolution_convolution\n",
"from eniric.utilities import band_limits, load_aces_spectrum, wav_selector\n",
Expand Down
6 changes: 3 additions & 3 deletions docs/Notebooks/Precison_with_doppler-K-band.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
"import numpy as np\n",
"from tqdm import tqdm\n",
"import PyAstronomy.pyasl as pyasl\n",
"import astropy.constants as const\n",
"from astropy import constants as const\n",
"\n",
"import eniric\n",
"#eniric.cache[\"location\"] = None # Disable caching for these tests\n",
"eniric.cache[\"location\"] = \".joblib\" # Enable caching\n",
"#eniric.config.cache[\"location\"] = None # Disable caching for these tests\n",
"eniric.config.cache[\"location\"] = \".joblib\" # Enable caching\n",
"\n",
"from eniric.broaden import rotational_convolution, resolution_convolution\n",
"from eniric.utilities import band_limits, load_aces_spectrum, wav_selector\n",
Expand Down
6 changes: 3 additions & 3 deletions docs/Notebooks/Precison_with_doppler-Z-band.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,11 @@
"import numpy as np\n",
"from tqdm import tqdm\n",
"import PyAstronomy.pyasl as pyasl\n",
"import astropy.constants as const\n",
"from astropy import constants as const\n",
" \n",
"import eniric\n",
"#eniric.cache[\"location\"] = None # Disable caching for these tests\n",
"eniric.cache[\"location\"] = \".joblib\" # Enable caching\n",
"#eniric.config.cache[\"location\"] = None # Disable caching for these tests\n",
"eniric.config.cache[\"location\"] = \".joblib\" # Enable caching\n",
"\n",
"from eniric.broaden import rotational_convolution, resolution_convolution\n",
"from eniric.utilities import band_limits, load_aces_spectrum, wav_selector\n",
Expand Down
2 changes: 1 addition & 1 deletion eniric/Qcalculator.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
import warnings
from typing import Optional, Tuple, Union

import astropy.constants as const
from astropy import constants as const
import astropy.units as u
import numpy as np
from astropy.units.quantity import Quantity
Expand Down
60 changes: 21 additions & 39 deletions eniric/__init__.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,32 @@
__version__ = "0.6-beta"
__all__ = [
"atmosphere",
"io_module",
"Qcalculator",
"resample",
"snr_normalization",
"utilities",
]

# Read the users config.yaml file.
# If it doesn't exist, print a useful help message
__version__ = "1.0rc1"

import os
import warnings

import yaml

try:
with open("config.yaml") as f:
config = yaml.safe_load(f)
except FileNotFoundError as e:
default = __file__[:-11] + "config.yaml"
import warnings
from ._config import Config

if os.path.exists("config.yaml"):
config = Config("config.yaml")
else:
base_dir = os.path.dirname(__file__)
default = os.path.join(base_dir, "config.yaml")
warnings.warn(
"Using the default config.yaml file located at {0}. "
"This is likely NOT what you want. Please create a similar "
"'config.yaml' file in your current working directory.".format(default),
UserWarning,
)
with open(default) as f:
config = yaml.safe_load(f)
config = Config(default)

# Read the YAML variables into package-level dictionaries to be used by the other programs.
name = config["name"]
paths = config["paths"]
bands = config["bands"]
custom_bands = config["custom_bands"]
atmmodel = config["atmmodel"]
cache = config["cache"]

# Turn list into path
for key, value in paths.items():
if isinstance(value, list):
paths[key] = os.path.join(*value)

if (cache["location"] is None) or (cache["location"] == "None"):
cache["location"] = None # Disables caching
else:
cache["location"] = os.path.join(*cache["location"])
__all__ = [
"atmosphere",
"broaden",
"config",
"corrections",
"legacy",
"io_module",
"Qcalculator",
"resample",
"snr_normalization",
"utilities",
]
48 changes: 48 additions & 0 deletions eniric/_config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import yaml
import os


class Config:
"""
This is a class to allow lazy-loading of config files. This means that the atributes are only read from the file
when accessed in the code, allowing for changes in the file without having to restart the python instance
"""

def __init__(self, filename):
self.filename = filename
# Format string for saving/reading orders
self.specfmt = "s{}_o{}"

def __getitem__(self, item):
with open(self.filename) as f:
base = yaml.safe_load(f)
return base[item]

def __getattribute__(self, item):
# Short circuit on filename or else infinite recursion
if item == 'filename':
return super().__getattribute__(item)
with open(self.filename) as f:
base = yaml.safe_load(f)
if item == 'paths':
paths = base['paths']
for key, value in paths.items():
if isinstance(value, list):
paths[key] = os.path.join(*value)
return paths
elif item == 'bands':
return base['bands']
elif item == 'custom_bands':
return base['custom_bands']
elif item == 'atmmodel':
return base['atmmodel']
elif item == 'cache':
cache = base['cache']
if (cache["location"] is None) or (cache["location"] == "None"):
cache["location"] = None # Disables caching
else:
if isinstance(cache["location"], list):
cache["location"] = os.path.join(*cache["location"])
return cache
else:
return super().__getattribute__(item)
6 changes: 3 additions & 3 deletions eniric/atmosphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,8 @@ def from_band(cls, band: str, bary: bool = False):

extension = "_bary.dat" if bary else ".dat"
atmmodel = join(
eniric.paths["atmmodel"],
"{0}_{1}{2}".format(eniric.atmmodel["base"], band, extension),
eniric.config.paths["atmmodel"],
"{0}_{1}{2}".format(eniric.config.atmmodel["base"], band, extension),
)

try:
Expand All @@ -146,7 +146,7 @@ def from_band(cls, band: str, bary: bool = False):
)
)
full_model = join(
eniric.paths["atmmodel"], "{0}.dat".format(eniric.atmmodel["base"])
eniric.config.paths["atmmodel"], "{0}.dat".format(eniric.config.atmmodel["base"])
)
atm = cls.from_file(full_model)

Expand Down
6 changes: 3 additions & 3 deletions eniric/broaden.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

import multiprocess as mprocess
import numpy as np
from astropy.constants import c
from astropy import constants as const
from joblib import Memory
from multiprocess import pool
from numpy.core.multiarray import ndarray
Expand All @@ -21,9 +21,9 @@
from eniric.utilities import band_selector, mask_between, wav_selector

# Cache convolution results.
memory = Memory(location=eniric.cache["location"], verbose=0)
memory = Memory(location=eniric.config.cache["location"], verbose=0)

c_kmps = c.value / 1000
c_kmps = const.c.to('km/s').value
num_procs_minus_1 = os.cpu_count() - 1


Expand Down
2 changes: 1 addition & 1 deletion eniric/obsolete/TAPASproc.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def read_allfiles(mask_limit=0.02):
mask.append(0.0)

write_4col_eeed(
os.path.join(outdir, "{0}_visible.dat".format(eniric.atmmodel["base"])),
os.path.join(outdir, "{0}_visible.dat".format(eniric.config.atmmodel["base"])),
wav,
mean_flux,
std_flux,
Expand Down
2 changes: 1 addition & 1 deletion eniric/obsolete/make_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from eniric.obsolete.prepare_data import main as prepare_data

if __name__ == "__main__":
print("Eniric paths: {}".format(eniric.paths))
print("Eniric paths: {}".format(eniric.config.paths))

# subprocess.call("python scripts/prepare_data.py -s M0 M3 M6 M9 -l 4.50 -m 0.0", shell=True)
prepare_data(
Expand Down
18 changes: 9 additions & 9 deletions eniric/obsolete/nIR_precision.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
rc("text", usetex=True) # set stuff for latex usage

ref_choices = ["SELF"]
ref_choices.extend(eniric.bands["all"])
ref_choices.extend(eniric.config.bands["all"])


def _parser():
Expand All @@ -41,7 +41,7 @@ def _parser():
"--bands",
type=str,
default="J",
choices=eniric.bands["all"],
choices=eniric.config.bands["all"],
help="Wavelength bands to select. Default=J.",
nargs="+",
)
Expand Down Expand Up @@ -82,7 +82,7 @@ def main(bands="J", use_unshifted=False, save=False, snr=100, ref_band="J"):
Save results to file.
"""
os.makedirs(eniric.paths["precision"], exist_ok=True)
os.makedirs(eniric.config.paths["precision"], exist_ok=True)

spectral_types = ["M0", "M3", "M6", "M9"]
if "ALL" in bands:
Expand Down Expand Up @@ -121,7 +121,7 @@ def main(bands="J", use_unshifted=False, save=False, snr=100, ref_band="J"):
# Save precision results
if save:
output_filename = os.path.join(
eniric.paths["precision"],
eniric.config.paths["precision"],
"precision_results_2017_ref_band-{0}_snr-{1}.dat".format(ref_band, snr),
)
ids = []
Expand Down Expand Up @@ -204,8 +204,8 @@ def calculate_prec(

if use_unshifted:
atmmodel = os.path.join(
eniric.paths["atmmodel"],
"{0}_{1}.dat".format(eniric.atmmodel["base"], band),
eniric.config.paths["atmmodel"],
"{0}_{1}.dat".format(eniric.config.atmmodel["base"], band),
)
print("Reading atmospheric model...")
atm = Atmosphere.from_file(atmmodel)
Expand Down Expand Up @@ -235,8 +235,8 @@ def calculate_prec(
mask_atm = barycenter_shift(wav_atm, mask_atm, rv_offset=rv_offset)
else:
shifted_atmmodel = os.path.join(
eniric.paths["atmmodel"],
"{0}_{1}_bary.dat".format(eniric.atmmodel["base"], band),
eniric.config.paths["atmmodel"],
"{0}_{1}_bary.dat".format(eniric.config.atmmodel["base"], band),
)
print("Reading pre-doppler-shifted atmospheric model...")
atm = Atmosphere.from_file(shifted_atmmodel)
Expand Down Expand Up @@ -274,7 +274,7 @@ def calculate_prec(
# print("Working on "+file_to_read+".")
try:
wav_stellar, flux_stellar = io.pdread_2col(
os.path.join(eniric.paths["resampled"], file_to_read)
os.path.join(eniric.config.paths["resampled"], file_to_read)
)
except FileNotFoundError:
# Turn list of strings into strings without symbols ["J", "K"] -> J K
Expand Down
10 changes: 5 additions & 5 deletions eniric/obsolete/nIR_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def _parser():
"--band",
type=str,
default="ALL",
choices=eniric.bands["all"],
choices=eniric.config.bands["all"],
help="Wavelength band to select",
nargs="+",
)
Expand Down Expand Up @@ -94,7 +94,7 @@ def main(
Multiple values of startype, vsini, resolution, band, and sample_rate can
be provided.
Read files from eniric.paths["phoenix_dat"]"
Read files from eniric.config.paths["phoenix_dat"]"
Parameters
----------
Expand Down Expand Up @@ -129,12 +129,12 @@ def main(

start_time = dt.now()

phoenix_path = eniric.paths["phoenix_dat"]
phoenix_path = eniric.config.paths["phoenix_dat"]

results_dir = eniric.paths["results"]
results_dir = eniric.config.paths["results"]
os.makedirs(results_dir, exist_ok=True)

resampled_dir = eniric.paths["resampled"]
resampled_dir = eniric.config.paths["resampled"]
os.makedirs(resampled_dir, exist_ok=True)

if not normalize:
Expand Down
6 changes: 3 additions & 3 deletions eniric/obsolete/nIRanalysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@
# set stuff for latex usage
rc("text", usetex=True)

data_rep = eniric.paths["phoenix_dat"]
results_dir = eniric.paths["results"]
resampled_dir = eniric.paths["resampled"]
data_rep = eniric.config.paths["phoenix_dat"]
results_dir = eniric.config.paths["results"]
resampled_dir = eniric.config.paths["resampled"]

# models form PHOENIX-ACES
M0_ACES = data_rep + "lte03900-4.50-0.0.PHOENIX-ACES-AGSS-COND-2011-HiRes_wave.dat"
Expand Down
4 changes: 2 additions & 2 deletions eniric/obsolete/prepare_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,11 @@ def main(
"""
if data_dir is None:
data_dir = eniric.paths["phoenix_dat"]
data_dir = eniric.config.paths["phoenix_dat"]
os.makedirs(data_dir, exist_ok=True)

if phoenix_dir is None:
phoenix_dir = eniric.paths["phoenix_raw"]
phoenix_dir = eniric.config.paths["phoenix_raw"]

# Get Phoenix wavelength data
wavelength_file = "WAVE_PHOENIX-ACES-AGSS-COND-2011.fits"
Expand Down
Loading

0 comments on commit eeff318

Please sign in to comment.