Skip to content

Commit f9c8e32

Browse files
rename tree -> children and sequence -> arangement (#96)
1 parent 7879a85 commit f9c8e32

File tree

6 files changed

+55
-60
lines changed

6 files changed

+55
-60
lines changed

apace/classes.py

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -242,53 +242,53 @@ class Lattice(Base):
242242
"""Defines the order of elements in the accelerator.
243243
244244
:param str name: Name of the lattice.
245-
:param tree: Nested tree of elements and lattices.
246-
:type tree: Tuple[Union[Element, Lattice]]
245+
:param children: List of elements and sub-lattices.
246+
:type children: List[Union[Element, Lattice]]
247247
:param str info: Additional information about the lattice.
248248
"""
249249

250-
def __init__(self, name, tree, info=""):
250+
def __init__(self, name, children, info=""):
251251
super().__init__(name, length=0, info=info)
252252
self._length_needs_update = True
253253
self.length_changed: Signal = Signal()
254254
"""Gets emitted when the length of lattice changes."""
255255
self.length_changed.connect(self._on_length_changed)
256256

257-
self._tree = tree
258-
for obj in set(tree):
257+
self._children = children
258+
for obj in set(children):
259259
obj.parent_lattices.add(self)
260260

261261
self._objects = {}
262262
self._elements = set()
263263
self._sub_lattices = set()
264-
self._arrangement = []
264+
self._sequence = []
265265
self._indices = {}
266-
self._init_tree_properties()
266+
self._init_properties()
267267

268268
self.element_changed: Signal = Signal()
269269
"""Gets emitted when an attribute of an element within this lattice changes."""
270270
self.element_changed.connect(self._on_element_changed)
271271

272-
self.n_elements = len(self.arrangement)
272+
self.n_elements = len(self.sequence)
273273
"""The number of elements within this lattice."""
274274

275275
@staticmethod
276-
def traverse_tree(tree) -> Iterator[Base]:
277-
"Returns iterator which traverses all nodes of the lattice tree."
278-
for obj in tree:
276+
def traverse_children(children) -> Iterator[Base]:
277+
"Returns iterator which traverses all children of a lattice."
278+
for obj in children:
279279
yield obj
280280
if isinstance(obj, Lattice):
281-
yield from Lattice.traverse_tree(obj.tree)
281+
yield from Lattice.traverse_children(obj.children)
282282

283-
def _init_tree_properties(self):
284-
"""A recursive helper function to initialize the tree properties."""
285-
arrangement = self._arrangement
283+
def _init_properties(self):
284+
"""A recursive helper function to initialize the properties."""
285+
sequence = self._sequence
286286
indices = self._indices
287287
elements = self._elements
288288
sub_lattices = self._sub_lattices
289289
objects = self._objects
290290
index = 0
291-
for obj in Lattice.traverse_tree(self.tree):
291+
for obj in Lattice.traverse_children(self.children):
292292
value = objects.get(obj.name)
293293
if value is None:
294294
objects[obj.name] = obj
@@ -303,20 +303,20 @@ def _init_tree_properties(self):
303303
if isinstance(obj, Lattice):
304304
sub_lattices.add(obj)
305305
else:
306-
arrangement.append(obj)
306+
sequence.append(obj)
307307
elements.add(obj)
308308
index += 1
309309

310310
def __getitem__(self, key):
311311
if isinstance(key, str):
312312
return self._objects[key]
313313
elif isinstance(key, (int, slice)):
314-
return self.arrangement[key]
314+
return self.sequence[key]
315315
elif isinstance(key, Base):
316316
return self.indices[Base]
317317

318318
def __del__(self):
319-
for obj in self.tree:
319+
for obj in self.children:
320320
obj.parent_lattices.discard(self)
321321

322322
@property
@@ -330,7 +330,7 @@ def update_length(self):
330330
"""Manually update the Length of the lattice (m)."""
331331
# TODO: can numpy be used to avoid rounding errors?
332332
# sum = (a + b) + (c + d)
333-
self._length = sum(obj.length for obj in self.tree)
333+
self._length = sum(obj.length for obj in self.children)
334334
self._length_needs_update = False
335335

336336
def _on_length_changed(self):
@@ -346,19 +346,19 @@ def _on_element_changed(self, element, attribute):
346346
lattice.element_changed(element, attribute)
347347

348348
@property
349-
def tree(self) -> List[Base]:
350-
"""List of elements and sub-lattices in physical order."""
351-
return self._tree
349+
def children(self) -> List[Base]:
350+
"""List of direct children (elements or sub-lattices) in physical order."""
351+
return self._children
352352

353353
@property
354-
def arrangement(self) -> List[Element]:
355-
"""List of elements in physical order. (Flattend :attr:`tree`)"""
356-
return self._arrangement
354+
def sequence(self) -> List[Element]:
355+
"""List of elements in physical order. (Flattend :attr:`children`)"""
356+
return self._sequence
357357

358358
@property
359359
def indices(self) -> Dict[Element, List[float]]:
360360
"""A dict which contains the a `List` of indices for each element.
361-
Can be thought of as inverse of arrangement. Sub-lattices are associated with
361+
Can be thought of as inverse of sequence. Sub-lattices are associated with
362362
the list of indices of their first element."""
363363
return self._indices
364364

@@ -385,7 +385,7 @@ def print_tree(self):
385385
def _print_tree(obj, prefix=""):
386386
string = f"{obj.name}\n"
387387
if isinstance(obj, Lattice):
388-
*others, last = obj.tree
388+
*others, last = obj.children
389389
for child in others:
390390
string += f"{prefix}├─── {Lattice._print_tree(child, prefix + '│ ')}"
391391
string += f"{prefix}└─── {Lattice._print_tree(last, prefix + ' ')}"
@@ -434,10 +434,10 @@ def as_dict(self):
434434

435435
# TODO: make sure lattices are sorted
436436
lattices_dict = {
437-
lattice.name: [obj.name for obj in lattice.tree]
437+
lattice.name: [obj.name for obj in lattice.children]
438438
for lattice in self.sub_lattices
439439
}
440-
lattices_dict[self.name] = [obj.name for obj in self.tree]
440+
lattices_dict[self.name] = [obj.name for obj in self.children]
441441

442442
return dict(
443443
version="2.0",

apace/matrixmethod.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ def n_steps(self) -> int:
138138

139139
def update_n_steps(self):
140140
"""Manually update the total number of kicks."""
141-
self._n_steps = sum(map(self.get_steps, self.lattice.arrangement))
141+
self._n_steps = sum(map(self.get_steps, self.lattice.sequence))
142142
self._n_steps_needs_update = False
143143

144144
def _on_n_steps_changed(self):
@@ -155,7 +155,7 @@ def update_element_indices(self):
155155
"""Manually update the indices of each element."""
156156
self._element_indices.clear()
157157
start = 0
158-
for element in self.lattice.arrangement:
158+
for element in self.lattice.sequence:
159159
end = start + self.get_steps(element)
160160
tmp = list(range(start, end))
161161
try:

apace/plot.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,10 @@ def draw_elements(
7878
plt.hlines(y0, x_min, x_max, color="black", linewidth=1)
7979
ax.set_ylim(y_min, y_max)
8080

81-
arrangement = lattice.arrangement
81+
sequence = lattice.sequence
8282
position = start = end = 0
8383
sign = 1
84-
for element, next_element in zip_longest(arrangement, arrangement[1:]):
84+
for element, next_element in zip_longest(sequence, sequence[1:]):
8585
position += element.length
8686
if element is next_element or position <= x_min:
8787
continue
@@ -130,7 +130,7 @@ def draw_sub_lattices(
130130
location: str = "bottom",
131131
):
132132
x_min, x_max = ax.get_xlim()
133-
length_gen = [0.0, *(obj.length for obj in lattice.tree)]
133+
length_gen = [0.0, *(obj.length for obj in lattice.children)]
134134
position_list = np.add.accumulate(length_gen)
135135
i_min = np.searchsorted(position_list, x_min)
136136
i_max = np.searchsorted(position_list, x_max)
@@ -151,7 +151,7 @@ def draw_sub_lattices(
151151

152152
ax.set_ylim(y_min, y_max)
153153
start = end = 0
154-
for obj in lattice.tree:
154+
for obj in lattice.children:
155155
end += obj.length
156156
if not isinstance(obj, Lattice) or start >= x_max or end <= x_min:
157157
continue
@@ -343,7 +343,7 @@ def __init__(
343343
if isinstance(section, (str, Base)):
344344
obj = self.lattice[section] if isinstance(section, str) else section
345345
index = self.lattice.indices[obj][0]
346-
x_min = sum(obj.length for obj in self.lattice.arrangement[:index])
346+
x_min = sum(obj.length for obj in self.lattice.sequence[:index])
347347
x_max = x_min + obj.length
348348
else:
349349
x_min, x_max = section
@@ -406,9 +406,9 @@ def floor_plan(
406406
end = np.zeros(2)
407407
x_min = y_min = 0
408408
x_max = y_max = 0
409-
arrangement = lattice.arrangement
409+
sequence = lattice.sequence
410410
sign = 1
411-
for element, next_element in zip_longest(arrangement, arrangement[1:]):
411+
for element, next_element in zip_longest(sequence, sequence[1:]):
412412
length = element.length
413413
if isinstance(element, Drift):
414414
color = Color.BLACK

apace/tracking_integration.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def track(self, initial_distribution, step_size=0.01):
5252

5353
end = 0
5454
i = 0
55-
for element in self.lattice.arrangement:
55+
for element in self.lattice.sequence:
5656
end += element.length
5757
while s[i] < end:
5858
step_size_arg = min(step_size, end - s[0])

docs/guide.rst

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -116,14 +116,14 @@ Its length should be 16 times the length of the :code:`dba_cell`::
116116
>>> dba_ring.length
117117
192.0
118118

119-
The Base Tree
119+
Direct children
120120
---------------
121121

122-
The structure which defines the order of elements in our DBA ring can be thought of as a `Tree <https://wikipedia.org/wiki/Tree_structure>`_, where :code:`dba_ring` is the root, the :code:`dba_cell` objects are the nodes and the :code:`bend`, :code:`drift` and :code:`quad` elements are the leafes. The attribute which stores the order of objects within a lattice is therefore called :attr:`~Lattice.tree`. Try to output the tree for the :code:`dba_ring` and :code:`dba_cell` objects::
122+
The structure which defines the order of elements in our DBA ring can be thought of as a `Tree <https://wikipedia.org/wiki/Tree_structure>`_, where :code:`dba_ring` is the root, the :code:`dba_cell` objects are the nodes and the :code:`bend`, :code:`drift` and :code:`quad` elements are the leafes. The attribute which stores the order of objects within a lattice is called :attr:`~Lattice.chilldren`. Try to pritn the children for the :code:`dba_ring` and :code:`dba_cell` objects::
123123

124-
>>> dba_ring.tree
124+
>>> dba_ring.children
125125
[DBA_CELL, DBA_CELL, DBA_CELL, DBA_CELL, DBA_CELL, DBA_CELL, DBA_CELL, DBA_CELL, DBA_CELL, DBA_CELL, DBA_CELL, DBA_CELL, DBA_CELL, DBA_CELL, DBA_CELL, DBA_CELL]
126-
>>> dba_cell.tree
126+
>>> dba_cell.children
127127
[Drift, Dipole, Drift, Quadrupole, Drift, Dipole, Drift]
128128

129129

@@ -149,23 +149,23 @@ This can be also visualized by calling the :meth:`Lattice.print_tree` method::
149149
├─── Dipole
150150
└─── Drift
151151

152-
As a nested structure is not always convenient to work with, there are three other representations of :attr:`~Lattice.tree` (internally called :code:`tree_properties`):
152+
As a nested structure is not always convenient to work with, there are three other representations of the nested :attr:`~Lattice.children` attribute:
153153

154-
#. The :attr:`~Lattice.lattice` attribute
154+
#. The :attr:`~Lattice.sequence` attribute
155155

156-
To loop over the exact arrangement of objects there is the :attr:`Lattice.lattice` attribute, which is a list of :class:`~Element` objects. It can be thought of a flattened version of the tree. The :attr:`~Lattice.lattice` attribute can be used in regular Python :code:`for ... in` loops::
156+
To loop over the exact sequence of objects there is the :attr:`Lattice.sequence` attribute, which is a list of :class:`~Element` objects. It can be thought of a flattened version of :attr:`~Lattice.children`. The :attr:`~Lattice.sequence` attribute can be used in regular Python :code:`for ... in` loops::
157157

158-
>>> sum(element.length for element in dba_ring.lattice)
158+
>>> sum(element.length for element in dba_ring.sequence)
159159
192
160160

161-
As the :code:`dba_cell` does not contain any other lattices, the :attr:`~Lattice.lattice` and :attr:`~Lattice.tree` attributes should be equal::
161+
As the :code:`dba_cell` does not contain any other lattices, the :attr:`~Lattice.sequence` and :attr:`~Lattice.children` attributes should be equal::
162162

163-
>>> dba_cell.tree == dba_cell.arrangement
163+
>>> dba_cell.children == dba_cell.sequence
164164
True
165165

166-
On the other hand, the :attr:`~Lattice.lattice` attribute of the :code:`dba_ring` should look different then its :attr:`~Lattice.tree`::
166+
On the other hand, the :attr:`~Lattice.sequence` attribute of the :code:`dba_ring` should look different then its :attr:`~Lattice.children`::
167167

168-
>>> dba_ring.arrangement
168+
>>> dba_ring.sequence
169169
[Drift, Dipole, Drift, Quadrupole, Drift, Dipole, Drift, Drift, Dipole, Drift, Quadrupole, Drift, Dipole, Drift, Drift, Dipole, Drift, Quadrupole, Drift, Dipole, Drift, Drift, Dipole, Drift, Quadrupole, Drift, Dipole, Drift, Drift, Dipole, Drift, Quadrupole, Drift, Dipole, Drift, Drift, Dipole, Drift, Quadrupole, Drift, Dipole, Drift, Drift, Dipole, Drift, Quadrupole, Drift, Dipole, Drift, Drift, Dipole, Drift, Quadrupole, Drift, Dipole, Drift, Drift, Dipole, Drift, Quadrupole, Drift, Dipole, Drift, Drift, Dipole, Drift, Quadrupole, Drift, Dipole, Drift, Drift, Dipole, Drift, Quadrupole, Drift, Dipole, Drift, Drift, Dipole, Drift, Quadrupole, Drift, Dipole, Drift, Drift, Dipole, Drift, Quadrupole, Drift, Dipole, Drift, Drift, Dipole, Drift, Quadrupole, Drift, Dipole, Drift, Drift, Dipole, Drift, Quadrupole, Drift, Dipole, Drift, Drift, Dipole, Drift, Quadrupole, Drift, Dipole, Drift]
170170

171171

@@ -192,7 +192,7 @@ As a nested structure is not always convenient to work with, there are three oth
192192

193193
#. The :attr:`~Lattice.sub_lattices` attribute
194194

195-
This attribute is equivalent to the :attr:`~Lattice.elements` attribute but for lattices. It contains all lattices within a given lattice, including grandchildren, great grandchildren, etc.
195+
This attribute is equivalent to the :attr:`~Lattice.elements` attribute but for lattices. It contains all sub-lattices within a given lattice, including grandchildren, great grandchildren, etc.
196196
The :attr:`~Lattice.sub_lattices` attribute should be empty for the :code:`dba_cell` as it does not contain any other lattices::
197197

198198
>>> dba_cell.sub_lattices
@@ -201,11 +201,7 @@ As a nested structure is not always convenient to work with, there are three oth
201201
Adding and Removing Objects
202202
---------------------------
203203

204-
As adding and removing objects from the :attr:`~Lattice.tree` significantly increased the
205-
code complextetiy, it was decided that :attr:`~Lattice.tree` cannont be altered after the
206-
:class:`Lattice` was created. If you needed to add/remove an object just create a new
207-
:class:`Lattice` object or initially add an :class:`Element` with length zero, which can
208-
be altered when needed.
204+
As adding and removing objects from the :attr:`~Lattice.children` significantly increased the code complextetiy, so it was decided that :attr:`~Lattice.children` cannont be altered after a :class:`Lattice` instance was created. If you needed to add/remove an object just create a new :class:`Lattice` instance or add an :class:`Element` with length zero, which can be altered when needed.
209205

210206
Load and Save Lattice Files
211207
---------------------------
@@ -289,7 +285,6 @@ Signals and Events
289285

290286
As we have already seen in the :ref:`parent-lattices` section, the :attr:`~Lattice.length` of of a :class:`Lattice` gets updated whenever the length of one of its :class:`Element` objects changes. The same happens for the transfer matrices of the :class:`Twiss` object. This is not only convenient - as one does not have to call an :func:`update` function every time an attribute changes - but is also more efficient, because apace has internal knowledge about which elements have changed and can accordingly only update the transfer matrices which have actually changed.
291287

292-
293288
This is achieved by a so called `Observer Pattern <https://wikipedia.org/wiki/Observer_pattern>`_, where an **subject** emits an **event** to all its **observers** whenever its state changes.
294289

295290
These events are implemented by the :class:`Signal` class. A callback can be connected to a given :class:`Signal` through the :meth:`~Signal.connect` method. Calling an instance of the :class:`Signal` will have the same effect as calling all connected callbacks.

examples/fodo_twiss.py

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

2424
print(
2525
f"Overview of {fodo_ring.name}",
26-
f"Num of elements: {len(fodo_ring.arrangement)}",
26+
f"Num of elements: {len(fodo_ring.sequence)}",
2727
f"Lattice Length : {fodo_ring.length}",
2828
f"Cell Length : {fodo_cell.length}",
2929
sep="\n",

0 commit comments

Comments
 (0)