[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[gnuastro-commits] master a6135abf 2/2: Library (python.h): functions re
From: |
Mohammad Akhlaghi |
Subject: |
[gnuastro-commits] master a6135abf 2/2: Library (python.h): functions renamed, configure warnings, doc edits |
Date: |
Mon, 29 Aug 2022 20:43:52 -0400 (EDT) |
branch: master
commit a6135abf4edbf5b7d7e7364fd19a4ed8f4650cfd
Author: Mohammad Akhlaghi <mohammad@akhlaghi.org>
Commit: Mohammad Akhlaghi <mohammad@akhlaghi.org>
Library (python.h): functions renamed, configure warnings, doc edits
Until now, the names of the two Python functions
('gal_py_npytype_to_galtype' and 'gal_py_galtype_to_npytype') were a little
cryptic and repated the term 'type' two times. This makes the functions
hard to group when looking in the header, and of course in the manual, or
when using editors that automatically fill the functions names. Gnuastro's
way of grouping functions by their name is to put their common parts at the
start. In this scenario: start both with 'gal_py_type' prefix.
With this commit, this has been done and the functions are now called
'gal_py_type_to_numpy' and 'gal_py_type_from_numpy'. In the end, one of the
types is a Gnuastro type, so there is no point in repeating Gnuastro
('gal', which is also at the start of the function name).
In the process, the following changes have also been made:
- The two functions have been added in the NEWS file.
- The section title "Python library" has been renamed to "Python
interface" to avoid confusion with a fully-fledged Python library: the
goal here is just to prive an interface to communicate with
fully-fledged Python libraries. It has also been brought up in the list
of the Gnuastro library section to be closer to other wrappers (like
Git).
- The starting paragraph of the Python interface section now has a better
introduction on what Python is, and how it relates to Gnuastro.
- The copyright notices of the new files have been put in the standard
format of all Gnuastro's C source files (and Jash has been set as the
original author.
- The '#ifdef HAVE_PYTHON' has been removed from the main body of
functions in 'lib/python.c' because this file is only compiled if a
working Numpy+Python3 was found.
- The indentation of the 'case' statement in the two Python interface
functions have beed edited to follow GNU's style.
- All comments in the format of '//' have been removed! In Gnuastro, we
only use the '/* */' commenting style.
- Warnings are printed by './configure' if the user doesn't have Python
(just like other dependencies): one short warning if a mandatory
dependency is missing, and a longer one at the end (which is only for
optional dependencies).
- The last two types of 'gal_py_type_from_numpy' were mistakely taking
Gnuastro's type identifier and returning Numpy's.
---
NEWS | 5 ++-
configure.ac | 36 +++++++++------
doc/gnuastro.texi | 59 +++++++++++++++----------
lib/Makefile.am | 4 --
lib/gnuastro/python.h | 20 +++++----
lib/python.c | 118 ++++++++++++++++++++++----------------------------
6 files changed, 123 insertions(+), 119 deletions(-)
diff --git a/NEWS b/NEWS
index 8d6b9a77..8e43bb48 100644
--- a/NEWS
+++ b/NEWS
@@ -110,17 +110,18 @@ See the end of the file for license conditions.
- gal_eps_shape_id_to_name: return the name of a shape from its ID.
- gal_eps_shape_name_to_id: return the ID of a shape from its name.
- gal_fits_unique_keyvalues: extract all unique values to a certain
- keyword in many files.
+ keyword in many files.
- gal_fits_with_keyvalue: select FITS image with a certain key value.
- gal_list_data_select_by_name: select a dataset from a list by its name.
- gal_list_str_cat: Concatenate (append) list to a space-separated string.
- gal_list_str_extract: Extract space-separated tokens to a list.
+ - gal_py_type_from_numpy: Convert Numpy's type identifier to Gnuastro's.
+ - gal_py_type_to_numpy: Convert Gnuastro's type identifier to Numpy's.
- gal_units_counts_to_sb: SB from counts, zeropoint and area.
- gal_units_mag_to_sb: surface brightness (SB) from magnitude and area.
- gal_units_sb_to_counts: counts from SB, zeropoint and area.
- gal_units_sb_to_mag: magnitude from SB and area.
-
** Removed features
** Changed features
diff --git a/configure.ac b/configure.ac
index c097aca4..c60f852d 100644
--- a/configure.ac
+++ b/configure.ac
@@ -755,22 +755,20 @@ AS_IF([test "x$has_topcat" = "xno"], [anywarnings=yes])
-# Check Python3 and NumPy:
-# checks if Python3 and NumPy are installed, in that order
-# and gets their include path if true. This is done using two
-# python scripts:
-## py_check_cmd: Uses the sysconfig package's get_paths method
-## to get the include path for Python.h header.
-## np_check_cmd: Uses numpy's get_include method to get the
-## include path for NumPy's core C-API.
-AC_ARG_WITH([python],
- [AS_HELP_STRING([--without-python],
- [disable support for python])],
- [], [with_python=yes])
+# Check Python3 and NumPy (in that order): and gets their include path if
+# they do. This is done using two python scripts:
+# py_check_cmd: Uses the sysconfig package's get_paths method
+# to get the include path for Python.h header.
+# np_check_cmd: Uses numpy's get_include method to get the
+# include path for NumPy's core C-API.
py_check_cmd='from sysconfig import get_paths; \
print(get_paths().get("include"))'
np_check_cmd='from numpy import get_include; \
print(get_include())'
+AC_ARG_WITH([python],
+ [AS_HELP_STRING([--without-python],
+ [disable support for python])],
+ [], [with_python=yes])
AS_IF([test "x$with_python" != xno],
[
AC_CHECK_PROG(has_python3, python3, [yes], [no])
@@ -790,8 +788,8 @@ AS_IF([test "x$with_python" != xno],
AC_SUBST(PYTHON_INCLUDE_DIR, [$python_includedir])
])
AS_IF([test "x$numpy_includedir" = x],
- [AC_MSG_RESULT([no])],
- [AC_MSG_RESULT([yes])])
+ [has_numpy=no; AC_MSG_RESULT([no])],
+ [has_numpy=yes; AC_MSG_RESULT([yes])])
AM_CONDITIONAL([COND_NUMPY], [test "x$numpy_includedir" != x])
@@ -834,6 +832,8 @@ AS_IF([test "x$missing_mandatory" = "xyes"],
[ AS_ECHO([" - Missing Libgit2: https://libgit2.org"])
])
AS_IF([test "x$has_curl" = "x0"],
[ AS_ECHO([" - Missing cURL: https://curl.haxx.se"])
])
+ AS_IF([test "x$has_numpy" = "x0"],
+ [ AS_ECHO([" - Missing Numpy (for Python wrappers):
https://numpy.org"]) ])
AS_IF([test "x$has_ds9" = "xno"],
[ AS_ECHO([" - Missing SAO DS9:
https://sites.google.com/cfa.harvard.edu/saoimageds9"]) ])
AS_IF([test "x$has_topcat" = "xno"],
@@ -1346,6 +1346,14 @@ AS_IF([test x$enable_guide_message = xyes],
AS_ECHO([" to improve your Makefiles for data analysis
workflows."])
AS_ECHO([]) ])
+ AS_IF([test "x$has_numpy" = "xno"],
+ [dependency_notice=yes
+ AS_ECHO([" - Numpy (https://numpy.org) headers couldn't be
found within a "])
+ AS_ECHO([" Python3 environment. If available, Gnuastro's
library will be "])
+ AS_ECHO([" installed with some functions that can help
Python wrappers "])
+ AS_ECHO([" communicate with Gnuastro's library (for example
pyGnuastro)."])
+ AS_ECHO([]) ])
+
AS_IF([test "x$has_ds9" = "xno"],
[dependency_notice=yes
AS_ECHO([" - (GUI only) SAO ds9
(https://sites.google.com/cfa.harvard.edu/saoimageds9)"])
diff --git a/doc/gnuastro.texi b/doc/gnuastro.texi
index deb193cb..78d9f688 100644
--- a/doc/gnuastro.texi
+++ b/doc/gnuastro.texi
@@ -293,9 +293,9 @@ General program usage tutorial
* Column statistics color-magnitude diagram:: Visualizing column correlations.
* Aperture photometry:: Doing photometry on a fixed aperture.
* Matching catalogs:: Easily find corresponding rows from two
catalogs.
-* Reddest clumps cutouts and parallelization::
-* FITS images in a publication::
-* Marking objects for publication::
+* Reddest clumps cutouts and parallelization:: Parallization and selecting a
subset of the data.
+* FITS images in a publication:: How to display FITS images in a PDF.
+* Marking objects for publication:: How to mark some objects over the image
in a PDF.
* Writing scripts to automate the steps:: Scripts will greatly help in
re-doing things fast.
* Citing and acknowledging Gnuastro:: How to cite and acknowledge Gnuastro in
your papers.
@@ -777,11 +777,11 @@ Gnuastro library
* Interpolation:: Interpolate (over blank values possibly).
* Color functions:: Definitions and operations related to colors.
* Git wrappers:: Wrappers for functions in libgit2.
+* Python interface:: Functions to help in writing Python wrappers.
* Unit conversion library:: Converting between recognized units.
* Spectral lines library:: Functions for operating on Spectral lines.
* Cosmology library:: Cosmological calculations.
* SAO DS9 library:: Take inputs from files generated by SAO DS9.
-* Python library:: Utility functions for the Gnuastro Python
Package.
Multithreaded programming (@file{threads.h})
@@ -1852,9 +1852,9 @@ This will help simulate future situations when you are
processing your own datas
* Column statistics color-magnitude diagram:: Visualizing column correlations.
* Aperture photometry:: Doing photometry on a fixed aperture.
* Matching catalogs:: Easily find corresponding rows from two
catalogs.
-* Reddest clumps cutouts and parallelization:: Parallization and selecting a
subset of the data.
-* FITS images in a publication:: How to display FITS images in a PDF.
-* Marking objects for publication:: How to mark some objects over the image in
a PDF.
+* Reddest clumps cutouts and parallelization:: Parallization and selecting a
subset of the data.
+* FITS images in a publication:: How to display FITS images in a PDF.
+* Marking objects for publication:: How to mark some objects over the image
in a PDF.
* Writing scripts to automate the steps:: Scripts will greatly help in
re-doing things fast.
* Citing and acknowledging Gnuastro:: How to cite and acknowledge Gnuastro in
your papers.
@end menu
@@ -28690,11 +28690,11 @@ If you use the Info version of this manual (see
@ref{Info}), you do not have to
* Interpolation:: Interpolate (over blank values possibly).
* Color functions:: Definitions and operations related to colors.
* Git wrappers:: Wrappers for functions in libgit2.
+* Python interface:: Functions to help in writing Python wrappers.
* Unit conversion library:: Converting between recognized units.
* Spectral lines library:: Functions for operating on Spectral lines.
* Cosmology library:: Cosmological calculations.
* SAO DS9 library:: Take inputs from files generated by SAO DS9.
-* Python library:: Utility functions for the Gnuastro Python
Package.
@end menu
@node Configuration information, Multithreaded programming, Gnuastro library,
Gnuastro library
@@ -35818,7 +35818,7 @@ Given the identifier of a color, write the color's
red-green-blue fractions in t
It is upto the caller to have the space for three 32-bit floating point
numbers to be already allocated before calling this function.
@end deftypefun
-@node Git wrappers, Unit conversion library, Color functions, Gnuastro library
+@node Git wrappers, Python interface, Color functions, Gnuastro library
@subsection Git wrappers (@file{git.h})
@cindex Git
@@ -35854,7 +35854,33 @@ not installed or the program calling this function is
not within a version
controlled directory, then the output will be the @code{NULL} pointer.
@end deftypefun
-@node Unit conversion library, Spectral lines library, Git wrappers, Gnuastro
library
+@node Python interface, Unit conversion library, Git wrappers, Gnuastro library
+@subsection Python interface (@file{python.h})
+
+@url{https://en.wikipedia.org/wiki/Python_(programming_language), Python} is a
high-level interpretted programming language that is used by some for data
analysis.
+Python itself is written in C, which is the same language that Gnuastro is
written in.
+Hence Gnuastro's library can be directly used in Python wrappers.
+The functions in this section provide some low-level features to simplify the
creation of Python modules that may want to use Gnuastro's advanced and
powerful features directly.
+To see why Gnuastro was written in C, please see @ref{Why C}.
+
+The Gnuastro Python Package is built using CPython.
+This entails using Python wrappers around currently existing Gnuastro library
functions to build @url{https://docs.python.org/3/extending/extending.html#,
Python Extension Modules}.
+It also makes use of the
@url{https://numpy.org/doc/stable/reference/c-api/index.html, NumPy C-API} for
dealing with data arrays.
+Writing an interfacing between these and Gnuastro can be simplified using the
functions below.
+Since many of these functions depend on the Gnuastro Library itself, it is
more convenient to package them with the Library to facilitate the work of
Python package.
+These functions will be expanding as Gnuastro's own Python module (pyGnuastro)
grows.
+
+@deftypefun int gal_py_type_to_numpy (uint8_t @code{type})
+Returns the NumPy datatype corresponding to a certain Gnuastro @code{type},
see @ref{Library data types}.
+@end deftypefun
+
+@deftypefun uint8_t gal_py_type_from_numpy (int @code{type})
+Returns Gnuastro's numerical datatype that correspondes to the input NumPy
@code{type}.
+For Gnuastro's recognized data types, see @ref{Library data types}.
+@end deftypefun
+
+
+@node Unit conversion library, Spectral lines library, Python interface,
Gnuastro library
@subsection Unit conversion library (@file{units.h})
Datasets can contain values in various formats or units.
@@ -36242,19 +36268,6 @@ polygon(53.187414,-27.779152,53.159507,-27.759633,...)
-@node Python library, , SAO DS9 library, Gnuastro library
-@subsection Python Library (@file{python.h})
-
-This library provides the utility functions required for building the Gnuastro
Python package. The Gnuastro Python Package is built using CPython. This
entails using Python wrappers around currently existing Gnuastro library
functions to build @url{https://docs.python.org/3/extending/extending.html#,
Python Extension Modules}. It also makes use of the
@url{https://numpy.org/doc/stable/reference/c-api/index.html, NumPy C-API} for
dealing with data arrays.
-The interfacing between all these tools requires a set of utility functions.
Since many of these functions depend on the Gnuastro Library itself, it is more
convenient to package them with the Library instead of the Python package.
-
-@deftypefun int gal_py_galtype_to_npytype(uint8_t @code{type})
-Returns the NumPy datatype corresponding the Gnuastro @code{type}.
-@end deftypefun
-
-@deftypefun int gal_py_npytype_to_galtype(uint_8 @code{type})
-Returns the Gnuastro's type identifier that correspondes to the NumPy
@code{type} passed.
-@end deftypefun
diff --git a/lib/Makefile.am b/lib/Makefile.am
index e0ebd1f6..d4fb552b 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -23,16 +23,12 @@
# Conditional compilation
headersdir=$(top_srcdir)/lib/gnuastro
-
if COND_HASWCSDIS_H
MAYBE_WCSDISTORTION = wcsdistortion.c
endif
if COND_HASGNUMAKE_H
MAYBE_GNUMAKE = libgnuastro_make.la
endif
-
-# Build the python utilities file,
-# only if Python and NumPy are present.
if COND_NUMPY
MAYBE_NUMPY_C = python.c
MAYBE_NUMPY_H = $(headersdir)/python.h
diff --git a/lib/gnuastro/python.h b/lib/gnuastro/python.h
index af171600..17a82257 100644
--- a/lib/gnuastro/python.h
+++ b/lib/gnuastro/python.h
@@ -1,19 +1,23 @@
/*********************************************************************
-python -- Functions to assist the python wrappers.
+python -- Functions to assist Python wrappers using Gnuastro's library.
This is part of GNU Astronomy Utilities (Gnuastro) package.
+
Original author:
- Mohammad Akhlaghi <mohammad@akhlaghi.org>
+ Jash Shah <jash28582@gmail.com>
Contributing author(s):
- Sachin Kumar Singh <sachinkumarsingh092@gmail.com>
-Copyright (C) 2017-2022 Free Software Foundation, Inc.
+ Mohammad Akhlaghi <mohammad@akhlaghi.org>
+Copyright (C) 2022 Free Software Foundation, Inc.
+
Gnuastro is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
+
Gnuastro is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
+
You should have received a copy of the GNU General Public License
along with Gnuastro. If not, see <http://www.gnu.org/licenses/>.
**********************************************************************/
@@ -51,13 +55,11 @@ __BEGIN_C_DECLS /* From C++ preparations */
/*************************************************************
************** Type codes ***************
*************************************************************/
-/* Convert Gnuastro type to NumPy datatype. */
int
-gal_py_galtype_to_npytype(uint8_t type);
+gal_py_type_to_numpy(uint8_t type);
-/* Convert Numpy datatype to Gnuastro type. */
-int
-gal_py_npytype_to_galtype(uint8_t type);
+uint8_t
+gal_py_type_from_numpy(int type);
diff --git a/lib/python.c b/lib/python.c
index 5230956e..1a31c976 100644
--- a/lib/python.c
+++ b/lib/python.c
@@ -1,19 +1,23 @@
/*********************************************************************
-python -- Functions to assist the python wrappers.
+python -- Functions to assist Python wrappers using Gnuastro's library.
This is part of GNU Astronomy Utilities (Gnuastro) package.
+
Original author:
- Mohammad Akhlaghi <mohammad@akhlaghi.org>
+ Jash Shah <jash28582@gmail.com>
Contributing author(s):
- Sachin Kumar Singh <sachinkumarsingh092@gmail.com>
-Copyright (C) 2017-2022 Free Software Foundation, Inc.
+ Mohammad Akhlaghi <mohammad@akhlaghi.org>
+Copyright (C) 2022 Free Software Foundation, Inc.
+
Gnuastro is free software: you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation, either version 3 of the License, or (at your
option) any later version.
+
Gnuastro is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
+
You should have received a copy of the GNU General Public License
along with Gnuastro. If not, see <http://www.gnu.org/licenses/>.
**********************************************************************/
@@ -22,60 +26,43 @@ along with Gnuastro. If not, see
<http://www.gnu.org/licenses/>.
#include <errno.h>
#include <error.h>
-#ifdef HAVE_PYTHON
- // This macro needs to be defined before including any NumPy headers
- // to avoid the compiler from raising a warning message.
- #define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
- #include <numpy/arrayobject.h>
-#endif
+/* This macro needs to be defined before including any NumPy headers
+ to avoid the compiler from raising a warning message. */
+#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
+#include <numpy/arrayobject.h>
+/* Gnuastro's Python headers. */
#include <gnuastro/python.h>
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
/*************************************************************
************** Type codes ***************
*************************************************************/
-#ifdef HAVE_PYTHON
-/* Convert Gnuastro type to NumPy datatype. */
+/* Convert Gnuastro type to NumPy datatype. Currently only converting types
+ directly compatible between the two. */
int
-gal_py_galtype_to_npytype(uint8_t type)
+gal_py_type_to_numpy(uint8_t type)
{
switch (type)
- {
- /* Types directly convertible between Gnuastro and NumPy. */
- case GAL_TYPE_INT8: return NPY_INT8;
- case GAL_TYPE_INT16: return NPY_INT16;
- case GAL_TYPE_INT32: return NPY_INT32;
- case GAL_TYPE_INT64: return NPY_LONG;
- case GAL_TYPE_UINT8: return NPY_UINT8;
- case GAL_TYPE_UINT16: return NPY_UINT16;
- case GAL_TYPE_UINT32: return NPY_UINT32;
- case GAL_TYPE_UINT64: return NPY_UINT64;
- case GAL_TYPE_FLOAT32: return NPY_FLOAT32;
- case GAL_TYPE_FLOAT64: return NPY_FLOAT64;
- case GAL_TYPE_STRING: return NPY_STRING;
- default:
- error(EXIT_FAILURE, 0, "%s: type code %d is not convertible"
+ {
+ case GAL_TYPE_INT8: return NPY_INT8;
+ case GAL_TYPE_INT16: return NPY_INT16;
+ case GAL_TYPE_INT32: return NPY_INT32;
+ case GAL_TYPE_INT64: return NPY_LONG;
+ case GAL_TYPE_UINT8: return NPY_UINT8;
+ case GAL_TYPE_UINT16: return NPY_UINT16;
+ case GAL_TYPE_UINT32: return NPY_UINT32;
+ case GAL_TYPE_UINT64: return NPY_UINT64;
+ case GAL_TYPE_FLOAT32: return NPY_FLOAT32;
+ case GAL_TYPE_FLOAT64: return NPY_FLOAT64;
+ case GAL_TYPE_STRING: return NPY_STRING;
+ default:
+ error(EXIT_FAILURE, 0, "%s: type code %d is not convertible"
"to NumPy.", __func__, type);
- }
+ }
return GAL_TYPE_INVALID;
}
@@ -84,31 +71,28 @@ gal_py_galtype_to_npytype(uint8_t type)
-/* Convert Numpy datatype to Gnuastro type. */
-int
-gal_py_npytype_to_galtype(uint8_t type)
+/* Convert Numpy datatype to Gnuastro type. Currently only converting types
+ directly compatible between the two. */
+uint8_t
+gal_py_type_from_numpy(int type)
{
- /* Types directly convertible between NumPy and Gnuastro. */
switch (type)
- {
- // Currently only converting types directly compatible
- // between the two.
- case NPY_INT8: return GAL_TYPE_INT8;
- case NPY_INT16: return GAL_TYPE_INT16;
- case NPY_INT32: return GAL_TYPE_INT32;
- case NPY_LONG: return GAL_TYPE_INT64;
- case NPY_UINT8: return GAL_TYPE_UINT8;
- case NPY_UINT16: return GAL_TYPE_UINT16;
- case NPY_UINT32: return GAL_TYPE_UINT32;
- case NPY_UINT64: return GAL_TYPE_UINT64;
- case NPY_FLOAT32: return GAL_TYPE_FLOAT32;
- case NPY_FLOAT64: return GAL_TYPE_FLOAT64;
- case GAL_TYPE_COMPLEX64: return NPY_COMPLEX64;
- case GAL_TYPE_STRING: return NPY_STRING;
- default:
- error(EXIT_FAILURE, 0, "%s: type code %d is not convertible"
+ {
+ case NPY_INT8: return GAL_TYPE_INT8;
+ case NPY_INT16: return GAL_TYPE_INT16;
+ case NPY_INT32: return GAL_TYPE_INT32;
+ case NPY_LONG: return GAL_TYPE_INT64;
+ case NPY_UINT8: return GAL_TYPE_UINT8;
+ case NPY_UINT16: return GAL_TYPE_UINT16;
+ case NPY_UINT32: return GAL_TYPE_UINT32;
+ case NPY_UINT64: return GAL_TYPE_UINT64;
+ case NPY_FLOAT32: return GAL_TYPE_FLOAT32;
+ case NPY_FLOAT64: return GAL_TYPE_FLOAT64;
+ case NPY_COMPLEX64: return GAL_TYPE_COMPLEX64;
+ case NPY_STRING: return GAL_TYPE_STRING;
+ default:
+ error(EXIT_FAILURE, 0, "%s: type code %d is not convertible"
"to Gnuastro.", __func__, type);
- }
+ }
return GAL_TYPE_INVALID;
}
-#endif