11 from mevis
import MLAB
12 from TestSupport
import Base
24 "int16": (-32768,32767),
25 "int32": (-2147483648,2147483647),
26 "int64": (-9223372036854775808,9223372036854775807),
29 "uint32": (0,4294967295),
30 "uint64": (0,18446744073709551615)
39 if isinstance(testDictionaries, (list, tuple)):
41 for d
in testDictionaries:
44 testDict = testDictionaries
46 testDict[key][0] = function
59 alsoInitNonEditableFields=False ):
60 context = Base.getTestCaseContext()
62 msg=
"Target module {} not found!".format( targetModuleName ), logOnSuccess=
False )
63 if templateModuleName
is None:
64 templateModuleName =
"{}_ParameterTemplate".format( targetModuleName )
66 def ___applyTemplateParameters():
67 ASSERT_TRUE( context.hasModule( templateModuleName ),
68 msg=
"Template module {} not found!".format( templateModuleName ), logOnSuccess=
False )
69 for f
in context.module( templateModuleName ).parameterFields():
70 if f.name !=
"instanceName" and f.type !=
"Trigger" and ( alsoInitNonEditableFields
or f.isEditable() ):
71 value = f.vectorValue()
if "vec" in f.type.lower()
else f.value
72 context.field(
"{}.{}".format( targetModuleName, f.name ) ).updateValue( value )
74 def ___applyOverrideParameters():
75 targetModule = context.module( targetModuleName )
76 for fieldName, fieldValue
in fieldValueOverrides.items():
78 msg =
"Invalid field value override: Field {} does not exist.".format( fieldName ), logOnSuccess=
False )
79 targetModule.field( fieldName ).updateValue( fieldValue )
81 ___applyTemplateParameters()
82 if fieldValueOverrides:
83 ___applyOverrideParameters()
92 """Determines max/min value of given fill value(s)."""
93 if isinstance(fv1, six.string_types):
94 fv1 = sorted([float(comp)
for comp
in fv1.split()])
97 max1 = fv1[len(fv1)-1]
101 if isinstance(fv2, six.string_types):
102 fv2 = sorted([float(comp)
for comp
in fv2.split()])
105 max2 = fv2[len(fv2)-1]
107 min = min1
if min1 < min2
else min2
108 max = max1
if max1 > max2
else max2
118 """Returns the number of components of the image data type."""
119 mlType = imageField.image().dataType()
120 return MLAB.ML.MLTypeGetNumComponents(mlType)
127 if "int" in dataType
or "float" in dataType
or "double" in dataType:
129 elif "complex" in dataType:
131 elif "quaternion" in dataType:
136 if i
in "0123456789":
141 num = int(num)**2
if "mat" in dataType
else int(num)
150 """Returns a NumPy data type object used with the image."""
152 if MLAB.ML.MLIsStandardType(imageField.image().dataType()):
153 return type(imageField.image().getTile((0, 0, 0), (1, 1, 1))[0][0][0])
157 td = context.addModule(MLAB.moduleLiteral(
"TypeDecomposer64"))
159 raise Exception(
"Use parameter \"context = ctx\" with \"getNumpyType()\" because the image uses an adv. data type!")
160 td.field(
"input0").connectFrom(imageField)
161 npType = type(td.field(
"output0").image().getTile((0, 0, 0), (1, 1, 1))[0][0][0])
170 if "unsigned" in dataType:
173 elif "16" in dataType:
174 npType = numpy.uint16
175 elif "32" in dataType:
176 npType = numpy.uint32
177 elif "64" in dataType:
178 npType = numpy.uint64
180 elif "int8" in dataType
or "i8" in dataType:
182 elif "int16" in dataType
or "i16" in dataType:
184 elif "int32" in dataType
or "i32" in dataType:
186 elif "int64" in dataType
or "i64" in dataType:
189 elif dataType
in (
"float",
"complexf",
"quaternionf")
or "vecf" in dataType
or "matf" in dataType:
190 npType = numpy.float32
191 elif dataType
in (
"double",
"complexd",
"quaterniond")
or "vec" in dataType
or "mat" in dataType:
192 npType = numpy.float64
193 elif dataType
in (
"long double",
"complexld",
"quaternionld"):
194 npType = numpy.float64
196 elif "stringuchar" in dataType:
209 """Returns the value casted to the given NumPy data type."""
210 typeName = numpyType.__name__
212 if (
"uint" in typeName
and (value < INTTYPES[
"uint32"][0]
or value > INTTYPES[
"uint32"][1]))
or \
213 (
"uint" not in typeName
and "int" in typeName
and (value < INTTYPES[
"int32"][0]
or value > INTTYPES[
"int32"][1])):
214 if sys.version_info.major == 2:
219 value = value % (INTTYPES[typeName][1]+1) + \
220 INTTYPES[typeName][0] * (value // (INTTYPES[typeName][1]+1) % 2)
221 return numpyType(value)
230 """Coverts fill values of any input data type to std output data type."""
238 newVoxel = voxel[0]
if "quaternion" not in oldType
else voxel[3]
248 def getImage3D(imageField, imageStart = (0, 0, 0), imageSize =
None, context =
None):
251 imageSize = imageField.size3D()
256 td = context.addModule(MLAB.moduleLiteral(
"TypeDecomposer64"))
258 raise Exception(
"Use parameter \"context = ctx\" with \"getImage3D()\" because the image uses an adv. data type!")
260 td.field(
"input0").connectFrom(imageField)
261 for comp
in range(numComp):
262 image.append(td.field(
"output{0}".format(comp)).image().getTile(imageStart, imageSize))
266 image.append(imageField.image().getTile(imageStart, imageSize))
275 def getImage6D(imageField, imageStart = (0, 0, 0, 0, 0, 0), imageSize =
None, context =
None):
278 imageSize = imageField.size6D()
283 td = context.addModule(MLAB.moduleLiteral(
"TypeDecomposer64"))
285 raise Exception(
"Use parameter \"context = ctx\" with \"getImage6D()\" because the image uses an adv. data type!")
287 td.field(
"input0").connectFrom(imageField)
288 for comp
in range(numComp):
289 image.append(td.field(
"output{0}".format(comp)).image().getTile(imageStart, imageSize))
293 image.append(imageField.image().getTile(imageStart, imageSize))
306 numComp = len(expectedImage)
307 expectedSize = [len(expectedImage[0][0][0]),
308 len(expectedImage[0][0]),
309 len(expectedImage[0])]
312 expectedImage = [expectedImage]
313 expectedSize = [len(expectedImage[0][0][0]),
314 len(expectedImage[0][0]),
315 len(expectedImage[0])]
318 resultSize = [len(resultImage[0][0][0]),
319 len(resultImage[0][0]),
322 resultImage = [resultImage]
323 resultSize = [len(resultImage[0][0][0]),
324 len(resultImage[0][0]),
327 if resultSize != expectedSize:
328 ERROR(
"Image compare failed: expected and output image are not not of the same size >> " \
329 +
"result {0} != expected {1}".format(resultSize, expectedSize))
332 elif numComp != len(resultImage):
333 ERROR(
"Image compare failed: expected and output image have different number of data type components >> " \
334 +
"result {0} != expected {1}".format(numComp, len(resultImage)))
337 if not numpy.allclose(expectedImage, resultImage, 0, EPSILON)
or LOGONSUCCESS:
338 for z
in range(resultSize[2]):
339 for y
in range(resultSize[1]):
340 for x
in range(resultSize[0]):
341 for comp
in range(numComp):
343 finiteExp = numpy.isfinite(expectedImage[comp][z][y][x])
344 finiteOut = numpy.isfinite(resultImage[comp][z][y][x])
346 if finiteExp != finiteOut \
347 or (finiteExp, finiteOut) == (
True,
True) \
348 and abs(resultImage[comp][z][y][x] - expectedImage[comp][z][y][x]) > EPSILON:
350 ERROR(
"""Voxel ({0},{1},{2}) of Result and Expected image are not equal (Epsilon = {3}):
351 >> {4} == {5} : False""".format(x, y, z, EPSILON, \
352 [resultImage[comp][z][y][x]
for comp
in range(numComp)], \
353 [expectedImage[comp][z][y][x]
for comp
in range(numComp)]))
358 elif LOGVOXEL
and LOGONSUCCESS
and comp == numComp-1:
359 INFO(
"""Voxel ({0},{1},{2}) of Result and Expected image are equal (Epsilon = {3}):
360 >> {4} == {5} : True""".format(x, y, z, EPSILON, \
361 [resultImage[comp][z][y][x]
for comp
in range(numComp)], \
362 [expectedImage[comp][z][y][x]
for comp
in range(numComp)]))
365 INFO(
"Image compare passed: expected and out image are equal")
367 ERROR(
"Image compare failed: expected and output image are not equal >> " \
368 +
"{0} of {1} voxels are different".format(numErrors, resultSize[0] * resultSize[1] * resultSize[2]))
381 numComp = len(expectedImage)
382 expectedSize = [len(expectedImage[0][0][0][0][0][0]),
383 len(expectedImage[0][0][0][0][0]),
384 len(expectedImage[0][0][0][0]),
385 len(expectedImage[0][0][0]),
386 len(expectedImage[0][0]),
387 len(expectedImage[0])]
390 expectedImage = [expectedImage]
391 expectedSize = [len(expectedImage[0][0][0][0][0][0]),
392 len(expectedImage[0][0][0][0][0]),
393 len(expectedImage[0][0][0][0]),
394 len(expectedImage[0][0][0]),
395 len(expectedImage[0][0]),
396 len(expectedImage[0])]
399 resultSize = [len(resultImage[0][0][0][0][0][0]),
400 len(resultImage[0][0][0][0][0]),
401 len(resultImage[0][0][0][0]),
402 len(resultImage[0][0][0]),
403 len(resultImage[0][0]),
407 resultImage = [resultImage]
408 resultSize = [len(resultImage[0][0][0][0][0][0]),
409 len(resultImage[0][0][0][0][0]),
410 len(resultImage[0][0][0][0]),
411 len(resultImage[0][0][0]),
412 len(resultImage[0][0]),
415 if resultSize != expectedSize:
416 ERROR(
"Image compare failed: expected and output image are not not of the same size >> " \
417 +
"result {0} != expected {1}".format(resultSize, expectedSize))
420 elif numComp != len(resultImage):
421 ERROR(
"Image compare failed: expected and output image have different number of data type components >> " \
422 +
"result {0} != expected {1}".format(numComp, len(resultImage)))
425 if not numpy.allclose(expectedImage, resultImage, 0, EPSILON)
or LOGONSUCCESS:
426 for u
in range(resultSize[5]):
427 for t
in range(resultSize[4]):
428 for c
in range(resultSize[3]):
429 for z
in range(resultSize[2]):
430 for y
in range(resultSize[1]):
431 for x
in range(resultSize[0]):
432 for comp
in range(numComp):
434 finiteExp = numpy.isfinite(expectedImage[comp][u][t][c][z][y][x])
435 finiteOut = numpy.isfinite(resultImage[comp][u][t][c][z][y][x])
437 if finiteExp != finiteOut \
438 or (finiteExp, finiteOut) == (
True,
True) \
439 and abs(resultImage[comp][u][t][c][z][y][x] - expectedImage[comp][u][t][c][z][y][x]) > EPSILON:
441 ERROR(
"""Voxel ({0},{1},{2},{3},{4},{5}) of Result and Expected image are not equal (Epsilon = {6}):
442 >> {7} == {8} : False""".format(x, y, z, c, t, u, EPSILON, \
443 [resultImage[comp][u][t][c][z][y][x]
for comp
in range(numComp)], \
444 [expectedImage[comp][u][t][c][z][y][x]
for comp
in range(numComp)]))
449 elif LOGVOXEL
and LOGONSUCCESS
and comp == numComp-1:
450 INFO(
"""Voxel ({0},{1},{2},{3},{4},{5}) of Result and Expected image are equal (Epsilon = {6}):
451 >> {7} == {8} : True""".format(x, y, z, c, t, u, EPSILON, \
452 [resultImage[comp][u][t][c][z][y][x]
for comp
in range(numComp)], \
453 [expectedImage[comp][u][t][c][z][y][x]
for comp
in range(numComp)]))
456 INFO(
"Image compare passed: expected and out image are equal")
458 ERROR(
"Image compare failed: expected and output image are not equal >> " \
459 +
"{0} of {1} voxels are different".format(numErrors, resultSize[0] * resultSize[1] * resultSize[2] * resultSize[3] * resultSize[4] * resultSize[5]))
def getNumComponentsFromImage(imageField)
Returns the number of components of an ML-image data type.
def getMinMax(fv1, fv2=None)
Determines max/min value of given fill value(s).
def generateTestDictionary(function, testDictionaries)
Concatenates test dictionaries and sets a function that needs to be tested in the final dictionary.
def getNumpyTypeFromImage(imageField, context=None)
Returns the NumPy data type of an ML-Image.
def getNumpyTypeFromString(dataType)
Returns the NumPy data type appropriate to a data type given as string.
def convertVoxelType(voxel, oldType, newType)
Converts a voxel to a new data type.
def getNumComponentsFromString(dataType)
Returns the number of components of a data type given as string.
def getImage6D(imageField, imageStart=(0, 0, 0, 0, 0, 0), imageSize=None, context=None)
Returns ML image output of given module in 6 dimensions.
def castToNumpy(value, numpyType)
Casts a value to a NumPy type.
def compareImages6D(resultImage, expectedImage)
Compares voxels of a result image with an expected image in 6 dimensions.
def initModuleFromTemplate(targetModuleName, templateModuleName=None, fieldValueOverrides=None, alsoInitNonEditableFields=False)
Copies all values from a reference module's editable parameter fields to the target ('test') module.
def compareImages3D(resultImage, expectedImage)
Compares voxels of a result image with an expected image in 3 dimensions.
def getImage3D(imageField, imageStart=(0, 0, 0), imageSize=None, context=None)
Returns ML image output of given module in 3 dimensions.
Adds GoogleTest like methods.
def ERROR(msg)
Create an error message.
def INFO(msg)
Create an info message.
def ASSERT_TRUE(expr, msg=None, logOnSuccess=None)
Throw exception if given expression does not evaluate to true.