Masking entities in python

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

@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.

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:

    # somehow select certain entities
    entities = s4l.model.EntityList()
    selection = [e for e in entities if "Cerebellum" in e.Name]

    masking_filter = s4l.analysis.core.FieldMaskingFilter()
    masking_filter.SetInputConnection(0, J_port)
    masking_filter.SetEntities(selection) # expects List[Entity]
    masking_filter.Update(0)