From 38198d7b622a70006cc0e40357107894504ca685 Mon Sep 17 00:00:00 2001 From: Alessandra Trapani Date: Thu, 16 May 2024 11:23:42 +0200 Subject: [PATCH 1/3] add scan_line_rate, grid_spacing and origin_coords from scanimage metadata --- .../scanimagetiff_utils.py | 28 ++++++++++++++++--- tests/test_scanimage_utils.py | 10 +++++++ 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/roiextractors/extractors/tiffimagingextractors/scanimagetiff_utils.py b/src/roiextractors/extractors/tiffimagingextractors/scanimagetiff_utils.py index bf559573..b27f7cc2 100644 --- a/src/roiextractors/extractors/tiffimagingextractors/scanimagetiff_utils.py +++ b/src/roiextractors/extractors/tiffimagingextractors/scanimagetiff_utils.py @@ -119,18 +119,38 @@ def parse_metadata(metadata: dict) -> dict: active_channels = parse_matlab_vector(metadata["SI.hChannels.channelsActive"]) channel_indices = np.array(active_channels) - 1 # Account for MATLAB indexing channel_names = np.array(metadata["SI.hChannels.channelName"].split("'")[1::2]) + scan_line_rate = 1 / float(metadata["SI.hRoiManager.linePeriod"]) channel_names = channel_names[channel_indices].tolist() num_channels = len(channel_names) - if "RoiGroups" in metadata.keys(): - roi_metadata = metadata["RoiGroups"] - else: - roi_metadata = None + roi_metadata = metadata["RoiGroups"] + grid_spacing = None + grid_spacing_unit = "n.a" + origin_coords = None + origin_coords_unit = "n.a" + try: + # Attempt to access the nested dictionary keys + scanfields = roi_metadata["imagingRoiGroup"]["rois"]["scanfields"] + fov_size_in_um = np.array(scanfields["sizeXY"]) + frame_dimension = np.array(scanfields["pixelResolutionXY"]) + grid_spacing = fov_size_in_um / frame_dimension + grid_spacing_unit = "micrometers" + origin_coords = scanfields["centerXY"] + origin_coords_unit = "micrometers" + + except (KeyError, Exception): + pass + metadata_parsed = dict( sampling_frequency=sampling_frequency, num_channels=num_channels, num_planes=num_planes, frames_per_slice=frames_per_slice, channel_names=channel_names, + scan_line_rate=scan_line_rate, + grid_spacing=grid_spacing, + grid_spacing_unit=grid_spacing_unit, + origin_coords=origin_coords, + origin_coords_unit=origin_coords_unit, roi_metadata=roi_metadata, ) return metadata_parsed diff --git a/tests/test_scanimage_utils.py b/tests/test_scanimage_utils.py index 8429eba0..cadadb85 100644 --- a/tests/test_scanimage_utils.py +++ b/tests/test_scanimage_utils.py @@ -70,6 +70,11 @@ def test_parse_matlab_vector_invalid(): "num_planes": 20, "frames_per_slice": 24, "channel_names": ["Channel 1"], + "scan_line_rate": 15847.40847329236, + "grid_spacing": [0.01757812, 0.01757812], + "grid_spacing_unit": "micrometers", + "origin_coords": [0, 0], + "origin_coords_unit": "micrometers", "roi_metadata": { "imagingRoiGroup": { "ver": 1, @@ -147,6 +152,11 @@ def test_parse_matlab_vector_invalid(): "num_planes": 2, "frames_per_slice": 2, "channel_names": ["Channel 1", "Channel 4"], + "scan_line_rate": 15843.868185354244, + "grid_spacing": None, + "grid_spacing_unit": "n.a", + "origin_coords": None, + "origin_coords_unit": "n.a", "roi_metadata": { "imagingRoiGroup": { "ver": 1, From dfdf2c2abe32c262c41b5251adf8bf3fde61d588 Mon Sep 17 00:00:00 2001 From: Alessandra Trapani Date: Thu, 16 May 2024 11:36:55 +0200 Subject: [PATCH 2/3] update CHANGELOG and docstring --- CHANGELOG.md | 1 + .../extractors/tiffimagingextractors/scanimagetiff_utils.py | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e62a546..b2421d71 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * Add InscopixImagingExtractor [#276](https://github.com/catalystneuro/roiextractors/pull/276) * Updated testing workflows to include python 3.12, m1/intel macos, and dev tests to check neuroconv: [PR #317](https://github.com/catalystneuro/roiextractors/pull/317) +* Updated `parse_metadata()` from `scaimagetiff_utils` to include `scan_line_rate`, `grid_spacing`, `grid_spacing_unit`,`origin_coords`, `origin_coords_unit` ### Fixes diff --git a/src/roiextractors/extractors/tiffimagingextractors/scanimagetiff_utils.py b/src/roiextractors/extractors/tiffimagingextractors/scanimagetiff_utils.py index b27f7cc2..2bfdb8d9 100644 --- a/src/roiextractors/extractors/tiffimagingextractors/scanimagetiff_utils.py +++ b/src/roiextractors/extractors/tiffimagingextractors/scanimagetiff_utils.py @@ -95,6 +95,12 @@ def parse_metadata(metadata: dict) -> dict: - frames_per_slice - channel_names - num_channels + - scan_line_rate, + - grid_spacing, + - grid_spacing_unit, + - origin_coords, + - origin_coords_unit, + - roi_metadata, Parameters ---------- From ca54999eb5a4187d2b845f01370085221a67081e Mon Sep 17 00:00:00 2001 From: Alessandra Trapani Date: Tue, 21 May 2024 10:20:53 +0200 Subject: [PATCH 3/3] replace try/except block with if block --- .../extractors/tiffimagingextractors/scanimagetiff_utils.py | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/roiextractors/extractors/tiffimagingextractors/scanimagetiff_utils.py b/src/roiextractors/extractors/tiffimagingextractors/scanimagetiff_utils.py index 2bfdb8d9..345cfd5f 100644 --- a/src/roiextractors/extractors/tiffimagingextractors/scanimagetiff_utils.py +++ b/src/roiextractors/extractors/tiffimagingextractors/scanimagetiff_utils.py @@ -133,8 +133,7 @@ def parse_metadata(metadata: dict) -> dict: grid_spacing_unit = "n.a" origin_coords = None origin_coords_unit = "n.a" - try: - # Attempt to access the nested dictionary keys + if roi_metadata["imagingRoiGroup"]["rois"].__contains__("scanfields"): scanfields = roi_metadata["imagingRoiGroup"]["rois"]["scanfields"] fov_size_in_um = np.array(scanfields["sizeXY"]) frame_dimension = np.array(scanfields["pixelResolutionXY"]) @@ -143,9 +142,6 @@ def parse_metadata(metadata: dict) -> dict: origin_coords = scanfields["centerXY"] origin_coords_unit = "micrometers" - except (KeyError, Exception): - pass - metadata_parsed = dict( sampling_frequency=sampling_frequency, num_channels=num_channels,