Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit f30e8a7

Browse files
committedJul 12, 2020
EHN: Add 'Select Legend File' button
addresses issue DCBIA-OrthoLab#41
1 parent b6f9f7a commit f30e8a7

File tree

1 file changed

+38
-24
lines changed

1 file changed

+38
-24
lines changed
 

‎Q3DC/Q3DC.py

+38-24
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
import vtk, qt, ctk, slicer
2-
from slicer.ScriptedLoadableModule import *
3-
from slicer.util import NodeModify
4-
import csv, os
1+
import csv
52
from collections import defaultdict
6-
from pathlib import Path
73
import json
8-
import time
4+
import logging
95
import math
6+
import os
7+
import time
108

9+
import ctk
1110
import numpy as np
12-
13-
# needed for kd-trees
11+
import qt
1412
import scipy.spatial
13+
import vtk
1514

1615
# needed for topological sort. Yes, this is basically just DFS.
1716
try:
@@ -21,6 +20,10 @@
2120
slicer.util.pip_install('networkx')
2221
import networkx as nx
2322

23+
import slicer
24+
from slicer.ScriptedLoadableModule import *
25+
from slicer.util import NodeModify
26+
2427

2528
#
2629
# CalculateDisplacement
@@ -100,7 +103,6 @@ def setup(self):
100103
self.anatomical_legend_space = self.ui.landmarkModifLayout
101104
self.anatomical_radio_buttons_layout = qt.QHBoxLayout()
102105
self.anatomical_legend_space.addLayout(self.anatomical_radio_buttons_layout)
103-
self.init_anatomical_radio_buttons()
104106

105107
self.anatomical_legend = None
106108
self.init_anatomical_legend()
@@ -113,7 +115,7 @@ def setup(self):
113115
)
114116
self.anatomical_legend_view.connect('selectionChanged()', self.on_legend_row_selected)
115117

116-
self.anatomical_radio_buttons[0].toggle()
118+
self.init_anatomical_radio_buttons()
117119

118120
self.ui.legendFileButton.connect('clicked()', self.on_select_legend_file_clicked)
119121

@@ -370,6 +372,7 @@ def init_anatomical_radio_buttons(self):
370372
lambda state, _radio_button=radio_button:
371373
self.on_anatomical_radio_button_toggled(state, _radio_button)
372374
)
375+
self.anatomical_radio_buttons[0].toggle()
373376

374377
def on_anatomical_radio_button_toggled(self, state, radio_button):
375378
if state:
@@ -434,14 +437,12 @@ def on_select_legend_file_clicked(self):
434437
if legend_filename == '':
435438
# User canceled the file selection dialog.
436439
return
437-
try:
438-
self.suggested_landmarks = self.logic.load_suggested_landmarks(
439-
legend_filename)
440-
except KeyError:
441-
slicer.util.delayDisplay('The selected file does not have the right column names.')
440+
suggested_landmarks = self.logic.load_suggested_landmarks(
441+
legend_filename)
442+
if suggested_landmarks is None:
442443
return
444+
self.suggested_landmarks = suggested_landmarks
443445
self.init_anatomical_radio_buttons()
444-
self.anatomical_radio_buttons[0].toggle()
445446

446447
def onModelChanged(self):
447448
print("-------Model Changed--------")
@@ -674,14 +675,27 @@ def __init__(self, interface):
674675
@staticmethod
675676
def load_suggested_landmarks(filepath):
676677
suggested_landmarks = defaultdict(list)
677-
with open(filepath, newline='') as suggestions_file:
678-
reader = csv.DictReader(suggestions_file)
679-
for row in reader:
680-
region = row['Region'].title()
681-
landmark = row['Landmark']
682-
name = row['Name']
683-
suggested_landmarks[region].append((landmark, name))
684-
return suggested_landmarks
678+
try:
679+
with open(filepath, newline='') as suggestions_file:
680+
reader = csv.DictReader(suggestions_file)
681+
for row in reader:
682+
region = row['Region'].title()
683+
landmark = row['Landmark']
684+
name = row['Name']
685+
suggested_landmarks[region].append((landmark, name))
686+
return suggested_landmarks
687+
except OSError as e:
688+
slicer.util.delayDisplay('Unable to find/open file.')
689+
logging.info('User attempted to open a landmark legend file.\n' + repr(e))
690+
return None
691+
except csv.Error as e:
692+
slicer.util.delayDisplay('The selected file is not formatted properly.')
693+
logging.info('User attempted to open a landmark legend file.\n' + repr(e))
694+
return None
695+
except KeyError as e:
696+
slicer.util.delayDisplay('The selected file does not have the right column names.')
697+
logging.info('User attempted to open a landmark legend file.\n' + repr(e))
698+
return None
685699

686700
def initComboboxdict(self):
687701
self.comboboxdict[self.interface.landmarkComboBoxA] = None

0 commit comments

Comments
 (0)
Please sign in to comment.