3.2. Conditional Statements
Table of Contents
`if`
Conditional statements let your Python code make decisions. In GATE simulations, you often use them to choose geometry options, enable or disable actors, or select parameters based on a configuration.
The simplest conditional is if. It has the form:
if condition:
# code that runs only if the condition is True
do_something()
The condition is an expression that evaluates to a Boolean value, either True or False. If it is True, the indented block below if runs. If it is False, that block is skipped.
Every line in the if block must be indented by the same number of spaces (commonly 4). Incorrect indentation will cause a Python error or change which lines belong to the condition.
For example, imagine you want to enable a PET geometry only when a flag is set:
use_pet = True
if use_pet:
add_pet_geometry(sim)
If use_pet is True, the PET geometry is added. If it is False, the function add_pet_geometry is not called.
You can also compare values in conditions:
events = 1_000_000
if events > 500_000:
print("This is a large simulation.")Common comparison operators are:
| Operator | Meaning | Example |
|---|---|---|
== | equal to | if mode == "PET": |
!= | not equal to | if particle != "e-": |
< | less than | if energy < 1 * MeV: |
> | greater than | if n_threads > 1: |
<= | less than or equal to | if angle <= 180: |
>= | greater than or equal to | if events >= 1000: |
You can combine conditions using logical operators:
if modality == "PET" and events > 100_000:
print("High-statistics PET simulation.")
Here and means both conditions must be True for the block to run. Other logical operators include or and not.
`elif`
Sometimes you need more than one possible condition. The keyword elif means “else if” and lets you check another condition if the previous ones were not True.
The structure is:
if condition1:
# runs if condition1 is True
...
elif condition2:
# runs if condition1 is False and condition2 is True
...
elif condition3:
# runs if all previous conditions are False and condition3 is True
...
Only one block in this chain can run. As soon as Python finds a True condition, it runs that block and ignores the rest.
For example, you might select different simulation modes:
mode = "PET"
if mode == "PET":
configure_pet(sim)
elif mode == "SPECT":
configure_spect(sim)
elif mode == "CT":
configure_ct(sim)
If mode is "PET", only configure_pet(sim) is called. If you later set mode to "SPECT", the SPECT configuration is used instead.
Another example related to statistics:
uncertainty = 0.12 # 12 percent
if uncertainty < 0.05:
print("High precision simulation.")
elif uncertainty < 0.15:
print("Medium precision simulation.")
elif uncertainty < 0.30:
print("Low precision simulation.")
Here the first condition that is True determines the message.
The order of elif conditions matters. Python checks them from top to bottom and stops at the first True condition.
`else`
The keyword else provides a default block that runs when none of the previous if or elif conditions are True. It does not have its own condition.
The full structure is:
if condition1:
...
elif condition2:
...
else:
# runs only if all above conditions are False
...
You usually use else to handle “all other cases” explicitly.
For example, selecting physics for different applications:
application = "proton"
if application == "PET":
configure_pet_physics(sim)
elif application == "SPECT":
configure_spect_physics(sim)
else:
configure_default_physics(sim)
If application is neither "PET" nor "SPECT", the else block configures a default physics list.
Another typical pattern is input validation:
threads = 0
if threads > 0:
n_threads = threads
else:
n_threads = 1 # safe defaultIf an invalid number of threads is given, you fall back to a safe default of 1.
You can also combine everything in one example that controls an option for visualization:
visualization = "none"
if visualization == "interactive":
enable_interactive_visualization(sim)
elif visualization == "batch":
save_geometry_screenshot(sim)
else:
print("Visualization disabled.")
Here the else covers any value that is not "interactive" or "batch", and clearly indicates that no visualization will be used.
Views: 15
KAHIBARO