datatools.py 2.47 KB
Newer Older
1
from PyQt5 import QtWidgets
Yan's avatar
Yan committed
2
from PyQt5 import QtCore
Yan's avatar
Yan committed
3
import numpy as np
4
import os.path
Yan's avatar
Yan committed
5

Yan's avatar
Yan committed
6

Yan's avatar
Yan committed
7
8
9
def argsubselect(array, minimum, maximum):
    """finds arguments which fits into boundary conditions,
    if there is no fit, return nearest argument"""
10
11
    bounds = sorted([minimum, maximum])
    args = np.where((array >= bounds[0]) & (array <= bounds[1]))[0]
Yan's avatar
Yan committed
12
13
    if len(args) == 0:
        aver = (minimum+maximum)/2
3Yan's avatar
3Yan committed
14
        args = np.array([(np.abs(array - aver)).argmin()])
Yan's avatar
Yan committed
15
    return args
16
17


Yan's avatar
Yan committed
18
19
def specttostr(augCanvas, delim=" "):
    lines = augCanvas.spectplot.get_lines()
20
21
22
23
    setnum = len(lines)
    names = delim.join(["mass ion_count" for i in range(setnum)])
    units = (delim+delim).join(["m/z" for i in range(setnum)])
    description = ("{}_{:.4}-{:.4}_minutes_of_the_aquisition\n".format(
Yan's avatar
Yan committed
24
25
                   os.path.basename(augCanvas.filename),
                   augCanvas.chrom['t_start'], augCanvas.chrom['t_end']))
26
27
28
29
30
31
32
33
34
35
36
37
    header = "\n".join([names, units, description])

    strdata = []
    for i in range(np.max([len(line.get_xdata()) for line in lines])):
        pairs = [("{}"+delim+"{}").format(line.get_xdata()[i], line.get_ydata()[i]) if\
                i < len(line.get_xdata()) else delim for line in lines]
        strline = delim.join(pairs)+"\n"
        strdata.append(strline)
    strdata = "".join(strdata)
    return "{}{}".format(header, strdata)


Yan's avatar
Yan committed
38
39
40
def clip_spectstr(augCanvas):
    string = specttostr(augCanvas, delim="\t")
    [QtWidgets.QApplication.clipboard().setText(string, i) for i in range(2)]
Yan's avatar
Yan committed
41
42


43
44
45
46
47
48
49
50
51
def clip_tablestr(augCanvas):
        pairs = [" ".join((augCanvas.paramstable.item(row, 1).text(),
                           augCanvas.paramstable.item(row, 2).text()))
                 for row in range(augCanvas.paramstable.rowCount())
                 if augCanvas.paramstable.cellWidget(row, 0).checkState()]
        text = ", ".join(pairs)
        [QtWidgets.QApplication.clipboard().setText(text, i) for i in range(2)]


Yan's avatar
Yan committed
52
53
54
55
56
57
58
59
60
61
62
63
64
65
def table(labels, minsizex=600, minsizey=0):
    """creates a reasonable table"""
    table = QtWidgets.QTableWidget(columnCount=len(labels))
    table.setSizePolicy(QtWidgets.QSizePolicy.Expanding,
                        QtWidgets.QSizePolicy.Expanding)
    table.setHorizontalHeaderLabels(labels)
    table.setVerticalScrollBarPolicy(QtCore.Qt.ScrollBarAlwaysOn)

    for n in range(table.columnCount()):
        table.horizontalHeader().setSectionResizeMode(
            n, QtWidgets.QHeaderView.Stretch)
    table.setMinimumSize(minsizex, minsizey)

    return table