crantpy.viz package#
Submodules#
Module contents#
- crantpy.viz.chunks_to_nm(xyz_ch, vol, voxel_resolution=[4, 4, 40])[source]#
Map a chunk location to Euclidean space. CV workaround Implemented here after Giacomoβs suggestion
- Parameters:
xyz_ch (array-like) β (N, 3) array of chunk indices.
vol (cloudvolume.CloudVolume) β CloudVolume object associated with the chunked space.
voxel_resolution (list, optional) β Voxel resolution.
- Returns:
(N, 3) array of spatial points.
- Return type:
np.array
- crantpy.viz.configure_urllib3_warning_suppression(enable=None)[source]#
Enable suppression of known cosmetic urllib3 warnings.
Trade-offs: Hiding warnings can make it harder to notice real connectivity issues. When enabled, only the specific connection pool message is filtered; other warnings remain visible. Does not call urllib3.disable_warnings().
Control via enable or environment variable CRANTPY_SUPPRESS_URLLIB3_WARNINGS.
Returns True if suppression is enabled, False otherwise.
- crantpy.viz.detect_soma(x, dataset=None, min_rad=800, N=3, progress=True)[source]#
Detect the soma (cell body) location of a neuron based on mesh radius.
This function attempts to identify the soma by finding regions of the mesh with a sufficiently large radius. It supports both single neurons and batch input.
- Parameters:
x (int, str, trimesh.Trimesh, navis.MeshNeuron, or list/array-like) β Neuron ID, mesh, or list thereof. Meshes must not be downsampled. If a list/array is provided, returns coordinates for each.
dataset (str, optional) β Dataset to query. If None, falls back to the default dataset.
min_rad (float, optional) β Minimum radius for a node to be considered a soma candidate. Default is 800.
N (int, optional) β Number of consecutive vertices with radius > min_rad to consider as soma candidates. Default is 3.
progress (bool, optional) β Whether to show a progress bar for batch input. Default is True.
- Returns:
If input is a single neuron, returns a (3,) array of x, y, z coordinates of the detected soma. If input is a list/array, returns (N, 3) array of coordinates for each neuron. If no soma is found, returns [None, None, None] for that neuron.
- Return type:
np.ndarray
- crantpy.viz.detect_soma_mesh(mesh)[source]#
Try detecting the soma based on vertex clusters.
Identifies dense vertex clusters that likely represent the soma.
- Parameters:
mesh (trimesh.Trimesh) β Coordinates in nanometers. Mesh must not be downsampled for accurate detection.
- Returns:
Array of vertex indices that belong to the detected soma region. Returns empty array if no soma is detected.
- Return type:
np.ndarray
- crantpy.viz.detect_soma_skeleton(s, min_rad=800, N=3)[source]#
Try detecting the soma based on radii.
Looks for consecutive nodes with large radii to identify soma. Includes additional checks to ensure the skeleton is valid.
- Parameters:
- Returns:
Node ID of the detected soma, or None if no soma found.
- Return type:
int or None
- crantpy.viz.divide_local_neighbourhood(mesh, radius)[source]#
Divide the mesh into locally connected patches of a given size (overlapping).
- crantpy.viz.find_anchor_loc(neurons, dataset=None, max_threads=4, progress=True)[source]#
Find a representative coordinate for neuron(s) using the L2 cache.
This works by querying the L2 cache and using the representative coordinate for the largest L2 chunk for each neuron.
- Parameters:
neurons (int, str, list, np.ndarray, or NeuronCriteria) β Root ID(s) to get coordinate for. Can be a single root ID, a list of root IDs, or an instance of NeuronCriteria.
dataset (str, optional) β Dataset to query. If None, will use the default dataset.
max_threads (int) β Number of parallel threads to use for batch queries.
progress (bool) β Whether to show a progress bar.
- Returns:
DataFrame with columns: root_id, x, y, z.
- Return type:
- crantpy.viz.get_brain_mesh_scene(neurons, dataset=None, omit_failures=None, threads=5, progress=True, brain_mesh_color='grey', brain_mesh_alpha=0.1, neuron_mesh_alpha=1, neuron_mesh_colors=None, backend='client')[source]#
Create a 3D scene of the brain mesh with the specified neurons in random colors.
- Parameters:
neurons (Union[int, str, tm.Trimesh, navis.MeshNeuron, navis.NeuronList, 'NeuronCriteria', List[Union[int, str, tm.Trimesh, navis.MeshNeuron, 'NeuronCriteria']]]) β The neurons to highlight in the scene.
dataset (Optional[str], optional) β The dataset to use for fetching neuron meshes, by default None
omit_failures (Optional[bool], optional) β Whether to omit neurons that fail to load, by default None
threads (int, optional) β The number of threads to use for loading meshes, by default 5
progress (bool, optional) β Whether to show a progress bar, by default True
brain_mesh_color (Optional[str], optional) β The color of the brain mesh, by default βgreyβ
brain_mesh_alpha (Optional[float], optional) β The transparency of the brain mesh, by default 0.1
neuron_mesh_alpha (Optional[float], optional) β The transparency of the neuron meshes, by default 1
backend (Optional[str], optional) β The pyvista backend to use (βstaticβ, βtrameβ, βclientβ), by default βclientβ
neuron_mesh_colors (list)
- Returns:
The 3D scene containing the brain mesh and highlighted neurons.
- Return type:
pv.Plotter
- crantpy.viz.get_l2_chunk_info(neurons, dataset=None, progress=True, chunk_size=2000)[source]#
Fetch info for L2 chunks associated with given neuron(s).
- Parameters:
neurons (int, str, list, np.ndarray, or NeuronCriteria) β Neurons to fetch L2 chunk info for. Can be a single root ID, a list of root IDs, or an instance of NeuronCriteria.
dataset (str, optional) β Dataset to fetch info from.
progress (bool) β Whether to show a progress bar.
chunk_size (int) β Number of L2 IDs per query.
- Returns:
DataFrame with L2 chunk info (coordinates, vectors, size).
- Return type:
- crantpy.viz.get_l2_dotprops(root_ids, min_size=None, sample=False, omit_failures=None, progress=True, max_threads=10, dataset=None, **kwargs)[source]#
Generate dotprops from L2 chunks for given neuron(s).
- Parameters:
root_ids (int, str, list, np.ndarray, or NeuronCriteria) β Root ID(s) of the FlyWire neuron(s) to generate dotprops for.
min_size (int, optional) β Minimum size (in nm^3) for the L2 chunks. Smaller chunks will be ignored.
sample (float (0 < sample < 1), optional) β If float, will create Dotprops based on a fractional sample of the L2 chunks.
omit_failures (bool, optional) β Behaviour when dotprops generation fails. None (default) raises, True skips, False returns empty Dotprops.
progress (bool) β Whether to show a progress bar.
max_threads (int) β Number of parallel requests to make when fetching the L2 IDs.
dataset (str, optional) β Against which FlyWire dataset to query. If None, will use the default dataset.
**kwargs β Additional keyword arguments passed to Dotprops initialization.
- Returns:
List of Dotprops.
- Return type:
navis.NeuronList
- crantpy.viz.get_l2_graph(root_ids, dataset=None, progress=True, max_threads=4)[source]#
Fetch L2 graph(s) for given neuron(s).
- Parameters:
root_ids (int, str, list, np.ndarray, or NeuronCriteria) β FlyWire root ID(s) for which to fetch the L2 graphs.
dataset (str, optional) β Against which FlyWire dataset to query. If None, will use the default dataset.
progress (bool) β Whether to show a progress bar.
max_threads (int) β Number of parallel threads to use for batch queries.
- Returns:
The L2 graph or list thereof.
- Return type:
networkx.Graph or list of networkx.Graph
- crantpy.viz.get_l2_info(neurons, dataset=None, progress=True, max_threads=4)[source]#
Fetch basic info for given neuron(s) using the L2 cache.
- Parameters:
neurons (int, str, list or NeuronCriteria) β Neurons to fetch info for. Can be a single root ID, a list of root IDs, or an instance of NeuronCriteria.
dataset (str, optional) β Dataset to fetch info from.
progress (bool) β Whether to show a progress bar.
max_threads (int) β Number of parallel requests to make.
- Returns:
DataFrame with basic L2 information for the given neuron(s). - length_um is the underestimated sum of the max diameter across all L2 chunks - bounds_nm is the rough bounding box based on the representative coordinates of the L2 chunks - chunks_missing is the number of L2 chunks not present in the L2 cache.
- Return type:
- crantpy.viz.get_l2_meshes(x, threads=10, progress=True, dataset=None)[source]#
Fetch L2 meshes for a single neuron or NeuronCriteria in CRANTb.
- crantpy.viz.get_l2_skeleton(root_ids, refine=True, drop_missing=True, l2_node_ids=False, omit_failures=None, progress=True, max_threads=4, dataset=None, **kwargs)[source]#
Generate skeleton(s) from L2 graph(s) for given neuron(s).
- Parameters:
root_ids (int, str, list, np.ndarray, or NeuronCriteria) β Root ID(s) of the CRANTb neuron(s) to skeletonize.
refine (bool) β If True, refine skeleton nodes by moving them to the center of their corresponding chunk meshes using the L2 cache.
drop_missing (bool) β Only relevant if
refine=True
: If True, drop chunks that donβt exist in the L2 cache.l2_node_ids (bool) β If True, use the L2 IDs as node IDs.
omit_failures (bool, optional) β Behaviour when skeleton generation fails. None (default) raises, True skips, False returns empty TreeNeuron.
progress (bool) β Whether to show a progress bar.
max_threads (int) β Number of parallel requests to make when fetching the L2 skeletons.
dataset (str, optional) β Against which CRANTb dataset to query. If None, will use the default dataset.
**kwargs β Additional keyword arguments passed to TreeNeuron initialization.
- Returns:
The extracted L2 skeleton(s).
- Return type:
navis.TreeNeuron or navis.NeuronList
- crantpy.viz.get_mesh_neuron(neurons, dataset=None, omit_failures=None, threads=5, progress=True)[source]#
Fetch one or more CRANTB neurons as navis.MeshNeuron objects.
This function retrieves mesh representations for the specified neuron(s) from the selected dataset. It supports batch queries, parallel fetching, and flexible error handling.
- Parameters:
neurons (int, str, list of int/str, or NeuronCriteria) β Neuron root ID(s) or a NeuronCriteria instance specifying which neurons to fetch. Accepts a single ID, a list/array of IDs, or a NeuronCriteria object.
dataset (str, optional) β Dataset to fetch info from. If None, uses the default dataset.
omit_failures (bool, optional) β
- Behavior when mesh download fails:
None (default): raise an exception
True: skip the offending neuron (may result in empty NeuronList)
False: return an empty MeshNeuron for failed fetches
threads (int, optional) β Number of parallel threads to use for batch queries. Default is 5.
progress (bool, optional) β Whether to show a progress bar during batch fetching. Default is True.
- Returns:
MeshNeuron if a single neuron is requested, or NeuronList for multiple neurons.
- Return type:
navis.MeshNeuron or navis.NeuronList
- crantpy.viz.get_skeletons(root_ids, dataset='latest', progress=True, omit_failures=None, max_threads=6, **kwargs)[source]#
Fetch skeletons for multiple neurons.
Tries to get precomputed skeletons first, then falls back to on-demand skeletonization if needed. if id more than one root_id, it will use the parallel skeletonization function.
- Parameters:
root_ids (list of int or np.ndarray) β Root IDs of neurons to fetch skeletons for.
dataset (str, default 'latest') β Dataset to query against.
progress (bool, default True) β Show progress during fetching.
omit_failures (bool, optional) β None: raise exception on failures True: skip failed neurons False: return empty TreeNeuron for failed cases
max_threads (int, default 6) β Number of parallel threads for fetching skeletons.
**kwargs β Additional arguments passed to skeletonization if needed.
- Returns:
List of successfully fetched/generated skeletons.
- Return type:
navis.NeuronList
- crantpy.viz.get_soma_from_annotations(root_id, client, dataset=None)[source]#
Try to get soma location from nucleus annotations.
- Parameters:
- Returns:
(x, y, z) coordinates of the soma in nanometers, or None if not found.
- Return type:
tuple or None
- crantpy.viz.load_whole_brain_mesh()[source]#
Download and decode a whole-brain tissue mesh.
- Returns:
The whole-brain tissue mesh.
- Return type:
trimesh.Trimesh
- crantpy.viz.skeletonize_neuron(client, root_id, shave_skeleton=True, remove_soma_hairball=False, assert_id_match=False, threads=2, save_to=None, progress=True, use_pcg_skel=False, **kwargs)[source]#
Skeletonize a neuron the main function.
- Parameters:
client (CAVEclient) β CAVE client for data access.
root_id (int) β Root ID of the neuron to skeletonize.
shave_skeleton (bool, default True) β Remove small protrusions and bristles from skeleton (from my understanding).
remove_soma_hairball (bool, default False) β Remove the hairball mesh from the soma
assert_id_match (bool, default False) β Verify skeleton nodes map to correct segment ID.
threads (int, default 2) β Number of parallel threads for mesh processing.
save_to (str, optional) β Save skeleton as SWC file to this path.
progress (bool, default True) β Show progress bars during processing.
use_pcg_skel (bool, default False) β Try pcg_skel first before skeletor (CAVE-client skeletonization).
**kwargs β Additional arguments for skeletonization algorithms.
- Returns:
navis.TreeNeuron β The skeletonized neuron.
# TODOs from fafbseg
# - Use synapse locations as constraints
# - Mesh preprocessing options
# - Chunked skeletonization for large meshes
# - Use soma annotations from external sources
# - Better error handling/logging
# - Allow user-supplied soma location/radius
# - Option to return intermediate results
# - Support more skeletonization algorithms
# - Merge disconnected skeletons
# - Custom node/edge attributes
- Return type:
navis.TreeNeuron | navis.NeuronList
- crantpy.viz.skeletonize_neurons_parallel(client, root_ids, n_cores=None, progress=True, color_map=None, **kwargs)[source]#
Skeletonize multiple neurons in parallel.
- Parameters:
client (CAVEclient) β CAVE client for data access.
root_ids (list of int or np.ndarray) β Root IDs of neurons to skeletonize.
n_cores (int, optional) β Number of cores to use. If None, uses half of available cores.
progress (bool, default True) β Show progress bars during processing.
color_map (str, optional) β Generate colors for each neuron using this colormap. Returns tuple of (neurons, colors) instead of just neurons.
**kwargs β Additional arguments passed to skeletonize_neuron.
- Returns:
NeuronList of skeletonized neurons, or tuple of (NeuronList, colors) if color_map is specified.
- Return type:
navis.NeuronList or tuple