Skip to content

Commit

Permalink
Use None to encode NaNs in metadata and enable JSON dumps of results
Browse files Browse the repository at this point in the history
  • Loading branch information
aMarcireau committed Mar 3, 2023
1 parent 995b302 commit 66aceee
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 6 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -599,7 +599,7 @@ git submodule update --recursive
Format the code:

```sh
clang-format -i astrometry_extension/astrometry_extension.c
clang-format -i astrometry_extension/astrometry_extension.c astrometry_extension/astrometry_extension_utilities.h
```

Build a local version:
Expand Down
7 changes: 7 additions & 0 deletions astrometry/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import astrometry_extension
import dataclasses
import enum
import json
import math
import operator
import pathlib
Expand Down Expand Up @@ -154,6 +155,12 @@ def has_match(self) -> bool:
def best_match(self) -> Match:
return self.matches[0]

def to_json(self):
solution_as_dict = dataclasses.asdict(self)
for match in solution_as_dict["matches"]:
match["index_path"] = str(match["index_path"])
return json.dumps(solution_as_dict)


class Solver(astrometry_extension.Solver):
def __init__(self, index_files: list[pathlib.Path]):
Expand Down
16 changes: 12 additions & 4 deletions astrometry_extension/astrometry_extension_utilities.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <libgen.h>
#include <math.h>
#include <string.h>
#include <structmember.h>

Expand Down Expand Up @@ -255,6 +256,13 @@ static void error_callback(
context->save = PyEval_SaveThread();
}

static PyObject* double_to_python_object(double value) {
if (isnan(value) || isinf(value)) {
Py_RETURN_NONE;
}
return PyFloat_FromDouble(value);
}

static PyObject*
tagalong_to_python_object(startree_t* tree, int column_index, const char* column_name, int star_id, PyObject* logging) {
int size = startree_get_tagalong_column_array_size(tree, column_index);
Expand All @@ -276,20 +284,20 @@ tagalong_to_python_object(startree_t* tree, int column_index, const char* column
if (row_size > 1) {
result = PyTuple_New(row_size);
for (int index = 0; index < row_size; ++index) {
PyTuple_SET_ITEM(result, index, PyFloat_FromDouble(((double*)row)[index]));
PyTuple_SET_ITEM(result, index, double_to_python_object(((double*)row)[index]));
}
} else {
result = PyFloat_FromDouble(*(double*)row);
result = double_to_python_object(*(double*)row);
}
break;
case TFITS_BIN_TYPE_E: // float
if (row_size > 1) {
result = PyTuple_New(row_size);
for (int index = 0; index < row_size; ++index) {
PyTuple_SET_ITEM(result, index, PyFloat_FromDouble(((float*)row)[index]));
PyTuple_SET_ITEM(result, index, double_to_python_object(((float*)row)[index]));
}
} else {
result = PyFloat_FromDouble(*(float*)row);
result = double_to_python_object(*(float*)row);
}
break;
case TFITS_BIN_TYPE_A: // char
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ def run(self):

setuptools.setup(
name="astrometry",
version="4.0.0",
version="4.1.0",
url="https://github.com/neuromorphicsystems/astrometry",
author="ICNS, Alexandre Marcireau",
author_email="[email protected]",
Expand Down

0 comments on commit 66aceee

Please sign in to comment.