Snippets
Print all runs and objects in a copick project
"""Print all objects and runs in a copick project."""
import copick
# Initialize the root object from a configuration file
root = copick.from_file("path/to/config.json")
# List all available objects
obj_info = [(o.name, o.label) for o in root.objects.values()]
print("Pickable objects in this project:")
for name, label in obj_info:
print(f" {name}: {label}")
# Execute a function on each run in the project
runs = root.runs
print("Runs in this project:")
for run in runs:
print(f"Run: {run.name}")
# Do something with the run
Read an object's density map into a numpy array
"""Read a density map from an object's zarr-store into a numpy array."""
import copick
import numpy as np
import zarr
# Initialize the root object from a configuration file
root = copick.from_file("path/to/config.json")
# Get the object named 'proteasome'
proteasome = root.get_object("proteasome")
# Read the density map for the object from its zarr-store
zarr_array = zarr.open(proteasome.zarr())["0"]
density_map = np.array(zarr_array)
Print all tomograms in a copick voxel spacing
"""Print the list of tomograms for a given voxel spacing."""
import copick
# Initialize the root object from a configuration file
root = copick.from_file("path/to/config.json")
# Get the run named 'TS_001'
run = root.get_run("TS_001")
# Get the voxel spacing with a resolution of 10 angstroms
voxel_spacing = run.get_voxel_spacing(10.000)
# List all available tomograms for the voxel spacing
tomograms = voxel_spacing.tomograms
for tomogram in tomograms:
print(f"Tomogram: {tomogram.name}")
Read a tomogram into a numpy array
"""Read a tomogram from a zarr-store into a numpy array."""
import copick
import numpy as np
import zarr
# Initialize the root object from a configuration file
root = copick.from_file("path/to/config.json")
# Get the run named 'TS_001'
run = root.get_run("TS_001")
# Get the voxel spacing with a resolution of 10 angstroms
voxel_spacing = run.get_voxel_spacing(10.000)
# Get the tomogram named 'wbp'
tomogram = voxel_spacing.get_tomogram("wbp")
# Read the tomogram from its zarr-store
# Scale "0" is the unbinned tomogram
zarr_array = zarr.open(tomogram.zarr())["0"]
tomogram_data = np.array(zarr_array)
# Scale "1" is the tomogram binned by 2
zarr_array_bin2 = zarr.open(tomogram.zarr())["1"]
tomogram_data_bin2 = np.array(zarr_array_bin2)
Read a feature array into a numpy array
"""Read a feature map from a zarr-store into a numpy array."""
import copick
import numpy as np
import zarr
# Initialize the root object from a configuration file
root = copick.from_file("path/to/config.json")
# Get the run named 'TS_001'
run = root.get_run("TS_001")
# Get the voxel spacing with a resolution of 10 angstroms
voxel_spacing = run.get_voxel_spacing(10.000)
# Get the tomogram named 'wbp'
tomogram = voxel_spacing.get_tomogram("wbp")
# Get the feature map named 'sobel'
feature_map = tomogram.get_features("sobel")
# Read the feature map from its zarr-store
zarr_array = zarr.open(feature_map.zarr())["0"]
feature_map_data = np.array(zarr_array)
Read copick picks into a numpy array
"""Read points from a CopickPicks object."""
import copick
import numpy as np
# Initialize the root object from a configuration file
root = copick.from_file("path/to/config.json")
# Get the first run in the project
run = root.runs[0]
# Get 'proteasome' picks of user 'alice'
picks = run.get_picks(object_name="proteasome", user_id="alice")[0]
# Get the points from the picks
point_arr = np.ndarray((len(picks.points), 3))
for idx, pt in enumerate(picks.points):
point_arr[idx, :] = [pt.location.x, pt.location.y, pt.location.z]
Print all segmentations in a copick run
"""Print all segmentations for a run in a copick project."""
import copick
# Initialize the root object from a configuration file
root = copick.from_file("path/to/config.json")
# Get the run named 'TS_001'
run = root.get_run("TS_001")
# List all available segmentations for the run
segmentations = run.segmentations
for segmentation in segmentations:
print(f"Segmentation: {segmentation.name}")
Read a segmentation into a numpy array
"""Read a segmentation from a CopickSegmentation object."""
import copick
import numpy as np
import zarr
# Initialize the root object from a configuration file
root = copick.from_file("path/to/config.json")
# Get the first run in the project
run = root.runs[0]
# Get 'proteasome' segmentation of user 'alice'
segmentation = run.get_segmentations(object_name="proteasome", user_id="alice")[0]
# Get the segmentation array from the segmentation
seg_zarr = zarr.open(segmentation.zarr())["0"]
seg = np.array(seg_zarr)
Read a mesh from a copick run
"""Read a mesh from a CopickMesh object and display it."""
import copick
# Initialize the root object from a configuration file
root = copick.from_file("path/to/config.json")
# Get the first run in the project
run = root.runs[0]
# Get a membrane mesh from user 'bob'
mesh = run.get_meshes(object_name="membrane", user_id="bob")[0]
# Show the mesh
mesh.mesh.show()