Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Python based (no gui) way of estimating diameters #1073

Closed
mat10d opened this issue Dec 6, 2024 · 3 comments
Closed

Python based (no gui) way of estimating diameters #1073

mat10d opened this issue Dec 6, 2024 · 3 comments

Comments

@mat10d
Copy link

mat10d commented Dec 6, 2024

I would like to replicate the diameter calibration outside of the GUI, for the pretrained models provided. I am running:

def estimate_cell_diameters(data, channels=[2,3], gpu=False):
    """Estimate diameter directly using Cellpose model's size estimator"""
    model = Cellpose(model_type="cyto3", gpu=gpu)
    diams, _ = model.sz.eval(data, channels=channels)
    return np.maximum(5.0, diams)

This is based on the calibrate size function in the GUI, found here. Is this a reliable way of getting optimal diameters in a purely python way?

Furthermore, every time I run this function, I get different calibrated diameters. Is this expected?

@carsen-stringer
Copy link
Member

that is correct and should be deterministic, can you please post what is written from the cellpose logger?

@mat10d
Copy link
Author

mat10d commented Feb 7, 2025

Sorry for not updating--this works! I think I was saving the diameters incorrectly.

def estimate_diameters(
    data,
    dapi_index,
    cyto_index,
    channels=[2, 3],  # Default channels for cell estimation
    cyto_model="cyto3",
    cellpose_kwargs=dict(flow_threshold=0.4, cellprob_threshold=0),
    gpu=False,
    logscale=True,
):
    """Estimate optimal cell diameter using Cellpose's diameter estimation.

    Args:
        data (numpy.ndarray): Multichannel image data
        dapi_index (int): Index of DAPI channel
        cyto_index (int): Index of cytoplasmic channel
        channels (list): Channel indices for diameter estimation [cytoplasm, nuclei]
        cyto_model (str): Cellpose model type to use
        cellpose_kwargs (dict): Additional keyword arguments for Cellpose
        gpu (bool): Whether to use GPU
        logscale (bool): Whether to apply log scaling to image

    Returns:
        tuple: Estimated diameters for (nuclei, cells)
    """
    # Prepare RGB image
    log_kwargs = cellpose_kwargs.pop(
        "log_kwargs", dict()
    )  # Extract log_kwargs from cellpose_kwargs
    rgb = prepare_cellpose(
        data, dapi_index, cyto_index, logscale, log_kwargs=log_kwargs
    )

    # Find optimal nuclei diameter
    print("Estimating nuclei diameters...")
    model_nuclei = Cellpose(model_type="nuclei", gpu=gpu)
    diam_nuclear, _ = model_nuclei.sz.eval(rgb, channels=[3, 0])
    diam_nuclear = np.maximum(5.0, diam_nuclear)
    diam_nuclear = float(diam_nuclear)
    print(f"Estimated nuclear diameter: {diam_nuclear:.1f} pixels")

    # Find optimal cell diameter
    print("Estimating cell diameters...")
    model_cyto = Cellpose(model_type=cyto_model, gpu=gpu)
    diam_cell, _ = model_cyto.sz.eval(rgb, channels=channels)
    diam_cell = np.maximum(5.0, diam_cell)
    diam_cell = float(diam_cell)
    print(f"Estimated cell diameter: {diam_cell:.1f} pixels")

    return diam_nuclear, diam_cell

@mat10d mat10d closed this as completed Feb 7, 2025
@carsen-stringer
Copy link
Member

phew thanks for letting me know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants