diff --git a/core/include/aare/Frame.hpp b/core/include/aare/Frame.hpp index e33563f..41c9b68 100644 --- a/core/include/aare/Frame.hpp +++ b/core/include/aare/Frame.hpp @@ -22,6 +22,7 @@ template class Frame { Frame(ssize_t rows, ssize_t cols); Frame(std::byte *fp, ssize_t rows, ssize_t cols); DataType get(int row, int col); + std::vector> get_array(); ssize_t rows() const{ return m_rows; } diff --git a/core/src/Frame.cpp b/core/src/Frame.cpp index 28d72b7..3d4e940 100644 --- a/core/src/Frame.cpp +++ b/core/src/Frame.cpp @@ -25,5 +25,17 @@ DataType Frame::get(int row, int col) { return m_data[row*m_cols + col]; } +template +std::vector> Frame::get_array() { + std::vector> array; + for (int i = 0; i < m_rows; i++) { + std::vector row; + row.assign(m_data + i*m_cols, m_data + (i+1)*m_cols); + array.push_back(row); + } + + return array; +} + template class Frame; diff --git a/python/aare/File.py b/python/aare/File.py index da454e9..fe8a026 100644 --- a/python/aare/File.py +++ b/python/aare/File.py @@ -21,7 +21,7 @@ class File: raise FileNotFoundError(f"File not found: {path}") ext = os.path.splitext(path)[1] - if ext not in (".raw", ".json"): + if ext not in (".raw", ".json", ".npy"): raise ValueError(f"Invalid file extension: {ext}") if ext == ".json": @@ -33,6 +33,17 @@ class File: bitdepth = 16 else: bitdepth = master_data["Dynamic Range"] + elif ext == ".npy": + # TODO: find solution for this. maybe add a None detector type + detector = "Jungfrau" + with open(path, "rb") as fobj: + import numpy as np + version = np.lib.format.read_magic(fobj) + # find what function to call based on the version + func_name = 'read_array_header_' + '_'.join(str(v) for v in version) + func = getattr(np.lib.format, func_name) + header = func(fobj) + bitdepth = header[2].itemsize * 8 else: NotImplementedError("Raw file not implemented yet") diff --git a/python/example/read_frame.py b/python/example/read_frame.py index 293ac45..2191305 100644 --- a/python/example/read_frame.py +++ b/python/example/read_frame.py @@ -1,13 +1,32 @@ import os from pathlib import Path from aare import File, Frame +import numpy as np if __name__ == "__main__": + + #get env variable root_dir = Path(os.environ.get("PROJECT_ROOT_DIR")) + + # read JSON master file data_path = str(root_dir / "data"/"jungfrau_single_master_0.json") file = File(data_path) frame = file.get_frame(0) print(frame.rows, frame.cols) - print(frame.get(0,0)) \ No newline at end of file + print(frame.get(0,0)) + + # read Numpy file + + data_path = str(root_dir / "data"/"test_numpy_file.npy") + file = File(data_path) + frame = file.get_frame(0) + print(frame.rows, frame.cols) + print(frame.get(0,0)) + + arr = np.array(frame.get_array()) + print(arr) + print(arr.shape) + + print(np.array_equal(arr, np.load(data_path)[0])) \ No newline at end of file diff --git a/python/src/bindings.cpp b/python/src/bindings.cpp index 91c880b..d14b0ee 100644 --- a/python/src/bindings.cpp +++ b/python/src/bindings.cpp @@ -1,6 +1,7 @@ #include #include #include +#include #include #include "aare/defs.hpp" @@ -27,10 +28,13 @@ PYBIND11_MODULE(_aare, m) { py::class_>(m, "_Frame16") .def(py::init()) .def("get", &Frame::get) + .def("get_array", &Frame::get_array) .def_property_readonly("rows", &Frame::rows) .def_property_readonly("cols", &Frame::cols) .def_property_readonly("bitdepth", &Frame::bitdepth); + +