allensdk.core.reference_space module

class allensdk.core.reference_space.ReferenceSpace(structure_tree, annotation, resolution)[source]

Bases: object

static check_and_write(base_dir, structure_id, fn)[source]

A many_structure_masks callback that writes the mask to a nrrd file if the file does not already exist.

check_coverage(structure_ids, domain_mask)[source]

Determines whether a spatial domain is completely covered by structures in a set.

Parameters:
structure_ids : list of int

Specifies the set of structures to check.

domain_mask : numpy ndarray

Same shape as annotation. 1 inside the mask, 0 out. Specifies spatial domain.

Returns:
numpy ndarray :

1 where voxels are missing from the candidate, 0 where the candidate exceeds the domain

direct_voxel_counts()[source]

Determines the number of voxels directly assigned to one or more structures.

Returns:
dict :

Keys are structure ids, values are the number of voxels directly assigned to those structures.

direct_voxel_map
downsample(target_resolution)[source]

Obtain a smaller reference space by downsampling

Parameters:
target_resolution : tuple of numeric

Resolution in microns of the output space.

interpolator : string

Method used to interpolate the volume. Currently only ‘nearest’ is supported

Returns:
ReferenceSpace :

A new ReferenceSpace with the same structure tree and a downsampled annotation.

get_slice_image(axis, position, cmap=None)[source]

Produce a AxBx3 RGB image from a slice in the annotation

Parameters:
axis : int

Along which to slice the annotation volume. 0 is coronal, 1 is horizontal, and 2 is sagittal.

position : int

In microns. Take the slice from this far along the specified axis.

cmap : dict, optional

Keys are structure ids, values are rgb triplets. Defaults to structure rgb_triplets.

Returns:
np.ndarray :

RGB image array.

Notes

If you assign a custom colormap, make sure that you take care of the background in addition to the structures.

make_structure_mask(structure_ids, direct_only=False)[source]

Return an indicator array for one or more structures

Parameters:
structure_ids : list of int

Make a mask that indicates the union of these structures’ voxels

direct_only : bool, optional

If True, only include voxels directly assigned to a structure in the mask. Otherwise include voxels assigned to descendants.

Returns:
numpy ndarray :

Same shape as annotation. 1 inside mask, 0 outside.

many_structure_masks(structure_ids, output_cb=None, direct_only=False)[source]

Build one or more structure masks and do something with them

Parameters:
structure_ids : list of int

Specify structures to be masked

output_cb : function, optional

Must have the following signature: output_cb(structure_id, fn). On each requested id, fn will be curried to make a mask for that id. Defaults to returning the structure id and mask.

direct_only : bool, optional

If True, only include voxels directly assigned to a structure in the mask. Otherwise include voxels assigned to descendants.

Yields:
Return values of output_cb called on each structure_id, structure_mask
pair.

Notes

output_cb is called on every yield, so any side-effects (such as writing to a file) will be carried out regardless of what you do with the return values. You do actually have to iterate through the output, though.

remove_unassigned(update_self=True)[source]

Obtains a structure tree consisting only of structures that have at least one voxel in the annotation.

Parameters:
update_self : bool, optional

If True, the contained structure tree will be replaced,

Returns:
list of dict :

elements are filtered structures

static return_mask_cb(structure_id, fn)[source]

A basic callback for many_structure_masks

total_voxel_counts()[source]

Determines the number of voxels assigned to a structure or its descendants

Returns:
dict :

Keys are structure ids, values are the number of voxels assigned to structures’ descendants.

total_voxel_map
validate_structures(structure_ids, domain_mask)[source]

Determines whether a set of structures produces an exact and nonoverlapping tiling of a spatial domain

Parameters:
structure_ids : list of int

Specifies the set of structures to check.

domain_mask : numpy ndarray

Same shape as annotation. 1 inside the mask, 0 out. Specifies spatial domain.

Returns:
set :

Ids of structures that are the ancestors of other structures in the supplied set.

numpy ndarray :

Indicator for missing voxels.