def load_original(): root = join('..','..','..','data','brain') # Get the dataset info dataset = pd.read_excel(join(root,'brain_info.xlsx')) # Initialise the data and label arrays n_imgs = len(dataset['status']) # X = np.zeros((n_imgs,109,91)) X = np.zeros((n_imgs,54,45)) y = np.zeros(n_imgs, dtype='uint8') for i in range(n_imgs): print('Image {} of {}'.format(i+1, n_imgs)) # Load the image and scale by factor of 0.5 fpath = join(root, 'images', dataset['ptid'][i] + '.png') tmp = misc.imread(fpath, flatten=True) tmp = misc.imresize(tmp, 0.25) tmp = np.rot90(tmp, 2) X[i] = signal_to_pdf(tmp.astype(np.float64)) if dataset['status'][i] == 'p': y[i] = 1 np.savez(join('optrans','datasets','adni_data'), X=X, y=y) return
def generate_radoncdt(): with np.load(join('optrans','datasets','adni_data.npz')) as data: X = data['X'] y = data['y'] img0 = signal_to_pdf(X.mean(axis=0)) radoncdt = RadonCDT() rcdt = [] transport_map = [] displacements = [] for i,img1 in enumerate(X): print('R-CDT {} of {}'.format(i+1, X.shape[0])) rcdt.append(radoncdt.forward(img0, img1)) transport_map.append(radoncdt.transport_map_) displacements.append(radoncdt.displacements_) rcdt = np.array(rcdt) transport_map = np.array(transport_map) displacements = np.array(displacements) np.save(join('optrans','datasets','adni_img0'), img0) np.savez(join('optrans','datasets','adni_rcdt'), rcdt=rcdt, y=y) np.savez(join('optrans','datasets','adni_rcdt_maps'), y=y, transport_map=transport_map) np.savez(join('optrans','datasets','adni_rcdt_disp'), y=y, displacements=displacements) return
def generate_radoncdt(): X, y = gaussians.load_data() n_imgs, h, w = X.shape img0 = signal_to_pdf(np.ones((h,w))) radoncdt = RadonCDT() rcdt = [] transport_map = [] displacements = [] for i,img1 in enumerate(X): print('R-CDT {} of {}'.format(i+1, n_imgs)) rcdt.append(radoncdt.forward(img0, img1)) transport_map.append(radoncdt.transport_map_) displacements.append(radoncdt.displacements_) rcdt = np.array(rcdt) transport_map = np.array(transport_map) displacements = np.array(displacements) np.save(join('optrans','datasets','gaussians_img0'), img0) np.savez(join('optrans','datasets','gaussians_rcdt'), rcdt=rcdt, y=y) np.savez(join('optrans','datasets','gaussians_rcdt_maps'), y=y, transport_map=transport_map) np.savez(join('optrans','datasets','gaussians_rcdt_disp'), y=y, displacements=displacements) return
def generate_image(): X, y = gaussians.make_gaussians(n_samples=100, n_dots=[1,2,3]) for i in range(X.shape[0]): X[i] = signal_to_pdf(X[i]) np.savez(join('optrans','datasets','gaussians_data'), X=X, y=y) return
def load_original(): root = 'X:/Data/MRI/oasis_cross-sectional' dataset = pd.read_excel(join(root, 'oasis_cross-sectional.xlsx')) # Only get session 1 data dataset = dataset[dataset['Session'] == 1] pid = dataset['ID'].tolist() y = np.array([0 if row == 'hc' else 1 for row in dataset['Dx'].tolist()]) sex = [0 if row == 'F' else 1 for row in dataset['M/F'].tolist()] age = dataset['Age'].tolist() edu = dataset['Educ'].tolist() ses = dataset['SES'].tolist() mmse = dataset['MMSE'].tolist() cdr = dataset['CDR'].tolist() etiv = dataset['eTIV'].tolist() nwbv = dataset['nWBV'].tolist() asf = dataset['ASF'].tolist() metadata = np.array([sex, age, edu, ses, mmse, cdr, etiv, nwbv, asf]).T n_samples = len(dataset) X = np.zeros((n_samples, 208, 176)) for i, p in enumerate(pid): print(i, p) fname = '*111_t88_masked_gfc.img' fpath = glob.glob( join(root, p, 'PROCESSED', 'MPRAGE', 'T88_111', fname))[0] vol = sitk.GetArrayFromImage(sitk.ReadImage(fpath)) print(vol[85].shape) tmp = np.rot90(vol[85], 2) X[i] = signal_to_pdf(tmp.astype(np.float64), total=1000.) np.savez(join('..', 'optrans', 'datasets', 'oasis_data'), X=X, y=y, metadata=metadata) return
- RadonCDT - VOT2D - MultiVOT2D - CLOT - SPOT2D Liam Cattell -- June 2018 """ # Load sample data img0 = oasis.load_img0() X, _, _ = oasis.load_data() img1 = X[0] # Convert images to PDFs img0 = signal_to_pdf(img0, sigma=1., total=100.) img1 = signal_to_pdf(img1, sigma=1., total=100.) # Initialise optimal transport methods methods = [RadonCDT(), VOT2D(alpha=0.001), MultiVOT2D(alpha=0.001), CLOT(lr=1e-7, tol=1e-4), SPOT2D()] # Plot settings fs = 16 vmin = min(img0.min(), img1.min()) vmax = max(img0.max(), img1.max()) # Initialise figure and plot original images
Liam Cattell -- May 2018 """ # Normalization parameters sigma = 1. epsilon = 8. total = 100. # Load images X, _ = adni.load_data() img0 = adni.load_img0() img1 = X[1] # CLOT is *very* sensitive to the image normalization img0 = signal_to_pdf(img0, sigma=sigma, epsilon=epsilon, total=total) img1 = signal_to_pdf(img1, sigma=sigma, epsilon=epsilon, total=total) # Continuous LOT clot = CLOT(max_iter=500, lr=1e-5, momentum=0.9, verbose=1) lot = clot.forward(img0, img1) # Reconstruct images using initial map img0_recon0 = clot.apply_forward_map(clot.transport_map_initial_, img1) img1_recon0 = clot.apply_inverse_map(clot.transport_map_initial_, img0) # Reconstruct images using final map img0_recon = clot.apply_forward_map(clot.transport_map_, img1) img1_recon = clot.apply_inverse_map(clot.transport_map_, img0) # Colour scaling
from optrans.continuous import RadonCDT """ Compute the forward and inverse Radon-CDT of a sample image. Liam Cattell -- January 2018 """ # Load a sample image img = img_as_float(data.camera()[::2, ::2]) # Select two patches to be our sample images img0 = img[50:162, 70:134] img1 = img[32:144, 64:128] # Convert images to PDFs img0 = signal_to_pdf(img0, sigma=1.) img1 = signal_to_pdf(img1, sigma=1.) # Compute Radon-CDT of img1 w.r.t img0 theta = np.arange(0, 179, 2) radoncdt = RadonCDT(theta=theta) img1_hat = radoncdt.forward(img0, img1) # Apply transport map in order to reconstruct images img0_recon = radoncdt.apply_forward_map(radoncdt.transport_map_, img1) img1_recon = radoncdt.apply_inverse_map(radoncdt.transport_map_, img0) # img1_recon = radoncdt.inverse() fig, ax = plt.subplots(3, 2) ax[0, 0].imshow(img0) ax[0, 0].set_title('Reference img0')
from optrans.utils import signal_to_pdf from optrans.continuous import CDT """ Compute the forward and inverse CDT of a Gaussian w.r.t a uniform distribution. Liam Cattell -- January 2018 """ # Create the uniform reference and Gaussian signal x = np.linspace(-5, 5, 256) sig0 = np.ones(x.size) sig1 = 1 / np.sqrt(2 * np.pi) * np.exp(-0.5 * x**2) # Convert signals to PDFs sig0 = signal_to_pdf(sig0) sig1 = signal_to_pdf(sig1) # Compute CDT of sig1 w.r.t sig0 cdt = CDT() sig1_hat = cdt.forward(sig0, sig1) # Apply transport map in order to reconstruct signals sig0_recon = cdt.apply_forward_map(cdt.transport_map_, sig1) sig1_recon = cdt.apply_inverse_map(cdt.transport_map_, sig0) # sig1_recon = cdt.inverse() # Plot results fig, ax = plt.subplots(3, 2, sharex=True) ax[0, 0].plot(sig0) ax[0, 0].set_title('Reference sig0')
""" Compute the geodesic between two 1D Gaussians. Liam Cattell -- January 2018 """ # Create two translated delta functions as test signals n = 128 sig0 = np.zeros(n) sig1 = np.zeros(n) sig0[32] = 1. sig1[96] = 1. # Smooth the delta functions into Gaussians and convert to PDFs sig0 = signal_to_pdf(sig0, sigma=6.) sig1 = signal_to_pdf(sig1, sigma=6.) # Compute the CDT of sig1 w.r.t. sig0 cdt = CDT() sig1_hat = cdt.forward(sig0, sig1) # Get the domain of our signal x = cdt.transport_map_ + cdt.displacements_ # Plot linear interpolation in signal space and CDT space fig, ax = plt.subplots(2, 5, sharex=True, sharey=True, figsize=(10,6)) for i,alpha in enumerate(np.linspace(0, 1, 5)): # Interpolation in signal space sig_interp = (1. - alpha) * sig0 + alpha * sig1 ax[0,i].plot(sig_interp)