Skip to content

Commit

Permalink
read_n returns remaining frames (#105)
Browse files Browse the repository at this point in the history
Modified read_n to return the number of frames available if less than
the number of frames requested.

```python
#f is a CtbRawFile containing 10 frames

f.read_n(7) # you get 7 frames
f.read_n(7) # you get 3 frames
f.read_n(7) # RuntimeError
```

Also added support for chunk_size when iterating over a file:

```python
# The file contains 10 frames


with CtbRawFile(fname, chunk_size = 7) as f:
    for headers, frames in f:
        #do something with the data
        # 1 iteration 7 frames
        # 2 iteration 3 frames
        # 3 iteration stops
```
  • Loading branch information
JulianHeymes authored Nov 26, 2024
2 parents a3f813f + 8e3d997 commit 06670a7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
25 changes: 22 additions & 3 deletions python/aare/CtbRawFile.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from . import _aare
import numpy as np
from .ScanParameters import ScanParameters

class CtbRawFile(_aare.CtbRawFile):
"""File reader for the CTB raw file format.
Expand All @@ -11,9 +12,10 @@ class CtbRawFile(_aare.CtbRawFile):
The function should take a numpy array of type uint8 and return one
or several numpy arrays.
"""
def __init__(self, fname, transform = None):
def __init__(self, fname, transform = None, chunk_size = 1):
super().__init__(fname)
self.transform = transform
self._chunk_size = chunk_size


def read_frame(self, frame_index: int | None = None ) -> tuple:
Expand Down Expand Up @@ -59,6 +61,10 @@ def read_n(self, n_frames:int) -> tuple:
Uses the position of the file pointer :py:meth:`~CtbRawFile.tell` to determine
where to start reading from.
If the number of frames requested is larger than the number of frames left in the file,
the function will read the remaining frames. If no frames are left in the file
a RuntimeError is raised.
Args:
n_frames (int): Number of frames to read.
Expand All @@ -68,6 +74,12 @@ def read_n(self, n_frames:int) -> tuple:
Raises:
RuntimeError: If EOF is reached.
"""
# Calculate the number of frames to actually read
n_frames = min(n_frames, self.frames_in_file - self.tell())
if n_frames == 0:
raise RuntimeError("No frames left in file.")


# Do the first read to figure out what we have
tmp_header, tmp_data = self.read_frame()

Expand All @@ -87,10 +99,12 @@ def read_n(self, n_frames:int) -> tuple:

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.frames_in_file)

def seek(self, frame_index:int) -> None:
Expand All @@ -101,7 +115,7 @@ def seek(self, frame_index:int) -> None:
"""
super().seek(frame_index)

def tell() -> int:
def tell(self) -> int:
"""Return the current frame position in the file.
Returns:
Expand Down Expand Up @@ -164,7 +178,12 @@ def __iter__(self):

def __next__(self):
try:
return self.read_frame()
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
Expand Down
2 changes: 1 addition & 1 deletion src/CtbRawFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ CtbRawFile::CtbRawFile(const std::filesystem::path &fname) : m_master(fname) {

void CtbRawFile::read_into(std::byte *image_buf, DetectorHeader* header) {
if(m_current_frame >= m_master.frames_in_file()){
throw std::runtime_error(LOCATION + "End of file reached");
throw std::runtime_error(LOCATION + " End of file reached");
}

if(m_current_frame != 0 && m_current_frame % m_master.max_frames_per_file() == 0){
Expand Down

0 comments on commit 06670a7

Please sign in to comment.