def func(subject_info, output_path): subject_id = subject_info.subject_id for model_name in model_names: print(subject_id, model_name) starting_point = mdt.fit_model(model_name, subject_info.get_input_data(), output_path + '/' + subject_id) with mdt.config_context(''' processing_strategies: sampling: max_nmr_voxels: 5000 '''): mdt.sample_model(model_name, subject_info.get_input_data(), output_path + '/' + subject_info.subject_id, nmr_samples=nmr_samples[model_name], initialization_data={'inits': starting_point}, store_samples=False) wm_mask = generate_simple_wm_mask(os.path.join(output_path, subject_id, 'Tensor', 'Tensor.FA.nii.gz'), subject_info.get_input_data().mask, threshold=0.3, median_radius=3, nmr_filter_passes=4) mdt.write_nifti(wm_mask, os.path.join(output_path, subject_id, 'wm_mask'))
def run(self, args, extra_args): mask_name = os.path.splitext( os.path.basename(os.path.realpath(args.mask)))[0] mask_name = mask_name.replace('.nii', '') output_folder = args.output_folder or os.path.join( os.path.dirname(args.dwi), 'output', mask_name) tmp_results_dir = args.tmp_results_dir for match, to_set in [('true', True), ('false', False), ('none', None)]: if tmp_results_dir.lower() == match: tmp_results_dir = to_set break noise_std = args.noise_std if noise_std is not None: if not os.path.isfile(os.path.realpath(noise_std)): noise_std = float(noise_std) def fit_model(): input_data = mdt.lib.input_data.load_input_data( os.path.realpath(args.dwi), os.path.realpath(args.protocol), os.path.realpath(args.mask), gradient_deviations=args.gradient_deviations, noise_std=noise_std, extra_protocol=get_extra_protocol(args.extra_protocol, os.path.realpath(''))) optimizer_options = {} if args.patience is not None: optimizer_options['patience'] = args.patience mdt.fit_model(args.model, input_data, output_folder, method=args.method, optimizer_options=optimizer_options, recalculate=args.recalculate, cl_device_ind=args.cl_device_ind, double_precision=args.double_precision, tmp_results_dir=tmp_results_dir, use_cascaded_inits=args.use_cascaded_inits) if args.config_context: with mdt.config_context(args.config_context): fit_model() else: fit_model()
def run(self, args, extra_args): mask_name = os.path.splitext( os.path.basename(os.path.realpath(args.mask)))[0] mask_name = mask_name.replace('.nii', '') output_folder = args.output_folder or os.path.join( os.path.dirname(args.dwi), 'output', mask_name) tmp_results_dir = args.tmp_results_dir for match, to_set in [('true', True), ('false', False), ('none', None)]: if tmp_results_dir.lower() == match: tmp_results_dir = to_set break noise_std = args.noise_std if noise_std is not None: if not os.path.isfile(os.path.realpath(noise_std)): noise_std = float(noise_std) def fit_model(): mdt.fit_model(args.model, mdt.load_input_data( os.path.realpath(args.dwi), os.path.realpath(args.protocol), os.path.realpath(args.mask), gradient_deviations=args.gradient_deviations, noise_std=noise_std, protocol_maps=get_protocol_maps( args.protocol_maps, os.path.realpath(''))), output_folder, recalculate=args.recalculate, only_recalculate_last=args.only_recalculate_last, cl_device_ind=args.cl_device_ind, double_precision=args.double_precision, cascade_subdir=args.cascade_subdir, tmp_results_dir=tmp_results_dir, save_user_script_info=None) if args.config_context: with mdt.config_context(args.config_context): fit_model() else: fit_model()
def CHARMED(dwi, mask): import mdt import os import nibabel as nib from mdt.configuration import SetGeneralOptimizer protocol = '/media/amr/HDD/Work/October_Acquistion/MDT_multishell_protocol.prtcl' model = 'CHARMED_r2 (Cascade|fixed)' algorithm = 'Levenberg-Marquardt' patience = 100 output_folder = os.getcwd() input_data = mdt.load_input_data(dwi, protocol, mask, noise_std=5, gradient_deviations=None, extra_protocol={}) with mdt.config_context( SetGeneralOptimizer(algorithm, settings={'patience': patience})): mdt.fit_model(model, input_data, output_folder, recalculate=True, only_recalculate_last=True, double_precision=False, cl_device_ind=[0]) os.chdir('CHARMED_r2') CHARMED_FA = os.path.abspath('Tensor.FA.nii.gz') CHARMED_MD = os.path.abspath('Tensor.MD.nii.gz') CHARMED_AD = os.path.abspath('Tensor.AD.nii.gz') CHARMED_RD = os.path.abspath('Tensor.RD.nii.gz') CHARMED_FR = os.path.abspath('FR.nii.gz') #I assumed that CHARMEDRestricted0.d to be the intra-axonal diffusvity CHARMED_IAD = os.path.abspath('CHARMEDRestricted0.d.nii.gz') return CHARMED_FA, CHARMED_MD, CHARMED_AD, CHARMED_RD, CHARMED_FR, CHARMED_IAD
def mcmc_sample(subject_info): output_folder = subject_info.data_folder[:-1] + '_output' subject_id = subject_info.subject_id for model_name in model_names: print(subject_id, model_name) starting_point = mdt.fit_model(model_name, subject_info.get_input_data(), output_folder + '/' + subject_id) with mdt.config_context(''' processing_strategies: sampling: max_nmr_voxels: 5000 '''): mdt.sample_model(model_name, subject_info.get_input_data(), output_folder + '/' + subject_id, nmr_samples=nmr_samples[model_name], initialization_data={'inits': starting_point}, store_samples=False)
snr) noise_std = simulations_unweighted_signal_height / snr current_pjoin = pjoin.create_extended(protocol_name, model_name) input_data = mdt.load_input_data( current_pjoin('noisy_signals_{}.nii'.format(snr)), pjoin(protocol_name + '.prtcl'), current_pjoin('mask.nii'), noise_std=noise_std) fit_results = mdt.fit_model( model_name + ' (Cascade)', input_data, current_pjoin('output', str(snr), str(trial_ind))) with mdt.config_context(''' processing_strategies: sampling: max_nmr_voxels: 5000 '''): mdt.sample_model( model_name, input_data, current_pjoin('output', str(snr), str(trial_ind)), nmr_samples=nmr_samples[model_name], burnin=1000, thinning=0, initialization_data={'inits': fit_results}, store_samples=False)