Exemple #1
0
def get_suite(plugin, plugin_kwargs=None, tags=None):

    if plugin_kwargs is None:
        plugin_kwargs = {}

    if tags is None:
        tags = []

    def test_input_d_1_default_remove_mean_threshold_stretch():

        arr_in = np.zeros((20, 30, 1), dtype=DTYPE)
        inker_shape = 5, 5
        arr_out = np.zeros((16, 26, 1), dtype=DTYPE)
        np.random.seed(42)
        data = np.random.randn(np.prod(arr_in.shape))
        arr_in[:] = data.reshape(arr_in.shape)

        idx = [[4, 3], [20, 12]]
        gt = np.array([[0.20177312], [0.21249016]], dtype=DTYPE)

        lnorm(arr_in, arr_out=arr_out,
              inker_shape=inker_shape, outker_shape=inker_shape,
              plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose(gv, gt, rtol=RTOL, atol=ATOL)

        arr_out = lnorm(arr_in,
                        inker_shape=inker_shape, outker_shape=inker_shape,
                        plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose(gv, gt, rtol=RTOL, atol=ATOL)

    def test_input_d_4_div_euclidean_remove_mean_false_default_rest():

        arr_in = np.zeros((20, 30, 4), dtype=DTYPE)
        inker_shape = 5, 5
        arr_out = np.zeros((16, 26, 4), dtype=DTYPE)
        np.random.seed(42)
        arr_in[:] = np.random.randn(np.prod(arr_in.shape)).reshape(arr_in.shape)

        idx = [[4, 3], [20, 12]]
        gt = np.array([[ 0.13273999, -0.09456467,
                        -0.01975331, -0.04648187],
                       [ 0.00148955, -0.00257985,
                         0.02118244, -0.01543736]],
                      dtype=DTYPE)

        lnorm(arr_in, arr_out=arr_out,
              inker_shape=inker_shape, outker_shape=inker_shape,
              div_method='euclidean', remove_mean=False,
              plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose(gv, gt, rtol=RTOL, atol=ATOL)

        arr_out = lnorm(arr_in,
                        inker_shape=inker_shape, outker_shape=inker_shape,
                        div_method='euclidean', remove_mean=False,
                        plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose(gv, gt, rtol=RTOL, atol=ATOL)

    def test_div_euclidean_remove_mean_false_threshold_1_stretch_1e_2():
        arr_in = np.zeros((20, 30, 4), dtype=DTYPE)
        inker_shape = 5, 5
        arr_out = np.zeros((16, 26, 4), dtype=DTYPE)
        np.random.seed(42)
        arr_in[:] = np.random.randn(np.prod(arr_in.shape)).reshape(arr_in.shape)

        idx = [[4, 3], [20, 12]]
        gt = np.array([[ 0.01255756, -0.00894607,
                        -0.00186872, -0.00439731],
                       [ 0.00013929, -0.00024125,
                         0.00198085, -0.0014436 ]],
                      dtype=DTYPE)

        lnorm(arr_in, arr_out=arr_out,
              inker_shape=inker_shape, outker_shape=inker_shape,
              div_method='euclidean', remove_mean=False, threshold=1, stretch=1e-2,
              plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose(gv, gt, rtol=RTOL, atol=ATOL)

        arr_out = lnorm(arr_in,
                        inker_shape=inker_shape, outker_shape=inker_shape,
                        div_method='euclidean', remove_mean=False, threshold=1, stretch=1e-2,
                        plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose(gv, gt, rtol=RTOL, atol=ATOL)

    def test_div_euclidean_remove_mean_true():

        arr_in = np.zeros((20, 30, 4), dtype=DTYPE)
        inker_shape = 5, 5
        arr_out = np.zeros((16, 26, 4), dtype=DTYPE)
        np.random.seed(42)
        arr_in[:] = np.random.randn(np.prod(arr_in.shape)).reshape(arr_in.shape)

        idx = [[4, 3], [20, 12]]

        gt = np.array([[  1.27813682e-01, -9.97862518e-02,
                         -2.48777084e-02, -5.16409911e-02],
                       [ -2.00690944e-02, -2.42322776e-02,
                          7.76741435e-05, -3.73861268e-02]],
                      dtype=DTYPE)

        lnorm(arr_in, arr_out=arr_out,
              inker_shape=inker_shape, outker_shape=inker_shape,
              div_method='euclidean', remove_mean=True,
              plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose(gv, gt, rtol=RTOL, atol=ATOL)

        arr_out = lnorm(arr_in,
                        inker_shape=inker_shape, outker_shape=inker_shape,
                        div_method='euclidean', remove_mean=True,
                        plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose(gv, gt, rtol=RTOL, atol=ATOL)

    def test_div_std_remove_mean_false():

        arr_in = np.zeros((20, 30, 4), dtype=DTYPE)
        inker_shape = 5, 5
        arr_out = np.zeros((16, 26, 4), dtype=DTYPE)
        np.random.seed(42)
        arr_in[:] = np.random.randn(np.prod(arr_in.shape)).reshape(arr_in.shape)

        idx = [[4, 8], [20, 12]]

        gt = np.array([[  1.32899761, -0.94678491,
                         -0.19777086, -0.46537822],
                       [  1.67757177,  0.42027149,
                         -0.70711917, -0.05593578]],
                      dtype=DTYPE)

        lnorm(arr_in, arr_out=arr_out,
              inker_shape=inker_shape, outker_shape=inker_shape,
              div_method='std', remove_mean=False,
              plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose(gv, gt, rtol=RTOL, atol=ATOL)

        arr_out = lnorm(arr_in,
                        inker_shape=inker_shape, outker_shape=inker_shape,
                        div_method='std', remove_mean=False,
                        plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose(gv, gt, rtol=RTOL, atol=ATOL)

    def test_div_std_remove_mean_true():

        arr_in = np.zeros((20, 30, 4), dtype=DTYPE)
        inker_shape = 5, 5
        arr_out = np.zeros((16, 26, 4), dtype=DTYPE)
        np.random.seed(42)
        arr_in[:] = np.random.randn(np.prod(arr_in.shape)).reshape(arr_in.shape)

        idx = [[4, 8], [20, 12]]
        gt = np.array([[  1.27801514, -0.99776751,
                         -0.2487534 , -0.51636076],
                       [  1.42037416,  0.16307378,
                         -0.9643169 , -0.31313351]],
                      dtype=DTYPE)

        lnorm(arr_in, arr_out=arr_out,
              inker_shape=inker_shape, outker_shape=inker_shape,
              div_method='std', remove_mean=True,
              plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose(gv, gt, rtol=RTOL, atol=ATOL)

        arr_out = lnorm(arr_in,
                        inker_shape=inker_shape, outker_shape=inker_shape,
                        div_method='std', remove_mean=True,
                        plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose(gv, gt, rtol=RTOL, atol=ATOL)

    def test_outker_shape_0_div_mag_remove_mean_false():
        arr_in = np.zeros((20, 30, 4), dtype=DTYPE)
        inker_shape = 5, 5
        outker_shape = 0, 0
        arr_out = np.zeros((16, 26, 4), dtype=DTYPE)
        np.random.seed(42)
        arr_in[:] = np.random.randn(np.prod(arr_in.shape)).reshape(arr_in.shape)

        idx = [[4, 3], [20, 12]]

        gt = np.array([[ 0.24052431, -0.18180957,
                        -0.04978044, -0.0898783 ],
                       [ 0.00301287, -0.00500357,
                         0.04109935, -0.03260877]],
                      dtype=DTYPE)

        lnorm(arr_in, arr_out=arr_out,
              inker_shape=inker_shape, outker_shape=outker_shape,
              div_method='euclidean', remove_mean=False,
              plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose(gv, gt, rtol=RTOL, atol=ATOL)

        arr_out = lnorm(arr_in,
                        inker_shape=inker_shape, outker_shape=outker_shape,
                        div_method='euclidean', remove_mean=False,
                        plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose(gv, gt, rtol=RTOL, atol=ATOL)

    def test_outker_shape_0_div_mag_remove_mean_true():
        arr_in = np.zeros((20, 30, 4), dtype=DTYPE)
        inker_shape = 5, 5
        outker_shape = 0, 0
        arr_out = np.zeros((16, 26, 4), dtype=DTYPE)
        np.random.seed(42)
        arr_in[:] = np.random.randn(np.prod(arr_in.shape)).reshape(arr_in.shape)

        idx = [[4, 3], [20, 12]]

        gt = np.array([[ 0.18866782, -0.17986178,
                        -0.05663793, -0.06177634],
                       [-0.00420652, -0.03951693,
                        -0.0673274 , -0.05859426]],
                      dtype=DTYPE)

        lnorm(arr_in, arr_out=arr_out,
              inker_shape=inker_shape, outker_shape=outker_shape,
              div_method='euclidean', remove_mean=True,
              plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose(gv, gt, rtol=RTOL, atol=ATOL)

        arr_out = lnorm(arr_in,
                        inker_shape=inker_shape, outker_shape=outker_shape,
                        div_method='euclidean', remove_mean=True,
              plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose(gv, gt, rtol=RTOL, atol=ATOL)

    def test_outker_shape_0_div_std_remove_mean_false():
        arr_in = np.zeros((20, 30, 4), dtype=DTYPE)
        inker_shape = 5, 5
        outker_shape = 0, 0
        arr_out = np.zeros((16, 26, 4), dtype=DTYPE)
        np.random.seed(42)
        arr_in[:] = np.random.randn(np.prod(arr_in.shape)).reshape(arr_in.shape)

        idx = [[4, 8], [20, 12]]

        gt = np.array([[ 1.26222396, -0.90901738,
                        -0.24902068, -0.45406818],
                       [ 1.54160333,  0.49371463,
                         -0.80440265, -0.05310058]],
                      dtype=DTYPE)

        lnorm(arr_in, arr_out=arr_out,
              inker_shape=inker_shape, outker_shape=outker_shape,
              div_method='std', remove_mean=False,
              plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose(gv, gt, rtol=RTOL, atol=ATOL)

        arr_out = lnorm(arr_in,
                        inker_shape=inker_shape, outker_shape=outker_shape,
                        div_method='std', remove_mean=False,
                        plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose(gv, gt, rtol=RTOL, atol=ATOL)


    def test_outker_shape_0_div_std_remove_mean_true():
        arr_in = np.zeros((20, 30, 4), dtype=DTYPE)
        inker_shape = 5, 5
        outker_shape = 0, 0
        arr_out = np.zeros((16, 26, 4), dtype=DTYPE)
        np.random.seed(42)
        arr_in[:] = np.random.randn(np.prod(arr_in.shape)).reshape(arr_in.shape)

        idx = [[4, 8], [20, 12]]
        gt = np.array([[ 0.94326323, -0.89923584,
                        -0.28315943, -0.30885619],
                       [ 1.27807069,  0.63492846,
                         -1.23798132, -0.50979644]],
                      dtype=DTYPE)

        lnorm(arr_in, arr_out=arr_out,
              inker_shape=inker_shape, outker_shape=outker_shape,
              div_method='std', remove_mean=True,
              plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose(gv, gt, rtol=RTOL, atol=ATOL)

        arr_out = lnorm(arr_in,
                        inker_shape=inker_shape, outker_shape=outker_shape,
                        div_method='std', remove_mean=True,
                        plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose(gv, gt, rtol=RTOL, atol=ATOL)


    def test_lena_npy_array():

        arr_in = lena()[::32, ::32].astype(DTYPE)

        idx = [[4, 2], [4, 2]]

        gt = np.array([0.2178068, 0.30647671],
                      dtype=DTYPE)

        arr_out = lnorm(arr_in,
                        plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose(gv, gt, rtol=RTOL, atol=ATOL)


    def test_lena_npy_array_non_C_contiguous():

        arr_in = lena()[::32, ::32].astype(DTYPE)
        arr_in = np.asfortranarray(arr_in)

        idx = [[4, 2], [4, 2]]

        gt = np.array([0.2178068, 0.30647671],
                      dtype=DTYPE)


        try:
            arr_out = lnorm(arr_in,
                            plugin=plugin, plugin_kwargs=plugin_kwargs)
            gv = arr_out[idx]
            assert_allclose(gv, gt, rtol=RTOL, atol=ATOL)
        except NotImplementedError:
            raise SkipTest

    def test_lena_pt3_array():

        lena32 = lena()[::32, ::32].astype(DTYPE)/255.
        arr_in = Array(lena32.shape, dtype=DTYPE)
        arr_in[:] = lena32

        idx = [[4, 2], [4, 2]]

        gt = np.array([0.21779411,  0.30645376],
                      dtype=DTYPE)

        arr_out = lnorm(arr_in,
                        plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose(gv, gt, rtol=RTOL, atol=ATOL)


    suite = {}
    if plugin_kwargs:
        plugin_kwarg_hash = '__kwargs_hash_' + get_pkl_sha1(plugin_kwargs)
    else:
        plugin_kwarg_hash = ""

    for key, value in locals().iteritems():
        if isinstance(value, types.FunctionType) and key.startswith('test_'):
            func = value
            func.__name__ += '__plugin_%s' % plugin + plugin_kwarg_hash
            func = attr(*tags)(info(plugin, plugin_kwargs, func))
            suite[func.__name__] = func

    return suite
Exemple #2
0
def get_suite(plugin, plugin_kwargs=None, tags=None):

    if plugin_kwargs is None:
        plugin_kwargs = {}

    if tags is None:
        tags = []

    def test_default_lena():

        arr_in = lena().astype(np.float32) / 1.0
        idx = [[4, 2], [4, 2]]

        gt = np.array([1442.0, 1455.0], dtype=DTYPE)

        arr_out = lpool(arr_in, plugin=plugin, plugin_kwargs=plugin_kwargs)
        assert_equals(arr_out.ndim, arr_in.ndim)
        gv = arr_out[idx]
        print gv.shape
        print gv

        assert_allclose_round(gv, gt, rtol=RTOL, atol=ATOL)

    def test_default_lena_non_C_contiguous():

        arr_in = lena() / 1.0
        arr_in = np.asfortranarray(arr_in)
        idx = [[4, 2], [4, 2]]

        gt = np.array([1442.0, 1455.0], dtype=DTYPE)

        try:
            arr_out = lpool(arr_in, plugin=plugin, plugin_kwargs=plugin_kwargs)
            assert_equals(arr_out.ndim, arr_in.ndim)
            gv = arr_out[idx]
            print gv.shape
            print gv

            assert_allclose_round(gv, gt, rtol=RTOL, atol=ATOL)
        except NotImplementedError:
            raise SkipTest

    def test_default_input_d_1():

        arr_in = np.zeros((20, 30, 1), dtype=DTYPE)
        ker_shape = 5, 5
        arr_out = np.zeros((16, 26, 1), dtype=DTYPE)
        np.random.seed(42)
        arr_in[:] = np.random.randn(np.prod(arr_in.shape)).reshape(arr_in.shape)

        idx = [[4, 8], [20, 12]]

        gt = np.array([[-3.59586287], [9.16217232]], dtype=DTYPE)

        lpool(arr_in, arr_out=arr_out, ker_shape=ker_shape, plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose_round(gv, gt, rtol=RTOL, atol=ATOL)

        arr_out = lpool(arr_in, ker_shape=ker_shape, plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose_round(gv, gt, rtol=RTOL, atol=ATOL)

    def test_ker_shape_5():
        arr_in = np.zeros((20, 30, 4), dtype=DTYPE)
        ker_shape = 5, 5
        arr_out = np.zeros((16, 26, 4), dtype=DTYPE)
        np.random.seed(42)
        arr_in[:] = np.random.randn(np.prod(arr_in.shape)).reshape(arr_in.shape)

        idx = [[4, 8], [20, 12]]

        gt = np.array(
            [[7.93315649, -0.24066222, 0.64046526, -3.51567388], [7.14803362, -2.99622989, 9.5001564, 11.99116325]],
            dtype=DTYPE,
        )

        lpool(arr_in, arr_out=arr_out, ker_shape=ker_shape, plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose_round(gv, gt, rtol=RTOL, atol=ATOL)

        arr_out = lpool(arr_in, ker_shape=ker_shape, plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose_round(gv, gt, rtol=RTOL, atol=ATOL)

    def test_ker_shape_5_order_2():
        arr_in = np.zeros((20, 30, 4), dtype=DTYPE)
        ker_shape = 5, 5
        arr_out = np.zeros((16, 26, 4), dtype=DTYPE)
        np.random.seed(42)
        arr_in[:] = np.random.randn(np.prod(arr_in.shape)).reshape(arr_in.shape)
        order = 2

        idx = [[4, 8], [20, 12]]

        gt = np.array(
            [[5.22081137, 4.9204731, 3.75381684, 4.892416], [5.60951042, 4.28514147, 4.77592659, 5.77252817]],
            dtype=DTYPE,
        )

        lpool(arr_in, arr_out=arr_out, ker_shape=ker_shape, order=order, plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose_round(gv, gt, rtol=RTOL, atol=ATOL)

        arr_out = lpool(arr_in, ker_shape=ker_shape, order=order, plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose_round(gv, gt, rtol=RTOL, atol=ATOL)

    def test_ker_shape_5_order_2_stride_2():

        arr_in = np.zeros((20, 30, 4), dtype=DTYPE)
        ker_shape = 5, 5
        arr_out = np.zeros((8, 13, 4), dtype=DTYPE)
        np.random.seed(42)
        arr_in[:] = np.random.randn(np.prod(arr_in.shape)).reshape(arr_in.shape)
        order = 2
        stride = 2

        idx = [[4, 3], [10, 6]]

        gt = np.array(
            [[4.81159449, 5.68032312, 5.07941389, 6.04614496], [4.87372255, 4.47074938, 4.07878876, 4.43441534]],
            dtype=DTYPE,
        )

        lpool(
            arr_in,
            arr_out=arr_out,
            ker_shape=ker_shape,
            order=order,
            stride=stride,
            plugin=plugin,
            plugin_kwargs=plugin_kwargs,
        )
        gv = arr_out[idx]
        assert_allclose_round(gv, gt, rtol=RTOL, atol=ATOL)

        arr_out = lpool(
            arr_in, ker_shape=ker_shape, order=order, stride=stride, plugin=plugin, plugin_kwargs=plugin_kwargs
        )
        gv = arr_out[idx]
        assert_allclose_round(gv, gt, rtol=RTOL, atol=ATOL)

    def test_strides_self():

        arr_in = lena() / 1.0
        arr_in = arr_in[:12, :12]

        for stride in xrange(1, 12):

            try:
                arr_out = lpool(arr_in, plugin=plugin, plugin_kwargs=plugin_kwargs)
                gt = arr_out[::stride, ::stride]
                gv = lpool(arr_in, stride=stride, plugin=plugin, plugin_kwargs=plugin_kwargs)
                assert_array_equal(gv, gt)
            except NotImplementedError:
                raise SkipTest

    def test_strides_scipy_naive():

        arr_in = lena() / 1.0
        arr_in = arr_in[:12, :12]

        for stride in xrange(1, 12):
            try:
                gt = lpool(arr_in, stride=stride, plugin="scipy_naive")
                gv = lpool(arr_in, stride=stride, plugin=plugin, plugin_kwargs=plugin_kwargs)
                assert_array_equal(gv, gt)
            except NotImplementedError:
                raise SkipTest

    def test_test_lena_npy_array():

        arr_in = lena()[::32, ::32].astype(DTYPE)

        idx = [[4, 2], [4, 2]]

        gt = np.array([1280.99987793, 992.0], dtype=DTYPE)

        arr_out = lpool(arr_in, plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose_round(gv, gt, rtol=RTOL, atol=ATOL)

    def test_test_lena_pt3_array():

        lena32 = lena()[::32, ::32].astype(DTYPE) / 255.0
        arr_in = Array(lena32.shape, dtype=DTYPE)
        arr_in[:] = lena32

        idx = [[4, 2], [4, 2]]

        gt = np.array([5.02353001, 3.89019608], dtype=DTYPE)

        arr_out = lpool(arr_in, plugin=plugin, plugin_kwargs=plugin_kwargs)
        gv = arr_out[idx]
        assert_allclose_round(gv, gt, rtol=RTOL, atol=ATOL)

    suite = {}
    if plugin_kwargs:
        plugin_kwarg_hash = "__kwargs_hash_" + get_pkl_sha1(plugin_kwargs)
    else:
        plugin_kwarg_hash = ""

    for key, value in locals().iteritems():
        if isinstance(value, types.FunctionType) and key.startswith("test_"):
            func = value
            func.__name__ += "__plugin_%s" % plugin + plugin_kwarg_hash
            func = attr(*tags)(info(plugin, plugin_kwargs, func))
            suite[func.__name__] = func

    return suite