Beispiel #1
0
def process_labels2(folder):
    """ Post process data tumor volume"""
    print(folder)
    util.setup(folder)
    conn = sqlite3.connect(util.DB_PATH, timeout=120)
    conn.text_factory = str
    cursor = conn.execute('''SELECT pid from Patient where study_id = ?''', ("qol_grade3,4", ))

    atlas_path = "/home/dahoiv/disk/Dropbox/Jobb/gbm/Atlas/Hammers/Hammers_mith-n30r95-MaxProbMap-full-MNI152-SPM12.nii.gz"
    resample = slicer.registration.brainsresample.BRAINSResample(command=util.BRAINSResample_PATH,
                                                                 inputVolume=atlas_path,
                                                                 outputVolume=os.path.abspath(folder +
                                                                                              'Hammers_mith-n30r95-MaxProbMap-full'
                                                                                              '-MNI152-SPM12_resample.nii.gz'),
                                                                 referenceVolume=os.path.abspath(util.TEMPLATE_VOLUME))
    resample.run()

    img = nib.load(folder + 'Hammers_mith-n30r95-MaxProbMap-full-MNI152-SPM12_resample.nii.gz')
    lobes_brain = img.get_data()
    label_defs = util.get_label_defs_hammers_mith()
    res_lobes_brain = {}

    book = Workbook()
    sheet = book.active

    sheet.cell(row=1, column=1).value = 'PID'
    sheet.cell(row=1, column=2).value = 'Lobe'
    # sheet.cell(row=1, column=3).value = 'Center of mass'
    k = 2
    for pid in cursor:
        pid = pid[0]

        _id = conn.execute('''SELECT id from Images where pid = ?''', (pid, )).fetchone()
        if not _id:
            print("---No data for ", pid)
            continue
        _id = _id[0]

        _filepath = conn.execute("SELECT filepath_reg from Labels where image_id = ?",
                                 (_id, )).fetchone()[0]
        if _filepath is None:
            print("No filepath for ", pid)
            continue

        com, com_idx = util.get_center_of_mass(util.DATA_FOLDER + _filepath)

        lobe = label_defs.get(lobes_brain[com_idx[0], com_idx[1], com_idx[2]], 'other')
        res_lobes_brain[pid] = lobe

        sheet.cell(row=k, column=1).value = pid
        sheet.cell(row=k, column=2).value = lobe
        sheet.cell(row=k, column=4).value = str(com_idx[0]) + " " + str(com_idx[1]) + " " + str(com_idx[2])
        k += 1

    book.save("brain_lobes_Hammers_mith_n30r95.xlsx")

    print(res_lobes_brain, len(res_lobes_brain))
def process_labels(folder, pids_to_exclude=()):
    """ Post process data tumor volume"""
    print(folder)
    util.setup(folder, 'MolekylareMarkorer')
    conn = sqlite3.connect(util.DB_PATH, timeout=120)
    conn.text_factory = str
    cursor = conn.execute(
        '''SELECT pid from MolekylareMarkorer ORDER BY pid''')

    atlas_path = util.ATLAS_FOLDER_PATH + 'Hammers/Hammers_mith-n30r95-MaxProbMap-full-MNI152-SPM12.nii.gz'
    atlas_resampled_path = folder + 'Hammers_mith-n30r95-MaxProbMap-full-MNI152-SPM12_resample.nii.gz'
    resample = brainsresample.BRAINSResample(
        command=util.BRAINSResample_PATH,
        inputVolume=atlas_path,
        outputVolume=os.path.abspath(atlas_resampled_path),
        referenceVolume=os.path.abspath(util.TEMPLATE_VOLUME))
    resample.run()

    img = nib.load(atlas_resampled_path)
    lobes_brain = img.get_data()
    label_defs = util.get_label_defs_hammers_mith()
    res_lobes_brain = {}

    coordinates_svz = util.get_label_coordinates(util.ATLAS_FOLDER_PATH +
                                                 'SubventricularZone2.nii.gz')
    surface_dg = util.get_surface(util.ATLAS_FOLDER_PATH +
                                  'DentateGyrus.nii.gz')

    book = Workbook()
    sheet = book.active

    sheet.cell(row=1, column=1).value = 'PID'
    sheet.cell(row=1, column=2).value = 'MM'
    sheet.cell(row=1, column=3).value = 'Lobe, center of tumor'
    sheet.cell(row=1,
               column=4).value = 'Distance from SVZ to center of tumor (mm)'
    sheet.cell(row=1,
               column=5).value = 'Distance from SVZ to border of tumor (mm)'
    sheet.cell(row=1,
               column=6).value = 'Distance from DG to center of tumor (mm)'
    sheet.cell(row=1,
               column=7).value = 'Distance from DG to border of tumor (mm)'
    sheet.cell(row=1, column=8).value = 'Tumor volume (mm^3)'
    i = 8
    label_defs_to_column = {}
    for key in label_defs:
        i += 1
        sheet.cell(row=1, column=i).value = label_defs[key]
        label_defs_to_column[key] = i
    k = 2
    for pid in cursor:
        pid = pid[0]

        if pid in pids_to_exclude:
            continue

        _id = conn.execute('''SELECT id from Images where pid = ?''',
                           (pid, )).fetchone()
        if not _id:
            print("---No data for ", pid)
            continue
        _id = _id[0]

        _filepath = conn.execute(
            "SELECT filepath_reg from Labels where image_id = ?",
            (_id, )).fetchone()[0]
        if _filepath is None:
            print("No filepath for ", pid)
            continue

        com, com_idx = util.get_center_of_mass(util.DATA_FOLDER + _filepath)
        surface = util.get_surface(util.DATA_FOLDER + _filepath)

        print(pid, com_idx)

        dist_from_svz_to_com = distance.cdist(coordinates_svz, [com],
                                              'euclidean').min()
        dist_from_svz_to_border = distance.cdist(coordinates_svz,
                                                 surface['point_cloud'],
                                                 'euclidean').min()
        dist_from_dg_to_com = util.get_min_distance(surface_dg, [com])
        dist_from_dg_to_border = util.get_min_distance(surface_dg,
                                                       surface['point_cloud'])

        lobe = label_defs.get(lobes_brain[com_idx[0], com_idx[1], com_idx[2]],
                              'other')
        res_lobes_brain[pid] = lobe

        img = nib.load(util.DATA_FOLDER + _filepath)
        tumor_data = img.get_data()
        voxel_size = img.header.get_zooms()
        voxel_volume = np.prod(voxel_size[0:3])
        n_voxels = (tumor_data > 0).sum()
        tumor_volume = n_voxels * voxel_volume

        union_data = lobes_brain * tumor_data
        union_data = union_data.flatten()
        lobe_overlap = ''
        for column in range(1, 1 + max(label_defs_to_column.values())):
            sheet.cell(row=k, column=column).value = 0
        for _lobe in np.unique(union_data):
            column = label_defs_to_column.get(_lobe)
            if column is None:
                continue
            sheet.cell(row=k, column=column).value = 1
            lobe_overlap += label_defs.get(_lobe, '') + ', '

        _mm = conn.execute(
            "SELECT Subgroup from MolekylareMarkorer where pid = ?",
            (pid, )).fetchone()[0]

        sheet.cell(row=k, column=1).value = pid
        sheet.cell(row=k, column=2).value = str(_mm)
        sheet.cell(row=k, column=3).value = lobe
        sheet.cell(row=k, column=4).value = round(dist_from_svz_to_com, 2)
        sheet.cell(row=k, column=5).value = round(dist_from_svz_to_border, 2)
        sheet.cell(row=k, column=6).value = round(dist_from_dg_to_com, 2)
        sheet.cell(row=k, column=7).value = round(dist_from_dg_to_border, 2)
        sheet.cell(row=k, column=8).value = round(tumor_volume, 1)

        k += 1

    book.save(folder + "brain_lobes_Hammers_mith_n30r95.xlsx")

    print(res_lobes_brain, len(res_lobes_brain))
Beispiel #3
0
def process_labels(folder):
    """ Post process data tumor volume"""
    print(folder)
    util.setup(folder, 'MolekylareMarkorer')
    conn = sqlite3.connect(util.DB_PATH, timeout=120)
    conn.text_factory = str
    cursor = conn.execute(
        '''SELECT pid from MolekylareMarkorer ORDER BY pid''')

    atlas_path = "/home/dahoiv/disk/Dropbox/Jobb/gbm/Atlas/Hammers/Hammers_mith-n30r95-MaxProbMap-full-MNI152-SPM12.nii.gz"
    resample = slicer.registration.brainsresample.BRAINSResample(
        command=BRAINSResample_PATH,
        inputVolume=atlas_path,
        outputVolume=os.path.abspath(folder +
                                     'Hammers_mith-n30r95-MaxProbMap-full'
                                     '-MNI152-SPM12_resample.nii.gz'),
        referenceVolume=os.path.abspath(util.TEMPLATE_VOLUME))
    resample.run()

    img = nib.load(
        folder +
        'Hammers_mith-n30r95-MaxProbMap-full-MNI152-SPM12_resample.nii.gz')
    lobes_brain = img.get_data()
    label_defs = util.get_label_defs_hammers_mith()
    res_lobes_brain = {}

    book = Workbook()
    sheet = book.active

    sheet.cell(row=1, column=1).value = 'PID'
    sheet.cell(row=1, column=3).value = 'MM'
    sheet.cell(row=1, column=3).value = 'Lobe, center of tumor'
    i = 3
    label_defs_to_column = {}
    for key in label_defs:
        i += 1
        sheet.cell(row=1, column=i).value = label_defs[key]
        label_defs_to_column[key] = i
    # sheet.cell(row=1, column=3).value = 'Center of mass'
    k = 2
    for pid in cursor:
        pid = pid[0]

        _id = conn.execute('''SELECT id from Images where pid = ?''',
                           (pid, )).fetchone()
        if not _id:
            print("---No data for ", pid)
            continue
        _id = _id[0]

        _filepath = conn.execute(
            "SELECT filepath_reg from Labels where image_id = ?",
            (_id, )).fetchone()[0]
        if _filepath is None:
            print("No filepath for ", pid)
            continue

        com, com_idx = util.get_center_of_mass(util.DATA_FOLDER + _filepath)

        print(pid, com_idx)

        lobe = label_defs.get(lobes_brain[com_idx[0], com_idx[1], com_idx[2]],
                              'other')
        res_lobes_brain[pid] = lobe

        tumor_data = nib.load(util.DATA_FOLDER + _filepath).get_data()
        union_data = lobes_brain * tumor_data
        union_data = union_data.flatten()
        lobe_overlap = ''
        for column in range(1, 1 + max(label_defs_to_column.values())):
            sheet.cell(row=k, column=column).value = 0
        for _lobe in np.unique(union_data):
            column = label_defs_to_column.get(_lobe)
            if column is None:
                continue
            sheet.cell(row=k, column=column).value = 1
            lobe_overlap += label_defs.get(_lobe, '') + ', '

        _mm = conn.execute(
            "SELECT Subgroup from MolekylareMarkorer where pid = ?",
            (pid, )).fetchone()[0]

        sheet.cell(row=k, column=1).value = pid
        sheet.cell(row=k, column=2).value = str(_mm)
        sheet.cell(row=k, column=3).value = lobe

        k += 1

    book.save(folder + "brain_lobes_Hammers_mith_n30r95.xlsx")

    print(res_lobes_brain, len(res_lobes_brain))
Beispiel #4
0
def process_labels(folder, pids_to_exclude=None):
    """ Create Excel-document with overview of which brain areas are affected by the tumors"""
    print(folder)
    util.setup(folder)
    conn = sqlite3.connect(util.DB_PATH, timeout=120)
    conn.text_factory = str
    pids, image_ids = util.get_pids_and_image_ids(study_id="GBM_survival_time",exclude_pid=pids_to_exclude)


    atlas_path = util.ATLAS_FOLDER_PATH + "Hammers/Hammers_mith-n30r95-MaxProbMap-full-MNI152-SPM12.nii.gz"
    atlas_resampled_path = folder + 'Hammers_mith-n30r95-MaxProbMap-full-MNI152-SPM12_resample.nii.gz'
    resample = brainsresample.BRAINSResample(command=util.BRAINSResample_PATH,
                                             inputVolume=atlas_path,
                                             outputVolume=os.path.abspath(atlas_resampled_path),
                                             referenceVolume=os.path.abspath(util.TEMPLATE_VOLUME))
    resample.run()

    img = nib.load(atlas_resampled_path)
    lobes_brain = img.get_data()
    label_defs = util.get_label_defs_hammers_mith()
    res_lobes_brain = {}

    ventricle_label = 49
    com_ventricle, com_idx_ventricle =  util.get_center_of_mass(folder + 'Hammers_mith-n30r95-MaxProbMap-full-MNI152-SPM12_resample.nii.gz',ventricle_label)

    book = Workbook()
    sheet = book.active

    sheet.cell(row=1, column=1).value = 'PID'
    sheet.cell(row=1, column=2).value = 'Lobe, center of tumor'
    sheet.cell(row=1, column=3).value = 'Distance from center of 3rd ventricle to center of tumor (mm)'
    sheet.cell(row=1, column=4).value = 'Distance from center of 3rd ventricle to border of tumor (mm)'
    i = 4
    label_defs_to_column = {}
    for key in label_defs:
        i += 1
        sheet.cell(row=1, column=i).value = label_defs[key]
        label_defs_to_column[key] = i
    # sheet.cell(row=1, column=3).value = 'Center of mass'
    k = 2
    for (pid, _id) in zip(pids, image_ids):

        _filepath = conn.execute("SELECT filepath_reg from Labels where image_id = ?",
                                 (_id, )).fetchone()[0]
        if _filepath is None:
            print("No filepath for ", pid)
            continue

        com, com_idx = util.get_center_of_mass(util.DATA_FOLDER + _filepath)
        surface = util.get_surface(util.DATA_FOLDER + _filepath)

        print(pid, com_idx)

        dist_from_ventricle_to_com = np.linalg.norm(np.array(com)-np.array(com_ventricle))
        dist_from_ventricle_to_border = util.get_min_distance(surface, [com_ventricle])

        lobe = label_defs.get(lobes_brain[com_idx[0], com_idx[1], com_idx[2]], 'other')
        res_lobes_brain[pid] = lobe

        tumor_data = nib.load(util.DATA_FOLDER + _filepath).get_data()
        union_data = lobes_brain * tumor_data
        union_data = union_data.flatten()
        lobe_overlap = ''
        for column in range(1, 1+max(label_defs_to_column.values())):
            sheet.cell(row=k, column=column).value = 0
        for _lobe in np.unique(union_data):
            column = label_defs_to_column.get(_lobe)
            if column is None:
                continue
            sheet.cell(row=k, column=column).value = 1
            lobe_overlap += label_defs.get(_lobe, '') + ', '

        sheet.cell(row=k, column=1).value = pid
        sheet.cell(row=k, column=2).value = lobe
        sheet.cell(row=k, column=3).value = round(dist_from_ventricle_to_com,2)
        sheet.cell(row=k, column=4).value = round(dist_from_ventricle_to_border,2)

        k += 1

    book.save(folder + "brain_lobes_Hammers_mith_n30r95.xlsx")

    print(res_lobes_brain, len(res_lobes_brain))