def test_get_significant_digits(self): # testing the function bika.lims.utils.analysis.get_significant_digits() from bika.lims.utils.analysis import get_significant_digits self.assertEqual(get_significant_digits('0'), 0) self.assertEqual(get_significant_digits('0.22'), 1) self.assertEqual(get_significant_digits('1.34'), 0) self.assertEqual(get_significant_digits('0.0021'), 3) self.assertEqual(get_significant_digits('2'), 0) self.assertEqual(get_significant_digits('22'), 0)
def getExponentialFormatPrecision(self, result=None): """ Returns the precision for the Analysis Service and result provided. Results with a precision value above this exponential format precision should be formatted as scientific notation. If the Calculate Precision according to Uncertainty is not set, the method will return the exponential precision value set in the Schema. Otherwise, will calculate the precision value according to the Uncertainty and the result. If Calculate Precision from the Uncertainty is set but no result provided neither uncertainty values are set, returns the fixed exponential precision. Will return positive values if the result is below 0 and will return 0 or positive values if the result is above 0. Given an analysis service with fixed exponential format precision of 4: Result Uncertainty Returns 5.234 0.22 0 13.5 1.34 1 0.0077 0.008 -3 32092 0.81 4 456021 423 5 For further details, visit https://jira.bikalabs.com/browse/LIMS-1334 :param result: if provided and "Calculate Precision according to the Uncertainty" is set, the result will be used to retrieve the uncertainty from which the precision must be calculated. Otherwise, the fixed-precision will be used. :returns: the precision """ if not result or self.getPrecisionFromUncertainty() is False: return self._getExponentialFormatPrecision() else: uncertainty = self.getUncertainty(result) if uncertainty is None: return self._getExponentialFormatPrecision() try: float(result) except ValueError: # if analysis result is not a number, then we assume in range return self._getExponentialFormatPrecision() return get_significant_digits(uncertainty)
def getPrecision(self, result=None): """ Returns the precision for the Analysis. - ManualUncertainty not set: returns the precision from the AnalysisService. - ManualUncertainty set and Calculate Precision from Uncertainty is also set in Analysis Service: calculates the precision of the result according to the manual uncertainty set. - ManualUncertainty set and Calculatet Precision from Uncertainty not set in Analysis Service: returns the result as-is. Further information at AnalysisService.getPrecision() """ serv = self.getService() schu = self.Schema().getField('Uncertainty').get(self) if schu and serv.getAllowManualUncertainty() == True \ and serv.getPrecisionFromUncertainty() == True: uncertainty = self.getUncertainty(result) if uncertainty == 0: return 1 return abs(get_significant_digits(uncertainty)) else: return serv.getPrecision(result)
def getPrecision(self, result=None): """Returns the precision for the Analysis. - If ManualUncertainty is set, calculates the precision of the result in accordance with the manual uncertainty set. - If Calculate Precision from Uncertainty is set in Analysis Service, calculates the precision in accordance with the uncertainty infered from uncertainties ranges. - If neither Manual Uncertainty nor Calculate Precision from Uncertainty are set, returns the precision from the Analysis Service - If you have a number with zero uncertainty: If you roll a pair of dice and observe five spots, the number of spots is 5. This is a raw data point, with no uncertainty whatsoever. So just write down the number. Similarly, the number of centimeters per inch is 2.54, by definition, with no uncertainty whatsoever. Again: just write down the number. Further information at AbstractBaseAnalysis.getPrecision() """ allow_manual = self.getAllowManualUncertainty() precision_unc = self.getPrecisionFromUncertainty() if allow_manual or precision_unc: uncertainty = self.getUncertainty(result) if uncertainty is None: return self.getField('Precision').get(self) if uncertainty == 0 and result is None: return self.getField('Precision').get(self) if uncertainty == 0: strres = str(result) numdecimals = strres[::-1].find('.') return numdecimals return get_significant_digits(uncertainty) return self.getField('Precision').get(self)