def test(self): ref_anat = os.path.join(self._tmp_dir, 'ref_anat.nii.gz') generate_cross_image((10, 10, 10), (1.0, 1.0, 1.0), ref_anat) output_path = os.path.join(self._tmp_dir, 'outpout.trk') tracts_path = _generate_streamlines(self._tmp_dir, (0.0, 0.0, 0.0)) self.call(main, tracts_path, ref_anat, output_path, '--gnc', '--fnc') created = nib.streamlines.load(output_path).streamlines.data self.assertEqual(8 * 5, len(created), 'Wrong number of points') tracts_path = _generate_streamlines(self._tmp_dir, (-0.5, -0.5, -0.5)) self.call(main, tracts_path, ref_anat, output_path, '-f', '--gnc', '--fnc') created = nib.streamlines.load(output_path).streamlines.data self.assertEqual(8 * 5, len(created), 'Wrong number of points') tracts_path = _generate_streamlines(self._tmp_dir, (0.0, 0.0, 0.0)) self.call(main, tracts_path, ref_anat, output_path, '-f', '--gnnc', '--nfnc') created = nib.streamlines.load(output_path).streamlines.data self.assertEqual(8 * 5, len(created), 'Wrong number of points') tracts_path = _generate_streamlines(self._tmp_dir, (-0.5, -0.5, -0.5)) self.call(main, tracts_path, ref_anat, output_path, '-f', '--gnnc', '--nfnc') created = nib.streamlines.load(output_path).streamlines.data self.assertEqual(5 * 4, len(created), 'Wrong number of points')
def test(self): image_path = os.path.join(self._tmp_dir, 'input_image.nii.gz') generate_cross_image((10, 10, 10), (1.0, 1.0, 1.0), image_path) with RedirectStdOut() as output: self.call(main, image_path) self.assertTrue(len(output) == 1, 'Should have 1 line') self.assertTrue( int(output[0]) == 22, 'There should be 8 + 7 + 7 non-zero voxels') output_path = os.path.join(self._tmp_dir, 'stats.txt') self.call(main, image_path, '-o' + output_path) with open(output_path, 'r') as f: lines = f.readlines() self.assertTrue(len(lines) == 1, 'Should have 1 line') self.assertTrue( int(lines[0]) == 22, 'There should be 8 + 7 + 7 non-zero voxels') self.call(main, image_path, '-o' + output_path, '--stats') with open(output_path, 'r') as f: lines = f.readlines() self.assertTrue(len(lines) == 2, 'Should have 2 lines') self.assertTrue( int(lines[1]) == 22, 'There should be 8 + 7 + 7 non-zero voxels') self.call(main, image_path, '-o' + output_path, '--stats', id=42) with open(output_path, 'r') as f: lines = f.readlines() self.assertTrue(len(lines) == 3, 'Should have 3 lines') self.assertTrue(lines[2] == "42 22", 'Third lihe should == "42 22"')
def test(self): image_path = os.path.join(self._tmp_dir, 'input_image.nii.gz') output_path = os.path.join(self._tmp_dir, 'outpout_image.nii.gz') bbox_path = os.path.join(self._tmp_dir, 'bbox.pkl') generate_cross_image((10, 10, 10), (0.5, 0.5, 0.5), image_path) # Crop without bbox should create the bbox file self.call(main, image_path, output_path, output_bbox=bbox_path) self.assertTrue( os.path.exists(bbox_path), 'The bbox file should exist.') with open(bbox_path, 'r') as bbox_file: wbbox = pickle.load(bbox_file) if list(wbbox.minimums) != [0.5, 0.5, 0.5]\ or list(wbbox.maximums) != [4.5, 4.5, 4.5]\ or wbbox.voxel_size != (0.5, 0.5, 0.5): self.fail('Bad values in bbox') created = nib.load(output_path) self.assertEqual((8, 8, 8), created.get_data().shape, 'Bad shape for cropped image.') # Crop with a modified bbox wbbox.maximums = np.array([4.0, 4.0, 4.0]) with open(bbox_path, 'w') as bbox_file: pickle.dump(wbbox, bbox_file) self.call(main, image_path, output_path, '-f', input_bbox=bbox_path) created = nib.load(output_path) self.assertEqual((7, 7, 7), created.get_data().shape, 'Bad shape for cropped image.')
def test(self): image_path = os.path.join(self._tmp_dir, 'image.nii.gz') mask_path = os.path.join(self._tmp_dir, 'mask.nii.gz') output_path = os.path.join(self._tmp_dir, 'histogram.png') s = 5 shape = (s, s, s) spacing = (1.0, 1.0, 1.0) generate_cross_image(shape, spacing, image_path) generate_half_mask(shape, spacing, mask_path) # 5*5*2 (50) voxels in mask. 49==0 and 1==1 in this ROI self.call(main, image_path, mask_path, output_path, label='test') self.compare_images(output_path, self.fetch('GT', 'plot_histogram.png'))
def test(self): spacing = (1.0, 1.0, 1.0) tracts_path = generate_streamlines(self._tmp_dir, spacing) image_path = os.path.join(self._tmp_dir, 'input.nii.gz') output_path = os.path.join(self._tmp_dir, 'output.nii.gz') generate_cross_image((10, 10, 10), spacing, image_path) self.call(main, tracts_path, image_path, output_path, tp='trackvis') self.assertTrue(_check(nib.load(output_path).get_data(), 1), 'Wrong voxels values') self.call(main, tracts_path, image_path, output_path, '-f', tp='trackvis', binary=42) self.assertTrue(_check(nib.load(output_path).get_data(), 42), 'Wrong voxels values')
def test(self): def check(img, expected_spacing, expected_shape): self.assertEqual(list(expected_spacing), list(img.header['pixdim'][1:4]), 'Bad spacing') self.assertEqual(expected_shape, img.shape, 'Bad shape') input_path = os.path.join(self._tmp_dir, 'input_image.nii.gz') output_path = os.path.join(self._tmp_dir, 'outpout_image.nii.gz') generate_cross_image((10, 10, 10), (0.5, 0.7, 0.7), input_path, float) self.call(main, input_path, output_path, '--iso_min', interp='quad') check(nib.load(output_path), (0.5, 0.5, 0.5), (10, 14, 14)) generate_cross_image((10, 10, 10), (0.5, 0.5, 0.5), input_path) self.call(main, input_path, output_path, '-f', resolution=1.0) check(nib.load(output_path), (1.0, 1.0, 1.0), (5, 5, 5)) reference_path = os.path.join(self._tmp_dir, 'reference_image.nii.gz') generate_cross_image((10, 10, 10), (1.0, 1.0, 1.0), reference_path) self.call(main, input_path, output_path, '-f', ref=reference_path) check(nib.load(output_path), (1.0, 1.0, 1.0), (5, 5, 5)) generate_cross_image( (20, 20, 20), (0.25, 0.25, 0.25), reference_path) self.call(main, input_path, output_path, '--enforce_dimensions', '-f', ref=reference_path) im = nib.load(output_path) check(im, (0.25, 0.25, 0.25), (20, 20, 20)) data = im.get_data() self.assertTrue((data[2:18, 10, 10] == 1).all(), 'Wrong data on X axis') self.assertTrue((data[10, 2:18, 10] == 1).all(), 'Wrong data on Y axis') self.assertTrue((data[10, 10, 2:18] == 1).all(), 'Wrong data on Z axis')
def test(self): image1 = os.path.join(self._tmp_dir, 'image1.nii.gz') generate_cross_image((10, 10, 10), (1.0, 1.0, 1.0), image1) image2 = os.path.join(self._tmp_dir, 'image2.nii.gz') generate_cross_image((10, 10, 10), (1.0, 1.0, 1.0), image2) image_float = os.path.join(self._tmp_dir, 'image3.nii.gz') generate_cross_image((10, 10, 10), (1.0, 1.0, 1.0), image_float, dtype=float) image_small = os.path.join(self._tmp_dir, 'image2.nii.gz') generate_cross_image((10, 10, 10), (1.0, 1.0, 1.0), image_small) output_path = os.path.join(self._tmp_dir, 'outpout_image.nii.gz') self.call(main, image1, image2, output_path) data = nib.load(output_path).get_data() if not ((data[1:9, 5, 5] == 2).all() and (data[5, 1:9, 5] == 2).all() and (data[5, 5, 1:9] == 2).all()): self.fail('All voxels in the cross should == 2') self.assertRaises(TypeError, self.call, image1, image_float, output_path) self.assertRaises(TypeError, self.call, image1, image_small, output_path)