-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
modified read_n also for File and RawFile
- Loading branch information
1 parent
996a886
commit 8bf9ac5
Showing
11 changed files
with
217 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
from . import _aare | ||
import numpy as np | ||
from .ScanParameters import ScanParameters | ||
|
||
class RawFile(_aare.RawFile): | ||
def __init__(self, fname, chunk_size = 1): | ||
super().__init__(fname) | ||
self._chunk_size = chunk_size | ||
|
||
|
||
def read(self) -> tuple: | ||
"""Read the entire file. | ||
Seeks to the beginning of the file before reading. | ||
Returns: | ||
tuple: header, data | ||
""" | ||
self.seek(0) | ||
return self.read_n(self.total_frames) | ||
|
||
@property | ||
def scan_parameters(self): | ||
"""Return the scan parameters. | ||
Returns: | ||
ScanParameters: Scan parameters. | ||
""" | ||
return ScanParameters(self.master.scan_parameters) | ||
|
||
@property | ||
def master(self): | ||
"""Return the master file. | ||
Returns: | ||
RawMasterFile: Master file. | ||
""" | ||
return super().master() | ||
|
||
def __len__(self) -> int: | ||
"""Return the number of frames in the file. | ||
Returns: | ||
int: Number of frames in file. | ||
""" | ||
return super().frames_in_file | ||
|
||
def __enter__(self): | ||
return self | ||
|
||
def __exit__(self, exc_type, exc_value, traceback): | ||
pass | ||
|
||
def __iter__(self): | ||
return self | ||
|
||
def __next__(self): | ||
try: | ||
if self._chunk_size == 1: | ||
return self.read_frame() | ||
else: | ||
return self.read_n(self._chunk_size) | ||
|
||
|
||
except RuntimeError: | ||
# TODO! find a good way to check that we actually have the right exception | ||
raise StopIteration |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import numpy as np | ||
|
||
def random_pixels(n_pixels, xmin=0, xmax=512, ymin=0, ymax=1024): | ||
"""Return a list of random pixels. | ||
Args: | ||
n_pixels (int): Number of pixels to return. | ||
rows (int): Number of rows in the image. | ||
cols (int): Number of columns in the image. | ||
Returns: | ||
list: List of (row, col) tuples. | ||
""" | ||
return [(np.random.randint(ymin, ymax), np.random.randint(xmin, xmax)) for _ in range(n_pixels)] | ||
|
||
|
||
def random_pixel(xmin=0, xmax=512, ymin=0, ymax=1024): | ||
"""Return a random pixel. | ||
Returns: | ||
tuple: (row, col) | ||
""" | ||
return random_pixels(1, xmin, xmax, ymin, ymax)[0] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
|
||
#include "aare/CtbRawFile.hpp" | ||
#include "aare/File.hpp" | ||
#include "aare/Frame.hpp" | ||
#include "aare/RawFile.hpp" | ||
#include "aare/RawMasterFile.hpp" | ||
#include "aare/RawSubFile.hpp" | ||
|
||
#include "aare/defs.hpp" | ||
// #include "aare/fClusterFileV2.hpp" | ||
|
||
#include <cstdint> | ||
#include <filesystem> | ||
#include <pybind11/iostream.h> | ||
#include <pybind11/numpy.h> | ||
#include <pybind11/pybind11.h> | ||
#include <pybind11/stl.h> | ||
#include <pybind11/stl/filesystem.h> | ||
#include <string> | ||
|
||
namespace py = pybind11; | ||
using namespace ::aare; | ||
|
||
void define_ctb_raw_file_io_bindings(py::module &m) { | ||
|
||
py::class_<CtbRawFile>(m, "CtbRawFile") | ||
.def(py::init<const std::filesystem::path &>()) | ||
.def("read_frame", | ||
[](CtbRawFile &self) { | ||
size_t image_size = self.image_size_in_bytes(); | ||
py::array image; | ||
std::vector<ssize_t> shape; | ||
shape.reserve(2); | ||
shape.push_back(1); | ||
shape.push_back(image_size); | ||
|
||
py::array_t<DetectorHeader> header(1); | ||
|
||
// always read bytes | ||
image = py::array_t<uint8_t>(shape); | ||
|
||
self.read_into( | ||
reinterpret_cast<std::byte *>(image.mutable_data()), | ||
header.mutable_data()); | ||
|
||
return py::make_tuple(header, image); | ||
}) | ||
.def("seek", &CtbRawFile::seek) | ||
.def("tell", &CtbRawFile::tell) | ||
.def("master", &CtbRawFile::master) | ||
|
||
.def_property_readonly("image_size_in_bytes", | ||
&CtbRawFile::image_size_in_bytes) | ||
|
||
.def_property_readonly("frames_in_file", &CtbRawFile::frames_in_file); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters