Skip to content

Commit b6db062

Browse files
Heckmannmrbean-bremen
authored andcommitted
Additional comments by Jan integrated
1 parent d16eea6 commit b6db062

4 files changed

Lines changed: 47 additions & 15 deletions

File tree

mevislab.github.io/content/tutorials/basicmechanisms/macromodules/pythondebugger.md

Lines changed: 47 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,32 +14,36 @@ menu:
1414
---
1515
# Example 5: Debugging Python files in MATE
1616
## 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.
17+
MeVisLab provides the powerful integrated text editor MATE. By default, MATE is used to create/edit files like Python scripts. In this tutorial, we want to show you how to debug Python scripts in MeVisLab.
1818

1919
## 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.
20+
We are using a very simple network of pre-defined modules, but you can also debug your self-written Python scripts. Add a `LocalImage` module to your workspace and connect it to a `DicomTagBrowser` module. The `DicomTagBrowser` module shows a table containing the DICOM tags of your currently opened file.
2121

2222
![Example Network](/images/tutorials/basicmechanics/Debug1.png "Example Network")
2323

2424
## Open Python script in MATE
2525
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.
2626

27+
{{<alert class="info" caption="Attention">}}
28+
MATE only opens Python files if default configuration in *MeVisLab/Preferences* is not changed for *Supportive Programs*.
29+
{{</alert>}}
30+
2731
![MATE](/images/tutorials/basicmechanics/Debug2.png "MATE")
2832

2933
{{<alert class="info" caption="Information">}}
30-
You can not only debug your own files, but also Python scripts of our existing MeVisLab modules.
34+
You can not only debug your own files, but also Python scripts of pre-defined MeVisLab modules.
3135
{{</alert>}}
3236

33-
The user interface of MATE provides some relevant sections to help debug your code.
37+
The user interface of MATE provides some relevant views for debugging.
3438

35-
### Outline section
36-
The *Outline* section provides a list of all functions defined in your currently opened script.
39+
### Outline view
40+
The *Outline* view shows a list of all functions defined in your currently opened script.
3741

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.
42+
### Project Workspace view
43+
The *Project Workspace* view 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.
4044

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.
45+
### Debug Output view
46+
The *Debug Output* view shows the messages you also see in MeVisLab. Additional views are available as soon as we start debugging our file.
4347

4448
## Debug a Python script
4549
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.
@@ -59,21 +63,43 @@ Another panel *Variables/Watches/Evaluate Expression* appears, where you can see
5963

6064
![Variables/Watches/Evaluate Expression](/images/tutorials/basicmechanics/Debug5.png "Variables/Watches/Evaluate Expression")
6165

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" >}}.
66+
Scroll to line 180 and left click {{< mousebutton "left" >}} on the line number.
67+
68+
{{< highlight >}}
69+
```Python
70+
179 def copyCurrentTagName():
71+
> 180 item = ctx.control("dicomTree").currentItem()
72+
181 if item:
73+
182 MLAB.copyToPasteboard(item.text(1))
74+
```
75+
{{</highlight>}}
76+
77+
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" >}}.
6378

6479
Go back to MeVisLab and right click {{< mousebutton "right" >}} on any DICOM tag in the `DicomTagBrowser` module. Select {{< menuitem "Copy Tag Name" >}}.
6580

6681
![Copy Tag Name](/images/tutorials/basicmechanics/Debug6.png "Copy Tag Name")
6782

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.
83+
MATE opens automatically and you can see an additional yellow arrow indicating the line about to be executed next.
6984

7085
![MATE Debugger](/images/tutorials/basicmechanics/Debug7.png "MATE Debugger")
7186

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.
87+
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 watched variables.
88+
89+
We want to see the name of the DICOM tag we selected in the `DicomTagBrowser` module. You can access the values the following way:
90+
91+
{{< highlight >}}
92+
```Python
93+
item.text(0) # shows the tag ID (first column)
94+
item.text(1) # shows the tag Name
95+
item.text(2) # shows the tag VR
96+
item.text(3) # shows the tag Value
97+
```
98+
{{</highlight>}}
7399

74100
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.
75101

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.
102+
Use the *Debugging* panel (fifth button *Step to next line*) or press {{< keyboard "F10" >}}. The 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.
77103

78104
![Watches panel](/images/tutorials/basicmechanics/Debug7b.png "Watches panel")
79105

@@ -88,7 +114,13 @@ You can also define conditions for your breakpoints. Remove breakpoint in line 1
88114

89115
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.
90116

117+
## Evaluate Expression
118+
The *Evaluate Expression* tab allows you to modify variables during execution. In our example you can set the result **item.text(1)** to something like **item.setText(1, "Hello")**. If you now step to the next line via {{< keyboard "F10" >}}, your watched value shows *"Hello"* instead of *"SOPClassUID"*.
119+
120+
{{< imagegallery 2 "/images/tutorials/basicmechanics" "Debug9" "Debug9a" >}}
121+
91122
## Summary
92-
* MATE allows debugging of your own Python files and Python files integrated into MeVisLab.
123+
* MATE allows debugging of any Python files including files pre-defined in MeVisLab.
93124
* Values of variables can be watched.
94125
* It is possible to define conditions for breakpoints, so that the execution is only stopped if the condition is met.
126+
* It is possible to change values of variables while program execution is stopped via *Evaluate Expression* panel.
3.18 KB
Loading
4.23 KB
Loading
7.45 KB
Loading

0 commit comments

Comments
 (0)