Skip to content

Commit

Permalink
Move dummy Add preprocessor to the proper location
Browse files Browse the repository at this point in the history
  • Loading branch information
markotoplak committed Sep 19, 2024
1 parent 44cd0cb commit 4a4187c
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 82 deletions.
103 changes: 21 additions & 82 deletions orangecontrib/snom/widgets/owpreprocessimage.py
Original file line number Diff line number Diff line change
@@ -1,95 +1,25 @@
import time

from AnyQt.QtWidgets import QFormLayout

from Orange.data import Domain, DiscreteVariable, ContinuousVariable
from Orange.widgets.settings import DomainContextHandler
from Orange.widgets.utils.itemmodels import DomainModel
from orangecontrib.snom.widgets.preprocessors.registry import preprocess_image_editors
from orangewidget import gui
from orangewidget.settings import SettingProvider, ContextSetting, Setting

import Orange.data
from Orange import preprocess
from Orange.preprocess import Preprocess
from Orange.widgets.widget import Output

from orangecontrib.spectroscopy.preprocess import SelectColumn, \
CommonDomain
from orangecontrib.spectroscopy.widgets.owhyper import ImagePlot

from orangecontrib.spectroscopy.widgets.owpreprocess import (
GeneralPreprocess,
create_preprocessor,
InterruptException,
)
from orangecontrib.spectroscopy.widgets.preprocessors.registry import PreprocessorEditorRegistry
from orangecontrib.spectroscopy.widgets.preprocessors.utils import BaseEditorOrange
from orangecontrib.spectroscopy.widgets.gui import lineEditFloatRange
from orangewidget.widget import Msg


class AddFeature(SelectColumn):
InheritEq = True


class _AddCommon(CommonDomain):

def __init__(self, amount, domain):
super().__init__(domain)
self.amount = amount

def transformed(self, data):
return data.X + self.amount


class AddConstant(Preprocess):

def __init__(self, amount=0.):
self.amount = amount

def __call__(self, data):
common = _AddCommon(self.amount, data.domain)
atts = [a.copy(compute_value=AddFeature(i, common))
for i, a in enumerate(data.domain.attributes)]
domain = Orange.data.Domain(atts, data.domain.class_vars,
data.domain.metas)
return data.from_table(domain, data)


class AddEditor(BaseEditorOrange):

name = "Add constant"
qualname = "orangecontrib.snom.add_constant_test"

def __init__(self, parent=None, **kwargs):
super().__init__(parent, **kwargs)

self.amount = 0.

form = QFormLayout()
amounte = lineEditFloatRange(self, self, "amount", callback=self.edited.emit)
form.addRow("Addition", amounte)
self.controlArea.setLayout(form)

def activateOptions(self):
pass # actions when user starts changing options

def setParameters(self, params):
self.amount = params.get("amount", 0.)

@classmethod
def createinstance(cls, params):
params = dict(params)
amount = float(params.get("amount", 0.))
return AddConstant(amount=amount)

def set_preview_data(self, data):
if data:
pass # TODO any settings


preprocess_editors = PreprocessorEditorRegistry()
preprocess_editors.register(AddEditor, 100)
from orangewidget.widget import Msg


class AImagePlot(ImagePlot):
Expand Down Expand Up @@ -138,7 +68,7 @@ class OWPreprocessImage(SpectralImagePreprocess):
_max_preview_spectra = 1000000
preview_curves = Setting(10000)

editor_registry = preprocess_editors
editor_registry = preprocess_image_editors
BUTTON_ADD_LABEL = "Add preprocessor..."

attr_value = ContextSetting(None)
Expand All @@ -158,8 +88,7 @@ class Information(SpectralImagePreprocess.Information):

def image_values(self):
attr_value = self.attr_value.name if self.attr_value else None
return lambda data, attr=attr_value: \
data.transform(Domain([data.domain[attr]]))
return lambda data, attr=attr_value: data.transform(Domain([data.domain[attr]]))

def image_values_fixed_levels(self):
return None
Expand All @@ -168,13 +97,19 @@ def __init__(self):
self.markings_list = []
super().__init__()

self.feature_value_model = DomainModel(DomainModel.SEPARATED,
valid_types=ContinuousVariable)
self.feature_value_model = DomainModel(
DomainModel.SEPARATED, valid_types=ContinuousVariable
)
self.feature_value = gui.comboBox(
self.preview_settings_box, self, "attr_value",
self.preview_settings_box,
self,
"attr_value",
label="Show feature",
contentsLength=12, searchable=True,
callback=self.update_feature_value, model=self.feature_value_model)
contentsLength=12,
searchable=True,
callback=self.update_feature_value,
model=self.feature_value_model,
)

self.contextAboutToBeOpened.connect(lambda x: self.init_interface_data(x[0]))

Expand Down Expand Up @@ -222,8 +157,11 @@ def set_data(self, data):
def valid_context(data):
if data is None:
return False
annotation_features = [v for v in data.domain.metas + data.domain.class_vars
if isinstance(v, (DiscreteVariable, ContinuousVariable))]
annotation_features = [
v
for v in data.domain.metas + data.domain.class_vars
if isinstance(v, (DiscreteVariable, ContinuousVariable))
]
return len(annotation_features) >= 1

if valid_context(data):
Expand Down Expand Up @@ -275,4 +213,5 @@ def progress_interrupt(i: float):

if __name__ == "__main__": # pragma: no cover
from Orange.widgets.utils.widgetpreview import WidgetPreview

WidgetPreview(OWPreprocessImage).run(Orange.data.Table("whitelight.gsf"))
1 change: 1 addition & 0 deletions orangecontrib/snom/widgets/preprocessors/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# load and register editors
from . import registration_example # noqa: F401
from . import phase_unwrap # noqa: F401
from . import multiply # noqa: F401
73 changes: 73 additions & 0 deletions orangecontrib/snom/widgets/preprocessors/multiply.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# this is just an example of registration

from AnyQt.QtWidgets import QFormLayout

from Orange.data import Domain
from Orange.preprocess import Preprocess
from orangecontrib.snom.widgets.preprocessors.registry import preprocess_image_editors

from orangecontrib.spectroscopy.preprocess import SelectColumn, CommonDomain

from orangecontrib.spectroscopy.widgets.preprocessors.utils import BaseEditorOrange
from orangecontrib.spectroscopy.widgets.gui import lineEditFloatRange


class AddFeature(SelectColumn):
InheritEq = True


class _AddCommon(CommonDomain):
def __init__(self, amount, domain):
super().__init__(domain)
self.amount = amount

def transformed(self, data):
return data.X + self.amount


class AddConstant(Preprocess):
def __init__(self, amount=0.0):
self.amount = amount

def __call__(self, data):
common = _AddCommon(self.amount, data.domain)
atts = [
a.copy(compute_value=AddFeature(i, common))
for i, a in enumerate(data.domain.attributes)
]
domain = Domain(atts, data.domain.class_vars, data.domain.metas)
return data.from_table(domain, data)


class AddEditor(BaseEditorOrange):
name = "Add constant"
qualname = "orangecontrib.snom.add_constant_test"

def __init__(self, parent=None, **kwargs):
super().__init__(parent, **kwargs)

self.amount = 0.0

form = QFormLayout()
amounte = lineEditFloatRange(self, self, "amount", callback=self.edited.emit)
form.addRow("Addition", amounte)
self.controlArea.setLayout(form)

def activateOptions(self):
pass # actions when user starts changing options

def setParameters(self, params):
self.amount = params.get("amount", 0.0)

@classmethod
def createinstance(cls, params):
params = dict(params)
amount = float(params.get("amount", 0.0))
return AddConstant(amount=amount)

def set_preview_data(self, data):
if data:
pass # TODO any settings


preprocess_image_editors.register(AddEditor, 100)
5 changes: 5 additions & 0 deletions orangecontrib/snom/widgets/preprocessors/registry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from orangecontrib.spectroscopy.widgets.preprocessors.registry import (
PreprocessorEditorRegistry,
)

preprocess_image_editors = PreprocessorEditorRegistry()

0 comments on commit 4a4187c

Please sign in to comment.