Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions news/fix-load-structure.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* Added method ``load_structure`` in ``__init__.py``

**Changed:**

* <news item>

**Deprecated:**

* Deprecated method ``loadStructure`` in ``__init__.py`` for removal in version 4.0.0

**Removed:**

* <news item>

**Fixed:**

* Fixed ``load_structure`` with successfully loading `Path` object

**Security:**

* <news item>
22 changes: 21 additions & 1 deletion src/diffpy/structure/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"""


import os
import sys

import diffpy.structure as _structure
Expand All @@ -46,8 +47,17 @@

# package version
from diffpy.structure.version import __version__
from diffpy.utils._deprecator import build_deprecation_message, deprecated

# Deprecations -------------------------------------------------------
base = "diffpy.structure"
removal_version = "4.0.0"
loadStructure_deprecation_msg = build_deprecation_message(
base,
"loadStructure",
"load_structure",
removal_version,
)


# @deprecated
Expand All @@ -72,7 +82,17 @@ def __getattr__(self, name):
# top level routines


@deprecated(loadStructure_deprecation_msg)
def loadStructure(filename, fmt="auto", **kw):
"""This function has been deprecated and will be removed in version
4.0.0.

Please use diffpy.structure.load_structure instead.
"""
return load_structure(filename, fmt, **kw)


def load_structure(filename, fmt="auto", **kw):
"""Load new structure object from the specified file.

Parameters
Expand All @@ -96,7 +116,7 @@ def loadStructure(filename, fmt="auto", **kw):
Return a more specific PDFFitStructure type for 'pdffit'
and 'discus' formats.
"""

filename = os.fspath(filename)
p = get_parser(fmt, **kw)
rv = p.parse_file(filename)
return rv
Expand Down
40 changes: 33 additions & 7 deletions tests/test_loadstructure.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

import pytest

from diffpy.structure import PDFFitStructure, Structure, loadStructure
from diffpy.structure import PDFFitStructure, Structure, load_structure, loadStructure
from diffpy.structure.structureerrors import StructureFormatError


Expand All @@ -19,22 +19,22 @@ def prepare_fixture(self, datafile):
def test_xcfg(self):
"""Check loading of atomeye xcfg format."""
f = self.datafile("BubbleRaftShort.xcfg")
stru = loadStructure(f)
stru = load_structure(f)
self.assertTrue(type(stru) is Structure)
self.assertRaises(StructureFormatError, loadStructure, f, "xyz")
self.assertRaises(StructureFormatError, load_structure, f, "xyz")
return

def test_discus(self):
"""Check loading of discus file format."""
f = self.datafile("Ni-discus.stru")
stru = loadStructure(f)
stru = load_structure(f)
self.assertTrue(type(stru) is PDFFitStructure)
return

def test_cif(self):
"""Check loading of CIF file format."""
f = self.datafile("PbTe.cif")
stru = loadStructure(f)
stru = load_structure(f)
self.assertTrue(isinstance(stru, Structure))
self.assertFalse(isinstance(stru, PDFFitStructure))
return
Expand All @@ -45,25 +45,51 @@ def test_badfile(self):
self.assertRaises(StructureFormatError, loadStructure, f)
return

def test_load_bad_file(self):
"""Check loading of CIF file format."""
f = self.datafile("Ni-bad.stru")
self.assertRaises(StructureFormatError, load_structure, f)
return

def test_goodkwarg(self):
"""Check loading of CIF file and passing of parser keyword
argument."""
f = self.datafile("graphite.cif")
stru = loadStructure(f, eps=1e-10)
stru = load_structure(f, eps=1e-10)
self.assertEqual(8, len(stru))
return

def test_badkwarg(self):
"""Check loading of xyz file format with invalid keyword
argument."""
f = self.datafile("bucky.xyz")
self.assertRaises(TypeError, loadStructure, f, eps=1e-10)
self.assertRaises(TypeError, load_structure, f, eps=1e-10)
return


# End of class TestLoadStructure


# ----------------------------------------------------------------------------
@pytest.mark.parametrize(
"filename, expected",
[ # C1: Load the cif file in Path object, expected to load the Structure instance.
("PbTe.cif", (True, False)),
],
)
def test_load_structure_cif_in_path(datafile, filename, expected):
from pathlib import Path

f = datafile(filename)
f_path = Path(f)
stru = load_structure(f_path)
actual = (
isinstance(stru, Structure),
isinstance(stru, PDFFitStructure),
)

assert actual == expected


if __name__ == "__main__":
unittest.main()
Loading