def test_grid_combo(self):
        print(' --------------------------------------------------')
        print('test_grid/combo')

        filename = os.path.abspath(my_path +
                                   "/../test/testdata/MNI_2mm_scaled.grd")

        combo = bis.bisComboTransformation()
        combo.load(filename)

        bsplinegrid = combo.grids[0]

        pt370 = [-1.5743, -0.0616, -1.1677]
        g = bsplinegrid.get_data()

        n = bsplinegrid.getNumberOfControlPoints()
        disp = [g[370], g[370 + n], g[370 + 2 * n]]
        error = 0.0

        print('pt370=', pt370, ' disp=', disp)
        for k in range(0, 3):
            error += abs(disp[k] - pt370[k])
        print("++++ checking bspline grid loading error0=", error)

        obj = {
            "dimensions": self.images[2].dimensions,
            "spacing": self.images[2].spacing,
        }

        wasm_out_g = libbiswasm.computeDisplacementFieldWASM(
            combo.grids[0], obj, 1)
        wasm_out_c = libbiswasm.computeDisplacementFieldWASM(combo, obj, 1)

        print('wasm_out_g=', wasm_out_g.dimensions)
        print('wasm_out_c=', wasm_out_c.dimensions)
        print('gold=', self.images[3].dimensions)

        x = wasm_out_g.get_data() - self.images[3].get_data()
        err_g = max(-x.min(), x.max())
        x = wasm_out_c.get_data() - self.images[3].get_data()
        err_c = max(-x.min(), x.max())

        x = wasm_out_c.get_data() - wasm_out_g.get_data()
        err_cg = max(-x.min(), x.max())
        print('error: grid=', err_g, 'combo=', err_c, ' combo-grid=', err_cg)

        success = False
        if err_g < 0.01 and err_c < 0.01 and err_cg < 0.01 and error < 0.001:
            success = True

        self.assertEqual(success, True)
    def test_grid_combo_savewasm(self):

        filename = os.path.abspath(my_path + "/../test/testdata/complex.grd")

        with open(filename, 'r') as file:
            text = file.read()
        print('=====================================================')
        print('read file length=', len(text))

        combo = bis.bisComboTransformation()
        combo.load(filename)

        s = libbiswasm.createComboTransformationTextFileWASM(combo)

        combo2 = libbiswasm.parseComboTransformTextFileWASM(s)

        bsplinegrid = combo.grids[0]
        bsplinegrid2 = combo2.grids[0]
        n = bsplinegrid.getNumberOfControlPoints()

        grid_data1 = bsplinegrid.get_data()
        grid_data2 = bsplinegrid2.get_data()

        error = 0.0
        data = [5, 77, 104, 2100, 5747]

        for i in range(0, len(data)):
            cp = int(data[i])

            i_data = [
                grid_data2[cp], grid_data2[cp + n], grid_data2[cp + 2 * n]
            ]
            o_data = [
                grid_data1[cp], grid_data1[cp + n], grid_data1[cp + 2 * n]
            ]
            error = 0.0

            print('pt', cp, ' gold=', i_data, ' grd=', o_data)
            for k in range(0, 3):
                error += abs(i_data[k] - o_data[k])

        print(
            "++++ checking bspline grid loading serializing and deserializing error0=",
            error)

        success = False
        if (error < 0.001):
            success = True

        self.assertEqual(success, True)
Beispiel #3
0
def deserialize_object(ptr, datatype='', offset=0, first_input=0):

    if datatype == '':
        datatype = getNameFromMagicCode(magic_code)

    if datatype == 'String':
        output = bis.bisVector()
        output.deserializeWasm(ptr, offset)
        return output.getString()

    if datatype == 'Matrix':
        output = bis.bisMatrix()
        output.deserializeWasm(ptr, offset)
        return output.get_data()

    if datatype == 'Vector':
        output = bis.bisVector()
        output.deserializeWasm(ptr, offset)
        return output.get_data()

    if datatype == 'bisComboTransformation':
        output = bis.bisComboTransformation()
        output.deserializeWasm(ptr, offset)
        return output

    if datatype == 'bisGridTransformation':
        output = bis.bisGridTransformation()
        output.deserializeWasm(ptr, offset)
        return output

    if datatype == 'bisLinearTransformation':
        output = bis.bisLinearTransformation()
        output.deserializeWasm(ptr, offset)
        return output.get_data()

    if datatype != 'bisImage':
        raise ValueError('Unknown datatype ', datatype)

    # Image from here
    output = bis.bisImage()
    output.deserializeWasm(ptr, offset)
    if type(first_input) is bis.bisImage:
        output.affine = first_input.affine

    return output
Beispiel #4
0
def processTestResult(toolname, resultFile, test_target, test_type,
                      test_threshold, test_comparison):

    threshold = test_threshold
    if (threshold == None):
        threshold = 0.01

    comparison = test_comparison
    if (comparison == None):
        comparison = "maxabs"

    if (test_type == 'image'):
        if (comparison != "maxabs" and comparison != "ssd"):
            comparison = "cc"

    if (test_type == 'matrix' or test_type == "matrixtransform"
            or test_type == "gridtransform"):
        if (comparison != "maxabs"):
            comparison = "ssd"

    print(
        '====\n==================================================================\n===='
    )
    print('==== comparing ', test_type, ' using ', comparison,
          ' and threshold=', threshold, '.\n====')

    print('==== files=', resultFile, test_target)

    if (test_type == "image"):
        out = bis_objects.bisImage()
        if (out.load(resultFile) != False):
            gold = bis_objects.bisImage()
            if (gold.load(test_target) != False):
                diff = 0
                if (comparison == 'cc'):
                    diff = -computeCC(out.get_data(), gold.get_data())
                    threshold = -threshold
                elif comparison == 'ssd':
                    diff = computeNorm2(out.get_data(), gold.get_data())
                else:
                    diff = maxabsdiff(gold.get_data(), out.get_data())
                return printResult(diff, threshold, toolname, test_type)
            else:
                print('---- Failed to load gold standard image')
                return False
        else:
            print('---- Failed to load input image')
            return False

    elif (test_type == "matrix"):

        out = bis_objects.bisMatrix()
        if (out.load(resultFile) != False):
            gold = bis_objects.bisMatrix()
            if (gold.load(test_target) != False):
                print('out ', resultFile, ' dims=', out.data_array.shape)
                print('gold ', test_target, ' dims=', gold.data_array.shape)
                if (comparison == 'maxabs'):
                    diff = maxabsdiff(gold.data_array, out.data_array)
                else:
                    diff = computeNorm2(gold.data_array, out.data_array)
                return printResult(diff, threshold, toolname, test_type)
            else:
                return False
        else:
            return False

    elif (test_type == "matrixtransform"):

        out = bis_objects.bisLinearTransformation()
        if (out.load(resultFile) != False):
            gold = bis_objects.bisLinearTransformation()
            if (gold.load(test_target) != False):
                print('out ', resultFile, ' dims=', out.data_array.shape)
                print('gold ', test_target, ' dims=', gold.data_array.shape)
                if (comparison == 'maxabs'):
                    diff = maxabsdiff(gold.data_array, out.data_array)
                else:
                    diff = computeNorm2(gold.data_array, out.data_array)
                return printResult(diff, threshold, toolname, test_type)
            else:
                return False
        else:
            return False

    elif (test_type == "gridtransform"):

        out = bis_objects.bisComboTransformation()
        if (out.load(resultFile) != False):
            print('out ', resultFile, ' dims=', out.grids[0].data_array.shape)
            gold = bis_objects.bisComboTransformation()
            if (gold.load(test_target) != False):
                print('gold ', test_target, ' dims=',
                      gold.grids[0].data_array.shape)
                if (comparison == 'maxabs'):
                    diff = maxabsdiff(gold.grids[0].data_array,
                                      out.grids[0].data_array)
                else:
                    diff = computeNorm2(gold.grids[0].data_array,
                                        out.grids[0].data_array)
                return printResult(diff, threshold, toolname, test_type)
            else:
                return False
        else:
            return False

    print('---- Cannot compare type :', test_type)
    return False