Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
haesleinhuepf committed Mar 14, 2023
2 parents 7b0855c + c60dc4b commit cdd258a
Show file tree
Hide file tree
Showing 10 changed files with 2,085 additions and 2,066 deletions.
60 changes: 31 additions & 29 deletions demo/intersection_over_target_area_explained.ipynb

Large diffs are not rendered by default.

1,471 changes: 1,471 additions & 0 deletions demo/measure_relationship_to_other_channels_functions.ipynb

Large diffs are not rendered by default.

429 changes: 429 additions & 0 deletions demo/measure_relationship_to_other_channels_plugin.ipynb

Large diffs are not rendered by default.

1,471 changes: 0 additions & 1,471 deletions demo/measure_things_inside_things_functions.ipynb

This file was deleted.

418 changes: 0 additions & 418 deletions demo/measure_things_inside_things_plugin.ipynb

This file was deleted.

2 changes: 1 addition & 1 deletion napari_skimage_regionprops/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from ._regionprops import regionprops, regionprops_table, regionprops_table_all_frames
from ._multichannel import link_two_label_images, measure_labels, measure_labels_with_intensity
from ._multichannel import measure_labels_in_labels_with_intensity, measure_labels_in_labels
from ._multichannel import regionprops_measure_things_inside_things
from ._multichannel import regionprops_measure_relationship_to_other_channels
from ._process_tables import merge_measurements_to_reference, make_summary_table
from ._parametric_images import visualize_measurement_on_labels, relabel, map_measurements_on_labels
from ._measure_points import measure_points
Expand Down
233 changes: 119 additions & 114 deletions napari_skimage_regionprops/_multichannel.py

Large diffs are not rendered by default.

36 changes: 18 additions & 18 deletions napari_skimage_regionprops/_process_tables.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import pandas
from typing import List


def merge_measurements_to_reference(
table_reference_labels_properties: "pandas.DataFrame",
table_linking_labels: List["pandas.DataFrame"],
table_labels_to_measure_properties: List["pandas.DataFrame"],
table_other_channel_labels_properties: List["pandas.DataFrame"],
suffixes=None) -> List["pandas.DataFrame"]:
"""
Merge measurements from target to reference table through a linking table.
Expand All @@ -19,7 +18,7 @@ def merge_measurements_to_reference(
a list of tables. Each table should contain 2 columns, a
label_reference' and a 'label_target'. Each table row associates a
target label to a reference label.
table_labels_to_measure_properties : List["pandas.DataFrame"]
table_other_channel_labels_properties : List["pandas.DataFrame"]
a list of tables to be used as targets with a column 'label' and other
columns with features.
suffixes : List[str], optional
Expand All @@ -40,20 +39,20 @@ def merge_measurements_to_reference(
list_table_linking_labels = [table_linking_labels]
else:
list_table_linking_labels = table_linking_labels
if not isinstance(table_labels_to_measure_properties, list):
list_table_labels_to_measure_properties = [
table_labels_to_measure_properties]
if not isinstance(table_other_channel_labels_properties, list):
list_table_other_channel_labels_properties = [
table_other_channel_labels_properties]
else:
list_table_labels_to_measure_properties = \
table_labels_to_measure_properties
list_table_other_channel_labels_properties = \
table_other_channel_labels_properties
# Build custom suffixes or check if provided suffixes match data size
n_measurement_tables = len(list_table_labels_to_measure_properties)
n_measurement_tables = len(list_table_other_channel_labels_properties)
if suffixes is None:
n_leading_zeros = n_measurement_tables // 10
suffixes = ['_reference'] + ['_' + str(i+1).zfill(1+n_leading_zeros)
for i in range(n_measurement_tables)]
else:
if len(suffixes) != len(table_labels_to_measure_properties) + 1:
if len(suffixes) != len(table_other_channel_labels_properties) + 1:
print(('Error: List of suffixes must have the same length as the'
'number of tables containing measurements'))
return
Expand All @@ -70,19 +69,19 @@ def merge_measurements_to_reference(
'label_target': 'label' + suffixes[i+1]},
inplace=True)
# Rename columns of tables with properties from other channels
for i, table_labels_to_measure_properties in enumerate(
list_table_labels_to_measure_properties):
table_labels_to_measure_properties.columns = [
for i, table_other_channel_labels_properties in enumerate(
list_table_other_channel_labels_properties):
table_other_channel_labels_properties.columns = [
props + suffixes[i+1]
for props in table_labels_to_measure_properties.columns]
for props in table_other_channel_labels_properties.columns]

output_table_list = []
# Consecutively merge linking_labels tables and properties from other
# channels tables to the reference table
for i, table_linking_labels, table_labels_to_measure_properties in zip(
for i, table_linking_labels, table_other_channel_labels_properties in zip(
range(n_measurement_tables),
list_table_linking_labels,
list_table_labels_to_measure_properties):
list_table_other_channel_labels_properties):
# Merge other labels to label_reference
output_table = pd.merge(table_reference_labels_properties,
table_linking_labels,
Expand All @@ -93,12 +92,13 @@ def merge_measurements_to_reference(
'label' + suffixes[i+1]].fillna(0)
# Merge other properties to output table based on new labels column
output_table = pd.merge(output_table,
table_labels_to_measure_properties,
table_other_channel_labels_properties,
how='outer', on='label' + suffixes[i+1])
# Ensure label columns type to be integer
for column in output_table.columns:
if column.startswith('label'):
output_table[column] = output_table[column].astype(int)
output_table = output_table.sort_values(by='label' + suffixes[0])
# Append output table to list (each table may have different shapes)
output_table_list.append(output_table)
return output_table_list
Expand Down Expand Up @@ -157,7 +157,7 @@ def make_summary_table(table: List["pandas.DataFrame"],
print(('Could not infer suffixes from column names. Please '
'provide a list of suffixes identifying different '
'channels'))
if isinstance(table, pandas.DataFrame):
if isinstance(table, pd.DataFrame):
table = [table]

if 'count' in statistics_list:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

def test_napari_regionprops_map_2channels_2D(make_napari_viewer):
import numpy as np
from napari_skimage_regionprops import regionprops_measure_things_inside_things
from napari_skimage_regionprops import regionprops_measure_relationship_to_other_channels

viewer = make_napari_viewer()

Expand All @@ -25,16 +25,16 @@ def test_napari_regionprops_map_2channels_2D(make_napari_viewer):
viewer.add_labels(ref_labels, name='ref_labels')
viewer.add_labels(target_labels, name='target_labels')

widget = regionprops_measure_things_inside_things()
regionprops_measure_things_inside_things_function = widget._function
widget = regionprops_measure_relationship_to_other_channels()
regionprops_measure_relationship_to_other_channels_function = widget._function
# Measure everything we can
table = regionprops_measure_things_inside_things_function(
table = regionprops_measure_relationship_to_other_channels_function(
ref_labels,
multichannel_image[..., 0],
[target_labels],
[multichannel_image[..., 1]],
intensity = True,
things_inside_things = True,
relate_to_other_channels = True,
size = True,
perimeter = True,
shape = True,
Expand Down Expand Up @@ -63,7 +63,7 @@ def test_napari_regionprops_map_2channels_2D(make_napari_viewer):

def test_napari_regionprops_map_3channels_2D(make_napari_viewer):
import numpy as np
from napari_skimage_regionprops import regionprops_measure_things_inside_things
from napari_skimage_regionprops import regionprops_measure_relationship_to_other_channels

viewer = make_napari_viewer()

Expand Down Expand Up @@ -91,17 +91,17 @@ def test_napari_regionprops_map_3channels_2D(make_napari_viewer):
viewer.add_labels(target_labels, name='target_labels')
viewer.add_labels(target_labels_B, name='target_labels_B')

widget = regionprops_measure_things_inside_things()
regionprops_measure_things_inside_things_function = widget._function
widget = regionprops_measure_relationship_to_other_channels()
regionprops_measure_relationship_to_other_channels_function = widget._function

# Measure everything we can
table = regionprops_measure_things_inside_things_function(
table = regionprops_measure_relationship_to_other_channels_function(
ref_labels,
multichannel_image[..., 0],
[target_labels, target_labels_B],
[multichannel_image[..., 1], multichannel_image[..., 2]],
intensity = True,
things_inside_things = True,
relate_to_other_channels = True,
size = True,
perimeter = True,
shape = True,
Expand Down Expand Up @@ -134,7 +134,7 @@ def test_napari_regionprops_map_3channels_2D(make_napari_viewer):

def test_napari_regionprops_map_2channels_3D(make_napari_viewer):
import numpy as np
from napari_skimage_regionprops import regionprops_measure_things_inside_things
from napari_skimage_regionprops import regionprops_measure_relationship_to_other_channels

viewer = make_napari_viewer()

Expand Down Expand Up @@ -170,17 +170,17 @@ def test_napari_regionprops_map_2channels_3D(make_napari_viewer):
viewer.add_labels(ref_labels_3D, name='ref_labels_3D')
viewer.add_labels(target_labels_3D, name='target_labels_3D')

widget = regionprops_measure_things_inside_things()
regionprops_measure_things_inside_things_function = widget._function
widget = regionprops_measure_relationship_to_other_channels()
regionprops_measure_relationship_to_other_channels_function = widget._function

# Measure everything we can
table = regionprops_measure_things_inside_things_function(
table = regionprops_measure_relationship_to_other_channels_function(
ref_labels_3D,
multichannel_image[0],
[target_labels_3D],
[multichannel_image[1]],
intensity = True,
things_inside_things = True,
relate_to_other_channels = True,
size = True,
perimeter = True,
shape = True,
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pandas
napari-tools-menu>=0.1.19
napari-workflows
imageio!=2.22.1
Deprecated

0 comments on commit cdd258a

Please sign in to comment.