Exemple #1
0
    def test_tortuosity(self):
        import skelet3d

        data = np.zeros([20, 20, 20], dtype=np.int8)
        # banana
        data[5, 3:11, 4:6] = 1
        data[5, 10:12, 5:12] = 1

        # bar
        data[5, 3:11, 15:17] = 1

        # import sed3 as ped
        # pe = ped.sed3(data)
        # pe.show()
        skel = skelet3d.skelet3d(data)

        # pe = ped.sed3(skel)
        # pe.show()

        skan = sk.SkeletonAnalyser(copy.copy(skel),
                                   volume_data=data,
                                   voxelsize_mm=[1, 1, 1])
        vessel_tree = skan.skeleton_analysis()

        # banana
        self.assertGreater(vessel_tree[1]['tortuosity'], 1.2)
        # bar
        self.assertLess(vessel_tree[2]['tortuosity'] - 1, 0.00001)
Exemple #2
0
def label_volumetric_vessel_tree(oseg,
                                 vessel_label=None,
                                 write_to_oseg=True,
                                 new_label_str_format="{}{:03d}"):
    """
    Split vessel by branches and put it in segmentation and slab.

    :param oseg: OrganSegmentation object with segmentation, voxelsize_mm and slab
    :param vessel_label: int or string label with vessel. Everything above zero is used if vessel_label is set None.
    :param write_to_oseg: Store output into oseg.segmentation if True. The slab is also updated.
    :param new_label_str_format: format of new slab
    :return:
    """
    import skelet3d
    if vessel_label is None:
        vessel_volume = oseg.segmentation > 0
    else:
        vessel_volume = oseg.select_label(vessel_label)

    # print(np.unique(vessel_volume))
    skel = skelet3d.skelet3d(vessel_volume)
    skan = skelet3d.SkeletonAnalyser(skel, volume_data=vessel_volume)
    skan.skeleton_analysis()
    bl = skan.get_branch_label()
    un = np.unique(bl)
    if write_to_oseg:
        for lb in un:
            if lb != 0:
                new_slabel = new_label_str_format.format(vessel_label, lb)
                new_nlabel = oseg.nlabels(new_slabel)
                oseg.segmentation[bl == lb] = new_nlabel

    # ima.distance_segmentation(oseg.select_label(vessel_label))
    return bl
Exemple #3
0
    def test_vessel_tree_vtk_from_skeleton_failing(self):
        print("skelet3d_installed", skelet3d_installed)

        import skelet3d
        import skelet3d.skeleton_analyser
        import shutil

        fn_out = "tree.vtk"
        if os.path.exists(fn_out):
            os.remove(fn_out)

        volume_data = np.zeros([3, 7, 9], dtype=np.int)
        volume_data[:, :, 1:3] = 1
        volume_data[:, 5, 2:9] = 1
        volume_data[:, 0:7, 5] = 1
        skelet = skelet3d.skelet3d(volume_data)

        skan = skelet3d.skeleton_analyser.SkeletonAnalyser(
            skelet, volume_data=volume_data, voxelsize_mm=[1, 1, 1]
        )
        stats = skan.skeleton_analysis()

        # tvg = TreeBuilder('vtk')
        tvg = TBVTK()
        tvg.set_model1d(stats)
        tvg.voxelsize_mm = [1, 1, 1]
        tvg.shape = [100, 100, 100]
        tvg.tree_data = stats
        output = tvg.buildTree()  # noqa
        tvg.saveToFile(fn_out)
        os.path.exists(fn_out)
Exemple #4
0
    def test_skeleton_from_portal_vein(self):
        import io3d.datasets
        data3d, segm, voxelsize_mm, slab, seeds_liver, seeds_porta = io3d.datasets.generate_synthetic_liver()

        skelet = skelet3d.skelet3d(segm == 2)
        self.assertEqual(np.max(skelet), 1)
        self.assertEqual(np.min(skelet), 0)
Exemple #5
0
    def test_skeleton_analyser_from_portal_vein_to_its_branches_and_save_to_pandas(self):
        filename = "test_output_synthetic_porta.yaml"

        # delete file if exists
        if os.path.exists(filename):
            os.remove(filename)
        import io3d.datasets

        data3d, segm, voxelsize_mm, slab, seeds_liver, seeds_porta = (
            io3d.datasets.generate_synthetic_liver()
        )

        data_segm = segm == 2

        # data_segm[41:44, 122:127, 68:70] = True
        data_segm[40:45, 77:80, 100:110] = False
        data_segm[42:44, 77:80, 103:106] = True
        data_skelet = skelet3d.skelet3d(data_segm)
        self.assertEqual(np.max(data_skelet), 1)
        self.assertEqual(np.min(data_skelet), 0)

        # import sed3
        # ed = sed3.sed3(data_skelet, contour=data_segm)# , contour=branche_label)
        # ed.show()

        skan = sk.SkeletonAnalyser(copy.copy(data_skelet), volume_data=data_segm)
        stats = skan.skeleton_analysis()

        print(stats)
        df = skan.stats_as_dataframe()
        assert df is not None
Exemple #6
0
    def test_tortuosity(self):
        import skelet3d

        data = np.zeros([20, 20, 20], dtype=np.int8)
        # banana
        data[5, 3:11, 4:6] = 1
        data[5, 10:12, 5:12] = 1

        # bar
        data[5, 3:11, 15:17] = 1

        # import sed3 as ped
        # pe = ped.sed3(data)
        # pe.show()
        skel = skelet3d.skelet3d(data)

        # pe = ped.sed3(skel)
        # pe.show()

        skan = sk.SkeletonAnalyser(copy.copy(skel), volume_data=data,
                                   voxelsize_mm=[1, 1, 1])
        vessel_tree = skan.skeleton_analysis()

        # banana
        self.assertGreater(vessel_tree[1]['tortuosity'], 1.2)
        # bar
        self.assertLess(
            vessel_tree[2]['tortuosity'] - 1,
            0.00001
        )
    def test_small_with_write_to_file(self):
        filename = "test_output.yaml"

        # delete file if exists
        if os.path.exists(filename):
            os.remove(filename)


        data = np.zeros([60, 60, 60], dtype=np.int8)
        data[5:8, 13:27, 5:9] = 1
        # crossing
        data[6:21, 18:20, 4:7] = 1

        data_skelet = skelet3d.skelet3d(data)
        # pe = ped.sed3(data)
        # pe.show()

        skan = sk.SkeletonAnalyser(copy.copy(data_skelet), volume_data=data)

        # output = skan.filter_small_objects(data, 3)
        skan.skeleton_analysis()

        # self.assertEqual(output[5, 8, 7], 0)
        skan.to_yaml(filename)
        self.assertTrue(os.path.exists(filename))
Exemple #8
0
    def test_skelet3d(self):
        data = np.zeros([8, 9, 10], dtype=np.int8)
        data[1:4, 3:7, 1:12] = 1
        # data[2:5, 2:8, 3:7] = 1
        # data[2:5, 2:8, 3:5] = 1
        skelet = skelet3d.skelet3d(data)

# expected result
        expected_skelet = np.zeros([8, 9, 10], dtype=np.int8)
        expected_skelet[2, 5, 2:9] = 1

        self.assertTrue(np.array_equal(expected_skelet, skelet))
Exemple #9
0
    def binar_to_skeleton(self):
        # create border with generated stuff for skeletonization
        #expanded_data = self.create_border_for_skeletonization(self.data3d_thr, size=50)
        expanded_data = self.data3d_thr

        expanded_skel = skelet3d.skelet3d(
            (expanded_data > 0).astype(np.int8)
        )

        # cut data shape back to original size
        border = (expanded_data.shape[0]-self.data3d_thr.shape[0])/2
        if border!=0:
            self.data3d_skel = expanded_skel[border:-border, border:-border, border:-border].copy()
        else:
            self.data3d_skel = expanded_skel
Exemple #10
0
    def test_skeleton_problem_with_some_types_of_rectangular_structures(self):
        # TODO fix bug in skeletonization algorithm.
        # It ignores vertical structures and horizontal structures. Sometimes is just change the shape inough.
        #
        import io3d.datasets

        data3d, segm, voxelsize_mm, slab, seeds_liver, seeds_porta = (
            io3d.datasets.generate_synthetic_liver())

        data_segm = segm == 2

        # data_segm[39:44, 120:170, 100:111] = True
        # data_segm[39:44, 50:120, 60:70] = True
        # data_segm[39:44, 60:120, 100:105] = True
        # data_segm[39:44, 60:120, 110:110 + 5] = True
        data_segm[39:44, 120:130, 120:240] = True
        data_segm[39:44, 60:120, 120:120 + 6] = True
        data_segm[39:44, 60:120, 130:130 + 7] = True
        data_segm[39:44, 60:120, 140:140 + 8] = True
        data_segm[39:44, 60:120, 160:160 + 9] = True
        data_segm[39:44, 60:120, 180:180 + 10] = True
        data_segm[39:44, 60:120, 200:200 + 11] = True
        data_segm[39:44, 60:120, 220:220 + 12] = True
        # data_segm[39:44, 60:120, 120:126] = True
        # data_segm[39:44, 60:120, 130:140] = True
        # data_segm[39:44, 60:120, 150:151] = True
        # data_segm[39:44, 60:120, 160:162] = True

        data_skelet = skelet3d.skelet3d(data_segm)

        import sed3

        ed = sed3.sed3(data_skelet,
                       contour=data_segm)  # , contour=branche_label)
        ed.show()

        self.assertEqual(
            np.max(data_skelet[39:44, 60:100, 140:140 + 8]),
            1,
            "In this branche is expected skeleton",
        )
        self.assertEqual(np.min(data_skelet), 0)
        self.assertEqual(np.max(data_skelet), 1)
Exemple #11
0
def label_volumetric_vessel_tree(oseg,
                                 vessel_label=None,
                                 write_to_oseg=True,
                                 new_label_str_format="{}{:03d}"):
    """
    Split vessel by branches and put it in segmentation and slab.

    :param oseg: OrganSegmentation object with segmentation, voxelsize_mm and slab
    :param vessel_label: int or string label with vessel. Everything above zero is used if vessel_label is set None.
    :param write_to_oseg: Store output into oseg.segmentation if True. The slab is also updated.
    :param new_label_str_format: format of new slab
    :return:
    """
    logger.debug("vessel_label {}".format(vessel_label))
    logger.debug("python version {} {}".format(sys.version_info,
                                               sys.executable))
    import skelet3d
    if vessel_label is None:
        vessel_volume = oseg.segmentation > 0
    else:
        vessel_volume = oseg.select_label(vessel_label)

    # print(np.unique(vessel_volume))
    skel = skelet3d.skelet3d(vessel_volume)
    skan = skelet3d.SkeletonAnalyser(skel, volume_data=vessel_volume)
    skan.skeleton_analysis()
    bl = skan.get_branch_label()
    un = np.unique(bl)
    logger.debug("skelet3d branch label min: {}, max: {}, dtype: {}".format(
        np.min(bl), np.max(bl), bl.dtype))
    if write_to_oseg:
        if 127 < np.max(bl) and ((oseg.segmentation.dtype == np.int8) or
                                 (oseg.segmentation.dtype == np.uint8)):
            oseg.segmentation = oseg.segmentation.astype(np.int16)
        for lb in un:
            if lb != 0:
                new_slabel = new_label_str_format.format(vessel_label, lb)
                new_nlabel = oseg.nlabels(new_slabel)
                oseg.segmentation[bl == lb] = new_nlabel

    # ima.distance_segmentation(oseg.select_label(vessel_label))
    return bl
Exemple #12
0
    def __vesselTree(self, binaryData3d, textLabel):
        import skelet3d
        import skeleton_analyser  # histology_analyser as skan
        data3d_thr = (binaryData3d > 0).astype(np.int8)
        data3d_skel = skelet3d.skelet3d(data3d_thr)

        skan = skeleton_analyser.SkeletonAnalyser(
            data3d_skel,
            volume_data=data3d_thr,
            voxelsize_mm=self.voxelsize_mm)
        stats = skan.skeleton_analysis(guiUpdateFunction=None)

        if 'graph' not in self.vessel_tree.keys():
            self.vessel_tree['voxelsize_mm'] = self.voxelsize_mm
            self.vessel_tree['graph'] = {}

        self.vessel_tree['graph'][textLabel] = stats
        # print sa.stats
# save skeleton to special file
        misc.obj_to_file(self.vessel_tree, 'vessel_tree.yaml', filetype='yaml')
Exemple #13
0
    def test_vessel_tree_vtk_from_skeleton_with_more_tubes(self):
        """Test dont fail but there is no right output in vtk file"""
        print("skelet3d_installed", skelet3d_installed)

        import skelet3d
        import skelet3d.skeleton_analyser
        import shutil

        fn_out = "tree_more_tubes.vtk"
        if os.path.exists(fn_out):
            os.remove(fn_out)

        volume_data = np.zeros([20, 21, 22], dtype=np.int8)
        # croess
        volume_data[8:11, 14:17, 4:14] = 1
        volume_data[9:15, 11:15, 9:19] = 1
        volume_data[8:12, 5:9, 4:14] = 1
        volume_data[9:15, 2:7, 9:19] = 1
        # volume_data[10:12, 2:15, 13] = 1
        # volume_data[11:13, 5:12, 14] = 1
        # volume_data[12:14, 16, 13] = 1
        skelet = skelet3d.skelet3d(volume_data)

        skan = skelet3d.skeleton_analyser.SkeletonAnalyser(
            skelet,
            volume_data=volume_data,
            voxelsize_mm=[1, 1, 1],
            cut_wrong_skeleton=False,
        )
        stats = skan.skeleton_analysis()

        # self.assertEqual(len(stats), 1, "There should be just one cylinder based on data with different diameter")
        # tvg = TreeBuilder('vtk')
        tvg = TBVTK()
        tvg.set_model1d(stats)
        tvg.voxelsize_mm = [1, 1, 1]
        tvg.shape = [100, 100, 100]
        tvg.tree_data = stats
        output = tvg.buildTree()  # noqa
        tvg.saveToFile(fn_out)
        self.assertTrue(os.path.exists(fn_out))
    def test_skeleton_analyser_from_portal_vein(self):
        filename = "test_output_synthetic_porta.yaml"

        # delete file if exists
        if os.path.exists(filename):
            os.remove(filename)
        import io3d.datasets
        data3d, segm, voxelsize_mm, slab, seeds_liver, seeds_porta = io3d.datasets.generate_synthetic_liver()

        data_segm = segm == 2
        data_skelet = skelet3d.skelet3d(data_segm)
        self.assertEqual(np.max(data_skelet), 1)
        self.assertEqual(np.min(data_skelet), 0)

        skan = sk.SkeletonAnalyser(copy.copy(data_skelet), volume_data=data_segm)

        skan.skeleton_analysis()

        # self.assertEqual(output[5, 8, 7], 0)
        skan.to_yaml(filename)
        self.assertTrue(os.path.exists(filename))
Exemple #15
0
    def test_fileter_small(self):
        import skelet3d

        data = np.zeros([20, 20, 20], dtype=np.int8)
        data[5, 3:17, 5] = 1
        # crossing
        data[5, 12, 5:13] = 1
        # vyrustek
        data[5, 8, 5:9] = 1

        data = skelet3d.skelet3d(data)
        # pe = ped.sed3(data)
        # pe.show()

        skan = sk.SkeletonAnalyser(copy.copy(data))
        output = skan.filter_small_objects(data, 3)
        # skan.skeleton_analysis()

        # pe = ped.sed3(output)
        # pe.show()

        self.assertEqual(output[5, 8, 7], 0)
    def test_skeleton_analyser_from_portal_vein_to_its_branches(self):
        filename = "test_output_synthetic_porta.yaml"

        # delete file if exists
        if os.path.exists(filename):
            os.remove(filename)
        import io3d.datasets

        data3d, segm, voxelsize_mm, slab, seeds_liver, seeds_porta = (
            io3d.datasets.generate_synthetic_liver())

        data_segm = segm == 2

        # data_segm[41:44, 122:127, 68:70] = True
        data_segm[40:45, 77:80, 100:110] = False
        data_segm[42:44, 77:80, 103:106] = True
        data_skelet = skelet3d.skelet3d(data_segm)
        self.assertEqual(np.max(data_skelet), 1)
        self.assertEqual(np.min(data_skelet), 0)

        # import sed3
        # ed = sed3.sed3(data_skelet, contour=data_segm)# , contour=branche_label)
        # ed.show()

        skan = sk.SkeletonAnalyser(copy.copy(data_skelet),
                                   volume_data=data_segm)

        branche_label = skan.get_branch_label()

        # import sed3
        # ed = sed3.sed3(branche_label) #, contour=skan.sklabel)
        # ed.show()

        # import sed3
        # ed = sed3.sed3(skan.sklabel)# , contour=branche_label)
        # ed.show()
        self.assertGreater(np.max(branche_label), 2)
        # self.assertLess(np.max(branche_label), -4)
        self.assertEqual(branche_label[0, 0, 0], 0)
Exemple #17
0
    def test_fileter_small(self):
        import skelet3d

        data = np.zeros([20, 20, 20], dtype=np.int8)
        data[5, 3:17, 5] = 1
        # crossing
        data[5, 12, 5:13] = 1
        # vyrustek
        data[5, 8, 5:9] = 1

        data = skelet3d.skelet3d(data)
        # pe = ped.sed3(data)
        # pe.show()

        skan = sk.SkeletonAnalyser(copy.copy(data))
        output = skan.filter_small(data, 3)
        # skan.skeleton_analysis()

        # pe = ped.sed3(output)
        # pe.show()

        self.assertEqual(output[5, 8, 7], 0)
Exemple #18
0
    def test_donut(self):
        print "donut"

        # Create donut shape
        data = np.ones([3, 7, 9])
        data[:, 3, 3:6] = 0

        expected_skelet = [
            [
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0]],
            [
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 1, 1, 1, 1, 1, 0, 0],
                [0, 1, 0, 0, 0, 0, 0, 1, 0],
                [0, 1, 0, 0, 0, 0, 0, 1, 0],
                [0, 1, 0, 0, 0, 0, 0, 1, 0],
                [0, 0, 1, 1, 1, 1, 1, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0]],
            [
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0]]]
        skelet = skelet3d.skelet3d(data)

        self.assertEqual(
            0,
            np.sum(np.abs(skelet - expected_skelet))
        )
    def test_skan_from_skeleton_of_one_tube(self):

        import skelet3d.skeleton_analyser

        # fn_out = 'tree_one_tube.vtk'
        # if os.path.exists(fn_out):
        #     os.remove(fn_out)

        volume_data = np.zeros([7, 8, 9], dtype=np.int)
        volume_data[4:8, 4:6, 1:3] = 1
        volume_data[:, 5, 2:9] = 1
        volume_data[:, 0:7, 5] = 1
        skelet = skelet3d.skelet3d(volume_data)

        skan = skelet3d.skeleton_analyser.SkeletonAnalyser(
            skelet, volume_data=volume_data, voxelsize_mm=[1, 1, 1])
        stats = skan.skeleton_analysis()

        self.assertEqual(
            len(stats),
            1,
            "There should be just one cylinder based on data with different diameter",
        )
Exemple #20
0
    def test_skeleton_analyser_from_portal_vein(self):
        filename = "test_output_synthetic_porta.yaml"

        # delete file if exists
        if os.path.exists(filename):
            os.remove(filename)
        import io3d.datasets
        data3d, segm, voxelsize_mm, slab, seeds_liver, seeds_porta = io3d.datasets.generate_synthetic_liver(
        )

        data_segm = segm == 2
        data_skelet = skelet3d.skelet3d(data_segm)
        self.assertEqual(np.max(data_skelet), 1)
        self.assertEqual(np.min(data_skelet), 0)

        skan = sk.SkeletonAnalyser(copy.copy(data_skelet),
                                   volume_data=data_segm)

        skan.skeleton_analysis()

        # self.assertEqual(output[5, 8, 7], 0)
        skan.to_yaml(filename)
        self.assertTrue(os.path.exists(filename))
Exemple #21
0
    def test_skeleton_problem_with_some_types_of_rectangular_structures(self):
        # TODO fix bug in skeletonization algorithm.
        # It ignores vertical structures and horizontal structures. Sometimes is just change the shape inough.
        #
        import io3d.datasets
        data3d, segm, voxelsize_mm, slab, seeds_liver, seeds_porta = io3d.datasets.generate_synthetic_liver()

        data_segm = segm == 2

        # data_segm[39:44, 120:170, 100:111] = True
        # data_segm[39:44, 50:120, 60:70] = True
        # data_segm[39:44, 60:120, 100:105] = True
        # data_segm[39:44, 60:120, 110:110 + 5] = True
        data_segm[39:44, 120:130, 120:240] = True
        data_segm[39:44, 60:120, 120:120 + 6] = True
        data_segm[39:44, 60:120, 130:130 + 7] = True
        data_segm[39:44, 60:120, 140:140 + 8] = True
        data_segm[39:44, 60:120, 160:160 + 9] = True
        data_segm[39:44, 60:120, 180:180 + 10] = True
        data_segm[39:44, 60:120, 200:200 + 11] = True
        data_segm[39:44, 60:120, 220:220 + 12] = True
        # data_segm[39:44, 60:120, 120:126] = True
        # data_segm[39:44, 60:120, 130:140] = True
        # data_segm[39:44, 60:120, 150:151] = True
        # data_segm[39:44, 60:120, 160:162] = True

        data_skelet = skelet3d.skelet3d(data_segm)

        import sed3
        ed = sed3.sed3(data_skelet, contour=data_segm)# , contour=branche_label)
        ed.show()

        self.assertEqual(np.max(
            data_skelet[39:44, 60:100, 140:140 + 8]
        ), 1, "In this branche is expected skeleton")
        self.assertEqual(np.min(data_skelet), 0)
        self.assertEqual(np.max(data_skelet), 1)
    def test_small_with_write_to_file(self):
        filename = "test_output.yaml"

        # delete file if exists
        if os.path.exists(filename):
            os.remove(filename)

        data = np.zeros([60, 60, 60], dtype=np.int8)
        data[5:8, 13:27, 5:9] = 1
        # crossing
        data[6:21, 18:20, 4:7] = 1

        data_skelet = skelet3d.skelet3d(data)
        # pe = ped.sed3(data)
        # pe.show()

        skan = sk.SkeletonAnalyser(copy.copy(data_skelet), volume_data=data)

        # output = skan.filter_small_objects(data, 3)
        skan.skeleton_analysis()

        # self.assertEqual(output[5, 8, 7], 0)
        skan.to_yaml(filename)
        self.assertTrue(os.path.exists(filename))
    def test_skeleton_analyser_from_portal_vein_to_its_branches(self):
        filename = "test_output_synthetic_porta.yaml"

        # delete file if exists
        if os.path.exists(filename):
            os.remove(filename)
        import io3d.datasets
        data3d, segm, voxelsize_mm, slab, seeds_liver, seeds_porta = io3d.datasets.generate_synthetic_liver()

        data_segm = segm == 2

        # data_segm[41:44, 122:127, 68:70] = True
        data_segm[40:45, 77:80, 100:110] = False
        data_segm[42:44, 77:80, 103:106] = True
        data_skelet = skelet3d.skelet3d(data_segm)
        self.assertEqual(np.max(data_skelet), 1)
        self.assertEqual(np.min(data_skelet), 0)

        # import sed3
        # ed = sed3.sed3(data_skelet, contour=data_segm)# , contour=branche_label)
        # ed.show()

        skan = sk.SkeletonAnalyser(copy.copy(data_skelet), volume_data=data_segm)

        branche_label = skan.get_branch_label()

        # import sed3
        # ed = sed3.sed3(branche_label) #, contour=skan.sklabel)
        # ed.show()

        # import sed3
        # ed = sed3.sed3(skan.sklabel)# , contour=branche_label)
        # ed.show()
        self.assertGreater(np.max(branche_label), 2)
        # self.assertLess(np.max(branche_label), -4)
        self.assertEqual(branche_label[0, 0, 0], 0)
Exemple #24
0
    def test_donut(self):
        # Create donut shape
        data = np.ones([3, 7, 9])
        data[:, 3, 3:6] = 0

        expected_skelet = [
            [
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0]],
            [
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 1, 1, 1, 1, 1, 0, 0],
                [0, 1, 0, 0, 0, 0, 0, 1, 0],
                [0, 1, 0, 0, 0, 0, 0, 1, 0],
                [0, 1, 0, 0, 0, 0, 0, 1, 0],
                [0, 0, 1, 1, 1, 1, 1, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0]],
            [
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0],
                [0, 0, 0, 0, 0, 0, 0, 0, 0]]]
        skelet = skelet3d.skelet3d(data)

        self.assertEqual(
            0,
            np.sum(np.abs(skelet - expected_skelet))
        )
Exemple #25
0
def label_volumetric_vessel_tree(oseg, vessel_label=None, write_to_oseg=True, new_label_str_format="{}{:03d}"):
    """
    Split vessel by branches and put it in segmentation and slab.

    :param oseg: OrganSegmentation object with segmentation, voxelsize_mm and slab
    :param vessel_label: int or string label with vessel. Everything above zero is used if vessel_label is set None.
    :param write_to_oseg: Store output into oseg.segmentation if True. The slab is also updated.
    :param new_label_str_format: format of new slab
    :return:
    """
    logger.debug("vessel_label {}".format(vessel_label))
    logger.debug("python version {} {}".format(sys.version_info, sys.executable))
    import skelet3d
    if vessel_label is None:
        vessel_volume = oseg.segmentation > 0
    else:
        vessel_volume = oseg.select_label(vessel_label)

    # print(np.unique(vessel_volume))
    skel = skelet3d.skelet3d(vessel_volume)
    skan = skelet3d.SkeletonAnalyser(skel, volume_data=vessel_volume)
    skan.skeleton_analysis()
    bl = skan.get_branch_label()
    un = np.unique(bl)
    logger.debug("skelet3d branch label min: {}, max: {}, dtype: {}".format(np.min(bl), np.max(bl), bl.dtype))
    if write_to_oseg:
        if 127 < np.max(bl) and ((oseg.segmentation.dtype == np.int8) or (oseg.segmentation.dtype == np.uint8)):
            oseg.segmentation = oseg.segmentation.astype(np.int16)
        for lb in un:
            if lb != 0:
                new_slabel = new_label_str_format.format(vessel_label, lb)
                new_nlabel = oseg.nlabels(new_slabel)
                oseg.segmentation[bl == lb] = new_nlabel

    # ima.distance_segmentation(oseg.select_label(vessel_label))
    return bl
Exemple #26
0
    def test_vessel_tree_vtk_from_skeleton_of_one_tube(self):
        print("skelet3d_installed", skelet3d_installed)

        import skelet3d
        import skelet3d.skeleton_analyser
        import shutil

        fn_out = "tree_one_tube.vtk"
        if os.path.exists(fn_out):
            os.remove(fn_out)

        volume_data = np.zeros([7, 8, 9], dtype=np.int)
        volume_data[4:8, 4:6, 1:3] = 1
        volume_data[:, 5, 2:9] = 1
        volume_data[:, 0:7, 5] = 1
        skelet = skelet3d.skelet3d(volume_data)

        skan = skelet3d.skeleton_analyser.SkeletonAnalyser(
            skelet, volume_data=volume_data, voxelsize_mm=[1, 1, 1]
        )
        stats = skan.skeleton_analysis()

        self.assertEqual(
            len(stats),
            1,
            "There should be just one cylinder based on data with different diameter",
        )
        # tvg = TreeBuilder('vtk')
        tvg = TBVTK()
        tvg.set_model1d(stats)
        tvg.voxelsize_mm = [1, 1, 1]
        tvg.shape = [100, 100, 100]
        tvg.tree_data = stats
        output = tvg.buildTree()  # noqa
        tvg.saveToFile(fn_out)
        self.assertTrue(os.path.exists(fn_out))
Exemple #27
0
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © %YEAR% %USER% <%MAIL%>
#
# Distributed under terms of the %LICENSE% license.
"""
%HERE%
"""

import skelet3d
import numpy as np
# Create donut shape
data = np.ones([3, 7, 9])
data[:, 3, 3:6] = 0

skelet = skelet3d.skelet3d(data)

print skelet
Exemple #28
0
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8

import numpy as np
import skelet3d
import skelet3d.skeleton_analyser
import skelet3d.tree
from skelet3d.tree import TreeBuilder

# output vtk file can be visualized with ParaView
fn_out = 'tree.vkt'

volume_data = np.zeros([3, 7, 9], dtype=np.int)
volume_data [:, :, 1:3] = 1
volume_data [:, 5, 2:9] = 1
volume_data [:, 0:7, 5] = 1
skelet = skelet3d.skelet3d(volume_data)

skan = skelet3d.skeleton_analyser.SkeletonAnalyser(skelet, volume_data=volume_data, voxelsize_mm=[1,1,1])
stats = skan.skeleton_analysis()

tvg = TreeBuilder('vtk')
tvg.voxelsize_mm = [1, 1, 1]
tvg.shape = [100, 100, 100]
tvg.tree_data = stats
output = tvg.buildTree() # noqa
tvg.saveToFile(fn_out)
print("Output saved as '{}'".format(fn_out))
Exemple #29
0
# -*- coding: utf-8 -*-
# vim:fenc=utf-8

import numpy as np
import skelet3d
import skelet3d.skeleton_analyser
import skelet3d.tree
from skelet3d.tree import TreeBuilder

# output vtk file can be visualized with ParaView
fn_out = 'tree.vkt'

volume_data = np.zeros([3, 7, 9], dtype=np.int)
volume_data[:, :, 1:3] = 1
volume_data[:, 5, 2:9] = 1
volume_data[:, 0:7, 5] = 1
skelet = skelet3d.skelet3d(volume_data)

skan = skelet3d.skeleton_analyser.SkeletonAnalyser(skelet,
                                                   volume_data=volume_data,
                                                   voxelsize_mm=[1, 1, 1])
stats = skan.skeleton_analysis()

tvg = TreeBuilder('vtk')
tvg.voxelsize_mm = [1, 1, 1]
tvg.shape = [100, 100, 100]
tvg.tree_data = stats
output = tvg.buildTree()  # noqa
tvg.saveToFile(fn_out)
print("Output saved as '{}'".format(fn_out))
Exemple #30
0
def voda_sk(organ_seg, liver_seg, voxelsize, cr):
    """
    build skeleton of porta
    voda_sk(xyz) build distance 3D map and compute volumes of liverpart for each skeleton element of chosen dataset
    voda_sk(xyz) takes argument from load_vdata - it need volumetric data of porta, liver and vox size (not ncsry..)
    """
    l_d = [organ_seg, liver_seg, voxelsize, cr]

    tempsz = (np.shape(l_d[0][1]))
    ylab = tempsz[1]
    xlab = tempsz[0]
    
    volume_data = l_d[0]
    #skeletonization - params are saved in var stats
    skelet = skelet3d.skelet3d(volume_data)
    skan = skelet3d.SkeletonAnalyser(skelet, volume_data = volume_data, voxelsize_mm = [1, 1, 1])
    stats = skan.skeleton_analysis()
    edge_number = 1

    #build of 3D distance map
    linesarr_x = []
    linesarr_y = []
    linesarr_z = []
    temparr = skan.sklabel
    counter = 0
    tempslx_l = 0
    for i in range(0,len(l_d[0])):
        for j in range(0, xlab):
            for k in range(0, ylab):
                if (temparr[i][j][k] != 0):
                    linesarr_x.append(k)
                    linesarr_y.append(j)
                    counter = counter + 1
        for z in range(0, counter):
            tempslx_l = i * l_d[2] #third value in l_data is space between slices of current datasete
            linesarr_z.append(tempslx_l)
            counter = 0
    distance_map = imlb.distance_segmentation(temparr)
    nodeArr = []
    for i in range(1, (len(stats)) + 1):
        #print(i)
        try:
            nodeArr.append((stats[i]['nodeA_ZYX']))
        except:
            None
        try:
            nodeArr.append((stats[i]['nodeB_ZYX']))
        except:
            None
    zdata = []
    xdata = []
    ydata = []
    temp_xp = 2
    temp_yp = 1
    temp_zp = 0
    for i in range(len(nodeArr)):
        zdata.append(nodeArr[i][temp_zp])
        ydata.append(nodeArr[i][temp_yp])
        xdata.append(nodeArr[i][temp_xp])
        zdata[i] = zdata[i]*l_d[2]
    #build list of all elements in skeleton
    list_= []
    for slx in range(len(l_d[1])):
        for x in range(0, xlab):
            for y in range(0, ylab):
                if(distance_map[slx, x, y]) < 1e5:
                    list_.append(distance_map[slx, x, y])
    set(list_)
    list_of_areas = set(list_)
    list_of_areas_arr = list(list_of_areas)
    list_of_areas_arr_edges = []
    for x in range(0, len(list_of_areas_arr)):
        if(list_of_areas_arr[x] > 0):
            list_of_areas_arr_edges.append(list_of_areas_arr[x])

    #plot data if needed          
    slicearr_p = []
    zte_p = []
    xte_p = []
    yte_p = []
    counter = 0

    for slx in range(len(l_d[0])):
        for x in range(0, xlab):
            for y in range(0, ylab):
                if(l_d[0][slx, x, y]) == 1:
                    xte_p.append(y)
                    yte_p.append(x)
                    counter = counter + 1
        for z in range(0, counter):
            tempslx_p = slx * l_d[2]
            zte_p.append(tempslx_p)
        counter = 0
    slicearr_p.append(xte_p)
    slicearr_p.append(yte_p)
    slicearr_p.append(zte_p)

    slicearr_l = []
    zte_l = []
    xte_l = []
    yte_l = []
    counter = 0

    for slx in range(len(l_d[1])):
        for x in range(0, xlab):
            for y in range(0, ylab):
                if(l_d[1][slx, x, y]) == 1:
                    xte_l.append(y)
                    yte_l.append(x)
                    counter = counter + 1
        for z in range(0, counter):
            tempslx_l = slx * l_d[2]
            zte_l.append(tempslx_l)
        counter = 0
    slicearr_l.append(xte_l)
    slicearr_l.append(yte_l)
    slicearr_l.append(zte_l)

    dist_map_x = []
    dist_map_y = []
    dist_map_z = []
    counter_sl = 0

    #compute of ALL VOLUMES - takes time - control print for each skelet element
    dist_map_final_liver_vol_temp = []
    dist_map_final_liver_vol = []

    loar_l = len(list_of_areas_arr)
    if(loar_l % 2 == 0):
        loar_l = loar_l/2
    else:
        loar_ = loar_l/2 + 1


    for area in range(0, loar_l):
        temp_area = list_of_areas_arr[area]
        for slc in range(0, len(l_d[1])):
            for x in range(0, xlab):
                for y in range(0, ylab):
                    if ((l_d[1][slc, x, y]) == 1):
                        if(((distance_map[slc, x, y]) == temp_area)):
                            dist_map_x.append(y)
                            dist_map_y.append(x)
                            counter_sl = counter_sl + 1
            for slic in range(0, counter_sl):
                dist_map_z.append(slc)
            counter_sl = 0
        dist_map_final_liver_vol_temp.append(dist_map_z)
        dist_map_final_liver_vol_temp.append(dist_map_y)
        dist_map_final_liver_vol_temp.append(dist_map_x)
        dist_map_final_liver_vol_temp.append(temp_area)
        dist_map_final_liver_vol.append(dist_map_final_liver_vol_temp)
        dist_map_final_liver_vol_temp = []
        dist_map_x = []
        dist_map_y = []
        dist_map_z = []
        print("computing volume of area: ", temp_area, "Num. of areas in this dataset: ", len(list_of_areas_arr))
        print("$$$$$-------------------------------------------------------------------$$$$$")

    # ret_array = []
    # ret_array.append(stats)
    # ret_array.append(list_of_areas_arr_edges)
    # ret_array.append(dist_map_final_liver_vol)
    return stats, list_of_areas_arr_edges, dist_map_final_liver_vol
Exemple #31
0
import os
import os.path

import pytest

path_to_script = os.path.dirname(os.path.abspath(__file__))
import unittest
import numpy as np
import sys

try:
    import skelet3d

    data3d = np.ones([3, 7, 9])
    data3d[:, 3, 3:6] = 0
    skelet3d.skelet3d(data3d)
    skelet3d_installed = True
    # skelet3d
except:
    skelet3d_installed = False
    logger.warning("skelet3d is not working")

try:
    import larcc

    larcc_installed = True
except:
    larcc_installed = False
    logger.warning("larcc is not working")

from fibrous.tree import TreeBuilder
Exemple #32
0
#! /usr/bin/env python
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
#
# Copyright © %YEAR% %USER% <%MAIL%>
#
# Distributed under terms of the %LICENSE% license.

"""
%HERE%
"""


import skelet3d
import numpy as np
# Create donut shape
data = np.ones([3,7,9])
data [:, 3, 3:6] = 0

skelet = skelet3d.skelet3d(data)

print(skelet)
Exemple #33
0
 def binar_to_skeleton(self, data3d_thr):
     data3d_thr = (data3d_thr > 0).astype(np.int8)
     data3d_skel = skelet3d.skelet3d(data3d_thr)
     return data3d_skel