Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add check_plane_segmentation_image_mask_shape_against_ref_images #257

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions nwbinspector/checks/ophys.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,15 @@ def check_excitation_lambda_in_nm(imaging_plane: ImagingPlane):
"""Check that emission lambda is in feasible range for unit nanometers."""
if imaging_plane.excitation_lambda < MIN_LAMBDA:
return InspectorMessage(f"excitation lambda of {imaging_plane.excitation_lambda} should be in units of nm.")


@register_check(importance=Importance.BEST_PRACTICE_VIOLATION, neurodata_type=PlaneSegmentation)
def check_plane_segmentation_image_mask_shape_against_ref_images(plane_segmentation: PlaneSegmentation):
if plane_segmentation.reference_images and "image_mask" in plane_segmentation.colnames:
mask_shape = plane_segmentation["image_mask"].shape[1:]
for ref_image in plane_segmentation.reference_images:
if mask_shape != ref_image.data.shape[1:]:
yield InspectorMessage(
f"image_mask of shape {mask_shape} does not match reference image {ref_image.name} with shape"
f" {ref_image.data.shape[1:]}."
)
99 changes: 98 additions & 1 deletion tests/unit_tests/test_ophys.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@
import numpy as np
from pynwb import NWBFile
from pynwb.device import Device
from pynwb.ophys import OpticalChannel, ImageSegmentation, RoiResponseSeries, ImagingPlane
from pynwb.ophys import (
OpticalChannel,
ImageSegmentation,
RoiResponseSeries,
ImagingPlane,
PlaneSegmentation,
TwoPhotonSeries,
)
from hdmf.common.table import DynamicTableRegion, DynamicTable

from nwbinspector import (
Expand All @@ -15,6 +22,7 @@
check_roi_response_series_link_to_plane_segmentation,
check_excitation_lambda_in_nm,
check_emission_lambda_in_nm,
check_plane_segmentation_image_mask_shape_against_ref_images,
)


Expand Down Expand Up @@ -230,3 +238,92 @@ def test_check_emission_lambda_in_nm():
def test_pass_check_emission_lambda_in_nm():
optical_channel = OpticalChannel(name="OpticalChannel", description="an optical channel", emission_lambda=500.0)
assert check_emission_lambda_in_nm(optical_channel) is None


def test_pass_check_plane_segmentation_image_mask_dims_against_imageseries():

device = Device(
name="Microscope", description="My two-photon microscope", manufacturer="The best microscope manufacturer"
)
optical_channel = OpticalChannel(name="OpticalChannel", description="an optical channel", emission_lambda=500.0)
imaging_plane = ImagingPlane(
name="ImagingPlane",
optical_channel=optical_channel,
imaging_rate=30.0,
description="a very interesting part of the brain",
device=device,
excitation_lambda=300.0,
indicator="GFP",
location="V1",
grid_spacing=[0.01, 0.01],
grid_spacing_unit="meters",
origin_coords=[1.0, 2.0, 3.0],
origin_coords_unit="meters",
)

two_photon_series = TwoPhotonSeries(
name="TwoPhotonSeries",
imaging_plane=imaging_plane,
data=np.ones((20, 10, 10)),
unit="n.a.",
rate=30.0,
)

plane_segmentation = PlaneSegmentation(
description="my plane segmentation",
imaging_plane=imaging_plane,
reference_images=two_photon_series,
)

plane_segmentation.add_roi(image_mask=np.ones((10, 10)))

assert check_plane_segmentation_image_mask_shape_against_ref_images(plane_segmentation) is None


def test_fail_check_plane_segmentation_image_mask_dims_against_imageseries():

device = Device(
name="Microscope", description="My two-photon microscope", manufacturer="The best microscope manufacturer"
)
optical_channel = OpticalChannel(name="OpticalChannel", description="an optical channel", emission_lambda=500.0)
imaging_plane = ImagingPlane(
name="ImagingPlane",
optical_channel=optical_channel,
imaging_rate=30.0,
description="a very interesting part of the brain",
device=device,
excitation_lambda=300.0,
indicator="GFP",
location="V1",
grid_spacing=[0.01, 0.01],
grid_spacing_unit="meters",
origin_coords=[1.0, 2.0, 3.0],
origin_coords_unit="meters",
)

two_photon_series = TwoPhotonSeries(
name="TwoPhotonSeries",
imaging_plane=imaging_plane,
data=np.ones((20, 10, 10)),
unit="n.a.",
rate=30.0,
)

plane_segmentation = PlaneSegmentation(
description="my plane segmentation",
imaging_plane=imaging_plane,
reference_images=two_photon_series,
)

plane_segmentation.add_roi(image_mask=np.ones((9, 10)))

assert check_plane_segmentation_image_mask_shape_against_ref_images(plane_segmentation) == [
InspectorMessage(
message="image_mask of shape (9, 10) does not match reference image TwoPhotonSeries with shape (10, 10).",
importance=Importance.BEST_PRACTICE_VIOLATION,
check_function_name="check_plane_segmentation_image_mask_shape_against_ref_images",
object_type="PlaneSegmentation",
object_name="ImagingPlane",
location="/",
)
]