Skip to content

Commit d16eea6

Browse files
authored
Added Python debugging tutorial (#109)
* Added Python debugging tutorial * Changes after review * Update after review
1 parent 648e0a0 commit d16eea6

11 files changed

Lines changed: 94 additions & 0 deletions

File tree

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
---
2+
title: "Example 5: Debugging Python in MATE"
3+
date: 2025-01-09
4+
status: "OK"
5+
draft: false
6+
weight: 455
7+
tags: ["Advanced", "Tutorial", "Python", "Debugging", "MATE"]
8+
menu:
9+
main:
10+
identifier: "pythondebugger"
11+
title: "Debugging Python in MATE"
12+
weight: 455
13+
parent: "basicmechanisms"
14+
---
15+
# Example 5: Debugging Python files in MATE
16+
## Introduction
17+
MeVisLab provides the powerful integrated text editor MATE. By default, MATE is used to develop MeVisLab text files like Python scripts. In this tutorial, we want to show you how to debug Python files in MeVisLab.
18+
19+
## Prepare your network
20+
We are using a very simple network of existing modules. Add a `LocalImage` module to your workspace and connect it to a `DicomTagBrowser` module. The `DicomTagBrowser` module shows DICOM tags of your currently opened file in a table.
21+
22+
![Example Network](/images/tutorials/basicmechanics/Debug1.png "Example Network")
23+
24+
## Open Python script in MATE
25+
To debug our module, we need to open the Python file. Right-click {{< mousebutton "right" >}} the module `DicomTagBrowser` and select {{< menuitem "Related Files (3)" "DicomTagBrowser.py" >}}. The file is opened in MATE.
26+
27+
![MATE](/images/tutorials/basicmechanics/Debug2.png "MATE")
28+
29+
{{<alert class="info" caption="Information">}}
30+
You can not only debug your own files, but also Python scripts of our existing MeVisLab modules.
31+
{{</alert>}}
32+
33+
The user interface of MATE provides some relevant sections to help debug your code.
34+
35+
### Outline section
36+
The *Outline* section provides a list of all functions defined in your currently opened script.
37+
38+
### Project Workspace section
39+
The *Project Workspace* section shows the content of the directories for all of your opened files. In this case, we only opened one file and only see the content of the directory for the `DicomTagBrowser` module.
40+
41+
### Debug Output section
42+
The *Debug Output* section shows the messages you also see in MeVisLab. Later additional sections are available as soon as we start debugging our file.
43+
44+
## Debug a Python script
45+
First we need to enable debugging. In the MATE main menu, select {{< menuitem "Debug" "Enable Debugging" >}}. You can see some new panels appearing in MATE.
46+
47+
### Debugging panel
48+
The *Debugging* panel allows you to step through your code.
49+
50+
![Debugging Panel](/images/tutorials/basicmechanics/Debug3.png "Debugging Panel")
51+
52+
### Stack Frames panel
53+
The *Stack Frames* panel shows your current stack trace while debugging.
54+
55+
![Stack Frames](/images/tutorials/basicmechanics/Debug4.png "Stack Frames")
56+
57+
### Variables/Watches/Evaluate Expression panel
58+
Another panel *Variables/Watches/Evaluate Expression* appears, where you can see all current local and global variables. Add your own variables to watch their current value and evaluate your own expressions.
59+
60+
![Variables/Watches/Evaluate Expression](/images/tutorials/basicmechanics/Debug5.png "Variables/Watches/Evaluate Expression")
61+
62+
Scroll to line 180 and left click {{< mousebutton "left" >}} on the line number. You can see a red dot marking a break point for debugging. Whenever this line of code is executed, execution will stop here and you can evaluate your variables. This line will be reached whenever you right-click {{< mousebutton "right" >}} on the list in the `DicomTagBrowser` module and select {{< menuitem "Copy Tag Name" >}}.
63+
64+
Go back to MeVisLab and right click {{< mousebutton "right" >}} on any DICOM tag in the `DicomTagBrowser` module. Select {{< menuitem "Copy Tag Name" >}}.
65+
66+
![Copy Tag Name](/images/tutorials/basicmechanics/Debug6.png "Copy Tag Name")
67+
68+
MATE opens automatically and you can see a yellow arrow in your red dot indicating that execution of the code stopped at this line.
69+
70+
![MATE Debugger](/images/tutorials/basicmechanics/Debug7.png "MATE Debugger")
71+
72+
You can now use the controls of the *Debugging* panels to step through your code or just continue execution of your code. Whenever your execution is stopped, you can use the *Stack Frames* and the *Variables/Watches/Evaluate Expression* panel to see the current value of all or just selected variables.
73+
74+
Select *Watches* panel and enter **item.text(1)**. Again copy any tag name in MeVisLab `DicomTagBrowser` module. You will see that MATE shows an error. The reason is that the execution stops before executing the current line of code. Your Python code in line 180 defines the variable *item*, and therefore it is not yet defined at this moment.
75+
76+
Use the *Debugging* panel or press {{< keyboard "F10" >}}. Your debugger jumps to the next line (181) and the variable *item* is defined. You can see the value of the *Tag Name* you just copied. You can add any variables you are interested in the same way.
77+
78+
![Watches panel](/images/tutorials/basicmechanics/Debug7b.png "Watches panel")
79+
80+
The *Variables* panel now shows all currently available local and global variables including their value(s). The *Stack Trace* panel shows that the *copyCurrentTagName* function has been called after the *DicomTagBrowser.MenuItem.command* from the \*.script file of the `DicomTagBrowser` module.
81+
82+
![Variables/Watches panel](/images/tutorials/basicmechanics/Debug7a.png "Variables/Watches panel")
83+
84+
## Conditions for Breakpoints
85+
You can also define conditions for your breakpoints. Remove breakpoint in line 180 and set a new one in line 181. In case you only want to stop the execution of your script if a specific condition is met, right click {{< mousebutton "right" >}} on your breakpoint and select {{< menuitem "Set Condition for Breakpoint" >}}. A dialog opens where you can define your condition. Enter **item.text(1) == 'SOPClassUID'** as condition.
86+
87+
![Conditions for Breakpoints](/images/tutorials/basicmechanics/Debug8.png "Conditions for Breakpoints")
88+
89+
Now, the code execution is only stopped if you copy the tag name *SOPClassUID*. In case another line is copied, the execution does not stop and just continues.
90+
91+
## Summary
92+
* MATE allows debugging of your own Python files and Python files integrated into MeVisLab.
93+
* Values of variables can be watched.
94+
* It is possible to define conditions for breakpoints, so that the execution is only stopped if the condition is met.
58.1 KB
Loading
167 KB
Loading
3.05 KB
Loading
2.71 KB
Loading
4.36 KB
Loading
31.2 KB
Loading
13.2 KB
Loading
28.1 KB
Loading
5.26 KB
Loading

0 commit comments

Comments
 (0)