Skip to content

Commit

Permalink
changed distances to angstrom (i.e. input for shape dimensions are no…
Browse files Browse the repository at this point in the history
… longer in pixels) (#26)

also corrected depth, height, width of ellipsoid as I spotted I'd mislabelled them
  • Loading branch information
milesagraham authored Jun 4, 2024
1 parent 90c0514 commit 5abacec
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 25 deletions.
8 changes: 4 additions & 4 deletions src/ttmask/cone.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def cone(
cone_height: float = typer.Option(...),
cone_base_diameter: float = typer.Option(...),
soft_edge_width: int = typer.Option(0),
mrc_voxel_size: float = typer.Option(...),
pixel_size: float = typer.Option(...),
output: str = typer.Option("cone.mrc")
):
c = sidelength // 2
Expand Down Expand Up @@ -41,10 +41,10 @@ def cone(
z_distance = centered[:, :, :, 0] # (100, 100, 100)

# Calculate the angle from the tip of the cone to the edge of the base
cone_base_radius = cone_base_diameter / 2
cone_base_radius = (cone_base_diameter / 2) / pixel_size
cone_angle = np.rad2deg(np.arctan(cone_base_radius / cone_height))

within_cone_height = z_distance < cone_height
within_cone_height = z_distance < (cone_height / pixel_size)
within_cone_angle = angles < cone_angle

# mask[within_cone_height] = 1
Expand All @@ -62,6 +62,6 @@ def cone(
mask[boundary_pixels] = (0.5 * np.cos(normalised_distance_from_edge) + 0.5)


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


8 changes: 4 additions & 4 deletions src/ttmask/cube.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def cube(
sidelength: int = typer.Option(...),
cube_sidelength: float =typer.Option(...),
soft_edge_width: float = typer.Option(0),
mrc_voxel_size: float = typer.Option(...),
pixel_size: float = typer.Option(...),
output: str = typer.Option("cube.mrc")
):
c = sidelength // 2
Expand All @@ -29,14 +29,14 @@ def cube(
print('calculating distance')
difference = np.abs(positions - center) # (100, 100, 100, 3)

idx = np.all(difference < (np.array(cube_sidelength) / 2), axis=-1)
in_cube = np.all(difference < (np.array(cube_sidelength / pixel_size) / 2), axis=-1)

mask[idx] = 1
mask[in_cube] = 1

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= mrc_voxel_size, overwrite=True)
mrcfile.write(output, mask, voxel_size= pixel_size, overwrite=True)
8 changes: 4 additions & 4 deletions src/ttmask/cuboid.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def cuboid(
sidelength: int = typer.Option(...),
cuboid_sidelengths: Annotated[Tuple[float, float, float], typer.Option()] = (None, None, None),
soft_edge_width: float = typer.Option(0),
mrc_voxel_size: float = typer.Option(...),
pixel_size: float = typer.Option(...),
output: str = typer.Option("cuboid.mrc"),
):
c = sidelength // 2
Expand All @@ -39,14 +39,14 @@ def cuboid(

# mask[np.logical_not(idx)] = 1 #if you wanted to do opposite for whatever reason

idx = np.all(difference < (np.array(cuboid_sidelengths) / 2), axis=-1)
inside_cuboid = np.all(difference < (np.array(cuboid_sidelengths) / (2 * pixel_size)), axis=-1)

mask[idx] = 1
mask[inside_cuboid] = 1

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= mrc_voxel_size, overwrite=True)
mrcfile.write(output, mask, voxel_size= pixel_size, overwrite=True)
10 changes: 5 additions & 5 deletions src/ttmask/cylinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def cylinder(
cylinder_outer_diameter: float = typer.Option(...),
cylinder_inner_diameter: float = typer.Option(0),
soft_edge_width: int = typer.Option(0),
mrc_voxel_size: float = typer.Option(...),
pixel_size: float = typer.Option(...),
output: str = typer.Option("cylinder.mrc")
):
cylinder_outer_radius = cylinder_outer_diameter / 2
Expand All @@ -32,9 +32,9 @@ def cylinder(

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
idx_z = difference[:, :, :, 0] < (cylinder_height / (2 * pixel_size))
idx_xy_outer = xy_distance < (cylinder_outer_radius / pixel_size)
idx_xy_inner = xy_distance < (cylinder_inner_radius / pixel_size)

mask[np.logical_and(idx_z, idx_xy_outer)] = 1
mask[np.logical_and(idx_z, idx_xy_inner)] = 0
Expand All @@ -45,5 +45,5 @@ def cylinder(

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

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

10 changes: 5 additions & 5 deletions src/ttmask/ellipsoid.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def ellipsoid(
height: float = typer.Option(...),
depth: float = typer.Option(...),
soft_edge_width: int = typer.Option(0),
mrc_voxel_size: float = typer.Option(...),
pixel_size: float = typer.Option(...),
output: str = typer.Option("ellipsoid.mrc"),
):
c = sidelength // 2
Expand All @@ -31,9 +31,9 @@ def ellipsoid(
y_magnitude = difference[:, :, :, 1]
z_magnitude = difference[:, :, :, 0]

x_axis_length = depth / 2
y_axis_length = height / 2
z_axis_length = width / 2
z_axis_length = depth / (2 * pixel_size)
y_axis_length = height / (2 * pixel_size)
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
Expand All @@ -45,4 +45,4 @@ def ellipsoid(

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

mrcfile.write(output, mask, voxel_size=mrc_voxel_size, overwrite=True)
mrcfile.write(output, mask, voxel_size=pixel_size, overwrite=True)
6 changes: 3 additions & 3 deletions src/ttmask/sphere.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def sphere(
sidelength: int = typer.Option(...),
sphere_diameter: float = typer.Option(...),
soft_edge_width: int = typer.Option(0),
mrc_voxel_size: float = typer.Option(...),
pixel_size: float = typer.Option(...),
output: str = typer.Option("sphere.mrc"),
):
sphere_radius = sphere_diameter / 2
Expand All @@ -31,7 +31,7 @@ def sphere(

# calculate whether each pixel is inside or outside the circle
print('calculating which pixels are in sphere')
idx = distance < sphere_radius
idx = distance < (sphere_radius / pixel_size)
mask[idx] = 1

distance_from_edge = distance_transform_edt(mask == 0)
Expand All @@ -40,5 +40,5 @@ def sphere(

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

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

0 comments on commit 5abacec

Please sign in to comment.