Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
python-common
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
This is an archived project. Repository and other project resources are read-only.
Show more breadcrumbs
nomad-lab
python-common
Commits
36d9ea2f
Commit
36d9ea2f
authored
9 years ago
by
Himanen, Lauri (himanel1)
Browse files
Options
Downloads
Patches
Plain Diff
Added an initial version for the coordinate reader which just calls ASE.
parent
f3dd8d96
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
python/nomadcore/coordinate_reader.py
+84
-0
84 additions, 0 deletions
python/nomadcore/coordinate_reader.py
with
84 additions
and
0 deletions
python/nomadcore/coordinate_reader.py
0 → 100644
+
84
−
0
View file @
36d9ea2f
import
ase.io
import
logging
logger
=
logging
.
getLogger
(
__name__
)
#===============================================================================
class
CoordinateReader
(
object
):
"""
Used to parse various different atomic coordinate files.
See the dictionary
'
formats
'
for all the supported formats and a brief
explanation. Reading is primarily done by ASE, but in some cases own
implementation must be used. Returns all coordinates as numpy arrays.
"""
def
__init__
(
self
):
self
.
formats
=
{
"
xyz
"
:
CoordinateFormat
(
"
.xyz
"
,
"
The XYZ file format.
"
,
self
.
ase_iread
),
"
cif
"
:
CoordinateFormat
(
"
.cif
"
,
"
Crystallographic Information File
"
,
self
.
ase_iread
),
"
pdb
"
:
CoordinateFormat
(
"
.pdb
"
,
"
Protein Data Bank
"
,
self
.
ase_iread
),
}
def
iread
(
self
,
file_handle
,
format
):
"""
Returns an iterator that goes through the given trajectory file one
configuration at a time.
Args:
file_handle: A file object pointing to the coordinate file.
format: String containing one of the supported formats.
"""
if
not
self
.
check_format_support
(
format
):
return
iread_function
=
self
.
formats
[
format
].
function
return
iread_function
(
file_handle
,
format
)
def
n_atoms
(
self
,
file_handle
,
format
):
"""
Read the first configuration of the coordinate file to extract the
number of atoms in it.
"""
iterator
=
self
.
iread
(
file_handle
,
format
)
pos
=
iterator
.
next
()
return
pos
.
shape
[
0
]
def
check_format_support
(
self
,
format
):
"""
Check if the given format is supported.
"""
if
format
not
in
self
.
formats
:
logger
.
error
(
"
The format
'
{}
'
is not supported by CoordinateReader.
"
.
format
(
format
))
return
False
else
:
return
True
def
ase_iread
(
self
,
file_handle
,
format
):
"""
Wrapper for ASE
'
s iread function. Returns numpy arrays instead of
Atoms objects.
"""
# The newest ASE version found in Github has an iread function.
# After reading the ASE source code, it seems that the ASE iread does
# actually read the entire file into memory and the yields the
# configurations from it. Should be checked at some point.
def
ase_generator
(
iterator
):
"""
Used to wrap an iterator returned by ase.io.iread so that it returns
the positions instead of the ase.Atoms object.
"""
for
value
in
iterator
:
yield
value
.
get_positions
()
iterator
=
ase
.
io
.
read
(
file_handle
,
index
=
"
:
"
,
format
=
format
)
return
ase_generator
(
iterator
)
def
custom_iread
(
self
,
file_handle
,
format
):
"""
"""
pass
#===============================================================================
class
CoordinateFormat
(
object
):
"""
Represents a coordinate format.
"""
def
__init__
(
self
,
extension
,
info
,
function
):
self
.
extension
=
extension
self
.
info
=
info
self
.
function
=
function
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment