Skip to content

Commit

Permalink
Image transformers that do not use domain transformations
Browse files Browse the repository at this point in the history
Some image transformation methods need the whole image because they do
both parameter estimation (=learning) and actual transformation (using
learned parameters) in a single operation. Therefore, when transforming,
instances are not independent and thus this can not be solved with
domain transformation in Orange.
  • Loading branch information
markotoplak committed Oct 1, 2024
1 parent 47484c0 commit cbf6cef
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 27 deletions.
32 changes: 32 additions & 0 deletions orangecontrib/snom/preprocess/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,38 @@ def image_transformer(self, data, image_opts):
raise NotImplementedError


class NoComputeValue:
def __call__(self, data):
return np.full(len(data), np.nan)


class PreprocessImageOpts2DOnlyWhole(PreprocessImageOpts):
def __call__(self, data, image_opts):
at = data.domain[image_opts["attr_value"]].copy(compute_value=NoComputeValue())
odata = data
domain = domain_with_single_attribute_in_x(at, data.domain)
data = data.transform(domain)
if len(data):
with data.unlocked(data.X):
data.X[:, 0] = odata.get_column(image_opts["attr_value"], copy=True)
try:
hypercube, _, indices = get_ndim_hyperspec(
data, (image_opts["attr_x"], image_opts["attr_y"])
)
image = hypercube[:, :, 0]
transformed = self.transform_image(image)
col = transformed[indices].reshape(-1)
except InvalidAxisException:
col = np.full(len(data), np.nan)
if len(data):
with data.unlocked(data.X):
data.X[:, 0] = col
return data

def transform_image(self, image):
raise NotImplementedError


def axes_to_ndim_linspace(coordinates):
# modified to avoid domains as much as possible
ls = []
Expand Down
17 changes: 3 additions & 14 deletions orangecontrib/snom/widgets/preprocessors/background_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@
from pySNOM.images import BackgroundPolyFit

from orangecontrib.snom.preprocess.utils import (
PreprocessImageOpts2D,
CommonDomainImage2D,
PreprocessImageOpts2DOnlyWhole,
)
from orangecontrib.snom.widgets.preprocessors.registry import preprocess_image_editors


class _BackGroundFitCommon(CommonDomainImage2D):
def __init__(self, xorder, yorder, domain, image_opts):
super().__init__(domain, image_opts)
class BackGroundFit(PreprocessImageOpts2DOnlyWhole):
def __init__(self, xorder=1, yorder=1):
self.xorder = xorder
self.yorder = yorder

Expand All @@ -25,15 +23,6 @@ def transform_image(self, image):
return d


class BackGroundFit(PreprocessImageOpts2D):
def __init__(self, xorder=1, yorder=1):
self.xorder = xorder
self.yorder = yorder

def image_transformer(self, data, image_opts):
return _BackGroundFitCommon(self.xorder, self.yorder, data.domain, image_opts)


class BackGroundFitEditor(BaseEditorOrange):
name = "Polynomial background fit"
qualname = "orangecontrib.snom.background_fit_test"
Expand Down
16 changes: 3 additions & 13 deletions orangecontrib/snom/widgets/preprocessors/linelevel.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,28 +8,18 @@

from orangecontrib.snom.widgets.preprocessors.registry import preprocess_image_editors
from orangecontrib.snom.preprocess.utils import (
PreprocessImageOpts2D,
CommonDomainImage2D,
PreprocessImageOpts2DOnlyWhole,
)


class _LineLevelCommon(CommonDomainImage2D):
def __init__(self, method, domain, image_opts):
super().__init__(domain, image_opts)
class LineLevelProcessor(PreprocessImageOpts2DOnlyWhole):
def __init__(self, method="median"):
self.method = method

def transform_image(self, image):
return LineLevel(method=self.method).transform(image)


class LineLevelProcessor(PreprocessImageOpts2D):
def __init__(self, method="median"):
self.method = method

def image_transformer(self, data, image_opts):
return _LineLevelCommon(self.method, data.domain, image_opts)


class LineLevelEditor(BaseEditorOrange):
name = "Line leveling"
qualname = "orangecontrib.snom.line_level_test"
Expand Down

0 comments on commit cbf6cef

Please sign in to comment.