reactivitytools.py 5.64 KB
Newer Older
Yan's avatar
Yan committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#!/usr/bin/env python3
from matplotlib.backends.backend_qt5agg import\
        FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from PyQt5 import QtCore
from PyQt5 import QtWidgets
from PyQt5 import QtGui
from PyQt5 import QtPrintSupport
import matplotlib
import numpy as np
import prasopes.datatools as dt
import prasopes.graphtools as gt
import prasopes.filetools as ft
import prasopes.config as cf
import prasopes.drltools as drl
import os.path
import logging
matplotlib.use("Qt5Agg")


3Yan's avatar
3Yan committed
21
logger = logging.getLogger('reactivityLogger')
Yan's avatar
Yan committed
22

3Yan's avatar
3Yan committed
23
24
25
26
27
28
29
30
31
32
def update_parselect(augCanvas, parselect):
    index = parselect.currentIndex()
    if index == -1:
        index = cf.settings().value("reactivity/index", type=int)
    parlist = [ ": ".join([str(i), j]) for i,j in
               enumerate(augCanvas.ms['params'][0])]
    parselect.clear()
    parselect.addItems(parlist)
    if index <= len(parlist):
        parselect.setCurrentIndex(index)
Yan's avatar
Yan committed
33

3Yan's avatar
3Yan committed
34
def pop_dial(augCanvas, drls, graph, labels, parselect, coef1, coef2):
Yan's avatar
Yan committed
35
36
37
38
    logger.debug("populating reactivity dialog")
    # Do not do anything when data set is not populated
    if len(augCanvas.ds) == 0:
        return
3Yan's avatar
3Yan committed
39
    update_parselect(augCanvas, parselect)
Yan's avatar
Yan committed
40
41
42
43
44
    graph.clear()
    gt.pop_plot([0], [0], graph, labels)
    names, times, intensities = drl.get_daughterset(augCanvas.ds, drls)
    if len(names) < 2:
        return
3Yan's avatar
3Yan committed
45
46
    params = augCanvas.ms['params'][1]
    parlen = len(params)
Yan's avatar
Yan committed
47
    pressures = []
3Yan's avatar
3Yan committed
48
    lastpos = 0
Yan's avatar
Yan committed
49
50
    for time in times:
        toavg = []
3Yan's avatar
3Yan committed
51
52
        for i in range(lastpos,parlen):
            if float(params[i][0]) == time:
3Yan's avatar
3Yan committed
53
54
                toavg.append((float(params[i][parselect.currentIndex()])
                              -coef1.value())*coef2.value())
3Yan's avatar
3Yan committed
55
                lastpos = i
3Yan's avatar
3Yan committed
56
57
58
            elif float(params[i][0]) > time and i > 0:
                # i>0 condition to handle possibility of invalid first scan.
                # (was observed in-wild on TSQ once)
3Yan's avatar
3Yan committed
59
                break
Yan's avatar
Yan committed
60
61
        if len(toavg) != 0:
            pressures.append([time, np.average(toavg)])
3Yan's avatar
3Yan committed
62
63
64
65
66
67
    if len(pressures) == 0:
        QtWidgets.QMessageBox.critical(None, "No times loaded",
                "Did not located any valid parameters.\n"
                "It is either start of the acquisition,\n"
                "or the timestamps has been corrupted.")
        return
3Yan's avatar
3Yan committed
68
69
    nptpressures = np.asarray(pressures).T[0]
    goodtimes = np.where([t in nptpressures for t in times])
Yan's avatar
Yan committed
70
71
72
73
74
75
76
    for i in range(1,len(intensities)):
        relint = np.divide(intensities[i], np.clip(np.sum(
            intensities, 0), np.finfo(np.float32).eps, None),
            dtype=np.float64)
        graph.plot(np.asarray(pressures).T[1], relint[goodtimes], label=names[i],
                color=(gt.colors[i % len(gt.colors)] / 255), marker=".",
                markersize=2, linestyle="None")
3Yan's avatar
3Yan committed
77
78
    graph.autoscale(True)
    graph.figure.canvas.draw()
Yan's avatar
Yan committed
79
80
81
82
83
84
85
86
87
88
89


def main_window(parent, augCanvas, update_signal, drls):
    """constructs a dialog window"""
    reactlabels = dict(name="", xlabel="pressure (mT)", ylabel="rel intensity")
    def onclose(widget, event, update_fnc):
        logger.debug("ZCE window custom close routine called")
        update_signal.signal.disconnect(update_fnc)
        QtWidgets.QDialog.closeEvent(widget, event)

    def update_fnc():
3Yan's avatar
3Yan committed
90
91
        pop_dial(augCanvas, drls, dialspect, reactlabels, parselect,
                 coef1, coef2)
Yan's avatar
Yan committed
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106

    dial_widget = QtWidgets.QDialog(
            parent, windowTitle='TSQ reactivity interpreter')
    dial_widget.closeEvent = lambda event: onclose(
        dial_widget, event, update_fnc)
    update_signal.signal.connect(update_fnc)
    dial_graph = Figure(figsize=(5, 2), dpi=100, facecolor="None")
    dialspect = dial_graph.add_subplot(111, facecolor=(1, 1, 1, 0.8))
    graph_canvas = FigureCanvas(dial_graph)
    graph_canvas.setStyleSheet("background-color:transparent;")
    graph_canvas.setAutoFillBackground(False)

    gt.zoom_factory(dialspect, 1.15, reactlabels)
    gt.pan_factory(dialspect, reactlabels)

3Yan's avatar
3Yan committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
    parlabel = QtWidgets.QLabel("Parameter: ")
    parselect = QtWidgets.QComboBox()
    parselect.currentIndexChanged.connect(lambda x:
            cf.settings().setValue("reactivity/index", x))
    formula = QtWidgets.QLabel(
        "Formula for the x-axis: (Parameter - a) * b")
    label1 = QtWidgets.QLabel("a: ", alignment=130)
    coef1 = QtWidgets.QDoubleSpinBox(
        decimals=4, minimum=float("-inf"), maximum=float("inf"))
    coef1.setValue(cf.settings().value("reactivity/coef1", type=float))
    coef1.valueChanged.connect(lambda x:
            cf.settings().setValue("reactivity/coef1", x))
    label2 = QtWidgets.QLabel("b: ", alignment=130)
    coef2 = QtWidgets.QDoubleSpinBox(
        decimals=4, minimum=float("-inf"), maximum=float("inf"))
    coef2.setValue(cf.settings().value("reactivity/coef2", type=float))
    coef2.valueChanged.connect(lambda x:
            cf.settings().setValue("reactivity/coef2", x))
    pushbtn = QtWidgets.QPushButton("Recalculate")
    pushbtn.clicked.connect(lambda: pop_dial(
        augCanvas, drls, dialspect, reactlabels, parselect, coef1, coef2))

    param_layout = QtWidgets.QHBoxLayout()
    [param_layout.addWidget(i) for i in [parlabel, parselect]]
    param_layout.addStretch()

    coef_layout = QtWidgets.QHBoxLayout()
    for i in [label1, coef1, label2, coef2]:
        coef_layout.addWidget(i)

Yan's avatar
Yan committed
137
138
    dial_layout = QtWidgets.QVBoxLayout(dial_widget)
    dial_layout.addWidget(graph_canvas)
3Yan's avatar
3Yan committed
139
140
141
142
    dial_layout.addLayout(param_layout)
    dial_layout.addWidget(formula)
    dial_layout.addLayout(coef_layout)
    dial_layout.addWidget(pushbtn)
Yan's avatar
Yan committed
143
144
    dial_widget.setFocus()
    dial_widget.show()
3Yan's avatar
3Yan committed
145
    pop_dial(augCanvas, drls, dialspect, reactlabels, parselect, coef1, coef2)