def test_load_nifti_multiple_echoes(self, monkeypatch): """ Assert passes with correct data for multiple echoes :return: """ if self.tmp_path.exists(): shutil.rmtree(self.data_path_volume) monkeypatch.setattr('sys.stdin', StringIO('0\n')) niftis, info, json_info = load_nifti(self.tmp_path) assert (len(info) == 2), "Wrong number od info data 1" assert (len(json_info) == 2), "Wrong number of JSON data 1" self._json_phase['EchoNumber'] = 1 assert (json.dumps(json_info[0], sort_keys=True) == json.dumps(self._json_phase, sort_keys=True)), \ "JSON file is not correctly loaded for first JSON1" self._json_phase['EchoNumber'] = 2 assert (json.dumps(json_info[1], sort_keys=True) == json.dumps(self._json_phase, sort_keys=True)), \ "JSON file is not correctly loaded for second JSON 1" assert (niftis.shape == (3, 3, 3, 2, 1)), "Wrong shape for the Nifti output data 1" monkeypatch.setattr('sys.stdin', StringIO('1\n')) niftis, info, json_info = load_nifti(self.tmp_path) assert (len(info) == 2), "Wrong number of info data 2" assert (len(json_info) == 2), "Wrong number of JSON data 2" self._json_phase['EchoNumber'] = 1 assert (json.dumps(json_info[0], sort_keys=True) == json.dumps(self._json_phase, sort_keys=True)), \ "JSON file is not correctly loaded for first JSON 2" self._json_phase['EchoNumber'] = 2 assert (json.dumps(json_info[1], sort_keys=True) == json.dumps(self._json_phase, sort_keys=True)), \ "JSON file is not correctly loaded for second JSON 2" assert (niftis.shape == (3, 3, 3, 2, 1)), "Wrong shape for the Nifti output data 2"
def test_load_nifti_modality_check(self, monkeypatch): """ Assert passes with correct data for multiple echoes :return: """ if self.data_path_2.exists(): shutil.rmtree(self.data_path_2) if self.data_path_volume.exists(): shutil.rmtree(self.data_path_volume) os.remove(os.path.join(self.data_path, "dummy2.nii")) os.remove(os.path.join(self.data_path, "dummy2.json")) dummy_data = nib.nifti1.Nifti1Image(dataobj=self._data, affine=self._aff) nib.save(dummy_data, os.path.join(self.data_path, 'dummy2.nii')) with open(os.path.join(self.data_path, 'dummy2.json'), 'w') as json_file: json.dump(self._json_mag, json_file) niftis, info, json_info = load_nifti(self.data_path) assert (len(info) == 1), "Wrong number od info data 1" assert (len(json_info) == 1), "Wrong number of JSON data 1" assert (json.dumps(json_info[0], sort_keys=True) == json.dumps(self._json_phase, sort_keys=True)), \ "JSON file is not correctly loaded for first JSON1" assert (niftis.shape == (3, 3, 3, 1, 1)), "Wrong shape for the Nifti output data 1" niftis, info, json_info = load_nifti(self.data_path, "magnitude") assert (len(info) == 1), "Wrong number of info data 2" assert (len(json_info) == 1), "Wrong number of JSON data 2" assert (json.dumps(json_info[0], sort_keys=True) == json.dumps(self._json_mag, sort_keys=True)), \ "JSON file is not correctly loaded for first JSON 2" assert (niftis.shape == (3, 3, 3, 1, 1)), "Wrong shape for the Nifti output data 2"
def test_load_nifti_no_folder_fail(self): """ Assert fails without existing path :return: """ with pytest.raises(RuntimeError, match="Not an existing NIFTI path"): load_nifti("dummy")
def test_load_nifti_json_missing_fail(self): """ Assert fails if json missing :return: """ os.remove(os.path.join(self.data_path, "dummy.json")) with pytest.raises(OSError, match="Missing json file"): load_nifti(self.data_path)
def test_load_nifti_mix_file_types_fail(self): """ Assert fails if folder and files in path :return: """ with pytest.raises(RuntimeError, match="Directories and files in input path"): load_nifti(self.toolbox_path)
def test_load_nifti_mix_file_types_fail(self): """ Assert fails if folder and files in path :return: """ try: load_nifti(self.toolbox_path) except: return 0 assert False, "Did not fail with folder and files in the same path"
def test_load_nifti_no_folder_fail(self): """ Assert fails without existing path :return: """ try: load_nifti("dummy") except RuntimeError: return 0 assert False, "Did not fail if no valid path given"
def test_load_nifti_json_missing_fail(self): """ Assert fails if json missing :return: """ os.remove(os.path.join(self.data_path, "dummy.json")) try: load_nifti(self.data_path) except ValueError: return 0 assert (False), "Did not fail with missing JSON file"
def test_load_nifti_quit(self, monkeypatch): """ Assert q quits loading with return 0 :return: """ monkeypatch.setattr('sys.stdin', StringIO('q\n')) ret = load_nifti(self.tmp_path) assert (ret == 0), "Should have returned 0 for quit input"
def test_load_nifti_multiple_run(self, monkeypatch): """ Assert data is correctly separated between runs :return: """ if self.data_path_2.exists(): shutil.rmtree(self.data_path_2) if self.data_path_volume.exists(): shutil.rmtree(self.data_path_volume) os.remove(os.path.join(self.data_path, "dummy2.nii")) os.remove(os.path.join(self.data_path, "dummy2.json")) dummy_data = nib.nifti1.Nifti1Image(dataobj=self._data, affine=self._aff) nib.save(dummy_data, os.path.join(self.data_path, 'dummy2.nii')) with open(os.path.join(self.data_path, 'dummy2.json'), 'w') as json_file: self._json_phase['AcquisitionNumber'] = 2 json.dump(self._json_phase, json_file) monkeypatch.setattr('sys.stdin', StringIO('1\n')) niftis, info, json_info = load_nifti(self.data_path) self._json_phase['AcquisitionNumber'] = 1 assert (len(info) == 1), "Wrong number od info data" assert (len(json_info) == 1), "Wrong number of JSON data" assert (json.dumps(json_info[0], sort_keys=True) == json.dumps( self._json_phase, sort_keys=True)), "JSON file is not correctly loaded" assert (niftis.shape == (3, 3, 3, 1, 1)), "Wrong shape for the Nifti output data" monkeypatch.setattr('sys.stdin', StringIO('2\n')) niftis, info, json_info = load_nifti(self.data_path) self._json_phase['AcquisitionNumber'] = 2 assert (len(info) == 1), "Wrong number od info data" assert (len(json_info) == 1), "Wrong number of JSON data" assert (json.dumps(json_info[0], sort_keys=True) == json.dumps( self._json_phase, sort_keys=True)), "JSON file is not correctly loaded" assert (niftis.shape == (3, 3, 3, 1, 1)), "Wrong shape for the Nifti output data" self._json_phase['AcquisitionNumber'] = 1
def test_load_nifti_volume(self): """ Assert data containing volume is correctly parsed :return: """ if self.data_path_2.exists(): shutil.rmtree(self.data_path_2) if self.data_path.exists(): shutil.rmtree(self.data_path) niftis, info, json_info = load_nifti(self.data_path_volume) assert (len(info) == 1), "Wrong number of info data" assert (len(json_info) == 1), "Wrong number of JSON data" assert (json.dumps(json_info[0], sort_keys=True) == json.dumps(self._json_phase, sort_keys=True)),\ "JSON file is not correctly loaded" assert (niftis.shape == (3, 3, 3, 1, 2)), "Wrong shape for the Nifti output data"
def test_load_nifti_folders(self, monkeypatch): """ Assert that pass if path contains only folders :return: """ if self.data_path_2.exists(): shutil.rmtree(self.data_path_2) os.remove(os.path.join(self.data_path, "dummy2.nii")) os.remove(os.path.join(self.data_path, "dummy2.json")) monkeypatch.setattr('sys.stdin', StringIO('0\n')) niftis, info, json_info = load_nifti(self.tmp_path) assert (len(info) == 1), "Wrong number od info data" assert (len(json_info) == 1), "Wrong number of JSON data" assert (json.dumps(json_info[0], sort_keys=True) == json.dumps(self._json_phase, sort_keys=True)),\ "JSON file is not correctly loaded" assert (niftis.shape == (3, 3, 3, 1, 1)), "Wrong shape for the Nifti output data"
def test_load_nifti_files(self): """ Assert that pass if path contains only files :return: """ if self.data_path_2.exists(): shutil.rmtree(self.data_path_2) if self.data_path_volume.exists(): shutil.rmtree(self.data_path_volume) os.remove(os.path.join(self.data_path, "dummy2.nii")) os.remove(os.path.join(self.data_path, "dummy2.json")) niftis, info, json_info = load_nifti(self.data_path) assert (len(info) == 1), "Wrong number of info data" assert (len(json_info) == 1), "Wrong number of JSON data" assert (json.dumps(json_info[0], sort_keys=True) == json.dumps(self._json_phase, sort_keys=True)),\ "JSON file is not correctly loaded" assert (niftis.shape == (3, 3, 3, 1, 1)), "Wrong shape for the Nifti output data"