Skip to content

Commit

Permalink
Utilities and 0.17.0 readiness (#79)
Browse files Browse the repository at this point in the history
* table exporting utilty and tests

* add triplets regex

* remove deprecated functions

* add runtime versions logging utility

* update versions to 0.17.0
  • Loading branch information
fsoubelet committed Mar 24, 2022
1 parent 52dcade commit 618064d
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 7 deletions.
4 changes: 2 additions & 2 deletions docs/release.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ The full list of releases can be found in the Github repository's `releases page

.. _release_0.17.0:

0.17.0-rc0
0.17.0
------

Enhancements
~~~~~~~~~~~~

* The `pyhdtoolkit.cpymadtools.matching` module has two new wrapper functions, `~pyhdtoolkit.cpymadtools.matching.match_tunes` and `~pyhdtoolkit.cpymadtools.matching.match_chromaticities`, to perform matching on either tunes or chromaticities only.
* The `pyhdtoolkit.cpymadtools.lhc` module has a new utility function, `~pyhdtoolkit.cpymadtools.lhc.get_magnets_powering`, to get the percentage of magnets' max powering used in a given configuration.
* The `pyhdtoolkit.cpymadtools.utils` module has a new function, `~pyhdtoolkit.cpymadtools.utils.export_madx_table`, to conveniently export an internal table to disk in a way that can be read by ``MAD-X`` later on.
* The `pyhdtoolkit.cpymadtools.utils` module has a new function, `~pyhdtoolkit.cpymadtools.utils.export_madx_table`, to conveniently export an internal table to disk with proper regex filtering in a way that can be read by ``MAD-X`` later on.
* The `pyhdtoolkit.cpymadtools.constants` module now includes a regex for the `(HL)LHC` triplets. Beware that ``MAD-X`` itself does not understand all regex features.

Bug Fixes
Expand Down
2 changes: 1 addition & 1 deletion pyhdtoolkit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
__title__ = "pyhdtoolkit"
__description__ = "An all-in-one toolkit package to easy my Python work in my PhD."
__url__ = "https://github.com/fsoubelet/PyhDToolkit"
__version__ = "0.17.0-rc0"
__version__ = "0.17.0"
__author__ = "Felix Soubelet"
__author_email__ = "felix.soubelet@cern.ch"
__license__ = "MIT"
7 changes: 6 additions & 1 deletion pyhdtoolkit/cpymadtools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@


def export_madx_table(
madx: Madx, table_name: str, file_name: Union[Path, str], headers_table: str = "SUMM", **kwargs
madx: Madx, table_name: str, file_name: Union[Path, str], pattern: str = None, headers_table: str = "SUMM", **kwargs
) -> None:
"""
Exports an internal table from the ``MAD-X`` process into a `~tfs.frame.TfsDataFrame` on disk.
Expand All @@ -32,13 +32,18 @@ def export_madx_table(
madx (cpymad.madx.Madx): an instanciated `~cpymad.madx.Madx` object.
table_name (str): the name of the internal table to retrieve.
file_name (str): the name of the file to export to.
pattern (str): if given, will be used as a regular expression to filter the extracted
table, by passing it as the *regex* parameter of `pandas.DataFrame.filter`.
headers_table (str): the name of the internal table to use for headers.
Defaults to ``SUMM``.
**kwargs: any keyword arguments will be passed to `~tfs.writer.write_tfs`.
"""
file_path = Path(file_name)
logger.debug(f"Exporting table {table_name} into '{file_path.absolute()}'")
dframe = get_table_tfs(madx, table_name, headers_table)
if pattern:
logger.debug(f"Setting NAME column as index and filtering extracted table with regex pattern '{pattern}'")
dframe = dframe.set_index("NAME").filter(regex=pattern, axis="index").reset_index()
if "NAME" not in dframe.headers:
logger.debug(f"No 'NAME' header found, adding a default value 'EXPORT'")
dframe.headers["NAME"] = "EXPORT"
Expand Down
9 changes: 9 additions & 0 deletions pyhdtoolkit/utils/_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,12 @@
from pathlib import Path
from typing import List

import cpymad

from cpymad.madx import Madx
from loguru import logger

from pyhdtoolkit import __version__
from pyhdtoolkit.cpymadtools import lhc

# ----- Constants ----- #
Expand Down Expand Up @@ -71,6 +74,12 @@ def get_opticsfiles_paths() -> List[Path]:
return sorted(desired_files, key=lambda x: float(x.suffix[1:])) # sort by the number after 'opticsfile.'


def log_runtime_versions() -> None:
"""Issues a ``CRITICAL``-level log stating the runtime versions of both `~pyhdtoolkit`, `cpymad` and ``MAD-X``."""
with Madx(stdout=False) as mad:
logger.critical(f"Using: pyhdtoolkit {__version__} | cpymad {cpymad.__version__} | {mad.version}")


# ----- MAD-X Setup Utilities ----- #


Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "pyhdtoolkit"
version = "0.17.0-rc0"
version = "0.17.0"
description = "An all-in-one toolkit package to ease my Python work in my PhD."
authors = ["Felix Soubelet <felix.soubelet@cern.ch>"]
license = "MIT"
Expand Down
8 changes: 6 additions & 2 deletions tests/test_cpymadtools/test_utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,17 @@ def test_k_strings_wrong_orientation_raises(self):
with pytest.raises(ValueError):
_get_k_strings(stop=5, orientation="wrong")

def test_table_export(self, _matched_base_lattice, tmp_path):
@pytest.mark.parametrize("regex", [None, "^QF*"])
def test_table_export(self, _matched_base_lattice, regex, tmp_path):
write_location = tmp_path / "test.tfs"
madx = _matched_base_lattice
madx.command.twiss()

twiss_df = get_table_tfs(madx, "twiss").reset_index(drop=True)
if regex:
twiss_df = twiss_df.set_index("NAME").filter(regex=regex, axis=0).reset_index()

export_madx_table(madx, table_name="twiss", file_name=write_location)
export_madx_table(madx, table_name="twiss", file_name=write_location, pattern=regex)
assert write_location.is_file()

new = tfs.read(write_location)
Expand Down

0 comments on commit 618064d

Please sign in to comment.