|
| 1 | +import os |
| 2 | +from pathlib import Path |
| 3 | + |
| 4 | +import pandas as pd |
| 5 | +from tqdm import tqdm |
| 6 | + |
| 7 | +from examples.dicom_select.a03_totalspineseg import black_list |
| 8 | +from TPTBox import BIDS_Family, BIDS_Global_info, Print_Logger, calc_poi_from_subreg_vert, to_nii |
| 9 | +from TPTBox.core.bids_constants import formats |
| 10 | +from TPTBox.spine.snapshot2D import snapshot |
| 11 | + |
| 12 | + |
| 13 | +def update_poi_and_snp(fam: BIDS_Family, f, new_vert=None, override=False, poi_not_younger=True): |
| 14 | + try: |
| 15 | + new_vert = new_vert or fam["msk_seg-vert"][0] |
| 16 | + except KeyError: |
| 17 | + return |
| 18 | + |
| 19 | + subreg = fam.get("msk_seg-subreg", fam.get("msk_seg-spine"))[0] # type: ignore |
| 20 | + p = fam[f][0] |
| 21 | + path = p.get_nii_file() |
| 22 | + if path is None: |
| 23 | + return |
| 24 | + snp_paths = p.get_changed_path( |
| 25 | + "png", |
| 26 | + bids_format="snp", |
| 27 | + parent="derivative-spineps", |
| 28 | + info={"seg": "spine", "mod": p.format, "ovl": None}, |
| 29 | + non_strict_mode=False, |
| 30 | + make_parent=False, |
| 31 | + ) |
| 32 | + poi_path = p.get_changed_path( |
| 33 | + "json", |
| 34 | + bids_format="ctd", |
| 35 | + parent="derivative-spineps", |
| 36 | + info={"seg": "spine", "mod": p.format, "ovl": None}, |
| 37 | + non_strict_mode=False, |
| 38 | + make_parent=False, |
| 39 | + ) |
| 40 | + |
| 41 | + if not override and poi_path.exists() and snp_paths.exists() and poi_not_younger: |
| 42 | + ref_mtime = os.path.getmtime(path) |
| 43 | + if os.path.getmtime(poi_path) >= ref_mtime and os.path.getmtime(snp_paths) >= ref_mtime: |
| 44 | + return |
| 45 | + poi = calc_poi_from_subreg_vert(new_vert, subreg, subreg_id=[40, 50]) |
| 46 | + poi.save(poi_path) # type: ignore |
| 47 | + snapshot(p, new_vert, poi_path, subreg, out_path=snp_paths, mode="CT" if f == "ct" else "MRI") |
| 48 | + |
| 49 | + |
| 50 | +# totalspineseg INPUT_FOLDER/ OUTPUT_FOLDER/ --step1 -k step1_levels step1_vert |
| 51 | +def do_update_cdt_via_canada(bgi: BIDS_Global_info, select_xlsx_path: Path): |
| 52 | + if not select_xlsx_path.exists(): |
| 53 | + return |
| 54 | + df_select = pd.read_excel(select_xlsx_path, dtype={"FileID": str}) |
| 55 | + # exit() |
| 56 | + t = tqdm(bgi.iter_subjects()) |
| 57 | + for sub, subj in t: |
| 58 | + t.desc = f"update cdt by {select_xlsx_path.name}; Subject {sub}" |
| 59 | + q = subj.new_query(flatten=True) |
| 60 | + q.filter_dixon_only_inphase() |
| 61 | + q.unflatten() |
| 62 | + for fam in q.loop_dict(): |
| 63 | + for f in formats: |
| 64 | + if f in fam: |
| 65 | + if f in black_list: |
| 66 | + continue |
| 67 | + update_poi_and_snp(fam, f, override=False) |
| 68 | + for fam in q.loop_dict(): |
| 69 | + try: |
| 70 | + poi_path = fam["ctd_seg-subreg"][0] if "ctd_seg-subreg" in fam else fam["ctd_seg-spine"][0] |
| 71 | + except KeyError: |
| 72 | + poi_path = None |
| 73 | + if poi_path is None: |
| 74 | + continue |
| 75 | + for f in formats: |
| 76 | + if f in fam: |
| 77 | + p = fam[f][0] |
| 78 | + selected_row = df_select[df_select["FileID"] == p.BIDS_key] |
| 79 | + if selected_row.empty: |
| 80 | + continue |
| 81 | + # selected_row_dict = selected_row.to_dict(orient="records")[0] |
| 82 | + |
| 83 | + update_canada = selected_row["vert_brocken_try_canada"].to_numpy()[0] |
| 84 | + update_canada = None if pd.isna(update_canada) else (update_canada) # type: ignore |
| 85 | + if update_canada is not None: |
| 86 | + subreg = fam["msk_seg-subreg"][0] if "msk_seg-subreg" in fam else fam["msk_seg-spine"][0] |
| 87 | + |
| 88 | + out = subreg.get_changed_path(parent=subreg.parent, info={"seg": "vert", "desc": "canada"}) |
| 89 | + if out.exists(): |
| 90 | + # if "msk_seg-vert" in fam and len(fam["msk_seg-vert"]) != 1: |
| 91 | + # print("remap", fam) |
| 92 | + # file = fam["msk_seg-vert"][0] |
| 93 | + # if file.get("desc") == "canada": |
| 94 | + # file = fam["msk_seg-vert"][1] |
| 95 | + # file = file.get_nii_file() |
| 96 | + # assert file is not None |
| 97 | + # file.rename(str(file).replace("vert_msk.", "vert_backup.")) |
| 98 | + # print("rename", str(file), "to", str(file).replace("vert_msk.", "vert_backup.")) |
| 99 | + # # exit() |
| 100 | + |
| 101 | + continue |
| 102 | + # vert = |
| 103 | + poi_path = fam["ctd_seg-subreg"][0] if "ctd_seg-subreg" in fam else fam["ctd_seg-spine"][0] |
| 104 | + |
| 105 | + vert_canada = subreg.get_changed_path( |
| 106 | + bids_format="msk", |
| 107 | + info={"seg": "vert", "desc": "totalspineseg", "mod": p.bids_format}, |
| 108 | + parent="derivative-canada", |
| 109 | + ) |
| 110 | + vert_c = to_nii(vert_canada, True) |
| 111 | + if subreg.get("mod") == "ct": |
| 112 | + vert_c[vert_c >= 60] = 0 |
| 113 | + vert_c[vert_c >= 61] = 0 |
| 114 | + vert_c[vert_c >= 100] = 0 |
| 115 | + new_vert = subreg.open_nii().clamp(0, 1) * vert_c |
| 116 | + new_vert.save(subreg.get_changed_path(parent=subreg.parent, info={"seg": "vert", "desc": "canada"})) |
| 117 | + if "msk_seg-vert" in fam: |
| 118 | + file = fam["msk_seg-vert"][0].get_nii_file() |
| 119 | + assert file is not None |
| 120 | + file.rename(str(file).replace("vert_msk.", "vert_backup.")) |
| 121 | + update_poi_and_snp(fam, f, new_vert, override=True) |
| 122 | + |
| 123 | + |
| 124 | +if __name__ == "__main__": |
| 125 | + dataset_path = Path("/DATA/NAS/datasets_source/dataset-MRCT-Spine/MR-CT-Spine/dataset-MRCT-Spine") |
| 126 | + parent_in = ["sourcedata2", "derivative-spineps"] |
| 127 | + parent_out = "sourcedata3" |
| 128 | + info_folder = "info" |
| 129 | + |
| 130 | + dataset_path = Path("/DATA/NAS/datasets_processed/MRI_spine/dataset-SpineGAN") |
| 131 | + dataset_name = Path(dataset_path).name |
| 132 | + parent_in = ["rawdata", "derivative-spineps"] |
| 133 | + t13_vertebra_are_a_lie = True |
| 134 | + # parent_out = "sourcedata3" |
| 135 | + update_cdt_via_reference = False |
| 136 | + |
| 137 | + select_xlsx = dataset_name + "_select.xlsx" |
| 138 | + |
| 139 | + select_xlsx_path = (dataset_path) / info_folder / select_xlsx |
| 140 | + # random.seed(42) |
| 141 | + log = Print_Logger() |
| 142 | + bgi = BIDS_Global_info([dataset_path], parents=parent_in) |
| 143 | + # filter_dataset(bgi) |
| 144 | + do_update_cdt_via_canada(bgi, select_xlsx_path) |
| 145 | + ################ |
0 commit comments