diff --git a/bmlab/export/fluorescence_export.py b/bmlab/export/fluorescence_export.py index 150e7cf..22f0beb 100644 --- a/bmlab/export/fluorescence_export.py +++ b/bmlab/export/fluorescence_export.py @@ -52,8 +52,10 @@ def export(self, configuration): for image_key in image_keys: channel = repetition.payload.get_channel(image_key) img_data = repetition.payload.get_image(image_key) - # Average all images acquired - img_data = np.nanmean(img_data, axis=0).astype(np.ubyte) + + # Average all images acquired if grayscale + if repetition.payload.get_class(image_key).casefold() == 'image_grayscale': + img_data = np.nanmean(img_data, axis=0).astype(np.ubyte) # Construct export path and create it if necessary if self.file.path.parent.name == 'RawData': @@ -162,8 +164,8 @@ def export(self, configuration): image_warped =\ Image.merge("RGB", (blank, blank, image_warped)) image_alpha = Image.fromarray( - (255 * np.logical_not( - np.isnan(image_data_warped))).astype(np.ubyte)) + np.nanmean(255 * np.logical_not( + np.isnan(image_data_warped)), axis=tuple(range(2,image_data_warped.ndim))).astype(np.ubyte)) image_warped.putalpha(image_alpha) filename = path / f"{self.file.path.stem}" \ diff --git a/bmlab/file.py b/bmlab/file.py index d75e853..60c6fb6 100644 --- a/bmlab/file.py +++ b/bmlab/file.py @@ -364,6 +364,28 @@ def get_channel(self, image_key): except Exception: return None + def get_class(self, image_key): + """" + Returns the image class of a payload image + with the given key + """ + try: + return self.data.get(image_key).attrs\ + .get('IMAGE_SUBCLASS')[0].decode('utf-8') + except Exception: + return None + + def get_mode(self, image_key): + """" + Returns the interlace mode of a payload image + with the given key + """ + try: + return self.data.get(image_key).attrs\ + .get('INTERLACE_MODE')[0].decode('utf-8') + except Exception: + return None + def get_ROI(self, image_key): """ Returns the region of interest of a payload image diff --git a/tests/data/ColorImage.h5 b/tests/data/ColorImage.h5 new file mode 100644 index 0000000..2a8af33 Binary files /dev/null and b/tests/data/ColorImage.h5 differ diff --git a/tests/test_export_controller.py b/tests/test_export_controller.py index 84a4e02..c70fe89 100644 --- a/tests/test_export_controller.py +++ b/tests/test_export_controller.py @@ -67,6 +67,30 @@ def test_export_fluorescence(tmp_dir): assert os.path.exists(plots_dir / image) +def test_export_color_images(tmp_dir): + shutil.copy( + data_file_path('ColorImage.h5'), Path.cwd() / 'ColorImage.h5') + + session = Session.get_instance() + session.set_file(Path('ColorImage.h5')) + + ec = ExportController() + config = ec.get_configuration() + config['fluorescenceCombined']['export'] = False + config['brillouin']['export'] = False + ec.export(config) + + session.clear() + + plots_dir = tmp_dir.parent / 'Plots' + images = [ + 'ColorImage_FLrep0_channelbrightfield.png', + 'ColorImage_FLrep0_channelbrightfield_aligned.png', + ] + for image in images: + assert os.path.exists(plots_dir / image) + + def test_export_fluorescence_combined(tmp_dir): shutil.copy( data_file_path('Fluorescence.h5'), Path.cwd() / 'Fluorescence.h5')