Skip to content

Commit

Permalink
Padding mask2map (#42)
Browse files Browse the repository at this point in the history
* added padding
  • Loading branch information
milesagraham authored Jun 18, 2024
1 parent 24f77fe commit 554b902
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
10 changes: 10 additions & 0 deletions src/ttmask/add_padding.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import numpy as np
from scipy.ndimage import distance_transform_edt


def add_padding(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)
output = np.copy(mask)
output[boundary_pixels] = 1
return output
8 changes: 5 additions & 3 deletions src/ttmask/map2mask.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

from ._cli import cli
from .soft_edge import add_soft_edge
from .add_padding import add_padding

@cli.command(name='map2mask')
def map2mask(
Expand All @@ -14,7 +15,7 @@ def map2mask(
output_mask: Path = typer.Option(Path("mask.mrc")),
pixel_size: float = typer.Option(...),
soft_edge_width: int = typer.Option(0),

padding_width: int = typer.Option(0),
):
with mrcfile.open(input_map) as mrc:
map_data = np.array(mrc.data)
Expand All @@ -25,6 +26,7 @@ def map2mask(
map_data[above_threshold] = 1
map_data[below_threshold] = 0

mask = add_soft_edge(map_data, soft_edge_width)
padded_mask = add_padding(map_data, padding_width)
mask = add_soft_edge(padded_mask, soft_edge_width)

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

0 comments on commit 554b902

Please sign in to comment.