return None if map is empty

This commit is contained in:
mazzol_a committed 2026-09-14 18:23:06 +02:00
1 parent 2fdaa0dea4
commit f47ef6a09f
4 files changed
+99 -20

No files matched your search

@@ -284,7 +284,7 @@ void StrixelPixelMap<N, M>::apply_group_remap(
if (output.shape() != order_map.shape()) {
throw std::invalid_argument(
"ApplyRemap: output shape does not match order map shape");
"output shape does not match order map shape");
}
const auto nrows = order_map.shape(0);
@@ -305,7 +305,7 @@ void StrixelPixelMap<N, M>::apply_group_remap(
// Corrupt map, must throw
if (flat_index >= input.size()) {
throw std::runtime_error(
"ApplyRemap: order map contains an invalid pixel index.");
"order map contains an invalid pixel index.");
}
// Correctly mapped pixel
+53 -12
View File
@@ -72,7 +72,11 @@ void define_StrixelPixelRemaps(py::module &m) {
auto *mapped_input_ptr =
new aare::NDArray<T, 2>(mapped_input);
result_list.append(return_image_data(mapped_input_ptr));
if (mapped_input_ptr->size() == 0) {
result_list.append(py::none());
} else {
result_list.append(return_image_data(mapped_input_ptr));
}
}
return result_list;
@@ -92,7 +96,7 @@ void define_StrixelPixelRemaps(py::module &m) {
-------
list of NDArray[uint16_t, 2]
Remapped arrays for each strixel group.
If a group is not covered by the user ROI, the corresponding array will be empty.
If a group is not covered by the user ROI, the corresponding array will be None.
)")
.def(
@@ -111,7 +115,11 @@ void define_StrixelPixelRemaps(py::module &m) {
auto *mapped_input_ptr =
new aare::NDArray<T, 2>(mapped_input);
result_list.append(return_image_data(mapped_input_ptr));
if (mapped_input_ptr->size() == 0) {
result_list.append(py::none());
} else {
result_list.append(return_image_data(mapped_input_ptr));
}
}
return result_list;
@@ -131,29 +139,62 @@ void define_StrixelPixelRemaps(py::module &m) {
-------
list of NDArray[uint16_t, 2]
Remapped arrays for each strixel group.
If a group is not covered by the user ROI, the corresponding array will be empty.
If a group is not covered by the user ROI, the corresponding array will be None.
)")
.def(
"__call__",
[](const aare::remap::StrixelPixelMap<N, M> &self,
py::array_t<T, py::array::c_style | py::array::forcecast> input,
std::array<
py::array_t<T, py::array::c_style | py::array::forcecast>, M>
&output) {
py::list &output) {
if (input.ndim() != 2) {
throw std::runtime_error("Input array must be 2D");
}
if (output.size() != M) {
throw std::runtime_error(
fmt::format("Output list size must be equal to the "
"number of strixel groups ({}), but got {}",
M, output.size()));
}
const auto group_maps = self.get_group_maps();
const auto input_view = make_view_2d(input);
for (size_t i = 0; i < group_maps.size(); ++i) {
aare::remap::detail::StrixelPixelMapBindingAccess::
apply_group_remap(self, input_view,
make_view_2d(output[i]),
group_maps[i].map.view());
if (group_maps[i].empty()) {
output[i] = py::none();
} else {
if (!py::isinstance<py::array_t<T, py::array::c_style>>(
output[i])) {
throw std::runtime_error(
fmt::format("Output list element at index {} "
"is not a numpy array",
i));
}
auto out = py::array_t<T, py::array::c_style>::ensure(
output[i]);
if (!out) {
throw std::runtime_error(
"Output entry must be a C-contiguous numpy "
"array");
}
if (out.ndim() != 2) {
throw std::runtime_error("Output entry must be 2D");
}
if (!out.writeable()) {
throw std::runtime_error(
"Output entry must be writable");
}
aare::remap::detail::StrixelPixelMapBindingAccess::
apply_group_remap(self, input_view,
make_view_2d(out),
group_maps[i].map.view());
}
}
},
py::arg("input").noconvert(), py::arg("output").noconvert(),
@@ -168,6 +209,6 @@ void define_StrixelPixelRemaps(py::module &m) {
Input array to be remapped.
output : list of NDArray[uint16_t, 2]
Preallocated arrays to store the remapped results for each strixel group.
If a group is not covered by the user ROI, the corresponding output array will be empty.
If a group is not covered by the user ROI, the corresponding output array will be None.
)");
}
+5
View File
@@ -37,6 +37,11 @@ void define_defs_bindings(py::module &m) {
.def_readwrite("ymin", &ROI::ymin)
.def_readwrite("ymax", &ROI::ymax)
.def("shape",
[](const ROI &self) {
return std::make_tuple(self.height(), self.width());
})
.def(
"slice",
[](const ROI &self) {
+39 -6
View File
@@ -126,9 +126,9 @@ def test_predefined_iLGAD_singlechip():
assert group_maps[0].map.shape == (9, 2)
assert group_maps[1].map.shape == (0, 0)
assert group_maps[1].empty() == True
assert group_maps[2].map.shape == (0, 0)
assert group_maps[2].empty() == True
group_map_0 = group_maps[0].map
@@ -142,9 +142,9 @@ def test_predefined_iLGAD_singlechip():
assert np.array_equal(output[0], np.array([[0, 3], [1,4], [2,5], [0, 3], [1,4], [2,5], [0, 3], [1,4], [2,5]]))
assert output[1].size == 0
assert output[1] == None
assert output[2].size == 0
assert output[2] == None
def test_predefined_iLGAD_quad_remap():
@@ -183,7 +183,40 @@ def test_predefined_iLGAD_quad_remap():
assert group_maps[0].map.shape == (762, 2)
# TODO test empty map
#def test_empty_map():
def test_empty_map():
""" Test empty map when ROI does not cover any strixel pixels """
exclusive_user_roi = ROI(0, 10, 0, 5)
strixelpixelmap = strixelremap.Jungfrau_iLGAD_StrixelPixelMap(module_placement = strixelremap.Chip1)
strixelpixelmap.calculate_map(exclusive_user_roi)
group_maps = strixelpixelmap.group_maps
assert len(group_maps) == 3
assert group_maps[0].empty() == True
assert group_maps[1].empty() == True
assert group_maps[2].empty() == True
input_data = np.random.randint(0, 65535, size=exclusive_user_roi.shape(), dtype=np.uint16)
mapped_output = strixelpixelmap(input_data)
assert len(mapped_output) == 3
assert mapped_output[0] == None
assert mapped_output[1] == None
assert mapped_output[2] == None
# check that the output call operator with preallocated output arrays works as expected
output_arrays = [np.random.randint(0, 65535, size=exclusive_user_roi.shape(), dtype=np.uint16) for _ in range(3)]
strixelpixelmap(input_data, output_arrays)
assert output_arrays[0] == None
assert output_arrays[1] == None
assert output_arrays[2] == None