Model fitting
Exceptions:
Functions:
|
Initialize a model. |
|
Fit a model to data. |
|
Apply a model to new data. |
|
Estimate marginal distributions over syllables. |
|
Edit the hyperparameters of a model. |
|
Calculate the expected marginal likelihood score for each model. |
- keypoint_moseq.fitting.init_model(*args, location_aware=False, allo_hypparams=None, trans_hypparams=None, metadata=None, **kwargs)[source]
Initialize a model. Wrapper for jax_moseq.models.keypoint_slds.init_model and jax_moseq.models.allo_keypoint_slds.init_model.
- Parameters:
location_aware (bool, default=False) – If True, the model will be initialized using the location-aware version of the keypoint-SLDS model (jax_moseq.models.allo_keypoint_slds).
allo_hypparams (dict, default=None) – Hyperparameters for the allo_keypoint_slds model. Keys left out take their default values.
trans_hypparams (dict) – Transition hyperparameters, required when location_aware=True (usually passed with **config()).
metadata (tuple (keys, bounds), default=None) – Segment metadata from
keypoint_moseq.util.format_data(). Used only to report the number of frames if initialization runs out of memory.
- Returns:
model – Model dictionary containing states, parameters, hyperparameters, noise prior, and random seed.
- Return type:
dict
- Raises:
MemoryError – If the device runs out of memory, with the data size, the settings in effect and ways to fit within memory.
- keypoint_moseq.fitting.fit_model(model, data, metadata, project_dir=None, model_name=None, num_iters=50, start_iter=0, verbose=False, ar_only=False, parallel_message_passing=None, jitter=0.001, generate_progress_plots=True, save_every_n_iters=25, location_aware=False, fps=None, **kwargs)[source]
Fit a model to data.
- This method optionally:
saves checkpoints of the model and data at regular intervals
plots of the model’s progress during fitting (see
keypoint_moseq.viz.plot_progress())
If a checkpoint for model_name already exists, it must have been saved from the same data (recording names, segment bounds and array shapes), and all model snapshots after start_iter are deleted.
- Parameters:
model (dict) – Model dictionary containing states, parameters, hyperparameters, noise prior, and random seed.
data (dict) – Data for model fitting (see
keypoint_moseq.util.format_data()).metadata (tuple (keys, bounds)) – Recordings and start/end frames for the data (see
keypoint_moseq.util.format_data()).project_dir (str, default=None) – Project directory; required unless save_every_n_iters=None.
model_name (str, default=None) – Name of the model. If None, the model is named using the current date and time.
num_iters (int, default=50) – Index of the last iteration. Fitting runs num_iters - start_iter + 1 resampling steps and saves the last as snapshot num_iters, so to continue a fit until its last snapshot is N iterations later, pass num_iters=start_iter + N.
start_iter (int, default=0) – Index of the starting iteration, which is non-zero when continuing a previous fit.
verbose (bool, default=False) – If True, print the model’s progress during fitting.
ar_only (bool, default=False) – If True, fit an AR-HMM model using the latent trajectory defined by model[‘states’][‘x’] (see
jax_moseq.models.arhmm.gibbs.resample_model()). Otherwise fit a full keypoint-SLDS model (seejax_moseq.models.keypoint_slds.gibbs.resample_model())save_every_n_iters (int, default=25) – Save the current model every save_every_n_iters. To only save the final model, set save_every_n_iters=-1. To save nothing, set save_every_n_iters=None.
generate_progress_plots (bool, default=True) – If True, generate plots of the model’s progress during fitting. The latest plot is saved to {project_dir}/{model_name}/fitting_progress.pdf.
parallel_message_passing (bool | string, default=None) – Use parallel implementation of Kalman sampling, which can be faster but has a significantly longer jit time. If None, will be set automatically based on the backend (True for GPU, False for CPU). A warning will be raised if parallel_message_passing=True and JAX is CPU-bound. Set to ‘force’ to skip this check.
jitter (float, default=0.001) – Amount to boost the diagonal of the dynamics covariance matrix when resampling pose trajectories. Increasing this value can help prevent NaNs during fitting.
location_aware (bool, default=False) – If True, the model will be fit using the location-aware version of the keypoint-SLDS model (jax_moseq.models.allo_keypoint_slds).
fps (float, default=None) – Frame rate of the recordings. If given, the progress plots show syllable durations in seconds; otherwise in frames. The notebook passes it with the rest of the config (**config()), which also sets verbose; other config entries are ignored.
- Returns:
model (dict) – Model dictionary containing states, parameters, hyperparameters, noise prior, and random seed.
model_name (str) – Name of the model.
- Raises:
ValueError – If a checkpoint for model_name exists and was saved from different data.
MemoryError – If fitting runs out of memory. The message gives the data size, the settings in effect and ways to fit within memory.
- keypoint_moseq.fitting.apply_model(model, data, metadata, project_dir=None, model_name=None, num_iters=None, ar_only=False, save_results=True, verbose=False, results_path=None, parallel_message_passing=None, return_model=False, location_aware=False, overwrite=False, jitter=0.001, **kwargs)[source]
Apply a model to new data.
- Parameters:
model (dict) – Model dictionary containing states, parameters, hyperparameters, noise prior, and random seed.
data (dict) – Data for model fitting (see
keypoint_moseq.util.format_data()).metadata (tuple (keys, bounds)) – Recordings and start/end frames for the data (see
keypoint_moseq.util.format_data()).project_dir (str, default=None) – Path to the project directory. Required if save_results=True and results_path=None.
model_name (str, default=None) – Name of the model. Required if save_results=True and results_path=None.
num_iters (int, default=None) – Number of iterations to run the model, 500 if None. With ar_only=True, a single iteration runs, since each one draws the syllable labels independently given the same pose trajectories.
ar_only (bool, default=False) – See
keypoint_moseq.fitting.fit_model().save_results (bool, default=True) – If True, the model outputs will be saved to disk (see
keypoint_moseq.io.extract_results()for the output format).verbose (bool, default=False) – Whether to print progress updates.
results_path (str, default=None) – Optional path for saving model outputs.
parallel_message_passing (bool | string, default=None) – Use parallel implementation of Kalman sampling, which can be faster but has a significantly longer jit time. If None, will be set automatically based on the backend (True for GPU, False for CPU). A warning will be raised if parallel_message_passing=True and JAX is CPU-bound. Set to ‘force’ to skip this check.
return_model (bool, default=False) – Whether to return the model after fitting.
location_aware (bool, default=False) – If True, apply the location-aware version of the keypoint-SLDS model (jax_moseq.models.allo_keypoint_slds). Set this to the value the model was fit with.
overwrite (bool, default=False) – If True, overwrite existing results for recordings that are already in the results file. This is required when applying a model to data that includes recordings from the original training set.
jitter (float, default=0.001) – See
keypoint_moseq.fitting.fit_model().
- Returns:
results (dict) – Dictionary of model outputs for the recordings in data (for results format, see
keypoint_moseq.io.extract_results()). When saved, they are added to the results file alongside any recordings already there.model (dict) – Model dictionary containing states, parameters, hyperparameters, noise prior, and random seed. Only returned if return_model=True.
- keypoint_moseq.fitting.estimate_syllable_marginals(model, data, metadata, burn_in_iters=200, num_samples=100, steps_per_sample=10, return_samples=False, verbose=False, parallel_message_passing=None, location_aware=False, jitter=0.001, **kwargs)[source]
Estimate marginal distributions over syllables.
- Parameters:
model (dict) – Model dictionary containing states, parameters, hyperparameters, noise prior, and random seed.
data (dict) – Data for model fitting (see
keypoint_moseq.util.format_data()).metadata (tuple (keys, bounds)) – Recordings and start/end frames for the data (see
keypoint_moseq.util.format_data()).burn_in_iters (int, default=200) – Number of resampling iterations to run before collecting samples.
num_samples (int, default=100) – Number of samples to collect for marginalization.
steps_per_sample (int, default=10) – Number of resampling iterations to run between collecting samples.
return_samples (bool, default=False) – Whether to store and return sampled syllable sequences. May require significant RAM.
verbose (bool, default=False) – Whether to print progress updates.
parallel_message_passing (bool | string, default=None) – Use parallel implementation of Kalman sampling, which can be faster but has a significantly longer jit time. If None, will be set automatically based on the backend (True for GPU, False for CPU). A warning will be raised if parallel_message_passing=True and JAX is CPU-bound. Set to ‘force’ to skip this check.
location_aware (bool, default=False) – If True, use the location-aware version of the keypoint-SLDS model (jax_moseq.models.allo_keypoint_slds), whose marginals include heading and centroid dynamics. Set this to the value the model was fit with.
jitter (float, default=0.001) – See
keypoint_moseq.fitting.fit_model().
- Returns:
marginal_estimates (dict) – Estimated marginal distributions over syllables in the form of a dictionary mapping recording names to arrays of shape
(num_timepoints, num_syllables).samples (dict) – Sampled syllable sequences in the form of a dictionary mapping recording names to arrays of shape
(num_timepoints, num_samples). Only returned if return_samples=True.
- keypoint_moseq.fitting.update_hypparams(model_dict, **kwargs)[source]
Edit the hyperparameters of a model.
Hyperparameters are stored as a nested dictionary in the hypparams key of the model dictionary. This function allows the user to update the hyperparameters of a model by passing in keyword arguments with the same name as the hyperparameter. The hyperparameter will be updated if it is a scalar value.
- Parameters:
model_dict (dict) – Model dictionary.
kwargs (dict) – Keyword arguments mapping hyperparameter names to new values.
- Returns:
model_dict – Model dictionary with updated hyperparameters.
- Return type:
dict
- Raises:
ValueError – If num_states, latent_dim, nlags, S_0_scale or K_0_scale is passed. These are fixed when the model is created; change them in the config and initialize a new model.
- keypoint_moseq.fitting.expected_marginal_likelihoods(project_dir=None, model_names=None, checkpoint_paths=None)[source]
Calculate the expected marginal likelihood score for each model.
The score is calculated as follows, where theta^i denotes the autoregressive parameters and transition matrix for the i’th model, x^i denotes the latent trajectories for the i’th model, and the number of models is N:
score(theta^i) = 1/(N-1) sum_{j ≠ i} P(x^j | theta^i)
- Parameters:
project_dir (str) – Path to the project directory. Required if
checkpoint_pathsis None.model_names (list of str) – Names of the models to compare. Required if
checkpoint_pathsis None.checkpoint_paths (list of str) – Paths to the checkpoints to compare. Required if
model_namesandproject_dirare None.
- Returns:
scores (numpy array) – Expected marginal likelihood score for each model.
standard_errors (numpy array) – Standard error of the expected marginal likelihood score for each model.