Skip to content

Commit

Permalink
Clean comments and RUFF format
Browse files Browse the repository at this point in the history
  • Loading branch information
Tolonen Luka committed Aug 22, 2024
1 parent a16885e commit f634dbd
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 65 deletions.
8 changes: 4 additions & 4 deletions invesalius/data/coordinates.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,18 +253,18 @@ def PolarisCoord(tracker_connection: "TrackerConnection", tracker_id: int, ref_m
angles_ref = np.degrees(tr.euler_from_quaternion(ref[2:6], axes="rzyx"))
trans_ref = np.array(ref[6:9]).astype(float)
coord2 = np.hstack((trans_ref, angles_ref))

obj_coords = []
for i in range(trck.objs.size()):
obj = trck.objs[i].decode(const.FS_ENCODE).split(",")
obj = trck.objs[i].decode(const.FS_ENCODE).split(",")
angles_obj = np.degrees(tr.euler_from_quaternion(obj[2:6], axes="rzyx"))
trans_obj = np.array(obj[6:9]).astype(float)
obj_coords.append(np.hstack((trans_obj, angles_obj)))

coord = np.vstack([coord1, coord2, *obj_coords])
marker_visibilities = [trck.probeID, trck.refID] + list(trck.objIDs)

return coord, marker_visibilities
return coord, marker_visibilities


def CameraCoord(tracker_connection: "TrackerConnection", tracker_id: int, ref_mode):
Expand Down
2 changes: 1 addition & 1 deletion invesalius/data/tracker_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,7 +428,7 @@ def Connect(self):
com_port = self.configuration["com_port"].encode(const.FS_ENCODE)
probe_dir = self.configuration["probe_dir"].encode(const.FS_ENCODE)
ref_dir = self.configuration["ref_dir"].encode(const.FS_ENCODE)
obj_dirs = pypolaris.StringVector() # SWIG fails to convert python list to vector<string>, so we directly create StringVector
obj_dirs = pypolaris.StringVector() # SWIG fails to convert python list to vector<string>, so we directly create StringVector
for obj_dir in self.configuration["obj_dirs"]:
obj_dirs.append(obj_dir.encode(const.FS_ENCODE))

Expand Down
7 changes: 4 additions & 3 deletions invesalius/data/viewer_volume.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,6 @@ def __init__(self, parent):
self.positions_above_threshold = None
self.cell_id_indexes_above_threshold = None

# LUKATODO: delete this?
Publisher.sendMessage("Press target mode button", pressed=False)

def UpdateCanvas(self):
Expand Down Expand Up @@ -616,7 +615,9 @@ def OnSensors(self, marker_visibilities):
colour2 = green_color
else:
colour2 = red_color
if any(coil_ids): #LUKATODO: add subscript to show how many coils are visible (green when all visible, orange when some not)
if any(
coil_ids
): # LUKATODO: add subscript to show how many coils are visible (green when all visible, orange when some not)
colour3 = green_color
else:
colour3 = red_color
Expand Down Expand Up @@ -2173,7 +2174,7 @@ def GetEnorm(self, enorm_data, plot_vector):
if session.GetConfig("debug_efield"):
self.e_field_norms = enorm_data[3][self.Id_list, 0]
self.e_field_col1 = enorm_data[3][self.Id_list, 1]
self.e_field_col2 = enorm_data[3][self.Id_list, 1] #LUKATODO: is this a typo?
self.e_field_col2 = enorm_data[3][self.Id_list, 1] # LUKATODO: is this a typo?
self.e_field_col3 = enorm_data[3][self.Id_list, 3]
self.Idmax = np.array(self.Id_list[np.array(self.e_field_norms).argmax()])
max = np.array(self.e_field_norms).argmax()
Expand Down
10 changes: 5 additions & 5 deletions invesalius/data/visualization/coil_visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,13 @@ def SetCoilAtTarget(self, state):

# Set the color of both target coil (representing the target) and the coil center (representing the actual coil).
self.target_coil_actor.GetProperty().SetDiffuseColor(target_coil_color)
# self.coil_center_actor.GetProperty().SetDiffuseColor(target_coil_color) # LUKATODO
# self.coil_center_actor.GetProperty().SetDiffuseColor(target_coil_color) # LUKATODO: use main_coil from navigation?

def OnNavigationStatus(self, nav_status, vis_status):
self.is_navigating = nav_status

# Called when 'show coil' button is pressed in the user interface or in code.
# LUKATODO: Right-click 'show coil' button to open combobox to choose specific coil to show/hide?
# LUKATODO: Right-click 'show coil' button to open combobox for choosing specific coil to show/hide
def ShowCoil(self, state, coil_name=None):
if coil_name is None: # Show/hide all coils
for coil in self.coils.values():
Expand All @@ -127,7 +127,7 @@ def ShowCoil(self, state, coil_name=None):
# LUKATODO: target?
if self.target_coil_actor is not None:
self.target_coil_actor.SetVisibility(state)
self.vector_field_assembly.SetVisibility(state)
# self.vector_field_assembly.SetVisibility(state) # LUKATODO: Keep this hidden for now

if not self.is_navigating:
self.interactor.Render()
Expand Down Expand Up @@ -177,7 +177,7 @@ def AddTargetCoil(self, m_target):
self.target_coil_actor.GetProperty().SetSpecular(0.5)
self.target_coil_actor.GetProperty().SetSpecularPower(10)
self.target_coil_actor.GetProperty().SetOpacity(0.3)
self.target_coil_actor.SetVisibility(True) #LUKATODO
self.target_coil_actor.SetVisibility(True)
self.target_coil_actor.SetUserMatrix(m_target)

self.renderer.AddActor(self.target_coil_actor)
Expand Down Expand Up @@ -288,4 +288,4 @@ def UpdateCoilPoses(self, m_imgs, coords):
self.coils[name]["center_actor"].SetUserMatrix(m_img_vtk)

# LUKATODO
self.vector_field_assembly.SetUserMatrix(m_img_vtk)
# self.vector_field_assembly.SetUserMatrix(m_img_vtk)
4 changes: 3 additions & 1 deletion invesalius/gui/default_viewers.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,9 @@ def __bind_events(self):

def MaximizeViewerVolume(self, enabled=True):
if enabled:
self.aui_manager.MaximizePane(self.aui_manager.GetAllPanes()[-1]) # Viewer volume is the last pane
self.aui_manager.MaximizePane(
self.aui_manager.GetAllPanes()[-1]
) # Viewer volume is the last pane
Publisher.sendMessage("Show raycasting widget")
else:
self.aui_manager.RestoreMaximizedPane()
Expand Down
4 changes: 3 additions & 1 deletion invesalius/gui/dialogs.py
Original file line number Diff line number Diff line change
Expand Up @@ -6867,7 +6867,9 @@ def _init_gui(self) -> None:
self.dir_objs.append(dir_obj)

row_obj = wx.BoxSizer(wx.VERTICAL)
row_obj.Add(wx.StaticText(self, wx.ID_ANY, f"Coil {i+1} ROM file:"), 0, wx.TOP | wx.RIGHT, 5)
row_obj.Add(
wx.StaticText(self, wx.ID_ANY, f"Coil {i+1} ROM file:"), 0, wx.TOP | wx.RIGHT, 5
)
row_obj.Add(dir_obj, 0, wx.ALL | wx.CENTER | wx.EXPAND)
row_objs.append(row_obj)

Expand Down
71 changes: 34 additions & 37 deletions invesalius/gui/preferences.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ def __init__(self, parent, navigation, tracker, pedal_connector, neuronavigation
self.pedal_connector = pedal_connector
self.neuronavigation_api = neuronavigation_api
self.navigation = navigation
self.coil_registrations = {}
self.coil_registrations = {}
self.__bind_events()

# Sizer for displaying instructions
Expand All @@ -494,7 +494,7 @@ def __init__(self, parent, navigation, tracker, pedal_connector, neuronavigation
),
)
self.inner_sel_sizer = inner_sel_sizer = wx.FlexGridSizer(10, 1, 1)

# Coils are selected by toggling coil-buttons
self.coil_btns = {}
self.no_coils_lbl = None
Expand Down Expand Up @@ -533,14 +533,13 @@ def __init__(self, parent, navigation, tracker, pedal_connector, neuronavigation
coil_sizer.Add(inner_coil_sizer, 0, wx.ALL | wx.EXPAND, 10)

# Angle/Dist thresholds, timestamp interval
self.angle_threshold = (
self.session.GetConfig("angle_threshold", const.DEFAULT_ANGLE_THRESHOLD)
self.angle_threshold = self.session.GetConfig(
"angle_threshold", const.DEFAULT_ANGLE_THRESHOLD
)
self.distance_threshold = (
self.session.GetConfig("distance_threshold", const.DEFAULT_DISTANCE_THRESHOLD)
self.distance_threshold = self.session.GetConfig(
"distance_threshold", const.DEFAULT_DISTANCE_THRESHOLD
)


# Change angles threshold
text_angles = wx.StaticText(self, -1, _("Angle threshold (degrees):"))
spin_size_angles = wx.SpinCtrlDouble(self, -1, "", size=wx.Size(50, 23))
Expand Down Expand Up @@ -601,11 +600,9 @@ def __init__(self, parent, navigation, tracker, pedal_connector, neuronavigation
]
)
self.SetSizerAndFit(main_sizer)

self.LoadConfig()
self.Layout()


self.LoadConfig()
self.Layout()

def AddCoilButton(self, coil_name):
if self.no_coils_lbl is not None:
Expand Down Expand Up @@ -641,13 +638,13 @@ def OnSetCoilCount(self, n_coils):

def LoadConfig(self):
state = self.session.GetConfig("navigation", {})
self.coil_registrations = self.session.GetConfig("coil_registrations", {})
# Add a button for each coil
self.coil_registrations = self.session.GetConfig("coil_registrations", {})
# Add a button for each coil
for coil_name in self.coil_registrations:
self.AddCoilButton(coil_name)

# Press the buttons for coils that were selected in config file
selected_coils = state.get("selected_coils", [])
selected_coils = state.get("selected_coils", [])
for coil_name in selected_coils:
self.coil_btns[coil_name][0].SetValue(True)

Expand All @@ -658,13 +655,12 @@ def LoadConfig(self):
self.sel_sizer.GetStaticBox().SetLabel(
f"TMS coil selection ({n_coils_selected} out of {n_coils})"
)

if n_coils_selected == n_coils:
# Allow only n_coils buttons to be pressed, so disable unpressed buttons
for btn, *junk in self.coil_btns.values():
btn.Enable(btn.GetValue())


def OnSelectCoil(self, event=None, name=None, select=False):
coil_registration = None
navigation = self.navigation
Expand Down Expand Up @@ -706,7 +702,7 @@ def OnSelectCoil(self, event=None, name=None, select=False):

# Select/Unselect coil
Publisher.sendMessage("Select coil", coil_name=name, coil_registration=coil_registration)

n_coils_selected = len(navigation.coil_registrations)
n_coils = navigation.n_coils
# Update label telling how many coils to select
Expand All @@ -724,9 +720,7 @@ def OnSelectCoil(self, event=None, name=None, select=False):
for btn, *junk in self.coil_btns.values():
btn.Enable(btn.GetValue())
else: # Enable all buttons
Publisher.sendMessage(
"Coil selection done", done=False
)
Publisher.sendMessage("Coil selection done", done=False)
for btn, *junk in self.coil_btns.values():
btn.Enable(True)

Expand Down Expand Up @@ -777,15 +771,17 @@ def DeleteCoil(event, name):
delete_coil = menu.Append(wx.ID_ANY, "Delete coil")
set_obj_id = menu.Append(wx.ID_ANY, "Set coil index")
save_coil = menu.Append(wx.ID_ANY, "Save coil to OBR file")

self.Bind(wx.EVT_MENU, (lambda event, name=name: DeleteCoil(event, name)), delete_coil)
self.Bind(wx.EVT_MENU, (lambda event, name=name: SetObjID(event, name)), set_obj_id)
self.Bind(wx.EVT_MENU, (lambda event, name=name: self.OnSaveCoilToOBR(event, name)), save_coil)
self.Bind(
wx.EVT_MENU, (lambda event, name=name: self.OnSaveCoilToOBR(event, name)), save_coil
)
self.PopupMenu(menu, event.GetPosition())
menu.Destroy()

def OnCreateNewCoil(self, event=None):
# Create a coil registration and save it by the given name
# Create a coil registration and save it by the given name
# Also used to edit coil registrations by overwriting to the same name
if self.tracker.IsTrackerInitialized():
dialog = dlg.ObjectCalibrationDialog(
Expand All @@ -801,17 +797,20 @@ def OnCreateNewCoil(self, event=None):
# Warn that we are overwriting an old registration
dialog = wx.TextEntryDialog(
None,
_("A registration with this name already exists. Enter a new name or overwrite an old coil registration"),
_(
"A registration with this name already exists. Enter a new name or overwrite an old coil registration"
),
_("Warning: Coil Name Conflict"),
value=coil_name,
)
if dialog.ShowModal() == wx.ID_OK:
coil_name = dialog.GetValue().strip() # Update coil_name with user input
coil_name = (
dialog.GetValue().strip()
) # Update coil_name with user input
else:
return # Cancel the operation if the user closes the dialog or cancels
dialog.Destroy()


# LUKATODO: update coil mesh elsewhere
# self.neuronavigation_api.update_coil_mesh(polydata)

Expand All @@ -832,11 +831,8 @@ def OnCreateNewCoil(self, event=None):
if coil_btn.GetValue():
coil_btn.SetValue(False)
self.OnSelectCoil(name=coil_name, select=False)

self.Layout()

# LUKATODO: delete the below with dependencies
Publisher.sendMessage("Press target mode button", pressed=False)
self.Layout()

except wx.PyAssertionError: # TODO FIX: win64
pass
Expand Down Expand Up @@ -870,7 +866,9 @@ def OnLoadCoilFromOBR(self, event=None):
# Warn that we are overwriting an old registration
dialog = wx.TextEntryDialog(
None,
_("A registration with this name already exists. Enter a new name or overwrite an old coil registration"),
_(
"A registration with this name already exists. Enter a new name or overwrite an old coil registration"
),
_("Warning: Coil Name Conflict"),
value=coil_name,
)
Expand All @@ -888,7 +886,7 @@ def OnLoadCoilFromOBR(self, event=None):
coil_path = os.path.join(inv_paths.OBJ_DIR, "magstim_fig8_coil.stl")

# LUKATODO: what is neuronavigation_api.update_coil_mesh ?
#if polydata:
# if polydata:
# self.neuronavigation_api.update_coil_mesh(polydata)

if np.isfinite(obj_fiducials).all() and np.isfinite(obj_orients).all():
Expand Down Expand Up @@ -925,7 +923,7 @@ def OnLoadCoilFromOBR(self, event=None):

def OnSaveCoilToOBR(self, evt, coil_name):
coil_registration = self.coil_registrations[coil_name]

filename = dlg.ShowLoadSaveDialog(
message=_("Save object registration as..."),
wildcard=_("Registration files (*.obr)|*.obr"),
Expand All @@ -935,7 +933,7 @@ def OnSaveCoilToOBR(self, evt, coil_name):
)
if filename:
hdr = (
coil_name
coil_name
+ "\t"
+ utils.decode(coil_registration["path"], const.FS_ENCODE)
+ "\t"
Expand All @@ -951,7 +949,6 @@ def OnSaveCoilToOBR(self, evt, coil_name):
np.savetxt(filename, data, fmt="%.4f", delimiter="\t", newline="\n", header=hdr)
wx.MessageBox(_("Object file successfully saved"), _("Save"))


def OnSelectAngleThreshold(self, evt, ctrl):
self.angle_threshold = ctrl.GetValue()
Publisher.sendMessage("Update angle threshold", angle=self.angle_threshold)
Expand Down Expand Up @@ -1152,7 +1149,7 @@ def OnChooseNoOfCoils(self, evt, ctrl):
else:
self.n_coils = 1

if self.n_coils != old_n_coils: # if n_coils was changed reset connection
if self.n_coils != old_n_coils: # if n_coils was changed reset connection
tracker_id = self.tracker.tracker_id
self.tracker.DisconnectTracker()
self.tracker.SetTracker(tracker_id, n_coils=self.n_coils)
Expand Down
Loading

0 comments on commit f634dbd

Please sign in to comment.