Skip to content

Commit

Permalink
EHN: Add 'Select Legend File' button
Browse files Browse the repository at this point in the history
addresses issue DCBIA-OrthoLab#41
  • Loading branch information
JEHoctor committed Jul 12, 2020
1 parent b6f9f7a commit f30e8a7
Showing 1 changed file with 38 additions and 24 deletions.
62 changes: 38 additions & 24 deletions Q3DC/Q3DC.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import vtk, qt, ctk, slicer
from slicer.ScriptedLoadableModule import *
from slicer.util import NodeModify
import csv, os
import csv
from collections import defaultdict
from pathlib import Path
import json
import time
import logging
import math
import os
import time

import ctk
import numpy as np

# needed for kd-trees
import qt
import scipy.spatial
import vtk

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

import slicer
from slicer.ScriptedLoadableModule import *
from slicer.util import NodeModify


#
# CalculateDisplacement
Expand Down Expand Up @@ -100,7 +103,6 @@ def setup(self):
self.anatomical_legend_space = self.ui.landmarkModifLayout
self.anatomical_radio_buttons_layout = qt.QHBoxLayout()
self.anatomical_legend_space.addLayout(self.anatomical_radio_buttons_layout)
self.init_anatomical_radio_buttons()

self.anatomical_legend = None
self.init_anatomical_legend()
Expand All @@ -113,7 +115,7 @@ def setup(self):
)
self.anatomical_legend_view.connect('selectionChanged()', self.on_legend_row_selected)

self.anatomical_radio_buttons[0].toggle()
self.init_anatomical_radio_buttons()

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

Expand Down Expand Up @@ -370,6 +372,7 @@ def init_anatomical_radio_buttons(self):
lambda state, _radio_button=radio_button:
self.on_anatomical_radio_button_toggled(state, _radio_button)
)
self.anatomical_radio_buttons[0].toggle()

def on_anatomical_radio_button_toggled(self, state, radio_button):
if state:
Expand Down Expand Up @@ -434,14 +437,12 @@ def on_select_legend_file_clicked(self):
if legend_filename == '':
# User canceled the file selection dialog.
return
try:
self.suggested_landmarks = self.logic.load_suggested_landmarks(
legend_filename)
except KeyError:
slicer.util.delayDisplay('The selected file does not have the right column names.')
suggested_landmarks = self.logic.load_suggested_landmarks(
legend_filename)
if suggested_landmarks is None:
return
self.suggested_landmarks = suggested_landmarks
self.init_anatomical_radio_buttons()
self.anatomical_radio_buttons[0].toggle()

def onModelChanged(self):
print("-------Model Changed--------")
Expand Down Expand Up @@ -674,14 +675,27 @@ def __init__(self, interface):
@staticmethod
def load_suggested_landmarks(filepath):
suggested_landmarks = defaultdict(list)
with open(filepath, newline='') as suggestions_file:
reader = csv.DictReader(suggestions_file)
for row in reader:
region = row['Region'].title()
landmark = row['Landmark']
name = row['Name']
suggested_landmarks[region].append((landmark, name))
return suggested_landmarks
try:
with open(filepath, newline='') as suggestions_file:
reader = csv.DictReader(suggestions_file)
for row in reader:
region = row['Region'].title()
landmark = row['Landmark']
name = row['Name']
suggested_landmarks[region].append((landmark, name))
return suggested_landmarks
except OSError as e:
slicer.util.delayDisplay('Unable to find/open file.')
logging.info('User attempted to open a landmark legend file.\n' + repr(e))
return None
except csv.Error as e:
slicer.util.delayDisplay('The selected file is not formatted properly.')
logging.info('User attempted to open a landmark legend file.\n' + repr(e))
return None
except KeyError as e:
slicer.util.delayDisplay('The selected file does not have the right column names.')
logging.info('User attempted to open a landmark legend file.\n' + repr(e))
return None

def initComboboxdict(self):
self.comboboxdict[self.interface.landmarkComboBoxA] = None
Expand Down

0 comments on commit f30e8a7

Please sign in to comment.