-
Notifications
You must be signed in to change notification settings - Fork 10
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
Save the pump-probe output to file #315
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,8 +7,13 @@ | |
Copyright (C) European X-Ray Free-Electron Laser Facility GmbH. | ||
All rights reserved. | ||
""" | ||
from enum import Enum | ||
import os | ||
import os.path as osp | ||
|
||
import numpy as np | ||
from PyQt5.QtCore import Qt | ||
from PyQt5.QtWidgets import QFrame, QVBoxLayout, QSplitter | ||
from PyQt5.QtWidgets import QFileDialog, QFrame, QVBoxLayout, QSplitter | ||
|
||
from .base_window import _AbstractPlotWindow | ||
from ..ctrl_widgets import PumpProbeCtrlWidget | ||
|
@@ -91,6 +96,11 @@ def refresh(self): | |
self._plot.setData(x, y) | ||
|
||
|
||
class SaveFile(Enum): | ||
NPZ = "NumPy Binary File (*.npz)" | ||
TXT = "Text File (*.txt)" | ||
|
||
|
||
class PumpProbeWindow(_AbstractPlotWindow): | ||
"""PumpProbeWindow class.""" | ||
_title = "Pump-probe" | ||
|
@@ -138,8 +148,61 @@ def initUI(self): | |
|
||
def initConnections(self): | ||
"""Override.""" | ||
pass | ||
self._ctrl_widget.save_btn.clicked.connect(self.saveToFile) | ||
|
||
def closeEvent(self, QCloseEvent): | ||
self._ctrl_widget.resetAnalysisType() | ||
super().closeEvent(QCloseEvent) | ||
|
||
def saveToFile(self): | ||
# Get the current data | ||
if not len(self._queue): | ||
return | ||
Comment on lines
+159
to
+160
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better if the button itself was disabled when there is no data to save, rather than silently exiting. |
||
data = self._queue[0] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This changed in #318, the GUI now receives a |
||
|
||
# Open file dialog | ||
filepath, filter = QFileDialog.getSaveFileName( | ||
caption="Save image", | ||
directory=osp.expanduser("~"), | ||
filter=f"{SaveFile.NPZ.value};;{SaveFile.TXT.value}") | ||
filter = SaveFile(filter) | ||
|
||
# Validate filepath | ||
if not filepath: | ||
return | ||
suffix = f".{filter.name.lower()}" | ||
if not filepath.lower().endswith(suffix): | ||
filepath += suffix | ||
|
||
# Copy reference file from tmp folder to desired destination | ||
os.makedirs(osp.dirname(filepath), exist_ok=True) | ||
Comment on lines
+177
to
+178
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment doesn't seem to match the code? And why is it necessary to use |
||
|
||
# Save the data | ||
pp = data.pp | ||
train_id = data.tid | ||
position = pp.x | ||
intensity_on = pp.y_on | ||
intensity_off = pp.y_off | ||
intensity_sub = pp.y | ||
|
||
if position is None: | ||
return | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should show an error dialog to make it clear to the user that nothing happened. |
||
|
||
if filter is SaveFile.NPZ: | ||
np.savez(filepath, | ||
trainId=train_id, | ||
position=position, | ||
intensity_on=intensity_on, | ||
intensity_off=intensity_off, | ||
intensity_subtracted=intensity_sub) | ||
elif filter is SaveFile.TXT: | ||
# write out conversion to tabular ASCII | ||
with open(filepath, 'w') as f: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we use Python's |
||
f.write(f" Train-ID: {train_id}\n\n") | ||
f.write(' q I_on ' | ||
' I_off I_diff\n\n') | ||
for i, q in enumerate(position): | ||
f.write(f"{q:12f}" | ||
f"{intensity_on[i]:16f}" | ||
f"{intensity_off[i]:16f}" | ||
f"{intensity_sub[i]:16f}\n") |
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.
would rather prefer a QAction in a toolbar on this window instead of a button to save to files.
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.
Hmm, @CammilleCC I leave this up to you. I think either UI is acceptable.