import SimpleITK as sitk
from shubow_tools import imreadseq_multithread, imsaveseq
import os
import re
import shutil

wkdir = r"/home/blue/SITK_registered_image_14um/3rd batch tibia"
dst = r"/home/blue/SITK_registered_image_14um/Flipped"
os.chdir(wkdir)

with open("failed.txt", "r") as f:
    retry_file = f.readlines()

retry_list = [i[:-1] for i in retry_file]

for fd in retry_list:

    if not 'right' in fd:
        continue
    print(fd)
    output = os.path.join(dst, fd)
    if os.path.exists(output):
        shutil.rmtree(output)

    os.mkdir(output)

    image = imreadseq_multithread(os.path.join(wkdir, fd))
    image = image[::-1, :, :]
    imsaveseq(image, os.path.join(dst, fd), fd)
    print('Next!')
Exemple #2
0
wkdir = r'D:/MicroCT data/Yoda1 11.13.2019/Tibia Femur fully seg'
masterdir = os.path.join(wkdir, 'week 3 tibia')
masteroutput = os.path.join(wkdir, 'Registered tibia week 3')
refdir = os.path.join(wkdir, 'Registered tibia week 0')

format = "%(asctime)s: %(message)s"
logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S")

for file in sorted(os.listdir(masterdir)):
    if re.search(r'(410.*left)', file):
        #if re.search(r'\d{3} week \d (left|right) tibia',file):
        imgtitle = file

        logging.info('Loading image of {} ...'.format(imgtitle))
        reftitle = file.replace('week 3', 'week 0') + ' registered'
        ref_img = imreadseq_multithread(os.path.join(refdir, reftitle))
        tar_img = imreadseq_multithread(os.path.join(masterdir, file),
                                        sitkimg=False,
                                        rmbckgrd=60,
                                        z_range=[0, 840])
        tar_img = sitk.GetImageFromArray(
            auto_crop(Rotate_by_Euler_angles(tar_img)))

        ini_transform = cent_transform(ref_img, tar_img)
        metric_values = []
        multires_iterations = []

        suboutput = os.path.join(masteroutput, imgtitle + " registered")
        if not os.path.exists(suboutput):
            os.mkdir(suboutput)
        logging.info('Registration of {} is in process...'.format(imgtitle))
masterdir = r"F:\Shubo MicroCT data\CTimages for plotting\3rd batch loading"
pattern = re.compile(
    r"(\d{3}.week.\d).(left|right|runner|nonrunner).*composite")
#pattern = re.compile(r"(320 week 4) (right) w0w4composite")
format = "%(asctime)s: %(message)s"
logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S")

for folder in sorted(os.listdir(masterdir)):
    pattern.search(folder)
    sampleID = pattern.search(folder)
    if sampleID:
        separator = "_"
        logging.info("Processing {}".format(sampleID.group(0)))
        try:
            trab_imseq = os.path.join(masterdir, folder, "Trab-ROI-wx")
            trab = imreadseq_multithread(trab_imseq)
            trab_nii = os.path.join(
                masterdir,
                separator.join(
                    [sampleID.group(1),
                     sampleID.group(2), "Trab.nii"]))
            sitk.WriteImage(trab, trab_nii)
            logging.info("Trabecular bone saved!")
        except Exception as ex:
            logging.info("Trabecular bone failed! {}".format(ex))
            pass

        try:
            cort_imseq = os.path.join(masterdir, folder, "Cort-ROI-wx")
            cort = imreadseq_multithread(cort_imseq)
            cort_nii = os.path.join(
Exemple #4
0
masterdir = os.path.join(wkdir, 'Registered femur week 3')
masteroutput = os.path.join(wkdir,
                            'VOI450-590_Registered femur week 3_thred75')
refdir = os.path.join(wkdir, 'Registered femur week 0')

format = "%(asctime)s: %(message)s"
logging.basicConfig(format=format, level=logging.INFO, datefmt="%H:%M:%S")

for file in sorted(os.listdir(masterdir)):
    #if re.search(r'416.*left femur',file):
    if re.search(r'\d{3} week \d (left|right) femur', file):
        imgtitle = file[0:-11]
        logging.info('Loading image of {} ...'.format(imgtitle))
        reftitle = imgtitle.replace('week 3', 'week 0') + ' registered'
        ref_img = imreadseq_multithread(os.path.join(refdir, reftitle),
                                        z_range=[450, 590],
                                        rmbckgrd=80)
        tar_img = imreadseq_multithread(os.path.join(masterdir, file),
                                        sitkimg=False,
                                        z_range=[410, 630],
                                        rmbckgrd=75)
        tar_img = sitk.GetImageFromArray(
            auto_crop(Rotate_by_Euler_angles(tar_img)))  # femur

        ini_transform = cent_transform(ref_img, tar_img)
        metric_values = []
        multires_iterations = []

        suboutput = os.path.join(masteroutput, imgtitle + " registered")

        logging.info('Registration of {} is in process...'.format(imgtitle))
Exemple #5
0
#! /home/spl/ml/sitk/bin/python

# -*- coding: utf-8 -*-

import SimpleITK as sitk
import os
import sys
from pathlib import Path
import re
from shubow_tools import imreadseq_multithread
import glob

src = r'/media/spl/D/MicroCT_data/Machine learning/SITK_reg_7um'
dst = r'/run/user/1000/gvfs/smb-share:server=lywanglab.local,share=micro_ct_data/Deep learning project/7_um_data'
os.chdir(src)

for fd in glob.glob('*registered'):
    print('reading: ' + fd)
    s = re.search(r'(\d{3}) week (\d) (left|right)', fd)
    name = '{}{}T_w{}'.format(s.group(1),s.group(3)[0].upper(), s.group(2))
    img = imreadseq_multithread(fd, z_range=[-300,None])
    sitk.WriteImage(img, os.path.join(dst,name+'.nii.gz'))
    print(name + '.nii.gz saved')
Exemple #6
0
masterdir = r"E:\Yoda1-tumor-loading 2.26.2021\Tibia & femur week 3"
masteroutput = r"E:\Yoda1-tumor-loading 2.26.2021\Registration week 3" 
refdir = r"E:\Yoda1-tumor-loading 2.26.2021\Registration week 0"

BASELINE_REGISTRATION = True

if not os.path.exists(masteroutput):
    os.mkdir(masteroutput)

format = "%(asctime)s: %(message)s"
logging.basicConfig(format = format, level = logging.INFO,
                    datefmt="%H:%M:%S")

if not BASELINE_REGISTRATION:
    logging.info('Loading reference image...')
    ref_img = imreadseq_multithread(refdir,thread = 2, sitkimg=True, z_range=[-355,-5])
#ref_img = down_scale(ref_img, down_scale_factor=1.0)

failed_list = []

'''with open("failed.txt", "r") as f :
    retry_file = f.readlines()

retry_list = [i[:-3] for i in retry_file]
read_range_list = [i[-2] for i in retry_file]'''

for file in sorted(os.listdir(masterdir)):
    if re.search(r"\d{3}.(week.\d) (left|right) tibia", file):
        imgtitle = file
        
        if BASELINE_REGISTRATION:
Exemple #7
0
import shutil
import numpy as np

wkdir = r"/media/spl/D/MicroCT_data/Machine learning/1st batch tibia registration"
os.chdir(wkdir)
masterdir = r"/media/spl/D/MicroCT_data/Machine learning/Jul 2018 1st batch L & R tibia/L & R tibia 7.9"
masteroutput = r"/media/spl/D/MicroCT_data/Machine learning/1st batch tibia registration" 

refdir = r"/media/spl/D/MicroCT_data/MicroCT registration ref/6w F WT tibia"

format = "%(asctime)s: %(message)s"
logging.basicConfig(format = format, level = logging.INFO, 
                    datefmt="%H:%M:%S")
logging.info('Loading reference image...')

ref_img = imreadseq_multithread(refdir,thread = 2, sitkimg=True, rmbckgrd=75, z_range=[-756,None])
ref_img = down_scale(ref_img, down_scale_factor=2.0)

failed_list = []
'''
with open("failed2.txt", "r") as f :
    retry = f.readlines()

retry = [i[:-1] for i in retry]
'''
for file in sorted(os.listdir(masterdir))[:]:
    if re.search(r"\d{3} (week \d) (left|right) tibia", file): #and file in retry:
        imgtitle = file
        logging.info('Loading image {} ...'.format(imgtitle))
        
        if 'right' in file: