Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
nomad-lab
parser-lib-atoms
Commits
ce07a7cc
Commit
ce07a7cc
authored
Jul 18, 2016
by
Carl Poelking
Browse files
XML parsing.
parent
12e80786
Changes
7
Expand all
Show whitespace changes
Inline
Side-by-side
parser/parser-lib-atoms/libAtomsParser.py
View file @
ce07a7cc
...
...
@@ -66,18 +66,20 @@ def parse(output_file_name):
jbe
.
startedParsingSession
(
output_file_name
,
parser_info
)
base_dir
=
os
.
path
.
dirname
(
os
.
path
.
abspath
(
output_file_name
))
terminal
=
LibAtomsParser
(
osio
)
terminal_trj
=
LibAtomsTrajectory
(
osio
)
terminal_trj
.
ParseOutput
(
output_file_name
)
out
=
terminal
trj
=
terminal_trj
terminal_gap
=
LibAtomsGapParser
(
osio
)
terminal_gap
.
ParseOutput
(
output_file_name
)
terminal_trj
=
terminal_gap
.
trj
log
(
"Start parsing ..."
)
log
(
"Base directory = '%s'"
%
base_dir
)
osio
<<
"Start parsing ..."
<<
osio
.
endl
osio
<<
"Base directory = '%s'"
%
base_dir
<<
osio
.
endl
gap
=
terminal_gap
trj
=
terminal_trj
with
open_section
(
jbe
,
'section_run'
)
as
gid_run
:
push
(
jbe
,
trj
,
'program_name'
)
push
(
jbe
,
trj
,
'program_version'
)
push
(
jbe
,
gap
,
'program_name'
)
push
(
jbe
,
gap
,
'program_version'
,
key2
=
'GAP_params.svn_version'
)
jbe
.
finishedParsingSession
(
"ParseSuccess"
,
None
)
return
...
...
parser/parser-lib-atoms/libLibAtomsParser.py
View file @
ce07a7cc
...
...
@@ -8,6 +8,7 @@ import os
import
sys
import
re
import
numpy
as
np
import
xml.dom.minidom
try
:
import
ase
...
...
@@ -17,6 +18,36 @@ except ImportError:
HAVE_ASE
=
False
pass
def
XmlGetUnique
(
root
,
tag
):
nodes
=
XmlGetAll
(
root
,
tag
)
if
len
(
nodes
)
>
1
:
raise
ValueError
(
"More than one node with tag '%s'"
%
tag
)
return
nodes
[
0
]
def
XmlGetAll
(
root
,
tag
):
nodes
=
root
.
getElementsByTagName
(
tag
)
ret_nodes
=
[]
for
node
in
nodes
:
if
node
.
localName
!=
None
:
ret_nodes
.
append
(
node
)
return
ret_nodes
def
XmlGetChildDict
(
root
):
tag_child
=
{}
for
child
in
root
.
childNodes
:
if
child
.
localName
==
None
:
continue
if
not
child
.
localName
in
tag_child
:
tag_child
[
child
.
localName
]
=
[]
tag_child
[
child
.
localName
].
append
(
child
)
return
tag_child
def
XmlGetAttributes
(
root
):
return
root
.
attributes
def
XmlGetText
(
root
):
return
root
.
firstChild
.
nodeValue
class
LibAtomsParser
(
object
):
def
__init__
(
self
,
log
=
None
):
self
.
log
=
log
...
...
@@ -33,7 +64,7 @@ class LibAtomsParser(object):
return
self
def
As
(
self
,
typ
=
None
):
if
typ
==
None
:
typ
=
type
(
self
.
selected_data_item
)
return
self
.
selected_data_item
return
typ
(
self
.
selected_data_item
)
def
SummarizeKeyDefaults
(
self
):
if
not
self
.
log
:
return
...
...
@@ -140,15 +171,125 @@ class LibAtomsParser(object):
self
.
Set
(
'program_version'
,
'n/a'
)
return
class
LibAtomsGapParser
(
LibAtomsParser
):
def
__init__
(
self
,
log
=
None
):
super
(
LibAtomsGapParser
,
self
).
__init__
(
log
)
self
.
logtag
=
'gap-xml'
self
.
trj
=
None
return
def
ParseOutput
(
self
,
output_file
):
self
.
Set
(
'program_name'
,
'libAtoms'
)
dom
=
xml
.
dom
.
minidom
.
parse
(
output_file
)
root
=
XmlGetUnique
(
dom
,
'GAP_params'
)
# Child keys should be: ['gpSparse', 'command_line', 'GAP_data', 'XYZ_data']
#print(list(child_nodes.keys()))
child_nodes
=
XmlGetChildDict
(
root
)
# 'GAP_params'
atts
=
XmlGetAttributes
(
root
)
keys
=
[
'label'
,
'svn_version'
]
for
key
in
keys
:
self
.
Set
(
'GAP_params.%s'
%
key
,
atts
[
key
].
value
)
# TODO Handle look-up errors
# 'GAP_params/GAP_data'
key
=
'GAP_data'
if
key
in
child_nodes
:
node
=
child_nodes
[
key
][
0
]
atts
=
XmlGetAttributes
(
node
)
keys
=
[
'do_core'
,
'e0'
]
for
key
in
keys
:
self
.
Set
(
'GAP_data.%s'
%
key
,
atts
[
key
].
value
)
# TODO Handle look-up errors
# 'GAP_params/command_line'
key
=
'command_line'
if
key
in
child_nodes
:
node
=
child_nodes
[
key
][
0
]
text
=
XmlGetText
(
node
)
self
.
Set
(
'command_line.command_line'
,
text
)
# 'GAP_params/gpSparse'
key
=
'gpSparse'
if
key
in
child_nodes
:
node
=
child_nodes
[
key
][
0
]
atts
=
XmlGetAttributes
(
node
)
keys
=
[
'n_coordinate'
]
for
key
in
keys
:
self
.
Set
(
'gpSparse.%s'
%
key
,
atts
[
key
].
value
)
# TODO Handle look-up errors
# GAP_params/gpSparse/gpCoordinates
gp_coord_node
=
XmlGetUnique
(
node
,
'gpCoordinates'
)
gp_coord_child_nodes
=
XmlGetChildDict
(
gp_coord_node
)
gp_coord_node_att
=
XmlGetAttributes
(
gp_coord_node
)
for
key
in
gp_coord_node_att
.
keys
():
self
.
Set
(
'gpCoordinates.%s'
%
key
,
gp_coord_node_att
[
key
].
value
)
# 'GAP_params/gpSparse/gpCoordinates/theta
key
=
'theta'
if
key
in
gp_coord_child_nodes
:
node
=
gp_coord_child_nodes
[
key
][
0
]
text
=
XmlGetText
(
node
).
strip
()
self
.
Set
(
'gpCoordinates.%s'
%
key
,
text
)
# 'GAP_params/gpSparse/gpCoordinates/descriptor
key
=
'descriptor'
if
key
in
gp_coord_child_nodes
:
node
=
gp_coord_child_nodes
[
key
][
0
]
text
=
XmlGetText
(
node
).
strip
()
self
.
Set
(
'gpCoordinates.%s'
%
key
,
text
)
# 'GAP_params/gpSparse/gpCoordinates/descriptor
key
=
'permutation'
if
key
in
gp_coord_child_nodes
:
node
=
gp_coord_child_nodes
[
key
][
0
]
att
=
XmlGetAttributes
(
node
)
text
=
XmlGetText
(
node
).
strip
()
self
.
Set
(
'gpCoordinates.perm.%s'
%
key
,
text
)
self
.
Set
(
'gpCoordinates.perm.i'
,
att
[
'i'
].
value
)
# 'GAP_params/gpSparse/gpCoordinates/sparseX
key
=
'sparseX'
if
key
in
gp_coord_child_nodes
:
n_sparseX
=
self
[
'gpCoordinates.n_sparseX'
].
As
(
int
)
n_dim
=
self
[
'gpCoordinates.dimensions'
].
As
(
int
)
sparseX_filename
=
self
[
'gpCoordinates.sparseX_filename'
].
As
(
str
)
# Read alpha coefficients
nodes
=
gp_coord_child_nodes
[
key
]
alpha_cutoff
=
np
.
zeros
((
n_sparseX
,
2
),
dtype
=
'float64'
)
for
i
,
node
in
enumerate
(
nodes
):
att
=
XmlGetAttributes
(
node
)
alpha_cutoff
[
i
,
0
]
=
float
(
att
[
"alpha"
].
value
)
alpha_cutoff
[
i
,
1
]
=
float
(
att
[
"sparseCutoff"
].
value
)
self
.
Set
(
'gpCoordinates.alpha'
,
alpha_cutoff
)
# Read descriptor matrix
X_matrix
=
np
.
loadtxt
(
sparseX_filename
)
assert
X_matrix
.
shape
[
0
]
==
n_sparseX
*
n_dim
# i-th row of X-matrix stores X-vector of config i
X_matrix
=
X_matrix
.
reshape
((
n_dim
,
n_sparseX
))
self
.
Set
(
'gpCoordinates.sparseX'
,
X_matrix
)
# 'GAP_params/XYZ_data'
key
=
'XYZ_data'
if
key
in
child_nodes
:
node
=
child_nodes
[
key
][
0
]
text
=
XmlGetText
(
node
)
trj_file
=
'lib-atoms-gap.from-xml.xyz'
ofs
=
open
(
trj_file
,
'w'
)
for
child
in
node
.
childNodes
:
if
child
.
nodeValue
==
None
:
continue
ln
=
child
.
nodeValue
.
strip
(
'
\n
'
)
if
ln
==
''
:
continue
ofs
.
write
(
ln
+
'
\n
'
)
ofs
.
close
()
self
.
trj
=
LibAtomsTrajectory
(
self
.
log
)
self
.
trj
.
ParseOutput
(
trj_file
)
return
class
LibAtomsTrajectory
(
LibAtomsParser
):
def
__init__
(
self
,
log
=
None
):
super
(
LibAtomsTrajectory
,
self
).
__init__
(
log
)
self
.
ase_configs
=
None
self
.
frames
=
[]
self
.
logtag
=
'trj'
def
ParseOutput
(
self
,
output_file
):
if
self
.
log
:
self
.
log
<<
self
.
log
.
mg
<<
"libAtomsParser::ParseOutput ..."
<<
self
.
log
.
endl
if
HAVE_ASE
:
read_fct
=
ase
.
io
.
read
read_fct_args
=
{
'index'
:
':'
}
...
...
@@ -156,11 +297,9 @@ class LibAtomsTrajectory(LibAtomsParser):
raise
NotImplementedError
(
"None-ASE read function requested, but not yet available."
)
read_fct
=
None
read_fct_args
=
None
# PARSE CONFIGURATIONS
self
.
ase_configs
=
read_fct
(
output_file
,
**
read_fct_args
)
self
.
LoadAseConfigs
(
self
.
ase_configs
)
self
.
Set
(
'program_name'
,
'libAtoms'
)
self
.
Set
(
'program_version'
,
'n/a'
)
return
...
...
@@ -169,7 +308,7 @@ class LibAtomsTrajectory(LibAtomsParser):
frame
=
LibAtomsFrame
(
self
.
log
)
frame
.
LoadAseConfig
(
config
)
self
.
frames
.
append
(
frame
)
if
self
.
log
:
log
<<
"Loaded %d configurations"
%
len
(
self
.
frames
)
<<
log
.
endl
if
self
.
log
:
self
.
log
<<
"Loaded %d configurations"
%
len
(
self
.
frames
)
<<
self
.
log
.
endl
return
class
LibAtomsFrame
(
LibAtomsParser
):
...
...
parser/parser-lib-atoms/libMomo.py
View file @
ce07a7cc
...
...
@@ -425,7 +425,7 @@ class ShellInterface(object):
try
:
height
,
width
=
os
.
popen
(
'stty size'
,
'r'
).
read
().
split
()
width
=
int
(
width
)
leftright
=
((
width
-
len
(
title
)
-
2
)
//
2
leftright
=
((
width
-
len
(
title
)
-
2
)
)
//
2
except
ValueError
:
leftright
=
40
return
trim
*
leftright
+
" "
+
title
+
" "
+
trim
*
leftright
...
...
src/main/scala/eu/nomad_lab/parsers/LibAtomsParser.scala
View file @
ce07a7cc
...
...
@@ -21,7 +21,7 @@ object LibAtomsParser extends SimpleExternalParserGenerator(
))
::
Nil
),
mainFileTypes
=
Seq
(
"text/.*"
),
mainFileRe
=
"""
ll
"""
.
r
,
mainFileRe
=
"""
\s*<GAP_params\s
"""
.
r
,
cmd
=
Seq
(
DefaultPythonInterpreter
.
pythonExe
(),
"${envDir}/parsers/lib-atoms/parser/parser-lib-atoms/libAtomsParser.py"
,
"${mainFilePath}"
),
resList
=
Seq
(
...
...
src/test/scala/eu/nomad_lab/parsers/LibAtomsParserSpec.scala
View file @
ce07a7cc
...
...
@@ -5,10 +5,10 @@ import org.specs2.mutable.Specification
object
LibAtomsParserSpec
extends
Specification
{
"LibAtomsParserTest"
>>
{
"test with json-events"
>>
{
ParserRun
.
parse
(
LibAtomsParser
,
"parsers/lib-atoms/test/lib-atoms-
tungsten/tungsten_gap_6.xyz
"
,
"json-events"
)
must_==
ParseResult
.
ParseSuccess
ParserRun
.
parse
(
LibAtomsParser
,
"parsers/lib-atoms/test/lib-atoms-
gap-test/gp.xml
"
,
"json-events"
)
must_==
ParseResult
.
ParseSuccess
}
"test with json"
>>
{
ParserRun
.
parse
(
LibAtomsParser
,
"parsers/lib-atoms/test/lib-atoms-
tungsten/tungsten_gap_6.xyz
"
,
"json"
)
must_==
ParseResult
.
ParseSuccess
ParserRun
.
parse
(
LibAtomsParser
,
"parsers/lib-atoms/test/lib-atoms-
gap-test/gp.xml
"
,
"json"
)
must_==
ParseResult
.
ParseSuccess
}
}
}
test/lib-atoms-gap-test/gp.xml
0 → 100644
View file @
ce07a7cc
This diff is collapsed.
Click to expand it.
test/lib-atoms-gap-test/gp.xml.sparseX.GAP_2013_5_30_60_14_20_28_9431
0 → 100644
View file @
ce07a7cc
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment