-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscratch-view.py
779 lines (654 loc) · 29.4 KB
/
scratch-view.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
""" Scratch View: Analizador de ensayos de rayado.
Licence at the end of the file.
"""
__author__ = "Marco Crivaro Nicolini <[email protected]>"
__version__ = 0.1
__year__ = 2023
__org__ = "INFINA, FCEN UBA"
__website__ = 'https://github.com/crivaronicolini/scratch-view/'
__appid__ = 'com.infina.scratch-view.0.1'
from pathlib import Path
import platform
import traceback
import sys
import os
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
from matplotlib.figure import Figure
from matplotlib.backends.backend_qtagg import FigureCanvasQTAgg, NavigationToolbar2QT as NavigationToolbar
from matplotlib.backend_tools import Cursors
from PyQt6.QtCore import Qt, QPoint, QPointF, pyqtSignal, QSize, QProcess, QSettings
from PyQt6.QtGui import QAction, QColor, QIcon, QImage, QPainter
from PyQt6.QtWidgets import QToolBar, QMainWindow, QWidget, QVBoxLayout, QHBoxLayout, QFileDialog, QLabel, QMessageBox, QPushButton, QDialog, QRadioButton, QButtonGroup, QDialogButtonBox, QFormLayout, QLineEdit
from QtImageViewer import QtImageViewer
# https://www.pythonguis.com/tutorials/packaging-pyside6-applications-windows-pyinstaller-installforge/
basedir = os.path.dirname(__file__)
try:
from ctypes import windll # Only exists on Windows.
windll.shell32.SetCurrentProcessExplicitAppUserModelID(__appid__)
except ImportError:
pass
def errorDialog(parent, title, message):
print(message)
QMessageBox.critical(parent, str(title), message)
return
class MainWindow(QMainWindow):
def __init__(self, ):
QMainWindow.__init__(self)
self.setWindowIcon(
QIcon(os.path.join(basedir, "data", "scratch-view.ico")))
self.setWindowTitle("Scratch View")
self.viewer = QtImageViewer()
self.plot = Plot(self)
self.label = QLabel('')
f = self.label.font()
f.setPointSize(15)
self.label.setFont(f)
self.label.setFont(self.label.font())
self.label.setAlignment(Qt.AlignmentFlag.AlignHCenter |
Qt.AlignmentFlag.AlignVCenter)
widget = QWidget()
layout = QVBoxLayout()
layout.addWidget(self.label)
layout.addWidget(self.viewer)
layout.addWidget(self.plot)
widget.setLayout(layout)
self.setCentralWidget(widget)
self._createActions()
self._createMenuBar()
self._createToolBars()
# Status Bar
self.status = self.statusBar()
self.setAcceptDrops(True)
self.viewer.setAcceptDrops(True)
self.viewer.dropEvent = self.dropEvent
self.plot.dropEvent = self.dropEvent
self.viewer.dragEnterEvent = self.dragEnterEvent
self.viewer.dragMoveEvent = self.dragMoveEvent
self.plot.dragEnterEvent = self.dragEnterEvent
self.p = None
self.imgFiletypes = ['.jpg', '.bmp', '.png']
self.dataFiletypes = ['.csv', '.tsv']
self.filetypes = self.imgFiletypes + self.dataFiletypes
self.imgFileFilter = " ".join(["*" + s for s in self.imgFiletypes])
self.dataFileFilter = " ".join(["*" + s for s in self.dataFiletypes])
self.fileFilter = self.imgFileFilter + " " + self.dataFileFilter
self.scales = {'olympus': 0.44}
self.scaleCurrentName = 'olympus'
self.scaleCurrentValue = 0.44
self.zeroEllipse = None
self.lastDir = ""
self.readSettings()
self.show()
def setScaleFromBtn(self, button):
self.scaleCurrentName = button.text()
self.scaleCurrentValue = self.scales[self.scaleCurrentName]
def setScaleFromText(self, nombre, valor):
self.scaleCurrentValue = self.scales[nombre] = float(valor)
self.scaleCurrentName = nombre
def eraseScale(self, nombre):
self.scales.pop(nombre)
def _createMenuBar(self):
menuBar = self.menuBar()
# File menu
fileMenu = menuBar.addMenu("Archivo")
fileMenu.addAction(self.newFileAction)
fileMenu.addSeparator()
fileMenu.addAction(self.saveAction)
fileMenu.addAction(self.saveAsAction)
fileMenu.addAction(self.exportLinesAction)
# Img menu
imgMenu = menuBar.addMenu("Imagen")
imgMenu.addAction(self.newStitchAction)
imgMenu.addAction(self.setScaleAction)
helpMenu = menuBar.addMenu("Ayuda")
helpMenu.addAction(self.showTutorialAction)
helpMenu.addAction(self.showAboutAction)
def _createToolBars(self):
mainToolBar = QToolBar("Main", self)
self.addToolBar(mainToolBar)
mainToolBar.addAction(self.enableSetZeroAction)
mainToolBar.addAction(self.enableMarcarLineaAction)
def _createActions(self):
self.newFileAction = QAction("Abrir", self)
self.newFileAction.triggered.connect(self.open)
self.saveAction = QAction("Guardar", self)
self.saveAction.triggered.connect(self.save)
self.saveAction.setDisabled(True)
self.saveAsAction = QAction("Guardar como", self)
self.saveAsAction.triggered.connect(self.saveAs)
self.saveAsAction.setDisabled(True)
self.exportLinesAction = QAction("Exportar lineas", self)
self.exportLinesAction.triggered.connect(self.exportLines)
self.exportLinesAction.setDisabled(True)
self.plot.lineAdded.connect(
lambda: self.exportLinesAction.setDisabled(False))
self.plot.lineAdded.connect(
lambda: self.saveAsAction.setDisabled(False))
self.newStitchAction = QAction("Juntar imagenes", self)
self.newStitchAction.triggered.connect(self.juntarImagenes)
self.enableSetZeroAction = QAction("Elegir origen", self)
self.enableSetZeroAction.setToolTip(
"Click derecho para definir el origen.")
self.enableSetZeroAction.setCheckable(True)
self.enableSetZeroAction.setDisabled(True)
self.enableSetZeroAction.toggled.connect(self.enableSetZero)
self.enableMarcarLineaAction = QAction("Marcar linea", self)
self.enableMarcarLineaAction.setToolTip(
"Click derecho para marcar linea en el grafico.")
self.enableMarcarLineaAction.setCheckable(True)
self.enableMarcarLineaAction.toggled.connect(self.enableMarcarLinea)
self.enableMarcarLineaAction.setDisabled(True)
self.viewer.mousePositionOnImageChanged.connect(self.printPos)
self.viewer.imageChanged.connect(self.setTitle)
self.viewer.imageChanged.connect(
lambda: self.enableSetZeroAction.setDisabled(False))
self.setScaleAction = QAction("Cambiar escala", self)
self.setScaleAction.triggered.connect(lambda: ScaleDialog(self))
self.showTutorialAction = QAction("Tutorial", self)
self.showTutorialAction.triggered.connect(self.showTutorial)
self.showAboutAction = QAction("Sobre Scratch View", self)
self.showAboutAction .triggered.connect(self.showAbout)
def enableSetZero(self, enable):
if enable:
self.viewer.viewport().setCursor(Qt.CursorShape.CrossCursor)
self.viewer.rightMouseButtonReleased.connect(self.setZero)
self.enableMarcarLineaAction.setChecked(False)
else:
self.viewer.viewport().setCursor(Qt.CursorShape.ArrowCursor)
self.viewer.rightMouseButtonReleased.disconnect(self.setZero)
def enableMarcarLinea(self, enable):
if enable:
if not self.zeroEllipse:
errorDialog(self, "Error", "Primero hay que definir el origen")
return
self.enableSetZeroAction.setChecked(False)
self.viewer.viewport().setCursor(Qt.CursorShape.CrossCursor)
self.viewer.mousePositionOnImageChanged.connect(
self.plot.mostrarLinea)
self.viewer.drawROI = 'Line'
self.viewer.rightMouseButtonReleased.connect(self.plot.marcarLinea)
else:
self.viewer.viewport().setCursor(Qt.CursorShape.ArrowCursor)
self.viewer.rightMouseButtonReleased.disconnect(
self.plot.marcarLinea)
self.viewer.mousePositionOnImageChanged.disconnect(
self.plot.mostrarLinea)
self.viewer.drawROI = None
def setZero(self, x, y):
if self.zeroEllipse:
self.viewer.scene.removeItem(self.zeroEllipse)
r = 25
self.zeroEllipse = self.viewer.scene.addEllipse(
x-r, y-r, 2*r, 2*r, pen=0, brush=QColor("#FFD141"))
self.zeroEllipsePos = QPointF(round(x-r), round(y-r))
self.enableSetZeroAction.toggle()
self.enableMarcarLineaAction.setDisabled(False)
def printPos(self, point):
if self.zeroEllipse:
self.plot.x, y = self._mapToum(
QPointF(point) - self.zeroEllipsePos)
self.plot.fIn = self.plot.getfIn(self.plot.x)
self.status.showMessage(
f"x={self.plot.x}, y={y} F={self.plot.fIn:.2f}N")
def _mapToum(self, point):
x, y = point.x(), point.y()
return round(self.scaleCurrentValue * x), round(self.scaleCurrentValue * y)
def setTitle(self, title):
self.imgPath = Path(title)
self.label.setText(self.imgPath.name)
def _whichFiji(self):
p = platform.system()
if p == 'Linux':
# return subprocess.run(["which", "fiji"])
return "fiji"
if p == 'Windows':
# TODO
return
return
def juntarImagenes(self):
"""Da para elegir una carpeta y corre una macro de Fiji sobre las imagenes de esa carpeta.
Guarda el resultado con el nombre de la carpeta"""
if not self.p:
fiji = self._whichFiji()
directory = QFileDialog.getExistingDirectory(
self, "Abrir carpeta de imagenes")
directory = Path(directory).resolve()
self.directory_juntadas = directory
self.files_juntadas = sorted(Path.iterdir(directory))
self.csv_juntadas = [
f for f in self.files_juntadas if f.name.endswith('csv')]
imgs = [
f for f in self.files_juntadas if f.name.endswith('jpg')]
if imgs[0].stem != '1':
# TODO convertir los archivos a 1 2 3...
pass
x = len(imgs)
y = 1
overlap = 20
# si está el csv de la medición uso eso como nombre de archivo
if self.csv_juntadas:
self.outpath = ''.join(
[str(directory.parent / self.csv_juntadas[0].stem), '.jpg'])
else:
self.outpath = ''.join(
[str(directory.parent / directory.name), '.jpg'])
self.status.showMessage('Uniendo imagenes')
self.p = QProcess()
self.p.finished.connect(self.p_finished)
self.p.start(fiji, ["--headless", "--run", "stitch-macro.py",
f'{x=},{y=},{overlap=},directory="{str(directory)}",outpath="{self.outpath}"'])
def p_finished(self):
self.status.showMessage(f'Imagen guardada en {self.outpath}')
self.viewer.open(self.outpath)
if self.csv_juntadas:
self.plot.open(self.csv_juntadas[0])
self.p = None
def open(self, filepaths=None):
"""Abre lista de archivos, o abre un seleccionador de archivos"""
if not filepaths:
filepaths, _ = QFileDialog.getOpenFileNames(
self, caption="Abrir imagen y csv", directory=self.lastDir, filter=self.fileFilter)
if not filepaths:
return
filepaths = [Path(filepath).resolve() for filepath in filepaths]
elif not isinstance(filepaths, list):
filepaths = [filepaths]
self.lastDir = str(filepaths[0].resolve())
for filepath in filepaths:
if filepath.suffix in self.imgFiletypes:
self.viewer.open(filepath=filepath)
elif filepath.suffix in self.dataFiletypes:
self.plot.open(filepath=filepath)
def dragEnterEvent(self, event):
"""Acepta archivos si alguno tiene formato valido"""
if (event.mimeData().hasFormat("text/uri-list")):
urls = event.mimeData().urls()
self.paths = [path for path in (
Path(url.path()) for url in urls) if path.suffix in self.filetypes]
if self.paths:
event.accept()
def dropEvent(self, _):
self.open(self.paths)
def dragMoveEvent(self, event):
pass
def closeEvent(self, event):
self.saveSettings()
event.accept()
def readSettings(self):
self.settings = QSettings("INFINA", "Scratch View")
self.resize(self.settings.value("size", QSize(1000, 500)))
self.move(self.settings.value("pos", QPoint(100, 100)))
if not self.settings.value("isMaximized", False):
self.showMaximized()
self.lastDir = self.settings.value("lastDir", self.lastDir)
self.scaleCurrentName = self.settings.value(
"scaleCurrentName", 'olympus')
self.scaleCurrentValue = float(
self.settings.value("scaleCurrentValue", 0.44))
self.scales = self.settings.value("scales", self.scales)
def saveSettings(self):
self.settings.setValue("size", self.size())
self.settings.setValue("pos", self.pos())
self.settings.setValue("isMaximized", self.isMaximized())
self.settings.setValue("lastDir", self.lastDir)
self.settings.setValue("scaleCurrentName", self.scaleCurrentName)
self.settings.setValue("scaleCurrentValue", self.scaleCurrentValue)
self.settings.setValue("scales", self.scales)
def showAbout(self):
msgBox = QMessageBox(self)
msgBox.setWindowTitle("Sobre Scratch View")
msgBox.setTextFormat(Qt.TextFormat(Qt.TextFormat.RichText))
msgBox.setText(
f"<center><p><img src='iconf128.png' width=128></img></p><h3>Scratch View </h3></center>")
msgBox.setInformativeText(
f"<center><p>{__version__}</p><p>Analiza ensayos de rayado</p><p><a href='{__website__}'>Web</a></p><p>{__author__}, {__year__}</p><p><small>{__org__}</small></p><p><small>Este programa no brinda absolutamente ninguna garantia.<br>Ver la <a href='https://www.gnu.org/licenses/gpl-3.0.html'>licencia GPL version 2 o superior</a> para mas informacion</small></p></center>")
msgBox.open()
def showTutorial(self):
pass
def save(self):
self.saveAs(filepath=self.savedFilepath)
def saveAs(self, filepath=None):
if not filepath:
filepath, _ = QFileDialog.getSaveFileName(
self, "Guardar imagen como", directory=self.lastDir, filter=self.imgFileFilter)
if not filepath:
return
self.savedFilepath = filepath
image = QImage(self.viewer._image.width, self.viewer._image.height,
QImage.Format.Format_ARGB32_Premultiplied)
painter = QPainter(image)
self.viewer.scene.render(painter)
painter.end()
image.save(filepath)
self.lastDir = str(Path(filepath).parent)
self.statusBar().showMessage(f"Imagen guardada en {filepath}")
self.saveAction.setDisabled(False)
def exportLines(self):
filepath, _ = QFileDialog.getSaveFileName(
self, "Guardar lineas como", directory=self.lastDir, filter="Texto (*.txt *.md)")
if not filepath:
return
with open(filepath, 'w') as f:
p = self.plot.params
f.write('\n'.join([f"{self.imgPath.stem}", "",
"x (um)\tFuerza (N)",
*[f"{x}\t{f:.3f}" for x,
f in sorted(self.plot.lineasMarcadas)], "", "",
f"fuerza inicial: {p['fin']}, fuerza final: {p['ffin']}, largo: {p['largo']}, velocidad: {p['vel']}, kP: {p['Kp']}, kI: {p['Ki']}, kD: {p['Kd']}",
]))
self.lastDir = str(Path(filepath).parent)
self.statusBar().showMessage(f"Lineas guardadas en {filepath}")
class ScaleDialog(QDialog):
def __init__(self, parent):
super(QDialog, self).__init__(parent)
self.parent = parent
self.pushButtons = []
self.trashIcon = QIcon.fromTheme('user-trash')
self.trashIconWidth = self.trashIcon.pixmap(100).width()
self.dialog = QDialog(parent)
self.dialog.setWindowTitle("Escala de microscopio")
self.buttonGroup = QButtonGroup(self)
self.vboxLayout = QVBoxLayout(self)
for i, (nombre, escala) in enumerate(parent.scales.items()):
self.addItem(i, nombre, escala)
btnBox = QDialogButtonBox(self)
btnBox.setStandardButtons(
QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel)
newScaleBtn = QPushButton("Nueva escala", btnBox)
newScaleBtn.clicked.connect(lambda: self.newScale(self.dialog))
btnBox.addButton(newScaleBtn, QDialogButtonBox.ButtonRole.ActionRole)
self.vboxLayout.addWidget(btnBox)
self.dialog.setLayout(self.vboxLayout)
self.buttonGroup.buttonReleased.connect(parent.setScaleFromBtn)
btnBox.accepted.connect(self.dialog.accept)
btnBox.rejected.connect(self.dialog.close)
self.btnBox = btnBox
if len(self.buttonGroup.buttons()) == 1:
self.disableTrashing()
self.dialog.open()
def removeItem(self, pushButton, radioButton, nombre, widget):
if len(self.buttonGroup.buttons()) > 1:
self.parent.eraseScale(nombre)
self.buttonGroup.removeButton(radioButton)
self.pushButtons.remove(pushButton)
widget.close()
if len(self.buttonGroup.buttons()) == 1:
self.disableTrashing()
self.checkItem(0)
def addItem(self, i, nombre, escala):
hbox = QWidget(self.dialog)
hboxLayout = QHBoxLayout(self.dialog)
button = QRadioButton(nombre, self.dialog)
button.setChecked(nombre == self.parent.scaleCurrentName)
self.buttonGroup.addButton(button)
hboxLayout.addWidget(button)
hboxLayout.addWidget(QLabel(str(escala)))
if not self.trashIcon.isNull():
hboxLayout.addWidget(borrar := QPushButton(self.trashIcon, ''))
borrar.setFixedWidth(self.trashIconWidth//2)
else:
hboxLayout.addWidget(borrar := QPushButton('Borrar'))
borrar.clicked.connect(lambda: self.removeItem(
borrar, button, nombre, hbox))
hbox.setLayout(hboxLayout)
self.vboxLayout.insertWidget(i, hbox)
self.pushButtons.append(borrar)
return button
def addItemAndCheck(self, i, nombre, escala):
button = self.addItem(i, nombre, escala)
button.setChecked(True)
self.parent.setScaleFromText(nombre, escala)
def checkItem(self, i):
button = self.buttonGroup.buttons()[i]
button.setChecked(True)
self.parent.setScaleFromBtn(button)
def enableTrashing(self):
self.pushButtons[0].setEnabled(True)
def disableTrashing(self):
self.pushButtons[0].setEnabled(False)
def newScale(self, parent):
dialog = QDialog(parent)
dialog.setWindowTitle("Nueva escala")
layout = QFormLayout(dialog)
layout.addRow("Nombre", nombre := QLineEdit())
layout.addRow("Valor (um/pixel)", valor := QLineEdit())
btnBox = QDialogButtonBox(dialog)
btnBox.setStandardButtons(
QDialogButtonBox.StandardButton.Ok | QDialogButtonBox.StandardButton.Cancel)
layout.addWidget(btnBox)
dialog.setLayout(layout)
# deshabilita el Ok si no estan los dos campos llenos
ok = btnBox.buttons()[0]
ok.setDisabled(True)
nombre.textChanged.connect(lambda: ok.setDisabled(
False) if valor.text() else None)
valor.textChanged.connect(lambda: ok.setDisabled(
False) if nombre.text() else None)
btnBox.accepted.connect(dialog.accept)
btnBox.accepted.connect(self.enableTrashing)
# selecciona ok en el dialogo anterior
btnBox.accepted.connect(
lambda: self.btnBox.buttons()[0].setFocus())
# agrega el nuevo item al menu y lo selecciona
btnBox.accepted.connect(
lambda: self.addItemAndCheck(len(self.parent.scales.keys()), nombre.text(), valor.text()))
btnBox.rejected.connect(dialog.close)
dialog.open()
class Plot(QWidget):
# doubleClickAction = Signal(str)
# lineAdded = Signal()
doubleClickAction = pyqtSignal(str)
lineAdded = pyqtSignal()
def __init__(self, parent=None):
# # super().__init__(parent)
# # super(Plot, self).__init__(parent)
# self._main = QtWidgets.QWidget()
# self.figure = Figure()
# self.figureCanvas = FigureCanvas(self.figure)
# self.navigationToolbar = NavigationToolbar(
# self.figureCanvas, self._main)
#
# layout = QVBoxLayout()
# # layout.addWidget(self.navigationToolbar)
super(Plot, self).__init__(parent)
self.figure = plt.figure()
self.figureCanvas = FigureCanvasQTAgg(self.figure)
self.navigationToolbar = NavigationToolbar(self.figureCanvas, self)
layout = QVBoxLayout()
layout.addWidget(self.navigationToolbar)
layout.addWidget(self.figureCanvas)
self.setLayout(layout)
self.ax = self.figure.add_subplot(111)
self.figureCanvas.show()
self.x = 0
self.fIn = 0
self.line = None
# TODO leerlas de los metadatos
self.lineasMarcadas = []
self.lineasMarcadasX = []
self.lineasMarcadasXnp = np.array([])
self.lines = []
self.cursorOnLine = False
self.closestLineIdx = 0
self.moved = None
self.point = None
self.pressed = False
self.start = False
self.figureCanvas.mpl_connect(
'button_press_event', self.mousePressEvent)
self.figureCanvas.mpl_connect(
'button_release_event', self.mouseReleaseEvent)
self.figureCanvas.mpl_connect(
'motion_notify_event', self.mouseMoveEvent)
def open(self, filepath):
"""Open file picker to open csv """
try:
if not filepath:
filepath, _ = QFileDialog.getOpenFileName(
self, "Abrir csv", "", "Spreadsheet files (*.csv *.tsv *.xlx)")
if not filepath:
return
filepath = Path(filepath).resolve()
self.df = pd.read_csv(filepath)
title = filepath.name
self.ajustardf()
self.plot(title)
except Exception as e:
errorDialog(self, e, traceback.format_exc())
def ajustardf(self):
df = self.df
params = {}
params["fin"], params["ffin"], params["largo"], params["vel"], params["Kp"], params["Ki"], params["Kd"], * \
_ = df.iloc[0].to_list()
self.params = params
# TODO setear escala bien
if df.x.mean() < -1:
df.x = - df.x
# saco el acercamiento y estabilizacion
l = len(df.x[df.x == 0.]) - 1
df = df.drop(df[:l].index).reset_index(drop=True)
df['um'] = df.x/25.6
df.fIn = df.fIn/1000
df.fSet = df.fSet/1000
self.df = df
def plot(self, title):
self.ax.cla()
self.ax.plot(self.df.um, self.df.fIn, "b")
self.ax.plot(self.df.um, self.df.fSet, "--", color="gray")
self.ax.set_xlabel(r"$\mathrm{Largo\ (\mu m)}$")
self.ax.set_ylabel("Fuerza (N)")
self.ax.set_title(title)
self.figureCanvas.draw_idle()
self.figure.tight_layout()
def getfIn(self, x):
try:
idx = np.searchsorted(self.df['um'], x, side='left')
return self.df.fIn[idx]
except (ValueError, KeyError):
return 0
def mostrarLinea(self, point):
if point.x() > -1:
if not self.line:
self.line = self.ax.axvline(self.x, ls='-.', color="gray")
else:
self.line.set(xdata=[self.x, self.x], visible=True)
else:
if self.line:
self.line.set(visible=False)
self.figureCanvas.draw()
def marcarLinea(self, x, y):
if self.line is not None:
self.lineasMarcadas.append((self.x, self.fIn))
self.lineasMarcadasX.append(self.x)
self.lineasMarcadasXnp = np.array(self.lineasMarcadasX)
self.line.set(ls=":", color="gray", alpha=0.5)
self.lines.append(self.line)
self.line = None
self.lineAdded.emit()
def mousePressEvent(self, event):
if self.ax.get_navigate_mode() != None:
return
if not event.inaxes:
return
if self.start:
return
self.point = event.xdata
self.pressed = True
def mouseReleaseEvent(self, event):
if self.ax.get_navigate_mode() != None:
return
if not event.inaxes:
return
if self.cursorOnLine:
self.lines.pop(self.closestLineIdx).remove()
self.lineasMarcadasX.pop(self.closestLineIdx)
self.lineasMarcadasXnp = np.array(self.lineasMarcadasX)
self.figureCanvas.draw_idle()
return
# if self.pressed:
# self.pressed = False
# self.start = False
# self.point = None
# return
def mouseMoveEvent(self, event):
if self.ax.get_navigate_mode() != None:
return
if self.lineasMarcadasX:
if np.min(m := np.abs(event.x - self.lineasMarcadasXnp)) < 20:
self.closestLineIdx = np.argmin(m)
self.figureCanvas.set_cursor(Cursors.HAND)
self.cursorOnLine = True
elif self.cursorOnLine:
self.figureCanvas.set_cursor(Cursors.POINTER)
self.cursorOnLine = False
if __name__ == '__main__':
import sys
from PyQt6.QtWidgets import QApplication
# Create the QApplication.
app = QApplication(sys.argv)
#
# Create an image viewer widget.
mainwindow = MainWindow()
viewer = mainwindow.viewer
def my_exception_hook(exctype, value, traceback):
# Print the error and traceback
print(exctype, value, traceback)
errorDialog(mainwindow, exctype, traceback)
# Call the normal Exception hook after
sys._excepthook(exctype, value, traceback)
sys.exit(1)
# Set the exception hook to our wrapping function
sys.excepthook = my_exception_hook
# Set viewer's aspect ratio mode.
# !!! ONLY applies to full image view.
# !!! Aspect ratio always ignored when zoomed.
# Qt.AspectRatioMode.IgnoreAspectRatio: Fit to viewport.
# Qt.AspectRatioMode.KeepAspectRatio: Fit in viewport using aspect ratio.
# Qt.AspectRatioMode.KeepAspectRatioByExpanding: Fill viewport using aspect ratio.
viewer.aspectRatioMode = Qt.AspectRatioMode.KeepAspectRatio
# Set the viewer's scroll bar behaviour.
# Qt.ScrollBarPolicy.ScrollBarAlwaysOff: Never show scroll bar.
# Qt.ScrollBarPolicy.ScrollBarAlwaysOn: Always show scroll bar.
# Qt.ScrollBarPolicy.ScrollBarAsNeeded: Show scroll bar only when zoomed.
viewer.setHorizontalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAsNeeded)
viewer.setVerticalScrollBarPolicy(Qt.ScrollBarPolicy.ScrollBarAsNeeded)
# Allow zooming by draggin a zoom box with the left mouse button.
# !!! This will still emit a leftMouseButtonReleased signal if no dragging occured,
# so you can still handle left mouse button clicks in this way.
# If you absolutely need to handle a left click upon press, then
# either disable region zooming or set it to the middle or right button.
viewer.regionZoomButton = None # set to None to disable
# Pop end of zoom stack (double click clears zoom stack).
# viewer.zoomOutButton = Qt.MouseButton.RightButton # set to None to disable
viewer.zoomOutButton = None # set to None to disable
# Mouse wheel zooming.
viewer.wheelZoomFactor = 1.25 # Set to None or 1 to disable
# Allow panning with the middle mouse button.
viewer.panButton = Qt.MouseButton.LeftButton # set to None to disable
# Handle left mouse clicks with your own custom slot
# handleLeftClick(x, y). (x, y) are image coordinates.
# For (row, col) matrix indexing, row=y and col=x.
# QtImageViewer also provides similar signals for
# left/right mouse button press, release and doubleclick.
# Here I bind the slot to leftMouseButtonReleased only because
# the leftMouseButtonPressed signal will not be emitted due to
# left clicks being handled by the regionZoomButton.
# viewer.middleMouseButtonReleased.connect(handleLeftClick)
# Show the viewer and run the application.formlayout
# mainwindow.show()
sys.exit(app.exec())
"""
Copyright (C) 2023 Marco Crivaro Nicolini
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
"""