示例#1
0
    def loadSingleInput(self, key, filename, objecttype):

        if (objecttype == 'image'):
            self.inputs[key] = bis_objects.bisImage()
        elif (objecttype == 'matrix' or objecttype == 'vector'):
            self.inputs[key] = bis_objects.bisMatrix()
        elif (objecttype == 'transformation' or objecttype == 'transform'):
            self.inputs[key] = bis_objects.loadTransformation(filename)
            if (self.inputs[key] == None):
                return False
            return True

        try:
            ok = self.inputs[key].load(filename)
            if (ok != False):
                sz = self.inputs[key].data_array.shape
                if (sz[1] == 1 and objecttype == 'vector'):
                    tmp = self.inputs[key]
                    self.inputs[key] = bis_objects.bisVector()
                    self.inputs[key].create(tmp.data_array.flatten())
                    ok = self.inputs[key]

        except:
            return False

        print('++++ \t loaded ' + objecttype + ' ' + key + ' from ' + filename)
        if (ok != False):
            return True
        return ok
示例#2
0
    def test_resample(self):
        imgnames = [
            'avg152T1_LR_nifti_resampled.nii.gz', 'avg152T1_LR_nifti.nii.gz',
            'avg152T1_LR_nifti_resampled_resliced.nii.gz'
        ]
        images = [0, 0, 0]
        names = [' reference', 'target', 'true']

        print('\n\n')
        print('----------------------------------------------------------')
        for i in range(0, 3):
            name = my_path + '/../test/testdata/' + imgnames[i]
            images[i] = bis.bisImage().load(name)
            print('__ loaded ', names[i], 'from ', name, 'dims=',
                  images[i].dimensions, images[i].spacing,
                  images[i].dimensions, images[i].get_data().dtype)
        print('----------------------------------------------------------')

        reslice_matr = [[0.866, -0.525, 0.000, 68.758],
                        [0.500, 0.909, 0.000, 9.793],
                        [0.000, 0.000, 1.000, 2.250],
                        [0.000, 0.000, 0.000, 1.000]]

        matr = np.zeros([4, 4], dtype=np.float32)
        for row in range(0, 4):
            for col in range(0, 4):
                matr[row][col] = reslice_matr[row][col]

        reference_image = images[0]
        target_image = images[1]
        true_image = images[2]

        paramobj = {
            "interpolation": 1,
            "dimensions": reference_image.dimensions,
            "spacing": reference_image.spacing,
            "datatype": "float",
            "backgroundValue": 0.0,
        }

        print('----------------------------------------------------------')

        out_obj = libbis.resliceImageWASM(images[1], matr, paramobj, debug=2)
        cc = np.corrcoef(images[2].get_data().flatten(),
                         out_obj.get_data().flatten())[0, 1]

        if cc > 0.999:
            testpass = True
        else:
            testpass = False

        print('----------------------------------------------------------')
        print('__ post reslicing correlation out v true=', cc, 'pass='******'----------------------------------------------------------')

        self.assertEqual(testpass, True)
示例#3
0
    def test_resample_module(self):

        imgnames = [
            'avg152T1_LR_nifti_resampled.nii.gz', 'avg152T1_LR_nifti.nii.gz',
            'avg152T1_LR_nifti_resampled_resliced.nii.gz'
        ]
        images = [0, 0, 0]
        names = [' reference', 'target', 'true']

        print('\n\n')
        print('----------------------------------------------------------')
        for i in range(0, 3):
            name = my_path + '/../test/testdata/' + imgnames[i]
            images[i] = bis.bisImage().load(name)
            print('__ loaded ', names[i], 'from ', name, 'dims=',
                  images[i].dimensions, images[i].spacing,
                  images[i].dimensions, images[i].get_data().dtype)
        print('----------------------------------------------------------')

        xformname = my_path + '/testdata/newtests/reslice_transform.matr'
        xform = bis.bisLinearTransformation()
        xform.load(xformname)

        module = resliceImage.resliceImage()
        reference_image = images[0]
        target_image = images[1]
        true_image = images[2]

        module.execute(
            {
                'input': target_image,
                'reference': reference_image,
                'transform': xform
            }, {'debug': True})
        out_obj = module.getOutputObject('output')
        cc = np.corrcoef(images[2].get_data().flatten(),
                         out_obj.get_data().flatten())[0, 1]

        if cc > 0.999:
            testpass = True
        else:
            testpass = False

        print(
            '----------------------------------------------------------------------'
        )
        print('__ post module reslicing correlation out v true=', cc, 'pass='******'----------------------------------------------------------------------'
        )

        self.assertEqual(testpass, True)
    def setUp(self):
        print(' --------------------------------------------------')
        self.imgnames = [
            'MNI_2mm_orig.nii.gz', 'MNI_2mm_scaled.nii.gz', 'MNI_6mm.nii.gz',
            'MNI_6mm_scaleddispfield.nii.gz'
        ]

        self.images = [0, 0, 0, 0]
        for i in range(0, 4):
            name = my_path + '/../test/testdata/' + self.imgnames[i]
            self.images[i] = bis.bisImage().load(name)
            print('__ loaded image', i, 'from ', name, 'dims=',
                  self.images[i].dimensions, self.images[i].spacing,
                  self.images[i].dimensions, self.images[i].get_data().dtype)
            print('----------------------------------------------------------')
示例#5
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
示例#6
0
a=len(sys.argv);

if a<5 :
    print('\n Not enough argmments specified\n\tUsage: indivParc indivfmri groupparc numexemplar smooth output');
    sys.exit(1)

imagename1=sys.argv[1];
imagename2=sys.argv[2];
numexemplar=int(sys.argv[3]);
smooth=int(sys.argv[4]);
output=sys.argv[5];

print('++++ Beginning image names',imagename1,imagename2,output,'\n\n');


fmri=bis.bisImage().load(imagename1); print('++++ \t fmri loaded from',imagename1,' dims=',fmri.dimensions);
group=bis.bisImage().load(imagename2); print('++++ \t group loaded from',imagename2,' dims=',group.dimensions);
print('++++\n calling C++ code\n');

paramobj= { 'numberofexemplars' : numexemplar };


if (fmri.dimensions != group.dimensions):
	group_resliced = group;
	print('++++ \t No reslicing required...');
else:   
	print('++++ \t Group parcellation being resliced to match the fMRI image dimension...');
	resl_paramobj = {
            "interpolation" : 0,
            "dimensions" : fmri.dimensions,
            "spacing" : fmri.spacing,
示例#7
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
示例#8
0
a=len(sys.argv);

if a<5 :
    print('\n Not enough argmments specified\n\tUsage: individualizedParcellation fmri groupparc numregions smooth output');
    sys.exit(1)

imagename1=sys.argv[1];
imagename2=sys.argv[2];
numregions=int(sys.argv[3]);
smooth=int(sys.argv[4]);
output=sys.argv[5];

print('++++ Beginning image names',imagename1,imagename2,output,'\n\n');


fmri=bis.bisImage().load(imagename1); print('++++ \t fmri loaded from',imagename1,' dims=',fmri.dimensions);
group=bis.bisImage().load(imagename2); print('++++ \t original (group) parcellation loaded from',imagename2,' dims=',group.dimensions);
print('++++\n calling C++ code\n');

paramobj= { 'numberofexemplars' : numregions };


if (fmri.dimensions != group.dimensions):
	group_resliced = group;
	print('++++ \t No reslicing required...');
else:   
	print('++++ \t Original (group) parcellation being resliced to match the fMRI image dimension...');
	resl_paramobj = {
            "interpolation" : 0,
            "dimensions" : fmri.dimensions,
            "spacing" : fmri.spacing,