Skip to content

Commit

Permalink
more robust implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
markotoplak committed Sep 26, 2024
1 parent 626664d commit 68b1e73
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 37 deletions.
51 changes: 51 additions & 0 deletions orangecontrib/snom/preprocess/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import numpy as np

from Orange.data import Domain
from Orange.preprocess import Preprocess
from orangecontrib.spectroscopy.utils import InvalidAxisException, values_to_linspace, index_values


class PreprocessImageOpts(Preprocess):
pass


def axes_to_ndim_linspace(coordinates):
# modified to avoid domains as much as possible
ls = []
indices = []

for i in range(coordinates.shape[1]):
coor = coordinates[:, i]
lsa = values_to_linspace(coor)
if lsa is None:
raise InvalidAxisException(i)
ls.append(lsa)
indices.append(index_values(coor, lsa))

return ls, tuple(indices)


def get_ndim_hyperspec(data, attrs):
# mostly copied from orangecontrib.spectroscopy.utils,
# but returns the indices too
# also avoid Orange domains as much as possible
coordinates = np.hstack([data.get_column(a).reshape(-1, 1) for a in attrs])

ls, indices = axes_to_ndim_linspace(coordinates)

# set data
new_shape = tuple([lsa[2] for lsa in ls]) + (data.X.shape[1],)
hyperspec = np.ones(new_shape) * np.nan

hyperspec[indices] = data.X

return hyperspec, ls, indices


def domain_with_single_attribute_in_x(attribute, domain):
"""Create a domain with only the attribute in domain.attributes and ensure
that the same attribute is removed from metas and class_vars if it was present
there. """
class_vars = [a for a in domain.class_vars if a.name != attribute.name]
metas = [a for a in domain.metas if a.name != attribute.name]
return Domain([attribute], class_vars, metas)
2 changes: 1 addition & 1 deletion orangecontrib/snom/widgets/owpreprocessimage.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from Orange.widgets.settings import DomainContextHandler
from Orange.widgets.utils.itemmodels import DomainModel
from orangecontrib.snom.widgets.preprocessors.registry import preprocess_image_editors
from orangecontrib.snom.widgets.preprocessors.utils import PreprocessImageOpts
from orangecontrib.snom.preprocess.utils import PreprocessImageOpts
from orangewidget import gui
from orangewidget.settings import SettingProvider, ContextSetting, Setting

Expand Down
47 changes: 17 additions & 30 deletions orangecontrib/snom/widgets/preprocessors/linelevel.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import numpy as np
from AnyQt.QtWidgets import QFormLayout

from Orange.data import Domain
from orangecontrib.spectroscopy.utils import InvalidAxisException

from orangewidget.gui import comboBox

Expand All @@ -11,26 +11,11 @@
from orangecontrib.spectroscopy.widgets.preprocessors.utils import BaseEditorOrange

from orangecontrib.snom.widgets.preprocessors.registry import preprocess_image_editors
from orangecontrib.snom.widgets.preprocessors.utils import PreprocessImageOpts


def get_ndim_hyperspec(data, attrs):
# mostly copied from orangecontrib.spectroscopy.utils,
# but returns the indices too
ndom = Domain(attrs)
datam = data.transform(ndom)

from orangecontrib.spectroscopy.utils import axes_to_ndim_linspace

ls, indices = axes_to_ndim_linspace(datam, attrs)

# set data
new_shape = tuple([lsa[2] for lsa in ls]) + (data.X.shape[1],)
hyperspec = np.ones(new_shape) * np.nan

hyperspec[indices] = data.X

return hyperspec, ls, indices
from orangecontrib.snom.preprocess.utils import (
PreprocessImageOpts,
get_ndim_hyperspec,
domain_with_single_attribute_in_x,
)


class _LineLevelCommon(CommonDomain):
Expand All @@ -41,14 +26,16 @@ def __init__(self, method, domain, image_opts):

def transformed(self, data):
vat = data.domain[self.image_opts["attr_value"]]
ndom = Domain([vat], data.domain.class_vars, data.domain.metas)
ndom = domain_with_single_attribute_in_x(vat, data.domain)
data = data.transform(ndom)
xat = data.domain[self.image_opts["attr_x"]]
yat = data.domain[self.image_opts["attr_y"]]
hypercube, _, indices = get_ndim_hyperspec(data, (xat, yat))
transformed = LineLevel(method=self.method).transform(hypercube[:, :, 0])
out = transformed[indices].reshape(len(data), -1)
return out
try:
hypercube, _, indices = get_ndim_hyperspec(
data, (self.image_opts["attr_x"], self.image_opts["attr_y"])
)
transformed = LineLevel(method=self.method).transform(hypercube[:, :, 0])
return transformed[indices].reshape(-1, 1)
except InvalidAxisException:
return np.full((len(data), 1), np.nan)


class LineLevelProcessor(PreprocessImageOpts):
Expand All @@ -60,8 +47,8 @@ def __call__(self, data, image_opts):
at = data.domain[image_opts["attr_value"]].copy(
compute_value=SelectColumn(0, common)
)
domain = Domain([at], data.domain.class_vars, data.domain.metas)
return data.from_table(domain, data)
domain = domain_with_single_attribute_in_x(at, data.domain)
return data.transform(domain)


class LineLevelEditor(BaseEditorOrange):
Expand Down
6 changes: 0 additions & 6 deletions orangecontrib/snom/widgets/preprocessors/utils.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import numpy as np

from Orange.preprocess import Preprocess


def reshape_to_image(data, x, y):
xres = np.size(np.unique(x))
yres = np.size(np.unique(y))

return np.reshape(data, (yres, xres))


class PreprocessImageOpts(Preprocess):
pass

0 comments on commit 68b1e73

Please sign in to comment.