S0 = np.nanmean(X_dw_all[np.squeeze(X_dw_all[:, bvalues == 0] > 0), bvalues == 0], axis=0) valid_id = np.squeeze(X_dw_all[:, bvalues == 0] < (S0 / 3)) X_dw_all[np.squeeze(valid_id), :] = 0 valid_id = np.sum(X_dw_all == 0, axis=1) == 0 X_dw_sel = X_dw_all[valid_id, :] S0 = np.nanmean(X_dw_sel[:, bvalues == 0], axis=1) X_dw_sel = X_dw_sel / S0[:, None] res = [ i for i, val in enumerate(X_dw_sel != X_dw_sel) if not val.any() ] net = deep.learn_IVIM(X_dw_sel[res], bvalues, arg) paramsNN = deep.infer_IVIM(X_dw_sel, bvalues, net) del net gofNN = goodness_of_fit(bvalues, paramsNN[0], paramsNN[1], paramsNN[2], paramsNN[3], X_dw_sel) names = [ 'geof_NN_{ii}_{net}_2'.format(ii=ii, net=run_net), 'Dp_NN_{ii}_{net}_2'.format(ii=ii, net=run_net), 'D_NN_{ii}_{net}_2'.format(ii=ii, net=run_net), 'f_NN_{ii}_{net}_2'.format(ii=ii, net=run_net), 'S0_NN_{ii}_{net}_2'.format(ii=ii, net=run_net) ] for k in range(len(names)): img = np.zeros([sx * sy * sz]) if k is 0: img[valid_id] = gofNN else:
datmean2=np.nanmean(datatot2,axis=0) if testdata: paramslsq = fit_least_squares_S0(bvalues, datmean, S0_output=True) paramslsq2 = fit_least_squares_S0(bvalues2, datmean2, S0_output=True) plt.plot(datmean[index]) plt.plot(ivimN(bvalues[index], paramslsq[0]*10, paramslsq[1]*1000, paramslsq[2]*10, paramslsq[3])) plt.plot(datmean2[index2]) plt.plot(ivimN(bvalues[index], paramslsq2[0]*10, paramslsq2[1]*1000, paramslsq2[2]*10, paramslsq2[3])) net = deep.learn_IVIM(np.transpose(np.repeat(np.expand_dims(datmean,1),1000,axis=1)), bvalues, arg) net2 = deep.learn_IVIM(np.transpose(np.repeat(np.expand_dims(datmean2,1),1000,axis=1)), bvalues2, arg) paramsNN=deep.infer_IVIM(np.expand_dims(datmean,0), bvalues, net) paramsNN2=deep.infer_IVIM(np.expand_dims(datmean2,0), bvalues2, net2) plt.plot(datmean[index]) plt.plot(ivimN(bvalues[index], paramsNN[0][0]*10, paramsNN[1][0]*1000, paramsNN[2][0]*10, paramsNN[3][0])) plt.plot(datmean2[index2]) plt.plot(ivimN(bvalues[index], paramsNN2[0][0]*10, paramsNN2[1][0]*1000, paramsNN2[2][0]*10, paramsNN2[3][0])) print('least squares fitting\n') if dolsq: if not load_lsq: if segmented: print('segmented\n') paramslsq=fit_segmented_array(bvalues,datatot)
X_dw_all = np.reshape(datas, (sx * sy * sz, n_b_values)) valid_id = np.sum(X_dw_all == 0, axis=1) == 0 datatot = np.append(datatot, X_dw_all[valid_id, :], axis=0) del valid_id, X_dw_all, datas, data bval = np.delete(bval, np.s_[3::4], 0) datatot = np.array(np.delete(datatot, np.s_[3::4], 1)) S0 = np.nanmean(datatot[:, bval == 0], axis=1) datatot = datatot / S0[:, None] datmean = np.nanmean(datatot, axis=0) res = [i for i, val in enumerate(datatot != datatot) if not val.any()] net = deep.learn_IVIM(datatot[res], bval, arg) torch.save(net, 'network_{nn}.pt'.format(nn=arg.run_net)) paramsNN = deep.infer_IVIM(datatot, bval, net) del net gofNN = goodness_of_fit(bval, paramsNN[0], paramsNN[1], paramsNN[2], paramsNN[3], datatot) names = [ 'geof_NN_{nn}.nii'.format(nn=arg.run_net), 'Dp_NN_{nn}.nii'.format(nn=arg.run_net), 'D_NN_{nn}.nii'.format(nn=arg.run_net), 'f_NN_{nn}.nii'.format(nn=arg.run_net), 'S0_NN_{nn}.nii'.format(nn=arg.run_net) ] tot = 0 bval = np.array([ 0, 0, 0, 0, 700, 700, 700, 700, 1, 1, 1, 1, 5, 5, 5, 5, 100, 100, 100, 100, 300, 300, 300, 300, 10, 10, 10, 10, 0, 0, 0, 0, 20, 20, 20, 20, 500, 500,
def sim(SNR, b, arg, run_net=None, sims=100000, num_samples_leval=10000, Dmin=0.5 / 1000, Dmax=3.0 / 1000, fmin=0.05, fmax=0.4, Dsmin=0.01, Dsmax=0.1, rician=False, segmented=False): IVIM_signal_noisy, f, D, Dp = sim_signal(SNR, b, sims=sims, Dmin=Dmin, Dmax=Dmax, fmin=fmin, fmax=fmax, Dsmin=Dsmin, Dsmax=Dsmax, rician=rician) D = D[:num_samples_leval] Dp = Dp[:num_samples_leval] f = f[:num_samples_leval] if arg.repeats > 1: paramsNN = np.zeros([arg.repeats, 4, num_samples_leval]) for aa in range(arg.repeats): start_time = time.time() net = deep.learn_IVIM(IVIM_signal_noisy, b, arg) elapsed_time = time.time() - start_time print('\ntime elapsed for training: {}\n'.format(elapsed_time)) start_time = time.time() if arg.repeats > 1: paramsNN[aa] = deep.infer_IVIM( IVIM_signal_noisy[:num_samples_leval, :], b, net) else: paramsNN = deep.infer_IVIM(IVIM_signal_noisy, b, net) elapsed_time = time.time() - start_time print('\ntime elapsed for inference: {}\n'.format(elapsed_time)) # ['Ke_NN.nii', 'f_NN.nii', 'tau_NN.nii', 'v_NN.nii'] del net print('results for NN') IVIM_signal_noisy = IVIM_signal_noisy[:num_samples_leval, :] if arg.repeats > 1: matNN = np.zeros([arg.repeats, 3, 3]) for aa in range(arg.repeats): matNN[aa] = print_errors(np.squeeze(D), np.squeeze(f), np.squeeze(Dp), paramsNN[aa]) matNN = np.mean(matNN, axis=0) stability = np.sqrt( np.mean(np.square(np.std(paramsNN, axis=0)), axis=1)) stability = stability[[1, 2, 0]] / [ np.mean(D), np.mean(f), np.mean(Dp) ] paramsNN = paramsNN[0] else: matNN = print_errors(np.squeeze(D), np.squeeze(f), np.squeeze(Dp), paramsNN) stability = np.zeros(3) dummy = np.array(paramsNN) del paramsNN plt.figure() plt.plot(D[:1000], Dp[:1000], 'rx', markersize=5) plt.xlim(0, 0.005) plt.ylim(0, 0.3) plt.xlabel('Dt') plt.ylabel('Dp') plt.gcf() plt.savefig( 'C:/Users/ojgurney-champion/Dropbox/Research/DeepLearning/deep_ivim/Output/inputDtDp.png' ) plt.ion() plt.show() plt.figure() plt.plot(f[:1000], Dp[:1000], 'rx', markersize=5) plt.xlim(0, 0.6) plt.ylim(0, 0.3) plt.xlabel('f') plt.ylabel('Dp') plt.gcf() plt.savefig( 'C:/Users/ojgurney-champion/Dropbox/Research/DeepLearning/deep_ivim/Output/inputfDp.png' ) plt.ion() plt.show() plt.figure() plt.plot(D[:1000], f[:1000], 'rx', markersize=5) plt.xlim(0, 0.005) plt.ylim(0, 0.6) plt.xlabel('Dt') plt.ylabel('f') plt.gcf() plt.savefig( 'C:/Users/ojgurney-champion/Dropbox/Research/DeepLearning/deep_ivim/Output/inputDtf.png' ) plt.ion() plt.show() plt.figure() plt.plot(dummy[1, :1000], dummy[0, :1000], 'rx', markersize=5) plt.xlim(0, 0.005) plt.ylim(0, 0.3) plt.xlabel('Dt') plt.ylabel('Dp') plt.gcf() plt.savefig( 'C:/Users/ojgurney-champion/Dropbox/Research/DeepLearning/deep_ivim/Output/NNDtDp.png' ) plt.ion() plt.show() plt.figure() plt.plot(dummy[2, :1000], dummy[0, :1000], 'rx', markersize=5) plt.xlim(0, 0.6) plt.ylim(0, 0.3) plt.xlabel('f') plt.ylabel('Dp') plt.gcf() plt.savefig( 'C:/Users/ojgurney-champion/Dropbox/Research/DeepLearning/deep_ivim/Output/NNfDp.png' ) plt.ion() plt.show() plt.figure() plt.plot(dummy[1, :1000], dummy[2, :1000], 'rx', markersize=5) plt.xlim(0, 0.005) plt.ylim(0, 0.6) plt.xlabel('Dt') plt.ylabel('f') plt.gcf() plt.savefig( 'C:/Users/ojgurney-champion/Dropbox/Research/DeepLearning/deep_ivim/Output/NNDtf.png' ) plt.ion() plt.show() plt.figure() plt.plot(Dp[:1000], dummy[0, :1000], 'rx', markersize=5) plt.xlim(0, 0.3) plt.ylim(0, 0.3) plt.ylabel('DpNN') plt.xlabel('Dpin') plt.gcf() plt.savefig( 'C:/Users/ojgurney-champion/Dropbox/Research/DeepLearning/deep_ivim/Output/DpNNDpin.png' ) plt.ion() plt.show() plt.figure() plt.plot(D[:1000], dummy[1, :1000], 'rx', markersize=5) plt.xlim(0, 0.005) plt.ylim(0, 0.005) plt.ylabel('DtNN') plt.xlabel('Dtin') plt.gcf() plt.savefig( 'C:/Users/ojgurney-champion/Dropbox/Research/DeepLearning/deep_ivim/Output/DtNNDtin.png' ) plt.ion() plt.show() plt.figure() plt.plot(f[:1000], dummy[2, :1000], 'rx', markersize=5) plt.xlim(0, 0.6) plt.ylim(0, 0.6) plt.ylabel('fNN') plt.xlabel('fin') plt.gcf() plt.savefig( 'C:/Users/ojgurney-champion/Dropbox/Research/DeepLearning/deep_ivim/Output/fNNfin.png' ) plt.ion() plt.show() start_time = time.time() if segmented: paramsf = fit.fit_segmented_array(b, IVIM_signal_noisy) else: paramsf = fit.fit_least_squares_array(b, IVIM_signal_noisy) elapsed_time = time.time() - start_time print('\ntime elapsed for lsqfit: {}\n'.format(elapsed_time)) print('results for lsqfit') matlsq = print_errors(np.squeeze(D), np.squeeze(f), np.squeeze(Dp), paramsf) dummy = np.array(paramsf) del paramsf, IVIM_signal_noisy plt.figure() plt.plot(dummy[1, :1000], dummy[0, :1000], 'rx', markersize=5) plt.xlim(0, 0.005) plt.ylim(0, 0.3) plt.xlabel('D') plt.ylabel('Dp') plt.gcf() plt.savefig( 'C:/Users/ojgurney-champion/Dropbox/Research/DeepLearning/deep_ivim/Output/LSQDtDp.png' ) plt.ion() plt.show() plt.figure() plt.plot(dummy[2, :1000], dummy[0, :1000], 'rx', markersize=5) plt.xlim(0, 0.6) plt.ylim(0, 0.3) plt.xlabel('f') plt.ylabel('Dp') plt.gcf() plt.savefig( 'C:/Users/ojgurney-champion/Dropbox/Research/DeepLearning/deep_ivim/Output/LSQfDp.png' ) plt.ion() plt.show() plt.figure() plt.plot(dummy[1, :1000], dummy[2, :1000], 'rx', markersize=5) plt.xlim(0, 0.005) plt.ylim(0, 0.6) plt.xlabel('D') plt.ylabel('f') plt.gcf() plt.savefig( 'C:/Users/ojgurney-champion/Dropbox/Research/DeepLearning/deep_ivim/Output/LSQDtf.png' ) plt.ion() plt.show() plt.figure() plt.plot(Dp[:1000], dummy[0, :1000], 'rx', markersize=5) plt.xlim(0, 0.3) plt.ylim(0, 0.3) plt.ylabel('Dplsq') plt.xlabel('Dpin') plt.gcf() plt.savefig( 'C:/Users/ojgurney-champion/Dropbox/Research/DeepLearning/deep_ivim/Output/DpLSQDpin.png' ) plt.ion() plt.show() plt.figure() plt.plot(D[:1000], dummy[1, :1000], 'rx', markersize=5) plt.xlim(0, 0.005) plt.ylim(0, 0.005) plt.ylabel('Dtlsq') plt.xlabel('Dtin') plt.gcf() plt.savefig( 'C:/Users/ojgurney-champion/Dropbox/Research/DeepLearning/deep_ivim/Output/DtLSQDtin.png' ) plt.ion() plt.show() plt.figure() plt.plot(f[:1000], dummy[2, :1000], 'rx', markersize=5) plt.xlim(0, 0.6) plt.ylim(0, 0.6) plt.ylabel('flsq') plt.xlabel('fin') plt.gcf() plt.savefig( 'C:/Users/ojgurney-champion/Dropbox/Research/DeepLearning/deep_ivim/Output/fLSQfin.png' ) plt.ion() plt.show() return matlsq, matNN, stability
index2 = np.argsort(bvalues2) S0 = np.nanmean(datatot[:,bvalues==0],axis=1) datatot = datatot/S0[:,None] S0 = np.nanmean(datatot3[:,bvalues==0],axis=1) datatot3 = datatot3/S0[:,None] datmean=np.nanmean(datatot,axis=0) datmean2=np.nanmean(datatot2,axis=0) res = [i for i, val in enumerate(datatot!=datatot) if not val.any()] res2 = [i for i, val in enumerate(datatot2!=datatot2) if not val.any()] paramsNN=np.zeros([20,4,np.shape(datatot3)[0]]) for qq in range(20): #[net, matNN] = deep.pretrain(bvalues, arg, SNR=15, net=run_net, state=qq, sims=100000) net = deep.learn_IVIM(datatot[res], bvalues, arg)# net=net) paramsNN[qq]=deep.infer_IVIM(datatot3, bvalues, net) #matNN_count[qq]=matNN del net # save NN results names = ['Dp_NN_{net}_rep'.format(net=run_net), 'D_NN_{net}_2'.format(net=run_net), 'f_NN_{net}_2'.format(net=run_net), 'S0_NN_{net}_2'.format(net=run_net)] multiple = [1000., 1000000., 10000., 1000.] fold = aa2[0] ss = 2 data=nib.load('{folder}/CR{fold:02d}/MRI{ss}_{dat}.nii.gz'.format(folder=fold1,fold=fold,ss=ss, dat=dattype)) datas=data.get_data() sx, sy, sz, n_b_values = datas.shape data.header.set_data_shape((sx,sy,sz,np.shape(paramsNN)[0])) X_dw_all = np.reshape(datas, (sx * sy * sz, n_b_values)) valid_id = np.sum(X_dw_all == 0, axis=1) == 0 imgtot=np.zeros([sx,sy,sz,np.shape(paramsNN)[0]]) for k in range(len(names)):