Skip to content

Commit 6261edc

Browse files
authored
Create a module to keep invesalius path (invesalius#183)
* created a file to keep all the inv paths * frame and task_slice using inv_paths * task slice, surface, export and data_notebook using invesalius.inv_paths * clut_raycasting using inv_paths * using pathlib in frame * dicom reader using inv_paths * neuronavigation using inv_paths * session using inv_paths * dialogs using inv_paths
1 parent a042b58 commit 6261edc

23 files changed

+290
-252
lines changed

app.py

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -65,21 +65,9 @@
6565
import invesalius.session as ses
6666
import invesalius.utils as utils
6767

68-
FS_ENCODE = sys.getfilesystemencoding()
69-
70-
if sys.platform == 'win32':
71-
from invesalius.expanduser import expand_user
72-
try:
73-
USER_DIR = expand_user()
74-
except:
75-
USER_DIR = utils.decode(os.path.expanduser('~'), FS_ENCODE)
76-
else:
77-
USER_DIR = utils.decode(os.path.expanduser('~'),FS_ENCODE)
68+
from invesalius import inv_paths
7869

79-
USER_INV_DIR = os.path.join(USER_DIR, u'.invesalius')
80-
USER_PRESET_DIR = os.path.join(USER_INV_DIR, u'presets')
81-
USER_RAYCASTING_PRESETS_DIRECTORY = os.path.join(USER_PRESET_DIR, u'raycasting')
82-
USER_LOG_DIR = os.path.join(USER_INV_DIR, u'logs')
70+
FS_ENCODE = sys.getfilesystemencoding()
8371

8472
# ------------------------------------------------------------------
8573

@@ -510,17 +498,14 @@ def main():
510498

511499
os.chdir(path)
512500

513-
# Create raycasting presets' folder, if it doens't exist
514-
if not os.path.isdir(USER_RAYCASTING_PRESETS_DIRECTORY):
515-
os.makedirs(USER_RAYCASTING_PRESETS_DIRECTORY)
516-
517-
# Create logs' folder, if it doesn't exist
518-
if not os.path.isdir(USER_LOG_DIR):
519-
os.makedirs(USER_LOG_DIR)
501+
if not inv_paths.USER_INV_DIR.exists():
502+
inv_paths.create_conf_folders()
503+
if inv_paths.OLD_USER_INV_DIR.exists():
504+
inv_paths.copy_old_files()
520505

521506
if hasattr(sys,"frozen") and sys.frozen == "windows_exe":
522507
# Set system standard error output to file
523-
path = os.path.join(USER_LOG_DIR, u"stderr.log")
508+
path = inv_paths.USER_LOG_DIR.join("stderr.log")
524509
sys.stderr = open(path, "w")
525510

526511
# Add current directory to PYTHONPATH, so other classes can

invesalius/constants.py

Lines changed: 13 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import itertools
2525

2626
from invesalius import utils
27+
from invesalius import inv_paths
2728

2829
#from invesalius.project import Project
2930
INVESALIUS_VERSION = "3.1.1"
@@ -335,59 +336,13 @@
335336
# PATHS
336337
FS_ENCODE = sys.getfilesystemencoding()
337338

338-
if sys.platform == 'win32':
339-
from invesalius.expanduser import expand_user
340-
try:
341-
USER_DIR = expand_user()
342-
except:
343-
USER_DIR = utils.decode(os.path.expanduser('~'), FS_ENCODE)
344-
else:
345-
USER_DIR = utils.decode(os.path.expanduser('~'), FS_ENCODE)
346-
347-
USER_INV_DIR = os.path.join(USER_DIR, u'.invesalius')
348-
USER_PRESET_DIR = os.path.join(USER_INV_DIR, u'presets')
349-
USER_LOG_DIR = os.path.join(USER_INV_DIR, u'logs')
350-
351-
FILE_PATH = utils.decode(os.path.split(__file__)[0], FS_ENCODE)
352-
353-
if hasattr(sys,"frozen") and (sys.frozen == "windows_exe"\
354-
or sys.frozen == "console_exe"):
355-
abs_path = os.path.abspath(FILE_PATH, u'..', u'..', u'..')
356-
ICON_DIR = os.path.join(abs_path, u"icons")
357-
SAMPLE_DIR = os.path.join(FILE_PATH, u'samples')
358-
DOC_DIR = os.path.join(FILE_PATH, u'docs')
359-
folder=RAYCASTING_PRESETS_DIRECTORY= os.path.join(abs_path, u"presets", u"raycasting")
360-
RAYCASTING_PRESETS_COLOR_DIRECTORY = os.path.join(abs_path, u"presets", u"raycasting", u"color_list")
361-
362-
else:
363-
ICON_DIR = os.path.abspath(os.path.join(FILE_PATH, u'..', u'icons'))
364-
SAMPLE_DIR = os.path.abspath(os.path.join(FILE_PATH, u'..', u'samples'))
365-
DOC_DIR = os.path.abspath(os.path.join(FILE_PATH, u'..', u'docs'))
366-
367-
folder=RAYCASTING_PRESETS_DIRECTORY= os.path.abspath(os.path.join(u".",
368-
u"presets",
369-
u"raycasting"))
370-
371-
RAYCASTING_PRESETS_COLOR_DIRECTORY = os.path.abspath(os.path.join(u".",
372-
u"presets",
373-
u"raycasting",
374-
u"color_list"))
375-
376-
377-
# MAC App
378-
if not os.path.exists(ICON_DIR):
379-
ICON_DIR = os.path.abspath(os.path.join(FILE_PATH, u'..', u'..', u'..', u'..', u'icons'))
380-
SAMPLE_DIR = os.path.abspath(os.path.join(FILE_PATH, u'..', u'..', u'..', u'..', u'samples'))
381-
DOC_DIR = os.path.abspath(os.path.join(FILE_PATH, u'..', u'..', u'..', u'..', u'docs'))
382-
383-
384-
ID_TO_BMP = {VOL_FRONT: [_("Front"), os.path.join(ICON_DIR, u"view_front.png")],
385-
VOL_BACK: [_("Back"), os.path.join(ICON_DIR, u"view_back.png")],
386-
VOL_TOP: [_("Top"), os.path.join(ICON_DIR, u"view_top.png")],
387-
VOL_BOTTOM: [_("Bottom"), os.path.join(ICON_DIR, u"view_bottom.png")],
388-
VOL_RIGHT: [_("Right"), os.path.join(ICON_DIR, u"view_right.png")],
389-
VOL_LEFT: [_("Left"), os.path.join(ICON_DIR, u"view_left.png")],
390-
VOL_ISO:[_("Isometric"), os.path.join(ICON_DIR, u"view_isometric.png")]
339+
ID_TO_BMP = {VOL_FRONT: [_("Front"), str(inv_paths.ICON_DIR.joinpath("view_front.png"))],
340+
VOL_BACK: [_("Back"), str(inv_paths.ICON_DIR.joinpath("view_back.png"))],
341+
VOL_TOP: [_("Top"), str(inv_paths.ICON_DIR.joinpath("view_top.png"))],
342+
VOL_BOTTOM: [_("Bottom"), str(inv_paths.ICON_DIR.joinpath("view_bottom.png"))],
343+
VOL_RIGHT: [_("Right"), str(inv_paths.ICON_DIR.joinpath("view_right.png"))],
344+
VOL_LEFT: [_("Left"), str(inv_paths.ICON_DIR.joinpath("view_left.png"))],
345+
VOL_ISO:[_("Isometric"), str(inv_paths.ICON_DIR.joinpath("view_isometric.png"))]
391346
}
392347

393348
# if 1, use vtkVolumeRaycastMapper, if 0, use vtkFixedPointVolumeRayCastMapper
@@ -433,10 +388,9 @@
433388
# os.path.isfile(os.path.join(folder,filename))]
434389

435390

436-
USER_RAYCASTING_PRESETS_DIRECTORY = os.path.join(USER_PRESET_DIR, u'raycasting')
437-
RAYCASTING_TYPES = [_(filename.split(".")[0]) for filename in
438-
os.listdir(USER_RAYCASTING_PRESETS_DIRECTORY) if
439-
os.path.isfile(os.path.join(USER_RAYCASTING_PRESETS_DIRECTORY, filename))]
391+
RAYCASTING_TYPES = [_(filename.name.split(".")[0]) for filename in
392+
inv_paths.USER_RAYCASTING_PRESETS_DIRECTORY.glob('*') if
393+
filename.is_file()]
440394
RAYCASTING_TYPES += RAYCASTING_FILES.keys()
441395
RAYCASTING_TYPES.append(_(' Off'))
442396
RAYCASTING_TYPES.sort()
@@ -446,8 +400,8 @@
446400
# If 0 dont't blur, 1 blur
447401
RAYCASTING_WWWL_BLUR = 0
448402

449-
RAYCASTING_PRESETS_FOLDERS = (RAYCASTING_PRESETS_DIRECTORY,
450-
USER_RAYCASTING_PRESETS_DIRECTORY)
403+
RAYCASTING_PRESETS_FOLDERS = (inv_paths.RAYCASTING_PRESETS_DIRECTORY,
404+
inv_paths.USER_RAYCASTING_PRESETS_DIRECTORY)
451405

452406

453407
####
@@ -761,14 +715,11 @@
761715
_("Select object center"),
762716
_("Attach sensor to object")]
763717

764-
CAL_DIR = os.path.abspath(os.path.join(FILE_PATH, '..', 'navigation', 'mtc_files', 'CalibrationFiles'))
765-
MAR_DIR = os.path.abspath(os.path.join(FILE_PATH, '..', 'navigation', 'mtc_files', 'Markers'))
766718
PROBE_NAME = "1Probe"
767719
REF_NAME = "2Ref"
768720
OBJ_NAME = "3Coil"
769721

770722
#OBJECT TRACKING
771-
OBJ_DIR = os.path.abspath(os.path.join(FILE_PATH, '..', 'navigation', 'objects'))
772723
ARROW_SCALE = 3
773724
ARROW_UPPER_LIMIT = 30
774725
#COIL_ANGLES_THRESHOLD = 3 * ARROW_SCALE

invesalius/control.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
import subprocess
4343
import sys
4444

45+
from invesalius import inv_paths
46+
4547
DEFAULT_THRESH_MODE = 0
4648

4749
class Controller():
@@ -920,13 +922,13 @@ def LoadImagedataInfo(self):
920922
def LoadRaycastingPreset(self, preset_name):
921923
if preset_name != const.RAYCASTING_OFF_LABEL:
922924
if preset_name in const.RAYCASTING_FILES.keys():
923-
path = os.path.join(const.RAYCASTING_PRESETS_DIRECTORY,
925+
path = os.path.join(inv_paths.RAYCASTING_PRESETS_DIRECTORY,
924926
const.RAYCASTING_FILES[preset_name])
925927
else:
926-
path = os.path.join(const.RAYCASTING_PRESETS_DIRECTORY,
928+
path = os.path.join(inv_paths.RAYCASTING_PRESETS_DIRECTORY,
927929
preset_name+".plist")
928930
if not os.path.isfile(path):
929-
path = os.path.join(const.USER_RAYCASTING_PRESETS_DIRECTORY,
931+
path = os.path.join(inv_paths.USER_RAYCASTING_PRESETS_DIRECTORY,
930932
preset_name+".plist")
931933
preset = plistlib.readPlist(path)
932934
prj.Project().raycasting_preset = preset
@@ -940,7 +942,7 @@ def LoadRaycastingPreset(self, preset_name):
940942
def SaveRaycastingPreset(self, preset_name):
941943
preset = prj.Project().raycasting_preset
942944
preset['name'] = preset_name
943-
preset_dir = os.path.join(const.USER_RAYCASTING_PRESETS_DIRECTORY,
945+
preset_dir = os.path.join(inv_paths.USER_RAYCASTING_PRESETS_DIRECTORY,
944946
preset_name + '.plist')
945947
plistlib.writePlist(preset, preset_dir)
946948

invesalius/data/imagedata_utils.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
import invesalius.utils as utils
3838
import invesalius.data.converters as converters
3939

40+
from invesalius import inv_paths
41+
4042
if sys.platform == 'win32':
4143
try:
4244
import win32api
@@ -335,7 +337,7 @@ def CreateImageData(filelist, zspacing, xyspacing,size,
335337
message = _("Generating multiplanar visualization...")
336338

337339
if not const.VTK_WARNING:
338-
log_path = os.path.join(const.USER_LOG_DIR, 'vtkoutput.txt')
340+
log_path = os.path.join(inv_paths.USER_LOG_DIR, 'vtkoutput.txt')
339341
fow = vtk.vtkFileOutputWindow()
340342
fow.SetFileName(log_path)
341343
ow = vtk.vtkOutputWindow()
@@ -440,7 +442,7 @@ def CreateImageData(self, filelist, zspacing, size, bits):
440442
message = _("Generating multiplanar visualization...")
441443

442444
if not const.VTK_WARNING:
443-
log_path = os.path.join(const.USER_LOG_DIR, 'vtkoutput.txt')
445+
log_path = os.path.join(inv_paths.USER_LOG_DIR, 'vtkoutput.txt')
444446
fow = vtk.vtkFileOutputWindow()
445447
fow.SetFileName(log_path)
446448
ow = vtk.vtkOutputWindow()

invesalius/data/trackers.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,16 @@ def DefaultTracker(tracker_id):
6161

6262
def ClaronTracker(tracker_id):
6363
import invesalius.constants as const
64+
from invesalius import inv_paths
6465

6566
trck_init = None
6667
try:
6768
import pyclaron
6869

6970
lib_mode = 'wrapper'
7071
trck_init = pyclaron.pyclaron()
71-
trck_init.CalibrationDir = const.CAL_DIR.encode(const.FS_ENCODE)
72-
trck_init.MarkerDir = const.MAR_DIR.encode(const.FS_ENCODE)
72+
trck_init.CalibrationDir = inv_paths.CAL_DIR.encode(const.FS_ENCODE)
73+
trck_init.MarkerDir = inv_paths.MAR_DIR.encode(const.FS_ENCODE)
7374
trck_init.NumberFramesProcessed = 1
7475
trck_init.FramesExtrapolated = 0
7576
trck_init.PROBE_NAME = const.PROBE_NAME.encode(const.FS_ENCODE)

invesalius/data/viewer_volume.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
import invesalius.style as st
4343
import invesalius.utils as utils
4444

45+
from invesalius import inv_paths
46+
4547
if sys.platform == 'win32':
4648
try:
4749
import win32api
@@ -873,7 +875,7 @@ def CreateTargetAim(self):
873875

874876
self.m_img_vtk = m_img_vtk
875877

876-
filename = os.path.join(const.OBJ_DIR, "aim.stl")
878+
filename = os.path.join(inv_paths.OBJ_DIR, "aim.stl")
877879

878880
reader = vtk.vtkSTLReader()
879881
reader.SetFileName(filename)
@@ -898,7 +900,7 @@ def CreateTargetAim(self):
898900
self.aim_actor = aim_actor
899901
self.ren.AddActor(aim_actor)
900902

901-
obj_polydata = self.CreateObjectPolyData(os.path.join(const.OBJ_DIR, "magstim_fig8_coil_no_handle.stl"))
903+
obj_polydata = self.CreateObjectPolyData(os.path.join(inv_paths.OBJ_DIR, "magstim_fig8_coil_no_handle.stl"))
902904

903905
transform = vtk.vtkTransform()
904906
transform.RotateZ(90)
@@ -1148,7 +1150,7 @@ def CreateObjectPolyData(self, filename):
11481150
wx.MessageBox(_("File format not reconized by InVesalius"), _("Import surface error"))
11491151
return
11501152
else:
1151-
filename = os.path.join(const.OBJ_DIR, "magstim_fig8_coil.stl")
1153+
filename = os.path.join(inv_paths.OBJ_DIR, "magstim_fig8_coil.stl")
11521154
reader = vtk.vtkSTLReader()
11531155

11541156
if _has_win32api:

invesalius/data/volume.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333
from vtk.util import numpy_support
3434
import invesalius.session as ses
3535

36+
from invesalius import inv_paths
37+
3638

3739
Kernels = {
3840
"Basic Smooth 5x5" : [1.0, 1.0, 1.0, 1.0, 1.0,
@@ -352,7 +354,7 @@ def Create8bColorTable(self, scale):
352354
color_preset = self.config['CLUT']
353355
if color_preset != "No CLUT":
354356
p = plistlib.readPlist(
355-
os.path.join(const.RAYCASTING_PRESETS_DIRECTORY,
357+
os.path.join(inv_paths.RAYCASTING_PRESETS_DIRECTORY,
356358
'color_list', color_preset + '.plist'))
357359
r = p['Red']
358360
g = p['Green']

0 commit comments

Comments
 (0)