Skip to content

Commit

Permalink
provides option to have shape visually centered if box size is even
Browse files Browse the repository at this point in the history
(hopefully got my git working now)
  • Loading branch information
milesagraham committed Sep 23, 2024
1 parent a88d3bb commit bf359a9
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 26 deletions.
7 changes: 5 additions & 2 deletions src/ttmask/box_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@
import einops


def box_setup(sidelength: int) -> np.ndarray:
def box_setup(sidelength: int, centering: str) -> np.ndarray:
c = sidelength // 2
center = np.array([c, c, c])
if centering == "visual" and sidelength % 2 == 0:
center = np.array([c, c, c]) - 0.5
else:
center = np.array([c, c, c])
mask = np.zeros(shape=(sidelength, sidelength, sidelength), dtype=np.float32)

# 3d coordinates of all voxels
Expand Down
8 changes: 5 additions & 3 deletions src/ttmask/cone.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,11 @@ def cone(
cone_height: float,
cone_base_diameter: float,
soft_edge_width: int,
pixel_size: float
pixel_size: float,
centering: str
) -> np.ndarray:
# establish our coordinate system and empty mask
coordinates_centered, mask = box_setup(sidelength)
coordinates_centered, mask = box_setup(sidelength, centering)
# distances between each pixel and center :
magnitudes = np.linalg.norm(coordinates_centered, axis=-1)
magnitudes = einops.rearrange(magnitudes, 'd h w -> d h w 1')
Expand Down Expand Up @@ -61,8 +62,9 @@ def cone_cli(
soft_edge_width: int = typer.Option(0),
pixel_size: float = typer.Option(1),
output: Path = typer.Option(Path("cone.mrc")),
centering: str = typer.Option("standard"),
):
mask = cone(sidelength, cone_height, cone_base_diameter, soft_edge_width, pixel_size)
mask = cone(sidelength, cone_height, cone_base_diameter, soft_edge_width, pixel_size, centering)

# Save the mask to an MRC file
with mrcfile.new(output, overwrite=True) as mrc:
Expand Down
10 changes: 6 additions & 4 deletions src/ttmask/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ def cube(
cube_sidelength: float,
soft_edge_width: float,
pixel_size: float,
wall_thickness: float
wall_thickness: float,
centering: str
) -> np.ndarray:
# establish our coordinate system and empty mask
coordinates_centered, mask = box_setup(sidelength)
# establish our coordinate system and empty mask
coordinates_centered, mask = box_setup(sidelength, centering)
#converting relative coordinates to xyz distances (i.e. not a negative number) :
xyz_distances = np.abs(coordinates_centered)

Expand All @@ -42,8 +43,9 @@ def cube_cli(
pixel_size: float = typer.Option(1),
output: Path = typer.Option(Path("cube.mrc")),
wall_thickness: float = typer.Option(0),
centering: str = typer.Option("standard"),
):
mask = cube(sidelength, cube_sidelength, soft_edge_width, pixel_size)
mask = cube(sidelength, cube_sidelength, soft_edge_width, pixel_size, wall_thickness, centering)

# Save the mask to an MRC file
with mrcfile.new(output, overwrite=True) as mrc:
Expand Down
8 changes: 5 additions & 3 deletions src/ttmask/cuboid.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,11 @@ def cuboid(
cuboid_sidelengths: Tuple[float, float, float],
soft_edge_width: float,
pixel_size: float,
wall_thickness: float
wall_thickness: float,
centering: str
) -> np.ndarray:
# establish our coordinate system and empty mask
coordinates_centered, mask = box_setup(sidelength)
coordinates_centered, mask = box_setup(sidelength, centering)
#converting relative coordinates to xyz distances (i.e. not a negative number) :
xyz_distances = np.abs(coordinates_centered)

Expand All @@ -44,8 +45,9 @@ def cuboid_cli(
pixel_size: float = typer.Option(1),
wall_thickness: float = typer.Option(0),
output: Path = typer.Option(Path("cuboid.mrc")),
centering: str = typer.Option("standard"),
):
mask = cuboid(sidelength, cuboid_sidelengths, soft_edge_width, pixel_size,wall_thickness)
mask = cuboid(sidelength, cuboid_sidelengths, soft_edge_width, pixel_size,wall_thickness, centering)

# Save the mask to an MRC file
with mrcfile.new(output, overwrite=True) as mrc:
Expand Down
8 changes: 5 additions & 3 deletions src/ttmask/curved_surface.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ def curved_surface(
fit_sphere_diameter: float,
soft_edge_width: int,
pixel_size: float,
surface_thickness: float
surface_thickness: float,
centering: str
) -> np.ndarray:
sphere_radius = fit_sphere_diameter / 2

# establish our coordinate system and empty mask
coordinates_centered, mask = box_setup(sidelength)
coordinates_centered, mask = box_setup(sidelength, centering)
coordinates_shifted = coordinates_centered - ([0, sphere_radius, 0])


Expand Down Expand Up @@ -47,8 +48,9 @@ def curved_surface_cli(
pixel_size: float = typer.Option(1),
output: Path = typer.Option(Path("curved_surface.mrc")),
surface_thickness: float = typer.Option(...),
centering: str = typer.Option("standard"),
):
mask = curved_surface(sidelength, fit_sphere_diameter, soft_edge_width, pixel_size, surface_thickness)
mask = curved_surface(sidelength, fit_sphere_diameter, soft_edge_width, pixel_size, surface_thickness, centering)

# Save the mask to an MRC file
with mrcfile.new(output, overwrite=True) as mrc:
Expand Down
8 changes: 5 additions & 3 deletions src/ttmask/cylinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,13 @@ def cylinder(
cylinder_diameter: float,
wall_thickness: float,
soft_edge_width: int,
pixel_size: float
pixel_size: float,
centering: str
) -> np.ndarray:
cylinder_radius = cylinder_diameter / 2

# establish our coordinate system and empty mask
coordinates_centered, mask = box_setup(sidelength)
coordinates_centered, mask = box_setup(sidelength, centering)

# converting relative coordinates to xyz distances (i.e. not a negative number) :
xyz_distances = np.abs(coordinates_centered)
Expand Down Expand Up @@ -53,8 +54,9 @@ def cylinder_cli(
soft_edge_width: int = typer.Option(0),
pixel_size: float = typer.Option(1),
output: Path = typer.Option(Path("cylinder.mrc")),
centering: str = typer.Option("standard"),
):
mask = cylinder(sidelength, cylinder_height, cylinder_diameter, wall_thickness, soft_edge_width, pixel_size)
mask = cylinder(sidelength, cylinder_height, cylinder_diameter, wall_thickness, soft_edge_width, pixel_size, centering)

# Save the mask to an MRC file
with mrcfile.new(output, overwrite=True) as mrc:
Expand Down
8 changes: 5 additions & 3 deletions src/ttmask/ellipsoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ def ellipsoid(
ellipsoid_dimensions: Tuple[float, float, float],
soft_edge_width: float,
pixel_size: float,
wall_thickness: float
wall_thickness: float,
centering: str
) -> np.ndarray:
# establish our coordinate system and empty mask
coordinates_centered, mask = box_setup(sidelength)
coordinates_centered, mask = box_setup(sidelength, centering)
#converting relative coordinates to xyz distances (i.e. not a negative number) :
xyz_distances = np.abs(coordinates_centered)

Expand Down Expand Up @@ -56,8 +57,9 @@ def ellipsoid_cli(
soft_edge_width: float = typer.Option(0),
pixel_size: float = typer.Option(1),
output: Path = typer.Option(Path("ellipsoid.mrc")),
centering: str = typer.Option("standard"),
):
mask = ellipsoid(sidelength, ellipsoid_dimensions, soft_edge_width, pixel_size)
mask = ellipsoid(sidelength, ellipsoid_dimensions, soft_edge_width, pixel_size, centering)

# Save the mask to an MRC file
with mrcfile.new(output, overwrite=True) as mrc:
Expand Down
8 changes: 5 additions & 3 deletions src/ttmask/sphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,13 @@ def sphere(
sphere_diameter: float,
soft_edge_width: int,
pixel_size: float,
wall_thickness: float
wall_thickness: float,
centering: str
) -> np.ndarray:
sphere_radius = sphere_diameter / 2

# establish our coordinate system and empty mask
coordinates_centered, mask = box_setup(sidelength)
coordinates_centered, mask = box_setup(sidelength, centering)

# determine distances of each pixel to the center
distance_to_center = np.linalg.norm(coordinates_centered, axis=-1)
Expand All @@ -44,8 +45,9 @@ def sphere_cli(
pixel_size: float = typer.Option(1),
output: Path = typer.Option(Path("sphere.mrc")),
wall_thickness: float = typer.Option(0),
centering: str = typer.Option("standard"),
):
mask = sphere(sidelength, sphere_diameter, soft_edge_width, pixel_size, wall_thickness)
mask = sphere(sidelength, sphere_diameter, soft_edge_width, pixel_size, wall_thickness, centering)

# Save the mask to an MRC file
with mrcfile.new(output, overwrite=True) as mrc:
Expand Down
6 changes: 4 additions & 2 deletions src/ttmask/tube.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ def tube(
wall_thickness: float,
soft_edge_width: int,
pixel_size: float,
centering: str
) -> np.ndarray:
tube_radius = tube_diameter / 2

# establish our coordinate system and empty mask
coordinates_centered, mask = box_setup(sidelength)
coordinates_centered, mask = box_setup(sidelength, centering)
#converting relative coordinates to xyz distances (i.e. not a negative number) :
xyz_distances = np.abs(coordinates_centered)

Expand All @@ -46,8 +47,9 @@ def tube_cli(
tube_diameter: float = typer.Option(...),
wall_thickness: float = typer.Option(0),
output: Path = typer.Option(Path("tube.mrc")),
centering: str = typer.Option("standard"),
):
mask = tube(sidelength, tube_height, tube_diameter, wall_thickness)
mask = tube(sidelength, tube_height, tube_diameter, wall_thickness, centering)

# Save the mask to an MRC file
with mrcfile.new(output, overwrite=True) as mrc:
Expand Down

0 comments on commit bf359a9

Please sign in to comment.