def test_erf(): import os import sys dirname = os.path.dirname(os.path.abspath(__file__)) main_dir = os.path.join(dirname, os.path.pardir, os.path.pardir) code_dir = os.path.join(main_dir, "code") sys.path.insert(0, code_dir) import plot_beam import surface as surf import parameters as par config_file = os.path.join(dirname, 'erf.cfg') par.set_parameters(config_file) # cfg overwrites par.INITIAL_SURFACE_TYPE = 'Flat' par.TOTAL_TIME = 1 # par.BEAM_CURRENT = 1e-12 par.XMAX = 500 par.XMIN = -500 filename, surface = plot_beam.simulate(config_file, False) if os.path.isfile(filename + '_save'): surface_save = surf.load(filename + '_save') else: raise FileNotFoundError(filename + '_save not found') distance = surface.distance(surface_save) assert distance < 0.00292933136297
def SALT(Video, new=True, BM=None, Patches=None, Param=None): if new: parameters.set_parameters(Video) Param = parameters.Param w_lr = Param.low_rank_weight w_s = Param.sparse_weight w_d = Param.data_weight m = Param.temporal_search_range + 1 patches_num = Param.patches_num psize = Param.psize frame_num = Param.frame_num if new: Patches = patches.overlapping_patches(Video, Param) BM = vbm.video_block_matching(Patches, Param) transform = scipy.fftpack.dct(np.eye(psize*m), norm='ortho', axis=0) Denoised_video = np.zeros(Video.shape) for t in range(frame_num): data_to_sparse = np.zeros((psize*m, patches_num)) for i in range(patches_num): data_to_sparse[:,i] = np.ravel(Patches[:,BM[i+t*patches_num,:m]]) sparsed = sparse_coding.sparse(data_to_sparse, Param, transform) transform = tu.trans_update(sparsed, data_to_sparse) sparsed = sparse_coding.sparse(data_to_sparse, Param, transform) for i in range(patches_num): s = np.zeros((psize, m)) KNN = Patches[:,BM[i+t*patches_num]] D = LR_approx(KNN, Param) index1 = BM[i+t*patches_num,:m] index2 = BM[i+t*patches_num,m:] s = (transform.T @ sparsed[:,i]).reshape((psize, m)) Patches[:,index1] = ((w_s*s + w_lr*D[:,:m] + w_d*Patches[:,index1]) / (w_s+w_d+w_lr)) Patches[:,index2] = ((w_lr*D[:,m:] + w_d*Patches[:,index2]) / (w_lr+w_d)) Denoised_img = ag.aggregation(Patches, Param, t) #plt.imshow(Denoised_img) #plt.gray() #plt.show() Denoised_video[:,:,t] = Denoised_img return Denoised_video
def simulate(config_file, do_plotting=True): """ Performs a simulation with the parameters given inside the config file :param config_file: parameters for simulation :return the surface object after last simulation step """ #time_start = clock() time_start = perf_counter() try: par.set_parameters(config_file) except FileNotFoundError as err: print(err) sys.exit(-1) config = "config" if (config_file.find(".cfg") != -1): config = config_file.replace(".cfg", "") tend = float(par.TOTAL_TIME) dt = float(par.TIME_STEP) time = 0 init_sputtering() surface = Surface() surface_filename = '{}.srf'.format(config) surface.write(surface_filename, time, 'w') while time < tend: try: adv.advance(surface, dt) except ValueError as Err: print(Err) break else: dt = adv.timestep(dt, time, tend) time += dt finally: surface.write(surface_filename, time, 'a') #time_compute = clock() - time_start time_compute = perf_counter() - time_start print("Computing Time = " + str(time_compute)) if par.PLOT_SURFACE and do_plotting: if os.path.isfile(surface_filename + "_save"): plot.plot(surface_filename, surface_filename + "_save") else: plot.plot(surface_filename) return surface
def test_raster_scan(): import os import sys dirname = os.path.dirname(os.path.abspath(__file__)) main_dir = os.path.join(dirname, os.path.pardir, os.path.pardir) code_dir = os.path.join(main_dir, "code") sys.path.insert(0, code_dir) import miniTopSim import surface as surf import parameters as par config_file = os.path.join(dirname, 'raster_scan_1.cfg') par.set_parameters(config_file) '''filename,''' sim = miniTopSim.simulate(config_file, True) surface_save = surf.load(os.path.join(dirname, 'raster_scan_1.srf_save')) assert sim.distance(surface_save) < 0.00292
def simulate(config_file, setconfig=True): """ Performs a simulation with the parameters given inside the config file :param config_file: parameters for simulation :return the surface object after last simulation step """ if setconfig is True: try: par.set_parameters(config_file) except FileNotFoundError as err: print(err) sys.exit(-1) config = "config" if (config_file.find(".cfg") != -1): config = config_file.replace(".cfg", "") tend = float(par.TOTAL_TIME) dt = float(par.TIME_STEP) #dt = float(par.DWELL_TIME) time = 0 init_sputtering() surface = Surface() surface_filename = '{}.srf'.format(config) surface.write(surface_filename, time, 'w') while time < tend: adv.advance(surface, dt) dt = adv.timestep(dt, time, tend) time += dt surface.write(surface_filename, time, 'a') return surface_filename, surface
section = 'Numerics' cfg.add_section(section) cfg.set(section, 'TIME_STEP', str(par.TIME_STEP)) section = 'Beam' cfg.add_section(section) cfg.set(section, 'TOTAL_TIME', str(par.TOTAL_TIME)) section = 'Physics' cfg.add_section(section) cfg.set(section, 'ETCH_RATE', str(par.ETCH_RATE)) section = 'Output' cfg.add_section(section) cfg.set(section, 'PLOT_SURFACE', str(par.PLOT_SURFACE)) with open(output_file, 'w') as output: cfg.write(output) try: check_flag = par.set_parameters(sys.argv[1]) except FileNotFoundError as err: print(err) sys.exit(-1) except IndexError: print('No file specified as systemargument') sys.exit(-1) _write_output(str(sys.argv[1]).replace('.cfg', '.dat'))
def SALT_gd(Video, new=True, BM=None, Patches=None, Param=None): if new: parameters.set_parameters(Video) Param = parameters.Param K = Param.similar_patches_number sp_treshold = Param.sparse_treshold w_lr = Param.low_rank_weight w_s = Param.sparse_weight w_d = Param.data_weight m = Param.temporal_search_range // 2 patches_num = Param.patches_num psize = Param.psize frame_num = Param.frame_num rank = 7 if new: Patches = patches.overlapping_patches(Video, Param) BM = vbm.video_block_matching(Patches, Param) transform1 = scipy.fftpack.dct(np.eye(psize * m), norm='ortho', axis=0) transform2 = scipy.fftpack.dct(np.eye(K * m), norm='ortho', axis=0) Denoised_video = np.zeros(Video.shape) rand1 = np.random.random_sample((psize, rank)) rand2 = np.random.random_sample((rank, K)) for t in range(frame_num): for i in range(patches_num): #u, s, vt = scipy.sparse.linalg.svds(Patches[:,BM[i+t*patches_num,:]], #rank) #s = np.diag(s) #P = u @ s #L = vt M = Patches[:, BM[i + t * patches_num, :]] P = rand1 L = rand2 for j in range(8): Ppinv = np.linalg.pinv(P) L = Ppinv @ M Lpinv = np.linalg.pinv(L) P = M @ Lpinv if j % 4 == 1: Pm = P[:, :m] p = np.ravel(Pm) sparsed_p = sparse_coding.sparse(p, Param, transform1) transform1 = tu.trans_update(sparsed_p, p) p = transform1.T @ hard_treshold(transform1 @ p, sp_treshold) Pm = p.reshape(Pm.shape) P[:, :m] = Pm Lm = L[:m, :] l = np.ravel(Lm) sparsed_l = sparse_coding.sparse(l, Param, transform2) transform2 = tu.trans_update(sparsed_l, l) l = transform2.T @ hard_treshold(transform2 @ l, sp_treshold) Lm = l.reshape(Lm.shape) L[:m, :] = Lm Patches[:, BM[i + t * patches_num, :]] = P @ L Denoised_img = ag.aggregation(Patches, Param, t) #plt.imshow(Denoised_img) #plt.gray() #plt.show() Denoised_video[:, :, t] = Denoised_img return Denoised_video
def main(): is_slab, number_of_layers, exchange_correlation = set_parameters() data = get_data(number_of_layers, exchange_correlation) graph_data(data) return
sigma = 15 mat_Video = sio.loadmat("salesman.mat") Video = mat_Video['clean'] #Video = skvideo.io.vread("video\Man_texting.mp4", #outputdict={"-pix_fmt": "gray"})[:, :, :, 0] Video = Video[:, :, :] #Video = np.transpose(Video, (1,2,0)) Video = Video[:, :, :2] Noisy_video = positive(Video + np.random.normal(0, sigma, Video.shape)) plt.imshow(Video[:, :, 0]) plt.gray() plt.show plt.imshow(Noisy_video[:, :, 0]) plt.gray() plt.show parameters.set_parameters(Noisy_video) Para = parameters.Param pat = patches.overlapping_patches(Noisy_video, Para) Blocks = vbm.video_block_matching(pat, Para) denoised = sd.SALT(Video, new=False, BM=Blocks, Patches=pat, Param=Para) plt.imshow(denoised[:, :, 0]) #denoised = np.transpose(denoised, (2,0,1)) #skvideo.io.vwrite("denoised_video.mp4", denoised, inputdict={"-r" : "13"}) #Noisy_video = np.transpose(Noisy_video, (2,0,1)) #skvideo.io.vwrite("noisy_video.mp4", Noisy_video, inputdict={"-r" : "13"})
# Version 1.01, Becky Arnold, February 2018 running on python version 2.7.13 import matplotlib.pyplot as plt import parameters import read_data import calc_drdv_and_sort import correct_dv import plotting ''' Set the parameters that will be needed to run the program. In this function the user should set the path_to_data variable to the path to the data file. They should also set error_flag to True if there are errors on the velocity data and False otherwise. ''' path_to_data, error_flag, dv_default, correct_inflation, bin_width, dr_start, dr_end = parameters.set_parameters( ) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ''' Read in the data. The user must fill in this function in read_data.py. n_stars is the number of stars, r is the position data of the stars, v is the velocity data, and verr is the error on the velocity data. If there are no errors on the velocities verr should be 0. ''' n_stars, r, v, verr = read_data.read_data(path_to_data) # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ''' Calculate dr and dv for every possible pair of stars. Then sort the pairs into dr bins and calculate the mean dv in each bin with errors.
based on miniTopSim.py """ import sys from time import clock import matplotlib.pyplot as plt import numpy as np from surface import Surface import advance as adv import parameters as par import plot try: par.set_parameters(sys.argv[1]) except FileNotFoundError as err: print(err) sys.exit(-1) except IndexError: print('No file specified as systemargument') sys.exit(-1) config = "config" if (sys.argv[1].find(".cfg") != -1): config = sys.argv[1].replace(".cfg", "") tend = float(par.TOTAL_TIME) dt = float(par.TIME_STEP) #arrays for the plot