-
Notifications
You must be signed in to change notification settings - Fork 5
Added Python debugging tutorial #109
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
94 changes: 94 additions & 0 deletions
94
...slab.github.io/content/tutorials/basicmechanisms/macromodules/pythondebugger.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| --- | ||
| title: "Example 5: Debugging Python in MATE" | ||
| date: 2025-01-09 | ||
| status: "OK" | ||
| draft: false | ||
| weight: 455 | ||
| tags: ["Advanced", "Tutorial", "Python", "Debugging", "MATE"] | ||
| menu: | ||
| main: | ||
| identifier: "pythondebugger" | ||
| title: "Debugging Python in MATE" | ||
| weight: 455 | ||
| parent: "basicmechanisms" | ||
| --- | ||
| # Example 5: Debugging Python files in MATE | ||
| ## Introduction | ||
| 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. | ||
|
|
||
| ## Prepare your network | ||
| 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. | ||
|
|
||
|  | ||
|
|
||
| ## Open Python script in MATE | ||
| 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. | ||
|
|
||
|  | ||
|
|
||
| {{<alert class="info" caption="Information">}} | ||
| You can not only debug your own files, but also Python scripts of our existing MeVisLab modules. | ||
| {{</alert>}} | ||
|
|
||
| The user interface of MATE provides some relevant sections to help debug your code. | ||
|
|
||
| ### Outline section | ||
| The *Outline* section provides a list of all functions defined in your currently opened script. | ||
|
|
||
| ### Project Workspace section | ||
| 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. | ||
|
|
||
| ### Debug Output section | ||
| 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. | ||
|
|
||
| ## Debug a Python script | ||
| 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. | ||
|
|
||
| ### Debugging panel | ||
| The *Debugging* panel allows you to step through your code. | ||
|
|
||
|  | ||
|
|
||
| ### Stack Frames panel | ||
| The *Stack Frames* panel shows your current stack trace while debugging. | ||
|
|
||
|  | ||
|
|
||
| ### Variables/Watches/Evaluate Expression panel | ||
| 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. | ||
|
|
||
|  | ||
|
|
||
| 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" >}}. | ||
|
|
||
| Go back to MeVisLab and right click {{< mousebutton "right" >}} on any DICOM tag in the `DicomTagBrowser` module. Select {{< menuitem "Copy Tag Name" >}}. | ||
|
|
||
|  | ||
|
|
||
| MATE opens automatically and you can see a yellow arrow in your red dot indicating that execution of the code stopped at this line. | ||
|
|
||
|  | ||
|
|
||
| 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. | ||
|
|
||
sahehb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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. | ||
|
|
||
sahehb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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. | ||
|
|
||
|  | ||
|
|
||
| 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. | ||
|
|
||
|  | ||
|
|
||
sahehb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ## Conditions for Breakpoints | ||
| 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. | ||
|
|
||
|  | ||
|
|
||
| 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. | ||
|
|
||
| ## Summary | ||
| * MATE allows debugging of your own Python files and Python files integrated into MeVisLab. | ||
| * Values of variables can be watched. | ||
| * It is possible to define conditions for breakpoints, so that the execution is only stopped if the condition is met. | ||
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
BIN
+28.1 KB
mevislab.github.io/static/images/tutorials/basicmechanics/Debug7a.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
BIN
+5.26 KB
mevislab.github.io/static/images/tutorials/basicmechanics/Debug7b.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.