diff --git a/requirements.txt b/requirements.txt index 29742e3..cc94a5e 100644 --- a/requirements.txt +++ b/requirements.txt @@ -15,3 +15,4 @@ pandas sphinx sphinx_rtd_theme pytest-json-report +ivimnet \ No newline at end of file diff --git a/src/original/OGC_AUMC_IVIMNET/Example_1_simple_map.py b/src/original/OGC_AUMC_IVIMNET/Example_1_simple_map.py new file mode 100644 index 0000000..5f55818 --- /dev/null +++ b/src/original/OGC_AUMC_IVIMNET/Example_1_simple_map.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- +""" +September 2020 by Oliver Gurney-Champion & Misha Kaandorp +oliver.gurney.champion@gmail.com / o.j.gurney-champion@amsterdamumc.nl +https://www.github.com/ochampion + +Code is uploaded as part of our publication in MRM (Kaandorp et al. Improved unsupervised physics-informed deep learning for intravoxel-incoherent motion modeling and evaluation in pancreatic cancer patients. MRM 2021) + +requirements: +numpy +torch +tqdm +matplotlib +scipy +joblib +""" +import IVIMNET.simulations as sim +from hyperparams import hyperparams as hp_example_1 +import IVIMNET.deep as deep +import time +import torch +import IVIMNET.fitting_algorithms as fit + +# Import parameters +arg = hp_example_1() +arg = deep.checkarg(arg) +print(arg.save_name) +for SNR in arg.sim.SNR: + # this simulates the signal + IVIM_signal_noisy, D, f, Dp = sim.sim_signal(SNR, arg.sim.bvalues, sims=arg.sim.sims, Dmin=arg.sim.range[0][0], + Dmax=arg.sim.range[1][0], fmin=arg.sim.range[0][1], + fmax=arg.sim.range[1][1], Dsmin=arg.sim.range[0][2], + Dsmax=arg.sim.range[1][2], rician=arg.sim.rician) + + start_time = time.time() + # train network + net = deep.learn_IVIM(IVIM_signal_noisy, arg.sim.bvalues, arg) + elapsed_time = time.time() - start_time + print('\ntime elapsed for training: {}\n'.format(elapsed_time)) + + # simulate IVIM signal for prediction + [dwi_image_long, Dt_truth, Fp_truth, Dp_truth] = sim.sim_signal_predict(arg, SNR) + + # predict + start_time = time.time() + paramsNN = deep.predict_IVIM(dwi_image_long, arg.sim.bvalues, net, arg) + elapsed_time = time.time() - start_time + print('\ntime elapsed for inference: {}\n'.format(elapsed_time)) + # remove network to save memory + del net + if arg.train_pars.use_cuda: + torch.cuda.empty_cache() + + start_time = time.time() + # all fitting is done in the fit.fit_dats for the other fitting algorithms (lsq, segmented and Baysesian) + paramsf = fit.fit_dats(arg.sim.bvalues, dwi_image_long, arg.fit) + elapsed_time = time.time() - start_time + print('\ntime elapsed for lsqfit: {}\n'.format(elapsed_time)) + print('results for lsqfit') + + # plot values predict and truth + sim.plot_example1(paramsNN, paramsf, Dt_truth, Fp_truth, Dp_truth, arg, SNR) diff --git a/src/original/OGC_AUMC_IVIMNET/Example_2_simulations.py b/src/original/OGC_AUMC_IVIMNET/Example_2_simulations.py new file mode 100644 index 0000000..815e4ba --- /dev/null +++ b/src/original/OGC_AUMC_IVIMNET/Example_2_simulations.py @@ -0,0 +1,48 @@ +""" +September 2020 by Oliver Gurney-Champion & Misha Kaandorp +oliver.gurney.champion@gmail.com / o.j.gurney-champion@amsterdamumc.nl +https://www.github.com/ochampion + +Code is uploaded as part of our publication in MRM (Kaandorp et al. Improved unsupervised physics-informed deep learning for intravoxel-incoherent motion modeling and evaluation in pancreatic cancer patients. MRM 2021) + +requirements: +numpy +torch +tqdm +matplotlib +scipy +joblib +""" + +# import +import numpy as np +import IVIMNET.simulations as sim +import IVIMNET.deep as deep +from hyperparams import hyperparams as hp_example + +# load hyperparameter +arg = hp_example() +arg = deep.checkarg(arg) + +matlsq = np.zeros([len(arg.sim.SNR), 3, 3]) +matNN = np.zeros([len(arg.sim.SNR), 3, 3]) +stability = np.zeros([len(arg.sim.SNR), 3]) +a = 0 + +for SNR in arg.sim.SNR: + print('\n simulation at SNR of {snr}\n'.format(snr=SNR)) + if arg.fit.do_fit: + matlsq[a, :, :], matNN[a, :, :], stability[a, :] = sim.sim(SNR, arg) + print('\nresults from lsq:') + print(matlsq) + else: + matNN[a, :, :], stability[a, :] = sim.sim(SNR, arg) + a = a + 1 + print('\nresults from NN: columns show themean, the RMSE/mean and the Spearman coef [DvDp,Dvf,fvDp] \n' + 'the rows show D, f and D*\n' + 'and the different matixes repressent the different SNR levels {}:'.format(arg.sim.SNR)) + print(matNN) + # if repeat is higher than 1, then print stability (CVNET) + if arg.sim.repeats > 1: + print('\nstability of NN for D, f and D* at different SNR levels:') + print(stability) diff --git a/src/original/OGC_AUMC_IVIMNET/Example_3_volunteer.py b/src/original/OGC_AUMC_IVIMNET/Example_3_volunteer.py new file mode 100644 index 0000000..e2e49ea --- /dev/null +++ b/src/original/OGC_AUMC_IVIMNET/Example_3_volunteer.py @@ -0,0 +1,108 @@ +""" +September 2020 by Oliver Gurney-Champion & Misha Kaandorp +oliver.gurney.champion@gmail.com / o.j.gurney-champion@amsterdamumc.nl +https://www.github.com/ochampion + +Code is uploaded as part of our publication in MRM (Kaandorp et al. Improved unsupervised physics-informed deep learning for intravoxel-incoherent motion modeling and evaluation in pancreatic cancer patients. MRM 2021) + +requirements: +numpy +torch +tqdm +matplotlib +scipy +joblib +""" + +# this loads all patient data and evaluates it all. +import os +import time +import nibabel as nib +import numpy as np +import IVIMNET.deep as deep +import torch +from IVIMNET.fitting_algorithms import fit_dats +from hyperparams import hyperparams as hp + +arg = hp() +arg = deep.checkarg(arg) + +testdata = False + +### folder patient data +folder = 'example_data' + +### load patient data +print('Load patient data \n') +# load and init b-values +text_file = np.genfromtxt('{folder}/bvalues.bval'.format(folder=folder)) +bvalues = np.array(text_file) +selsb = np.array(bvalues) == 0 +# load nifti +data = nib.load('{folder}/data.nii.gz'.format(folder=folder)) +datas = data.get_fdata() +# reshape image for fitting +sx, sy, sz, n_b_values = datas.shape +X_dw = np.reshape(datas, (sx * sy * sz, n_b_values)) + +### select only relevant values, delete background and noise, and normalise data +S0 = np.nanmean(X_dw[:, selsb], axis=1) +S0[S0 != S0] = 0 +S0 = np.squeeze(S0) +valid_id = (S0 > (0.5 * np.median(S0[S0 > 0]))) +datatot = X_dw[valid_id, :] +# normalise data +S0 = np.nanmean(datatot[:, selsb], axis=1).astype('