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

added cylinder script #10

Merged
merged 1 commit into from
May 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ttmask/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@

from ._cli import cli
from .sphere import sphere
from .cylinder import cylinder

46 changes: 46 additions & 0 deletions src/ttmask/cylinder.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import numpy as np
import einops
import typer
from scipy.ndimage import distance_transform_edt
import mrcfile
from ._cli import cli


@cli.command(name='cylinder')
def cylinder(
boxsize: int = typer.Option(...),
cylinder_height: float = typer.Option(...),
cylinder_outer_diameter: float = typer.Option(...),
cylinder_inner_diameter: float = typer.Option(0),
soft_edge_size: int = typer.Option(...),
):
cylinder_outer_radius = cylinder_outer_diameter / 2
cylinder_inner_radius = cylinder_inner_diameter / 2

c = boxsize // 2
center = np.array([c, c, c])
mask = np.zeros(shape=(boxsize, boxsize, boxsize))

# 3d positions of all voxels
positions = np.indices([boxsize, boxsize, boxsize])
positions = einops.rearrange(positions, 'zyx d h w -> d h w zyx')

print('calculating distance')
difference = np.abs(positions - center) # (100, 100, 100, 3)

xy_distance = np.sum(difference[:, :, :, [1, 2]] ** 2, axis=-1) ** 0.5

idx_z = difference[:, :, :, 0] < cylinder_height / 2
idx_xy_outer = xy_distance < cylinder_outer_radius
idx_xy_inner = xy_distance < cylinder_inner_radius

mask[np.logical_and(idx_z, idx_xy_outer)] = 1
mask[np.logical_and(idx_z, idx_xy_inner)] = 0

distance_from_edge = distance_transform_edt(mask == 0)
boundary_pixels = (distance_from_edge <= soft_edge_size) & (distance_from_edge != 0)
normalised_distance_from_edge = (distance_from_edge[boundary_pixels] / soft_edge_size) * np.pi

mask[boundary_pixels] = (0.5 * np.cos(normalised_distance_from_edge) + 0.5)

mrcfile.write("cylinder.mrc", mask, voxel_size=4, overwrite=True)
2 changes: 0 additions & 2 deletions src/ttmask/sphere.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from ._cli import cli
import numpy as np
import einops
import napari
import typer
from scipy.ndimage import distance_transform_edt
import mrcfile
Expand Down Expand Up @@ -39,5 +38,4 @@ def sphere(

mask[boundary_pixels] = (0.5 * np.cos(normalised_distance_from_edge) + 0.5)

# mrcfile.read("bla.mrc")
mrcfile.write("sphere.mrc", mask, voxel_size=4, overwrite=True)
Loading