Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Sep 15, 2023
1 parent bd8f1ec commit 279d051
Show file tree
Hide file tree
Showing 53 changed files with 2,286 additions and 2,078 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ Reduction scripts for the Liquids Reflectometer. This includes both automated re
- reduction v2.0.8 [04/2023] Move pre/post cuts to template.
- reduction v2.0.9 [04/2023] Subtract normalization background & add x-direction option
- reduction v2.0.13 [08/2023] Get correct angle with free liquids

2 changes: 1 addition & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,4 @@
- Catch index error in reduce_30Hz_from_ws()

**RefRed**
- Add option to LRDirectBeamSort to use the order of the files are given instead of sorting them.
- Add option to LRDirectBeamSort to use the order of the files are given instead of sorting them.
140 changes: 65 additions & 75 deletions launcher/apps/dynamic_30Hz.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,34 @@
#!/usr/bin/python3
import sys
import os
import json
import os
import subprocess

from qtpy import QtWidgets, QtGui, QtCore

from qtpy.QtWidgets import (QWidget, QGridLayout,
QFileDialog, QLabel,
QPushButton, QMessageBox)

from qtpy import QtCore, QtGui, QtWidgets
from qtpy.QtWidgets import QFileDialog, QGridLayout, QLabel, QMessageBox, QPushButton, QWidget

REFERENCE_DIRECTIVE = "Click to choose a 60Hz reference R(Q) file"
TEMPLATE_DIRECTIVE = "Click to choose a 30Hz template"
OUTPUT_DIR_DIRECTIVE = "Click to choose an output directory"


class Dynamic30Hz(QWidget):

def __init__(self):
QWidget.__init__(self)
self.setWindowTitle('Time-resolved reduction')
self.setWindowTitle("Time-resolved reduction")
layout = QGridLayout()
self.setLayout(layout)

self.settings = QtCore.QSettings()

# 30Hz template file
self.choose_template = QPushButton('30Hz template')
self.choose_template = QPushButton("30Hz template")
layout.addWidget(self.choose_template, 1, 1)

self.template_path = QLabel(self)
layout.addWidget(self.template_path, 1, 2)

# 60Hz reference file
self.choose_ref = QPushButton('60Hz R(Q) reference')
self.choose_ref = QPushButton("60Hz R(Q) reference")
layout.addWidget(self.choose_ref, 2, 1)

self.ref_path = QLabel(self)
Expand All @@ -50,7 +44,7 @@ def __init__(self):

# 30Hz data to process
self.data_run_number_ledit = QtWidgets.QLineEdit()
#self.data_run_number_ledit.setValidator(QtGui.QIntValidator())
# self.data_run_number_ledit.setValidator(QtGui.QIntValidator())
layout.addWidget(self.data_run_number_ledit, 4, 1)
self.data_run_number_label = QLabel(self)
self.data_run_number_label.setText("Enter a 30Hz run number to reduce")
Expand All @@ -65,20 +59,20 @@ def __init__(self):
layout.addWidget(self.time_slice_label, 5, 2)

# Output directory
self.choose_output_dir = QPushButton('Output directory')
self.choose_output_dir = QPushButton("Output directory")
layout.addWidget(self.choose_output_dir, 6, 1)

self.output_dir_label = QLabel(self)
layout.addWidget(self.output_dir_label, 6, 2)

# Process button
self.perform_reduction_old = QPushButton('Reduce [old]')
self.perform_reduction_old = QPushButton("Reduce [old]")
layout.addWidget(self.perform_reduction_old, 7, 1)
self.perform_reduction = QPushButton('Reduce [new]')
self.perform_reduction = QPushButton("Reduce [new]")
layout.addWidget(self.perform_reduction, 7, 2)
#self.perform_reduction_q = QPushButton('Reduce [Const-Q binning]')
#layout.addWidget(self.perform_reduction_q, 8, 2)
self.load_settings = QPushButton('Load settings')
# self.perform_reduction_q = QPushButton('Reduce [Const-Q binning]')
# layout.addWidget(self.perform_reduction_q, 8, 2)
self.load_settings = QPushButton("Load settings")
layout.addWidget(self.load_settings, 8, 2)

# connections
Expand All @@ -87,62 +81,54 @@ def __init__(self):
self.choose_output_dir.clicked.connect(self.output_dir_selection)
self.perform_reduction_old.clicked.connect(self.reduce_old)
self.perform_reduction.clicked.connect(self.reduce_new)
#self.perform_reduction_q.clicked.connect(self.reduce_q)
# self.perform_reduction_q.clicked.connect(self.reduce_q)
self.load_settings.clicked.connect(self.load_settings_from_file)

# Populate from previous session
self.read_settings()

def load_settings_from_file(self):
"""
Load the reduction options from a json file produced as the output
of an earlier reduction. This file is saved in the same directory
as the time-resolved reflectivity curves.
Load the reduction options from a json file produced as the output
of an earlier reduction. This file is saved in the same directory
as the time-resolved reflectivity curves.
"""
_settings_file, _ = QFileDialog.getOpenFileName(self, 'Open file',
self.output_dir_label.text(),
'Settings file (*.json)')
_settings_file, _ = QFileDialog.getOpenFileName(self, "Open file", self.output_dir_label.text(), "Settings file (*.json)")

if os.path.isfile(_settings_file):
with open(_settings_file, 'r') as fd:
with open(_settings_file, "r") as fd:
options = json.load(fd)

if 'template_30Hz' in options:
self.template_path.setText(options['template_30Hz'])
if "template_30Hz" in options:
self.template_path.setText(options["template_30Hz"])

if 'ref_data_60Hz' in options:
self.ref_path.setText(options['ref_data_60Hz'])
if "ref_data_60Hz" in options:
self.ref_path.setText(options["ref_data_60Hz"])

if 'output_dir' in options:
self.output_dir_label.setText(options['output_dir'])
if "output_dir" in options:
self.output_dir_label.setText(options["output_dir"])

if 'ref_run_30Hz' in options:
self.ref_run_number_ledit.setText(str(options['ref_run_30Hz']))
if "ref_run_30Hz" in options:
self.ref_run_number_ledit.setText(str(options["ref_run_30Hz"]))

if 'meas_run_30Hz' in options:
self.data_run_number_ledit.setText(str(options['meas_run_30Hz']))
if "meas_run_30Hz" in options:
self.data_run_number_ledit.setText(str(options["meas_run_30Hz"]))

if 'time_interval' in options:
self.time_slice_ledit.setText(str(options['time_interval']))
if "time_interval" in options:
self.time_slice_ledit.setText(str(options["time_interval"]))

def template_selection(self):
_template_file, _ = QFileDialog.getOpenFileName(self, 'Open file',
self.template_path.text(),
'Template file (*.xml)')
_template_file, _ = QFileDialog.getOpenFileName(self, "Open file", self.template_path.text(), "Template file (*.xml)")
if os.path.isfile(_template_file):
self.template_path.setText(_template_file)

def ref_selection(self):
_ref_file, _ = QFileDialog.getOpenFileName(self, 'Open file',
self.ref_path.text(),
'60Hz reference file (*.txt)')
_ref_file, _ = QFileDialog.getOpenFileName(self, "Open file", self.ref_path.text(), "60Hz reference file (*.txt)")
if os.path.isfile(_ref_file):
self.ref_path.setText(_ref_file)

def output_dir_selection(self):
_dir = QFileDialog.getExistingDirectory(None, 'Select a folder:',
self.output_dir_label.text(),
QFileDialog.ShowDirsOnly)
_dir = QFileDialog.getExistingDirectory(None, "Select a folder:", self.output_dir_label.text(), QFileDialog.ShowDirsOnly)
if os.path.isdir(_dir):
self.output_dir_label.setText(_dir)

Expand All @@ -162,22 +148,22 @@ def read_settings(self):
_out_dir = OUTPUT_DIR_DIRECTIVE
self.output_dir_label.setText(_out_dir)

_ref_run = self.settings.value("30Hz_ref_run_number", '')
_ref_run = self.settings.value("30Hz_ref_run_number", "")
self.ref_run_number_ledit.setText(_ref_run)

_data_run = self.settings.value("30Hz_data_run_number", '')
_data_run = self.settings.value("30Hz_data_run_number", "")
self.data_run_number_ledit.setText(_data_run)

_interval = self.settings.value("30Hz_time_slice", '')
_interval = self.settings.value("30Hz_time_slice", "")
self.time_slice_ledit.setText(_interval)

def save_settings(self):
self.settings.setValue('30Hz_template', self.template_path.text())
self.settings.setValue('30Hz_reference', self.ref_path.text())
self.settings.setValue('30Hz_ref_run_number', self.ref_run_number_ledit.text())
self.settings.setValue('30Hz_data_run_number', self.data_run_number_ledit.text())
self.settings.setValue('30Hz_time_slice', self.time_slice_ledit.text())
self.settings.setValue('30Hz_output_dir', self.output_dir_label.text())
self.settings.setValue("30Hz_template", self.template_path.text())
self.settings.setValue("30Hz_reference", self.ref_path.text())
self.settings.setValue("30Hz_ref_run_number", self.ref_run_number_ledit.text())
self.settings.setValue("30Hz_data_run_number", self.data_run_number_ledit.text())
self.settings.setValue("30Hz_time_slice", self.time_slice_ledit.text())
self.settings.setValue("30Hz_output_dir", self.output_dir_label.text())

def check_inputs(self):
error = None
Expand All @@ -204,29 +190,29 @@ def show_dialog(self, text):

def parse_run_list(self, text):
"""
Parse the run list string and expand it.
Parse the run list string and expand it.
"""
run_list = []
for _r in text.split(','):
for _r in text.split(","):
try:
run_list.append(int(_r))
except:
sub_toks = _r.split('-')
sub_toks = _r.split("-")
if len(sub_toks) == 2:
run_list.extend(range(int(sub_toks[0]), int(sub_toks[1])+1))
run_list.extend(range(int(sub_toks[0]), int(sub_toks[1]) + 1))

return run_list

def reduce_old(self):
return self.reduce(reduction_script='scripts/template_reduction.py')
return self.reduce(reduction_script="scripts/template_reduction.py")

def reduce_new(self):
return self.reduce(reduction_script='scripts/time_resolved_reduction.py')
return self.reduce(reduction_script="scripts/time_resolved_reduction.py")

def reduce_q(self):
return self.reduce(reduction_script='scripts/time_resolved_reduction.py', q_summing=True)
return self.reduce(reduction_script="scripts/time_resolved_reduction.py", q_summing=True)

def reduce(self, reduction_script='scripts/time_resolved_reduction.py', q_summing=False):
def reduce(self, reduction_script="scripts/time_resolved_reduction.py", q_summing=False):
if not self.check_inputs():
print("Invalid inputs found")
return
Expand All @@ -238,15 +224,19 @@ def reduce(self, reduction_script='scripts/time_resolved_reduction.py', q_summin
run_list = self.parse_run_list(self.data_run_number_ledit.text())
for run in run_list:
# python3 template_reduction.py dynamic30Hz <meas_run_30Hz> <ref_run_30Hz> <ref_data_60Hz> <template_30Hz> <time_interval> <output_dir>
args = ['python3', reduction_script, 'dynamic30Hz',
str(run),
self.ref_run_number_ledit.text(),
self.ref_path.text(),
self.template_path.text(),
self.time_slice_ledit.text(),
self.output_dir_label.text()]
args = [
"python3",
reduction_script,
"dynamic30Hz",
str(run),
self.ref_run_number_ledit.text(),
self.ref_path.text(),
self.template_path.text(),
self.time_slice_ledit.text(),
self.output_dir_label.text(),
]
if not run == run_list[-1]:
args.append('--no-plot')
args.append("--no-plot")
if q_summing:
args.append('--qsumming')
args.append("--qsumming")
subprocess.run(args)
Loading

0 comments on commit 279d051

Please sign in to comment.