def process3(folder): """ Post process data """ print(folder) util.setup(folder) params = ['Mobility', 'Selfcare', 'Activity', 'Pain', 'Anxiety', 'karnofsky', 'Index_value'] image_ids = find_images() result = util.post_calculations(image_ids) print(len(result['all'])) # util.avg_calculation(result['all'], 'all_N=112', None, True, folder, save_sum=True) # util.avg_calculation(result['img'], 'img_N=112', None, True, folder) for qol_param in params: if qol_param == "Delta_qol2": (image_ids_with_qol, qol) = util.get_qol(image_ids, "Delta_qol") qol = [-1 if _temp <= -0.15 else 0 if _temp < 0.15 else 1 for _temp in qol] else: (image_ids_with_qol, qol) = util.get_qol(image_ids, qol_param) if qol_param not in ["karnofsky", "Delta_kps"]: qol = [_temp * 100 for _temp in qol] default_value = -100 print(qol_param) print(len(qol)) result = util.post_calculations(image_ids_with_qol) for label in result: if label == 'img': continue print(label) util.avg_calculation(result[label], label + '_' + qol_param + '_N=112', qol, True, folder, default_value=default_value) util.median_calculation(result[label], label + '_' + qol_param + '_N=112', qol, True, folder, default_value=default_value)
def process2(folder): """ Post process data Delta""" print(folder) util.setup(folder) params = ['Delta_qol', 'Delta_qol2', 'Delta_mobility', 'Delta_selfcare', 'Delta_activity', 'Delta_pain', 'Delta_anixety', 'Delta_kps'] image_ids = find_images() result = util.post_calculations(image_ids) print(len(result['all'])) util.avg_calculation(result['all'], 'all_N=112', None, True, folder, save_sum=True) util.avg_calculation(result['img'], 'img_N=112', None, True, folder) print("\n\n\n\n\n") for qol_param in params: if qol_param == "Delta_qol2": (image_ids_with_qol, qol) = util.get_qol(image_ids, "Delta_qol") qol = [-1 if _temp <= -0.15 else 0 if _temp < 0.15 else 1 for _temp in qol] else: (image_ids_with_qol, qol) = util.get_qol(image_ids, qol_param) qol = [_temp * 100 for _temp in qol] default_value = -300 print(qol_param, len(qol)) result = util.post_calculations(image_ids_with_qol) for label in result: if label == 'img': continue print(label) # util.avg_calculation(result[label], label + '_' + qol_param, qol, True, folder, default_value=default_value) util.median_calculation(result[label], label + '_' + qol_param, qol, True, folder, default_value=default_value)
def process(folder, censor_date, survival_groups, pids_to_exclude=None): """ Post process data """ print(folder) util.setup(folder) image_ids, survival_days = util.get_image_id_and_survival_days(study_id="GBM_survival_time", exclude_pid=pids_to_exclude, censor_date_str=censor_date) result = util.post_calculations(image_ids) print('Total: ' + str(len(result['all'])) + ' patients') util.avg_calculation(result['all'], 'tumor', None, True, folder, save_sum=True) util.mortality_rate_calculation(result['all'], '_all_year', survival_days, True, folder, default_value=-1, max_value=150, per_year=True) util.avg_calculation(result['img'], 'volume', None, True, folder) image_ids, survival_days = util.get_image_id_and_survival_days(study_id="GBM_survival_time", exclude_pid=pids_to_exclude, censor_date_str=censor_date,resection=True) result = util.post_calculations(image_ids) print('Resected: ' + str(len(result['all'])) + ' patients') util.avg_calculation(result['all'], 'tumor_resected', None, True, folder, save_sum=True) util.avg_calculation(result['img'], 'volume_resected', None, True, folder) util.mortality_rate_calculation(result['all'], '_resected_year', survival_days, True, folder, default_value=-1, max_value=150, per_year=True) for group in survival_groups: image_ids, survival_days = util.get_image_id_and_survival_days(study_id="GBM_survival_time", exclude_pid=pids_to_exclude, censor_date_str=censor_date,survival_group=group) result = util.post_calculations(image_ids) print('Group ' + str(group) + ': ' + str(len(result['all'])) + ' patients') label = 'tumor_' + str(group[0]) + '-' + str(group[1]) util.avg_calculation(result['all'], label, None, True, folder, save_sum=True)
def process(folder, censor_date): """ Post process data """ print(folder) util.setup(folder) image_ids, survival_days = util.get_image_id_and_survival_days( study_id="GBM_survival_time", censor_date_str=censor_date) result = util.post_calculations(image_ids) print('Total: ' + str(len(result['all'])) + ' patients') util.avg_calculation(result['all'], 'tumor', None, True, folder, save_sum=True) util.avg_calculation(result['img'], 'volume', None, True, folder) util.mortality_rate_calculation(result['all'], '', survival_days, True, folder, default_value=-1) image_ids, survival_days = util.get_image_id_and_survival_days( study_id="GBM_survival_time", censor_date_str=censor_date, resection=True) result = util.post_calculations(image_ids) print('Resected: ' + str(len(result['all'])) + ' patients') util.avg_calculation(result['all'], 'tumor_resected', None, True, folder, save_sum=True) util.avg_calculation(result['img'], 'volume_resected', None, True, folder) util.mortality_rate_calculation(result['all'], '_resected', survival_days, True, folder, default_value=-1) survival_groups = [[0, 182], [183, 730], [731, float('Inf')]] for group in survival_groups: image_ids, survival_days = util.get_image_id_and_survival_days( study_id="GBM_survival_time", censor_date_str=censor_date, survival_group=group) result = util.post_calculations(image_ids) print('Group ' + str(group) + ': ' + str(len(result['all'])) + ' patients') label = 'tumor_' + str(group[0]) + '-' + str(group[1]) util.avg_calculation(result['all'], label, None, True, folder, save_sum=True)
def process(folder): """ Post process data distribution and baseline""" print(folder) util.setup(folder) params = ['Mobility', 'Selfcare', 'Activity', 'Pain', 'Anxiety', 'karnofsky', 'Index_value'] image_ids = find_images_163() result = util.post_calculations(image_ids) print(len(result['all'])) util.avg_calculation(result['all'], 'all_N=163', None, True, folder, save_sum=True) util.avg_calculation(result['img'], 'img_N=163', None, True, folder) image_ids = do_img_registration_GBM.find_images() result = util.post_calculations(image_ids) print(len(result['all'])) util.avg_calculation(result['all'], 'all_N=170', None, True, folder, save_sum=True) util.avg_calculation(result['img'], 'img_N=170', None, True, folder) for qol_param in params: (image_ids_with_qol, qol) = util.get_qol(image_ids, qol_param) if qol_param not in ["karnofsky", "Delta_kps"]: qol = [_temp * 100 for _temp in qol] default_value = -100 print(qol_param) print(len(qol)) result = util.post_calculations(image_ids_with_qol) for label in result: if label == 'img': continue print(label) # util.avg_calculation(result[label], label + '_' + qol_param, qol, True, folder, default_value=default_value) util.median_calculation(result[label], label + '_' + qol_param, qol, True, folder, default_value=default_value)
def process(folder, exclude): """ Post process data """ print(folder) util.setup(folder, "meningiomer") image_ids = find_images(exclude=exclude) result = util.post_calculations(image_ids) print(len(result['all'])) util.avg_calculation(result['all'], 'all', None, True, folder, save_sum=True, save_pngs=True) util.avg_calculation(result['img'], 'img', None, True, folder)
def process(folder): """ Post process data """ print(folder) util.setup(folder) for grade in [2, 3, 4]: image_ids, survival_days = util.get_image_id_and_survival_days( glioma_grades=[grade]) result = util.post_calculations(image_ids) print(len(result['all'])) util.avg_calculation(result['all'], 'all_', None, True, folder, save_sum=True) util.avg_calculation(result['img'], 'img_', None, True, folder) for label in result: if label == 'img': continue util.avg_calculation(result[label], 'survival_time_grade_' + str(grade), survival_days, True, folder, default_value=-1)
def process(folder, glioma_grades): """ Post process data """ print(folder) util.setup(folder) params = [ 'Index_value', 'Global_index', 'Mobility', 'Selfcare', 'Activity', 'Pain', 'Anxiety', 'karnofsky' ] (image_ids, qol) = util.get_image_id_and_qol(None, exclude_pid, glioma_grades=glioma_grades) print(len(image_ids)) result = util.post_calculations(image_ids) print(len(result['all'])) util.avg_calculation(result['all'], 'all', None, True, folder, save_sum=True) util.avg_calculation(result['img'], 'img', None, True, folder) # (image_ids, qol) = util.get_image_id_and_qol('Index_value', exclude_pid, glioma_grades=glioma_grades) # result = util.post_calculations(image_ids) # util.calculate_t_test(result['all'], 1) for qol_param in params: (image_ids, qol) = util.get_image_id_and_qol(qol_param, exclude_pid) if not qol_param == "karnofsky": qol = [_temp * 100 for _temp in qol] if not qol_param == "Index_value": default_value = -100 else: default_value = 0 print(image_ids) result = util.post_calculations(image_ids) for label in result: print(label) if label == 'img': continue util.avg_calculation(result[label], label + '_' + qol_param, qol, True, folder, default_value=default_value) util.std_calculation(result[label], label + '_' + qol_param, qol, True, folder)
def process(folder, pids_to_exclude=()): """ Post process data """ 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''') image_ids = [] image_ids_1 = [] image_ids_2 = [] image_ids_3 = [] tag_data_1 = [] tag_data_2 = [] tag_data_3 = [] img = nib.load("/media/leb/data/Atlas/lobes_brain.nii") lobes_brain = img.get_data() label_defs = util.get_label_defs() res_right_left_brain = {} res_lobes_brain = {} patients = '\nPID MM\n----------------\n' 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] _mm = conn.execute( "SELECT Subgroup from MolekylareMarkorer where pid = ?", (pid, )).fetchone()[0] if _mm is None: print("No mm data for ", pid) patients += str(pid) + ': ?\n' continue _desc = conn.execute( "SELECT comments from MolekylareMarkorer where pid = ?", (pid, )).fetchone()[0] if _desc is None: _desc = "" _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) val = {} val['Name'] = str(pid) + "_" + str(_mm) val['PositionGlobal'] = str(com[0]) + "," + str(com[1]) + "," + str( com[2]) val['desc'] = str(_desc) lobe = label_defs[lobes_brain[com_idx[0], com_idx[1], com_idx[2]]] right_left = 'left' if com_idx[0] < 99 else 'right' res_lobes_brain[lobe] = res_lobes_brain.get(lobe, [0, 0, 0]) res_right_left_brain[right_left] = res_right_left_brain.get( right_left, [0, 0, 0]) print(right_left, lobe) if _mm == 1: res_lobes_brain[lobe][0] += 1 res_right_left_brain[right_left][0] += 1 image_ids_1.extend([_id]) elif _mm == 2: res_lobes_brain[lobe][1] += 1 res_right_left_brain[right_left][1] += 1 image_ids_2.extend([_id]) elif _mm == 3: res_lobes_brain[lobe][2] += 1 res_right_left_brain[right_left][2] += 1 image_ids_3.extend([_id]) image_ids.extend([_id]) print(pid, _mm) patients += str(pid) + ': ' + str(_mm) + '\n' if _mm == 1: tag_data_1.append(val) elif _mm == 2: tag_data_2.append(val) elif _mm == 3: tag_data_3.append(val) print(format_dict(res_lobes_brain)) lobes_brain_file = open(folder + "lobes_brain.txt", 'w') lobes_brain_file.write(format_dict(res_lobes_brain)) lobes_brain_file.close() lobes_brain_file = open(folder + "lobes_brain.txt", 'a') lobes_brain_file.write(format_dict(res_right_left_brain)) lobes_brain_file.write(patients) lobes_brain_file.close() print(len(image_ids)) result = util.post_calculations(image_ids_1) util.avg_calculation(result['all'], 'mm_1', None, True, folder, save_sum=True) result = util.post_calculations(image_ids_2) util.avg_calculation(result['all'], 'mm_2', None, True, folder, save_sum=True) result = util.post_calculations(image_ids_3) util.avg_calculation(result['all'], 'mm_3', None, True, folder, save_sum=True) result = util.post_calculations(image_ids) util.avg_calculation(result['all'], 'mm_1_2_3', None, True, folder, save_sum=True) return tag_data = { "tag_data_1": tag_data_1, "tag_data_2": tag_data_2, "tag_data_3": tag_data_3 } pickle.dump(tag_data, open("tag_data.pickle", "wb")) cursor.close() conn.close() util.write_fcsv("mm_1", folder, tag_data_1, '1 0 0', 13) util.write_fcsv("mm_2", folder, tag_data_2, '0 1 0', 5) util.write_fcsv("mm_3", folder, tag_data_3, '0 0 1', 6) result = util.post_calculations(image_ids) util.avg_calculation(result['all'], 'all', None, True, folder, save_sum=True) util.avg_calculation(result['img'], 'img', None, True, folder)
def process(folder): """ Post process data """ print(folder) util.setup(folder) image_ids = find_images("post") if True: import nibabel as nib conn = sqlite3.connect(util.DB_PATH) conn.text_factory = str cursor = conn.execute( '''SELECT pid from Patient where study_id = ? ''', ("LGG_reseksjonsgrad", )) k = 0 for row in cursor: k += 1 cursor2 = conn.execute( '''SELECT id from Images where pid = ? and diag_pre_post = ?''', (row[0], "post")) ids = [] for _id in cursor2: ids.append(_id[0]) cursor2.close() if not ids: continue images_post = post_calculations(ids) file_name_post = images_post['all'][0] cursor2 = conn.execute( '''SELECT id from Images where pid = ? and diag_pre_post = ?''', (row[0], "pre")) ids = [] for _id in cursor2: ids.append(_id[0]) images_pre = util.post_calculations(ids) file_name_pre = images_pre['all'][0] img_pre = nib.load(file_name_pre) img_post = nib.load(file_name_post) temp = img_pre.get_data() - img_post.get_data() temp = temp.flatten() print(sum(temp < 0), file_name_pre, file_name_post) cursor.close() conn.close() # return result_post = post_calculations(image_ids) print(len(result_post['all'])) util.avg_calculation(result_post['all'], 'all_post', None, True, folder, save_sum=True) util.avg_calculation(result_post['img'], 'img_post', None, True, folder) image_ids = find_images("pre") result_pre = util.post_calculations(image_ids) print(len(result_pre['all'])) util.avg_calculation(result_pre['all'], 'all_pre', None, True, folder, save_sum=True) util.avg_calculation(result_pre['img'], 'img_pre', None, True, folder) util.calc_resection_prob(result_pre['all'], result_post['all'], 'resection_prob', True, folder, -1)
# util.setup("LGG_POST_RES/", "LGG") # elif False: # import do_img_registration_LGG_PRE as do_img_registration # util.setup("LGG_PRE_RES/", "LGG") # elif False: # import do_img_registration_GBM as do_img_registration # util.setup("GBM_RES2/", "GBM") params = ['Index_value', 'Global_index', 'Mobility', 'Selfcare', 'Activity', 'Pain', 'Anxiety'] util.mkdir_p("LGG_GBM_RES") FOLDER = "LGG_GBM_RES/" # "LGG_GBM_RES/GBM" util.setup(FOLDER) (image_ids, qol) = util.get_image_id_and_qol('Index_value') print(image_ids, len(image_ids)) result = util.post_calculations(image_ids) util.calculate_t_test(result['all'], 0.85) for qol_param in params: (image_ids, qol) = util.get_image_id_and_qol(qol_param) print(image_ids) result = util.post_calculations(image_ids) for label in result: print(label) if label == 'img': continue util.avg_calculation(result[label], label + '_' + qol_param, qol, True, FOLDER) util.avg_calculation(result['img'], label, None, True, FOLDER) util.sum_calculation(result['img'], label, None, True, FOLDER)