def test_neuro_module_branch(self): nmb = NeuroBranch('best_branch_ever') nmb += PipelineElement('SmoothImages', fwhm=10) nmb += PipelineElement('ResampleImages', voxel_size=5) nmb += PipelineElement('BrainAtlas', rois=['Hippocampus_L', 'Hippocampus_R'], atlas_name="AAL", extract_mode='vec') nmb.base_element.cache_folder = self.cache_folder_path CacheManager.clear_cache_files(nmb.base_element.cache_folder, True) # set the config so that caching works nmb.set_params(**{'SmoothImages__fwhm': 10, 'ResampleImages__voxel_size': 5}) # okay we are transforming 8 Niftis with 3 elements, so afterwards there should be 3*8 nr_niftis = 7 nmb.transform(self.X[:nr_niftis]) nr_files_in_folder = len(glob.glob(os.path.join(nmb.base_element.cache_folder, "*.p"))) self.assertTrue(nr_files_in_folder == 3 * nr_niftis) self.assertTrue(len(nmb.base_element.cache_man.cache_index.items()) == (3*nr_niftis)) # transform 3 items that should have been cached and two more that need new processing nmb.transform(self.X[nr_niftis-2::]) # now we should have 10 * 3 nr_files_in_folder = len(glob.glob(os.path.join(nmb.base_element.cache_folder, "*.p"))) self.assertTrue(nr_files_in_folder == (3 * len(self.X))) self.assertTrue(len(nmb.base_element.cache_man.cache_index.items()) == (3 * len(self.X)))
def test_single_subject_smoothing(self): # nilearn from nilearn.image import smooth_img nilearn_smoothed_img = smooth_img(self.X[0], fwhm=[3, 3, 3]) nilearn_smoothed_array = nilearn_smoothed_img.dataobj # photon smoother = PipelineElement( "SmoothImages", hyperparameters={}, fwhm=3, batch_size=1 ) photon_smoothed_array, _, _ = smoother.transform(self.X[0]) branch = NeuroBranch("NeuroBranch", output_img=True) branch += smoother photon_smoothed_img, _, _ = branch.transform(self.X[0]) # assert self.assertIsInstance(photon_smoothed_array, np.ndarray) self.assertIsInstance(photon_smoothed_img, Nifti1Image) self.assertTrue(np.array_equal(photon_smoothed_array, nilearn_smoothed_array)) self.assertTrue( np.array_equal(photon_smoothed_img.dataobj, nilearn_smoothed_img.dataobj) )
def test_multi_subject_resampling(self): voxel_size = [3, 3, 3] # nilearn from nilearn.image import resample_img, index_img nilearn_resampled = resample_img( self.X[:3], interpolation="nearest", target_affine=np.diag(voxel_size) ) nilearn_resampled_img = [ index_img(nilearn_resampled, i) for i in range(nilearn_resampled.shape[-1]) ] nilearn_resampled_array = np.moveaxis(nilearn_resampled.dataobj, -1, 0) # photon resampler = PipelineElement( "ResampleImages", hyperparameters={}, voxel_size=voxel_size ) resampled_img, _, _ = resampler.transform(self.X[:3]) branch = NeuroBranch("NeuroBranch", output_img=True) branch += resampler branch_resampled_img, _, _ = branch.transform(self.X[:3]) # assert self.assertIsInstance(resampled_img, np.ndarray) self.assertIsInstance(branch_resampled_img, list) self.assertIsInstance(branch_resampled_img[0], Nifti1Image) self.assertTrue(np.array_equal(nilearn_resampled_array, resampled_img)) self.assertTrue( np.array_equal( branch_resampled_img[1].dataobj, nilearn_resampled_img[1].dataobj ) )
def test_single_subject_resampling(self): voxel_size = [3, 3, 3] # nilearn from nilearn.image import resample_img nilearn_resampled_img = resample_img( self.X[0], interpolation="nearest", target_affine=np.diag(voxel_size) ) nilearn_resampled_array = nilearn_resampled_img.dataobj # photon resampler = PipelineElement( "ResampleImages", hyperparameters={}, voxel_size=voxel_size, batch_size=1 ) single_resampled_img, _, _ = resampler.transform(self.X[0]) branch = NeuroBranch("NeuroBranch", output_img=True) branch += resampler branch_resampled_img, _, _ = branch.transform(self.X[0]) # assert self.assertIsInstance(single_resampled_img, np.ndarray) self.assertIsInstance(branch_resampled_img[0], Nifti1Image) self.assertTrue(np.array_equal(nilearn_resampled_array, single_resampled_img)) self.assertTrue( np.array_equal(single_resampled_img, branch_resampled_img[0].dataobj) )
def create_instances_and_transform(neuro_class_str, param_dict, transformed_X): for i in range(1, 4): if i == 1 or i == 3: obj = NeuroBranch(name="single core application", nr_of_processes=1) else: obj = NeuroBranch(name="multi core application", nr_of_processes=3) if i < 3: obj += PipelineElement(neuro_class_str, **param_dict) if i >= 3: obj += PipelineElement(neuro_class_str, batch_size=5, **param_dict) # transform data obj.base_element.cache_folder = self.cache_folder_path obj.base_element.current_config = {"test_suite": 1} new_X, _, _ = obj.transform(self.X) obj.base_element.clear_cache() # compare output to nilearn version for index, nilearn_nifti in enumerate(transformed_X): photon_nifti = new_X[index] if isinstance(photon_nifti, Nifti1Image): self.assertTrue( np.array_equal(photon_nifti.dataobj, nilearn_nifti.dataobj) ) else: self.assertTrue( np.array_equal( np.asarray(photon_nifti), nilearn_nifti.dataobj ) ) print("finished testing object: all images are fine.")
def test_multi_subject_smoothing(self): # nilearn from nilearn.image import smooth_img nilearn_smoothed_img = smooth_img(self.X[0:3], fwhm=[3, 3, 3]) nilearn_smoothed_array = nilearn_smoothed_img[1].dataobj # photon smoother = PipelineElement('SmoothImages', hyperparameters={}, fwhm=3) photon_smoothed_array, _, _ = smoother.transform(self.X[0:3]) branch = NeuroBranch('NeuroBranch', output_img=True) branch += smoother photon_smoothed_img, _, _ = branch.transform(self.X[0:3]) # assert self.assertIsInstance(photon_smoothed_array, np.ndarray) self.assertIsInstance(photon_smoothed_img[0], Nifti1Image) self.assertTrue(np.array_equal(photon_smoothed_array[1], nilearn_smoothed_array)) self.assertTrue(np.array_equal(photon_smoothed_img[1].dataobj, nilearn_smoothed_img[1].dataobj))