Skip to content

Commit

Permalink
cim2pp converter: Using lxml to parse XML files (better performance) (e…
Browse files Browse the repository at this point in the history
…2nIEE#2400)

* - [FIXED] bug in :code:`cim2pp`: Changed zero prioritized generators with voltage controller to sgens (like PowerFactory does)
- [ADDED] cim2pp: added description fields for each asset and added BusbarSection information to nodes

* changed logging TopologicalNodes warning

* changed logging TopologicalNodes warning

* cim converter: Avoid huge logging output when ignore_erros = True

* fixed some small bugs in the CGMES converter and improved its speed

* cim2pp converter: Using lxml to parse XML files (better performance)

* cim2pp converter: Using lxml to parse XML files (better performance)

* cim2pp converter: Using lxml to parse XML files (better performance)
  • Loading branch information
mrifraunhofer authored Sep 19, 2024
1 parent 75b80f4 commit e3fa0c6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ Change Log
- [CHANGED] accelerate _integrate_power_elements_connected_with_switch_buses() in get_equivalent()
- [CHANGED] accelerate distributed slack power flow calculation by using sparse-aware operations in _subnetworks()
- [ADDED] Discrete shunt controller for local voltage regulation with shunt steps
- [ADDED] cim2pp converter: Using lxml to parse XML files (better performance)

[2.14.7] - 2024-06-14
-------------------------------
Expand Down
18 changes: 9 additions & 9 deletions pandapower/converter/cim/cim_classes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
from typing import Dict, List
import pandas as pd
import numpy as np
import xml.etree.ElementTree
import xml.etree.cElementTree as xmlET
from lxml import etree
from .other_classes import ReportContainer, Report, LogLevel, ReportCode
from .cim_tools import get_cim_schema

Expand Down Expand Up @@ -389,7 +388,7 @@ def _parse_element(self, element, parsed=None):
def _get_df(self, items):
return pd.DataFrame([self._parse_element(child) for child in iter(items)])

def _get_cgmes_profile_from_xml(self, root: xml.etree.ElementTree.Element, ignore_errors: bool = False,
def _get_cgmes_profile_from_xml(self, root: etree.Element, ignore_errors: bool = False,
default_profile: str = 'unknown') -> str:
"""
Get the CGMES profile from the XML file.
Expand Down Expand Up @@ -486,17 +485,16 @@ def _parse_source_file(self, file: str, output: dict, encoding: str, profile_nam
temp_dir.cleanup()
del temp_dir, temp_dir_path
return
with open(file, mode='r', encoding=encoding, errors='ignore') as f:
cim_str = f.read()
xml_tree = xmlET.fromstring(cim_str)
parser = etree.XMLParser(encoding=encoding, resolve_entities=False)
xml_tree = etree.parse(file, parser)
if profile_name is None:
prf = self._get_cgmes_profile_from_xml(xml_tree)
prf = self._get_cgmes_profile_from_xml(xml_tree.getroot())
else:
prf = profile_name
self.file_names[prf] = file
self._parse_xml_tree(xml_tree, prf, output)
self._parse_xml_tree(xml_tree.getroot(), prf, output)

def _parse_xml_tree(self, xml_tree: xmlET, profile_name: str, output: Dict | None = None):
def _parse_xml_tree(self, xml_tree: etree.ElementTree, profile_name: str, output: Dict | None = None):
output = self.cim if output is None else output
# get all CIM elements to parse
element_types = pd.Series([ele.tag for ele in list(xml_tree)])
Expand All @@ -507,6 +505,8 @@ def _parse_xml_tree(self, xml_tree: xmlET, profile_name: str, output: Dict | Non
if prf not in ns_dict.keys():
ns_dict[prf] = dict()
for _, element_type in element_types.items():
if not isinstance(element_type, str):
continue
element_type_c = re.sub('{.*}', '', element_type)
prf_content[element_type_c] = self._get_df(xml_tree.findall(element_type))
# rename the columns (remove the namespaces)
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ dependencies = [
"tqdm",
"deepdiff",
"geojson",
"lxml",
"typing_extensions~=4.9",
]
keywords = [
Expand Down

0 comments on commit e3fa0c6

Please sign in to comment.