Skip to content

Commit

Permalink
Add equality methods to Visibilities and VisMeta.
Browse files Browse the repository at this point in the history
  • Loading branch information
DanRyanIrish committed May 25, 2024
1 parent 4f1d619 commit 0467e63
Show file tree
Hide file tree
Showing 2 changed files with 95 additions and 3 deletions.
28 changes: 25 additions & 3 deletions xrayvision/tests/test_visibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

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

import xrayvision.visibility as vm

Expand All @@ -14,14 +16,22 @@ def test_data_dir():


@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 @@ -70,3 +80,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
70 changes: 70 additions & 0 deletions xrayvision/visibility.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,35 @@ def __repr__(self):
"""
return f"{self.__class__.__name__}< {self.u.size}, {self.visibilities}>"

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

Check warning on line 362 in xrayvision/visibility.py

View check run for this annotation

Codecov / codecov/patch

xrayvision/visibility.py#L362

Added line #L362 was not covered by tests
if not apu.quantity.allclose(self.u, other.u):
return False

Check warning on line 364 in xrayvision/visibility.py

View check run for this annotation

Codecov / codecov/patch

xrayvision/visibility.py#L364

Added line #L364 was not covered by tests
if not apu.quantity.allclose(self.v, other.v):
return False

Check warning on line 366 in xrayvision/visibility.py

View check run for this annotation

Codecov / codecov/patch

xrayvision/visibility.py#L366

Added line #L366 was not covered by tests
if not apu.quantity.allclose(self.phase_center, other.phase_center):
return False

Check warning on line 368 in xrayvision/visibility.py

View check run for this annotation

Codecov / codecov/patch

xrayvision/visibility.py#L368

Added line #L368 was not covered by tests
if not apu.quantity.allclose(self.amplitude, other.amplitude):
return False

Check warning on line 370 in xrayvision/visibility.py

View check run for this annotation

Codecov / codecov/patch

xrayvision/visibility.py#L370

Added line #L370 was not covered by tests
if not apu.quantity.allclose(self.phase, other.phase):
return False

Check warning on line 372 in xrayvision/visibility.py

View check run for this annotation

Codecov / codecov/patch

xrayvision/visibility.py#L372

Added line #L372 was not covered by tests

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

Check warning on line 380 in xrayvision/visibility.py

View check run for this annotation

Codecov / codecov/patch

xrayvision/visibility.py#L380

Added line #L380 was not covered by tests

return True


class VisMeta(VisMetaABC, dict):
"""
Expand Down Expand Up @@ -406,6 +435,47 @@ def instrument(self):
i += 1
return instr

@property
def __eq__(self, other):
if not _attrs_both_none_or_neither(self.observer_coordinate, other.observer_coordinate):
return False
if hasattr(self.observer_coordinate, "__len__"):
if not hasattr(other.observer_coordinate, "__len__"):
if all(self.observer_coordinate == other.observer_coordinate):
return False

Check warning on line 445 in xrayvision/visibility.py

View check run for this annotation

Codecov / codecov/patch

xrayvision/visibility.py#L440-L445

Added lines #L440 - L445 were not covered by tests
else:
return False
elif hasattr(other.observer_coordinate, "__len__"):
return False
elif self.observer_coordinate != other.observer_coordinate:
return False

Check warning on line 451 in xrayvision/visibility.py

View check run for this annotation

Codecov / codecov/patch

xrayvision/visibility.py#L447-L451

Added lines #L447 - L451 were not covered by tests

if (not _attrs_both_none_or_neither(self.vis_labels, other.vis_labels)

Check warning on line 453 in xrayvision/visibility.py

View check run for this annotation

Codecov / codecov/patch

xrayvision/visibility.py#L453

Added line #L453 was not covered by tests
or tuple(self.vis_labels) != tuple(other.vis_labels)):
return False

Check warning on line 455 in xrayvision/visibility.py

View check run for this annotation

Codecov / codecov/patch

xrayvision/visibility.py#L455

Added line #L455 was not covered by tests

if (not _attrs_both_none_or_neither(self.instrument, other.instrument)

Check warning on line 457 in xrayvision/visibility.py

View check run for this annotation

Codecov / codecov/patch

xrayvision/visibility.py#L457

Added line #L457 was not covered by tests
or tuple(self.instrument) != tuple(other.instrument)):
return False

Check warning on line 459 in xrayvision/visibility.py

View check run for this annotation

Codecov / codecov/patch

xrayvision/visibility.py#L459

Added line #L459 was not covered by tests

if not (_attrs_both_none_or_neither(self.spectral_range, other.spectral_range)

Check warning on line 461 in xrayvision/visibility.py

View check run for this annotation

Codecov / codecov/patch

xrayvision/visibility.py#L461

Added line #L461 was not covered by tests
and apu.quantity.allclose(self.spectral_range, other.spectral_range)):
return False

Check warning on line 463 in xrayvision/visibility.py

View check run for this annotation

Codecov / codecov/patch

xrayvision/visibility.py#L463

Added line #L463 was not covered by tests

if not (_attrs_both_none_or_neither(self.time_range, other.time_range)

Check warning on line 465 in xrayvision/visibility.py

View check run for this annotation

Codecov / codecov/patch

xrayvision/visibility.py#L465

Added line #L465 was not covered by tests
and np.allclose(self.time_range.mjd == other.time_range.mjd)):
return False
return True

Check warning on line 468 in xrayvision/visibility.py

View check run for this annotation

Codecov / codecov/patch

xrayvision/visibility.py#L467-L468

Added lines #L467 - L468 were not covered by tests


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

Check warning on line 474 in xrayvision/visibility.py

View check run for this annotation

Codecov / codecov/patch

xrayvision/visibility.py#L473-L474

Added lines #L473 - L474 were not covered by tests
elif attr2 is None:
return False

Check warning on line 476 in xrayvision/visibility.py

View check run for this annotation

Codecov / codecov/patch

xrayvision/visibility.py#L476

Added line #L476 was not covered by tests
return True


class BaseVisibility:
r"""
Expand Down

0 comments on commit 0467e63

Please sign in to comment.