<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="./sabs_CT_normalized/image_*.nii.gz"
SEG_BNAME="./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: int(x.split("_")[-1].split(".nii.gz")[0])  ) ]
segs = [ fid for fid in sorted(segs, key = lambda x: int(x.split("_")[-1].split(".nii.gz")[0])  ) ]

imgs
# lb = nio.read_nii_image(segs[0])
import nibabel as nib
lb = nib.load(segs[0]).get_fdata()

lb.shape
1 in lb[..., 40]
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'./sabs_CT_normalized/classmap_{MIN_TP}.json'
for _lb in LABEL_NAME:
    classmap[_lb] = {}
    for pid in range(len(segs)):
        classmap[_lb][str(pid)] = []

for pid, seg in enumerate(segs):
    # lb_vol = nio.read_nii_image(seg)
    lb_vol = nib.load(seg).get_fdata()
    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!')
    
with open(fid, 'w') as fopen:
    json.dump(classmap, fopen)
    fopen.close()<jupyter_output>pid 0 finished!
pid 1 finished!
pid 2 finished!
pid 3 finished!
pid 4 finished!
pid 5 finished!
pid 6 finished!
pid 7 finished!
pid 8 finished!
pid 9 finished!
pid 10 finished!
pid 11 finished!
pid 12 finished!
pid 13 finished!
pid 14 finished!
pid 15 finished!
pid 16 finished!
pid 17 finished!
pid 18 finished!
pid 19 finished!
pid 20 finished!
pid 21 finished!
pid 22 finished!
pid 23 finished!
pid 24 finished!
pid 25 finished!
pid 26 finished!
pid 27 finished!
pid 28 finished!
pid 29 finished!
{'fraction_non_alphanumeric': 0.13706644379440033, 'fraction_numerical': 0.026744671959882994, 'mean_word_length': 3.895705521472393, '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': '74083', 'license': 'MIT', 'n_tokens_mistral': 971, 'n_tokens_neox': 909, 'n_words': 272, 'path': 'data/SABS/Synapse_abdominal_classmap.ipynb', 'repo_name': 'chence17/Self-supervised-Fewshot-Medical-Image-Segmentation', 'size': '2393'}