diff --git a/.github/workflows/build_pkg.yml b/.github/workflows/build_pkg.yml deleted file mode 100644 index 2534a867..00000000 --- a/.github/workflows/build_pkg.yml +++ /dev/null @@ -1,43 +0,0 @@ -name: Build packages but don't deploy - -on: - workflow_dispatch: - push: - branches: - - main - - developer - - #run on PRs as well? - -jobs: - build: - strategy: - fail-fast: false - matrix: - platform: [ubuntu-latest, ] # macos-12, windows-2019] - python-version: ["3.12",] - - runs-on: ${{ matrix.platform }} - - # The setup-miniconda action needs this to activate miniconda - defaults: - run: - shell: "bash -l {0}" - - steps: - - uses: actions/checkout@v4 - - - name: Get conda - uses: conda-incubator/setup-miniconda@v3.0.4 - with: - python-version: ${{ matrix.python-version }} - channels: conda-forge - - - name: Prepare - run: conda install conda-build conda-verify pytest anaconda-client - - - name: Build - env: - CONDA_TOKEN: ${{ secrets.CONDA_TOKEN }} - run: conda build conda-recipe - diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml index ba14770f..81edde3f 100644 --- a/.github/workflows/deploy.yml +++ b/.github/workflows/deploy.yml @@ -1,7 +1,10 @@ -name: Deploy to slsdetectorgroup conda channel +name: Build pkgs and deploy if on main on: - workflow_dispatch + push: + branches: + - main + - developer jobs: build: @@ -28,9 +31,10 @@ jobs: channels: conda-forge - name: Prepare - run: conda install conda-build conda-verify pytest anaconda-client + run: conda install conda-build=24.9 conda-verify pytest anaconda-client - name: Enable upload + if: github.ref == 'refs/heads/main' run: conda config --set anaconda_upload yes - name: Build diff --git a/conda-recipe/meta.yaml b/conda-recipe/meta.yaml index ec1d0dd6..ca5178ef 100644 --- a/conda-recipe/meta.yaml +++ b/conda-recipe/meta.yaml @@ -1,6 +1,6 @@ package: name: aare - version: 2024.11.15.dev0 #TODO! how to not duplicate this? + version: 2024.11.26.dev0 #TODO! how to not duplicate this? source: diff --git a/include/aare/PixelMap.hpp b/include/aare/PixelMap.hpp index 37fee950..1b7a8903 100644 --- a/include/aare/PixelMap.hpp +++ b/include/aare/PixelMap.hpp @@ -7,6 +7,8 @@ namespace aare { NDArray GenerateMoench03PixelMap(); NDArray GenerateMoench05PixelMap(); +NDArray GenerateMoench05PixelMap1g(); +NDArray GenerateMoench05PixelMapOld(); //Matterhorn02 NDArrayGenerateMH02SingleCounterPixelMap(); diff --git a/pyproject.toml b/pyproject.toml index 00230f77..2a90f8af 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -4,7 +4,7 @@ build-backend = "scikit_build_core.build" [project] name = "aare" -version = "2024.11.15.dev0" +version = "2024.11.26.dev0" [tool.scikit-build] diff --git a/python/aare/CtbRawFile.py b/python/aare/CtbRawFile.py index 4684c372..00f48867 100644 --- a/python/aare/CtbRawFile.py +++ b/python/aare/CtbRawFile.py @@ -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. @@ -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: @@ -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. @@ -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() @@ -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: @@ -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: @@ -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 diff --git a/python/aare/transform.py b/python/aare/transform.py index a99f9c49..414eb276 100644 --- a/python/aare/transform.py +++ b/python/aare/transform.py @@ -9,6 +9,24 @@ def __init__(self): def __call__(self, data): return np.take(data.view(np.uint16), self.pixel_map) + + +class Moench05Transform1g: + #Could be moved to C++ without changing the interface + def __init__(self): + self.pixel_map = _aare.GenerateMoench05PixelMap1g() + + def __call__(self, data): + return np.take(data.view(np.uint16), self.pixel_map) + + +class Moench05TransformOld: + #Could be moved to C++ without changing the interface + def __init__(self): + self.pixel_map = _aare.GenerateMoench05PixelMapOld() + + def __call__(self, data): + return np.take(data.view(np.uint16), self.pixel_map) class Matterhorn02Transform: @@ -25,4 +43,6 @@ def __call__(self, data): #on import generate the pixel maps to avoid doing it every time moench05 = Moench05Transform() +moench05_1g = Moench05Transform1g() +moench05_old = Moench05TransformOld() matterhorn02 = Matterhorn02Transform() \ No newline at end of file diff --git a/python/src/pixel_map.hpp b/python/src/pixel_map.hpp index 3a8e366e..46b1bc48 100644 --- a/python/src/pixel_map.hpp +++ b/python/src/pixel_map.hpp @@ -20,6 +20,14 @@ void define_pixel_map_bindings(py::module &m) { .def("GenerateMoench05PixelMap", []() { auto ptr = new NDArray(GenerateMoench05PixelMap()); return return_image_data(ptr); + }) + .def("GenerateMoench05PixelMap1g", []() { + auto ptr = new NDArray(GenerateMoench05PixelMap1g()); + return return_image_data(ptr); + }) + .def("GenerateMoench05PixelMapOld", []() { + auto ptr = new NDArray(GenerateMoench05PixelMapOld()); + return return_image_data(ptr); }) .def("GenerateMH02SingleCounterPixelMap", []() { auto ptr = new NDArray(GenerateMH02SingleCounterPixelMap()); diff --git a/src/CtbRawFile.cpp b/src/CtbRawFile.cpp index 1f3a5074..4d9d8950 100644 --- a/src/CtbRawFile.cpp +++ b/src/CtbRawFile.cpp @@ -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){ diff --git a/src/PixelMap.cpp b/src/PixelMap.cpp index b1a6dc4c..d62759a1 100644 --- a/src/PixelMap.cpp +++ b/src/PixelMap.cpp @@ -31,6 +31,49 @@ NDArray GenerateMoench03PixelMap() { } NDArray GenerateMoench05PixelMap() { + std::array adc_numbers = {5, 9, 1}; + NDArray order_map({160, 150}); + int n_pixel = 0; + for (int row = 0; row < 160; row++) { + for (int i_col = 0; i_col < 50; i_col++) { + n_pixel = row * 50 + i_col; + for (int i_sc = 0; i_sc < 3; i_sc++) { + int col = 50 * i_sc + i_col; + int adc_nr = adc_numbers[i_sc]; + int i_analog = n_pixel * 12 + adc_nr; + + // analog_frame[row * 150 + col] = analog_data[i_analog] & 0x3FFF; + order_map(row, col) = i_analog; + + } + } + } + return order_map; +} + +NDArray GenerateMoench05PixelMap1g() { + std::array adc_numbers = {1, 2, 0}; + NDArray order_map({160, 150}); + int n_pixel = 0; + for (int row = 0; row < 160; row++) { + for (int i_col = 0; i_col < 50; i_col++) { + n_pixel = row * 50 + i_col; + for (int i_sc = 0; i_sc < 3; i_sc++) { + int col = 50 * i_sc + i_col; + int adc_nr = adc_numbers[i_sc]; + int i_analog = n_pixel * 3 + adc_nr; + + + // analog_frame[row * 150 + col] = analog_data[i_analog] & 0x3FFF; + order_map(row, col) = i_analog; + + } + } + } + return order_map; +} + +NDArray GenerateMoench05PixelMapOld() { std::array adc_numbers = {9, 13, 1}; NDArray order_map({160, 150}); int n_pixel = 0;