Ejemplo n.º 1
0
    def test(self):
        anat = generate_fa(self._tmp_dir)
        centroids_path = os.path.join(self._tmp_dir, 'centroids.trk')
        voxel_map_path = os.path.join(self._tmp_dir, 'voxel_map.nii.gz')
        gt_dict =\
            {'volume': {'01': 8.0, '02': 8.0, '03': 8.0, '04': 8.0, '05': 8.0}}

        for generate in (generate_streamlines,
                         generate_streamlines_with_duplicates):
            bundle_path = generate(self._tmp_dir)
            bundle_name, _ = os.path.splitext(os.path.basename(bundle_path))

            # We first need to create the centroids
            self.call(main_centroids,
                      '-f',
                      bundle_path,
                      centroids_path,
                      nb_points=5)

            self.call(main_voxel_map,
                      '-f',
                      bundle_path,
                      centroids_path,
                      anat,
                      voxel_map_path,
                      upsample=1)

            with RedirectStdOut() as output:
                self.call(main_volume_per_label, '-f', voxel_map_path,
                          bundle_name)

            output_dict = json.loads('\n'.join(output))
            self.assertEqual(
                output_dict, {bundle_name: gt_dict},
                "Wrong volume per label in {}.".format(bundle_name))
Ejemplo n.º 2
0
    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"')
Ejemplo n.º 3
0
    def test(self):
        anat = generate_fa(self._tmp_dir)
        endpoints_map_path = os.path.join(self._tmp_dir,
                                          'endpoints_map.nii.gz')

        gt_dict = {'count': 16}
        for generate, fill_gt_data in ((generate_streamlines, _normal_gt_data),
                                       (generate_streamlines_with_duplicates,
                                        _dup_gt_data)):
            bundle_path = generate(self._tmp_dir)
            bundle_name, _ = os.path.splitext(os.path.basename(bundle_path))

            with RedirectStdOut() as output:
                self.call(main_endpoints_map, '-f', bundle_path, anat,
                          endpoints_map_path)

            output_dict = json.loads('\n'.join(output))
            self.assertEqual(
                output_dict, {bundle_name: gt_dict},
                "Wrong volume per label in {}.".format(bundle_name))

            # Generate the fake ground truth
            fake_gt = np.zeros((5, 5, 5), dtype=np.float)
            fill_gt_data(fake_gt)
            save_to = os.path.join(self._tmp_dir, 'fake_gt.nii.gz')
            nib.save(nib.Nifti1Image(fake_gt, np.identity(4)), save_to)

            self.compare_images(endpoints_map_path, save_to)
Ejemplo n.º 4
0
    def _test(self, bundle_path, gt, gt_dw=None):
        metrics = generate_metrics(self._tmp_dir)
        bundle_name, _ = os.path.splitext(os.path.basename(bundle_path))

        with RedirectStdOut() as output:
            self.call(main, bundle_path, *metrics)

        output_dict = json.loads('\n'.join(output))
        self.assertEqual(output_dict, {bundle_name: gt},
                         "Wrong mean/std in {}.".format(bundle_name))

        # Also test the density_weighting option
        with RedirectStdOut() as output:
            self.call(main, '--density_weighting', bundle_path, *metrics)

        output_dict = json.loads('\n'.join(output))
        self.compare_dict_almost_equal(output_dict[bundle_name], gt_dw or gt)
Ejemplo n.º 5
0
    def _test(self, bundle_path, gt):
        metrics = generate_metrics(self._tmp_dir)
        bundle_name, _ = os.path.splitext(os.path.basename(bundle_path))

        with RedirectStdOut() as output:
            self.call(main, bundle_path, *metrics, num_points=5)

        output_dict = json.loads('\n'.join(output))
        self.compare_dict_almost_equal(output_dict, {bundle_name: gt})
Ejemplo n.º 6
0
    def test_non_weighted_with_itselt(self):
        reference = generate_fa(self._tmp_dir)
        streamlines = generate_streamlines(self._tmp_dir)
        gt_dict = {'dice': 1.0}

        with RedirectStdOut() as output:
            self.call(main_weighted_dice,
                      streamlines, streamlines, reference)

        output_dict = json.loads('\n'.join(output))
        self.assertEqual(
            output_dict, gt_dict,
            "Wrong dice coeff. {} != {}.".format(output_dict['dice'],
                                                 gt_dict['dice']))
Ejemplo n.º 7
0
    def test(self):
        for generate, nb_streamlines in (
                (generate_streamlines, 8),
                (generate_streamlines_with_duplicates, 10)):
            bundle_path = generate(self._tmp_dir)
            bundle_name, _ = os.path.splitext(os.path.basename(bundle_path))

            with RedirectStdOut() as output:
                self.call(main, bundle_path)

            output_dict = json.loads('\n'.join(output))
            self.assertEqual(
                output_dict,
                {bundle_name: {'tract_count': nb_streamlines}},
                "Wrong number of streamlines in {}.".format(bundle_name))
Ejemplo n.º 8
0
    def run_volume(self, spacing, gt):
        anat = generate_fa(self._tmp_dir, spacing)
        for generate, _ in ((generate_streamlines, 8),
                            (generate_streamlines_with_duplicates, 10)):
            bundle_path = generate(self._tmp_dir, spacing)
            bundle_name, _ = os.path.splitext(os.path.basename(bundle_path))

            with RedirectStdOut() as output:
                self.call(main, bundle_path, anat)

            output_dict = json.loads('\n'.join(output))
            self.assertEqual(
                output_dict, {bundle_name: {
                    'volume': gt
                }}, "Wrong streamline volume in {}.".format(bundle_name))
Ejemplo n.º 9
0
    def test_weighted_duplicate(self):
        reference = generate_fa(self._tmp_dir)
        streamlines = generate_streamlines_with_duplicates(self._tmp_dir)
        streamlines_missing_corner =\
            generate_streamlines_with_duplicates_missing_corner(self._tmp_dir)
        gt_dice = 0.88888  # Confirmed manually

        with RedirectStdOut() as output:
            self.call(main_weighted_dice, '--weighted',
                      streamlines, streamlines_missing_corner, reference)

        output_dict = json.loads('\n'.join(output))
        output_dice = float(output_dict['dice'])
        self.assertAlmostEqual(
            output_dice, gt_dice, 4,
            "Wrong dice coeff. {} != {}.".format(output_dice,
                                                 gt_dice))
Ejemplo n.º 10
0
    def test_weighted(self):
        reference = generate_fa(self._tmp_dir)
        streamlines = generate_streamlines(self._tmp_dir)
        streamlines_missing_corner =\
            generate_streamlines_missing_corner(self._tmp_dir)
        gt_dice = 0.8571  # Confirmed using ANTs ImageMath 3 DiceAndMinDistSum

        with RedirectStdOut() as output:
            self.call(main_weighted_dice, '--weighted',
                      streamlines, streamlines_missing_corner, reference)

        output_dict = json.loads('\n'.join(output))
        output_dice = float(output_dict['dice'])
        self.assertAlmostEqual(
            output_dice, gt_dice, 4,
            "Wrong dice coeff. {} != {}.".format(output_dice,
                                                 gt_dice))
Ejemplo n.º 11
0
    def _test(self, bundle_path, gt):
        centroids_path = os.path.join(self._tmp_dir, 'centroids.trk')
        label_map = os.path.join(self._tmp_dir, 'label.npz')
        distance_map = os.path.join(self._tmp_dir, 'distance.npz')
        metrics = generate_metrics(self._tmp_dir)
        meanstdperpoint_path = os.path.join(
            self._tmp_dir, 'meanstdperpoint.json')

        # We need to create the centroids, label and distance maps, then
        # the mean/std per point in order to test the plot.
        self.call(main_centroids,
                  '-f', bundle_path, centroids_path, nb_points=5)
        self.call(main_label_and_distance_maps,
                  '-f', bundle_path,
                  centroids_path, label_map, distance_map)
        with RedirectStdOut() as output:
            self.call(main_meanstdperpoint,
                      bundle_path, label_map, distance_map, *metrics)

        with open(meanstdperpoint_path, 'w') as meanstdperpoint_file:
            meanstdperpoint_file.writelines(output)

        bundle_name, _ = os.path.splitext(os.path.basename(bundle_path))
        save_plots_to = os.path.join(self._tmp_dir, bundle_name)
        os.mkdir(save_plots_to)
        self.call(main_plot, meanstdperpoint_path, save_plots_to)

        save_plots_gt_to = os.path.join(self._tmp_dir, bundle_name + '_gt')
        os.mkdir(save_plots_gt_to)

        for metric_path in metrics:
            metric, _ = split_name_with_nii(os.path.basename(metric_path))
            fig = plot_metrics_stats(
                np.array(gt[metric]['means']), np.array(gt[metric]['stds']),
                title=bundle_name,
                xlabel='Location along the streamline',
                ylabel=metric)
            fig.savefig(
                os.path.join(save_plots_gt_to,
                             '{}_{}.png'.format(bundle_name, metric)),
                bbox_inches='tight')

        dcmp = dircmp(save_plots_to, save_plots_gt_to)
        if dcmp.diff_files:
            self.failureException()
Ejemplo n.º 12
0
    def test_distance_density_weighting(self):
        fake1 = {'mean': 1.11111111111, 'std': 0.99380799}
        fake2 = {'mean': 0.444444444444, 'std': 0.496903995}
        bundle_path = generate_streamlines_with_duplicates(self._tmp_dir)
        self._create_maps(bundle_path)
        gt = {
            'fake_metric_1': {l: fake1
                              for l in self.labels},
            'fake_metric_2': {l: fake2
                              for l in self.labels}
        }

        with RedirectStdOut() as output:
            self.call(main_meanstdperpoint, '--distance_weighting',
                      '--density_weighting', bundle_path, self.label_map,
                      self.distance_map, *self.metrics)

        self._compare(bundle_path, gt, output)
Ejemplo n.º 13
0
    def test_distance_weighting(self):
        fake1 = {'mean': 2.0 / 3.0, 'std': 0.942809041582}
        fake2 = {'mean': 2.0 / 3.0, 'std': 0.471404520791}
        bundle_path = generate_streamlines(self._tmp_dir)
        self._create_maps(bundle_path)
        gt = {
            'fake_metric_1': {l: fake1
                              for l in self.labels},
            'fake_metric_2': {l: fake2
                              for l in self.labels}
        }

        with RedirectStdOut() as output:
            self.call(main_meanstdperpoint, '--distance_weighting',
                      bundle_path, self.label_map, self.distance_map,
                      *self.metrics)

        self._compare(bundle_path, gt, output)
Ejemplo n.º 14
0
    def test_no_weighting(self):
        fake1 = {'mean': 1.0, 'std': 1.0}
        fake2 = {'mean': 0.5, 'std': 0.5}

        bundle_path = generate_streamlines(self._tmp_dir)
        self._create_maps(bundle_path)
        gt = {
            'fake_metric_1': {l: fake1
                              for l in self.labels},
            'fake_metric_2': {l: fake2
                              for l in self.labels}
        }

        with RedirectStdOut() as output:
            self.call(main_meanstdperpoint, bundle_path, self.label_map,
                      self.distance_map, *self.metrics)

        self._compare(bundle_path, gt, output)
    def test(self):
        fodf_path = self.fetch('fodf.nii.gz')
        fa_path = self.fetch('fa.nii.gz')
        md_path = self.fetch('md.nii.gz')

        with RedirectStdOut() as output:
            self.call(main, fodf_path, fa_path, md_path)
        answer = float(output[0].split(' ')[-1])
        self.assertAlmostEqual(answer, 0.284157056528,
                               'Wrong fodf max in ventricles')

        max_output = os.path.join(self._tmp_dir, 'output.txt')
        mask_output = os.path.join(self._tmp_dir, 'output.nii.gz')
        self.call(main, fodf_path, fa_path, md_path,
                  fa_t=0.09, md_t=0.0001,
                  max_value_output=max_output,
                  mask_output=mask_output)
        self.compare_images(mask_output,
                            self.fetch('GT', 'fodf_max_ventricles.nii.gz'))
        with open(max_output, 'r') as f:
            answer = float(f.readlines()[0])
            self.assertAlmostEqual(answer, 0.356223350131,
                                   'Wrong fodf max in ventricles')