Skeletons

Functions related to fetching and manipulating skeletons.

fetch_skeleton(body[, heal, export_path, ...])

Equivalent to Client.fetch_skeleton().

heal_skeleton(skeleton_df[, max_distance, ...])

Attempt to repair a fragmented skeleton into a single connected component.

reorient_skeleton(skeleton_df[, rowId, xyz, ...])

Change the root node of a skeleton.

skeleton_swc_to_df(swc)

Create a DataFrame from and SWC file.

skeleton_df_to_swc(df[, export_path])

Create an SWC file from a skeleton DataFrame.

skeleton_df_to_nx(df[, with_attributes, ...])

Create a networkx.Graph from a skeleton DataFrame.

skeleton_segments(skeleton_df)

Compute a table of skeleton segments.

upsample_skeleton(skeleton_df, ...)

Insert new nodes into a skeleton make it "higher resolution".

attach_synapses_to_skeleton(skeleton_df, ...)

Attach a neuron's synapses to its skeleton as new skeleton nodes.

Reference

neuprint.skeleton.fetch_skeleton(body, heal=False, export_path=None, format='pandas', with_distances=False, *, client=None)[source]

Equivalent to Client.fetch_skeleton(). See that function for details.

neuprint.skeleton.heal_skeleton(skeleton_df, max_distance=inf, root_parent=None)[source]

Attempt to repair a fragmented skeleton into a single connected component.

Rather than a single tree, skeletons from neuprint sometimes consist of multiple fragments, i.e. multiple connected components. That’s due to artifacts in the underlying segmentation from which the skeletons were generated. In such skeletons, there will be multiple ‘root’ nodes (SWC rows where link == -1).

This function ‘heals’ a fragmented skeleton by joining its fragments into a single tree.

First, each fragment is joined to every other fragment at their nearest points. The resulting graph has unnecessary edges, which are then removed by extracting the minimum spanning tree. The MST is returned as the healed skeleton.

Parameters
  • skeleton_df – DataFrame as returned by Client.fetch_skeleton()

  • max_distance – If a skeleton’s fragments are very spatially distant, it may not be desirable to connect them with a new edge. This parameter specifies the maximum length of new edges introduced by the healing procedure. If a skeleton fragment cannot be connected to the rest of the skeleton because it’s too far away, the skeleton will remain fragmented.

  • root_parent – Typically, root nodes point to a special ID, e.g. -1. If this ID is known, then it saves us time when deciding if no healing is necessary.

Returns

DataFrame, with link column updated with updated edges.

neuprint.skeleton.reorient_skeleton(skeleton_df, rowId=None, xyz=None, use_max_radius=False)[source]

Change the root node of a skeleton.

In general, the root node of the skeletons stored in neuprint is not particularly significant, so the directionality of the nodes (parent to child or vice-versa) on any given neuron branch is arbitrary.

This function allows you to pick a different root node and reorient the tree with respect to that node. Replaces the ‘link’ column in each row of the skeleton dataframe so that its parent corresponds to a depth-first traversal from the new root node.

You can specify the new root node either by its row, or by a coordinate (the closest node to that coordinate will be selected) or by size (the largest node will be selected).

Works in-place. Only the ‘link’ column is changed.

If the given skeleton has more than one connected component (and thus more than one root node), the orientation of the edges in other components will be arbitrary.

Parameters
  • skeleton_df – A skeleton dataframe, e.g. as returned by py:func:fetch_skeleton(…, heal=True)

  • rowId – A rowId to use as the new root node

  • xyz – If given, chooses the node closest to the given coordinate as the new root node.

  • use_max_radius – If True, choose the largest node (by radius) to use as the new root node.

neuprint.skeleton.skeleton_swc_to_df(swc)[source]

Create a DataFrame from and SWC file. The ‘node_type’ column is discarded.

Parameters

swc – Either a filepath ending in ‘.swc’, or a file object, or the contents of an SWC file (as a string).

Returns

pd.DataFrame

neuprint.skeleton.skeleton_df_to_swc(df, export_path=None)[source]

Create an SWC file from a skeleton DataFrame.

Parameters
  • df – DataFrame, as returned by Client.fetch_skeleton()

  • export_path – Optional. Write the SWC file to disk a the given location.

Returns

str

neuprint.skeleton.skeleton_df_to_nx(df, with_attributes=True, directed=True, with_distances=False, virtual_roots=False, root_dist=inf)[source]

Create a networkx.Graph from a skeleton DataFrame.

Parameters
  • df – DataFrame as returned by Client.fetch_skeleton()

  • with_attributes – If True, store node attributes for x, y, z, radius

  • directed – If True, return nx.DiGraph, otherwise nx.Graph. Edges will point from child to parent.

  • with_distances – If True, add an edge attribute ‘distance’ indicating the euclidean distance between skeleton nodes.

  • virtual_roots – If True, include nodes for the ‘virtual roots’, i.e. node -1.

  • root_dist – If virtual_roots are requested, this value will be stored as the ‘distance’ of the virtual segment(s) connecting them.

Returns

nx.DiGraph or nx.Graph

neuprint.skeleton.skeleton_segments(skeleton_df)[source]

Compute a table of skeleton segments.

A skeleton dataframe is a table of nodes (points) and their parent nodes.

This function computes a table of segments, where each row lists both the child and parent point, along with some attributes describing the segment: length, average radius, and segment volume.

neuprint.skeleton.upsample_skeleton(skeleton_df, max_segment_length)[source]

Insert new nodes into a skeleton make it “higher resolution”. For all child-parent segments with length greater than the given maximum length, subdivide each segment into N smaller equal-length segments, such that all of the new segments are (ideally) not larger than the given max.

The ‘radius’ column is interpolated between the child and parent radius values.

Note

By default, skeletons use float32 to store point coordinates. Due to rounding errors, the final segment lengths after upsampling may still be slightly larger than the requested max! If you need better precision, cast your skeleton coordinates to float64 first:

sk = sk.astype({k: np.float64 for k in 'xyz'})
Returns

DataFrame. In the result, all previously existing nodes will retain their original rowIds and coordinates, but their ‘link’ (parent) and radii may have changed.

neuprint.skeleton.attach_synapses_to_skeleton(skeleton_df, synapses_df)[source]

Attach a neuron’s synapses to its skeleton as new skeleton nodes. Synapses are attached to their nearest skeleton node (in euclidean terms).

Note

Skeletons are often “low resolution” so some nodes can be relatively far apart. As a consequence, the nearest node to a given synapse may not be close to the nearest point on the nearest line segment to the synapse. To combat this problem, see py:func:upsample_skeleton().

Parameters
  • skeleton_df – A DataFrame containing a neuron skeleton, as produced by fetch_skeleton()

  • synapses_df – A DataFrame of synapse points, as produced by fetch_synapses().

Returns

DataFrame Rows are appended to the skeleton (one per synapse), and a new column is added to distinguish between nodes which belonged to the original skeleton (‘neurite’) and those which were added for synapses (‘pre’ or ‘post’).

Example

In [4]: from neuprint import fetch_skeleton, fetch_synapses, attach_synapses_to_skeleton
   ...: body = 1136399017
   ...: skeleton = fetch_skeleton(body, heal=True)
   ...: synapses = fetch_synapses(body)
   ...: attach_synapses_to_skeleton(skeleton, synapses)

Out[4]:
    rowId             x             y             z     radius  link structure
0         1  12798.000000  30268.000000  15812.000000  21.000000    -1   neurite
1         2  12746.700195  30370.699219  15788.700195  55.464100     1   neurite
2         3  12727.200195  30411.199219  15767.599609  68.081200     2   neurite
3         4  12705.299805  30475.599609  15716.400391  58.952702     3   neurite
4         5  12687.400391  30499.500000  15692.500000  50.619999     4   neurite
...     ...           ...           ...           ...        ...   ...       ...
2032   2033  12073.000000  32575.000000  14386.000000   0.000000   651      post
2033   2034  10072.000000  32572.000000  14464.000000   0.000000   685      post
2034   2035  10647.000000  31797.000000  14057.000000   0.000000   760      post
2035   2036  11203.000000  30673.000000  14839.000000   0.000000  1086      post
2036   2037  11116.000000  30613.000000  14707.000000   0.000000  1068      post

[2037 rows x 7 columns]