| 1 |
# |
| 2 |
# This file is part of the PyMeasure package. |
| 3 |
# |
| 4 |
# Copyright (c) 2013-2024 PyMeasure Developers |
| 5 |
# |
| 6 |
# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
# of this software and associated documentation files (the "Software"), to deal |
| 8 |
# in the Software without restriction, including without limitation the rights |
| 9 |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 10 |
# copies of the Software, and to permit persons to whom the Software is |
| 11 |
# furnished to do so, subject to the following conditions: |
| 12 |
# |
| 13 |
# The above copyright notice and this permission notice shall be included in |
| 14 |
# all copies or substantial portions of the Software. |
| 15 |
# |
| 16 |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
| 22 |
# THE SOFTWARE. |
| 23 |
# |
| 24 |
|
| 25 |
import logging |
| 26 |
from functools import partial |
| 27 |
|
| 28 |
import numpy as np |
| 29 |
import pyqtgraph as pg |
| 30 |
import pandas as pd |
| 31 |
|
| 32 |
from ..Qt import QtCore, QtWidgets, QtGui |
| 33 |
from .tab_widget import TabWidget |
| 34 |
from ...experiment import Procedure |
| 35 |
|
| 36 |
SORT_ROLE = QtCore.Qt.ItemDataRole.UserRole + 1 |
| 37 |
SORTING_ENABLED = True # Allow to disable sorting, for debug purpose only |
| 38 |
|
| 39 |
log = logging.getLogger(__name__) |
| 40 |
log.addHandler(logging.NullHandler()) |
| 41 |
|
| 42 |
|
| 43 |
class ResultsTable(QtCore.QObject): |
| 44 |
""" Class representing a panda dataframe """ |
| 45 |
data_changed = QtCore.Signal(int, int, int, int) |
| 46 |
|
| 47 |
def __init__(self, results, color, column_index=None, |
| 48 |
force_reload=False, wdg=None, **kwargs): |
| 49 |
super().__init__() |
| 50 |
self.results = results |
| 51 |
self.color = color |
| 52 |
self.force_reload = force_reload |
| 53 |
self.last_row_count = 0 |
| 54 |
self.wdg = wdg |
| 55 |
self.column_index = column_index |
| 56 |
self.data = self.results.data |
| 57 |
self._started = False |
| 58 |
|
| 59 |
@property |
| 60 |
def data(self): |
| 61 |
return self._data |
| 62 |
|
| 63 |
@data.setter |
| 64 |
def data(self, value): |
| 65 |
self._data = value |
| 66 |
if self.column_index is not None: |
| 67 |
self._data = self._data.set_index(self.column_index) |
| 68 |
else: |
| 69 |
self._data.reset_index() |
| 70 |
|
| 71 |
@property |
| 72 |
def rows(self): |
| 73 |
return self._data.shape[0] |
| 74 |
|
| 75 |
@property |
| 76 |
def columns(self): |
| 77 |
return self._data.shape[1] |
| 78 |
|
| 79 |
def init(self): |
| 80 |
self.last_row_count = 0 |
| 81 |
|
| 82 |
def start(self): |
| 83 |
self._started = True |
| 84 |
|
| 85 |
def stop(self): |
| 86 |
self._started = False |
| 87 |
|
| 88 |
def update_data(self): |
| 89 |
if not self._started: |
| 90 |
return |
| 91 |
if self.force_reload: |
| 92 |
self.results.reload() |
| 93 |
self.data = self.results.data |
| 94 |
current_row_count, columns = self._data.shape |
| 95 |
if (self.last_row_count < current_row_count): |
| 96 |
# Request cells content update |
| 97 |
self.data_changed.emit(self.last_row_count, 0, |
| 98 |
current_row_count - 1, columns - 1) |
| 99 |
self.last_row_count = current_row_count |
| 100 |
|
| 101 |
def set_color(self, color): |
| 102 |
self.color = color |
| 103 |
|
| 104 |
def set_index(self, index): |
| 105 |
self.column_index = index |
| 106 |
|
| 107 |
|
| 108 |
class PandasModelBase(QtCore.QAbstractTableModel): |
| 109 |
""" This class provided a model to manage multiple panda dataframes and |
| 110 |
display them as a single table. |
| 111 |
|
| 112 |
The multiple pandas dataframes are provided as ResultTable class instances |
| 113 |
and all of them share the same number of columns. |
| 114 |
|
| 115 |
There are some assumptions: |
| 116 |
- Series in the dataframe are identical, we call this number k |
| 117 |
- Series length can be different, we call this number l(x), where x=1..n |
| 118 |
|
| 119 |
The data can be presented as follow: |
| 120 |
- By column: each series in a separate column, in this case table shape |
| 121 |
will be: (k*n) x (max(l(x) x=1..n) |
| 122 |
- By row: column fixed to the number of series, in this case table shape |
| 123 |
will be: k x (sum of l(x) x=1..n) |
| 124 |
""" |
| 125 |
|
| 126 |
float_digits = 6 |
| 127 |
concat_axis = 0 |
| 128 |
|
| 129 |
def __init__(self, column_index=None, results_list=[], parent=None): |
| 130 |
super().__init__(parent) |
| 131 |
self.column_index = column_index |
| 132 |
self._init_data(results_list) |
| 133 |
|
| 134 |
def _init_data(self, results_list=None): |
| 135 |
if results_list is None: |
| 136 |
results_list = [] |
| 137 |
self.results_list = results_list |
| 138 |
self.row_count = self.pandas_row_count() |
| 139 |
self.column_count = self.pandas_column_count() |
| 140 |
|
| 141 |
def clear(self): |
| 142 |
self.beginResetModel() |
| 143 |
for results in self.results_list: |
| 144 |
results.stop() |
| 145 |
self._init_data() |
| 146 |
self.endResetModel() |
| 147 |
|
| 148 |
def add_results(self, results): |
| 149 |
if results not in self.results_list: |
| 150 |
self.beginResetModel() |
| 151 |
self.results_list.append(results) |
| 152 |
results.data_changed.connect(partial(self._data_changed, results)) |
| 153 |
self.endResetModel() |
| 154 |
results.init() |
| 155 |
results.start() |
| 156 |
results.update_data() |
| 157 |
|
| 158 |
def remove_results(self, results): |
| 159 |
self.beginResetModel() |
| 160 |
if results in self.results_list: |
| 161 |
self.results_list.remove(results) |
| 162 |
self.row_count = self.pandas_row_count() |
| 163 |
self.column_count = self.pandas_column_count() |
| 164 |
results.stop() |
| 165 |
self.endResetModel() |
| 166 |
|
| 167 |
def rowCount(self, parent=None): |
| 168 |
return self.row_count |
| 169 |
|
| 170 |
def columnCount(self, parent=None): |
| 171 |
return self.column_count |
| 172 |
|
| 173 |
def data(self, index, role=QtCore.Qt.ItemDataRole.DisplayRole): |
| 174 |
if index.isValid() and role in (QtCore.Qt.ItemDataRole.DisplayRole, SORT_ROLE): |
| 175 |
try: |
| 176 |
results, row, col = self.translate_to_local(index.row(), index.column()) |
| 177 |
value = results.data.iat[row, col] |
| 178 |
column_type = results.data.dtypes.iat[col] |
| 179 |
# Cast to column type |
| 180 |
value_render = column_type.type(value) |
| 181 |
except (IndexError, ValueError, TypeError): |
| 182 |
value = np.nan |
| 183 |
value_render = "" |
| 184 |
if isinstance(value_render, np.float64): |
| 185 |
# limit maximum number of decimal digits displayed |
| 186 |
value_render = f"{value_render:.{self.float_digits:d}g}" |
| 187 |
|
| 188 |
if role == QtCore.Qt.ItemDataRole.DisplayRole: |
| 189 |
return str(value_render) |
| 190 |
elif role == SORT_ROLE: |
| 191 |
# For numerical sort |
| 192 |
return float(value) |
| 193 |
|
| 194 |
return None |
| 195 |
|
| 196 |
def _get_new_rows_columns(self, results, r1, c1, r2, c2): |
| 197 |
new_rows = self.pandas_row_count() - self.row_count |
| 198 |
new_rows_start = self.row_count |
| 199 |
|
| 200 |
new_columns = self.pandas_column_count() - self.column_count |
| 201 |
new_columns_start = self.column_count |
| 202 |
|
| 203 |
return new_rows, new_rows_start, new_columns, new_columns_start |
| 204 |
|
| 205 |
def headerData(self, section, orientation, role): |
| 206 |
""" Return header information |
| 207 |
|
| 208 |
Override method from QAbstractTableModel |
| 209 |
""" |
| 210 |
if role == QtCore.Qt.ItemDataRole.DisplayRole: |
| 211 |
if orientation == QtCore.Qt.Orientation.Horizontal: |
| 212 |
return str(self.horizontal_header[section]) |
| 213 |
|
| 214 |
if orientation == QtCore.Qt.Orientation.Vertical: |
| 215 |
return str(self.vertical_header[section]) |
| 216 |
elif role == QtCore.Qt.ItemDataRole.DecorationRole: |
| 217 |
if orientation == QtCore.Qt.Orientation.Horizontal: |
| 218 |
return self.horizontal_header_decoration(section) |
| 219 |
|
| 220 |
if orientation == QtCore.Qt.Orientation.Vertical: |
| 221 |
return self.vertical_header_decoration(section) |
| 222 |
|
| 223 |
return None |
| 224 |
|
| 225 |
def _data_changed(self, results, r1, c1, r2, c2): |
| 226 |
""" Internal method to handle data changed signal """ |
| 227 |
rows, rows_start, columns, columns_start = \ |
| 228 |
self._get_new_rows_columns(results, r1, c1, r2, c2) |
| 229 |
if rows or columns: |
| 230 |
if rows > 0: |
| 231 |
# New rows available |
| 232 |
self.beginInsertRows(QtCore.QModelIndex(), |
| 233 |
rows_start, |
| 234 |
rows_start + rows - 1) |
| 235 |
self.row_count += rows |
| 236 |
self.endInsertRows() |
| 237 |
|
| 238 |
if columns > 0: |
| 239 |
# New columns available |
| 240 |
self.beginInsertColumns(QtCore.QModelIndex(), |
| 241 |
columns_start, |
| 242 |
columns_start + columns - 1) |
| 243 |
self.column_count += columns |
| 244 |
self.endInsertColumns() |
| 245 |
else: |
| 246 |
top_bottom = self._get_row_column_set(results, r1, c1, r2, c2) |
| 247 |
for r1, c1, r2, c2 in top_bottom: |
| 248 |
self.dataChanged.emit(self.createIndex(r1, c1), |
| 249 |
self.createIndex(r2, c2)) |
| 250 |
|
| 251 |
def pandas_row_count(self): |
| 252 |
""" Return total row count of the panda dataframes |
| 253 |
|
| 254 |
The value depends on the geometry selected to display dataframes |
| 255 |
""" |
| 256 |
raise Exception("Subclass should implement it") |
| 257 |
|
| 258 |
def pandas_column_count(self): |
| 259 |
""" Return total column count of the panda dataframes |
| 260 |
|
| 261 |
The value depends on the geometry selected to display dataframes |
| 262 |
""" |
| 263 |
raise Exception("Subclass should implement it") |
| 264 |
|
| 265 |
def _get_row_column_set(self, results, r1, c1, r2, c2): |
| 266 |
""" Return set of top/bottom coordinates for data changed event. |
| 267 |
|
| 268 |
Depending on the geometry of the table a single top/bottom could be |
| 269 |
translated in multiple tops/bottoms |
| 270 |
""" |
| 271 |
raise Exception("Subclass should implement it") |
| 272 |
|
| 273 |
def translate_to_local(self, row, col): |
| 274 |
""" Translate from full table coordinate to single results coordinates """ |
| 275 |
raise Exception("Subclass should implement it") |
| 276 |
|
| 277 |
def translate_to_global(self, results, row, col): |
| 278 |
""" Translate from single results coordinates to full table coordinates """ |
| 279 |
raise Exception("Subclass should implement it") |
| 280 |
|
| 281 |
@property |
| 282 |
def horizontal_header(self): |
| 283 |
raise Exception("Subclass should implement it") |
| 284 |
|
| 285 |
@property |
| 286 |
def vertical_header(self): |
| 287 |
return range(self.row_count) |
| 288 |
|
| 289 |
def horizontal_header_decoration(self, section): |
| 290 |
return None |
| 291 |
|
| 292 |
def vertical_header_decoration(self, section): |
| 293 |
return None |
| 294 |
|
| 295 |
def export_df(self): |
| 296 |
df_list = [results.data for results in self.results_list] |
| 297 |
if not df_list: # Empty list |
| 298 |
df = None |
| 299 |
else: |
| 300 |
# Concatenate pandas data frames |
| 301 |
df = pd.concat(df_list, axis=self.concat_axis).replace(to_replace=np.nan, value="") |
| 302 |
return df |
| 303 |
|
| 304 |
def set_index(self, index): |
| 305 |
self.column_index = index |
| 306 |
# Update results list |
| 307 |
for r in self.results_list: |
| 308 |
r.stop() |
| 309 |
r.set_index(index) |
| 310 |
self.beginResetModel() |
| 311 |
for r in self.results_list: |
| 312 |
r.start() |
| 313 |
r.update_data() |
| 314 |
self.row_count = self.pandas_row_count() |
| 315 |
self.column_count = self.pandas_column_count() |
| 316 |
self.endResetModel() |
| 317 |
|
| 318 |
def copy_model(self, model_class): |
| 319 |
model = model_class(self.column_index, self.results_list[:]) |
| 320 |
return model |
| 321 |
|
| 322 |
|
| 323 |
class PandasModelByRow(PandasModelBase): |
| 324 |
concat_axis = 0 |
| 325 |
|
| 326 |
def pandas_row_count(self): |
| 327 |
rows = 0 |
| 328 |
for r in self.results_list: |
| 329 |
rows += r.rows |
| 330 |
return rows |
| 331 |
|
| 332 |
def pandas_column_count(self): |
| 333 |
cols = 0 |
| 334 |
if self.results_list: |
| 335 |
cols = self.results_list[0].columns |
| 336 |
return cols |
| 337 |
|
| 338 |
def _get_row_column_set(self, results, r1, c1, r2, c2): |
| 339 |
top = self.translate_to_global(results, r1, c1) |
| 340 |
bottom = self.translate_to_global(results, r2, c2) |
| 341 |
return (top + bottom), |
| 342 |
|
| 343 |
def translate_to_local(self, row, col): |
| 344 |
""" Translate from full table coordinate to single results coordinates """ |
| 345 |
for index, results in enumerate(self.results_list): |
| 346 |
if row < results.rows: |
| 347 |
break |
| 348 |
row -= results.rows |
| 349 |
return results, row, col |
| 350 |
|
| 351 |
def translate_to_global(self, results, row, col): |
| 352 |
""" Translate from single results coordinates to full table coordinates """ |
| 353 |
rows = 0 |
| 354 |
for res in self.results_list: |
| 355 |
if res == results: |
| 356 |
break |
| 357 |
rows += results.rows |
| 358 |
return rows + row, col |
| 359 |
|
| 360 |
@property |
| 361 |
def vertical_header(self): |
| 362 |
if self.column_index is None: |
| 363 |
header = range(self.row_count) |
| 364 |
else: |
| 365 |
header = [] |
| 366 |
for r in self.results_list: |
| 367 |
header.extend(r.data.index) |
| 368 |
return header |
| 369 |
|
| 370 |
@property |
| 371 |
def horizontal_header(self): |
| 372 |
if self.results_list: |
| 373 |
return self.results_list[0].data.columns |
| 374 |
else: |
| 375 |
return [] |
| 376 |
|
| 377 |
def vertical_header_decoration(self, section): |
| 378 |
results, _, _ = self.translate_to_local(section, 0) |
| 379 |
pixelmap = QtGui.QPixmap(6, 6) |
| 380 |
pixelmap.fill(results.color) |
| 381 |
return pixelmap |
| 382 |
|
| 383 |
|
| 384 |
class PandasModelByColumn(PandasModelBase): |
| 385 |
concat_axis = 1 |
| 386 |
|
| 387 |
def pandas_row_count(self): |
| 388 |
if self.column_index is None: |
| 389 |
return max([0] + [r.rows for r in self.results_list]) |
| 390 |
else: |
| 391 |
return len(self.vertical_header) |
| 392 |
|
| 393 |
def pandas_column_count(self): |
| 394 |
cols = 0 |
| 395 |
size = len(self.results_list) |
| 396 |
if size > 0: |
| 397 |
cols = self.results_list[0].columns * size |
| 398 |
return cols |
| 399 |
|
| 400 |
def _get_row_column_set(self, results, r1, c1, r2, c2): |
| 401 |
top_bottoms = [] |
| 402 |
for i in range(c1, c2 + 1): |
| 403 |
top = self.translate_to_global(results, r1, i) |
| 404 |
bottom = self.translate_to_global(results, r2, i) |
| 405 |
top_bottoms.append(top + bottom) |
| 406 |
|
| 407 |
return top_bottoms |
| 408 |
|
| 409 |
def translate_to_local(self, row, col): |
| 410 |
""" Translate from full table coordinate to single results coordinates """ |
| 411 |
columns = 0 |
| 412 |
for index, results in enumerate(self.results_list): |
| 413 |
if col < (columns + results.columns): |
| 414 |
break |
| 415 |
columns += results.columns |
| 416 |
if (self.column_index is not None): |
| 417 |
# Remap row to matching index entry when indexing is used |
| 418 |
try: |
| 419 |
index = self.vertical_header[row] |
| 420 |
row = list(results.data.index).index(index) |
| 421 |
except ValueError: |
| 422 |
row = None |
| 423 |
return results, row, col - columns |
| 424 |
|
| 425 |
def translate_to_global(self, results, row, col): |
| 426 |
""" Translate from single results coordinates to full table coordinates """ |
| 427 |
columns = 0 |
| 428 |
for res in self.results_list: |
| 429 |
if res == results: |
| 430 |
break |
| 431 |
columns += results.columns |
| 432 |
return row, col + columns |
| 433 |
|
| 434 |
@property |
| 435 |
def horizontal_header(self): |
| 436 |
size = len(self.results_list) |
| 437 |
if size: |
| 438 |
v = list(self.results_list[0].data.columns) |
| 439 |
return v * size |
| 440 |
else: |
| 441 |
return [] |
| 442 |
|
| 443 |
def horizontal_header_decoration(self, section): |
| 444 |
results, _, _ = self.translate_to_local(0, section) |
| 445 |
pixelmap = QtGui.QPixmap(6, 6) |
| 446 |
pixelmap.fill(results.color) |
| 447 |
return pixelmap |
| 448 |
|
| 449 |
@property |
| 450 |
def vertical_header(self): |
| 451 |
header = set([]) |
| 452 |
for r in self.results_list: |
| 453 |
header = header.union(set(r.data.index)) |
| 454 |
header = sorted(list(header)) |
| 455 |
return header |
| 456 |
|
| 457 |
|
| 458 |
class Table(QtWidgets.QTableView): |
| 459 |
""" Table format view of :class:`Experiment<pymeasure.display.manager.Experiment>` |
| 460 |
objects |
| 461 |
|
| 462 |
""" |
| 463 |
|
| 464 |
supported_formats = { |
| 465 |
"CSV file (*.csv)": 'csv', |
| 466 |
"Excel file (*.xlsx)": 'excel', |
| 467 |
"HTML file (*.html *.htm)": 'html', |
| 468 |
"JSON file (*.json)": 'json', |
| 469 |
"LaTeX file (*.tex)": 'latex', |
| 470 |
"Markdown file (*.md)": 'markdown', |
| 471 |
"XML file (*.xml)": 'xml', |
| 472 |
} |
| 473 |
|
| 474 |
def __init__(self, refresh_time=0.2, check_status=True, |
| 475 |
force_reload=False, layout_class=PandasModelByColumn, |
| 476 |
column_index=None, float_digits=6, parent=None): |
| 477 |
super().__init__(parent) |
| 478 |
self.force_reload = force_reload |
| 479 |
self.float_digits = float_digits |
| 480 |
model = layout_class(column_index=column_index) |
| 481 |
self.setModel(model) |
| 482 |
self.horizontalHeader().setStyleSheet("font: bold;") |
| 483 |
self.sortByColumn(-1, QtCore.Qt.SortOrder.AscendingOrder) |
| 484 |
self.setSortingEnabled(True) |
| 485 |
self.horizontalHeader().setSectionsMovable(True) |
| 486 |
self.horizontalHeader().setSectionResizeMode( |
| 487 |
QtWidgets.QHeaderView.ResizeMode.ResizeToContents |
| 488 |
) |
| 489 |
|
| 490 |
self.setup_context_menu() |
| 491 |
|
| 492 |
self.refresh_time = refresh_time |
| 493 |
self.check_status = check_status |
| 494 |
if self.refresh_time is not None: |
| 495 |
self.timer = QtCore.QTimer(self) |
| 496 |
self.timer.timeout.connect(self.update_tables) |
| 497 |
self.timer.start(int(self.refresh_time * 1e3)) |
| 498 |
|
| 499 |
def setModel(self, model): |
| 500 |
model.float_digits = self.float_digits |
| 501 |
if SORTING_ENABLED: |
| 502 |
proxyModel = QtCore.QSortFilterProxyModel(self) |
| 503 |
proxyModel.setSourceModel(model) |
| 504 |
model = proxyModel |
| 505 |
|
| 506 |
model.setSortRole(SORT_ROLE) |
| 507 |
super().setModel(model) |
| 508 |
|
| 509 |
def source_model(self): |
| 510 |
model = self.model() |
| 511 |
if SORTING_ENABLED: |
| 512 |
model = model.sourceModel() |
| 513 |
return model |
| 514 |
|
| 515 |
def export_action(self): |
| 516 |
df = self.source_model().export_df() |
| 517 |
|
| 518 |
if df is not None: |
| 519 |
formats = ";;".join(self.supported_formats.keys()) |
| 520 |
filename_and_ext = QtWidgets.QFileDialog.getSaveFileName( |
| 521 |
self, |
| 522 |
"Save File", |
| 523 |
"", |
| 524 |
formats, |
| 525 |
) |
| 526 |
filename = filename_and_ext[0] |
| 527 |
ext = filename_and_ext[1] |
| 528 |
if filename: |
| 529 |
mode = self.supported_formats[ext] |
| 530 |
prefix = df.style if mode == "latex" else df |
| 531 |
getattr(prefix, 'to_' + mode)(filename) |
| 532 |
|
| 533 |
def refresh_action(self): |
| 534 |
self.update_tables() |
| 535 |
|
| 536 |
def copy_action(self): |
| 537 |
df = self.source_model().export_df() |
| 538 |
if df is not None: |
| 539 |
df.to_clipboard() |
| 540 |
|
| 541 |
def setup_context_menu(self): |
| 542 |
self.setContextMenuPolicy(QtCore.Qt.ContextMenuPolicy.CustomContextMenu) |
| 543 |
self.customContextMenuRequested.connect(self.context_menu) |
| 544 |
self.copy = QtGui.QAction("Copy table data", self) |
| 545 |
self.copy.triggered.connect(self.copy_action) |
| 546 |
self.refresh = QtGui.QAction("Refresh table data", self) |
| 547 |
self.refresh.triggered.connect(self.refresh_action) |
| 548 |
self.export = QtGui.QAction("Export table data", self) |
| 549 |
self.export.triggered.connect(self.export_action) |
| 550 |
|
| 551 |
def context_menu(self, point): |
| 552 |
menu = QtWidgets.QMenu(self) |
| 553 |
menu.addAction(self.copy) |
| 554 |
menu.addAction(self.refresh) |
| 555 |
menu.addAction(self.export) |
| 556 |
menu.exec(self.mapToGlobal(point)) |
| 557 |
|
| 558 |
def update_tables(self, force=False): |
| 559 |
model = self.source_model() |
| 560 |
for item in model.results_list: |
| 561 |
if not self.check_status or force: |
| 562 |
item.update_data() |
| 563 |
else: |
| 564 |
if item.results.procedure.status == Procedure.RUNNING: |
| 565 |
item.update_data() |
| 566 |
|
| 567 |
def set_color(self, table, color): |
| 568 |
table.set_color(color) |
| 569 |
|
| 570 |
def add_table(self, table): |
| 571 |
model = self.source_model() |
| 572 |
model.add_results(table) |
| 573 |
|
| 574 |
def remove_table(self, table): |
| 575 |
model = self.source_model() |
| 576 |
model.remove_results(table) |
| 577 |
table.stop() |
| 578 |
if model.rowCount() == 0: |
| 579 |
# Empty table, reset sorting policy |
| 580 |
self.setSortingEnabled(False) |
| 581 |
self.sortByColumn(-1, QtCore.Qt.SortOrder.AscendingOrder) |
| 582 |
self.setSortingEnabled(True) |
| 583 |
|
| 584 |
def clear(self): |
| 585 |
model = self.source_model() |
| 586 |
|
| 587 |
model.clear() |
| 588 |
self.setSortingEnabled(False) |
| 589 |
self.sortByColumn(-1, QtCore.Qt.SortOrder.AscendingOrder) |
| 590 |
self.setSortingEnabled(True) |
| 591 |
|
| 592 |
def set_index(self, index): |
| 593 |
model = self.source_model() |
| 594 |
model.set_index(index) |
| 595 |
|
| 596 |
def set_model(self, model_class): |
| 597 |
""" Replace model with new instance of model_class """ |
| 598 |
model = self.source_model() |
| 599 |
new_model = model.copy_model(model_class) |
| 600 |
self.setModel(new_model) |
| 601 |
|
| 602 |
|
| 603 |
class TableWidget(TabWidget, QtWidgets.QWidget): |
| 604 |
""" Widget to display experiment data in a tabular format |
| 605 |
""" |
| 606 |
layout_class_map = { |
| 607 |
'By Row': PandasModelByRow, |
| 608 |
'By Column': PandasModelByColumn, |
| 609 |
} |
| 610 |
|
| 611 |
def __init__(self, name, columns, by_column=True, |
| 612 |
column_index=None, refresh_time=0.2, |
| 613 |
float_digits=6, |
| 614 |
check_status=True, parent=None): |
| 615 |
super().__init__(name, parent) |
| 616 |
self.columns = columns |
| 617 |
self.layout_names = list(self.layout_class_map.keys()) |
| 618 |
self.table_layout = self.layout_names[1] if by_column else self.layout_names[0] |
| 619 |
self.column_index = column_index |
| 620 |
self.refresh_time = refresh_time |
| 621 |
self.check_status = check_status |
| 622 |
self.float_digits = float_digits |
| 623 |
self._setup_ui() |
| 624 |
self._layout() |
| 625 |
|
| 626 |
def _setup_ui(self): |
| 627 |
self.column_index_label = QtWidgets.QLabel(self) |
| 628 |
self.column_index_label.setMaximumSize(QtCore.QSize(45, 16777215)) |
| 629 |
self.column_index_label.setText('Index:') |
| 630 |
self.layout_label = QtWidgets.QLabel(self) |
| 631 |
self.layout_label.setMaximumSize(QtCore.QSize(45, 16777215)) |
| 632 |
self.layout_label.setText('Layout:') |
| 633 |
|
| 634 |
self.column_index_combo = QtWidgets.QComboBox(self) |
| 635 |
self.layout = QtWidgets.QComboBox(self) |
| 636 |
self.column_index_combo.addItem('<None>') |
| 637 |
for column in self.columns: |
| 638 |
self.column_index_combo.addItem(column) |
| 639 |
if self.column_index is not None: |
| 640 |
self.column_index_combo.setCurrentText(self.column_index) |
| 641 |
|
| 642 |
for key in self.layout_names: |
| 643 |
self.layout.addItem(key) |
| 644 |
self.layout.setCurrentText(self.table_layout) |
| 645 |
|
| 646 |
self.column_index_combo.activated.connect(self.update_column_index) |
| 647 |
self.layout.activated.connect(self.update_layout) |
| 648 |
|
| 649 |
self.table = Table(refresh_time=self.refresh_time, |
| 650 |
check_status=self.check_status, |
| 651 |
force_reload=False, |
| 652 |
layout_class=self.layout_class_map[self.table_layout], |
| 653 |
column_index=self.column_index, |
| 654 |
float_digits=self.float_digits, |
| 655 |
parent=self, |
| 656 |
) |
| 657 |
|
| 658 |
def _layout(self): |
| 659 |
vbox = QtWidgets.QVBoxLayout(self) |
| 660 |
vbox.setSpacing(0) |
| 661 |
|
| 662 |
hbox = QtWidgets.QHBoxLayout() |
| 663 |
hbox.setSpacing(10) |
| 664 |
hbox.setContentsMargins(-1, 6, -1, 6) |
| 665 |
hbox.addWidget(self.column_index_label) |
| 666 |
hbox.addWidget(self.column_index_combo) |
| 667 |
hbox.addWidget(self.layout_label) |
| 668 |
hbox.addWidget(self.layout) |
| 669 |
|
| 670 |
vbox.addLayout(hbox) |
| 671 |
vbox.addWidget(self.table) |
| 672 |
self.setLayout(vbox) |
| 673 |
|
| 674 |
def update_layout(self, entry): |
| 675 |
model = self.layout.itemText(entry) |
| 676 |
self.table_layout = entry |
| 677 |
self.table.set_model(self.layout_class_map[model]) |
| 678 |
|
| 679 |
def update_column_index(self, entry): |
| 680 |
index = self.column_index_combo.itemText(entry) |
| 681 |
if index == '<None>': |
| 682 |
index = None |
| 683 |
self.column_index = index |
| 684 |
self.table.set_index(index) |
| 685 |
|
| 686 |
def new_curve(self, results, color=pg.intColor(0), **kwargs): |
| 687 |
return ResultsTable(results, color, self.column_index, wdg=self, **kwargs) |
| 688 |
|
| 689 |
def load(self, table): |
| 690 |
self.table.add_table(table) |
| 691 |
|
| 692 |
def remove(self, table): |
| 693 |
self.table.remove_table(table) |
| 694 |
|
| 695 |
def set_color(self, table, color): |
| 696 |
""" Change the color of the pen of the curve """ |
| 697 |
self.table.set_color(table, color) |
| 698 |
|
| 699 |
def preview_widget(self, parent=None): |
| 700 |
""" Return a widget suitable for preview during loading """ |
| 701 |
by_column = False if self.table_layout == self.layout_names[0] else True |
| 702 |
return TableWidget("Table preview", |
| 703 |
columns=self.columns, |
| 704 |
by_column=by_column, |
| 705 |
refresh_time=None, |
| 706 |
check_status=False, |
| 707 |
float_digits=self.float_digits, |
| 708 |
parent=None, |
| 709 |
) |
| 710 |
|
| 711 |
def clear_widget(self): |
| 712 |
self.table.clear() |