Пример #1
0
def test_edt32_indices_wrong_format(shape: Tuple[int, ...], dtype: np.dtype):
    x = np.random.random((10, )) > 0.5
    indices = np.zeros((x.ndim, ) + shape, dtype=dtype)
    with pytest.raises(RuntimeError):
        stats.distance_transform_edt_float32(input=x,
                                             sampling=[1.2],
                                             indices=indices)
Пример #2
0
def test_distance_transform_edt01(type_):
    # euclidean distance transform (edt)
    data = np.array(
        [
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 1, 1, 1, 0, 0, 0],
            [0, 0, 1, 1, 1, 1, 1, 0, 0],
            [0, 0, 1, 1, 1, 1, 1, 0, 0],
            [0, 0, 1, 1, 1, 1, 1, 0, 0],
            [0, 0, 0, 1, 1, 1, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
        ],
        type_,
    )
    out, ft = stats.distance_transform_edt_float32(data, return_indices=True)
    bf = ndimage.distance_transform_bf(data, "euclidean")
    assert_array_almost_equal(bf, out)

    dt = ft - np.indices(ft.shape[1:], dtype=ft.dtype)
    dt = dt.astype(np.float64)
    np.multiply(dt, dt, dt)
    dt = np.add.reduce(dt, axis=0)
    np.sqrt(dt, dt)

    assert_array_almost_equal(bf, dt)
Пример #3
0
def test_distance_transform_edt4(type_):
    data = np.array(
        [
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 1, 1, 1, 0, 0, 0],
            [0, 0, 1, 1, 1, 1, 1, 0, 0],
            [0, 0, 1, 1, 1, 1, 1, 0, 0],
            [0, 0, 1, 1, 1, 1, 1, 0, 0],
            [0, 0, 0, 1, 1, 1, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
        ],
        type_,
    )
    ref = ndimage.distance_transform_bf(data, "euclidean", sampling=[2, 1])
    out = stats.distance_transform_edt_float32(data, sampling=[2, 1])
    assert_array_almost_equal(ref, out)
Пример #4
0
def test_distance_transform_edt5():
    # Ticket #954 regression test
    out = stats.distance_transform_edt_float32(False)
    assert_array_almost_equal(out, [0.])
Пример #5
0
def test_distance_transform_edt02(type_):
    data = np.array(
        [
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 1, 1, 1, 0, 0, 0],
            [0, 0, 1, 1, 1, 1, 1, 0, 0],
            [0, 0, 1, 1, 1, 1, 1, 0, 0],
            [0, 0, 1, 1, 1, 1, 1, 0, 0],
            [0, 0, 0, 1, 1, 1, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0, 0, 0],
        ],
        type_,
    )
    tdt, tft = stats.distance_transform_edt_float32(data, return_indices=True)
    dts = []
    fts = []
    dt = np.zeros(data.shape, dtype=np.float32)
    stats.distance_transform_edt_float32(data, distances=dt)
    dts.append(dt)
    ft = stats.distance_transform_edt_float32(data,
                                              return_distances=0,
                                              return_indices=True)
    fts.append(ft)
    ft = np.indices(data.shape, dtype=np.int32)
    stats.distance_transform_edt_float32(data,
                                         return_distances=False,
                                         return_indices=True,
                                         indices=ft)
    fts.append(ft)
    dt, ft = stats.distance_transform_edt_float32(data, return_indices=True)
    dts.append(dt)
    fts.append(ft)
    dt = np.zeros(data.shape, dtype=np.float32)
    ft = stats.distance_transform_edt_float32(data,
                                              distances=dt,
                                              return_indices=True)
    dts.append(dt)
    fts.append(ft)
    ft = np.indices(data.shape, dtype=np.int32)
    dt = stats.distance_transform_edt_float32(data,
                                              return_indices=True,
                                              indices=ft)
    dts.append(dt)
    fts.append(ft)
    dt = np.zeros(data.shape, dtype=np.float32)
    ft = np.indices(data.shape, dtype=np.int32)
    stats.distance_transform_edt_float32(data,
                                         distances=dt,
                                         return_indices=True,
                                         indices=ft)
    dts.append(dt)
    fts.append(ft)
    for dt in dts:
        assert_array_almost_equal(tdt, dt)
    for ft in fts:
        assert_array_almost_equal(tft, ft)