Skip to content

Commit

Permalink
Merge pull request #699 from nicholsonbt/unify_cuts
Browse files Browse the repository at this point in the history
[ENH] Preprocess Spectra: Unify cuts
  • Loading branch information
markotoplak authored Dec 19, 2023
2 parents c048dd4 + 00c2ba1 commit 27753a8
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 18 deletions.
3 changes: 1 addition & 2 deletions doc/widgets/preprocess-spectra.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ Below is an example of the **Preprocess Spectra** widget in action with some exp
Preprocessing Methods
---------------------

- Cut (keep): Select the cutoff value of the spectral area you wish to keep.
- Cut (remove): Select the cutoff value of the spectral area you wish to discard.
- Cut: Select the cutoff value of the spectral area and whether you wish to keep or discard that area.
- Gaussian smoothing: apply Gaussian smoothing.
- Savitzky-Golay Filter: apply Savitzky-Golay filter.
- Baseline Correction: correct the baseline
Expand Down
32 changes: 32 additions & 0 deletions orangecontrib/spectroscopy/tests/test_editor_cut.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from orangecontrib.spectroscopy.tests.test_owpreprocess import PreprocessorEditorTest
from orangecontrib.spectroscopy.widgets.owpreprocess import OWPreprocess
from orangecontrib.spectroscopy.tests.test_preprocess import SMALL_COLLAGEN
from orangecontrib.spectroscopy.widgets.preprocessors.misc import CutEditor
from orangecontrib.spectroscopy.preprocess import Cut


class TestCutEditor(PreprocessorEditorTest):

def setUp(self):
self.widget = self.create_widget(OWPreprocess)
self.editor = self.add_editor(CutEditor, self.widget)
self.data = SMALL_COLLAGEN
self.send_signal(self.widget.Inputs.data, self.data)
self.wait_for_preview() # ensure initialization with preview data

def test_no_interaction(self):
p = self.commit_get_preprocessor()
self.assertIsInstance(p, Cut)
self.assertEqual(p.lowlim, 995.902)
self.assertEqual(p.highlim, 1711.779)
self.assertEqual(p.inverse, False)

def test_basic(self):
self.editor.lowlim = 5
self.editor.highlim = 10
self.editor.inverse = True
self.editor.edited.emit()
p = self.commit_get_preprocessor()
self.assertEqual(p.lowlim, 5)
self.assertEqual(p.highlim, 10)
self.assertEqual(p.inverse, True)
6 changes: 6 additions & 0 deletions orangecontrib/spectroscopy/widgets/owpreprocess.py
Original file line number Diff line number Diff line change
Expand Up @@ -946,6 +946,12 @@ def migrate_preprocessor(cls, preprocessor, version):
new_ranges = [[l, r, w, 0.0] for l, r, w in ranges]
settings["ranges"] = new_ranges
version = 7
if name == "orangecontrib.infrared.cut":
name = "orangecontrib.spectroscopy.cut"
settings["inverse"] = False
if name == "orangecontrib.infrared.cutinverse":
name = "orangecontrib.spectroscopy.cut"
settings["inverse"] = True
return [((name, settings), version)]

@classmethod
Expand Down
29 changes: 13 additions & 16 deletions orangecontrib/spectroscopy/widgets/preprocessors/misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ class CutEditor(BaseEditorOrange):
"""
Editor for Cut
"""
name = "Cut (keep)"
qualname = "orangecontrib.infrared.cut"
name = "Cut"
qualname = "orangecontrib.spectroscopy.cut"
replaces = ["orangecontrib.infrared.cut",
"orangecontrib.infrared.cutinverse"]

class Warning(BaseEditorOrange.Warning):
out_of_range = Msg("Limits are out of range.")
Expand All @@ -77,15 +79,21 @@ def __init__(self, parent=None, **kwargs):

self.lowlim = 0.
self.highlim = 1.
self.inverse = False

layout = QFormLayout()
self.controlArea.setLayout(layout)

self._lowlime = lineEditFloatRange(self, self, "lowlim", callback=self.edited.emit)
self._highlime = lineEditFloatRange(self, self, "highlim", callback=self.edited.emit)
self._inverse = gui.radioButtons(self, self, "inverse", orientation=Qt.Horizontal, callback=self.edited.emit)

gui.appendRadioButton(self._inverse, "Keep")
gui.appendRadioButton(self._inverse, "Remove")

layout.addRow("Low limit", self._lowlime)
layout.addRow("High limit", self._highlime)
layout.addRow(None, self._inverse)

self._lowlime.focusIn.connect(self.activateOptions)
self._highlime.focusIn.connect(self.activateOptions)
Expand All @@ -111,13 +119,15 @@ def setParameters(self, params):
self.user_changed = True
self.lowlim = params.get("lowlim", 0.)
self.highlim = params.get("highlim", 1.)
self.inverse = params.get("inverse", False)

@staticmethod
def createinstance(params):
params = dict(params)
lowlim = params.get("lowlim", None)
highlim = params.get("highlim", None)
return Cut(lowlim=floatornone(lowlim), highlim=floatornone(highlim))
inverse = params.get("inverse", None)
return Cut(lowlim=floatornone(lowlim), highlim=floatornone(highlim), inverse=inverse)

def set_preview_data(self, data):
self.Warning.out_of_range.clear()
Expand All @@ -144,18 +154,6 @@ def set_preview_data(self, data):
self.Warning.out_of_range()


class CutEditorInverse(CutEditor):
name = "Cut (remove)"
qualname = "orangecontrib.infrared.cutinverse"

@staticmethod
def createinstance(params):
params = dict(params)
lowlim = params.get("lowlim", None)
highlim = params.get("highlim", None)
return Cut(lowlim=floatornone(lowlim), highlim=floatornone(highlim), inverse=True)


class SpSubtractEditor(BaseEditorOrange):
"""
Editor for preprocess.SpSubtract
Expand Down Expand Up @@ -460,7 +458,6 @@ def update_reference_info(self):


preprocess_editors.register(CutEditor, 25)
preprocess_editors.register(CutEditorInverse, 50)
preprocess_editors.register(GaussianSmoothingEditor, 75)
preprocess_editors.register(SavitzkyGolayFilteringEditor, 100)
preprocess_editors.register(PCADenoisingEditor, 200)
Expand Down

0 comments on commit 27753a8

Please sign in to comment.