Exemple #1
0
def dicom_convert(population, data_dir, workspace_dir):
    count = 0
    for subject in population:
        count += 1
        print '====================================================================='
        print '%s- DICOM CONVERSION for for %s' % (count, subject)
        # define dicom directory for each subject
        dicom_dir = os.path.join(data_dir, subject, 'DICOM')

        # define desitation directory for nifti outputs
        try:
            os.makedirs(
                os.path.join(workspace_dir, subject, 'anatomical_original'))
        except OSError:
            out_nifti_dir = str(
                os.path.join(workspace_dir, subject, 'anatomical_original'))
        out_nifti_dir = str(
            os.path.join(workspace_dir, subject, 'anatomical_original'))

        if not os.path.isfile(os.path.join(out_nifti_dir, 'ANATOMICAL.nii')):
            # create a list of all dicoms with absolute paths for each file
            dicom_list = []
            for dicom in os.listdir(dicom_dir):
                dicomstr = os.path.join(dicom_dir, dicom)
                dicom_list.append(dicomstr)

            # grab SeriesDescription and append T1 files to list
            T1_list = []
            print 'Reading dicom series descriptions'
            for dicom in dicom_list:
                try:
                    dcm_read = pydicom.read_file(dicom, force=True)
                    sequence = dcm_read.SeriesDescription
                except AttributeError:
                    continue

                if 'mp2rage_p3_602B_UNI_Images' in sequence:
                    T1_list.append(dicom)

            # convert T1 anatomical to NIFTI with SPM
            print 'Converting Dicom to Nifti for %s' % subject
            spm_dicom_convert = spmu.DicomImport()
            spm_dicom_convert.inputs.format = 'nii'
            spm_dicom_convert.inputs.in_files = T1_list
            spm_dicom_convert.inputs.output_dir = out_nifti_dir
            spm_dicom_convert.run()

            #rename file
            for file in os.listdir(out_nifti_dir):
                if file.endswith('nii'):
                    os.rename(
                        str(os.path.join(out_nifti_dir, file)),
                        str(os.path.join(out_nifti_dir, 'ANATOMICAL.nii')))
        else:
            print 'subject already processed.......moving on'

        print '====================================================================='
        print ''
def dicom_convert(population, data_dir, workspace_dir):
    for subject in population:
        print '##############################################'
        print '  SPM DICOM CONVERSION for for %s' % subject
        # define dicom directory for each subject
        dicom_dir = os.path.join(data_dir, subject[0:4], 'RawData', subject)

        # define desitation directory for nifti outputs
        try:
            os.makedirs(
                os.path.join(workspace_dir, subject[0:4],
                             'anatomical_original'))
        except OSError:
            continue

        out_nifti_dir = str(
            os.path.join(workspace_dir, subject[0:4], 'anatomical_original'))

        # create a list of all dicoms with absolute paths for each file
        dicom_list = []
        for dicom in os.listdir(dicom_dir):
            dicom = os.path.join(dicom_dir, dicom)
            dicom_list.append(dicom)

        # grab SeriesDescription and append T1 files to list
        T1_list = []
        print 'reading dicom series descriptions'
        for dicom in dicom_list:
            try:
                dcm_read = pydicom.read_file(dicom)
                sequence = dcm_read.SeriesDescription
            except AttributeError:
                continue

            if 't1_mpr_sag_short' in sequence:
                T1_list.append(dicom)

            else:
                continue

        # convert T1 anatomical to NIFTI with SPM
        print 'Converting Dicom to Nifti for %s' % subject
        spm_dicom_convert = spmu.DicomImport()
        spm_dicom_convert.inputs.format = 'nii'
        spm_dicom_convert.inputs.in_files = T1_list
        spm_dicom_convert.inputs.output_dir = out_nifti_dir
        spm_dicom_convert.run()
        print '..................................'

        #rename file
        for file in os.listdir(out_nifti_dir):
            if file.endswith('nii'):
                os.rename(str(os.path.join(out_nifti_dir, file)),
                          str(os.path.join(out_nifti_dir, 'ANATOMICAL.nii')))

        print '  DONE  %s' % subject
        print '##############################################'
        print ''
Exemple #3
0
def test_dicom_import():
    dicom = example_data(infile='dicomdir/123456-1-1.dcm')
    di = spmu.DicomImport(matlab_cmd='mymatlab')
    assert_equal(di.inputs.matlab_cmd, 'mymatlab')
    assert_equal(di.inputs.output_dir_struct, 'flat')
    assert_equal(di.inputs.output_dir, './converted_dicom')
    assert_equal(di.inputs.format, 'nii')
    assert_equal(di.inputs.icedims, False)
    assert_raises(TraitError, di.inputs.trait_set, output_dir_struct='wrong')
    assert_raises(TraitError, di.inputs.trait_set, format='FAT')
    assert_raises(TraitError, di.inputs.trait_set, in_files=['does_sfd_not_32fn_exist.dcm'])
    di.inputs.in_files = [dicom]
    assert_equal(di.inputs.in_files, [dicom])
Exemple #4
0
def test_dicom_import():
    dicom = example_data(infile='dicomdir/123456-1-1.dcm')
    di = spmu.DicomImport(matlab_cmd='mymatlab')
    assert di.inputs.matlab_cmd == 'mymatlab'
    assert di.inputs.output_dir_struct == 'flat'
    assert di.inputs.output_dir == './converted_dicom'
    assert di.inputs.format == 'nii'
    assert not di.inputs.icedims
    with pytest.raises(TraitError):
        di.inputs.trait_set(output_dir_struct='wrong')
    with pytest.raises(TraitError):
        di.inputs.trait_set(format='FAT')
    with pytest.raises(TraitError):
        di.inputs.trait_set(in_files=['does_sfd_not_32fn_exist.dcm'])
    di.inputs.in_files = [dicom]
    assert di.inputs.in_files == [dicom]
Exemple #5
0
def test_dicom_import():
    dicom = example_data(infile="dicomdir/123456-1-1.dcm")
    di = spmu.DicomImport(matlab_cmd="mymatlab")
    assert di.inputs.matlab_cmd == "mymatlab"
    assert di.inputs.output_dir_struct == "flat"
    assert di.inputs.output_dir == "./converted_dicom"
    assert di.inputs.format == "nii"
    assert not di.inputs.icedims
    with pytest.raises(TraitError):
        di.inputs.trait_set(output_dir_struct="wrong")
    with pytest.raises(TraitError):
        di.inputs.trait_set(format="FAT")
    with pytest.raises(TraitError):
        di.inputs.trait_set(in_files=["does_sfd_not_32fn_exist.dcm"])
    di.inputs.in_files = [dicom]
    assert di.inputs.in_files == [dicom]
Exemple #6
0
def make_svs_anatomical(population, workspace, voxel_name):

    for subject in population:

        subject_dir = os.path.join(workspace, subject)
        dicom_dir   = os.path.join(subject_dir, 'DICOM')
        svs_dir     = os.path.join(subject_dir, 'SVS')
        nifti_dir   = mkdir_path(os.path.join(subject_dir, 'NIFTI'))

        ########## Get MP2RAGE UNI
        if not os.path.isfile( os.path.join(nifti_dir, 'ANATOMICAL.nii')):
            print 'Converting MP2RAGE '

            dicoms = [os.path.join(dicom_dir, dicom) for dicom in os.listdir(dicom_dir)]
            T1_list = []

            for dicom in dicoms:
                try:

                    dcm_read = pydicom.read_file(dicom, force=True)
                    sequence = dcm_read.SeriesDescription
                except AttributeError:
                    continue

                if 'mp2rage_p3_602B_UNI_Images' in sequence:
                    T1_list.append(dicom)

            # convert T1 anatomical to NIFTI with SPM
            print 'Converting Dicom to Nifti for %s' % subject
            spm_dicom_convert = spmu.DicomImport()
            spm_dicom_convert.inputs.format = 'nii'
            spm_dicom_convert.inputs.in_files = T1_list
            spm_dicom_convert.inputs.output_dir = nifti_dir
            spm_dicom_convert.run()

            # rename output file
            os.system('mv %s/*nii %s/ANATOMICAL.nii' %(nifti_dir, nifti_dir))


        # make svs mask
        rda =[]
        if not os.path.isfile(os.path.join(svs_dir, voxel_name, 'RDA', '.nii')):
            for root, dirs, files, in os.walk(os.path.join(svs_dir, voxel_name, 'RDA'), topdown=False):
                for name in files:
                    if 'supp' in name and 'edit1' in name:
                        rda.append(os.path.join(name))

            if rda is []:
                print 'RDA metabolite data does not exist for subject %s' % subject


            T1Path = os.path.join(subject_dir, 'NIFTI' + '/')
            T1Image = 'ANATOMICAL.nii'
            svs_path = os.path.join(svs_dir, voxel_name, 'RDA' + '/')
            svs_file = rda[0]

            matlab_command = ['matlab', '--version', '8.2', '-nodesktop', '-nosplash', '-nojvm',
                              '-r "RDA_TO_NIFTI(\'%s\', \'%s\', \'%s\', \'%s\') ; quit;"'
                              % (T1Path, T1Image, svs_path, svs_file)]
            subprocess.call(matlab_command)

            os.system('mv %s/*Mask.nii %s' % (nifti_dir, os.path.join(svs_dir, voxel_name, 'RDA')))
            os.system('mv %s/*coord.txt %s' % (nifti_dir, os.path.join(svs_dir, voxel_name, 'RDA')))