Skip to content
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

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions extra_foam/gui/ctrl_widgets/pump_probe_ctrl_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ def __init__(self, *args, **kwargs):
self._abs_difference_cb.setChecked(True)

self._reset_btn = QPushButton("Reset")
self.save_btn = QPushButton("Save to file")
Copy link
Contributor

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.

Copy link
Member

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.

self.save_btn.setFixedWidth(150)

self.initUI()
self.initConnections()
Expand All @@ -88,6 +90,7 @@ def initUI(self):
layout.addWidget(self._off_pulse_le, 1, 3)

layout.addWidget(self._abs_difference_cb, 0, 4, AR)
layout.addWidget(self.save_btn, 2, 4, AR)

self.setLayout(layout)

Expand Down
67 changes: 65 additions & 2 deletions extra_foam/gui/windows/pump_probe_w.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Copy link
Member

Choose a reason for hiding this comment

The 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]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changed in #318, the GUI now receives a dict of all the data from the workers. So here we'd now need self._queue[0]["processed"].


# 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
Copy link
Member

Choose a reason for hiding this comment

The 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 os.makedirs() here? I thought we could trust that any directory returned by QFileDialog.getSaveFileName() already exists since the user would need to create it first in the dialog.


# 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
Copy link
Member

Choose a reason for hiding this comment

The 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:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use Python's csv library here?

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")