Skip to content

Commit

Permalink
Extend validation for products
Browse files Browse the repository at this point in the history
  • Loading branch information
sergiopasra committed Jun 10, 2020
1 parent 1a0ea28 commit 72eef3e
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 9 deletions.
60 changes: 51 additions & 9 deletions megaradrp/core/recipe.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#
# Copyright 2011-2019 Universidad Complutense de Madrid
# Copyright 2011-2020 Universidad Complutense de Madrid
#
# This file is part of Megara DRP
#
Expand All @@ -21,14 +21,7 @@


class MegaraBaseRecipe(BaseRecipe):
"""Base clase for all MEGARA Recipes
Parameters
----------
intermediate_results : bool, optional
If True, save intermediate results of the Recipe
"""Base class for all MEGARA Recipes
Attributes
----------
Expand All @@ -45,6 +38,55 @@ class MegaraBaseRecipe(BaseRecipe):
logger = logging.getLogger('numina.recipes.megara')
datamodel = MegaraDataModel()

def validate_input(self, recipe_input):
""""Validate the input of the recipe"""

import numina.types.multitype
# print('ATTRS', recipe_input.attrs())
# print('STORED', recipe_input.stored())
# print('tag_names', recipe_input.tag_names())

# Find reference tags
ref_tags = {}
for key, val in recipe_input.attrs().items():
if key == 'obresult':
ref_tags = val.tags
break

# super(MegaraBaseRecipe, self).validate_input(recipe_input)
# check all the rest against reference tags
stored = recipe_input.stored()
attrs = recipe_input.attrs()
val_results = []
for key, val in attrs.items():
if val is None:
continue
# If we evaluate query_expr with ref_tags and the tags from the object
# The result must be true
req = stored[key]
rtype = req.type
# TODO: develop a method to select images that are valid
# for different filters or insmodes; perhaps a ANY keyword
tags = rtype.extract_tags(val)
# FIXME: this should be handled by the type, not with a special case
if isinstance(rtype, numina.types.multitype.MultiType):
# get query from first node
query_expr = rtype.node_type[0].query_expr
else:
query_expr = rtype.query_expr

q2 = query_expr.fill_placeholders(**ref_tags)
self.logger.debug('type %s with tags %s, expr %s', rtype, tags, q2)
is_valid = q2.eval(**tags)
if not is_valid:
val_results.append((key, q2, tags, ref_tags))
msg = 'invalid {} with expression {} and tags {} and obs_tags {}'
for key, q2, tags, ref_tags in val_results:
self.logger.error(msg.format(key, q2, tags, ref_tags))
if val_results:
raise ValueError('Validation error', val_results)


def run_qc(self, recipe_input, recipe_result):
"""Run Quality Control checks."""
recipe_result.qc = QC.GOOD
Expand Down
15 changes: 15 additions & 0 deletions megaradrp/ntypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,21 @@ def validate_hdulist(self, hdulist):
checker = valid.check_as_datatype(self.DATATYPE)
return checker(hdulist)

def extract_tags(self, obj):
"""Extract tags from serialized file"""

objl = self.convert(obj)
ext = self.datamodel.extractor_map['fits']
tags = {}

if objl:
with objl.open() as hdulist:
for field in self.names_t:
tags[field] = ext.extract(field, hdulist)
return tags
else:
return {}


class ProcessedFrame(MegaraFrame):
"""A processed frame"""
Expand Down
1 change: 1 addition & 0 deletions megaradrp/products/tracemap.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@


class GeometricTrace(GeometricAperture):
"""Representation of fiber trace on the image"""
def __init__(self, fibid, boxid, start, stop, fitparms=None):
super(GeometricTrace, self).__init__(fibid, boxid, start, stop)
self.fitparms = fitparms if fitparms is not None else []
Expand Down

0 comments on commit 72eef3e

Please sign in to comment.