moseq_infer.py
Code adapted from the Datta Lab DataJoint Schema for Keypoint-MoSeq inference pipeline
activate(infer_schema_name, *, create_schema=True, create_tables=True, linking_module=None)
¶
Activate this schema.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
infer_schema_name
|
str
|
Schema name on the database server to activate the |
required |
create_schema
|
bool
|
When True (default), create schema in the database if it does not yet exist. |
True
|
create_tables
|
bool
|
When True (default), create schema tables in the database if they do not yet exist. |
True
|
linking_module
|
str
|
A module (or name) containing the required dependencies. |
None
|
Functions:
Name | Description |
---|---|
get_kpms_root_data_dir |
Returns absolute path for root data director(y/ies) with all behavioral recordings, as (list of) string(s) |
get_kpms_processed_data_dir |
Optional. Returns absolute path for processed data. |
Source code in element_moseq/moseq_infer.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
|
Model
¶
Bases: Manual
Register a model.
Attributes:
Name | Type | Description |
---|---|---|
model_id |
int)
|
Unique ID for each model. |
model_name |
varchar)
|
User-friendly model name. |
model_dir |
varchar)
|
Model directory relative to root data directory (e.g. |
latent_dim |
int)
|
Latent dimension of the model. |
kappa |
float)
|
Kappa value of the model. |
model_desc |
varchar)
|
Optional. User-defined description of the model |
Source code in element_moseq/moseq_infer.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
|
VideoRecording
¶
Bases: Manual
Set of video recordings for the Keypoint-MoSeq inference.
Attributes:
Name | Type | Description |
---|---|---|
Session |
foreign key)
|
|
recording_id |
int)
|
Unique ID for each recording. |
Device |
foreign key)
|
Device primary key. |
Source code in element_moseq/moseq_infer.py
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 |
|
File
¶
Bases: Part
File IDs and paths associated with a given recording_id
.
Attributes:
Name | Type | Description |
---|---|---|
VideoRecording |
foreign key)
|
|
file_id(int) |
|
Unique ID for each file. |
file_path |
varchar)
|
Filepath of each video, relative to root data directory. |
Source code in element_moseq/moseq_infer.py
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
|
InferenceTask
¶
Bases: Manual
Staging table to define the Inference task and its output directory.
Attributes:
Name | Type | Description |
---|---|---|
VideoRecording |
foreign key)
|
|
Model |
foreign key)
|
|
PoseEstimationMethod |
foreign key)
|
Pose estimation method used for the specified |
inference_output_dir |
varchar)
|
Optional. Sub-directory where the results will be stored. |
inference_desc |
varchar)
|
Optional. User-defined description of the inference task. |
num_iterations |
int)
|
Optional. Number of iterations to use for the model inference. If null, the default number internally is 50. |
Source code in element_moseq/moseq_infer.py
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
|
Inference
¶
Bases: Computed
Infer the model from the checkpoint file and save the results as results.h5
file.
Attributes:
Name | Type | Description |
---|---|---|
InferenceTask |
foreign_key)
|
|
inference_duration |
float)
|
Time duration (seconds) of the inference computation. |
Source code in element_moseq/moseq_infer.py
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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
|
MotionSequence
¶
Bases: Part
Store the results of the model inference.
Attributes:
Name | Type | Description |
---|---|---|
video_name |
varchar)
|
Name of the video. |
syllable |
longblob)
|
Syllable labels (z). The syllable label assigned to each frame (i.e. the state indexes assigned by the model). |
latent_state |
longblob)
|
Inferred low-dim pose state (x). Low-dimensional representation of the animal's pose in each frame. These are similar to PCA scores, are modified to reflect the pose dynamics and noise estimates inferred by the model. |
centroid |
longblob)
|
Inferred centroid (v). The centroid of the animal in each frame, as estimated by the model. |
heading |
longblob)
|
Inferred heading (h). The heading of the animal in each frame, as estimated by the model. |
Source code in element_moseq/moseq_infer.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
|
GridMoviesSampledInstances
¶
Bases: Part
Store the sampled instances of the grid movies.
Attributes:
Name | Type | Description |
---|---|---|
syllable |
int)
|
Syllable label. |
instances |
longblob)
|
List of instances shown in each in grid movie (in row-major order), where each instance is specified as a tuple with the video name, start frame and end frame. |
Source code in element_moseq/moseq_infer.py
190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
|
make(key)
¶
This function is used to infer the model results from the checkpoint file and store the results in MotionSequence
and GridMoviesSampledInstances
tables.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
dict
|
|
required |
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If no pca model ( |
FileNotFoundError
|
If no model ( |
NotImplementedError
|
If the format method is not |
FileNotFoundError
|
If no valid |
High-level Logic:
1. Fetch the inference_output_dir
where the results will be stored, and if it does not exist, create it.
2. Fetch the model_name
and the num_iterations
from the InferenceTask
table.
3. Load the most recent model checkpoint and the pca model from files in the kpms_project_output_dir
.
4. Load the keypoint data for inference as filepath_patterns
and format it.
5. Initialize and apply the model with the new keypoint data.
6. If the num_iterations
is set, fit the model with the new keypoint data for num_iterations
iterations; otherwise, fit the model with the default number of iterations (50).
7. Save the results as a CSV file and store the histogram showing the frequency of each syllable.
8. Generate and save the plots showing the median trajectory of poses associated with each given syllable.
9. Generate and save video clips showing examples of each syllable.
10. Generate and save the dendrogram representing distances between each syllable's median trajectory.
11. Insert the inference duration in the Inference
table.
12. Insert the results in the MotionSequence
and GridMoviesSampledInstances
tables.
Source code in element_moseq/moseq_infer.py
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 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 |
|