Skip to content

Commit b4ff24d

Browse files
authored
Lint and tweak beams (#110)
* first autolint set * new round for dict, list, tuple * some more for optional, union * fix implicit optionals * refix many imports rules * fix use of literal dicts * propagate error change here * fix trailing whitespaces * miscellaneous * fix error messages not declared before raising * fix magic values use in comparisons * miscellaneous * place *args correctly * miscellaneous * use dict literal * miscellaneous * no implicit namespace * correctly propagate kwargs * clean up logic * message outside raise * message outside raise * trade map for list comprehension * cleaner checks * format and last lints * no mypy * no interrogate * rename to nemitt_[xy], accept old and let user override npart and sigma_e * formatting * remove trailing whitespaces * pytest parametrize expects tuples * dict membership test does not need .keys() * mpl * names * unecessary list comprehensions * fixtures want lists of tuples * provide 'match' argument to every pytest.raises, normalize error messages * minimal linting here * remove old helper functions * bump version for minor release * release 1.6.0 file * added to this list
1 parent fc5754e commit b4ff24d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

100 files changed

+1092
-1066
lines changed

Makefile

+5-3
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ format:
8484
@python -m isort examples && black -l 95 examples
8585

8686
install: format clean
87-
@echo "Installing with $(D)pip$(E) in the current environment."
88-
@python -m pip install . -v
87+
@echo "Installing (editable) with $(D)pip$(E) in the current environment."
88+
@python -m pip install --editable . -v
8989

9090
lines: format
9191
@tokei .
@@ -113,5 +113,7 @@ alltests:
113113

114114
# Catch-all unknow targets without returning an error. This is a POSIX-compliant syntax.
115115
.DEFAULT:
116-
@echo "Make caught an invalid target! See help output below for available targets."
116+
@echo "Make caught an invalid target."
117+
@echo "See help output below for available targets."
118+
@echo ""
117119
@make help

docs/conf.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@
125125
"sphinx.ext.coverage", # Collect doc coverage stats
126126
"sphinx.ext.doctest", # Test snippets in the documentation
127127
"sphinx.ext.githubpages", # Publish HTML docs in GitHub Pages
128-
"sphinx.ext.intersphinx", # Link to other projects documentation
128+
"sphinx.ext.intersphinx", # Link to other projects' documentation
129129
"sphinx.ext.mathjax", # Render math via JavaScript
130130
"sphinx.ext.napoleon", # Support for NumPy and Google style docstrings
131131
"sphinx.ext.todo", # Support for todo items
@@ -214,7 +214,7 @@
214214
#
215215
html_static_path = ["_static"]
216216

217-
# A dictionary of values to pass into the template engines context for all
217+
# A dictionary of values to pass into the template engine's context for all
218218
# pages. Single values can also be put in this dictionary using the
219219
# -A command-line option of sphinx-build.
220220
html_context = {

docs/release.rst

+8
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ Release Notes
33

44
The full list of releases can be found in the GitHub repository's `releases page <https://github.com/fsoubelet/PyhDToolkit/releases>`_.
55

6+
Version 1.6.0
7+
-------------
8+
9+
.. toctree::
10+
:maxdepth: 2
11+
12+
releases/v1.6.0
13+
614
Version 1.5.0
715
-------------
816

docs/releases/v1.6.0.rst

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
.. _release_1.6.0:
2+
3+
1.6.0
4+
-----
5+
6+
Release `1.6.0` brings a few changes, and a great amount of maintenance fixes to the package.
7+
8+
Enhancements
9+
~~~~~~~~~~~~
10+
11+
* The `pyhdtoolkit.cpymadtools.lhc.make_lhc_beams` function now accepts the user providing values for `npart` and `sige`. The previously hard-coded values are now used as defaults.
12+
13+
Changes
14+
~~~~~~~
15+
16+
* The `pyhdtoolkit.cpymadtools.lhc.make_lhc_beams` function now expects `nemitt_[xy]` arguments instead of the previous `emittance_[xy]`. Providing the previous name is still accepted but will raise a warning.
17+
18+
Maintenance
19+
~~~~~~~~~~~
20+
21+
* A great amount of linting has been done to ensure best practices in every line of code.
22+
* Switched all relevant type hints to use the latest practices.
23+
* Fixed the `__all__` exports in the `__init__.py` files.
24+
* Moved type-hint relevant imports in a `TYPE_CHECKING` block to lighten import times.
25+
26+
See `v1.6.0 release notes on GitHub <https://github.com/fsoubelet/PyhDToolkit/releases/tag/1.6.0>`_ and the `full changes since v1.5.0 <https://github.com/fsoubelet/PyhDToolkit/compare/1.5.0...1.6.0>`_.

examples/demo_acd_tracking_spectra.py

+7-8
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,17 @@
66
AC Dipole Driven Tracking Spectrum
77
==================================
88
9-
This example shows how to use the `~.lhc.install_ac_dipole_as_kicker` and
10-
`~.track.track_single_particle` function to track a particle with the
9+
This example shows how to use the `~.lhc.install_ac_dipole_as_kicker` and
10+
`~.track.track_single_particle` function to track a particle with the
1111
``TRACK`` command of ``MAD-X``, and visualise its coordinates and spectrum.
1212
13-
In this example we will use the LHC lattice to illustrate the ACD tracking
13+
In this example we will use the LHC lattice to illustrate the ACD tracking
1414
workflow when using `~pyhdtoolkit.cpymadtools`.
1515
1616
.. note::
17-
This is very similar to the :ref:`free tracking example <demo-free-tracking>`
18-
with the difference that there is special care to take to install the AC Dipole
19-
element. It is recommended to read that tutorial first as this one will focus
17+
This is very similar to the :ref:`free tracking example <demo-free-tracking>`
18+
with the difference that there is special care to take to install the AC Dipole
19+
element. It is recommended to read that tutorial first as this one will focus
2020
on the specificities of the AC Dipole setup.
2121
2222
.. important::
@@ -33,7 +33,6 @@
3333
import matplotlib.pyplot as plt
3434
import numpy as np
3535
import pandas as pd
36-
3736
from cpymad.madx import Madx
3837

3938
from pyhdtoolkit.cpymadtools import lhc, matching, track
@@ -45,7 +44,7 @@
4544

4645
###############################################################################
4746
# Let's start by setting up the LHC in ``MAD-X``, in this case at top energy.
48-
# To understand the function below have a look at the :ref:`lhc setup example
47+
# To understand the function below have a look at the :ref:`lhc setup example
4948
# <demo-lhc-setup>`.
5049

5150
madx: Madx = lhc.prepare_lhc_run3(

examples/demo_aperture.py

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
Accelerator Aperture
77
====================
88
9-
This example shows how to use the `~.plotting.aperture.plot_aperture` and
9+
This example shows how to use the `~.plotting.aperture.plot_aperture` and
1010
`~.plotting.aperture.plot_physical_apertures` functions to visualise the
1111
available aperture in your machine, with the LHC used for this example.
1212
@@ -23,7 +23,6 @@
2323
"""
2424

2525
import matplotlib.pyplot as plt
26-
2726
from cpymad.madx import Madx
2827

2928
from pyhdtoolkit.cpymadtools import lhc
@@ -35,7 +34,7 @@
3534
plt.rcParams.update(_SPHINX_GALLERY_PARAMS) # for readability of this tutorial
3635

3736
###############################################################################
38-
# Let's start by setting up the LHC in ``MAD-X``, in this case at injection
37+
# Let's start by setting up the LHC in ``MAD-X``, in this case at injection
3938
# optics and energy. To understand the function below have a look at the
4039
# :ref:`lhc setup example <demo-lhc-setup>`.
4140

@@ -86,7 +85,7 @@
8685

8786

8887
###############################################################################
89-
# We can also go for a different type of aperture plot, which tries to give
88+
# We can also go for a different type of aperture plot, which tries to give
9089
# the elements' real physical apertures, with the `~.plotting.aperture.plot_physical_apertures`
9190
# function:
9291

examples/demo_beam_enveloppe.py

+5-6
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
# sphinx_gallery_thumbnail_number = 1
1515
import matplotlib.pyplot as plt
1616
import numpy as np
17-
1817
from cpymad.madx import Madx
1918

2019
from pyhdtoolkit.plotting.envelope import plot_beam_envelope
@@ -59,7 +58,7 @@
5958
madx.input(
6059
f"""
6160
circum = {circumference};
62-
n_cells = {n_cells}; ! Number of cells
61+
n_cells = {n_cells}; ! Number of cells
6362
lcell = {l_cell};
6463
lq = {l_quad}; ! Length of a quadrupole
6564
ldip = {l_bend};
@@ -71,14 +70,14 @@
7170
mb: sbend, l=ldip, angle=2.0*pi/(4*n_cells), apertype=ellipse, aperture={{{h_gap_dipole}, {v_gap_dipole}}};
7271
f = {f_m};
7372
74-
! Define quadrupoles as multipoles
75-
qf: multipole, knl:={{0,1/f+qtrim_f}};
73+
! Define quadrupoles as multipoles
74+
qf: multipole, knl:={{0,1/f+qtrim_f}};
7675
qd: multipole, knl:={{0,-1/f+qtrim_d}};
77-
qf: quadrupole, l=lq, K1:=1/f/lq + qtrim_f/lq, apertype=ellipse, aperture={{{r_quadrupole}, {r_quadrupole}}};
76+
qf: quadrupole, l=lq, K1:=1/f/lq + qtrim_f/lq, apertype=ellipse, aperture={{{r_quadrupole}, {r_quadrupole}}};
7877
qd: quadrupole, l=lq, K1:=-1/f/lq + qtrim_d/lq, apertype=ellipse, aperture={{{r_quadrupole}, {r_quadrupole}}};
7978
8079
! Define the sextupoles as multipole
81-
! ATTENTION: must use knl:= and NOT knl= to match later!
80+
! ATTENTION: must use knl:= and NOT knl= to match later!
8281
lsex = 0.00001; ! dummy length, only used in the sequence
8382
msf: multipole, knl:={{0,0,ksf}};
8483
msd: multipole, knl:={{0,0,ksd}};

examples/demo_ir_errors.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
to assign magnet errors in the Insertion Region magnets of the LHC.
1111
1212
.. warning::
13-
The implementation of this function makes it valid only for LHC IP IRs, which are
13+
The implementation of this function makes it valid only for LHC IP IRs, which are
1414
1, 2, 5 and 8. Other IRs have different layouts that are incompatible.
1515
1616
.. important::
@@ -27,7 +27,6 @@
2727
# sphinx_gallery_thumbnail_number = 1
2828
import matplotlib.pyplot as plt
2929
import numpy as np
30-
3130
from cpymad.madx import Madx
3231

3332
from pyhdtoolkit.cpymadtools import lhc, matching
@@ -38,7 +37,7 @@
3837
plt.rcParams.update(_SPHINX_GALLERY_PARAMS) # for readability of this tutorial
3938

4039
###############################################################################
41-
# Let's start by setting up the LHC in ``MAD-X``, in this case at injection
40+
# Let's start by setting up the LHC in ``MAD-X``, in this case at injection
4241
# optics and energy. To understand the function below have a look at the
4342
# :ref:`lhc setup example <demo-lhc-setup>`.
4443

examples/demo_lattice.py

-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
of several parameters to control the plot on the example case of the LHC.
1414
"""
1515
import matplotlib.pyplot as plt
16-
1716
from cpymad.madx import Madx
1817

1918
from pyhdtoolkit.cpymadtools import lhc, matching

examples/demo_lhc_crossing_schemes.py

+1-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
This is very LHC-specific and will not work with other machines.
1414
"""
1515
import matplotlib.pyplot as plt
16-
1716
from cpymad.madx import Madx
1817

1918
from pyhdtoolkit.cpymadtools import lhc
@@ -35,7 +34,7 @@
3534

3635
###############################################################################
3736
# Let's explicitely re-cycle both sequences to avoid having IR1 split at beginning
38-
# and end of lattice. Note that it is important to re-cycle both sequences from
37+
# and end of lattice. Note that it is important to re-cycle both sequences from
3938
# the same points for the plots later on.
4039

4140
lhc.re_cycle_sequence(madx, sequence="lhcb1", start="IP3")

examples/demo_lhc_rigid_waist_shift.py

+16-17
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,18 @@
66
LHC Rigid Waist Shift
77
=====================
88
9-
This example shows how to use the `~.lhc.apply_lhc_rigidity_waist_shift_knob`
10-
function to force a waist shift at a given IP and break the symmetry of the
11-
:math:`\\beta`-functions in the Interaction Region. This is done by
9+
This example shows how to use the `~.lhc.apply_lhc_rigidity_waist_shift_knob`
10+
function to force a waist shift at a given IP and break the symmetry of the
11+
:math:`\\beta`-functions in the Interaction Region. This is done by
1212
over-powering one triplet knob and under-powering the other, by the same
1313
powering delta.
1414
1515
In :cite:t:`PRAB:Soubelet:Rigid_Waist_Shift_Method_Local_Coupling_Correction_LHC_IR`
1616
(2023) one can find out about studies and achievements at the LHC done with the Rigid
1717
Waist Shift.
1818
19-
We will do a comparison of the interaction region situation before and after
20-
applying a rigid waist shift, and look in more details at the waist shift
19+
We will do a comparison of the interaction region situation before and after
20+
applying a rigid waist shift, and look in more details at the waist shift
2121
itself.
2222
2323
.. note::
@@ -36,13 +36,12 @@
3636
builds, but you can use any branch you want.
3737
"""
3838
# sphinx_gallery_thumbnail_number = 3
39-
from collections import namedtuple
4039
from multiprocessing import cpu_count
40+
from typing import NamedTuple
4141

4242
import matplotlib.pyplot as plt
4343
import numpy as np
4444
import tfs
45-
4645
from cpymad.madx import Madx
4746
from joblib import Parallel, delayed
4847

@@ -325,28 +324,28 @@
325324
# We can use the above to determine these values for different knob settings.
326325
# First, let's define some structures and functions.
327326

328-
Waist = namedtuple("Waist", ["x", "y"])
329-
BetasIP = namedtuple("Betas", ["x", "y"])
330-
Result = namedtuple("Result", ["waists", "betas"])
327+
Waist = NamedTuple("Waist", ["x", "y"])
328+
BetasIP = NamedTuple("Betas", ["x", "y"])
329+
Result = NamedTuple("Result", ["waists", "betas"])
331330

332331

333332
def find_waists(current_twiss: tfs.TfsDataFrame, initial_twiss: tfs.TfsDataFrame) -> Waist:
334333
initial = initial_twiss.copy()
335-
ip_s = current_twiss.S[f"IP1"]
334+
ip_s = current_twiss.S["IP1"]
336335
slimits = (ip_s - 10, ip_s + 10)
337336

338337
around_ip = current_twiss[current_twiss.S.between(*slimits)]
339338
initial = initial[initial.S.between(*slimits)].copy()
340-
hor_waist_location = around_ip.S[around_ip.BETX == around_ip.BETX.min()].iloc[0]
341-
ver_waist_location = around_ip.S[around_ip.BETY == around_ip.BETY.min()].iloc[0]
339+
hor_waist_location = around_ip.S[around_ip.BETX.min() == around_ip.BETX].iloc[0]
340+
ver_waist_location = around_ip.S[around_ip.BETY.min() == around_ip.BETY].iloc[0]
342341
initial = initial_twiss.copy()
343-
ip_s = current_twiss.S[f"IP1"]
342+
ip_s = current_twiss.S["IP1"]
344343
slimits = (ip_s - 10, ip_s + 10)
345344

346345
around_ip = current_twiss[current_twiss.S.between(*slimits)]
347346
initial = initial[initial.S.between(*slimits)].copy()
348-
hor_waist_location = around_ip.S[around_ip.BETX == around_ip.BETX.min()].iloc[0]
349-
ver_waist_location = around_ip.S[around_ip.BETY == around_ip.BETY.min()].iloc[0]
347+
hor_waist_location = around_ip.S[around_ip.BETX.min() == around_ip.BETX].iloc[0]
348+
ver_waist_location = around_ip.S[around_ip.BETY.min() == around_ip.BETY].iloc[0]
350349
return Waist(ip_s - hor_waist_location, ip_s - ver_waist_location)
351350

352351

@@ -363,7 +362,7 @@ def simulation(knob_value: float) -> Result:
363362
run=3, opticsfile="R2022a_A30cmC30cmA10mL200cm.madx", slicefactor=4, stdout=False
364363
) as madx:
365364
lhc.add_markers_around_lhc_ip(
366-
madx, sequence=f"lhcb1", ip=1, n_markers=1000, interval=0.001
365+
madx, sequence="lhcb1", ip=1, n_markers=1000, interval=0.001
367366
)
368367
ref_twiss = twiss.get_twiss_tfs(madx)
369368
lhc.apply_lhc_rigidity_waist_shift_knob(madx, knob_value, ir=1)

examples/demo_lhc_setup.py

+4-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
===============
88
99
These examples show how to use the functions in `pyhdtoolkit.cpymadtools.lhc._setup`
10-
managers to easily and quickly set up LHC simulations. These are exported and
10+
managers to easily and quickly set up LHC simulations. These are exported and
1111
available at the level of the `pyhdtoolkit.cpymadtools.lhc` module.
1212
1313
.. note::
@@ -26,7 +26,6 @@
2626
"""
2727
# sphinx_gallery_thumbnail_number = 4
2828
import matplotlib.pyplot as plt
29-
3029
from cpymad.madx import Madx
3130

3231
from pyhdtoolkit.cpymadtools import coupling, lhc, twiss
@@ -70,7 +69,7 @@
7069
# to set up the LHC simulations quickly and effortlessly:
7170
# `~.cpymadtools.lhc._setup.setup_lhc.prepare_lhc_run2` and
7271
# `~.cpymadtools.lhc._setup.setup_lhc.prepare_lhc_run3`.
73-
#
72+
#
7473
# They both return a `cpyamad.Madx` instance with the desired LHC sequence and
7574
# optics loaded, beams defined for both ``lhcb1`` and ``lhcb2`` sequences,
7675
# potentially sliced lattices etc. The very minimum required at function call
@@ -126,7 +125,7 @@
126125

127126
###############################################################################
128127
# Notice we don't need to call ``madx.exit()`` as the context manager takes care
129-
# of that.
128+
# of that.
130129
#
131130
# These quick setups, with context manager option, allow to do quick "one-shot"
132131
# simulations. For example, one can very quickly compare beam sizes around say
@@ -171,7 +170,7 @@
171170
# We'll need to call these to have aperture limitations
172171
madx.call("lhc/aperture.b1.madx")
173172
madx.call("lhc/aper_tol.b1.madx")
174-
173+
175174
df = twiss.get_twiss_tfs(madx)
176175
ips = get_lhc_ips_positions(df)
177176
limits = (ips["IP5"] - 350, ips["IP5"] + 350)

examples/demo_machine_survey.py

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
"""
1212
# sphinx_gallery_thumbnail_number = 2
1313
import matplotlib.pyplot as plt
14-
1514
from cpymad.madx import Madx
1615

1716
from pyhdtoolkit.cpymadtools._generators import LatticeGenerator

examples/demo_phase_space.py

-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
# sphinx_gallery_thumbnail_number = 2
1818
import matplotlib.pyplot as plt
1919
import numpy as np
20-
2120
from cpymad.madx import Madx
2221

2322
from pyhdtoolkit.cpymadtools._generators import LatticeGenerator

0 commit comments

Comments
 (0)