Ejemplo n.º 1
0
 def isValid(self):
     # Valid dimensions?
     if (not (len(self._dimensions)==1
              and isinstance(self._dimensions[0], IndexMarker)) #IndexMarkers are valid and...
         and not (self.data.shape == (1,) and len(self._dimensions)==0)): #...so are zero dim fields.
         dimshape = []
         for d in self._dimensions:
             if len(d.data.shape)>1:
                 _logger.debug("Dimension %s is not 1-d." % d.longname)
                 return False
             dimshape.append(d.data.shape[0])
         if self.data.shape!=tuple(dimshape):
             _logger.debug("Shape of data %s and of dimensions %s do not match for field\n:%s" %
                           (self.data.shape, dimshape, self))
             return False
         for d in self._dimensions:
             if not d.isValid():
                 _logger.debug("Invalid dimension %s."%d.longname)
                 return False
     # Valid mask?
     if (self.mask!=None) and (self.data.shape!=self.mask.shape):
         _logger.debug("Shape of data %s and of mask %s do not match."%(self.data.shape, self.mask.shape))
         return False
     # Valid error?
     if (self.error!=None) and (self.data.shape!=self.error.shape):
         _logger.debug("Shape of data %s and of error %s do not match."%(self.data.shape, self.error.shape))
         return False
     return True
Ejemplo n.º 2
0
 def isValid(self):
     # Valid dimensions?
     if (not (len(self._dimensions) == 1
              and isinstance(self._dimensions[0], IndexMarker)
              )  #IndexMarkers are valid and...
             and not (self.data.shape == (1, ) and len(self._dimensions)
                      == 0)):  #...so are zero dim fields.
         dimshape = []
         for d in self._dimensions:
             if len(d.data.shape) > 1:
                 _logger.debug("Dimension %s is not 1-d." % d.longname)
                 return False
             dimshape.append(d.data.shape[0])
         if self.data.shape != tuple(dimshape):
             _logger.debug(
                 "Shape of data %s and of dimensions %s do not match for field\n:%s"
                 % (self.data.shape, dimshape, self))
             return False
         for d in self._dimensions:
             if not d.isValid():
                 _logger.debug("Invalid dimension %s." % d.longname)
                 return False
     # Valid mask?
     if (self.mask != None) and (self.data.shape != self.mask.shape):
         _logger.debug("Shape of data %s and of mask %s do not match." %
                       (self.data.shape, self.mask.shape))
         return False
     # Valid error?
     if (self.error != None) and (self.data.shape != self.error.shape):
         _logger.debug("Shape of data %s and of error %s do not match." %
                       (self.data.shape, self.error.shape))
         return False
     return True
Ejemplo n.º 3
0
 def __eq__(self, other, rtol=1e-5, atol=1e-8):
     if type(self) != type(other):
         if type(other) != IndexMarker and type(other) != NoneType:
             _logger.debug('Cannot compare objects with different type (%s and %s).' % (type(self),type(other)))
         return False
     if not (self.typeString == other.typeString):
         _logger.debug('The typeString is not identical.')
         return False
     if (self.mask==None) and (other.mask!=None):
         _logger.debug('The mask of the first field container has not been set, while the mask of the second field container is set to %s.' % other.mask)
         return False
     elif  self.mask!=None and (other.mask==None):
         _logger.debug('The mask of the second field container has not been set, while the mask of the first field container is set to %s.' % self.mask)
         return False
     if not (numpy.alltrue(self.mask==other.mask)):
         _logger.debug('The masks are not identical: %s\n%s' % (self.mask,other.mask))
         return False
     if self.mask!=None:
         data = self.data[numpy.logical_not(self.mask)]
         otherData = other.data[numpy.logical_not(other.mask)]
         if self.error!=None:
             error = self.error[numpy.logical_not(self.mask)]
         else:
             error = self.error
         if other.error!=None:
             otherError = other.error[numpy.logical_not(other.mask)]
         else:
             otherError = other.error
     else:
         data = self.data
         error = self.error
         otherData = other.data
         otherError = other.error
     if (isQuantity(self.unit) or isQuantity(other.unit)):
         try:
             if not (self.unit.inBaseUnits().unit == other.unit.inBaseUnits().unit):
                 _logger.debug('The units are different.')
                 return False
         except AttributeError:
             _logger.debug('Cannot compare unit with normed quantity: %s, %s' % (self.unit,other.unit))
             return False
         try:
             scaledData = data*self.unit.value
             scaledOtherData = otherData*other.unit.inUnitsOf(self.unit.unit).value
             if not numpy.allclose(scaledData,scaledOtherData,rtol,atol):
                 if numpy.sometrue(numpy.isnan(scaledData)):
                     _logger.debug('The fields cannot be compared, because some elements of the first field are NaN and the mask has not been set.')
                 if numpy.sometrue(numpy.isnan(scaledOtherData)):
                     _logger.debug('The fields cannot be compared, because some elements of the second field are NaN and the mask has not been set.')
                 else:
                     difference = numpy.abs(scaledData-scaledOtherData)
                     _logger.debug('The scaled fields differ, data-otherData: %s\n%s\n%s' % (difference.max(),
                                                                                    scaledData,
                                                                                    scaledOtherData))
                 return False
         except ValueError:
             _logger.debug('Shape mismatch: %s != %s' % (self.data.shape,other.data.shape))
             return False
         if error!=None:
             scaledError = error*self.unit.value
             if otherError!=None:
                 otherScaledError = otherError*other.unit.inUnitsOf(self.unit.unit).value
             else:
                 _logger.debug('The errors differ: The error of the second argument is none, while the error of the first argument is %s.' % error)
                 return False
             if not numpy.allclose(scaledError,otherScaledError,rtol,atol):
                 _logger.debug('The normed errors differ: %s\n%s' % (scaledError,otherScaledError))
                 return False
     else:
         if not data.dtype.char in ['S','U']:
             try:
                 scaledData = data*self.unit
                 scaledOtherData = otherData*other.unit
                 if not numpy.allclose(scaledData,scaledOtherData,rtol,atol):
                     _logger.debug('The scaled fields differ: %s\n%s'%(scaledData,scaledOtherData))
                     return False
             except ValueError:
                 _logger.debug('Shape mismatch: %s != %s' % (self.data.shape,other.data.shape))
                 return False
             if error==None:
                 if not (otherError==None):
                     _logger.debug('The errors differ: Error of first argument is None, but the error of the second argument is not None.')
                     return False
             else:
                 scaledError = error*self.unit
                 otherScaledError = otherError*other.unit
                 if not numpy.allclose(scaledError,otherScaledError,rtol,atol):
                     _logger.debug('The errors differ: %s\n%s' % (scaledError,otherScaledError))
                     return False
     if not self.attributes == other.attributes:
         _logger.debug('The attribute dictionary differs.')
         return False
     for dimSelf,dimOther in zip(self._dimensions,other.dimensions):
         if dimSelf != dimOther:
             _logger.debug('Different dimensions: %s, %s' % (dimSelf,dimOther))
             return False
     return True
Ejemplo n.º 4
0
 def __eq__(self, other, rtol=1e-5, atol=1e-8):
     if type(self) != type(other):
         if type(other) != IndexMarker and type(other) != NoneType:
             _logger.debug(
                 'Cannot compare objects with different type (%s and %s).' %
                 (type(self), type(other)))
         return False
     if not (self.typeString == other.typeString):
         _logger.debug('The typeString is not identical.')
         return False
     if (self.mask == None) and (other.mask != None):
         _logger.debug(
             'The mask of the first field container has not been set, while the mask of the second field container is set to %s.'
             % other.mask)
         return False
     elif self.mask != None and (other.mask == None):
         _logger.debug(
             'The mask of the second field container has not been set, while the mask of the first field container is set to %s.'
             % self.mask)
         return False
     if not (numpy.alltrue(self.mask == other.mask)):
         _logger.debug('The masks are not identical: %s\n%s' %
                       (self.mask, other.mask))
         return False
     if self.mask != None:
         data = self.data[numpy.logical_not(self.mask)]
         otherData = other.data[numpy.logical_not(other.mask)]
         if self.error != None:
             error = self.error[numpy.logical_not(self.mask)]
         else:
             error = self.error
         if other.error != None:
             otherError = other.error[numpy.logical_not(other.mask)]
         else:
             otherError = other.error
     else:
         data = self.data
         error = self.error
         otherData = other.data
         otherError = other.error
     if (isQuantity(self.unit) or isQuantity(other.unit)):
         try:
             if not (self.unit.inBaseUnits().unit
                     == other.unit.inBaseUnits().unit):
                 _logger.debug('The units are different.')
                 return False
         except AttributeError:
             _logger.debug(
                 'Cannot compare unit with normed quantity: %s, %s' %
                 (self.unit, other.unit))
             return False
         try:
             scaledData = data * self.unit.value
             scaledOtherData = otherData * other.unit.inUnitsOf(
                 self.unit.unit).value
             if not numpy.allclose(scaledData, scaledOtherData, rtol, atol):
                 if numpy.sometrue(numpy.isnan(scaledData)):
                     _logger.debug(
                         'The fields cannot be compared, because some elements of the first field are NaN and the mask has not been set.'
                     )
                 if numpy.sometrue(numpy.isnan(scaledOtherData)):
                     _logger.debug(
                         'The fields cannot be compared, because some elements of the second field are NaN and the mask has not been set.'
                     )
                 else:
                     difference = numpy.abs(scaledData - scaledOtherData)
                     _logger.debug(
                         'The scaled fields differ, data-otherData: %s\n%s\n%s'
                         % (difference.max(), scaledData, scaledOtherData))
                 return False
         except ValueError:
             _logger.debug('Shape mismatch: %s != %s' %
                           (self.data.shape, other.data.shape))
             return False
         if error != None:
             scaledError = error * self.unit.value
             if otherError != None:
                 otherScaledError = otherError * other.unit.inUnitsOf(
                     self.unit.unit).value
             else:
                 _logger.debug(
                     'The errors differ: The error of the second argument is none, while the error of the first argument is %s.'
                     % error)
                 return False
             if not numpy.allclose(scaledError, otherScaledError, rtol,
                                   atol):
                 _logger.debug('The normed errors differ: %s\n%s' %
                               (scaledError, otherScaledError))
                 return False
     else:
         if not data.dtype.char in ['S', 'U']:
             try:
                 scaledData = data * self.unit
                 scaledOtherData = otherData * other.unit
                 if not numpy.allclose(scaledData, scaledOtherData, rtol,
                                       atol):
                     _logger.debug('The scaled fields differ: %s\n%s' %
                                   (scaledData, scaledOtherData))
                     return False
             except ValueError:
                 _logger.debug('Shape mismatch: %s != %s' %
                               (self.data.shape, other.data.shape))
                 return False
             if error == None:
                 if not (otherError == None):
                     _logger.debug(
                         'The errors differ: Error of first argument is None, but the error of the second argument is not None.'
                     )
                     return False
             else:
                 scaledError = error * self.unit
                 otherScaledError = otherError * other.unit
                 if not numpy.allclose(scaledError, otherScaledError, rtol,
                                       atol):
                     _logger.debug('The errors differ: %s\n%s' %
                                   (scaledError, otherScaledError))
                     return False
     if not self.attributes == other.attributes:
         _logger.debug('The attribute dictionary differs.')
         return False
     for dimSelf, dimOther in zip(self._dimensions, other.dimensions):
         if dimSelf != dimOther:
             _logger.debug('Different dimensions: %s, %s' %
                           (dimSelf, dimOther))
             return False
     return True