This repository has been archived by the owner on Nov 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
frontend.py
583 lines (477 loc) · 21 KB
/
frontend.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
""" ShapeOut - wx frontend components
"""
from __future__ import division, print_function
import chaco
import cv2
import numpy as np
import os
import platform
import sys
import traceback
import wx
import dclab
from ..configuration import ConfigurationFile
from ..util import findfile
from .controls import ControlPanel
from .explorer import ExplorerPanel
import gaugeframe
from .. import analysis
from .. import tlabwrap
from . import autosave
from . import update
from . import plot_main
from . import misc
from . import video
from . import export
from . import batch
from . import plot_export
from . import session
########################################################################
class ExceptionDialog(wx.MessageDialog):
""""""
def __init__(self, msg):
"""Constructor"""
wx.MessageDialog.__init__(self, None, msg, _("Error"),
wx.OK|wx.ICON_ERROR)
########################################################################
class Frame(gaugeframe.GaugeFrame):
""""""
def __init__(self, version, session_file=None):
"""Constructor"""
self.config = ConfigurationFile(findfile("shapeout.cfg"))
self.version = version
#size = (1300,900)
size = (1200,700)
minsize = (900, 700)
gaugeframe.GaugeFrame.__init__(self, None, -1,
title = _("%(progname)s - version %(version)s") % {
"progname": "ShapeOut", "version": version},
size = size)
self.SetMinSize(minsize)
sys.excepthook = MyExceptionHook
## Menus, Toolbar
self.InitUI()
self.sp = wx.SplitterWindow(self, style=wx.SP_THIN_SASH)
# This is necessary to prevent "Unsplit" of the SplitterWindow:
self.sp.SetMinimumPaneSize(100)
self.spright = wx.SplitterWindow(self.sp, style=wx.SP_THIN_SASH)
if platform.system() == "Linux":
sy = 270
else:
sy = 230
self.spright.SetMinimumPaneSize(sy)
# Splitter Window for control panel and cell view
self.sptop = wx.SplitterWindow(self.spright, style=wx.SP_THIN_SASH)
self.sptop.SetMinimumPaneSize(sy)
# Controls
self.PanelTop = ControlPanel(self.sptop, frame=self)
# Cell Images
self.ImageArea = video.ImagePanel(self.sptop, frame=self)
sizer = wx.BoxSizer(wx.HORIZONTAL)
sizer.Add(self.PanelTop, 2, wx.ALL|wx.EXPAND, 5)
sizer.Add(self.ImageArea, 1, wx.ALL|wx.EXPAND, 5)
self.sptop.SplitVertically(self.PanelTop, self.ImageArea, sy)
self.sptop.SetSashGravity(.46)
# Main Plots
self.PlotArea = plot_main.PlotPanel(self.spright, self)
self.spright.SplitHorizontally(self.sptop, self.PlotArea, sy)
## left panel (file selection)
## We need a splitter window here
self.PanelLeft = ExplorerPanel(self.sp, self)
self.PanelLeft.BindAnalyze(self.NewAnalysis)
self.sp.SplitVertically(self.PanelLeft, self.spright,
self.PanelLeft.normal_width)
# We set this to 100 again after show is complete.
self.spright.SetMinimumPaneSize(sy)
# Fake analysis
ddict = {"Area" : np.arange(10)*30,
"Defo" : np.arange(10)*.02}
rtdc_ds = dclab.RTDC_DataSet(ddict=ddict)
rtdc_ds.Configuration["Plotting"]["Contour Color"] = "white"
self.NewAnalysis([rtdc_ds])
## Go
self.Centre()
self.Show()
self.Maximize()
self.spright.SetMinimumPaneSize(100)
self.sptop.SetMinimumPaneSize(100)
# Check if we have an autosaved session that we did not delete
recover = autosave.check_recover(self)
# Load session file if provided
if session_file is not None and not recover:
self.OnMenuLoad(session_file=session_file)
# Search for updates
update.Update(self)
# Start autosaving
autosave.autosave_run(self)
# Set window icon
try:
self.MainIcon = misc.getMainIcon()
wx.Frame.SetIcon(self, self.MainIcon)
except:
self.MainIcon = None
def InitUI(self):
"""Menus, Toolbar, Statusbar"""
## Menubar
self.menubar = wx.MenuBar()
self.SetMenuBar(self.menubar)
## File menu
fileMenu = wx.Menu()
self.menubar.Append(fileMenu, _('&File'))
# data
fpath = fileMenu.Append(wx.ID_REPLACE, _('Find Measurements'),
_('Select .tdms file location'))
self.Bind(wx.EVT_MENU, self.OnMenuSearchPath, fpath)
fpathadd = fileMenu.Append(wx.ID_FIND, _('Add Measurements'),
_('Select .tdms file location'))
self.Bind(wx.EVT_MENU, self.OnMenuSearchPathAdd, fpathadd)
# clear measurements
fpathclear = fileMenu.Append(wx.ID_CLEAR, _('Clear Measurements'),
_('Clear unchecked items in project list'))
self.Bind(wx.EVT_MENU, self.OnMenuClearMeasurements, fpathclear)
fileMenu.AppendSeparator()
# save
fsave = fileMenu.Append(wx.ID_SAVEAS, _('Save Session'),
_('Select .zmso file'))
self.Bind(wx.EVT_MENU, self.OnMenuSaveSimple, fsave)
# load
fload = fileMenu.Append(wx.ID_OPEN, _('Open Session'),
_('Select .zmso file'))
self.Bind(wx.EVT_MENU, self.OnMenuLoad, fload)
fileMenu.AppendSeparator()
# quit
fquit = fileMenu.Append(wx.ID_EXIT, _('Quit'),
_('Quit ShapeOut'))
self.Bind(wx.EVT_MENU, self.OnMenuQuit, fquit)
## Export menu
exportMenu = wx.Menu()
self.menubar.Append(exportMenu, _('&Export'))
e2dat = exportMenu.Append(wx.ID_ANY, _('All &event data (*.tsv)'),
_('Export the plotted event data as tab-separated values'))
self.Bind(wx.EVT_MENU, self.OnMenuExportData, e2dat)
e2pdf = exportMenu.Append(wx.ID_ANY, _('Graphical &plot (*.pdf)'),
_('Export the plot as a portable document file'))
self.Bind(wx.EVT_MENU, self.OnMenuExportPDF, e2pdf)
# export PNG disabled:
# https://github.com/ZELLMECHANIK-DRESDEN/ShapeOut/issues/62
#e2png = exportMenu.Append(wx.ID_ANY, _('Graphical &plot (*.png)'),
# _('Export the plot as a portable network graphic'))
#self.Bind(wx.EVT_MENU, self.OnMenuExportPNG, e2png)
e2stat = exportMenu.Append(wx.ID_ANY, _('Computed &statistics (*.tsv)'),
_('Export the statistics data as tab-separated values'))
self.Bind(wx.EVT_MENU, self.OnMenuExportStatistics, e2stat)
## Batch menu
batchMenu = wx.Menu()
self.menubar.Append(batchMenu, _('&Batch'))
b_filter = batchMenu.Append(wx.ID_ANY, _('&Statistical analysis'),
_('Apply one filter setting to multiple measurements.'))
self.Bind(wx.EVT_MENU, self.OnMenuBatchFolder, b_filter)
## Help menu
helpmenu = wx.Menu()
self.menubar.Append(helpmenu, _('&Help'))
menuSoftw = helpmenu.Append(wx.ID_ANY, _("&Software"),
_("Information about the software used"))
self.Bind(wx.EVT_MENU, self.OnHelpSoftware, menuSoftw)
menuAbout = helpmenu.Append(wx.ID_ABOUT, _("&About"),
_("Information about this program"))
self.Bind(wx.EVT_MENU, self.OnHelpAbout, menuAbout)
## Toolbar
self.toolbar = wx.ToolBar(self, style=wx.TB_FLAT|wx.TB_HORIZONTAL|wx.TB_NODIVIDER)
iconsize = (36,36)
self.toolbar.SetToolBitmapSize(iconsize)
names = [['Load Measurements', wx.ID_REPLACE, wx.ART_FIND_AND_REPLACE],
['Add Measurements', wx.ID_FIND, wx.ART_FIND],
['Save Session', wx.ID_SAVEAS, wx.ART_FILE_SAVE_AS],
['Open Session', wx.ID_OPEN, wx.ART_FILE_OPEN],
]
def add_icon(name):
self.toolbar.AddLabelTool(name[1],
_(name[0]),
bitmap=wx.ArtProvider.GetBitmap(
name[2],
wx.ART_TOOLBAR,
iconsize))
def add_image(name, height=-1, width=-1):
png = wx.Image(findfile(name), wx.BITMAP_TYPE_ANY)
image = wx.StaticBitmap(self.toolbar, -1, png.ConvertToBitmap(), size=(width,height))
self.toolbar.AddControl(image)
for name in names:
add_icon(name)
add_image("transparent_h50.png", width=75, height=iconsize[0])
add_image("zm_logo_h36.png")
try:
# This only works with wxPython3
self.toolbar.AddStretchableSpace()
except:
pass
add_image("shapeout_logotype_h36.png")
try:
# This only works with wxPython3
self.toolbar.AddStretchableSpace()
except:
pass
add_image("transparent_h50.png", height=iconsize[0])
add_icon(['Quit', wx.ID_EXIT, wx.ART_QUIT])
self.toolbar.Realize()
self.SetToolBar(self.toolbar)
#self.background_color = self.statusbar.GetBackgroundColour()
#self.statusbar.SetBackgroundColour(self.background_color)
#self.statusbar.SetBackgroundColour('RED')
#self.statusbar.SetBackgroundColour('#E0E2EB')
#self.statusbar.Refresh()
self.Bind(wx.EVT_CLOSE, self.OnMenuQuit)
def NewAnalysis(self, data, search_path="./"):
""" Create new analysis object and show data """
wx.BeginBusyCursor()
anal = analysis.Analysis(data, search_path=search_path)
# Get Plotting and Filtering parameters from previous analysis
if hasattr(self, "analysis"):
fpar = self.analysis.GetParameters("Filtering")
ppar = self.analysis.GetParameters("Plotting")
newcfg = {"Filtering" : fpar,
"Plotting" : ppar }
# set colors if more than one:
anal.SetParameters(newcfg)
# reset contour accuracies
anal.SetContourAccuracies()
# set default contour colors
anal.SetContourColors()
# set contour colors with previous colors
# - only works if len(colors) matches number of measurements
colors = self.analysis.GetContourColors()
anal.SetContourColors(colors)
self.analysis = anal
self.PanelTop.NewAnalysis(anal)
self.PlotArea.Plot(anal)
wx.EndBusyCursor()
def OnHelpAbout(self, e=None):
description = ("ShapeOut is a data evaluation tool"+
"\nfor real-time deformability cytometry (RT-DC)."+
"\nShapeOut is written in Python.")
licence = """ShapeOut 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 2 of the License,
or (at your option) any later version.
ShapeOut 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 <http://www.gnu.org/licenses/>.
"""
info = wx.AboutDialogInfo()
#info.SetIcon(wx.Icon('hunter.png', wx.BITMAP_TYPE_PNG))
info.SetName('ShapeOut')
info.SetVersion(self.version)
info.SetDescription(description)
info.SetCopyright(u'(C) 2015 Paul Müller')
info.SetWebSite(u"http://zellmechanik.com/")
info.SetLicence(licence)
info.SetIcon(misc.getMainIcon(pxlength=64))
info.AddDeveloper(u'Paul Müller')
info.AddDeveloper(u'Maik Herbig')
info.AddDeveloper(u'Philipp Rosendahl')
info.AddDocWriter(u'Paul Müller')
wx.AboutBox(info)
def OnHelpSoftware(self, e=None):
# Show About Information
from dclab import __version__ as dcversion
from scipy import __version__ as spversion
from pyper import __version__ as pyperversion
from .. import _version as so_version
from ..util import cran
r_version = cran.get_R_version()
if hasattr(so_version, "repo_tag"):
version = so_version.repo_tag # @UndefinedVariable
else:
version = so_version.version
text = "ShapeOut "+version+\
"\n\nPython "+sys.version+\
"\n\nModules:"+\
"\n - chaco "+chaco.__version__+\
"\n - dclab "+dcversion+\
"\n - NumPy "+np.__version__+\
"\n - OpenCV "+cv2.__version__+\
"\n - pyper "+pyperversion+\
"\n - SciPy "+spversion+\
"\n - wxPython "+wx.__version__
if hasattr(sys, 'frozen'):
pyinst = "\n\n"
pyinst += _("This executable has been created using PyInstaller.")
text += pyinst
if 'Anaconda' in sys.version or "Continuum Analytics" in sys.version:
conda = "\n\nPowered by Anaconda"
text += conda
mtext = "\n\n"
mtext += "Other software:\n"
mtext += "\n".join([ " "+r for r in r_version.split("\n")])
text += mtext
wx.MessageBox(text, 'Software', wx.OK|wx.ICON_INFORMATION)
def OnMenuBatchFolder(self, e=None):
batch.BatchFilterFolder(self, self.analysis)
def OnMenuClearMeasurements(self, e=None):
tree = self.PanelLeft.htreectrl
r = tree.GetRootItem()
dellist = []
# iterate through all measurements
for c in r.GetChildren():
for ch in c.GetChildren():
# keep those:
# - bold means it was analyzed
# - checked means user wants to analyze next
if not ch.IsChecked() and not ch.IsBold():
dellist.append(ch)
for ch in dellist:
tree.Delete(ch)
dellist = []
# find empty parents
for c in r.GetChildren():
if len(c.GetChildren()) == 0:
dellist.append(c)
for ch in dellist:
tree.Delete(ch)
def OnMenuExportData(self, e=None):
""" Export the event data of the entire analysis
This will open a choice dialog for the user
- which data (filtered/unfiltered)
- which columns (Area, Deformation, etc)
- to which folder should be exported
"""
# Generate dialog
export.ExportAnalysisEventsTSV(self, self.analysis)
def OnMenuExportPDF(self, e=None):
""" Saves plot container as PDF
Uses heuristic methods to resize
- the plot
- the scatter plot markers
and then changes everything back
"""
plot_export.export_plot_pdf(self)
def OnMenuExportPNG(self, e=None):
""" Saves plot container as png
"""
plot_export.export_plot_png(self)
def OnMenuExportStatistics(self, e=None):
""" Saves statistics results from tab to text file
"""
export.export_statistics_tsv(self)
def OnMenuLoad(self, e=None, session_file=None):
""" Load entire analysis """
# Determine which session file to open
if session_file is None:
# User dialog
dlg = wx.FileDialog(self, "Open session file",
self.config.GetWorkingDirectory(name="Session"), "",
"ShapeOut session (*.zmso)|*.zmso", wx.FD_OPEN)
if dlg.ShowModal() == wx.ID_OK:
self.config.SetWorkingDirectory(dlg.GetDirectory(),
name="Session")
fname = dlg.GetPath()
dlg.Destroy()
else:
self.config.SetWorkingDirectory(dlg.GetDirectory(),
name="Session")
dlg.Destroy()
return # nothing more to do here
else:
fname = session_file
dirname = os.path.dirname(fname)
self.config.SetWorkingDirectory(dirname)
session.open_session(fname, self)
def OnMenuSearchPath(self, e=None):
""" Set path of working directory
Display Dialog to select folder and update Content of PanelLeft.
This calls `PanelLeft.SetProjectTree`.
"""
dlg = wx.DirDialog(self, _("Please select a directory"),
defaultPath=self.config.GetWorkingDirectory(name="Main"))
answer = dlg.ShowModal()
if answer == wx.ID_OK:
path = dlg.GetPath()
self.config.SetWorkingDirectory(path, name="Main")
dlg.Destroy()
self.GaugeIndefiniteStart(
func=tlabwrap.GetTDMSTreeGUI,
func_args=(path,),
post_call=self.PanelLeft.SetProjectTree,
msg=_("Searching for .tdms files")
)
def OnMenuSearchPathAdd(self, e=None, add=True, path=None,
marked=[]):
""" Convenience wrapper around OnMenuSearchPath"""
if path is None:
dlg = wx.DirDialog(self, _("Please select a directory"),
defaultPath=self.config.GetWorkingDirectory())
answer = dlg.ShowModal()
path = dlg.GetPath()
self.config.SetWorkingDirectory(path, name="Main")
dlg.Destroy()
if answer != wx.ID_OK:
return
self.GaugeIndefiniteStart(
func=tlabwrap.GetTDMSTreeGUI,
func_args=(path,),
post_call=self.PanelLeft.SetProjectTree,
post_call_kwargs = {"add":add, "marked":marked},
msg=_("Searching for .tdms files")
)
def OnMenuQuit(self, e=None):
if hasattr(self, "analysis") and self.analysis is not None:
# Ask to save the session
dial = wx.MessageDialog(self,
'Do you want to save the current Session?',
'Save Session?',
wx.ICON_QUESTION | wx.CANCEL | wx.YES_NO | wx.NO_DEFAULT )
result = dial.ShowModal()
dial.Destroy()
if result == wx.ID_CANCEL:
return # abort
elif result == wx.ID_YES:
filename = self.OnMenuSaveSimple()
if filename is None:
# User did not save session - abort
return
# remove the autosaved file
os.remove(autosave.autosave_file)
os._exit(0)
def OnMenuSaveSimple(self, e=None):
""" Save configuration without measurement data """
dlg = wx.FileDialog(self, "Save ShapeOut session",
self.config.GetWorkingDirectory(name="Session"), "",
"ShapeOut session (*.zmso)|*.zmso",
wx.FD_SAVE|wx.FD_OVERWRITE_PROMPT)
if dlg.ShowModal() == wx.ID_OK:
# Save everything
path = dlg.GetPath()
if not path.endswith(".zmso"):
path += ".zmso"
dirname = os.path.dirname(path)
self.config.SetWorkingDirectory(dirname, name="Session")
session.save_session(path, self.analysis)
return path
else:
dirname = dlg.GetDirectory()
self.config.SetWorkingDirectory(dirname, name="Session")
dlg.Destroy()
def OnMenuSaveFull(self, e=None):
""" Save configuration including measurement data """
pass
########################################################################
def MyExceptionHook(etype, value, trace):
"""
Handler for all unhandled exceptions.
:param `etype`: the exception type (`SyntaxError`, `ZeroDivisionError`, etc...);
:type `etype`: `Exception`
:param string `value`: the exception error message;
:param string `trace`: the traceback header, if any (otherwise, it prints the
standard Python header: ``Traceback (most recent call last)``.
"""
wx.GetApp().GetTopWindow()
tmp = traceback.format_exception(etype, value, trace)
exception = "".join(tmp)
dlg = ExceptionDialog(exception)
dlg.ShowModal()
dlg.Destroy()
wx.EndBusyCursor()