def test_atlas_returns_sagittal_image_at_coordinate_or_zeros(coord): atlas = Atlas(volume=np.random.randint(0, 10, size=(3, 3, 3)), annotation_volume=np.empty((3, 3, 3)), resolution_um=2) image = atlas.make_sagittal_slice_at(coord) if 0 <= coord < 6: npt.assert_almost_equal(image.channels[0], atlas.volume[:, :, int(coord / 2)]) else: npt.assert_almost_equal(image.channels, np.zeros((1, 3, 3)))
def __call__(self, request: LoadAtlasRequest) -> Result[LoadAtlasData, str]: if isinstance(request, LoadBrainglobeAtlas): atlas_data = self._remote_atlas_reader.read(name=request.name) if atlas_data is None: return Err("Atlas not loaded.") atlas = Atlas( volume=atlas_data.registration_volume, resolution_um=atlas_data.resolution_um, annotation_volume=atlas_data.annotation_volume, ) elif isinstance(request, LoadAtlasFromFile): atlas_data2 = self._local_atlas_reader.read( filename=request.filename) if atlas_data2 is None: return Err("Atlas not loaded.") atlas = Atlas( volume=atlas_data2.registration_volume, resolution_um=request.resolution_um, annotation_volume=None, ) self._repo.set_atlas(atlas=atlas) return Ok( LoadAtlasData( volume=atlas.volume, transform=atlas.shared_space_transform, resolution=atlas.resolution_um, annotation_volume=atlas.annotation_volume, ))
def test_scale_matrix_is_scaled_to_um_according_to_resolution_and_not_shape(res, w, h, d): atlas = Atlas(volume=np.empty((w, h, d)), resolution_um=res) expected = np.array([ [res, 0, 0, 0], [0, res, 0, 0], [0, 0, res, 0], [0, 0, 0, 1], ]) assert np.all(np.isclose(atlas.scale_matrix, expected))
def test_section_registration_cuts_correctly_with_diff_resolutions(case): volume = np.zeros((3, 3, 3)) volume[1, 1, 1] = 1 registration = Registration(section=Section.create( image=Image(channels=np.empty((1, 3, 3)), resolution_um=case["section_res"]), physical_transform=PhysicalTransformer(**case["pos"]), ), atlas=Atlas( volume=volume, resolution_um=case['atlas_res'], )) atlas_slice = registration.slice_atlas() npt.assert_almost_equal(atlas_slice.channels[0], case['expected'])
def test_section_registration_to_an_atlas_gets_an_image_with_same_image_parameters( ): registration = Registration( section=Section.create( image=Image(channels=np.empty((3, 4, 5)), resolution_um=10), image_transform=ImageTransformer(i_shift=3, j_shift=5, theta=20), physical_transform=PhysicalTransformer(x=10, y=-5, z=10, rx=20, ry=0, rz=-5), ), atlas=Atlas(volume=np.empty((5, 5, 5)), resolution_um=20), ) atlas_slice = registration.slice_atlas() assert type(atlas_slice) is Image assert atlas_slice.width == 5 and atlas_slice.height == 4
def test_atlas_detects_if_xyz_coordinate_is_inside_volume(x, y, z, res): atlas = Atlas(volume=np.empty((5, 6, 7)), annotation_volume=np.empty((5, 6, 7)), resolution_um=res) in_volume = atlas.coord_is_in_volume(x=x, y=y, z=z) expected = True if 0 <= x < 5 * res and 0 <= y < 6 * res and 0 <= z < 7 * res else False assert in_volume is expected
def test_atlas_contains_an_annotation_volume(): atlas = Atlas(volume=np.empty((5, 4, 6)), annotation_volume=np.empty((5, 4, 6)), resolution_um=10) assert atlas.annotation_volume.shape == atlas.volume.shape
def test_can_get_atlas_center_in_shared_space(res, w, h, d): atlas = Atlas(volume=np.empty((w, h, d)), resolution_um=res) assert atlas.center == approx((w * res / 2, h * res / 2, d * res / 2))
def read(filename: str, resolution_um: int) -> Atlas: return Atlas(volume=tif.imread(filename), resolution_um=resolution_um)