Skip to content

Commit

Permalink
Merge pull request #590 from Sjoerd1993/Implement-smoothen-options
Browse files Browse the repository at this point in the history
Implement smoothen options
  • Loading branch information
sstendahl authored Nov 17, 2023
2 parents 48f7bb0 + 698e47c commit f27aa6c
Show file tree
Hide file tree
Showing 11 changed files with 189 additions and 12 deletions.
1 change: 1 addition & 0 deletions data/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ blueprints_source = [
'ui/import.blp',
'ui/help_overlay.blp',
'ui/item_box.blp',
'ui/smoothen_settings.blp',
'ui/style_color_box.blp',
'ui/style_editor.blp',
'ui/style_preview.blp',
Expand Down
1 change: 1 addition & 0 deletions data/se.sjoerd.Graphs.gresource.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<file preprocess="xml-stripblanks">ui/fitting_parameters.ui</file>
<file preprocess="xml-stripblanks">ui/import.ui</file>
<file preprocess="xml-stripblanks">ui/item_box.ui</file>
<file preprocess="xml-stripblanks">ui/smoothen_settings.ui</file>
<file preprocess="xml-stripblanks">ui/style_color_box.ui</file>
<file preprocess="xml-stripblanks">ui/style_editor.ui</file>
<file preprocess="xml-stripblanks">ui/style_preview.ui</file>
Expand Down
21 changes: 21 additions & 0 deletions data/se.sjoerd.Graphs.gschema.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
<value nick="middle-x" value="1"/>
</enum>

<enum id="se.sjoerd.Graphs.actions.smoothen-types">
<value nick="savgol" value="0"/>
<value nick="moving-average" value="1"/>
</enum>

<enum id="se.sjoerd.Graphs.export-figure.file-formats">
<value nick="Encapsulated Postscript" value="0"/>
<value nick="Joint Photographic Experts Group" value="1"/>
Expand Down Expand Up @@ -54,9 +59,25 @@
</schema>

<schema id="se.sjoerd.Graphs.actions">
<child name="smoothen" schema="se.sjoerd.Graphs.actions.smoothen"/>
<key name="center" enum="se.sjoerd.Graphs.actions.center-values">
<default>"middle-x"</default>
</key>
<key name="smoothen" enum="se.sjoerd.Graphs.actions.smoothen-types">
<default>"savgol"</default>
</key>
</schema>

<schema id="se.sjoerd.Graphs.actions.smoothen">
<key name="savgol-window" type="i">
<default>10</default>
</key>
<key name="savgol-polynomial" type="i">
<default>3</default>
</key>
<key name="moving-average-box" type="i">
<default>4</default>
</key>
</schema>

<schema id="se.sjoerd.Graphs.figure">
Expand Down
67 changes: 67 additions & 0 deletions data/ui/smoothen_settings.blp
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
using Gtk 4.0;
using Adw 1;

template $GraphsSmoothenWindow : Adw.Window {
modal: true;
default-height: 125;
title: _("Smoothen settings");
focus-widget: reset_button;

ShortcutController {
Shortcut {
trigger: "Escape";
action: "action(window.close)";
}
}

Adw.ToolbarView {
[top]
Adw.HeaderBar {
[start]
Button reset_button {
icon-name: "history-undo-symbolic";
}
}

content: Adw.Clamp {
margin-start: 12;
margin-end: 12;
margin-top: 12;
margin-bottom: 12;

Box {
orientation: vertical;
spacing: 10;
Adw.PreferencesGroup {
title: _("Savitzky–Golay filter");
Adw.SpinRow savgol_window {
title: _("Span percentage");
subtitle: _("What percentage of the data span to use for the filter window");
adjustment: Adjustment {
step-increment: 1;
upper: 99;
};
}
Adw.SpinRow savgol_polynomial {
title: _("Polynomial degree");
adjustment: Adjustment {
step-increment: 1;
upper: 20;
};
}
}
Adw.PreferencesGroup {
title: _("Moving Average");
Adw.SpinRow moving_average_box {
title: _("Box points");
subtitle: _("Amount of points to use when calculating a moving average");
adjustment: Adjustment {
step-increment: 1;
upper: 9999;
};
}
}
}
};
}
}
30 changes: 27 additions & 3 deletions data/ui/window.blp
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ template $GraphsWindow: Adw.ApplicationWindow {
orientation: vertical;
width-request: 350;
Adw.HeaderBar sidebar_headerbar {
width-request: 350;
styles ["flat"]
[start]
MenuButton add_button {
Expand Down Expand Up @@ -279,6 +278,7 @@ template $GraphsWindow: Adw.ApplicationWindow {
Button normalize_button {
hexpand: true;
sensitive: bind shift_button.sensitive;
width-request: 130;
layout {
column: 1;
row: 0;
Expand All @@ -292,19 +292,22 @@ template $GraphsWindow: Adw.ApplicationWindow {
clicked => $perform_operation();
}

Button smoothen_button {
hexpand: true;
Adw.SplitButton smoothen_button {
hexpand: false;
can-shrink: true;
sensitive: bind shift_button.sensitive;
layout {
column: 0;
row: 1;
}
Adw.ButtonContent {
halign: center;
can-shrink: true;
icon-name: "smoothen-symbolic";
label: _("Smoothen");
}
tooltip-text: _("Smoothen data");
menu-model: smoothen_menu;
clicked => $perform_operation();
}

Expand Down Expand Up @@ -772,4 +775,25 @@ menu center_menu {
action: "app.center";
target: "middle-x";
}
}

menu smoothen_menu {
section {
item {
label: _("Savitzky–Golay filter");
action: "app.smoothen";
target: "savgol";
}
item {
label: _("Moving average");
action: "app.smoothen";
target: "moving-average";
}
}
section {
item {
label: _("Advanced settings");
action: "app.smoothen_settings";
}
}
}
22 changes: 20 additions & 2 deletions src/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
"""Main actions."""
from gettext import gettext as _

from gi.repository import Graphs

from graphs import operations, ui, utilities
from graphs.add_equation import AddEquationWindow
from graphs.export_figure import ExportFigureWindow
Expand All @@ -18,9 +20,15 @@ def perform_operation(_action, target, self):
elif operation == "cut" and self.get_mode() != 2:
return
args = []
if operation in ("center", ):
if operation in ("center", "smoothen"):
args = [self.get_settings("actions").get_enum(operation)]
if operation == "shift":
if operation == "smoothen":
params = {}
settings = self.get_settings("actions").get_child("smoothen")
for setting in settings:
params[setting] = int(settings.get_int(setting))
args += [params]
elif operation == "shift":
figure_settings = self.get_data().get_figure_settings()
right_range = (figure_settings.get_max_right()
- figure_settings.get_min_right())
Expand Down Expand Up @@ -123,6 +131,16 @@ def save_project_action(_action, _target, self):
ui.save_project_dialog(self)


def smoothen_settings_action(_action, _target, self):
def _on_reset(_button, self):
params = self.get_settings("actions").get_child("smoothen")
for key in params.list_keys():
params.reset(key)

window = Graphs.SmoothenWindow.new(self)
window.get_reset_button().connect("clicked", _on_reset, self)


def zoom_in_action(_action, _target, self):
canvas = self.get_window().get_canvas()
canvas.zoom(1.15, respect_mouse=False)
Expand Down
8 changes: 5 additions & 3 deletions src/application.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
"quit", "about", "figure_settings", "add_data", "add_equation",
"select_all", "select_none", "undo", "redo", "optimize_limits",
"view_back", "view_forward", "export_data", "export_figure",
"save_project", "open_project", "delete_selected", "zoom_in", "zoom_out",
"save_project", "smoothen_settings", "open_project", "delete_selected",
"zoom_in", "zoom_out",
]


Expand Down Expand Up @@ -99,8 +100,9 @@ def __init__(self, application_id, **kwargs):
operation_action.connect("activate", actions.perform_operation, self)
self.add_action(operation_action)

center_action = settings.get_child("actions").create_action("center")
self.add_action(center_action)
for action_key in ["center", "smoothen"]:
action = settings.get_child("actions").create_action(action_key)
self.add_action(action)

self.get_data().connect(
"notify::items", ui.on_items_change, self,
Expand Down
1 change: 1 addition & 0 deletions src/meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ graphs_lib = shared_library(
'item.vala',
'inline-stack-switcher.vala',
'misc.vala',
'smoothen_settings.vala',
'utilities.vala',
'window.vala',
), blueprints_hack, gresource_bundle,
Expand Down
16 changes: 12 additions & 4 deletions src/operations.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,19 @@ def normalize(_item, xdata, ydata):
return xdata, [value / max(ydata) for value in ydata], False, False


def smoothen(_item, xdata, ydata):
def smoothen(_item, xdata, ydata, smooth_type, params):
"""Smoothen y-data."""
box_points = 4
box = numpy.ones(box_points) / box_points
new_ydata = numpy.convolve(ydata, box, mode="same")
if smooth_type == 0:
minimum = params["savgol-polynomial"] + 1
window_percentage = params["savgol-window"] / 100
window = max(minimum, int(len(xdata) * window_percentage))
new_ydata = scipy.signal.savgol_filter(ydata,
window,
params["savgol-polynomial"])
elif smooth_type == 1:
box_points = params["moving-average-box"]
box = numpy.ones(box_points) / box_points
new_ydata = numpy.convolve(ydata, box, mode="same")
return xdata, new_ydata, False, False


Expand Down
31 changes: 31 additions & 0 deletions src/smoothen_settings.vala
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// SPDX-License-Identifier: GPL-3.0-or-later
using Adw;
using Gtk;

namespace Graphs {
[GtkTemplate (ui = "/se/sjoerd/Graphs/ui/smoothen_settings.ui")]
public class SmoothenWindow : Adw.Window {
[GtkChild]
public unowned Adw.SpinRow savgol_window { get; }

[GtkChild]
public unowned Adw.SpinRow savgol_polynomial { get; }

[GtkChild]
public unowned Adw.SpinRow moving_average_box { get; }

[GtkChild]
public unowned Button reset_button { get; }

public SmoothenWindow (Application application) {
Object (
application: application,
transient_for: application.window
);
Tools.bind_settings_to_widgets (
application.settings.get_child ("actions").get_child ("smoothen"), this
);
present ();
}
}
}
3 changes: 3 additions & 0 deletions src/utilities.vala
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ namespace Graphs {
settings.bind (key, widget, "enable-expansion", 0);
widget.set_expanded (true);
}
else if (widget is Adw.SpinRow) {
settings.bind (key, widget, "value", 0);
}
}
}
}
Expand Down

0 comments on commit f27aa6c

Please sign in to comment.