Skip to content
  • Search
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse

ZMT zurich med tech

  1. Home
  2. Sim4Life
  3. Python API
  4. Masking entities in python

Masking entities in python

Scheduled Pinned Locked Moved Python API
6 Posts 4 Posters 871 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Frodi
    wrote on last edited by
    #1

    Hello Sim4Life community,

    I have problems changing the mask setting using the python API. I want to export data where the background is masked away. If I do it interactively in Sim4Life and choose 'to Python' the masking part of the code (setting entities to True/False) seems to be missing. I think I have to use the 'SetMaterial' function in the Analysis.Core.FieldMaskingFilter. I just can't figure out what input type it wants for the materials.

    Any help would be wonderful. This is my code:

    import numpy
    import s4l_v1.analysis as analysis
    import s4l_v1.document as document
    import s4l_v1.model as model
    import s4l_v1.units as units
    from s4l_v1 import ReleaseVersion
    from s4l_v1 import Unit

    try:
    # Define the version to use for default values
    ReleaseVersion.set_active(ReleaseVersion.version6_2)

    size = numpy.linspace(1,34,34)
    for k in size:
    	# Creating the analysis pipeline
    	# Adding a new SimulationExtractor
    	simulation = document.AllSimulations["Simulation  " + str(k)]
    	simulation_extractor = simulation.Results()
    
    	# Adding a new EmSensorExtractor
    	em_sensor_extractor = simulation_extractor["Overall Field"]
    	em_sensor_extractor.FrequencySettings.ExtractedFrequency = u"All"
    	em_sensor_extractor.SurfaceCurrent.SurfaceResolution = 0.001, units.Meters
    	document.AllAlgorithms.Add(em_sensor_extractor)
    
    	# Adding a new FieldMaskingFilter
    	inputs = [em_sensor_extractor.Outputs["EM E(x,y,z,f0)"]]
    	field_masking_filter = analysis.core.FieldMaskingFilter(inputs=inputs)
    	field_masking_filter.UpdateAttributes()
    	document.AllAlgorithms.Add(field_masking_filter)
    
    	# Adding a new MatlabExporter
    	inputs = [field_masking_filter.Outputs["EM E(x,y,z,f0)"]]
    	matlab_exporter = analysis.exporters.MatlabExporter(inputs=inputs)
    	matlab_exporter.FileName = u"[some path]"+str(k)+".mat"
    	matlab_exporter.UpdateAttributes()
    	matlab_exporter.Update()
    	document.AllAlgorithms.Add(matlab_exporter)
    

    except Exception as exc:
    import traceback
    traceback.print_exc(exc)
    # Reset active version to default
    ReleaseVersion.reset()
    raise(exc)

    Best,
    Frodi

    S 1 Reply Last reply
    0
    • F Frodi

      Hello Sim4Life community,

      I have problems changing the mask setting using the python API. I want to export data where the background is masked away. If I do it interactively in Sim4Life and choose 'to Python' the masking part of the code (setting entities to True/False) seems to be missing. I think I have to use the 'SetMaterial' function in the Analysis.Core.FieldMaskingFilter. I just can't figure out what input type it wants for the materials.

      Any help would be wonderful. This is my code:

      import numpy
      import s4l_v1.analysis as analysis
      import s4l_v1.document as document
      import s4l_v1.model as model
      import s4l_v1.units as units
      from s4l_v1 import ReleaseVersion
      from s4l_v1 import Unit

      try:
      # Define the version to use for default values
      ReleaseVersion.set_active(ReleaseVersion.version6_2)

      size = numpy.linspace(1,34,34)
      for k in size:
      	# Creating the analysis pipeline
      	# Adding a new SimulationExtractor
      	simulation = document.AllSimulations["Simulation  " + str(k)]
      	simulation_extractor = simulation.Results()
      
      	# Adding a new EmSensorExtractor
      	em_sensor_extractor = simulation_extractor["Overall Field"]
      	em_sensor_extractor.FrequencySettings.ExtractedFrequency = u"All"
      	em_sensor_extractor.SurfaceCurrent.SurfaceResolution = 0.001, units.Meters
      	document.AllAlgorithms.Add(em_sensor_extractor)
      
      	# Adding a new FieldMaskingFilter
      	inputs = [em_sensor_extractor.Outputs["EM E(x,y,z,f0)"]]
      	field_masking_filter = analysis.core.FieldMaskingFilter(inputs=inputs)
      	field_masking_filter.UpdateAttributes()
      	document.AllAlgorithms.Add(field_masking_filter)
      
      	# Adding a new MatlabExporter
      	inputs = [field_masking_filter.Outputs["EM E(x,y,z,f0)"]]
      	matlab_exporter = analysis.exporters.MatlabExporter(inputs=inputs)
      	matlab_exporter.FileName = u"[some path]"+str(k)+".mat"
      	matlab_exporter.UpdateAttributes()
      	matlab_exporter.Update()
      	document.AllAlgorithms.Add(matlab_exporter)
      

      except Exception as exc:
      import traceback
      traceback.print_exc(exc)
      # Reset active version to default
      ReleaseVersion.reset()
      raise(exc)

      Best,
      Frodi

      S Offline
      S Offline
      sayimgokyar
      wrote on last edited by
      #2

      @frodi
      I have a similar problem too.
      You may see my workaround in the post entitled as: ‘Exporting Simulation Results for Specific Tissues’.

      If you have a limited number of tissues, you may use my workaround. However, if the number of tissues are changing, then we need a different solution.

      1 Reply Last reply
      0
      • brynB Offline
        brynB Offline
        bryn
        ZMT
        wrote on last edited by bryn
        #3

        Here is an example how you can change the mask programmatically, for materials that where already voxeled in the simulation:

            inputs = [em_sensor_extractor.Outputs["EM E(x,y,z,f0)"]]
            field_masking_filter = analysis.core.FieldMaskingFilter(inputs=inputs)
        
            # - clear mask
            field_masking_filter.SetAllMaterials(False)
        
            # - add ventricles to mask
            materials = field_masking_filter.VoxeledMaterials()()
            for m in materials:
                if "Ventricle_lumen_left" in m.Name or "Ventricle_lumen_right" in m.Name:
                    field_masking_filter.SetMaterial(m.ID, True)
            field_masking_filter.Update()
        

        And here is an example of how you can use entities from the modeler:

            inputs = [em_sensor_extractor.Outputs["EM E(x,y,z,f0)"]]
            field_masking_filter = analysis.core.FieldMaskingFilter(inputs=inputs) 
        
            # somehow select certain entities
            entities = s4l.model.EntityList()
            selection = [e for e in entities if "Cerebellum" in e.Name]
        
            masking_filter.SetEntities(selection) # expects List[Entity]
            masking_filter.Update(0)
        
        S 1 Reply Last reply
        0
        • brynB bryn

          Here is an example how you can change the mask programmatically, for materials that where already voxeled in the simulation:

              inputs = [em_sensor_extractor.Outputs["EM E(x,y,z,f0)"]]
              field_masking_filter = analysis.core.FieldMaskingFilter(inputs=inputs)
          
              # - clear mask
              field_masking_filter.SetAllMaterials(False)
          
              # - add ventricles to mask
              materials = field_masking_filter.VoxeledMaterials()()
              for m in materials:
                  if "Ventricle_lumen_left" in m.Name or "Ventricle_lumen_right" in m.Name:
                      field_masking_filter.SetMaterial(m.ID, True)
              field_masking_filter.Update()
          

          And here is an example of how you can use entities from the modeler:

              inputs = [em_sensor_extractor.Outputs["EM E(x,y,z,f0)"]]
              field_masking_filter = analysis.core.FieldMaskingFilter(inputs=inputs) 
          
              # somehow select certain entities
              entities = s4l.model.EntityList()
              selection = [e for e in entities if "Cerebellum" in e.Name]
          
              masking_filter.SetEntities(selection) # expects List[Entity]
              masking_filter.Update(0)
          
          S Offline
          S Offline
          Spuky
          wrote on last edited by
          #4

          @bryn

          thank you for posting the code here, however could you please specify what should be the J_port in

          masking_filter.SetInputConnection(0, J_port)
          

          line of your code?

          Further in console, I got warning that this feature is DEPRECATED and it is recommended to use Inputs[key].Connect() however when I try to use

          masking_filter.Inputs[inputs].Connect()
          

          I receive error that there is no attribute Connect()

          Spuky

          1 Reply Last reply
          0
          • brynB Offline
            brynB Offline
            bryn
            ZMT
            wrote on last edited by
            #5

            @Spuky sorry, I guess the legacy code was confusing:

                masking_filter = s4l.analysis.core.FieldMaskingFilter()
                masking_filter.SetInputConnection(0, J_port)
            

            I edited the original answer to use the current API. The J_port was the output port for the J(x,y,z,f0) field.

            S 1 Reply Last reply
            0
            • brynB bryn

              @Spuky sorry, I guess the legacy code was confusing:

                  masking_filter = s4l.analysis.core.FieldMaskingFilter()
                  masking_filter.SetInputConnection(0, J_port)
              

              I edited the original answer to use the current API. The J_port was the output port for the J(x,y,z,f0) field.

              S Offline
              S Offline
              Spuky
              wrote on last edited by Spuky
              #6
              This post is deleted!
              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Search