Skip to content

Commit

Permalink
Merge pull request #999 from JunxiangXu/suite_dev
Browse files Browse the repository at this point in the history
image_register&segment
  • Loading branch information
jiajic authored Jul 31, 2024
2 parents 8f982e9 + 11efdd9 commit bb14457
Show file tree
Hide file tree
Showing 15 changed files with 1,019 additions and 1 deletion.
5 changes: 5 additions & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export(binSpect)
export(binSpectMulti)
export(binSpectSingle)
export(binSpectSingleMatrix)
export(calculateAffineMatrixFromLandmarks)
export(calculateHVF)
export(calculateMetaTable)
export(calculateMetaTableCells)
Expand Down Expand Up @@ -181,6 +182,7 @@ export(doLouvainSubCluster)
export(doRandomWalkCluster)
export(doSNNCluster)
export(doScrubletDetect)
export(estimateAutomatedImageRegistrationWithSIFT)
export(estimateImageBg)
export(exportGiottoViewer)
export(exprCellCellcom)
Expand Down Expand Up @@ -271,6 +273,7 @@ export(insertCrossSectionFeatPlot3D)
export(insertCrossSectionSpatPlot3D)
export(installGiottoEnvironment)
export(instructions)
export(interactiveLandmarkSelection)
export(jackstrawPlot)
export(joinGiottoObjects)
export(loadGiotto)
Expand All @@ -291,6 +294,7 @@ export(overlapToMatrix)
export(overlapToMatrixMultiPoly)
export(overlaps)
export(pDataDT)
export(performCellposeSegmentation)
export(pieCellTypesFromEnrichment)
export(plotCCcomDotplot)
export(plotCCcomHeatmap)
Expand Down Expand Up @@ -328,6 +332,7 @@ export(plotUMAP)
export(plotUMAP_2D)
export(plotUMAP_3D)
export(polyStamp)
export(preprocessImageToMatrix)
export(print.combIcfObject)
export(print.icfObject)
export(processGiotto)
Expand Down
134 changes: 134 additions & 0 deletions R/cell_segmentation.R
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,137 @@ doCellSegmentation <- function(

print(segmentation_result)
}





#'
#' @title perform cellpose segmentation
#' @description
#'
#' perform the Giotto Wrapper of cellpose segmentation. This is for a model inference to generate segmentation mask file from input image.
#' main parameters needed
#' @name performCellposeSegmentation
#' @param image_dir character, required. Provide a path to a gray scale or a three channel image.
#' @param python_path python environment with cellpose installed. default = "giotto_cellpose".
#' @param mask_output required. Provide a path to the output mask file.
#' @param channel_1 channel number for cytoplasm, default to 0(gray scale)
#' @param channel_2 channel number for Nuclei, default to 0(gray scale)
#' @param model_name Name of the model to run inference. Default to 'cyto3', if you want to run cutomized trained model, place your model file in ~/.cellpose/models and specify your model name.
#' @param batch_size Cellpose Parameter, Number of 224x224 patches to run simultaneously on the GPU. Can make smaller or bigger depending on GPU memory usage. Defaults to 8.
#' @param resample Cellpose Parameter
#' @param channel_axis Cellpose Parameter
#' @param z_axis Cellpose Parameter
#' @param normalize Cellpose Parameter
#' @param invert Cellpose Parameter
#' @param rescale Cellpose Parameter
#' @param diameter Cellpose Parameter
#' @param flow_threshold Cellpose Parameter
#' @param cellprob_threshold Cellpose Parameter
#' @param do_3D Cellpose Parameter
#' @param anisotropy Cellpose Parameter
#' @param stitch_threshold Cellpose Parameter
#' @param min_size Cellpose Parameter
#' @param niter Cellpose Parameter
#' @param augment Cellpose Parameter
#' @param tile Cellpose Parameter
#' @param tile_overlap Cellpose Parameter
#' @param bsize Cellpose Parameter
#' @param interp Cellpose Parameter
#' @param compute_masks Cellpose Parameter
#' @param progress Cellpose Parameter
#' @returns No return variable, as this will write directly to output path provided.
#' @examples
#' # example code
#' performCellposeSegmentation(image_dir = input_image, mask_output = output, channel_1 = 2, channel_2 = 1, model_name = 'cyto3',batch_size=4)
#' @export
performCellposeSegmentation <- function(python_env = 'giotto_cellpose',
image_dir,
mask_output,
channel_1 = 0,
channel_2 = 0,
model_name = 'cyto3',
batch_size=8,
resample=TRUE,
channel_axis=NULL,
z_axis=NULL,
normalize=TRUE,
invert=FALSE,
rescale=NULL,
diameter=NULL,
flow_threshold=0.4,
cellprob_threshold=0.0,
do_3D=FALSE,
anisotropy=NULL,
stitch_threshold=0.0,
min_size=15,
niter=NULL,
augment=FALSE,
tile=TRUE,
tile_overlap=0.1,
bsize=224,
interp=TRUE,
compute_masks=TRUE,
progress=NULL,
verbose = TRUE,...){


#Check Input arguments
model_name <- match.arg(model_name, unique(c('cyto3', 'cyto2', 'cyto','nuclei', model_name)))
## Load required python libraries
GiottoClass::set_giotto_python_path(python_env)
GiottoUtils::package_check('cellpose',repository = 'pip')

cellpose <- reticulate::import("cellpose")
np <- reticulate::import("numpy")
cv2 <- reticulate::import("cv2")
torch <- reticulate::import("torch")
message('successfully loaded giotto environment with cellpose.')

if (!(torch$cuda$is_available())){
warning('GPU is not available for this session, inference may be slow.\n ')
}

GiottoUtils::vmsg(.v = verbose, .is_debug = F,'Loading Image from ',image_dir)

img <- cellpose$io$imread(image_dir)
GiottoUtils::vmsg(.v = verbose, .is_debug = F,'Loading Model...')

model_to_seg <- cellpose$models$Cellpose(model_type=model_name,gpu = torch$cuda$is_available())
channel_to_seg <- as.integer(c(channel_1,channel_2))

GiottoUtils::vmsg(.v = verbose, .is_debug = F,'Segmenting Image...')
segmentation <- model_to_seg$eval

result <- segmentation(img,
diameter=diameter,
channels=channel_to_seg,
batch_size = batch_size,
resample=resample,
channel_axis=channel_axis,
z_axis=z_axis,
normalize=normalize,
invert=invert,
rescale=rescale,
flow_threshold=flow_threshold,
cellprob_threshold=cellprob_threshold,
do_3D=do_3D,
anisotropy=anisotropy,
stitch_threshold=stitch_threshold,
min_size=min_size,
niter=niter,
augment=augment,
tile=tile,
tile_overlap=tile_overlap,
bsize=bsize,
interp=interp,
compute_masks=compute_masks,
progress=progress)
masks <- result[[1]]
GiottoUtils::vmsg(.v = verbose, .is_debug = F,'Segmentation finished... Saving mask file...')
GiottoUtils::package_check('terra')
rast = terra::rast(masks)
terra::writeRaster(rast, mask_output,overwrite=TRUE)
}

Loading

0 comments on commit bb14457

Please sign in to comment.