/
abstractanalysis.py
1187 lines (1033 loc) · 45.3 KB
/
abstractanalysis.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# -*- coding: utf-8 -*-
#
# This file is part of SENAITE.CORE.
#
# SENAITE.CORE is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License as published by the Free Software
# Foundation, version 2.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# this program; if not, write to the Free Software Foundation, Inc., 51
# Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
# Copyright 2018-2024 by it's authors.
# Some rights reserved, see README and LICENSE.
import cgi
import copy
import json
import math
from decimal import Decimal
from six import string_types
from AccessControl import ClassSecurityInfo
from bika.lims import api
from bika.lims import bikaMessageFactory as _
from bika.lims import deprecated
from bika.lims import logger
from bika.lims.browser.fields import HistoryAwareReferenceField
from bika.lims.browser.fields import InterimFieldsField
from bika.lims.browser.fields import ResultRangeField
from bika.lims.browser.fields import UIDReferenceField
from bika.lims.browser.fields.uidreferencefield import get_backreferences
from bika.lims.browser.widgets import RecordsWidget
from bika.lims.config import LDL
from bika.lims.config import UDL
from bika.lims.content.abstractbaseanalysis import AbstractBaseAnalysis
from bika.lims.content.abstractbaseanalysis import schema
from bika.lims.interfaces import IDuplicateAnalysis
from senaite.core.permissions import FieldEditAnalysisResult
from senaite.core.permissions import ViewResults
from bika.lims.utils import formatDecimalMark
from bika.lims.utils.analysis import format_numeric_result
from bika.lims.utils.analysis import get_significant_digits
from bika.lims.workflow import getTransitionActor
from bika.lims.workflow import getTransitionDate
from DateTime import DateTime
from senaite.core.browser.fields.datetime import DateTimeField
from Products.Archetypes.Field import IntegerField
from Products.Archetypes.Field import StringField
from Products.Archetypes.references import HoldingReference
from Products.Archetypes.Schema import Schema
from Products.CMFCore.permissions import View
# A link directly to the AnalysisService object used to create the analysis
AnalysisService = UIDReferenceField(
'AnalysisService'
)
# Attachments which are added manually in the UI, or automatically when
# results are imported from a file supplied by an instrument.
Attachment = UIDReferenceField(
'Attachment',
multiValued=1,
allowed_types=('Attachment',),
relationship='AnalysisAttachment'
)
# The final result of the analysis is stored here
Result = StringField(
'Result',
read_permission=ViewResults,
write_permission=FieldEditAnalysisResult,
)
# When the result is changed, this value is updated to the current time.
# Only the most recent result capture date is recorded here and used to
# populate catalog values, however the workflow review_history can be
# used to get all dates of result capture
ResultCaptureDate = DateTimeField(
'ResultCaptureDate',
read_permission=View,
write_permission=FieldEditAnalysisResult,
max="current",
)
# Returns the retracted analysis this analysis is a retest of
RetestOf = UIDReferenceField(
'RetestOf',
relationship="AnalysisRetestOf",
)
# If the result is outside of the detection limits of the method or instrument,
# the operand (< or >) is stored here. For routine analyses this is taken
# from the Result, if the result entered explicitly startswith "<" or ">"
DetectionLimitOperand = StringField(
'DetectionLimitOperand',
read_permission=View,
write_permission=FieldEditAnalysisResult,
)
# The ID of the logged in user who submitted the result for this Analysis.
Analyst = StringField(
'Analyst'
)
# The actual uncertainty for this analysis' result, populated from the ranges
# specified in the analysis service when the result is submitted.
Uncertainty = StringField(
'Uncertainty',
read_permission=View,
write_permission="Field: Edit Result",
precision=10,
)
# transitioned to a 'verified' state. This value is set automatically
# when the analysis is created, based on the value set for the property
# NumberOfRequiredVerifications from the Analysis Service
NumberOfRequiredVerifications = IntegerField(
'NumberOfRequiredVerifications',
default=1
)
# Routine Analyses and Reference Analysis have a versioned link to
# the calculation at creation time.
Calculation = HistoryAwareReferenceField(
'Calculation',
read_permission=View,
write_permission=FieldEditAnalysisResult,
allowed_types=('Calculation',),
relationship='AnalysisCalculation',
referenceClass=HoldingReference
)
# InterimFields are defined in Calculations, Services, and Analyses.
# In Analysis Services, the default values are taken from Calculation.
# In Analyses, the default values are taken from the Analysis Service.
# When instrument results are imported, the values in analysis are overridden
# before the calculation is performed.
InterimFields = InterimFieldsField(
'InterimFields',
read_permission=View,
write_permission=FieldEditAnalysisResult,
schemata='Method',
widget=RecordsWidget(
label=_("Calculation Interim Fields"),
description=_(
"Values can be entered here which will override the defaults "
"specified in the Calculation Interim Fields."),
)
)
# Results Range that applies to this analysis
ResultsRange = ResultRangeField(
"ResultsRange",
required=0
)
schema = schema.copy() + Schema((
AnalysisService,
Analyst,
Attachment,
DetectionLimitOperand,
# NumberOfRequiredVerifications overrides AbstractBaseClass
NumberOfRequiredVerifications,
Result,
ResultCaptureDate,
RetestOf,
Uncertainty,
Calculation,
InterimFields,
ResultsRange,
))
class AbstractAnalysis(AbstractBaseAnalysis):
security = ClassSecurityInfo()
displayContentsTab = False
schema = schema
@deprecated('[1705] Currently returns the Analysis object itself. If you '
'need to get the service, use getAnalysisService instead')
@security.public
def getService(self):
return self
def getServiceUID(self):
"""Return the UID of the associated service.
"""
return self.getRawAnalysisService()
@security.public
def getNumberOfVerifications(self):
return len(self.getVerificators())
@security.public
def getNumberOfRemainingVerifications(self):
required = self.getNumberOfRequiredVerifications()
done = self.getNumberOfVerifications()
if done >= required:
return 0
return required - done
# TODO Workflow - analysis . Remove?
@security.public
def getLastVerificator(self):
verifiers = self.getVerificators()
return verifiers and verifiers[-1] or None
@security.public
def getVerificators(self):
"""Returns the user ids of the users that verified this analysis
"""
verifiers = list()
actions = ["retest", "verify", "multi_verify"]
for event in api.get_review_history(self, rev=False):
if event.get("review_state") == "verified":
# include all transitions their end state is 'verified'
verifiers.append(event["actor"])
elif event.get("action") in actions:
# include some transitions their end state is not 'verified'
verifiers.append(event["actor"])
return verifiers
@security.public
def getDefaultUncertainty(self, result=None):
"""Return the uncertainty value, if the result falls within
specified ranges for the service from which this analysis was derived.
"""
if result is None:
result = self.getResult()
uncertainties = self.getUncertainties()
if uncertainties:
try:
res = float(result)
except (TypeError, ValueError):
# if analysis result is not a number, then we assume in range
return None
for d in uncertainties:
# convert to min/max
unc_min = api.to_float(d["intercept_min"], default=0)
unc_max = api.to_float(d["intercept_max"], default=0)
if unc_min <= res and res <= unc_max:
_err = str(d["errorvalue"]).strip()
if _err.endswith("%"):
try:
percvalue = float(_err.replace("%", ""))
except ValueError:
return None
# calculate uncertainty from result
uncertainty = res / 100 * percvalue
else:
uncertainty = api.to_float(_err, default=0)
# convert back to string value
return api.float_to_string(uncertainty, default=None)
return None
@security.public
def getUncertainty(self, result=None):
"""Returns the uncertainty for this analysis and result.
Returns the value from Schema's Uncertainty field if the Service has
the option 'Allow manual uncertainty'.
Otherwise, do a callback to getDefaultUncertainty().
Returns empty string if no result specified and the current result for this
analysis is below or above detections limits.
"""
uncertainty = self.getField("Uncertainty").get(self)
if result is None:
if self.isAboveUpperDetectionLimit():
return None
if self.isBelowLowerDetectionLimit():
return None
if uncertainty and self.getAllowManualUncertainty():
return api.float_to_string(uncertainty, default=None)
return self.getDefaultUncertainty(result)
@security.public
def setUncertainty(self, unc):
"""Sets the uncertainty for this analysis
If the result is a Detection Limit or the value is below LDL or upper
UDL, set the uncertainty to None``
"""
# Uncertainty calculation on DL
# https://jira.bikalabs.com/browse/LIMS-1808
if self.isAboveUpperDetectionLimit():
unc = None
if self.isBelowLowerDetectionLimit():
unc = None
field = self.getField("Uncertainty")
field.set(self, api.float_to_string(unc, default=None))
@security.public
def setDetectionLimitOperand(self, value):
"""Set detection limit operand for this analysis
Allowed detection limit operands are `<` and `>`.
"""
manual_dl = self.getAllowManualDetectionLimit()
selector = self.getDetectionLimitSelector()
if not manual_dl and not selector:
# Don't allow the user to set the limit operand if manual assignment
# is not allowed and selector is not visible
return
# Changing the detection limit operand has a side effect on the result
result = self.getResult()
if value in [LDL, UDL]:
# flush uncertainty
self.setUncertainty("")
# If no previous result or user is not allowed to manually set the
# the detection limit, override the result with default LDL/UDL
has_result = api.is_floatable(result)
if not has_result or not manual_dl:
# set the result according to the system default UDL/LDL values
if value == LDL:
result = self.getLowerDetectionLimit()
else:
result = self.getUpperDetectionLimit()
else:
value = ""
# Set the result
self.getField("Result").set(self, result)
# Set the detection limit to the field
self.getField("DetectionLimitOperand").set(self, value)
# Method getLowerDetectionLimit overrides method of class BaseAnalysis
@security.public
def getLowerDetectionLimit(self):
"""Returns the Lower Detection Limit (LDL) that applies to this
analysis in particular. If no value set or the analysis service
doesn't allow manual input of detection limits, returns the value set
by default in the Analysis Service
"""
if self.isLowerDetectionLimit():
result = self.getResult()
if api.is_floatable(result):
return result
logger.warn("The result for the analysis %s is a lower detection "
"limit, but not floatable: '%s'. Returning AS's "
"default LDL." % (self.id, result))
return AbstractBaseAnalysis.getLowerDetectionLimit(self)
# Method getUpperDetectionLimit overrides method of class BaseAnalysis
@security.public
def getUpperDetectionLimit(self):
"""Returns the Upper Detection Limit (UDL) that applies to this
analysis in particular. If no value set or the analysis service
doesn't allow manual input of detection limits, returns the value set
by default in the Analysis Service
"""
if self.isUpperDetectionLimit():
result = self.getResult()
if api.is_floatable(result):
return result
logger.warn("The result for the analysis %s is an upper detection "
"limit, but not floatable: '%s'. Returning AS's "
"default UDL." % (self.id, result))
return AbstractBaseAnalysis.getUpperDetectionLimit(self)
@security.public
def isBelowLowerDetectionLimit(self):
"""Returns True if the result is below the Lower Detection Limit or
if Lower Detection Limit has been manually set
"""
if self.isLowerDetectionLimit():
return True
result = self.getResult()
if result and str(result).strip().startswith(LDL):
return True
if api.is_floatable(result):
ldl = self.getLowerDetectionLimit()
return api.to_float(result) < api.to_float(ldl, 0.0)
return False
@security.public
def isAboveUpperDetectionLimit(self):
"""Returns True if the result is above the Upper Detection Limit or
if Upper Detection Limit has been manually set
"""
if self.isUpperDetectionLimit():
return True
result = self.getResult()
if result and str(result).strip().startswith(UDL):
return True
if api.is_floatable(result):
udl = self.getUpperDetectionLimit()
return api.to_float(result) > api.to_float(udl, 0.0)
return False
# TODO: REMOVE: nowhere used
@deprecated("This Method will be removed in version 2.5")
@security.public
def getDetectionLimits(self):
"""Returns a two-value array with the limits of detection (LDL and
UDL) that applies to this analysis in particular. If no value set or
the analysis service doesn't allow manual input of detection limits,
returns the value set by default in the Analysis Service
"""
ldl = self.getLowerDetectionLimit()
udl = self.getUpperDetectionLimit()
return [api.to_float(ldl, 0.0), api.to_float(udl, 0.0)]
@security.public
def isLowerDetectionLimit(self):
"""Returns True if the result for this analysis represents a Lower
Detection Limit. Otherwise, returns False
"""
return self.getDetectionLimitOperand() == LDL
@security.public
def isUpperDetectionLimit(self):
"""Returns True if the result for this analysis represents an Upper
Detection Limit. Otherwise, returns False
"""
return self.getDetectionLimitOperand() == UDL
@security.public
def getDependents(self):
"""Return a list of analyses who depend on us to calculate their result
"""
raise NotImplementedError("getDependents is not implemented.")
@security.public
def getDependencies(self, with_retests=False):
"""Return a list of siblings who we depend on to calculate our result.
:param with_retests: If false, siblings with retests are dismissed
:type with_retests: bool
:return: Analyses the current analysis depends on
:rtype: list of IAnalysis
"""
raise NotImplementedError("getDependencies is not implemented.")
@security.public
def setResult(self, value):
"""Validate and set a value into the Result field, taking into
account the Detection Limits.
:param value: is expected to be a string.
"""
# Convert to list ff the analysis has result options set with multi
if self.getResultOptions() and "multi" in self.getResultType():
if not isinstance(value, (list, tuple)):
value = filter(None, [value])
# Handle list results
if isinstance(value, (list, tuple)):
value = json.dumps(value)
# Ensure result integrity regards to None, empty and 0 values
val = str("" if not value and value != 0 else value).strip()
# Check if an string result is expected
string_result = self.getStringResult()
# UDL/LDL directly entered in the results field
if not string_result and val[:1] in [LDL, UDL]:
# Strip off the detection limit operand from the result
operand = val[0]
val = val.replace(operand, "", 1).strip()
# Result becomes the detection limit
selector = self.getDetectionLimitSelector()
allow_manual = self.getAllowManualDetectionLimit()
if any([selector, allow_manual]):
# Set the detection limit operand
self.setDetectionLimitOperand(operand)
if not allow_manual:
# Manual introduction of DL is not permitted
if operand == LDL:
# Result is default LDL
val = self.getLowerDetectionLimit()
else:
# Result is default UDL
val = self.getUpperDetectionLimit()
elif not self.getDetectionLimitSelector():
# User cannot choose the detection limit from a selection list,
# but might be allowed to manually enter the dl with the result.
# If so, reset the detection limit operand, cause the previous
# entered result might be an DL, but current doesn't
self.setDetectionLimitOperand("")
# Set the result field
self.getField("Result").set(self, val)
@security.public
def calculateResult(self, override=False, cascade=False):
"""Calculates the result for the current analysis if it depends of
other analysis/interim fields. Otherwise, do nothing
"""
if self.getResult() and override is False:
return False
calc = self.getCalculation()
if not calc:
return False
# get the formula from the calculation
formula = calc.getMinifiedFormula()
# Include the current context UID in the mapping, so it can be passed
# as a param in built-in functions, like 'get_result(%(context_uid)s)'
mapping = {"context_uid": '"{}"'.format(self.UID())}
# Interims' priority order (from low to high):
# Calculation < Analysis
interims = calc.getInterimFields() + self.getInterimFields()
# Add interims to mapping
for i in interims:
interim_keyword = i.get("keyword")
if not interim_keyword:
continue
# skip unset values
interim_value = i.get("value", "")
if interim_value == "":
continue
# Convert to floatable if necessary
if api.is_floatable(interim_value):
interim_value = float(interim_value)
else:
# If the interim value is a string, since the formula is also a string,
# it is needed to wrap the string interim values in between inverted commas.
#
# E.g. formula = '"ok" if %(var)s == "example_value" else "not ok"'
#
# if interim_value = "example_value" after
# formula = eval("'%s'%%mapping" % formula, {'mapping': {'var': interim_value}})
# print(formula)
# > '"ok" if example_value == "example_value" else "not ok"' -> Error
#
# else if interim_value ='"example_value"' after
# formula = eval("'%s'%%mapping" % formula, {'mapping': {'var': interim_value}})
# print(formula)
# > '"ok" if "example_value" == "example_value" else "not ok"' -> Correct
interim_value = '"{}"'.format(interim_value)
# Convert 'Numeric' interim values using `float`. Convert the rest using `str`
converter = "s" if i.get("result_type") else "f"
formula = formula.replace(
"[" + interim_keyword + "]", "%(" + interim_keyword + ")" + converter
)
mapping[interim_keyword] = interim_value
# Add dependencies results to mapping
dependencies = self.getDependencies()
for dependency in dependencies:
result = dependency.getResult()
# check if the dependency is a string result
str_result = dependency.getStringResult()
keyword = dependency.getKeyword()
if not result:
# Dependency without results found
if cascade:
# Try to calculate the dependency result
dependency.calculateResult(override, cascade)
result = dependency.getResult()
else:
return False
if result:
try:
# we need to quote a string result because of the `eval` below
result = '"%s"' % result if str_result else float(str(result))
key = dependency.getKeyword()
ldl = dependency.getLowerDetectionLimit()
udl = dependency.getUpperDetectionLimit()
bdl = dependency.isBelowLowerDetectionLimit()
adl = dependency.isAboveUpperDetectionLimit()
mapping[key] = result
mapping['%s.%s' % (key, 'RESULT')] = result
mapping['%s.%s' % (key, 'LDL')] = api.to_float(ldl, 0.0)
mapping['%s.%s' % (key, 'UDL')] = api.to_float(udl, 0.0)
mapping['%s.%s' % (key, 'BELOWLDL')] = int(bdl)
mapping['%s.%s' % (key, 'ABOVEUDL')] = int(adl)
except (TypeError, ValueError):
return False
# replace placeholder -> formatting string
# https://docs.python.org/2.7/library/stdtypes.html?highlight=built#string-formatting-operations
converter = "s" if str_result else "f"
formula = formula.replace("[" + keyword + "]", "%(" + keyword + ")" + converter)
# convert any remaining placeholders, e.g. from interims etc.
# NOTE: we assume remaining values are all floatable!
formula = formula.replace("[", "%(").replace("]", ")f")
# Calculate
try:
formula = eval("'%s'%%mapping" % formula,
{"__builtins__": None,
'math': math,
'context': self},
{'mapping': mapping})
result = eval(formula, calc._getGlobals())
except ZeroDivisionError:
self.setResult('0/0')
return True
except (KeyError, TypeError, ImportError) as e:
msg = "Cannot eval formula ({}): {}".format(e.message, formula)
logger.error(msg)
self.setResult("NA")
return True
self.setResult(str(result))
return True
@security.public
def getVATAmount(self):
"""Compute the VAT amount without member discount.
:return: the result as a float
"""
vat = self.getVAT()
price = self.getPrice()
return Decimal(price) * Decimal(vat) / 100
@security.public
def getTotalPrice(self):
"""Obtain the total price without client's member discount. The function
keeps in mind the client's bulk discount.
:return: the result as a float
"""
return Decimal(self.getPrice()) + Decimal(self.getVATAmount())
@security.public
def getDuration(self):
"""Returns the time in minutes taken for this analysis.
If the analysis is not yet 'ready to process', returns 0
If the analysis is still in progress (not yet verified),
duration = date_verified - date_start_process
Otherwise:
duration = current_datetime - date_start_process
:return: time in minutes taken for this analysis
:rtype: int
"""
starttime = self.getStartProcessDate()
if not starttime:
# The analysis is not yet ready to be processed
return 0
endtime = self.getDateVerified() or DateTime()
# Duration in minutes
duration = (endtime - starttime) * 24 * 60
return duration
@security.public
def getEarliness(self):
"""The remaining time in minutes for this analysis to be completed.
Returns zero if the analysis is neither 'ready to process' nor a
turnaround time is set.
earliness = duration - max_turnaround_time
The analysis is late if the earliness is negative
:return: the remaining time in minutes before the analysis reaches TAT
:rtype: int
"""
maxtime = self.getMaxTimeAllowed()
if not maxtime:
# No Turnaround time is set for this analysis
return 0
return api.to_minutes(**maxtime) - self.getDuration()
@security.public
def isLateAnalysis(self):
"""Returns true if the analysis is late in accordance with the maximum
turnaround time. If no maximum turnaround time is set for this analysis
or it is not yet ready to be processed, or there is still time
remaining (earliness), returns False.
:return: true if the analysis is late
:rtype: bool
"""
return self.getEarliness() < 0
@security.public
def getLateness(self):
"""The time in minutes that exceeds the maximum turnaround set for this
analysis. If the analysis has no turnaround time set or is not ready
for process yet, returns 0. The analysis is not late if the lateness is
negative
:return: the time in minutes that exceeds the maximum turnaround time
:rtype: int
"""
return -self.getEarliness()
@security.public
def isInstrumentAllowed(self, instrument):
"""Checks if the specified instrument can be set for this analysis,
:param instrument: string,Instrument
:return: True if the assignment of the passed in instrument is allowed
:rtype: bool
"""
uid = api.get_uid(instrument)
return uid in self.getRawAllowedInstruments()
@security.public
def isMethodAllowed(self, method):
"""Checks if the analysis can follow the method specified
:param method: string,Method
:return: True if the analysis can follow the method specified
:rtype: bool
"""
uid = api.get_uid(method)
return uid in self.getRawAllowedMethods()
@security.public
def getAllowedMethods(self):
"""Returns the allowed methods for this analysis, either if the method
was assigned directly (by using "Allows manual entry of results") or
indirectly via Instrument ("Allows instrument entry of results") in
Analysis Service Edit View.
:return: A list with the methods allowed for this analysis
:rtype: list of Methods
"""
service = self.getAnalysisService()
if not service:
return []
# get the available methods of the service
return service.getMethods()
@security.public
def getRawAllowedMethods(self):
"""Returns the UIDs of the allowed methods for this analysis
"""
service = self.getAnalysisService()
if not service:
return []
return service.getRawMethods()
@security.public
def getAllowedInstruments(self):
"""Returns the allowed instruments from the service
:return: A list of instruments allowed for this Analysis
:rtype: list of instruments
"""
service = self.getAnalysisService()
if not service:
return []
return service.getInstruments()
@security.public
def getRawAllowedInstruments(self):
"""Returns the UIDS of the allowed instruments from the service
"""
service = self.getAnalysisService()
if not service:
return []
return service.getRawInstruments()
@security.public
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 _getExponentialFormatPrecision(self):
field = self.getField('ExponentialFormatPrecision')
value = field.get(self)
if value is None:
# https://github.com/bikalims/bika.lims/issues/2004
# We require the field, because None values make no sense at all.
value = self.Schema().getField(
'ExponentialFormatPrecision').getDefault(self)
return value
@security.public
def getFormattedResult(self, specs=None, decimalmark='.', sciformat=1,
html=True):
"""Formatted result:
0: If the result type is StringResult, return it without being formatted
1. If the result is a detection limit, returns '< LDL' or '> UDL'
2. Print ResultText of matching ResultOptions
3. If the result is not floatable, return it without being formatted
4. If the analysis specs has hidemin or hidemax enabled and the
result is out of range, render result as '<min' or '>max'
5. If the result is below Lower Detection Limit, show '<LDL'
6. If the result is above Upper Detecion Limit, show '>UDL'
7. Otherwise, render numerical value
:param specs: Optional result specifications, a dictionary as follows:
{'min': <min_val>,
'max': <max_val>,
'error': <error>,
'hidemin': <hidemin_val>,
'hidemax': <hidemax_val>}
:param decimalmark: The string to be used as a decimal separator.
default is '.'
:param sciformat: 1. The sci notation has to be formatted as aE^+b
2. The sci notation has to be formatted as a·10^b
3. As 2, but with super html entity for exp
4. The sci notation has to be formatted as a·10^b
5. As 4, but with super html entity for exp
By default 1
:param html: if true, returns an string with the special characters
escaped: e.g: '<' and '>' (LDL and UDL for results like < 23.4).
"""
result = self.getResult()
# If result options, return text of matching option
choices = self.getResultOptions()
if choices:
# Create a dict for easy mapping of result options
values_texts = dict(map(
lambda c: (str(c["ResultValue"]), c["ResultText"]), choices
))
# Result might contain a single result option
match = values_texts.get(str(result))
if match:
return match
# Result might be a string with multiple options e.g. "['2', '1']"
try:
raw_result = json.loads(result)
texts = map(lambda r: values_texts.get(str(r)), raw_result)
texts = filter(None, texts)
return "<br/>".join(texts)
except (ValueError, TypeError):
pass
# If string result, return without any formatting
if self.getStringResult():
return cgi.escape(result) if html else result
# If a detection limit, return '< LDL' or '> UDL'
dl = self.getDetectionLimitOperand()
if dl:
try:
res = api.float_to_string(float(result))
fdm = formatDecimalMark(res, decimalmark)
hdl = cgi.escape(dl) if html else dl
return '%s %s' % (hdl, fdm)
except (TypeError, ValueError):
logger.warn(
"The result for the analysis %s is a detection limit, "
"but not floatable: %s" % (self.id, result))
return formatDecimalMark(result, decimalmark=decimalmark)
# If not floatable, return without any formatting
try:
result = float(result)
except (TypeError, ValueError):
return formatDecimalMark(result, decimalmark=decimalmark)
# If specs are set, evaluate if out of range
specs = specs if specs else self.getResultsRange()
hidemin = specs.get('hidemin', '')
hidemax = specs.get('hidemax', '')
try:
belowmin = hidemin and result < float(hidemin) or False
except (TypeError, ValueError):
belowmin = False
try:
abovemax = hidemax and result > float(hidemax) or False
except (TypeError, ValueError):
abovemax = False
# If below min and hidemin enabled, return '<min'
if belowmin:
fdm = formatDecimalMark('< %s' % hidemin, decimalmark)
return fdm.replace('< ', '< ', 1) if html else fdm
# If above max and hidemax enabled, return '>max'
if abovemax:
fdm = formatDecimalMark('> %s' % hidemax, decimalmark)
return fdm.replace('> ', '> ', 1) if html else fdm
# If below LDL, return '< LDL'
ldl = self.getLowerDetectionLimit()
ldl = api.to_float(ldl, 0.0)
if result < ldl:
# LDL must not be formatted according to precision, etc.
ldl = api.float_to_string(ldl)
fdm = formatDecimalMark('< %s' % ldl, decimalmark)
return fdm.replace('< ', '< ', 1) if html else fdm
# If above UDL, return '< UDL'
udl = self.getUpperDetectionLimit()
udl = api.to_float(udl, 0.0)
if result > udl:
# UDL must not be formatted according to precision, etc.
udl = api.float_to_string(udl)
fdm = formatDecimalMark('> %s' % udl, decimalmark)
return fdm.replace('> ', '> ', 1) if html else fdm
# Render numerical values
return format_numeric_result(self, self.getResult(),
decimalmark=decimalmark,
sciformat=sciformat)
@security.public
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 inferred
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 api.to_float(uncertainty) == 0 and result is None:
return self.getField("Precision").get(self)
if api.to_float(uncertainty) == 0:
strres = str(result)
numdecimals = strres[::-1].find('.')
return numdecimals