pylhc / tfs

Python package to handle TFS files
https://pylhc.github.io/tfs/
MIT License
9 stars 4 forks source link

[Feature Request]: Check for madx-compatibility #104

Open JoschD opened 2 years ago

JoschD commented 2 years ago

Feature Description

Option to check for madx-compatibility on writing

Possible Implementation

into the writer add a flag: madx_compatibility with the options None (default), warn or error. If not none it should be checked that:

a) the header contains TYPE b) header-names do not contain spaces

... maybe more?

JoschD commented 2 years ago
    @pytest.mark.parametrize("file_fixture", ["_tfs_file_wise"], ids=["wise"])
    def test_madx_can_read_it(self, file_fixture, request):
        """ Use this test to assert that madx can read all our example files"""
        file_path = request.getfixturevalue(file_fixture)
        dframe = tfs.read(file_path)

        with Madx() as madx:
            madx.command.readtable(file=str(file_path), table="test_table")
            assert madx.table.test_table is not None  # check table has loaded

            # for the wise test, the header is all scrambled:
            # headers = {k.upper(): v for k, v in madx.table.test_table.summary.items()}

            # Check validity of the loaded table, here we use pandas.Series and assert_series_equal instead
            # of numpy.array_equal to allow for (very) small relative numerical differences on loading
            for column in dframe.columns:
                assert column in madx.table.test_table
                try:
                    series = dframe[column].str.lower()  # MADX converts ALL strings to lowercase
                except AttributeError:
                    series = dframe[column]

                assert_series_equal(
                    pandas.Series(madx.table.test_table[column]), series, check_names=False
                )

possible test implementation

fsoubelet commented 2 months ago

Will happen at the same time as checking for MAD-NG validity, WIP in https://github.com/pylhc/tfs/tree/madng

will link PR when open