Skip to content

REF: templated comparisons in constraints tool - #189

Merged
ken-lauer merged 6 commits into
bmad-sim:masterfrom
tangkong:ref_templated_comparisons
Aug 11, 2026
Merged

REF: templated comparisons in constraints tool#189
ken-lauer merged 6 commits into
bmad-sim:masterfrom
tangkong:ref_templated_comparisons

Conversation

@tangkong

@tangkong tangkong commented Aug 3, 2026

Copy link
Copy Markdown
Collaborator

Description

  • adds ability to refer to comparisons by name (string). These names refer to a top-level list of comparisons which can be reused across the entire configuration.

Context

The current schema resulted in lots of comparison duplication, and we'd like to DRY this out a bit.

@tangkong
tangkong force-pushed the ref_templated_comparisons branch from 19acab8 to 7310709 Compare August 10, 2026 16:57
@tangkong
tangkong marked this pull request as ready for review August 10, 2026 17:05
@tangkong
tangkong requested a review from ken-lauer August 10, 2026 17:07
Comment thread pytao/constraints/config.py Outdated
crs, reg = super().run(obs_map, saved_obs_map, comparison_map, group)
if not isinstance(self.comparison, IsLess):
raise TypeError(
f"Referenced comparison ({self.comparison}) is of"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I make this mistake all the time so I pick up on it more quickly nowadays: missing a trailing space (message will be is ofincorrect type after concatenation)

Comment thread pytao/constraints/config.py Outdated
if isinstance(self.comparison, str):
if self.comparison not in comparison_map:
raise ValueError(f"Referenced comparison ({self.comparison}) not defined")
self.comparison = cast(CompT, comparison_map[self.comparison])

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dereferencing the comparison is necessary, but if you update self.comparison you can no longer serialize back to the input constraints.yaml correctly.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A good point. Perhaps it's worth stashing the object in a separate, non-serialized attribute

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There'll be more to do in the followup to ensure roundtrips are reasonable (or at least faithful to the way configs are written currently)

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I promise to DRY this out in a followup 😥

Comment thread pytao/constraints/config.py Outdated
comparison_map : dict[str, AnyComparison]
mapping string -> shared comparisons
group : str | None
_description_

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_description_

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I clearly had ambitions to complete the docstrings here but forgot about them...

Comment thread pytao/constraints/config.py Outdated
)
comparisons: dict[str, AnyComparison] = Field(
default_factory=dict,
description="Mapping from unimque comparison identifier to reusable comparison settings",

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
description="Mapping from unimque comparison identifier to reusable comparison settings",
description="Mapping from unique comparison identifier to reusable comparison settings",

A unique spelling of unique, indeed 😉

Comment thread pytao/constraints/config.py Outdated
self,
obs_map: dict[Observable, Observation],
compare_map: dict[Observable, Observation] | None,
saved_obs_map: dict[Observable, Observation] | None,

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So saved observations are like the baseline to compare against, obs_map is the current set of observations that we're going to compare using comparison_map comparisons... right?

Baseline, reference, or perhaps "expected" are all more clear (personally) from a testing/validation standpoint.

Naming is also a bit unfortunate with x_map as it doesn't tell you anything about the key or value. x_to_y is so much nicer to work with from an API/user standpoint, but observable_to_observation is too long, and both words have identical prefixes so shortening them is complicated.

OK, that's all the nitpicking I'll do about the naming. Maybe we can revisit the incredible number of classes and naming down the line.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had similar troubles regarding naming here. The saved observations here are the observations generated from the lattice files, I think "expected_obs_map" could work here.

I do intend to take a second pass at the submodule to try and simplify things, but I can make that change along with compare_map -> common_comparisons_map

@ken-lauer
ken-lauer merged commit f20df3e into bmad-sim:master Aug 11, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants