def fill_queue_paths(self, input_queue, output_queue): """Fill the first queue (paths)""" assert input_queue is None serie = self.serie if not serie: logger.warning("add 0 image. No image to process.") return names = serie.get_name_arrays() for name in names: path_im_output = self.path_dir_result / name path_im_input = str(self.path_dir_src / name) if self.how_saving == "complete": if not path_im_output.exists(): output_queue[name] = path_im_input else: output_queue[name] = path_im_input if not names: if self.how_saving == "complete": logger.warning( 'topology in mode "complete" and work already done.') else: logger.warning("Nothing to do") return nb_names = len(names) logger.info(f"Add {nb_names} images to compute.") logger.info(f"First files to process: {names[:4]}") logger.debug(f"All files: {names}")
def _add_dock(self, title="Dock", size=(500, 200), position="bottom"): if self.area is None: self.area = dockarea.DockArea() self.win.setCentralWidget(self.area) d = dockarea.Dock(title, size=size) self.area.addDock(d, position) logger.debug("Function _add_dock: " + repr(d))
def _win(self, key=None, attr="__init__"): if self.area is None: win = self.win elif key is None: win = self.area.docks.items()[0][1] else: win = self.area.docks[key] logger.debug("Function _win: " + repr(win)) return win
def fill_queue_paths(self, input_queue, output_queues): assert input_queue is None queue_paths = output_queues[0] queue_couples_of_names = output_queues[1] serie = self.serie if len(serie) == 0: logger.warning("add 0 image. No image to process.") return names = serie.get_name_arrays() for name in names: path_im_output = self.path_dir_result / name path_im_input = str(self.path_dir_src / name) if self.how_saving == "complete": if not path_im_output.exists(): queue_paths[name] = path_im_input else: queue_paths[name] = path_im_input if len(names) == 0: if self.how_saving == "complete": logger.warning( 'topology in mode "complete" and work already done.') else: logger.warning("Nothing to do") return nb_names = len(names) logger.info(f"Add {nb_names} images to compute.") logger.info("First files to process: " + str(names[:4])) logger.debug("All files: " + str(names)) series = self.series if not series: logger.warning("add 0 couple. No phase to correct.") return nb_series = len(series) logger.info(f"Add {nb_series} phase to correct.") for iserie, serie in enumerate(series): if iserie > 1: break logger.info("Files of serie {}: {}".format( iserie, serie.get_name_arrays())) # for the first corrected angle : corrected_angle = angle ind_serie, serie = next(series.items()) name = serie.get_name_arrays()[0] queue_couples_of_names[ind_serie - 1] = (name, name) for ind_serie, serie in series.items(): queue_couples_of_names[ind_serie] = serie.get_name_arrays()
def _add_gfx_item(self, obj, item): # GWidget = pg.graphicsItems.GraphicsWidget.GraphicsWidget GPlotItem = pg.graphicsItems.GraphicsLayout.PlotItem GItem = pg.graphicsItems.GraphicsItem.GraphicsItem attr = " " if hasattr(obj, "addPlot") and isinstance(item, GPlotItem): obj.addPlot(item) attr = "addPlot" elif hasattr(obj, "addItem") and isinstance(item, GItem): obj.addItem(item) attr = "addItem" elif hasattr(obj, "addWidget"): obj.addWidget(item) attr = "addWidget" elif hasattr(obj, "setCentralWidget"): obj.setCentralWidget(item) attr = "setCentralWidget" logger.debug("Function _add_gfx_item: " + attr + repr(item))
def fill_couples_of_names_and_paths(self, input_queue, output_queues): """Fill the two first queues""" assert input_queue is None queue_couples_of_names = output_queues[0] queue_paths = output_queues[1] series = self.series if not series: logger.warning("add 0 couple. No PIV to compute.") return if self.how_saving == "complete": index_series = [] for ind_serie, serie in self.series.items(): name_piv = get_name_piv(serie, prefix="piv") if not (self.path_dir_result / name_piv).exists(): index_series.append(ind_serie) if not index_series: logger.warning( 'topology in mode "complete" and work already done.') return series.set_index_series(index_series) if logger.isEnabledFor(DEBUG): logger.debug( repr([serie.get_name_arrays() for serie in series])) nb_series = len(series) logger.info(f"Add {nb_series} PIV fields to compute.") for iserie, serie in enumerate(series): if iserie > 1: break logger.info("Files of serie {}: {}".format( iserie, serie.get_name_arrays())) for ind_serie, serie in series.items(): queue_couples_of_names[ind_serie] = serie.get_name_arrays() for name, path in serie.get_name_path_arrays(): queue_paths[name] = path
def init_series(self) -> List[str]: """Initializes the SeriesOfArrays object `self.series` based on input parameters.""" series = self.series if not series: logger.warning( "encountered empty series. No images to preprocess.") return if self.how_saving == "complete": index_subsets = [] for ind_subset, subset in self.series.items(): names_serie = subset.get_name_arrays() name_preproc = get_name_preproc( subset, names_serie, ind_subset, series.nb_series, self.params.saving.format, ) if not (self.path_dir_result / name_preproc).exists(): index_subsets.append(ind_subset) series.set_index_series(index_subsets) if logger.isEnabledFor(DEBUG): logger.debug( repr([subset.get_name_arrays() for subset in series])) nb_subsets = len(series) if nb_subsets == 0: logger.warning( 'topology in mode "complete" and work already done.') return elif nb_subsets == 1: plurial = "" else: plurial = "s" logger.info(f"Add {nb_subsets} image serie{plurial} to compute.")
def compute(self, sequential=None, has_to_exit=True): """Compute (run all works to be done). Parameters ---------- sequential : None If bool(sequential) is True, the computations are run in sequential (useful for debugging). has_to_exit : True If bool(has_to_exit) is True and if the computation has to stop because of a signal 12 (cluster), a signal 99 is sent at exit. """ if hasattr(self, "path_output"): logger.info("path results:\n" + str(self.path_output)) if hasattr(self, "params"): tmp_path_params = str( self.path_output / ("params_" + time_as_str() + f"_{os.getpid()}")) if not os.path.exists(tmp_path_params + ".xml"): path_params = tmp_path_params + ".xml" else: i = 1 while os.path.exists(tmp_path_params + "_" + str(i) + ".xml"): i += 1 path_params = tmp_path_params + "_" + str(i) + ".xml" self.params._save_as_xml(path_params) self.t_start = time() log_memory_usage(time_as_str(2) + ": starting execution. mem usage") self.nb_workers_cpu = 0 self.nb_workers_io = 0 workers = [] class CheckWorksThread(threading.Thread): cls_to_be_updated = threading.Thread def __init__(self): self.has_to_stop = False super().__init__() self.exitcode = None self.daemon = True def in_time_loop(self): t_tmp = time() for worker in workers: if (isinstance(worker, self.cls_to_be_updated) and worker.fill_destination()): workers.remove(worker) t_tmp = time() - t_tmp if t_tmp > 0.2: logger.info("update list of workers with fill_destination " "done in {:.3f} s".format(t_tmp)) sleep(dt_update) def run(self): try: while not self.has_to_stop: self.in_time_loop() except Exception as e: print("Exception in UpdateThread") self.exitcode = 1 self.exception = e class CheckWorksProcess(CheckWorksThread): cls_to_be_updated = Process def in_time_loop(self): # weird bug subprocessing py3 for worker in workers: if not worker.really_started: # print('check if worker has really started.' + # worker.key) try: worker.really_started = ( worker.comm_started.get_nowait()) except queue.Empty: pass if (not worker.really_started and time() - worker.t_start > 10): # bug! The worker does not work. We kill it! :-) logger.error( cstring( "Mysterious bug multiprocessing: " "a launched worker has not started. " "We kill it! ({}, key: {}).".format( worker.work_name, worker.key), color="FAIL", )) # the case of this worker has been worker.really_started = True worker.terminate() super().in_time_loop() self.thread_check_works_t = CheckWorksThread() self.thread_check_works_t.start() self.thread_check_works_p = CheckWorksProcess() self.thread_check_works_p.start() while not self._has_to_stop and (any( [not q.is_empty() for q in self.queues]) or len(workers) > 0): # debug # if logger.level == 10 and \ # all([q.is_empty() for q in self.queues]) and len(workers) == 1: # for worker in workers: # try: # is_alive = worker.is_alive() # except AttributeError: # is_alive = None # logger.debug( # str((worker, worker.key, worker.exitcode, is_alive))) # if time() - worker.t_start > 60: # from fluiddyn import ipydebug # ipydebug() self.nb_workers = len(workers) # slow down this loop... sleep(dt_small) if self.nb_workers_cpu >= nb_max_workers: logger.debug( cstring( ("The workers are saturated: " "{}, sleep {} s").format(self.nb_workers_cpu, dt), color="WARNING", )) sleep(dt) for q in self.queues: if not q.is_empty(): logger.debug(q) logger.debug("check_and_act for work: " + repr(q.work)) try: new_workers = q.check_and_act(sequential=sequential) except OSError: logger.error( cstring( "Memory full: to free some memory, no more " "computing job will be launched while the last " "(saving) waiting queue is not empty.", color="FAIL", )) log_memory_usage(color="FAIL", mode="error") self._clear_save_queue(workers, sequential) logger.info( cstring( "The last waiting queue has been emptied.", color="FAIL", )) log_memory_usage(color="FAIL", mode="info") continue if new_workers is not None: for worker in new_workers: workers.append(worker) logger.debug("workers: " + repr(workers)) if self.thread_check_works_t.exitcode: raise self.thread_check_works_t.exception if self.thread_check_works_p.exitcode: raise self.thread_check_works_p.exception if len(workers) != self.nb_workers: gc.collect() if self._has_to_stop: logger.info( cstring( "Will exist because of signal 12.", "Waiting for all workers to finish...", color="FAIL", )) self._clear_save_queue(workers, sequential) self.thread_check_works_t.has_to_stop = True self.thread_check_works_p.has_to_stop = True self.thread_check_works_t.join() self.thread_check_works_p.join() self.print_at_exit(time() - self.t_start) log_memory_usage(time_as_str(2) + ": end of `compute`. mem usage") if self._has_to_stop and has_to_exit: logger.info(cstring("Exit with signal 99.", color="FAIL")) exit(99) self._reset_std_as_default()
def add_series(self, series): if len(series) == 0: logger.warning("add 0 couple. No PIV to compute.") return if self.how_saving == "complete": names = [] index_series = [] for i, serie in enumerate(series): name_piv = get_name_piv(serie, prefix="piv") if os.path.exists(os.path.join(self.path_dir_result, name_piv)): continue for name in serie.get_name_arrays(): if name not in names: names.append(name) index_series.append(i * series.ind_step + series.ind_start) if len(index_series) == 0: logger.warning( 'topology in mode "complete" and work already done.') return series.set_index_series(index_series) logger.debug(repr(names)) logger.debug(repr([serie.get_name_arrays() for serie in series])) else: names = series.get_name_all_arrays() nb_series = len(series) print("Add {} PIV fields to compute.".format(nb_series)) for i, serie in enumerate(series): if i > 1: break print("Files of serie {}: {}".format(i, serie.get_name_arrays())) self.wq0.add_name_files(names) self.wq_images.add_series(series) k, o = self.wq0.popitem() im = self.wq0.work(o) self.wq0.fill_destination(k, im) # a little bit strange, to apply mask... try: params_mask = self.params.mask except AttributeError: params_mask = None couple = ArrayCouple(names=("", ""), arrays=(im, im), params_mask=params_mask) im, _ = couple.get_arrays() self.piv_work._prepare_with_image(im)