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

refactor soft edge generation into a function #31

Merged
merged 3 commits into from
Jun 5, 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: 0 additions & 1 deletion src/ttmask/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,3 @@
from .cone import cone
from .ellipsoid import ellipsoid


21 changes: 8 additions & 13 deletions src/ttmask/cone.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from scipy.ndimage import distance_transform_edt
import mrcfile
from ._cli import cli
from .soft_edge import add_soft_edge


@cli.command(name='cone')
Expand All @@ -13,7 +14,7 @@ def cone(
cone_base_diameter: float = typer.Option(...),
soft_edge_width: int = typer.Option(0),
pixel_size: float = typer.Option(...),
output: str = typer.Option("cone.mrc")
output: str = typer.Option("cone.mrc"),
):
c = sidelength // 2
center = np.array([c, c, c])
Expand All @@ -23,7 +24,7 @@ def cone(
positions = np.indices([sidelength, sidelength, sidelength])
positions = einops.rearrange(positions, 'zyx d h w -> d h w zyx')

centered = positions - center #pixels relative to center point
centered = positions - center # pixels relative to center point
magnitudes = np.linalg.norm(centered, axis=-1)

magnitudes = einops.rearrange(magnitudes, 'd h w -> d h w 1')
Expand All @@ -50,18 +51,12 @@ def cone(
# mask[within_cone_height] = 1
mask[np.logical_and(within_cone_height, within_cone_angle)] = 1

# need to adjust the center of the hollow cone otherwise the cone thins towards the apex
# thickness will need to decrease towards the apex anyway - it's surely not possible / realistic?

# Shift the mask in the z-axis by cone_height / 2
z_shift = -int(cone_height / 2)
mask = np.roll(mask, z_shift, axis=0)
mask = add_soft_edge(mask, soft_edge_width)


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

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


mrcfile.write(output, mask, voxel_size= pixel_size, overwrite=True)


mrcfile.write(output, mask, voxel_size=pixel_size, overwrite=True)
16 changes: 6 additions & 10 deletions src/ttmask/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@
from ._cli import cli
from scipy.ndimage import distance_transform_edt
import mrcfile
from .soft_edge import add_soft_edge


@cli.command(name='cube')
def cube(
sidelength: int = typer.Option(...),
cube_sidelength: float =typer.Option(...),
cube_sidelength: float = typer.Option(...),
soft_edge_width: float = typer.Option(0),
pixel_size: float = typer.Option(...),
output: str = typer.Option("cube.mrc"),
Expand All @@ -32,15 +33,10 @@ def cube(

in_cube = np.all(difference < np.array(cube_sidelength) / (pixel_size * 2), axis=-1)
mask[in_cube] = 1

if wall_thickness != 0:
within_hollowing = np.all(difference < ((np.array(cube_sidelength) / (pixel_size * 2)) - wall_thickness), axis=-1)
mask[within_hollowing] = 0

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

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

mrcfile.write(output, mask, voxel_size= pixel_size, overwrite=True)

mask = add_soft_edge(mask, soft_edge_width)
mrcfile.write(output, mask, voxel_size=pixel_size, overwrite=True)
12 changes: 5 additions & 7 deletions src/ttmask/cuboid.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from typing_extensions import Annotated
from scipy.ndimage import distance_transform_edt
import mrcfile
from .soft_edge import add_soft_edge


@cli.command(name='cuboid')
Expand Down Expand Up @@ -43,15 +44,12 @@ def cuboid(
inside_cuboid = np.all(difference < (np.array(cuboid_sidelengths) / (2 * pixel_size)), axis=-1)

mask[inside_cuboid] = 1

if wall_thickness != 0:
within_hollowing = np.all(difference < ((np.array(cuboid_sidelengths) / (2 * pixel_size)) - wall_thickness), axis=-1)
mask[within_hollowing] = 0

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

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

mask = add_soft_edge(mask, soft_edge_width)

mrcfile.write(output, mask, voxel_size= pixel_size, overwrite=True)

10 changes: 3 additions & 7 deletions src/ttmask/cylinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from scipy.ndimage import distance_transform_edt
import mrcfile
from ._cli import cli
from .soft_edge import add_soft_edge


@cli.command(name='cylinder')
Expand Down Expand Up @@ -40,11 +41,6 @@ def cylinder(
within_xy_hollowing = xy_distance < ((cylinder_radius - wall_thickness) / pixel_size)
mask[np.logical_and(within_z_hollowing, within_xy_hollowing)] = 0

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

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

mrcfile.write(output, mask, voxel_size= pixel_size, overwrite=True)
mask = add_soft_edge(mask, soft_edge_width)

mrcfile.write(output, mask, voxel_size=pixel_size, overwrite=True)
11 changes: 4 additions & 7 deletions src/ttmask/ellipsoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import typer
from scipy.ndimage import distance_transform_edt
import mrcfile
from .soft_edge import add_soft_edge


@cli.command(name='ellipsoid')
Expand Down Expand Up @@ -37,18 +38,14 @@ def ellipsoid(
x_axis_length = width / (2 * pixel_size)

in_ellipsoid = (((x_magnitude) ** 2) / (x_axis_length ** 2)) + ((y_magnitude ** 2) / (y_axis_length ** 2)) + (
(z_magnitude ** 2) / (z_axis_length ** 2)) <= 1
(z_magnitude ** 2) / (z_axis_length ** 2)) <= 1
mask[in_ellipsoid] = 1

if wall_thickness != 0:
in_hollowing = (((x_magnitude) ** 2) / ((x_axis_length - wall_thickness) ** 2)) + ((y_magnitude ** 2) / ((y_axis_length - wall_thickness) ** 2)) + (
(z_magnitude ** 2) / ((z_axis_length - wall_thickness) ** 2)) <= 1
mask[in_hollowing] = 0

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

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

mrcfile.write(output, mask, voxel_size=pixel_size, overwrite=True)
11 changes: 11 additions & 0 deletions src/ttmask/soft_edge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import numpy as np
from scipy.ndimage import distance_transform_edt


def add_soft_edge(mask: np.ndarray, width: float) -> np.ndarray:
distance_from_edge = distance_transform_edt(mask == 0)
boundary_pixels = (distance_from_edge <= width) & (distance_from_edge != 0)
normalised_distance_from_edge = (distance_from_edge[boundary_pixels] / width) * np.pi
output = np.copy(mask)
output[boundary_pixels] = (0.5 * np.cos(normalised_distance_from_edge) + 0.5)
return output
14 changes: 5 additions & 9 deletions src/ttmask/sphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import typer
from scipy.ndimage import distance_transform_edt
import mrcfile
from .soft_edge import add_soft_edge


@cli.command(name='sphere')
Expand Down Expand Up @@ -34,16 +35,11 @@ def sphere(
print('calculating which pixels are in sphere')
idx = distance < (sphere_radius / pixel_size)
mask[idx] = 1

if wall_thickness != 0:
within_hollowing = distance < ((sphere_radius - wall_thickness) / pixel_size)
mask[within_hollowing] = 0

mask = add_soft_edge(mask, soft_edge_width)

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

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

mrcfile.write(output, mask, voxel_size= pixel_size, overwrite=True)

mrcfile.write(output, mask, voxel_size=pixel_size, overwrite=True)
Loading