Example #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
Example #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)
Example #3
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)
Example #4
0
 def __get_weights(self, da: np.ndarray):
     return ArrayUtils.to_col_matrix(np.divide(1, da))
Example #5
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