Ejemplo n.º 1
0
def compute_cov_eq_linear_transform_matrix(
    input_scene,  # type: ndarray
    scene_to_match,  # type: ndarray
    input_scene_mask=None,  # type: ndarray
    scene_to_match_mask=None  # type: ndarray
):  # type: (...) -> ndarray
    flattened_1 = spectral_tools.flatten_image_cube(input_scene)
    flattened_2 = spectral_tools.flatten_image_cube(scene_to_match)
    flattened_mask_1 = image_utils.flatten_image_band(input_scene_mask)
    flattened_mask_2 = image_utils.flatten_image_band(scene_to_match_mask)
    big_l = spectral_image_processing_1d.compute_cov_eq_linear_transform_matrix(
        flattened_1, flattened_2, flattened_mask_1, flattened_mask_2)
    return big_l
Ejemplo n.º 2
0
    def test_masked_mean_1d(self):
        print("")
        print("MASKED MEAN TEST 1D")
        image_cube = image_utils.create_uniform_image_data(nx,
                                                           ny,
                                                           nbands,
                                                           values=0,
                                                           dtype=np.float32)
        image_cube[y_loc, x_loc, :] = np.arange(0, nbands)
        flattened_image = spectral_utils.flatten_image_cube(image_cube)

        image_mask = np.zeros((ny, nx))
        image_mask[y_loc, x_loc] = 1
        flattened_mask = image_utils.flatten_image_band(image_mask)

        raw_mean = sp1d.compute_image_cube_spectral_mean(flattened_image)
        masked_mean = sp1d.compute_image_cube_spectral_mean(
            flattened_image, flattened_mask)

        assert len(masked_mean) == nbands
        assert len(raw_mean) == nbands
        logging.debug("length of means equals number of spectral bands")

        assert masked_mean.max() == 0
        assert raw_mean.max() != 0
        logging.debug(
            "1d image cube raw and masked means have different results")
        logging.debug("masked_mean_1d test passed")
        print("MASKED MEAN TEST 1D - TEST PASSED")
Ejemplo n.º 3
0
    def test_sam_ace_compare(self):
        print("")
        print("SAM / ACE EQUALITY TEST WHEN INVERSE COVARIANCE IS IDENTITY")
        random_cube = np.random.random((ny, nx, nbands))
        signal_x_axis = np.arange(0, nbands) / nbands * 2 * np.pi
        signal_to_embed = np.sin(signal_x_axis) * 100
        background_to_embed = np.cos(signal_x_axis) * 100
        image_cube = random_cube + background_to_embed
        image_cube[y_loc,
                   x_loc, :] = image_cube[y_loc, x_loc, :] + signal_to_embed
        flattened_image = spectral_utils.flatten_image_cube(image_cube)

        ace_inv_cov = np.eye(nbands)
        ace_mean = np.zeros(nbands)

        ace_detection_result = sp1d.ace(flattened_image, signal_to_embed,
                                        ace_mean, ace_inv_cov)
        sam_detection_result = sp1d.sam(flattened_image, signal_to_embed)

        # TODO: use higher precision if we need better accuracy
        np.testing.assert_allclose(ace_detection_result,
                                   np.square(sam_detection_result),
                                   rtol=1.e-6)
        logging.debug("SAM squared is equivalent to ACE if the inverse "
                      "covariance is the identify matrix")
        print("ACE / SAM comparison test passed.")
Ejemplo n.º 4
0
    def test_sam_1d(self):
        print("")
        print("SAM TEST 1D")
        random_cube = np.random.random((ny, nx, nbands))
        signal_x_axis = np.arange(0, nbands) / nbands * 2 * np.pi
        signal_to_embed = np.sin(signal_x_axis) * 100
        background_to_embed = np.cos(signal_x_axis) * 100
        image_cube = random_cube + background_to_embed
        image_cube[y_loc,
                   x_loc, :] = image_cube[y_loc, x_loc, :] + signal_to_embed
        flattened_image = spectral_utils.flatten_image_cube(image_cube)
        detection_result = sp1d.sam(flattened_image, signal_to_embed)

        detection_result_2d = image_utils.unflatten_image_band(
            detection_result, nx, ny)
        detection_max_locs_y_x = np.where(
            detection_result_2d == detection_result_2d.max())
        detection_max_y = detection_max_locs_y_x[0][0]
        detection_max_x = detection_max_locs_y_x[1][0]

        assert detection_max_y == y_loc
        assert detection_max_x == x_loc
        logging.debug("location of highest detection return matches x/y "
                      "location of embedded signal")
        print("1-D SAM TEST PASSED")
Ejemplo n.º 5
0
    def test_masked_covar_1d(self):
        print("")
        print("MASKED COVARIANCE TEST 1D")
        image_cube = image_utils.create_uniform_image_data(nx,
                                                           ny,
                                                           nbands,
                                                           values=0,
                                                           dtype=np.float32)
        image_cube[y_loc, x_loc, :] = np.arange(0, nbands)
        flattened_image = spectral_utils.flatten_image_cube(image_cube)

        image_mask = np.zeros((ny, nx))
        image_mask[y_loc, x_loc] = 1
        flattened_mask = image_utils.flatten_image_band(image_mask)

        raw_covar = sp1d.compute_image_cube_spectral_covariance(
            flattened_image)
        masked_covar = sp1d.compute_image_cube_spectral_covariance(
            flattened_image, flattened_mask)

        assert masked_covar.shape == (nbands, nbands)
        assert raw_covar.shape == (nbands, nbands)
        logging.debug("covariance shape is (nbands x nbands)")

        assert masked_covar.max() == 0
        assert raw_covar.max() != 0
        logging.debug(
            "1d image cube raw and masked covariances have different results")
        print("MASKED_COVAR_1D TEST PASSED")
Ejemplo n.º 6
0
def compute_image_cube_spectral_covariance(
    spectral_image,  # type: ndarray
    image_mask=None  # type: ndarray
):  # type: (...) -> ndarray
    spectral_image = spectral_tools.flatten_image_cube(spectral_image)
    if image_mask is not None:
        image_mask = image_utils.flatten_image_band(image_mask)
    masked_covariance = spectral_image_processing_1d.compute_image_cube_spectral_covariance(
        spectral_image, image_mask)
    return masked_covariance
Ejemplo n.º 7
0
def sam(
        spectral_image,  # type: ndarray
        target_spectra  # type: ndarray
):  # type: (...) -> ndarray
    nx, ny, nbands = spectral_tools.get_2d_cube_nx_ny_nbands(spectral_image)
    spectral_image = spectral_tools.flatten_image_cube(spectral_image)
    sam_result = spectral_image_processing_1d.sam(spectral_image,
                                                  target_spectra)
    sam_result = image_utils.unflatten_image_band(sam_result, nx, ny)
    return sam_result
Ejemplo n.º 8
0
def rx_anomaly_detector(
    spectral_image,  # type: ndarray
    spectral_mean=None,  # type: ndarray
    inverse_covariance=None  # type: ndarray
):  # type: (...) -> ndarray
    nx, ny, nbands = spectral_tools.get_2d_cube_nx_ny_nbands(spectral_image)
    spectral_image = spectral_tools.flatten_image_cube(spectral_image)
    rx_result = spectral_image_processing_1d.rx_anomaly_detector(
        spectral_image, spectral_mean, inverse_covariance)
    rx_result = image_utils.unflatten_image_band(rx_result, nx, ny)
    return rx_result
Ejemplo n.º 9
0
def covariance_equalization_mean_centered(
    input_scene_demeaned,  # type: ndarray
    scene_to_match_demeaned,  # type: ndarray
    input_scene_mask=None,  # type: ndarray
    scene_to_match_mask=None  # type: ndarray
):  # type: (...) -> ndarray
    nx, ny, nbands = spectral_tools.get_2d_cube_nx_ny_nbands(
        input_scene_demeaned)
    flattened_1 = spectral_tools.flatten_image_cube(input_scene_demeaned)
    flattened_2 = spectral_tools.flatten_image_cube(scene_to_match_demeaned)
    if input_scene_mask is not None:
        input_scene_mask = image_utils.flatten_image_band(input_scene_mask)
    if scene_to_match_mask is not None:
        scene_to_match_mask = image_utils.flatten_image_band(
            scene_to_match_mask)
    scene_1_cov_equalized = spectral_image_processing_1d.covariance_equalization_mean_centered(
        flattened_1, flattened_2, input_scene_mask, scene_to_match_mask)
    scene_1_cov_equalized = spectral_tools.unflatten_image_cube(
        scene_1_cov_equalized, nx, ny)
    return scene_1_cov_equalized
Ejemplo n.º 10
0
def ace_demeaned(
        demeaned_image,  # type: ndarray
        demeaned_target_spectrum,  # type: ndarray
        inverse_covariance,  # type: ndarray
):  # type: (...) -> ndarray
    nx, ny, nbands = spectral_tools.get_2d_cube_nx_ny_nbands(demeaned_image)
    spectral_image = spectral_tools.flatten_image_cube(demeaned_image)
    ace_result = spectral_image_processing_1d.ace_demeaned(
        spectral_image, demeaned_target_spectrum, inverse_covariance)
    ace_result = image_utils.unflatten_image_band(ace_result, nx, ny)
    return ace_result
Ejemplo n.º 11
0
def demean_image_data(
    spectral_image,  # type: ndarray
    image_mask=None  # type: ndarray
):  # type: (...) -> ndarray
    nx, ny, nbands = spectral_tools.get_2d_cube_nx_ny_nbands(spectral_image)
    spectral_image = spectral_tools.flatten_image_cube(spectral_image)
    if image_mask is not None:
        image_mask = image_utils.flatten_image_band(image_mask)
    demeaned_image_data = spectral_image_processing_1d.demean_image_data(
        spectral_image, image_mask)
    demeaned_image_data = spectral_tools.unflatten_image_cube(
        demeaned_image_data, nx, ny)
    return demeaned_image_data
Ejemplo n.º 12
0
    def test_flatten_image_cube(self):
        print("")
        print("FLATTEN IMAGE CUBE TEST")
        image_cube = image_utils.create_uniform_image_data(nx,
                                                           ny,
                                                           nbands,
                                                           values=0,
                                                           dtype=np.float32)
        image_cube[y_loc, x_loc, :] = np.arange(0, nbands)
        flattened_image = spectral_utils.flatten_image_cube(image_cube)
        unflattend_image_cube = spectral_utils.unflatten_image_cube(
            flattened_image, nx, ny)

        assert (unflattend_image_cube == image_cube).all()
        print("flattening and unflattening image cube matches with original")
Ejemplo n.º 13
0
 def test_covariance_equalization(self):
     print("")
     print("COVARIANCE EQUALIZATION TEST")
     scene_1 = np.random.random((ny, nx, nbands))
     scene_1 = spectral_utils.flatten_image_cube(scene_1)
     scene_1 = scene_1 + (
         np.sin(np.arange(0, nbands) / nbands * (2 * np.pi)) / 4 + 0.75)
     scene_2 = scene_1 * 2.0
     scene_1_equalized = sp1d.covariance_equalization_mean_centered(
         scene_1, scene_2)
     scene_2_cov = sp1d.compute_image_cube_spectral_covariance(scene_2)
     scene_1_equalized_cov = sp1d.compute_image_cube_spectral_covariance(
         scene_1_equalized)
     np.testing.assert_almost_equal(scene_2_cov, scene_1_equalized_cov)
     logging.debug(
         "equalized covariance from scene_1 matches covariance of scene_2")
     print("COVARIANCE EQUALIZATION TEST PASSED")
Ejemplo n.º 14
0
def ace(
        spectral_image,  # type: ndarray
        target_spectra,  # type: ndarray
        spectral_mean=None,  # type: ndarray
        inverse_covariance=None,  # type: ndarray
        image_mask=None,  # type: ndarray
):  # type: (...) -> ndarray
    nx, ny, nbands = spectral_tools.get_2d_cube_nx_ny_nbands(spectral_image)
    spectral_image = spectral_tools.flatten_image_cube(spectral_image)
    if image_mask is not None:
        image_mask = image_utils.flatten_image_band(image_mask)
    ace_result = spectral_image_processing_1d.ace(spectral_image,
                                                  target_spectra,
                                                  spectral_mean,
                                                  inverse_covariance,
                                                  image_mask)
    ace_result = image_utils.unflatten_image_band(ace_result, nx, ny)
    return ace_result
Ejemplo n.º 15
0
    def test_rx_1d(self):
        print("")
        print("RX ANAMOLY TEST 1D")
        image_cube = np.random.random((ny, nx, nbands))
        signal_x_axis = np.arange(0, nbands) / nbands * 2 * np.pi
        signal_to_embed = np.sin(signal_x_axis) * 100
        image_cube[y_loc, x_loc, :] = signal_to_embed
        flattened_image = spectral_utils.flatten_image_cube(image_cube)
        image_mask = np.zeros((ny, nx))
        image_mask[y_loc, x_loc] = 1
        flattened_mask = image_utils.flatten_image_band(image_mask)

        masked_mean = sp1d.compute_image_cube_spectral_mean(
            flattened_image, flattened_mask)
        masked_covar = sp1d.compute_image_cube_spectral_covariance(
            flattened_image, flattened_mask)
        masked_inv_cov = np.linalg.inv(masked_covar)

        detection_result = sp1d.rx_anomaly_detector(flattened_image,
                                                    masked_mean,
                                                    masked_inv_cov)
        detection_result_2d = image_utils.unflatten_image_band(
            detection_result, nx, ny)
        detection_max_locs_y_x = np.where(
            detection_result_2d == detection_result_2d.max())
        detection_max_y = detection_max_locs_y_x[0][0]
        detection_max_x = detection_max_locs_y_x[1][0]

        assert detection_max_y == y_loc
        assert detection_max_x == x_loc
        assert len(detection_result.shape) == 1
        assert detection_result.shape[0] == nx * ny

        logging.debug("location of highest detection return matches x/y "
                      "location of embedded signal")
        print("1d rx passed ")
        print("")
Ejemplo n.º 16
0
    def test_sam_ace_sanity_check(self):
        print("")
        print("SAM / ACE COMPARISON AND SANITY CHECK TEST")
        random_cube = np.random.random((ny, nx, nbands))
        signal_x_axis = np.arange(0, nbands) / nbands * 2 * np.pi
        signal_to_embed = np.sin(signal_x_axis) * 100
        background_to_embed = np.cos(signal_x_axis) * 100
        image_cube = random_cube + background_to_embed
        image_cube[y_loc,
                   x_loc, :] = image_cube[y_loc, x_loc, :] + signal_to_embed
        flattened_image = spectral_utils.flatten_image_cube(image_cube)

        demeaned_target_sig_array = signal_to_embed

        inverse_covariance = np.eye(nbands)
        signal_x_axis = np.arange(0, nbands) / nbands * 2 * np.pi
        signal_to_embed = np.sin(signal_x_axis) * 100
        target_spectra = signal_to_embed

        # checking ace / sam numerator right hand side

        # Python 3.5+
        # ace_numerator = inverse_covariance @ flattened_image.transpose()
        # Python 2.7 - 3.4
        ace_numerator = np.dot(inverse_covariance, flattened_image.transpose())

        ace_numerator = np.multiply(target_spectra, ace_numerator.transpose())
        ace_numerator = np.square(np.sum(ace_numerator, axis=1))

        # Python 3.5+
        # ace_den_left = (
        #    inverse_covariance @ demeaned_target_sig_array.transpose())
        # Python 2.7 - 3.4
        ace_den_left = np.dot(inverse_covariance,
                              demeaned_target_sig_array.transpose())

        ace_den_left = np.multiply(demeaned_target_sig_array,
                                   ace_den_left.transpose())
        ace_den_left = np.sum(ace_den_left)

        # Python 3.5+
        # ace_den_right = inverse_covariance @ flattened_image.transpose()
        # Python 2.7 - 3.4
        ace_den_right = np.dot(inverse_covariance, flattened_image.transpose())

        ace_den_right = np.multiply(flattened_image, ace_den_right.transpose())
        ace_den_right = np.sum(ace_den_right, axis=1)

        # Python 3.5+
        # sam_numerator = target_spectra @ flattened_image.transpose()
        # Python 2.7 - 3.4
        sam_numerator = np.dot(target_spectra, flattened_image.transpose())

        # Python 3.5+
        # sam_den_left = np.sqrt(target_spectra @ target_spectra)
        # Python 2.7 - 3.4
        sam_den_left = np.sqrt(np.dot(target_spectra, target_spectra))

        sam_den_right = np.sqrt(
            np.sum(np.multiply(flattened_image, flattened_image), axis=1))

        np.testing.assert_allclose(ace_numerator,
                                   np.square(sam_numerator),
                                   rtol=1.e-6)
        np.testing.assert_allclose(ace_den_left,
                                   np.square(sam_den_left),
                                   rtol=1.e-6)
        np.testing.assert_allclose(ace_den_right,
                                   np.square(sam_den_right),
                                   rtol=1.e-6)

        logging.debug(
            "sam and ace portions of numerators and denominators are "
            "close when the covariance is an identify matrix.")
        print("SAM / ACE SANITY CHECK TEST PASSED")