def _create_script_backup(self):
     """Create and save a copy of the script that initialized the Experiment"""
     #################### Attempt to Copy Source Script if Allowed ####################
     try:
         if G.Env.result_paths["script_backup"] is not None:
             try:
                 self._source_copy_helper()
             except FileNotFoundError:
                 os.makedirs(self.result_paths["script_backup"],
                             exist_ok=False)
                 self._source_copy_helper()
             G.log("Created backup of file: '{}'".format(
                 self.source_script))
         else:
             G.log("Skipped creating backup of file: '{}'".format(
                 self.source_script))
     #################### 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
Пример #2
0
 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")
Пример #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])
Пример #4
0
 def _validate_environment(self):
     """Check that 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 with key: "{self.cross_experiment_key}"')
     else:
         raise EnvironmentInvalidError('An experiment is in progress. It must finish before a new one can be started')
 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 with key: '{self.cross_experiment_key}'"
         )
     else:
         raise EnvironmentInvalidError(
             "Current experiment must finish before starting another")
Пример #6
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
         os.makedirs(self.tested_keys_dir)
Пример #7
0
 def _create_script_backup(self):
     """Create and save a copy of the script that initialized the Experiment"""
     #################### Attempt to Copy Source Script if Allowed ####################
     try:
         if G.Env.result_paths['script_backup'] is not None:
             try:
                 shutil.copyfile(self.source_script, F'{self.result_paths["script_backup"]}/{self.experiment_id}.py')
             except FileNotFoundError:
                 os.makedirs(self.result_paths["script_backup"], exist_ok=False)
                 shutil.copyfile(self.source_script, F'{self.result_paths["script_backup"]}/{self.experiment_id}.py')
             G.log('Created backup of file: "{}"'.format(self.source_script))
         else:
             G.log('Skipped creating backup of file: "{}"'.format(self.source_script))
     #################### 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='G.Env lacks "result_paths" attribute\n{!s}'.format(_ex))
         raise
     except KeyError as _ex:
         if 'script_backup' not in G.Env.result_paths:
             raise EnvironmentInvalidError(extra='G.Env.result_paths lacks "script_backup" key\n{!s}'.format(_ex))
         raise