From 68829bd771f044137c7f5485f351dd3309a82426 Mon Sep 17 00:00:00 2001 From: michael-petersen Date: Fri, 15 Dec 2023 07:10:44 -0500 Subject: [PATCH 1/9] Expose assign methods --- coefs/Coefficients.H | 3 ++- pyEXP/CoefWrappers.cc | 30 +++++++++++++++++++++++++++--- 2 files changed, 29 insertions(+), 4 deletions(-) diff --git a/coefs/Coefficients.H b/coefs/Coefficients.H index a2345075d..8f369f069 100644 --- a/coefs/Coefficients.H +++ b/coefs/Coefficients.H @@ -2,6 +2,7 @@ #define _COEFFICIENTS_H #include +#include // Needed by member functions for writing parameters and stanzas #include @@ -604,7 +605,7 @@ namespace CoefClasses if (d=='x') return NmaxX; if (d=='y') return NmaxY; if (d=='z') return NmaxZ; - std::runtime_error("CubeCoefs: error in nmax accessor"); + throw std::runtime_error("CubeCoefs: error in nmax accessor"); } }; diff --git a/pyEXP/CoefWrappers.cc b/pyEXP/CoefWrappers.cc index 835a4c2de..7067b4041 100644 --- a/pyEXP/CoefWrappers.cc +++ b/pyEXP/CoefWrappers.cc @@ -608,13 +608,37 @@ void CoefficientClasses(py::module &m) { py::class_, CoefStruct>(m, "SphStruct") - .def(py::init<>(), "Spherical coefficient data structure object"); + .def(py::init<>(), "Spherical coefficient data structure object") + .def("assign", &SphStruct::assign, + R"( + Assign a coefficient matrix to CoefStruct. + + Returns + ------- + None + )"); py::class_, CoefStruct>(m, "CylStruct") - .def(py::init<>(), "Cylindrical coefficient data structure object"); + .def(py::init<>(), "Cylindrical coefficient data structure object") + .def("assign", &CylStruct::assign, + R"( + Assign a coefficient matrix to CoefStruct. + + Returns + ------- + None + )"); py::class_, CoefStruct>(m, "TblStruct") - .def(py::init<>(), "Multicolumn table data structure object"); + .def(py::init<>(), "Multicolumn table data structure object") + .def("assign", &TblStruct::assign, + R"( + Assign a coefficient matrix to CoefStruct. + + Returns + ------- + None + )"); py::class_, PyCoefs>(m, "Coefs") .def(py::init(), From ee8e38e1fcc965b7dc6f7b840ec2a90ba3c25e9e Mon Sep 17 00:00:00 2001 From: "Martin D. Weinberg" Date: Fri, 15 Dec 2023 07:47:51 -0500 Subject: [PATCH 2/9] Set cols value in assign from array size --- coefs/CoefStruct.H | 1 + 1 file changed, 1 insertion(+) diff --git a/coefs/CoefStruct.H b/coefs/CoefStruct.H index 8cf3a54e2..3ac3bec89 100644 --- a/coefs/CoefStruct.H +++ b/coefs/CoefStruct.H @@ -295,6 +295,7 @@ namespace CoefClasses //! Assign array void assign(const Eigen::VectorXcd& arr) { + cols = arr.size(); store = arr; coefs = std::make_shared(store.data(), store.size()); } From f6d318f7579379118c9ae98613d6a718f00c91f2 Mon Sep 17 00:00:00 2001 From: "Martin D. Weinberg" Date: Fri, 15 Dec 2023 22:41:18 -0500 Subject: [PATCH 3/9] Mistake copying the coefficients to the return matrix in TableData; TblStruct::coefs is a vector not a matrix --- coefs/Coefficients.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/coefs/Coefficients.cc b/coefs/Coefficients.cc index 4a15e2510..3be56da2b 100644 --- a/coefs/Coefficients.cc +++ b/coefs/Coefficients.cc @@ -1495,7 +1495,7 @@ namespace CoefClasses for (int t=0; tcoefs)(0, c).real(); + ret(c, t) = (*cof->coefs)(c).real(); } } From beaf93bc825f4a3d95fd530e773fb2e4e42d89e3 Mon Sep 17 00:00:00 2001 From: "Martin D. Weinberg" Date: Fri, 15 Dec 2023 22:46:52 -0500 Subject: [PATCH 4/9] Expose the ascii file constructor to Python --- pyEXP/CoefWrappers.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/pyEXP/CoefWrappers.cc b/pyEXP/CoefWrappers.cc index 7067b4041..eb33d5c7f 100644 --- a/pyEXP/CoefWrappers.cc +++ b/pyEXP/CoefWrappers.cc @@ -1149,6 +1149,7 @@ void CoefficientClasses(py::module &m) { py::class_, PyTableData, CoefClasses::Coefs>(m, "TableData", "Container for simple data tables with multiple columns") .def(py::init()) + .def(py::init()) .def("getAllCoefs", &CoefClasses::TableData::getAllCoefs, R"( Return a 2-dimensional ndarray indexed by column and time From f0d645897dfb40160d19637a59416299d17635aa Mon Sep 17 00:00:00 2001 From: "Martin D. Weinberg" Date: Fri, 15 Dec 2023 22:56:36 -0500 Subject: [PATCH 5/9] Expose ascii file constructor for TableData in Python --- pyEXP/CoefWrappers.cc | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/pyEXP/CoefWrappers.cc b/pyEXP/CoefWrappers.cc index eb33d5c7f..7ea6ac01e 100644 --- a/pyEXP/CoefWrappers.cc +++ b/pyEXP/CoefWrappers.cc @@ -1148,8 +1148,34 @@ void CoefficientClasses(py::module &m) { py::class_, PyTableData, CoefClasses::Coefs>(m, "TableData", "Container for simple data tables with multiple columns") - .def(py::init()) - .def(py::init()) + .def(py::init(), + R"( + Construct a null TableData object + + Parameters + ---------- + verbose : bool + display verbose information. + + Returns + ------- + TableData instance + )") + .def(py::init(), + R"( + Construct a TableData object from a data file + + Parameters + ---------- + type : str + ascii table data file + verbose : bool + display verbose information. + + Returns + ------- + TableData instance + )") .def("getAllCoefs", &CoefClasses::TableData::getAllCoefs, R"( Return a 2-dimensional ndarray indexed by column and time From f6f90024fe839e689eeb28664ea81e29d574b0e9 Mon Sep 17 00:00:00 2001 From: michael-petersen Date: Sat, 16 Dec 2023 07:21:24 -0500 Subject: [PATCH 6/9] documentation only (for initialisations) --- pyEXP/CoefWrappers.cc | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/pyEXP/CoefWrappers.cc b/pyEXP/CoefWrappers.cc index 7ea6ac01e..6854cd51b 100644 --- a/pyEXP/CoefWrappers.cc +++ b/pyEXP/CoefWrappers.cc @@ -943,7 +943,19 @@ void CoefficientClasses(py::module &m) { py::arg("coef"), py::arg("name")=""); py::class_, PySphCoefs, CoefClasses::Coefs>(m, "SphCoefs", "Container for spherical coefficients") - .def(py::init()) + .def(py::init(), + R"( + Construct a null SphCoefs object + + Parameters + ---------- + verbose : bool + display verbose information. + + Returns + ------- + SphCoefs instance + )") .def("__call__", &CoefClasses::SphCoefs::getMatrix, R"( @@ -1004,7 +1016,19 @@ void CoefficientClasses(py::module &m) { )"); py::class_, PyCylCoefs, CoefClasses::Coefs>(m, "CylCoefs", "Container for cylindrical coefficients") - .def(py::init()) + .def(py::init(), + R"( + Construct a null CylCoefs object + + Parameters + ---------- + verbose : bool + display verbose information. + + Returns + ------- + CylCoefs instance + )") .def("__call__", &CoefClasses::CylCoefs::getMatrix, R"( @@ -1091,7 +1115,19 @@ void CoefficientClasses(py::module &m) { py::class_, PyCubeCoefs, CoefClasses::Coefs>(m, "CubeCoefs", "Container for cube coefficients") - .def(py::init()) + .def(py::init(), + R"( + Construct a null CubeCoefs object + + Parameters + ---------- + verbose : bool + display verbose information. + + Returns + ------- + CubeCoefs instance + )") .def("__call__", &CoefClasses::CubeCoefs::getTensor, R"( From 10bfbccb67030681cabf968db62ae0dbcf73fd6b Mon Sep 17 00:00:00 2001 From: "Martin D. Weinberg" Date: Sat, 16 Dec 2023 08:57:42 -0500 Subject: [PATCH 7/9] Expose the assign menthod for Cube struct --- pyEXP/CoefWrappers.cc | 51 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 47 insertions(+), 4 deletions(-) diff --git a/pyEXP/CoefWrappers.cc b/pyEXP/CoefWrappers.cc index 7ea6ac01e..6da65b141 100644 --- a/pyEXP/CoefWrappers.cc +++ b/pyEXP/CoefWrappers.cc @@ -25,11 +25,11 @@ void CoefficientClasses(py::module &m) { "maintained, and interfaced by the Coefs class. Access to the\n" "underlying data is provided to Python in case you need to change\n" "or rewrite the data for some reason. We have also provided a\n" - "create() member so that you can instaniate and load a coefficient\n" + "assign() member so that you can instaniate and load a coefficient\n" "structure using Python. To do this, use the constructor to make\n" - "a blank instance, assign the dimensions and use create() to create\n" - "a data matrix of initially zero values. The dimensions are \n" - "(lmax, nmax) for SphStruct, (mmax,nmax) for a CylStruct, and\n" + "a blank instance, assign the dimensions and use assign() to create\n" + "a data matrix with the supplied matrix or array. The dimensions are\n" + "(lmax, nmax) for SphStruct, (mmax, nmax) for a CylStruct, and\n" "(cols) for a TblStruct.\n\n" "Coefs\n" "-----\n" @@ -613,6 +613,15 @@ void CoefficientClasses(py::module &m) { R"( Assign a coefficient matrix to CoefStruct. + Parameters + ---------- + mat : numpy.ndarray + Matrix of coefficients + lmax : int + angular order + nmax : int + radial order + Returns ------- None @@ -624,17 +633,51 @@ void CoefficientClasses(py::module &m) { R"( Assign a coefficient matrix to CoefStruct. + Parameters + ---------- + mat : numpy.ndarray + Matrix of coefficients + mmax : int + angular order + nmax : int + radial order + Returns ------- None )"); + py::class_, CoefStruct>(m, "CubeStruct") + .def(py::init<>(), "Cube coefficient data structure object") + .def("assign", &CubeStruct::assign, + R"( + Assign a coefficient matrix to CoefStruct. + + Parameters + ---------- + mat : numpy.ndarray, complex + complex-valued NumPy tensor of coefficient values + + Returns + ------- + None + + Notes + ----- + The dimensions are inferred from the 3-dimensional NumPy array (tensor) + )"); + py::class_, CoefStruct>(m, "TblStruct") .def(py::init<>(), "Multicolumn table data structure object") .def("assign", &TblStruct::assign, R"( Assign a coefficient matrix to CoefStruct. + Parameters + ---------- + mat : numpy.ndarray, complex + complex-valued NumPy array of table values + Returns ------- None From 45142c7b0659a80bdd61a9cffbe5f6dea74009c6 Mon Sep 17 00:00:00 2001 From: "Martin D. Weinberg" Date: Sat, 16 Dec 2023 09:06:23 -0500 Subject: [PATCH 8/9] Updated documentation for the inclusion of CubeCoefs; and a few typo fixes --- pyEXP/CoefWrappers.cc | 54 ++++++------------------------------------- 1 file changed, 7 insertions(+), 47 deletions(-) diff --git a/pyEXP/CoefWrappers.cc b/pyEXP/CoefWrappers.cc index f659df8b6..7deec626e 100644 --- a/pyEXP/CoefWrappers.cc +++ b/pyEXP/CoefWrappers.cc @@ -18,8 +18,8 @@ void CoefficientClasses(py::module &m) { "----------\n" "The CoefStruct class is low-level structure that stores the data\n" "and metadata specific to each geometry. These are spherical\n" - "(SphStruct), cylindrical (CylStruct), and table data (TblStruct).\n" - "EXP also knows about rectangular grids and slabs. These may be\n" + "(SphStruct), cylindrical (CylStruct), cube (CubeStruct) and table\n" + "data (TblStruct). EXP also knows about slabs. These may be\n" "added in a future release if there is a need. Instances of these\n" "structures represent individual times points and are created,\n" "maintained, and interfaced by the Coefs class. Access to the\n" @@ -29,14 +29,14 @@ void CoefficientClasses(py::module &m) { "structure using Python. To do this, use the constructor to make\n" "a blank instance, assign the dimensions and use assign() to create\n" "a data matrix with the supplied matrix or array. The dimensions are\n" - "(lmax, nmax) for SphStruct, (mmax, nmax) for a CylStruct, and\n" - "(cols) for a TblStruct.\n\n" + "(lmax, nmax) for SphStruct, (mmax, nmax) for a CylStruct, (nmaxx,\n" + "nmaxy, nmaxz) for a CubeStruct and (cols) for a TblStruct.\n\n" "Coefs\n" "-----\n" "The base class, 'Coefs', provides a factory reader that will\n" - "create one of the derived coefficient classes, SphCoef, CylCoef,\n" - "or TblCoef, deducing the type from the input file. The input\n" - "files may be EXP native or HDF5 cofficient files. The Basis\n" + "create one of the derived coefficient classes, SphCoefs, CylCoefs,\n" + "CubeCoefs, or TblCoefs, deducing the type from the input file. The\n" + "input files may be EXP native or HDF5 cofficient files. The Basis\n" "factory, Basis::createCoefficients, will create set of coef-\n" "ficients from phase-space snapshots. See help(pyEXP.basis).\n" "Files which are not recognized as EXP coefficient files are\n" @@ -134,14 +134,6 @@ void CoefficientClasses(py::module &m) { PYBIND11_OVERRIDE_PURE(void, Coefs, setData, time, array); } - // This has left the interface - /* - using ValueError = std::tuple; - ValueError interpolate(double time) override { - PYBIND11_OVERRIDE(ValueError, Coefs, interpolate, time); - } - */ - std::shared_ptr getCoefStruct(double time) override { PYBIND11_OVERRIDE_PURE(std::shared_ptr, Coefs, getCoefStruct, time); } @@ -223,14 +215,6 @@ void CoefficientClasses(py::module &m) { time); } - // No longer in the abstract interface - /* - void dump(int mmin, int mmax, int nmin, int nmax) override { - PYBIND11_OVERRIDE(void, SphCoefs,dump, - mmin, mmax, nmin, nmax); - } - */ - std::vector Times() override { PYBIND11_OVERRIDE(std::vector, SphCoefs, Times,); } @@ -310,14 +294,6 @@ void CoefficientClasses(py::module &m) { time); } - // Left the interface - /* - void dump(int mmin, int mmax, int nmin, int nmax) override { - PYBIND11_OVERRIDE(void, CylCoefs, dump, - mmin, mmax, nmin, nmax); - } - */ - std::vector Times() override { PYBIND11_OVERRIDE(std::vector, CylCoefs, Times,); } @@ -396,14 +372,6 @@ void CoefficientClasses(py::module &m) { time); } - // Left the interface - /* - void dump(int mmin, int mmax, int nmin, int nmax) override { - PYBIND11_OVERRIDE(void, CubeCoefs,dump, - mmin, mmax, nmin, nmax); - } - */ - std::vector Times() override { PYBIND11_OVERRIDE(std::vector, CubeCoefs, Times,); } @@ -483,14 +451,6 @@ void CoefficientClasses(py::module &m) { time); } - // No longer in the interface - /* - void dump(int mmin, int mmax, int nmin, int nmax) override { - PYBIND11_OVERRIDE(void, TableData, dump, - mmin, mmax, nmin, nmax); - } - */ - std::vector Times() override { PYBIND11_OVERRIDE(std::vector, TableData, Times,); } From b062ef080b7015446f8081507ab3fa72aa4ef5e8 Mon Sep 17 00:00:00 2001 From: "Martin D. Weinberg" Date: Sat, 16 Dec 2023 09:16:07 -0500 Subject: [PATCH 9/9] Manually add default TableData constructor without boolean verbose flag - User can simply ignore the boolean - For example: coefsread = pyEXP.coefs.TableData('coef.txt') --- pyEXP/CoefWrappers.cc | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/pyEXP/CoefWrappers.cc b/pyEXP/CoefWrappers.cc index 7deec626e..efe8f9232 100644 --- a/pyEXP/CoefWrappers.cc +++ b/pyEXP/CoefWrappers.cc @@ -1196,6 +1196,19 @@ void CoefficientClasses(py::module &m) { verbose : bool display verbose information. + Returns + ------- + TableData instance + )") + .def(py::init(), + R"( + Construct a TableData object from a data file + + Parameters + ---------- + type : str + ascii table data file + Returns ------- TableData instance