Skip to content

Commit

Permalink
format and last lints
Browse files Browse the repository at this point in the history
  • Loading branch information
fsoubelet committed Aug 12, 2024
1 parent 76dec80 commit 249203f
Show file tree
Hide file tree
Showing 45 changed files with 175 additions and 82 deletions.
1 change: 1 addition & 0 deletions pyhdtoolkit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
:copyright: (c) 2019-2020 by Felix Soubelet.
:license: MIT, see LICENSE for more details.
"""

from . import cpymadtools, maths, models, optics, plotting, utils, version # noqa: F401, TID252

__title__ = "pyhdtoolkit"
Expand Down
17 changes: 13 additions & 4 deletions pyhdtoolkit/cpymadtools/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Specific constants to be used in `~.cpymadtools` functions, to help with consistency.
"""

_MAX_SECTOR_VALUE: int = 8

# fmt: off
Expand Down Expand Up @@ -136,16 +137,24 @@
for sector in [1, 2, 3, 4, 5, 6, 7, 8]
]
LHC_KSS_KNOBS: list[str] = [ # skew sextupole correctors
f"kss.a{sector}{sector+1 if sector < _MAX_SECTOR_VALUE else 1}b{beam}" for beam in [1, 2] for sector in [1, 2, 3, 4, 5, 6, 7, 8]
f"kss.a{sector}{sector+1 if sector < _MAX_SECTOR_VALUE else 1}b{beam}"
for beam in [1, 2]
for sector in [1, 2, 3, 4, 5, 6, 7, 8]
]
LHC_KCS_KNOBS: list[str] = [ # spool piece (skew) sextupoles
f"kcs.a{sector}{sector+1 if sector < _MAX_SECTOR_VALUE else 1}b{beam}" for beam in [1, 2] for sector in [1, 2, 3, 4, 5, 6, 7, 8]
f"kcs.a{sector}{sector+1 if sector < _MAX_SECTOR_VALUE else 1}b{beam}"
for beam in [1, 2]
for sector in [1, 2, 3, 4, 5, 6, 7, 8]
]
LHC_KCO_KNOBS: list[str] = [ # spool piece (skew) octupoles
f"kco.a{sector}{sector+1 if sector < _MAX_SECTOR_VALUE else 1}b{beam}" for beam in [1, 2] for sector in [1, 2, 3, 4, 5, 6, 7, 8]
f"kco.a{sector}{sector+1 if sector < _MAX_SECTOR_VALUE else 1}b{beam}"
for beam in [1, 2]
for sector in [1, 2, 3, 4, 5, 6, 7, 8]
]
LHC_KCD_KNOBS: list[str] = [ # spool piece (skew) decapoles
f"kcd.a{sector}{sector+1 if sector < _MAX_SECTOR_VALUE else 1}b{beam}" for beam in [1, 2] for sector in [1, 2, 3, 4, 5, 6, 7, 8]
f"kcd.a{sector}{sector+1 if sector < _MAX_SECTOR_VALUE else 1}b{beam}"
for beam in [1, 2]
for sector in [1, 2, 3, 4, 5, 6, 7, 8]
]
LHC_KO_KNOBS: list[str] = [ # octupoles in arc short straight sections
f"ko{family}.a{sector}{sector+1 if sector < _MAX_SECTOR_VALUE else 1}b{beam}"
Expand Down
5 changes: 3 additions & 2 deletions pyhdtoolkit/cpymadtools/coupling.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
Module with functions to perform ``MAD-X`` actions through a `~cpymad.madx.Madx` object, that
retate to betatron coupling in the machine.
"""

from collections.abc import Sequence

import numpy as np
Expand Down Expand Up @@ -78,10 +79,10 @@ def get_closest_tune_approach(
# Say we have set the LHC coupling knobs to 1e-3
dqmin = get_closest_tune_approach(
madx,
"lhc", # will find the knobs automatically
"lhc", # will find the knobs automatically
sequence="lhcb1",
telescopic_squeeze=True, # influences the knobs definition
run3=True, # influences the knobs definition (LHC Run 3)
run3=True, # influences the knobs definition (LHC Run 3)
)
# returns 0.001
"""
Expand Down
1 change: 1 addition & 0 deletions pyhdtoolkit/cpymadtools/lhc/_coupling.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
The functions below are betatron coupling utilities for the ``LHC``.
"""

import tfs
from cpymad.madx import Madx
from loguru import logger
Expand Down
2 changes: 2 additions & 0 deletions pyhdtoolkit/cpymadtools/lhc/_elements.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,13 @@
The functions below are utilities to install elements or markers in the ``LHC``.
"""

from cpymad.madx import Madx
from loguru import logger

_MAX_TRACKING_TOP_TURNS: int = 6600


def install_ac_dipole_as_kicker(
madx: Madx,
/,
Expand Down
6 changes: 4 additions & 2 deletions pyhdtoolkit/cpymadtools/lhc/_errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
The functions below are utilities to implement errors in elements of the ``LHC``.
"""

from collections.abc import Sequence

from cpymad.madx import Madx
Expand Down Expand Up @@ -122,7 +123,8 @@ def misalign_lhc_ir_quadrupoles(
.. code-block:: python
misalign_lhc_ir_quadrupoles(
madx, ips=[5],
madx,
ips=[5],
quadrupoles=[7, 8, 9, 10],
beam=1,
sides="RL",
Expand All @@ -140,7 +142,7 @@ def misalign_lhc_ir_quadrupoles(
beam=1,
sides="RL",
dy=1e-5, # ok too as cpymad converts this to a string first
dpsi="1E-3 + 8E-4 * TGAUSS(2.5)"
dpsi="1E-3 + 8E-4 * TGAUSS(2.5)",
)
"""
if any(ip not in (1, 2, 5, 8) for ip in ips):
Expand Down
1 change: 1 addition & 0 deletions pyhdtoolkit/cpymadtools/lhc/_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
_BEAM4: int = 4 # LHC beam 4 is special case
_VRF_THRESHOLD: int = 5000


def make_sixtrack_output(madx: Madx, /, energy: int) -> None:
"""
.. versionadded:: 0.15.0
Expand Down
2 changes: 2 additions & 0 deletions pyhdtoolkit/cpymadtools/lhc/_powering.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
The functions below are magnets or knobs powering utilities for the ``LHC``.
"""

from collections.abc import Sequence

from cpymad.madx import Madx
Expand All @@ -14,6 +15,7 @@
_QUAD_CIRCUIT_HAS_B: int = 7 # Q7 has a .b in the circuit name
_MAX_IR_QUAD_NUMBER: int = 11 # beyond Q11 are MQTs etc


def apply_lhc_colinearity_knob(madx: Madx, /, colinearity_knob_value: float = 0, ir: int | None = None) -> None:
"""
.. versionadded:: 0.15.0
Expand Down
1 change: 1 addition & 0 deletions pyhdtoolkit/cpymadtools/lhc/_queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
The functions below are settings query utilities for the ``LHC``.
"""

from collections.abc import Sequence

import tfs
Expand Down
5 changes: 4 additions & 1 deletion pyhdtoolkit/cpymadtools/lhc/_routines.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
The functions below are routines mimicking manipulations that would be done in the ``LHC``.
"""

import numpy as np
import tfs
from cpymad.madx import Madx
Expand Down Expand Up @@ -52,7 +53,9 @@ def do_kmodulation(
.. code-block:: python
tune_results = do_kmodulation(madx, ir=1, side="right", steps=100, stepsize=3e-8)
tune_results = do_kmodulation(
madx, ir=1, side="right", steps=100, stepsize=3e-8
)
"""
element = f"MQXA.1R{ir:d}" if side.lower() == "right" else f"MQXA.1L{ir:d}"
powering_variable = f"KTQX1.R{ir:d}" if side.lower() == "right" else f"KTQX1.L{ir:d}"
Expand Down
16 changes: 12 additions & 4 deletions pyhdtoolkit/cpymadtools/lhc/_setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
The functions below are setup utilities for the ``LHC``, to easily get simulations ready.
"""

from pathlib import Path

from cpymad.madx import Madx
Expand Down Expand Up @@ -59,7 +60,9 @@ def prepare_lhc_run2(
.. code-block:: python
madx = prepare_lhc_run2(
"/afs/cern.ch/eng/lhc/optics/runII/2018/PROTON/opticsfile.22", beam=2, stdout=True
"/afs/cern.ch/eng/lhc/optics/runII/2018/PROTON/opticsfile.22",
beam=2,
stdout=True,
)
"""
if use_b4 and beam != _BEAM_FOR_B4:
Expand Down Expand Up @@ -221,14 +224,19 @@ class LHCSetup:
.. code-block:: python
with LHCSetup(run=2, opticsfile="2018/PROTON/opticsfile.22", beam=2) as madx:
pass # do some stuff
pass # do some stuff
Get a Run 3 setup for beam 1, with a sliced sequence and muted output:
.. code-block:: python
with LHCSetup(run=3, opticsfile="R2022a_A30cmC30cmA10mL200cm.madx", slicefactor=4, stdout=False) as madx:
pass # do some stuff
with LHCSetup(
run=3,
opticsfile="R2022a_A30cmC30cmA10mL200cm.madx",
slicefactor=4,
stdout=False,
) as madx:
pass # do some stuff
"""

def __init__(
Expand Down
1 change: 1 addition & 0 deletions pyhdtoolkit/cpymadtools/lhc/_twiss.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
The functions below are twiss utilities for the ``LHC`` insertion regions.
"""

from collections.abc import Sequence

import tfs
Expand Down
13 changes: 7 additions & 6 deletions pyhdtoolkit/cpymadtools/matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Module with functions to perform ``MAD-X`` matchings through a `~cpymad.madx.Madx` object.
"""

from collections.abc import Sequence

from cpymad.madx import Madx
Expand Down Expand Up @@ -101,7 +102,7 @@ def match_tunes_and_chromaticities(
matching.match_tunes_and_chromaticities(
madx,
None, # this is not LHC or HLLHC
None, # this is not LHC or HLLHC
sequence="CAS3",
q1_target=6.335,
q2_target=6.29,
Expand Down Expand Up @@ -131,7 +132,7 @@ def match_tunes_and_chromaticities(
matching.match_tunes_and_chromaticities(
madx,
"lhc", # will find the knobs automatically
"lhc", # will find the knobs automatically
sequence="lhcb1",
q1_target=62.31,
q2_target=60.32,
Expand Down Expand Up @@ -242,7 +243,7 @@ def match_tunes(
matching.match_tunes(
madx,
None, # this is not LHC or HLLHC
None, # this is not LHC or HLLHC
sequence="CAS3",
q1_target=6.335,
q2_target=6.29,
Expand All @@ -268,7 +269,7 @@ def match_tunes(
matching.match_tunes(
madx,
"lhc", # will find the knobs automatically
"lhc", # will find the knobs automatically
sequence="lhcb1",
q1_target=62.31,
q2_target=60.32,
Expand Down Expand Up @@ -344,7 +345,7 @@ def match_chromaticities(
matching.match_chromaticities(
madx,
None, # this is not LHC or HLLHC
None, # this is not LHC or HLLHC
sequence="CAS3",
dq1_target=100,
dq2_target=100,
Expand All @@ -370,7 +371,7 @@ def match_chromaticities(
matching.match_chromaticities(
madx,
"lhc", # will find the knobs automatically
"lhc", # will find the knobs automatically
sequence="lhcb1",
dq1_target=2.0,
dq2_target=2.0,
Expand Down
11 changes: 9 additions & 2 deletions pyhdtoolkit/cpymadtools/ptc.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Module with functions to manipulate ``MAD-X`` ``PTC`` functionality through a
`~cpymad.madx.Madx` object.
"""

from collections.abc import Sequence
from pathlib import Path

Expand All @@ -20,6 +21,7 @@
_MAX_PTC_AMPDET_ORDER: int = 2
_MIN_PTC_AMPDET_ORDER: int = 1


def get_amplitude_detuning(
madx: Madx, /, order: int = 2, file: Path | str | None = None, fringe: bool = False, **kwargs
) -> tfs.TfsDataFrame:
Expand Down Expand Up @@ -428,8 +430,13 @@ def ptc_track_particle(
.. code-block:: python
tracks_dict = ptc_track_particle(
madx, nturns=10, initial_coordinates=(2e-4, 0, 1e-4, 0, 0, 0),
model=3, method=6, nst=3, exact=True
madx,
nturns=10,
initial_coordinates=(2e-4, 0, 1e-4, 0, 0, 0),
model=3,
method=6,
nst=3,
exact=True,
)
"""
logger.debug("Performing single particle PTC (thick) tracking")
Expand Down
1 change: 1 addition & 0 deletions pyhdtoolkit/cpymadtools/track.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Module with functions to manipulate ``MAD-X`` ``TRACK`` functionality through a
`~cpymad.madx.Madx` object.
"""

from collections.abc import Sequence

import pandas as pd
Expand Down
3 changes: 2 additions & 1 deletion pyhdtoolkit/cpymadtools/tune.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Module with functions to manipulate ``MAD-X`` functionality around the tune through
a `~cpymad.madx.Madx` object.
"""

import math
import sys
from pathlib import Path
Expand Down Expand Up @@ -250,7 +251,7 @@ def _get_dynap_string_rep(dynap_dframe: tfs.TfsDataFrame) -> str:
return string_rep


def _make_tune_groups(dynap_string_rep: str, dsigma: float = 1.0) -> list[list[dict[str, float]]]:
def _make_tune_groups(dynap_string_rep: str, dsigma: float = 1.0) -> list[list[dict[str, float]]]: # noqa: ARG001
"""
Creates appropriate tune points groups from the arcane string representation returned by
`~.tune._get_dynap_string_rep` based on starting amplitude and angle for each particle.
Expand Down
1 change: 1 addition & 0 deletions pyhdtoolkit/cpymadtools/twiss.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
Module with functions to manipulate ``MAD-X`` ``TWISS`` functionality through a
`~cpymad.madx.Madx` object.
"""

from collections.abc import Sequence

import tfs
Expand Down
1 change: 1 addition & 0 deletions pyhdtoolkit/cpymadtools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Module with utility functions to do mundane operations with `~cpymad.madx.Madx` objects.
"""

from pathlib import Path

import pandas as pd
Expand Down
2 changes: 2 additions & 0 deletions pyhdtoolkit/maths/stats_fitting.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Module implementing methods to find the best fit of statistical distributions to data.
"""

import warnings

import matplotlib.pyplot as plt # noqa: F401 | if omitted, get AttributeError: module 'matplotlib' has no attribute 'axes'
Expand Down Expand Up @@ -46,6 +47,7 @@ def set_distributions_dict(dist_dict: dict[st.rv_continuous, str]) -> None:
.. code-block:: python
import scipy.stats as st
tested_dists = {st.chi: "Chi", st.expon: "Exponential", st.laplace: "Laplace"}
set_distributions_dict(tested_dists)
"""
Expand Down
3 changes: 2 additions & 1 deletion pyhdtoolkit/maths/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,8 @@ def get_scaled_values_and_magnitude_string(
.. code-block:: python
import numpy as np
q = np.array([-330, 230, 430, -720, 750, -110, 410, -340, -950, -630])
q = np.array([-330, 230, 430, -720, 750, -110, 410, -340, -950, -630])
get_scaled_values_and_magnitude_string(q)
# returns (array([-3.3, 2.3, 4.3, -7.2, 7.5, -1.1, 4.1, -3.4, -9.5, -6.3]), '{-2}')
"""
Expand Down
1 change: 1 addition & 0 deletions pyhdtoolkit/models/beam.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
Module with ``pydantic`` models to validate and store data structures relative to particle beams.
"""

from math import sqrt

from pydantic import BaseModel
Expand Down
1 change: 1 addition & 0 deletions pyhdtoolkit/models/htc.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class HTCTaskSummary(BaseModel):
Class to encompass and validate a specific job's line in the ``condor_q`` output.
"""

# This is so pydantic accepts pendulum.DateTime as a validated type
model_config = ConfigDict(arbitrary_types_allowed=True)

Expand Down
Loading

0 comments on commit 249203f

Please sign in to comment.