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

Generalized masking #1092

Merged
merged 21 commits into from
Apr 25, 2024
Merged

Generalized masking #1092

merged 21 commits into from
Apr 25, 2024

Conversation

camisowers
Copy link
Contributor

@camisowers camisowers commented Dec 5, 2023

If you haven't already, please read through our contributing guidelines before opening your PR

What is the purpose of this PR?

Closes #1091.

How did you implement your changes

Add two user functions, generate_signal_masks() and generate_cell_masks() for either signal based masking or cell based masking.

Remaining issues

  • create masking notebook

@camisowers camisowers added the enhancement New feature or request label Dec 5, 2023
@camisowers camisowers self-assigned this Dec 5, 2023
Copy link

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

@camisowers
Copy link
Contributor Author

I still need to write the notebook tests, but would love any preliminary input on the masking functionalities you have helped write or seen lab members use. @alex-l-kong @srivarra

@camisowers camisowers marked this pull request as ready for review December 5, 2023 18:59
Copy link
Contributor

@srivarra srivarra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems EzSeg already has some of this functionality in one form or another. We could figure out how to use it here, or we could make changes in ezseg to account for this workflow. Thoughts?

src/ark/utils/masking_utils.py Outdated Show resolved Hide resolved
src/ark/utils/masking_utils.py Outdated Show resolved Hide resolved
Copy link
Contributor

@alex-l-kong alex-l-kong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@srivarra pretty much covered it.

@camisowers
Copy link
Contributor Author

camisowers commented Apr 16, 2024

Updated to use the already built ezseg functions. Decided to forego the notebook tests, since it's just a utility notebook with only top-level function calls that are already tested.

Copy link
Contributor

@alex-l-kong alex-l-kong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few comments about what should be visible or not, but otherwise looks great.

src/ark/utils/masking_utils.py Show resolved Hide resolved
return cell_mask


def generate_cell_masks(seg_dir, mask_dir, cell_table, cell_types, cluster_col, mask_name,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would add a few defaults here:

  • cell_types: default to None, in which case include all cell types in create_cell_masks
  • cluster_col: should be ark.settings.cell_type. In that way you may not have to explicitly specify it in the notebook.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cluster_col default is a great idea! For cell_types, it wouldn't really make sense to mask all cell types in the image (it would just be a blurred version of the segmentation masks), so forcing the user to specify a list is robably the way to go.

src/ark/utils/masking_utils.py Outdated Show resolved Hide resolved
@@ -0,0 +1,155 @@
{
Copy link
Contributor

@alex-l-kong alex-l-kong Apr 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Personally I prefer leaving sigma, min_mask_size, and max_hole_size as hidden default arguments inside generate_signal_masks. If you expect the user will change these, then I'd specify them as customizable variables in the cell above.


Reply via ReviewNB

Copy link
Contributor Author

@camisowers camisowers Apr 18, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The defaults set here are currently based only on what Noah used for the TONIC data, so I don't want users to think the masks generated with these are the only output possible. I'll add them to the description and set them in the cell above.

@@ -0,0 +1,155 @@
{
Copy link
Contributor

@alex-l-kong alex-l-kong Apr 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Piggybacking off of a comment from ark.utils.masking_utils.py, I'd prefer leaving cluster_col as a hidden default argument in generate_cell_masks. 


Reply via ReviewNB

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar to the spatial scripts, this cell masking would often be used for a more broad cell type grouping specified by the user previously, so rarely would we use the 'cell_meta_cluster' column. It's just set here as a example!

templates/Generalized_Masking.ipynb Show resolved Hide resolved
Copy link
Contributor

@srivarra srivarra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks pretty good, just a couple of clarifications.

@@ -154,7 +154,9 @@ def _create_object_mask(
misc_utils.verify_in_list(object_shape_type=[object_shape_type], object_shape_options=["blob", "projection"])

# Copy the input image, and get its shape
img2mask: np.ndarray = input_image.copy().to_numpy()
img2mask: np.ndarray = input_image.copy()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In what instance would this not be a numpy array? If it's an xarray, all normal numpy ops and functions which make use of numpy ops will work, so we wouldn't need to convert it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Where I'm implementing it in masking_utils, img2mask is already an np.array, so I get an AttributeError: 'numpy.ndarray' object has no attribute 'to_numpy'.

"composite_directory": composite_directory,
"composite_name": composite_name,
}
log_creator(variables_to_log, log_dir, f"{composite_name}_composite_log.txt")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we not want to return the composite images here as well? For example what if the user passes in a path for log_dir but there isn't a path for the composite_directory. It wouldn't return the dictionary nor save any of the composite files.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ezseg functions only save the composites, they don't return them so I would have to change the ezseg notebook to handle a return (not a big deal, but unnecessary when it's not even used). Since the only case we would need the composites returned is for the internal function call inside generate_signal_masks(), I didn't think it was needed.

src/ark/segmentation/ez_seg/composites.py Show resolved Hide resolved
Copy link
Contributor

@srivarra srivarra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@camisowers camisowers added this pull request to the merge queue Apr 24, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Apr 24, 2024
@alex-l-kong alex-l-kong added this pull request to the merge queue Apr 25, 2024
Merged via the queue into main with commit c877314 Apr 25, 2024
18 checks passed
@alex-l-kong alex-l-kong deleted the signal_masking branch April 25, 2024 21:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

General signal masking notebook
3 participants