Skip to content

Commit 1fca6d7

Browse files
authored
Plotting styles and logging utils (#98)
1 parent 8fc39f7 commit 1fca6d7

40 files changed

+623
-303
lines changed

docs/api/plotting.rst

+11
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,17 @@
33
Plotting
44
========
55

6+
7+
.. automodule:: pyhdtoolkit.plotting.styles
8+
:members:
9+
10+
11+
.. automodule:: pyhdtoolkit.plotting.styles.paper
12+
:members:
13+
14+
.. automodule:: pyhdtoolkit.plotting.styles.thesis
15+
:members:
16+
617
.. automodule:: pyhdtoolkit.plotting.aperture
718
:members:
819

docs/api/utils.rst

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ Utils
99
.. automodule:: pyhdtoolkit.utils.contexts
1010
:members:
1111

12-
.. automodule:: pyhdtoolkit.utils.defaults
13-
:members:
14-
1512
.. automodule:: pyhdtoolkit.utils.executors
1613
:members:
1714

1815
.. automodule:: pyhdtoolkit.utils.htc_monitor
1916
:members:
2017

18+
.. automodule:: pyhdtoolkit.utils.logging
19+
:members:
20+
2121
.. automodule:: pyhdtoolkit.utils._misc
2222
:members:
2323

docs/conf.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ def __call__(self, *args, **kwargs):
193193

194194
# Config for the matplotlib plot directive
195195
plot_formats = [("svg", 250)]
196-
plot_rcparams = pyhdtoolkit.utils.defaults.PLOT_PARAMS
197196

198197
# -- Options for HTML output ----------------------------------------------
199198

@@ -528,4 +527,5 @@ def __call__(self, *args, **kwargs):
528527
"tfs": ("https://pylhc.github.io/tfs/", None),
529528
"cpymad": ("https://hibtc.github.io/cpymad/", None),
530529
"optics_functions": ("https://pylhc.github.io/optics_functions/", None),
530+
"loguru": ("https://loguru.readthedocs.io/en/stable/", None),
531531
}

docs/quickstart.rst

+4-5
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ One can find many examples of the `~pyhdtoolkit.cpymadtools` apis' use in the :r
100100
Utilities
101101
^^^^^^^^^
102102

103-
The :ref:`utils <pyhdtoolkit-utils>` module contains useful functions to set up logging or
104-
plotting :ref:`defaults <utils-defaults>`, run external programs through the :ref:`command line <utils-cmdline>`,
103+
The :ref:`utils <pyhdtoolkit-utils>` module contains useful functions to set up (:ref:`logging <utils-logging>`, run external programs through the :ref:`command line <utils-cmdline>`,
105104
run your functions through :ref:`useful contexts <utils-contexts>`, easily wrap and :ref:`parallelise <utils-executors>`
106105
functions, or perform many convenient :ref:`operations <utils-operations>` on miscellaneous Python objects.
107106

@@ -125,12 +124,12 @@ Alternatively one can easily parallelise an I/O-intensive function through multi
125124
)
126125

127126
.. tip::
128-
A useful tidbit is this line which sets up the logging level for functions in the package:
127+
A useful tidbit is the following which sets up the logging level for functions in the package:
129128

130129
.. prompt:: python >>>
131130

132-
from pyhdtoolkit.utils import defaults
133-
defaults.config_logger(level="trace") # the lowest level used, will give ALL logging
131+
from pyhdtoolkit.utils import logging
132+
logging.config_logger(level="trace") # the lowest level used, will give ALL logging
134133

135134
Additional Helpers
136135
^^^^^^^^^^^^^^^^^^

docs/releases/v1.0.0.rst

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
-----
55

66
Release `1.0.0` brings many breaking changes.
7-
Be sure to read these release notes carefully before upgrading, as much of the code using ``pyhdtoolki`` will need to be adapted.
7+
Be sure to read these release notes carefully before upgrading, as much of the code using ``pyhdtoolkit`` will need to be adapted.
88

99
Breaking Changes
1010
~~~~~~~~~~~~~~~~
@@ -14,23 +14,28 @@ Breaking Changes
1414
* The `~pyhdtoolkit.plotting.helpers` module has been renamed to `~pyhdtoolkit.plotting.utils` for consistency.
1515
* The `~pyhdtoolkit.plotting.sbs.utils` module has been merged into `~pyhdtoolkit.plotting.utils`.
1616
* The `~pyhdtoolkit.cpymadtools.plotters.DynamicAperture` functionality has been completely removed.
17+
* The `~pyhdtoolkit.utils.defaults` module has been removed, and its functionality transferred in the relevant (new) places.
1718

1819
Enhancements
1920
~~~~~~~~~~~~
2021

2122
* The machine layout plotting functionality has been made public api in a dedicated module, `~pyhdtoolkit.plotting.layout`.
2223
* A new module has been added to the `~pyhdtoolkit.cpymadtools`, `~pyhdtoolkit.cpymadtools.setup`, with functions to conveniently set up a `~cpymad.madx.Madx` instance for either the Run 2 or Run 3 of the ``LHC``, also as a context manager.
2324
* A new module has been added in `~pyhdtoolkit.plotting`, `~pyhdtoolkit.plotting.layout`, to make public the API to plot a machine's element onto an axis.
25+
* A new module has been added in `~pyhdtoolkit.utils`, `~pyhdtoolkit.utils.logging`, to host the logging setup functionality that was previously in `~pyhdtoolkit.utils.defaults`.
26+
* A new module has been added in `~pyhdtoolkit.plotting`, `~pyhdtoolkit.plotting.styles`, with various useful styles definitions and the functionality to install them locally.
2427
* New function in `~pyhdtoolkit.cpymadtools.lhc`, `~pyhdtoolkit.cpymadtools.lhc.add_markers_around_lhc_ip`, to do exactly that, for high granularity of calculations around the IP point.
2528
* New function in `~pyhdtoolkit.cpymadtools.lhc`, `~pyhdtoolkit.cpymadtools.lhc.get_sizes_at_ip`, to compute horizontal and vertical beam sizes at a given IP through ``Ripken`` parameters, which will be accurate in the presence of coupling.
2629
* New function in `~pyhdtoolkit.plotting.utils`, `~pyhdtoolkit.plotting.utils.get_lhc_ips_positions`, to get a `dict` of different IP elements and their ``S`` coordinates.
2730
* New function in `~pyhdtoolkit.plotting.utils`, `~pyhdtoolkit.plotting.utils.draw_ip_locations`, to highlight the positions of given IPs onto the current axis as vertical lines, potentially also writing out their names.
2831
* The element plotting functionality handled by `~pyhdtoolkit.plotting.layout.plot_machine_layout` now determines reasonable default ``ylimits`` if none were provided. This allowed removing hardcoded default values of these limits, and should help get a good looking plot for any machine by default.
32+
* The `~pyhdtoolkit.cpymadtools.lhc.make_lhc_beams` function can now handle the ``bv`` flag in case the user is trying to set up for tracking with B2 (which needs loading **lhcb4** and a different bv flag).
33+
* The `config_logger` function, now in `~pyhdtoolkit.utils.logging`, now accepts custom formats and three default formats are provided in the module.
2934

3035
Maintenance
3136
~~~~~~~~~~~
3237

33-
* Documentation has been updated. Refer to the gallery for example uses.
38+
* Documentation has been updated for all new APIs. Refer to the gallery for example uses.
3439
* Tests have been adapted.
3540

3641
See `v1.0.0 release notes on GitHub <https://github.com/fsoubelet/PyhDToolkit/releases/tag/1.0.0>`_ and the `full changes since v0.21.0 <https://github.com/fsoubelet/PyhDToolkit/compare/0.21.0...1.0.0>`_.

examples/README.rst

+28-12
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,17 @@ This page contains a gallery showcasing either plotting functionality provided i
1010
submodules, or plots made from results of convenient functions available in `~pyhdtoolkit`.
1111

1212
.. important::
13-
The examples shown here are plotted with a customized but simple ``rcParams`` style. If one uses
14-
this package and sets their own preferences or uses their own **mplstyle**, the resulting plots
15-
might look significantly different.
13+
The examples shown here are plotted with a customized but simple ``rcParams`` style, simply for
14+
visibility in these galleries. If one uses this package and sets their own preferences or uses
15+
their own **mplstyle**, the resulting plots might look significantly different.
1616

17-
The package provides a plotting style in the `~pyhdtoolkit.utils.defaults` module, which should be
18-
compatible with the plotters. One can use the style in two ways:
17+
The package provides several plotting styles in the `~pyhdtoolkit.plotting.styles` submodules,
18+
which are tailored for good rendering in my LaTeX documents and are made for good compatibility
19+
with the various plotters in `~pyhdtoolkit.plotting`. These styles are described in the
20+
:ref:`styles <plotting-styles>` documentation section.
21+
22+
One can use them in two ways, shown below with as example the ``MEDIUM`` style defined in
23+
`pyhdtoolkit.plotting.styles.thesis`.
1924

2025
.. tabbed:: Import and Set the Style
2126

@@ -24,21 +29,32 @@ submodules, or plots made from results of convenient functions available in `~py
2429
.. prompt:: python >>>
2530

2631
from matplotlib import pyplot as plt
27-
from pyhdtoolkit.utils.defaults import PLOT_PARAMS
32+
from pyhdtoolkit.plotting.styles.thesis import MEDIUM
33+
34+
plt.rcParams.update(MEDIUM)
35+
# plotting code here
36+
37+
Or, for a temporary update of the ``rcParams``:
38+
39+
.. prompt:: python >>>
40+
41+
from matplotlib import pyplot as plt
42+
from pyhdtoolkit.plotting.styles.thesis import MEDIUM
2843

29-
plt.rcParams.update(PLOT_PARAMS)
44+
with plt.rc_context(MEDIUM):
45+
# Plotting code here
3046

31-
.. tabbed:: Install and Load the Style
47+
.. tabbed:: Install the Styles and Load One
3248

33-
Do a one-time install of the style as an **.mplstyle** file to import in `~matplotlib`:
49+
Do a one-time install of the styles as **.mplstyle** files to use in `~matplotlib`:
3450

3551
.. prompt:: python >>>
3652

3753
from matplotlib import pyplot as plt
38-
from pyhdtoolkit.utils import defaults
54+
from pyhdtoolkit.plotting.styles import install_mpl_styles
3955

40-
defaults.install_mpl_style() # only run this once
41-
plt.style.use("phd") # created file is 'phd.mplstyle'
56+
install_mpl_styles() # only run this once
57+
plt.style.use("thesis-medium") # loaded from created file 'thesis-medium.mplstyle'
4258

4359
In both cases, re-updating the ``rcParams`` later on will always overwrite these settings.
4460

examples/demo_acd_tracking_spectra.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@
2626
from cpymad.madx import Madx
2727

2828
from pyhdtoolkit.cpymadtools import lhc, matching, track
29-
from pyhdtoolkit.utils import defaults
29+
from pyhdtoolkit.plotting.styles import _SPHINX_GALLERY_PARAMS
30+
from pyhdtoolkit.utils import logging
3031

31-
defaults.config_logger(level="warning")
32-
plt.rcParams.update(defaults._SPHINX_GALLERY_PARAMS) # for readability of this tutorial
32+
logging.config_logger(level="warning")
33+
plt.rcParams.update(_SPHINX_GALLERY_PARAMS) # for readability of this tutorial
3334

3435
###############################################################################
3536
# Let's start by setting up the LHC in ``MAD-X``, in this case at top energy:

examples/demo_aperture.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616

1717
from pyhdtoolkit.cpymadtools import lhc
1818
from pyhdtoolkit.plotting.aperture import plot_aperture
19-
from pyhdtoolkit.utils import defaults
19+
from pyhdtoolkit.plotting.styles import _SPHINX_GALLERY_PARAMS
20+
from pyhdtoolkit.utils import logging
2021

21-
defaults.config_logger(level="warning")
22-
plt.rcParams.update(defaults._SPHINX_GALLERY_PARAMS) # for readability of this tutorial
22+
logging.config_logger(level="warning")
23+
plt.rcParams.update(_SPHINX_GALLERY_PARAMS) # for readability of this tutorial
2324

2425
###############################################################################
2526
# Setup a simple LHC simulation in ``MAD-X``, at injection energy (450 GeV)

examples/demo_beam_enveloppe.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919

2020
from pyhdtoolkit.models.beam import BeamParameters
2121
from pyhdtoolkit.plotting.envelope import plot_envelope, plot_stay_clear
22-
from pyhdtoolkit.utils import defaults
22+
from pyhdtoolkit.plotting.styles import _SPHINX_GALLERY_PARAMS
23+
from pyhdtoolkit.utils import logging
2324

24-
defaults.config_logger(level="warning")
25-
plt.rcParams.update(defaults._SPHINX_GALLERY_PARAMS) # for readability of this tutorial
25+
logging.config_logger(level="warning")
26+
plt.rcParams.update(_SPHINX_GALLERY_PARAMS) # for readability of this tutorial
2627

2728
###############################################################################
2829
# Define beam parameters for injection and top energy (1.9 GeV -> 19 GeV):

examples/demo_ir_errors.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
from cpymad.madx import Madx
2121

2222
from pyhdtoolkit.cpymadtools import errors, lhc, matching
23-
from pyhdtoolkit.utils import defaults
23+
from pyhdtoolkit.plotting.styles import _SPHINX_GALLERY_PARAMS
24+
from pyhdtoolkit.utils import logging
2425

25-
defaults.config_logger(level="warning")
26-
plt.rcParams.update(defaults._SPHINX_GALLERY_PARAMS) # for readability of this tutorial
26+
logging.config_logger(level="warning")
27+
plt.rcParams.update(_SPHINX_GALLERY_PARAMS) # for readability of this tutorial
2728

2829
###############################################################################
2930
# Setup a simple LHC simulation in ``MAD-X``, at injection energy (450 GeV)

examples/demo_lattice.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
from pyhdtoolkit.cpymadtools import lhc, matching, orbit
2020
from pyhdtoolkit.cpymadtools.generators import LatticeGenerator
2121
from pyhdtoolkit.plotting.lattice import plot_latwiss
22-
from pyhdtoolkit.utils import defaults
22+
from pyhdtoolkit.plotting.styles import _SPHINX_GALLERY_PARAMS
23+
from pyhdtoolkit.utils import logging
2324

24-
defaults.config_logger(level="warning")
25-
plt.rcParams.update(defaults._SPHINX_GALLERY_PARAMS) # for readability of this tutorial
25+
logging.config_logger(level="warning")
26+
plt.rcParams.update(_SPHINX_GALLERY_PARAMS) # for readability of this tutorial
2627

2728
###############################################################################
2829
# Let's start by generating a simple lattice and setup your simulation:

examples/demo_lhc_crossing_schemes.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,11 @@
1818

1919
from pyhdtoolkit.cpymadtools import lhc
2020
from pyhdtoolkit.plotting.crossing import plot_two_lhc_ips_crossings
21-
from pyhdtoolkit.utils import defaults
21+
from pyhdtoolkit.plotting.styles import _SPHINX_GALLERY_PARAMS
22+
from pyhdtoolkit.utils import logging
2223

23-
defaults.config_logger(level="warning")
24-
plt.rcParams.update(defaults._SPHINX_GALLERY_PARAMS) # for readability of this tutorial
24+
logging.config_logger(level="warning")
25+
plt.rcParams.update(_SPHINX_GALLERY_PARAMS) # for readability of this tutorial
2526

2627

2728
###############################################################################

examples/demo_lhc_rigid_waist_shift.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,11 @@
2828

2929
from pyhdtoolkit.cpymadtools import lhc, matching
3030
from pyhdtoolkit.plotting.lattice import plot_latwiss
31-
from pyhdtoolkit.utils import defaults
31+
from pyhdtoolkit.plotting.styles import _SPHINX_GALLERY_PARAMS
32+
from pyhdtoolkit.utils import logging
3233

33-
defaults.config_logger(level="warning")
34-
plt.rcParams.update(defaults._SPHINX_GALLERY_PARAMS) # for readability of this tutorial
34+
logging.config_logger(level="warning")
35+
plt.rcParams.update(_SPHINX_GALLERY_PARAMS) # for readability of this tutorial
3536

3637
###############################################################################
3738
# Showcasing the Waist Shift

examples/demo_machine_survey.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@
1616

1717
from pyhdtoolkit.cpymadtools.generators import LatticeGenerator
1818
from pyhdtoolkit.plotting.lattice import plot_machine_survey
19-
from pyhdtoolkit.utils import defaults
19+
from pyhdtoolkit.plotting.styles import _SPHINX_GALLERY_PARAMS
20+
from pyhdtoolkit.utils import logging
2021

21-
defaults.config_logger(level="warning")
22-
plt.rcParams.update(defaults._SPHINX_GALLERY_PARAMS) # for readability of this tutorial
22+
logging.config_logger(level="warning")
23+
plt.rcParams.update(_SPHINX_GALLERY_PARAMS) # for readability of this tutorial
2324

2425
###############################################################################
2526
# Generate a simple lattice and setup your simulation:

examples/demo_phase_space.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,11 @@
2626
plot_courant_snyder_phase_space,
2727
plot_courant_snyder_phase_space_colored,
2828
)
29-
from pyhdtoolkit.utils import defaults
29+
from pyhdtoolkit.plotting.styles import _SPHINX_GALLERY_PARAMS
30+
from pyhdtoolkit.utils import logging
3031

31-
defaults.config_logger(level="warning")
32-
plt.rcParams.update(defaults._SPHINX_GALLERY_PARAMS) # for readability of this tutorial
32+
logging.config_logger(level="warning")
33+
plt.rcParams.update(_SPHINX_GALLERY_PARAMS) # for readability of this tutorial
3334

3435
###############################################################################
3536
# Define some constants, generate a simple lattice and setup your simulation:

examples/demo_sbs_plotting.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,11 @@
1414
import tfs
1515

1616
from pyhdtoolkit.plotting.sbs import coupling, phase
17-
from pyhdtoolkit.utils import defaults
17+
from pyhdtoolkit.plotting.styles import _SPHINX_GALLERY_PARAMS
18+
from pyhdtoolkit.utils import logging
1819

19-
defaults.config_logger(level="warning")
20-
plt.rcParams.update(defaults._SPHINX_GALLERY_PARAMS) # for readability of this tutorial
20+
logging.config_logger(level="warning")
21+
plt.rcParams.update(_SPHINX_GALLERY_PARAMS) # for readability of this tutorial
2122
plt.rcParams.update({"text.usetex": True, "legend.fontsize": 16}) # for these specific plots
2223

2324
###############################################################################

examples/demo_stats_fitting.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,12 @@
2323
import scipy.stats as st
2424

2525
from pyhdtoolkit.maths import stats_fitting as fitting
26+
from pyhdtoolkit.plotting.styles import _SPHINX_GALLERY_PARAMS
2627
from pyhdtoolkit.plotting.utils import set_arrow_label
27-
from pyhdtoolkit.utils import defaults
28+
from pyhdtoolkit.utils import logging
2829

29-
defaults.config_logger(level="warning")
30-
plt.rcParams.update(defaults._SPHINX_GALLERY_PARAMS) # for readability of this tutorial
30+
logging.config_logger(level="warning")
31+
plt.rcParams.update(_SPHINX_GALLERY_PARAMS) # for readability of this tutorial
3132

3233
###############################################################################
3334
# We will for this example create a chi-square_ distribution with known parameters. As the

examples/demo_track_spectra.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,11 @@
1919
from cpymad.madx import Madx
2020

2121
from pyhdtoolkit.cpymadtools import lhc, track
22-
from pyhdtoolkit.utils import defaults
22+
from pyhdtoolkit.plotting.styles import _SPHINX_GALLERY_PARAMS
23+
from pyhdtoolkit.utils import logging
2324

24-
defaults.config_logger(level="warning")
25-
plt.rcParams.update(defaults._SPHINX_GALLERY_PARAMS) # for readability of this tutorial
25+
logging.config_logger(level="warning")
26+
plt.rcParams.update(_SPHINX_GALLERY_PARAMS) # for readability of this tutorial
2627

2728
###############################################################################
2829
# Let's start by setting up the LHC in ``MAD-X``, in this case at top energy:

examples/demo_tune_diagram.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
# sphinx_gallery_thumbnail_number = -1
1313
import matplotlib.pyplot as plt
1414

15+
from pyhdtoolkit.plotting.styles import _SPHINX_GALLERY_PARAMS
1516
from pyhdtoolkit.plotting.tune import plot_tune_diagram
16-
from pyhdtoolkit.utils import defaults
17+
from pyhdtoolkit.utils import logging
1718

18-
defaults.config_logger(level="warning")
19-
plt.rcParams.update(defaults._SPHINX_GALLERY_PARAMS) # for readability of this tutorial
19+
logging.config_logger(level="warning")
20+
plt.rcParams.update(_SPHINX_GALLERY_PARAMS) # for readability of this tutorial
2021

2122
###############################################################################
2223
# The tune diagram allows on to visualise resonance lines up to certain orders,

0 commit comments

Comments
 (0)