Commit 57899ed8 authored by Yan's avatar Yan
Browse files

Completely changed signalising and tables

* drltools heavily rewritten to less spaghetti-like code
* reduced number of calls produced by the user
* minimized call impact only the logical one
* TODO: make daughter ion-table generation less demanding
parent 7d9728b3
......@@ -2,19 +2,6 @@ from PyQt5 import QtGui
import numpy as np
def floatize(imptext):
"""grabs the text and transforms it safely to folat,
if the text is not acceptable as float, returns zero"""
validator = QtGui.QDoubleValidator()
validator.setBottom(0)
status = validator.validate(imptext, 0)[0]
if status == QtGui.QValidator.Acceptable:
outfloat = float(imptext)
else:
outfloat = 0
return outfloat
def argsubselect(array, minimum, maximum):
"""finds arguments which fits into boundary conditions,
if there is no fit, return nearest argument"""
......
......@@ -21,12 +21,29 @@ class HBar(QtWidgets.QFrame):
self.setFrameShape(QtWidgets.QFrame.HLine)
def update_profile(start, end, spectrum, dataset):
def floatize(table, row, column):
"""grabs the tableWidgetItem and transforms its text safely to
float, if the text is not acceptable as float, returns zero"""
imptext = table.item(row, column).text()
validator = QtGui.QDoubleValidator()
validator.setBottom(0)
status = validator.validate(imptext, 0)[0]
if status == QtGui.QValidator.Acceptable:
outfloat = float(imptext)
else:
outfloat = 0
return outfloat
def update_profile(pt, row, dataset):
"""spectrum updating procedure"""
# Dont do anything to graph when the spectrum is not populated
if isinstance(dataset['masses'], type(None)):
return
start = floatize(pt, row, 1)
end = floatize(pt, row, 2)
spectrum = pt.cellWidget(row, 3).figure.get_axes()[0]
masses = dataset['masses']
massargs = dt.argsubselect(masses, start, end)
yshape = np.mean(dataset['matrix'], axis=0)
......@@ -52,16 +69,17 @@ def get_daughterset(ptable, dtable, ds):
# TODO: resolve intensities trouble
for row in range(dtable.rowCount()):
if dtable.cellWidget(row, 0).checkState() == 2:
startm = dt.floatize(ptable.cellWidget(row, 1).text())
endm = dt.floatize(ptable.cellWidget(row, 2).text())
startm = floatize(ptable, row, 1)
endm = floatize(ptable, row, 2)
massargs = dt.argsubselect(ds['masses'], startm, endm)
intensity = (np.sum(
ds['matrix'].T[massargs].T, axis=1)) / ds['chrom_dat'][1]
cor = dtable.cellWidget(row, 1).currentIndex() -1
cor = dtable.cellWidget(row, 1).currentIndex() - 1
# TODO: check if cor still produce the mess or it started to behave
if cor not in (-2, -1):
factor = dt.floatize(dtable.cellWidget(row, 2).text())
startm = dt.floatize(ptable.cellWidget(cor, 1).text())
endm = dt.floatize(ptable.cellWidget(cor, 2).text())
factor = floatize(dtable, row, 2)
startm = floatize(ptable, cor, 1)
endm = floatize(ptable, cor, 2)
massargs = dt.argsubselect(ds['masses'], startm, endm)
correction = ((np.sum(
ds['matrix'].T[massargs].T, axis=1))
......@@ -74,9 +92,11 @@ def get_daughterset(ptable, dtable, ds):
def update_drlspectrum(ptable, dtable, ds, drlspectrum):
# Dont do when the dataset is not populated
# TODO: write a less resources demanding function
if isinstance(ds['masses'], type(None)):
return
colors = np.array([[0,0,0], [255, 0, 0], [0, 255, 0], [0, 0, 255],
colors = np.array([[0, 0, 0], [255, 0, 0], [0, 255, 0], [0, 0, 255],
[0, 200, 255], [255, 200, 0], [255, 100, 0],
[200, 50, 0], [255, 0, 200], [0, 100, 0],
[0, 100, 255], [100, 100, 100]])
......@@ -88,9 +108,11 @@ def update_drlspectrum(ptable, dtable, ds, drlspectrum):
i = 0
for row in range(dtable.rowCount()):
if dtable.cellWidget(row, 0).checkState() == 2:
dtable.blockSignals(True)
dtable.item(row, 0).setBackground(QtGui.QBrush(
QtGui.QColor(*colors[row % len(colors)], alpha=50)))
label = " {}".format(ptable.cellWidget(row, 0).text())
dtable.blockSignals(False)
label = " {}".format(ptable.item(row, 0).text())
drlspectrum.plot(times, intensities[i], label=label,
color=(colors[row % len(colors)] / 255))
i += 1
......@@ -101,15 +123,16 @@ def update_drlspectrum(ptable, dtable, ds, drlspectrum):
drlspectrum.set_ylim(top=np.amax(intensities)*1.1,
bottom=np.amax(intensities)*-0.01)
drlspectrum.legend(loc=2)
drlspectrum.figure.canvas.draw()
drlspectrum.figure.canvas.draw()
def gettableitemlist(ptable):
ion_list = []
for row in range(ptable.rowCount()):
text = []
for i in range(3):
if not isinstance(ptable.cellWidget(row, i), type(None)):
frg = ptable.cellWidget(row, i).text()
if not isinstance(ptable.item(row, i), type(None)):
frg = ptable.item(row, i).text()
else:
frg = ""
text.append(frg)
......@@ -118,127 +141,113 @@ def gettableitemlist(ptable):
return ion_list
def update_drl(start_mz, end_mz, spectrum, dataset, pname,
ptable, dtable, drlspectrum):
"""cares that masses are in order and valid, then refresh tables"""
for line in start_mz, end_mz:
status = QtGui.QDoubleValidator().validate(line.text(), 0)[0]
if status != QtGui.QValidator.Acceptable:
line.setText("0")
def update_corrfors(ptable, dtable):
"""update corrections selection layout of the daughter table"""
ionlist = gettableitemlist(ptable)
for row in range(dtable.rowCount()):
corfor = dtable.cellWidget(row, 1)
index = corfor.currentIndex()
corfor.blockSignals(True)
corfor.clear()
corfor.addItem("")
corfor.addItems(ionlist)
corfor.setCurrentIndex(index)
corfor.blockSignals(False)
start = dt.floatize(start_mz.text())
end = dt.floatize(end_mz.text())
if start > end:
start, end = end, start
def ptable_changed(row, column, ptable, dtable, ds):
"""routine called by change of the ptable spectra"""
update_corrfors(ptable, dtable)
dtable.item(row, 0).setText(gettableitemlist(ptable)[row])
if column in (1, 2):
update_profile(ptable, row, ds)
if float(0) not in (start, end) and pname.text() is "":
pname.setText(str(int((start+end)/2)))
def dtable_changed(row, column, ptable, dtable, ds, drlspectrum):
"""routine called by change of the dtable spectra"""
if dtable.cellWidget(row, 0).checkState() == 2:
if (column == 0)\
or (column == 2
and dtable.cellWidget(row, 1).currentIndex() != 0):
update_drlspectrum(ptable, dtable, ds, drlspectrum)
ptnames = gettableitemlist(ptable)
for row in range(dtable.rowCount()):
dtable.item(row, 0).setText(ptnames[row])
index = dtable.cellWidget(row, 1).currentIndex()
dtable.cellWidget(row, 1).clear()
dtable.cellWidget(row, 1).addItem("")
dtable.cellWidget(row, 1).addItems(gettableitemlist(ptable))
dtable.cellWidget(row, 1).setCurrentIndex(index)
update_profile(start, end, spectrum, dataset)
update_drlspectrum(ptable, dtable, dataset, drlspectrum)
def corr_changed(row, ptable, dtable, ds, drlspectrum):
"""routine called by change of correction for ion"""
if (dtable.cellWidget(row, 0).checkState() == 2
and floatize(dtable, row, 2) != 0):
update_drlspectrum(ptable, dtable, ds, drlspectrum)
def remove_rows(ptable, dtable):
def remove_rows(ptable, dtable, ds, drlspectrum):
# TODO: maybe nicer selection in future, but this works for now
rows = reversed(list(set(
map(lambda x: x.row(), ptable.selectedIndexes()))))
for row in rows:
# TODO: Check what happens to checkboxes - will they mess-up same way as corrfors?
dtable.removeRow(row)
ptable.removeRow(row)
for i in range(dtable.rowCount()):
index = dtable.cellWidget(i, 1).currentIndex()
dtable.cellWidget(i, 1).clear()
dtable.cellWidget(i, 1).addItem("")
dtable.cellWidget(i, 1).addItems(gettableitemlist(ptable))
corfor = dtable.cellWidget(i, 1)
corfor.disconnect()
index = corfor.currentIndex()
corfor.clear()
corfor.addItem("")
corfor.addItems(gettableitemlist(ptable))
if index == row+1:
dtable.cellWidget(i, 1).setCurrentIndex(0)
corfor.setCurrentIndex(0)
corr_changed(i, ptable, dtable, ds, drlspectrum)
elif index > row+1:
dtable.cellWidget(i, 1).setCurrentIndex(index-1)
def setdoublecell(table, row, column):
"""populate table cell with float-validated posititve text"""
table.setCellWidget(row, column, QtWidgets.QLineEdit())
validator = QtGui.QDoubleValidator()
validator.setBottom(0)
table.cellWidget(row, column).setValidator(validator)
table.cellWidget(row, column).setFrame(False)
table.cellWidget(row, column).setText("0")
table.horizontalHeader().setSectionResizeMode(
column, QtWidgets.QHeaderView.ResizeToContents)
return table.cellWidget(row, column)
corfor.setCurrentIndex(index-1)
else:
corfor.setCurrentIndex(index)
corfor.currentIndexChanged.connect(lambda: corr_changed(
i, ptable, dtable, ds, drlspectrum))
def add_line(ds, parenttable, daughtertable, drlspectrum):
"""add parent ion to the table"""
newrow = parenttable.rowCount()
parenttable.blockSignals(True)
daughtertable.blockSignals(True)
parenttable.setRowCount(newrow + 1)
for i in range(3):
parenttable.setItem(newrow, i, QtWidgets.QTableWidgetItem())
if newrow is not 0:
parenttable.item(newrow, i).setText(str(floatize(
parenttable, newrow-1, i)+1))
ion_graph = Figure(figsize=(3, 1.5), dpi=100, facecolor="None")
ionspect = ion_graph.add_subplot(
111, facecolor=(1, 1, 1, 0.8), position=(-0.01, -0.01, 1.02, 1.02))
ion_graph.add_subplot(111, facecolor=(1, 1, 1, 0.8),
position=(-0.01, -0.01, 1.02, 1.02))
graph_canvas = FigureCanvas(ion_graph)
graph_canvas.setStyleSheet("background-color:transparent;")
graph_canvas.setAutoFillBackground(False)
newrow = parenttable.rowCount()
parenttable.setRowCount(newrow + 1)
pname = QtWidgets.QLineEdit()
pname.setFrame(False)
parenttable.setCellWidget(newrow, 0, pname)
start_mz = setdoublecell(parenttable, newrow, 1)
end_mz = setdoublecell(parenttable, newrow, 2)
parenttable.setCellWidget(newrow, 3, graph_canvas)
if newrow is not 0:
for i in range(3):
parenttable.cellWidget(newrow, i).setText(
str(dt.floatize(
parenttable.cellWidget(newrow-1, i).text())+1))
daughtertable.setRowCount(newrow + 1)
checkbox = QtWidgets.QCheckBox()
dname = QtWidgets.QTableWidgetItem()
dname.setFlags(dname.flags() & ~QtCore.Qt.ItemIsEditable)
dname.setTextAlignment(QtCore.Qt.AlignRight)
daughtertable.setItem(newrow, 0, dname)
daughtertable.setCellWidget(newrow, 0, checkbox)
corfor = QtWidgets.QComboBox()
corfor.setFrame(False)
daughtertable.setCellWidget(newrow, 1, corfor)
daughtertable.setItem(newrow, 2, QtWidgets.QTableWidgetItem())
parenttable.blockSignals(False)
daughtertable.blockSignals(False)
ptable_changed(newrow, 1, parenttable, daughtertable, ds)
corto = QtWidgets.QComboBox()
corto.setFrame(False)
corto.addItem("")
corto.addItems(gettableitemlist(parenttable))
daughtertable.setCellWidget(newrow, 1, corto)
factor = setdoublecell(daughtertable, newrow, 2)
update_drl(start_mz, end_mz, ionspect, ds, pname,
parenttable, daughtertable, drlspectrum)
pname.editingFinished.connect(lambda: update_drl(
start_mz, end_mz, ionspect, ds, pname, parenttable,
daughtertable, drlspectrum))
start_mz.editingFinished.connect(lambda: update_drl(
start_mz, end_mz, ionspect, ds, pname, parenttable,
daughtertable, drlspectrum))
end_mz.editingFinished.connect(lambda: update_drl(
start_mz, end_mz, ionspect, ds, pname, parenttable,
daughtertable, drlspectrum))
checkbox.stateChanged.connect(lambda: update_drlspectrum(
parenttable, daughtertable, ds, drlspectrum))
corto.currentIndexChanged.connect(lambda: update_drlspectrum(
parenttable, daughtertable, ds, drlspectrum))
factor.editingFinished.connect(lambda: update_drlspectrum(
parenttable, daughtertable, ds, drlspectrum))
corfor.currentIndexChanged.connect(lambda: corr_changed(
newrow, parenttable, daughtertable, ds, drlspectrum))
def iontable(labels):
......@@ -289,14 +298,14 @@ def load_drltables(parent, ptable, dtable, dataset, drlspectrum):
# first populate only parent table
for i in range(len(names)):
add_line(dataset, ptable, dtable, drlspectrum)
ptable.cellWidget(i, 0).setText(names[i])
ptable.cellWidget(i, 1).setText(start_masses[i])
ptable.cellWidget(i, 2).setText(end_masses[i])
ptable.item(i, 0).setText(names[i])
ptable.item(i, 1).setText(start_masses[i])
ptable.item(i, 2).setText(end_masses[i])
# and after that daughter table
for i in range(len(names)):
dtable.cellWidget(i, 1).setCurrentIndex(
int(corrected_to[i]))
dtable.cellWidget(i, 2).setText(corr_factors[i])
dtable.item(i, 2).setText(corr_factors[i])
dtable.cellWidget(i, 0).setCheckState(int(states[i]))
......@@ -312,10 +321,10 @@ def save_drlconfig(ptable, dtable, parent):
for row in range(ptable.rowCount()):
vals = []
for i in range(3):
vals.append(ptable.cellWidget(row, i).text())
vals.append(ptable.item(row, i).text())
vals.append(dtable.cellWidget(row, 0).checkState())
vals.append(dtable.cellWidget(row, 1).currentIndex())
vals.append(dtable.cellWidget(row, 2).text())
vals.append(dtable.item(row, 2).text())
expf.write("{}\n".format((",".join(map(str, vals)))))
expf.close()
......@@ -408,7 +417,8 @@ def main_window(parent, ds, filename, cache):
btn_add.clicked.connect(lambda: add_line(
ds, pt, dtable, chromplot))
btn_rem.clicked.connect(lambda: remove_rows(pt,dtable))
btn_rem.clicked.connect(lambda: remove_rows(
pt, dtable, ds, chromplot))
drl_load.clicked.connect(lambda: load_drltables(
main_widget, pt, dtable, ds, chromplot))
drl_save.clicked.connect(lambda: save_drlconfig(
......@@ -418,6 +428,12 @@ def main_window(parent, ds, filename, cache):
drl_export.clicked.connect(lambda: export_drlspectrum(
main_widget, filename, pt, dtable, ds))
pt.itemChanged.connect(lambda item: ptable_changed(
item.row(), item.column(), pt, dtable, ds))
dtable.itemChanged.connect(lambda item: dtable_changed(
item.row(), item.column(), pt, dtable, ds, chromplot))
main_layout = QtWidgets.QVBoxLayout(main_widget)
sub_layout = QtWidgets.QHBoxLayout()
tablelayout = QtWidgets.QVBoxLayout()
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment