-
Notifications
You must be signed in to change notification settings - Fork 287
/
app.py
610 lines (477 loc) · 19 KB
/
app.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
#!/usr/bin/env python3
# --------------------------------------------------------------------------
# Software: InVesalius - Software de Reconstrucao 3D de Imagens Medicas
# Copyright: (C) 2001 Centro de Pesquisas Renato Archer
# Homepage: http://www.softwarepublico.gov.br
# Contact: invesalius@cti.gov.br
# License: GNU - GPL 2 (LICENSE.txt/LICENCA.txt)
# --------------------------------------------------------------------------
# Este programa e software livre; voce pode redistribui-lo e/ou
# modifica-lo sob os termos da Licenca Publica Geral GNU, conforme
# publicada pela Free Software Foundation; de acordo com a versao 2
# da Licenca.
#
# Este programa eh distribuido na expectativa de ser util, mas SEM
# QUALQUER GARANTIA; sem mesmo a garantia implicita de
# COMERCIALIZACAO ou de ADEQUACAO A QUALQUER PROPOSITO EM
# PARTICULAR. Consulte a Licenca Publica Geral GNU para obter mais
# detalhes.
# -------------------------------------------------------------------------
import argparse
import multiprocessing
import os
import re
import shutil
import sys
import traceback
if sys.platform == "darwin":
try:
import certifi
os.environ["SSL_CERT_FILE"] = certifi.where()
except ImportError:
pass
if sys.platform == "win32":
try:
import winreg
except ImportError:
import _winreg as winreg
# else:
# if sys.platform != 'darwin':
# import wxversion
# #wxversion.ensureMinimal('2.8-unicode', optionsRequired=True)
# #wxversion.select('2.8-unicode', optionsRequired=True)
# # wxversion.ensureMinimal('4.0')
# Forcing to use X11, OpenGL in wxPython doesn't work with Wayland.
if sys.platform not in ("win32", "darwin"):
os.environ["GDK_BACKEND"] = "x11"
import wx
from wx.adv import SPLASH_CENTRE_ON_SCREEN, SPLASH_TIMEOUT, SplashScreen
# import wx.lib.agw.advancedsplash as agw
# if sys.platform.startswith('linux'):
# _SplashScreen = agw.AdvancedSplash
# else:
# if sys.platform != 'darwin':
# _SplashScreen = wx.SplashScreen
import invesalius.gui.language_dialog as lang_dlg
import invesalius.gui.log as log
import invesalius.i18n as i18n
import invesalius.session as ses
import invesalius.utils as utils
from invesalius import inv_paths
from invesalius.pubsub import pub as Publisher
FS_ENCODE = sys.getfilesystemencoding()
LANG = None
# ------------------------------------------------------------------
if sys.platform in ("linux2", "linux", "win32"):
if not hasattr(wx, "GetXDisplay"):
setattr(wx, "GetXDisplay", lambda: None)
session = ses.Session()
if session.ReadConfig():
lang = session.GetConfig("language")
if lang:
try:
LANG = lang
except FileNotFoundError:
pass
class InVesalius(wx.App):
"""
InVesalius wxPython application class.
"""
def OnInit(self):
"""
Initialize splash screen and main frame.
"""
from multiprocessing import freeze_support
freeze_support()
self.SetAppName("InVesalius 3")
self.splash = Inv3SplashScreen()
self.splash.Show()
wx.CallLater(1000, self.Startup2)
return True
def MacOpenFile(self, filename):
"""
Open drag & drop files under darwin
"""
path = os.path.abspath(filename)
Publisher.sendMessage("Open project", filepath=path)
def Startup2(self):
self.control = self.splash.control
self.frame = self.splash.main
self.SetTopWindow(self.frame)
self.frame.Show()
self.frame.Raise()
# logger = log.MyLogger()
log.invLogger.configureLogging()
# ------------------------------------------------------------------
class Inv3SplashScreen(SplashScreen):
"""
Splash screen to be shown in InVesalius initialization.
"""
def __init__(self):
# Splash screen image will depend on the current language
lang = LANG
self.locale = wx.Locale(wx.LANGUAGE_ENGLISH)
# Language information is available in session configuration
# file. First we need to check if this file exist, if now, it
# should be created
create_session = LANG is None
install_lang = False
if lang:
install_lang = True
# If no language is set into session file, show dialog so
# user can select language
if not install_lang:
dialog = lang_dlg.LanguageDialog()
# FIXME: This works ok in linux2, darwin and win32,
# except on win64, due to wxWidgets bug
try:
ok = dialog.ShowModal() == wx.ID_OK
except wx.PyAssertionError:
ok = True
finally:
if ok:
lang = dialog.GetSelectedLanguage()
session.SetConfig("language", lang)
i18n.tr.reset()
else:
homedir = os.path.expanduser("~")
config_dir = os.path.join(homedir, ".invesalius")
shutil.rmtree(config_dir)
sys.exit()
dialog.Destroy()
# Session file should be created... So we set the recently chosen language.
if create_session:
session.CreateConfig()
session.SetConfig("language", lang)
# Only after language was defined, splash screen will be shown.
if lang:
# import locale
# try:
# locale.setlocale(locale.LC_ALL, '')
# except locale.Error:
# pass
# For pt_BR, splash_pt.png should be used
if lang.startswith("pt"):
icon_file = "splash_pt.png"
else:
icon_file = "splash_" + lang + ".png"
if hasattr(sys, "frozen") and (
getattr(sys, "frozen") == "windows_exe" or getattr(sys, "frozen") == "console_exe"
):
abs_file_path = os.path.abspath(".." + os.sep)
path = abs_file_path
path = os.path.join(path, "icons", icon_file)
else:
path = os.path.join(inv_paths.ICON_DIR, icon_file)
if not os.path.exists(path):
path = os.path.join(inv_paths.ICON_DIR, "splash_en.png")
bmp = wx.Image(path).ConvertToBitmap()
style = SPLASH_TIMEOUT | SPLASH_CENTRE_ON_SCREEN
SplashScreen.__init__(
self, bitmap=bmp, splashStyle=style, milliseconds=1500, id=-1, parent=None
)
self.Bind(wx.EVT_CLOSE, self.OnClose)
wx.GetApp().Yield()
wx.CallLater(200, self.Startup)
def Startup(self):
# Importing takes sometime, therefore it will be done
# while splash is being shown
from invesalius.control import Controller
from invesalius.gui.frame import Frame
self.main = Frame(None)
self.control = Controller(self.main)
self.fc = wx.CallLater(200, self.ShowMain)
args = parse_command_line()
wx.CallLater(1, use_cmd_optargs, args)
# Check for updates
from threading import Thread
p = Thread(target=utils.UpdateCheck, args=())
p.start()
if not session.ExitedSuccessfullyLastTime():
# Reopen project
project_path = session.GetState("project_path")
if project_path is not None:
filepath = os.path.join(project_path[0], project_path[1])
if os.path.exists(filepath):
Publisher.sendMessage("Open project", filepath=filepath)
else:
utils.debug(f"File doesn't exist: {filepath}")
session.CloseProject()
else:
session.CreateState()
def OnClose(self, evt):
# Make sure the default handler runs too so this window gets
# destroyed
evt.Skip()
self.Hide()
evt.GetEventObject().Destroy()
# If the timer is still running then go ahead and show the
# main frame now
if self.fc.IsRunning():
self.fc.Stop()
self.ShowMain()
def ShowMain(self):
# Show main frame
self.main.Show()
if self.fc.IsRunning():
self.Raise()
def non_gui_startup(args):
if LANG:
lang = LANG
else:
lang = "en"
_ = i18n.InstallLanguage(lang)
from invesalius.control import Controller
session = ses.Session()
if not session.ReadConfig():
session.CreateConfig()
session.SetConfig("language", lang)
_ = Controller(None)
use_cmd_optargs(args)
# ------------------------------------------------------------------
def parse_command_line():
"""
Handle command line arguments.
"""
# Parse command line arguments
parser = argparse.ArgumentParser()
# -d or --debug: print all pubsub messages sent
parser.add_argument("-d", "--debug", action="store_true", dest="debug")
parser.add_argument("project_file", nargs="?", default="", help="InVesalius 3 project file")
parser.add_argument("--no-gui", action="store_true", dest="no_gui")
# -i or --import: import DICOM directory
# chooses largest series
parser.add_argument("-i", "--import", action="store", dest="dicom_dir")
parser.add_argument("--import-all", action="store")
parser.add_argument("--import-folder", action="store", dest="import_folder")
parser.add_argument(
"-o", "--import-other", dest="other_file", help="Import Nifti, Analyze, PAR/REC file"
)
parser.add_argument("--remote-host", action="store", dest="remote_host")
parser.add_argument("-s", "--save", help="Save the project after an import.")
parser.add_argument(
"-t", "--threshold", help="Define the threshold for the export (e.g. 100-780)."
)
parser.add_argument("-e", "--export", help="Export to STL.")
parser.add_argument("-a", "--export-to-all", help="Export to STL for all mask presets.")
parser.add_argument("--export-project", help="Export slices and mask to HDF5 or Nifti file.")
parser.add_argument(
"--no-masks",
action="store_false",
dest="save_masks",
default=True,
help="Make InVesalius not export mask when exporting project.",
)
parser.add_argument(
"--use-pedal", action="store_true", dest="use_pedal", help="Use an external trigger pedal"
)
parser.add_argument(
"--debug-efield",
action="store_true",
dest="debug_efield",
help="Debug navigated TMS E-field computation",
)
parser.add_argument("--cranioplasty", help="Creates an AI-based cranioplasty implant.")
args = parser.parse_args()
return args
def use_cmd_optargs(args):
# If import DICOM argument...
if args.dicom_dir:
import_dir = args.dicom_dir
Publisher.sendMessage("Import directory", directory=import_dir, use_gui=not args.no_gui)
if args.save:
Publisher.sendMessage("Save project", filepath=os.path.abspath(args.save))
exit(0)
if args.cranioplasty:
check_for_cranioplasty(args)
else:
check_for_export(args)
return True
elif args.import_folder:
Publisher.sendMessage("Import folder", folder=args.import_folder)
if args.save:
Publisher.sendMessage("Save project", filepath=os.path.abspath(args.save))
exit(0)
if args.cranioplasty:
check_for_cranioplasty(args)
else:
check_for_export(args)
elif args.other_file:
Publisher.sendMessage("Open other files", filepath=args.other_file)
if args.save:
Publisher.sendMessage("Save project", filepath=os.path.abspath(args.save))
exit(0)
if args.cranioplasty:
check_for_cranioplasty(args)
else:
check_for_export(args)
elif args.import_all:
import invesalius.reader.dicom_reader as dcm
for patient in dcm.GetDicomGroups(args.import_all):
for group in patient.GetGroups():
Publisher.sendMessage("Import group", group=group, use_gui=not args.no_gui)
check_for_export(args, suffix=group.title, remove_surfaces=False)
Publisher.sendMessage("Remove masks", mask_indexes=(0,))
return True
# Check if there is a file path somewhere in what the user wrote
# In case there is, try opening as it was a inv3
else:
if args.project_file:
file = utils.decode(args.project_file, FS_ENCODE)
if os.path.isfile(file):
path = os.path.abspath(file)
Publisher.sendMessage("Open project", filepath=path)
check_for_export(args)
return True
file = utils.decode(args.project_file, sys.stdin.encoding)
if os.path.isfile(file):
path = os.path.abspath(file)
Publisher.sendMessage("Open project", filepath=path)
check_for_export(args)
return True
return False
def check_for_cranioplasty(args):
import invesalius.constants as const
from invesalius.i18n import tr as _
if args.cranioplasty:
from invesalius.data import slice_
from invesalius.project import Project
# create cranium mask
Publisher.sendMessage("Update threshold limits", threshold_range=(226, 3071))
Publisher.sendMessage("Appy threshold all slices")
# create implant mask
Publisher.sendMessage("Create implant for cranioplasty")
path_ = args.export
# convert masks to surfaces and exports them.
Publisher.sendMessage(
"Export all surfaces separately", folder=path_, filetype=const.FILETYPE_STL
)
def sanitize(text):
text = str(text).strip().replace(" ", "_")
return re.sub(r"(?u)[^-\w.]", "", text)
def check_for_export(args, suffix="", remove_surfaces=False):
suffix = sanitize(suffix)
if args.export:
if not args.threshold:
print("Need option --threshold when using --export.")
exit(1)
threshold_range = tuple([int(n) for n in args.threshold.split(",")])
if suffix:
if args.export.endswith(".stl"):
path_ = f"{args.export[:-4]}-{suffix}.stl"
else:
path_ = f"{args.export}-{suffix}.stl"
else:
path_ = args.export
export(path_, threshold_range, remove_surface=remove_surfaces)
elif args.export_to_all:
# noinspection PyBroadException
try:
from invesalius.project import Project
for threshold_name, threshold_range in Project().presets.thresh_ct.items():
if isinstance(threshold_range[0], int):
path_ = f"{args.export_to_all}-{suffix}-{threshold_name}.stl"
export(path_, threshold_range, remove_surface=True)
except Exception:
traceback.print_exc()
finally:
exit(0)
if args.export_project:
from invesalius.project import Project
prj = Project()
export_filename = args.export_project
if suffix:
export_filename, ext = os.path.splitext(export_filename)
export_filename = f"{export_filename}-{suffix}{ext}"
prj.export_project(export_filename, save_masks=args.save_masks)
print(f"Saved {export_filename}")
def export(path_, threshold_range, remove_surface=False):
import invesalius.constants as const
from invesalius.i18n import tr as _
Publisher.sendMessage("Set threshold values", threshold_range=threshold_range)
surface_options = {
"method": {
"algorithm": "Default",
"options": {},
},
"options": {
"index": 0,
"name": "",
"quality": _("Optimal *"),
"fill": False,
"keep_largest": False,
"overwrite": False,
},
}
Publisher.sendMessage("Create surface from index", surface_parameters=surface_options)
Publisher.sendMessage("Export surface to file", filename=path_, filetype=const.FILETYPE_STL)
if remove_surface:
Publisher.sendMessage("Remove surfaces", surface_indexes=(0,))
def print_events(topic=Publisher.AUTO_TOPIC, **msg_data):
"""
Print pubsub messages
"""
utils.debug(f"{topic}\n\tParameters: {msg_data}")
def init():
"""
Initialize InVesalius.
Mostly file-system related initializations.
"""
# Is needed because of pyinstaller
multiprocessing.freeze_support()
# Needed in win 32 exe
if hasattr(sys, "frozen") and sys.platform == "win32":
# Click in the .inv3 file support
root = winreg.HKEY_CLASSES_ROOT
key = "InVesalius 3.1\InstallationDir"
hKey = winreg.OpenKey(root, key, 0, winreg.KEY_READ)
value, type_ = winreg.QueryValueEx(hKey, "")
path = os.path.join(value, "dist")
os.chdir(path)
if not inv_paths.USER_INV_DIR.exists():
inv_paths.create_conf_folders()
if inv_paths.OLD_USER_INV_DIR.exists():
inv_paths.copy_old_files()
if hasattr(sys, "frozen") and getattr(sys, "frozen") == "windows_exe":
# Set system standard error output to file
path = inv_paths.USER_LOG_DIR.joinpath("stderr.log")
sys.stderr = open(path, "w")
def main(connection=None, remote_host=None):
"""
Start InVesalius.
Parameters:
connection: An object to communicate with the outside world.
In theory, can be any object supports certain function calls.
See invesalius.net.neuronavigation_api for a comprehensive
description of how the object is used.
Note that if InVesalius is started in the usual way by running
app.py, the connection object defaults to None. To enable this
functionality, InVesalius needs to be started by calling the main
function directly with a proper connection object.
remote_host: Specifies the address and the port of the remote host with
which InVesalius should communicate. If provided, overrides any value
passed through the command line using the --remote-host argument.
"""
init()
args = parse_command_line()
session = ses.Session()
session.SetConfig("debug", args.debug)
session.SetConfig("debug_efield", args.debug_efield)
if args.debug:
Publisher.subscribe(print_events, Publisher.ALL_TOPICS)
if remote_host is not None or args.remote_host is not None:
from invesalius.net.remote_control import RemoteControl
remote_control = RemoteControl(remote_host or args.remote_host)
remote_control.connect()
if args.use_pedal:
from invesalius.net.pedal_connection import MidiPedal
MidiPedal().start()
from invesalius.net.neuronavigation_api import NeuronavigationApi
NeuronavigationApi(connection)
if args.no_gui:
non_gui_startup(args)
else:
application = InVesalius(False)
application.MainLoop()
if __name__ == "__main__":
main()