Skip to content

Commit

Permalink
Enable equality operator on Visibilities and VisMeta classes (#64)
Browse files Browse the repository at this point in the history
* Add equality methods to Visibilities and VisMeta.
  • Loading branch information
DanRyanIrish authored May 27, 2024
1 parent 6c46be2 commit adfb913
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 3 deletions.
1 change: 1 addition & 0 deletions changelog/64.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Add equality operator to `~xrayvision.visibility.Visibilities` and `~xrayvision.visibility.VisMeta`.
32 changes: 29 additions & 3 deletions xrayvision/tests/test_visibility.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import astropy.units as apu
import pytest
from astropy.coordinates import get_body
from astropy.tests.helper import assert_quantity_allclose
from astropy.time import Time
from numpy.testing import assert_array_equal

import xrayvision.visibility as vm
Expand All @@ -17,14 +19,26 @@ def test_visibility():


@pytest.fixture
def visibilities():
def vis_meta():
return vm.VisMeta(
{
"observer_coordinate": get_body("Earth", Time("2000-01-01 00:00:00")),
"energy_range": [6, 10] * apu.keV,
"time_range": Time(["2000-01-01 00:00:00", "2000-01-01 00:00:01"]),
"vis_labels": ["3a", "10b"],
"instrument": "stix",
}
)


@pytest.fixture
def visibilities(vis_meta):
visibilities = [1 + 2 * 1j, 3 + 3 * 1j] * apu.ct
u = [0.023, -0.08] * 1 / apu.arcsec
v = [-0.0013, 0.013] * 1 / apu.arcsec
phase_center = [0, 0] * apu.arcsec
unc = [0.01, 0.15] * apu.ct
meta = vm.VisMeta({"vis_labels": ["3a", "10b"]})
return vm.Visibilities(visibilities, u, v, phase_center, uncertainty=unc, meta=meta)
return vm.Visibilities(visibilities, u, v, phase_center, uncertainty=unc, meta=vis_meta)


def test_vis_u(visibilities):
Expand Down Expand Up @@ -73,3 +87,15 @@ def test_vis_phase_uncertainty(visibilities):
expected_phase_uncertainty = [0.22918312, 1.4323945] * apu.deg

assert_quantity_allclose(output_phase_uncertainty, expected_phase_uncertainty)


def test_vis_eq(visibilities):
vis = visibilities
assert vis == vis


def test_meta_eq(vis_meta):
meta = vis_meta
assert meta == meta
meta = vm.VisMeta(dict())
assert meta == meta
41 changes: 41 additions & 0 deletions xrayvision/visibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,47 @@ def __repr__(self):
"""
return f"{self.__class__.__name__}< {self.u.size}, {self.visibilities}>"

def __eq__(self, other):
"""
Checks whether two Visibilities objects are equal.
Does not check whether their metas are equal.
"""
if not apu.quantity.allclose(self.visibilities, other.visibilities):
return False
if not apu.quantity.allclose(self.u, other.u):
return False
if not apu.quantity.allclose(self.v, other.v):
return False
if not apu.quantity.allclose(self.phase_center, other.phase_center):
return False
if not apu.quantity.allclose(self.amplitude, other.amplitude):
return False
if not apu.quantity.allclose(self.phase, other.phase):
return False

uncerts = (
(self.uncertainty, other.uncertainty),
(self.amplitude_uncertainty, other.amplitude_uncertainty),
(self.phase_uncertainty, other.phase_uncertainty),
)
for self_uncert, other_uncert in uncerts:
if not _attrs_both_none_or_neither(self_uncert, other_uncert) or (
self_uncert is not None and not apu.quantity.allclose(self_uncert, other_uncert)
):
return False

return True


def _attrs_both_none_or_neither(attr1, attr2):
if attr1 is None:
if attr2 is not None:
return False
elif attr2 is None:
return False
return True


class Visibility:
r"""
Expand Down

0 comments on commit adfb913

Please sign in to comment.