Skip to content

Commit

Permalink
ASE: Use guess=true while checking parser support (#29)
Browse files Browse the repository at this point in the history
Co-authored-by: Michael F. Herbst <info@michael-herbst.com>
  • Loading branch information
rashidrafeek and mfherbst committed Nov 18, 2023
1 parent 59e6b8c commit d10476e
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
2 changes: 1 addition & 1 deletion lib/AtomsIOPython/Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "AtomsIOPython"
uuid = "9e4c859b-2281-48ef-8059-f50fe53c37b0"
authors = ["Michael F. Herbst <info@michael-herbst.com> and contributors"]
version = "0.1.0"
version = "0.1.1"

[deps]
ASEconvert = "3da9722f-58c2-4165-81be-b4d7253e8fd2"
Expand Down
36 changes: 27 additions & 9 deletions lib/AtomsIOPython/src/ase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,45 @@ Supported formats:
- ASE trajectory files
- [XYZ](https://openbabel.org/wiki/XYZ) and [extxyz](https://github.com/libAtoms/extxyz#extended-xyz-specification-and-parsing-tools) files
"""
struct AseParser <: AbstractParser end
Base.@kwdef struct AseParser <: AbstractParser
guess::Bool = true
end


function AtomsIO.supports_parsing(::AseParser, file; save, trajectory)
function AtomsIO.supports_parsing(parser::AseParser, file; save, trajectory)
format = ""

if !save && !parser.guess
@warn("There is a bug in ASE (as of 08/11/2023, ASE 3.22), which gets triggered " *
"when trying to read files with `AseParser(; guess=false)`. This could mean " *
"that AtomsIO falsely reports a file as unsupported even though it is indeed " *
"supported for reading with ASE. In this case use `AseParser(; guess=true)` and" *
"try again.")
end

try
format = ase.io.formats.filetype(file; read=!save, guess=false)
# read=true causes ASE to open the file, read a few bytes and check for magic bytes
format = ase.io.formats.filetype(file; read=!save, guess=parser.guess)
catch e
e isa PyException && return false
rethrow()
end

if !(format in ase.io.formats.ioformats)
return false
elseif trajectory
# Loading/saving multiple systems is supported only if + in code
supports_trajectory = '+' in ase.io.formats.ioformats[format].code
return supports_trajectory
else
return true
end

ioformat = ase.io.formats.ioformats[format]
supports_trajectory = '+' in ioformat.code
if save && !pyconvert(Bool, ioformat.can_write)
return false # Want to write, but ASE cannot write
elseif !save && !pyconvert(Bool, ioformat.can_read)
return false # Want to read, but ASE cannot read
elseif trajectory && !supports_trajectory
return false # Trajectory operations, but not supported by ASE
end

true # We got here, so all is good
end

function AtomsIO.load_system(::AseParser, file::AbstractString, index=nothing;
Expand Down
18 changes: 17 additions & 1 deletion lib/AtomsIOPython/test/ase.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,14 @@ end
@test supports_parsing(AseParser(), prefix * ".cif"; save=true, trajectory=true )
@test supports_parsing(AseParser(), prefix * ".traj"; save=true, trajectory=false)
@test supports_parsing(AseParser(), prefix * ".traj"; save=true, trajectory=true )
@test supports_parsing(AseParser(), prefix * ".xyz"; save=true, trajectory=false)
@test supports_parsing(AseParser(), prefix * ".xyz"; save=true, trajectory=true )
@test supports_parsing(AseParser(), prefix * ".xsf"; save=true, trajectory=false)
@test supports_parsing(AseParser(), prefix * ".xsf"; save=true, trajectory=true )
@test supports_parsing(AseParser(), prefix * ".vasp"; save=true, trajectory=false)
@test !supports_parsing(AseParser(), prefix * ".vasp"; save=true, trajectory=true )

for ext in (".pwi", ".cif", ".traj")
for ext in (".pwi", ".cif", ".traj", ".xyz", ".xsf", ".vasp")
save_system(AseParser(), prefix * ext, make_ase_system().system)
end

Expand All @@ -74,5 +80,15 @@ end
@test supports_parsing(AseParser(), prefix * ".cif"; save=false, trajectory=true )
@test supports_parsing(AseParser(), prefix * ".traj"; save=false, trajectory=false)
@test supports_parsing(AseParser(), prefix * ".traj"; save=false, trajectory=true )
@test supports_parsing(AseParser(), prefix * ".xyz"; save=false, trajectory=false)
@test supports_parsing(AseParser(), prefix * ".xyz"; save=false, trajectory=true )
@test supports_parsing(AseParser(), prefix * ".xsf"; save=false, trajectory=false)
@test supports_parsing(AseParser(), prefix * ".xsf"; save=false, trajectory=true )
@test supports_parsing(AseParser(), prefix * ".vasp"; save=false, trajectory=false)
@test !supports_parsing(AseParser(), prefix * ".vasp"; save=false, trajectory=true )

@test_logs (:warn, ) begin
supports_parsing(AseParser(; guess=false), prefix * ".pwi"; save=false, trajectory=false)
end
end
end

2 comments on commit d10476e

@mfherbst
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register subdir=lib/AtomsIOPython

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/95571

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a AtomsIOPython-v0.1.1 -m "<description of version>" d10476eda2ab3e45cfdc717488a7a4223ef26f58
git push origin AtomsIOPython-v0.1.1

Please sign in to comment.