-
Notifications
You must be signed in to change notification settings - Fork 2
Use temporary local Zarr for regionprops to prevent file errors #189
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
572a459
use temp local zarr for regionprops
akhanf 2f94b87
Simplify ZarrNii loading from temp directory
akhanf da40fec
Potential fix for pull request finding
akhanf f7f9b1c
Potential fix for pull request finding
akhanf b2425a7
Fix zip extraction to be conditional on ozx/zip store type, add level=0
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 28 additions & 10 deletions
38
spimquant/workflow/scripts/compute_filtered_regionprops.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,23 +1,41 @@ | ||
| """Compute region properties from filtered segmentation masks using ZarrNii. | ||
|
|
||
| This script reads a segmentation mask from an OME-Zarr file, performs | ||
| connected components on chunks with overlap, applys filters based on | ||
| region properties, and outputs region properties on these filtered objects | ||
| """ | ||
|
|
||
| import os | ||
| import shutil | ||
| import tempfile | ||
| import zipfile | ||
|
Copilot marked this conversation as resolved.
Outdated
|
||
| from dask_setup import get_dask_client | ||
| from zarrnii import ZarrNii | ||
|
|
||
| if __name__ == "__main__": | ||
| with get_dask_client(snakemake.config["dask_scheduler"], snakemake.threads): | ||
|
|
||
| znimg = ZarrNii.from_file( | ||
| snakemake.input.mask, | ||
| level=0, # input image is already downsampled to the wildcard level | ||
| ) | ||
| # 1. Create a secure, temporary directory in the system default tempdir | ||
| with tempfile.TemporaryDirectory(suffix=".ome.zarr") as temp_dir: | ||
| print(f"Extracting zip archive to temporary directory: {temp_dir}") | ||
|
|
||
| # 2. Open and extract the entire input zip file safely | ||
|
akhanf marked this conversation as resolved.
Outdated
|
||
| with zipfile.ZipFile(snakemake.input.mask, "r") as zip_ref: | ||
| zip_ref.extractall(temp_dir) | ||
|
|
||
| # 3. Locate the extracted directory/file path inside the temp folder | ||
| # OME-Zarr is usually a single top-level directory inside the zip. | ||
| extracted_contents = os.listdir(temp_dir) | ||
| if not extracted_contents: | ||
| raise ValueError("The input zip file is empty.") | ||
|
|
||
| # Direct path to the extracted .zarr directory structure | ||
| zarr_temp_path = os.path.join(temp_dir, extracted_contents[0]) | ||
|
|
||
| # 4. Point ZarrNii to the unzipped DirectoryStore instead of the ZipStore | ||
| znimg = ZarrNii.from_file(temp_dir) | ||
|
|
||
| znimg.compute_region_properties( | ||
| output_path=snakemake.output.regionprops_parquet, | ||
| region_filters=snakemake.params.region_filters, | ||
| output_properties=snakemake.params.output_properties, | ||
| ) | ||
| znimg.compute_region_properties( | ||
| output_path=snakemake.output.regionprops_parquet, | ||
| region_filters=snakemake.params.region_filters, | ||
| output_properties=snakemake.params.output_properties, | ||
| ) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.