From 787ddd8b80d4f3ae7b7c20d53cca5253be34e1a0 Mon Sep 17 00:00:00 2001 From: Leo Singer Date: Fri, 2 Feb 2018 00:31:06 -0500 Subject: [PATCH 1/2] Don't import pyplot at module level Importing pyplot has lots of side effects. It should be imported at the very last possible moment. --- lalinference/python/lalinference/plot/spindisk.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lalinference/python/lalinference/plot/spindisk.py b/lalinference/python/lalinference/plot/spindisk.py index 193faf21e9..d750c863aa 100644 --- a/lalinference/python/lalinference/plot/spindisk.py +++ b/lalinference/python/lalinference/plot/spindisk.py @@ -2,12 +2,12 @@ import numpy as np import os -from matplotlib import pyplot as plt from matplotlib.lines import Line2D __all__=('make_disk_plot',) def make_disk_plot(post,outpath=None): + from matplotlib import pyplot as plt from matplotlib import rc rc('text', usetex=True) rc('font', family='lmodern') -- GitLab From 5c910752258a01b6c731a3a9dc27537a2f1bf5e4 Mon Sep 17 00:00:00 2001 From: Leo Singer Date: Fri, 2 Feb 2018 01:00:46 -0500 Subject: [PATCH 2/2] Perform minimal Python 3 porting Just get to the point where _bayespputils.c builds under Python 3, and I can import lalinference.plot again. --- .../python/lalinference/_bayespputils.c | 38 +++++++++---------- .../python/lalinference/plot/spindisk.py | 4 +- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/lalinference/python/lalinference/_bayespputils.c b/lalinference/python/lalinference/_bayespputils.c index d957e64f7e..a5ab43ec16 100644 --- a/lalinference/python/lalinference/_bayespputils.c +++ b/lalinference/python/lalinference/_bayespputils.c @@ -9,18 +9,12 @@ #include #include +#include "six.h" #include #define MAX_IN_FILES 128 -#define MODULE_NAME "_bayespputils" - -/* doc string */ -const char BUDocstring[] = -"This module provides C extensions for Bayesian analysis and post-\n" -"processing codes."; - double findMaxLogL(FILE * input, double maxLogL); typedef struct tagBurnInInput{ @@ -49,8 +43,6 @@ static PyObject* _burnin(PyObject *self, PyObject *args) { PyObject* py_inputfile_list=NULL ;// List of files containing posterior chains." Py_ssize_t nfiles; PyObject* py_spin=NULL; - PyObject* py_deltaLogL=NULL; - PyObject* py_outputfilename=NULL ; //RETURN PyObject* py_pos_array=NULL; @@ -64,9 +56,7 @@ static PyObject* _burnin(PyObject *self, PyObject *args) { /***PARSE/PROCESS INPUT***/ - if (!PyArg_ParseTuple(args,"O!O!O!O!",&PyList_Type,&py_inputfile_list,&PyBool_Type,&py_spin,&PyFloat_Type,&py_deltaLogL,&PyString_Type,&py_outputfilename)) return NULL; - - input->deltaLogL=PyFloat_AsDouble(py_deltaLogL); + if (!PyArg_ParseTuple(args,"O!O!ds",&PyList_Type,&py_inputfile_list,&PyBool_Type,&py_spin,&input->deltaLogL,&input->output_file_name)) return NULL; input->nfiles=PyList_Size(py_inputfile_list); @@ -75,15 +65,17 @@ static PyObject* _burnin(PyObject *self, PyObject *args) { Py_ssize_t i; for(i=0;infiles;i++){ char* ptemp=NULL; - if((ptemp=PyString_AsString(PyList_GetItem(py_inputfile_list,i)))!=0){ +#if PY_MAJOR_VERSION >= 3 + if((ptemp=PyUnicode_AsUTF8(PyList_GetItem(py_inputfile_list,i)))!=0){ +#else + if((ptemp=PyString_AsString(PyList_GetItem(py_inputfile_list,i)))!=0){ +#endif input->files[i]=ptemp; } else { nfiles--; } } - - input->output_file_name = PyString_AsString(py_outputfilename); Py_INCREF(Py_True); if(py_spin==Py_True) { @@ -125,7 +117,7 @@ static PyObject* _burnin(PyObject *self, PyObject *args) { */ -static struct PyMethodDef BUmethods[] = { +static struct PyMethodDef methods[] = { {"_burnin", _burnin, METH_VARARGS, "This function 'burns in' MCMC chains." }, @@ -133,9 +125,17 @@ static struct PyMethodDef BUmethods[] = { }; -void init_bayespputils(void); -void init_bayespputils(void) +static PyModuleDef moduledef = { + PyModuleDef_HEAD_INIT, + "_bayespputils", + "This module provides C extensions for Bayesian analysis and post-processing codes.", + -1, methods, NULL, NULL, NULL, NULL +}; + + +PyMODINIT_FUNC PyInit__bayespputils(void); /* Silence -Wmissing-prototypes */ +PyMODINIT_FUNC PyInit__bayespputils(void) { - (void)Py_InitModule3(MODULE_NAME,BUmethods,BUDocstring); import_array(); + return PyModule_Create(&moduledef); } diff --git a/lalinference/python/lalinference/plot/spindisk.py b/lalinference/python/lalinference/plot/spindisk.py index d750c863aa..779dc72234 100644 --- a/lalinference/python/lalinference/plot/spindisk.py +++ b/lalinference/python/lalinference/plot/spindisk.py @@ -24,7 +24,7 @@ def make_disk_plot(post,outpath=None): try: import corner except ImportError: - print "cannot import corner. Won't plot spin disk" + print("cannot import corner. Won't plot spin disk") return None a1='a1' @@ -38,7 +38,7 @@ def make_disk_plot(post,outpath=None): if not i in names: allin*=0.0 if allin==0.0: - print "Cannot plot spin disk plot. Not all required spin parameters exist in the posterior file. Skipping...\n" + print("Cannot plot spin disk plot. Not all required spin parameters exist in the posterior file. Skipping...\n") return None # Spin disk plot -- GitLab