Skip to content

Commit 80e902b

Browse files
committed
Remove private indicator _ from ParameterBeam.mu and ParameterBeam.cov
1 parent 3db3994 commit 80e902b

11 files changed

+90
-92
lines changed

cheetah/accelerator/cavity.py

+23-23
Original file line numberDiff line numberDiff line change
@@ -106,9 +106,9 @@ def _track_beam(self, incoming: Beam) -> Beam:
106106

107107
tm = self.transfer_map(incoming.energy, incoming.species)
108108
if isinstance(incoming, ParameterBeam):
109-
outgoing_mu = torch.matmul(tm, incoming._mu.unsqueeze(-1)).squeeze(-1)
110-
outgoing_cov = torch.matmul(
111-
tm, torch.matmul(incoming._cov, tm.transpose(-2, -1))
109+
outgoingmu = torch.matmul(tm, incoming.mu.unsqueeze(-1)).squeeze(-1)
110+
outgoingcov = torch.matmul(
111+
tm, torch.matmul(incoming.cov, tm.transpose(-2, -1))
112112
)
113113
else: # ParticleBeam
114114
outgoing_particles = torch.matmul(incoming.particles, tm.transpose(-2, -1))
@@ -128,12 +128,12 @@ def _track_beam(self, incoming: Beam) -> Beam:
128128
)
129129

130130
if isinstance(incoming, ParameterBeam):
131-
outgoing_mu[..., 5] = incoming._mu[..., 5] * incoming.energy * beta0 / (
131+
outgoingmu[..., 5] = incoming.mu[..., 5] * incoming.energy * beta0 / (
132132
outgoing_energy * beta1
133133
) + self.voltage * beta0 / (outgoing_energy * beta1) * (
134-
torch.cos(-incoming._mu[..., 4] * beta0 * k + phi) - torch.cos(phi)
134+
torch.cos(-incoming.mu[..., 4] * beta0 * k + phi) - torch.cos(phi)
135135
)
136-
outgoing_cov[..., 5, 5] = incoming._cov[..., 5, 5]
136+
outgoingcov[..., 5, 5] = incoming.cov[..., 5, 5]
137137
else: # ParticleBeam
138138
outgoing_particles[..., 5] = incoming.particles[
139139
..., 5
@@ -196,22 +196,22 @@ def _track_beam(self, incoming: Beam) -> Beam:
196196
)
197197

198198
if isinstance(incoming, ParameterBeam):
199-
outgoing_mu[..., 4] = outgoing_mu[..., 4] + (
200-
T566 * incoming._mu[..., 5] ** 2
201-
+ T556 * incoming._mu[..., 4] * incoming._mu[..., 5]
202-
+ T555 * incoming._mu[..., 4] ** 2
199+
outgoingmu[..., 4] = outgoingmu[..., 4] + (
200+
T566 * incoming.mu[..., 5] ** 2
201+
+ T556 * incoming.mu[..., 4] * incoming.mu[..., 5]
202+
+ T555 * incoming.mu[..., 4] ** 2
203203
)
204-
outgoing_cov[..., 4, 4] = (
205-
T566 * incoming._cov[..., 5, 5] ** 2
206-
+ T556 * incoming._cov[..., 4, 5] * incoming._cov[..., 5, 5]
207-
+ T555 * incoming._cov[..., 4, 4] ** 2
204+
outgoingcov[..., 4, 4] = (
205+
T566 * incoming.cov[..., 5, 5] ** 2
206+
+ T556 * incoming.cov[..., 4, 5] * incoming.cov[..., 5, 5]
207+
+ T555 * incoming.cov[..., 4, 4] ** 2
208208
)
209-
outgoing_cov[..., 4, 5] = (
210-
T566 * incoming._cov[..., 5, 5] ** 2
211-
+ T556 * incoming._cov[..., 4, 5] * incoming._cov[..., 5, 5]
212-
+ T555 * incoming._cov[..., 4, 4] ** 2
209+
outgoingcov[..., 4, 5] = (
210+
T566 * incoming.cov[..., 5, 5] ** 2
211+
+ T556 * incoming.cov[..., 4, 5] * incoming.cov[..., 5, 5]
212+
+ T555 * incoming.cov[..., 4, 4] ** 2
213213
)
214-
outgoing_cov[..., 5, 4] = outgoing_cov[..., 4, 5]
214+
outgoingcov[..., 5, 4] = outgoingcov[..., 4, 5]
215215
else: # ParticleBeam
216216
outgoing_particles[..., 4] = outgoing_particles[..., 4] + (
217217
T566.unsqueeze(-1) * incoming.particles[..., 5] ** 2
@@ -223,12 +223,12 @@ def _track_beam(self, incoming: Beam) -> Beam:
223223

224224
if isinstance(incoming, ParameterBeam):
225225
outgoing = ParameterBeam(
226-
mu=outgoing_mu,
227-
cov=outgoing_cov,
226+
mu=outgoingmu,
227+
cov=outgoingcov,
228228
energy=outgoing_energy,
229229
total_charge=incoming.total_charge,
230-
device=outgoing_mu.device,
231-
dtype=outgoing_mu.dtype,
230+
device=outgoingmu.device,
231+
dtype=outgoingmu.dtype,
232232
)
233233
return outgoing
234234
else: # ParticleBeam

cheetah/accelerator/element.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ def track(self, incoming: Beam) -> Beam:
6868
"""
6969
if isinstance(incoming, ParameterBeam):
7070
tm = self.transfer_map(incoming.energy, incoming.species)
71-
mu = torch.matmul(tm, incoming._mu.unsqueeze(-1)).squeeze(-1)
72-
cov = torch.matmul(tm, torch.matmul(incoming._cov, tm.transpose(-2, -1)))
71+
mu = torch.matmul(tm, incoming.mu.unsqueeze(-1)).squeeze(-1)
72+
cov = torch.matmul(tm, torch.matmul(incoming.cov, tm.transpose(-2, -1)))
7373
return ParameterBeam(
7474
mu,
7575
cov,

cheetah/accelerator/screen.py

+14-16
Original file line numberDiff line numberDiff line change
@@ -151,13 +151,13 @@ def track(self, incoming: Beam) -> Beam:
151151
copy_of_incoming = incoming.clone()
152152

153153
if isinstance(incoming, ParameterBeam):
154-
copy_of_incoming._mu, _ = torch.broadcast_tensors(
155-
copy_of_incoming._mu, self.misalignment[..., 0]
154+
copy_of_incoming.mu, _ = torch.broadcast_tensors(
155+
copy_of_incoming.mu, self.misalignment[..., 0]
156156
)
157-
copy_of_incoming._mu = copy_of_incoming._mu.clone()
157+
copy_of_incoming.mu = copy_of_incoming.mu.clone()
158158

159-
copy_of_incoming._mu[..., 0] -= self.misalignment[..., 0]
160-
copy_of_incoming._mu[..., 2] -= self.misalignment[..., 1]
159+
copy_of_incoming.mu[..., 0] -= self.misalignment[..., 0]
160+
copy_of_incoming.mu[..., 2] -= self.misalignment[..., 1]
161161
elif isinstance(incoming, ParticleBeam):
162162
copy_of_incoming.particles, _ = torch.broadcast_tensors(
163163
copy_of_incoming.particles,
@@ -178,8 +178,8 @@ def track(self, incoming: Beam) -> Beam:
178178
if self.is_active and self.is_blocking:
179179
if isinstance(incoming, ParameterBeam):
180180
return ParameterBeam(
181-
mu=incoming._mu,
182-
cov=incoming._cov,
181+
mu=incoming.mu,
182+
cov=incoming.cov,
183183
energy=incoming.energy,
184184
total_charge=torch.zeros_like(incoming.total_charge),
185185
)
@@ -208,30 +208,28 @@ def reading(self) -> torch.Tensor:
208208
dtype=self.misalignment.dtype,
209209
)
210210
elif isinstance(read_beam, ParameterBeam):
211-
if torch.numel(read_beam._mu[..., 0]) > 1:
211+
if torch.numel(read_beam.mu[..., 0]) > 1:
212212
raise NotImplementedError(
213213
"`Screen` does not support vectorization of `ParameterBeam`. "
214214
"Please use `ParticleBeam` instead. If this is a feature you would "
215215
"like to see, please open an issue on GitHub."
216216
)
217217

218-
transverse_mu = torch.stack(
219-
[read_beam._mu[..., 0], read_beam._mu[..., 2]], dim=-1
218+
transversemu = torch.stack(
219+
[read_beam.mu[..., 0], read_beam.mu[..., 2]], dim=-1
220220
)
221-
transverse_cov = torch.stack(
221+
transversecov = torch.stack(
222222
[
223223
torch.stack(
224-
[read_beam._cov[..., 0, 0], read_beam._cov[..., 0, 2]], dim=-1
224+
[read_beam.cov[..., 0, 0], read_beam.cov[..., 0, 2]], dim=-1
225225
),
226226
torch.stack(
227-
[read_beam._cov[..., 2, 0], read_beam._cov[..., 2, 2]], dim=-1
227+
[read_beam.cov[..., 2, 0], read_beam.cov[..., 2, 2]], dim=-1
228228
),
229229
],
230230
dim=-1,
231231
)
232-
dist = MultivariateNormal(
233-
loc=transverse_mu, covariance_matrix=transverse_cov
234-
)
232+
dist = MultivariateNormal(loc=transversemu, covariance_matrix=transversecov)
235233

236234
left = self.extent[0]
237235
right = self.extent[1]

cheetah/particles/parameter_beam.py

+27-27
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ def __init__(
3737
factory_kwargs = {"device": device, "dtype": dtype}
3838
super().__init__()
3939

40-
self.register_buffer("_mu", None)
41-
self.register_buffer("_cov", None)
40+
self.register_buffer("mu", None)
41+
self.register_buffer("cov", None)
4242
self.register_buffer("energy", None)
4343
self.register_buffer("total_charge", torch.tensor(0.0, **factory_kwargs))
4444

45-
self._mu = torch.as_tensor(mu, **factory_kwargs)
46-
self._cov = torch.as_tensor(cov, **factory_kwargs)
45+
self.mu = torch.as_tensor(mu, **factory_kwargs)
46+
self.cov = torch.as_tensor(cov, **factory_kwargs)
4747
self.energy = torch.as_tensor(energy, **factory_kwargs)
4848
if total_charge is not None:
4949
self.total_charge = torch.as_tensor(total_charge, **factory_kwargs)
@@ -467,8 +467,8 @@ def as_particle_beam(self, num_particles: int) -> "ParticleBeam": # noqa: F821
467467
cov_taup=self.cov_taup,
468468
energy=self.energy,
469469
total_charge=self.total_charge,
470-
device=self._mu.device,
471-
dtype=self._mu.dtype,
470+
device=self.mu.device,
471+
dtype=self.mu.dtype,
472472
)
473473

474474
def linspaced(self, num_particles: int) -> "ParticleBeam": # noqa: F821
@@ -498,82 +498,82 @@ def linspaced(self, num_particles: int) -> "ParticleBeam": # noqa: F821
498498
energy=self.energy,
499499
total_charge=self.total_charge,
500500
species=self.species,
501-
device=self._mu.device,
502-
dtype=self._mu.dtype,
501+
device=self.mu.device,
502+
dtype=self.mu.dtype,
503503
)
504504

505505
@property
506506
def mu_x(self) -> torch.Tensor:
507-
return self._mu[..., 0]
507+
return self.mu[..., 0]
508508

509509
@property
510510
def sigma_x(self) -> torch.Tensor:
511-
return torch.sqrt(torch.clamp_min(self._cov[..., 0, 0], 1e-20))
511+
return torch.sqrt(torch.clamp_min(self.cov[..., 0, 0], 1e-20))
512512

513513
@property
514514
def mu_px(self) -> torch.Tensor:
515-
return self._mu[..., 1]
515+
return self.mu[..., 1]
516516

517517
@property
518518
def sigma_px(self) -> torch.Tensor:
519-
return torch.sqrt(torch.clamp_min(self._cov[..., 1, 1], 1e-20))
519+
return torch.sqrt(torch.clamp_min(self.cov[..., 1, 1], 1e-20))
520520

521521
@property
522522
def mu_y(self) -> torch.Tensor:
523-
return self._mu[..., 2]
523+
return self.mu[..., 2]
524524

525525
@property
526526
def sigma_y(self) -> torch.Tensor:
527-
return torch.sqrt(torch.clamp_min(self._cov[..., 2, 2], 1e-20))
527+
return torch.sqrt(torch.clamp_min(self.cov[..., 2, 2], 1e-20))
528528

529529
@property
530530
def mu_py(self) -> torch.Tensor:
531-
return self._mu[..., 3]
531+
return self.mu[..., 3]
532532

533533
@property
534534
def sigma_py(self) -> torch.Tensor:
535-
return torch.sqrt(torch.clamp_min(self._cov[..., 3, 3], 1e-20))
535+
return torch.sqrt(torch.clamp_min(self.cov[..., 3, 3], 1e-20))
536536

537537
@property
538538
def mu_tau(self) -> torch.Tensor:
539-
return self._mu[..., 4]
539+
return self.mu[..., 4]
540540

541541
@property
542542
def sigma_tau(self) -> torch.Tensor:
543-
return torch.sqrt(torch.clamp_min(self._cov[..., 4, 4], 1e-20))
543+
return torch.sqrt(torch.clamp_min(self.cov[..., 4, 4], 1e-20))
544544

545545
@property
546546
def mu_p(self) -> torch.Tensor:
547-
return self._mu[..., 5]
547+
return self.mu[..., 5]
548548

549549
@property
550550
def sigma_p(self) -> torch.Tensor:
551-
return torch.sqrt(torch.clamp_min(self._cov[..., 5, 5], 1e-20))
551+
return torch.sqrt(torch.clamp_min(self.cov[..., 5, 5], 1e-20))
552552

553553
@property
554554
def cov_xpx(self) -> torch.Tensor:
555-
return self._cov[..., 0, 1]
555+
return self.cov[..., 0, 1]
556556

557557
@property
558558
def cov_ypy(self) -> torch.Tensor:
559-
return self._cov[..., 2, 3]
559+
return self.cov[..., 2, 3]
560560

561561
@property
562562
def cov_taup(self) -> torch.Tensor:
563-
return self._cov[..., 4, 5]
563+
return self.cov[..., 4, 5]
564564

565565
def clone(self) -> "ParameterBeam":
566566
return ParameterBeam(
567-
mu=self._mu.clone(),
568-
cov=self._cov.clone(),
567+
mu=self.mu.clone(),
568+
cov=self.cov.clone(),
569569
energy=self.energy.clone(),
570570
total_charge=self.total_charge.clone(),
571571
)
572572

573573
def __repr__(self) -> str:
574574
return (
575-
f"{self.__class__.__name__}(mu={repr(self._mu)}, "
576-
+ f"cov={repr(self._cov)}, "
575+
f"{self.__class__.__name__}(mu={repr(self.mu)}, "
576+
+ f"cov={repr(self.cov)}, "
577577
+ f"energy={repr(self.energy)}, "
578578
+ f"total_charge={repr(self.total_charge)}, "
579579
+ f"species={repr(self.species)})"

cheetah/particles/particle_beam.py

+10-10
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
from cheetah.utils import (
1414
elementwise_linspace,
1515
format_axis_with_prefixed_unit,
16-
unbiased_weighted_covariance,
1716
unbiased_weighted_std,
17+
unbiased_weightedcovariance,
1818
verify_device_and_dtype,
1919
)
2020

@@ -263,8 +263,8 @@ def from_parameters(
263263
cov = cov.expand(*vector_shape, 6, 6)
264264
particles = torch.ones((*vector_shape, num_particles, 7), **factory_kwargs)
265265
distributions = [
266-
MultivariateNormal(sample_mean, covariance_matrix=sample_cov)
267-
for sample_mean, sample_cov in zip(mean.view(-1, 6), cov.view(-1, 6, 6))
266+
MultivariateNormal(sample_mean, covariance_matrix=samplecov)
267+
for sample_mean, samplecov in zip(mean.view(-1, 6), cov.view(-1, 6, 6))
268268
]
269269
particles[..., :6] = torch.stack(
270270
[distribution.sample((num_particles,)) for distribution in distributions],
@@ -887,7 +887,7 @@ def transformed_to(
887887
mu_x, mu_px, mu_y, mu_py, mu_tau, mu_p = torch.broadcast_tensors(
888888
mu_x, mu_px, mu_y, mu_py, mu_tau, mu_p
889889
)
890-
new_mu = torch.stack([mu_x, mu_px, mu_y, mu_py, mu_tau, mu_p], dim=-1)
890+
newmu = torch.stack([mu_x, mu_px, mu_y, mu_py, mu_tau, mu_p], dim=-1)
891891
sigma_x, sigma_px, sigma_y, sigma_py, sigma_tau, sigma_p = (
892892
torch.broadcast_tensors(
893893
sigma_x, sigma_px, sigma_y, sigma_py, sigma_tau, sigma_p
@@ -897,7 +897,7 @@ def transformed_to(
897897
[sigma_x, sigma_px, sigma_y, sigma_py, sigma_tau, sigma_p], dim=-1
898898
)
899899

900-
old_mu = torch.stack(
900+
oldmu = torch.stack(
901901
[self.mu_x, self.mu_px, self.mu_y, self.mu_py, self.mu_tau, self.mu_p],
902902
dim=-1,
903903
)
@@ -915,10 +915,10 @@ def transformed_to(
915915

916916
phase_space = self.particles[..., :6]
917917
phase_space = (
918-
(phase_space.transpose(-2, -1) - old_mu.unsqueeze(-1))
918+
(phase_space.transpose(-2, -1) - oldmu.unsqueeze(-1))
919919
/ old_sigma.unsqueeze(-1)
920920
* new_sigma.unsqueeze(-1)
921-
+ new_mu.unsqueeze(-1)
921+
+ newmu.unsqueeze(-1)
922922
).transpose(-2, -1)
923923

924924
particles = torch.ones(*phase_space.shape[:-1], 7)
@@ -1546,7 +1546,7 @@ def cov_xpx(self) -> torch.Tensor:
15461546
Returns the covariance between x and px. :math:`\sigma_{x, px}^2`.
15471547
It is weighted by the survival probability of the particles.
15481548
"""
1549-
return unbiased_weighted_covariance(
1549+
return unbiased_weightedcovariance(
15501550
self.x, self.px, weights=self.survival_probabilities, dim=-1
15511551
)
15521552

@@ -1556,7 +1556,7 @@ def cov_ypy(self) -> torch.Tensor:
15561556
Returns the covariance between y and py. :math:`\sigma_{y, py}^2`.
15571557
It is weighted by the survival probability of the particles.
15581558
"""
1559-
return unbiased_weighted_covariance(
1559+
return unbiased_weightedcovariance(
15601560
self.y, self.py, weights=self.survival_probabilities, dim=-1
15611561
)
15621562

@@ -1566,7 +1566,7 @@ def cov_taup(self) -> torch.Tensor:
15661566
Returns the covariance between tau and p. :math:`\sigma_{\tau, p}^2`.
15671567
It is weighted by the survival probability of the particles.
15681568
"""
1569-
return unbiased_weighted_covariance(
1569+
return unbiased_weightedcovariance(
15701570
self.tau, self.p, weights=self.survival_probabilities, dim=-1
15711571
)
15721572

cheetah/utils/__init__.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
format_axis_with_prefixed_unit,
1010
)
1111
from .statistics import ( # noqa: F401
12-
unbiased_weighted_covariance,
1312
unbiased_weighted_std,
1413
unbiased_weighted_variance,
14+
unbiased_weightedcovariance,
1515
)
1616
from .unique_name_generator import UniqueNameGenerator # noqa: F401

cheetah/utils/statistics.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import torch
22

33

4-
def unbiased_weighted_covariance(
4+
def unbiased_weightedcovariance(
55
input1: torch.Tensor, input2: torch.Tensor, weights: torch.Tensor, dim: int = None
66
) -> torch.Tensor:
77
"""

0 commit comments

Comments
 (0)