From a4975ee4430f9bc0d28f204886d05621da9671ed Mon Sep 17 00:00:00 2001 From: David Graham Date: Thu, 19 Jun 2025 17:33:52 -0400 Subject: [PATCH 1/4] minimal working example --- pyproject.toml | 2 +- src/core/bindings.cpp | 2 +- src/core/gather/gather.hpp | 3 +++ tests/fixtures/testTrace.bin | Bin 0 -> 32 bytes tests/libseis/test_gather.py | 2 +- 5 files changed, 6 insertions(+), 3 deletions(-) create mode 100644 tests/fixtures/testTrace.bin diff --git a/pyproject.toml b/pyproject.toml index e26d86e..00836d0 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -48,7 +48,7 @@ cache-keys = [ [tool.scikit-build] wheel.expand-macos-universal-tags = true minimum-version = "build-system.requires" -build-dir = "build/" +build-dir = "build/{wheel_tag}" [tool.pytest.ini_options] diff --git a/src/core/bindings.cpp b/src/core/bindings.cpp index 5f685c6..c0ae0e0 100644 --- a/src/core/bindings.cpp +++ b/src/core/bindings.cpp @@ -41,9 +41,9 @@ PYBIND11_MODULE(_gather, m) { py::class_(m, "Gather") .def(py::init()) .def_readwrite("id", &Gather::id) - .def_readwrite("dt", &Gather::dt) .def_readwrite("nt", &Gather::nt) .def_readwrite("nx", &Gather::nx) + .def_readwrite("dt", &Gather::dt) .def_readonly("data", &Gather::data) .def("__str__", &Gather::display) .def("from_bin_file", &Gather::from_bin_file); diff --git a/src/core/gather/gather.hpp b/src/core/gather/gather.hpp index 6c179f0..5f75fc1 100644 --- a/src/core/gather/gather.hpp +++ b/src/core/gather/gather.hpp @@ -18,6 +18,9 @@ class Gather { double dt; std::vector data; + // Gather() = default; + Gather() {} + Gather(const int id, const int nt, const int nx, const double dt) : id(id), nt(nt), nx(nx), dt(dt), data() {} diff --git a/tests/fixtures/testTrace.bin b/tests/fixtures/testTrace.bin new file mode 100644 index 0000000000000000000000000000000000000000..a5bbb2c24976ec7da1067537cf630d7d5fd21253 GIT binary patch literal 32 kcma!_x&4*Z78M8fbCQ9#q(u%efB}a?gU9|_*&PfA0Ku#Z)c^nh literal 0 HcmV?d00001 diff --git a/tests/libseis/test_gather.py b/tests/libseis/test_gather.py index 0684690..54c154c 100644 --- a/tests/libseis/test_gather.py +++ b/tests/libseis/test_gather.py @@ -9,7 +9,7 @@ class TestSeismicGather: @pytest.fixture def gather(self): """Fixture to create a Gather instance.""" - return Gather() + return Gather(1, 2, 3, 4.0) def test_gather_init(self, gather): """Test the Gather class base constructor.""" From 17342c11b526652afa694b02f5690ae53a7f3ed7 Mon Sep 17 00:00:00 2001 From: David Graham Date: Thu, 19 Jun 2025 17:54:20 -0400 Subject: [PATCH 2/4] transform Gather init to use kwargs --- src/core/bindings.cpp | 4 +++- src/libseis/_gather.pyi | 4 +++- tests/libseis/test_gather.py | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/core/bindings.cpp b/src/core/bindings.cpp index c0ae0e0..3610105 100644 --- a/src/core/bindings.cpp +++ b/src/core/bindings.cpp @@ -39,7 +39,9 @@ PYBIND11_MODULE(_num, m) { PYBIND11_MODULE(_gather, m) { py::class_(m, "Gather") - .def(py::init()) + // .def(py::init()) + .def(py::init(), py::arg("id"), py::arg("nt"), + py::arg("nx"), py::arg("dt")) .def_readwrite("id", &Gather::id) .def_readwrite("nt", &Gather::nt) .def_readwrite("nx", &Gather::nx) diff --git a/src/libseis/_gather.pyi b/src/libseis/_gather.pyi index 839be4c..972f1eb 100644 --- a/src/libseis/_gather.pyi +++ b/src/libseis/_gather.pyi @@ -11,7 +11,9 @@ class Gather: dt: float data: list[float] - def __init__(self, id: int = 0, nt: int = 0, nx: int = 0) -> Self: ... + def __init__( + self, id: int = 0, nt: int = 0, nx: int = 0, dt: float = 0 + ) -> Self: ... @staticmethod def from_bin_file( filename: str, diff --git a/tests/libseis/test_gather.py b/tests/libseis/test_gather.py index 54c154c..b994cac 100644 --- a/tests/libseis/test_gather.py +++ b/tests/libseis/test_gather.py @@ -9,7 +9,7 @@ class TestSeismicGather: @pytest.fixture def gather(self): """Fixture to create a Gather instance.""" - return Gather(1, 2, 3, 4.0) + return Gather(id=1, nt=2, nx=3, dt=4.0) def test_gather_init(self, gather): """Test the Gather class base constructor.""" From 7de776c000974fbf30d971a8e50e1a5ba3c4b446 Mon Sep 17 00:00:00 2001 From: David Graham Date: Thu, 19 Jun 2025 18:16:11 -0400 Subject: [PATCH 3/4] use brace initialization consistently --- src/core/gather/gather.hpp | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/core/gather/gather.hpp b/src/core/gather/gather.hpp index 5f75fc1..7e6c097 100644 --- a/src/core/gather/gather.hpp +++ b/src/core/gather/gather.hpp @@ -12,17 +12,18 @@ class Gather { public: - int id; - int nt; - int nx; - double dt; + int id{}; + int nt{}; + int nx{}; + double dt{}; std::vector data; - // Gather() = default; - Gather() {} + Gather() = default; Gather(const int id, const int nt, const int nx, const double dt) - : id(id), nt(nt), nx(nx), dt(dt), data() {} + : id{id}, nt{nt}, nx{nx}, dt{dt} { + data.resize(nt * nx); + } [[nodiscard]] std::string display() const { return "Gather id: " + std::to_string(id); From e8f8957d277cc4580cacaf59768fd3869254b7c5 Mon Sep 17 00:00:00 2001 From: David Graham Date: Thu, 19 Jun 2025 18:45:13 -0400 Subject: [PATCH 4/4] add inline documentation to Gather class --- src/core/bindings.cpp | 2 +- src/core/gather/gather.hpp | 24 +++++++++++++++++++++++- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/core/bindings.cpp b/src/core/bindings.cpp index 3610105..2886e56 100644 --- a/src/core/bindings.cpp +++ b/src/core/bindings.cpp @@ -47,6 +47,6 @@ PYBIND11_MODULE(_gather, m) { .def_readwrite("nx", &Gather::nx) .def_readwrite("dt", &Gather::dt) .def_readonly("data", &Gather::data) - .def("__str__", &Gather::display) + .def("__str__", &Gather::str) .def("from_bin_file", &Gather::from_bin_file); } diff --git a/src/core/gather/gather.hpp b/src/core/gather/gather.hpp index 7e6c097..52fb0d7 100644 --- a/src/core/gather/gather.hpp +++ b/src/core/gather/gather.hpp @@ -25,10 +25,32 @@ class Gather { data.resize(nt * nx); } - [[nodiscard]] std::string display() const { + /** + * @brief a helper function used for Python __str__ method. + */ + [[nodiscard]] std::string str() const { return "Gather id: " + std::to_string(id); } + /** + * @brief overload the output stream operator for Gather. + */ + friend std::ostream &operator<<(std::ostream &os, const Gather &gather) { + os << "Gather id: " << gather.id << std::endl; + return os; + } + + /** + * @brief Read a binary file and create a Gather object. + * + * This function takes a file path (string), the number of time samples (nt), + * and the number of traces (nx) to read from the binary file. + * + * @param path the path to the binary file. + * @param nt the number of time samples per trace. + * @param nx the number of traces per gather. + * @return Gather object populated with data from the file. + */ static Gather from_bin_file(const std::string &path, int nt, int nx, double dt = 0.0) { std::ifstream file(path, std::ios::binary);