def threshold(self, image, subscriber=0): th = float(self.paramThreshold.value) if self.paramUnit.value.lower() != 'ignore': from pyphant.quantities import Quantity, isQuantity try: unit = float(self.paramUnit.value) assert not isQuantity(image.unit) except ValueError: try: unit = Quantity(self.paramUnit.value) except TypeError: unit = Quantity(1.0, self.paramUnit.value) assert isQuantity(image.unit) assert unit.isCompatible(image.unit.unit) th *= unit / image.unit resultArray = scipy.where(image.data < th, ImageProcessing.FEATURE_COLOR, ImageProcessing.BACKGROUND_COLOR) result = DataContainer.FieldContainer(resultArray, dimensions=copy.deepcopy( image.dimensions), longname=u"Binary Image", shortname=u"B") result.seal() return result
def __call__(self, longname, column): tupples = filter(lambda c: type(c) == type((0,)), column) hasTupples = len(tupples) > 0 if hasTupples: tuppleLength = max(map(len, tupples)) if hasTupples: if tuppleLength == 2: indexDatum = 0 indexError = 1 if isQuantity(tupples[0][1]) and tupples[0][1].isCompatible('s'): shortname = 't_%i' % self.Nt self.Nt += 1 else: shortname = 'p_%i' % self.Np self.Np += 1 for i, element in enumerate(column): if not type(element) == type((0,)): column[i] = (numpy.NaN, None) elif tuppleLength == 3: shortname = tupples[0][0] indexDatum = 1 indexError = 2 for i, element in enumerate(column): if not type(element) == type((0,)): column[i] = (shortname, numpy.NaN, None) try: data = [element[indexDatum] for element in column] except: print longname, column import sys sys.exit(0) error = [element[indexError] for element in column] unitCandidates = [element.unit for element in data \ if isQuantity(element)] if len(unitCandidates) == 0: unit = 1.0 else: unit = unitCandidates[0] normation = lambda arg: self.norm(arg, unit) field = numpy.array(map(normation, data)) ErrorNormation = lambda arg: self.norm(arg, unit, error=True) result = DataContainer.FieldContainer(field, error=numpy.array(map(ErrorNormation, error)), mask=numpy.isnan(field), unit=Quantity(1.0, unit), shortname=shortname, longname=longname) else: #Joining lists of strings if type(column[0]) == type([]): firstElement = column[0][0] else: firstElement = column[0] if type(firstElement) in (type(''), type(u'')): for i in xrange(len(column)): if type(column[i]) == type([]): column[i] = ','.join(column[i]) result = DataContainer.FieldContainer(numpy.array(column), longname=longname) return result
def inUnitsOf(self, other): if not isQuantity(self.unit): if isQuantity(other.unit): raise ValueError( "Incompatible Units: " "self.unit = <%s>, other.unit = <%s>" % ( self.unit, other.unit ) ) factor = float(self.unit) / float(other.unit) elif not isQuantity(other.unit): raise ValueError( "Incompatible Units: " "self.unit = <%s>, other.unit = <%s>" % (self.unit, other.unit) ) else: if not self.unit.isCompatible(other.unit.unit): raise ValueError( "Incompatible Units: " "self.unit = <%s>, other.unit = <%s>" % ( self.unit, other.unit ) ) factor = self.unit.inUnitsOf( other.unit.unit ).value / other.unit.value newSelf = copy.deepcopy(self) newSelf.data *= factor if newSelf.error != None: newSelf.error *= factor newSelf.unit = copy.deepcopy(other.unit) return newSelf
def __sub__(self, other): if isinstance(other, FieldContainer): if self.error != None or other.error != None: error = other.error + self.error else: error = None if len(self._dimensions) != len(other.dimensions): return NotImplemented for i in xrange(len(self._dimensions)): if not self._dimensions[i] == other.dimensions[i]: return NotImplemented if isQuantity(self.unit): if not ( isQuantity(other.unit) and self.unit.isCompatible(other.unit.unit) ): return NotImplemented if self.unit >= other.unit: data = self.data - ( other.data * other.unit.value * other.unit.unit.conversionFactorTo(self.unit.unit) ) / self.unit.value unit = self.unit else: data = ( ( self.data * self.unit.value * self.unit.unit.conversionFactorTo(other.unit.unit) ) / other.unit.value ) - other.data unit = other.unit else: if isQuantity(other.unit): return NotImplemented data = (self.data * self.unit) - (other.data * other.unit) unit = 1.0 if self.mask == None: mask = other.mask elif other.mask == None: mask = self.mask else: mask = self.mask + other.mask longname = u"Difference of %s and %s." % ( self.longname, other.longname ) shortname = u"%s - %s" % (self.shortname, other.shortname) return FieldContainer(data, unit, error, mask, copy.deepcopy(self._dimensions), longname, shortname) return NotImplemented
def createFieldContainer(key, array): def longname2unit(name): reg = re.compile(r"\[([^]]*)\]") m = reg.search(name) if not m or m.group(1) == "counts" or m.group(1) == "": return 1.0 else: return quantities.Quantity('1 ' + str(m.group(1))) fieldUnit = longname2unit(key) if quantities.isQuantity(fieldUnit): fieldContainer = DataContainer.FieldContainer(numpy.array(array), unit=fieldUnit, longname=key) else: try: quantities = [ quantities.Quantity(string.encode('latin-1')) for string in array ] firstUnit = quantities[0].unit scaledArray = [ quant.inUnitsOf(firstUnit).value for quant in quantities ] fieldContainer = DataContainer.FieldContainer( numpy.array(scaledArray), unit='1. %s' % firstUnit.name(), longname=key) except: fieldContainer = DataContainer.FieldContainer(numpy.array(array), unit=fieldUnit, longname=key) return fieldContainer
def refreshParams(self, subscriber=None, field=None): if self.socketField.isFull() or field != None: try: templ = self.socketField.getResult(subscriber) except: templ = field if templ == self.oldField: return self.oldField = templ self._params = [] for i, dim in enumerate(templ.dimensions): if quantities.isQuantity(dim.unit): intStart = (dim.data.min() * dim.unit).value intEnd = (dim.data.max() * dim.unit).value unitname = dim.unit.unit.name() else: intStart = dim.data.min() * dim.unit intEnd = dim.data.max() * dim.unit unitname = '' param = ('dim%i'%i, "%s %s (index #0:%i):" % (dim.longname, dim.shortname, len(dim.data) - 1), "%.4f %s:%.4f %s" % (intStart, unitname, intEnd, unitname), None) self._params.append(param) self._params.reverse() self.initParams(self._params)
def rescale(self): if isQuantity(self.unit): oldUnit = self.unit.inBaseUnits() else: return #Compute decade of field and multiply it to oldUnit oldFieldAmplitude = max(abs(numpy.amax(self.data)), abs(numpy.amin(self.data))) oldUnit *= oldFieldAmplitude #Compute next lower decade decade = scipy.log10(oldUnit.value) newDecade = 10**(scipy.floor(decade)) #Find appropriate prefix baseUnit = oldUnit.unit.name() if baseUnit == 'm': prefixes = PREFIXES_METER else: prefixes = PREFIXES prefixCandidates = map(lambda i: (i[0], abs(i[1] - newDecade)), prefixes) optPrefix = min([prefix[1] for prefix in prefixCandidates]) newPrefix = filter(lambda prefix: prefix[1] == optPrefix, prefixCandidates)[0][0] newUnitName = newPrefix + baseUnit #Convert to new unit newUnit = oldUnit.inUnitsOf(newUnitName) unitAmplitude = newUnit.value if self.data.dtype.name.startswith('int'): self.unit = newUnit / oldFieldAmplitude return self.data *= unitAmplitude / oldFieldAmplitude self.unit = newUnit / unitAmplitude
def saveField(self, path): if not isQuantity(self.dataContainer.unit): self.ordinate = self.dataContainer.data * self.dataContainer.unit else: self.ordinate = (self.dataContainer.data * self.dataContainer.unit.value) self.abscissa = self.dataContainer.dimensions[0].data outData = scipy.transpose(scipy.array([self.abscissa, self.ordinate])) if path[-3:] == 'csv': outFile = file(path, 'wb') csvWriter = csv.writer(outFile, dialect='excel') csvWriter.writerow([ self.dataContainer.dimensions[0].label, self.dataContainer.label ]) csvWriter.writerows(outData.tolist()) else: outFile = file(path, 'w') outFile.write( str([ self.dataContainer.dimensions[0].label, self.dataContainer.label ]) + "\n") write_array(outFile, outData) outFile.close()
def saveField(self, path): if not isQuantity(self.dataContainer.unit): self.ordinate = self.dataContainer.data * self.dataContainer.unit else: self.ordinate = ( self.dataContainer.data * self.dataContainer.unit.value ) self.abscissa = self.dataContainer.dimensions[0].data outData = scipy.transpose(scipy.array([self.abscissa, self.ordinate])) if path[-3:] == 'csv': outFile = file(path, 'wb') csvWriter = csv.writer(outFile, dialect='excel') csvWriter.writerow( [self.dataContainer.dimensions[0].label, self.dataContainer.label] ) csvWriter.writerows(outData.tolist()) else: outFile = file(path, 'w') outFile.write( str([self.dataContainer.dimensions[0].label, self.dataContainer.label]) + "\n" ) write_array(outFile, outData) outFile.close()
def refreshParams(self, subscriber=None, field=None): if self.socketField.isFull() or field != None: try: templ = self.socketField.getResult(subscriber) except: templ = field if templ == self.oldField: return self.oldField = templ self._params = [] for i, dim in enumerate(templ.dimensions): if quantities.isQuantity(dim.unit): intStart = (dim.data.min() * dim.unit).value intEnd = (dim.data.max() * dim.unit).value unitname = dim.unit.unit.name() else: intStart = dim.data.min() * dim.unit intEnd = dim.data.max() * dim.unit unitname = '' param = ('dim%i' % i, "%s %s (index #0:%i):" % (dim.longname, dim.shortname, len(dim.data) - 1), "%.4f %s:%.4f %s" % (intStart, unitname, intEnd, unitname), None) self._params.append(param) self._params.reverse() self.initParams(self._params)
def createFieldContainer(key, array): def longname2unit(name): reg = re.compile(r"\[([^]]*)\]") m = reg.search(name) if not m or m.group(1) == "counts" or m.group(1) == "": return 1.0 else: return Quantity('1 ' + str(m.group(1))) fieldUnit = longname2unit(key) if isQuantity(fieldUnit): fieldContainer = DataContainer.FieldContainer( numpy.array(array), unit=fieldUnit, longname=key ) else: try: quantities = [ Quantity(string.encode('latin-1')) for string in array ] firstUnit = quantities[0].unit scaledArray = [ quant.inUnitsOf(firstUnit).value for quant in quantities ] fieldContainer = DataContainer.FieldContainer( numpy.array(scaledArray), unit='1. %s' % firstUnit.name(), longname=key ) except: fieldContainer = DataContainer.FieldContainer( numpy.array(array), unit=fieldUnit, longname=key ) return fieldContainer
def rescale(self): if isQuantity(self.unit): oldUnit = self.unit.inBaseUnits() else: return #Compute decade of field and multiply it to oldUnit oldFieldAmplitude = max(abs(numpy.amax(self.data)),abs(numpy.amin(self.data))) oldUnit *= oldFieldAmplitude #Compute next lower decade decade = scipy.log10(oldUnit.value) newDecade = 10**(scipy.floor(decade)) #Find appropriate prefix baseUnit=oldUnit.unit.name() if baseUnit == 'm': prefixes = PREFIXES_METER else: prefixes = PREFIXES prefixCandidates = map(lambda i: (i[0],abs(i[1]-newDecade)),prefixes) optPrefix = min([prefix[1] for prefix in prefixCandidates]) newPrefix = filter(lambda prefix: prefix[1]==optPrefix,prefixCandidates)[0][0] newUnitName = newPrefix+baseUnit #Convert to new unit newUnit = oldUnit.inUnitsOf(newUnitName) unitAmplitude = newUnit.value if self.data.dtype.name.startswith('int'): self.unit = newUnit/oldFieldAmplitude return self.data *= unitAmplitude/oldFieldAmplitude self.unit = newUnit/unitAmplitude
def _getShortLabel(self): if not isQuantity(self.unit) and self.unit == 1: if self.longname == 'index': label = u"%s $%s$" % (self.longname.title(),self.shortname) else: label = u"%s $%s$ / a.u." % (self.longname.title(),self.shortname) else: label = u"%s $%s$ / %s" % (self.longname.title(), self.shortname, self.unit) return label.replace('1.0 ',r'')#.replace('mu',u'\\textmu{}')
def format(val): if not isQuantity(val): if type(val) in (type(' '),type(u' ')): valstr = val else: valstr = "%.4g" % val else: valstr = "%.3f %s" % (val.value,val.unit.name()) return valstr
def __sub__(self, other): if isinstance(other, FieldContainer): if self.error != None or other.error != None: error = other.error + self.error else: error = None if len(self._dimensions) != len(other.dimensions): return NotImplemented for i in xrange(len(self._dimensions)): if not self._dimensions[i] == other.dimensions[i]: return NotImplemented if isQuantity(self.unit): if not (isQuantity(other.unit) and self.unit.isCompatible(other.unit.unit)): return NotImplemented if self.unit >= other.unit: data = self.data - (other.data * other.unit.value * other.unit.unit.conversionFactorTo( self.unit.unit)) / self.unit.value unit = self.unit else: data = ( (self.data * self.unit.value * self.unit.unit.conversionFactorTo(other.unit.unit)) / other.unit.value) - other.data unit = other.unit else: if isQuantity(other.unit): return NotImplemented data = (self.data * self.unit) - (other.data * other.unit) unit = 1.0 if self.mask == None: mask = other.mask elif other.mask == None: mask = self.mask else: mask = self.mask + other.mask longname = u"Difference of %s and %s." % (self.longname, other.longname) shortname = u"%s - %s" % (self.shortname, other.shortname) return FieldContainer(data, unit, error, mask, copy.deepcopy(self._dimensions), longname, shortname) return NotImplemented
def _getShortLabel(self): if not isQuantity(self.unit) and self.unit == 1: if self.longname == 'index': label = u"%s $%s$" % (self.longname.title(), self.shortname) else: label = u"%s $%s$ / a.u." % (self.longname.title(), self.shortname) else: label = u"%s $%s$ / %s" % (self.longname.title(), self.shortname, self.unit) return label.replace('1.0 ', r'') #.replace('mu',u'\\textmu{}')
def norm(self, datum, unit, error=False): if isQuantity(datum): try: return datum.inUnitsOf(unit).value except: raise ValueError, "The datum %s cannot be expressed \ in terms of %s." % (datum, unit) elif error: return 0.0 else: return numpy.NaN
def _formatUnit(self): unit = unicode(self.unit) try: if not isQuantity(self.unit) and self.unit == 1: return u'a.u.' except: # is this bug still present? # why catch everything? pass # just a ScientificPython bug unit = unit.replace(u'1.0 ', u'') #.replace(u'mu',u'\\textmu{}') if u' ' in unit or u'/' in unit or u'*' in unit: unit = u'(%s)' % (unit, ) return unit
def _formatUnit(self): unit = unicode(self.unit) try: if not isQuantity(self.unit) and self.unit == 1: return u'a.u.' except: # is this bug still present? # why catch everything? pass # just a ScientificPython bug unit = unit.replace(u'1.0 ', u'')#.replace(u'mu',u'\\textmu{}') if u' ' in unit or u'/' in unit or u'*' in unit: unit = u'(%s)' % (unit, ) return unit
def threshold(self, image, subscriber=0): th = float(self.paramThreshold.value) if self.paramUnit.value.lower() != 'ignore': from pyphant.quantities import Quantity, isQuantity try: unit = float(self.paramUnit.value) assert not isQuantity(image.unit) except ValueError: try: unit = Quantity(self.paramUnit.value) except TypeError: unit = Quantity(1.0, self.paramUnit.value) assert isQuantity(image.unit) assert unit.isCompatible(image.unit.unit) th *= unit / image.unit resultArray = scipy.where(image.data < th, ImageProcessing.FEATURE_COLOR, ImageProcessing.BACKGROUND_COLOR) result = DataContainer.FieldContainer(resultArray, dimensions=copy.deepcopy(image.dimensions), longname=u"Binary Image", shortname=u"B") result.seal() return result
def _getLabel(self): if len(self._dimensions)>0: shortnames = [dim.shortname for dim in self._dimensions] shortnames.reverse() dependency = '(%s)' % ','.join(shortnames) else: dependency = '' label = u"%s $%s%s$ / %s" % (self.longname.title(), self.shortname, dependency, self.unit) try: if not isQuantity(self.unit) and self.unit == 1: label = u"%s $%s%s$ / a.u." % (self.longname.title(),self.shortname,dependency) except: pass #just a ScientificPython bug return label.replace('1.0 ',r'')#.replace('mu',u'\\textmu{}')
def inUnitsOf(self, other): if not isQuantity(self.unit): if isQuantity(other.unit): raise ValueError( "Incompatible Units: self.unit = <%s>, other.unit = <%s>" % (self.unit, other.unit)) factor = float(self.unit) / float(other.unit) elif not isQuantity(other.unit): raise ValueError( "Incompatible Units: self.unit = <%s>, other.unit = <%s>" % (self.unit, other.unit)) else: if not self.unit.isCompatible(other.unit.unit): raise ValueError( "Incompatible Units: self.unit = <%s>, other.unit = <%s>" % (self.unit, other.unit)) factor = self.unit.inUnitsOf( other.unit.unit).value / other.unit.value newSelf = copy.deepcopy(self) newSelf.data *= factor if newSelf.error != None: newSelf.error *= factor newSelf.unit = copy.deepcopy(other.unit) return newSelf
def _getLabel(self): if len(self._dimensions) > 0: shortnames = [dim.shortname for dim in self._dimensions] shortnames.reverse() dependency = '(%s)' % ','.join(shortnames) else: dependency = '' label = u"%s $%s%s$ / %s" % (self.longname.title(), self.shortname, dependency, self.unit) try: if not isQuantity(self.unit) and self.unit == 1: label = u"%s $%s%s$ / a.u." % (self.longname.title(), self.shortname, dependency) except: pass #just a ScientificPython bug return label.replace('1.0 ', r'') #.replace('mu',u'\\textmu{}')
def __call__(self, longname, column): tupples = filter(lambda c: type(c) == type((0, )), column) hasTupples = len(tupples) > 0 if hasTupples: tuppleLength = max(map(len, tupples)) if hasTupples: if tuppleLength == 2: indexDatum = 0 indexError = 1 if isQuantity( tupples[0][1]) and tupples[0][1].isCompatible('s'): shortname = 't_%i' % self.Nt self.Nt += 1 else: shortname = 'p_%i' % self.Np self.Np += 1 for i, element in enumerate(column): if not type(element) == type((0, )): column[i] = (numpy.NaN, None) elif tuppleLength == 3: shortname = tupples[0][0] indexDatum = 1 indexError = 2 for i, element in enumerate(column): if not type(element) == type((0, )): column[i] = (shortname, numpy.NaN, None) try: data = [element[indexDatum] for element in column] except: print longname, column import sys sys.exit(0) error = [element[indexError] for element in column] unitCandidates = [element.unit for element in data \ if isQuantity(element)] if len(unitCandidates) == 0: unit = 1.0 else: unit = unitCandidates[0] normation = lambda arg: self.norm(arg, unit) field = numpy.array(map(normation, data)) ErrorNormation = lambda arg: self.norm(arg, unit, error=True) result = DataContainer.FieldContainer( field, error=numpy.array(map(ErrorNormation, error)), mask=numpy.isnan(field), unit=Quantity(1.0, unit), shortname=shortname, longname=longname) else: #Joining lists of strings if type(column[0]) == type([]): firstElement = column[0][0] else: firstElement = column[0] if type(firstElement) in (type(''), type(u'')): for i in xrange(len(column)): if type(column[i]) == type([]): column[i] = ','.join(column[i]) result = DataContainer.FieldContainer(numpy.array(column), longname=longname) return result
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
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