Skip to main content

Python wrapper for RDFRules.

Project description

PyPI - Version PyPI - Python Version PyPI - Python Version

PyRDFRules

RDFRules Quickstart Documentation VŠE


Table of Contents

PyRDFRules

PyRDFRules is a Python wrapper for the graph rule mining tool RDFRules. Currently supports the RDFRules version 1.9.0. The added value of this library is exposing a Pythonic interface to define pipelines for tasks that can be handled by RDFRules, as well as provisioning RDFRules locally, and finally, some convenience methods and other APIs are added to faciliate easier usage of RDFRules.

RDFRules

RDFRules is a powerful analytical tool for rule mining from RDF knowledge graphs. It offers a complex rule mining solution including RDF data pre-processing, rules post-processing and prediction abilities from rules. The core of RDFRules is written in the Scala language. Besides the Scala API, RDFRules also provides REST web service with graphical user interface via a web browser.

Repository for RDFRules can be found at propi/rdfrules.

Quickstart

To try out PyRDFRules, you can use Google Colab to provision an environment for you.

  • Template RDFRules Notebook - use this notebook as a start for your analysis workloads, provisions the PyRDFRules library and local RDFRules.
  • Pipeline sample - a sample pipeline on a local instance of RDFRules, from starting the instance to getting the results.
  • Histogram example Showcases the histogram functionality of (Py)RDFRules, and can be used as a first step in analysis workloads for to understand the data.

Getting started

PyRDFRules is distributed as a Python library through PyPi. The primary goal of this API is to faciliate the easy use of RDFRules through a Python interface.

Prerequisites

A minimum Python version of 3.12.2 is required. You can check your Python version using python --version.

Installation

pip install pyrdfrules

Usage

Full documentation is available at a dedicated documentation site. Code samples can be found in the sample directory, including a Python notebook.

Currently, using a remote HTTP instance of RDFRules or a local instance of RDFRules is supported. Automatic installation of JVM if not present and of RDFRules is supported, and the library takes care of running the RDFRules application.

Remote instance

To connect to a remote instance of RDFRules, create an application and use the start_remote method.

from pydantic_core import Url

import pyrdfrules.application

app = pyrdfrules.application.Application()

rdfrules = app.start_remote(
    url = Url("http://YOUR_RDFRULES_INSTANCE/api/")
)

Local instance

To set up a local instance of PyRDFRules

# Recommended: Configure your workspace directory.

config = Config(
    workspace_path=os.path.realpath("YOUR_WORKSPACE_DIRECTORY")
)

app = pyrdfrules.application.Application()
        
app.start_local(
    install_jvm = True, # If you wish for Python to install JVM for you, set to true.
    install_rdfrules = True, # If you wish for Python to install RDFRules, set to true.
    rdfrules_path = "...", # Installation path for RDFRules. If you set install_rdfrules to False, it will expect RDFRule to be installed in this location.
    jvm_path = "", # Installation path for the JVM.
    config = config
)

As a last step, launch the pipeline, wait for all results and print the head, body and measures of each mined rule.

Run a task

A task is a series of steps (a pipeline) provided to RDFRules. Tasks are used to mine rules, index, cache or otherwise manipulate data...

If you have a JSON task ready, you can execute it in the following way:

from pyrdfrules.common.task.task import Task

task : Task = None

with open("./task.json", "r") as file:        
    task_json_from_file = file.read()
    task = rdfrules.task.create_task_from_string(task_json_from_file)

You can also specify the pipeline in code.

pipeline = Pipeline(
    tasks=[
        LoadGraph(
            graphName = "<dbpedia>",
            path = "/dbpedia_yago/mappingbased_objects_sample.ttl"
        ),
        ... # your other tasks go here
        GetRules()
    ]
)

task = self.rdfrules.task.create_task(pipeline)
    
for step in self.rdfrules.task.run_task(task):
      print(step)
  
print(task.result) # access task result dictionary - pure output from RDFRules
print(task.get_result()) # returns formatted outputs

Task execution is non blocking and you can stop it, as long as it is not finished in RDFRules.

Full pipeline sample matching the DBpedia & YAGO example from the RDFRules web instance can be found in documentation doc, or in src/tests/test_pipeline.py.

Developing

To initialize your environment:

./init.sh

To run a build and run jupyter lab:

./run.sh

Releasing a new version

  • Up the version in src/__about__.py.
  • Run hatch build
  • Run hatch publish

Alternatively, after upping the version, run ./build.sh.

Updating RDFRules

  • In src/pyrdfrules/rdfrules/release.py update the URI to the RDFRules HTTP ZIP path.
  • If the startup options change, please ensure that the function start_rdfrules in src/pyrdfrules/engine/util/jvm.py matches the changes.

Roadmap

  • Sample interface
  • Implement JSON serialization of pipeline
  • Implement communication with RDFRules

Contributing

If you have a suggestion to improve this project, please fork the repo and create a pull request. If you encounter any issues, please do raise an issue with an appropriate tag. Feature requests, enhancements and bug reports are welcome.

To contribute to this project, first:

  • Fork the Project
  • Create your Feature Branch (git checkout -b feature/AmazingFeature)
  • Commit your Changes (git commit -m 'Add some AmazingFeature')
  • Push to the Branch (git push origin feature/AmazingFeature)
  • Open a Pull Request

License

pyrdfrules is distributed under the terms of the Apache License. See LICENSE for more information.

Acknowledgments

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pyrdfrules-1.0.2.tar.gz (48.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pyrdfrules-1.0.2-py3-none-any.whl (60.1 kB view details)

Uploaded Python 3

File details

Details for the file pyrdfrules-1.0.2.tar.gz.

File metadata

  • Download URL: pyrdfrules-1.0.2.tar.gz
  • Upload date:
  • Size: 48.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.25.1

File hashes

Hashes for pyrdfrules-1.0.2.tar.gz
Algorithm Hash digest
SHA256 4f6d277dec705ee3f9419c8dcb20f28dad386fc831a07c44f2a79605a34adafc
MD5 56873cc43a5f71c9a8c8f1679f79a714
BLAKE2b-256 5a9d221a2a011cee39272d8b8a1daa5c3f7103873ca80e3955d27e2324e7cc7d

See more details on using hashes here.

File details

Details for the file pyrdfrules-1.0.2-py3-none-any.whl.

File metadata

  • Download URL: pyrdfrules-1.0.2-py3-none-any.whl
  • Upload date:
  • Size: 60.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: python-httpx/0.25.1

File hashes

Hashes for pyrdfrules-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 e36a395efa13e0c7b2b1c24eeed573dcc5d25514f4b4d7e1e67b0b286a4717fa
MD5 859ab3746fb4c115d95746d245a8ca1d
BLAKE2b-256 a8a1d527ce01eb7e3b121038472575556d7e728ef25c3dd272c0c5469b46b386

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page