workspace#

Defines the Workspace class for managing workspaces in CryoSPARC.

Use cs.find_workspace() or project.find_workspace() to get a WorkspaceController instance.

Use project.create_workspace() to create a new workspace in a project.

Classes:

WorkspaceController

Accessor class to a workspace in CryoSPARC with ability create jobs and save results.

class WorkspaceController#

Accessor class to a workspace in CryoSPARC with ability create jobs and save results. Should be initialized with cs.find_workspace() or project.find_workspace().

Parameters:

workspace (tuple[str, str] | Workspace) – either _(Project UID, Workspace UID)_ tuple or Workspace model, e.g. ("P3", "W4")

model#

All workspace data from the CryoSPARC database. Contents may change over time, use refresh() to update.

Type:

Workspace

Attributes:

desc

Workspace description

project_uid

Project unique ID, e.g., "P3"

title

Workspace title

uid

Workspace unique ID, e.g., "W42"

Methods:

__init__(cs, workspace)

create_external_job([title, desc])

Add a new External job to the workspace to save computed outputs to.

create_job(type[, connections, params, ...])

Add a new job with the given type to the workspace.

delete(*[, wait])

Delete the workspace and any jobs exclusively in the workspace.

find_job(job_uid)

Get a job in the workspace by its unique ID.

find_jobs(*[, order])

Search jobs in the workspace.

import_job(path, *[, wait])

Import a job into the workspace from a location on disk.

link_job(job)

Link a job into this workspace.

refresh()

Reload this workspace from the CryoSPARC database.

save_external_result(dataset, type[, name, ...])

Save the a result dataset to a workspace, via External Job.

set_description(desc)

Set workspace description.

set_title(title)

Set workspace title.

unlink_job(job)

Unlink a job from this workspace.

__init__(cs: CryoSPARC, workspace: Tuple[str, str] | Workspace | Session) → None#
create_external_job(title: str = '', desc: str = '') → ExternalJobController#

Add a new External job to the workspace to save computed outputs to.

Parameters:
  • workspace_uid (str) – Workspace UID to create job in, e.g., “W1”

  • title (str, optional) – Title for external job (recommended). Defaults to “”.

  • desc (str, optional) – Markdown description for external job. Defaults to “”.

Returns:

created external job instance

Return type:

ExternalJobController

create_job(type: str, connections: Dict[str, Tuple[str | JobController, str] | List[Tuple[str | JobController, str]]] = {}, params: Dict[str, Any] = {}, title: str = '', desc: str = '') → JobController#

Add a new job with the given type to the workspace.

All available job types and associated metadata are available from cs.job_register.

Parameters:
  • project_uid (str) – Project UID to create job in, e.g., “P3”

  • workspace_uid (str) – Workspace UID to create job in, e.g., “W1”

  • type (str) – Job type identifier, e.g., “homo_abinit”

  • connections (dict[str, tuple[str | JobController, str] | list[tuple[str | JobController, str]]]) – Initial input connections. Each key is an input name and each value is a (job, output name) tuple. Defaults to {}

  • params (dict[str, Any], optional) – Specify parameter values. Defaults to {}.

  • title (str, optional) – Job title. Defaults to “”.

  • desc (str, optional) –

    Job Markdown description. Defaults to “”.

Returns:

created job instance.

Return type:

JobController

Raises:

APIError – Job cannot be created.

Examples

Create an Import Movies job.

>>> from cryosparc.tools import CryoSPARC
>>> cs = CryoSPARC("http://localhost:61000")
>>> workspace = cs.find_workspace("P3", "W3")
>>> import_job = workspace.create_job("W1", "import_movies")
>>> import_job.set_param("blob_paths", "/bulk/data/t20s/*.tif")
True

Create a 3-class ab-initio job connected to existing particles.

>>> abinit_job = workspace.create_job("homo_abinit"
...     connections={"particles": ("J20", "particles_selected")}
...     params={"abinit_K": 3}
... )
delete(*, wait: bool = False)#

Delete the workspace and any jobs exclusively in the workspace.

Workspace jobs with the following conditions are never deleted: - Linked to other workspaces (unlinked instead) - Have “final” or “ancestor of final” status - Have descendants in other workspaces.

If the workspace cannot be emptied due to these conditions, it is not marked as deleted.

Parameters:

wait (bool, optional) – If True, wait for the delete operation to complete before returning. Defaults to False.

Raises:

WorkspaceError – If the workspace could not be deleted. See cryosparc log api for details.

property desc: str | None#

Workspace description

find_job(job_uid: str) → JobController#

Get a job in the workspace by its unique ID.

Parameters:

job_uid (str) – Job UID to find, e.g., “J42”

Returns:

job accessor object

Return type:

JobController

find_jobs(*, order: Literal[1, -1] = 1, **search: Unpack[JobSearch]) → Iterable[JobController]#

Search jobs in the workspace.

Example

>>> jobs = workspace.find_jobs()  # all jobs in workspace
>>> jobs = workspace.find_jobs(
...     type="homo_reconstruct",
...     completed_at=(datetime(2025, 3, 1), datetime(2025, 3, 31)),
...     order=-1,
... )
>>> for job in jobs:
...     print(job.uid)
Parameters:
  • order (int, optional) – Sort order for resulting jobs, 1 for ascending, -1 for descending. Defaults to 1.

  • **search (JobSearch) – Additional search parameters to filter jobs, specified as keyword arguments.

Returns:

job accessor objects

Return type:

Iterable[JobController]

import_job(path: str | PurePosixPath, *, wait: bool = False) → JobController#

Import a job into the workspace from a location on disk.

The exported job directory must be copied into the target project directory with all its symbolic links resolved. By convention, the exported job directory should be located in the project directory → imports subfolder.

The resulting job will be in an “importing” state until CryoSPARC verifies its contents and outputs. Set wait=True to block until the job is ready to use.

Parameters:
  • path (str | Path) – Path to job directory, must be in the project directory. If the CryoSPARC instance is hosted remotely, this should be a path available on the server file system. e.g., "/projects/CS-project/imports/jobs/J134_homo_abinit" or "imports/jobs/J134_homo_abinit"

  • wait (bool, optional) – If True, wait until job import is complete before returning. Defaults to False.

Raises:
  • APIError – Job cannot be imported.

  • JobError – Job import failed. See cryosparc log api for details.

Link a job into this workspace.

Parameters:

job (str | JobController) – Target job to link into this workspace. Can specify by UID, or with a JobController instance, e.g., from project.find_job().

Raises:

APIError – If the job cannot be linked, e.g. if it is already linked to this workspace.

project_uid: str#

Project unique ID, e.g., “P3”

refresh()#

Reload this workspace from the CryoSPARC database.

Returns:

self

Return type:

WorkspaceController

save_external_result(dataset: Dataset[R], type: Literal['exposure', 'particle', 'template', 'volume', 'volume_multi', 'mask', 'live', 'ml_model', 'symmetry_candidate', 'flex_mesh', 'flex_model', 'hyperparameter', 'denoise_model', 'annotation_model'], name: str | None = None, slots: List[str | Slot | Datafield] | None = None, passthrough: Tuple[str, str] | None = None, title: str = '', desc: str = '', image: str | PurePath | IO[bytes] | Any | None = None, savefig_kw: dict = {'bbox_inches': 'tight', 'pad_inches': 0}) → str#

Save the a result dataset to a workspace, via External Job. Specify at least the dataset to save and the type of data.

Parameters:
  • dataset (Dataset) – Result dataset.

  • type (Datatype) – Type of output dataset.

  • name (str, optional) – Name of output on created External job. Same as type if unspecified. Defaults to None.

  • slots (list[SlotSpec], optional) – List of slots expected to be created for this output such as location or blob. Do not specify any slots that were passed through from an input unless those slots are modified in the output. Defaults to None.

  • passthrough (tuple[str, str], optional) – Indicates that this output inherits slots from the specified output. e.g., ("J1", "particles"). Defaults to None.

  • title (str, optional) – Human-readable title for this output. Defaults to “”.

  • desc (str, optional) –

    Markdown description for this output. Defaults to “”.

  • image (str | Path | IO | Figure, optional) – Optional image file or matplotlib Figure to set as the image for this output. Defaults to None.

  • savefig_kw (dict, optional) – Additional keyword arguments to pass to figure.savefig() when saving matplotlib Figures. Defaults to dict(bbox_inches="tight", pad_inches=0).

Returns:

UID of External job where output was saved

Return type:

str

Examples

Save all particle data

>>> particles = Dataset()
>>> workspace.save_external_result(particles, 'particle')
"J43"

Save new particle locations that inherit passthrough slots from a parent job

>>> particles = Dataset()
>>> workspace.save_external_result(
...     dataset=particles,
...     type='particle',
...     name='particles',
...     slots=['location'],
...     passthrough=('J42', 'selected_particles'),
...     title='Re-centered particles'
... )
"J44"

Save a result with multiple slots of the same type.

>>> workspace.save_external_result(
...     dataset=particles,
...     type="particle",
...     name="particle_alignments",
...     slots=[
...         {"dtype": "alignments3D", "prefix": "alignments_class_0", "required": True},
...         {"dtype": "alignments3D", "prefix": "alignments_class_1", "required": True},
...         {"dtype": "alignments3D", "prefix": "alignments_class_2", "required": True},
...     ]
... )
"J45"
set_description(desc: str)#

Set workspace description. May include Markdown formatting.

Parameters:

desc (str) – New workspace description

set_title(title: str)#

Set workspace title.

Parameters:

title (str) – New workspace title

property title: str | None#

Workspace title

uid: str#

Workspace unique ID, e.g., “W42”

Unlink a job from this workspace.

Parameters:

job (str | JobController) – Target job to unlink from this workspace. Can specify by UID, or with a JobController instance, e.g., from find_job() or create_job().

Raises:

APIError – If the job is not linked to the workspace, or if this is the only workspace the job is linked to.