Registration API
Core API
register_cmd()
label_cmd(path, out_dir, template_points, session_id)
Mark landmarks on a recording for registration to a template using the landmarks GUI.
Parameters:
-
path(str) –Path to preprocessed HDF5 file or NWB file.
-
out_dir(str) –Output directory for registration landmarks file.
-
template_points(str) –Path to template landmark points in CSV or Fiji XML points format.
-
session_id(str) –Session ID for the recording.
Returns:
-
dict(dict) –Dictionary with the landmarks and their x-y coordinates. Dictionary keys are landmark names, while x-y coordinates are stored as an (x, y) tuple, i.e. (column, row).
Source code in src/mesoscopy/register/__init__.py
landmarks_cmd(path, out_dir, recording_points, template_points, output_width=None, output_height=None)
Register a recording to a template based on defined landmarks.
Parameters:
-
path(str) –Path to preprocessed recording HDF5 or NWB file.
-
out_dir(str) –Output directory for registered recording.
-
recording_points(str) –Path to recording landmark points in CSV or Fiji XML points format.
-
template_points(str) –Path to template landmark points in CSV or Fiji XML points format.
-
output_width(int, default:None) –Width of the registered frames. Defaults to the Allen CCF template width.
-
output_height(int, default:None) –Height of the registered frames. Defaults to the Allen CCF template height.
Returns:
-
str(str) –Path to the registered recording file.
Raises:
-
ValueError–If the path to recording landmarks cannot be inferred.
Source code in src/mesoscopy/register/__init__.py
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 | |
session_id_from_path(path)
Derive a session identifier from a recording path.
Parameters:
-
path(str) –Path to a recording file.
Returns:
-
str(str) –The file name without its extension or the "_preprocessed" suffix.
Source code in src/mesoscopy/register/__init__.py
landmarks_path_candidates(path, out_dir=None)
List the paths a recording's landmarks file may have been written to.
register label names its output after the session ID, which drops the "_preprocessed"
suffix, so the landmarks file rarely sits at <recording>_landmarks.csv.
Parameters:
-
path(str) –Path to the recording file.
-
out_dir(str, default:None) –Output directory the landmarks may have been written to.
Returns:
-
list[Path]–list[pathlib.Path]: Candidate landmark file paths, in search order, without duplicates.
Source code in src/mesoscopy/register/__init__.py
load_maxips(path)
Load maximum intensity projections from a preprocessed HDF5 file.
Parameters:
-
path(str) –Path to the preprocessed HDF5 file.
Returns:
-
tuple[ndarray | None, ndarray | None]–tuple[np.ndarray | None, np.ndarray | None]: Maximum intensity projection for the gcamp and isosb channels, or (None, None) if the file holds no gcamp projection.
Source code in src/mesoscopy/register/__init__.py
linked_preprocessed_path(nwb_path)
Resolve the preprocessed HDF5 file that an NWB file links its dF/F series to.
Parameters:
-
nwb_path(str) –Path to the NWB file.
Returns:
-
str | None–str | None: Path to the linked HDF5 file, or None if the dF/F series is not an external link.
Source code in src/mesoscopy/register/__init__.py
update_nwb(nwb_path, h5_path, tform_params)
Update an NWB file with registered imaging data stored in an HDF5 file.
Creates a link between the NWB file and the HDF5 file. See https://pynwb.readthedocs.io/en/stable/tutorials/advanced_io/linking_data.html.
The registration is a single global affine, so the same 3x3 matrix is stored for every frame of the xy_translation series, giving it a shape of (n_timestamps, 3, 3).
Parameters:
-
nwb_path(str) –Path to the NWB file.
-
h5_path(str) –Path to the HDF5 file containing the registered images.
-
tform_params(ndarray) –Affine transformation matrix, as a 3x3 array.
Returns:
-
NWBFile(NWBFile) –The updated NWB file object. Note that its link to the HDF5 file is closed on return, so the registered image data is only readable by re-opening nwb_path.
Source code in src/mesoscopy/register/__init__.py
Transform API
align_landmarks(recording_landmarks, template_landmarks)
Pair two landmark sets by name.
The transform is fitted from two arrays of points, so the pairing between them is positional.
Building those arrays from dict.values() silently mispairs the points whenever the two sets
are ordered differently or one of them is missing a landmark, so they are matched by name here.
Parameters:
-
recording_landmarks(dict) –Recording landmarks, as {name: (x, y)}.
-
template_landmarks(dict) –Template landmarks, as {name: (x, y)}.
Returns:
-
tuple[list[str], ndarray, ndarray]–tuple[list[str], np.ndarray, np.ndarray]: The shared landmark names in template order, and the matching template and recording points as (n, 2) arrays of (x, y) coordinates.
Raises:
-
ValueError–If the two sets share fewer than MIN_LANDMARKS landmarks.
Source code in src/mesoscopy/register/transform.py
landmark_residuals(tform, template_points, recording_points)
Measure how far each marked landmark lands from its template position once registered.
Residuals are reported in template pixels, i.e. in the space of the registered frames, so they are comparable across recordings of different sizes.
Note that this does not detect a transposed coordinate convention: an affine least-squares fit solves each output axis independently, so swapping the axes of one point set permutes the rows of the fitted matrix and leaves the residuals unchanged.
Parameters:
-
tform(ProjectiveTransform) –The fitted transform, mapping template to recording space.
-
template_points(ndarray) –Template points as an (n, 2) array of (x, y) coordinates.
-
recording_points(ndarray) –Matching recording points, in the same order.
Returns:
-
ndarray–np.ndarray: Residual distance per landmark, in template pixels.
Source code in src/mesoscopy/register/transform.py
landmarks_affine(deltaf_series, recording_landmarks, template_landmarks, output_shape=None)
Warp a DeltaF/F series to match a template using anatomical landmarks.
Both landmark sets must use the same (x, y) — i.e. (column, row) — coordinate convention as
skimage.transform.
The registered frames are in template space, so their shape is that of the template rather than
that of the recording. It defaults to the shape of the Allen CCF atlas, but can be overridden with output_shape.
Parameters:
-
deltaf_series(ndarray) –DeltaF/F series.
-
recording_landmarks(dict) –Recording landmarks, as {name: (x, y)}.
-
template_landmarks(dict) –Template landmarks, as {name: (x, y)}.
-
output_shape(tuple[int, int], default:None) –Shape of the registered frames, as (height, width). Defaults to the shape of the Allen CCF atlas template.
Returns:
-
tuple[ndarray, ProjectiveTransform]–tuple[np.ndarray, trf.ProjectiveTransform]: Registered DeltaF/F series and affine transformation matrix.
Raises:
-
ValueError–If the landmarks do not define a usable affine transform.
Source code in src/mesoscopy/register/transform.py
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
Landmarks GUI
mark_landmarks(maxip_image, alt_image, template_landmarks={}, template_shape=None)
Launch the napari viewer to identify anatomical landmarks on a maximum intensity projection image.
The following landmarks are identified for registration: - bregma: Just plain ol' bregma. - cFP: Frontal pole center. - rFP: Rightmost aspect of the frontal pole. - lFP: Leftmost aspect of the frontal pole. - rPB: Right lateral edge of the parietal bone. - lPB: Left lateral edge of the parietal bone. - lpRSP: Left posterior aspect of the retrosplenial cortex. - rpRSP: Right posterior aspect of the retrosplenial cortex. - aIPB: Anterior aspect of the interparietal bone.
Landmark coordinates are stored as (x, y) tuples, i.e. (column, row), matching the convention used
by the template landmark files and by skimage.transform. Napari works in (row, column) order, so
coordinates are transposed on the way into and out of the viewer.
Marked points are identified by their label property rather than by their position in the
points layer, since deleting and re-marking a point moves it to the end of the layer.
Parameters:
-
maxip_image(NDArray) –Maximum intensity projection image. Could be either channel.
-
alt_image(NDArray) –Alternative image to be displayed alongside the maximum intensity projection image. Usually a second channel.
-
template_landmarks(dict, default:{}) –Dictionary with the landmarks and their x-y coordinates, used to seed the initial point positions. Dictionary keys are landmark names, while x-y coordinates are stored as an (x, y) tuple. Defaults to {}.
-
template_shape(tuple[int, int], default:None) –Shape of the image the template landmarks were marked on, as (height, width). When given, the seed points are scaled to the size of the recording so they do not bunch up in a corner. Defaults to None (no scaling).
Returns:
-
dict(dict) –Dictionary with the landmarks and their x-y coordinates, in template order. Dictionary keys are landmark names, while x-y coordinates are stored as an (x, y) tuple.
Raises:
-
ValueError–If the maximum intensity projection and alternative image do not have the same dimensions.
Source code in src/mesoscopy/register/landmarks_gui.py
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | |