diff --git a/.gitignore b/.gitignore index d14f3fb84..dff704b9a 100644 --- a/.gitignore +++ b/.gitignore @@ -29,4 +29,5 @@ tmp/ # daisy logs daisy_logs/ -*.csv \ No newline at end of file +*.csv +*.private \ No newline at end of file diff --git a/dacapo/blockwise/empanada_function.py b/dacapo/blockwise/empanada_function.py index 301a282e8..09871de88 100644 --- a/dacapo/blockwise/empanada_function.py +++ b/dacapo/blockwise/empanada_function.py @@ -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. diff --git a/examples/blockwise/segment_config.yaml b/examples/blockwise/segment_config.yaml new file mode 100644 index 000000000..80e0ff128 --- /dev/null +++ b/examples/blockwise/segment_config.yaml @@ -0,0 +1,7 @@ + +steps: + gaussian_smooth: + sigma: 2.0 + threshold: + threshold: 200 + label: {} diff --git a/examples/blockwise/segment_function.py b/examples/blockwise/segment_function.py new file mode 100644 index 000000000..c49239c4e --- /dev/null +++ b/examples/blockwise/segment_function.py @@ -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 diff --git a/examples/blockwise/segment_script.sh b/examples/blockwise/segment_script.sh new file mode 100644 index 000000000..ec4f191fe --- /dev/null +++ b/examples/blockwise/segment_script.sh @@ -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