crantpy.utils package#
Subpackages#
- crantpy.utils.cave package
- Submodules
- Module contents
clear_all_caches()clear_cave_client_cache()clear_cloudvolume_cache()generate_cave_token()get_cave_client()get_cave_datastacks()get_cloudvolume()get_current_cave_token()get_dataset_segmentation_source()get_datastack_segmentation_source()is_latest_roots()is_valid_root()is_valid_supervoxel()parse_root_ids()set_cave_token()validate_cave_client()
Submodules#
Module contents#
- exception crantpy.utils.FilteringError(message=None)[source]#
Bases:
ValueErrorRaised if a filtering operation fails.
- Parameters:
message (str, optional) β The error message.
- Return type:
None
- exception crantpy.utils.NoMatchesError(message=None)[source]#
Bases:
ValueErrorRaised if no matches are found.
- Parameters:
message (str, optional) β The error message.
- Return type:
None
- crantpy.utils.add_annotation_layer(annotations, scene, name=None, connected=False)[source]#
Add annotations as new layer to scene.
- Parameters:
annotations (array or list) β Coordinates for annotations (in voxel space): - (N, 3): Point annotations at x/y/z coordinates - (N, 2, 3): Line segments with start and end points - (N, 4): Ellipsoids with x/y/z center and radius
scene (dict) β Scene to add annotation layer to.
name (str, optional) β Name for the annotation layer.
connected (bool, default False) β If True, point annotations will be connected as a path (TODO).
- Returns:
Modified scene with annotation layer added.
- Return type:
Examples
>>> # Add point annotations >>> points = np.array([[100, 200, 50], [150, 250, 60]]) >>> scene = add_annotation_layer(points, scene, name="my_points") >>> >>> # Add line annotations >>> lines = np.array([ ... [[100, 200, 50], [150, 250, 60]], ... [[150, 250, 60], [200, 300, 70]] ... ]) >>> scene = add_annotation_layer(lines, scene, name="my_lines")
- crantpy.utils.add_skeleton_layer(skeleton, scene, name=None)[source]#
Add skeleton as line annotation layer to scene.
- Parameters:
- Returns:
Modified scene with skeleton layer added.
- Return type:
Examples
>>> skeleton = crt.viz.get_skeletons([720575940621039145])[0] >>> scene = construct_scene() >>> scene = add_skeleton_layer(skeleton, scene)
- crantpy.utils.cached_per_id(cache_name, id_param='x', max_age=7200, result_id_column='old_id')[source]#
Decorator for caching function results on a per-ID basis.
This decorator caches results for individual IDs rather than entire function calls. When the function is called with a list of IDs, it will: 1. Check which IDs have valid cached results 2. Only call the function for uncached IDs 3. Merge cached and new results 4. Cache the new results
This is particularly useful for functions like update_ids() where we want to avoid re-computing results for IDs weβve already processed.
- Parameters:
cache_name (str) β Name of the global cache to use.
id_param (str, default 'x') β Name of the parameter containing the IDs to cache.
max_age (int, default MAXIMUM_CACHE_DURATION) β Maximum age of cached results in seconds.
result_id_column (str, default 'old_id') β Column name in the result DataFrame that contains the ID.
- Returns:
The decorated function with per-ID caching capabilities.
- Return type:
callable
Notes
The decorated function must return a pandas DataFrame
The ID parameter can be a list, array, or single ID
Cache entries are stored with timestamps for staleness checking
The function gains a clear_cache method to manually clear the cache
Examples
>>> @cached_per_id(cache_name="update_ids_cache", id_param="x") >>> def update_ids(x, dataset=None): >>> # Process IDs >>> return pd.DataFrame({'old_id': x, 'new_id': x, 'changed': False}) >>> >>> # First call - computes all IDs >>> result1 = update_ids([1, 2, 3]) >>> >>> # Second call - uses cached results for IDs 2 and 3 >>> result2 = update_ids([2, 3, 4])
- crantpy.utils.cached_result(cache_name, max_age=7200, key_fn=None, should_cache_fn=None, validate_cache_fn=None)[source]#
Decorator for caching function results.
WARNING: This decorator is not thread-safe. It is recommended to use threading.Lock() to ensure thread safety when using this decorator in a multi-threaded environment.
This decorator provides a flexible caching mechanism for function results. It supports custom cache keys, validation, and conditional caching, making it suitable for a variety of use cases.
The cache stores entries in a dictionary structure: {
βresultβ: original_function_result, βmetadataβ: {
β_created_atβ: timestamp
}
}
This approach avoids modifying the original result objects directly, ensuring compatibility with immutable types.
- Parameters:
cache_name (str) β Name of the global cache to use. This is used to group cached results under a specific namespace.
max_age (int, default MAXIMUM_CACHE_DURATION) β Maximum age of cached result in seconds. Cached results older than this duration are considered stale and will be refreshed.
key_fn (callable, optional) β Function to generate a unique cache key based on the functionβs arguments. Defaults to using the first positional argument or the βdatasetβ keyword argument. If the function returns None, an error will be raised.
should_cache_fn (callable, optional) β Function to determine whether the result of the function should be cached. It takes the function result and arguments as input and returns a boolean.
validate_cache_fn (callable, optional) β Function to validate if a cached result is still valid beyond the age check. It takes the cached result and the function arguments as input and returns a boolean.
- Returns:
The decorated function with caching capabilities.
- Return type:
callable
Examples
>>> # Basic Caching: >>> @cached_result(cache_name="example_cache") >>> def expensive_function(x): >>> return x ** 2
>>> # Custom Cache Key: >>> @cached_result(cache_name="example_cache", key_fn=lambda x: f"key_{x}") >>> def expensive_function(x): >>> return x ** 2
>>> # Conditional Caching: >>> @cached_result(cache_name="example_cache", should_cache_fn=lambda result, *args: result > 10) >>> def expensive_function(x): >>> return x ** 2
>>> # Cache Validation: >>> def validate_cache(result, *args): >>> return result is not None
>>> @cached_result(cache_name="example_cache", validate_cache_fn=validate_cache) >>> def expensive_function(x): >>> return x ** 2
Notes
The decorated function gains a clear_cache method to manually clear the cache for the specified cache_name.
The check_stale parameter can be used to skip staleness checks when calling the decorated function.
- crantpy.utils.clear_global_cache(cache_name)[source]#
Clear a named global cache.
- Parameters:
cache_name (str) β Name of the cache to clear
- Return type:
None
- crantpy.utils.construct_scene(*, image=True, segmentation=True, brain_mesh=True, merge_biased_seg=False, nuclei=False, base_neuroglancer=False, layout='xy-3d', dataset=None)[source]#
Construct a basic neuroglancer scene for CRANT data.
- Parameters:
image (bool, default True) β Whether to add the aligned EM image layer.
segmentation (bool, default True) β Whether to add the proofreadable segmentation layer.
brain_mesh (bool, default True) β Whether to add the brain mesh layer.
merge_biased_seg (bool, default False) β Whether to add the merge-biased segmentation layer (for proofreading).
nuclei (bool, default False) β Whether to add the nuclei segmentation layer.
base_neuroglancer (bool, default False) β Whether to use base neuroglancer (affects segmentation layer format).
layout (str, default "xy-3d") β Layout to show. Options: β3dβ, βxy-3dβ, βxyβ, β4panelβ.
dataset (str, optional) β Which dataset to use (βlatestβ or βsandboxβ). If None, uses default.
- Returns:
Neuroglancer scene dictionary with requested layers.
- Return type:
Examples
>>> # Create a minimal visualization scene >>> scene = construct_scene(image=True, segmentation=True, brain_mesh=True) >>> >>> # Create a full proofreading scene >>> scene = construct_scene( ... image=True, ... segmentation=True, ... brain_mesh=True, ... merge_biased_seg=True, ... nuclei=True ... )
- crantpy.utils.create_sql_query(table_name, fields, condition=None, limit=None, start=None)[source]#
Creates a SQL query to get the specified fields from the specified table.
- Parameters:
table_name (str) β The name of the table to query.
fields (List[str]) β The list of field names to include in the query.
condition (str, optional) β The WHERE clause of the query.
limit (int, optional) β The maximum number of rows to return.
start (int, optional) β The number of rows to skip (OFFSET).
- Returns:
The constructed SQL query string.
- Return type:
- crantpy.utils.decode_url(url, format='json')[source]#
Decode neuroglancer URL to extract information.
- Parameters:
- Returns:
Decoded information in requested format.
- Return type:
dict or DataFrame
Examples
>>> url = "https://spelunker.cave-explorer.org/#!{...}" >>> info = decode_url(url, format='brief') >>> print(info['selected']) # List of selected segment IDs >>> print(info['position']) # [x, y, z] coordinates
- crantpy.utils.encode_url(segments=None, annotations=None, coords=None, skeletons=None, skeleton_names=None, seg_colors=None, seg_groups=None, invis_segs=None, scene=None, base_neuroglancer=False, layout='xy-3d', open=False, to_clipboard=False, shorten=False, *, dataset=None)[source]#
Encode data as CRANT neuroglancer scene URL.
- Parameters:
segments (int or list of int, optional) β Segment IDs (root IDs) to have selected in the scene.
annotations (array or dict, optional) β Coordinates for annotations: - (N, 3) array: Point annotations at x/y/z coordinates (in voxels) - dict: Multiple annotation layers {name: (N, 3) array}
coords ((3,) array, optional) β X, Y, Z coordinates (in voxels) to center the view on.
skeletons (TreeNeuron or NeuronList, optional) β Skeleton(s) to add as annotation layer(s). Must be in nanometers.
skeleton_names (str or list of str, optional) β Names for the skeleton(s) to display in the UI. If a single string is provided, it will be used for all skeletons. If a list is provided, its length must match the number of skeletons.
seg_colors (str, tuple, list, dict, or array, optional) β Colors for segments: - str or tuple: Single color for all segments - list: List of colors matching segments - dict: Mapping of segment IDs to colors - array: Labels that will be converted to colors
seg_groups (list or dict, optional) β Group segments into separate layers: - list: Group labels matching segments - dict: {group_name: [seg_id1, seg_id2, β¦]}
invis_segs (int or list, optional) β Segment IDs to select but keep invisible.
scene (dict or str, optional) β Existing scene to modify (as dict or URL string).
base_neuroglancer (bool, default False) β Whether to use base neuroglancer instead of CAVE Spelunker.
layout (str, default "xy-3d") β Layout to show. Options: β3dβ, βxy-3dβ, βxyβ, β4panelβ.
open (bool, default False) β If True, opens the URL in a web browser.
to_clipboard (bool, default False) β If True, copies the URL to clipboard (requires pyperclip).
shorten (bool, default False) β If True, creates a shortened URL (requires state server).
dataset (str, optional) β Which dataset to use. If None, uses default.
- Returns:
Neuroglancer URL.
- Return type:
Examples
>>> # Simple scene with segments >>> url = encode_url(segments=[720575940621039145, 720575940621039146]) >>> >>> # Scene with colored segments >>> url = encode_url( ... segments=[720575940621039145, 720575940621039146], ... seg_colors={720575940621039145: 'red', 720575940621039146: 'blue'} ... ) >>> >>> # Scene with skeleton and centered view >>> import navis >>> skeleton = crt.viz.get_skeletons([720575940621039145])[0] >>> url = encode_url( ... segments=[720575940621039145], ... skeletons=skeleton, ... coords=[24899, 14436, 3739] ... )
- crantpy.utils.filter_df(df, column, value, regex=False, case=False, match_all=False, exact=True)[source]#
This function filters a df based on a column and a value. It can handle string, numeric, and list-containing columns.
- Parameters:
(pd.DataFrame) (df)
(str) (column)
(Any) (value)
(bool) (exact)
(bool)
(bool) β if True, requires all filter values to be present in the cellβs list. If False, requires at least one filter value to be present. Defaults to False.
(bool)
df (pandas.DataFrame)
column (str)
value (Any)
regex (bool)
case (bool)
match_all (bool)
exact (bool)
- Returns:
pd.DataFrame
- Return type:
The filtered df.
- crantpy.utils.generate_cave_token(save=False)[source]#
Generates a token for the CAVE client. If save is True, the token will be saved (overwriting any existing token).
- Parameters:
save (bool, default False) β Whether to save the token after generation.
- Return type:
None
- crantpy.utils.get_cave_client(dataset=None, clear_cache=False, check_stale=True)[source]#
Returns a CAVE client instance. If a token is already set, it will be used for authentication. Otherwise, a new token will be generated.
- Parameters:
clear_cache (bool, default False) β If True, bypasses the cache and fetches a new client.
check_stale (bool, default True) β If True, checks if the cached client is stale based on materialization and maximum cache duration.
dataset (str, optional) β The dataset to use. If not provided, uses the default dataset.
- Returns:
A CAVE client instance authenticated with the token.
- Return type:
CAVEclient
- Raises:
ValueError β If no token is found after attempting to generate one.
- crantpy.utils.get_cloudvolume(dataset=None, clear_cache=False, check_stale=True, **kwargs)[source]#
Returns a cloudvolume instance.
- crantpy.utils.get_current_cave_token()[source]#
Retrieves the current token from the CAVE client.
- Returns:
The current CAVE token.
- Return type:
- Raises:
ValueError β If no token is found.
- crantpy.utils.get_dataset_segmentation_source(dataset)[source]#
Get segmentation source for given dataset.
- crantpy.utils.get_datastack_segmentation_source(datastack)[source]#
Get segmentation source for given CAVE datastack.
- Return type:
- crantpy.utils.inject_dataset(allowed=None, disallowed=None, param_name='dataset')[source]#
Inject current default dataset.
- Parameters:
- Returns:
Decorator function that injects the dataset.
- Return type:
Callable
- crantpy.utils.is_latest_roots(x, timestamp=None, dataset=None, progress=True, batch_size=100000, validate_ids=True, use_http_session=True)[source]#
Check if the given root IDs are the latest based on the timestamp.
- Parameters:
x (IDs = str | int | np.int64) β The root IDs to check.
timestamp (Timestamp = str | int | np.int64 | datetime | np.datetime64 | pd.Timestamp) β The timestamp to compare against. Can also be βmatβ for the latest materialization timestamp.
dataset (str, optional) β The dataset to use.
progress (bool, default True) β Whether to show progress bar for large batches.
batch_size (int, default 100_000) β Batch size for processing large numbers of IDs.
validate_ids (bool, default True) β Whether to validate root IDs before processing.
use_http_session (bool, default True) β Whether to use direct HTTP session for better performance.
- Returns:
A boolean array indicating whether each root ID is the latest.
- Return type:
np.ndarray
Examples
>>> from crantpy.utils.cave.helpers import is_latest_roots >>> is_latest_roots([123456789, 987654321]) array([ True, False])
>>> # Check against latest materialization >>> is_latest_roots([123456789], timestamp="mat") array([ True])
- crantpy.utils.is_valid_root(x, dataset=None, raise_exc=False)[source]#
Check if ID is (potentially) valid root ID.
- Parameters:
- Returns:
A boolean array indicating whether each root ID is valid.
- Return type:
np.ndarray
- Raises:
ValueError β If raise_exc is True and invalid IDs are found.
- crantpy.utils.is_valid_supervoxel(x, dataset=None, raise_exc=False)[source]#
Check if ID is (potentially) valid supervoxel ID.
- Parameters:
- Returns:
If x is a single ID, returns bool. If x is iterable, returns array.
- Return type:
bool or np.ndarray
- Raises:
ValueError β If raise_exc is True and invalid IDs are found.
See also
is_valid_rootUse this function to check if a root ID is valid.
- crantpy.utils.make_iterable(x, force_type=None)[source]#
Convert input to an numpy array.
- Parameters:
x (Any) β The input to convert.
force_type (Optional[type]) β If specified, the input will be cast to this type.
- Returns:
The converted numpy array.
- Return type:
np.ndarray
- crantpy.utils.map_position_to_node(neuron, position, return_distance=False)[source]#
Map a spatial position to the nearest node in a skeleton.
This utility function finds the closest node in a skeleton to a given position using a KDTree for efficient spatial lookup. Useful for soma detection, synapse attachment, and other spatial queries.
- Parameters:
neuron (navis.TreeNeuron) β The skeleton neuron to search.
position (list or np.ndarray) β The [x, y, z] coordinates to map. Should be in the same coordinate system as the neuron (typically nanometers).
return_distance (bool, optional) β If True, also return the Euclidean distance to the nearest node. Default is False.
- Returns:
node_id (int) β The node_id of the nearest node.
distance (float (optional)) β The Euclidean distance to the nearest node in nanometers. Only returned if return_distance=True.
- Return type:
Examples
>>> import crantpy as cp >>> import numpy as np >>> skel = cp.get_l2_skeleton(576460752664524086) >>> # Map a position to nearest node >>> node_id = cp.map_position_to_node(skel, [240000, 85000, 96000]) >>> print(f"Nearest node: {node_id}") >>> # Get distance too >>> node_id, dist = cp.map_position_to_node(skel, [240000, 85000, 96000], return_distance=True) >>> print(f"Nearest node: {node_id} at distance {dist:.2f} nm")
See also
reroot_at_somaReroot a skeleton at its soma location.
detect_somaDetect soma location in a neuron.
- crantpy.utils.match_dtype(value, dtype)[source]#
Match the dtype of a value to a given dtype.
- Parameters:
- Returns:
The converted value.
- Return type:
Any
- Raises:
ValueError β If the dtype is not supported.
- crantpy.utils.neurons_to_url(neurons, include_skeleton=True, downsample=None, **kwargs)[source]#
Create neuroglancer URLs for a list of neurons.
- Parameters:
neurons (NeuronList) β List of neurons to create URLs for. Must have root_id attribute.
include_skeleton (bool, default True) β Whether to include the skeleton in the URL.
downsample (int, optional) β Factor by which to downsample skeletons before adding to scene.
**kwargs β Additional arguments passed to encode_url().
- Returns:
DataFrame with columns: id, name, url
- Return type:
DataFrame
Examples
>>> neurons = crt.viz.get_skeletons([720575940621039145, 720575940621039146]) >>> urls = neurons_to_url(neurons) >>> print(urls[['id', 'url']])
- crantpy.utils.parse_neuroncriteria(allow_empty=False)[source]#
Parse all NeuronCriteria arguments into an array of root IDs.
This decorator automatically converts any NeuronCriteria objects in function arguments to arrays of root IDs. It uses type checking by class name to avoid circular imports.
- Parameters:
allow_empty (bool, default False) β Whether to allow the NeuronCriteria to not match any neurons.
- Returns:
Decorator function that processes NeuronCriteria arguments.
- Return type:
Callable
Examples
>>> @parse_neuroncriteria() >>> def process_neurons(neurons): >>> # neurons will be an array of root IDs >>> return neurons >>> >>> # Can be called with a NeuronCriteria object >>> result = process_neurons(NeuronCriteria(cell_class='example'))
- crantpy.utils.parse_root_ids(neurons)[source]#
Parse various neuron input types to a list of root ID strings. :param neurons: The neuron(s) to parse. Can be a single root ID (int or str),
a list of root IDs, or a NeuronCriteria object.
- crantpy.utils.parse_timestamp(x)[source]#
Parse a timestamp string to Unix timestamp.
- Parameters:
x (Timestamp) β The timestamp string to parse. Int must be unix timestamp. String must be ISO 8601 - e.g. β2021-11-15β. datetime, np.datetime64, pd.Timestamp are also accepted.
- Returns:
The Unix timestamp.
- Return type:
- crantpy.utils.plot_em_image(x, y, z, size=1000)[source]#
Fetch and return an EM image slice from the precomputed CloudVolume. Currently only supports slices through the Z axis (i.e. XY plane).
- Parameters:
- Returns:
The EM image slice as a numpy array.
- Return type:
np.ndarray
- crantpy.utils.reroot_at_soma(neurons, soma_coords=None, detect_soma_kwargs=None, inplace=True, progress=False)[source]#
Reroot skeleton(s) at their soma location.
This convenience function combines soma detection and rerooting. If soma coordinates are not provided, they will be automatically detected using detect_soma(). The skeleton is then rerooted at the node nearest to the soma location.
- Parameters:
neurons (TreeNeuron | NeuronList) β Single neuron or list of neurons to reroot.
soma_coords (np.ndarray or list of np.ndarray, optional) β Soma coordinates in pixels [x, y, z]. If not provided, soma will be automatically detected using detect_soma(). For multiple neurons, provide a list of coordinates in the same order as neurons.
detect_soma_kwargs (dict, optional) β Additional keyword arguments to pass to detect_soma() if soma coordinates are not provided.
inplace (bool, optional) β If True, reroot neurons in place. If False, return rerooted copies. Default is True.
progress (bool, optional) β If True, show progress bar when processing multiple neurons or detecting somas. Default is False.
- Returns:
Rerooted neuron(s). Same as input if inplace=True, otherwise copies.
- Return type:
TreeNeuron | NeuronList
Examples
>>> import crantpy as cp >>> # Get skeleton >>> skel = cp.get_l2_skeleton(576460752664524086) >>> # Reroot at automatically detected soma >>> skel_rerooted = cp.reroot_at_soma(skel) >>> print(f"Root node: {skel_rerooted.root}") >>> # Reroot with provided soma coordinates >>> soma = [28000, 9000, 2200] # in pixels >>> skel_rerooted = cp.reroot_at_soma(skel, soma_coords=soma) >>> # Process multiple neurons >>> skels = cp.get_l2_skeleton([576460752664524086, 576460752590602315]) >>> skels_rerooted = cp.reroot_at_soma(skels, progress=True)
See also
map_position_to_nodeMap a position to the nearest node.
detect_somaDetect soma location in a neuron.
- crantpy.utils.retry(func, retries=5, cooldown=2)[source]#
Retry function on HTTPError.
This also suppresses UserWarnings (commonly raised by l2 cache requests)
- crantpy.utils.retry_func(retries=5, cooldown=2)[source]#
Retry decorator for functions on HTTPError. This also suppresses UserWarnings (commonly raised by l2 cache requests) :param cooldown: Cooldown period in seconds between attempts. :type cooldown: int | float :param retries: Number of retries before we give up. Every subsequent retry
will delay by an additional retry.
- crantpy.utils.scene_to_url(scene, base_neuroglancer=False, shorten=False, open=False, to_clipboard=False)[source]#
Convert neuroglancer scene dictionary to URL.
- Parameters:
scene (dict) β Neuroglancer scene dictionary.
base_neuroglancer (bool, default False) β Whether to use base neuroglancer instead of CAVE Spelunker.
shorten (bool, default False) β Whether to create a shortened URL (requires state server).
open (bool, default False) β If True, opens URL in web browser.
to_clipboard (bool, default False) β If True, copies URL to clipboard.
- Returns:
Neuroglancer URL.
- Return type:
- crantpy.utils.set_cave_token(token)[source]#
Sets the CAVE token for the CAVE client.
- Parameters:
token (str) β The CAVE token to set.
- Return type:
None