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
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
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)
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)
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
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
def arange_neighbours_select_arr(i, ind): return ArrayUtils.arange_include_last(ij_shift[i, ind] - 2, ij_shift[i, ind])