1- from .exceptions import NegativeStdScalerException
21import numpy as np
32from parsers .data_types import ScanImage
43from 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+ )
0 commit comments