예제 #1
0
def is_sbml_consistent(sbml_document: libsbml.SBMLDocument, check_units=False):
    """Check for SBML validity / consistency

    Arguments:
        sbml_document: SBML document to check
        check_units: Also check for unit-related issues
    Returns:
        False if problems were detected, otherwise True
    """

    if not check_units:
        sbml_document.setConsistencyChecks(
            libsbml.LIBSBML_CAT_UNITS_CONSISTENCY, False)

    has_problems = sbml_document.checkConsistency()
    if has_problems:
        log_sbml_errors(sbml_document)
        logger.warning(
            'WARNING: Generated invalid SBML model. Check messages above.')

    return not has_problems
예제 #2
0
def validate_doc(
    doc: libsbml.SBMLDocument,
    name=None,
    log_errors=True,
    units_consistency=True,
    modeling_practice=True,
    internal_consistency=True,
) -> ValidationResult:
    """Validate SBMLDocument.

    :param doc: SBMLDocument to check
    :param name: identifier or path for report
    :param log_errors: boolean flag of errors should be logged
    :param units_consistency: boolean flag units consistency
    :param modeling_practice: boolean flag modeling practise
    :param internal_consistency: boolean flag internal consistency

    :return: ValidationResult
    """
    if not name:
        name = str(doc)

    # set the unit checking, similar for the other settings
    doc.setConsistencyChecks(libsbml.LIBSBML_CAT_UNITS_CONSISTENCY,
                             units_consistency)
    doc.setConsistencyChecks(libsbml.LIBSBML_CAT_MODELING_PRACTICE,
                             modeling_practice)

    # time
    current = time.perf_counter()

    # all, error, warn
    if internal_consistency:
        results_internal = _check_consistency(doc, internal_consistency=True)
    else:
        results_internal = ValidationResult()
    results_not_internal = _check_consistency(doc, internal_consistency=False)

    # sum up
    vresults = ValidationResult.from_results(
        [results_internal, results_not_internal])

    lines = [
        "",
        "-" * 80,
        str(name),
        "{:<25}: {}".format("valid",
                            str(vresults.is_valid()).upper()),
    ]
    if not vresults.is_perfect():
        lines += [
            "{:<25}: {}".format("validation error(s)", vresults.error_count),
            "{:<25}: {}".format("validation warnings(s)",
                                vresults.warning_count),
        ]
    lines += [
        "{:<25}: {:.3f}".format("check time (s)",
                                time.perf_counter() - current),
        "-" * 80,
        "",
    ]
    info = "\n".join(lines)

    if vresults.is_valid():
        info = bcolors.OKGREEN + info + bcolors.ENDC
    else:
        info = bcolors.FAIL + info + bcolors.ENDC
    info = bcolors.BOLD + info + bcolors.ENDC

    # overall validation report
    if vresults.is_perfect():
        print(info)
    else:
        if vresults.is_valid():
            logging.warning(info)
        else:
            logging.error(info)

    # individual error and warning report
    if log_errors:
        vresults.log()

    return vresults