5. Generate High-Res 2D Classes#

This example covers how to generate full-resolution 2D Class images for publication. The results are saved as PNG and PDF format.

First initialize the CryoSPARC client.

from cryosparc.tools import CryoSPARC

cs = CryoSPARC(host="cryoem5", base_port=40000)
assert cs.test_connection()
Connection succeeded to CryoSPARC command_core at http://cryoem5:40002
Connection succeeded to CryoSPARC command_vis at http://cryoem5:40003

Load the templates output from the Select 2D job with the final selected templates.

project = cs.find_project("P251")
job = project.find_job("J10")
templates_selected = job.load_output("templates_selected", slots=["blob"])

This job has 10 selected templates. Each template is stored in a MRC file created by the 2D Classification job. The templates_selected contains the location of these. Load all the unique paths (organized by path in a Python dictionary).

unique_mrc_paths = set(templates_selected["blob/path"])
all_templates_blobs = {path: project.download_mrc(path)[1] for path in unique_mrc_paths}

Use matplotlib to create a 5x2 grid to display these templates. Load the MRC template image data for each template. Particles for this dataset were extracted at a box size of 386px. Use a DPI slightly higher than this to allow for margins.

%matplotlib inline

from pathlib import Path
import matplotlib.pyplot as plt

fig = plt.figure(figsize=(5, 3), dpi=400)
plt.margins(x=0, y=0)

for i, template in enumerate(templates_selected.rows()):
    path = template["blob/path"]
    index = template["blob/idx"]
    blob = all_templates_blobs[path][index]
    plt.subplot(3, 5, i + 1)
    plt.axis("off")
    plt.imshow(blob, cmap="gray", origin="lower")

fig.tight_layout(pad=0, h_pad=0.4, w_pad=0.4)
fig.savefig(Path.home() / "class2d.png", bbox_inches="tight", pad_inches=0)
fig.savefig(Path.home() / "class2d.pdf", bbox_inches="tight", pad_inches=0)
../_images/a4273631b49e8b990d33259d5c996abff751fe481cbf045352bad193bccb7b67.png

The resulting output may also be be appended to the job’s event log.

job.log_plot(fig, text="2D Classes", raw_data="Hello,Templates", raw_data_format="txt")
'6396a9188ab506cc3b75ddb1'