Skip to content

Commit

Permalink
ENH: Allow to flip order of restoration and model.
Browse files Browse the repository at this point in the history
  • Loading branch information
jwboth committed Oct 17, 2023
1 parent 39dc808 commit 15ffbb3
Showing 1 changed file with 29 additions and 40 deletions.
69 changes: 29 additions & 40 deletions src/darsia/multi_image_analysis/concentrationanalysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@


class ConcentrationAnalysis:
"""
Class providing the capabilities to determine concentration/saturation
"""Class providing the capabilities to determine concentration/saturation
profiles based on image comparison, and tuning of concentration-intensity
maps.
Expand All @@ -40,8 +39,7 @@ def __init__(
labels: Optional[np.ndarray] = None,
**kwargs,
) -> None:
"""
Constructor of ConcentrationAnalysis.
"""Constructor of ConcentrationAnalysis.
Args:
base (Image or list of such): baseline image(s); if multiple provided,
Expand Down Expand Up @@ -96,6 +94,9 @@ def __init__(
self._diff_option = kwargs.get("diff option", "absolute")
"""Option for defining differences of images."""

self.first_restoration_then_model = kwargs.get("restoration -> model", True)
"""Option for defining order of routines."""

# Define a cleaning filter based on remaining images.
self.find_cleaning_filter()

Expand All @@ -113,8 +114,7 @@ def update(
base: Optional[darsia.Image] = None,
mask: Optional[np.ndarray] = None,
) -> None:
"""
Update of the baseline image or parameters.
"""Update of the baseline image or parameters.
Args:
base (Image, optional): image array
Expand All @@ -134,8 +134,7 @@ def find_cleaning_filter(
baseline_images: Optional[list[darsia.Image]] = None,
reset: bool = False,
) -> None:
"""
Determine structural noise by studying a series of baseline images.
"""Determine structural noise by studying a series of baseline images.
The resulting cleaning filter will be used prior to the conversion
of signal to concentration. The cleaning filter should be understood
as thresholding mask.
Expand Down Expand Up @@ -181,8 +180,7 @@ def find_cleaning_filter(
)

def read_cleaning_filter_from_file(self, path: Union[str, Path]) -> None:
"""
Read cleaning filter from file.
"""Read cleaning filter from file.
Args:
path (str or Path): path to cleaning filter array.
Expand All @@ -200,8 +198,7 @@ def read_cleaning_filter_from_file(self, path: Union[str, Path]) -> None:
)

def write_cleaning_filter_to_file(self, path_to_filter: Union[str, Path]) -> None:
"""
Store cleaning filter to file.
"""Store cleaning filter to file.
Args:
path_to_filter (str or Path): path for storage of the cleaning filter.
Expand Down Expand Up @@ -246,11 +243,14 @@ def __call__(self, img: darsia.Image) -> darsia.Image:
# Balance signal (take into account possible heterogeneous effects)
balanced_signal = self._balance_signal(clean_signal)

# Regularize/upscale signal to Darcy scale
smooth_signal = self._prepare_signal(balanced_signal)

# Convert from signal to concentration
concentration = self._convert_signal(smooth_signal, diff)
# Regularize/upscale signal to Darcy scale and convert from signal to concentration
# or other way around.
if self.first_restoration_then_model:
smooth_signal = self._restore_signal(balanced_signal)
concentration = self._convert_signal(smooth_signal, diff)
else:
nonsmooth_concentration = self._convert_signal(balanced_signal, diff)
concentration = self._restore_signal(nonsmooth_concentration)

# Invoke plot
if self.verbosity >= 1:
Expand All @@ -265,8 +265,7 @@ def __call__(self, img: darsia.Image) -> darsia.Image:

# ! ---- Inspection routines
def _inspect_diff(self, img: np.ndarray) -> None:
"""
Routine allowing for plotting of intermediate results.
"""Routine allowing for plotting of intermediate results.
Requires overwrite.
Args:
Expand All @@ -278,8 +277,7 @@ def _inspect_diff(self, img: np.ndarray) -> None:
plt.imshow(img)

def _inspect_scalar_signal(self, img: np.ndarray) -> None:
"""
Routine allowing for plotting of intermediate results.
"""Routine allowing for plotting of intermediate results.
Requires overwrite.
Args:
Expand All @@ -291,8 +289,7 @@ def _inspect_scalar_signal(self, img: np.ndarray) -> None:
plt.imshow(img)

def _inspect_clean_signal(self, img: np.ndarray) -> None:
"""
Routine allowing for plotting of intermediate results.
"""Routine allowing for plotting of intermediate results.
Requires overwrite.
Args:
Expand All @@ -305,8 +302,7 @@ def _inspect_clean_signal(self, img: np.ndarray) -> None:

# ! ---- Pre- and post-processing methods
def _subtract_background(self, img: darsia.Image) -> darsia.Image:
"""
Take difference between input image and baseline image, based
"""Take difference between input image and baseline image, based
on cached option.
Args:
Expand Down Expand Up @@ -346,8 +342,7 @@ def _subtract_background(self, img: darsia.Image) -> darsia.Image:
return diff

def _reduce_signal(self, img: np.ndarray) -> np.ndarray:
"""
Make a scalar image from potentially multi-colored image.
"""Make a scalar image from potentially multi-colored image.
Args:
img (np.ndarray): image
Expand All @@ -362,8 +357,7 @@ def _reduce_signal(self, img: np.ndarray) -> np.ndarray:
return self.signal_reduction(img)

def _clean_signal(self, img: np.ndarray) -> np.ndarray:
"""
Apply cleaning thresholds.
"""Apply cleaning thresholds.
Args:
img (np.ndarray): input image
Expand All @@ -379,8 +373,7 @@ def _clean_signal(self, img: np.ndarray) -> np.ndarray:
)

def _balance_signal(self, img: np.ndarray) -> np.ndarray:
"""
Routine responsible for rescaling wrt segments.
"""Routine responsible for rescaling wrt segments.
Here, it is assumed that only one segment is present.
Thus, no rescaling is performed.
Expand All @@ -394,9 +387,8 @@ def _balance_signal(self, img: np.ndarray) -> np.ndarray:
"""
return img if self.balancing is None else self.balancing(img)

def _prepare_signal(self, signal: np.ndarray) -> np.ndarray:
"""
Apply restoration.
def _restore_signal(self, signal: np.ndarray) -> np.ndarray:
"""Apply restoration.
Args:
signal (np.ndarray): input signal
Expand All @@ -411,8 +403,7 @@ def _prepare_signal(self, signal: np.ndarray) -> np.ndarray:
return signal

def _convert_signal(self, signal: np.ndarray, diff: np.ndarray) -> np.ndarray:
"""
Postprocessing routine, essentially converting a continuous
"""Postprocessing routine, essentially converting a continuous
signal into physical data (binary, continuous concentration etc.)
Args:
Expand All @@ -433,8 +424,7 @@ def _convert_signal(self, signal: np.ndarray, diff: np.ndarray) -> np.ndarray:


class PriorPosteriorConcentrationAnalysis(ConcentrationAnalysis):
"""
Special case of the ConcentrationAnalysis performing a
"""Special case of the ConcentrationAnalysis performing a
prior-posterior analysis, i.e., allowing to review the
conversion performed through a prior model.
Expand Down Expand Up @@ -470,8 +460,7 @@ def __init__(
)

def _convert_signal(self, signal: np.ndarray, diff: np.ndarray) -> np.ndarray:
"""
Postprocessing routine, essentially converting a continuous
"""Postprocessing routine, essentially converting a continuous
signal into physical data (binary, continuous concentration etc.)
Use a prior-posterior approach, allowing to review the prior choice.
Expand Down

0 comments on commit 15ffbb3

Please sign in to comment.