Skip to content

Commit a929804

Browse files
authoredNov 8, 2024··
Fix docstrings in the acceptance.py module (#855)
* fic docstrings in acceptance.py module * typos * black
1 parent 986fd9e commit a929804

File tree

1 file changed

+74
-73
lines changed

1 file changed

+74
-73
lines changed
 

‎pyat/at/acceptance/acceptance.py

+74-73
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,6 @@
11
"""Acceptance computation"""
22

3-
import numpy
4-
from .boundary import GridMode
5-
6-
# noinspection PyProtectedMember
7-
from .boundary import boundary_search
8-
from typing import Optional, Sequence
9-
import multiprocessing
10-
from ..lattice import Lattice, Refpts, frequency_control
11-
3+
from __future__ import annotations
124

135
__all__ = [
146
"get_acceptance",
@@ -18,24 +10,35 @@
1810
"get_momentum_acceptance",
1911
]
2012

13+
import multiprocessing
14+
from typing import Sequence
15+
16+
import numpy as np
17+
18+
from .boundary import GridMode
19+
20+
# noinspection PyProtectedMember
21+
from .boundary import boundary_search
22+
from ..lattice import Lattice, Refpts, frequency_control
23+
2124

2225
@frequency_control
2326
def get_acceptance(
2427
ring: Lattice,
2528
planes,
2629
npoints,
2730
amplitudes,
28-
nturns: Optional[int] = 1024,
29-
refpts: Optional[Refpts] = None,
30-
dp: Optional[float] = None,
31+
nturns: int | None = 1024,
32+
refpts: Refpts | None = None,
33+
dp: float | None = None,
3134
offset: Sequence[float] = None,
3235
bounds=None,
33-
grid_mode: Optional[GridMode] = GridMode.RADIAL,
34-
use_mp: Optional[bool] = False,
35-
verbose: Optional[bool] = True,
36-
divider: Optional[int] = 2,
37-
shift_zero: Optional[float] = 1.0e-6,
38-
start_method: Optional[str] = None,
36+
grid_mode: GridMode | None = GridMode.RADIAL,
37+
use_mp: bool | None = False,
38+
verbose: bool | None = True,
39+
divider: int | None = 2,
40+
shift_zero: float | None = 1.0e-6,
41+
start_method: str | None = None,
3942
):
4043
# noinspection PyUnresolvedReferences
4144
r"""Computes the acceptance at ``repfts`` observation points
@@ -57,7 +60,7 @@ def get_acceptance(
5760
dp: static momentum offset
5861
offset: initial orbit. Default: closed orbit
5962
bounds: defines the tracked range: range=bounds*amplitude.
60-
It can be use to select quadrants. For example, default values are:
63+
It can be used to select quadrants. For example, default values are:
6164
6265
* :py:attr:`.GridMode.CARTESIAN`: ((-1, 1), (0, 1))
6366
* :py:attr:`GridMode.RADIAL/RECURSIVE <.GridMode.RADIAL>`: ((0, 1),
@@ -76,9 +79,9 @@ def get_acceptance(
7679
start_method: Python multiprocessing start method. The default
7780
``None`` uses the python default that is considered safe.
7881
Available parameters: ``'fork'``, ``'spawn'``, ``'forkserver'``.
79-
The default for linux is ``'fork'``, the default for MacOS and
80-
Windows is ``'spawn'``. ``'fork'`` may used for MacOS to speed-up
81-
the calculation or to solve runtime errors, however it is
82+
The default for linux is ``'fork'``, the default for macOS and
83+
Windows is ``'spawn'``. ``'fork'`` may be used on macOS to speed up
84+
the calculation or to solve runtime errors, however it is
8285
considered unsafe.
8386
8487
Returns:
@@ -91,9 +94,9 @@ def get_acceptance(
9194
9295
Examples:
9396
94-
>>> bf,sf,gf = ring.get_acceptance(planes, npoints, amplitudes)
95-
>>> plt.plot(*gf,'.')
96-
>>> plt.plot(*sf,'.')
97+
>>> bf, sf, gf = ring.get_acceptance(planes, npoints, amplitudes)
98+
>>> plt.plot(*gf, ".")
99+
>>> plt.plot(*sf, ".")
97100
>>> plt.plot(*bf)
98101
>>> plt.show()
99102
@@ -114,7 +117,7 @@ def get_acceptance(
114117

115118
if verbose:
116119
nproc = multiprocessing.cpu_count()
117-
print("\n{0} cpu found for acceptance calculation".format(nproc))
120+
print(f"\n{nproc} cpu found for acceptance calculation")
118121
if use_mp:
119122
nprocu = nproc
120123
print("Multi-process acceptance calculation selected...")
@@ -125,27 +128,25 @@ def get_acceptance(
125128
print("Single process acceptance calculation selected...")
126129
if nproc > 1:
127130
print("Consider use_mp=True for parallelized computations")
128-
np = numpy.atleast_1d(npoints)
131+
npts = np.atleast_1d(npoints)
129132
na = 2
130-
if len(np) == 2:
131-
na = np[1]
132-
npp = numpy.prod(npoints)
133-
rpp = 2 * numpy.ceil(numpy.log2(np[0])) * numpy.ceil(na / nprocu)
133+
if len(npts) == 2:
134+
na = npts[1]
135+
npp = np.prod(npoints)
136+
rpp = 2 * np.ceil(np.log2(npts[0])) * np.ceil(na / nprocu)
134137
mpp = npp / nprocu
135138
if rpp > mpp:
136139
cond = grid_mode is GridMode.RADIAL or grid_mode is GridMode.CARTESIAN
137140
else:
138141
cond = grid_mode is GridMode.RECURSIVE
139142
if rpp > mpp and not cond:
140-
print("The estimated load for grid mode is {0}".format(mpp))
141-
print("The estimated load for recursive mode is {0}".format(rpp))
142-
print(
143-
"{0} or {1} is recommended".format(GridMode.RADIAL, GridMode.CARTESIAN)
144-
)
143+
print(f"The estimated load for grid mode is {mpp}")
144+
print(f"The estimated load for recursive mode is {rpp}")
145+
print(f"{GridMode.RADIAL} or {GridMode.CARTESIAN} is recommended")
145146
elif rpp < mpp and not cond:
146-
print("The estimated load for grid mode is {0}".format(mpp))
147-
print("The estimated load for recursive mode is {0}".format(rpp))
148-
print("{0} is recommended".format(GridMode.RECURSIVE))
147+
print(f"The estimated load for grid mode is {mpp}")
148+
print(f"The estimated load for recursive mode is {rpp}")
149+
print(f"{GridMode.RECURSIVE} is recommended")
149150

150151
b, s, g = boundary_search(
151152
ring,
@@ -172,16 +173,16 @@ def get_1d_acceptance(
172173
plane: str,
173174
resolution: float,
174175
amplitude: float,
175-
nturns: Optional[int] = 1024,
176-
refpts: Optional[Refpts] = None,
177-
dp: Optional[float] = None,
176+
nturns: int | None = 1024,
177+
refpts: Refpts | None = None,
178+
dp: float | None = None,
178179
offset: Sequence[float] = None,
179-
grid_mode: Optional[GridMode] = GridMode.RADIAL,
180-
use_mp: Optional[bool] = False,
181-
verbose: Optional[bool] = False,
182-
divider: Optional[int] = 2,
183-
shift_zero: Optional[float] = 1.0e-6,
184-
start_method: Optional[str] = None,
180+
grid_mode: GridMode | None = GridMode.RADIAL,
181+
use_mp: bool | None = False,
182+
verbose: bool | None = False,
183+
divider: int | None = 2,
184+
shift_zero: float | None = 1.0e-6,
185+
start_method: str | None = None,
185186
):
186187
r"""Computes the 1D acceptance at ``refpts`` observation points
187188
@@ -208,7 +209,7 @@ def get_1d_acceptance(
208209
* :py:attr:`.GridMode.RADIAL`: full [:math:`\:r, \theta\:`] grid
209210
* :py:attr:`.GridMode.RECURSIVE`: radial recursive search
210211
use_mp: Use python multiprocessing (:py:func:`.patpass`,
211-
default use :py:func:`.lattice_pass`). In case multi-processing
212+
default use :py:func:`.lattice_pass`). In case multiprocessing
212213
is not enabled, ``grid_mode`` is forced to
213214
:py:attr:`.GridMode.RECURSIVE` (most efficient in single core)
214215
verbose: Print out some information
@@ -218,15 +219,15 @@ def get_1d_acceptance(
218219
start_method: Python multiprocessing start method. The default
219220
``None`` uses the python default that is considered safe.
220221
Available parameters: ``'fork'``, ``'spawn'``, ``'forkserver'``.
221-
The default for linux is ``'fork'``, the default for MacOS and
222-
Windows is ``'spawn'``. ``'fork'`` may used for MacOS to speed-up
223-
the calculation or to solve runtime errors, however it is considered
222+
The default for linux is ``'fork'``, the default for macOS and
223+
Windows is ``'spawn'``. ``'fork'`` may be used on macOS to speed up
224+
the calculation or to solve runtime errors, however it is considered
224225
unsafe.
225226
226227
Returns:
227228
boundary: (len(refpts),2) array: 1D acceptance
228-
tracked: (n,) array: Coordinates of tracked particles
229229
survived: (n,) array: Coordinates of surviving particles
230+
tracked: (n,) array: Coordinates of tracked particles
230231
231232
In case of multiple ``tracked`` and ``survived`` are lists of arrays,
232233
with one array per ref. point.
@@ -244,10 +245,10 @@ def get_1d_acceptance(
244245
"""
245246
if not use_mp:
246247
grid_mode = GridMode.RECURSIVE
247-
assert len(numpy.atleast_1d(plane)) == 1, "1D acceptance: single plane required"
248-
assert numpy.isscalar(resolution), "1D acceptance: scalar args required"
249-
assert numpy.isscalar(amplitude), "1D acceptance: scalar args required"
250-
npoint = numpy.ceil(amplitude / resolution)
248+
assert len(np.atleast_1d(plane)) == 1, "1D acceptance: single plane required"
249+
assert np.isscalar(resolution), "1D acceptance: scalar args required"
250+
assert np.isscalar(amplitude), "1D acceptance: scalar args required"
251+
npoint = np.ceil(amplitude / resolution)
251252
if grid_mode is not GridMode.RECURSIVE:
252253
assert (
253254
npoint > 1
@@ -268,7 +269,7 @@ def get_1d_acceptance(
268269
shift_zero=shift_zero,
269270
offset=offset,
270271
)
271-
return numpy.squeeze(b), s, g
272+
return np.squeeze(b), s, g
272273

273274

274275
def get_horizontal_acceptance(
@@ -298,7 +299,7 @@ def get_horizontal_acceptance(
298299
* :py:attr:`.GridMode.RADIAL`: full [:math:`\:r, \theta\:`] grid
299300
* :py:attr:`.GridMode.RECURSIVE`: radial recursive search
300301
use_mp: Use python multiprocessing (:py:func:`.patpass`,
301-
default use :py:func:`.lattice_pass`). In case multi-processing
302+
default use :py:func:`.lattice_pass`). In case multiprocessing
302303
is not enabled, ``grid_mode`` is forced to
303304
:py:attr:`.GridMode.RECURSIVE` (most efficient in single core)
304305
verbose: Print out some information
@@ -308,15 +309,15 @@ def get_horizontal_acceptance(
308309
start_method: Python multiprocessing start method. The default
309310
``None`` uses the python default that is considered safe.
310311
Available parameters: ``'fork'``, ``'spawn'``, ``'forkserver'``.
311-
The default for linux is ``'fork'``, the default for MacOS and
312-
Windows is ``'spawn'``. ``'fork'`` may used for MacOS to speed-up
313-
the calculation or to solve runtime errors, however it is considered
312+
The default for linux is ``'fork'``, the default for macOS and
313+
Windows is ``'spawn'``. ``'fork'`` may be used on macOS to speed up
314+
the calculation or to solve runtime errors, however it is considered
314315
unsafe.
315316
316317
Returns:
317318
boundary: (len(refpts),2) array: 1D acceptance
318-
tracked: (n,) array: Coordinates of tracked particles
319319
survived: (n,) array: Coordinates of surviving particles
320+
tracked: (n,) array: Coordinates of tracked particles
320321
321322
In case of multiple ``tracked`` and ``survived`` are lists of arrays,
322323
with one array per ref. point.
@@ -362,7 +363,7 @@ def get_vertical_acceptance(
362363
* :py:attr:`.GridMode.RADIAL`: full [:math:`\:r, \theta\:`] grid
363364
* :py:attr:`.GridMode.RECURSIVE`: radial recursive search
364365
use_mp: Use python multiprocessing (:py:func:`.patpass`,
365-
default use :py:func:`.lattice_pass`). In case multi-processing
366+
default use :py:func:`.lattice_pass`). In case multiprocessing
366367
is not enabled, ``grid_mode`` is forced to
367368
:py:attr:`.GridMode.RECURSIVE` (most efficient in single core)
368369
verbose: Print out some information
@@ -372,15 +373,15 @@ def get_vertical_acceptance(
372373
start_method: Python multiprocessing start method. The default
373374
``None`` uses the python default that is considered safe.
374375
Available parameters: ``'fork'``, ``'spawn'``, ``'forkserver'``.
375-
The default for linux is ``'fork'``, the default for MacOS and
376-
Windows is ``'spawn'``. ``'fork'`` may used for MacOS to speed-up
377-
the calculation or to solve runtime errors, however it is considered
376+
The default for linux is ``'fork'``, the default for macOS and
377+
Windows is ``'spawn'``. ``'fork'`` may be used on macOS to speed up
378+
the calculation or to solve runtime errors, however it is considered
378379
unsafe.
379380
380381
Returns:
381382
boundary: (len(refpts),2) array: 1D acceptance
382-
tracked: (n,) array: Coordinates of tracked particles
383383
survived: (n,) array: Coordinates of surviving particles
384+
tracked: (n,) array: Coordinates of tracked particles
384385
385386
In case of multiple ``tracked`` and ``survived`` are lists of arrays,
386387
with one array per ref. point.
@@ -426,7 +427,7 @@ def get_momentum_acceptance(
426427
* :py:attr:`.GridMode.RADIAL`: full [:math:`\:r, \theta\:`] grid
427428
* :py:attr:`.GridMode.RECURSIVE`: radial recursive search
428429
use_mp: Use python multiprocessing (:py:func:`.patpass`,
429-
default use :py:func:`.lattice_pass`). In case multi-processing is
430+
default use :py:func:`.lattice_pass`). In case multiprocessing is
430431
not enabled, ``grid_mode`` is forced to
431432
:py:attr:`.GridMode.RECURSIVE` (most efficient in single core)
432433
verbose: Print out some information
@@ -436,15 +437,15 @@ def get_momentum_acceptance(
436437
start_method: Python multiprocessing start method. The default
437438
``None`` uses the python default that is considered safe.
438439
Available parameters: ``'fork'``, ``'spawn'``, ``'forkserver'``.
439-
The default for linux is ``'fork'``, the default for MacOS and
440-
Windows is ``'spawn'``. ``'fork'`` may used for MacOS to speed-up
441-
the calculation or to solve runtime errors, however it is considered
440+
The default for linux is ``'fork'``, the default for macOS and
441+
Windows is ``'spawn'``. ``'fork'`` may be used on macOS to speed up
442+
the calculation or to solve runtime errors, however it is considered
442443
unsafe.
443444
444445
Returns:
445446
boundary: (len(refpts),2) array: 1D acceptance
446-
tracked: (n,) array: Coordinates of tracked particles
447447
survived: (n,) array: Coordinates of surviving particles
448+
tracked: (n,) array: Coordinates of tracked particles
448449
449450
In case of multiple ``tracked`` and ``survived`` are lists of arrays,
450451
with one array per ref. point.

0 commit comments

Comments
 (0)
Please sign in to comment.