This Python package provides functionality for reading cim data from triple 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 easily define their own 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.000198In the example above the client will query repo "" in the default server GraphDB for AC line values.
See the sparql templates folder (cimsparql/sparql) to the query used.
>>> 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)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#'}Tests can be run against RDF4J databases if a container with the correct images are available.
docker pull eclipse/rdf4j-workbenchLaunch one or both containers and specify the following environment variables
RDF4J_URL = "localhost:8080/rdf4j-server"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).
- micro:
MicroGrid/Type1_T1/CGMES_v2.4.15_MicroGridTestConfiguration_T1_NL_Complete_v2 - small: See separate documentation
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 BlazeGraphContributions are always welcome and encouraged! Whether it's reporting a bug, suggesting an enhancement, or submitting a pull request, your input helps improve the project.
Dependencies are managed through uv, install with uv sync.
It's recommended to install the pre-commit hooks so checks are run automatically on every commit.
After installing pre-commit itself, install the hooks with pre-commit install.
Checks are normally only run on modified files when committing, but you can run all checks on all files with
pre-commit run --all.