def main(model, metrics): if metrics: old_metrics = {} new_metrics = {} files = f'data/predict/{model}/*_0000.nii.gz' files = f'data/predict/{model}/*/*.nii.gz' for file in glob.glob(files): sample, i = os.path.basename(file).split('_') i = i[:4] header = util.header(file) volume = util.read_vol(file) if metrics: label = util.read_vol( glob.glob(f'data/labels/{sample}/{sample}_0_all_brains.nii.gz') [0]) old_metrics[sample] = util.dice_coef(label, volume) n = 2 if sample in constants.TWINS else 1 volume = remove_artifacts(volume, n) if metrics: new_metrics[sample] = util.dice_coef(label, volume) os.makedirs(f'data/predict_cleaned/{model}/{sample}', exist_ok=True) util.save_vol( volume, f'data/predict_cleaned/{model}/{sample}/{sample}_{i}.nii.gz', header) if metrics: print('before: ', old_metrics) print('after: ', new_metrics)
def predict(self, generator, subj_dirs=False): n = len(generator) for i in range(n): #TODO: why is this hard coded here. input_file = generator.input_files[i] subj_dir = input_file.split("/")[-2] path = f'{self.output_location}/{subj_dir}' os.makedirs(path, exist_ok=True) fname = input_file.split('/')[-1] header = util.header(input_file) #vol = nib.load(input_file) vol = generator[i] #vol = vol[0,:,:,:] sub_vols, sub_codes = simple_preproc.get_subvolumes( vol[0], self.input_size) preds = [] for sub_vol in sub_vols: preds.append( self.model.predict(sub_vol[np.newaxis, :, :, :, :])[0]) pred = simple_preproc.sub_vols_to_original_shape( preds, sub_codes, vol.shape[1:]) util.save_vol( pred, os.path.join(path, fname), )
def predict(self, generator, path): preds = self.model.predict_generator(generator, verbose=1) # FIXME for i in range(preds.shape[0]): fname = generator.inputs[i].split('/')[-1] header = util.header(generator.inputs[i]) util.save_vol(uncrop(preds[i], generator.shape), os.path.join(path, fname), header)
def save_prediction(pred, input_file, tile, path, scale=False): fname = input_file.split('/')[-1] sample = fname.split('_')[0] path = os.path.join(path, sample) os.makedirs(path, exist_ok=True) shape = util.shape(input_file) header = util.header(input_file) vol = process.postprocess(pred, shape, resize=True, tile=tile) util.save_vol(vol, os.path.join(path, fname), header, scale) print(fname, flush=True)
def save_predictions(preds, generator, path, scale=False): os.makedirs(path, exist_ok=True) if generator.tile_inputs: preds = np.reshape(preds, (-1, 8) + preds.shape[1:]) for i in range(preds.shape[0]): input_file = generator.input_files[i] fname = input_file.split('/')[-1] header = util.header(input_file) shape = util.shape(input_file) volume = process.postprocess(preds[i], shape, resize=True, tile=generator.tile_inputs) util.save_vol(volume, os.path.join(path, fname), header, scale)
def main(model): folder = options.sample if options.sample else '*' files = f'data/predict/{model}/{folder}/*.nii.gz' for file in glob.glob(files): sample, i = os.path.basename(file).split('_') i = i[:4] header = util.header(file) volume = util.read_vol(file) n = 2 if sample in constants.TWINS else 1 volume = remove_artifacts(volume, n) os.makedirs(f'data/predict_cleaned/{model}/{sample}', exist_ok=True) util.save_vol( volume, f'data/predict_cleaned/{model}/{sample}/{sample}_{i}.nii.gz', header)
def dilate(mask, sample, iterations=4): struct = sp.generate_binary_structure(3, 1) dil = sp.binary_dilation(mask, iterations=iterations) util.save_vol(dil, '{}mask/{}.nii.gz'.format(registration_dir, sample)) return dil
import glob import numpy as np import util for sample in [ '010918L', '010918S', '012115', '013018L', '013018S', '013118L', '013118S', '021015', '021218L', '021218S', '022318L', '022318S', '022415', '022618', '030217', '030315', '031317L', '031317T', '031516', '031615', '031616', '031716', '032217', '032318a', '032318b', '032318c', '032318d', '032818', '040218', '040417' ]: files = glob.glob('data/labels/{}/{}_0_*_brain.nii.gz'.format( sample, sample)) volume = np.zeros( util.shape('data/raw/{}/{}_0.nii.gz'.format(sample, sample))) header = util.header(files[0]) for file in files: volume += util.read_vol(file) util.save_vol( volume, 'data/labels/{}/{}_0_all_brains.nii.gz'.format(sample, sample), header)
def main(sample, order): files = sorted(glob.glob(f'data/nifti/{sample}/*.nii.gz')) vols = np.concatenate([util.read_vol(file) for file in files], axis=-1) if vols.shape[0] == vols.shape[1] == vols.shape[2]: axis = int(input(f'shape: {vols.shape}\n> ')) elif vols.shape[0] == vols.shape[1]: axis = 2 elif vols.shape[0] == vols.shape[2]: axis = 1 elif vols.shape[1] == vols.shape[2]: axis = 0 else: axis = int(input(f'shape: {vols.shape}\n> ')) vols = np.moveaxis(vols, axis, 0) shape = vols.shape even_i = np.arange(0, shape[0], 2) even_slices = vols[::2, ...] even_interpolator = interp1d(even_i, even_slices, kind='linear', axis=0) odd_i = np.arange(1, shape[0], 2) odd_slices = vols[1::2, ...] odd_interpolator = interp1d(odd_i, odd_slices, kind='linear', axis=0) if shape[0] % 2 == 0: evens = even_interpolator(np.arange(0, shape[0] - 1)) odds = odd_interpolator(np.arange(1, shape[0])) evens = np.concatenate((evens, evens[-1:, ...])) else: evens = even_interpolator(np.arange(0, shape[0])) odds = odd_interpolator(np.arange(1, shape[0] - 1)) odds = np.concatenate((odds, odds[-1:, ...])) odds = np.concatenate((odds[:1, ...], odds)) new_shape = list(shape) new_shape[-1] *= 2 series = np.zeros(new_shape) if order is None: import matplotlib.pyplot as plt even_1 = evens[shape[0] // 3, shape[1] // 2, ...] even_2 = evens[shape[0] * 2 // 3, shape[1] // 2, ...] odd_1 = odds[shape[0] // 3, shape[1] // 2, ...] odd_2 = odds[shape[0] * 2 // 3, shape[1] // 2, ...] even_img_1 = np.zeros((shape[2], shape[3] * 2)) even_img_1[:, ::2] = even_1 even_img_1[:, 1::2] = odd_1 even_img_2 = np.zeros((shape[2], shape[3] * 2)) even_img_2[:, ::2] = even_2 even_img_2[:, 1::2] = odd_2 odd_img_1 = np.zeros((shape[2], shape[3] * 2)) odd_img_1[:, ::2] = odd_1 odd_img_1[:, 1::2] = even_1 odd_img_2 = np.zeros((shape[2], shape[3] * 2)) odd_img_2[:, ::2] = odd_2 odd_img_2[:, 1::2] = even_2 img_1 = np.concatenate((odd_img_1, even_img_1), axis=0) img_2 = np.concatenate((odd_img_2, even_img_2), axis=0) fig = plt.figure(figsize=(9, 9)) fig.add_subplot(2, 1, 1) plt.imshow(img_1) plt.axis('off') fig.add_subplot(2, 1, 2) plt.imshow(img_2) plt.axis('off') plt.suptitle(sample) plt.show(block=False) order = input('0. 3D\n1. odd\n2. even\n> ') if order == '0': even = evens[shape[0] // 2, ...] odd = odds[shape[0] // 2, ...] even_img = np.zeros((shape[1], shape[2], shape[3] * 2)) even_img[..., ::2] = even even_img[..., 1::2] = odd odd_img = np.zeros((shape[1], shape[2], shape[3] * 2)) odd_img[..., ::2] = odd odd_img[..., 1::2] = even temp_folder = 'data/temp' os.makedirs(temp_folder, exist_ok=True) util.save_vol(even_img, temp_folder + 'even.nii.gz') util.save_vol(odd_img, temp_folder + 'odd.nii.gz') os.system(f'open {temp_folder}/even.nii.gz') os.system(f'open {temp_folder}/odd.nii.gz') order = input('1. odd\n2. even\n> ') plt.close() if order == '1' or order == 'o': series[..., ::2] = odds series[..., 1::2] = evens elif order == '2' or order == 'e': series[..., ::2] = evens series[..., 1::2] = odds else: raise ValueError('Must be even or odd slice.') series = np.moveaxis(series, 0, axis) new_folder = f'data/raw/{sample}/' os.makedirs(new_folder, exist_ok=True) for i in range(new_shape[-1]): util.save_vol(series[..., i], new_folder + sample + f'_{str(i).zfill(4)}.nii.gz')
def main(folder, volume): files = glob.glob(folder + '*.nii.gz') vols = np.concatenate([util.read_vol(file) for file in files], axis=-1) if vols.shape[0] == vols.shape[1] == vols.shape[2]: axis = int(input('shape: {}\n> '.format(vols.shape))) elif vols.shape[0] == vols.shape[1]: axis = 2 elif vols.shape[0] == vols.shape[2]: axis = 1 elif vols.shape[1] == vols.shape[2]: axis = 0 else: axis = int(input('shape: {}\n> '.format(vols.shape))) vols = np.moveaxis(vols, axis, 0) shape = vols.shape even_i = np.arange(0, shape[0], 2) even_slices = vols[::2, ...] even_interpolator = interp1d(even_i, even_slices, kind='linear', axis=0) odd_i = np.arange(1, shape[0], 2) odd_slices = vols[1::2, ...] odd_interpolator = interp1d(odd_i, odd_slices, kind='linear', axis=0) if shape[0] % 2 == 0: evens = even_interpolator(np.arange(0, shape[0] - 1)) odds = odd_interpolator(np.arange(1, shape[0])) evens = np.concatenate((evens, evens[np.newaxis, -1, ...])) else: evens = even_interpolator(np.arange(0, shape[0])) odds = odd_interpolator(np.arange(1, shape[0] - 1)) odds = np.concatenate((odds, odds[np.newaxis, -1, ...])) odds = np.concatenate((np.zeros([ 1, ] + list(shape[1:])), odds)) if volume: even_1 = evens[shape[0] // 3, ...] even_2 = evens[shape[0] * 2 // 3, ...] odd_1 = odds[shape[0] // 3, ...] odd_2 = odds[shape[0] * 2 // 3, ...] even_img_1 = np.zeros((shape[1], shape[2], shape[3] * 2)) even_img_1[..., ::2] = even_1 even_img_1[..., 1::2] = odd_1 even_img_2 = np.zeros((shape[1], shape[2], shape[3] * 2)) even_img_2[..., ::2] = even_2 even_img_2[..., 1::2] = odd_2 odd_img_1 = np.zeros((shape[1], shape[2], shape[3] * 2)) odd_img_1[..., ::2] = odd_1 odd_img_1[..., 1::2] = even_1 odd_img_2 = np.zeros((shape[1], shape[2], shape[3] * 2)) odd_img_2[..., ::2] = odd_2 odd_img_2[..., 1::2] = even_2 temp_folder = 'data/temp/' os.makedirs(temp_folder, exist_ok=True) util.save_vol(even_img_1, temp_folder + 'even_1.nii.gz') util.save_vol(even_img_2, temp_folder + 'even_2.nii.gz') util.save_vol(odd_img_1, temp_folder + 'odd_1.nii.gz') util.save_vol(odd_img_2, temp_folder + 'odd_2.nii.gz') else: even_1 = evens[shape[0] // 3, shape[1] // 2, ...] even_2 = evens[shape[0] * 2 // 3, shape[1] // 2, ...] odd_1 = odds[shape[0] // 3, shape[1] // 2, ...] odd_2 = odds[shape[0] * 2 // 3, shape[1] // 2, ...] even_img_1 = np.zeros((shape[2], shape[3] * 2)) even_img_1[:, ::2] = even_1 even_img_1[:, 1::2] = odd_1 even_img_2 = np.zeros((shape[2], shape[3] * 2)) even_img_2[:, ::2] = even_2 even_img_2[:, 1::2] = odd_2 odd_img_1 = np.zeros((shape[2], shape[3] * 2)) odd_img_1[:, ::2] = odd_1 odd_img_1[:, 1::2] = even_1 odd_img_2 = np.zeros((shape[2], shape[3] * 2)) odd_img_2[:, ::2] = odd_2 odd_img_2[:, 1::2] = even_2 fig = plt.figure(figsize=(16, 8)) fig.add_subplot(2, 2, 1) plt.imshow(odd_img_1) plt.axis('off') plt.title('odd') fig.add_subplot(2, 2, 2) plt.imshow(even_img_1) plt.axis('off') plt.title('even') fig.add_subplot(2, 2, 3) plt.imshow(odd_img_2) plt.axis('off') plt.title('odd') fig.add_subplot(2, 2, 4) plt.imshow(even_img_2) plt.axis('off') plt.title('even') plt.show(block=False) order = input('1. odd\n2. even\n> ') plt.close() new_shape = list(shape) new_shape[-1] *= 2 series = np.zeros(new_shape) if order == '1': series[..., ::2] = odds series[..., 1::2] = evens elif order == '2': series[..., ::2] = evens series[..., 1::2] = odds else: raise ValueError('Must be even or odd slice.') series = np.moveaxis(series, 0, axis) sample = folder.split('/')[2] new_folder = 'data/originals/{}/'.format(folder.split('/')[2]) os.makedirs(new_folder, exist_ok=True) for i in range(new_shape[-1]): util.save_vol(series[..., i], new_folder + sample + '_{}.nii.gz'.format(i))