Skip to content

Commit

Permalink
Merge pull request #192 from yfukai/refine_logic
Browse files Browse the repository at this point in the history
Refine logic for outlier filter
  • Loading branch information
yfukai authored Feb 15, 2022
2 parents 6a532b4 + 701c6bd commit f370d3f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "m2stitch"
version = "0.5.2"
version = "0.6.0"
description = "M2Stitch"
authors = ["Yohsuke Fukai <[email protected]>"]
license = "MIT"
Expand Down
6 changes: 3 additions & 3 deletions src/m2stitch/_stage_model.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import itertools
from typing import Callable
from typing import Tuple

import numpy as np
import pandas as pd
from sklearn.base import BaseEstimator

from ._typing_utils import Float
from ._typing_utils import Int


def compute_image_overlap2(
grid: pd.DataFrame, direction: str, sizeY: Int, sizeX: Int, predictor: BaseEstimator
grid: pd.DataFrame, direction: str, sizeY: Int, sizeX: Int, predictor: Callable
) -> Tuple[Float, ...]:
"""Compute the value of the image overlap.
Expand Down Expand Up @@ -42,7 +42,7 @@ def compute_image_overlap2(
]
)
translation = translation[:, np.all(np.isfinite(translation), axis=0)]
c = predictor.fit_predict(translation.T)
c = predictor(translation.T)
res = np.median(translation[:, c == 1], axis=1)
assert len(res) == 2
return tuple(res)
Expand Down
1 change: 1 addition & 0 deletions src/m2stitch/_typing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
NumArray = npt.NDArray[Any]
FloatArray = npt.NDArray[np.float_]
IntArray = npt.NDArray[np.int_]
BoolArray = npt.NDArray[np.bool_]

Int = Union[int, np.int_]
Float = Union[float, np.float_]
17 changes: 16 additions & 1 deletion src/m2stitch/stitching.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""This module provides microscope image stitching with the algorithm by MIST."""
import itertools
import warnings
from dataclasses import dataclass
from typing import Any
from typing import Optional
from typing import Sequence
Expand All @@ -23,10 +24,24 @@
from ._translation_computation import interpret_translation
from ._translation_computation import multi_peak_max
from ._translation_computation import pcm
from ._typing_utils import BoolArray
from ._typing_utils import Float
from ._typing_utils import NumArray


@dataclass
class ElipticEnvelopPredictor:
contamination: float
epsilon: float
random_seed: int

def __call__(self, X: NumArray) -> BoolArray:
ee = EllipticEnvelope(contamination=self.contamination)
rng = np.random.default_rng(self.random_seed)
X = rng.normal(size=X.shape) * self.epsilon + X
return ee.fit_predict(X) > 0


def stitch_images(
images: Union[Sequence[NumArray], NumArray],
rows: Optional[Sequence[Any]] = None,
Expand Down Expand Up @@ -173,10 +188,10 @@ def get_lims(dimension, size):
for j, key in enumerate(["ncc", "y", "x"]):
grid.loc[i2, f"{direction}_{key}_first"] = max_peak[j]

predictor = EllipticEnvelope(contamination=0.4)
# TODO make threshold adjustable
assert np.any(grid["top_ncc_first"] > 0.5), "there is no good top pair"
assert np.any(grid["left_ncc_first"] > 0.5), "there is no good left pair"
predictor = ElipticEnvelopPredictor(contamination=0.4, epsilon=0.01, random_seed=0)
left_displacement = compute_image_overlap2(
grid[grid["left_ncc_first"] > 0.5], "left", sizeY, sizeX, predictor
)
Expand Down

0 comments on commit f370d3f

Please sign in to comment.