-
Notifications
You must be signed in to change notification settings - Fork 3
Add QtGraphs-based analysis page to AdvancedPy example #39
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
AndrewSazonov
wants to merge
11
commits into
develop
Choose a base branch
from
qtgraphs
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
2e143e2
Add qtgraphs example
AndrewSazonov f13672e
Remove unused file
AndrewSazonov 7802c18
Add analysis QML files to AdvancedPy.pyproject
AndrewSazonov 5190d00
Fix point count and X range in mock analysis data generator
AndrewSazonov eaa3b8e
Validate analysis point count input before updating backend
AndrewSazonov ddaad32
Merge remote-tracking branch 'origin/develop' into qtgraphs
AndrewSazonov 53b2437
Rename group on analysis page
AndrewSazonov 3dd99ea
Update comments regarding methods on the analysis page
AndrewSazonov 599dd38
Replace TextEdit with non-editable Text for axis labels
AndrewSazonov a97962b
Get rid of memoryview
AndrewSazonov d0972f8
Add exception when generating new data
AndrewSazonov File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
examples/AdvancedPy/src/AdvancedPy/Backends/MockQml/Analysis.qml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // SPDX-FileCopyrightText: 2024 EasyApp contributors | ||
| // SPDX-License-Identifier: BSD-3-Clause | ||
| // © 2024 Contributors to the EasyApp project <https://github.com/easyscience/EasyApp> | ||
|
|
||
| pragma Singleton | ||
|
|
||
| import QtQuick | ||
| import QtGraphs | ||
|
|
||
| import Gui.Globals as Globals | ||
|
|
||
|
|
||
| QtObject { | ||
|
|
||
| property int dataSize: 50 | ||
| property var axesRanges: { | ||
| "xmin": 0.0, | ||
| "xmax": 180.0, | ||
| "ymin": 0.0, | ||
| "ymax": 100.0, | ||
| } | ||
|
|
||
| signal dataPointsChanged(var points) | ||
|
|
||
| function generateData() { | ||
| console.debug(`* Generating ${dataSize} data points...`) | ||
| const xmin = axesRanges.xmin | ||
| const xmax = axesRanges.xmax | ||
| const ymin = axesRanges.ymin | ||
| const ymax = axesRanges.ymax | ||
|
|
||
| const pointCount = Math.max(1, dataSize) | ||
| const stepSize = pointCount > 1 ? (xmax - xmin) / (pointCount - 1) : 0 | ||
|
|
||
| let dataPoints = [] | ||
| for (let i = 0; i < pointCount; i++) { | ||
| const x = xmin + i * stepSize | ||
| const y = ymin + Math.random() * (ymax - ymin) | ||
| dataPoints.push(Qt.point(x, y)) | ||
| } | ||
| console.debug(" Data generation completed.") | ||
|
|
||
| console.debug(`* Sending ${pointCount} data points to series...`) | ||
| dataPointsChanged(dataPoints) | ||
| console.debug(" Data update signal emitted.") | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| module MockQml | ||
|
|
||
| singleton Project Project.qml | ||
| singleton Analysis Analysis.qml | ||
| singleton Report Report.qml | ||
| singleton Status Status.qml |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
132 changes: 132 additions & 0 deletions
132
examples/AdvancedPy/src/AdvancedPy/Backends/real_py/analysis.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| # SPDX-FileCopyrightText: 2024 EasyApp contributors | ||
| # SPDX-License-Identifier: BSD-3-Clause | ||
| # © 2024 Contributors to the EasyApp project <https://github.com/easyscience/EasyApp> | ||
|
|
||
| import numpy as np | ||
| from PySide6.QtCore import QObject, Signal, Slot, Property, QPointF | ||
|
|
||
| from EasyApp.Logic.Logging import console | ||
|
|
||
|
|
||
| class Analysis(QObject): | ||
| """ | ||
| Backend object that generates synthetic diffraction-like data | ||
| and exposes it to QML as a list of QPointF values plus axis ranges. | ||
| """ | ||
|
|
||
| # Signals | ||
| dataSizeChanged = Signal() | ||
| dataPointsChanged = Signal("QVariantList") # Emitted with list<QPointF> | ||
| axesRangesChanged = Signal() # Emitted when range dict updates | ||
|
|
||
| def __init__(self): | ||
| super().__init__() | ||
|
|
||
| self._dataSize = 10000 | ||
| self._axesRanges = { | ||
| "xmin": 0.0, | ||
| "xmax": 180.0, | ||
| "ymin": 0.0, | ||
| "ymax": 100.0, | ||
| } | ||
|
|
||
| # ------------------------------------------------------------------ | ||
| # QML-accessible Properties | ||
| # ------------------------------------------------------------------ | ||
|
|
||
| @Property(int, notify=dataSizeChanged) | ||
| def dataSize(self): | ||
| """Number of X/Y data points to generate.""" | ||
| return self._dataSize | ||
|
|
||
| @dataSize.setter | ||
| def dataSize(self, value): | ||
| value = int(value) | ||
| if self._dataSize == value: | ||
| return | ||
| self._dataSize = value | ||
| self.dataSizeChanged.emit() | ||
|
|
||
| @Property("QVariantMap", notify=axesRangesChanged) | ||
| def axesRanges(self): | ||
| """ | ||
| Axis ranges used by the graph: | ||
| { "xmin": float, "xmax": float, "ymin": float, "ymax": float } | ||
| Access in QML using: axisX.min: analysis.axesRanges["xmin"] | ||
| """ | ||
| return self._axesRanges | ||
|
|
||
| # ------------------------------------------------------------------ | ||
| # Public Slot Called from QML | ||
| # ------------------------------------------------------------------ | ||
|
|
||
| @Slot() | ||
| def generateData(self): | ||
| """Generate new synthetic data and notify QML.""" | ||
| console.debug(f"* Generating {self.dataSize} data points...") | ||
| try: | ||
| x, y = self._generate_data(n_points=self.dataSize) | ||
| console.debug(" Data generation completed.") | ||
|
|
||
| console.debug(f"* Converting and sending {self.dataSize} data points to series...") | ||
| self.dataPointsChanged.emit(self._ndarrays_to_qpoints(x, y)) | ||
| console.debug(" Data update signal emitted.") | ||
|
|
||
| self._updateAxesRanges(x.min(), x.max(), y.min(), y.max()) | ||
| except Exception as exception: | ||
| console.error(f"Failed to generate analysis data: {exception}") | ||
|
|
||
| # ------------------------------------------------------------------ | ||
| # Internal Helpers | ||
| # ------------------------------------------------------------------ | ||
|
|
||
| def _updateAxesRanges(self, xmin, xmax, ymin, ymax): | ||
| """Store axis ranges and notify QML.""" | ||
| vmargin = 10.0 | ||
| self._axesRanges["xmin"] = float(xmin) | ||
| self._axesRanges["xmax"] = float(xmax) | ||
| self._axesRanges["ymin"] = max(0, float(ymin) - vmargin) | ||
| self._axesRanges["ymax"] = float(ymax) + vmargin | ||
| self.axesRangesChanged.emit() | ||
|
|
||
| @staticmethod | ||
| def _ndarrays_to_qpoints(x: np.ndarray, y: np.ndarray): | ||
| """ | ||
| Convert NumPy X/Y arrays to list[QPointF]. | ||
| """ | ||
| return [QPointF(xi, yi) for xi, yi in zip(x, y, strict=True)] | ||
|
|
||
| @staticmethod | ||
| def _generate_data( | ||
| n_points=2000, | ||
| n_peaks=100, | ||
| x_range=(0.0, 180.0), | ||
| intensity_range=(0, 100), | ||
| width_range=(0.05, 0.5), | ||
| noise_level=1.0, | ||
| background=20.0, | ||
| ): | ||
| """ | ||
| Generate synthetic diffraction-like pattern from sum of random Gaussians. | ||
| Returns (x, y) NumPy arrays. | ||
| """ | ||
| # Sample x grid | ||
| x = np.linspace(*x_range, n_points) | ||
| y = np.zeros_like(x) | ||
|
|
||
| # Random peak positions, intensities, widths | ||
| positions = np.random.uniform(*x_range, n_peaks) | ||
| amplitudes = np.random.uniform(*intensity_range, n_peaks) | ||
| widths = np.random.uniform(*width_range, n_peaks) | ||
|
|
||
| # Gaussian peak contributions | ||
| for pos, amp, width in zip(positions, amplitudes, widths): | ||
| y += amp * np.exp(-0.5 * ((x - pos) / width) ** 2) | ||
|
|
||
| # Noise | ||
| y += np.random.normal(scale=noise_level, size=n_points) | ||
|
|
||
| # Background | ||
| y += background | ||
|
|
||
| return x, y |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
examples/AdvancedPy/src/AdvancedPy/Gui/Pages/Analysis/Layout.qml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| // SPDX-FileCopyrightText: 2024 EasyApp contributors | ||
| // SPDX-License-Identifier: BSD-3-Clause | ||
| // © 2024 Contributors to the EasyApp project <https://github.com/easyscience/EasyApp> | ||
|
|
||
| import QtQuick | ||
| import QtQuick.Controls | ||
|
|
||
| import EasyApp.Gui.Style as EaStyle | ||
| import EasyApp.Gui.Globals as EaGlobals | ||
| import EasyApp.Gui.Elements as EaElements | ||
| import EasyApp.Gui.Components as EaComponents | ||
|
|
||
| import Gui.Globals as Globals | ||
|
|
||
|
|
||
| EaComponents.ContentPage { | ||
|
|
||
| mainView: EaComponents.MainContent { | ||
| tabs: [ | ||
| EaElements.TabButton { text: qsTr('Chart') } | ||
| ] | ||
|
|
||
| items: [ | ||
| Loader { source: 'MainArea/Chart.qml' } | ||
| ] | ||
| } | ||
|
|
||
| sideBar: EaComponents.SideBar { | ||
| tabs: [ | ||
| EaElements.TabButton { text: qsTr('Basic controls') } | ||
| ] | ||
|
|
||
| items: [ | ||
| Loader { source: 'Sidebar/Basic/Layout.qml' } | ||
| ] | ||
|
|
||
| continueButton.text: qsTr('Continue') | ||
|
|
||
| continueButton.onClicked: { | ||
| console.debug(`Clicking '${continueButton.text}' button ::: ${this}`) | ||
| Globals.References.applicationWindow.appBarCentralTabs.summaryButton.enabled = true | ||
| Globals.References.applicationWindow.appBarCentralTabs.summaryButton.toggle() | ||
| } | ||
| } | ||
|
|
||
| Component.onCompleted: console.debug(`Analysis page loaded ::: ${this}`) | ||
| Component.onDestruction: console.debug(`Analysis page destroyed ::: ${this}`) | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the default
dataSizein correspondinganalysis.pyis set to 10000. Why the difference?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This mismatch is intentional. In the mock backend I only generate random noise, so I kept the dataset small to keep the chart responsive during QML-side development. In the Python backend the generated data is closer to a real peak pattern, and I chose 10000 to demonstrate that chart updates are still smooth at that size.