Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solving issue with wcs.wcs.cunit equality #8480

Merged
merged 3 commits into from
Apr 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,11 @@ astropy.visualization
astropy.wcs
^^^^^^^^^^^

- Added a ``PyUnitListProxy_richcmp`` method in ``UnitListProxy`` class to enable
``WCS.wcs.cunit`` equality testing. It helps to check whether the two instances of
``WCS.wcs.cunit`` are equal or not by comparing the data members of
``UnitListProxy`` class [#8480]

Other Changes and Additions
---------------------------

Expand Down
40 changes: 36 additions & 4 deletions astropy/wcs/src/unit_list_proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,15 @@
***************************************************************************/

#define MAXSIZE 68
#define ARRAYSIZE 72
geekypathak21 marked this conversation as resolved.
Show resolved Hide resolved

static PyTypeObject PyUnitListProxyType;

typedef struct {
PyObject_HEAD
/*@null@*/ /*@shared@*/ PyObject* pyobject;
Py_ssize_t size;
char (*array)[72];
char (*array)[ARRAYSIZE];
PyObject* unit_class;
} PyUnitListProxy;

Expand Down Expand Up @@ -94,7 +95,7 @@ PyUnitListProxy_clear(
PyUnitListProxy_New(
/*@shared@*/ PyObject* owner,
Py_ssize_t size,
char (*array)[72]) {
char (*array)[ARRAYSIZE]) {

PyUnitListProxy* self = NULL;
PyObject *units_module;
Expand Down Expand Up @@ -186,6 +187,36 @@ PyUnitListProxy_getitem(
return result;
}

static PyObject*
PyUnitListProxy_richcmp(
PyObject *a,
PyObject *b,
int op){
PyUnitListProxy *lhs, *rhs;
assert(a != NULL && b != NULL);
if (!PyObject_TypeCheck(a, &PyUnitListProxyType) ||
!PyObject_TypeCheck(b, &PyUnitListProxyType)) {
Py_RETURN_NOTIMPLEMENTED;
}
if (op != Py_EQ && op != Py_NE) {
Py_RETURN_NOTIMPLEMENTED;
}
lhs = (PyUnitListProxy *)a;
rhs = (PyUnitListProxy *)b;
int equal = PyObject_RichCompareBool(lhs->unit_class, rhs->unit_class, Py_EQ);
if (equal == -1) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coverage report failure is because of this case as PyObject_RichCompareBool is not returning -1 in any of the case present in the test. For this I think, it needs direct testing of PyUnitListProxy which will be more cumbersome. So right now we can merge it

return NULL; // Exception will be set because the rich-compare failed
}
equal = equal == 1 && !strncmp(lhs->array, rhs->array, ARRAYSIZE) && lhs->size == rhs->size;
if ((op == Py_EQ && equal == 1) ||
(op == Py_NE && equal == 0)) {
Py_RETURN_TRUE;
}
else {
Py_RETURN_FALSE;
}
}

static int
PyUnitListProxy_setitem(
PyUnitListProxy* self,
Expand Down Expand Up @@ -274,7 +305,7 @@ static PyTypeObject PyUnitListProxyType = {
0, /* tp_doc */
(traverseproc)PyUnitListProxy_traverse, /* tp_traverse */
(inquiry)PyUnitListProxy_clear, /* tp_clear */
0, /* tp_richcompare */
(richcmpfunc)PyUnitListProxy_richcmp, /* tp_richcompare */
0, /* tp_weaklistoffset */
0, /* tp_iter */
0, /* tp_iternext */
Expand All @@ -298,7 +329,7 @@ set_unit_list(
const char* propname,
PyObject* value,
Py_ssize_t len,
char (*dest)[72]) {
char (*dest)[ARRAYSIZE]) {

PyObject* unit = NULL;
PyObject* proxy = NULL;
Expand Down Expand Up @@ -362,3 +393,4 @@ _setup_unit_list_proxy_type(

return 0;
}

25 changes: 25 additions & 0 deletions astropy/wcs/tests/test_wcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -1183,3 +1183,28 @@ def test_footprint_contains():

hasCoord = test_wcs.footprint_contains(SkyCoord(24,2,unit='deg'))
assert hasCoord == False


def test_cunit():
# Initializing WCS
w1 = wcs.WCS(naxis=2)
w2 = wcs.WCS(naxis=2)
w3 = wcs.WCS(naxis=2)
# Initializing the values of cunit
w1.wcs.cunit = ['deg', 'm/s']
w2.wcs.cunit = ['km/h', 'km/h']
w3.wcs.cunit = ['deg', 'm/s']

# Equality checking a cunit with itself
assert w1.wcs.cunit == w1.wcs.cunit
geekypathak21 marked this conversation as resolved.
Show resolved Hide resolved
# Equality checking of two different cunit object having same values
assert w1.wcs.cunit == w3.wcs.cunit
# Inequality checking of two different cunit object having different values
assert not w1.wcs.cunit == w2.wcs.cunit
# Inequality checking of cunit with a list of literals
assert not w1.wcs.cunit == [1, 2, 3]
# Inequality checking with some characters
assert w1.wcs.cunit != ['a', 'b', 'c']
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I added this test because of !=

# Comparison is not implemented TypeError will raise
with pytest.raises(TypeError):
w1.wcs.cunit < w2.wcs.cunit