Example #1
0
 def __init__(cls, name, bases, dict):
     
     for field in ATOMIC_FIELDS.values():
         
         if field.private:
             continue
         
         meth = field.meth
         getMeth = 'get' + meth
         setMeth = 'set' + meth
         # Define public method for retrieving a copy of data array
         if field.call:
             def getData(self, var=field.var, call=field.call):
                 if self._ag._data[var] is None:
                     [getattr(self._ag, meth)() for meth in call]
                 return self._ag._data[var][self._index] 
         else:
             def getData(self, var=field.var):
                 array = self._ag._data[var]
                 if array is None:
                     return None
                 return array[self._index] 
         getData = wrapGetMethod(getData)
         getData.__name__ = getMeth
         getData.__doc__ = field.getDocstr('get', False)
         setattr(cls, getMeth, getData)
         setattr(cls, '_' + getMeth, getData)
         
         if field.readonly:
             continue
         
         # Define public method for setting values in data array
         def setData(self, value, var=field.var, none=field.none):
             array = self._ag._data[var]
             if array is None:
                 raise AttributeError('attribute of the AtomGroup is '
                                      'not set')
             array[self._index] = value
             if none:
                 [self._ag.__setattr__(nm,  None) if nm[0] == '_' else
                  self._ag._data.__setitem__(nm,  None) for nm in none]
         setData = wrapSetMethod(setData)
         setData.__name__ = setMeth 
         setData.__doc__ = field.getDocstr('set', False)
         setattr(cls, setMeth, setData)
Example #2
0
    def __init__(cls, name, bases, dict):

        for field in ATOMIC_DATA_FIELDS.values():
            meth = field.meth_pl
            getMeth = 'get' + meth
            setMeth = 'set' + meth
            # Define public method for retrieving a copy of data array
            if field.call:
                def getData(self, var=field.var, call=field.call):
                    for meth in call:
                        getattr(self._ag, meth)()
                    array = self._ag._data[var]
                    return array[self._indices]
            else:
                def getData(self, var=field.var):
                    array = self._ag._data[var]
                    if array is None:
                        return None
                    return array[self._indices] 
            getData = wrapGetMethod(getData)
            getData.__name__ = getMeth
            getData.__doc__ = field.getDocstr('get')
            setattr(cls, getMeth, getData)
            setattr(cls, '_' + getMeth, getData)
            
            if field.readonly:
                continue
            
            # Define public method for setting values in data array
            def setData(self, value, var=field.var, none=field.none):
                array = self._ag._data[var]
                if array is None:
                    raise AttributeError(var + ' data is not set')
                array[self._indices] = value
                if none:
                    self._ag.__setattr__('_'+none,  None)
            setData = wrapSetMethod(setData)
            setData.__name__ = setMeth 
            setData.__doc__ = field.getDocstr('set')  
            setattr(cls, setMeth, setData)
Example #3
0
    def __init__(cls, name, bases, dict):
    
        for field in ATOMIC_DATA_FIELDS.values():

            meth = field.meth_pl
            getMeth = 'get' + meth
            setMeth = 'set' + meth
            # Define public method for retrieving a copy of data array
            if field.call:
                def getData(self, var=field.var, call=field.call):
                    for meth in call:
                        getattr(self, meth)()
                    array = self._data[var]
                    return array.copy()                 
            else:
                def getData(self, var=field.var):
                    array = self._data[var]
                    if array is None:
                        return None
                    return array.copy() 
            getData = wrapGetMethod(getData)
            getData.__name__ = getMeth
            getData.__doc__ = field.getDocstr('get')
            setattr(cls, getMeth, getData)
            
            # Define private method for retrieving actual data array
            def _getData(self, var=field.var):
                return self._data[var]
            _getData = wrapGetMethod(_getData)
            _getData.__name__ = '_' + getMeth
            _getData.__doc__ = field.getDocstr('_get')
            setattr(cls, '_' + getMeth, _getData)
            
            if field.readonly:
                continue
            
            # Define public method for setting values in data array
            def setData(self, array, var=field.var, dtype=field.dtype, 
                        ndim=field.ndim, none=field.none):
                if self._n_atoms == 0:
                    self._n_atoms = len(array)
                elif len(array) != self._n_atoms:
                    raise ValueError('length of array must match numAtoms')
                    
                if isinstance(array, list):
                    array = np.array(array, dtype)
                elif not isinstance(array, np.ndarray):
                    raise TypeError('array must be an ndarray or a list')
                elif array.ndim != ndim:
                        raise ValueError('array must be {0:d} dimensional'
                                         .format(ndim))
                elif array.dtype != dtype:
                    try:
                        array = array.astype(dtype)
                    except ValueError:
                        raise ValueError('array cannot be assigned type '
                                         '{0:s}'.format(dtype))
                self._data[var] = array
                if none:
                    self.__setattr__('_'+none,  None)
            setData = wrapSetMethod(setData)
            setData.__name__ = setMeth 
            setData.__doc__ = field.getDocstr('set')
            setattr(cls, setMeth, setData)