Inference API
Single Inferencer
- class cryoPARES.inference.inferencer.SingleInferencer(particles_star_fname, checkpoint_dir, results_dir, data_halfset='allParticles', model_halfset='matchingHalf', particles_dir=None, batch_size=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, num_dataworkers=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, use_cuda=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, n_cpus_if_no_cuda=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, compile_model=False, top_k_poses_nnet=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, top_k_poses_localref=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, grid_distance_degs=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, reference_map=None, reference_mask=None, directional_zscore_thr=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, skip_localrefinement=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, skip_reconstruction=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, subset_idxs=None, n_first_particles=None, show_debug_stats=False)[source]
Bases:
object
- Parameters:
particles_star_fname (str)
checkpoint_dir (str)
results_dir (str)
data_halfset (Literal['half1', 'half2', 'allParticles'])
model_halfset (Literal['half1', 'half2', 'allCombinations', 'matchingHalf'])
particles_dir (str | None)
batch_size (int)
num_dataworkers (int)
use_cuda (bool)
n_cpus_if_no_cuda (int)
compile_model (bool)
top_k_poses_nnet (int)
top_k_poses_localref (int)
grid_distance_degs (float)
reference_map (str | None)
reference_mask (str | None)
directional_zscore_thr (float | None)
skip_localrefinement (bool)
skip_reconstruction (bool)
n_first_particles (int | None)
show_debug_stats (bool)
- __init__(particles_star_fname, checkpoint_dir, results_dir, data_halfset='allParticles', model_halfset='matchingHalf', particles_dir=None, batch_size=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, num_dataworkers=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, use_cuda=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, n_cpus_if_no_cuda=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, compile_model=False, top_k_poses_nnet=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, top_k_poses_localref=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, grid_distance_degs=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, reference_map=None, reference_mask=None, directional_zscore_thr=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, skip_localrefinement=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, skip_reconstruction=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, subset_idxs=None, n_first_particles=None, show_debug_stats=False)[source]
Initializes the SingleInferencer for running inference on a set of particles.
- Parameters:
particles_star_fname (
str
) – Path to the STAR file containing particle information.checkpoint_dir (
str
) – Directory where the trained model checkpoints are stored.results_dir (
str
) – Directory where the inference results will be saved.data_halfset (
Literal
['half1'
,'half2'
,'allParticles'
]) – Specifies which half-set of the data to use (“half1”, “half2”, or “allParticles”).model_halfset (
Literal
['half1'
,'half2'
,'allCombinations'
,'matchingHalf'
]) – Specifies which half-set of the model to use (“half1”, “half2”, “allCombinations”, or “matchingHalf”).particles_dir (
Optional
[str
]) – Directory where the particle images are located. If None, paths in the STAR file are assumed to be absolute.batch_size (
int
) – The number of particles to process in each batch.num_dataworkers (
int
) – The number of worker processes to use for data loading.use_cuda (
bool
) – Whether to use a CUDA-enabled GPU for inference.n_cpus_if_no_cuda (
int
) – The number of CPU cores to use if CUDA is not available.compile_model (
bool
) – Whether to compile the model using torch.compile for potential speed-up.top_k_poses_nnet (
int
) – The number of top predictions to predict with the nn for each particle.top_k_poses_localref (
int
) – The number of top predictions to return after local refinement.grid_distance_degs (
float
) – The size of the local refinement grid in degrees. Grid will go from -grid_distance_degs to +grid_distance_degsreference_map (
Optional
[str
]) – Path to the reference map for local refinement. If not provided, it will be loaded from the checkpoint.reference_mask (
Optional
[str
]) – Path to the mask of the reference map. Used only for FSC calculation.directional_zscore_thr (
Optional
[float
]) – The threshold for the directional Z-score to filter particles.skip_localrefinement (
bool
) – Whether to skip local refinement of the particle poses.skip_reconstruction (
bool
) – Whether to skip 3D reconstruction from the inferred poses.subset_idxs (
Optional
[List
[int
]]) – A list of indices to process a subset of particles.n_first_particles (
Optional
[int
]) – The number of first particles to process. Cannot be used with subset_idxs.show_debug_stats (
bool
) – Whether to print debug statistics, such as rotation errors if ground truth in the starfile.
- property symmetry
The symmetry of the model, loaded from the hyperparameters file.
- run()[source]
Runs the inference process. It iterates through the specified data and model half-sets, performing inference for each combination.
- Returns:
A list of tuples, where each tuple contains the particle metadata and the reconstructed volume for each inference run.
- static resolve_halfset_lists(data_halfset, model_halfset)[source]
Return normalized lists of data and model-half policies for iteration. - data list is concrete halves: [“half1”], [“half2”], or [“half1”,”half2”]. - model list is [“half1”], [“half2”], [None] (matchingHalf), or [“half1”,”half2”] (allCombinations).
SingleInferencer Class
- class cryoPARES.inference.inferencer.SingleInferencer(particles_star_fname, checkpoint_dir, results_dir, data_halfset='allParticles', model_halfset='matchingHalf', particles_dir=None, batch_size=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, num_dataworkers=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, use_cuda=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, n_cpus_if_no_cuda=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, compile_model=False, top_k_poses_nnet=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, top_k_poses_localref=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, grid_distance_degs=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, reference_map=None, reference_mask=None, directional_zscore_thr=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, skip_localrefinement=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, skip_reconstruction=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, subset_idxs=None, n_first_particles=None, show_debug_stats=False)[source]
Bases:
object
- Parameters:
particles_star_fname (str)
checkpoint_dir (str)
results_dir (str)
data_halfset (Literal['half1', 'half2', 'allParticles'])
model_halfset (Literal['half1', 'half2', 'allCombinations', 'matchingHalf'])
particles_dir (str | None)
batch_size (int)
num_dataworkers (int)
use_cuda (bool)
n_cpus_if_no_cuda (int)
compile_model (bool)
top_k_poses_nnet (int)
top_k_poses_localref (int)
grid_distance_degs (float)
reference_map (str | None)
reference_mask (str | None)
directional_zscore_thr (float | None)
skip_localrefinement (bool)
skip_reconstruction (bool)
n_first_particles (int | None)
show_debug_stats (bool)
- __init__(particles_star_fname, checkpoint_dir, results_dir, data_halfset='allParticles', model_halfset='matchingHalf', particles_dir=None, batch_size=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, num_dataworkers=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, use_cuda=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, n_cpus_if_no_cuda=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, compile_model=False, top_k_poses_nnet=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, top_k_poses_localref=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, grid_distance_degs=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, reference_map=None, reference_mask=None, directional_zscore_thr=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, skip_localrefinement=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, skip_reconstruction=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, subset_idxs=None, n_first_particles=None, show_debug_stats=False)[source]
Initializes the SingleInferencer for running inference on a set of particles.
- Parameters:
particles_star_fname (
str
) – Path to the STAR file containing particle information.checkpoint_dir (
str
) – Directory where the trained model checkpoints are stored.results_dir (
str
) – Directory where the inference results will be saved.data_halfset (
Literal
['half1'
,'half2'
,'allParticles'
]) – Specifies which half-set of the data to use (“half1”, “half2”, or “allParticles”).model_halfset (
Literal
['half1'
,'half2'
,'allCombinations'
,'matchingHalf'
]) – Specifies which half-set of the model to use (“half1”, “half2”, “allCombinations”, or “matchingHalf”).particles_dir (
Optional
[str
]) – Directory where the particle images are located. If None, paths in the STAR file are assumed to be absolute.batch_size (
int
) – The number of particles to process in each batch.num_dataworkers (
int
) – The number of worker processes to use for data loading.use_cuda (
bool
) – Whether to use a CUDA-enabled GPU for inference.n_cpus_if_no_cuda (
int
) – The number of CPU cores to use if CUDA is not available.compile_model (
bool
) – Whether to compile the model using torch.compile for potential speed-up.top_k_poses_nnet (
int
) – The number of top predictions to predict with the nn for each particle.top_k_poses_localref (
int
) – The number of top predictions to return after local refinement.grid_distance_degs (
float
) – The size of the local refinement grid in degrees. Grid will go from -grid_distance_degs to +grid_distance_degsreference_map (
Optional
[str
]) – Path to the reference map for local refinement. If not provided, it will be loaded from the checkpoint.reference_mask (
Optional
[str
]) – Path to the mask of the reference map. Used only for FSC calculation.directional_zscore_thr (
Optional
[float
]) – The threshold for the directional Z-score to filter particles.skip_localrefinement (
bool
) – Whether to skip local refinement of the particle poses.skip_reconstruction (
bool
) – Whether to skip 3D reconstruction from the inferred poses.subset_idxs (
Optional
[List
[int
]]) – A list of indices to process a subset of particles.n_first_particles (
Optional
[int
]) – The number of first particles to process. Cannot be used with subset_idxs.show_debug_stats (
bool
) – Whether to print debug statistics, such as rotation errors if ground truth in the starfile.
- property symmetry
The symmetry of the model, loaded from the hyperparameters file.
- run()[source]
Runs the inference process. It iterates through the specified data and model half-sets, performing inference for each combination.
- Returns:
A list of tuples, where each tuple contains the particle metadata and the reconstructed volume for each inference run.
- static resolve_halfset_lists(data_halfset, model_halfset)[source]
Return normalized lists of data and model-half policies for iteration. - data list is concrete halves: [“half1”], [“half2”], or [“half1”,”half2”]. - model list is [“half1”], [“half2”], [None] (matchingHalf), or [“half1”,”half2”] (allCombinations).
Distributed Inference
- cryoPARES.inference.infer.distributed_inference(particles_star_fname, checkpoint_dir, results_dir, data_halfset='allParticles', model_halfset='matchingHalf', particles_dir=None, batch_size=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, n_jobs=None, num_dataworkers=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, use_cuda=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, n_cpus_if_no_cuda=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, compile_model=False, top_k_poses_nnet=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, top_k_poses_localref=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, grid_distance_degs=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, reference_map=None, reference_mask=None, directional_zscore_thr=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, skip_localrefinement=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, skip_reconstruction=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, subset_idxs=None, n_first_particles=None, check_interval_secs=2.0)[source]
Distributed inference across particles and devices, mirroring the halfset selection logic used by
cryoPARES.inference.inference.SingleInferencer
.- Parameters:
particles_star_fname (str) – Path to input RELION particles .star file
checkpoint_dir (str) – Path to training directory (or .zip file) containing half-set models with checkpoints and hyperparameters. By default they are called version_0, version_1, etc.
results_dir (str) – Output directory for inference results including predicted poses and optional reconstructions
data_halfset ({"half1", "half2", "allParticles"}, default "allParticles") – Which particle half-set(s) to process: “half1”, “half2”, or “allParticles”
model_halfset ({"half1", "half2", "allCombinations", "matchingHalf"}, default "matchingHalf") – Model half-set selection policy: “half1”, “half2”, “allCombinations”, or “matchingHalf” (uses matching data/model pairs)
particles_dir (str, optional) – Root directory for particle image paths. If provided, overrides paths in the .star file
batch_size (int) – Number of particles per batch for inference
n_jobs (int, optional) – Number of worker processes. Defaults to number of GPUs if CUDA enabled, otherwise 1
num_dataworkers (int) – Number of parallel data loading workers per GPU. Each worker is a separate CPU process. Set to 0 to load data in the main thread (useful only for debugging). Try not to oversubscribe by asking more workers than CPUs
use_cuda (bool) – Enable GPU acceleration for inference. If False, runs on CPU only
n_cpus_if_no_cuda (int) – Maximum CPU threads per worker when CUDA is disabled
compile_model (bool) – Compile model with torch.compile for faster inference (experimental, requires PyTorch 2.0+)
top_k_poses_nnet (int) – Number of top pose predictions to retrieve from neural network before local refinement
top_k_poses_localref (int) – Number of best matching poses to keep after local refinement
grid_distance_degs (float) – Maximum angular distance in degrees for local refinement search. Grid ranges from -grid_distance_degs to +grid_distance_degs around predicted pose
reference_map (str, optional) – Path to reference map (.mrc) for FSC computation during validation
reference_mask (str, optional) – Path to reference mask (.mrc) for masked FSC calculation
directional_zscore_thr (float, optional) – Confidence z-score threshold for filtering particles. Particles with scores below this are discarded as low-confidence
skip_localrefinement (bool) – Skip local pose refinement step and use only neural network predictions
skip_reconstruction (bool) – Skip 3D reconstruction step and output only predicted poses
subset_idxs (list[int], optional) – List of particle indices to process (for debugging or partial processing)
n_first_particles (int, optional) – Process only the first N particles from dataset (debug feature)
check_interval_secs (float) – Polling interval in seconds for parent loop in distributed processing
Notes
For n_jobs == 1, this function delegates completely to SingleInferencer, which internally handles data_halfset and model_halfset (including “matchingHalf” and “allCombinations”).
For n_jobs > 1, particles are split by half-set and distributed across workers; model half selection follows the resolved policy per half to avoid cross-half coupling.
Daemon Mode
Queue Manager
manager_server.py - Creates and manages the shared queue server. This script must be run first to establish the Manager server.
- class cryoPARES.inference.daemon.queueManager.QueueManager(address=None, authkey=None, serializer='pickle', ctx=None, *, shutdown_timeout=1.0)[source]
Bases:
BaseManager
Custom Manager class to handle our shared queue.
Factory function to create the shared queue.
- cryoPARES.inference.daemon.queueManager.connect_to_queue(ip='localhost', port=50000, authkey='shared_queue_key')[source]
Connect to the shared queue managed by the server.
- cryoPARES.inference.daemon.queueManager.queue_manager_server(ip, port, authkey, queue_maxsize)[source]
Context manager for the QueueManager server.
- cryoPARES.inference.daemon.queueManager.signal_handler(signum, frame)[source]
Handle Ctrl+C or SIGTERM gracefully.
- cryoPARES.inference.daemon.queueManager.queue_connection(ip='localhost', port=50000, authkey='shared_queue_key')[source]
Context manager for connecting to the shared queue.
Spooling Filler
spooler.py - Monitors a directory for new .star files and adds them to the shared queue for DaemonInferencer.
- cryoPARES.inference.daemon.spoolingFiller.monitor_directory(directory, pattern='*.star', interval=10)[source]
Monitor a directory for new files matching the pattern and yield their paths.
Daemon Inference
- class cryoPARES.inference.daemon.daemonInference.DaemonInferencer(checkpoint_dir, results_dir, net_address='localhost', net_port=50000, net_authkey='shared_queue_key', model_halfset='half1', particles_dir=None, batch_size=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, num_dataworkers=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, use_cuda=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, n_cpus_if_no_cuda=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, compile_model=False, top_k_poses_nnet=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, top_k_poses_localref=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, reference_map=None, reference_mask=None, directional_zscore_thr=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, skip_localrefinement=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, skip_reconstruction=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, show_debug_stats=False, secs_between_partial_results_written=5, resubmit_poison_pill=True)[source]
Bases:
SingleInferencer
- Parameters:
checkpoint_dir (str)
results_dir (str)
net_address (str)
net_port (int)
net_authkey (str | None)
model_halfset (Literal['half1', 'half2'])
particles_dir (str | None)
batch_size (int)
num_dataworkers (int)
use_cuda (bool)
n_cpus_if_no_cuda (int)
compile_model (bool)
top_k_poses_nnet (int)
top_k_poses_localref (int)
reference_map (str | None)
reference_mask (str | None)
directional_zscore_thr (float | None)
skip_localrefinement (bool)
skip_reconstruction (bool)
show_debug_stats (bool)
secs_between_partial_results_written (int)
resubmit_poison_pill (bool)
- __init__(checkpoint_dir, results_dir, net_address='localhost', net_port=50000, net_authkey='shared_queue_key', model_halfset='half1', particles_dir=None, batch_size=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, num_dataworkers=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, use_cuda=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, n_cpus_if_no_cuda=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, compile_model=False, top_k_poses_nnet=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, top_k_poses_localref=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, reference_map=None, reference_mask=None, directional_zscore_thr=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, skip_localrefinement=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, skip_reconstruction=<cryoPARES.configManager.inject_defaults.CONFIG_PARAM object>, show_debug_stats=False, secs_between_partial_results_written=5, resubmit_poison_pill=True)[source]
Initializes the DaemonInferencer for running inference on a set of particles from a queue.
- Parameters:
checkpoint_dir (
str
) – Directory where the trained model checkpoints are stored.results_dir (
str
) – Directory where the inference results will be saved.net_address (
str
) – Network address of the queue manager.net_port (
int
) – Network port of the queue manager.net_authkey (
Optional
[str
]) – Network authentication key for the queue manager.model_halfset (
Literal
['half1'
,'half2'
]) – Specifies which half-set of the model to use (“half1”, “half2”).particles_dir (
Optional
[str
]) – Directory where the particle images are located. If None, paths in the STAR file are assumed to be absolute.batch_size (
int
) – The number of particles to process in each batch.num_dataworkers (
int
) – The number of worker processes to use for data loading.use_cuda (
bool
) – Whether to use a CUDA-enabled GPU for inference.n_cpus_if_no_cuda (
int
) – The number of CPU cores to use if CUDA is not available.compile_model (
bool
) – Whether to compile the model using torch.compile for potential speed-up.top_k_poses_nnet (
int
) – The number of top predictions to predict with the nn for each particle.top_k_poses_localref (
int
) – The number of top predictions to return after local refinement.reference_map (
Optional
[str
]) – Path to the reference map for local refinement. If not provided, it will be loaded from the checkpoint.reference_mask (
Optional
[str
]) – Path to the mask of the reference map. Used only for FSC calculation.directional_zscore_thr (
Optional
[float
]) – The threshold for the directional Z-score to filter particles.skip_localrefinement (
bool
) – Whether to skip local refinement of the particle poses.skip_reconstruction (
bool
) – Whether to skip 3D reconstruction from the inferred poses.show_debug_stats (
bool
) – Whether to print debug statistics, such as rotation errors if ground truth in the starfile.secs_between_partial_results_written (
int
) – Partial results are saved from RAM to disk every few secondsresubmit_poison_pill (
bool
) – If True, posion pills are re-submitted, to ensure that all workers will die