コード例 #1
0
ファイル: test_image.py プロジェクト: HernandoMV/slicereg
def test_downsampling_image_produces_correct_resolution_and_data_shape(scale):
    image = ImageData(channels=np.arange(24).reshape(1, 6, 4), pixel_resolution_um=12)
    image2 = image.resample(scale)
    assert image2.pixel_resolution_um == (image.pixel_resolution_um / scale)
    assert image2.num_channels == image.num_channels
    assert approx(image.width * scale == image2.width, abs=1)
    assert approx(image.height * scale == image2.height, abs=1)
コード例 #2
0
def test_downsampling_image_produces_correct_resolution_and_data_shape(
        to_resolution):
    from_resolution = 12
    image = ImageData(channels=np.arange(24).reshape(1, 6, 4),
                      pixel_resolution_um=from_resolution)
    image2 = image.resample(resolution_um=to_resolution)
    assert image2.pixel_resolution_um == to_resolution
    assert image2.num_channels == image.num_channels

    scale_ratio = from_resolution / to_resolution
    assert approx(image.width * scale_ratio == image2.width, abs=1)
    assert approx(image.height * scale_ratio == image2.height, abs=1)
コード例 #3
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
コード例 #4
0
def repo():
    repo = Mock(BaseSectionRepo)
    repo.sections = [
        Section(image=ImageData(channels=np.random.random((2, 3, 4)),
                                pixel_resolution_um=20.))
    ]
    return repo
コード例 #5
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)
コード例 #6
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)
コード例 #7
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
コード例 #8
0
ファイル: test_image.py プロジェクト: HernandoMV/slicereg
def test_image_scale_matrix_converts_pixel_resolution_to_um_space(width, height, channels, res):
    image = ImageData(channels=np.random.random(size=(channels, height, width)), pixel_resolution_um=res)
    r = res
    expected = np.array([
        [1/r, 0, 0, 0],
        [0, 1/r, 0, 0],
        [0, 0, 1, 0],
        [0, 0, 0, 1],
    ])
    assert np.all(np.isclose(image.scale_matrix, expected))
コード例 #9
0
def test_image_shape_matrix_calculation(width, height):
    image = ImageData(channels=np.random.random((2, height, width)),
                      pixel_resolution_um=10)
    expected = np.array([
        [1, 0, 0, image.height],
        [0, 1, 0, image.width],
        [0, 0, 1, 0],
        [0, 0, 0, 1],
    ])
    assert approx(image.shape_matrix == expected)
コード例 #10
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)
コード例 #11
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)
コード例 #12
0
ファイル: ome_tiff.py プロジェクト: HernandoMV/slicereg
    def read(self, filename: str) -> ImageData:
        f = tifffile.TiffFile(filename)
        image = f.asarray()
        assert image.ndim == 3
        assert image.dtype == uint16

        metadata = xmltodict.parse(f.ome_metadata)
        pix_mdata = metadata['OME']['Image']['Pixels']
        res_x, res_y = pix_mdata['@PhysicalSizeX'], pix_mdata['@PhysicalSizeY']
        assert res_x == res_y, \
            "Pixels are not square"

        return ImageData(channels=image, pixel_resolution_um=1 / float(res_x))
コード例 #13
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
コード例 #14
0
ファイル: test_image.py プロジェクト: HernandoMV/slicereg
def test_downsampling_beyond_dimensions_produces_valueerror(scale):
    image = ImageData(channels=np.arange(24).reshape(1, 4, 6), pixel_resolution_um=12)
    if scale <= 0:
        with pytest.raises(ValueError,  match=r".* positive.*"):
            image.resample(scale)
    elif scale < 0.25:
        with pytest.raises(ValueError,  match=r".* small.*"):
            image.resample(scale)
コード例 #15
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)
コード例 #16
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))
コード例 #17
0
def reader(image_data):
    reader = Mock(BaseSectionReader)
    reader.read.return_value = ImageData(channels=image_data,
                                         pixel_resolution_um=10)
    return reader
コード例 #18
0
def test_downsampling_beyond_dimensions_produces_valueerror(to_resolution):
    image = ImageData(channels=np.arange(24).reshape(1, 4, 6),
                      pixel_resolution_um=12)
    with pytest.raises(ValueError, match=r".* positive.*"):
        image.resample(resolution_um=to_resolution)
コード例 #19
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
コード例 #20
0
def test_image_aspect_ratio_calculation(width, height):
    image = ImageData(channels=np.random.random((2, height, width)),
                      pixel_resolution_um=10)
    assert approx(image.aspect_ratio == width / height)
コード例 #21
0
def section2():
    return Section(image=ImageData(
        channels=np.arange(12).reshape(2, 3, 2),
        pixel_resolution_um=12,
    ), )
コード例 #22
0
ファイル: test_image.py プロジェクト: HernandoMV/slicereg
def test_image_reports_correct_number_of_channels(shape, num_channels):
    image = ImageData(channels=np.random.random(size=shape), pixel_resolution_um=12)
    assert image.num_channels == num_channels
コード例 #23
0
ファイル: test_image.py プロジェクト: HernandoMV/slicereg
def test_downsampling_image_produces_im_with_similar_statistical_properties_as_original(scale):
    image = ImageData(channels=np.random.random((2, 120, 120)), pixel_resolution_um=12)
    im2 = image.resample(scale)
    assert np.all(np.isclose(image.channels.mean(), im2.channels.mean(), rtol=3e-2))
    assert np.all(np.isclose(image.channels.std(), im2.channels.std(), rtol=3e-2))
コード例 #24
0
ファイル: test_image.py プロジェクト: HernandoMV/slicereg
def test_image_width(shape, width):
    image = ImageData(channels=np.random.random(size=shape), pixel_resolution_um=12)
    assert image.width == width
コード例 #25
0
ファイル: test_image.py プロジェクト: HernandoMV/slicereg
def test_image_height(shape, height):
    image = ImageData(channels=np.random.random(size=shape), pixel_resolution_um=12)
    assert image.height == height