Skip to content

vrml visualization with image volumes #1069

Description

@vanhoesens

I am having trouble with visualizing simulations that include image volumes. I can run test009 just fine, but I want to be able to visualize similar simulations so I created the following (which is mostly just the test009 script but with vrml visualization).

if __name__ == "__main__":
    sim = gate.Simulation()
    sim.output_dir = paths[2,1]
    # sim.g4_verbose = True
    # sim.g4_verbose_level = 3
    sim.physics_manager.physics_list_name = 'QBBC'
    sim.progress_bar = True
    sim.volume_manager.add_material_database("GateMaterials.db")
    sim.visu = True
    sim.visu_type = 'vrml'
    # sim.store_json_archive = True
    # sim.json_archive_filename = "visu_test.json"
    
    world = sim.world
    world.size = [1*m, 1*m, 1*m]
    # add a simple fake volume to test hierarchy
    # translation and rotation like in the Gate macro
    fake = sim.add_volume("Box", "fake")
    fake.size = [40*cm, 40*cm, 40*cm]
    fake.material = "G4_AIR"
    fake.color = [1, 0, 1, 1]
    fake.rotation = Rotation.from_euler("x", 20, degrees=True).as_matrix()
    
    # image
    patient = sim.add_volume("Image", "patient")
    patient.image = paths[1,1] + "\\patient-4mm.mhd"
    patient.mother = "fake"
    patient.material = "G4_AIR"  # material used by default
    patient.voxel_materials = [
        [-2000, -900, "G4_AIR"],
        [-900, -100, "Lung"],
        [-100, 0, "G4_ADIPOSE_TISSUE_ICRP"],
        [0, 300, "G4_TISSUE_SOFT_ICRP"],
        [300, 800, "G4_B-100_BONE"],
        [800, 6000, "G4_BONE_COMPACT_ICRU"],
    ]
    # or alternatively, from a file (like in Gate)
    vm = read_voxel_materials(paths[1,1] + "\\patient-HU2mat-v1.txt")
    vm[0][0] = -2000
    # Note: the voxel_materials are turned into a structured array when setting the user info
    # Therefore, we store the previous one, assign the new one, and only then compare them!
    assert patient.voxel_materials == vm
    patient.voxel_materials = vm
    # write the image of labels (None by default)
    patient.dump_label_image = paths[1,1] + "\\test009_label.mhd"
    
    # default source for tests
    source = sim.add_source("GenericSource", "mysource")
    source.energy.mono = 130*MeV
    source.particle = "proton"
    source.position.type = "sphere"
    source.position.radius = 10*mm
    source.position.translation = [0, 0, -14*cm]
    source.activity = 100*Bq
    source.direction.type = "momentum"
    source.direction.momentum = [0, 0, 1]
    # cuts
    patient.set_production_cut(
        particle_name="electron",
        value=3*mm,
    )
    
    # add dose actor
    dose_actor = sim.add_actor("DoseActor", "dose_actor")
    dose_actor.attached_to = "patient"
    dose_actor.edep_uncertainty.active = True
    dose_actor.size = [99, 99, 99]
    dose_actor.spacing = [2*mm, 2*mm, 2*mm]
    dose_actor.output_coordinate_system = "attached_to_image"
    dose_actor.translation = [2*mm, 3*mm, -2*mm]
    dose_actor.hit_type = "random"
    
    # add stat actor
    stats_actor = sim.add_actor("SimulationStatisticsActor", "Stats")
    stats_actor.track_types_flag = True
    
    # print info
    print(sim.volume_manager.dump_volumes())
    
    # start simulation
    sim.run(start_new_process=True)
    

This is the error when this script is run:

Simulation: START (in a new process) (around 100 events expected)
■                                                [00m:00s<00m:00s] ☠️ Fatal in C:\Users\svanhoesen\AppData\Local\anaconda3\envs\gate_env\Lib\site-packages\opengate\processing.py line 60
☠️ The queue is empty. The spawned process probably died or crashed.
C:\Users\svanhoesen\AppData\Local\anaconda3\envs\gate_env\Lib\site-packages\opengate_core
Unexpected exception formatting exception. Falling back to standard exception
Traceback (most recent call last):
  File "C:\Users\svanhoesen\AppData\Local\anaconda3\envs\gate_env\Lib\site-packages\opengate\processing.py", line 58, in dispatch_to_subprocess
    return q.get(block=False)
           ~~~~~^^^^^^^^^^^^^
  File "<string>", line 2, in get
  File "C:\Users\svanhoesen\AppData\Local\anaconda3\envs\gate_env\Lib\multiprocessing\managers.py", line 847, in _callmethod
    raise convert_to_error(kind, result)
_queue.Empty

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\svanhoesen\AppData\Local\anaconda3\envs\gate_env\Lib\site-packages\spyder_kernels\customize\code_runner.py", line 640, in _exec_code
    exec_encapsulate_locals(
    ~~~~~~~~~~~~~~~~~~~~~~~^
        ast_code, ns_globals, ns_locals, exec_fun, filename
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "C:\Users\svanhoesen\AppData\Local\anaconda3\envs\gate_env\Lib\site-packages\spyder_kernels\customize\utils.py", line 209, in exec_encapsulate_locals
    exec_fun(compile(code_ast, filename, "exec"), globals, None)
    ~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "c:\users\svanhoesen\.spyder-py3\sub_pixel\volume_visu_test.py", line 105, in <module>
    sim.run(start_new_process=True)
    ~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\svanhoesen\AppData\Local\anaconda3\envs\gate_env\Lib\site-packages\opengate\managers.py", line 1905, in run
    output = dispatch_to_subprocess(self._run_simulation_engine, True)
  File "C:\Users\svanhoesen\AppData\Local\anaconda3\envs\gate_env\Lib\site-packages\opengate\processing.py", line 60, in dispatch_to_subprocess
    fatal("The queue is empty. The spawned process probably died or crashed.")
    ~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\svanhoesen\AppData\Local\anaconda3\envs\gate_env\Lib\site-packages\opengate\exception.py", line 39, in fatal
    raise Exception(s)
Exception: The queue is empty. The spawned process probably died or crashed.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\svanhoesen\AppData\Local\anaconda3\envs\gate_env\Lib\site-packages\IPython\core\interactiveshell.py", line 2223, in showtraceback
    stb = self.InteractiveTB.structured_traceback(
        etype, value, tb, tb_offset=tb_offset
    )
  File "C:\Users\svanhoesen\AppData\Local\anaconda3\envs\gate_env\Lib\site-packages\IPython\core\ultratb.py", line 1228, in structured_traceback
    return FormattedTB.structured_traceback(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        self, etype, evalue, etb, tb_offset, context
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "C:\Users\svanhoesen\AppData\Local\anaconda3\envs\gate_env\Lib\site-packages\IPython\core\ultratb.py", line 1099, in structured_traceback
    return VerboseTB.structured_traceback(
           ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        self, etype, evalue, etb, tb_offset, context
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "C:\Users\svanhoesen\AppData\Local\anaconda3\envs\gate_env\Lib\site-packages\IPython\core\ultratb.py", line 907, in structured_traceback
    formatted_exceptions: list[list[str]] = self.format_exception_as_a_whole(
                                            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^
        etype, evalue, etb, context, tb_offset
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
    )
    ^
  File "C:\Users\svanhoesen\AppData\Local\anaconda3\envs\gate_env\Lib\site-packages\IPython\core\ultratb.py", line 791, in format_exception_as_a_whole
    frames.append(self.format_record(record))
                  ~~~~~~~~~~~~~~~~~~^^^^^^^^
  File "C:\Users\svanhoesen\AppData\Local\anaconda3\envs\gate_env\Lib\site-packages\IPython\core\ultratb.py", line 667, in format_record
    frame_info.lines,
    ^^^^^^^^^^^^^^^^
  File "C:\Users\svanhoesen\AppData\Local\anaconda3\envs\gate_env\Lib\site-packages\IPython\core\tbtools.py", line 356, in lines
    return self._sd.lines  # type: ignore[misc]
           ^^^^^^^^^^^^^^
  File "C:\Users\svanhoesen\AppData\Local\anaconda3\envs\gate_env\Lib\site-packages\stack_data\utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
                                               ~~~~~~~~~^^^^^
  File "C:\Users\svanhoesen\AppData\Local\anaconda3\envs\gate_env\Lib\site-packages\stack_data\core.py", line 734, in lines
    pieces = self.included_pieces
             ^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\svanhoesen\AppData\Local\anaconda3\envs\gate_env\Lib\site-packages\stack_data\utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
                                               ~~~~~~~~~^^^^^
  File "C:\Users\svanhoesen\AppData\Local\anaconda3\envs\gate_env\Lib\site-packages\stack_data\core.py", line 677, in included_pieces
    scope_pieces = self.scope_pieces
                   ^^^^^^^^^^^^^^^^^
  File "C:\Users\svanhoesen\AppData\Local\anaconda3\envs\gate_env\Lib\site-packages\stack_data\utils.py", line 145, in cached_property_wrapper
    value = obj.__dict__[self.func.__name__] = self.func(obj)
                                               ~~~~~~~~~^^^^^
  File "C:\Users\svanhoesen\AppData\Local\anaconda3\envs\gate_env\Lib\site-packages\stack_data\core.py", line 614, in scope_pieces
    scope_start, scope_end = self.source.line_range(self.scope)
                             ~~~~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^
  File "C:\Users\svanhoesen\AppData\Local\anaconda3\envs\gate_env\Lib\site-packages\stack_data\core.py", line 178, in line_range
    return line_range(self.asttext(), node)
                      ^^^^^^^^^^^^
AttributeError: 'Source' object has no attribute 'asttext'

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions