Commit 982f0a52 authored by Andrea Bizzego's avatar Andrea Bizzego
Browse files

before removal

parent af40f558
import pandas as pd
import numpy as np
import os
#%%
sequence_code_files = '/home/andrea/Trento/DATI/OPBG/sequence_codes_MB.txt'
sequence_codes = pd.read_table(sequence_code_files, sep='\t', header=None)
#%%
codes_dict = []
series_types = []
for i in range(len(sequence_codes)):
subject = sequence_codes.iloc[i, 0]
series_numbers = sequence_codes.iloc[i, 1].split(',')
series_names = sequence_codes.iloc[i, 2]
series_names = series_names.replace(' ', '')
series_names = series_names.split(',')
series_types.append(np.array(series_names))
series_dict = dict([(x,y) for x,y in zip(series_numbers, series_names)])
codes_dict.append((subject, series_dict))
codes_dict = dict(codes_dict)
series_types = np.unique(np.concatenate(series_types))
#%% check available numbers
DATADIR = '/home/andrea/Trento/DATI/OPBG/MB_COREG/'
subs = os.listdir(DATADIR)
available_numbers_dict = []
for SUB in subs:
series_dict = codes_dict[SUB]
SUBDIR = os.path.join(DATADIR, SUB)
files = os.listdir(SUBDIR)
available_series = []
for F in files:
if F.startswith(f'{SUB}_coreg'):
n = F.split('_')[2].split('.')[0]
try:
available_series.append((n, series_dict[n]))
except:
pass
available_numbers_dict.append((SUB, dict(available_series)))
available_numbers_dict = dict(available_numbers_dict)
#%%
numbers = []
for TYPE in series_types:
counter = 0
for SUB in available_numbers_dict.keys():
sub_dict = available_numbers_dict[SUB]
S = list(sub_dict.values())
if TYPE in S:
counter+=1
numbers.append(counter)
#%%
numbers = np.array(numbers)
np.c_[numbers[np.argsort(numbers)], series_types[np.argsort(numbers)]]
#%%
selected_series = []
for SUB in available_numbers_dict.keys():
sub_dict = available_numbers_dict[SUB]
if 'T2' in list(sub_dict.values()):
series_number = np.array(list(sub_dict.keys()))[np.where(np.array(list(sub_dict.values())) == 'T2')[0]][0]
selected_series.append((SUB, series_number))
selected_series = dict(selected_series)
#%%
selected = pd.DataFrame({'idpaz':list(selected_series.keys()), 'number':list(selected_series.values())})
selected.to_csv('selected_FLAIR.txt', sep='\t')
import numpy as np
import nibabel as nb
import os
import matplotlib.pyplot as plt
from nibabel.orientations import *
from dicom_utils import visualize as viz
from nibabel.processing import resample_from_to
#%%
class IndexTracker(object):
def __init__(self, X, ax):
self.X = X
self.ax = ax
n_rows = int(np.ceil((len(X)+1) / 3))
_, _, slices = X[0].shape
self.slices = slices
self.ind = self.slices//2
self.axs = []
self.ims = []
mode_iter = 0
for i in range(n_rows):
for j in range(3):
if mode_iter < len(X):
self.axs.append(self.ax[i][j])
image = self.X[mode_iter][:, :, self.ind] #+ self.X[-1][:, :, self.ind]*100000
self.ims.append(self.ax[i][j].imshow(image, 'gray'))
mode_iter +=1
self.update()
def onscroll(self, event):
if event.button == 'up':
self.ind = (self.ind + 5)
else:
self.ind = (self.ind - 5)
self.update()
def update(self):
for i, im in enumerate(self.ims):
im.set_data(self.X[i][:, :, self.ind])
im.axes.figure.canvas.draw()
#%%
DATADIR = '/home/andrea/Trento/DATI/OPBG/MB_COREG/'
subjects = os.listdir(DATADIR)
#%%
#for SUB in subjects:
SUB = 'OPBG0011'
#%%
SUBDIR = os.path.join(DATADIR, SUB)
files = os.listdir(SUBDIR)
#%%
ref_img = None
ref_seg = None
#%%
modalities = []
for FILE in files:
image = nb.load(os.path.join(SUBDIR, FILE))
if ref_img is None and not FILE.startswith('OPBG'):
ref_img = image
codes = axcodes2ornt(aff2axcodes(image.affine))
image_oriented = nb.orientations.apply_orientation(image.get_fdata(), codes)
print(image_oriented.shape)
if FILE.startswith('OPBG'):
ref_seg = image
else:
modalities.append(image_oriented)
seg_resampled = resample_from_to(ref_seg, ref_img)
codes = axcodes2ornt(aff2axcodes(ref_img.affine))
seg_oriented = nb.orientations.apply_orientation(seg_resampled.get_fdata(), codes)
modalities.append(seg_oriented)
#%%
n_rows = int(np.ceil(len(modalities) / 3))
fig, ax = plt.subplots(n_rows, 3)
tracker = IndexTracker(modalities, ax)
fig.canvas.mpl_connect('scroll_event', tracker.onscroll)
plt.show()
\ 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