Commit 6dfbf88c authored by Fernando's avatar Fernando

Add some CLI scripts

parent ae698a38
import sys
from pathlib import Path
import numpy as np
from PIL import Image
from tqdm import tqdm
import nibabel as nib
from utils import sglob, get_stem, nifti
input_dir, output_dir = sys.argv[1:]
input_dir = Path(input_dir).expanduser()
output_dir = Path(output_dir).expanduser()
output_dir.mkdir(exist_ok=True)
fps = sglob(input_dir, '*Parcel*.nii*')
for src in tqdm(fps):
dst = output_dir / f"{get_stem(src)}_gif_brain_seg.nii.gz"
if dst.is_file(): continue
nii = nifti.load(src)
data = nii.get_data()
brain = data >= 4
nifti.save(brain.astype(np.uint8), affine=nii.affine, path=dst )
import sys
from pathlib import Path
import numpy as np
from tqdm import tqdm
from utils import sglob, nifti
input_dir = sys.argv[1]
input_dir = Path(input_dir).expanduser()
fps = [fp for fp in sglob(input_dir, '**/*.nii*') if not fp.is_symlink()]
for src in tqdm(fps):
nii = nifti.load(src)
data = nii.get_data()
nans = np.isnan(data)
has_nans = nans.any()
if not has_nans:
continue
tqdm.write(str(src))
data[nans] = 0
nifti.save(data, affine=nii.affine, path=src)
import sys
from pathlib import Path
import numpy as np
from PIL import Image
from tqdm import tqdm
import nibabel as nib
from utils import sglob, get_stem
input_dir, k, output_dir = sys.argv[1:]
input_dir = Path(input_dir).expanduser()
output_dir = Path(output_dir).expanduser()
output_dir.mkdir(exist_ok=True)
def normalize(array):
nan = np.isnan(array)
array = array.astype(np.float32)
if np.count_nonzero(nan) > 0:
print('NaNs found in array')
array[nan] = 0
array -= array.min()
array /= array.max()
array *= 255
return array.astype(np.uint8)
fps = sglob(input_dir, '*.nii*')
for src in tqdm(fps):
dst = output_dir / f'{get_stem(src)}.jpg'
nii = nib.load(str(src))
array = normalize(nii.dataobj[:, :, k])
image = Image.fromarray(array)
image.save(dst)
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment