Skip to content

Commit

Permalink
Fix tying for Visibilities
Browse files Browse the repository at this point in the history
  • Loading branch information
samaloney committed May 24, 2024
1 parent c6d92f7 commit 21cb514
Showing 1 changed file with 56 additions and 54 deletions.
110 changes: 56 additions & 54 deletions xrayvision/visibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

__all__ = ["Visibility", "Visibilities", "VisMeta", "VisibilitiesABC", "VisMetaABC"]

from astropy.units import Quantity

_E_RANGE_KEY = "spectral_range"
_T_RANGE_KEY = "time_range"
_OBS_COORD_KEY = "observer_coordinate"
Expand Down Expand Up @@ -133,6 +135,60 @@ def phase_uncertainty(self) -> Union[Iterable[apu.Quantity[apu.deg]], None]:
"""


class VisMeta(VisMetaABC, dict):
"""
A class for holding Visibility-specific metadata.
Parameters
----------
meta: `dict`
A dictionary of the metadata
"""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Check controlled/expected inputs are of correct type and units.
controled_args = (
(_OBS_COORD_KEY, SkyCoord),
(_E_RANGE_KEY, apu.Quantity, apu.keV, apu.spectral()),
(_T_RANGE_KEY, Time),
)
for args in controled_args:
self._check_input_type_and_unit(*args)

def _check_input_type_and_unit(self, key, key_type, unit=None, equivalencies=None):
value = self.get(key, None)
if not isinstance(value, (key_type, type(None))):
raise KeyError(f"Inputs must include a key, '{key}', that gives a {key_type}.")

Check warning on line 162 in xrayvision/visibility.py

View check run for this annotation

Codecov / codecov/patch

xrayvision/visibility.py#L162

Added line #L162 was not covered by tests
if unit is not None and value is not None and not value.unit.is_equivalent(unit, equivalencies=equivalencies):
raise ValueError(f"'{key}' must have angular units.")

Check warning on line 164 in xrayvision/visibility.py

View check run for this annotation

Codecov / codecov/patch

xrayvision/visibility.py#L164

Added line #L164 was not covered by tests

@property
def observer_coordinate(self):
return self.get(_OBS_COORD_KEY, None)

Check warning on line 168 in xrayvision/visibility.py

View check run for this annotation

Codecov / codecov/patch

xrayvision/visibility.py#L168

Added line #L168 was not covered by tests

@property
def spectral_range(self):
return self.get(_E_RANGE_KEY, None)

Check warning on line 172 in xrayvision/visibility.py

View check run for this annotation

Codecov / codecov/patch

xrayvision/visibility.py#L172

Added line #L172 was not covered by tests

@property
def time_range(self):
return self.get(_T_RANGE_KEY, None)

Check warning on line 176 in xrayvision/visibility.py

View check run for this annotation

Codecov / codecov/patch

xrayvision/visibility.py#L176

Added line #L176 was not covered by tests

@property
def vis_labels(self):
return self.get(_VIS_LABELS_KEY, None)

@property
def instrument(self):
instr = None
i, n = 0, len(_INSTR_KEYS)
while not instr and i < n:
instr = self.get(_INSTR_KEYS[i], None)
i += 1
return instr

Check warning on line 189 in xrayvision/visibility.py

View check run for this annotation

Codecov / codecov/patch

xrayvision/visibility.py#L184-L189

Added lines #L184 - L189 were not covered by tests


class Visibilities(VisibilitiesABC):
@apu.quantity_input()
def __init__(
Expand Down Expand Up @@ -357,60 +413,6 @@ def __repr__(self):
return f"{self.__class__.__name__}< {self.u.size}, {self.visibilities}>"


class VisMeta(VisMetaABC, dict):
"""
A class for holding Visibility-specific metadata.
Parameters
----------
meta: `dict`
A dictionary of the metadata
"""

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Check controlled/expected inputs are of correct type and units.
controled_args = (
(_OBS_COORD_KEY, SkyCoord),
(_E_RANGE_KEY, apu.Quantity, apu.keV, apu.spectral()),
(_T_RANGE_KEY, Time),
)
for args in controled_args:
self._check_input_type_and_unit(*args)

def _check_input_type_and_unit(self, key, key_type, unit=None, equivalencies=None):
value = self.get(key, None)
if not isinstance(value, (key_type, type(None))):
raise KeyError(f"Inputs must include a key, '{key}', that gives a {key_type}.")
if unit is not None and value is not None and not value.unit.is_equivalent(unit, equivalencies=equivalencies):
raise ValueError(f"'{key}' must have angular units.")

@property
def observer_coordinate(self):
return self.get(_OBS_COORD_KEY, None)

@property
def spectral_range(self):
return self.get(_E_RANGE_KEY, None)

@property
def time_range(self):
return self.get(_T_RANGE_KEY, None)

@property
def vis_labels(self):
return self.get(_VIS_LABELS_KEY, None)

@property
def instrument(self):
instr = None
i, n = 0, len(_INSTR_KEYS)
while not instr and i < n:
instr = self.get(_INSTR_KEYS[i], None)
i += 1
return instr


class Visibility:
r"""
Hold a set of related visibilities and information.
Expand Down

0 comments on commit 21cb514

Please sign in to comment.