From 66aceee75917e23a9d77070cd4966ccf5f47ceac Mon Sep 17 00:00:00 2001 From: Alexandre Marcireau Date: Fri, 3 Mar 2023 11:13:01 +1100 Subject: [PATCH] Use None to encode NaNs in metadata and enable JSON dumps of results --- README.md | 2 +- astrometry/__init__.py | 7 +++++++ .../astrometry_extension_utilities.h | 16 ++++++++++++---- setup.py | 2 +- 4 files changed, 21 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 45e0236..96d5f94 100644 --- a/README.md +++ b/README.md @@ -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: diff --git a/astrometry/__init__.py b/astrometry/__init__.py index 771d0f5..29321d7 100644 --- a/astrometry/__init__.py +++ b/astrometry/__init__.py @@ -2,6 +2,7 @@ import astrometry_extension import dataclasses import enum +import json import math import operator import pathlib @@ -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]): diff --git a/astrometry_extension/astrometry_extension_utilities.h b/astrometry_extension/astrometry_extension_utilities.h index fefb622..0c5aaf6 100644 --- a/astrometry_extension/astrometry_extension_utilities.h +++ b/astrometry_extension/astrometry_extension_utilities.h @@ -5,6 +5,7 @@ #define PY_SSIZE_T_CLEAN #include #include +#include #include #include @@ -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); @@ -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 diff --git a/setup.py b/setup.py index d3b492a..b0ec5a4 100644 --- a/setup.py +++ b/setup.py @@ -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="alexandre.marcireau@gmail.com",