|
51 | 51 | from bika.lims.workflow import getTransitionDate
|
52 | 52 | from DateTime import DateTime
|
53 | 53 | from Products.Archetypes.Field import DateTimeField
|
54 |
| -from Products.Archetypes.Field import FixedPointField |
55 | 54 | from Products.Archetypes.Field import IntegerField
|
56 | 55 | from Products.Archetypes.Field import StringField
|
57 | 56 | from Products.Archetypes.references import HoldingReference
|
|
109 | 108 |
|
110 | 109 | # The actual uncertainty for this analysis' result, populated from the ranges
|
111 | 110 | # specified in the analysis service when the result is submitted.
|
112 |
| -Uncertainty = FixedPointField( |
| 111 | +Uncertainty = StringField( |
113 | 112 | 'Uncertainty',
|
114 | 113 | read_permission=View,
|
115 | 114 | write_permission="Field: Edit Result",
|
@@ -240,56 +239,66 @@ def getDefaultUncertainty(self, result=None):
|
240 | 239 | return None
|
241 | 240 |
|
242 | 241 | for d in uncertainties:
|
243 |
| - _min = float(d['intercept_min']) |
244 |
| - _max = float(d['intercept_max']) |
245 |
| - if _min <= res and res <= _max: |
246 |
| - if str(d['errorvalue']).strip().endswith('%'): |
| 242 | + |
| 243 | + # convert to min/max |
| 244 | + unc_min = api.to_float(d["intercept_min"], default=0) |
| 245 | + unc_max = api.to_float(d["intercept_max"], default=0) |
| 246 | + |
| 247 | + if unc_min <= res and res <= unc_max: |
| 248 | + _err = str(d["errorvalue"]).strip() |
| 249 | + if _err.endswith("%"): |
247 | 250 | try:
|
248 |
| - percvalue = float(d['errorvalue'].replace('%', '')) |
| 251 | + percvalue = float(_err.replace("%", "")) |
249 | 252 | except ValueError:
|
250 | 253 | return None
|
| 254 | + # calculate uncertainty from result |
251 | 255 | uncertainty = res / 100 * percvalue
|
252 | 256 | else:
|
253 |
| - uncertainty = float(d['errorvalue']) |
| 257 | + uncertainty = api.to_float(_err, default=0) |
254 | 258 |
|
255 |
| - return uncertainty |
| 259 | + # convert back to string value |
| 260 | + return api.float_to_string(uncertainty, default=None) |
256 | 261 | return None
|
257 | 262 |
|
258 | 263 | @security.public
|
259 | 264 | def getUncertainty(self, result=None):
|
260 | 265 | """Returns the uncertainty for this analysis and result.
|
| 266 | +
|
261 | 267 | Returns the value from Schema's Uncertainty field if the Service has
|
262 |
| - the option 'Allow manual uncertainty'. Otherwise, do a callback to |
263 |
| - getDefaultUncertainty(). Returns None if no result specified and the |
264 |
| - current result for this analysis is below or above detections limits. |
| 268 | + the option 'Allow manual uncertainty'. |
| 269 | + Otherwise, do a callback to getDefaultUncertainty(). |
| 270 | +
|
| 271 | + Returns empty string if no result specified and the current result for this |
| 272 | + analysis is below or above detections limits. |
265 | 273 | """
|
266 |
| - uncertainty = self.getField('Uncertainty').get(self) |
267 |
| - if result is None and (self.isAboveUpperDetectionLimit() or |
268 |
| - self.isBelowLowerDetectionLimit()): |
269 |
| - return None |
| 274 | + uncertainty = self.getField("Uncertainty").get(self) |
| 275 | + if result is None: |
| 276 | + if self.isAboveUpperDetectionLimit(): |
| 277 | + return None |
| 278 | + if self.isBelowLowerDetectionLimit(): |
| 279 | + return None |
| 280 | + |
| 281 | + if uncertainty and self.getAllowManualUncertainty(): |
| 282 | + return api.float_to_string(uncertainty, default=None) |
270 | 283 |
|
271 |
| - if uncertainty and self.getAllowManualUncertainty() is True: |
272 |
| - try: |
273 |
| - uncertainty = float(uncertainty) |
274 |
| - return uncertainty |
275 |
| - except (TypeError, ValueError): |
276 |
| - # if uncertainty is not a number, return default value |
277 |
| - pass |
278 | 284 | return self.getDefaultUncertainty(result)
|
279 | 285 |
|
280 | 286 | @security.public
|
281 | 287 | def setUncertainty(self, unc):
|
282 |
| - """Sets the uncertainty for this analysis. If the result is a |
283 |
| - Detection Limit or the value is below LDL or upper UDL, sets the |
284 |
| - uncertainty value to 0 |
| 288 | + """Sets the uncertainty for this analysis |
| 289 | +
|
| 290 | + If the result is a Detection Limit or the value is below LDL or upper |
| 291 | + UDL, set the uncertainty to None`` |
285 | 292 | """
|
286 | 293 | # Uncertainty calculation on DL
|
287 | 294 | # https://jira.bikalabs.com/browse/LIMS-1808
|
288 |
| - if self.isAboveUpperDetectionLimit() or \ |
289 |
| - self.isBelowLowerDetectionLimit(): |
290 |
| - self.getField('Uncertainty').set(self, None) |
291 |
| - else: |
292 |
| - self.getField('Uncertainty').set(self, unc) |
| 295 | + if self.isAboveUpperDetectionLimit(): |
| 296 | + unc = None |
| 297 | + if self.isBelowLowerDetectionLimit(): |
| 298 | + unc = None |
| 299 | + |
| 300 | + field = self.getField("Uncertainty") |
| 301 | + field.set(self, api.float_to_string(unc, default=None)) |
293 | 302 |
|
294 | 303 | @security.public
|
295 | 304 | def setDetectionLimitOperand(self, value):
|
@@ -972,10 +981,10 @@ def getPrecision(self, result=None):
|
972 | 981 | if allow_manual or precision_unc:
|
973 | 982 | uncertainty = self.getUncertainty(result)
|
974 | 983 | if uncertainty is None:
|
975 |
| - return self.getField('Precision').get(self) |
976 |
| - if uncertainty == 0 and result is None: |
977 |
| - return self.getField('Precision').get(self) |
978 |
| - if uncertainty == 0: |
| 984 | + return self.getField("Precision").get(self) |
| 985 | + if api.to_float(uncertainty) == 0 and result is None: |
| 986 | + return self.getField("Precision").get(self) |
| 987 | + if api.to_float(uncertainty) == 0: |
979 | 988 | strres = str(result)
|
980 | 989 | numdecimals = strres[::-1].find('.')
|
981 | 990 | return numdecimals
|
|
0 commit comments