Skip to content

Commit

Permalink
added supplement versions table
Browse files Browse the repository at this point in the history
  • Loading branch information
javierggt committed Feb 11, 2021
1 parent eb5a369 commit 8c21663
Show file tree
Hide file tree
Showing 4 changed files with 104 additions and 9 deletions.
4 changes: 4 additions & 0 deletions agasc/scripts/update_obs_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@
from astropy import table
import pyyaks.logger

import agasc
from agasc.supplement.magnitudes import star_obs_catalogs
from agasc.supplement.utils import save_version

logger = logging.getLogger('agasc.supplement')

Expand Down Expand Up @@ -76,6 +78,7 @@ def add_bad_star(bad_star_ids, bad_star_source, suppl_file, dry_run):
logger.info('and installation.')
if not dry_run:
dat.write(str(suppl_file), format='hdf5', path='bad', append=True, overwrite=True)
save_version(suppl_file, obs=agasc.__version__)


def update_obs_table(filename, obs_status_override, dry_run=False):
Expand Down Expand Up @@ -138,6 +141,7 @@ def update_obs_table(filename, obs_status_override, dry_run=False):

if not dry_run:
obs_status.write(str(filename), format='hdf5', path='obs', append=True, overwrite=True)
save_version(filename, obs=agasc.__version__)
else:
logger.info('dry run, not saving anything')

Expand Down
3 changes: 3 additions & 0 deletions agasc/supplement/magnitudes/update_mag_supplement.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
from astropy import table
from astropy import time, units as u

import agasc
from agasc.supplement.magnitudes import star_obs_catalogs, mag_estimate, mag_estimate_report as msr
from agasc.supplement.utils import save_version
from cxotime import CxoTime


Expand Down Expand Up @@ -283,6 +285,7 @@ def update_supplement(agasc_stats, filename, include_all=True):
if 'mags' in h5.root:
h5.remove_node('/mags')
h5.create_table('/', 'mags', outliers)
save_version(filename, mags=agasc.__version__)

return new_stars, updated_stars

Expand Down
71 changes: 67 additions & 4 deletions agasc/supplement/utils.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
# Licensed under a 3-clause BSD style license - see LICENSE.rst
from pathlib import Path
import logging
import warnings

from ska_helpers.utils import lru_cache_timed
import tables
from astropy.table import Table
from cxotime import CxoTime
from astropy.table import Table, Column

from ..paths import SUPPLEMENT_FILENAME, default_agasc_dir


__all__ = ['get_supplement_table']
__all__ = ['get_supplement_table', 'save_version']


logger = logging.getLogger('agasc.supplement')


@lru_cache_timed(timeout=3600)
Expand Down Expand Up @@ -43,8 +48,9 @@ def get_supplement_table(name, agasc_dir=None, as_dict=False):
"""
agasc_dir = default_agasc_dir() if agasc_dir is None else Path(agasc_dir)

if name not in ('mags', 'bad', 'obs'):
raise ValueError("table name must be one of 'mags', 'bad', or 'obs'")
if name not in ('mags', 'bad', 'obs', 'last_updated', 'versions'):
raise ValueError("table name must be one of "
"'mags', 'bad', 'obs', 'last_updated' or 'versions'")

supplement_file = agasc_dir / SUPPLEMENT_FILENAME
with tables.open_file(supplement_file) as h5:
Expand Down Expand Up @@ -73,3 +79,60 @@ def get_supplement_table(name, agasc_dir=None, as_dict=False):
out = Table(dat)

return out


def _load_or_create(filename, table_name):
try:
table = Table.read(filename, format='hdf5', path=table_name)
except OSError as e:
logger.debug(f'Creating agasc supplement table "{table_name}"')
if not filename.exists() or str(e) == f'Path {table_name} does not exist':
table = Table()
else:
raise
return table


def save_version(filename, **kwargs):
"""Save the version of a supplement table to the "versions" table.
Along with the version, the time of update is also added to another table called "last_updated"
Example usage::
from agasc.supplement.utils import save_version
save_version('agasc_supplement.h5', mags='4.10.2')
The different versions can be retrieved from the default supplement::
from agasc.supplement.utils import get_supplement_table
versions = get_supplement_table('versions')
:param filename: pathlib.Path
:param kwargs: dict
A dictionary of table name/version pairs,
"""
import agasc
version_dtype = '<U32'
filename = Path(filename)

versions = _load_or_create(filename, 'versions')
last_updated = _load_or_create(filename, 'last_updated')

time = CxoTime().iso
kwargs.update({'supplement': agasc.__version__})
for key, value in kwargs.items():
pass
if key not in versions.colnames:
logger.debug(f'Adding "{key}" to agasc supplement "versions" table')
versions[key] = Column(data=['unknown'], dtype=version_dtype)
if key not in last_updated.colnames:
logger.debug(f'Adding "{key}" to agasc supplement "last_updated" table')
last_updated[key] = Column(data=['unknown'], dtype=version_dtype)
versions[key][:] = value
last_updated[key][:] = time

versions.write(str(filename), format='hdf5', path='versions',
append=True, overwrite=True)
last_updated.write(str(filename), format='hdf5', path='last_updated',
append=True, overwrite=True)
35 changes: 30 additions & 5 deletions agasc/tests/test_obs_status.py
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,13 @@ def test_update_obs_blank_slate(monkeypatch):
monkeypatch.setattr(star_obs_catalogs, 'STARS_OBS', STARS_OBS)

def mock_write(fname, *args, **kwargs):
obs_status = {
(r['obsid'], r['agasc_id']): {'status': r['status'], 'comments': r['comments']}
for r in args[0]
}
assert obs_status == TEST_DATA[fname]['obs']
assert kwargs['path'] in ['versions', 'last_updated', 'obs']
if kwargs['path'] == 'obs':
obs_status = {
(r['obsid'], r['agasc_id']): {'status': r['status'], 'comments': r['comments']}
for r in args[0]
}
assert obs_status == TEST_DATA[fname]['obs']

for filename in TEST_YAML:
monkeypatch.setattr(table.Table,
Expand Down Expand Up @@ -434,3 +436,26 @@ def recreate_test_supplement():
update_obs_status.update_obs_table(TEST_DATA_DIR / 'agasc_supplement.h5',
obs_status_override,
dry_run=False)


def test_save_version(monkeypatch):
# this test takes the following dictionary and passes it to save_version
# it then checks that a corresponding astropy table with the right structure is created and its
# write method is called
versions = dict(obs='v2.3', mags='v1.1')

def mock_write(*args, **kwargs):
assert 'format' in kwargs and kwargs['format'] == 'hdf5'
assert 'path' in kwargs
assert kwargs['path'] in ['last_updated', 'versions']
assert len(args[0]) == 1
assert 'supplement' in args[0].colnames
if kwargs['path'] == 'versions':
for k, v in versions.items():
assert k in args[0].colnames
assert args[0][k][0] == versions[k]

monkeypatch.setattr(table.Table, 'write', mock_write)

from agasc.supplement.utils import save_version
save_version('test_save_version.h5', **versions)

0 comments on commit 8c21663

Please sign in to comment.