Programmatic class selection in select_2D via cryosparc-tools?

I’m trying to automate select_2D jobs using cryosparc-tools (v5 API). The goal is to script which 2D class averages are retained without manual interaction in the GUI.

Here’s what I’ve attempted so far:

from cryosparc.tools import CryoSPARC

cs = CryoSPARC(“http://myhost:39000”)
project = cs.find_project(“P310”)

job = project.create_job(“W1”, “select_2D”)
job.connect(“particles”, “J99”, “particles_selected”)
job.connect(“templates”, “J99”, “templates_selected”)
job.set_param(“selected_templates”, [0, 2, 5, 8]) # 0-based class indices to keep
job.queue(hostname=“worker-node”)

Upon execution of this code, the Select2D job is created with both inputs (particles and templates) connected. However, when the job runs it still opens the interactive class selection interface with nothing pre-selected, effectively ignoring the param.

Is there a supported way to run select_2D non-interactively with pre-defined class selections? This would be very useful for batch processing pipelines where class assignments are already known.

Thanks in advance,

Mike

Short version: there is no supported way to run select_2D non-interactively. The job has no parameter that pre-selects class indices, the only params that make it non-interactive are the two Auto-Threshold cutoffs (resolution / particle count), which can’t express an arbitrary list like [0, 2, 5, 8].

The fix is to skip select_2D entirely. Any 2D-classified particle set already stores its 0-based class assignment in alignments2D/class, so you can just load the classification job’s particles, keep the ones whose class is in your list, and write the subset as an External job.

This is what I ended up using:

keep = [0, 2, 5, 8]
mask = np.isin(particles[“alignments2D/class”], keep)
kept = particles.mask(mask)
print(f"Keeping {len(kept)} / {len(particles)} particles from classes {keep}")

project.save_external_result(
workspace_uid=“W1”,
dataset=kept,
type=“particle”,
name=“particles_selected”,
slots=[“blob”, “alignments2D”],
passthrough=(src.uid, “particles”),
title=f"Select 2D classes {keep}",
)