seung-lab / igneous

Scalable Neuroglancer compatible Downsampling, Meshing, Skeletonizing, Contrast Normalization, Transfers and more.
GNU General Public License v3.0
43 stars 17 forks source link

How do I export the resulting skeleton to a swc file? #154

Closed liuyx599 closed 1 year ago

liuyx599 commented 1 year ago

I now know that it seems to be possible to get the corresponding skeleton and convert it to swc by cloudvolume, but I seem to have some bugs

vol = cloudvolume.CloudVolume(
    'precomputed:xxxx/seg',  # neuroglancer layer
    use_https=True,  #
    mip=2
)

segment_id = 45
skel = vol.skeleton.get(45)
skel.viewer()

with open('segment_45.swc', 'w') as f:
    f.write(skel.to_swc())

then there is bug

Traceback (most recent call last):
  File "C:\Users\Administrator\miniconda3\lib\site-packages\IPython\core\interactiveshell.py", line 3398, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-125-874f70be3ddb>", line 1, in <cell line: 1>
    skel0.to_swc()
  File "C:\Users\Administrator\miniconda3\lib\site-packages\cloudvolume\skeleton.py", line 1049, in to_swc
    swc += generate_swc(skel, offset) + "\n"
  File "C:\Users\Administrator\miniconda3\lib\site-packages\cloudvolume\skeleton.py", line 1028, in generate_swc
    T=skel.vertex_types[node],
IndexError: index 0 is out of bounds for axis 0 with size 0

but the skeleton is not empty

Skeleton(segid=45, vertices=(shape=226, float32), edges=(shape=225, uint32), radius=(226, float32), vertex_types=(0, uint8), space='physical' transform=[[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0]])

Ok, when I wrote this issue I seem to have solved the bug, I need to manually set the type of these nodes

segment_id = 45
skel = vol.skeleton.get(45)
skel.vertex_types = np.zeros(shape=skel.vertices.shape[0], dtype=np.uint8)   # manually setting
skel.viewer()

with open('segment_45.swc', 'w') as f:
    f.write(skel.to_swc())

By the way, can't Igneous discern the node type of the skeleton using kimimaro's skeletonization method? For example, endpoint, soma, etc.? If I want to determine that a node is an endpoint, do you have a better suggestion?

william-silversmith commented 1 year ago

Hi liuyx599,

Thank you for persisting and figuring out the issue! You make a good point, it should be possible to annotate at least some of the points using radius and structural information. This is something we could add. Endpoints in particular are very easy.

However, one reason we strip the vertex_types when making neuroglancer compatible skeletons is (afaik) neuroglancer doesn't support integer vertex attributes. Once that is fixed, we can stop stripping them.

liuyx599 commented 1 year ago

Thanks for your reply! So kimimaro, like teaser, does not generate node type information during the skeletonization process? I now have a simple idea to speak about nodes of degree 1 (leaf nodes) and root nodes as endpoints of the skeleton, which can be achieved with the help of navis as follows:

skel = navis.read_swc(".\segment_45.swc")
skel.leafs
Out[8]: 
     node_id label       x       y       z      radius  parent_id type
99       104     0  2328.0  2088.0  2970.0  107.331261        103  end
221      226     0  5256.0  3192.0  2970.0   24.000000        225  end
225       79     0  1824.0  1968.0  2970.0   53.665630         78  end

If kimimaro can't automatically determine the type of these nodes, I can't think of a better way to determine the endpoints

william-silversmith commented 1 year ago

Hi! Navis is definitely more sophisticated, but the Skeleton class used in Igneous, CloudVolume, and Kimimaro does have the capability to compute terminal and branch nodes. We could augment the SWC generator with this information.

https://github.com/seung-lab/cloud-volume/blob/master/cloudvolume/skeleton.py#L308-L322

skel.terminals()
skel.branches()
liuyx599 commented 1 year ago

This is very cool, and it seems to be consistent with navis' results, where cloud-volume's terminals are equivalent to navis' leafs plus root