예제 #1
0
    def ps_topofit_fun(phase: np.ndarray, bperp_meaned: np.ndarray, nr_trial_wraps: float):
        # To make sure that the results are correct we transform bperp_meaned into column matrix
        if (len(bperp_meaned.shape) == 1):
            bperp_meaned = ArrayUtils.to_col_matrix(bperp_meaned)

        # The result of get_nr_trial_wraps is not correct in this case, so we need to find it again
        bperp_range = np.amax(bperp_meaned) - np.amin(bperp_meaned)

        CONST = 8 * nr_trial_wraps  # todo what const? Why 8
        trial_multi_start = -np.ceil(CONST)
        trial_multi_end = np.ceil(CONST)
        trial_multi = ArrayUtils.arange_include_last(trial_multi_start, trial_multi_end, 1)

        trial_phase = bperp_meaned / bperp_range * math.pi / 4
        trial_phase = np.exp(np.outer(-1j * trial_phase, trial_multi))

        # In order to successfully multiply, we need to transform 'phase' array to column matrix
        phase = ArrayUtils.to_col_matrix(phase)
        phase_tile = np.tile(phase, (1, len(trial_multi)))
        phaser = np.multiply(trial_phase, phase_tile)

        phaser_sum = MatlabUtils.sum(phaser)

        phase_abs_sum = MatlabUtils.sum(np.abs(phase))
        trial_coherence = np.abs(phaser_sum) / phase_abs_sum
        trial_coherence_max_ind = np.where(trial_coherence == MatlabUtils.max(trial_coherence))

        k_0 = (math.pi / 4 / bperp_range) * trial_multi[trial_coherence_max_ind][0]

        re_phase = np.multiply(phase, np.exp(-1j * (k_0 * bperp_meaned)))
        phase_offset = MatlabUtils.sum(re_phase)
        re_phase = np.angle(re_phase * phase_offset.conjugate())
        weigth = np.abs(phase)
        bperp_meaned_weighted = weigth * bperp_meaned
        re_phase_weighted = weigth * re_phase
        # Numpy linalg functions work only with 2d array
        if len(bperp_meaned_weighted.shape) > 2:
            bperp_meaned_weighted = bperp_meaned_weighted[0]
        if len(re_phase_weighted.shape) > 2:
            re_phase_weighted = re_phase_weighted[0]

        mopt = np.linalg.lstsq(bperp_meaned_weighted, re_phase_weighted)[0][0]
        # In StaMPS k0
        k_0 = k_0 + mopt

        phase_residual = np.multiply(phase, np.exp(-1j * (k_0 * bperp_meaned)))
        phase_residual_sum = MatlabUtils.sum(phase_residual)
        # In StaMPS c0
        static_offset = np.angle(phase_residual_sum)
        # In StaMPS coh0
        coherence_0 = np.abs(phase_residual_sum) / MatlabUtils.sum(np.abs(phase_residual))

        return phase_residual, coherence_0, static_offset, k_0
예제 #2
0
    def test_load_files(self):
        self.__start_process()

        ps1_mat = scipy.io.loadmat(os.path.join(self._PATCH_1_FOLDER, 'ps1.mat'))
        # These are saved using Matlab 7.3
        ph1 = self.__load_mat73(os.path.join(self._PATCH_1_FOLDER, 'ph1.mat'), 'ph')
        bp1 = self.__load_mat73(os.path.join(self._PATCH_1_FOLDER, 'bp1.mat'), 'bperp_mat')
        da1 = self.__load_mat73(os.path.join(self._PATCH_1_FOLDER, 'da1.mat'), 'D_A')
        la1 = self.__load_mat73(os.path.join(self._PATCH_1_FOLDER, 'la1.mat'), 'la')
        hgt1 = self.__load_mat73(os.path.join(self._PATCH_1_FOLDER, 'hgt1.mat'), 'hgt')

        self.assertEqual(len(self._ps_files.bperp), len(bp1))
        np.testing.assert_allclose(self._ps_files.bperp, bp1)

        self.assertEqual(len(self._ps_files.da), len(da1))
        # Loadmat results are array of arrays
        np.testing.assert_allclose(np.reshape(self._ps_files.da, (len(self._ps_files.da), 1)), da1)

        ps1_mat_bperp = np.reshape(ps1_mat['bperp'], len(ps1_mat['bperp']))
        np.testing.assert_allclose(self._ps_files.bperp_meaned, ps1_mat_bperp)
        np.testing.assert_allclose(self._ps_files.pscands_ij.view(np.ndarray), ps1_mat['ij'])
        # In our process there isn't first column. That's why we take last two
        np.testing.assert_allclose(self._ps_files.xy, ps1_mat['xy'][:, 1:])

        self.assertAlmostEqual(self._ps_files.mean_range, ps1_mat['mean_range'])

        np.testing.assert_allclose(self._ps_files.mean_incidence.view(np.ndarray),
                                   ps1_mat['mean_incidence'])

        np.testing.assert_allclose(self._ps_files.ll, ps1_mat['ll0'])

        np.testing.assert_allclose(self._ps_files.lonlat, ps1_mat['lonlat'])

        # Because those values aren't added to .mat files so we can only check if these are filled
        self.assertNotEqual(self._ps_files.wavelength, 0)
        self.assertIsNotNone(self._ps_files.heading)

        master_date_days = date.toordinal(self._ps_files.master_date) + 366
        self.assertEqual(master_date_days, ps1_mat['master_day'][0])
        self.assertEqual(self._ps_files.master_nr, ps1_mat['master_ix'])

        ifg_date_days = ArrayUtils.to_col_matrix(
            np.array([date.toordinal(x) + 366 for x in self._ps_files.ifg_dates]))
        np.testing.assert_array_equal(ifg_date_days, ps1_mat['day'])

        np.testing.assert_allclose(self._ps_files.sort_ind.view(np.ndarray), la1)

        self.assertEqual(len(self._ps_files.ifgs), ps1_mat['n_ifg'])

        self.assertEqual(len(self._ps_files.pscands_ij), ps1_mat['n_ps'])

        # hgt1 is array of arrays, need to reshape
        np.testing.assert_allclose(self._ps_files.hgt, np.reshape(hgt1, len(hgt1)))

        self.assertEqual(len(self._ps_files.ph), len(ph1))
        self.assert_ph(self._ps_files.ph, ph1)
예제 #3
0
        def get_ph_bit_ind_array(ps_bit_col: int, ph_bit_len):
            slc_osf = self.__slc_osf - 1
            ind_array = ArrayUtils.arange_include_last(
                start=ps_bit_col - slc_osf,
                end=ps_bit_col + slc_osf).astype(np.int32)
            ind_array = ind_array[(ind_array > 0) & (0 <= ph_bit_len)]

            # Python can't take anything from empty/ no values list
            if len(ind_array) == 0:
                ind_array = np.zeros(1).astype(np.int16)

            return ind_array
예제 #4
0
    def __get_low_pass(self):
        start = -(
            self.__clap_win) / self.__filter_grid_size / self.__clap_win / 2
        stop = (self.__clap_win -
                2) / self.__filter_grid_size / self.__clap_win / 2
        step = 1 / self.__filter_grid_size / self.__clap_win
        freg_i = ArrayUtils.arange_include_last(start, stop, step)

        freg_0 = 1 / self.__clap_low_pass_wavelength

        subtract = 1 + np.power(freg_i / freg_0, 10)
        butter_i = np.divide(1, subtract)

        return np.fft.fftshift(
            np.asmatrix(butter_i).conj().transpose() * butter_i)
예제 #5
0
    def test_start_process_with_matlab_data(self):
        def get_keep_ix():
            """Used to get keep_ix array to use in Numpy arrys.
            This conversion is needed because in Numpy there is boolean indexing, but on Matlab
            there are 1/ 0. For that we make array of boolean using np.where."""
            keep_ix = select1_mat['keep_ix']
            keep_ix = np.reshape(keep_ix, len(select1_mat['keep_ix']))
            keep_ix = np.where(keep_ix == 1)

            return keep_ix[0]

        self.__fill_est_gamma_with_matlab_data()

        self.__start_process()

        select1_mat = scipy.io.loadmat(
            os.path.join(self._PATCH_1_FOLDER, 'select1.mat'))

        # Matlab indexes begin from zero and Numpy arrays from one.
        # So we need to add one to Numpy's array
        np.testing.assert_array_almost_equal(
            np.add(self._ps_select.ifg_ind, 1), select1_mat['ifg_index'][0])
        # Reshape because Matlab's array is col-array
        np.testing.assert_array_almost_equal(
            np.add(self._ps_select.coh_thresh_ind, 1),
            select1_mat['ix'].reshape(len(select1_mat['ix'])))
        np.testing.assert_array_almost_equal(self._ps_select.ph_patch,
                                             select1_mat['ph_patch2'],
                                             decimal=self._PLACES)
        np.testing.assert_array_almost_equal(self._ps_select.k_ps,
                                             select1_mat['K_ps2'])
        np.testing.assert_array_almost_equal(self._ps_select.ph_res,
                                             select1_mat['ph_res2'])
        np.testing.assert_array_almost_equal(self._ps_select.coh_ps2,
                                             select1_mat['coh_ps2'])
        coh_thresh = select1_mat['coh_thresh']
        np.testing.assert_array_almost_equal(
            ArrayUtils.to_col_matrix(self._ps_select.coh_thresh), coh_thresh)
        keep_ix = get_keep_ix()
        np.testing.assert_array_almost_equal(self._ps_select.keep_ind,
                                             keep_ix,
                                             decimal=self._PLACES)
예제 #6
0
    def __init__(
        self,
        ps_files: PsFiles,
        rand_dist_cached_file=False,
        outter_rand_dist=np.array([])) -> None:
        """rand_dist_cached_file= when True loads array of random numbers 'tmp_rand_dist' from cache
        (see function 'self.__make_random_dist')
        outter_rand_dist = array of random numbers that are usually if found in function
        'self.__make_random_dist'. This is used for testing"""

        self.__logger = LoggerFactory.create("PsEstGamma")

        self.__ps_files = ps_files
        self.__set_internal_params()
        self.rand_dist_cached = rand_dist_cached_file
        self.outter_rand_dist = outter_rand_dist

        # In StaMPS this is called 'coh_bins'
        self.coherence_bins = ArrayUtils.arange_include_last(
            0.005, 0.995, 0.01)
예제 #7
0
    def __get_min_coh_and_da_mean(
            self, coh_ps: np.ndarray, max_rand: float,
            data: __DataDTO) -> (np.ndarray, np.ndarray, bool):

        # Internal parameters because full names are bad to write and read all the time
        coherence_bins = self.__ps_est_gamma.coherence_bins
        rand_dist = self.__ps_est_gamma.rand_dist

        array_size = data.da_max.size - 1

        min_coh = np.zeros(array_size)
        # In StaMPS this is size(da_max, 1) what is same as length(da_max)
        da_mean = np.zeros(array_size)
        for i in range(array_size):
            # You can use np.all or np.logical here too. Bitwize isn't must
            coh_chunk = coh_ps[(data.da > data.da_max[i])
                               & (data.da <= data.da_max[i + 1])]

            da_mean[i] = np.mean(data.da[(data.da > data.da_max[i])
                                         & (data.da <= data.da_max[i + 1])])
            # Remove pixels that we could not find coherence
            coh_chunk = coh_chunk[coh_chunk != 0]
            # In StaMPS this is called 'Na'
            hist, _ = MatlabUtils.hist(coh_chunk, coherence_bins)

            hist_low_coh_sum = MatlabUtils.sum(hist[:self.__low_coh_tresh])
            rand_dist_low_coh_sum = MatlabUtils.sum(
                rand_dist[:self.__low_coh_tresh])
            nr = rand_dist * hist_low_coh_sum / rand_dist_low_coh_sum  # todo What does this 'nr' mean?

            # In StaMPS here is also possibility to make graph

            hist[hist == 0] = 1

            # Percent_rand calculate
            # np.flip allows to use one-dimencional arrays, thats why we don't use np.fliplr
            nr_cumsum = np.cumsum(np.flip(nr, axis=0), axis=0)
            if self.__select_method is self._SelectMethod.PERCENT:
                hist_cumsum = np.cumsum(np.flip(hist, axis=0), axis=0) * 100
                percent_rand = np.flip(np.divide(nr_cumsum, hist_cumsum),
                                       axis=0)
            else:
                percent_rand = np.flip(nr_cumsum, axis=0)

            ok_ind = np.where(percent_rand < max_rand)[0]

            if len(ok_ind) == 0:
                # When coherence is over limit
                min_coh[i] = 1
            else:
                # Here we don't need to add one to indexes because on 'ok_ind' array it is already
                # done. This means that all those 'magical constants' are that where in StaMPS

                min_fit_ind = MatlabUtils.min(ok_ind) - 3  # todo Why 3?

                if min_fit_ind <= 0:
                    min_coh[i] = np.nan
                else:
                    max_fit_ind = MatlabUtils.min(ok_ind) + 2  # todo Why 2?

                    # In StaMPS this is just constant 100. Not length of array.
                    if max_fit_ind > len(percent_rand) - 1:
                        max_fit_ind = len(percent_rand) - 1

                    x_cordinates = percent_rand[min_fit_ind:max_fit_ind + 1]

                    y_cordinates = ArrayUtils.arange_include_last(
                        (min_fit_ind + 1) * 0.01, (max_fit_ind + 1) * 0.01,
                        0.01)
                    min_coh[i] = MatlabUtils.polyfit_polyval(
                        x_cordinates, y_cordinates, 3, max_rand)

        # Check if min_coh is unusable (full of nan's
        # This is bit different on StaMPS. I find min_coh'i ja da_mean in same method and in
        # same time
        not_nan_ind = np.where(min_coh != np.nan)[0]
        is_min_coh_nan_array = sum(not_nan_ind) == 0
        # When there isn't differences then we don't need to take subsets of arrays
        if not is_min_coh_nan_array or (not_nan_ind == array_size):
            min_coh = min_coh[not_nan_ind]
            da_mean = da_mean[not_nan_ind]

        return min_coh, da_mean, is_min_coh_nan_array
예제 #8
0
        def create_grid(nr_win: int):
            grid_array = ArrayUtils.arange_include_last(0, (nr_win / 2) - 1)
            grid_x, grid_y = np.meshgrid(grid_array, grid_array)
            grid = grid_x + grid_y

            return grid
예제 #9
0
 def __get_weights(self, da: np.ndarray):
     return ArrayUtils.to_col_matrix(np.divide(1, da))
예제 #10
0
    def __drop_noisy(self, data: __DataDTO, selectable_ps: np.ndarray,
                     ifg_ind: np.ndarray,
                     edges: np.ndarray) -> (np.ndarray, np.ndarray):
        def get_ph_weed(bperp: np.ndarray, k_ps: np.ndarray, ph: np.ndarray,
                        c_ps: np.ndarray, master_nr: int):
            exped = np.exp(-1j * (k_ps * bperp.conj().transpose()))
            ph_weed = np.multiply(ph, exped)
            ph_weed = np.divide(ph_weed, np.abs(ph_weed))
            # Adding master noise. It is done when small_baseline_flag != 'y'. Reshape is needed
            # because 'c_ps' is array of array's
            ph_weed[:,
                    (master_nr - 1)] = np.exp(1j * c_ps).reshape(len(ph_weed))

            return ph_weed

        def get_time_deltas_in_days(index: int) -> np.ndarray:
            """For getting days in ints from date object"""
            return np.array([(ifg_dates[index] - ifg_dates[x]).days
                             for x in np.nditer(ifg_ind)])

        def get_dph_mean(dph_space, edges_len, weight_factor):
            repmat = np.matlib.repmat(weight_factor, edges_len, 1)
            dph_mean = np.sum(np.multiply(dph_space, repmat), axis=1)

            return dph_mean

        ph_filtered = data.ph[selectable_ps]
        k_ps_filtered = data.k_ps[selectable_ps]
        c_ps_filtered = data.c_ps[selectable_ps]
        bperp_meaned = data.bperp_meaned
        master_nr = data.master_nr
        ifg_dates = data.ifg_dates

        ph_weed = get_ph_weed(bperp_meaned, k_ps_filtered, ph_filtered,
                              c_ps_filtered, master_nr)

        dph_space = np.multiply(ph_weed[edges[:, 2] - 1],
                                ph_weed[edges[:, 1] - 1].conj())
        dph_space = dph_space[:, ifg_ind]

        #todo drop_ifg_index logic

        # This all is made when small_baseline_flag != 'y'

        dph_shape = (len(edges), len(ifg_ind))
        dph_smooth = np.zeros(dph_shape).astype(np.complex128)
        dph_smooth2 = np.zeros(dph_shape).astype(np.complex128)
        for i in range(len(ifg_ind)):
            time_delta = get_time_deltas_in_days(i)
            weight_factor = np.exp(-(np.power(time_delta, 2)) / 2 /
                                   math.pow(self.__time_win, 2))
            weight_factor = weight_factor / np.sum(weight_factor)

            dph_mean = get_dph_mean(dph_space, len(edges), weight_factor)

            repmat = np.matlib.repmat(
                ArrayUtils.to_col_matrix(dph_mean).conj(), 1, len(ifg_ind))
            dph_mean_adj = np.angle(np.multiply(dph_space, repmat))

            G = np.array([np.ones(len(ifg_ind)), time_delta]).transpose()
            # 'm' in Stamps
            weighted_least_sqrt = MatlabUtils.lscov(
                G,
                dph_mean_adj.conj().transpose(), weight_factor)
            #todo Find better name
            least_sqrt_G = np.asarray(
                (np.asmatrix(G) *
                 np.asmatrix(weighted_least_sqrt)).conj().transpose())
            dph_mean_adj = np.angle(np.exp(1j * (dph_mean_adj - least_sqrt_G)))
            # 'm2' in Stamps
            weighted_least_sqrt2 = MatlabUtils.lscov(
                G,
                dph_mean_adj.conj().transpose(), weight_factor)

            # We don't make transpose for weighted_least_sqrt because it doesn't
            # do anything in this case
            dph_smooth_val_exp = np.exp(
                1j * (weighted_least_sqrt[0, :] + weighted_least_sqrt2[0, :]))
            dph_smooth[:, i] = np.multiply(dph_mean, dph_smooth_val_exp)
            weight_factor[i] = 0  # Let's make ourselves as zero

            dph_smooth2[:, i] = get_dph_mean(dph_space, len(edges),
                                             weight_factor)

        dph_noise = np.angle(np.multiply(dph_space, dph_smooth2.conj()))
        ifg_var = np.var(dph_noise, 0)

        dph_noise = np.angle(np.multiply(dph_space, dph_smooth.conj()))
        K_weights = np.divide(1, ifg_var)
        K = MatlabUtils.lscov(bperp_meaned,
                              dph_noise.conj().transpose(),
                              K_weights).conj().transpose()
        dph_noise -= K * bperp_meaned.transpose()

        edge_std = MatlabUtils.std(dph_noise, axis=1)
        edge_max = np.max(np.abs(dph_noise), axis=1)

        return edge_std, edge_max
예제 #11
0
 def arange_neighbours_select_arr(i, ind):
     return ArrayUtils.arange_include_last(ij_shift[i, ind] - 2,
                                           ij_shift[i, ind])