comparison 2d_feature_extraction.py @ 7:048545339ced draft default tip

planemo upload for repository https://github.com/BMCV/galaxy-image-analysis/tree/master/tools/2d_feature_extraction/ commit b8e0b656d417db6e2ad0f187fc3c5afff0c3acd7
author imgteam
date Tue, 06 Jan 2026 09:25:17 +0000
parents 5530132d500e
children
comparison
equal deleted inserted replaced
6:8e3a52b74876 7:048545339ced
43 tool.parse_args() 43 tool.parse_args()
44 44
45 # Validate the input image 45 # Validate the input image
46 try: 46 try:
47 label_image = tool.args.input_images['labels'] 47 label_image = tool.args.input_images['labels']
48 if any(label_image.shape[label_image.axes.index(axis)] > 1 for axis in label_image.axes if axis not in 'ZYX'): 48 if any(label_image.shape[label_image.axes.index(axis)] > 1 for axis in label_image.axes if axis not in 'XYZ'):
49 raise ValueError(f'This tool is not applicable to images with {label_image.original_axes} axes.') 49 raise ValueError(f'This tool is not applicable to images with {label_image.original_axes} axes.')
50 50
51 # Extract the image features 51 # Extract the image features
52 for section in tool.run('ZYX'): # the validation code above guarantees that we will have only a single iteration 52 for section in tool.run('XYZ'): # the validation code above guarantees that we will have only a single iteration
53 df = pd.DataFrame() 53 df = pd.DataFrame()
54 54
55 # Get the labels array and cast to `uint8` if it is `bool` (`skimage.measure.regionprops` refuses `bool` typed arrays) 55 # Get the labels array and cast to `uint8` if it is `bool` (`skimage.measure.regionprops` refuses `bool` typed arrays)
56 labels_section_data = section['labels'].data.squeeze() 56 labels_section_data = section['labels'].data.squeeze()
57 if np.issubdtype(labels_section_data.dtype, bool): 57 if np.issubdtype(labels_section_data.dtype, bool):
87 elif feature_name == 'perimeter' and labels_section_data.ndim == 3: 87 elif feature_name == 'perimeter' and labels_section_data.ndim == 3:
88 df['perimeter'] = df['it'].map( 88 df['perimeter'] = df['it'].map(
89 lambda ait: surface(labels_section_data, regions[ait].label), # `skimage.measure.regionprops` cannot compute perimeters for 3-D data 89 lambda ait: surface(labels_section_data, regions[ait].label), # `skimage.measure.regionprops` cannot compute perimeters for 3-D data
90 ) 90 )
91 91
92 # Add the object centroid using separate columns for the different coordinates
93 elif feature_name == 'centroid':
94 for axis_idx, axis in enumerate(section['labels'].axes): # XYZ
95 if section['labels'].shape[axis_idx] > 1:
96 df[f'{feature_name}_{axis.lower()}'] = df['it'].map(
97 lambda ait: getattr(regions[ait], feature_name)[axis_idx],
98 )
99
92 # Skip features that are not available when processing 3-D images 100 # Skip features that are not available when processing 3-D images
93 elif feature_name in ('eccentricity', 'moments_hu', 'orientation') and labels_section_data.ndim == 3: 101 elif feature_name in ('eccentricity', 'moments_hu', 'orientation') and labels_section_data.ndim == 3:
94 print(f'Skip feature that is not available for 3-D images: "{feature_name}"') 102 print(f'Skip feature that is not available for 3-D images: "{feature_name}"')
95 103
96 # Add another feature from `regions` that was computed via `skimage.measure.regionprops` 104 # Add another feature from `regions` that was computed via `skimage.measure.regionprops`