Skip to content

Operations

FMU

Unpack and repack facilities for FMU archives.

Extracts an FMU (.fmu zip archive) into a temporary directory so that operations can be applied to its modelDescription.xml descriptor. After manipulation, the FMU can be repacked into a new archive.

The temporary directory is removed by close(), at the end of a with block, or at the latest when the object is garbage-collected or the interpreter exits:

with FMU("module.fmu") as fmu:
    fmu.apply_operation(OperationStripTopLevel())
    fmu.repack("module-stripped.fmu")

Attributes:

Name Type Description
FMI2_TYPES tuple[str, ...]

FMI-2 scalar variable type names.

FMI3_TYPES tuple[str, ...]

FMI-3 variable type names.

fmu_filename str

Path to the original .fmu file.

tmp_directory str

Path to the temporary extraction directory.

fmi_version int | None

Detected FMI version (2 or 3), set during parsing.

descriptor_filename str

Path to the extracted modelDescription.xml.

Raises:

Type Description
FMUError

If the file does not exist or is not a valid FMU.

Source code in fmu_manipulation_toolbox/operations.py
class FMU:
    """Unpack and repack facilities for FMU archives.

    Extracts an FMU (`.fmu` zip archive) into a temporary directory so that
    operations can be applied to its `modelDescription.xml` descriptor.
    After manipulation, the FMU can be repacked into a new archive.

    The temporary directory is removed by `close()`, at the end of a `with`
    block, or at the latest when the object is garbage-collected or the
    interpreter exits:

        with FMU("module.fmu") as fmu:
            fmu.apply_operation(OperationStripTopLevel())
            fmu.repack("module-stripped.fmu")

    Attributes:
        FMI2_TYPES (tuple[str, ...]): FMI-2 scalar variable type names.
        FMI3_TYPES (tuple[str, ...]): FMI-3 variable type names.
        fmu_filename (str): Path to the original `.fmu` file.
        tmp_directory (str): Path to the temporary extraction directory.
        fmi_version (int | None): Detected FMI version (`2` or `3`), set
            during parsing.
        descriptor_filename (str): Path to the extracted `modelDescription.xml`.

    Raises:
        FMUError: If the file does not exist or is not a valid FMU.
    """

    FMI2_TYPES = ('Real', 'Integer', 'String', 'Boolean', 'Enumeration')
    FMI3_TYPES = ('Float64', 'Float32',
                  'Int8', 'UInt8', 'Int16', 'UInt16', 'Int32', 'UInt32', 'Int64', 'UInt64',
                  'String', 'Boolean', 'Enumeration', 'Clock', 'Binary')

    def __init__(self, fmu_filename):
        self.fmu_filename = fmu_filename
        self.tmp_directory = tempfile.mkdtemp()
        self.fmi_version = None
        # Unlike `__del__`, a finalizer also runs at interpreter exit, and never on a half-built object.
        self._finalizer = weakref.finalize(self, shutil.rmtree, self.tmp_directory, ignore_errors=True)
        self.descriptor_filename = os.path.join(self.tmp_directory, "modelDescription.xml")
        self._model_description: ModelDescription | None = None
        self._descriptor_signature: tuple[int, int] | None = None

        try:
            self._extract()
        except FMUError:
            self.close()
            raise

    def _extract(self):
        try:
            with zipfile.ZipFile(self.fmu_filename) as zin:
                zin.extractall(self.tmp_directory)
        except FileNotFoundError:
            raise FMUError(f"'{self.fmu_filename}' does not exist.") from None
        except (IsADirectoryError, PermissionError) as error:
            raise FMUError(f"'{self.fmu_filename}' cannot be read: {error.strerror}.") from None
        except zipfile.BadZipFile:
            raise FMUError(f"'{self.fmu_filename}' is not valid: not a ZIP archive.") from None
        if not os.path.isfile(self.descriptor_filename):
            raise FMUError(f"'{self.fmu_filename}' is not valid: modelDescription.xml not found.")

    @property
    def model_description(self) -> ModelDescription:
        """The descriptor tree, parsed once and shared by the successive operations.

        It is parsed again if `modelDescription.xml` was changed on disk by
        other means (different modification time or size).

        Raises:
            FMUError: If the descriptor cannot be read.
        """
        self._check_open()
        signature = self._signature()
        if self._model_description is None or signature != self._descriptor_signature:
            try:
                self._model_description = ModelDescription.load(self.descriptor_filename)
            except ModelDescriptionError as error:
                raise FMUError(f"'{self.fmu_filename}': {error}") from error
            self._descriptor_signature = signature
        return self._model_description

    def _signature(self) -> tuple[int, int]:
        stat = os.stat(self.descriptor_filename)
        return stat.st_mtime_ns, stat.st_size

    def _save_model_description(self):
        self._model_description.save(self.descriptor_filename)
        self._descriptor_signature = self._signature()

    def close(self):
        """Remove the temporary directory. The FMU can no longer be used afterwards."""
        self._finalizer()

    @property
    def closed(self) -> bool:
        return not self._finalizer.alive

    def _check_open(self):
        if self.closed:
            raise FMUError(f"'{self.fmu_filename}' is closed.")

    def __enter__(self):
        return self

    def __exit__(self, exc_type, exc_value, traceback):
        self.close()

    def save_descriptor(self, filename):
        """Save a copy of the current `modelDescription.xml` to a file.

        Args:
            filename (str): Destination path for the descriptor copy.
        """
        self._check_open()
        shutil.copyfile(self.descriptor_filename, filename)

    def repack(self, filename):
        """Repack the (possibly modified) FMU into a new `.fmu` archive.

        Args:
            filename (str): Output path for the repacked FMU.
        """
        self._check_open()
        with zipfile.ZipFile(filename, "w", zipfile.ZIP_DEFLATED) as zout:
            for root, _dirs, files in os.walk(self.tmp_directory):
                for file in files:
                    zout.write(os.path.join(root, file),
                               os.path.relpath(os.path.join(root, file), self.tmp_directory))
        # TODO: Add check on output file

    def apply_operation(self, operation, apply_on=None, *, types=None, variabilities=None):
        """Apply an operation to the FMU's `modelDescription.xml`.

        Invokes the operation's callbacks on the descriptor tree (parsed once
        per FMU), and writes back the descriptor unless the operation is
        `read_only`. The filters combine: a port is processed only if every
        filter set accepts it.

        Args:
            operation (OperationAbstract): The operation to apply.
            apply_on (list[str] | None): If set, only apply the operation
                to ports with a causality in this list.
            types (list[str] | None): If set, only apply the operation to ports
                of these types: the exact type (`Real`, `Float64`, `Int32`...) or,
                on an FMI-3 FMU, an FMI-2 type for its family (`Real` selects
                `Float32` and `Float64`, `Integer` every integer type).
            variabilities (list[str] | None): If set, only apply the operation to
                ports with a variability in this list. A port without `variability`
                has the default of the standard.

        Example:
            ```python
            with FMU("model.fmu") as fmu:
                fmu.apply_operation(OperationRemoveRegexp(".*"), ["parameter"], variabilities=["fixed"])
                fmu.repack("model-tunable.fmu")
            ```
        """
        self._check_open()
        manipulation = Manipulation(operation, self)
        try:
            manipulation.manipulate(self.descriptor_filename, apply_on, types=types,
                                    variabilities=variabilities)
        except BaseException:
            self._model_description = None  # the tree may be half-modified: parse the file again next time
            raise

model_description property

The descriptor tree, parsed once and shared by the successive operations.

It is parsed again if modelDescription.xml was changed on disk by other means (different modification time or size).

Raises:

Type Description
FMUError

If the descriptor cannot be read.

apply_operation(operation, apply_on=None, *, types=None, variabilities=None)

Apply an operation to the FMU's modelDescription.xml.

Invokes the operation's callbacks on the descriptor tree (parsed once per FMU), and writes back the descriptor unless the operation is read_only. The filters combine: a port is processed only if every filter set accepts it.

Parameters:

Name Type Description Default
operation OperationAbstract

The operation to apply.

required
apply_on list[str] | None

If set, only apply the operation to ports with a causality in this list.

None
types list[str] | None

If set, only apply the operation to ports of these types: the exact type (Real, Float64, Int32...) or, on an FMI-3 FMU, an FMI-2 type for its family (Real selects Float32 and Float64, Integer every integer type).

None
variabilities list[str] | None

If set, only apply the operation to ports with a variability in this list. A port without variability has the default of the standard.

None
Example
with FMU("model.fmu") as fmu:
    fmu.apply_operation(OperationRemoveRegexp(".*"), ["parameter"], variabilities=["fixed"])
    fmu.repack("model-tunable.fmu")
Source code in fmu_manipulation_toolbox/operations.py
def apply_operation(self, operation, apply_on=None, *, types=None, variabilities=None):
    """Apply an operation to the FMU's `modelDescription.xml`.

    Invokes the operation's callbacks on the descriptor tree (parsed once
    per FMU), and writes back the descriptor unless the operation is
    `read_only`. The filters combine: a port is processed only if every
    filter set accepts it.

    Args:
        operation (OperationAbstract): The operation to apply.
        apply_on (list[str] | None): If set, only apply the operation
            to ports with a causality in this list.
        types (list[str] | None): If set, only apply the operation to ports
            of these types: the exact type (`Real`, `Float64`, `Int32`...) or,
            on an FMI-3 FMU, an FMI-2 type for its family (`Real` selects
            `Float32` and `Float64`, `Integer` every integer type).
        variabilities (list[str] | None): If set, only apply the operation to
            ports with a variability in this list. A port without `variability`
            has the default of the standard.

    Example:
        ```python
        with FMU("model.fmu") as fmu:
            fmu.apply_operation(OperationRemoveRegexp(".*"), ["parameter"], variabilities=["fixed"])
            fmu.repack("model-tunable.fmu")
        ```
    """
    self._check_open()
    manipulation = Manipulation(operation, self)
    try:
        manipulation.manipulate(self.descriptor_filename, apply_on, types=types,
                                variabilities=variabilities)
    except BaseException:
        self._model_description = None  # the tree may be half-modified: parse the file again next time
        raise

close()

Remove the temporary directory. The FMU can no longer be used afterwards.

Source code in fmu_manipulation_toolbox/operations.py
def close(self):
    """Remove the temporary directory. The FMU can no longer be used afterwards."""
    self._finalizer()

repack(filename)

Repack the (possibly modified) FMU into a new .fmu archive.

Parameters:

Name Type Description Default
filename str

Output path for the repacked FMU.

required
Source code in fmu_manipulation_toolbox/operations.py
def repack(self, filename):
    """Repack the (possibly modified) FMU into a new `.fmu` archive.

    Args:
        filename (str): Output path for the repacked FMU.
    """
    self._check_open()
    with zipfile.ZipFile(filename, "w", zipfile.ZIP_DEFLATED) as zout:
        for root, _dirs, files in os.walk(self.tmp_directory):
            for file in files:
                zout.write(os.path.join(root, file),
                           os.path.relpath(os.path.join(root, file), self.tmp_directory))

save_descriptor(filename)

Save a copy of the current modelDescription.xml to a file.

Parameters:

Name Type Description Default
filename str

Destination path for the descriptor copy.

required
Source code in fmu_manipulation_toolbox/operations.py
def save_descriptor(self, filename):
    """Save a copy of the current `modelDescription.xml` to a file.

    Args:
        filename (str): Destination path for the descriptor copy.
    """
    self._check_open()
    shutil.copyfile(self.descriptor_filename, filename)

FMUError

Bases: Exception

Exception raised for FMU-related errors.

Attributes:

Name Type Description
reason str

Human-readable description of the error.

Source code in fmu_manipulation_toolbox/operations.py
class FMUError(Exception):
    """Exception raised for FMU-related errors.

    Attributes:
        reason (str): Human-readable description of the error.
    """

    def __init__(self, reason):
        self.reason = reason

    def __repr__(self):
        return self.reason

Manipulation

Applies an operation to modelDescription.xml, on its ElementTree.

Loads the descriptor as a ModelDescription, invokes the operation's callbacks in document order, modifies the tree in place and writes it back. Everything the operation does not touch is kept as is (annotations, aliases, comments, namespaces...).

When ports are removed, the references to them are updated: FMI-2 <ModelStructure> indexes and derivative attributes are renumbered, entries and dependencies of <ModelStructure> that refer to removed ports are dropped.

The operation is refused (OperationError, descriptor left unchanged) when it would produce an FMU that breaks the FMI standard:

  • every variable removed (empty <ModelVariables>, forbidden by the XSD);
  • a removed variable still referenced by a kept one (derivative, previous, clocks, <Dimension valueReference>);
  • several variables (or FMI-3 aliases) with the same name.

Attributes:

Name Type Description
operation OperationAbstract

The operation being applied.

fmu FMU

The FMU being manipulated.

model_description ModelDescription | None

The descriptor tree, once loaded.

port_translation list[int | None]

Maps original port indices (0-based) to new 1-based indices, or None for removed ports.

port_names_list list[str | None]

Original names of all encountered ports.

port_removed_vr set[str | None]

Value references of removed ports.

Source code in fmu_manipulation_toolbox/operations.py
class Manipulation:
    """Applies an operation to `modelDescription.xml`, on its ElementTree.

    Loads the descriptor as a
    [ModelDescription][fmu_manipulation_toolbox.model_description.ModelDescription],
    invokes the operation's callbacks in document order, modifies the tree in
    place and writes it back. Everything the operation does not touch is kept
    as is (annotations, aliases, comments, namespaces...).

    When ports are removed, the references to them are updated: FMI-2
    `<ModelStructure>` indexes and `derivative` attributes are renumbered,
    entries and dependencies of `<ModelStructure>` that refer to removed ports
    are dropped.

    The operation is refused (`OperationError`, descriptor left unchanged) when
    it would produce an FMU that breaks the FMI standard:

    - every variable removed (empty `<ModelVariables>`, forbidden by the XSD);
    - a removed variable still referenced by a kept one (`derivative`,
      `previous`, `clocks`, `<Dimension valueReference>`);
    - several variables (or FMI-3 aliases) with the same name.

    Attributes:
        operation (OperationAbstract): The operation being applied.
        fmu (FMU): The FMU being manipulated.
        model_description (ModelDescription | None): The descriptor tree, once loaded.
        port_translation (list[int | None]): Maps original port indices (0-based)
            to new 1-based indices, or `None` for removed ports.
        port_names_list (list[str | None]): Original names of all encountered ports.
        port_removed_vr (set[str | None]): Value references of removed ports.
    """

    def __init__(self, operation, fmu):
        self.operation = operation
        self.fmu = fmu
        self.model_description: ModelDescription | None = None
        self.apply_on = None
        self.types = None
        self.variabilities = None

        self.current_port_number: int = 0
        self.port_translation: list[int | None] = []
        self.port_names_list: list[str | None] = []
        self.port_removed_vr: set[str | None] = set()

        self.operation.set_fmu(fmu)

    def manipulate(self, descriptor_filename, apply_on=None, *, types=None, variabilities=None):
        """Apply the operation and rewrite the descriptor file (unless the operation is `read_only`).

        Args:
            descriptor_filename (str): Path to the `modelDescription.xml` file.
                The file is modified in place.
            apply_on (list[str] | None): If set, only process ports with a
                causality in this list.
            types (list[str] | None): If set, only process ports of these types
                (see [ModelVariable.has_type][fmu_manipulation_toolbox.model_description.ModelVariable.has_type]).
            variabilities (list[str] | None): If set, only process ports with a
                variability in this list (the default of the standard when not declared).

        Raises:
            FMUError: If the descriptor cannot be loaded.
            OperationError: If the result would break the FMI standard. The
                descriptor file is then left unchanged.
        """
        self.apply_on = apply_on
        self.types = types
        self.variabilities = variabilities
        shared = descriptor_filename == self.fmu.descriptor_filename
        if shared:
            md = self.fmu.model_description
        else:
            try:
                md = ModelDescription.load(descriptor_filename)
            except ModelDescriptionError as error:
                raise FMUError(f"'{self.fmu.fmu_filename}': {error}") from error
        self.model_description = md
        self.fmu.fmi_version = md.fmi_version
        self.operation.model_description = md

        self.operation.fmi_attrs(md.attributes(md.root))
        self.handle_toplevel_elements(md)
        names_before = self.duplicate_names(md)
        variables = md.variables()
        removed = self.handle_ports(md)
        self.check_result(md, variables, removed, names_before)
        self.remove_children(md.model_variables, removed)
        self.renumber_derivatives(md)
        self.handle_model_structure(md)
        self.operation.closure()
        if not self.operation.read_only:
            if shared:
                self.fmu._save_model_description()
            else:
                md.save(descriptor_filename)

    # ---------------------------------------------------------------- Callbacks
    def handle_toplevel_elements(self, md: ModelDescription):
        callbacks = {
            "CoSimulation": self.operation.cosimulation_attrs,
            "ModelExchange": self.operation.modelexchange_attrs,
            "DefaultExperiment": self.operation.experiment_attrs,
        }
        for element in list(md.root):
            callback = callbacks.get(element.tag)
            if callback:
                try:
                    callback(md.attributes(element))
                except ManipulationSkipTag:
                    md.root.remove(element)

    def handle_ports(self, md: ModelDescription) -> list[ET.Element]:
        """Call `port_attrs` on every port. Returns the elements to remove."""
        removed = []
        for element in md.variables():
            port = ModelVariable(element, md.fmi_version)
            # name, valueReference and causality are attributes of <ScalarVariable> (FMI-2) or of
            # the variable element (FMI-3): read them directly, before the operation renames the port.
            name = element.get("name")
            vr = element.get("valueReference")
            remove = False
            if self.in_scope(port):
                try:
                    remove = bool(self.operation.port_attrs(port))
                except ManipulationSkipTag:
                    remove = True

            self.port_names_list.append(name)
            if remove:
                logger.info(f"Port '{name}' is removed.")
                self.port_translation.append(None)
                self.port_removed_vr.add(vr)
                removed.append(element)
            else:
                self.current_port_number += 1
                self.port_translation.append(self.current_port_number)
        return removed

    def in_scope(self, port: ModelVariable) -> bool:
        """Whether the operation applies to this port: every filter set must accept it."""
        # Read before the operation runs: it may rename the port or change its attributes.
        return ((not self.apply_on or port.causality in self.apply_on)
                and (not self.types or port.has_type(self.types))
                and (not self.variabilities or port.variability in self.variabilities))

    # ------------------------------------------------------- Conformity checks
    @staticmethod
    def all_names(md: ModelDescription) -> list[str | None]:
        """Names of variables and FMI-3 aliases, which must all be unique."""
        names = [variable.get("name") for variable in md.variables()]
        if md.fmi_version == 3:
            names += [alias.get("name") for alias in md.model_variables.iter("Alias")]
        return names

    @classmethod
    def duplicate_names(cls, md: ModelDescription) -> set[str | None]:
        seen, duplicates = set(), set()
        for name in cls.all_names(md):
            if name in seen:
                duplicates.add(name)
            seen.add(name)
        return duplicates

    def check_result(self, md: ModelDescription, variables: list[ET.Element], removed: list[ET.Element],
                     names_before: set[str]):
        """Refuse a result that breaks the FMI standard (only for what the operation changed)."""
        if variables and len(removed) == len(variables):
            raise OperationError("The operation would remove every variable: <ModelVariables> cannot be empty.")

        broken = self.broken_references(md, variables, removed)
        if broken:
            more = f" (and {len(broken) - 10} more)" if len(broken) > 10 else ""
            raise OperationError("The operation would remove variables that are still referenced: " +
                                 "; ".join(broken[:10]) + more + ".")

        removed_ids = {id(element) for element in removed}
        kept_names = [variable.get("name") for variable in variables if id(variable) not in removed_ids]
        if md.fmi_version == 3:
            kept_names += [alias.get("name") for variable in variables if id(variable) not in removed_ids
                           for alias in variable.iter("Alias")]
        seen, duplicates = set(), []
        for name in kept_names:
            if name in seen and name not in names_before and name not in duplicates:
                duplicates.append(name)
            seen.add(name)
        if duplicates:
            raise OperationError("The operation would give the same name to several variables: " +
                                 ", ".join(f"'{name}'" for name in duplicates[:10]) +
                                 (f" (and {len(duplicates) - 10} more)" if len(duplicates) > 10 else "") + ".")

    @staticmethod
    def broken_references(md: ModelDescription, variables: list[ET.Element],
                          removed: list[ET.Element]) -> list[str]:
        """References from kept variables to removed ones, as human-readable strings."""
        if not removed:
            return []
        removed_ids = {id(element) for element in removed}
        kept = [variable for variable in variables if id(variable) not in removed_ids]
        broken = []
        if md.fmi_version == 2:
            removed_indexes = {index for index, variable in enumerate(variables, start=1)
                               if id(variable) in removed_ids}
            for variable in kept:
                port = ModelVariable(variable, 2)
                derivative = port.typed_element.get("derivative") if port.typed_element is not None else None
                if derivative and derivative.isdigit() and int(derivative) in removed_indexes:
                    state = variables[int(derivative) - 1].get("name")
                    broken.append(f"'{state}' is referenced by '{variable.get('name')}' (derivative)")
        else:
            removed_vrs = {variable.get("valueReference"): variable.get("name") for variable in removed}
            for variable in kept:
                references = [("derivative", variable.get("derivative")), ("previous", variable.get("previous"))]
                references += [("clocks", vr) for vr in (variable.get("clocks") or "").split()]
                references += [("Dimension", dimension.get("valueReference"))
                               for dimension in variable.findall("Dimension")]
                for kind, vr in references:
                    if vr in removed_vrs:
                        broken.append(f"'{removed_vrs[vr]}' is referenced by '{variable.get('name')}' ({kind})")
        return list(dict.fromkeys(broken))  # a variable can refer twice to the same one (e.g. two dimensions)

    # -------------------------------------------------------------- References
    def renumber_derivatives(self, md: ModelDescription):
        """FMI-2: `derivative` is the 1-based index of the state variable."""
        if md.fmi_version != 2 or self.current_port_number == len(self.port_translation):
            return  # nothing removed
        for variable in md.variables():
            typed = ModelVariable(variable, 2).typed_element
            if typed is None:
                continue
            derivative = typed.get("derivative")
            if derivative and derivative.isdigit() and 0 < int(derivative) <= len(self.port_translation):
                typed.set("derivative", str(self.port_translation[int(derivative) - 1]))

    def handle_model_structure(self, md: ModelDescription):
        structure = md.model_structure
        if structure is None:
            return
        if md.fmi_version == 2:
            for section in list(structure):
                if section.tag not in ModelDescription.FMI2_STRUCTURE_SECTIONS:
                    continue
                skipped = []
                for unknown in [child for child in section if child.tag == "Unknown"]:
                    try:
                        self.operation.model_structure_attrs(section.tag, unknown.attrib)
                        self.unknown_attrs(unknown.attrib)
                    except ManipulationSkipTag:
                        skipped.append(unknown)
                self.remove_children(section, skipped)
                if not any(child.tag == "Unknown" for child in section):
                    logger.debug(f"Remove tag <{section.tag}> from modelDescription.xml.")
                    structure.remove(section)
        else:
            skipped = []
            for section_name, entry in md.model_structure_entries():
                try:
                    self.handle_structure(section_name, entry.attrib)
                except ManipulationSkipTag:
                    skipped.append(entry)
            self.remove_children(structure, skipped)

    @staticmethod
    def remove_children(parent: ET.Element, children: list[ET.Element]):
        """Remove several children at once (`Element.remove` is linear: one call per child is quadratic)."""
        if children:
            removed = {id(child) for child in children}
            parent[:] = [child for child in parent if id(child) not in removed]

    def unknown_attrs(self, attrs):
        """FMI-2: renumber an `<Unknown>` entry. Raises `ManipulationSkipTag` if its port is removed."""
        index = int(attrs['index'])
        new_index = self.port_translation[index-1]
        if new_index is None:
            logger.warning(f"Removed port '{self.port_names_list[index-1]}' is involved in the dependencies tree.")
            raise ManipulationSkipTag

        attrs['index'] = str(new_index)
        if attrs.get('dependencies', ""):
            kept = [(str(self.port_translation[int(dependency)-1]), kind)
                    for dependency, kind in self.dependency_pairs(attrs)
                    if self.port_translation[int(dependency)-1] is not None]
            self.set_dependencies(attrs, kept)

    def handle_structure(self, section, attrs):
        """FMI-3: filter a `<ModelStructure>` entry. Raises `ManipulationSkipTag` if its port is removed."""
        self.operation.model_structure_attrs(section, attrs)

        try:
            vr = attrs['valueReference']
            if vr in self.port_removed_vr:
                logger.warning(f"Removed port with valueReference {vr} is involved in the dependencies tree.")
                raise ManipulationSkipTag
        except KeyError:
            return

        if attrs.get('dependencies', ""):
            kept = [(dependency, kind) for dependency, kind in self.dependency_pairs(attrs)
                    if dependency not in self.port_removed_vr]
            self.set_dependencies(attrs, kept)

    @staticmethod
    def dependency_pairs(attrs) -> list[tuple[str, str | None]]:
        """`(dependency, dependencyKind)` pairs; the kind is `None` without `dependenciesKind`."""
        dependencies = attrs['dependencies'].split(' ')
        if 'dependenciesKind' in attrs:
            return list(zip(dependencies, attrs['dependenciesKind'].split(' '), strict=False))
        return [(dependency, None) for dependency in dependencies]

    @staticmethod
    def set_dependencies(attrs, kept: list[tuple[str, str | None]]):
        """Write back filtered `dependencies` (and `dependenciesKind`), or drop them if empty."""
        if kept:
            attrs['dependencies'] = " ".join(dependency for dependency, _ in kept)
            if 'dependenciesKind' in attrs:     # then every dependency has a kind (dependency_pairs)
                attrs['dependenciesKind'] = " ".join(kind for _, kind in kept if kind is not None)
        else:
            attrs.pop('dependencies')
            attrs.pop('dependenciesKind', None)

all_names(md) staticmethod

Names of variables and FMI-3 aliases, which must all be unique.

Source code in fmu_manipulation_toolbox/operations.py
@staticmethod
def all_names(md: ModelDescription) -> list[str | None]:
    """Names of variables and FMI-3 aliases, which must all be unique."""
    names = [variable.get("name") for variable in md.variables()]
    if md.fmi_version == 3:
        names += [alias.get("name") for alias in md.model_variables.iter("Alias")]
    return names

broken_references(md, variables, removed) staticmethod

References from kept variables to removed ones, as human-readable strings.

Source code in fmu_manipulation_toolbox/operations.py
@staticmethod
def broken_references(md: ModelDescription, variables: list[ET.Element],
                      removed: list[ET.Element]) -> list[str]:
    """References from kept variables to removed ones, as human-readable strings."""
    if not removed:
        return []
    removed_ids = {id(element) for element in removed}
    kept = [variable for variable in variables if id(variable) not in removed_ids]
    broken = []
    if md.fmi_version == 2:
        removed_indexes = {index for index, variable in enumerate(variables, start=1)
                           if id(variable) in removed_ids}
        for variable in kept:
            port = ModelVariable(variable, 2)
            derivative = port.typed_element.get("derivative") if port.typed_element is not None else None
            if derivative and derivative.isdigit() and int(derivative) in removed_indexes:
                state = variables[int(derivative) - 1].get("name")
                broken.append(f"'{state}' is referenced by '{variable.get('name')}' (derivative)")
    else:
        removed_vrs = {variable.get("valueReference"): variable.get("name") for variable in removed}
        for variable in kept:
            references = [("derivative", variable.get("derivative")), ("previous", variable.get("previous"))]
            references += [("clocks", vr) for vr in (variable.get("clocks") or "").split()]
            references += [("Dimension", dimension.get("valueReference"))
                           for dimension in variable.findall("Dimension")]
            for kind, vr in references:
                if vr in removed_vrs:
                    broken.append(f"'{removed_vrs[vr]}' is referenced by '{variable.get('name')}' ({kind})")
    return list(dict.fromkeys(broken))  # a variable can refer twice to the same one (e.g. two dimensions)

check_result(md, variables, removed, names_before)

Refuse a result that breaks the FMI standard (only for what the operation changed).

Source code in fmu_manipulation_toolbox/operations.py
def check_result(self, md: ModelDescription, variables: list[ET.Element], removed: list[ET.Element],
                 names_before: set[str]):
    """Refuse a result that breaks the FMI standard (only for what the operation changed)."""
    if variables and len(removed) == len(variables):
        raise OperationError("The operation would remove every variable: <ModelVariables> cannot be empty.")

    broken = self.broken_references(md, variables, removed)
    if broken:
        more = f" (and {len(broken) - 10} more)" if len(broken) > 10 else ""
        raise OperationError("The operation would remove variables that are still referenced: " +
                             "; ".join(broken[:10]) + more + ".")

    removed_ids = {id(element) for element in removed}
    kept_names = [variable.get("name") for variable in variables if id(variable) not in removed_ids]
    if md.fmi_version == 3:
        kept_names += [alias.get("name") for variable in variables if id(variable) not in removed_ids
                       for alias in variable.iter("Alias")]
    seen, duplicates = set(), []
    for name in kept_names:
        if name in seen and name not in names_before and name not in duplicates:
            duplicates.append(name)
        seen.add(name)
    if duplicates:
        raise OperationError("The operation would give the same name to several variables: " +
                             ", ".join(f"'{name}'" for name in duplicates[:10]) +
                             (f" (and {len(duplicates) - 10} more)" if len(duplicates) > 10 else "") + ".")

dependency_pairs(attrs) staticmethod

(dependency, dependencyKind) pairs; the kind is None without dependenciesKind.

Source code in fmu_manipulation_toolbox/operations.py
@staticmethod
def dependency_pairs(attrs) -> list[tuple[str, str | None]]:
    """`(dependency, dependencyKind)` pairs; the kind is `None` without `dependenciesKind`."""
    dependencies = attrs['dependencies'].split(' ')
    if 'dependenciesKind' in attrs:
        return list(zip(dependencies, attrs['dependenciesKind'].split(' '), strict=False))
    return [(dependency, None) for dependency in dependencies]

handle_ports(md)

Call port_attrs on every port. Returns the elements to remove.

Source code in fmu_manipulation_toolbox/operations.py
def handle_ports(self, md: ModelDescription) -> list[ET.Element]:
    """Call `port_attrs` on every port. Returns the elements to remove."""
    removed = []
    for element in md.variables():
        port = ModelVariable(element, md.fmi_version)
        # name, valueReference and causality are attributes of <ScalarVariable> (FMI-2) or of
        # the variable element (FMI-3): read them directly, before the operation renames the port.
        name = element.get("name")
        vr = element.get("valueReference")
        remove = False
        if self.in_scope(port):
            try:
                remove = bool(self.operation.port_attrs(port))
            except ManipulationSkipTag:
                remove = True

        self.port_names_list.append(name)
        if remove:
            logger.info(f"Port '{name}' is removed.")
            self.port_translation.append(None)
            self.port_removed_vr.add(vr)
            removed.append(element)
        else:
            self.current_port_number += 1
            self.port_translation.append(self.current_port_number)
    return removed

handle_structure(section, attrs)

FMI-3: filter a <ModelStructure> entry. Raises ManipulationSkipTag if its port is removed.

Source code in fmu_manipulation_toolbox/operations.py
def handle_structure(self, section, attrs):
    """FMI-3: filter a `<ModelStructure>` entry. Raises `ManipulationSkipTag` if its port is removed."""
    self.operation.model_structure_attrs(section, attrs)

    try:
        vr = attrs['valueReference']
        if vr in self.port_removed_vr:
            logger.warning(f"Removed port with valueReference {vr} is involved in the dependencies tree.")
            raise ManipulationSkipTag
    except KeyError:
        return

    if attrs.get('dependencies', ""):
        kept = [(dependency, kind) for dependency, kind in self.dependency_pairs(attrs)
                if dependency not in self.port_removed_vr]
        self.set_dependencies(attrs, kept)

in_scope(port)

Whether the operation applies to this port: every filter set must accept it.

Source code in fmu_manipulation_toolbox/operations.py
def in_scope(self, port: ModelVariable) -> bool:
    """Whether the operation applies to this port: every filter set must accept it."""
    # Read before the operation runs: it may rename the port or change its attributes.
    return ((not self.apply_on or port.causality in self.apply_on)
            and (not self.types or port.has_type(self.types))
            and (not self.variabilities or port.variability in self.variabilities))

manipulate(descriptor_filename, apply_on=None, *, types=None, variabilities=None)

Apply the operation and rewrite the descriptor file (unless the operation is read_only).

Parameters:

Name Type Description Default
descriptor_filename str

Path to the modelDescription.xml file. The file is modified in place.

required
apply_on list[str] | None

If set, only process ports with a causality in this list.

None
types list[str] | None

If set, only process ports of these types (see ModelVariable.has_type).

None
variabilities list[str] | None

If set, only process ports with a variability in this list (the default of the standard when not declared).

None

Raises:

Type Description
FMUError

If the descriptor cannot be loaded.

OperationError

If the result would break the FMI standard. The descriptor file is then left unchanged.

Source code in fmu_manipulation_toolbox/operations.py
def manipulate(self, descriptor_filename, apply_on=None, *, types=None, variabilities=None):
    """Apply the operation and rewrite the descriptor file (unless the operation is `read_only`).

    Args:
        descriptor_filename (str): Path to the `modelDescription.xml` file.
            The file is modified in place.
        apply_on (list[str] | None): If set, only process ports with a
            causality in this list.
        types (list[str] | None): If set, only process ports of these types
            (see [ModelVariable.has_type][fmu_manipulation_toolbox.model_description.ModelVariable.has_type]).
        variabilities (list[str] | None): If set, only process ports with a
            variability in this list (the default of the standard when not declared).

    Raises:
        FMUError: If the descriptor cannot be loaded.
        OperationError: If the result would break the FMI standard. The
            descriptor file is then left unchanged.
    """
    self.apply_on = apply_on
    self.types = types
    self.variabilities = variabilities
    shared = descriptor_filename == self.fmu.descriptor_filename
    if shared:
        md = self.fmu.model_description
    else:
        try:
            md = ModelDescription.load(descriptor_filename)
        except ModelDescriptionError as error:
            raise FMUError(f"'{self.fmu.fmu_filename}': {error}") from error
    self.model_description = md
    self.fmu.fmi_version = md.fmi_version
    self.operation.model_description = md

    self.operation.fmi_attrs(md.attributes(md.root))
    self.handle_toplevel_elements(md)
    names_before = self.duplicate_names(md)
    variables = md.variables()
    removed = self.handle_ports(md)
    self.check_result(md, variables, removed, names_before)
    self.remove_children(md.model_variables, removed)
    self.renumber_derivatives(md)
    self.handle_model_structure(md)
    self.operation.closure()
    if not self.operation.read_only:
        if shared:
            self.fmu._save_model_description()
        else:
            md.save(descriptor_filename)

remove_children(parent, children) staticmethod

Remove several children at once (Element.remove is linear: one call per child is quadratic).

Source code in fmu_manipulation_toolbox/operations.py
@staticmethod
def remove_children(parent: ET.Element, children: list[ET.Element]):
    """Remove several children at once (`Element.remove` is linear: one call per child is quadratic)."""
    if children:
        removed = {id(child) for child in children}
        parent[:] = [child for child in parent if id(child) not in removed]

renumber_derivatives(md)

FMI-2: derivative is the 1-based index of the state variable.

Source code in fmu_manipulation_toolbox/operations.py
def renumber_derivatives(self, md: ModelDescription):
    """FMI-2: `derivative` is the 1-based index of the state variable."""
    if md.fmi_version != 2 or self.current_port_number == len(self.port_translation):
        return  # nothing removed
    for variable in md.variables():
        typed = ModelVariable(variable, 2).typed_element
        if typed is None:
            continue
        derivative = typed.get("derivative")
        if derivative and derivative.isdigit() and 0 < int(derivative) <= len(self.port_translation):
            typed.set("derivative", str(self.port_translation[int(derivative) - 1]))

set_dependencies(attrs, kept) staticmethod

Write back filtered dependencies (and dependenciesKind), or drop them if empty.

Source code in fmu_manipulation_toolbox/operations.py
@staticmethod
def set_dependencies(attrs, kept: list[tuple[str, str | None]]):
    """Write back filtered `dependencies` (and `dependenciesKind`), or drop them if empty."""
    if kept:
        attrs['dependencies'] = " ".join(dependency for dependency, _ in kept)
        if 'dependenciesKind' in attrs:     # then every dependency has a kind (dependency_pairs)
            attrs['dependenciesKind'] = " ".join(kind for _, kind in kept if kind is not None)
    else:
        attrs.pop('dependencies')
        attrs.pop('dependenciesKind', None)

unknown_attrs(attrs)

FMI-2: renumber an <Unknown> entry. Raises ManipulationSkipTag if its port is removed.

Source code in fmu_manipulation_toolbox/operations.py
def unknown_attrs(self, attrs):
    """FMI-2: renumber an `<Unknown>` entry. Raises `ManipulationSkipTag` if its port is removed."""
    index = int(attrs['index'])
    new_index = self.port_translation[index-1]
    if new_index is None:
        logger.warning(f"Removed port '{self.port_names_list[index-1]}' is involved in the dependencies tree.")
        raise ManipulationSkipTag

    attrs['index'] = str(new_index)
    if attrs.get('dependencies', ""):
        kept = [(str(self.port_translation[int(dependency)-1]), kind)
                for dependency, kind in self.dependency_pairs(attrs)
                if self.port_translation[int(dependency)-1] is not None]
        self.set_dependencies(attrs, kept)

ManipulationSkipTag

Bases: Exception

Internal exception used to skip XML content until a matching closing tag.

Raised during SAX parsing to signal that the current element and all its children should be omitted from the output.

Source code in fmu_manipulation_toolbox/operations.py
class ManipulationSkipTag(Exception):
    """Internal exception used to skip XML content until a matching closing tag.

    Raised during SAX parsing to signal that the current element and all
    its children should be omitted from the output.
    """

OperationAbstract

Base class for all FMU manipulation operations.

Subclass this to implement custom operations on FMU descriptors. The methods act as callbacks invoked, in document order, while modelDescription.xml is walked: fmi_attrs, then cosimulation_attrs, modelexchange_attrs and experiment_attrs, then port_attrs for each variable, then model_structure_attrs, and finally closure.

The attrs dictionaries given to the callbacks are those of the descriptor tree: changing them changes the descriptor.

Attributes:

Name Type Description
fmu FMU | None

The FMU being processed, set via set_fmu.

model_description ModelDescription | None

The whole descriptor tree, set before the first callback. Use it for changes that the callbacks cannot express (e.g. removing an element).

read_only bool

Set to True in an operation that never changes the descriptor: it is then not written back. Defaults to False, which is always safe.

Source code in fmu_manipulation_toolbox/operations.py
class OperationAbstract:
    """Base class for all FMU manipulation operations.

    Subclass this to implement custom operations on FMU descriptors. The
    methods act as callbacks invoked, in document order, while
    `modelDescription.xml` is walked: `fmi_attrs`, then `cosimulation_attrs`,
    `modelexchange_attrs` and `experiment_attrs`, then `port_attrs` for each
    variable, then `model_structure_attrs`, and finally `closure`.

    The `attrs` dictionaries given to the callbacks are those of the descriptor
    tree: changing them changes the descriptor.

    Attributes:
        fmu (FMU | None): The FMU being processed, set via `set_fmu`.
        model_description (ModelDescription | None): The whole descriptor tree,
            set before the first callback. Use it for changes that the
            callbacks cannot express (e.g. removing an element).
        read_only (bool): Set to `True` in an operation that never changes
            the descriptor: it is then not written back. Defaults to `False`,
            which is always safe.
    """

    fmu: FMU | None = None
    model_description: ModelDescription | None = None
    read_only: bool = False

    def set_fmu(self, fmu):
        """Bind this operation to an FMU.

        Called automatically before parsing begins.

        Args:
            fmu (FMU): The FMU to operate on.
        """
        self.fmu = fmu

    def fmi_attrs(self, attrs):
        """Called when the `<fmiModelDescription>` element is encountered.

        Args:
            attrs (dict[str, str]): XML attributes of the root element.
        """
        pass

    def cosimulation_attrs(self, attrs):
        """Called when the `<CoSimulation>` element is encountered.

        Args:
            attrs (dict[str, str]): XML attributes of the co-simulation element.
        """
        pass

    def modelexchange_attrs(self, attrs):
        """Called when the `<ModelExchange>` element is encountered.

        Args:
            attrs (dict[str, str]): XML attributes of the model-exchange element.
        """
        pass

    def experiment_attrs(self, attrs):
        """Called when the `<DefaultExperiment>` element is encountered.

        Args:
            attrs (dict[str, str]): XML attributes of the default experiment.
        """
        pass

    def model_structure_attrs(self, section: str, attrs: dict[str, str]):
        """Called for each entry of the `<ModelStructure>` section.

        For FMI-2, `section` is the name of the enclosing sub-section
        (`"Outputs"`, `"Derivatives"`, `"InitialUnknowns"`) and `attrs` holds the
        attributes of an `<Unknown>` element (1-based `index` of the variable).

        For FMI-3, `section` is the name of the element itself
        (`"Output"`, `"ContinuousStateDerivative"`, `"InitialUnknown"`,
        `"EventIndicator"`, `"ClockedState"`) and `attrs` holds its attributes
        (including `valueReference`).

        Args:
            section (str): Name of the model-structure section or element.
            attrs (dict[str, str]): XML attributes of the entry.
        """
        pass

    def port_attrs(self, fmu_port: ModelVariable) -> int:
        """Called for each port (variable) in the descriptor.

        Override this to inspect or modify port attributes.

        Args:
            fmu_port (ModelVariable): The port being processed: a view on the
                variable of the descriptor tree (see
                [ModelVariable][fmu_manipulation_toolbox.model_description.ModelVariable]).
                Attributes can be modified in place.

        Returns:
            int: `0` to keep the port, non-zero to remove it.
        """
        return 0

    def closure(self):
        """Called after the descriptor has been fully parsed.

        Override this for post-processing or cleanup.
        """
        pass

closure()

Called after the descriptor has been fully parsed.

Override this for post-processing or cleanup.

Source code in fmu_manipulation_toolbox/operations.py
def closure(self):
    """Called after the descriptor has been fully parsed.

    Override this for post-processing or cleanup.
    """
    pass

cosimulation_attrs(attrs)

Called when the <CoSimulation> element is encountered.

Parameters:

Name Type Description Default
attrs dict[str, str]

XML attributes of the co-simulation element.

required
Source code in fmu_manipulation_toolbox/operations.py
def cosimulation_attrs(self, attrs):
    """Called when the `<CoSimulation>` element is encountered.

    Args:
        attrs (dict[str, str]): XML attributes of the co-simulation element.
    """
    pass

experiment_attrs(attrs)

Called when the <DefaultExperiment> element is encountered.

Parameters:

Name Type Description Default
attrs dict[str, str]

XML attributes of the default experiment.

required
Source code in fmu_manipulation_toolbox/operations.py
def experiment_attrs(self, attrs):
    """Called when the `<DefaultExperiment>` element is encountered.

    Args:
        attrs (dict[str, str]): XML attributes of the default experiment.
    """
    pass

fmi_attrs(attrs)

Called when the <fmiModelDescription> element is encountered.

Parameters:

Name Type Description Default
attrs dict[str, str]

XML attributes of the root element.

required
Source code in fmu_manipulation_toolbox/operations.py
def fmi_attrs(self, attrs):
    """Called when the `<fmiModelDescription>` element is encountered.

    Args:
        attrs (dict[str, str]): XML attributes of the root element.
    """
    pass

model_structure_attrs(section, attrs)

Called for each entry of the <ModelStructure> section.

For FMI-2, section is the name of the enclosing sub-section ("Outputs", "Derivatives", "InitialUnknowns") and attrs holds the attributes of an <Unknown> element (1-based index of the variable).

For FMI-3, section is the name of the element itself ("Output", "ContinuousStateDerivative", "InitialUnknown", "EventIndicator", "ClockedState") and attrs holds its attributes (including valueReference).

Parameters:

Name Type Description Default
section str

Name of the model-structure section or element.

required
attrs dict[str, str]

XML attributes of the entry.

required
Source code in fmu_manipulation_toolbox/operations.py
def model_structure_attrs(self, section: str, attrs: dict[str, str]):
    """Called for each entry of the `<ModelStructure>` section.

    For FMI-2, `section` is the name of the enclosing sub-section
    (`"Outputs"`, `"Derivatives"`, `"InitialUnknowns"`) and `attrs` holds the
    attributes of an `<Unknown>` element (1-based `index` of the variable).

    For FMI-3, `section` is the name of the element itself
    (`"Output"`, `"ContinuousStateDerivative"`, `"InitialUnknown"`,
    `"EventIndicator"`, `"ClockedState"`) and `attrs` holds its attributes
    (including `valueReference`).

    Args:
        section (str): Name of the model-structure section or element.
        attrs (dict[str, str]): XML attributes of the entry.
    """
    pass

modelexchange_attrs(attrs)

Called when the <ModelExchange> element is encountered.

Parameters:

Name Type Description Default
attrs dict[str, str]

XML attributes of the model-exchange element.

required
Source code in fmu_manipulation_toolbox/operations.py
def modelexchange_attrs(self, attrs):
    """Called when the `<ModelExchange>` element is encountered.

    Args:
        attrs (dict[str, str]): XML attributes of the model-exchange element.
    """
    pass

port_attrs(fmu_port)

Called for each port (variable) in the descriptor.

Override this to inspect or modify port attributes.

Parameters:

Name Type Description Default
fmu_port ModelVariable

The port being processed: a view on the variable of the descriptor tree (see ModelVariable). Attributes can be modified in place.

required

Returns:

Name Type Description
int int

0 to keep the port, non-zero to remove it.

Source code in fmu_manipulation_toolbox/operations.py
def port_attrs(self, fmu_port: ModelVariable) -> int:
    """Called for each port (variable) in the descriptor.

    Override this to inspect or modify port attributes.

    Args:
        fmu_port (ModelVariable): The port being processed: a view on the
            variable of the descriptor tree (see
            [ModelVariable][fmu_manipulation_toolbox.model_description.ModelVariable]).
            Attributes can be modified in place.

    Returns:
        int: `0` to keep the port, non-zero to remove it.
    """
    return 0

set_fmu(fmu)

Bind this operation to an FMU.

Called automatically before parsing begins.

Parameters:

Name Type Description Default
fmu FMU

The FMU to operate on.

required
Source code in fmu_manipulation_toolbox/operations.py
def set_fmu(self, fmu):
    """Bind this operation to an FMU.

    Called automatically before parsing begins.

    Args:
        fmu (FMU): The FMU to operate on.
    """
    self.fmu = fmu

OperationError

Bases: Exception

Exception raised for operation-related errors.

Attributes:

Name Type Description
reason str

Human-readable description of the error.

Source code in fmu_manipulation_toolbox/operations.py
class OperationError(Exception):
    """Exception raised for operation-related errors.

    Attributes:
        reason (str): Human-readable description of the error.
    """

    def __init__(self, reason):
        self.reason = reason

    def __repr__(self):
        return self.reason

OperationKeepOnlyRegexp

Bases: OperationAbstract

Keep only ports whose names match a regular expression; remove all others.

Parameters:

Name Type Description Default
regex_string str

Regular expression pattern. Only ports with names matching this pattern (from the start) are kept.

required

Attributes:

Name Type Description
regex_string str

The original regex pattern string.

regex Pattern

Compiled regular expression.

Source code in fmu_manipulation_toolbox/operations.py
class OperationKeepOnlyRegexp(OperationAbstract):
    """Keep only ports whose names match a regular expression; remove all others.

    Args:
        regex_string (str): Regular expression pattern. Only ports with names
            matching this pattern (from the start) are kept.

    Attributes:
        regex_string (str): The original regex pattern string.
        regex (re.Pattern): Compiled regular expression.
    """

    def __repr__(self):
        return f"Keep only ports matching '{self.regex_string}'"

    def __init__(self, regex_string):
        self.regex_string = regex_string
        self.regex = re.compile(regex_string)

    def port_attrs(self, fmu_port):
        name = fmu_port['name']
        if self.regex.match(name):
            return 0
        else:
            return 1  # Remove port

OperationMergeTopLevel

Bases: OperationAbstract

Merge the top-level bus prefix into port names by replacing "." with "_".

Only the first dot is replaced. For example, "Bus1.signal_name" becomes "Bus1_signal_name".

Source code in fmu_manipulation_toolbox/operations.py
class OperationMergeTopLevel(OperationAbstract):
    """Merge the top-level bus prefix into port names by replacing `"."` with `"_"`.

    Only the first dot is replaced. For example, `"Bus1.signal_name"` becomes
    `"Bus1_signal_name"`.
    """

    def __repr__(self):
        return "Merge Top Level Bus with signal names"

    def port_attrs(self, fmu_port):
        old = fmu_port['name']
        fmu_port['name'] = old.replace('.', '_', 1)
        return 0

OperationRemoveRegexp

Bases: OperationAbstract

Remove all ports whose names match a regular expression.

Parameters:

Name Type Description Default
regex_string str

Regular expression pattern. Ports with names matching this pattern (from the start) are removed.

required

Attributes:

Name Type Description
regex_string str

The original regex pattern string.

regex Pattern

Compiled regular expression.

Source code in fmu_manipulation_toolbox/operations.py
class OperationRemoveRegexp(OperationAbstract):
    """Remove all ports whose names match a regular expression.

    Args:
        regex_string (str): Regular expression pattern. Ports with names
            matching this pattern (from the start) are removed.

    Attributes:
        regex_string (str): The original regex pattern string.
        regex (re.Pattern): Compiled regular expression.
    """

    def __repr__(self):
        return f"Remove ports matching '{self.regex_string}'"

    def __init__(self, regex_string):
        self.regex_string = regex_string
        self.regex = re.compile(regex_string)
        self.current_port_number = 0
        self.port_translation = []

    def port_attrs(self, fmu_port):
        name = fmu_port['name']
        if self.regex.match(name):
            return 1  # Remove port
        else:
            return 0

OperationRemoveSources

Bases: OperationAbstract

Remove the sources/ directory from the FMU.

Strips the embedded C/C++ source files that some FMUs include for recompilation on the target platform, and the <SourceFiles> elements (FMI-2) that list them. For FMI-3, the list of sources is sources/buildDescription.xml, removed with the directory.

Source code in fmu_manipulation_toolbox/operations.py
class OperationRemoveSources(OperationAbstract):
    """Remove the `sources/` directory from the FMU.

    Strips the embedded C/C++ source files that some FMUs include for
    recompilation on the target platform, and the `<SourceFiles>` elements
    (FMI-2) that list them. For FMI-3, the list of sources is
    `sources/buildDescription.xml`, removed with the directory.
    """

    def __repr__(self):
        return "Remove sources"

    def closure(self):
        try:
            shutil.rmtree(os.path.join(self.fmu.tmp_directory, "sources"))
        except FileNotFoundError:
            logger.info("This FMU does not embed sources.")

        for interface in self.model_description.interfaces.values():
            for source_files in interface.findall("SourceFiles"):
                interface.remove(source_files)

OperationRename

Bases: OperationAbstract

Rename or remove ports according to a mapping of names.

If the new name is empty, the port is removed. Ports not in the mapping are kept unchanged.

Parameters:

Name Type Description Default
translations dict[str, str]

Mapping from original to new names.

required

Attributes:

Name Type Description
translations dict[str, str]

Mapping from original to new names.

found set[str]

Original names of the mapping met in the FMU (among the ports in scope), once the operation is applied.

Example
with FMU("model.fmu") as fmu:
    fmu.apply_operation(OperationRename({"u": "speed", "debug": ""}))
    fmu.repack("model-renamed.fmu")
Source code in fmu_manipulation_toolbox/operations.py
class OperationRename(OperationAbstract):
    """Rename or remove ports according to a mapping of names.

    If the new name is empty, the port is removed. Ports not in the mapping
    are kept unchanged.

    Args:
        translations (dict[str, str]): Mapping from original to new names.

    Attributes:
        translations (dict[str, str]): Mapping from original to new names.
        found (set[str]): Original names of the mapping met in the FMU (among
            the ports in scope), once the operation is applied.

    Example:
        ```python
        with FMU("model.fmu") as fmu:
            fmu.apply_operation(OperationRename({"u": "speed", "debug": ""}))
            fmu.repack("model-renamed.fmu")
        ```
    """

    def __repr__(self):
        return f"Rename {len(self.translations)} port(s)"

    def __init__(self, translations: dict[str, str]):
        self.translations = dict(translations)
        self.found: set[str] = set()

    def port_attrs(self, fmu_port):
        name = fmu_port['name']
        try:
            new_name = self.translations[name]
            self.found.add(name)
        except KeyError:
            new_name = name  # if port is not in the mapping, keep old name

        if new_name:
            fmu_port['name'] = new_name
            return 0
        else:
            # we want to delete this name!
            return 1

OperationRenameFromCSV

Bases: OperationRename

Rename or remove ports according to a CSV mapping file.

Reads a semicolon-delimited CSV where column 0 is the original name and column 1 is the new name. If the new name is empty, the port is removed. Ports not listed in the CSV are kept unchanged.

Attributes:

Name Type Description
csv_filename str

Path to the CSV mapping file.

translations dict[str, str]

Mapping from original to new names.

Raises:

Type Description
OperationError

If the CSV file is not found or malformed.

Source code in fmu_manipulation_toolbox/operations.py
class OperationRenameFromCSV(OperationRename):
    """Rename or remove ports according to a CSV mapping file.

    Reads a semicolon-delimited CSV where column 0 is the original name
    and column 1 is the new name. If the new name is empty, the port is
    removed. Ports not listed in the CSV are kept unchanged.

    Attributes:
        csv_filename (str): Path to the CSV mapping file.
        translations (dict[str, str]): Mapping from original to new names.

    Raises:
        OperationError: If the CSV file is not found or malformed.
    """

    def __repr__(self):
        return f"Rename according to '{self.csv_filename}'"

    def __init__(self, csv_filename):
        self.csv_filename = csv_filename
        translations = {}

        try:
            with open_text(csv_filename) as csvfile:
                reader = csv.reader(csvfile, delimiter=';', quotechar="'")
                for row in reader:
                    translations[row[0]] = row[1]
        except FileNotFoundError:
            raise OperationError(f"File '{csv_filename}' is not found.") from None
        except IndexError:
            raise OperationError(f"File '{csv_filename}' should contain two columns.") from None
        super().__init__(translations)

OperationSaveNamesToCSV

Bases: OperationAbstract

Export all port names and metadata to a CSV file.

Generates a semicolon-delimited CSV with columns: name, newName, valueReference, causality, variability, scalarType, startValue.

The resulting file can be edited and used with OperationRenameFromCSV to rename or remove ports.

Attributes:

Name Type Description
output_filename str

Path to the output CSV file.

Source code in fmu_manipulation_toolbox/operations.py
class OperationSaveNamesToCSV(OperationAbstract):
    """Export all port names and metadata to a CSV file.

    Generates a semicolon-delimited CSV with columns: `name`, `newName`,
    `valueReference`, `causality`, `variability`, `scalarType`, `startValue`.

    The resulting file can be edited and used with
    [OperationRenameFromCSV][fmu_manipulation_toolbox.operations.OperationRenameFromCSV]
    to rename or remove ports.

    Attributes:
        output_filename (str): Path to the output CSV file.
    """

    read_only = True

    def __repr__(self):
        return f"Dump names into '{self.output_filename}'"

    def __init__(self, filename):
        self.output_filename = filename
        self.csvfile = open(filename, 'w', newline='', encoding=ENCODING)
        self.writer = csv.writer(self.csvfile, delimiter=';', quotechar="'", quoting=csv.QUOTE_MINIMAL)
        self.writer.writerow(['name', 'newName', 'valueReference', 'causality', 'variability', 'scalarType',
                              'startValue'])

    def closure(self):
        self.csvfile.close()

    def port_attrs(self, fmu_port: ModelVariable) -> int:
        self.writer.writerow([fmu_port["name"],
                              fmu_port["name"],
                              fmu_port["valueReference"],
                              fmu_port.get("causality", "local"),
                              fmu_port.get("variability", "continuous"),
                              fmu_port.fmi_type,
                              fmu_port.get("start", "")])

        return 0

OperationStripTopLevel

Bases: OperationAbstract

Remove the top-level bus prefix from all port names.

Strips everything before and including the first "." separator. For example, "Bus1.signal_name" becomes "signal_name".

Source code in fmu_manipulation_toolbox/operations.py
class OperationStripTopLevel(OperationAbstract):
    """Remove the top-level bus prefix from all port names.

    Strips everything before and including the first `"."` separator.
    For example, `"Bus1.signal_name"` becomes `"signal_name"`.
    """

    def __repr__(self):
        return "Remove Top Level Bus"

    def port_attrs(self, fmu_port):
        new_name = fmu_port['name'].split('.', 1)[-1]
        fmu_port['name'] = new_name
        return 0

OperationSummary

Bases: OperationAbstract

Log a detailed summary of the FMU contents.

Reports FMI properties, co-simulation capabilities, default experiment values, supported platforms, embedded resources, and port counts grouped by causality.

Attributes:

Name Type Description
nb_port_per_causality dict[str, int]

Count of ports per causality.

number_of_continuous_states int

Model-Exchange number of continuous states (nx), only reported when the FMU declares a <ModelExchange> section.

number_of_event_indicators int

Model-Exchange number of event indicators (nz), idem.

Source code in fmu_manipulation_toolbox/operations.py
class OperationSummary(OperationAbstract):
    """Log a detailed summary of the FMU contents.

    Reports FMI properties, co-simulation capabilities, default experiment
    values, supported platforms, embedded resources, and port counts
    grouped by causality.

    Attributes:
        nb_port_per_causality (dict[str, int]): Count of ports per causality.
        number_of_continuous_states (int): Model-Exchange number of continuous
            states (`nx`), only reported when the FMU declares a `<ModelExchange>`
            section.
        number_of_event_indicators (int): Model-Exchange number of event
            indicators (`nz`), idem.
    """

    read_only = True

    def __init__(self):
        self.nb_port_per_causality = {}
        self.fmi_version = 2
        self.has_model_exchange = False
        self.number_of_continuous_states = 0
        self.number_of_event_indicators = 0

    def __repr__(self):
        return "FMU Summary"

    def fmi_attrs(self, attrs):
        logger.info(f"| fmu filename = {self.fmu.fmu_filename}")
        self.fmi_version = int(float(attrs.get("fmiVersion", "2.0")))
        logger.info(f"| temporary directory = {self.fmu.tmp_directory}")
        hash_md5 = hashlib.md5()
        with open(self.fmu.fmu_filename, "rb") as f:
            for chunk in iter(lambda: f.read(4096), b""):
                hash_md5.update(chunk)
        digest = hash_md5.hexdigest()
        logger.info(f"| MD5Sum = {digest}")
        logger.info("|")
        logger.info("| FMI properties: ")
        for (k, v) in attrs.items():
            logger.info(f"|  - {k} = {v}")
        logger.info("|")

    def cosimulation_attrs(self, attrs):
        logger.info("| Co-Simulation capabilities: ")
        for (k, v) in attrs.items():
            logger.info(f"|  - {k} = {v}")
        logger.info("|")

    def modelexchange_attrs(self, attrs):
        self.has_model_exchange = True
        logger.info("| Model Exchange capabilities: ")
        for (k, v) in attrs.items():
            logger.info(f"|  - {k} = {v}")
        logger.info("|")

    def experiment_attrs(self, attrs):
        logger.info("| Default Experiment values: ")
        for (k, v) in attrs.items():
            logger.info(f"|  - {k} = {v}")
        logger.info("|")

    def port_attrs(self, fmu_port) -> int:
        causality = fmu_port.get("causality", "local")

        try:
            self.nb_port_per_causality[causality] += 1
        except KeyError:
            self.nb_port_per_causality[causality] = 1

        return 0

    def closure(self):
        logger.info("| Supported platforms: ")
        try:
            for platform in os.listdir(os.path.join(self.fmu.tmp_directory, "binaries")):
                logger.info(f"|  - {platform}")
        except FileNotFoundError:
            pass  # no binaries

        if os.path.isdir(os.path.join(self.fmu.tmp_directory, "sources")):
            logger.info("|  - RT (sources available)")

        resource_dir = os.path.join(self.fmu.tmp_directory, "resources")
        if os.path.isdir(resource_dir):
            logger.info("|")
            logger.info("| Embedded resources:")
            for resource in os.listdir(resource_dir):
                logger.info(f"|  - {resource}")

        extra_dir = os.path.join(self.fmu.tmp_directory, "extra")
        if os.path.isdir(extra_dir):
            logger.info("|")
            logger.info("| Additional (meta-)data:")
            for extra in os.listdir(extra_dir):
                logger.info(f"|  - {extra}")

        logger.info("|")
        logger.info("| Number of ports")
        for causality, nb_ports in self.nb_port_per_causality.items():
            logger.info(f"|  {causality} : {nb_ports}")

        if self.has_model_exchange:
            self.number_of_continuous_states, self.number_of_event_indicators = \
                self.model_description.model_exchange_sizes(os.path.basename(self.fmu.fmu_filename))
            logger.info("|")
            logger.info("| Model Exchange sizes")
            logger.info(f"|  continuous states : {self.number_of_continuous_states}")
            logger.info(f"|  event indicators : {self.number_of_event_indicators}")

        terminals = Terminals(self.fmu.tmp_directory)
        if terminals:
            logger.info("|")
            logger.info("| Terminals:")
            for terminal in terminals:
                logger.info(f"|  - {terminal}")

        logger.info("|")
        logger.info("| [End of report]")

OperationTrimUntil

Bases: OperationAbstract

Trim port names up to and including a separator string.

For example, with separator "__", the name "prefix__signal" becomes "signal".

Parameters:

Name Type Description Default
separator str

The separator string to search for in port names.

required

Attributes:

Name Type Description
separator str

The separator string.

Source code in fmu_manipulation_toolbox/operations.py
class OperationTrimUntil(OperationAbstract):
    """Trim port names up to and including a separator string.

    For example, with separator `"__"`, the name `"prefix__signal"` becomes
    `"signal"`.

    Args:
        separator (str): The separator string to search for in port names.

    Attributes:
        separator (str): The separator string.
    """

    def __init__(self, separator):
        self.separator = separator

    def __repr__(self):
        return f"Trim names until (and including) '{self.separator}'"

    def port_attrs(self, fmu_port) -> int:
        name = fmu_port['name']
        try:
            fmu_port['name'] = name[name.index(self.separator)+len(self.separator):]
        except ValueError:
            pass  # no separator

        return 0