def test__crop_img_to(): data = np.zeros((5, 6, 7)) data[2:4, 1:5, 3:6] = 1 affine = np.diag((4, 3, 2, 1)) img = nibabel.Nifti1Image(data, affine=affine) slices = [slice(2, 4), slice(1, 5), slice(3, 6)] cropped_img = image._crop_img_to(img, slices, copy=False) new_origin = np.array((4, 3, 2)) * np.array((2, 1, 3)) # check that correct part was extracted: assert_true((cropped_img.get_data() == 1).all()) assert_true(cropped_img.shape == (2, 4, 3)) # check that affine was adjusted correctly assert_true((cropped_img.get_affine()[:3, 3] == new_origin).all()) # check that data was really not copied data[2:4, 1:5, 3:6] = 2 assert_true((cropped_img.get_data() == 2).all()) # check that copying works copied_cropped_img = image._crop_img_to(img, slices) data[2:4, 1:5, 3:6] = 1 assert_true((copied_cropped_img.get_data() == 2).all())
def _generate_preprocessed_samples_core(path_brats_preprocessed, sample, mean_samples, std_smaples, resized_img_shape=(144, 144, 144)): modality_images = [] modality_filenames = [] for mod in modalities[:-1]: mod = '*' + mod + '.nii.gz' mod_wise_files = glob(os.path.join(sample, mod)) assert len(mod_wise_files) == 1, 'Minimum one modality nifti image expected' modality_filenames.append(mod_wise_files[0]) slices = _crop_to_non_zero_content(modality_filenames) for mod_fn in modality_filenames: data = nib.load(mod_fn) data = _crop_img_to(data, slices, copy=True) data, new_spacing_mi = _resize_nifti_images(data, interpolation='linear', resized_img_shape=resized_img_shape) modality_images.append(data) modality_images = np.asarray(modality_images, dtype=np.float32) modality_images -= mean_samples[:, np.newaxis, np.newaxis, np.newaxis] modality_images /= std_smaples[:, np.newaxis, np.newaxis, np.newaxis] affine = np.eye(4) new_spacing_mi = np.append(new_spacing_mi, 1) np.fill_diagonal(a=affine, val=new_spacing_mi) mod_data_resized_nifti = nib.Nifti1Image(modality_images, affine=affine) mod_filename = os.path.basename(sample) + '_mod.nii.gz' nib.save(mod_data_resized_nifti, os.path.join(path_brats_preprocessed, mod_filename)) seg = '*' + 'seg' + '.nii.gz' seg_file = glob(os.path.join(sample, seg)) assert len(seg_file) == 1, 'Minimum one segmeantion image expected' seg_data = nib.load(seg_file[0]) seg_data = _crop_img_to(seg_data, slices, copy=True) seg_data, new_spacing_seg = _resize_nifti_images(seg_data, interpolation='nearest', resized_img_shape=resized_img_shape) seg_data = seg_data[np.newaxis] affine = np.eye(4) new_spacing_seg = np.append(new_spacing_seg, 1) np.fill_diagonal(a=affine, val=new_spacing_seg) seg_data_resized_nifti = nib.Nifti1Image(seg_data, affine=affine) seg_filename = os.path.basename(sample) + '_seg.nii.gz' nib.save(seg_data_resized_nifti, os.path.join(path_brats_preprocessed, seg_filename))
def _generate_mean_std_from_sample(sample, resized_img_shape): modality_images = [] modality_filenames = [] for mod in modalities[:-1]: mod = '*' + mod + '.nii.gz' mod_wise_files = glob(os.path.join(sample, mod)) assert len(mod_wise_files) == 1, 'Minimum one modality nifti image expected' modality_filenames.append(mod_wise_files[0]) slices = _crop_to_non_zero_content(modality_filenames) for mod_fn in modality_filenames: data = nib.load(mod_fn) data = _crop_img_to(data, slices, copy=True) data, _ = _resize_nifti_images(data, interpolation='linear', resized_img_shape=resized_img_shape) modality_images.append(data) modality_images = np.asarray(modality_images) std = np.std(modality_images, axis=(-3, -2, -1)) mean = np.mean(modality_images, axis=(-3, -2, -1)) return mean, std