Skip to content

Commit

Permalink
create XML file with all the parameters
Browse files Browse the repository at this point in the history
- script to generate XML file from existing CSV files
- script to reconstruct CSV files from XML file
  • Loading branch information
jimustafa committed May 9, 2024
1 parent 7673284 commit bbf3f1c
Show file tree
Hide file tree
Showing 3 changed files with 1,307 additions and 0 deletions.
42 changes: 42 additions & 0 deletions docs/docs/parameters/generate-parameter-tables.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import xml.etree.ElementTree as ET


groups = {
'w90': [
'disentanglement',
'job',
'plot',
'system',
'transport',
'wannierise',
],
'postw90': [
'berry',
'boltzwann',
'dos',
'geninterp',
'global',
'gyrotropic',
'kpath',
'kslice',
],
}

tree = ET.parse('parameters.xml')
root = tree.getroot()

for tool in ['w90', 'postw90']:
for group in groups[tool]:
parameters = root.findall(f'./parameter[@tool="{tool}"][@group="{group}"]')
with open(f'{tool}-{group}-parameters.csv', 'w') as fp:
print('Keyword,Type,Description', file=fp)
for parameter in parameters:
name = parameter.find('name')
type = parameter.find('type')
description = parameter.find('description')
if 'optional_prefix' in name.attrib:
print(f'[{name.attrib["optional_prefix"]}]{name.text},', end='', file=fp)
else:
print(f'{name.text},', end='', file=fp)
print(f'{type.text},', end='', file=fp)
print(f'"{description.text}"', file=fp)
63 changes: 63 additions & 0 deletions docs/docs/parameters/parameters-csv2xml.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import re
import xml.etree.ElementTree as ET

import pandas as pd


csv_files = [
'w90-system-parameters.csv',
'w90-job-parameters.csv',
'w90-disentanglement-parameters.csv',
'w90-wannierise-parameters.csv',
'w90-plot-parameters.csv',
'w90-transport-parameters.csv',
'postw90-global-parameters.csv',
'postw90-berry-parameters.csv',
'postw90-dos-parameters.csv',
'postw90-kpath-parameters.csv',
'postw90-kslice-parameters.csv',
'postw90-gyrotropic-parameters.csv',
'postw90-boltzwann-parameters.csv',
'postw90-geninterp-parameters.csv',
]

root = ET.Element('root')
tree = ET.ElementTree(root)

pattern = re.compile(r'(?P<tool>.+)-(?P<group>.+)-parameters.csv')

for csv_file in csv_files:
match1 = pattern.match(csv_file)

table = pd.read_csv(
csv_file,
header=0,
names=('keyword', 'type', 'description'),
converters={'keyword': str.strip, 'type': str.strip, 'description': str.strip}
)

for row in table.itertuples():
parameter = ET.SubElement(root, 'parameter')
parameter.set('tool', match1['tool'])
parameter.set('group', match1['group'])
# tool = ET.SubElement(parameter, 'tool')
# group = ET.SubElement(parameter, 'group')
name = ET.SubElement(parameter, 'name')
# optional_prefix = ET.SubElement(parameter, 'optional_prefix')
type = ET.SubElement(parameter, 'type')
description = ET.SubElement(parameter, 'description')

if (match2 := re.compile(r'^\[(?P<prefix>.+_)\]').match(row.keyword)) is not None:
name.text = row.keyword.replace(f'[{match2["prefix"]}]', '')
# optional_prefix.text = match2['prefix']
name.set('optional_prefix', match2['prefix'])
else:
name.text = row.keyword
# tool.text = match1['tool']
# group.text = match1['group']
type.text = row.type
description.text = row.description

ET.indent(tree)
with open('parameters.xml', 'wb') as fp:
tree.write(fp)
Loading

0 comments on commit bbf3f1c

Please sign in to comment.