def _create_script_backup(self):
        """Create and save a copy of the script that initialized the Experiment if allowed to, and
        if :attr:`source_script` ends with a ".py" extension"""
        #################### Attempt to Copy Source Script if Allowed ####################
        try:
            if not self.source_script.endswith(".py"):
                G.Env.result_paths["script_backup"] = None

            if G.Env.result_paths["script_backup"] is not None:
                self._source_copy_helper()
                G.log(
                    "Created source backup:  '{}'".format(self.source_script),
                    4)
            else:
                G.log(
                    "Skipped source backup:  '{}'".format(self.source_script),
                    4)
        #################### Exception Handling ####################
        except AttributeError as _ex:
            if G.Env is None:
                raise EnvironmentInactiveError(extra="\n{!s}".format(_ex))
            if not hasattr(G.Env, "result_paths"):
                raise EnvironmentInvalidError(
                    extra=f"G.Env lacks 'result_paths' attr\n{_ex!s}")
            raise
        except KeyError as _ex:
            if "script_backup" not in G.Env.result_paths:
                raise EnvironmentInvalidError(
                    extra=
                    f"G.Env.result_paths lacks 'script_backup' key\n{_ex!s}")
            raise
 def _validate_environment():
     """Check that there is a currently active and unoccupied Environment instance"""
     if G.Env is None:
         raise EnvironmentInactiveError()
     if G.Env.current_task is None:
         G.log_(f'Validated Environment with key: "{G.Env.cross_experiment_key}"')
     else:
         raise EnvironmentInvalidError("Must finish current task before starting a new one")
Esempio n. 3
0
    def __init__(self):
        """Base class for other classes that record various Experiment result files. Critical
        attributes of the descendants of :class`recorders.BaseRecorder` are set here, enabling them
        to function properly

        Returns
        -------
        None
            If :attr:`result_path` is None, which means the present result file was blacklisted by
            the active Environment

        Raises
        ------
        EnvironmentInactiveError
            If :attr:`settings.G.Env` is None
        EnvironmentInvalidError
            If any of the following occur: 1) :attr:`settings.G.Env` does not have an attribute
            named 'result_paths', 2) :attr:`settings.G.Env.result_paths` does not contain the
            current `result_path_key`, 3) :attr:`settings.G.Env.current_task` is None"""
        self.result_path = None
        self.result = None

        ##################################################
        # Get Result Path for Record, or Exit Early
        ##################################################
        try:
            self.result_path = G.Env.result_paths[self.result_path_key]
        except AttributeError as _ex:
            if G.Env is None:
                raise EnvironmentInactiveError(str(_ex)).with_traceback(
                    exc_info()[2])
            if not hasattr(G.Env, "result_paths"):
                _err_message = f"{_ex!s}\nG.Env missing 'result_paths' attr"
                raise EnvironmentInvalidError(_err_message).with_traceback(
                    exc_info()[2])
        except KeyError as _ex:
            _err_message = f"{_ex!s}\nG.Env.result_paths missing the key: '{self.result_path_key}'"
            raise EnvironmentInvalidError(_err_message).with_traceback(
                exc_info()[2])

        if self.result_path is None:
            return  # Result file blacklisted and should not be recorded. Kill recording process now

        ##################################################
        # Gather Attributes Required for Record
        ##################################################
        for required_attribute in self.required_attributes:
            try:
                setattr(self, required_attribute,
                        getattr(G.Env.current_task, required_attribute))
            except AttributeError as _ex:
                if G.Env.current_task is None:
                    _err_message = f"{_ex!s}\nNo active experiment found"
                    raise EnvironmentInvalidError(_err_message).with_traceback(
                        exc_info()[2])
                raise EnvironmentInvalidError(str(_ex)).with_traceback(
                    exc_info()[2])
Esempio n. 4
0
 def _validate_environment(self):
     """Ensure there is a currently active Environment instance that is not already occupied"""
     if G.Env is None:
         raise EnvironmentInactiveError("")
     if G.Env.current_task is None:
         G.Env.current_task = self
         G.log(f"Validated Environment:  '{self.cross_experiment_key}'")
     else:
         raise EnvironmentInvalidError(
             "Current experiment must finish before starting another")
Esempio n. 5
0
 def validate_environment(self):
     """Check that the currently active Environment is suitable"""
     if G.Env is None:
         raise EnvironmentInactiveError("")
     if not all([hasattr(G.Env, _) for _ in ["result_paths", "cross_experiment_key"]]):
         raise EnvironmentInvalidError("")
     try:
         # Ensure :attr:`tested_keys_dir` exists before calling :meth:`does_key_exist`, so "None" paths won't be checked
         if os.path.exists(self.tested_keys_dir) is False:
             # TypeError may also be raised if :func:`os.path.exists` receives invalid input
             raise TypeError
     except TypeError:  # Key-making blacklisted
         if self.tested_keys_dir is None:
             return
         make_dirs(self.tested_keys_dir)