|
| 1 | +MAD-X and MAD-NG Compatibility |
| 2 | +============================== |
| 3 | + |
| 4 | +As `tfs-pandas` allows one to write `TfsDataFrames` as files in the **TFS** format, which are typically output by simulations codes, compatibility of these files with said codes is crucial. |
| 5 | +Specifically, `tfs-pandas` aims to ensure the files it writes to disk are accepted as input for `MAD-X <https://madx.web.cern.ch/>`_ and `MAD-NG <https://madx.web.cern.ch/releases/madng/html/>`_. |
| 6 | + |
| 7 | +However, as ``MAD-NG`` is the successor to ``MAD-X``, it includes new features regarding the **TFS** format, and files including these features will not be accepted by ``MAD-X``. |
| 8 | +To circumvent this issue, `tfs-pandas` offers functionality - named validation - to ensure compatibility with either code. |
| 9 | + |
| 10 | +TfsDataFrame Validation |
| 11 | +----------------------- |
| 12 | + |
| 13 | +It is possible to perform automatic validation of a `TfsDataFrame` both when reading and writing, or to validate them at any time using the `tfs.frame.validate` function. |
| 14 | +Validation enforces the rules described in the :ref:`caveats section <tfs-pandas caveats>`, both to guarantee the integrity of the dataframe and compatibility of written files with simulation codes. |
| 15 | + |
| 16 | +.. admonition:: When Does Validation Happen? |
| 17 | + |
| 18 | + Validation is **optional**, and is by default turned off at both read-time and write-time. |
| 19 | + |
| 20 | +Validation is done by providing a `TfsDataFrame` and a compatibility mode to `tfs.frame.validate` (see the :ref:`API reference <modules/index:frame>`). |
| 21 | +It goes as: |
| 22 | + |
| 23 | +.. code-block:: python |
| 24 | +
|
| 25 | + import tfs |
| 26 | + from tfs.frame import validate |
| 27 | +
|
| 28 | + df = tfs.read("path/to/file.tfs") |
| 29 | +
|
| 30 | + # To validate with MAD-X compatibility |
| 31 | + validate(df, compatibility="mad-x") # or use "madx" |
| 32 | +
|
| 33 | + # To validate with MAD-NG compatibility |
| 34 | + validate(df, compatibility="mad-ng") # or use "madng" |
| 35 | +
|
| 36 | +In case of compatibility issue, an exception is raised which will point to the specific incompatible element. |
| 37 | +All exceptions inherit from the `TfsFormatError`, which one can `except` as a catch-all for this package. |
| 38 | + |
| 39 | +.. _common rules: |
| 40 | + |
| 41 | +Common Validation Rules |
| 42 | +----------------------- |
| 43 | + |
| 44 | +In either compatibility mode, some common rules are enforced. |
| 45 | +These rules are listed and described in the :ref:`API reference <modules/index:frame>` for the `tfs.frame.validate` function. |
| 46 | + |
| 47 | +When validating a `TfsDataFrame`, the behavior in case one of these rules is violated depends on the value of the `non_unique_behavior` parameter. |
| 48 | +These rules are *always* checked against when validating a `TfsDataFrame`. |
| 49 | +Additional checks can be performed by setting the `compatibility` parameter, as described in the :ref:`MAD-NG <madng mode>` and :ref:`MAD-X <madx mode>` below. |
| 50 | + |
| 51 | +.. _madng mode: |
| 52 | + |
| 53 | +MAD-NG Compatibility |
| 54 | +-------------------- |
| 55 | + |
| 56 | +.. admonition:: Supported Versions |
| 57 | + |
| 58 | + The compatibility with ``MAD-NG`` is guaranteed starting with version `1.0.0`. |
| 59 | + See below for details and caveats. |
| 60 | + |
| 61 | +Since ``MAD-NG`` implements and accepts more features into its **TFS** files, its compatibility mode is naturally less restrictive. |
| 62 | +Namely, the following are accepted by ``MAD-NG`` and ``MAD-NG`` only: |
| 63 | + |
| 64 | +- Boolean dtype for header parameters and table columns. |
| 65 | +- Complex dtype for header parameters and table columns. |
| 66 | +- Nullable dtype for header parameters and table columns (value `nil`). |
| 67 | + |
| 68 | +.. admonition:: Complex Number Representation |
| 69 | + |
| 70 | + In Python, the imaginary part of a complex number is represented by the letter ``j``, as in `1.4 + 2.6j`. |
| 71 | + When writing complex values to file, `tfs-pandas` will instead use the ``MAD-NG`` (read `Lua`) representation, and uses the letter ``i``, as in `1.4 + 2.6i`, so that ``MAD-NG`` can properly read such a file. |
| 72 | + Both of these representations will be correctly read by `tfs-pandas` (including when ``MAD-NG`` uses ``I`` for special complex numbers). |
| 73 | + |
| 74 | +.. admonition:: Handling of Nullable Types |
| 75 | + |
| 76 | + ``MAD-NG`` uses the nullable `nil`, which is accepted by `tfs-pandas` with the following caveats: |
| 77 | + |
| 78 | + - When reading from file, `nil` values in the headers are converted to `None` while those in the dataframe are cast to `NaN`, except in string-dtyped columns where they are converted to `None`. |
| 79 | + - When writing to file, `None` values anywhere are written as `nil`, and `NaN` values in the dataframe are written as `NaN` (remember that setting a `None` in a numeric `pandas.DataFrame` column automatically casts it as `NaN`). |
| 80 | + |
| 81 | +.. attention:: |
| 82 | + |
| 83 | + The exotic "features" of ``MAD-NG`` such as the ``Lua`` operator overloading for ranges and tables, and their inclusion in **TFS** files are not supported by `tfs-pandas`. |
| 84 | + Should one need to use these features, it is recommended to go through the `pymadng <https://pymadng.readthedocs.io/en/latest/>`_ package to handle them in-memory. |
| 85 | + |
| 86 | +.. _madx mode: |
| 87 | + |
| 88 | +MAD-X Compatibility |
| 89 | +------------------- |
| 90 | + |
| 91 | +The ``MAD-X`` compatibility mode is more restrictive, and enforces that none of the features listed in the :ref:`MAD-NG section <madng mode>` appear in the `TfsDataFrame`. |
| 92 | + |
| 93 | +Additionally, ``MAD-X`` will refuse to read into a table any **TFS** file that does not include a `TYPE` entry in the headers (which should be a string). |
| 94 | +As such, when checking for compatibility with ``MAD-X``: |
| 95 | + |
| 96 | + - If the dataframe has no headers, an error will be raised indicating the dataframe should have headers. |
| 97 | + - If the dataframe has headers but no `TYPE` entry is not found, `tfs-pandas` will log a warning and add one with the value `"Added by tfs-pandas for MAD-X compatibility"` |
| 98 | + |
| 99 | +.. admonition:: Default mode |
| 100 | + |
| 101 | + The default compatibility mode enforced by the `tfs.frame.validate` function is ``MAD-X``. |
0 commit comments