<jupyter_start><jupyter_text>Get class-pid-index map for synapse abdominal CT dataset<jupyter_code>%reset
%load_ext autoreload
%autoreload 2
import numpy as np
import os
import glob
import SimpleITK as sitk
import matplotlib.pyplot as plt
import sys
sys.path.insert(0, '../../dataloaders/')
import niftiio as nio
# lets save them in a same way as mmwhs for the ease of modifying dataloader

# normalization: cut top 2% of histogram, then doing volume-wise normalization

IMG_BNAME="/home/htang6/workspace/data/abdomen/superpixel/sabs_CT_normalized/image_*.nii.gz"
SEG_BNAME="/home/htang6/workspace/data/abdomen/superpixel/sabs_CT_normalized/label_*.nii.gz"





imgs = glob.glob(IMG_BNAME)
segs = glob.glob(SEG_BNAME)
imgs = [ fid for fid in sorted(imgs, key = lambda x: x.split("_")[-1].split(".nii.gz")[0]) ]
segs = [ fid for fid in sorted(segs, key = lambda x: x.split("_")[-1].split(".nii.gz")[0]) ]
pids = [pid.split("_")[-1].split(".")[0] for pid in imgs]
pids
imgs
lb = nio.read_nii_bysitk(segs[0])
lb.shape
import json
classmap = {}
LABEL_NAME = ["BGD", "SPLEEN", "KID_R", "KID_l", "GALLBLADDER", "ESOPHAGUS", "LIVER", "STOMACH", "AORTA", "IVC", "PS_VEIN", "PANCREAS", "AG_R", "AG_L"]     

MIN_TP=1 # minimum number of true positive pixels in a slice

fid = f'/home/htang6/workspace/data/abdomen/superpixel/sabs_CT_normalized/classmap_{MIN_TP}.json'
for _lb in LABEL_NAME:
    classmap[_lb] = {}
    for pid in pids:
        classmap[_lb][str(pid)] = []

for pid, seg in zip(pids, segs):
    lb_vol = nio.read_nii_bysitk(seg)
    lb_vol = np.swapaxes(lb_vol, 0, -1)
    n_slice = lb_vol.shape[-1]
    for slc in range(n_slice):
        for cls in range(len(LABEL_NAME)):
            if cls in lb_vol[..., slc]:
                if np.sum( lb_vol[..., slc] == cls) >= MIN_TP:
                    classmap[LABEL_NAME[cls]][str(pid)].append(slc)
    print(f'pid {str(pid)} finished!', lb_vol.shape)
    
with open(fid, 'w') as fopen:
    json.dump(classmap, fopen)
    fopen.close()<jupyter_output>pid 084 finished! (257, 257, 209)
pid 085 finished! (257, 257, 214)
pid 086 finished! (257, 257, 268)
pid 087 finished! (257, 257, 220)
pid 088 finished! (257, 257, 204)
pid 089 finished! (257, 257, 196)
pid 090 finished! (257, 257, 281)
pid 091 finished! (257, 257, 274)
pid 092 finished! (257, 257, 218)
pid 093 finished! (257, 257, 268)
pid 094 finished! (257, 257, 261)
pid 095 finished! (257, 257, 212)
pid 096 finished! (257, 257, 261)
pid 097 finished! (257, 257, 196)
pid 098 finished! (257, 257, 268)
pid 099 finished! (257, 257, 264)
pid 100 finished! (257, 257, 205)
pid 101 finished! (257, 257, 224)
pid 102 finished! (257, 257, 243)
pid 103 finished! (257, 257, 259)
pid 104 finished! (257, 257, 222)
pid 105 finished! (257, 257, 193)
pid 106 finished! (257, 257, 195)
pid 107 finished! (257, 257, 201)
pid 108 finished! (257, 257, 192)
pid 109 finished! (257, 257, 186)
pid 110 finished! (257, 257, 191)
pid 111 finished! (257, 257, 186)
pid 112 finished! (257, 257, 177)
pid 113 finish[...]
{'fraction_non_alphanumeric': 0.1529255319148936, 'fraction_numerical': 0.12234042553191489, 'mean_word_length': 4.126064735945485, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '560660', 'license': 'MIT', 'n_tokens_mistral': 1495, 'n_tokens_neox': 1178, 'n_words': 360, 'path': 'data/SABS/Synapse_abdominal_classmap.ipynb', 'repo_name': 'tanghaotommy/Self-supervised-Fewshot-Medical-Image-Segmentation', 'size': '3008'}