### Install cellSAM Library Source: https://vanvalenlab.github.io/cellSAM/_sources/reproducibility.md.txt Install the cellSAM library from the parent directory after activating the virtual environment. ```bash pip install .. ``` -------------------------------- ### Manual Installation of Napari Dependencies Source: https://vanvalenlab.github.io/cellSAM/_sources/napari.md.txt Manually install napari and specific Qt bindings if finer-grained control is needed. ```bash pip install napari pyside2 magicgui ``` -------------------------------- ### Install CellSAM with Napari from Git Source: https://vanvalenlab.github.io/cellSAM/_sources/napari.md.txt Install cellSAM and its napari dependencies directly from a GitHub repository. ```bash pip install "cellSAM[napari] @ git+https://github.com/vanvalenlab/cellSAM@master" ``` -------------------------------- ### Install CellSAM with Napari from GitHub Source: https://vanvalenlab.github.io/cellSAM/napari.html Install the cellSAM package, including napari dependencies, directly from the GitHub repository. This command installs the latest version from the master branch. ```bash pip install "cellSAM[napari] @ git+https://github.com/vanvalenlab/cellSAM@master" ``` -------------------------------- ### Manually Install Napari Dependencies Source: https://vanvalenlab.github.io/cellSAM/napari.html Manually install napari and its required dependencies like PySide2 if you need finer control over Qt bindings. This method bypasses the automatic installation of optional dependencies. ```bash pip install napari pyside2 magicgui ``` -------------------------------- ### Install CellSAM with Napari Dependencies Source: https://vanvalenlab.github.io/cellSAM/_sources/napari.md.txt Install the cellSAM package with optional napari dependencies from a local source. ```bash pip install .[napari] ``` -------------------------------- ### Install CellSAM Source: https://vanvalenlab.github.io/cellSAM/_sources/index.rst.txt Install CellSAM using pip from its GitHub repository. This command fetches and installs the latest version of the library. ```bash pip install git+https://github.com/vanvalenlab/cellSAM.git ``` -------------------------------- ### Verify Empty Virtual Environment Source: https://vanvalenlab.github.io/cellSAM/reproducibility.html Check the installed packages in the newly created virtual environment. Initially, only pip should be listed. ```bash $ pip list Package Version ------- ------- pip 24.3.1 ``` -------------------------------- ### Install Evaluation Dependencies Source: https://vanvalenlab.github.io/cellSAM/_sources/reproducibility.md.txt Install the specific requirements for the evaluation suite using a requirements file. This may downgrade some existing packages. ```bash pip install -r requirements.txt ``` -------------------------------- ### Install CellSAM with Napari Dependencies from Source Source: https://vanvalenlab.github.io/cellSAM/napari.html Install the cellSAM package with optional napari dependencies when building from a local repository. This ensures all necessary components for the napari plugin are included. ```bash pip install .[napari] ``` -------------------------------- ### Unpack Evaluation Dataset with Parallel Decompression Source: https://vanvalenlab.github.io/cellSAM/_sources/reproducibility.md.txt Unpack the downloaded dataset archive, specifying a target directory and using parallel decompression for speed. This example uses 8 threads and unpacks to /data. ```bash tar --use-compress-program="unpigz -p 8" -xf $HOME/.deepcell/datasets/cellsam-data_v1.2.tar.gz -C /data ``` -------------------------------- ### Import CellSAM Libraries Source: https://vanvalenlab.github.io/cellSAM/_sources/tutorial.md.txt Imports necessary libraries for CellSAM, including image processing and visualization tools. Ensure these are installed before running. ```python import imageio.v3 as iio import numpy as np import scipy as sp import matplotlib.pyplot as plt import matplotlib.patches as patches from cellSAM import cellsam_pipeline, get_model from cellSAM.utils import format_image_shape, normalize_image ``` -------------------------------- ### Load and Segment Electron Microscopy Image Source: https://vanvalenlab.github.io/cellSAM/auto_examples/plot_electron_microscopy.html This snippet loads an electron microscopy image from a Zarr store, segments it using the cellsam_pipeline, and visualizes the original image and the segmentation mask using napari. It's useful for processing EM data where bacterial biofilms are present. Ensure zarr and napari are installed. ```python import zarr import napari from cellSAM import cellsam_pipeline # NOTE: data is stored with zarr_format 3 assert int(zarr.__version__[0]) > 2 # Access EM image store = zarr.storage.FsspecStore.from_url( "s3://cellsam-gallery-sample-data/sample-data.zarr", storage_options = {"anon": True}, read_only=True, ) z = zarr.open_group(store=store, mode="r") # Load EM image into local memory # Limit to lower-right quadrant to reduce CI computation load tilesize = 512 img = z["biofilm_electron_microscopy"][tilesize:, tilesize:] print(img.shape) # Segment mask = cellsam_pipeline(img, use_wsi=False) # Visualize nim = napari.Viewer() nim.add_image(img, name="EMimage"); nim.add_labels(mask, name="Cellsam segmentation"); if __name__ == "__main__": napari.run() ``` -------------------------------- ### Load and Segment H&E Image Source: https://vanvalenlab.github.io/cellSAM/auto_examples/plot_HandE.html This snippet loads an H&E image from sample data, converts it to grayscale, and performs segmentation using CellSAM. It then visualizes the original image and the segmentation mask using napari. Ensure zarr, skimage, and napari are installed. ```python import zarr import skimage import napari from cellSAM import cellsam_pipeline # NOTE: data is stored with zarr_format 3 assert int(zarr.__version__[0]) > 2 # Access H&E image store = zarr.storage.FsspecStore.from_url( "s3://cellsam-gallery-sample-data/sample-data.zarr", storage_options={"anon": True}, read_only=True, ) z = zarr.open_group(store=store, mode="r") # Load H&E image into local memory # Limit to upper-left quadrant to reduce CI computation load tilesize = 512 img = z["HBM248_QRTB_362"][:tilesize, :tilesize, :] print(img.shape) # NOTE: H&E images are often RGB - CellSAM expects RGB images to # be condensed to a single channel, as with `skimage.color.rgb2gray`, # for example mask = cellsam_pipeline(skimage.color.rgb2gray(img), use_wsi=False) nim = napari.Viewer() nim.add_image(img, name="H&E image"); nim.add_labels(mask, name="Cellsam segmentation"); if __name__ == "__main__": napari.run() ``` -------------------------------- ### Process and Visualize CODEX Image with cellSAM and napari Source: https://vanvalenlab.github.io/cellSAM/_downloads/29b944c4b286bab0bb395ec3ee6a3929/plot_multiplexed.ipynb This snippet loads a CODEX image, applies cellSAM for segmentation, and displays the original image and segmentation mask using napari. Ensure you have `imageio.v3`, `napari`, and `cellSAM` installed. The image is expected to be a 3-channel RGB PNG where specific channels represent nuclear and membrane stains. ```python import imageio.v3 as iio import napari from cellSAM import cellsam_pipeline img = iio.imread("../sample_imgs/tissuenet.png") # Image is 3-channel RGB where Channel 1 (G) represents a nuclear stain # and Channel 2 (B) a membrane stain. Channel 0 (R) is blank. print(img.sum(axis=(0, 1))) mask = cellsam_pipeline(img, use_wsi=False) nim = napari.Viewer() nim.add_image(img, name="CODEX image"); nim.add_labels(mask, name="Cellsam segmentation"); if __name__ == "__main__": napari.run() ``` -------------------------------- ### Run the CellSAM Napari Plugin Source: https://vanvalenlab.github.io/cellSAM/_sources/napari.md.txt Launch the napari application with the cellSAM plugin enabled from the command line. ```bash cellsam napari ``` -------------------------------- ### Run the Full Evaluation Suite Source: https://vanvalenlab.github.io/cellSAM/_sources/reproducibility.md.txt Execute the main evaluation script after ensuring model and dataset paths are correctly configured in the script. Results are saved to summary.csv. ```bash ./all_runs.sh ``` -------------------------------- ### Run All Evaluations Source: https://vanvalenlab.github.io/cellSAM/reproducibility.html Execute the main evaluation script 'all_runs.sh'. Ensure model and dataset paths at the top of the script are correctly configured. ```bash $ ./all_runs.sh ``` -------------------------------- ### Segment Yeast Cells with cellSAM and Visualize with Napari Source: https://vanvalenlab.github.io/cellSAM/_downloads/d65265cb414994074e6aa21044ba9394/plot_yeaz.ipynb This snippet imports necessary libraries, loads a yeast image, performs segmentation using the cellSAM pipeline, and displays the original image and segmentation mask in napari. It's designed for direct execution. ```python import imageio.v3 as iio import napari from cellSAM import cellsam_pipeline img = iio.imread("../sample_imgs/YeaZ.png") mask = cellsam_pipeline(img, use_wsi=False) nim = napari.Viewer() nim.add_image(img, name="YeaZ"); nim.add_labels(mask, name="Cellsam segmentation"); if __name__ == "__main__": napari.run() ``` -------------------------------- ### Download Pretrained Model Weights Source: https://vanvalenlab.github.io/cellSAM/_sources/reproducibility.md.txt Download the latest version of the pretrained model weights using the get_model function. Specify a version using the 'version=' keyword argument if needed. ```python from cellSAM import get_model get_model(); ``` -------------------------------- ### Create and Activate Python Virtual Environment Source: https://vanvalenlab.github.io/cellSAM/_sources/reproducibility.md.txt Use Python's built-in venv module to create an isolated environment for evaluation. Replace XX with your desired Python version. ```bash python3.XX -m venv cs-eval-env source cs-eval-env/bin/activate ``` -------------------------------- ### Initialize CellSAM Model and Predict Mask Source: https://vanvalenlab.github.io/cellSAM/tutorial.html Load the CellSAM model using `get_model` and predict a mask for a specified bounding box. The bounding box needs to be converted to (x1, x2, y1, y2) format, and the image preprocessed. ```python predictor = get_model() # We can pass the bounding boxes to the model prediction function x1, y1, w, h = box # Bounding-box prompts should have format (x1, x2, y1, y2), where (x1, y1) is # the lower-left corner of the box and (x2, y2) is the upper right x2, y2 = x1 + w, y1 + h # Preprocess the image im = format_image_shape(img) im = normalize_image(im) im = np.transpose(im, (2, 0, 1)) im = np.expand_dims(im, axis=0) pred_mask, *_ = predictor.predict(im, boxes_per_heatmap=[[[x1, y1, x2, y2]]]) ``` -------------------------------- ### Prompted Cell Segmentation with CellSAM Model Source: https://vanvalenlab.github.io/cellSAM/_sources/tutorial.md.txt Initializes the CellSAM model and performs segmentation using a provided bounding box. The image is preprocessed, and the bounding box is converted to the required format for the predictor. This is suitable for users needing to segment specific cells. ```python predictor = get_model() # We can pass the bounding boxes to the model prediction function x1, y1, w, h = box # Bounding-box prompts should have format (x1, x2, y1, y2), where (x1, y1) is # the lower-left corner of the box and (x2, y2) is the upper right x2, y2 = x1 + w, y1 + h # Preprocess the image im = format_image_shape(img) im = normalize_image(im) im = np.transpose(im, (2, 0, 1)) im = np.expand_dims(im, axis=0) pred_mask, *_ = predictor.predict(im, boxes_per_heatmap=[[[x1, y1, x2, y2]]]) ``` -------------------------------- ### Load and Prepare cells3d Dataset for CellSAM Source: https://vanvalenlab.github.io/cellSAM/reference/generated/cellSAM.cellsam_pipeline.cellsam_pipeline.html Loads the cells3d dataset and prepares a 2D slice for CellSAM by rearranging channels to the expected (blank, nuclear, membrane) format. Sets use_wsi to False for small images and skips preprocessing. ```python import numpy as np import skimage data = skimage.data.cells3d() data.shape img = data[30, ...] seg = np.zeros((*img.shape[1:], 3), dtype=img.dtype) seg[..., 1] = img[1, ...] # nuclear channel seg[..., 2] = img[0, ...] # membrane channel mask = cellsam_pipeline(seg, use_wsi=False) ``` -------------------------------- ### Segment Yeast Cells in Brightfield Image Source: https://vanvalenlab.github.io/cellSAM/auto_examples/plot_yeastnet.html Loads a brightfield microscopy image and performs cell segmentation using the cellSAM pipeline. Displays the original image and the segmentation mask using napari. ```python import imageio.v3 as iio import napari from cellSAM import cellsam_pipeline img = iio.imread("../sample_imgs/YeastNet.png") mask = cellsam_pipeline(img, use_wsi=False) nim = napari.Viewer() nim.add_image(img, name="Yeast Net"); nim.add_labels(mask, name="Cellsam segmentation"); if __name__ == "__main__": napari.run() ``` -------------------------------- ### Load, Segment, and Visualize EM Image with CellSAM Source: https://vanvalenlab.github.io/cellSAM/_downloads/8cc31c72f1608449ea86eafd868b99b1/plot_electron_microscopy.ipynb Loads an electron microscopy image from S3, segments it using CellSAM, and visualizes the original image and segmentation mask with napari. Requires zarr and napari libraries. The data is accessed via FsspecStore from an S3 URL. ```python import zarr import napari from cellSAM import cellsam_pipeline # NOTE: data is stored with zarr_format 3 assert int(zarr.__version__[0]) > 2 # Access EM image store = zarr.storage.FsspecStore.from_url( "s3://cellsam-gallery-sample-data/sample-data.zarr", storage_options={"anon": True}, read_only=True, ) z = zarr.open_group(store=store, mode="r") # Load EM image into local memory # Limit to lower-right quadrant to reduce CI computation load tilesize = 512 img = z["biofilm_electron_microscopy"][tilesize:, tilesize:] print(img.shape) # Segment mask = cellsam_pipeline(img, use_wsi=False) # Visualize nim = napari.Viewer() nim.add_image(img, name="EMimage"); nim.add_labels(mask, name="Cellsam segmentation"); if __name__ == "__main__": napari.run() ``` -------------------------------- ### Load and Segment Electron Microscopy Image Source: https://vanvalenlab.github.io/cellSAM/_sources/auto_examples/plot_electron_microscopy.rst.txt Loads an electron microscopy image from Zarr storage and segments it using the cellsam_pipeline. Requires zarr, napari, and cellSAM libraries. The image is downsampled to a tilesize of 512 for processing. ```Python import zarr import napari from cellSAM import cellsam_pipeline # NOTE: data is stored with zarr_format 3 assert int(zarr.__version__[0]) > 2 # Access EM image store = zarr.storage.FsspecStore.from_url( "s3://cellsam-gallery-sample-data/sample-data.zarr", storage_options = {"anon": True}, read_only=True, ) z = zarr.open_group(store=store, mode="r") # Load EM image into local memory # Limit to lower-right quadrant to reduce CI computation load tilesize = 512 img = z["biofilm_electron_microscopy"][tilesize:, tilesize:] print(img.shape) # Segment mask = cellsam_pipeline(img, use_wsi=False) # Visualize nim = napari.Viewer() nim.add_image(img, name="EMimage"); nim.add_labels(mask, name="Cellsam segmentation"); if __name__ == "__main__": napari.run() ``` -------------------------------- ### Visualize Bounding Box for Prompted Segmentation Source: https://vanvalenlab.github.io/cellSAM/_sources/tutorial.md.txt Displays the input image with a specified bounding box highlighted in red. This box will be used to prompt CellSAM for segmenting a specific cell. ```python # Here's the cell we want to segment! box = [290, 365, 60, 38] # x, y, w, h rect = patches.Rectangle( (box[0], box[1]), box[2], box[3], linewidth=1, edgecolor='r', facecolor='none' ) plt.figure(figsize=(5, 5)) plt.imshow(img, cmap='gray') plt.gca().add_patch(rect); ``` -------------------------------- ### Download CellSAM Training Data Source: https://vanvalenlab.github.io/cellSAM/API-key.html Use the `download_training_data` function from the cellSAM library to download the training dataset. Ensure you have sufficient disk space and network bandwidth as the dataset is approximately 14GB. ```python from cellSAM import download_training_data download_training_data() ``` -------------------------------- ### cellsam_pipeline Source: https://vanvalenlab.github.io/cellSAM/reference/index.html Runs the CellSAM inference pipeline on an input image. ```APIDOC ## cellsam_pipeline ### Description Runs the CellSAM inference pipeline on `img`. ### Method Not applicable (Python function) ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Parameters * **img** - The input image. * **chunks** - Optional parameter. * **model_path** - Optional parameter for specifying the model path. * **...** - Additional optional parameters. ### Request Example ```python cellsam_pipeline(img, chunks=None, model_path=None) ``` ### Response #### Success Response Details of the pipeline output. #### Response Example ```json { "output": "pipeline results" } ``` ``` -------------------------------- ### Load and Segment H&E Image with CellSAM Source: https://vanvalenlab.github.io/cellSAM/_downloads/5379285575e6ad68e34c5bfa6aa9d42d/plot_HandE.ipynb Loads an H&E image from a Zarr store, converts it to grayscale, segments cells using cellSAM, and visualizes the original image and segmentation mask with napari. Ensure zarr version is greater than 2. ```python import zarr import skimage import napari from cellSAM import cellsam_pipeline # NOTE: data is stored with zarr_format 3 assert int(zarr.__version__[0]) > 2 # Access H&E image store = zarr.storage.FsspecStore.from_url( "s3://cellsam-gallery-sample-data/sample-data.zarr", storage_options={"anon": True}, read_only=True, ) z = zarr.open_group(store=store, mode="r") # Load H&E image into local memory # Limit to upper-left quadrant to reduce CI computation load tilesize = 512 img = z["HBM248_QRTB_362"][:tilesize, :tilesize, :] print(img.shape) # NOTE: H&E images are often RGB - CellSAM expects RGB images to # be condensed to a single channel, as with `skimage.color.rgb2gray`, # for example mask = cellsam_pipeline(skimage.color.rgb2gray(img), use_wsi=False) nim = napari.Viewer() nim.add_image(img, name="H&E image"); nim.add_labels(mask, name="Cellsam segmentation"); if __name__ == "__main__": napari.run() ``` -------------------------------- ### Download Evaluation Dataset Source: https://vanvalenlab.github.io/cellSAM/_sources/reproducibility.md.txt Download the evaluation dataset using the download_training_data function. The data will be saved to $HOME/.deepcell/datasets/. ```python from cellSAM import download_training_data download_training_data() ``` -------------------------------- ### Image Data Summary Source: https://vanvalenlab.github.io/cellSAM/_sources/auto_examples/plot_multiplexed.rst.txt Prints the sum of pixel values for each channel of the input image. This helps in understanding the distribution of stain intensity across channels. ```none [ 0 18481553 27300386] ``` -------------------------------- ### Segment Mammalian Cells in Phase Microscopy Images Source: https://vanvalenlab.github.io/cellSAM/_downloads/c0d8ceb5926e42505157ef07cd6bbeb1/plot_ep_micro.ipynb Use this code to segment phase microscopy images of mammalian cells. It requires the imageio and napari libraries, along with the cellSAM pipeline. The output is visualized in a napari viewer. ```python import imageio.v3 as iio import napari from cellSAM import cellsam_pipeline img = iio.imread("../sample_imgs/ep_micro.png") mask = cellsam_pipeline(img, use_wsi=False) nim = napari.Viewer() nim.add_image(img, name="Phase microscopy image"); nim.add_labels(mask, name="Cellsam segmentation"); if __name__ == "__main__": napari.run() ``` -------------------------------- ### Automatic Cell Segmentation with CellSAM Pipeline Source: https://vanvalenlab.github.io/cellSAM/_sources/tutorial.md.txt Uses the `cellsam_pipeline` to automatically detect and segment all cells in an image. This function handles weight downloading and can utilize a GPU if available. It's recommended for biologists needing a quick segmentation. ```python # Load sample data and trim padding img = iio.imread("../sample_imgs/YeaZ.png")[8:-7, 8:-7] # Run inference pipeline mask = cellsam_pipeline( img, use_wsi=False, low_contrast_enhancement=False, gauge_cell_size=False ) # Visualize results plt.imshow(mask); ``` -------------------------------- ### cellsam_pipeline Source: https://vanvalenlab.github.io/cellSAM/_sources/reference/index.rst.txt The main pipeline for performing inference with CellSAM. This module orchestrates the various steps involved in cell segmentation and analysis. ```APIDOC ## cellsam_pipeline ### Description The main pipeline for performing inference with CellSAM. This module orchestrates the various steps involved in cell segmentation and analysis. ### Module `cellSAM.cellsam_pipeline` ``` -------------------------------- ### Set DEEPCELL_ACCESS_TOKEN Environment Variable Source: https://vanvalenlab.github.io/cellSAM/API-key.html Set your API token as an environment variable to authenticate with DeepCell services. This is typically added to your shell configuration file. ```bash export DEEPCELL_ACCESS_TOKEN= ``` -------------------------------- ### Visualize Prompted Segmentation Mask Source: https://vanvalenlab.github.io/cellSAM/_sources/tutorial.md.txt Superimposes the predicted segmentation mask as a red edge onto the original image to clearly visualize the segmented cell. This helps in verifying the accuracy of the prompted segmentation. ```python # Use several iterations to make the mask edge visible when plotting dilated_mask = sp.ndimage.binary_dilation(pred_mask > 0, iterations=5) edges = (dilated_mask > pred_mask).astype(np.uint8) # Plot the image plt.imshow(img, cmap="gray") # And the outlines from the mask plt.imshow(255 * edges, cmap="Reds", alpha=edges); ``` -------------------------------- ### cellsam_pipeline Function Source: https://vanvalenlab.github.io/cellSAM/_sources/reference/generated/cellSAM.cellsam_pipeline.cellsam_pipeline.rst.txt The `cellsam_pipeline` function orchestrates the cellSAM analysis workflow. ```APIDOC ## cellsam_pipeline ### Description Orchestrates the cellSAM analysis workflow. ### Signature `cellsam_pipeline()` ### Parameters This function does not accept any parameters. ### Returns This function does not explicitly return a value, but it executes the cellSAM pipeline. ``` -------------------------------- ### Define Bounding Box for Specific Cell Source: https://vanvalenlab.github.io/cellSAM/tutorial.html Define a bounding box (x, y, w, h) to specify a region of interest for segmenting a particular cell. This is useful for targeted segmentation. ```python # Here's the cell we want to segment! box = [290, 365, 60, 38] # x, y, w, h rect = patches.Rectangle( (box[0], box[1]), box[2], box[3], linewidth=1, edgecolor='r', facecolor='none' ) plt.figure(figsize=(5, 5)) plt.imshow(img, cmap='gray') plt.gca().add_patch(rect); ``` -------------------------------- ### CellSAM Citation Source: https://vanvalenlab.github.io/cellSAM/_sources/index.rst.txt BibTeX citation for the CellSAM paper. Use this in your academic work when referencing the CellSAM model. ```latex @article{israel2023foundation, title={A Foundation Model for Cell Segmentation}, author={Israel, Uriah and Marks, Markus and Dilip, Rohit and Li, Qilin and Schwartz, Morgan and Pradhan, Elora and Pao, Edward and Li, Shenyi and Pearson-Goulart, Alexander and Perona, Pietro and others}, journal={bioRxiv}, publisher={Cold Spring Harbor Laboratory Preprints}, doi = {10.1101/2023.11.17.567630}, } ``` -------------------------------- ### Visualize Predicted Cell Mask Source: https://vanvalenlab.github.io/cellSAM/tutorial.html Superimpose the predicted mask as an edge onto the original image to visualize the segmentation results clearly. This involves dilating the mask to create visible edges. ```python # Use several iterations to make the mask edge visible when plotting dilated_mask = sp.ndimage.binary_dilation(pred_mask > 0, iterations=5) edges = (dilated_mask > pred_mask).astype(np.uint8) # Plot the image plt.imshow(img, cmap="gray") # And the outlines from the mask plt.imshow(255 * edges, cmap="Reds", alpha=edges); ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.