| import os, json, random, glob |
| import numpy as np |
| import yaml |
|
|
|
|
| def load_config(path="configs/default.yaml"): |
| with open(path, "r") as f: |
| cfg = yaml.safe_load(f) |
| return cfg |
|
|
|
|
| def set_seed(seed=42): |
| random.seed(seed) |
| np.random.seed(seed) |
| try: |
| import torch |
| torch.manual_seed(seed) |
| torch.cuda.manual_seed_all(seed) |
| except Exception: |
| pass |
|
|
|
|
| def ensure_dir(p): |
| os.makedirs(p, exist_ok=True) |
| return p |
|
|
|
|
| |
| def load_nii(path): |
| """Return (array[z,y,x] -> we keep numpy in (x,y,z)? ) We use SimpleITK and keep |
| arrays in numpy index order [x, y, z] together with spacing/origin/direction.""" |
| import SimpleITK as sitk |
| img = sitk.ReadImage(path) |
| arr = sitk.GetArrayFromImage(img) |
| arr = np.transpose(arr, (2, 1, 0)) |
| meta = dict( |
| spacing=np.array(img.GetSpacing(), dtype=np.float64), |
| origin=np.array(img.GetOrigin(), dtype=np.float64), |
| direction=np.array(img.GetDirection(), dtype=np.float64), |
| ) |
| return arr, meta |
|
|
|
|
| def save_nii(arr_xyz, meta, path): |
| import SimpleITK as sitk |
| arr = np.transpose(arr_xyz, (2, 1, 0)) |
| img = sitk.GetImageFromArray(arr) |
| img.SetSpacing(tuple(float(s) for s in meta["spacing"])) |
| img.SetOrigin(tuple(float(o) for o in meta["origin"])) |
| img.SetDirection(tuple(float(d) for d in meta["direction"])) |
| sitk.WriteImage(img, path) |
|
|
|
|
| def resample_to_spacing(arr_xyz, meta, new_spacing, is_label=False): |
| """Resample a volume (numpy [x,y,z]) to an isotropic-ish new_spacing (list of 3).""" |
| import SimpleITK as sitk |
| arr = np.transpose(arr_xyz, (2, 1, 0)) |
| img = sitk.GetImageFromArray(arr) |
| img.SetSpacing(tuple(float(s) for s in meta["spacing"])) |
| img.SetOrigin(tuple(float(o) for o in meta["origin"])) |
| img.SetDirection(tuple(float(d) for d in meta["direction"])) |
|
|
| old_spacing = np.array(img.GetSpacing()) |
| old_size = np.array(img.GetSize()) |
| new_spacing = np.array(new_spacing, dtype=np.float64) |
| new_size = np.round(old_size * (old_spacing / new_spacing)).astype(int).tolist() |
|
|
| rs = sitk.ResampleImageFilter() |
| rs.SetOutputSpacing(tuple(float(s) for s in new_spacing)) |
| rs.SetSize([int(s) for s in new_size]) |
| rs.SetOutputOrigin(img.GetOrigin()) |
| rs.SetOutputDirection(img.GetDirection()) |
| rs.SetInterpolator(sitk.sitkNearestNeighbor if is_label else sitk.sitkLinear) |
| out = rs.Execute(img) |
|
|
| o_arr = sitk.GetArrayFromImage(out) |
| o_arr = np.transpose(o_arr, (2, 1, 0)) |
| o_meta = dict(spacing=new_spacing, |
| origin=np.array(out.GetOrigin()), |
| direction=np.array(out.GetDirection())) |
| return o_arr, o_meta |
|
|
|
|
| def numeric_id(case_id): |
| """Extract leading number from a case folder name like '031-修牙髓...' -> 31.""" |
| import re |
| m = re.match(r"\s*0*(\d+)", str(case_id)) |
| return int(m.group(1)) if m else -1 |
|
|
|
|
| def write_json(obj, path): |
| with open(path, "w") as f: |
| json.dump(obj, f, ensure_ascii=False, indent=2) |
|
|
|
|
| def read_json(path): |
| with open(path, "r") as f: |
| return json.load(f) |
|
|