1
- import vtk , qt , ctk , slicer
2
- from slicer .ScriptedLoadableModule import *
3
- from slicer .util import NodeModify
4
- import csv , os
1
+ import csv
5
2
from collections import defaultdict
6
- from pathlib import Path
7
3
import json
8
- import time
4
+ import logging
9
5
import math
6
+ import os
7
+ import time
10
8
9
+ import ctk
11
10
import numpy as np
12
-
13
- # needed for kd-trees
11
+ import qt
14
12
import scipy .spatial
13
+ import vtk
15
14
16
15
# needed for topological sort. Yes, this is basically just DFS.
17
16
try :
21
20
slicer .util .pip_install ('networkx' )
22
21
import networkx as nx
23
22
23
+ import slicer
24
+ from slicer .ScriptedLoadableModule import *
25
+ from slicer .util import NodeModify
26
+
24
27
25
28
#
26
29
# CalculateDisplacement
@@ -100,7 +103,6 @@ def setup(self):
100
103
self .anatomical_legend_space = self .ui .landmarkModifLayout
101
104
self .anatomical_radio_buttons_layout = qt .QHBoxLayout ()
102
105
self .anatomical_legend_space .addLayout (self .anatomical_radio_buttons_layout )
103
- self .init_anatomical_radio_buttons ()
104
106
105
107
self .anatomical_legend = None
106
108
self .init_anatomical_legend ()
@@ -113,7 +115,7 @@ def setup(self):
113
115
)
114
116
self .anatomical_legend_view .connect ('selectionChanged()' , self .on_legend_row_selected )
115
117
116
- self .anatomical_radio_buttons [ 0 ]. toggle ()
118
+ self .init_anatomical_radio_buttons ()
117
119
118
120
self .ui .legendFileButton .connect ('clicked()' , self .on_select_legend_file_clicked )
119
121
@@ -370,6 +372,7 @@ def init_anatomical_radio_buttons(self):
370
372
lambda state , _radio_button = radio_button :
371
373
self .on_anatomical_radio_button_toggled (state , _radio_button )
372
374
)
375
+ self .anatomical_radio_buttons [0 ].toggle ()
373
376
374
377
def on_anatomical_radio_button_toggled (self , state , radio_button ):
375
378
if state :
@@ -434,14 +437,12 @@ def on_select_legend_file_clicked(self):
434
437
if legend_filename == '' :
435
438
# User canceled the file selection dialog.
436
439
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 :
442
443
return
444
+ self .suggested_landmarks = suggested_landmarks
443
445
self .init_anatomical_radio_buttons ()
444
- self .anatomical_radio_buttons [0 ].toggle ()
445
446
446
447
def onModelChanged (self ):
447
448
print ("-------Model Changed--------" )
@@ -674,14 +675,27 @@ def __init__(self, interface):
674
675
@staticmethod
675
676
def load_suggested_landmarks (filepath ):
676
677
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
685
699
686
700
def initComboboxdict (self ):
687
701
self .comboboxdict [self .interface .landmarkComboBoxA ] = None
0 commit comments