Skip to content

Commit

Permalink
Streamlined build, new transforms (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
erikfrojdh authored Nov 26, 2024
2 parents 8f729fc + 996a886 commit 2d33fd4
Show file tree
Hide file tree
Showing 10 changed files with 105 additions and 52 deletions.
43 changes: 0 additions & 43 deletions .github/workflows/build_pkg.yml

This file was deleted.

10 changes: 7 additions & 3 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion conda-recipe/meta.yaml
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
2 changes: 2 additions & 0 deletions include/aare/PixelMap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ namespace aare {

NDArray<ssize_t, 2> GenerateMoench03PixelMap();
NDArray<ssize_t, 2> GenerateMoench05PixelMap();
NDArray<ssize_t, 2> GenerateMoench05PixelMap1g();
NDArray<ssize_t, 2> GenerateMoench05PixelMapOld();

//Matterhorn02
NDArray<ssize_t, 2>GenerateMH02SingleCounterPixelMap();
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
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
20 changes: 20 additions & 0 deletions python/aare/transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()
8 changes: 8 additions & 0 deletions python/src/pixel_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ void define_pixel_map_bindings(py::module &m) {
.def("GenerateMoench05PixelMap", []() {
auto ptr = new NDArray<ssize_t,2>(GenerateMoench05PixelMap());
return return_image_data(ptr);
})
.def("GenerateMoench05PixelMap1g", []() {
auto ptr = new NDArray<ssize_t,2>(GenerateMoench05PixelMap1g());
return return_image_data(ptr);
})
.def("GenerateMoench05PixelMapOld", []() {
auto ptr = new NDArray<ssize_t,2>(GenerateMoench05PixelMapOld());
return return_image_data(ptr);
})
.def("GenerateMH02SingleCounterPixelMap", []() {
auto ptr = new NDArray<ssize_t,2>(GenerateMH02SingleCounterPixelMap());
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
43 changes: 43 additions & 0 deletions src/PixelMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,49 @@ NDArray<ssize_t, 2> GenerateMoench03PixelMap() {
}

NDArray<ssize_t, 2> GenerateMoench05PixelMap() {
std::array<int, 3> adc_numbers = {5, 9, 1};
NDArray<ssize_t, 2> 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<ssize_t, 2> GenerateMoench05PixelMap1g() {
std::array<int, 3> adc_numbers = {1, 2, 0};
NDArray<ssize_t, 2> 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<ssize_t, 2> GenerateMoench05PixelMapOld() {
std::array<int, 3> adc_numbers = {9, 13, 1};
NDArray<ssize_t, 2> order_map({160, 150});
int n_pixel = 0;
Expand Down

0 comments on commit 2d33fd4

Please sign in to comment.