2023-05-31 15:20:51 +00:00
|
|
|
from itertools import cycle
|
|
|
|
|
2023-05-31 17:35:03 +00:00
|
|
|
from numpy import array
|
2023-05-31 15:20:51 +00:00
|
|
|
from pyqtgraph import mkPen, mkBrush
|
|
|
|
|
|
|
|
from nmreval.lib.colors import Tab10
|
2023-05-26 16:22:13 +00:00
|
|
|
from ..Qt import QtWidgets, QtCore
|
|
|
|
from .._py.tnmh_dialog import Ui_Dialog
|
|
|
|
from ..lib.pg_objects import PlotItem, RegionItem
|
|
|
|
|
|
|
|
from nmreval.data import DSC
|
2023-05-23 17:23:13 +00:00
|
|
|
|
|
|
|
|
2023-05-24 17:34:06 +00:00
|
|
|
class TgCalculator(QtWidgets.QDialog, Ui_Dialog):
|
|
|
|
def __init__(self, management, parent=None):
|
2023-05-23 17:23:13 +00:00
|
|
|
super().__init__(parent=parent)
|
|
|
|
|
2023-05-24 17:34:06 +00:00
|
|
|
self.setupUi(self)
|
|
|
|
self._management = management
|
2023-05-31 15:20:51 +00:00
|
|
|
self._colors = cycle(Tab10)
|
2023-05-24 17:34:06 +00:00
|
|
|
|
2023-05-31 15:20:51 +00:00
|
|
|
self._dsc = {}
|
2023-06-03 15:04:14 +00:00
|
|
|
self._plots = {}
|
|
|
|
self._tg_value = {}
|
|
|
|
|
2023-05-26 16:22:13 +00:00
|
|
|
self.limits = RegionItem(), RegionItem()
|
|
|
|
for lim in self.limits:
|
|
|
|
self.graphicsView.addItem(lim)
|
|
|
|
|
|
|
|
self.add_sets()
|
|
|
|
|
2023-06-01 18:09:11 +00:00
|
|
|
self.listWidget.itemClicked.connect(self.show_tg_values)
|
|
|
|
|
2023-05-26 15:28:06 +00:00
|
|
|
def __call__(self):
|
|
|
|
self.clear()
|
|
|
|
self.add_sets()
|
|
|
|
|
|
|
|
def clear(self):
|
|
|
|
self.listWidget.clear()
|
2023-06-03 15:04:14 +00:00
|
|
|
for plots in self._plots.values():
|
|
|
|
for val in plots:
|
|
|
|
self.graphicsView.removeItem(val)
|
|
|
|
self.graphicsView_2.removeItem(val)
|
2023-05-31 17:35:03 +00:00
|
|
|
|
2023-05-26 15:28:06 +00:00
|
|
|
def add_sets(self):
|
2023-05-31 15:20:51 +00:00
|
|
|
min_x = 10_000_000
|
|
|
|
max_x = -10_000_000
|
|
|
|
for (key, name), c in zip(self._management.active_sets, self._colors):
|
|
|
|
data = self._management[key].data
|
|
|
|
if not isinstance(data, DSC):
|
2023-05-26 16:22:13 +00:00
|
|
|
continue
|
2023-05-26 15:28:06 +00:00
|
|
|
|
2023-05-31 15:20:51 +00:00
|
|
|
min_x = min(min_x, data.x.min())
|
|
|
|
max_x = max(max_x, data.x.max())
|
|
|
|
|
2023-05-26 15:28:06 +00:00
|
|
|
item = QtWidgets.QListWidgetItem(name)
|
2023-06-03 15:04:14 +00:00
|
|
|
item.setCheckState(QtCore.Qt.Checked)
|
2023-05-26 15:28:06 +00:00
|
|
|
item.setData(QtCore.Qt.UserRole, key)
|
2023-05-31 15:20:51 +00:00
|
|
|
item.setForeground(mkBrush(c.rgb()))
|
2023-05-30 15:36:44 +00:00
|
|
|
self.listWidget.addItem(item)
|
|
|
|
|
2023-06-03 15:04:14 +00:00
|
|
|
self._dsc[key] = data
|
|
|
|
|
|
|
|
data_plot = PlotItem(x=data.x, y=data.y, pen=mkPen(c.rgb()))
|
|
|
|
self.graphicsView.addItem(data_plot)
|
|
|
|
|
|
|
|
glass = PlotItem()
|
|
|
|
glass.set_line(style=2, color=c)
|
|
|
|
self.graphicsView.addItem(glass)
|
|
|
|
|
|
|
|
liquid = PlotItem()
|
|
|
|
liquid.set_line(style=2, color=c)
|
|
|
|
self.graphicsView.addItem(liquid)
|
|
|
|
|
|
|
|
tangent = PlotItem()
|
|
|
|
tangent.set_line(style=2, color=c)
|
|
|
|
self.graphicsView.addItem(tangent)
|
|
|
|
|
|
|
|
tg_plot = PlotItem(pen=None, symbolBrush=c.rgb(), symbol='o')
|
|
|
|
self.graphicsView.addItem(tg_plot)
|
|
|
|
|
|
|
|
fictive_cp = PlotItem(pen=mkPen(c.rgb()))
|
|
|
|
self.graphicsView_2.addItem(fictive_cp)
|
|
|
|
|
|
|
|
tnmh_fit = PlotItem()
|
|
|
|
tnmh_fit.set_line(style=2, color=c)
|
|
|
|
self.graphicsView_2.addItem(tnmh_fit)
|
|
|
|
|
|
|
|
self._plots[key] = (data_plot, tg_plot, glass, liquid, tangent, fictive_cp, tnmh_fit)
|
2023-05-31 15:20:51 +00:00
|
|
|
|
|
|
|
dist = max_x - min_x
|
|
|
|
self.limits[0].setRegion((min_x, min_x+0.1*dist))
|
|
|
|
self.limits[1].setRegion((max_x-0.1*dist, max_x))
|
2023-05-30 15:36:44 +00:00
|
|
|
|
|
|
|
@QtCore.pyqtSlot(name='on_pushButton_clicked')
|
|
|
|
def calc_tg(self):
|
|
|
|
baselines = tuple(lim.getRegion() for lim in self.limits)
|
|
|
|
if baselines[0][0] > baselines[1][0]:
|
|
|
|
baselines = baselines[1], baselines[0]
|
|
|
|
|
|
|
|
for idx in range(self.listWidget.count()):
|
2023-05-31 15:20:51 +00:00
|
|
|
item = self.listWidget.item(idx)
|
|
|
|
key = item.data(QtCore.Qt.UserRole)
|
|
|
|
color = item.foreground()
|
2023-06-03 15:04:14 +00:00
|
|
|
|
|
|
|
plot = self._plots[key]
|
|
|
|
data = self._dsc[key]
|
|
|
|
|
2023-05-31 15:20:51 +00:00
|
|
|
tg_results, glass, liquid, tangent = data.glass_transition(*baselines)
|
2023-05-30 15:36:44 +00:00
|
|
|
|
2023-06-03 15:04:14 +00:00
|
|
|
for i, line in enumerate((glass, liquid, tangent)):
|
|
|
|
plot[i+2].setData(x=line[:, 0], y=line[:, 1])
|
|
|
|
|
|
|
|
plot[1].setData(array(list(tg_results.values())))
|
|
|
|
self._tg_value[key] = tg_results
|
2023-05-30 15:36:44 +00:00
|
|
|
|
2023-06-01 18:09:11 +00:00
|
|
|
def show_tg_values(self, item):
|
2023-06-03 15:04:14 +00:00
|
|
|
values = self._tg_value.get(item.data(QtCore.Qt.UserRole))
|
2023-06-01 18:09:11 +00:00
|
|
|
|
|
|
|
if values is not None:
|
|
|
|
self.listWidget_2.clear()
|
|
|
|
for name, pos in values.items():
|
|
|
|
tg_item = QtWidgets.QListWidgetItem(f'{name.capitalize()}: {pos[0]:.2f} K')
|
|
|
|
self.listWidget_2.addItem(tg_item)
|
|
|
|
|
|
|
|
@QtCore.pyqtSlot(name='on_pushButton_2_clicked')
|
|
|
|
def get_fictive(self):
|
|
|
|
baselines = tuple(lim.getRegion() for lim in self.limits)
|
|
|
|
if baselines[0][0] > baselines[1][0]:
|
|
|
|
baselines = baselines[1], baselines[0]
|
|
|
|
|
|
|
|
for idx in range(self.listWidget.count()):
|
|
|
|
item = self.listWidget.item(idx)
|
|
|
|
key = item.data(QtCore.Qt.UserRole)
|
2023-06-03 15:04:14 +00:00
|
|
|
|
|
|
|
plot = self._plots[key]
|
|
|
|
data = self._dsc[key]
|
2023-06-01 18:09:11 +00:00
|
|
|
cp, tg = data.get_fictive_cp(*baselines)
|
|
|
|
|
2023-06-03 15:04:14 +00:00
|
|
|
plot[5].setData(cp.x, cp.y)
|
2023-06-01 18:09:11 +00:00
|
|
|
|
2023-06-03 15:04:14 +00:00
|
|
|
self._tg_value[key]['fictive'] = (tg, 0)
|
|
|
|
|
|
|
|
plot[1].setData(array(list(self._tg_value[key].values())))
|
2023-06-01 18:09:11 +00:00
|
|
|
|
2023-06-03 15:04:14 +00:00
|
|
|
res = data.calculate_tnmh([60, 0.5, 1, 2e5], *baselines, return_fictive=False)
|
2023-06-01 18:09:11 +00:00
|
|
|
print(res.pprint())
|
2023-06-03 15:04:14 +00:00
|
|
|
plot[-1].setData(res.x, res.y)
|
2023-06-01 18:09:11 +00:00
|
|
|
|
2023-05-30 15:36:44 +00:00
|
|
|
def close(self) -> bool:
|
|
|
|
self.clear()
|
|
|
|
return super().close()
|
|
|
|
|