TestCenter Reference
Image.py
Go to the documentation of this file.
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 ----------------------------------------------------------------------------{{{-
15import os
16import sys
17import traceback
18
19# ----------------------------------------------------------------------------------------------}}}-
20
21# -- local imports -----------------------------------------------------------------------------{{{-
22import mevis
23
24from TestSupport.TestHelper import TestHelper
25
26# ----------------------------------------------------------------------------------------------}}}-
27
28
29# -- def calculateHashFromImage ----------------------------------------------------------------{{{-
30
37def calculateHashFromImage(fileName, hashMethod="SHA256", encoder="Hex"):
38 helpCtx = TestHelper.getInstance().getHelperContext()
39
40 modImgLoad = helpCtx.module("iHSImgLoad")
41 modImgHash = helpCtx.module("iHSImageHash")
42
43 hashSelectorField = modImgHash.field("hashSelector")
44 if hashMethod not in hashSelectorField.items():
45 raise Exception(
46 "Hash method %s unknown. Valid values are: %s" % (hashMethod, ", ".join(hashSelectorField.items()))
47 )
48 hashSelectorField.value = hashMethod
49
50 encoderSelectorField = modImgHash.field("encoderSelector")
51 if encoder not in encoderSelectorField.items():
52 raise Exception("Encoder %s unknown. Valid values are: %s" % (encoder, ", ".join(encoderSelectorField.items())))
53 encoderSelectorField.value = encoder
54
55 result = ""
56 modImgLoad.field("filename").value = fileName
57 if not modImgHash.field("hashValid").value:
58 Logging_error("Failed to create hash of image (%s)" % (fileName))
59 else:
60 result = modImgHash.field("hash").value
61 modImgLoad.field("close").touch()
62 return result
63
64
65# ----------------------------------------------------------------------------------------------}}}-
66
67
68# -- def getVoxelValue -------------------------------------------------------------------------{{{-
69
77def getVoxelValue(outFieldName, position, coordinateType="Voxel", useStoredValue=False):
78 testCtx = Base_getTestCaseContext()
79 helpCtx = TestHelper.getInstance().getHelperContext()
80
81 result = (None, None)
82
83 outField = testCtx.field(outFieldName)
84 if outField:
85 module = helpCtx.module("GetVoxelValue")
86 module.field("input0").connectFrom(outField)
87 module.field("coordinateType").value = coordinateType
88 module.field("position").value = position
89 module.field("updateButton").touch()
90 if not useStoredValue:
91 result = (module.field("outputStringValue").value, module.field("outputValue").value)
92 else:
93 result = (module.field("storedStringValue").value, module.field("storedValue").value)
94 if not outField or not module.field("valid").value:
95 Logging_error("Failed to retrieve voxel value.")
96
97 return result
98
99
100# ----------------------------------------------------------------------------------------------}}}-
101
102
103# -- def __imageCompare ------------------------------------------------------------------------{{{-
104
105def __imageCompare(compareModule, outFieldA, outFieldB):
106 testCtx = Base_getTestCaseContext()
107 helpCtx = TestHelper.getInstance().getHelperContext()
108
109 fieldA = testCtx.field(outFieldA)
110 fieldB = testCtx.field(outFieldB)
111 if fieldA and fieldB:
112 module = helpCtx.module(compareModule)
113 module.field("input0").connectFrom(fieldA)
114 module.field("input1").connectFrom(fieldB)
115 module.field("compare").touch()
116 if module.field("testPassed").value:
117 Logging_info("Image Compare with (%s): No differences found" % (compareModule))
118 else:
119 Logging_info("Image Compare with (%s): differences found" % (compareModule))
120 return module.field("testPassed").value
121
122
123# ----------------------------------------------------------------------------------------------}}}-
124
125
126# -- def compareImages -------------------------------------------------------------------------{{{-
127
128def compareImages(outFieldA, outFieldB):
129 return __imageCompare("ImageCompareDefault", outFieldA, outFieldB)
130
131
132# ----------------------------------------------------------------------------------------------}}}-
133
134
135# -- def compareImagesProperties -------------------------------------------------------------------------{{{-
136
138def compareImagesProperties(outFieldA, outFieldB):
139 return __imageCompare("ImageCompareProperties", outFieldA, outFieldB)
140
141
142# ----------------------------------------------------------------------------------------------}}}-
143
144
145# -- def compareImagesData ---------------------------------------------------------------------{{{-
146
147def compareImagesData(outFieldA, outFieldB):
148 return __imageCompare("ImageCompareData", outFieldA, outFieldB)
149
150
151# ----------------------------------------------------------------------------------------------}}}-
152
153
154# -- def compareImagesAllProps -----------------------------------------------------------------{{{-
155
156def compareImagesAllProps(outFieldA, outFieldB):
157 return __imageCompare("ImageCompareAllProps", outFieldA, outFieldB)
158
159
160# ----------------------------------------------------------------------------------------------}}}-
161
162
163# -- def getTrueMinMaxValues ------------------------------------------------------------------------{{{-
164def getTrueMinMaxValues(outFieldName):
165 """! Returns the true min/max range of the image data, which is therefore scanned entirely using a MinMaxScan module.
166 @param outFieldName String: Name of the output image field
167 @return tuple(Number, Number): Returns a tuple (trueMinimumValue, trueMaximumValue) or (None, None) if the image could not be scanned
168 """
169 testCtx = Base_getTestCaseContext()
170 helpCtx = TestHelper.getInstance().getHelperContext()
171 trueMin = None
172 trueMax = None
173 outField = testCtx.field(outFieldName)
174 if outField and outField.isValid():
175 module = helpCtx.module("MinMaxScan")
176 module.field("input0").connectFrom(testCtx.field(outFieldName))
177 module.field("update").touch()
178 if module.field("upToDate").value:
179 trueMin = module.field("trueMinValue").value
180 trueMax = module.field("trueMaxValue").value
181 return trueMin, trueMax
182
183
184# ----------------------------------------------------------------------------------------------}}}-
185
186
187# -- def verifyMinMaxPropertyValues -----------------------------------------------------------------{{{-
189 outFieldName, expectedMin=None, expectedMax=None, requireExactMin=False, requireExactMax=False, logOnSuccess=True
190):
191 """! Verifies the min/max image property values. By default (none of the optional arguments
192 provided) the functions just checks if the min/max range specified by the image properties include the actual
193 min/max range of the image data, which is therefore scanned. Note that this check is always done, since ML
194 images always need to fulfill the condition. In addition, it is possible to check against expected values, or
195 to require exact matches of the range boundaries (using the expected values, if provided, and the actual
196 values otherwise). Logs an error if the output image is valid and any of the checks fail.
197 @outFieldName String: Name of the output image field to perform the check on.
198 @param expectedMin Number: If given, the minimum value image property is compared against expectedMin
199 @param expectedMax Number: If given, the maximum value image property is compared against expectedMax
200 @param requireExactMin Bool: If given, the minimum value image property must be identical to expectedMin (if given) or the actual image minimum (otherwise)
201 @param requireExactMax Bool: If given, the maximum value image property must be identical to expectedMax (if given) or the actual image maximum (otherwise)
202 @param logOnSuccess Bool: If enabled, provide success message
203 """
204 errorOccurred = False
205 outField = Base_getTestCaseContext().field(outFieldName)
206 comparisonErrorComment = "Min/max value error for field %s." % outFieldName
207 if outField.isValid():
208 trueMin, trueMax = getTrueMinMaxValues(outFieldName)
209 if trueMin is None or trueMax is None:
210 errorOccurred = True
211 Logging_error(
212 "Image.verifyMinMaxPropertyValues(): Unexpected error scanning the image at field %s!" % outFieldName
213 )
214 else:
215 if expectedMin is None:
216 expectedMin = outField.minValue()
217 if expectedMax is None:
218 expectedMax = outField.maxValue()
219 if requireExactMin:
220 if not Base_compareFloatEqual(trueMin, expectedMin, comparisonErrorComment):
221 errorOccurred = True
222 else:
223 if not Base_compareFloatLessThanOrEqual(expectedMin, trueMin, comparisonErrorComment):
224 errorOccurred = True
225 if requireExactMax:
226 if not Base_compareFloatEqual(trueMax, expectedMax, comparisonErrorComment):
227 errorOccurred = True
228 else:
229 if not Base_compareFloatLessThanOrEqual(trueMax, expectedMax, comparisonErrorComment):
230 errorOccurred = True
231
232 if not errorOccurred and logOnSuccess:
233 Logging_info("Image.verifyMinMaxPropertyValues(%s): SUCCESS" % outFieldName)
234 return
235
236
237# ----------------------------------------------------------------------------------------------}}}-
238
239from .Logging import error as Logging_error, info as Logging_info
240from .Base import (
241 getTestCaseContext as Base_getTestCaseContext,
242 compareFloatEqual as Base_compareFloatEqual,
243 compareFloatLessThanOrEqual as Base_compareFloatLessThanOrEqual,
244)
calculateHashFromImage(fileName, hashMethod="SHA256", encoder="Hex")
Calculate the hash of the given image.
Definition Image.py:37
compareImagesProperties(outFieldA, outFieldB)
Compare just the properties of the two given images (excluding page extent, the extension properties,...
Definition Image.py:138
getTrueMinMaxValues(outFieldName)
Returns the true min/max range of the image data, which is therefore scanned entirely using a MinMaxS...
Definition Image.py:164
compareImagesAllProps(outFieldA, outFieldB)
Compare the two given images.
Definition Image.py:156
getVoxelValue(outFieldName, position, coordinateType="Voxel", useStoredValue=False)
Get the voxel value of the given field at the given position.
Definition Image.py:77
compareImages(outFieldA, outFieldB)
Compare the two given images (excluding the extention properties).
Definition Image.py:128
compareImagesData(outFieldA, outFieldB)
Compare the two given images (only verifying data).
Definition Image.py:147
verifyMinMaxPropertyValues(outFieldName, expectedMin=None, expectedMax=None, requireExactMin=False, requireExactMax=False, logOnSuccess=True)
Verifies the min/max image property values.
Definition Image.py:190