Ejemplo n.º 1
0
def test_nonexistent_image_coords_raise_error_and_doesnt_if_exists(i, j):
    section = Section(image=ImageData(channels=arange(180).reshape(2, 3, 30),
                                      pixel_resolution_um=1), )
    if i < 0 or i >= section.image.height or j < 0 or j >= section.image.width:
        with pytest.raises(ValueError):
            section.pos_from_coord(i=i, j=j)
    else:
        assert section.pos_from_coord(i=i, j=j)
Ejemplo n.º 2
0
def test_section_recenter_sets_shift_to_half_the_width_and_height(
        width, height):
    section = Section(image=ImageData(channels=np.random.random((3, height,
                                                                 width)),
                                      pixel_resolution_um=123))
    assert section.plane_2d.x == 0 and section.plane_2d.y == 0
    section2 = section.recenter()
    assert section2.plane_2d.x == approx(
        -width / 2) and section2.plane_2d.y == approx(-height / 2)
Ejemplo n.º 3
0
def test_can_get_3d_position_from_2d_pixel_coordinate_in_section(
        i, j, dx, dy, dz, pixel_resolution):
    section = Section(
        image=ImageData(
            channels=arange(24).reshape(2, 3, 4),
            pixel_resolution_um=pixel_resolution,
        ),
        plane_3d=Plane3D(x=dx, y=dy, z=dz),
    )
    x, y, z = section.pos_from_coord(i=i, j=j)  # observed 3D positions
    assert x == approx((j * 1 / pixel_resolution) + dx)
    assert y == approx((-i * 1 / pixel_resolution) + dy)
    assert z == approx(dz)
Ejemplo n.º 4
0
def test_can_get_correct_3d_position_with_image_shifts_and_planar_rotations(
        j, pixel_resolution, x_shift, y_shift, theta):
    section = Section(
        image=ImageData(channels=arange(2400).reshape(2, 30, 40),
                        pixel_resolution_um=pixel_resolution),
        plane_2d=Image2DTransform(i=x_shift, j=y_shift, theta=theta),
    )
    x, y, z = section.pos_from_coord(i=0, j=j)
    assert x == approx(
        (pixel_resolution) * (j * cos(radians(theta)) + x_shift))
    assert y == approx(
        (pixel_resolution) * (j * sin(radians(theta)) + y_shift))
    assert z == 0
Ejemplo n.º 5
0
def test_can_get_3d_position_from_2d_pixel_coordinate_in_section(
        i, j, right, superior, anterior, pixel_resolution):
    section = Section(
        image=ImageData(
            channels=arange(24).reshape(2, 3, 4),
            pixel_resolution_um=pixel_resolution,
        ),
        plane_3d=AtlasTransform(right=right,
                                superior=superior,
                                anterior=anterior),
    )
    x, y, z = section.pos_from_coord(i=i, j=j)  # observed 3D positions
    assert x == approx((j * pixel_resolution) + right)
    assert y == approx((-i * pixel_resolution) + superior)
    assert z == approx(anterior)
Ejemplo n.º 6
0
def test_section_registration_cuts_correctly_with_diff_resolutions(case):
    volume = np.zeros((3, 3, 3))
    volume[1, 1, 1] = 1
    volume[0, 0, 0] = 2
    volume[0, 0, 0] = 2
    atlas = Atlas(
        volume=volume,
        resolution_um=case['atlas_res'],
    )
    section = Section(
        image=ImageData(
            channels=np.ones((1, 3, 3)),
            pixel_resolution_um=case["section_res"],
        ),
        plane_3d=AtlasTransform(**case["pos"]),
    )
    atlas_slice = register(section, atlas).image.channels[0]
    expected_slice = np.array(case['expected']).astype(float)
    try:
        assert np.all(np.isclose(atlas_slice, expected_slice))
    except:
        assert np.all(
            atlas_slice ==
            expected_slice)  # similar, but nicer printout of arrays in pytest


# # different dimensions
# # rotate
# # plane_2d: image origin
# # (get visibility on atlas indices)
Ejemplo n.º 7
0
def repo():
    repo = Mock(BaseSectionRepo)
    repo.sections = [
        Section(image=ImageData(channels=np.arange(12).reshape(2, 3, 2),
                                pixel_resolution_um=12), )
    ]
    return repo
Ejemplo n.º 8
0
def repo():
    repo = Mock(BaseSectionRepo)
    repo.sections = [
        Section(image=ImageData(channels=np.random.random((2, 3, 4)),
                                pixel_resolution_um=20.))
    ]
    return repo
Ejemplo n.º 9
0
def test_section_origin_set_sets_shift_to_half_half_and_shift_matrix_to_half_width_height(
        width, height):
    section = Section(image=ImageData(channels=np.random.random((3, height,
                                                                 width)),
                                      pixel_resolution_um=120))
    assert section.plane_2d.i == 0 and section.plane_2d.j == 0
    section2 = section.set_image_origin_to_center()
    res = section2.image.pixel_resolution_um
    height, width = section2.image.height, section2.image.width
    assert section2.plane_2d.i == -0.5 and section2.plane_2d.j == -0.5
    expected_shift_matrix = np.array([
        [res, 0, 0, -res * height / 2],
        [0, res, 0, -res * width / 2],
        [0, 0, 1, 0],
        [0, 0, 0, 1],
    ])
    assert np.all(np.isclose(section2.shift_matrix, expected_shift_matrix))
Ejemplo n.º 10
0
def repo():
    repo = Mock(BaseSectionRepo)
    repo.sections = [
        Section(
            image=ImageData(channels=np.random.random((2, 3, 4)), pixel_resolution_um=12.),
            plane_3d=AtlasTransform(right=5, superior=2, anterior=20),
        )
    ]
    return repo
Ejemplo n.º 11
0
    def slice(self, plane: AtlasTransform) -> Section:
        new_volume = affine_transform(self.volume,
                                      matrix=plane.affine_transform,
                                      cval=0.)
        slice_image = new_volume[:, :, 0][np.newaxis, :, :]

        return Section(image=ImageData(channels=slice_image,
                                       pixel_resolution_um=self.resolution_um),
                       plane_3d=plane,
                       thickness_um=self.resolution_um)
Ejemplo n.º 12
0
def test_repo_overwrites_existing_section(repo, section1: Section):
    assert len(repo.sections) == 0
    repo.save_section(section=section1)
    assert len(repo.sections) == 1
    repo.save_section(section=section1)
    assert len(repo.sections) == 1

    section_moved = section1.translate(right=3)
    repo.save_section(section=section_moved)
    assert len(repo.sections) == 1
Ejemplo n.º 13
0
    def __call__(self, filename: str) -> None:  # type: ignore

        log = lambda msg: print(msg,
                                section,
                                section.affine_transform,
                                section.image.affine_transform,
                                (section.image.height, section.image.width),
                                sep="\n",
                                end="\n\n")
        slice_image = self._reader.read(filename=filename)
        section = Section(image=slice_image)
        log("Loaded")
        section = section.set_image_origin_to_center()
        log("Origin Set to Center")
        section = section.resample(resolution_um=10)
        log("Resampled")

        self._repo.save_section(section=section)
        self.section_loaded.emit(image=section.image.channels[0],
                                 transform=section.affine_transform)
Ejemplo n.º 14
0
def register(section: Section, atlas: Atlas) -> Section:
    width, height = section.image.width, section.image.height
    inds = inds_homog(height=height, width=width)
    transform = np.linalg.inv(atlas.affine_transform) @ section.affine_transform
    brightness_3d = _register(inds, volume=atlas.volume, transform=transform)
    registered_slice = section.with_new_image(
        ImageData(
            channels=brightness_3d.reshape(1, height, width), 
            pixel_resolution_um=section.image.pixel_resolution_um,
        )
    )
    return registered_slice
Ejemplo n.º 15
0
def test_section_registration_to_an_atlas_gets_a_section_that_matches_sections_parameters():
    section = Section(
        image=ImageData(
            channels=np.random.random((3, 4, 5)), 
            pixel_resolution_um=10,
        ),
        plane_2d=Plane2D(x=3, y=5, theta=20),
        plane_3d=Plane3D(x=10, y=-5, z=10),
        )
    atlas = Atlas(volume=np.random.random((5, 5, 5)), resolution_um=20)
    s2 = register(section, atlas)
    assert type(s2) is Section
    assert s2.image.pixel_resolution_um == section.image.pixel_resolution_um
    assert s2.id != section.id and s2 is not section
    assert s2.image.width == section.image.width
    assert s2.image.height == section.image.height
    assert np.all(np.isclose(s2.affine_transform, section.affine_transform))
Ejemplo n.º 16
0
 def __call__(self, filename: str) -> None:  # type: ignore
     slice_image = self._reader.read(filename=filename)
     section = Section(image=slice_image).recenter()
     self._repo.save_section(section=section)
     self.section_loaded.emit(image=section.image.channels[0],
                              transform=section.affine_transform)
Ejemplo n.º 17
0
def test_resample_section_gets_new_section_with_resampled_image():
    section = Section(image=ImageData(channels=np.random.random((3, 4, 4)),
                                      pixel_resolution_um=12))
    section2 = section.resample(resolution_um=24)
    assert isinstance(section2, Section)
    assert section2.image.pixel_resolution_um == 24
Ejemplo n.º 18
0
def section2():
    return Section(image=ImageData(
        channels=np.arange(12).reshape(2, 3, 2),
        pixel_resolution_um=12,
    ), )