Skip to content

Commit

Permalink
Examples/segment blockwise script (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
rhoadesScholar authored Jul 10, 2024
2 parents a2d5750 + b339972 commit f46735c
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 2 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ tmp/
# daisy logs
daisy_logs/

*.csv
*.csv
*.private
2 changes: 1 addition & 1 deletion dacapo/blockwise/empanada_function.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def segment_function(input_array, block, **parameters):
Args:
input_array (np.ndarray): The 3D array to segment.
block (dask.array.core.Block): The block object.
block (daisy.Block): The block object.
**parameters: Parameters for the empanada-napari segmenter.
Returns:
np.ndarray: The segmented 3D array.
Expand Down
7 changes: 7 additions & 0 deletions examples/blockwise/segment_config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

steps:
gaussian_smooth:
sigma: 2.0
threshold:
threshold: 200
label: {}
38 changes: 38 additions & 0 deletions examples/blockwise/segment_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import logging

import scipy.ndimage
import yaml

logger = logging.getLogger(__file__)


def segment_function(input_array, block, config_path):
"""
Segment a 3D block using a small numpy-based post-processing script.
Args:
input_array (np.ndarray): The 3D array to segment.
block (daisy.Block): The block object.
config_path (str): The path to the configuration yaml file.
Returns:
np.ndarray: The segmented 3D array.
"""
data = input_array.to_ndarray(block.read_roi)
steps = yaml.load(config_path, Loader=yaml.FullLoader)

# Apply the segmentation function here
for step, params in steps.items():
if step == "gaussian_smooth":
sigma = params.get("sigma", 1.0)
logger.info(f"Applying Gaussian smoothing with sigma={sigma}")
data = scipy.ndimage.gaussian_filter(data, sigma=sigma)
elif step == "threshold":
threshold = params.get("threshold", 0.5)
logger.info(f"Applying thresholding with threshold={threshold}")
data = data > threshold
elif step == "label":
structuring_element = params.get("structuring_element", None)
logger.info("Applying labeling")
data, _ = scipy.ndimage.label(data, structuring_element) # type: ignore

return data
25 changes: 25 additions & 0 deletions examples/blockwise/segment_script.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This file contains the commands used to run an example of the segment-blockwise command.
# The arguments to the segment-blockwise command are:
# -sf: path to the python file with the segmentation_function (in this case the empanada_function.py file)
# -tr: Total ROI to process. It is a list of 3 elements, each one is a list with the start and end of the ROI in the x, y and z axis respectively.
# e.g. -tr "[320000:330000, 100000:110000, 10000:20000]" \
# -rr: ROI to read. It is a list of 3 elements, each one is a list with the start and end of the ROI in the x, y and z axis respectively.
# -wr: ROI to write. It is a list of 3 elements, each one is a list with the start and end of the ROI in the x, y and z axis respectively.
# -nw: Number of workers to use.
# -ic: Input container. It is the path to the input zarr file.
# -id: Input dataset. It is the path to the input dataset inside the input zarr file.
# -oc: Output container. It is the path to the output zarr file.
# -od: Output dataset. It is the path to the output dataset inside the output zarr file.
# --config_path: Path to the config yaml file.


dacapo segment-blockwise \
-sf segment_function.py \
-rr "[256,256,256]" \
-wr "[256,256,256]" \
-nw 32 \
-ic predictions/c-elegen/bw/c_elegen_bw_op50_ld_scratch_0_300000.zarr \
-id ld/ld \
-oc predictions/c-elegen/bw/jrc_c-elegans-bw-1_postprocessed.zarr \
-od ld \
--config_path segment_config.yaml

0 comments on commit f46735c

Please sign in to comment.