Пример #1
0
    def test_mask(self):
        self.test[8:10, 6:10, 9:10] = 2
        img = sitk.GetImageFromArray(self.test)

        mask = np.zeros(self.test.shape)
        mask[5:10, 5:10, 5:10] = 1
        mask = sitk.GetImageFromArray(mask)

        (seeds, info) = sitkstrats.com_calc(img, self.MAX_SIZE,
                                            self.MIN_SIZE, mask)

        self.assertEqual(len(seeds), 1)
        self.assertEqual(len(seeds[0]), 3)
        self.assertEqual(type(seeds[0][0]), int)
        self.assertEqual(seeds[0], [9, 7, 8])

        (nmask_seeds, info) = sitkstrats.com_calc(img, 1e10, 0, self.ones_img)

        self.assertEqual(len(nmask_seeds), 2)
        self.assertEqual(len(nmask_seeds), info['nseeds'])
        self.assertEqual(nmask_seeds[1], seeds[0])
        self.assertEqual(nmask_seeds[0], [2, 2, 2])
Пример #2
0
    def test_size_gate(self):
        img = sitk.GetImageFromArray(self.test)

        blob_size = float(np.count_nonzero(self.test)) / \
                    reduce(lambda x, y: x*y, self.test.shape)

        (seeds, info) = sitkstrats.com_calc(img,
                                            blob_size - 0.001,
                                            0,
                                            self.ones_img)

        self.assertEqual(seeds, [])
        self.assertEqual(info['nseeds'], len(seeds))
        self.assertEqual(info['max_size'], blob_size - 0.001)

        (seeds, info) = sitkstrats.com_calc(img,
                                            self.MAX_SIZE,
                                            blob_size + 0.001,
                                            self.ones_img)

        self.assertEqual(seeds, [])
        self.assertEqual(info['nseeds'], len(seeds))
        self.assertEqual(info['min_size'], blob_size + 0.001)
Пример #3
0
    def test_simple(self):
        self.test[2:4, 2:4, 2:4] = 1
        img = sitk.GetImageFromArray(self.test)

        (seeds, info) = sitkstrats.com_calc(
            img, self.MAX_SIZE, self.MIN_SIZE, self.ones_img)

        self.assertEqual(len(seeds), 1)
        self.assertEqual(len(seeds[0]), 3)
        self.assertEqual(type(seeds[0][0]), int)
        self.assertEqual(seeds[0], [2, 2, 2])
        self.assertEqual(info['nseeds'], 1)
        self.assertEqual(info['min_size'], self.MIN_SIZE)
        self.assertEqual(info['max_size'], self.MAX_SIZE)
        self.assertEqual(info['seeds'], seeds)
Пример #4
0
def run_img(img, sha, nseeds, root_dir, addl_seed):  # pylint: disable=C0111
    '''Run the entire protocol on a particular image starting with sha hash'''
    img_info = {}

    lung_img, lung_info = debug_log(sitkstrats.segment_lung, (img, {
        'probe_size': 7
    }), root_dir, sha)
    img_info['lungseg'] = lung_info

    # (img, tmp_info) = debug_log(sitkstrats.crop_to_segmentation,
    #                             (img_in, lung_img),
    #                             root_dir, sha, subdir="crop")
    # lung_img = sitkstrats.crop_to_segmentation(lung_img, lung_img)[0]
    # img_info['crop'] = tmp_info

    segstrats = configure_strats()
    seed_indep_imgs = {}
    seed_indep_info = {}
    seeds = []

    for (sname, strat) in [(strnam, segstrats[strnam]['seed-independent'])
                           for strnam in segstrats]:

        try:
            optha = opthash(strat['opts'])
            fname = os.path.join(root_dir, strat['strategy'].__name__,
                                 sha + "-" + optha + ".nii")
            tmp_img = sitkstrats.read(fname)
            tmp_info = strat['opts']
            tmp_info['file'] = os.path.join(fname)
            logging.info(
                "Loaded seed-independent image for '%s', '%s' from file",
                sname, fname)
        except RuntimeError:
            logging.debug("Building seed-independent image '%s', '%s'.", sname,
                          fname)
            (tmp_img, tmp_info) = debug_log(strat['strategy'],
                                            (img, strat['opts']), root_dir,
                                            sha)
            logging.info("Built seed-independent image '%s', '%s' in %s",
                         sname, fname, tmp_info['time'])

        seed_indep_imgs[sname] = tmp_img
        seed_indep_info[sname] = tmp_info

    # compute seeds, first by taking the centers of mass of a bunch of the
    # watershed segemented regions, then by adding a bunch of random ones that
    # are inside the lung field.
    if nseeds > 1 or addl_seed is None:
        (seeds,
         tmp_info) = sitkstrats.com_calc(img=seed_indep_imgs['watershed'],
                                         max_size=0.05,
                                         min_size=1e-5,
                                         lung_img=lung_img)
        img_info['deterministic-seeds'] = tmp_info
        seeds.extend(sitkstrats.distribute_seeds(lung_img,
                                                 nseeds - len(seeds)))
    else:
        print("Provided seed only.")
        img_info['deterministic-seeds'] = None

    if addl_seed is not None:
        # additional seed is given in terms of the input image, and must
        # be corrected for cropping.
        if 'crop' in img_info:
            origin = img_info['crop']['origin']
            assert len(addl_seed) == len(origin)
            addl_seed = [
                addl_seed[i] - origin[i] for i in range(len(addl_seed))
            ]
        seeds.insert(0, addl_seed)

    if len(seeds) > nseeds:
        logging.warning(
            "The number of seeds generated in the deterministic " +
            "phase (%s) is greater than the allowed number of " +
            "seeds (%s). The list of seeds is being truncated.", len(seeds),
            nseeds)

    # with many deterministic seeds, this list can be longer than nseeds.
    seeds = seeds[0:nseeds]

    seg_info = seeddep(seed_indep_imgs, seeds, root_dir, sha, segstrats,
                       img_info['lungseg']['size'], img)

    img_info['noduleseg'] = {}
    for seed in seg_info:
        for segstrat in seg_info[seed]:
            combined_info = {'seed-dependent': seg_info[seed][segstrat]}

            if segstrat in seed_indep_info:
                combined_info['seed-independent'] = seed_indep_info[segstrat]

            img_info['noduleseg'].setdefault(seed,
                                             {})[segstrat] = combined_info

    return img_info
Пример #5
0
def run_img(img, sha, nseeds, root_dir, addl_seed):  # pylint: disable=C0111
    '''Run the entire protocol on a particular image starting with sha hash'''
    img_info = {}

    lung_img, lung_info = debug_log(sitkstrats.segment_lung,
                                    (img, {'probe_size': 7}),
                                    root_dir, sha)
    img_info['lungseg'] = lung_info

    # (img, tmp_info) = debug_log(sitkstrats.crop_to_segmentation,
    #                             (img_in, lung_img),
    #                             root_dir, sha, subdir="crop")
    # lung_img = sitkstrats.crop_to_segmentation(lung_img, lung_img)[0]
    # img_info['crop'] = tmp_info

    segstrats = configure_strats()
    seed_indep_imgs = {}
    seed_indep_info = {}

    for (sname, strat) in [(strnam, segstrats[strnam]['seed-independent'])
                           for strnam in segstrats]:

        try:
            optha = opthash(strat['opts'])
            fname = os.path.join(root_dir, strat['strategy'].__name__,
                                 sha + "-" + optha + ".nii")
            tmp_img = sitkstrats.read(fname)
            tmp_info = strat['opts']
            tmp_info['file'] = os.path.join(fname)
            logging.info(
                "Loaded seed-independent image for '%s', '%s' from file",
                sname, fname)
        except RuntimeError:
            logging.debug(
                "Building seed-independent image '%s', '%s'.", sname, fname)
            (tmp_img, tmp_info) = debug_log(strat['strategy'],
                                            (img, strat['opts']),
                                            root_dir,
                                            sha)
            logging.info(
                "Built seed-independent image '%s', '%s' in %s",
                sname, fname, tmp_info['time'])

        seed_indep_imgs[sname] = tmp_img
        seed_indep_info[sname] = tmp_info

    # compute seeds, first by taking the centers of mass of a bunch of the
    # watershed segemented regions, then by adding a bunch of random ones that
    # are inside the lung field.
    (seeds, tmp_info) = sitkstrats.com_calc(img=seed_indep_imgs['watershed'],
                                            max_size=0.05, min_size=1e-5,
                                            lung_img=lung_img)
    img_info['deterministic-seeds'] = tmp_info
    seeds.extend(sitkstrats.distribute_seeds(lung_img, nseeds-len(seeds)))

    if addl_seed is not None:
        # additional seed is given in terms of the input image, and must
        # be corrected for cropping.
        origin = img_info['crop']['origin']
        assert len(addl_seed) == len(origin)
        addl_seed = [addl_seed[i] - origin[i]
                     for i in range(len(addl_seed))]
        seeds.insert(0, addl_seed)

    if len(seeds) > nseeds:
        logging.warning("The number of seeds generated in the deterministic " +
                        "phase (%s) is greater than the allowed number of " +
                        "seeds (%s). The list of seeds is being truncated.",
                        len(seeds), nseeds)

    # with many deterministic seeds, this list can be longer than nseeds.
    seeds = seeds[0:nseeds]

    seg_info = seeddep(seed_indep_imgs, seeds,
                       root_dir, sha, segstrats, img_info['lungseg']['size'],
                       img)

    img_info['noduleseg'] = {}
    for seed in seg_info:
        for segstrat in seg_info[seed]:
            combined_info = {'seed-dependent': seg_info[seed][segstrat]}

            if segstrat in seed_indep_info:
                combined_info['seed-independent'] = seed_indep_info[segstrat]

            img_info['noduleseg'].setdefault(
                seed, {})[segstrat] = combined_info

    return img_info