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

add skeletonization code #65

Merged
merged 10 commits into from
Apr 28, 2024
Merged

add skeletonization code #65

merged 10 commits into from
Apr 28, 2024

Conversation

Hanyi11
Copy link
Contributor

@Hanyi11 Hanyi11 commented Apr 11, 2024

No description provided.

@codecov-commenter
Copy link

codecov-commenter commented Apr 11, 2024

Codecov Report

Attention: Patch coverage is 0% with 135 lines in your changes are missing coverage. Please review.

Project coverage is 0.00%. Comparing base (bea5ae0) to head (d005be5).
Report is 5 commits behind head on main.

Files Patch % Lines
...embrain_seg/segmentation/skeletonization/diff3d.py 0.00% 34 Missing ⚠️
...membrain_seg/segmentation/skeletonization/eig3d.py 0.00% 31 Missing ⚠️
...rain_seg/segmentation/skeletonization/nonmaxsup.py 0.00% 31 Missing ⚠️
src/membrain_seg/segmentation/skeletonize.py 0.00% 31 Missing ⚠️
src/membrain_seg/segmentation/cli/ske_cli.py 0.00% 6 Missing ⚠️
src/membrain_seg/segmentation/cli/__init__.py 0.00% 1 Missing ⚠️
src/membrain_seg/segmentation/networks/unet.py 0.00% 1 Missing ⚠️

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@          Coverage Diff           @@
##            main     #65    +/-   ##
======================================
  Coverage   0.00%   0.00%            
======================================
  Files         40      45     +5     
  Lines       1411    1545   +134     
======================================
- Misses      1411    1545   +134     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -0,0 +1,77 @@
import numpy as np
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need this function? Could be replaced by torch Gaussian filtering, I think? This should be even faster when running on GPU.

from scipy.fftpack import fftn, fftshift, ifftn


def angauss(T: np.ndarray, std: float, r: float) -> np.ndarray:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Also, this is a function from Antonio's PySeg. Please make sure to add a note to all of these functions that they come from Antonio's repo :)

Nx, Ny, Nz = T.shape

# Initialize arrays for forward and backward differences
Idp = np.zeros((Nx, Ny, Nz), dtype="float64")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we make these variable names a bit more descriptive? Would make it easier to read the function

from skimage.util import img_as_float32


def read_nifti(nifti_file: str) -> np.ndarray:
Copy link
Collaborator

Choose a reason for hiding this comment

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

We already have all of these functions in
https://github.com/teamtomo/membrain-seg/blob/main/src/membrain_seg/segmentation/dataloading/data_utils.py

Please use these instead of defining them again here :)

This command runs the skeletonization process from the command line.
"""
# labels can be .nii files or mrc files
if label_path.endswith(".nii") or label_path.endswith(".nii.gz"):
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can neglect the case of nifti files here. Let's just keep the mrc files.
Also, let's not check for file endings here because there are multiple valid file extensions that work in a similar way and can be processed out-of-the-box by the "load_tomogram" function (e.g. .mrc, .rec, .em, ...).


# Segmentation consists of 0, 1 and 2 respectively
# for background, mask and labels to be ignored.
labels = (segmentation == 1) * 1.0
Copy link
Collaborator

Choose a reason for hiding this comment

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

normally, we will not have the case that we have ignore labels (that's just for the training patches).
So maybe we can make this criterion (segmentation > 0) * 1.0
This will account also for multiple instance values

hessianZZ = angauss(hessianZZ, std, 1)
hessianXY = angauss(hessianXY, std, 1)
hessianXZ = angauss(hessianXZ, std, 1)
hessianYZ = angauss(hessianYZ, std, 1)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could you please factor out the hessian computation into a separate function?
I.e. a function that computes the gradients, the hessian, and then smoothes the hessian?

print("Genration of skeleton based on non-maximum suppression algorithm.")
first_eigenvalue = ndimage.gaussian_filter(first_eigenvalue, sigma=1)
first_eigenvalue = np.abs(first_eigenvalue)
Skeleton = nonmaxsup(
Copy link
Collaborator

Choose a reason for hiding this comment

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

capitalized variable names indicate classes, so better call it "skeleton" instead of "Skeleton"

)

# Save the skeleton
write_nifti(save_path, Skeleton)
Copy link
Collaborator

Choose a reason for hiding this comment

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

default output should be an .mrc file. Please use

def normalize_tomogram(tomogram: np.ndarray) -> np.ndarray:

@LorenzLamm
Copy link
Collaborator

Thanks a lot for the updates @Hanyi11
It still has some issues with small points close to the membrane, but efficiency is okay now.
Let's merge this and improve performance in a future update :)

@LorenzLamm LorenzLamm merged commit 1317d16 into teamtomo:main Apr 28, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants