def test_masking_biascorrection(): """Test the masking function using bias correction.""" with tempfile.TemporaryDirectory() as test_dir: test_in_file = np.ones((63, 96, 48)) test_in_file = nib.Nifti1Image(test_in_file, np.eye(4)) test_in_file_path = Path(test_dir) / 'test_in_file.nii.gz' nib.save(test_in_file, test_in_file_path) # for input_type in ['anat', 'func']: for input_type in ['anat']: config = { "masking_config": { F"masking_config_{input_type}": { "bias_field_correction": { "shrink_factor": 2 }, "testing": True } } } config_path = Path(test_dir) / 'test_config.json' with open(config_path, 'w') as jsonfile: json.dump(config, jsonfile, indent=4) nii_path_masked, [resampled_mask_path ], resampled_mask_path = predict_mask( in_file=test_in_file_path, input_type=input_type, masking_config_path=config_path) assert Path(nii_path_masked).exists( ), f'nii_path_masked "{nii_path_masked}" does not exist, but should exist.' assert Path( resampled_mask_path).exists(), f'resampled_mask_path "{resampled_mask_path}" does not exist, ' \ f'but should exist.'
def tester(data_dir, studies, save_dir, model_path, data_type='anat'): if not os.path.exists(save_dir): os.makedirs(save_dir) visualisation = { 'bool': True, 'path': save_dir, } if data_type == 'anat': data_paths = data_loader.load_bidsdata(data_dir, studies=studies) for path in data_paths: print(path) masked_path = \ predict_mask(path, visualisation_bool=visualisation['bool'], visualisation_path=visualisation['path'], bias_correct_bool=True, anat_model_path=model_path)[0] command = 'mv {a} {b}'.format( a=masked_path, b=os.path.join(save_dir, 'masked_' + os.path.basename(path))) print(command) os.system(command) if data_type == 'func': data_paths = data_loader.load_bidsdata(data_dir, studies=['irsabi_bidsdata'], input_type='func') if not os.path.exists(save_dir): os.makedirs(save_dir) for path in data_paths: print(path) masked_path = predict_mask( path, input_type='func', visualisation_bool=visualisation['bool'], visualisation_path=visualisation['path'], bias_correct_bool=True, func_model_path=model_path)[0] command = 'mv {a} {b}'.format( a=masked_path, b=os.path.join(save_dir, 'masked_' + os.path.basename(path))) print(command) os.system(command) command = 'rm *.nii.gz' os.system(command)
def test_masker(): test_dir = os.path.join(os.path.dirname(mlebe.__file__), 'masking/tests/temp/') os.makedirs(test_dir, exist_ok=True) test_config = json_to_dict('configs/test_config.json') with open(os.path.join(test_dir, 'test_config.json'), 'w') as jsonfile: json.dump(test_config, jsonfile, indent=4) test_in_file = np.ones((63, 96, 48)) test_in_file = nib.Nifti1Image(test_in_file, np.eye(4)) test_in_file_dir = os.path.join(test_dir, 'test_in_file.nii.gz') nib.save(test_in_file, test_in_file_dir) _, _, _ = predict_mask(test_in_file_dir, os.path.join(test_dir, 'test_config.json')) shutil.rmtree(test_dir)
def tester(data_dir, studies, save_dir, data_type='anat', visualisation_format='pdf', bias_correct=True, with_bids_cropping=False, model_config_path=''): config_path = 'visualisation/vis_config.json' config = json_file_to_pyobj(config_path) if not os.path.exists(save_dir): os.makedirs(save_dir) if data_type == 'anat': write_to_jsonfile( config_path, [('masking_config.masking_config_anat.model_config_path', model_config_path), ('masking_config.masking_config_anat.bias_correct_bool', bias_correct), ('masking_config.masking_config_anat.visualisation_format', visualisation_format), ('masking_config.masking_config_anat.visualisation_bool', True), ('masking_config.masking_config_anat.visualisation_path', save_dir), ('masking_config.masking_config_anat.with_bids_cropping', with_bids_cropping)]) data_paths = data_loader.load_bidsdata(data_dir, studies=studies) for path in tqdm(data_paths): print(path) masked_path = \ predict_mask(path, config_path)[ 0] command = 'mv {a} {b}'.format( a=masked_path, b=os.path.join(save_dir, 'masked_' + os.path.basename(path))) print(command) os.system(command) if data_type == 'func': write_to_jsonfile( config_path, [('masking_config.masking_config_func.model_config_path', model_config_path), ('masking_config.masking_config_func.bias_correct_bool', bias_correct), ('masking_config.masking_config_func.visualisation_format', visualisation_format), ('masking_config.masking_config_func.visualisation_bool', True), ('masking_config.masking_config_func.visualisation_path', save_dir), ('masking_config.masking_config_func.with_bids_cropping', with_bids_cropping)]) data_paths = data_loader.load_bidsdata(data_dir, studies=['irsabi_bidsdata'], input_type='func') if not os.path.exists(save_dir): os.makedirs(save_dir) for path in tqdm(data_paths): print(path) masked_path = predict_mask(path, config_path, input_type='func')[0] command = 'mv {a} {b}'.format( a=masked_path, b=os.path.join(save_dir, 'masked_' + os.path.basename(path))) print(command) os.system(command) command = 'rm *.nii.gz' os.system(command)