Skip to content

Commit 9398e77

Browse files
authored
New validation API (#288)
For petab.v2, refactor the current validation setup to * be more modular * allow PEtab extensions to register additional validation steps in the future * make validation messages accessible programmatically (previously, they were logged directly) Also: * Make `petablint` work with both v1 and v2 problems. * Add first validation rules that are specific to v2
1 parent 6fff855 commit 9398e77

File tree

6 files changed

+731
-37
lines changed

6 files changed

+731
-37
lines changed

doc/modules.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,6 @@ API Reference
2828
petab.v1.simplify
2929
petab.v1.visualize
3030
petab.v1.yaml
31+
petab.v2
32+
petab.v2.lint
33+
petab.v2.problem

petab/petablint.py

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,13 @@
88

99
from colorama import Fore
1010
from colorama import init as init_colorama
11+
from jsonschema.exceptions import ValidationError as SchemaValidationError
1112

1213
import petab
14+
from petab.v1.C import FORMAT_VERSION
15+
from petab.v2.lint import lint_problem
16+
from petab.versions import get_major_version
17+
from petab.yaml import validate
1318

1419
logger = logging.getLogger(__name__)
1520

@@ -153,13 +158,9 @@ def main():
153158
ch.setLevel(logging.WARN)
154159

155160
if args.yaml_file_name:
156-
from jsonschema.exceptions import ValidationError
157-
158-
from petab.yaml import validate
159-
160161
try:
161162
validate(args.yaml_file_name)
162-
except ValidationError as e:
163+
except SchemaValidationError as e:
163164
logger.error(
164165
"Provided YAML file does not adhere to PEtab " f"schema: {e}"
165166
)
@@ -171,38 +172,54 @@ def main():
171172
# problem = petab.CompositeProblem.from_yaml(args.yaml_file_name)
172173
return
173174

174-
problem = petab.Problem.from_yaml(args.yaml_file_name)
175-
176-
else:
177-
# DEPRECATED
178-
logger.debug("Looking for...")
179-
if args.sbml_file_name:
180-
logger.debug(f"\tSBML model: {args.sbml_file_name}")
181-
if args.condition_file_name:
182-
logger.debug(f"\tCondition table: {args.condition_file_name}")
183-
if args.observable_file_name:
184-
logger.debug(f"\tObservable table: {args.observable_file_name}")
185-
if args.measurement_file_name:
186-
logger.debug(f"\tMeasurement table: {args.measurement_file_name}")
187-
if args.parameter_file_name:
188-
logger.debug(f"\tParameter table: {args.parameter_file_name}")
189-
if args.visualization_file_name:
190-
logger.debug(
191-
"\tVisualization table: " f"{args.visualization_file_name}"
192-
)
175+
match get_major_version(args.yaml_file_name):
176+
case 1:
177+
problem = petab.Problem.from_yaml(args.yaml_file_name)
178+
ret = petab.lint.lint_problem(problem)
179+
sys.exit(ret)
180+
case 2:
181+
validation_issues = lint_problem(args.yaml_file_name)
182+
if validation_issues:
183+
validation_issues.log(logger=logger)
184+
sys.exit(1)
185+
logger.info("PEtab format check completed successfully.")
186+
sys.exit(0)
187+
case _:
188+
logger.error(
189+
"The provided PEtab files are of unsupported version "
190+
f"or the `{FORMAT_VERSION}` field is missing in the yaml "
191+
"file."
192+
)
193+
194+
# DEPRECATED - only supported for v1
195+
logger.debug("Looking for...")
196+
if args.sbml_file_name:
197+
logger.debug(f"\tSBML model: {args.sbml_file_name}")
198+
if args.condition_file_name:
199+
logger.debug(f"\tCondition table: {args.condition_file_name}")
200+
if args.observable_file_name:
201+
logger.debug(f"\tObservable table: {args.observable_file_name}")
202+
if args.measurement_file_name:
203+
logger.debug(f"\tMeasurement table: {args.measurement_file_name}")
204+
if args.parameter_file_name:
205+
logger.debug(f"\tParameter table: {args.parameter_file_name}")
206+
if args.visualization_file_name:
207+
logger.debug(
208+
"\tVisualization table: " f"{args.visualization_file_name}"
209+
)
193210

194-
try:
195-
problem = petab.Problem.from_files(
196-
sbml_file=args.sbml_file_name,
197-
condition_file=args.condition_file_name,
198-
measurement_file=args.measurement_file_name,
199-
parameter_file=args.parameter_file_name,
200-
observable_files=args.observable_file_name,
201-
visualization_files=args.visualization_file_name,
202-
)
203-
except FileNotFoundError as e:
204-
logger.error(e)
205-
sys.exit(1)
211+
try:
212+
problem = petab.Problem.from_files(
213+
sbml_file=args.sbml_file_name,
214+
condition_file=args.condition_file_name,
215+
measurement_file=args.measurement_file_name,
216+
parameter_file=args.parameter_file_name,
217+
observable_files=args.observable_file_name,
218+
visualization_files=args.visualization_file_name,
219+
)
220+
except FileNotFoundError as e:
221+
logger.error(e)
222+
sys.exit(1)
206223

207224
ret = petab.lint.lint_problem(problem)
208225
sys.exit(ret)

0 commit comments

Comments
 (0)