Skip to content

Commit f7aaa84

Browse files
use LatticeJSON Schema version 2.0 (#78)
1 parent 4b36d0e commit f7aaa84

File tree

4 files changed

+53
-52
lines changed

4 files changed

+53
-52
lines changed

apace/classes.py

Lines changed: 43 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,15 @@ class Base:
99
"""Abstract base for all element and lattice classes.
1010
1111
:param str name: The name of the object.
12-
:param description: A brief description of the object.
13-
:type description: str, optional
12+
:param info: Additional information about the object.
13+
:type info: str, optional
1414
"""
1515

16-
def __init__(self, name, length, description=""):
16+
def __init__(self, name, length, info=""):
1717
self.name: str = name
1818
"""The name of the object."""
19-
self.description: str = description
20-
"""A brief description of the object"""
19+
self.info: str = info
20+
"""Additional information about the object"""
2121
self.parent_lattices: Set["Lattice"] = set()
2222
"""All lattices which contain the object."""
2323
self._length = length
@@ -55,12 +55,12 @@ class Element(Base):
5555
5656
:param str name: The name of the element.
5757
:param float length: The length of the element (m).
58-
:param str description: A brief description of the element.
59-
:type description: str, optional
58+
:param str info: Additional information about the element.
59+
:type info: str, optional
6060
"""
6161

62-
def __init__(self, name, length, description=""):
63-
super().__init__(name, length, description)
62+
def __init__(self, name, length, info=""):
63+
super().__init__(name, length, info)
6464
self._length = length
6565
self.length_changed: Signal = Signal()
6666
"""Gets emitted when the length changes."""
@@ -94,8 +94,8 @@ class Drift(Element):
9494
9595
:param str name: The name of the element.
9696
:param float length: The length of the element (m).
97-
:param str description: A brief description of the element.
98-
:type description: str, optional
97+
:param str info: Additional information about the element.
98+
:type info: str, optional
9999
"""
100100

101101
pass
@@ -111,12 +111,12 @@ class Dipole(Element):
111111
:type e1: float, optional
112112
:param e2: Exit angle in rad.
113113
:type e2: float, optional
114-
:param description: A brief description of the element.
115-
:type description: str, optional
114+
:param info: Additional information about the element.
115+
:type info: str, optional
116116
"""
117117

118-
def __init__(self, name, length, angle, e1=0, e2=0, description=""):
119-
super().__init__(name, length, description)
118+
def __init__(self, name, length, angle, e1=0, e2=0, info=""):
119+
super().__init__(name, length, info)
120120
self._angle = angle
121121
self._e1 = e1
122122
self._e2 = e2
@@ -176,12 +176,12 @@ class Quadrupole(Element):
176176
:param str name: Name of the element.
177177
:param float length: Length of the element (m).
178178
:param float k1: Geometric quadrupole strength (m^-2).
179-
:param description: A brief description of the element.
180-
:type description: str, optional
179+
:param info: Additional information about the element.
180+
:type info: str, optional
181181
"""
182182

183-
def __init__(self, name, length, k1, description=""):
184-
super().__init__(name, length, description)
183+
def __init__(self, name, length, k1, info=""):
184+
super().__init__(name, length, info)
185185
self._k1 = k1
186186

187187
@property
@@ -201,12 +201,12 @@ class Sextupole(Element):
201201
:param str name: Name of the element.
202202
:param float length: Length of the element (m).
203203
:param float k1: Geometric quadrupole strength (m^-3).
204-
:param description: A brief description of the element.
205-
:type description: str, optional
204+
:param info: Additional information about the element.
205+
:type info: str, optional
206206
"""
207207

208-
def __init__(self, name, length, k2, description=""):
209-
super().__init__(name, length, description)
208+
def __init__(self, name, length, k2, info=""):
209+
super().__init__(name, length, info)
210210
self._k2 = k2
211211

212212
@property
@@ -226,12 +226,12 @@ class Octupole(Element):
226226
:param str name: Name of the element.
227227
:param float length: Length of the element (m).
228228
:param float k3: Geometric quadrupole strength (m^-4).
229-
:param description: A brief description of the element.
230-
:type description: str, optional
229+
:param info: Additional information about the element.
230+
:type info: str, optional
231231
"""
232232

233-
def __init__(self, name, length, k3, description=""):
234-
super().__init__(name, length, description)
233+
def __init__(self, name, length, k3, info=""):
234+
super().__init__(name, length, info)
235235
self._k3 = k3
236236

237237
@property
@@ -251,11 +251,11 @@ class Lattice(Base):
251251
:param str name: Name of the lattice.
252252
:param tree: Nested tree of elements and lattices.
253253
:type tree: Tuple[Union[Element, Lattice]]
254-
:param str description: A brief description of the element.
254+
:param str info: Additional information about the lattice.
255255
"""
256256

257-
def __init__(self, name, tree, description=None):
258-
super().__init__(name, description)
257+
def __init__(self, name, tree, info=None):
258+
super().__init__(name, info)
259259
self._tree = tree
260260
for obj in set(tree):
261261
obj.parent_lattices.add(self)
@@ -409,16 +409,13 @@ def from_dict(cls, data) -> "Lattice":
409409
objects[name] = class_(name=name, **attributes)
410410

411411
# TODO: make sure sub_lattices are loaded in correct order
412-
sub_lattices = data["sub_lattices"]
413-
for name, tree_names in sub_lattices.items():
414-
tree = [objects[name] for name in tree_names]
415-
objects[name] = Lattice(name, tree)
416-
417-
return cls(
418-
name=data["name"],
419-
tree=[objects[name] for name in data["lattice"]],
420-
description=data.get("description", ""),
421-
)
412+
for name, child_names in data["lattices"].items():
413+
children = [objects[name] for name in child_names]
414+
objects[name] = Lattice(name, children)
415+
416+
root_lattice = objects[data["root"]]
417+
root_lattice.info = data.get("info", "")
418+
return root_lattice
422419

423420
def as_dict(self):
424421
"""Serializes the `Lattice` object into a latticeJSON compliant dictionary."""
@@ -430,15 +427,17 @@ def as_dict(self):
430427
name = attributes.pop("name")
431428
elements_dict[name] = [type_.__name__, attributes]
432429

433-
sub_lattices_dict = {
430+
lattices_dict = {
434431
lattice.name: [obj.name for obj in lattice.tree]
435432
for lattice in self.sub_lattices
436433
}
434+
lattices_dict[self.name] = [obj.name for obj in self.tree]
437435

438436
return dict(
439-
name=self.name,
440-
description=self.description,
437+
version="2.0",
438+
root=self.name,
439+
info=self.info,
441440
lattice=[obj.name for obj in self.tree],
442441
elements=elements_dict,
443-
sub_lattices=sub_lattices_dict,
442+
lattices=lattices_dict,
444443
)

data/lattices/fodo_ring.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
{
2-
"name": "FODO_RING",
3-
"description": "This is the simplest possible strong focusing lattice.",
2+
"version": "2.0",
3+
"title": "FODO Lattice",
4+
"info": "This is the simplest possible strong focusing lattice.",
5+
"root": "RING",
46
"elements": {
57
"D1": ["Drift", {"length": 0.55}],
68
"Q1": ["Quadrupole", {"length": 0.2, "k1": 1.2}],
79
"Q2": ["Quadrupole", {"length": 0.4, "k1": -1.2}],
810
"B1": ["Dipole", {"length": 1.5, "angle": 0.392701, "e1": 0.1963505, "e2": 0.1963505}]
911
},
10-
"sub_lattices": {
11-
"FODO": ["Q1", "D1", "B1", "D1", "Q2", "D1", "B1", "D1", "Q1"]
12-
},
13-
"lattice": ["FODO", "FODO", "FODO", "FODO", "FODO", "FODO", "FODO", "FODO"]
12+
"lattices": {
13+
"CELL": ["Q1", "D1", "B1", "D1", "Q2", "D1", "B1", "D1", "Q1"],
14+
"RING": ["CELL", "CELL", "CELL", "CELL", "CELL", "CELL", "CELL", "CELL"]
15+
}
1416
}

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.1.1",
22+
"latticejson>=0.1.2",
2323
"numpy",
2424
"scipy",
2525
"matplotlib",

tests/test_io.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def test_attributes(fodo_ring):
2424
assert 0.4 == fodo_ring["Q2"].length
2525
assert -1.2 == fodo_ring["Q2"].k1
2626

27-
fodo_cell = fodo_ring["FODO"]
27+
fodo_cell = fodo_ring["CELL"]
2828
assert isinstance(fodo_cell, ap.Lattice)
2929
assert 6.0 == fodo_cell.length
3030

0 commit comments

Comments
 (0)