Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to get elements by (unique made) names from line.get_table() #561

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions tests/test_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -1092,4 +1092,14 @@ def test_insert_repeated_names():
line.insert_element("m2",xt.Marker(),at="d")
assert line.element_names[0]=="m2"

def test_line_table_unique_names():
line = xt.Line(
elements = {"obm": xt.Bend(length=0.5)},
element_names= ["obm","obm"]
)
table = line.get_table()
assert np.all(np.unique_counts(table.name).counts == 1), "Not all elements are unique"
for name, env_name in zip(table.name, table.env_name):
if name == '_end_point': continue
assert line[name] == line[env_name]

2 changes: 2 additions & 0 deletions xtrack/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -4057,6 +4057,8 @@ def __getitem__(self, key):
return self.vv[key]
elif hasattr(self, 'lines') and key in self.lines: # Want to reuse the method for the env
return self.lines[key]
elif "::" in key and (env_name := key.split("::")[0]) in self.element_dict:
return self[env_name]
else:
raise KeyError(f'Name {key} not found')

Expand Down