pypet/trajectory.py
def f_expand(self, build_dict, fail_safe=True):
        """Similar to :func:`~pypet.trajectory.Trajectory.f_explore`, but can be used to enlarge
        already completed trajectories.

        Please ensure before usage, that all explored parameters are loaded!

        :param build_dict:

            Dictionary containing the expansion

        :param fail_safe:

            If old ranges should be **deep-copied** in order to allow to restore
            the original exploration if something fails during expansion.
            Set to `False` if deep-copying your parameter ranges causes errors.

        :raises:

            TypeError: If not all explored parameters are enlarged

            AttributeError: If keys of dictionary cannot be found in the trajectory

            NotUniqueNodeError:

                If dictionary keys do not unambiguously map to single parameters

            ValueError: If not all explored parameter ranges are of the same length

        """
        if len(self._explored_parameters) == 0:
            self._logger.info('Your trajectory has not been explored, yet. '
                              'I will call `f_explore` instead.')
            return self.f_explore(build_dict)

        enlarge_set = set([self.f_get(key).v_full_name
                           for key in build_dict.keys()])

        # Check if all explored parameters will be enlarged, otherwise
        # We cannot enlarge the trajectory
        if not set(self._explored_parameters.keys()) == enlarge_set:
            raise TypeError('You have to enlarge dimensions you have explored before! Currently'
                            ' explored parameters are not the ones you specified in your building'
                            ' dictionary, i.e. %s != %s' %
                            (str(set(self._explored_parameters.keys())),
                             str(set(build_dict.keys()))))
        if any(x is None for x in self._explored_parameters.values()):
            raise TypeError('At least one of your explored parameters is not fully loaded, '
                            'please load it.')

        old_ranges = None
        if fail_safe:
            old_ranges = {}
            for param_name in self._explored_parameters:
                old_ranges[param_name] = self._explored_parameters[param_name].f_get_range()
            try:
                old_ranges = cp.deepcopy(old_ranges)
            except Exception:
                self._logger.error('Cannot deepcopy old parameter ranges, if '
                                     'something fails during `f_expand` I cannot revert the '
                                     'trajectory to old settings.')
                old_ranges = None

        try:
            count = 0
            length = None
            for key, builditerable in build_dict.items():
                act_param = self.f_get(key)

                act_param.f_unlock()
                act_param._expand(builditerable)

                name = act_param.v_full_name

                self._explored_parameters[name] = act_param

                # Compare the length of two consecutive parameters in the `build_dict`
                if count == 0:
                    length = act_param.f_get_range_length()
                elif not length == act_param.f_get_range_length():
                    raise ValueError('The parameters to explore have not the same size!')
                count += 1

            original_length = len(self)
            for irun in range(original_length, length):
                self._add_run_info(irun)
            self._test_run_addition(length)

            # We need to update the explored parameters in case they were stored:
            self._remove_exploration()

        except Exception:
            if old_ranges is not None:
                # Try to restore the original parameter exploration
                for param_name in old_ranges:
                    param_range = old_ranges[param_name]
                    param = self._explored_parameters[param_name]
                    param.f_unlock()
                    try:
                        param._shrink()
                    except Exception as exc:
                        self._logger.error('Could not shrink parameter `%s` '
                                           'because of:`%s`' % (param_name, repr(exc)))
                    param._explore(param_range)
                    param._explored = True
            raise
def f_expand(self, build_dict, fail_safe=True):
        """Similar to :func:`~pypet.trajectory.Trajectory.f_explore`, but can be used to enlarge
        already completed trajectories.

        Please ensure before usage, that all explored parameters are loaded!

        :param build_dict:

            Dictionary containing the expansion

        :param fail_safe:

            If old ranges should be **deep-copied** in order to allow to restore
            the original exploration if something fails during expansion.
            Set to `False` if deep-copying your parameter ranges causes errors.

        :raises:

            TypeError: If not all explored parameters are enlarged

            AttributeError: If keys of dictionary cannot be found in the trajectory

            NotUniqueNodeError:

                If dictionary keys do not unambiguously map to single parameters

            ValueError: If not all explored parameter ranges are of the same length

        """
        if len(self._explored_parameters) == 0:
            self._logger.info('Your trajectory has not been explored, yet. '
                              'I will call `f_explore` instead.')
            return self.f_explore(build_dict)

        enlarge_set = set([self.f_get(key).v_full_name
                           for key in build_dict.keys()])

        # Check if all explored parameters will be enlarged, otherwise
        # We cannot enlarge the trajectory
        if not set(self._explored_parameters.keys()) == enlarge_set:
            raise TypeError('You have to enlarge dimensions you have explored before! Currently'
                            ' explored parameters are not the ones you specified in your building'
                            ' dictionary, i.e. %s != %s' %
                            (str(set(self._explored_parameters.keys())),
                             str(set(build_dict.keys()))))
        if any(x is None for x in self._explored_parameters.values()):
            raise TypeError('At least one of your explored parameters is not fully loaded, '
                            'please load it.')

        old_ranges = None
        if fail_safe:
            old_ranges = {}
            for param_name in self._explored_parameters:
                old_ranges[param_name] = self._explored_parameters[param_name].f_get_range()
            try:
                old_ranges = cp.deepcopy(old_ranges)
            except Exception:
                self._logger.error('Cannot deepcopy old parameter ranges, if '
                                     'something fails during `f_expand` I cannot revert the '
                                     'trajectory to old settings.')
                old_ranges = None

        try:
            count = 0
            length = None
            for key, builditerable in build_dict.items():
                act_param = self.f_get(key)

                act_param.f_unlock()
                act_param._expand(builditerable)

                name = act_param.v_full_name

                self._explored_parameters[name] = act_param

                # Compare the length of two consecutive parameters in the `build_dict`
                if count == 0:
                    length = act_param.f_get_range_length()
                elif not length == act_param.f_get_range_length():
                    raise ValueError('The parameters to explore have not the same size!')
                count += 1

            original_length = len(self)
            for irun in range(original_length, length):
                self._add_run_info(irun)
            self._test_run_addition(length)

            # We need to update the explored parameters in case they were stored:
            self._remove_exploration()

        except Exception:
            if old_ranges is not None:
                # Try to restore the original parameter exploration
                for param_name in old_ranges:
                    param_range = old_ranges[param_name]
                    param = self._explored_parameters[param_name]
                    param.f_unlock()
                    try:
                        param._shrink()
                    except Exception as exc:
                        self._logger.error('Could not shrink parameter `%s` '
                                           'because of:`%s`' % (param_name, repr(exc)))
                    param._explore(param_range)
                    param._explored = True
            raise
Similar to :func:`~pypet.trajectory.Trajectory.f_explore`, but can be used to enlarge
        already completed trajectories.

        Please ensure before usage, that all explored parameters are loaded!

        :param build_dict:

            Dictionary containing the expansion

        :param fail_safe:

            If old ranges should be **deep-copied** in order to allow to restore
            the original exploration if something fails during expansion.
            Set to `False` if deep-copying your parameter ranges causes errors.

        :raises:

            TypeError: If not all explored parameters are enlarged

            AttributeError: If keys of dictionary cannot be found in the trajectory

            NotUniqueNodeError:

                If dictionary keys do not unambiguously map to single parameters

            ValueError: If not all explored parameter ranges are of the same length