def run_profile(sim, nb_dim=2, path_results=".", plot=False, verbose=True): """Profile a simulation run and save the results in `profile.pstats` Parameters ---------- sim : Simul An initialized simulation object nb_dim : int Dimension of the solver path_results : str Path where all pstats files will be saved """ path, t_as_str = get_path_file(sim, path_results, "profile", ".pstats") t0 = time() with stdout_redirected(not verbose): cProfile.runctx("sim.time_stepping.start()", globals(), locals(), path) t_end = time() if sim.oper.rank == 0: times = analyze_stats(path, nb_dim, plot) print(f"\nelapsed time = {t_end - t0:.3f} s") name_solver = sim.__module__.rsplit(".", maxsplit=1)[0] print( f"To display the results:\nfluidsim-profile -p -sf {path} -s {name_solver}" ) print("\nwith gprof2dot and graphviz (command dot):\n" f"gprof2dot -f pstats {path} | dot -Tpng -o profile.png") else: # Retain only rank 0 profiles os.remove(path) return path
def test_main(self): with stdout_redirected(): command = "fluidimviewer" args = command.split() args.append(str(path_image_samples / "Karman/Images")) sys.argv = args args = parse_args() ImageViewer(args) args = command.split() args.append(str(path_image_samples / "Karman/Images/*")) sys.argv = args args = parse_args() self = ImageViewer(args) self.set_autoclim(None) self._switch() self._switch() self._increase_ifile() self._decrease_ifile() self._submit_n("2") self._increase_ifile_n() self._decrease_ifile_n() self._change_cmin("1") self._change_cmax("2")
def tttest_surftrack(self): params = TopologySurfaceTracking.create_default_params() params.film.path = str(self.path_input_files) params.film.ind_start = 1 params.film.path_ref = str(self.path_input_files) params.surface_tracking.xmin = 125 params.surface_tracking.xmax = 290 params.series.ind_start = 1 # params.saving.how has to be equal to 'complete' for idempotent jobs # (on clusters) params.saving.plot = False params.saving.how_many = 100 params.saving.how = "complete" params.saving.postfix = self.postfix print(params) topology = TopologySurfaceTracking(params, logging_level="info") # topology.make_code_graphviz('topo.dot') seq = False with stdout_redirected(): topology = TopologySurfaceTracking(params, logging_level="info") topology.compute(sequential=seq) # print(topology.path_dir_result) # log = LogTopology(topology.path_dir_result) # topology.compute(sequential=seq) # not generating plots if seq mode is false if seq == False: params.saving.plot = False
def test_piv(self): params = TopologyPIV.create_default_params() params.series.path = str(self.path_input_files) params.series.ind_start = 1 # temporary, avoid a bug on Windows params.piv0.method_correl = "pythran" params.piv0.shape_crop_im0 = 16 # compute only few vectors params.piv0.grid.overlap = -8 params.multipass.number = 2 params.multipass.use_tps = False params.saving.how = "recompute" params.saving.postfix = self.postfix with stdout_redirected(): topology = TopologyPIV(params, logging_level="info") topology.compute() log = LogTopology(topology.path_dir_result) log.plot_durations() log.plot_nb_workers() log.plot_memory()
def init_df(path): with stdout_redirected(): sim = load_sim(str(path)) # dico_sp = sim.output.spatial_means.load() dico = sim.output.spectra.load2d_mean() kmax = dico["kh"].max() dealias = sim.params.oper.coef_dealiasing ratio = sim.params.preprocess.viscosity_const * np.pi kdiss = kmax / ratio # last_file = sorted(path.glob("state_phys*"))[-1].name kf = _k_f(sim.params) Lf = np.pi / kf eps, E, ts, tmax = epsetstmax(path) c = sim.params.c2**0.5 # Fr = (eps / kf) ** (1./3) / c Fr = (eps * Lf)**(1.0 / 3) / c return { "short name": os.path.basename(path), "kmax": kmax, "kmax_resolved": kmax * dealias, "k_d": kdiss, "kmax_by_kdiss": ratio, "c": c, "$n$": sim.params.oper.nx, "nu": sim.params.nu_2, "visc_const": sim.params.preprocess.viscosity_const, "dealias": dealias, "tmax": float(tmax), "$F_f$": float(Fr), "E": float(E), "$\epsilon$": float(eps), }
def test_minimal_piv(self): params = WorkPIV.create_default_params() # for a very short computation params.piv0.shape_crop_im0 = 32 params.piv0.grid.overlap = -3 # still buggy # params.piv0.nb_peaks_to_search = 2 params.multipass.number = 2 params.fix.displacement_max = 2 params.fix.threshold_diff_neighbour = 2 piv = WorkPIV(params=params) with stdout_redirected(): result = piv.calcul(self.serie) result.piv0.save(self.path_tmp) result.save(self.path_tmp) path_file = next(self.path_tmp.iterdir()) MultipassPIVResults(path_file) result.save(self.path_tmp, "uvmat") light = result.make_light_result() light.save(self.path_tmp) path_file = next(self.path_tmp.glob("*light*")) LightPIVResults(str_path=str(path_file))
def avg_shock_seperation_from_shortname( short_name, save_as="dataframes/shock_sep.csv", dict_paths=paths_sim, t_approx=None, ): path = dict_paths[short_name] print("Processing:", path) with stdout_redirected(): # Need to load so because type_fft maybe set to use an MPI class params, Simul = fls.load_for_restart(path, t_approx, merge_missing_params=True) params.output.HAS_TO_SAVE = False params.output.ONLINE_PLOT_OK = False params.ONLY_COARSE_OPER = False params.oper.type_fft = "default" sim = Simul(params) mean, std = avg_shock_seperation(sim) sim.output.close_files() del sim gc.collect() if not os.path.exists(save_as): with open(save_as, "a") as f: heading = "short_name,path,mean,std\n" f.write(heading) with open(save_as, "a") as f: result = f"{short_name},{path},{mean},{std}\n" f.write(result) return mean, std
def test_piv_list(self): params = WorkPIV.create_default_params() # for a very short computation params.piv0.shape_crop_im0 = [33, 44] params.piv0.grid.overlap = -3 # params.piv0.nb_peaks_to_search = 2 params.multipass.use_tps = False piv = WorkPIV(params=params) with stdout_redirected(): result = piv.calcul(self.serie) piv0 = result.piv0 im0, im1 = piv0.get_images() with stdout_redirected(): DisplayPIV(im0, im1, piv0) d = DisplayPIV(im0, im1, piv0, show_interp=True, hist=True) d.switch() d.select_arrow([0], artist=d.q) d.select_arrow([0], artist=None) event = MyObj() event.key = "alt+h" event.inaxes = None d.onclick(event) event.inaxes = d.ax1 event.key = "alt+s" d.onclick(event) event.key = "alt+left" d.onclick(event) event.key = "alt+right" d.onclick(event) event.artist = None d.onpick(event) event.artist = d.q event.ind = [0] d.onpick(event)
def test_main(self): path = path_image_samples / "Karman/Images.civ/0_XML/Karman_1-4.xml" command = f"run {str(path)} --mode recompute" sys.argv = command.split() with stdout_redirected(): main()
def bench(sim, path_results): """Benchmark a simulation run and save the results in a JSON file. Parameters ---------- sim : Simul An initialized simulation object path_results : str Directory path to save results in """ path, t_as_str = get_path_file(sim, path_results) print("running a benchmark simulation... ", end="") with stdout_redirected(): t0_usr = time() t0_sys = clock() sim.time_stepping.start() t_elapsed_sys = clock() - t0_sys t_elapsed_usr = time() - t0_usr print("done.\n{} time steps computed in {:.2f} s".format( sim.time_stepping.it, t_elapsed_usr)) if sim.oper.rank != 0: return results = { "t_elapsed_usr": t_elapsed_usr, "t_elapsed_sys": t_elapsed_sys, "key_solver": sim.info_solver.short_name.lower(), "n0": sim.oper.shapeX_seq[0], "n1": sim.oper.shapeX_seq[1], "n0_loc": sim.oper.shapeX_loc[0], "n1_loc": sim.oper.shapeX_loc[1], "k0_loc": sim.oper.shapeK_loc[0], "k1_loc": sim.oper.shapeK_loc[1], "nb_proc": mpi.nb_proc, "pid": os.getpid(), "time_as_str": t_as_str, "hostname": socket.gethostname(), "nb_iter": sim.params.time_stepping.it_end, "type_fft": sim.oper.type_fft, } try: results.update({ "n2": sim.oper.shapeX_seq[2], "n2_loc": sim.oper.shapeX_loc[2], "k2_loc": sim.oper.shapeK_loc[2], }) except IndexError: pass with open(path, "w") as file: json.dump(results, file, sort_keys=True) file.write("\n") print("results benchmarks saved in\n" + path + "\n")
def test_im2im(self): params = Topo.create_default_params() params.series.path = str(path_image_samples / "Karman/Images") params.series.ind_start = 1 params.im2im = "fluidimage.preproc.image2image.Im2ImExample" params.args_init = ((1024, 2048), "clip") params.saving.how = "recompute" params.saving.postfix = self.postfix with stdout_redirected(): topology = Topo(params, logging_level=False) topology.compute()
def _profile(type_fft): """Run profile once.""" Simul = solver.Simul params = Simul.create_default_params() if dim == "2d": modif_params2d(params, n0, n1, name_run="bench", type_fft=type_fft, it_end=it_end) elif dim == "3d": modif_params3d( params, n0, n1, n2, name_run="bench", type_fft=type_fft, it_end=it_end, ) else: raise ValueError("dim has to be in ['2d', '3d']") nb_dim = int(dim[0]) with stdout_redirected(not verbose): sim = Simul(params) try: run_profile(sim, nb_dim, path_dir, verbose=verbose) except Exception as e: if _is_testing: raise else: print("WARNING: Some error occurred while running benchmark" " / saving results!") raise print(e) finally: tear_down(sim) gc.collect()
def test_calibrate(self): """Tests construct_object_points and CalibCV methods.""" path_input = pathbase / "cam0" / "0mm_cam0.tif" with stdout_redirected(): calib = CalibCV("fluidimage_test_calib_cv.h5") result_cache = self.calib_cache.params origin = (250, 250) # Hardcoded origin imgpoints = [construct_image_points(path_input, origin)] objpoints = [construct_object_points(7, 7, 0.0, 3.0)] zs = [0] im_shape = imread(str(path_input)).shape[::-1] ret, mtx, dist, rvecs, tvecs = calib.calibrate( imgpoints, objpoints, zs, im_shape, origin, debug=True ) assert_array_equal(mtx, result_cache.cam_mtx) assert_almost_equal(rvecs[0], result_cache.rotation[2]) assert_almost_equal(tvecs[0], result_cache.translate[2])
def test(self): """Test classes TomoMLOSCV and ArrayTomoCV.""" with stdout_redirected(): tomo = TomoMLOSCV( path_calib, xlims=(-10, 10), ylims=(-10, 10), zlims=(-5, 5), nb_voxels=(11, 11, 5), ) tomo.verify_projection() pix = tomo.phys2pix("cam0") tomo.array.init_paths(path_particle, path_output) tomo.reconstruct(pix, path_particle, threshold=None, save=True) path_result = list(path_output.glob("*"))[0] array = ArrayTomoCV(h5file_path=path_result) array.describe() array.load_dataset(copy=True) array.plot_slices(0, 1) array.clear()
def run(self, result=None): with stdout_redirected(self.has_to_redirect_stdout): super().run(result=result)
def setUpClass(cls): cls.init_params() with stdout_redirected(cls.has_to_redirect_stdout): cls.sim = cls.Simul(cls.params)
def setUpClass(cls): cls.init_params() with stdout_redirected(cls.has_to_redirect_stdout), cls.Simul( cls.params) as sim: cls.sim = sim sim.time_stepping.start()