Mask Filter from Python, How to exclude the background (for example)

The API of the MaskFilter algorithm does not allow un-selecting a given entity. The only way to select "everything except one" is actually to first unselect everything and then enable all the entities (except one) again.

Below is an example that does that for an anatomical model (or any other group of entities).

It uses a short helper function that returns the list of all model objects (aka entities) that are within a certain model folder (aka Entity Group). This list of objects is then passed to the MaskFilter.

import s4l_v1.analysis as analysis
import s4l_v1.document as document
import s4l_v1.model as model
import itertools

def all_entities_within_group(entity_group):
    '''return a list of all model entities within a given group, including all subdirectories'''
    if isinstance(entity_group, model.EntityGroup):
        return list(itertools.chain.from_iterable(
        all_entities_within_group(e) for e in entity_group.Entities))
        return [entity_group]

vip_group = model.AllEntities()['Duke']
entities_from_vip_model = all_entities_within_group(vip_group)

# Creating the analysis pipeline
# Adding a new SimulationExtractor
simulation = document.AllSimulations["EM"]
simulation_extractor = simulation.Results()

# Adding a new EmSensorExtractor
em_sensor_extractor = simulation_extractor["Overall Field"]

# Adding a new FieldMaskingFilter
inputs = [em_sensor_extractor.Outputs["EM E(x,y,z,f0)"]]
field_masking_filter = analysis.core.FieldMaskingFilter(inputs=inputs)