pypet/trajectory.py
def _check_if_both_have_same_parameters(self, other_trajectory,
                                            ignore_data, consecutive_merge):
        """ Checks if two trajectories live in the same space and can be merged. """

        if not isinstance(other_trajectory, Trajectory):
            raise TypeError('Can only merge trajectories, the other trajectory'
                            ' is of type `%s`.' % str(type(other_trajectory)))

        if self._stored and not consecutive_merge:
            self.f_load_skeleton()
        if other_trajectory._stored:
            other_trajectory.f_load_skeleton()

        # Check wildcard set
        other_wildcard_set = set(x[1] for x in other_trajectory._wildcard_functions.keys())
        wildcard_set = set(x[1] for x in self._wildcard_functions.keys())
        diff = wildcard_set.symmetric_difference(other_wildcard_set)
        if diff:
            raise TypeError('The wildcard sets are not matching. `%s` != `%s`' %
                            (str(wildcard_set), str(other_wildcard_set)))

        # Load all parameters of the current and the other trajectory
        if self._stored:
            # To suppress warnings if nothing needs to be loaded
            with self._nn_interface._disable_logging:
                self.f_load_items(self._parameters.keys(), only_empties=True)
        if other_trajectory._stored:
            with self._nn_interface._disable_logging:
                other_trajectory.f_load_items(other_trajectory._parameters.keys(),
                                              only_empties=True)

        self.f_restore_default()
        other_trajectory.f_restore_default()

        allmyparams = self._parameters.copy()
        allotherparams = other_trajectory._parameters.copy()

        # If not ignored, add also the trajectory derived parameters to check for merging
        if 'derived_parameters' in self:
            my_traj_dpars = self._derived_parameters
            if self._stored:
                with self._nn_interface._disable_logging:
                    self.f_load_items(my_traj_dpars.keys(), only_empties=True)
            allmyparams.update(my_traj_dpars)
            other_traj_dpars = other_trajectory._derived_parameters
            if other_trajectory._stored:
                with self._nn_interface._disable_logging:
                    other_trajectory.f_load_items(other_traj_dpars.keys(), only_empties=True)
            allotherparams.update(other_traj_dpars)

        # Check if the trajectories have the same parameters:
        my_keyset = set(allmyparams.keys())
        other_keyset = set(allotherparams.keys())
        diff = my_keyset.symmetric_difference(other_keyset) - ignore_data
        run_dummys = (self.f_wildcard('$', -1), other_trajectory.f_wildcard('$', -1))
        if diff:
            run_difference_can_be_resolved = True
            for full_name in diff:
                split_name = full_name.split('.')
                if not any(x in self._run_information or
                                x in other_trajectory._run_information or
                                    x in run_dummys
                                        for x in split_name):
                    run_difference_can_be_resolved = False
                    break
                elif full_name in allotherparams:
                    del allotherparams[full_name]
            if not run_difference_can_be_resolved:
                raise TypeError('Cannot merge trajectories, '
                                'they do not live in the same space,the '
                                'set of parameters `%s` is only '
                                'found in one trajectory.' % str(diff))

        # Check if corresponding parameters in both trajectories are of the same type
        for key, other_param in allotherparams.items():
            if key in ignore_data:
                continue
            my_param = self.f_get(key)
            split_key = key.split('.')
            if any(x in self._run_information or
                        x in other_trajectory._run_information
                            for x in split_key):
                pass
            else:
                if not my_param._values_of_same_type(my_param.f_get(), other_param.f_get()):
                    raise TypeError('Cannot merge trajectories, values of parameters `%s` are not '
                                    'of the same type. Types are %s (current) and %s (other).' %
                                    (key, str(type(my_param.f_get())),
                                     str(type(other_param.f_get()))))
def _check_if_both_have_same_parameters(self, other_trajectory,
                                            ignore_data, consecutive_merge):
        """ Checks if two trajectories live in the same space and can be merged. """

        if not isinstance(other_trajectory, Trajectory):
            raise TypeError('Can only merge trajectories, the other trajectory'
                            ' is of type `%s`.' % str(type(other_trajectory)))

        if self._stored and not consecutive_merge:
            self.f_load_skeleton()
        if other_trajectory._stored:
            other_trajectory.f_load_skeleton()

        # Check wildcard set
        other_wildcard_set = set(x[1] for x in other_trajectory._wildcard_functions.keys())
        wildcard_set = set(x[1] for x in self._wildcard_functions.keys())
        diff = wildcard_set.symmetric_difference(other_wildcard_set)
        if diff:
            raise TypeError('The wildcard sets are not matching. `%s` != `%s`' %
                            (str(wildcard_set), str(other_wildcard_set)))

        # Load all parameters of the current and the other trajectory
        if self._stored:
            # To suppress warnings if nothing needs to be loaded
            with self._nn_interface._disable_logging:
                self.f_load_items(self._parameters.keys(), only_empties=True)
        if other_trajectory._stored:
            with self._nn_interface._disable_logging:
                other_trajectory.f_load_items(other_trajectory._parameters.keys(),
                                              only_empties=True)

        self.f_restore_default()
        other_trajectory.f_restore_default()

        allmyparams = self._parameters.copy()
        allotherparams = other_trajectory._parameters.copy()

        # If not ignored, add also the trajectory derived parameters to check for merging
        if 'derived_parameters' in self:
            my_traj_dpars = self._derived_parameters
            if self._stored:
                with self._nn_interface._disable_logging:
                    self.f_load_items(my_traj_dpars.keys(), only_empties=True)
            allmyparams.update(my_traj_dpars)
            other_traj_dpars = other_trajectory._derived_parameters
            if other_trajectory._stored:
                with self._nn_interface._disable_logging:
                    other_trajectory.f_load_items(other_traj_dpars.keys(), only_empties=True)
            allotherparams.update(other_traj_dpars)

        # Check if the trajectories have the same parameters:
        my_keyset = set(allmyparams.keys())
        other_keyset = set(allotherparams.keys())
        diff = my_keyset.symmetric_difference(other_keyset) - ignore_data
        run_dummys = (self.f_wildcard('$', -1), other_trajectory.f_wildcard('$', -1))
        if diff:
            run_difference_can_be_resolved = True
            for full_name in diff:
                split_name = full_name.split('.')
                if not any(x in self._run_information or
                                x in other_trajectory._run_information or
                                    x in run_dummys
                                        for x in split_name):
                    run_difference_can_be_resolved = False
                    break
                elif full_name in allotherparams:
                    del allotherparams[full_name]
            if not run_difference_can_be_resolved:
                raise TypeError('Cannot merge trajectories, '
                                'they do not live in the same space,the '
                                'set of parameters `%s` is only '
                                'found in one trajectory.' % str(diff))

        # Check if corresponding parameters in both trajectories are of the same type
        for key, other_param in allotherparams.items():
            if key in ignore_data:
                continue
            my_param = self.f_get(key)
            split_key = key.split('.')
            if any(x in self._run_information or
                        x in other_trajectory._run_information
                            for x in split_key):
                pass
            else:
                if not my_param._values_of_same_type(my_param.f_get(), other_param.f_get()):
                    raise TypeError('Cannot merge trajectories, values of parameters `%s` are not '
                                    'of the same type. Types are %s (current) and %s (other).' %
                                    (key, str(type(my_param.f_get())),
                                     str(type(other_param.f_get()))))
def _check_if_both_have_same_parameters ( self , other_trajectory , ignore_data , consecutive_merge ) : if not isinstance ( other_trajectory , Trajectory ) : raise TypeError ( 'Can only merge trajectories, the other trajectory' ' is of type `%s`.' % str ( type ( other_trajectory ) ) ) if self . _stored and not consecutive_merge : self . f_load_skeleton ( ) if other_trajectory . _stored : other_trajectory . f_load_skeleton ( ) # Check wildcard set other_wildcard_set = set ( x [ 1 ] for x in other_trajectory . _wildcard_functions . keys ( ) ) wildcard_set = set ( x [ 1 ] for x in self . _wildcard_functions . keys ( ) ) diff = wildcard_set . symmetric_difference ( other_wildcard_set ) if diff : raise TypeError ( 'The wildcard sets are not matching. `%s` != `%s`' % ( str ( wildcard_set ) , str ( other_wildcard_set ) ) ) # Load all parameters of the current and the other trajectory if self . _stored : # To suppress warnings if nothing needs to be loaded with self . _nn_interface . _disable_logging : self . f_load_items ( self . _parameters . keys ( ) , only_empties = True ) if other_trajectory . _stored : with self . _nn_interface . _disable_logging : other_trajectory . f_load_items ( other_trajectory . _parameters . keys ( ) , only_empties = True ) self . f_restore_default ( ) other_trajectory . f_restore_default ( ) allmyparams = self . _parameters . copy ( ) allotherparams = other_trajectory . _parameters . copy ( ) # If not ignored, add also the trajectory derived parameters to check for merging if 'derived_parameters' in self : my_traj_dpars = self . _derived_parameters if self . _stored : with self . _nn_interface . _disable_logging : self . f_load_items ( my_traj_dpars . keys ( ) , only_empties = True ) allmyparams . update ( my_traj_dpars ) other_traj_dpars = other_trajectory . _derived_parameters if other_trajectory . _stored : with self . _nn_interface . _disable_logging : other_trajectory . f_load_items ( other_traj_dpars . keys ( ) , only_empties = True ) allotherparams . update ( other_traj_dpars ) # Check if the trajectories have the same parameters: my_keyset = set ( allmyparams . keys ( ) ) other_keyset = set ( allotherparams . keys ( ) ) diff = my_keyset . symmetric_difference ( other_keyset ) - ignore_data run_dummys = ( self . f_wildcard ( '$' , - 1 ) , other_trajectory . f_wildcard ( '$' , - 1 ) ) if diff : run_difference_can_be_resolved = True for full_name in diff : split_name = full_name . split ( '.' ) if not any ( x in self . _run_information or x in other_trajectory . _run_information or x in run_dummys for x in split_name ) : run_difference_can_be_resolved = False break elif full_name in allotherparams : del allotherparams [ full_name ] if not run_difference_can_be_resolved : raise TypeError ( 'Cannot merge trajectories, ' 'they do not live in the same space,the ' 'set of parameters `%s` is only ' 'found in one trajectory.' % str ( diff ) ) # Check if corresponding parameters in both trajectories are of the same type for key , other_param in allotherparams . items ( ) : if key in ignore_data : continue my_param = self . f_get ( key ) split_key = key . split ( '.' ) if any ( x in self . _run_information or x in other_trajectory . _run_information for x in split_key ) : pass else : if not my_param . _values_of_same_type ( my_param . f_get ( ) , other_param . f_get ( ) ) : raise TypeError ( 'Cannot merge trajectories, values of parameters `%s` are not ' 'of the same type. Types are %s (current) and %s (other).' % ( key , str ( type ( my_param . f_get ( ) ) ) , str ( type ( other_param . f_get ( ) ) ) ) )
Checks if two trajectories live in the same space and can be merged.
Checks if two trajectories live in the same space and can be merged .