Skip to content

Commit

Permalink
allowed tweaking outlier_predictor
Browse files Browse the repository at this point in the history
  • Loading branch information
yfukai committed Dec 26, 2023
1 parent c23930d commit 5102121
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
17 changes: 14 additions & 3 deletions src/m2stitch/_stage_model.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import itertools
from typing import Callable
from typing import Optional
from typing import Tuple

import numpy as np
Expand All @@ -11,7 +12,11 @@


def compute_image_overlap2(
grid: pd.DataFrame, direction: str, sizeY: Int, sizeX: Int, predictor: Callable
grid: pd.DataFrame,
direction: str,
sizeY: Int,
sizeX: Int,
predictor: Optional[Callable],
) -> Tuple[Float, ...]:
"""Compute the value of the image overlap.
Expand All @@ -25,6 +30,8 @@ def compute_image_overlap2(
the image width
sizeX : Int
the image height
predictor : Optional[Callable], optional
the predictor function of outliers
Returns
-------
Expand All @@ -43,8 +50,12 @@ def compute_image_overlap2(
]
)
translation = translation[:, np.all(np.isfinite(translation), axis=0)]
c = predictor(translation.T)
res = np.median(translation[:, c == 1], axis=1)
if predictor is not None:
c = predictor(translation.T)
ind = c == 1
else:
ind = np.ones(translation.shape[1], dtype=bool)
res = np.median(translation[:, ind], axis=1)
assert len(res) == 2
return tuple(res)

Expand Down
28 changes: 24 additions & 4 deletions src/m2stitch/stitching.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@


@dataclass
class ElipticEnvelopPredictor:
class EllipticEnvelopePredictor:
contamination: float
epsilon: float
random_seed: int
Expand All @@ -55,6 +55,7 @@ def stitch_images(
full_output: bool = False,
row_col_transpose: bool = True,
ncc_threshold: Float = 0.5,
outlier_predictor_name: str = "elliptic_envelope",
) -> Tuple[pd.DataFrame, dict]:
"""Compute image positions for stitching.
Expand Down Expand Up @@ -95,6 +96,10 @@ def stitch_images(
the threshold of the normalized cross correlation used to select the initial
stitched pairs.
outlier_predictor_name : str, default "elliptic_envelope"
the name of the outlier predictor.
"elliptic_envelope" or "none" are supported.
Returns
-------
grid : pd.DataFrame
Expand All @@ -104,6 +109,14 @@ def stitch_images(
prop_dict : dict
the dict of estimated parameters. (to be documented)
"""

if outlier_predictor_name == "elliptic_envelope":
outlier_predictor = EllipticEnvelopePredictor(0.1, 0.1, 0)
elif outlier_predictor_name == "none":
outlier_predictor = None
else:
raise ValueError(f"Unknown outlier_predictor_name: {outlier_predictor_name}")

images = np.array(images)
assert (position_indices is not None) or (rows is not None and cols is not None)
if position_indices is None:
Expand Down Expand Up @@ -202,12 +215,19 @@ def get_lims(dimension, size):
assert np.any(
grid["left_ncc_first"] > ncc_threshold
), "there is no good left pair, (try lowering the ncc_threshold)"
predictor = ElipticEnvelopPredictor(contamination=0.4, epsilon=0.01, random_seed=0)
left_displacement = compute_image_overlap2(
grid[grid["left_ncc_first"] > ncc_threshold], "left", sizeY, sizeX, predictor
grid[grid["left_ncc_first"] > ncc_threshold],
"left",
sizeY,
sizeX,
outlier_predictor,
)
top_displacement = compute_image_overlap2(
grid[grid["top_ncc_first"] > ncc_threshold], "top", sizeY, sizeX, predictor
grid[grid["top_ncc_first"] > ncc_threshold],
"top",
sizeY,
sizeX,
outlier_predictor,
)
overlap_top = np.clip(100 - top_displacement[0] * 100, pou, 100 - pou)
overlap_left = np.clip(100 - left_displacement[1] * 100, pou, 100 - pou)
Expand Down

0 comments on commit 5102121

Please sign in to comment.