def test_whiten_data(self):
     '''Test that whitening transform produce unit diagonal covariance.'''
     import spectral as spy
     stats = spy.calc_stats(self.data)
     wdata = stats.get_whitening_transform()(self.data)
     wstats = spy.calc_stats(wdata)
     assert_allclose(wstats.cov, np.eye(wstats.cov.shape[0]), atol=1e-8)
 def test_matrix_sqrt_eigs(self):
     import spectral as spy
     from spectral.algorithms.spymath import matrix_sqrt
     stats = spy.calc_stats(self.data)
     (evals, evecs) = np.linalg.eig(stats.cov)
     S = matrix_sqrt(eigs=(evals, evecs))
     assert_allclose(S.dot(S), self.C, atol=1e-8)
示例#3
0
def denoise(data, num=None, snr=None):
    """
    Denoises nd array with the format n x p x b

    Parameters:
    -----------
    data : nd array
        3-d numpy array with b = band
    num : int
        number of bands used
    snr : int
        threshold
    Returns
    -------
    denoised array with same shape as data
    """
    signal = spectral.calc_stats(data)
    noise = spectral.noise_from_diffs(data)
    mnfr = spectral.mnf(signal, noise)
    if num:
        denoised, trans = mnfr.denoise(data, num=num)
        print(50*'_')
        print(trans.shape)
    elif snr:
        denoised = mnfr.denoise(data, snr=snr)
        print("--------------")
        print(mnfr.num_with_snr(snr=snr))
    else:
        raise ValueError('"snr" or "num" must be given!')
    return denoised, trans
示例#4
0
 def setup(self):
     from spectral.algorithms.detectors import MatchedFilter
     self.data = spy.open_image('92AV3C.lan').load()
     self.background = spy.calc_stats(self.data)
     self.target_ij = [33, 87]
     #        self.target = self.data[33, 87]
     (i, j) = self.target_ij
     self.mf = MatchedFilter(self.background, self.data[i, j])
示例#5
0
 def test_mnf_all_equals_data(self):
     '''Test that MNF transform with all components equals original data.'''
     data = self.data
     signal = spy.calc_stats(data)
     noise = spy.noise_from_diffs(data[117:137, 85:122, :])
     mnfr = spy.mnf(signal, noise)
     denoised = mnfr.denoise(data, num=data.shape[-1])
     assert_allclose(denoised, data)
示例#6
0
    def setup(self):
        from spectral.algorithms.detectors import MatchedFilter
        self.data = spy.open_image('92AV3C.lan').load()
        self.background = spy.calc_stats(self.data)
        self.target_ij = [33, 87]
#        self.target = self.data[33, 87]
        (i, j) = self.target_ij
        self.mf = MatchedFilter(self.background, self.data[i, j])
示例#7
0
 def test_mnf_all_equals_data(self):
     '''Test that MNF transform with all components equals original data.'''
     data = self.data
     signal = spy.calc_stats(data)
     noise = spy.noise_from_diffs(data[117: 137, 85: 122, :])
     mnfr = spy.mnf(signal, noise)
     denoised = mnfr.denoise(data, num=data.shape[-1])
     assert(np.allclose(denoised, data))
示例#8
0
    def test_ppi_centered(self):
        '''Tests that ppi with mean-subtracted data works as expected.'''
        data = self.data
        s = np.random.get_state()
        p = spy.ppi(data, 4)

        np.random.set_state(s)
        data_centered = data - spy.calc_stats(data).mean
        p2 = spy.ppi(data_centered, 4)
        np.all(p == p2)
示例#9
0
 def test_ppi_centered(self):
     '''Tests that ppi with mean-subtracted data works as expected.'''
     data = self.data
     s = np.random.get_state()
     p = spy.ppi(data, 4)
     
     np.random.set_state(s)
     data_centered = data - spy.calc_stats(data).mean
     p2 = spy.ppi(data_centered, 4)
     assert(np.all(p == p2))
示例#10
0
 def test_pca_runs_from_stats(self):
     '''Should be able to pass image stats to PCA function.'''
     data = self.data
     stats = spy.calc_stats(data)
     xdata = spy.principal_components(stats).transform(data)
示例#11
0
 def test_stats_property_sqrt_inv_cov(self):
     stats = spy.calc_stats(self.data)
     s = stats.sqrt_inv_cov.dot(stats.sqrt_inv_cov)
     assert_allclose(s, stats.inv_cov, atol=1e-8)
示例#12
0
 def test_matrix_sqrt_eigs(self):
     stats = spy.calc_stats(self.data)
     (evals, evecs) = np.linalg.eig(stats.cov)
     S = matrix_sqrt(eigs=(evals, evecs))
     assert_allclose(S.dot(S), self.C, atol=1e-8)
示例#13
0
 def setup(self):
     self.data = spy.open_image('92AV3C.lan').open_memmap()
     self.C = spy.calc_stats(self.data).cov
     self.X = np.array([[2., 1.], [1., 2.]])
instack = r"/home/jb/Downloads/stack_masked.tif"
outstack = r"/home/jb/Downloads/stack_denoised5.tif"

with rasterio.open(instack) as intif:
    stack = intif.read()
    land = stack[7, :, :] > 500
    np.count_nonzero(land)
    out_meta = intif.meta.copy()
    (stack[:, land]) = 0
    t_stack = np.transpose(stack, (1, 2, 0))
help(spectral.calc_stats)
ss = stack.shape[0]
print(t_stack.shape)
# help(spectral.calc_stats)
#view =imshow(t_stack,(8,3,2))
signal = spectral.calc_stats(t_stack)
noise = spectral.noise_from_diffs(t_stack)
mnfr = spectral.mnf(signal, noise)
t_denoised = mnfr.denoise(t_stack, num=5)
t_reduced = mnfr.reduce(t_stack, num=5)
t_reduced.shape
t_denoised.shape
tt_denoised = np.transpose(t_denoised, (2, 0, 1))
tt_reduced = np.transpose(t_reduced, (2, 0, 1))
tt_stack = np.transpose(t_stack, (2, 0, 1))
tt_reduced.shape
out_meta['count'] = 13
tt_denoised.shape
tt_denoised.shape
np.max(tt_denoised[2:, ])
np.min(tt_denoised[1:, ])
示例#15
0
 def setup(self):
     import spectral as spy
     self.data = spy.open_image('92AV3C.lan').open_memmap()
     self.C = spy.calc_stats(self.data).cov
     self.X = np.array([[2., 1.],[1., 2.]])
示例#16
0
 def setup(self):
     self.data = spy.open_image('92AV3C.lan').load()
     self.bg = spy.calc_stats(self.data)
     self.X = self.data[:20, :20, :]
示例#17
0
 def test_rx_bg_eq_zero(self):
     from spectral.algorithms.detectors import rx, RX
     d = rx(self.data)
     stats = spy.calc_stats(self.data)
     np.testing.assert_approx_equal(rx(stats.mean, background=stats), 0)
示例#18
0
 def setup(self):
     self.data = spy.open_image('92AV3C.lan').load()
     self.background = spy.calc_stats(self.data)
示例#19
0
 def setup(self):
     self.data = spy.open_image('92AV3C.lan').load()
     self.bg = spy.calc_stats(self.data)
     self.X = self.data[:20, :20, :]
示例#20
0
 
 if args.filled or args.mnf or args.pca:
     output_data[args.key+'-filled'] = fill_holes(output_data[args.key])
     
 if args.mnf:
     import spectral
     
     image = output_data[args.key+'-filled']
     #This is the reverse of the numpy masked array mask, 1 values will be used
     mask = numpy.ma.masked_invalid(output_data[args.key]).mask.sum(2) < image.shape[2] // 2
     
     deltas = image[:-1, :-1, :] - image[1:, 1:, :]
     deltas_mask = (mask[:-1, :-1].astype(numpy.int) + mask[1:, 1:].astype(numpy.int)) == 2
     
     
     signal = spectral.calc_stats(image, mask=mask)
     noise = spectral.calc_stats(deltas, mask=deltas_mask)
     noise.cov /= 2.0        
     #noise = spectral.noise_from_diffs( img[img.shape[0]//2-25:img.shape[0]//2+25, img.shape[1]//2-25:img.shape[1]//2+25,...])
     #import IPython
     #IPython.embed()
     mnfr = spectral.mnf(signal, noise)
     mnfimage = numpy.ma.MaskedArray(mnfr.reduce(image, num=image.shape[2]),numpy.repeat(~mask[:,:,numpy.newaxis],image.shape[2]))
     
     output_data[args.key+'-mnf-transform'] = mnfr                                           
     output_data[args.key+'-mnf'] = mnfimage
     
 if args.pca:
     import sklearn.decomposition
     
     pca = sklearn.decomposition.PCA(svd_solver='full', whiten=False)
示例#21
0
 def test_pca_runs_from_stats(self):
     '''Should be able to pass image stats to PCA function.'''
     data = self.data
     stats = spy.calc_stats(data)
     xdata = spy.principal_components(stats).transform(data)
示例#22
0
 def test_rx_bg_eq_zero(self):
     from spectral.algorithms.detectors import rx, RX
     d = rx(self.data)
     stats = spy.calc_stats(self.data)
     np.testing.assert_approx_equal(rx(stats.mean, background=stats), 0)
示例#23
0
 def setup(self):
     self.data = spy.open_image('92AV3C.lan').load()
     self.background = spy.calc_stats(self.data)
 def test_stats_property_sqrt_inv_cov(self):
     import spectral as spy
     from spectral.algorithms.spymath import matrix_sqrt
     stats = spy.calc_stats(self.data)
     s = stats.sqrt_inv_cov.dot(stats.sqrt_inv_cov)
     assert_allclose(s, stats.inv_cov, atol=1e-8)
示例#25
0
def ace(X, target, background=None, window=None, cov=None, **kwargs):
    r'''Returns Adaptive Coherence/Cosine Estimator (ACE) detection scores.

    Usage:

        y = ace(X, target, background)

        y = ace(X, target, window=<win> [, cov=<cov>])
        
    Arguments:

        `X` (numpy.ndarray):

            For the first calling method shown, `X` can be an ndarray with
            shape (R, C, B) or an ndarray of shape (R * C, B). If the
            `background` keyword is given, it will be used for the image
            background statistics; otherwise, background statistics will be
            computed from `X`.

            If the `window` keyword is given, `X` must be a 3-dimensional
            array and background statistics will be computed for each point
            in the image using a local window defined by the keyword.

        `target` (ndarray or sequence of ndarray):

            If `X` has shape (R, C, B), `target` can be any of the following:

                A length-B ndarray. In this case, `target` specifies a single
                target spectrum to be detected. The return value will be an
                ndarray with shape (R, C).

                An ndarray with shape (D, B). In this case, `target` contains
                `D` length-B targets that define a subspace for the detector.
                The return value will be an ndarray with shape (R, C).
    
                A length-D sequence (e.g., list or tuple) of length-B ndarrays.
                In this case, the detector will be applied seperately to each of
                the `D` targets. This is equivalent to calling the function
                sequentially for each target and stacking the results but is
                much faster. The return value will be an ndarray with shape
                (R, C, D).
    
        `background` (`GaussianStats`):

            The Gaussian statistics for the background (e.g., the result
            of calling :func:`calc_stats` for an image). This argument is not
            required if `window` is given.

        `window` (2-tuple of odd integers):

            Must have the form (`inner`, `outer`), where the two values
            specify the widths (in pixels) of inner and outer windows centered
            about the pixel being evaulated. Both values must be odd integers.
            The background mean and covariance will be estimated from pixels
            in the outer window, excluding pixels within the inner window. For
            example, if (`inner`, `outer`) = (5, 21), then the number of
            pixels used to estimate background statistics will be
            :math:`21^2 - 5^2 = 416`. If this argument is given, `background`
            is not required (and will be ignored, if given).

            The window is modified near image borders, where full, centered
            windows cannot be created. The outer window will be shifted, as
            needed, to ensure that the outer window still has height and width
            `outer` (in this situation, the pixel being evaluated will not be
            at the center of the outer window). The inner window will be
            clipped, as needed, near image borders. For example, assume an
            image with 145 rows and columns. If the window used is
            (5, 21), then for the image pixel at (0, 0) (upper left corner),
            the the inner window will cover `image[:3, :3]` and the outer
            window will cover `image[:21, :21]`. For the pixel at (50, 1), the
            inner window will cover `image[48:53, :4]` and the outer window
            will cover `image[40:51, :21]`.
            
        `cov` (ndarray):

            An optional covariance to use. If this parameter is given, `cov`
            will be used for all matched filter calculations (background
            covariance will not be recomputed in each window) and only the
            background mean will be recomputed in each window. If the
            `window` argument is specified, providing `cov` will allow the
            result to be computed *much* faster.

    Keyword Arguments:

        `vectorize` (bool, default True):

            Specifies whether the function should attempt to vectorize
            operations. This typicall results in faster computation but will
            consume more memory.

    Returns numpy.ndarray:

        The return value will be the ACE scores for each input pixel. The shape
        of the returned array will be either (R, C) or (R, C, D), depending on
        the value of the `target` argument.


    References:

    Kraut S. & Scharf L.L., "The CFAR Adaptive Subspace Detector is a Scale-
    Invariant GLRT," IEEE Trans. Signal Processing., vol. 47 no. 9, pp. 2538-41,
    Sep. 1999
    '''
    import spectral as spy
    if background is not None and window is not None:
        raise ValueError('`background` and `window` keywords are mutually ' \
                         'exclusive.')
    detector = ACE(target, background, **kwargs)
    if window is None:
        # Use common background statistics for all pixels
        if isinstance(target, np.ndarray):
            # Single detector score for target subspace for each pixel
            result = detector(X)
        else:
            # Separate score arrays for each target in target list
            if background is None:
                detector.set_background(spy.calc_stats(X))
            def apply_to_target(t):
                detector.set_target(t)
                return detector(X)
            result = np.array([apply_to_target(t) for t in target])
            if result.ndim == 3:
                result = result.transpose(1, 2, 0)
    else:
        # Compute local background statistics for each pixel
        from spectral.algorithms.spatial import map_outer_window_stats
        if isinstance(target, np.ndarray):
            # Single detector score for target subspace for each pixel
            def ace_wrapper(bg, x):
                detector.set_background(bg)
                return detector(x)
            result = map_outer_window_stats(ace_wrapper, X, window[0], window[1],
                                            dim_out=1, cov=cov)
        else:
            # Separate score arrays for each target in target list
            def apply_to_target(t, x):
                detector.set_target(t)
                return detector(x)
            def ace_wrapper(bg, x):
                detector.set_background(bg)
                return [apply_to_target(t, x) for t in target]
            result = map_outer_window_stats(ace_wrapper, X, window[0], window[1],
                                            dim_out=len(target), cov=cov)
            if result.ndim == 3:
                result = result.transpose(1, 2, 0)

    # Convert NaN values to zero
    result = np.nan_to_num(result)
    if isinstance(result, np.ndarray):
        return np.clip(result, 0, 1, out=result)
    else:
        return np.clip(result, 0, 1)
示例#26
0
def ace(X, target, background=None, window=None, cov=None, **kwargs):
    r'''Returns Adaptive Coherence/Cosine Estimator (ACE) detection scores.

    Usage:

        y = ace(X, target, background)

        y = ace(X, target, window=<win> [, cov=<cov>])
        
    Arguments:

        `X` (numpy.ndarray):

            For the first calling method shown, `X` can be an ndarray with
            shape (R, C, B) or an ndarray of shape (R * C, B). If the
            `background` keyword is given, it will be used for the image
            background statistics; otherwise, background statistics will be
            computed from `X`.

            If the `window` keyword is given, `X` must be a 3-dimensional
            array and background statistics will be computed for each point
            in the image using a local window defined by the keyword.

        `target` (ndarray or sequence of ndarray):

            If `X` has shape (R, C, B), `target` can be any of the following:

                A length-B ndarray. In this case, `target` specifies a single
                target spectrum to be detected. The return value will be an
                ndarray with shape (R, C).

                An ndarray with shape (D, B). In this case, `target` contains
                `D` length-B targets that define a subspace for the detector.
                The return value will be an ndarray with shape (R, C).
    
                A length-D sequence (e.g., list or tuple) of length-B ndarrays.
                In this case, the detector will be applied seperately to each of
                the `D` targets. This is equivalent to calling the function
                sequentially for each target and stacking the results but is
                much faster. The return value will be an ndarray with shape
                (R, C, D).
    
        `background` (`GaussianStats`):

            The Gaussian statistics for the background (e.g., the result
            of calling :func:`calc_stats` for an image). This argument is not
            required if `window` is given.

        `window` (2-tuple of odd integers):

            Must have the form (`inner`, `outer`), where the two values
            specify the widths (in pixels) of inner and outer windows centered
            about the pixel being evaulated. Both values must be odd integers.
            The background mean and covariance will be estimated from pixels
            in the outer window, excluding pixels within the inner window. For
            example, if (`inner`, `outer`) = (5, 21), then the number of
            pixels used to estimate background statistics will be
            :math:`21^2 - 5^2 = 416`. If this argument is given, `background`
            is not required (and will be ignored, if given).

            The window is modified near image borders, where full, centered
            windows cannot be created. The outer window will be shifted, as
            needed, to ensure that the outer window still has height and width
            `outer` (in this situation, the pixel being evaluated will not be
            at the center of the outer window). The inner window will be
            clipped, as needed, near image borders. For example, assume an
            image with 145 rows and columns. If the window used is
            (5, 21), then for the image pixel at (0, 0) (upper left corner),
            the the inner window will cover `image[:3, :3]` and the outer
            window will cover `image[:21, :21]`. For the pixel at (50, 1), the
            inner window will cover `image[48:53, :4]` and the outer window
            will cover `image[40:51, :21]`.
            
        `cov` (ndarray):

            An optional covariance to use. If this parameter is given, `cov`
            will be used for all matched filter calculations (background
            covariance will not be recomputed in each window). Only the
            background mean will be recomputed in each window). If the
            `window` argument is specified, providing `cov` will allow the
            result to be computed *much* faster.

    Keyword Arguments:

        `vectorize` (bool, default True):

            Specifies whether the function should attempt to vectorize
            operations. This typicall results in faster computation but will
            consume more memory.

    Returns numpy.ndarray:

        The return value will be the ACE scores for each input pixel. The shape
        of the returned array will be either (R, C) or (R, C, D), depending on
        the value of the `target` argument.


    References:

    Kraut S. & Scharf L.L., "The CFAR Adaptive Subspace Detector is a Scale-
    Invariant GLRT," IEEE Trans. Signal Processing., vol. 47 no. 9, pp. 2538-41,
    Sep. 1999
    '''
    import spectral as spy
    if background is not None and window is not None:
        raise ValueError('`background` and `window` keywords are mutually ' \
                         'exclusive.')
    detector = ACE(target, background, **kwargs)
    if window is None:
        # Use common background statistics for all pixels
        if isinstance(target, np.ndarray):
            # Single detector score for target subspace for each pixel
            result = detector(X)
        else:
            # Separate score arrays for each target in target list
            if background is None:
                detector.set_background(spy.calc_stats(X))

            def apply_to_target(t):
                detector.set_target(t)
                return detector(X)

            result = np.array([apply_to_target(t) for t in target])
            if result.ndim == 3:
                result = result.transpose(1, 2, 0)
    else:
        # Compute local background statistics for each pixel
        from spectral.algorithms.spatial import map_outer_window_stats
        if isinstance(target, np.ndarray):
            # Single detector score for target subspace for each pixel
            def ace_wrapper(bg, x):
                detector.set_background(bg)
                return detector(x)

            result = map_outer_window_stats(ace_wrapper,
                                            X,
                                            window[0],
                                            window[1],
                                            dim_out=1,
                                            cov=cov)
        else:
            # Separate score arrays for each target in target list
            def apply_to_target(t, x):
                detector.set_target(t)
                return detector(x)

            def ace_wrapper(bg, x):
                detector.set_background(bg)
                return [apply_to_target(t, x) for t in target]

            result = map_outer_window_stats(ace_wrapper,
                                            X,
                                            window[0],
                                            window[1],
                                            dim_out=len(target),
                                            cov=cov)
            if result.ndim == 3:
                result = result.transpose(1, 2, 0)

    # Convert NaN values to zero
    result = np.nan_to_num(result)
    if isinstance(result, np.ndarray):
        return np.clip(result, 0, 1, out=result)
    else:
        return np.clip(result, 0, 1)