pypet/trajectory.py
def _copy_from(self, node,
                          copy_leaves=True,
                          overwrite=False,
                          with_links=True):
        """Pass a ``node`` to insert the full tree to the trajectory.

        Considers all links in the given node!
        Ignored nodes already found in the current trajectory.

        :param node: The node to insert

        :param copy_leaves:

            If leaves should be **shallow** copied or simply referred to by both trees.
            **Shallow** copying is established using the copy module.

            Accepts the setting ``'explored'`` to only copy explored parameters.
            Note that ``v_full_copy`` determines how these will be copied.

        :param overwrite:

            If existing elemenst should be overwritten. Requries ``__getstate__`` and
            ``__setstate__`` being implemented in the leaves.

        :param with_links: If links should be ignored or followed and copied as well

        :return: The corresponding (new) node in the tree.

        """

        def _copy_skeleton(node_in, node_out):
            """Copies the skeleton of from `node_out` to `node_in`"""
            new_annotations = node_out.v_annotations
            node_in._annotations = new_annotations
            node_in.v_comment = node_out.v_comment

        def _add_leaf(leaf):
            """Adds a leaf to the trajectory"""
            leaf_full_name = leaf.v_full_name
            try:
                found_leaf = self.f_get(leaf_full_name,
                                        with_links=False,
                                        shortcuts=False,
                                        auto_load=False)
                if overwrite:
                    found_leaf.__setstate__(leaf.__getstate__())
                return found_leaf
            except AttributeError:
                pass
            if copy_leaves is True or (copy_leaves == 'explored' and
                                           leaf.v_is_parameter and leaf.v_explored):
                new_leaf = self.f_add_leaf(cp.copy(leaf))
            else:
                new_leaf = self.f_add_leaf(leaf)
            if new_leaf.v_is_parameter and new_leaf.v_explored:
                self._explored_parameters[new_leaf.v_full_name] = new_leaf
            return new_leaf

        def _add_group(group):
            """Adds a new group to the trajectory"""
            group_full_name = group.v_full_name
            try:
                found_group = self.f_get(group_full_name,
                                             with_links=False,
                                             shortcuts=False,
                                             auto_load=False)
                if overwrite:
                    _copy_skeleton(found_group, group)
                return found_group
            except AttributeError:
                pass
            new_group = self.f_add_group(group_full_name)
            _copy_skeleton(new_group, group)
            return new_group

        is_run = self._is_run
        self._is_run = False  # So that we can copy Config Groups and Config Data
        try:
            if node.v_is_leaf:
                return _add_leaf(node)
            elif node.v_is_group:
                other_root = node.v_root
                if other_root is self:
                    raise RuntimeError('You cannot copy a given tree to itself!')
                result = _add_group(node)
                nodes_iterator = node.f_iter_nodes(recursive=True, with_links=with_links)
                has_links = []
                if node._links:
                    has_links.append(node)
                for child in nodes_iterator:
                    if child.v_is_leaf:
                        _add_leaf(child)
                    else:
                        _add_group(child)
                        if child._links:
                            has_links.append(child)

                if with_links:
                    for current in has_links:
                        mine = self.f_get(current.v_full_name, with_links=False,
                                          shortcuts=False, auto_load=False)
                        my_link_set = set(mine._links.keys())
                        other_link_set = set(current._links.keys())
                        new_links = other_link_set - my_link_set
                        for link in new_links:
                            where_full_name = current._links[link].v_full_name
                            mine.f_add_link(link, where_full_name)

                return result
            else:
                raise RuntimeError('You shall not pass!')
        except Exception:
            self._is_run = is_run
def _copy_from(self, node,
                          copy_leaves=True,
                          overwrite=False,
                          with_links=True):
        """Pass a ``node`` to insert the full tree to the trajectory.

        Considers all links in the given node!
        Ignored nodes already found in the current trajectory.

        :param node: The node to insert

        :param copy_leaves:

            If leaves should be **shallow** copied or simply referred to by both trees.
            **Shallow** copying is established using the copy module.

            Accepts the setting ``'explored'`` to only copy explored parameters.
            Note that ``v_full_copy`` determines how these will be copied.

        :param overwrite:

            If existing elemenst should be overwritten. Requries ``__getstate__`` and
            ``__setstate__`` being implemented in the leaves.

        :param with_links: If links should be ignored or followed and copied as well

        :return: The corresponding (new) node in the tree.

        """

        def _copy_skeleton(node_in, node_out):
            """Copies the skeleton of from `node_out` to `node_in`"""
            new_annotations = node_out.v_annotations
            node_in._annotations = new_annotations
            node_in.v_comment = node_out.v_comment

        def _add_leaf(leaf):
            """Adds a leaf to the trajectory"""
            leaf_full_name = leaf.v_full_name
            try:
                found_leaf = self.f_get(leaf_full_name,
                                        with_links=False,
                                        shortcuts=False,
                                        auto_load=False)
                if overwrite:
                    found_leaf.__setstate__(leaf.__getstate__())
                return found_leaf
            except AttributeError:
                pass
            if copy_leaves is True or (copy_leaves == 'explored' and
                                           leaf.v_is_parameter and leaf.v_explored):
                new_leaf = self.f_add_leaf(cp.copy(leaf))
            else:
                new_leaf = self.f_add_leaf(leaf)
            if new_leaf.v_is_parameter and new_leaf.v_explored:
                self._explored_parameters[new_leaf.v_full_name] = new_leaf
            return new_leaf

        def _add_group(group):
            """Adds a new group to the trajectory"""
            group_full_name = group.v_full_name
            try:
                found_group = self.f_get(group_full_name,
                                             with_links=False,
                                             shortcuts=False,
                                             auto_load=False)
                if overwrite:
                    _copy_skeleton(found_group, group)
                return found_group
            except AttributeError:
                pass
            new_group = self.f_add_group(group_full_name)
            _copy_skeleton(new_group, group)
            return new_group

        is_run = self._is_run
        self._is_run = False  # So that we can copy Config Groups and Config Data
        try:
            if node.v_is_leaf:
                return _add_leaf(node)
            elif node.v_is_group:
                other_root = node.v_root
                if other_root is self:
                    raise RuntimeError('You cannot copy a given tree to itself!')
                result = _add_group(node)
                nodes_iterator = node.f_iter_nodes(recursive=True, with_links=with_links)
                has_links = []
                if node._links:
                    has_links.append(node)
                for child in nodes_iterator:
                    if child.v_is_leaf:
                        _add_leaf(child)
                    else:
                        _add_group(child)
                        if child._links:
                            has_links.append(child)

                if with_links:
                    for current in has_links:
                        mine = self.f_get(current.v_full_name, with_links=False,
                                          shortcuts=False, auto_load=False)
                        my_link_set = set(mine._links.keys())
                        other_link_set = set(current._links.keys())
                        new_links = other_link_set - my_link_set
                        for link in new_links:
                            where_full_name = current._links[link].v_full_name
                            mine.f_add_link(link, where_full_name)

                return result
            else:
                raise RuntimeError('You shall not pass!')
        except Exception:
            self._is_run = is_run
Pass a ``node`` to insert the full tree to the trajectory.

        Considers all links in the given node!
        Ignored nodes already found in the current trajectory.

        :param node: The node to insert

        :param copy_leaves:

            If leaves should be **shallow** copied or simply referred to by both trees.
            **Shallow** copying is established using the copy module.

            Accepts the setting ``'explored'`` to only copy explored parameters.
            Note that ``v_full_copy`` determines how these will be copied.

        :param overwrite:

            If existing elemenst should be overwritten. Requries ``__getstate__`` and
            ``__setstate__`` being implemented in the leaves.

        :param with_links: If links should be ignored or followed and copied as well

        :return: The corresponding (new) node in the tree.