Skip to main content

CIM query utilities

Project description

PyPI version Python Versions License: MIT Code style: black codecov

CIMSPARQL Query CIM data using sparql

This Python package provides functionality for reading cim data from tripple stores such as GraphDB, BlazeGraph or Rdf4j into Python memory as pandas dataframes.

The package provides a set of predefined functions/queries to load CIM data such as generator, demand or branch data, though the user can easiliy define their own queries.

Usage

Load data using predefined functions/queries

>>> from cimsparql.graphdb import ServiceConfig
>>> from cimsparql.model import get_single_client_model
>>> model = get_single_client_model(ServiceConfig(limit=3))
>>> ac_lines = model.ac_lines()
>>> print(ac_lines[['name', 'x', 'r', 'bch']])
         name       x       r       bch
0  <branch 1>  1.9900  0.8800  0.000010
1  <branch 2>  1.9900  0.8800  0.000010
2  <branch 3>  0.3514  0.1733  0.000198

In the example above the client will query repo "" in the default server GraphDB for AC line values.

Inspect/view predefined queries

See the sparql templates folder (cimsparql/sparql) to the query used.

Load data using user specified queries

>>> from string import Template
>>> query = 'PREFIX cim:<${cim}>\nPREFIX rdf: <${rdf}>\nSELECT ?mrid where {?mrid rdf:type cim:ACLineSegment}'
>>> query_result = model.get_table_and_convert(model.template_to_query(Template(query)))
>>> print(query_result)

Prefix and namespace

Available namespace for current graphdb client (gdbc in the examples above), which can be used in queries (such as rdf and cim) can by found by

>>> print(model.prefixes)
{'wgs': 'http://www.w3.org/2003/01/geo/wgs84_pos#',
 'rdf': 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
 'owl': 'http://www.w3.org/2002/07/owl#',
 'cim': 'http://iec.ch/TC57/2010/CIM-schema-cim15#',
 'gn': 'http://www.geonames.org/ontology#',
 'xsd': 'http://www.w3.org/2001/XMLSchema#',
 'rdfs': 'http://www.w3.org/2000/01/rdf-schema#',
 'SN': 'http://www.statnett.no/CIM-schema-cim15-extension#',
 'ALG': 'http://www.alstom.com/grid/CIM-schema-cim15-extension#'}

Running Tests Against Docker Databases

Tests can be run agains RDF4J and/or BlazeGraph databases if a container with the correct images are available.

docker pull eclipse/rdf4j-workbench
docker pull openkbs/blazegraph

Launch one or both containers and specify the following environment variables

RDF4J_URL = "localhost:8080/rdf4j-server"
BLAZEGRAPH_URL = "localhost:9999/blazegraph/namespace

Note 1: The port numbers may differ depending on your local Docker configurations. Note 2: You don't have to install RDF4J or BlazeGraph. Tests requiring these will be skipped in case they are not available. They will in any case be run in the CI pipeline on GitHub (where both always are available).

Ontology (for developers)

Ontologies for the CIM model can be found at (ENTSOE's webpages)[https://www.entsoe.eu/digital/common-information-model/cim-for-grid-models-exchange/]. For convenience and testing purposes the ontology are located under tests/data/ontology. CIM models used for testing purposes in Cimsparql should be stored in N-quads format. In case you have a model in XML format it can be converted to N-quads by launching a DB (for example RDF4J) and upload all the XML files and the ontology.

Execute

PREFIX cims: <http://iec.ch/TC57/1999/rdf-schema-extensions-19990926#>

DELETE {?s ?p ?o}
INSERT {?s ?p ?o_cast} WHERE {
  ?s ?p ?o .
  ?p cims:dataType ?_dtype .
  ?_dtype cims:stereotype ?stereotype .
  BIND(IF(?stereotype = "Primitive",
    URI(concat("http://www.w3.org/2001/XMLSchema#", lcase(strafter(str(?_dtype), "#")))),
    ?_dtype) as ?dtype)
  BIND(STRDT(?o, ?dtype) as ?o_cast)
}

and export as N-quads.

Test models

  1. micro_t1_nl: MicroGrid/Type1_T1/CGMES_v2.4.15_MicroGridTestConfiguration_T1_NL_Complete_v2

Rest APIs

CimSparql mainly uses SparqlWrapper to communicate with the databases. However, there are certain operations which are performed directly via REST calls. Since there are small differences between different APIs you may have to specify which API you are using. This can be done when initializing the ServiceCfg class or by specifying the SPARQL_REST_API environment variable. Currently, RDF4J and blazegraph is supported (if not given RDF4J is default).

export SPARQL_REST_API=RDF4J  # To use RDF4J
export SPARQL_REST_API=BLAZEGRAPH  # To use BlazeGraph

Project details


Release history Release notifications | RSS feed

Download files

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

Source Distribution

cimsparql-7.0.5.tar.gz (634.8 kB view details)

Uploaded Source

Built Distribution

cimsparql-7.0.5-py3-none-any.whl (61.4 kB view details)

Uploaded Python 3

File details

Details for the file cimsparql-7.0.5.tar.gz.

File metadata

  • Download URL: cimsparql-7.0.5.tar.gz
  • Upload date:
  • Size: 634.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for cimsparql-7.0.5.tar.gz
Algorithm Hash digest
SHA256 9875aa299caeabd4ada0af2c03ada7115e58ac774916d1613dfca3e0cc6e3956
MD5 c60fcd99f02b5b7ce46bd47dddce6851
BLAKE2b-256 38cd10fdd0b4db68ac0a0953988b9f0b5ebd709d097581ba6424d87337397b1b

See more details on using hashes here.

File details

Details for the file cimsparql-7.0.5-py3-none-any.whl.

File metadata

  • Download URL: cimsparql-7.0.5-py3-none-any.whl
  • Upload date:
  • Size: 61.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for cimsparql-7.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 9bfa0ee0358dd69248a8fb3cc05a0d8a3587c4d094a60bd4c4d9dd5b25e79b72
MD5 84fe640d1f7237ff8170990bd2f4aa5c
BLAKE2b-256 234e3367c6e81bff2b7dda0b3c37e0f3c812e55afc362262b2c43482d4ad86a0

See more details on using hashes here.

Supported by

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