Exemplo n.º 1
0
def test_XRunnableManager():
    app = a99.get_QApplication()
    rm = pyfant.RunnableManager()
    w = pyfant.XRunnableManager(None, rm)
Exemplo n.º 2
0
            combos.append(combo)
            ll.append(a)

            i += 1
            ii += 1
            if ii == 1000:
                print(a99.format_progress(i, n))
                ii = 0

    # # Runs pfant
    logger.info("Running pfant's...")

    # FLAG_CLEAN = True
    # rm = RunnableManager(flag_auto_clean=FLAG_CLEAN)

    rm = pyfant.RunnableManager()
    rm.add_runnables(combos)
    app = a99.get_QApplication([])
    form = pyfant.XRunnableManager(None, rm)
    form.show()
    # it is good to start the manager as late as possible, otherwise
    # the program will hang if, for example, the form fails to be created.
    rm.start()
    app.exec_()
    rm.exit()

    # # Saves log
    LOG_FILENAME = "tune-zinf-status.log"
    with open(LOG_FILENAME, "w") as h:
        h.write(str(rm))
    logger.info("Final status saved to file '%s'" % LOG_FILENAME)
Exemplo n.º 3
0
def run_parallel(rr,
                 max_simultaneous=None,
                 flag_console=False,
                 runnable_manager=None,
                 flag_verbose=False,
                 flag_exit_if_fail=False):
    """
    Args:
        rr: list of Runnable instances
        max_simultaneous: (defaults to RunnableManager.max_simultaneous)
            maximum number of simultaneous processes. **Note** ineffective if runnable_manager is passed
        flag_console: if True, will display menu with options, otherwise will exit when all runnables finish
        runnable_manager: if passed, will use passed; if not, will create new.
        flag_verbose: whether of not to log any messages (besides console) **Note** if runnable_manager is passed, it will keep its own
        flag_exit_if_fail: exit as soon as possible if any runnable fails? **Note** ineffective if runnable_manager is passed

    Returns: the RunnableManager object
    """
    import pyfant

    # Adds to pool
    logger = a99.get_python_logger()
    if runnable_manager:
        assert isinstance(runnable_manager, pyfant.RunnableManager)
        rm = runnable_manager
    else:
        rm = pyfant.RunnableManager(max_simultaneous=max_simultaneous,
                                    flag_verbose=flag_verbose,
                                    flag_exit_if_fail=flag_exit_if_fail)
    flag_had_to_start = False
    if not rm.flag_start_called:
        rm.start()
        flag_had_to_start = True

    rm.add_runnables(rr)

    # Primitive thread monitor
    if flag_console:
        while True:
            print(("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" +
                   (" ALIVE" if rm.is_alive() else " DEAD")))
            print(rm)
            print(("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" +
                   (" ALIVE" if rm.is_alive() else " DEAD")))
            s = input(
                "[Enter] -- [e]xit & keep in loop -- [q]uit -- [k]ill running >>> "
            )
            if s.lower() == "q":
                if rm.is_alive():
                    try:
                        rm.exit()
                    except:
                        logger.exception("Error trying to exit")
                break
            if s.lower() == "e":
                try:
                    rm.exit()
                except:
                    logger.exception("Error trying to exit")
            if s.lower() == "k":
                rm.kill_runnables()
    else:
        rm.wait_until_finished()
        if flag_had_to_start:
            rm.exit()

    if flag_verbose:
        a99.get_python_logger().info(
            ("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" +
             (" ALIVE" if rm.is_alive() else " DEAD")))
        a99.get_python_logger().info("test-tm2 [SUPPOSED TO HAVE] EXITED")
        a99.get_python_logger().info(rm)

    return rm
Exemplo n.º 4
0
    def __run(self):
        # Called from run() to lower one indentation lever.
        # **Note** If something is not right here: *raise*.
        #
        # **Note** It plays around with Conf, SID, IdMaker objects
        import pyfant as pf

        # # Preparation
        self.__status.stage = "preparing"
        if self.__custom_id:
            self.__sid.id = self.__custom_id
        else:
            self.__sid.make_id()

        # This id maker will create directories inside the
        # multi-session directory.
        # It will replace the Pfant's default id maker
        custom_id_maker = pyfant.IdMaker()
        custom_id_maker.session_prefix_singular = os.path.join(
            self.__sid.dir, "session-")

        # # Runs innewmarcs and hydro2
        ih = pyfant.Combo([pyfant.FOR_INNEWMARCS, pyfant.FOR_HYDRO2])
        ih.conf.flag_output_to_dir = True
        ih.conf.logger = self.__logger
        ih.conf.opt = copy.copy(self.__options)
        ih.conf.sid = self.__sid
        ih.conf.file_main = copy.copy(self.__file_main)
        # Runs innewmarcs and hydro2;
        # it is also expected to create the "multi-session" directory
        ih.run()

        ####
        # # Runs pfant, pfant creates several .norm files
        self.__status.stage = "pfant stage"
        self.__logger.info("+++ pfant stage...")
        pfant_list = []
        symbols = list(self.__file_abxfwhm.ab.keys())
        abdiffss = list(self.__file_abxfwhm.ab.values())
        n_abdif = len(abdiffss[0])
        fwhms = self.__file_abxfwhm.get_fwhms()
        for fwhm in fwhms:
            if fwhm > 9.99:
                raise RuntimeError("fhwm maximum is 9.99")
        for j in range(n_abdif):
            file_abonds_ = copy.deepcopy(self.__file_abonds)

            for i, symbol in enumerate(symbols):
                found = False
                for k in range(len(file_abonds_.ele)):
                    if file_abonds_.ele[k] == symbol:
                        abdif = abdiffss[i][j]
                        file_abonds_.abol[k] += abdif
                        self.__logger.debug(j, " - ", symbol, "with abundance",
                                            file_abonds_.abol[k])
                        found = True

                if not found:
                    raise RuntimeError("Atom '%s' not found" % symbol)

            pfant_name = self.__file_abxfwhm.pfant_names[j] \
                if self.__file_abxfwhm.pfant_names \
                else "%02d" % j
            flprefix = "%s_%s" % (self.__file_main.titrav, pfant_name)

            pfant = pyfant.Pfant()
            pfant.conf.flag_output_to_dir = False
            pfant.conf.opt = copy.copy(self.__options)
            pfant.conf.rename_outputs(
                [pyfant.FOR_INNEWMARCS, pyfant.FOR_HYDRO2], sid=self.sid)
            pfant.conf.sid.id_maker = custom_id_maker
            pfant.conf.sid.id = flprefix
            pfant.conf.opt.flprefix = self.__sid.join_with_session_dir(
                flprefix)
            pfant.conf.file_main = copy.copy(self.__file_main)
            pfant.conf.file_abonds = file_abonds_
            pfant.conf.file_dissoc = file_abonds_.get_file_dissoc()

            self.__logger.debug("LOOK FLPREFIX " +
                                str(pfant.conf.opt.flprefix))

            pfant_list.append(pfant)
        rm = self.__runnable_manager = pyfant.RunnableManager()
        pyfant.run_parallel(pfant_list,
                            flag_console=False,
                            runnable_manager=rm)
        if self._flag_killed:
            return

        if not rm.flag_success:
            raise RuntimeError("Not all pfant's succeeded running.")

        ####
        # # Runs nulbad, saves .sp and .spl files
        # function to convert given FWHM to string to use as part of a file name
        fmt_fwhm = lambda x: "%03d" % round(x * 100)

        # ## Prepares nulbad's
        nulbad_list = []
        sp_filenames_by_fwhm = {
        }  # dictionary containing a list of .sp filenames for each FWHM
        for pfant in pfant_list:
            # prefix is sth like: multi-session-1/Sun_00
            prefix = pfant.conf.opt.flprefix

            for fwhm in fwhms:
                nulbad = pyfant.Nulbad()
                nulbad.conf.flag_output_to_dir = False
                nulbad.conf.sid.id_maker = custom_id_maker
                nulbad.conf.opt = copy.copy(self.__options)
                nulbad.conf.opt.fn_flux = pfant.conf.opt.flprefix + ".norm"
                nulbad.conf.opt.fwhm = fwhm
                nulbad.conf.opt.fn_cv = "%s_%s.sp" % (prefix, fmt_fwhm(fwhm))
                nulbad_list.append(nulbad)

                if not fwhm in sp_filenames_by_fwhm:
                    sp_filenames_by_fwhm[fwhm] = []
                sp_filenames_by_fwhm[fwhm].append(nulbad.conf.opt.fn_cv)

        # ## Saves files for lineplot.py (lists of spectra)
        # Each item of each list is a full path to a spectrum file
        for fwhm, sp_filenames in list(sp_filenames_by_fwhm.items()):
            spl_filename = os.path.join(self.__sid.dir,
                                        "cv_%s.spl" % fmt_fwhm(fwhm))
            with open(spl_filename, "w") as h:
                for sp_filename in sp_filenames:
                    h.write(
                        os.path.abspath(
                            os.path.join(self.__sid.dir,
                                         os.path.basename(sp_filename) +
                                         "\n")))

        # ## Runs nulbads
        self.__status.stage = "nulbad stage"
        self.__logger.info("+++ nulbad stage...")
        rm = self.__runnable_manager = pyfant.RunnableManager()
        pyfant.run_parallel(nulbad_list,
                            flag_console=False,
                            runnable_manager=rm)
        if self._flag_killed:
            return
        if not rm.flag_success:
            raise RuntimeError("Not all nulbad's succeeded running.")

        ####
        # # Deletes session-* directories if successful
        FLAG_CLEAN = True
        if FLAG_CLEAN:
            self.__logger.info("+++ Cleaning up...")
            for pfant in pfant_list:
                pfant.sid.clean()
            for nulbad in nulbad_list:
                nulbad.sid.clean()
        else:
            self.__logger.info("+++ NOT cleaning up...")