3.5 Python Modules
Table of Contents
Importing modules
In GATE simulations you will often need functions and classes from existing Python files and external libraries. Python uses modules to organize code. A module is simply a .py file that defines variables, functions, classes, or other objects that you can reuse.
To import a standard library module, you typically write at the top of your script:
import mathYou can then access objects inside this module using the module name followed by a dot, for example:
x = math.sqrt(4.0)
pi_value = math.piSometimes you only need a few specific names from a module. In that case, you can import them directly:
from math import sqrt, pi
x = sqrt(4.0)
pi_value = piFor scientific computing and GATE-related analysis, you will often see imports like:
import numpy as np
import matplotlib.pyplot as plt
The as keyword creates a short alias. This is common practice for large libraries so that later code is more concise. For example:
a = np.array([1, 2, 3])
plt.plot(a)
Python searches for modules in the current directory and in locations listed in the sys.path variable. For modules that are installed system-wide or in your environment, you can usually import them without additional configuration. For your own files, make sure the .py file is in the same directory as your main script or in a directory that Python can find.
In GATE simulations you will frequently import:
import opengate as og
import numpy as np
The opengate module provides the simulation tools, while numpy and other libraries are used later for handling results and analysis. All these import statements should appear near the top of your script, before you start defining your geometry, sources, and actors.
If you get an ImportError or ModuleNotFoundError, it usually means the module is not installed in the current environment or Python cannot find its location. In that case, check that you activated the correct Python environment and that the package has been installed there.
Always place your import statements at the top of your script and avoid circular imports where two modules import each other.
Creating your own modules
As your GATE simulations grow, putting everything into a single Python file becomes difficult to maintain. Python allows you to split your code into multiple files and reuse common parts by turning them into your own modules.
A simple module is just a .py file. Suppose you want to reuse a function that creates a water box phantom. You can create a file named my_geometry.py:
# my_geometry.py
def add_water_box(sim, size, position):
box = sim.add_volume("Box", "water_box")
box.size = size
box.material = "G4_WATER"
box.translation = position
return box
This file defines one function, add_water_box. In your main simulation script, saved for example as main_sim.py in the same directory, you can import and use this function:
# main_sim.py
import opengate as og
from my_geometry import add_water_box
sim = og.Simulation()
world = sim.world
world.size = [1.0, 1.0, 1.0]
water = add_water_box(sim, size=[0.2, 0.2, 0.2], position=[0, 0, 0])
sim.run()
Python sees my_geometry.py as the module my_geometry. The file name (without .py) is the module name. The from my_geometry import add_water_box line makes that function available directly in your script.
You can group related utilities into several modules. For instance, you might have:
geometry_utils.pyfor detector and phantom construction functions.source_utils.pyfor functions that create sources with typical energy or activity settings.actor_utils.pyfor functions that attach standard actors to your simulation.
In that case your imports might look like:
from geometry_utils import create_pet_ring
from source_utils import add_f18_source
from actor_utils import add_dose_actorYou can also import the entire module if you prefer to keep names clearly grouped:
import geometry_utils as gu
ring = gu.create_pet_ring(sim)
When you create modules, keep these points in mind. Place your module files in the same folder as your main script, especially when you begin. Use clear, descriptive names for both modules and functions, such as create_gamma_camera rather than do_stuff. Avoid putting code that runs immediately at the top level of a module. Instead, put reusable definitions there. If you need to test something inside a module, wrap the test code in:
if __name__ == "__main__":
# test code hereThis block runs only when you execute the module directly and not when you import it from another script.
For larger projects, you can organize modules into directories that become packages by adding an __init__.py file. This is a more advanced pattern that will be useful once your GATE work involves many components, but the basic idea remains the same: split logic into separate files and import what you need.
To create your own module, save reusable functions and classes in a .py file, place it in Python’s search path, and import it by its file name without the .py extension.
By structuring your GATE simulations with modules, you can reuse geometry definitions, sources, and analysis routines across multiple projects and keep each script focused and easy to read.
Views: 13
KAHIBARO