-
Notifications
You must be signed in to change notification settings - Fork 9
Lint mapping table #480
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
Merged
Merged
Lint mapping table #480
Changes from 15 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
4dc1d5a
update data types
m-philipps 5414b96
add checks for mapping table
m-philipps 465c3a0
Use SBML in test
m-philipps 6b0e65d
ensure that all observables have a noise_formula
m-philipps 8660033
rollback
m-philipps e164fed
remove lint checks that are covered by pydantic
m-philipps f938fee
move from sciml pr
dilpath 8c59d6a
move more from sciml pr
dilpath fcf4e79
fix ruff
BSnelling 10dabc6
aliased model logic
BSnelling d903f22
keep FIXME comment
BSnelling 10b1fe9
Apply suggestions from code review
BSnelling 56434cd
update test to pysb and feedback from code review
BSnelling 97d3570
ignore mapping table and update test
BSnelling f7943b3
Update petab/v2/lint.py
BSnelling 72d7a29
update parameter check docstring
BSnelling File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -44,6 +44,7 @@ | |
| "CheckPriorDistribution", | ||
| "CheckUndefinedExperiments", | ||
| "CheckInitialChangeSymbols", | ||
| "CheckMappingTable", | ||
| "lint_problem", | ||
| "default_validation_tasks", | ||
| ] | ||
|
|
@@ -445,31 +446,31 @@ def run(self, problem: Problem) -> ValidationIssue | None: | |
|
|
||
| # check for uniqueness of all primary keys | ||
| counter = Counter(c.id for c in problem.conditions) | ||
| duplicates = {id_ for id_, count in counter.items() if count > 1} | ||
| duplicates = sorted(id_ for id_, count in counter.items() if count > 1) | ||
|
|
||
| if duplicates: | ||
| return ValidationError( | ||
| f"Condition table contains duplicate IDs: {duplicates}" | ||
| ) | ||
|
|
||
| counter = Counter(o.id for o in problem.observables) | ||
| duplicates = {id_ for id_, count in counter.items() if count > 1} | ||
| duplicates = sorted(id_ for id_, count in counter.items() if count > 1) | ||
|
|
||
| if duplicates: | ||
| return ValidationError( | ||
| f"Observable table contains duplicate IDs: {duplicates}" | ||
| ) | ||
|
|
||
| counter = Counter(e.id for e in problem.experiments) | ||
| duplicates = {id_ for id_, count in counter.items() if count > 1} | ||
| duplicates = sorted(id_ for id_, count in counter.items() if count > 1) | ||
|
|
||
| if duplicates: | ||
| return ValidationError( | ||
| f"Experiment table contains duplicate IDs: {duplicates}" | ||
| ) | ||
|
|
||
| counter = Counter(p.id for p in problem.parameters) | ||
| duplicates = {id_ for id_, count in counter.items() if count > 1} | ||
| duplicates = sorted(id_ for id_, count in counter.items() if count > 1) | ||
|
|
||
| if duplicates: | ||
| return ValidationError( | ||
|
|
@@ -508,7 +509,9 @@ def run(self, problem: Problem) -> ValidationIssue | None: | |
| for experiment in problem.experiments: | ||
| # Check that there are no duplicate timepoints | ||
| counter = Counter(period.time for period in experiment.periods) | ||
| duplicates = {time for time, count in counter.items() if count > 1} | ||
| duplicates = sorted( | ||
| time for time, count in counter.items() if count > 1 | ||
| ) | ||
| if duplicates: | ||
| messages.append( | ||
| f"Experiment {experiment.id} contains duplicate " | ||
|
|
@@ -551,7 +554,10 @@ def run(self, problem: Problem) -> ValidationIssue | None: | |
|
|
||
| class CheckAllParametersPresentInParameterTable(ValidationTask): | ||
| """Ensure all required parameters are contained in the parameter table | ||
| with no additional ones.""" | ||
| with no additional ones. | ||
|
|
||
| This also ensures that the mapping table petab ids | ||
| are used in the PEtab problem.""" | ||
|
|
||
| def run(self, problem: Problem) -> ValidationIssue | None: | ||
| if problem.model is None: | ||
|
|
@@ -825,17 +831,17 @@ def run(self, problem: Problem) -> ValidationIssue | None: | |
|
|
||
| if parameter.prior_distribution not in PRIOR_DISTRIBUTIONS: | ||
| messages.append( | ||
| f"Prior distribution `{parameter.prior_distribution}' " | ||
| f"for parameter `{parameter.id}' is not valid." | ||
| f"Prior distribution `{parameter.prior_distribution}` " | ||
| f"for parameter `{parameter.id}` is not valid." | ||
| ) | ||
| continue | ||
|
|
||
| if ( | ||
| exp_num_par := self._num_pars[parameter.prior_distribution] | ||
| ) != len(parameter.prior_parameters): | ||
| messages.append( | ||
| f"Prior distribution `{parameter.prior_distribution}' " | ||
| f"for parameter `{parameter.id}' requires " | ||
| f"Prior distribution `{parameter.prior_distribution}` " | ||
| f"for parameter `{parameter.id}` requires " | ||
| f"{exp_num_par} parameters, but got " | ||
| f"{len(parameter.prior_parameters)} " | ||
| f"({parameter.prior_parameters})." | ||
|
|
@@ -848,8 +854,8 @@ def run(self, problem: Problem) -> ValidationIssue | None: | |
| _ = parameter.prior_dist.sample(1) | ||
| except Exception as e: | ||
| messages.append( | ||
| f"Prior parameters `{parameter.prior_parameters}' " | ||
| f"for parameter `{parameter.id}' are invalid " | ||
| f"Prior parameters `{parameter.prior_parameters}` " | ||
| f"for parameter `{parameter.id}` are invalid " | ||
| f"(hint: {e})." | ||
| ) | ||
|
|
||
|
|
@@ -874,16 +880,16 @@ def run(self, problem: Problem) -> ValidationIssue | None: | |
| continue | ||
|
|
||
| messages.append( | ||
| f"Measurement `{measurement}' does not have a model ID, " | ||
| f"Measurement `{measurement}` does not have a model ID, " | ||
| "but there are multiple models available. " | ||
| "Please specify the model ID in the measurement table." | ||
| ) | ||
| continue | ||
|
|
||
| if measurement.model_id not in available_models: | ||
| messages.append( | ||
| f"Measurement `{measurement}' has model ID " | ||
| f"`{measurement.model_id}' which does not match " | ||
| f"Measurement `{measurement}` has model ID " | ||
| f"`{measurement.model_id}` which does not match " | ||
| "any of the available models: " | ||
| f"{available_models}." | ||
| ) | ||
|
|
@@ -894,6 +900,79 @@ def run(self, problem: Problem) -> ValidationIssue | None: | |
| return None | ||
|
|
||
|
|
||
| class CheckMappingTable(ValidationTask): | ||
| """Validate the mapping table.""" | ||
|
|
||
| def run(self, problem: Problem) -> ValidationIssue | None: | ||
| # Mapping table is optional | ||
| if not problem.mappings: | ||
| return None | ||
|
|
||
| messages = [] | ||
|
|
||
| # Check that each id, across both the petabEntityId and | ||
| # modelEntityId columns, occurs only once | ||
| must_be_unique_ids = [] | ||
| for mapping in problem.mappings: | ||
| petab_id = mapping.petab_id | ||
| model_id = mapping.model_id | ||
|
|
||
| if petab_id: | ||
| must_be_unique_ids.append(petab_id) | ||
| # Identity mappings are permitted for annotation | ||
| if petab_id == model_id: | ||
| continue | ||
| if model_id: | ||
| must_be_unique_ids.append(model_id) | ||
|
|
||
| non_unique_ids = sorted( | ||
| id_ | ||
| for id_, count in Counter(must_be_unique_ids).items() | ||
| if count > 1 | ||
| ) | ||
| if non_unique_ids: | ||
| return ValidationError( | ||
| f"Mapping table contains non-unique IDs: {non_unique_ids}." | ||
| ) | ||
|
|
||
| # petabEntityId is not defined elsewhere in the PEtab problem | ||
| new_petab_ids = { | ||
| m.petab_id | ||
| for m in problem.mappings | ||
| # Ignore identity mappings used for annotation | ||
| if m.petab_id != m.model_id | ||
| } | ||
| old_petab_ids = ( | ||
| {c.id for c in problem.conditions} | ||
| | {e.id for e in problem.experiments} | ||
| | {o.id for o in problem.observables} | ||
| ) | ||
| if overdefined_ids := sorted(new_petab_ids & old_petab_ids): | ||
| messages.append( | ||
| f"PEtab IDs `{overdefined_ids}` are " | ||
| "defined in the mapping table but also defined through " | ||
| "other PEtab tables." | ||
| ) | ||
|
|
||
| for mapping in problem.mappings: | ||
| # petabEntityId not referenced in any model, if alias | ||
| for model in problem.models: | ||
| if ( | ||
| mapping.petab_id != mapping.model_id | ||
| and model.has_entity_with_id(mapping.petab_id) | ||
| ): | ||
| messages.append( | ||
| f"`{mapping.petab_id}` is used in the mapping " | ||
| "table and referenced directly in the model " | ||
| f"`{model.model_id}`." | ||
| ) | ||
|
|
||
| if messages: | ||
| return ValidationError("\n".join(messages)) | ||
|
|
||
| return None | ||
|
|
||
|
|
||
| def get_valid_parameters_for_parameter_table( | ||
| problem: Problem, | ||
| ) -> set[str]: | ||
|
|
@@ -984,13 +1063,9 @@ def get_required_parameters_for_parameter_table( | |
| for change in cond.changes | ||
| } | ||
|
|
||
| # Add parameters from measurement table, unless they are fixed parameters | ||
| def append_overrides(overrides): | ||
| parameter_ids.update( | ||
| str_p | ||
| for p in overrides | ||
| if isinstance(p, sp.Symbol) | ||
| and (str_p := str(p)) not in condition_targets | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not necessary since this is collected as a set and the |
||
| str(p) for p in overrides if isinstance(p, sp.Symbol) | ||
| ) | ||
|
|
||
| for m in problem.measurements: | ||
|
|
@@ -1033,7 +1108,7 @@ def append_overrides(overrides): | |
| if not problem.model.has_entity_with_id(str(p)) | ||
| ) | ||
|
|
||
| # parameters that are overridden via the condition table are not allowed | ||
| # Parameters that are overridden via the condition table are not allowed | ||
| parameter_ids -= condition_targets | ||
|
|
||
| return parameter_ids | ||
|
|
@@ -1090,5 +1165,5 @@ def get_placeholders( | |
| CheckUnusedConditions(), | ||
| CheckPriorDistribution(), | ||
| CheckInitialChangeSymbols(), | ||
| # TODO validate mapping table | ||
| CheckMappingTable(), | ||
| ] | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where does it do this? There is no
ValidationErrorwith a message like "Unused mapping table PEtab IDs: ...."There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, it doesn't do this yet as I'm planning to handle mapping/parameter table interaction in the sciml branch. I've removed from the docstring.