Skip to content

Commit

Permalink
Merge branch 'main' into jcfr-updat-readme-install-from-extensions-ma…
Browse files Browse the repository at this point in the history
…nager
  • Loading branch information
wyli authored Aug 16, 2023
2 parents 85f735e + 6975cb1 commit 85d57ff
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 37 deletions.
134 changes: 98 additions & 36 deletions MONAIViz/MONAIViz.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ def setup(self):
self.ui.removeTransformButton.connect("clicked(bool)", self.onRemoveTransform)
self.ui.moveUpButton.connect("clicked(bool)", self.onMoveUpTransform)
self.ui.moveDownButton.connect("clicked(bool)", self.onMoveDownTransform)
self.ui.loadTransformButton.connect("clicked(bool)", self.onLoadTransform)
self.ui.saveTransformButton.connect("clicked(bool)", self.onSaveTransform)
self.ui.modulesComboBox.connect("currentIndexChanged(int)", self.onSelectModule)
self.ui.transformTable.connect("cellClicked(int, int)", self.onSelectTransform)
self.ui.transformTable.connect("cellDoubleClicked(int, int)", self.onEditTransform)
Expand All @@ -169,12 +171,15 @@ def setup(self):
self.ui.runTransformButton.setIcon(self.icon("icons8-red-circle-48.png"))
self.ui.previewTransformButton.setIcon(self.icon("icons8-preview-48.png"))
self.ui.clearTransformButton.setIcon(self.icon("icons8-delete-document-48.png"))
self.ui.loadTransformButton.setIcon(self.icon("icons8-load-48.png"))
self.ui.saveTransformButton.setIcon(self.icon("icons8-save-48.png"))

headers = ["Status", "Target", "Args"]
headers = ["Active", "Status", "Target", "Args"]
self.ui.transformTable.setColumnCount(len(headers))
self.ui.transformTable.setHorizontalHeaderLabels(headers)
self.ui.transformTable.setColumnWidth(0, 60)
self.ui.transformTable.setColumnWidth(1, 200)
self.ui.transformTable.setColumnWidth(0, 40)
self.ui.transformTable.setColumnWidth(1, 60)
self.ui.transformTable.setColumnWidth(2, 160)
self.ui.transformTable.setEditTriggers(qt.QTableWidget.NoEditTriggers)
self.ui.transformTable.setSelectionBehavior(qt.QTableView.SelectRows)

Expand Down Expand Up @@ -332,12 +337,14 @@ def onImportBundle(self):

print(f"Importing Transform: {name} => {args}")
# table.setCellWidget(pos, 0, EditButtonsWidget())
box = qt.QCheckBox()
table.setCellWidget(box)
item = qt.QTableWidgetItem()
item.setIcon(self.icon("icons8-yellow-circle-48.png"))
table.setItem(pos, 0, item)
table.setItem(pos, 1, item)

table.setItem(pos, 1, qt.QTableWidgetItem(name))
table.setItem(pos, 2, qt.QTableWidgetItem(ClassUtils.args_to_expression(args)))
table.setItem(pos, 2, qt.QTableWidgetItem(name))
table.setItem(pos, 3, qt.QTableWidgetItem(ClassUtils.args_to_expression(args)))

def onSelectModule(self):
module = self.ui.modulesComboBox.currentText
Expand All @@ -348,6 +355,12 @@ def onSelectModule(self):
self.ui.transformsComboBox.clear()
self.ui.transformsComboBox.addItems(filtered)

def onBoxClicked(self, clicked, current_row):
next_idx = current_row
next_exp = self.get_exp(next_idx)
self.ctx.set_next(next_idx, next_exp)
self.ctx.reset()

def onSelectTransform(self, row, col):
selected = True if row >= 0 and self.ui.transformTable.rowCount else False
self.ui.editTransformButton.setEnabled(selected)
Expand All @@ -356,15 +369,16 @@ def onSelectTransform(self, row, col):
self.ui.moveDownButton.setEnabled(selected and row < self.ui.transformTable.rowCount - 1)
self.ui.runTransformButton.setEnabled(selected)
self.ui.clearTransformButton.setEnabled(self.ctx.valid())
self.ui.saveTransformButton.setEnabled(selected)

def onEditTransform(self, row=-1, col=-1):
print(f"Selected Transform for Edit: {row}")
row = self.ui.transformTable.currentRow() if row < 0 else row
if row < 0:
return

name = str(self.ui.transformTable.item(row, 1).text())
exp = str(self.ui.transformTable.item(row, 2).text())
name = str(self.ui.transformTable.item(row, 2).text())
exp = str(self.ui.transformTable.item(row, 3).text())

doc_html = os.path.join(self.tmpdir, "transforms.html")
doc_url = f"https://docs.monai.io/en/{self.ui.monaiVersionComboBox.currentText}/transforms.html"
Expand Down Expand Up @@ -419,7 +433,7 @@ def onEditTransform(self, row=-1, col=-1):
if exp != new_exp:
if row < self.ctx.next_idx or row == self.ui.transformTable.rowCount - 1:
self.onClearTransform()
self.ui.transformTable.item(row, 2).setText(new_exp)
self.ui.transformTable.item(row, 3).setText(new_exp)
print("Updated for new args...")

def onAddTransform(self):
Expand All @@ -440,18 +454,32 @@ def onAddTransform(self):

self.addTransform(-1, None, t, v)

def addTransform(self, pos, m, t, v):
def addTransform(self, pos, m, t, v, active=True):
table = self.ui.transformTable
pos = pos if pos >= 0 else table.rowCount if table.currentRow() < 0 else table.currentRow() + 1

table.insertRow(pos)
# table.setCellWidget(pos, 0, EditButtonsWidget())

box = qt.QCheckBox()
box.setChecked(active)
box.setProperty("row", pos)
widget = qt.QWidget()
box.connect("clicked(bool)", lambda checked: self.onBoxClicked(checked, box.row))
layout = qt.QHBoxLayout(widget)
layout.addWidget(box)
layout.setAlignment(qt.Qt.AlignCenter)
layout.setContentsMargins(0, 0, 0, 0)
widget.setLayout(layout)

table.setCellWidget(pos, 0, widget)

item = qt.QTableWidgetItem()
item.setIcon(self.icon("icons8-yellow-circle-48.png"))
table.setItem(pos, 0, item)
table.setItem(pos, 1, item)

table.setItem(pos, 1, qt.QTableWidgetItem(f"{m}.{t}" if m else t))
table.setItem(pos, 2, qt.QTableWidgetItem(v if v else ""))
table.setItem(pos, 2, qt.QTableWidgetItem(f"{m}.{t}" if m else t))
table.setItem(pos, 3, qt.QTableWidgetItem(v if v else ""))

table.selectRow(pos)
self.onSelectTransform(pos, 0)
Expand All @@ -468,20 +496,22 @@ def onMoveUpTransform(self):
if row < 0:
return

t = str(self.ui.transformTable.item(row, 1).text())
v = str(self.ui.transformTable.item(row, 2).text())
t = str(self.ui.transformTable.item(row, 2).text())
v = str(self.ui.transformTable.item(row, 3).text())
active = self.ui.transformTable.cellWidget(row, 0).findChild("QCheckBox").isChecked()
self.onRemoveTransform()
self.addTransform(row - 1, None, t, v)
self.addTransform(row - 1, None, t, v, active)

def onMoveDownTransform(self):
row = self.ui.transformTable.currentRow()
if row < 0:
return

t = str(self.ui.transformTable.item(row, 1).text())
v = str(self.ui.transformTable.item(row, 2).text())
t = str(self.ui.transformTable.item(row, 2).text())
v = str(self.ui.transformTable.item(row, 3).text())
active = self.ui.transformTable.cellWidget(row, 0).findChild("QCheckBox").isChecked()
self.onRemoveTransform()
self.addTransform(row + 1, None, t, v)
self.addTransform(row + 1, None, t, v, active)

def prepare_dict(self):
image = self.ui.imagePathLineEdit.currentPath
Expand All @@ -497,8 +527,8 @@ def prepare_dict(self):
return d

def get_exp(self, row):
name = str(self.ui.transformTable.item(row, 1).text())
args = str(self.ui.transformTable.item(row, 2).text())
name = str(self.ui.transformTable.item(row, 2).text())
args = str(self.ui.transformTable.item(row, 3).text())
return f"monai.transforms.{name}({args})"

def onRunTransform(self):
Expand Down Expand Up @@ -528,20 +558,24 @@ def onRunTransform(self):

if self.ctx.last_exp != current_exp:
for row in range(self.ctx.next_idx, current_row + 1):
exp = self.get_exp(row)
print("")
print("====================================================================")
print(f"Run:: {exp}")
print("====================================================================")

t = eval(exp)
if isinstance(d, list):
d = [t(dx) for dx in d] # Batched Transforms
if self.ui.transformTable.cellWidget(row, 0).findChild("QCheckBox").isChecked():
exp = self.get_exp(row)
print("")
print("====================================================================")
print(f"Run:: {exp}")
print("====================================================================")

t = eval(exp)
if isinstance(d, list):
d = [t(dx) for dx in d] # Batched Transforms
else:
d = t(d)

self.ctx.set_d(d, exp, key=image_key)
self.ui.transformTable.item(row, 1).setIcon(self.icon("icons8-green-circle-48.png"))
else:
d = t(d)

self.ctx.set_d(d, exp, key=image_key)
self.ui.transformTable.item(row, 0).setIcon(self.icon("icons8-green-circle-48.png"))
self.ui.transformTable.item(row, 1).setIcon(self.icon("icons8-yellow-circle-48.png"))
continue

next_idx = current_row
next_exp = self.get_exp(next_idx)
Expand All @@ -551,7 +585,7 @@ def onRunTransform(self):

self.ui.transformTable.selectRow(next_idx)
for row in range(next_idx, self.ui.transformTable.rowCount):
self.ui.transformTable.item(row, 0).setIcon(self.icon("icons8-yellow-circle-48.png"))
self.ui.transformTable.item(row, 1).setIcon(self.icon("icons8-yellow-circle-48.png"))

v = self.ctx.get_tensor(key=image_key)
volumeNode = slicer.util.addVolumeFromArray(v)
Expand Down Expand Up @@ -583,9 +617,37 @@ def onRunTransform(self):
def onClearTransform(self):
self.ctx.reset()
for row in range(0, self.ui.transformTable.rowCount):
self.ui.transformTable.item(row, 0).setIcon(self.icon("icons8-yellow-circle-48.png"))
self.ui.transformTable.item(row, 1).setIcon(self.icon("icons8-yellow-circle-48.png"))
self.ui.clearTransformButton.setEnabled(self.ctx.valid())

def onLoadTransform(self):
fname = qt.QFileDialog().getOpenFileName(None, "Select json file to import", "", "(*.json)")
with open(fname) as transformFile:
transforms = json.load(transformFile)

for idx, transform in transforms.items():
t = transform["name"]
v = ""

if t[-1] == "d": # this is a dictionary transform
# now exclude some transforms whose name happens to end with d
if t not in ["AffineGrid", "Decollated", "RandAffineGrid", "RandDeformGrid"]:
v = transform["args"]

self.addTransform(int(idx), None, t, v)

def onSaveTransform(self):
fname = qt.QFileDialog().getSaveFileName(None, "Save file", "", "json (*.json)")
rows = self.ui.transformTable.rowCount
table = {}
for row in range(rows):
name = str(self.ui.transformTable.item(row, 2).text())
args = str(self.ui.transformTable.item(row, 3).text())
table[row] = {"name": name, "args": args}

with open(fname, "w") as output:
json.dump(table, output)

def onShowDictionary(self):
dlg = TransformDictDialog(self.ctx.get_d(None, d=self.prepare_dict()), self.resourcePath)
dlg.exec()
Expand Down
Binary file added MONAIViz/Resources/Icons/icons8-load-48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added MONAIViz/Resources/Icons/icons8-save-48.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
40 changes: 39 additions & 1 deletion MONAIViz/Resources/UI/MONAIViz.ui
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,44 @@
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="loadTransformButton">
<property name="enabled">
<bool>true</bool>
</property>
<property name="maximumSize">
<size>
<width>48</width>
<height>16777215</height>
</size>
</property>
<property name="toolTip">
<string>Load transforms</string>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="saveTransformButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="maximumSize">
<size>
<width>48</width>
<height>16777215</height>
</size>
</property>
<property name="toolTip">
<string>Save transforms as json file</string>
</property>
<property name="text">
<string/>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
Expand Down Expand Up @@ -303,7 +341,7 @@
<bool>true</bool>
</property>
<property name="columnCount">
<number>3</number>
<number>4</number>
</property>
<attribute name="horizontalHeaderStretchLastSection">
<bool>true</bool>
Expand Down

0 comments on commit 85d57ff

Please sign in to comment.