Skip to content

Commit 9140bdb

Browse files
Replace ap.load_lattice with ap.Lattice.from_file (#58)
Use `@classmethod` to create another class constructor for `ap.Lattice` instead of `ap.load_lattice`
1 parent 87b6677 commit 9140bdb

File tree

12 files changed

+30
-121
lines changed

12 files changed

+30
-121
lines changed

apace/__init__.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
Octupole,
1010
Lattice,
1111
)
12-
from .io import load_lattice, save_lattice
1312
from .twiss import Twiss
1413
from .matrix_tracking import MatrixTracking
1514
from .distributions import create_particle_distribution

apace/classes.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import inspect
2+
import latticejson
23
import sys
34
from typing import List, Dict, Set, Union
45
from .utils import Signal, Attribute, AmbiguousNameError
@@ -388,7 +389,18 @@ def _tree_as_string(obj, prefix=""):
388389
return string
389390

390391
@classmethod
391-
def from_dict(cls, data):
392+
def from_file(cls, location, file_format=None) -> "Lattice":
393+
"""Creates a new `Lattice` from file at `location` (path or url).
394+
:param location: path-like or url-like string which locates the lattice file
395+
:type location: Union[AnyStr, Path]
396+
:param file_format str: File format of the lattice file
397+
:type file_format: str, optional (use file extension)
398+
:rtype Lattice
399+
"""
400+
return cls.from_dict(latticejson.load(location, file_format))
401+
402+
@classmethod
403+
def from_dict(cls, data) -> "Lattice":
392404
"""Creates a new `Lattice` object from a latticeJSON compliant dictionary."""
393405

394406
objects = {} # dict containing all elements + lattices

apace/io.py

Lines changed: 0 additions & 102 deletions
This file was deleted.

apace/plot.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -209,11 +209,13 @@ def _twiss_plot_section(
209209
if y_max is None:
210210
y_max = ax.get_ylim()[1]
211211

212-
draw_lattice(twiss.lattice, ax, x_min, x_max, annotate_elements=annotate_elements)
213212
ax.set_xlim((x_min, x_max))
214213
ax.set_ylim((y_min, y_max))
214+
draw_lattice(twiss.lattice, ax, x_min, x_max, annotate_elements=annotate_elements)
215215

216216

217+
# TODO: make sub_class of figure
218+
# add attribute which defines which twiss parameters are plotted
217219
def twiss_plot(
218220
twiss,
219221
main=True,
@@ -278,7 +280,7 @@ def twiss_plot(
278280
n_x_ticks=None,
279281
)
280282

281-
fig.suptitle(twiss.lattice.name)
283+
fig.suptitle(twiss.lattice.name, ha="right", x=0.9925)
282284
fig.tight_layout()
283285
# fig.subplots_adjust(top=0.93)
284286
if path:

apace/twiss.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ def update_twiss_array(self):
178178
if self._twiss_array.shape[0] != n_points:
179179
self._twiss_array = np.empty((8, n_points))
180180

181-
if not self.stable:
181+
if not self.stable: # TODO: replace with warning
182182
print(
183183
f"Horizontal plane stability: {self.stable_x}\n"
184184
f"Vertical plane stability: {self.stable_y}"

docs/guide.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,9 @@ be altered when needed.
209209

210210
Load and Save Lattice Files
211211
---------------------------
212-
lattices can also be imported from a lattice file. This can be done using the :func:`load_lattice` function::
212+
lattices can also be imported from a lattice file. This can be done using the :func:`Lattice.from_file` method::
213213

214-
lattice = ap.load_lattice('/path/to/file')
214+
lattice = ap.Lattice.from_file('/path/to/file')
215215

216216
Individual elements and sub-lattices can be accessed through the :attr:`~Lattice.elements` and :attr:`~Lattice.sub_lattices`, respectively::
217217

@@ -220,7 +220,7 @@ Individual elements and sub-lattices can be accessed through the :attr:`~Lattice
220220

221221
A given lattice can be saved to a lattice file using the :func:`save_lattice` function::
222222

223-
ap.load_lattice(lattice, '/path/to/file')
223+
ap.Lattice.from_file(lattice, '/path/to/file')
224224

225225
The Twiss class
226226
===============

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
packages=find_packages(),
2020
package_data={"data": ["data"],},
2121
install_requires=[
22-
"latticejson>=0.0.3",
22+
"latticejson>=0.0.4",
2323
"numpy",
2424
"scipy",
2525
"matplotlib",

tests/test_classes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
BASE_PATH = os.path.dirname(__file__)
66
FILE_PATH = os.path.join(BASE_PATH, "data", "lattices", "fodo_ring.json")
7-
FODO_RING = ap.load_lattice(FILE_PATH)
7+
FODO_RING = ap.Lattice.from_file(FILE_PATH)
88

99

1010
def test_length_changed():

tests/test_io.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# TODO: fix linter/mypy erros
1010
# https://stackoverflow.com/questions/59187502/mypy-dict-containing-instances-of-different-subclasses
1111
def test_attributes():
12-
fodo = ap.load_lattice(FILE_PATH)
12+
fodo = ap.Lattice.from_file(FILE_PATH)
1313
d1 = fodo["D1"]
1414
assert isinstance(d1, ap.Drift)
1515
assert 0.55 == fodo["D1"].length
@@ -39,11 +39,9 @@ def test_attributes():
3939
assert 48 == fodo.length
4040

4141

42-
def test_save_lattice():
43-
fodo = ap.load_lattice(FILE_PATH)
44-
path = "/tmp/tmp_lattice.json"
45-
ap.save_lattice(fodo, path)
46-
fodo_reload = ap.load_lattice(path)
42+
def test_serialize_lattice():
43+
fodo = ap.Lattice.from_file(FILE_PATH)
44+
fodo_reload = ap.Lattice.from_dict(fodo.as_dict())
4745
assert fodo.length == fodo_reload.length
4846
assert len(fodo.elements) == len(fodo_reload.elements)
4947
assert len(fodo.sub_lattices) == len(fodo_reload.sub_lattices)

tests/test_matrix_method.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88

99
def test_angle_k1():
10-
lattice = ap.load_lattice(FILE_PATH)
10+
lattice = ap.Lattice.from_file(FILE_PATH)
1111
b1 = lattice["B1"]
1212
q1 = lattice["Q1"]
1313
matrix_method = ap.MatrixMethod(lattice)

0 commit comments

Comments
 (0)