moseq_infer.py
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
18 19 20 21 22 23 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 |
|
get_kpms_root_data_dir()
¶
Pulls relevant func from parent namespace to specify root data dir(s).
It is recommended that all paths in DataJoint Elements stored as relative paths, with respect to some user-configured "root" director(y/ies). The root(s) may vary between data modalities and user machines. Returns a full path string or list of strings for possible root data directories.
Source code in element_moseq/moseq_infer.py
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
|
get_kpms_processed_data_dir()
¶
Pulls relevant func from parent namespace. Defaults to KPMS's project /videos/.
Method in parent namespace should provide a string to a directory where KPMS output files will be stored. If unspecified, output files will be stored in the session directory 'videos' folder, per Keypoint-MoSeq default.
Source code in element_moseq/moseq_infer.py
85 86 87 88 89 90 91 92 93 94 95 |
|
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
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
|
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
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 |
|
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
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
|
PoseEstimationMethod
¶
Bases: Lookup
Pose estimation methods supported by the keypoint loader of keypoint-moseq
package.
Attributes:
Name | Type | Description |
---|---|---|
pose_estimation_method |
(str
|
Supported pose estimation method (deeplabcut, sleap, anipose, sleap-anipose, nwb, facemap) |
pose_estimation_desc |
(str
|
Optional. Pose estimation method description with the supported formats. |
Source code in element_moseq/moseq_infer.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 |
|
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
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
|
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
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 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 |
|
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
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 |
|
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
248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
|
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
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 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 |
|