brainglobe

Searchâ€¦

bg-atlasapi

cellfinder

cellfinder-core

cellfinder-napari

BRAINREG-NAPARI

brainreg-segment

Brainrender

Usage

How to use BG-Space

To define a new anatomical space, it is sufficient to give the directions of the stack origin position:

1

source_origin = ("Left", "Superior", "Anterior")

2

target_origin = ("Inferior", "Posterior", "Right")

Copied!

A stack can be then easily transformed from the source to the target space:

1

import bg_space as bg

2

import numpy as np

3

stack = np.random.rand(3, 2, 4)

4

â€‹

5

mapped_stack = bg.map_stack_to(source_origin, target_origin, stack)

Copied!

The transformation is handled only with numpy index operations; *i.e.*, no complex image affine transformations are applied. This is often useful as the preparatory step for starting any kind of image registration.

A shortened syntax can be used to define a space using initials of the origin directions:

1

mapped_stack = bg.map_stack_to("lsa", "ipr", stack)

Copied!

When you work with a stack, the origin is the upper left corner when you show the first element

`stack[0, :, :]`

with matplotlib or when you open the stack with ImageJ. First dimension is the one that you are slicing, the second the height of the image, and the third the width of the image.The

`SpaceConvention`

classSometimes, together with the stack we have to move a set of points (cell coordinates, meshes, etc.). This introduces the additional complexity of keeping track, together with the axes swaps and flips, of the change of the origin offset.

To handle this situation, we can define a source space using the

`SpaceConvention`

class, specifying also the stack shape:1

stack = np.random.rand(3, 2, 4) # a stack in source space

2

annotations = np.array([[0, 0, 0], [2, 1, 3]]) # related point annotations

3

â€‹

4

source_space = bg.SpaceConvention(target_origin, stack.shape)

5

â€‹

6

mapped_stack = source_space.map_stack_to("ipr", stack) # transform the stack

7

mapped_annotations = source_space.map_points_to("ipr", annotations) # transform the points

Copied!

The points are transformed through the generation of a transformation matrix. Finally, if we want to log this matrix (e.g., to reconstruct the full transformations sequence of a registration), we can get it:

1

target_space = bg.SpaceConvention("ipr", stack.shape)

2

transformation_matrix = SpaceConvention.transformation_matrix_to(target_space)

3

# equivalent to:

4

transformation_matrix = SpaceConvention.transformation_matrix_to("ipr", stack.shape)

Copied!

The target get always be defined as a

`bg.SpaceConvention`

object, or a valid origin specification plus a shape (the shape is required only if axes flips are required).Matching space resolutions and offsets

The

`SpaceConvention`

class can deal also with stack resampling/padding/cropping. This requires simply specifying values for resolutions and offsets when instantiating a `SpaceConvention`

object. Once that is done, using `SpaceConvention.transformation_matrix_to`

creating affine transformation matrices from one space to the other will be a piece of cake!1

source_space = bgs.SpaceConvention("asl", resolution=(2, 1, 2), offset=(1, 0, 0))

2

target_space = bgs.SpaceConvention("sal", resolution=(1, 1, 1), offset=(0, 0, 2))

3

source_space.transformation_matrix_to(target_space)

Copied!

Moreover, we can now use those space objects to resample stacks, and to generate stacks matching a target shape with the correct padding/cropping simply by specifying a target offset:

1

source_space = bgs.SpaceConvention("asl", resolution=(2, 1, 2), offset=(1, 0, 0))

2

target_space = bgs.SpaceConvention("asl", resolution=(1, 1, 1), shape=(5, 4, 2)) # we need a target shape

3

source_space.transformation_matrix_to(target_space, stack, to_target_shape=True)

Copied!

Easy iteration over projections

Finally, another convenient feature of BG**-**Space is the possibility of iterating easily through the projections of the stack and generate section names and axes labels:

1

sc = bg.SpaceConvention("asl") # origin for the stack to be plotted

2

â€‹

3

for i, (plane, labels) in enumerate(zip(sc.sections, sc.axis_labels)):

4

axs[i].imshow(stack.mean(i))

5

â€‹

6

axs[i].set_title(f"{plane.capitalize()} view")

7

axs[i].set_ylabel(labels[0])

8

axs[i].set_xlabel(labels[1])

Copied!

Last modified 1yr ago