Skip to content

Commit 2bfa4ec

Browse files
committed
Make preview png work correctly
- missed a lib function in the preview pipeline - converted it to a result container - integrate it into the pipeline
1 parent edc63c2 commit 2bfa4ec

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

packages/scratch-core/src/conversion/display.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
from .exceptions import NegativeStdScalerException
21
import numpy as np
32
from parsers.data_types import ScanImage
43
from utils.array_definitions import ScanMap2DArray
4+
from returns.result import safe
5+
from utils.logger import log_railway_function
56

67

7-
def normalize(
8+
def _normalize(
89
input_array: ScanMap2DArray, lower: float, upper: float
910
) -> ScanMap2DArray:
1011
"""Perform min-max normalization on the input array and scale to the [0, 255] interval."""
@@ -15,7 +16,7 @@ def normalize(
1516
return (input_array - lower) / (upper - lower) * 255.0
1617

1718

18-
def clip_data(
19+
def _clip_data(
1920
data: ScanMap2DArray, std_scaler: float
2021
) -> tuple[ScanMap2DArray, float, float]:
2122
"""
@@ -28,16 +29,19 @@ def clip_data(
2829
:returns: A tuple containing the clipped data, the lower bound, and the upper bound of the clipped data.
2930
"""
3031
if std_scaler <= 0.0:
31-
raise NegativeStdScalerException("`std_scaler` must be a positive number.")
32+
raise ValueError("`std_scaler` must be a positive number.")
3233
mean = np.nanmean(data)
3334
std = np.nanstd(data, ddof=1) * std_scaler
3435
upper = float(mean + std)
3536
lower = float(mean - std)
36-
clipped = np.clip(data, lower, upper)
37-
return clipped, lower, upper
37+
return np.clip(data, lower, upper), lower, upper
3838

3939

40-
def get_array_for_display(image: ScanImage, std_scaler: float = 2.0) -> ScanMap2DArray:
40+
@log_railway_function("Failed to retreive array for display")
41+
@safe
42+
def get_array_for_display(
43+
scan_image: ScanImage, *, std_scaler: float = 2.0
44+
) -> ScanImage:
4145
"""
4246
Clip and normalize image data for displaying purposes.
4347
@@ -48,6 +52,8 @@ def get_array_for_display(image: ScanImage, std_scaler: float = 2.0) -> ScanMap2
4852
:param std_scaler: The multiplier `S` for the standard deviation used above when clipping the image.
4953
:returns: An array containing the clipped and normalized image data.
5054
"""
51-
clipped, lower, upper = clip_data(data=image.data, std_scaler=std_scaler)
52-
normalized = normalize(clipped, lower, upper)
53-
return normalized
55+
return ScanImage(
56+
data=_normalize(*_clip_data(data=scan_image.data, std_scaler=std_scaler)),
57+
scale_x=scan_image.scale_x,
58+
scale_y=scan_image.scale_y,
59+
)

packages/scratch-core/tests/conversion/test_display.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,5 @@ def test_get_image_for_display_matches_baseline_image(
1313
scan_image_with_nans: ScanImage,
1414
):
1515
verified = np.load(BASELINE_IMAGES_DIR / "display_array.npy")
16-
display_image = get_array_for_display(scan_image_with_nans)
17-
assert_array_almost_equal(display_image, verified)
16+
display_image = get_array_for_display(scan_image_with_nans).unwrap()
17+
assert_array_almost_equal(display_image.data, verified)

src/preprocessors/pipelines.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
from http import HTTPStatus
33
from pathlib import Path
44

5+
from conversion.display import get_array_for_display
56
from conversion.subsample import subsample_array
67
from fastapi.exceptions import HTTPException
78
from image_generation.data_formats import ScanImage
@@ -101,7 +102,8 @@ def surface_map_pipeline(parsed_scan: ScanImage, output_path: Path, parameters:
101102
def _preview_pipeline(parsed_scan: ScanImage, output_path: Path) -> IOResultE[Path]:
102103
return flow(
103104
parsed_scan,
104-
scan_to_image,
105+
get_array_for_display,
106+
bind(scan_to_image),
105107
bind(partial(save_image, output_path=output_path)),
106108
)
107109

0 commit comments

Comments
 (0)