Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
10 changes: 6 additions & 4 deletions .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ jobs:
python -m pip install pytest
python -m pip install git+https://github.com/desihub/desiutil.git@${DESIUTIL_VERSION}#egg=desiutil
python -m pip install -r requirements.txt
curl -L https://github.com/desihub/redrock-templates/archive/${RR_TEMPLATE_VER}.tar.gz | tar -x -z -v -C py/redrock
/bin/mv py/redrock/redrock-templates-${RR_TEMPLATE_VER} py/redrock/templates
PYTHONPATH=$(pwd)/py:$PYTHONPATH PATH=$(pwd)/bin:$PATH install_redrock_templates -v ${RR_TEMPLATE_VER}
### curl -L https://github.com/desihub/redrock-templates/archive/${RR_TEMPLATE_VER}.tar.gz | tar -x -z -v -C py/redrock
### /bin/mv py/redrock/redrock-templates-${RR_TEMPLATE_VER} py/redrock/data/templates

- name: Run the test
run: pytest
Expand Down Expand Up @@ -71,8 +72,9 @@ jobs:
python -m pip install pytest pytest-cov coveralls
python -m pip install git+https://github.com/desihub/desiutil.git@${DESIUTIL_VERSION}#egg=desiutil
python -m pip install -r requirements.txt
curl -L https://github.com/desihub/redrock-templates/archive/${RR_TEMPLATE_VER}.tar.gz | tar -x -z -v -C py/redrock
/bin/mv py/redrock/redrock-templates-${RR_TEMPLATE_VER} py/redrock/templates
PYTHONPATH=$(pwd)/py:$PYTHONPATH PATH=$(pwd)/bin:$PATH install_redrock_templates -v ${RR_TEMPLATE_VER}
### curl -L https://github.com/desihub/redrock-templates/archive/${RR_TEMPLATE_VER}.tar.gz | tar -x -z -v -C py/redrock
### /bin/mv py/redrock/redrock-templates-${RR_TEMPLATE_VER} py/redrock/data/templates
- name: Run the test with coverage
run: pytest --cov
- name: Coveralls
Expand Down
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ __pycache__
htmlcov
.coverage

# in-place installed templates
py/redrock/data/templates

# Sphinx
doc/api
doc/_build
Expand Down
74 changes: 74 additions & 0 deletions bin/install_redrock_templates
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
#!/usr/bin/env python

"""
Install Redrock templates into the code directory so that they can be used
without $RR_TEMPLATE_DIR being set
"""

import os
import sys
import importlib
import subprocess
import argparse

import redrock.templates

def main():
parser = argparse.ArgumentParser(description="install Redrock templates")
parser.add_argument('-v', '--version', default='main',
help="Template version/tag/branch to install (default %(default)s)")
parser.add_argument('--template-url', default='https://github.com/desihub/redrock-templates',
help="URL of repository with redrock templates (default %(default)s)")
parser.add_argument('-o', '--outdir',
help="write template files into this directory instead of into the code installation")
args = parser.parse_args()

#- Don't get confused by previously defined $RR_TEMPLATE_DIR
if 'RR_TEMPLATE_DIR' in os.environ:
print('WARNING: ignoring $RR_TEMPLATE_DIR; use --outdir to force a specific location if needed')
del os.environ['RR_TEMPLATE_DIR']

#- Check if the output directory already exists
templatedir = redrock.templates.get_template_dir(args.outdir)
if os.path.exists(templatedir):
print(f'ERROR: {templatedir} already exists; remove then rerun')
return 1

#- GitHub doesn't require '.git', but other git servers might
if not args.template_url.endswith('.git'):
args.template_url += '.git'

#- full clone for main, depth 1 for other tags/branches
if args.version == 'main':
gitcmd = f"git clone {args.template_url} {templatedir}"
else:
gitcmd = f"git clone --depth 1 --branch {args.version} {args.template_url} {templatedir}"

#- Proceed with installing
print(f'Installing redrock-templates/{args.version} to {templatedir}')
print(gitcmd)
p = subprocess.run(gitcmd.split(), capture_output=True)
if p.returncode != 0:
if len(p.stdout)>0:
print(p.stdout.decode()) #- .decode because it is bytes not str
if len(p.stderr)>0:
print(p.stderr.decode())

print('ERROR: failed to install redrock-templates/{args.version}')
return p.returncode

#- Check that the installation worked
print('Checking that the installed templates work')
if args.outdir is not None:
os.environ['RR_TEMPLATE_DIR'] = templatedir
templates = redrock.templates.load_templates()
assert len(templates) > 0
print('SUCCESS')

if args.outdir is not None:
print(f'Remember to set $RR_TEMPLATE_DIR={templatedir} before running Redrock')

return 0

if __name__ == '__main__':
sys.exit(main())
6 changes: 5 additions & 1 deletion py/redrock/external/desi.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

from ..targets import (Spectrum, Target, DistTargets)

from ..templates import load_dist_templates
from ..templates import load_dist_templates, get_template_dir

from ..results import write_zscan

Expand Down Expand Up @@ -79,6 +79,10 @@ def _get_header(templates, archetypes=None, spec_header=None):
if key in os.environ:
setdep(header, key, os.environ[key])

setdep(header, 'RR_TEMPLATE_DIR', get_template_dir())
if 'RR_ARCHETYPE_DIR' in os.environ:
setdep(header, 'RR_ARCHETYPE_DIR', os.environ['RR_ARCHETYPE_DIR'])

if spec_header is not None:
for key in ('SPGRP', 'SPGRPVAL', 'TILEID', 'SPECTRO', 'PETAL',
'NIGHT', 'EXPID', 'HPXPIXEL', 'HPXNSIDE', 'HPXNEST',
Expand Down
37 changes: 21 additions & 16 deletions py/redrock/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
from glob import glob
import os
import traceback
import importlib
import pathlib

import numpy as np
from astropy.io import fits
Expand All @@ -32,7 +34,7 @@ class Template(object):

Args:
filename (str): the path to the template file, either absolute or
relative to the RR_TEMPLATE_DIR environment variable.
relative to the template path ($RR_TEMPLATE_DIR or installed with code)

"""
def __init__(self, filename=None, spectype=None, redshifts=None,
Expand All @@ -47,7 +49,7 @@ def __init__(self, filename=None, spectype=None, redshifts=None,
if os.path.exists(filename):
fx = fits.open(filename, memmap=False)
else:
xfilename = os.path.join(os.getenv('RR_TEMPLATE_DIR'), filename)
xfilename = os.path.join(get_template_dir(), filename)
if os.path.exists(xfilename):
fx = fits.open(xfilename, memmap=False)
else:
Expand Down Expand Up @@ -295,30 +297,33 @@ def make_fulltype(spectype, subtype):

return fulltype

def get_template_dir(template_path=None):
"""Return template_path is set, otherwise $RR_TEMPLATE_DIR, or (code)/data/templates"""
if template_path is None:
if 'RR_TEMPLATE_DIR' in os.environ:
template_path = os.environ['RR_TEMPLATE_DIR']
else:
template_path = str(importlib.resources.files('redrock').joinpath('data/templates'))

return template_path


def find_templates(template_path=None):
"""Return list of Redrock template files

`template_path` can be one of 5 things:

* None (use $RR_TEMPLATE_DIR instead)
* None (use get_template_dir())
* list/tuple/set/array of template files
* path to directory containing template files
* path to single template file to use
* path to text file listing which template files to use

Returns list of template files to use
"""
if template_path is None:
if 'RR_TEMPLATE_DIR' in os.environ:
template_path = os.environ['RR_TEMPLATE_DIR']
else:
thisdir = os.path.dirname(__file__)
tempdir = os.path.join(os.path.abspath(thisdir), 'templates')
if os.path.exists(tempdir):
template_path = tempdir

if template_path is None:
raise IOError("ERROR: can't find template_path, $RR_TEMPLATE_DIR, or {rrcode}/templates/")
template_path = get_template_dir(template_path)
if isinstance(template_path, (str, pathlib.PosixPath)) and not os.path.exists(template_path):
raise ValueError("ERROR: can't find template_path, $RR_TEMPLATE_DIR, or (rrcode)/data/templates/")
else:
print(f'DEBUG: Reading templates from {template_path}')

Expand Down Expand Up @@ -405,7 +410,7 @@ def header2templatefiles(hdr, template_dir=None):

#- preprend template directory
if template_dir is None:
template_dir = os.environ['RR_TEMPLATE_DIR']
template_dir = get_template_dir()

filenames = [os.path.join(template_dir, fn) for fn in filenames]

Expand Down Expand Up @@ -442,7 +447,7 @@ def load_templates(template_path=None, zscan_galaxy=None, zscan_star=None, zscan
`template_path` is list of template file paths, or path to provide to
find_templates, i.e. a path to a directory with templates, a path to
a text file containing a list of templates, a path to a single template
file, or None to use $RR_TEMPLATE_DIR instead.
file, or None to use $RR_TEMPLATE_DIR or templates installed with code instead.

Returns: list or dict of Template objects

Expand Down
26 changes: 25 additions & 1 deletion py/redrock/test/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
from shutil import rmtree
import unittest
from uuid import uuid1
import importlib.resources
import numpy as np

from .. import utils as rrutils
from ..templates import DistTemplate, find_templates, load_templates, load_dist_templates
from ..templates import DistTemplate, find_templates, load_templates, load_dist_templates, get_template_dir

from . import util as testutil

Expand All @@ -19,6 +20,7 @@ def setUpClass(cls):
cls.testDir = tempfile.mkdtemp()
cls.testfile = os.path.join(cls.testDir, 'test-{uuid}.h5'.format(uuid=uuid1()))
cls.starting_cwd = os.getcwd()
cls.orig_rr_template_dir = os.getenv('RR_TEMPLATE_DIR')

# create dummy template files in a separate dir
cls.testTemplateDir = tempfile.mkdtemp()
Expand Down Expand Up @@ -64,6 +66,28 @@ def setUp(self):
#- make sure we are in starting directory every time
os.chdir(self.starting_cwd)

#- reset $RR_TEMPLATE_DIR
if self.orig_rr_template_dir is None:
if 'RR_TEMPLATE_DIR' in os.environ:
del os.environ['RR_TEMPLATE_DIR']
else:
os.environ['RR_TEMPLATE_DIR'] = self.orig_rr_template_dir

def test_get_template_dir(self):
tdir = get_template_dir('blat')
self.assertEqual(tdir, 'blat')

tdir = get_template_dir('/blat/foo')
self.assertEqual(tdir, '/blat/foo')

os.environ['RR_TEMPLATE_DIR'] = '/biz/bat'
tdir = get_template_dir()
self.assertEqual(tdir, '/biz/bat')

del os.environ['RR_TEMPLATE_DIR']
tdir = get_template_dir()
self.assertEqual(tdir, str(importlib.resources.files('redrock').joinpath('data/templates')))

### @unittest.skipIf('RR_TEMPLATE_DIR' not in os.environ, '$RR_TEMPLATE_DIR not set')
def test_find_templates(self):
templates = find_templates()
Expand Down
Loading