-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
incorporating validation for CellML 2.0
- Loading branch information
Showing
15 changed files
with
172 additions
and
7 deletions.
There are no files selected for viewing
This file contains 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
Empty file.
This file contains 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 |
---|---|---|
@@ -0,0 +1,61 @@ | ||
""" Utilities for validating CellML models | ||
:Author: Jonathan Karr <[email protected]> | ||
:Date: 2021-05-10 | ||
:Copyright: 2021, Center for Reproducible Biomedical Modeling | ||
:License: MIT | ||
""" | ||
|
||
import libcellml | ||
import os | ||
|
||
|
||
def validate_model(filename, name=None): | ||
""" Check that a model is valid | ||
Args: | ||
filename (:obj:`str`): path to model | ||
name (:obj:`str`, optional): name of model for use in error messages | ||
Returns: | ||
:obj:`tuple`: | ||
* nested :obj:`list` of :obj:`str`: nested list of errors (e.g., required ids missing or ids not unique) | ||
* nested :obj:`list` of :obj:`str`: nested list of errors (e.g., required ids missing or ids not unique) | ||
""" | ||
errors = [] | ||
warnings = [] | ||
|
||
if not os.path.isfile(filename): | ||
errors.append(['`{}` is not a file.'.format(filename)]) | ||
return (errors, warnings) | ||
|
||
# read model | ||
parser = libcellml.Parser() | ||
with open(filename, 'r') as file: | ||
model = parser.parseModel(file.read()) | ||
|
||
for i_error in range(parser.errorCount()): | ||
error = parser.error(i_error) | ||
errors.append([error.description()]) | ||
|
||
for i_warning in range(parser.warningCount()): | ||
warning = parser.warning(i_warning) | ||
warnings.append([warning.description()]) | ||
|
||
if errors: | ||
return (errors, warnings) | ||
|
||
# validate model | ||
validator = libcellml.Validator() | ||
validator.validateModel(model) | ||
|
||
for i_error in range(validator.errorCount()): | ||
error = validator.error(i_error) | ||
errors.append([error.description()]) | ||
|
||
for i_warning in range(validator.warningCount()): | ||
warning = validator.warning(i_warning) | ||
warnings.append([warning.description()]) | ||
|
||
return (errors, warnings) |
This file contains 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 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 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
# [cellml] | ||
# libcellml | ||
[cellml] | ||
libcellml | ||
|
||
[neuroml] | ||
libneuroml | ||
|
This file contains 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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from biosimulators_utils.cellml.validation import validate_model | ||
from biosimulators_utils.utils.core import flatten_nested_list_of_strings | ||
import os | ||
import unittest | ||
|
||
|
||
class CellMlValidationTestCase(unittest.TestCase): | ||
FIXTURE_DIR = os.path.join(os.path.dirname(__file__), '..', 'fixtures', 'cellml') | ||
|
||
def test(self): | ||
errors, warnings = validate_model(os.path.join(self.FIXTURE_DIR, 'level2.xml')) | ||
self.assertEqual(errors, []) | ||
self.assertEqual(warnings, []) | ||
|
||
errors, warnings = validate_model(os.path.join(self.FIXTURE_DIR, 'not_a_path.xml')) | ||
self.assertIn("is not a file", flatten_nested_list_of_strings(errors)) | ||
self.assertEqual(warnings, []) | ||
|
||
errors, warnings = validate_model(os.path.join(self.FIXTURE_DIR, 'invalid_cellml_2.0.xml')) | ||
self.assertIn("Start tag expected", flatten_nested_list_of_strings(errors)) | ||
self.assertEqual(warnings, []) | ||
|
||
errors, warnings = validate_model(os.path.join(self.FIXTURE_DIR, 'missing-attribute.xml')) | ||
self.assertIn("does not have a valid name attribute", flatten_nested_list_of_strings(errors)) | ||
self.assertEqual(warnings, []) |
This file contains 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 |
---|---|---|
@@ -0,0 +1 @@ | ||
This is not a CellML file. |
This file contains 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?xml version="1.0"?> | ||
<model xmlns="http://www.cellml.org/cellml/2.0#" xmlns:cellml="http://www.cellml.org/cellml/2.0#" xmlns:xlink="http://www.w3.org/1999/xlink" name="level2_model"> | ||
<component name="level2_component"> | ||
<variable name="time" units="dimensionless" interface="public"/> | ||
<variable name="parameter" units="dimensionless" interface="public"/> | ||
<variable name="cosine" units="dimensionless" interface="public" initial_value="0"/> | ||
<math xmlns="http://www.w3.org/1998/Math/MathML"> | ||
<apply><eq/> | ||
<apply><diff/> | ||
<bvar><ci>time</ci></bvar> | ||
<ci>cosine</ci> | ||
</apply> | ||
<apply><sin/> | ||
<apply><times/> | ||
<ci>parameter</ci> | ||
<ci>time</ci> | ||
</apply> | ||
</apply> | ||
</apply> | ||
</math> | ||
</component> | ||
<component name="isnt_imported"/> | ||
<import xlink:href="this_file_should_not_exist..._ever!.xml"> | ||
<component name="also_never_imported" component_ref="not_sure_what_to_look_for"/> | ||
</import> | ||
</model> |
This file contains 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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
<?xml version="1.0"?> | ||
<model xmlns="http://www.cellml.org/cellml/2.0#" xmlns:cellml="http://www.cellml.org/cellml/2.0#" xmlns:xlink="http://www.w3.org/1999/xlink" name="level2_model"> | ||
<component> | ||
<variable name="time" units="dimensionless" interface="public"/> | ||
<variable name="parameter" units="dimensionless" interface="public"/> | ||
<variable name="cosine" units="dimensionless" interface="public" initial_value="0"/> | ||
<math xmlns="http://www.w3.org/1998/Math/MathML"> | ||
<apply><eq/> | ||
<apply><diff/> | ||
<bvar><ci>time</ci></bvar> | ||
<ci>cosine</ci> | ||
</apply> | ||
<apply><sin/> | ||
<apply><times/> | ||
<ci>parameter</ci> | ||
<ci>time</ci> | ||
</apply> | ||
</apply> | ||
</apply> | ||
</math> | ||
</component> | ||
<component name="isnt_imported"/> | ||
<import xlink:href="this_file_should_not_exist..._ever!.xml"> | ||
<component name="also_never_imported" component_ref="not_sure_what_to_look_for"/> | ||
</import> | ||
</model> |
File renamed without changes.
File renamed without changes.
This file contains 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 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 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