跳转到主要内容

一个Python库和一组命令行工具,用于交换符合或与Biolink模型对齐的知识图谱(KG)。

项目描述

知识图谱交换

Python Run testsDocumentation Status Quality Gate Status Maintainability Rating Coverage PyPI Docker

KGX(知识图谱交换)是一个Python库和一组命令行工具,用于交换符合或与Biolink模型对齐的知识图谱(KG)。

核心数据模型是一个属性图(PG),在Python中使用networkx MultiDiGraph模型内部表示。

KGX允许转换为和从

KGX 还将提供验证,确保知识图谱符合 Biolink 模型:确保节点使用 Biolink 类别进行分类,边使用有效的 Biolink 关系类型进行标记,并使用有效属性。

内部表示是属性图,具体是 networkx MultiDiGraph。

该图的预期结构符合 Biolink 模型标准,如KGX 格式规范中指定。

除了主要代码库外,KGX 还提供了一系列命令行操作

示例用法

验证

poetry run kgx validate -i tsv tests/resources/merge/test2_nodes.tsv tests/resources/merge/test2_edges.tsv

合并

poetry run kgx merge —merge-config tests/resources/test-merge.yaml 

图摘要

poetry run kgx graph-summary -i tests/resources/graph_nodes.tsv  -o summary.txt

转换

poetry run kgx transform —transform-config tests/resources/test-transform-tsv-rdf.yaml

错误检测和报告

KGX Transformer、Validator、GraphSummary 和 MetaKnowledgeGraph 操作现在提供非冗余的 JSON 格式结构化错误日志。请参阅各种单元测试以了解通用设计模式(以 Validator 为例)。

from kgx.validator import Validator
from kgx.transformer import Transformer

Validator.set_biolink_model("2.11.0")

# Validator assumes the currently set Biolink Release
validator = Validator()

transformer = Transformer(stream=True)

transformer.transform(
    input_args = {
        "filename": [
            "graph_nodes.tsv",
            "graph_edges.tsv",
        ],
        "format": "tsv",
    },
    output_args={
        "format": "null"
    },
    inspector=validator,
)

# Both the Validator and the Transformer can independently capture errors

# The Validator, from the overall semantics of the graph...
# Here, we just report severe Errors from the Validator (no Warnings)
validator.write_report(open("validation_errors.json", "w"), "Error")

# The Transformer, from the syntax of the input files... 
# Here, we catch *all* Errors and Warnings (by not providing a filter)
transformer.write_report(open("input_errors.json", "w"))

JSON 错误输出将类似于以下内容

{
    "ERROR": {
        "MISSING_EDGE_PROPERTY": {
            "Required edge property 'id' is missing": [
                "A:123->X:1",
                "B:456->Y:2"
            ],
            "Required edge property 'object' is missing": [
                "A:123->X:1"
            ],
            "Required edge property 'predicate' is missing": [
                "A:123->X:1"
            ],
            "Required edge property 'subject' is missing": [
                "A:123->X:1",
                "B:456->Y:2"
            ]
        }
    },
    "WARNING": {
        "DUPLICATE_NODE": {
          "Node 'id' duplicated in input data": [
            "MONDO:0010011",
            "REACT:R-HSA-5635838"
          ]
        }
    }
}

该系统减少了早期行导向 KGX 日志文本输出文件的显著冗余,即具有相同错误类的图实体仅按名称/标识符的列表在 JSON 结构的叶子级别进行聚合。

顶级 JSON 标签来自 error_detection 模块中的 MessageLevel 类,而第二级标签来自 ErrorType 类,第三级消息在代码中硬编码为 log_error 方法消息。

未来很可能使用此通用框架有效地捕获并报告 KGX 中的附加错误条件。

安装

从 PyPI 安装

KGX 可在 PyPI 上找到,并可以使用以下 pip 安装:

pip install kgx

要安装特定版本的 KGX,请务必指定版本号:

pip install kgx==0.5.0

从 GitHub 安装

克隆 GitHub 仓库,然后安装:

git clone https://github.com/biolink/kgx
cd kgx
poetry install

为 Neo4j 设置测试环境

KGX 的此版本支持与 Neo4j 4.3 版本的图源和目标事务。

KGX 具有一套依赖于 Docker 容器来运行特定于 Neo4j 测试的测试。

要设置所需的容器,请首先在您的本地机器上安装 Docker

一旦 Docker 启动并运行,请运行以下命令

docker run -d --rm --name kgx-neo4j-integration-test -p 7474:7474 -p 7687:7687 --env NEO4J_AUTH=neo4j/test neo4j:4.3
docker run -d --rm --name kgx-neo4j-unit-test -p 8484:7474 -p 8888:7687 --env NEO4J_AUTH=neo4j/test neo4j:4.3

注意:设置 Neo4j 容器是可选的。如果没有设置容器,则依赖于它们的测试将被跳过。

项目详情


下载文件

下载适合您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码分发

kgx-2.4.2.tar.gz (95.6 kB 查看哈希值)

上传时间 源代码

构建分发

kgx-2.4.2-py3-none-any.whl (119.0 kB 查看哈希值)

上传时间 Python 3

由以下组织支持