Skip to content

Commit

Permalink
make sure unsorted measurement lists work, added test
Browse files Browse the repository at this point in the history
  • Loading branch information
haesleinhuepf committed Mar 11, 2023
1 parent 5c1afa0 commit 9bdead9
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 11 deletions.
21 changes: 10 additions & 11 deletions napari_skimage_regionprops/_parametric_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,10 @@ def map_measurements_on_labels(labels_layer:"napari.layers.Labels", column:str =
axis=0)
return stack
else:
measurements = np.asarray(table[column]).tolist()

# Ensure background measurements are present in measurements
# if also present in labels
if (0 not in table['label'].values) and (0 in labels):
measurements.insert(0, 0)
return relabel_skimage(labels, measurements)
label_list = np.asarray(table['label']).tolist()
measurement_list = np.asarray(table[column]).tolist()

return relabel_skimage(labels, label_list, measurement_list)



Expand Down Expand Up @@ -135,12 +132,14 @@ def relabel_timepoint_with_map_array(labels, table, column, frame_column, timepo
table_one_timepoint = table[table[frame_column] == timepoint]
else:
table_one_timepoint = table
measurements = np.asarray(table_one_timepoint[column]).tolist()
return relabel_skimage(labels_one_timepoint, measurements)

def relabel_skimage(image, measurements):
label_list = np.asarray(table_one_timepoint['label']).tolist()
measurement_list = np.asarray(table_one_timepoint[column]).tolist()
return relabel_skimage(labels_one_timepoint, label_list, measurement_list)

def relabel_skimage(image, label_list, measurement_list):
from skimage.util import map_array
return map_array(image, np.unique(image), np.array(measurements))
return map_array(image, np.asarray(label_list), np.array(measurement_list))

def relabel_timepoint(labels, table, column, frame_column, timepoint):
labels_one_timepoint = labels[timepoint]
Expand Down
26 changes: 26 additions & 0 deletions napari_skimage_regionprops/_tests/test_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,32 @@ def test_map_measurements_WITHOUT_BG_on_NON_sequential_labels_WITHOUT_BG(make_na
result = map_measurements_on_labels(labels_layer, column='measurements')
assert np.array_equal(output, result)


def test_map_measurements_WITHOUT_BG_on_NON_sequential_labels_WITHOUT_BG_unsorted(make_napari_viewer):
import numpy as np
from pandas import DataFrame
from napari_skimage_regionprops._parametric_images import map_measurements_on_labels

measurements = [6, 9, 12, 15]
labels = np.array(
[[2, 4],
[6, 8]]
)
output = np.array(
[[9, 6],
[12, 15]]
)

table = DataFrame({
'label': [4, 2, 6, 8],
'measurements': measurements})

viewer = make_napari_viewer()
labels_layer = viewer.add_labels(labels, features=table)
result = map_measurements_on_labels(labels_layer, column='measurements')
assert np.array_equal(output, result)


def test_map_measurements_WITHOUT_BG_on_sequential_labels_WITH_BG(make_napari_viewer):
import numpy as np
from pandas import DataFrame
Expand Down

0 comments on commit 9bdead9

Please sign in to comment.