def test_wfi_dualR2s(): filename = '/Users/mnd/Projects/FatParameterEstimation/data/DiagnostikBilanz/20170609_125718_0402_ImDataParams.mat' imDataParams = h5io.load_ImDataParams_mat(filename) imDataParams = imDataParams['ImDataParams'] filename = '/Users/mnd/Projects/FatParameterEstimation/data/DiagnostikBilanz/20170609_125718_0402_WFIparams_CSS_GANDALF2D_VL.mat' wfiParams = h5io.load_WFIparams_mat(filename) fieldmap_Hz = np.transpose(wfiParams['WFIparams']['fieldmap_Hz']) fieldmap_Hz_equalized = wfi.equalize_fieldmap_periods(fieldmap_Hz, imDataParams['TE_s']) options = {'init_fieldmap_Hz': fieldmap_Hz_equalized, 'iSlice': slice(35, 37)} wfiParams = wfi.wfi_css_varpro(imDataParams, options) assert {'water', 'fat', 'fieldmap_Hz', 'R2s_Hz', 'pdff_percent'}.\ issubset(wfiParams.keys()) Cr = np.eye(len(wfiParams['Cr']), 2) Cr[1:, 1] = 1 options2 = wfiParams.copy() options2.pop('water') options2.pop('fat') options2.pop('pdff_percent') options2.pop('fieldmap_Hz') options2.pop('R2s_Hz') options2.pop('param_maps') options2.pop('resnorm') options2.pop('iterations') options2.update({'Cr': Cr}) wfiParams2 = wfi.wfi_css_varpro(imDataParams, options2) close_all() show_arr3d(np.clip(wfiParams['pdff_percent'], 0, 100)) show_arr3d(np.clip(wfiParams2['pdff_percent'], 0, 100)) close_all() show_arr3d(np.clip(wfiParams['R2s_Hz'], 0, 400)) show_arr3d(np.clip(wfiParams2['R2s_Hz'], 0, 400)) show_arr3d(np.clip(wfiParams2['param_maps'][6], 0, 400)) close_all() show_arr3d(np.clip(wfiParams['fieldmap_Hz'], -400, 200)) show_arr3d(np.clip(wfiParams2['fieldmap_Hz'], -400, 200)) close_all() show_arr3d(np.clip(wfiParams['resnorm'], 0, 1e2)) show_arr3d(np.clip(wfiParams2['resnorm'], 0, 1e2)) close_all() show_arr3d(np.clip(wfiParams['iterations'], 0, 1e2)) show_arr3d(np.clip(wfiParams2['iterations'], 0, 1e2))
def test_css_varpro(): filename = '/Users/mnd/Projects/FatParameterEstimation/data/DiagnostikBilanz/20170609_125718_0402_ImDataParams.mat' imDataParams = load_ImDataParams_mat(filename) imDataParams = imDataParams['ImDataParams'] iz = slice(35, 37) imDataParams['signal'] = imDataParams['signal'][:, :, iz, :] imDataParams['TE_s'] = imDataParams['TE_s'].ravel() tissue_mask = wfi.calculate_tissue_mask(imDataParams['signal']) filename = '/Users/mnd/Projects/FatParameterEstimation/data/DiagnostikBilanz/20170609_125718_0402_WFIparams_CSS_GANDALF2D_VL.mat' h5file = h5.File(filename, 'r') path = '/WFIparams' f = h5.File(filename, 'r') fieldmap_Hz = np.transpose(f['/WFIparams/fieldmap_Hz'][...])[:, :, iz] fieldmap_Hz_equalized = wfi.equalize_fieldmap_periods( fieldmap_Hz, imDataParams['TE_s']) fieldmap_Hz_equalized = tissue_mask * \ wfi.equalize_fieldmap_periods(fieldmap_Hz, imDataParams['TE_s']) F = Fatmodel() F.set_fatmodel('Hamilton VAT') F.set_constraints_matrices() Cm, Cp, Cf, Cr = F.constraints_matrices options = {} options['Cm'] = Cm options['Cp'] = Cp options['Cf'] = Cf options['Cr'] = Cr options['Pm0'] = wfi.build_Pm0(F.get_chemical_shifts_Hz(), fieldmap_Hz_equalized[tissue_mask]) options['mask'] = tissue_mask options['tol'] = 1e-5 options['itermax'] = 100 outParams = css.css_varpro(imDataParams, options) assert set(outParams.keys()) == { 'elapsed_time_s', 'iterations', 'param_maps', 'resnorm' } assert outParams['param_maps'][0].shape == tissue_mask.shape
def test_wfi_css_varpro_wuq(): filename = '/Users/mnd/Projects/FatParameterEstimation/data/FatParamEstimation/BMR/BMR106_EN/20171013_131406_1302_ImDataParams.mat' imDataParams = h5io.load_ImDataParams_mat(filename) imDataParams = imDataParams['ImDataParams'] filename = '/Users/mnd/Projects/FatParameterEstimation/data/FatParamEstimation/BMR/BMR106_EN/20171013_131406_1302_WFIparams_GANDALF3D_VL.mat' wfiParams = h5io.load_WFIparams_mat(filename) fieldmap_Hz = np.transpose(wfiParams['WFIparams']['fieldmap_Hz']) fieldmap_Hz_equalized = wfi.equalize_fieldmap_periods(fieldmap_Hz, imDataParams['TE_s']) options = {'init_fieldmap_Hz': fieldmap_Hz_equalized, 'iSlice': slice(35, 37), 'iTE': slice(2, 20), 'uncertainty_quant': True} wfiParams = wfi.wfi_css_varpro(imDataParams, options) show_arr3d(wfi.calculate_echoMIP(imDataParams['signal'])) print(wfiParams.keys()) crlb = wfiParams['CRLBs'][0] assert len(wfiParams['CRLBs']) == 6 close_all() show_arr3d(np.log(wfiParams['CRLBs'][0])) show_arr3d(np.log(wfiParams['CRLBs'][1])) show_arr3d(np.log(wfiParams['CRLBs'][2])) show_arr3d(np.log(wfiParams['CRLBs'][3])) show_arr3d(np.log(wfiParams['CRLBs'][4])) show_arr3d(np.log(wfiParams['CRLBs'][5])) close_all() show_arr3d(wfiParams['NSAs'][0]) show_arr3d(wfiParams['NSAs'][1]) show_arr3d(wfiParams['NSAs'][2]) show_arr3d(wfiParams['NSAs'][3]) show_arr3d(wfiParams['NSAs'][4]) show_arr3d(wfiParams['NSAs'][5]) trF = wfiParams['trFIM'] trInvF = wfiParams['trInvFIM'] detF = wfiParams['detFIM'] detInvF = np.divide(1, detF, out=-np.ones_like(detF), where=detF!=0) close_all() show_arr3d(np.log(trF)) show_arr3d(-np.log(trInvF)) show_arr3d(np.log(detF)) show_arr3d(-np.log(detInvF))
def test_dualR2s_wat(): filename = '/Users/mnd/Projects/FatParameterEstimation/data/FatParamEstimation/BMR/BMR106_EN/20171013_131406_1302_ImDataParams.mat' imDataParams = h5io.load_ImDataParams_mat(filename) imDataParams = imDataParams['ImDataParams'] filename = '/Users/mnd/Projects/FatParameterEstimation/data/FatParamEstimation/BMR/BMR106_EN/20171013_131406_1302_WFIparams_GANDALF3D_VL.mat' wfiParams = h5io.load_WFIparams_mat(filename) fieldmap_Hz = np.transpose(wfiParams['WFIparams']['fieldmap_Hz']) fieldmap_Hz_equalized = wfi.equalize_fieldmap_periods(fieldmap_Hz, imDataParams['TE_s']) options = {'init_fieldmap_Hz': fieldmap_Hz_equalized, 'iSlice': slice(35, 37)} wfiParams = wfi.wfi_css_varpro(imDataParams, options) Cr = np.eye(len(wfiParams['Cr']), 2) Cr[1:, 1] = 1 wfiParams2 = wfi.wfi_css_varpro(imDataParams, options2) close_all() show_arr3d(np.clip(wfiParams['pdff_percent'], 0, 100)) show_arr3d(np.clip(wfiParams2['pdff_percent'], 0, 100)) close_all() show_arr3d(np.clip(wfiParams['R2s_Hz'], 0, 400)) show_arr3d(np.clip(wfiParams2['R2s_Hz'], 0, 400)) show_arr3d(np.clip(wfiParams2['param_maps'][6], 0, 400)) close_all() show_arr3d(np.clip(wfiParams['fieldmap_Hz'], -400, 200)) show_arr3d(np.clip(wfiParams2['fieldmap_Hz'], -400, 200)) close_all() show_arr3d(np.clip(wfiParams['resnorm'], 0, 1e2)) show_arr3d(np.clip(wfiParams2['resnorm'], 0, 1e2)) close_all() show_arr3d(np.clip(wfiParams['iterations'], 0, 1e2)) show_arr3d(np.clip(wfiParams2['iterations'], 0, 1e2))
def test_wfi_css_varpro(): filename = '/Users/mnd/Projects/FatParameterEstimation/data/DiagnostikBilanz/20170609_125718_0402_ImDataParams.mat' imDataParams = h5io.load_ImDataParams_mat(filename) imDataParams = imDataParams['ImDataParams'] filename = '/Users/mnd/Projects/FatParameterEstimation/data/DiagnostikBilanz/20170609_125718_0402_WFIparams_CSS_GANDALF2D_VL.mat' wfiParams = h5io.load_WFIparams_mat(filename) fieldmap_Hz = np.transpose(wfiParams['WFIparams']['fieldmap_Hz']) fieldmap_Hz_equalized = wfi.equalize_fieldmap_periods(fieldmap_Hz, imDataParams['TE_s']) options = {'init_fieldmap_Hz': fieldmap_Hz_equalized, 'iSlice': slice(35, 37)} wfiParams = wfi.wfi_css_varpro(imDataParams, options) assert {'water', 'fat', 'fieldmap_Hz', 'R2s_Hz', 'pdff_percent'}.\ issubset(wfiParams.keys()) close_all() show_arr3d(np.clip(wfiParams['pdff_percent'], 0, 100))
Sig = ImDataParams['signal'][tissue_mask] print(Sig.shape) filename = '/Users/mnd/Projects/FatParameterEstimation/data/DiagnostikBilanz/20170609_125718_0402_WFIparams_CSS_GANDALF2D_VL.mat' h5file = h5.File(filename, 'r') path = '/WFIparams' f = h5.File(filename, 'r') pprint(list(f['/WFIparams'].keys())) TE_s = ImDataParams['TE_s'].ravel() fieldmap_Hz = np.transpose(f['/WFIparams/fieldmap_Hz'][...]) fieldmap_Hz = fieldmap_Hz[:, :, iz] fieldmap_Hz_equalized = tissue_mask * wfi.equalize_fieldmap_periods( fieldmap_Hz, TE_s) fieldmap_Hz_equalized = fieldmap_Hz_equalized[tissue_mask] F = Fatmodel(modelname='Hamilton VAT') F.set_constraints_matrices() Cm, Cp, Cf, Cr = F.constraints_matrices F.set_params_matrix() tol, itermax = 1e-3, 100 # nVoxel = Sig.shape[0] chemical_shifts_Hz = np.concatenate( ([0], F.centerfreq_Hz * 1e-6 * F.deshielding_ppm)) wfi.build_Pm0(chemical_shifts_Hz, fieldmap_Hz_equalized) Pm0 = np.zeros((nVoxel, len(chemical_shifts_Hz), 4))
def test_contrainedPhase(): filename = '/Users/mnd/Projects/FatParameterEstimation/data/DiagnostikBilanz/20170609_125718_0402_ImDataParams.mat' imDataParams = h5io.load_ImDataParams_mat(filename) imDataParams = imDataParams['ImDataParams'] filename = '/Users/mnd/Projects/FatParameterEstimation/data/DiagnostikBilanz/20170609_125718_0402_WFIparams_CSS_GANDALF2D_VL.mat' wfiParams = h5io.load_WFIparams_mat(filename) fieldmap_Hz = np.transpose(wfiParams['WFIparams']['fieldmap_Hz']) fieldmap_Hz_equalized = wfi.equalize_fieldmap_periods(fieldmap_Hz, imDataParams['TE_s']) options = {'init_fieldmap_Hz': fieldmap_Hz_equalized, 'iSlice': slice(35, 37)} wfiParams = wfi.wfi_css_varpro(imDataParams, options) assert {'water', 'fat', 'fieldmap_Hz', 'R2s_Hz', 'pdff_percent'}.\ issubset(wfiParams.keys()) options2 = options.copy() Cp = np.zeros((10, 1)) Cp[:, 0] = 1. options2['Cp'] = Cp wfiParams2 = wfi.wfi_css_varpro(imDataParams, options2) close_all() show_arr3d(np.clip(wfiParams['pdff_percent'], 0, 100)) show_arr3d(np.clip(wfiParams2['pdff_percent'], 0, 100)) show_arr3d(np.clip(wfiParams2['pdff_percent'], 0, 100) - \ np.clip(wfiParams['pdff_percent'], 0, 100)) close_all() show_arr3d(np.angle(wfiParams['water'])) show_arr3d(np.angle(wfiParams['fat'])) show_arr3d(np.angle(wfiParams2['water'])) show_arr3d(np.angle(wfiParams2['fat'])) assert ~np.allclose(np.angle(wfiParams['water']), np.angle(wfiParams['fat'])) assert np.allclose(np.angle(wfiParams2['water']), np.angle(wfiParams2['fat'])) close_all() show_arr3d(np.clip(wfiParams['R2s_Hz'], 0, 400)) show_arr3d(np.clip(wfiParams2['R2s_Hz'], 0, 400)) show_arr3d(np.clip(wfiParams2['R2s_Hz'], 0, 400) - \ np.clip(wfiParams['R2s_Hz'], 0, 400)) close_all() show_arr3d(np.clip(wfiParams['fieldmap_Hz'], -400, 200)) show_arr3d(np.clip(wfiParams2['fieldmap_Hz'], -400, 200)) show_arr3d(np.clip(wfiParams2['R2s_Hz'], -400, 200) - \ np.clip(wfiParams['R2s_Hz'], -400, 200)) close_all() show_arr3d(np.clip(wfiParams['resnorm'], 0, 1e2)) show_arr3d(np.clip(wfiParams2['resnorm'], 0, 1e2)) close_all() show_arr3d(wfiParams['iterations'], ) show_arr3d(wfiParams2['iterations'])
def test_wfi_css_varpro_wuq_dualR2s(): filename = '/Users/mnd/Projects/FatParameterEstimation/data/FatParamEstimation/BMR/BMR106_EN/20171013_131406_1302_ImDataParams.mat' imDataParams = h5io.load_ImDataParams_mat(filename) imDataParams = imDataParams['ImDataParams'] imDataParams['TE_s'] = imDataParams['TE_s'].ravel() filename = '/Users/mnd/Projects/FatParameterEstimation/data/FatParamEstimation/BMR/BMR106_EN/20171013_131406_1302_WFIparams_GANDALF3D_VL.mat' wfiParams = h5io.load_WFIparams_mat(filename) fieldmap_Hz = np.transpose(wfiParams['WFIparams']['fieldmap_Hz']) fieldmap_Hz_equalized = wfi.equalize_fieldmap_periods(fieldmap_Hz, imDataParams['TE_s']) options = {'init_fieldmap_Hz': fieldmap_Hz_equalized, 'iSlice': slice(35, 37), 'iTE': slice(4, 16), 'uncertainty_quant': True} wfiParams = wfi.wfi_css_varpro(imDataParams, options) options2 = options.copy() options2.update({'Cr': np.array([[1., 0.], [0., 1.], [0., 1.], [0., 1.], [0., 1.], [0., 1.], [0., 1.], [0., 1.], [0., 1.], [0., 1.], [0., 1.]])}) wfiParams2 = wfi.wfi_css_varpro(imDataParams, options2) FIMparams = css.compute_FIMmaps(imDataParams['TE_s'], wfiParams['params_matrices'], wfiParams['Cm'], wfiParams['Cp'], wfiParams['Cf'], wfiParams['Cr'], wfiParams['mask']) FIMparams2 = css.compute_FIMmaps(imDataParams['TE_s'], wfiParams['params_matrices'], wfiParams['Cm'], wfiParams['Cp'], wfiParams['Cf'], wfiParams2['Cr'], wfiParams['mask']) close_all() show_arr3d(np.log(FIMparams['CRLBs'][0])) show_arr3d(np.log(FIMparams2['CRLBs'][0])) close_all() show_arr3d(np.log(FIMparams['trFIM'])) show_arr3d(np.log(FIMparams2['trFIM'])) show_arr3d(np.log(FIMparams['trFIM']) - np.log(FIMparams2['trFIM'])) echoMIP = wfi.calculate_echoMIP(imDataParams['signal'][:, :, 35:37, 4:16]) diff_dict = {} for k in ['CRLBs', 'NSAs']: diff_dict[k] = np.array(wfiParams[k]) - np.array(wfiParams2[k][:-1]) for k in ['trFIM', 'trInvFIM', 'detFIM']: diff_dict[k] = np.array(wfiParams[k]) - np.array(wfiParams2[k]) show_arr3d(diff_dict['CRLBs'][0]) show_arr3d(diff_dict['CRLBs'][1]) show_arr3d(diff_dict['CRLBs'][2]) show_arr3d(diff_dict['CRLBs'][3]) show_arr3d(diff_dict['CRLBs'][4]) show_arr3d(diff_dict['CRLBs'][5]) show_arr3d(diff_dict['trFIM']) close_all() maptofuse = 'pdff_percent' # maptofuse = 'fieldmap_Hz' vmin, vmax = 0, 100 # vmin, vmax = -400, 200 wheretofuse = FIMparams['trFIM'] >= FIMparams2['trFIM'] # wheretofuse = FIMparams['detFIM'] >= FIMparams2['detFIM'] # wheretofuse = FIMparams['trInvFIM'] <= FIMparams2['trInvFIM'] # wheretofuse = FIMparams['detInvFIM'] <= FIMparams2['detInvFIM'] # wheretofuse = FIMparams['NSAs'][5] >= FIMparams2['NSAs'][5] frankenstein = np.where(wheretofuse, wfiParams[maptofuse], wfiParams2[maptofuse]) show_arr3d(wfiParams[maptofuse]) show_arr3d(wfiParams2[maptofuse]) show_arr3d(wheretofuse * echoMIP) show_arr3d(np.clip(frankenstein, vmin, vmax)) show_arr3d(np.clip(frankenstein, vmin, vmax) - np.clip(wfiParams[maptofuse], vmin, vmax)) show_arr3d(wfiParams['resnorm']) close_all() maptofuse = 'fat' vmin, vmax = 0, 200 wheretofuse = FIMparams['detFIM'] >= FIMparams2['detFIM'] frankenstein = np.where(wheretofuse, wfiParams[maptofuse], wfiParams2[maptofuse]) show_arr3d(wheretofuse * echoMIP) show_arr3d(np.clip(wfiParams[maptofuse], vmin, vmax)) show_arr3d(np.clip(frankenstein, vmin, vmax)) show_arr3d(np.clip(frankenstein, vmin, vmax) - np.clip(wfiParams[maptofuse], vmin, vmax)) close_all() maptofuse = 'R2s_Hz' maptofuse2 = 'waterR2s_Hz' vmin, vmax = 0, 200 wheretofuse = FIMparams['detFIM'] >= FIMparams2['detFIM'] frankenstein = np.where(wheretofuse, wfiParams[maptofuse], wfiParams2[maptofuse2]) show_arr3d(wheretofuse * echoMIP) show_arr3d(np.clip(wfiParams[maptofuse], vmin, vmax)) show_arr3d(np.clip(frankenstein, vmin, vmax)) show_arr3d(np.clip(frankenstein, vmin, vmax) - np.clip(wfiParams[maptofuse], vmin, vmax)) close_all() maptofuse = 'R2s_Hz' maptofuse2 = 'fatR2s_Hz' vmin, vmax = 0, 200 wheretofuse = FIMparams['detFIM'] >= FIMparams2['detFIM'] frankenstein = np.where(wheretofuse, wfiParams[maptofuse], wfiParams2[maptofuse2]) show_arr3d(wheretofuse * echoMIP) show_arr3d(np.clip(wfiParams[maptofuse], vmin, vmax)) show_arr3d(np.clip(frankenstein, vmin, vmax)) show_arr3d(np.clip(frankenstein, vmin, vmax) - np.clip(wfiParams[maptofuse], vmin, vmax)) close_all() show_arr3d(np.log(FIMparams['detFIM'])) show_arr3d(np.log(FIMparams2['detFIM'])) show_arr3d(np.log(FIMparams['detFIM']) - np.log(FIMparams2['detFIM'])) show_arr3d(np.log(FIMparams['trFIM']) - np.log(FIMparams2['trFIM'])) show_arr3d(np.log(FIMparams['trInvFIM']) - np.log(FIMparams2['trInvFIM'])) show_arr3d(FIMparams['NSAs'][0] - FIMparams2['NSAs'][0]) show_arr3d(FIMparams['NSAs'][1] - FIMparams2['NSAs'][1]) show_arr3d(FIMparams['NSAs'][4] - FIMparams2['NSAs'][4]) show_arr3d(wfiParams['fieldmap_Hz'] - wfiParams2['fieldmap_Hz'])
import matplotlib.pyplot as plt from utils.utils import * from matplotlib2tikz import save as tikzsave import deepdish as dd # filename = '/Users/mnd/Projects/FatParameterEstimation/data/FatParamEstimation/DiagnostikBilanz/20170609_125718_0402_ImDataParams.mat' # imDataParams = h5io.load_ImDataParams_mat(filename) # imDataParams = imDataParams['ImDataParams'] # dd.io.save('imDataParams.h5', imDataParams) imDataParams = dd.io.load('imDataParams.h5') echoMIP = wfi.calculate_echoMIP(imDataParams['signal']) filename = '/Users/mnd/Projects/FatParameterEstimation/data/FatParamEstimation/DiagnostikBilanz/20170609_125718_0402_WFIparams_CSS_GANDALF2D_VL.mat' wfiParams = h5io.load_WFIparams_mat(filename) fieldmap_Hz = np.transpose(wfiParams['WFIparams']['fieldmap_Hz']) fieldmap_Hz_equalized = wfi.equalize_fieldmap_periods(fieldmap_Hz, imDataParams['TE_s']) # single R2* options = { 'init_fieldmap_Hz': fieldmap_Hz_equalized, 'iSlice': slice(35, 36), 'uncertainty_quant': True } wfiParams = wfi.wfi_css_varpro(imDataParams, options) dd.io.save('wfiParams.h5', wfiParams) # wfiParams = dd.io.load('wfiParams.h5') # show_arr3d(np.clip(wfiParams['pdff_percent'], 0, 100)) # double R2* Cr = np.eye(len(wfiParams['Cr']), 2)