TestCenter Reference
Image.py
Go to the documentation of this file.
1 #
2 # Copyright 2009, MeVis Medical Solutions AG
3 #
4 # The user may use this file in accordance with the license agreement provided with
5 # the Software or, alternatively, in accordance with the terms contained in a
6 # written agreement between the user and MeVis Medical Solutions AG.
7 #
8 # For further information use the contact form at https://www.mevislab.de/contact
9 #
10 
11 
13 
14 # -- system imports ----------------------------------------------------------------------------{{{-
15 import os
16 import sys
17 import traceback
18 # ----------------------------------------------------------------------------------------------}}}-
19 
20 # -- local imports -----------------------------------------------------------------------------{{{-
21 import mevis
22 
23 from TestSupport.TestHelper import TestHelper
24 # ----------------------------------------------------------------------------------------------}}}-
25 
26 # -- def calculateHashFromImage ----------------------------------------------------------------{{{-
27 
34 def calculateHashFromImage (fileName, hashMethod='SHA256', encoder='Hex'):
35  helpCtx = TestHelper.getInstance().getHelperContext()
36 
37  modImgLoad = helpCtx.module("iHSImgLoad")
38  modImgHash = helpCtx.module("iHSImageHash")
39 
40  hashSelectorField = modImgHash.field('hashSelector')
41  if hashMethod not in hashSelectorField.items():
42  raise Exception("Hash method %s unknown. Valid values are: %s" % (hashMethod, ', '.join(hashSelectorField.items())))
43  hashSelectorField.value = hashMethod
44 
45  encoderSelectorField = modImgHash.field('encoderSelector')
46  if encoder not in encoderSelectorField.items():
47  raise Exception("Encoder %s unknown. Valid values are: %s" % (encoder, ', '.join(encoderSelectorField.items())))
48  encoderSelectorField.value = encoder
49 
50  result = ""
51  modImgLoad.field('filename').value = fileName
52  if not modImgHash.field('hashValid').value:
53  Logging_error("Failed to create hash of image (%s)" % (fileName))
54  else:
55  result = modImgHash.field('hash').value
56  modImgLoad.field('close').touch()
57  return result
58 # ----------------------------------------------------------------------------------------------}}}-
59 
60 # -- def getVoxelValue -------------------------------------------------------------------------{{{-
61 
69 def getVoxelValue (outFieldName, position, coordinateType="Voxel", useStoredValue=False):
70  testCtx = Base_getTestCaseContext()
71  helpCtx = TestHelper.getInstance().getHelperContext()
72 
73  result = (None, None)
74 
75  outField = testCtx.field(outFieldName)
76  if outField:
77  module = helpCtx.module("GetVoxelValue")
78  module.field("input0").connectFrom(outField)
79  module.field("coordinateType").value = coordinateType
80  module.field("position").value = position
81  module.field("updateButton").touch()
82  if not useStoredValue:
83  result = (module.field("outputStringValue").value, module.field("outputValue").value)
84  else:
85  result = (module.field("storedStringValue").value, module.field("storedValue").value)
86  if not outField or not module.field("valid").value:
87  Logging_error("Failed to retrieve voxel value.")
88 
89  return result
90 # ----------------------------------------------------------------------------------------------}}}-
91 
92 # -- def __imageCompare ------------------------------------------------------------------------{{{-
93 
94 def __imageCompare (compareModule, outFieldA, outFieldB):
95  testCtx = Base_getTestCaseContext()
96  helpCtx = TestHelper.getInstance().getHelperContext()
97 
98  fieldA = testCtx.field(outFieldA)
99  fieldB = testCtx.field(outFieldB)
100  if fieldA and fieldB:
101  module = helpCtx.module(compareModule)
102  module.field("input0").connectFrom(fieldA)
103  module.field("input1").connectFrom(fieldB)
104  module.field("compare").touch()
105  if module.field("testPassed").value:
106  Logging_info("Image Compare with (%s): No differences found" % (compareModule))
107  else:
108  Logging_info("Image Compare with (%s): differences found" % (compareModule))
109  return module.field("testPassed").value
110 # ----------------------------------------------------------------------------------------------}}}-
111 
112 # -- def compareImages -------------------------------------------------------------------------{{{-
113 
114 def compareImages (outFieldA, outFieldB):
115  return __imageCompare("ImageCompareDefault", outFieldA, outFieldB)
116 # ----------------------------------------------------------------------------------------------}}}-
117 
118 # -- def compareImagesProperties -------------------------------------------------------------------------{{{-
119 
121 def compareImagesProperties (outFieldA, outFieldB):
122  return __imageCompare("ImageCompareProperties", outFieldA, outFieldB)
123 # ----------------------------------------------------------------------------------------------}}}-
124 
125 # -- def compareImagesData ---------------------------------------------------------------------{{{-
126 
127 def compareImagesData (outFieldA, outFieldB):
128  return __imageCompare("ImageCompareData", outFieldA, outFieldB)
129 # ----------------------------------------------------------------------------------------------}}}-
130 
131 # -- def compareImagesAllProps -----------------------------------------------------------------{{{-
132 
133 def compareImagesAllProps (outFieldA, outFieldB):
134  return __imageCompare("ImageCompareAllProps", outFieldA, outFieldB)
135 # ----------------------------------------------------------------------------------------------}}}-
136 
137 # -- def getTrueMinMaxValues ------------------------------------------------------------------------{{{-
138 def getTrueMinMaxValues(outFieldName):
139  """! Returns the true min/max range of the image data, which is therefore scanned entirely using a MinMaxScan module.
140  @param outFieldName String: Name of the output image field
141  @return tuple(Number, Number): Returns a tuple (trueMinimumValue, trueMaximumValue) or (None, None) if the image could not be scanned
142  """
143  testCtx = Base_getTestCaseContext()
144  helpCtx = TestHelper.getInstance().getHelperContext()
145  trueMin = None
146  trueMax = None
147  outField = testCtx.field(outFieldName)
148  if outField and outField.isValid():
149  module = helpCtx.module("MinMaxScan")
150  module.field("input0").connectFrom(testCtx.field(outFieldName))
151  module.field("update").touch()
152  if module.field("upToDate").value:
153  trueMin = module.field("trueMinValue").value
154  trueMax = module.field("trueMaxValue").value
155  return trueMin, trueMax
156 # ----------------------------------------------------------------------------------------------}}}-
157 
158 # -- def verifyMinMaxPropertyValues -----------------------------------------------------------------{{{-
159 def verifyMinMaxPropertyValues(outFieldName, expectedMin = None, expectedMax = None, requireExactMin = False, requireExactMax = False, logOnSuccess = True):
160  """! Verifies the min/max image property values. By default (none of the optional arguments
161  provided) the functions just checks if the min/max range specified by the image properties include the actual
162  min/max range of the image data, which is therefore scanned. Note that this check is always done, since ML
163  images always need to fulfill the condition. In addition, it is possible to check against expected values, or
164  to require exact matches of the range boundaries (using the expected values, if provided, and the actual
165  values otherwise). Logs an error if the output image is valid and any of the checks fail.
166  @outFieldName String: Name of the output image field to perform the check on.
167  @param expectedMin Number: If given, the minimum value image property is compared against expectedMin
168  @param expectedMax Number: If given, the maximum value image property is compared against expectedMax
169  @param requireExactMin Bool: If given, the minimum value image property must be identical to expectedMin (if given) or the actual image minimum (otherwise)
170  @param requireExactMax Bool: If given, the maximum value image property must be identical to expectedMax (if given) or the actual image maximum (otherwise)
171  @param logOnSuccess Bool: If enabled, provide success message
172  """
173  errorOccurred = False
174  outField = Base_getTestCaseContext().field( outFieldName )
175  comparisonErrorComment = "Min/max value error for field %s." % outFieldName
176  if outField.isValid():
177  trueMin, trueMax = getTrueMinMaxValues( outFieldName )
178  if trueMin is None or trueMax is None:
179  errorOccurred = True
180  Logging_error("Image.verifyMinMaxPropertyValues(): Unexpected error scanning the image at field %s!" % outFieldName)
181  else:
182  if expectedMin is None:
183  expectedMin = outField.minValue()
184  if expectedMax is None:
185  expectedMax = outField.maxValue()
186  if requireExactMin:
187  if not Base_compareFloatEqual(trueMin, expectedMin, comparisonErrorComment):
188  errorOccurred = True
189  else:
190  if not Base_compareFloatLessThanOrEqual(expectedMin, trueMin, comparisonErrorComment):
191  errorOccurred = True
192  if requireExactMax:
193  if not Base_compareFloatEqual(trueMax, expectedMax, comparisonErrorComment):
194  errorOccurred = True
195  else:
196  if not Base_compareFloatLessThanOrEqual(trueMax, expectedMax, comparisonErrorComment):
197  errorOccurred = True
198 
199  if not errorOccurred and logOnSuccess:
200  Logging_info( "Image.verifyMinMaxPropertyValues(%s): SUCCESS" % outFieldName )
201  return
202 
203  # ----------------------------------------------------------------------------------------------}}}-
204 
205 from .Logging import error as Logging_error, info as Logging_info
206 from .Base import getTestCaseContext as Base_getTestCaseContext, \
207  compareFloatEqual as Base_compareFloatEqual, \
208  compareFloatLessThanOrEqual as Base_compareFloatLessThanOrEqual
def touch(fieldName, verbose=True)
Touch the given field.
Definition: Fields.py:33
def verifyMinMaxPropertyValues(outFieldName, expectedMin=None, expectedMax=None, requireExactMin=False, requireExactMax=False, logOnSuccess=True)
Verifies the min/max image property values.
Definition: Image.py:159
def getTrueMinMaxValues(outFieldName)
Returns the true min/max range of the image data, which is therefore scanned entirely using a MinMaxS...
Definition: Image.py:138
def compareImages(outFieldA, outFieldB)
Compare the two given images (excluding the extention properties).
Definition: Image.py:114
def calculateHashFromImage(fileName, hashMethod='SHA256', encoder='Hex')
Calculate the hash of the given image.
Definition: Image.py:34
def getVoxelValue(outFieldName, position, coordinateType="Voxel", useStoredValue=False)
Get the voxel value of the given field at the given position.
Definition: Image.py:69
def compareImagesData(outFieldA, outFieldB)
Compare the two given images (only verifying data).
Definition: Image.py:127
def compareImagesAllProps(outFieldA, outFieldB)
Compare the two given images.
Definition: Image.py:133
def compareImagesProperties(outFieldA, outFieldB)
Compare just the properties of the two given images (excluding page extent, the extension properties,...
Definition: Image.py:121