Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# If this is part of a combined transform, some of the inputs
# may be NaNs.
# Set NaNs to the ``_no_label`` value
mapper_keys = list(self.mapper.keys())
# Loop over the keys in mapper and compare to inputs.
# Find the indices where they are within ``atol``
# and evaluate the transform to get the corresponding label.
for key in mapper_keys:
ind = np.isclose(key, keys, atol=self._atol)
inputs = [a[ind] for a in args]
res[ind] = self.mapper[key](*inputs)
res.shape = shape
return res
class LabelMapperRange(_LabelMapper):
"""
The structure this class uses maps a range of values to a transform.
Given an input value it finds the range the value falls in and returns
the corresponding transform. When evaluated the transform returns a label.
Example: Pick a transform based on wavelength range.
For an IFU observation, the keys are (lambda_min, lambda_max) tuples
and values are transforms which return a label corresponding to a slice.
Parameters
----------
inputs : tuple of str
Names for the inputs, e.g. ('alpha', 'beta', 'lambda')
mapper : dict
Maps tuples of length 2 to transforms.
... 4: [[772, 990], [2047, 990], [2047, 1042], [772, 1042], [772, 990]]
... }
>>> mapper = LabelMapperArray.from_vertices((2400, 2400), regions)
"""
labels = np.array(list(regions.keys()))
mask = np.zeros(shape, dtype=labels.dtype)
for rid, vert in regions.items():
pol = region.Polygon(rid, vert)
mask = pol.scan(mask)
return cls(mask)
class LabelMapperDict(_LabelMapper):
"""
Maps a number to a transform, which when evaluated returns a label.
Use case: inverse transforms of an IFU.
For an IFU observation, the keys are constant angles (corresponding to a slice)
and values are transforms which return a slice number.
Parameters
----------
inputs : tuple of str
Names for the inputs, e.g. ('alpha', 'beta', lam')
mapper : dict
Maps key values to transforms.
inputs_mapping : `~astropy.modeling.mappings.Mapping`
An optional Mapping model to be prepended to the LabelMapper
def outputs(self, val):
"""
The name(s) of the output variable(s).
"""
self._outputs = val
@property
def n_inputs(self):
return self._n_inputs
@property
def n_outputs(self):
return self._n_outputs
class LabelMapper(_LabelMapper):
"""
Maps inputs to regions. Returns the region labels corresponding to the inputs.
Labels are strings or numbers which uniquely identify a location.
For example, labels may represent slices of an IFU or names of spherical polygons.
Parameters
----------
mapper : `~astropy.modeling.core.Model`
A function which returns a region.
no_label : str or int
"" or 0
A return value for a location which has no corresponding label.
inputs_mapping : `~astropy.modeling.mappings.Mapping` or tuple
An optional Mapping model to be prepended to the LabelMapper
with the purpose to filter the inputs or change their order.
def mapper(self):
return self._mapper
@property
def inputs_mapping(self):
return self._inputs_mapping
@property
def no_label(self):
return self._no_label
def evaluate(self, *args):
raise NotImplementedError("Subclasses should implement this method.")
class LabelMapperArray(_LabelMapper):
"""
Maps array locations to labels.
Parameters
----------
mapper : ndarray
An array of integers or strings where the values
correspond to a label in `~gwcs.selector.RegionsSelector` model.
For pixels for which the transform is not defined the value should
be set to 0 or " ".
inputs_mapping : `~astropy.modeling.mappings.Mapping`
An optional Mapping model to be prepended to the LabelMapper
with the purpose to filter the inputs or change their order
so that the output of it is (x, y) values to index the array.
name : str
def __init__(self, inputs, mapper, no_label=np.nan, inputs_mapping=None, name=None, **kwargs):
self._no_label = no_label
self._inputs = inputs
self._n_inputs = len(inputs)
self._outputs = tuple(['x{0}'.format(ind) for ind in list(range(mapper.n_outputs))])
if isinstance(inputs_mapping, tuple):
inputs_mapping = astmodels.Mapping(inputs_mapping)
elif inputs_mapping is not None and not isinstance(inputs_mapping, astmodels.Mapping):
raise TypeError("inputs_mapping must be an instance of astropy.modeling.Mapping.")
self._inputs_mapping = inputs_mapping
self._mapper = mapper
self._input_units_strict = {key: False for key in self._inputs}
self._input_units_allow_dimensionless = {key: False for key in self._inputs}
super(_LabelMapper, self).__init__(name=name, **kwargs)
self.outputs = ('label',)