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
2 changes: 1 addition & 1 deletion src/diffpy/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
#
##############################################################################

# Placeholder until this gets scikit-packaged later
# Placeholder until this gets scikit-packaged later
2 changes: 1 addition & 1 deletion src/diffpy/distanceprinter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
Blank namespace package.
"""

__import__('pkg_resources').declare_namespace(__name__)
__import__("pkg_resources").declare_namespace(__name__)


# End of file
119 changes: 77 additions & 42 deletions src/diffpy/distanceprinter/distanceprinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,29 @@
from diffpy.pdffit2 import PdfFit
from diffpy.structure import PDFFitStructure


def calDistance(strufile, atomi, atomj, lb, ub, complete):

stru = PDFFitStructure(filename=strufile)
pdffit = PdfFit()
pdffit.add_structure(stru)
ele = stru.element

rv = pdffit.bond_length_types(atomi, atomj, lb, ub)
dij = np.around(rv['dij'], 6)
ddij = np.around(rv['ddij'], 10)
rv['all0ddij'] = np.all(ddij == 0)
ij0 = rv['ij0']

chardtype = 'S8' if complete else 'S4'
dtypec = [('distance', float), ('dd', float), ('i', chardtype), ('j', chardtype)]
dij = np.around(rv["dij"], 6)
ddij = np.around(rv["ddij"], 10)
rv["all0ddij"] = np.all(ddij == 0)
ij0 = rv["ij0"]

chardtype = "S8" if complete else "S4"
dtypec = [
("distance", float),
("dd", float),
("i", chardtype),
("j", chardtype),
]
distlist = np.zeros(len(dij), dtype=dtypec)

if not complete:
for i, dist, dd, ij in zip(range(len(dij)), dij, ddij, ij0):
if ij[0] > ij[1]:
Expand All @@ -44,55 +50,82 @@ def calDistance(strufile, atomi, atomj, lb, ub, complete):
distlist = np.unique(distlist)
else:
for i, dist, dd, ij in zip(range(len(dij)), dij, ddij, ij0):
distlist[i] = (dist, dd, '%s.%i' % (ele[ij[0]], ij[0]), '%s.%i' % (ele[ij[1]], ij[1]))

distlist.sort(order='distance')
rv['distlist'] = distlist
rv['atomi'] = atomi
rv['atomj'] = atomj
rv['lb'] = lb
rv['ub'] = ub
rv['complete'] = complete
rv['stru'] = stru
rv['strufile'] = strufile
distlist[i] = (
dist,
dd,
"%s.%i" % (ele[ij[0]], ij[0]),
"%s.%i" % (ele[ij[1]], ij[1]),
)

distlist.sort(order="distance")
rv["distlist"] = distlist
rv["atomi"] = atomi
rv["atomj"] = atomj
rv["lb"] = lb
rv["ub"] = ub
rv["complete"] = complete
rv["stru"] = stru
rv["strufile"] = strufile
return rv



def formatResults(stru, distlist, complete, all0ddij, **kw):
'''
"""
format the distlist to string
'''
"""
lines = []
# header
lines.append("# Structure file: %s" % kw['strufile'])
lines.append('# ')
lines.append("# Structure file: %s" % kw["strufile"])
lines.append("# ")
strustr = stru.__str__().splitlines()
lines.append('# ' + strustr[0])
lines.append("# " + strustr[0])
for i in range(1, len(strustr)):
lines.append('# %2i ' % (i - 1) + strustr[i])
lines.append('# ')
lines.append('# Inter-atomic distance of (%s, %s) in (%2.2f, %2.2f) A'
% (kw['atomi'], kw['atomj'], kw['lb'], kw['ub']))
lines.append('')

lines.append("# %2i " % (i - 1) + strustr[i])
lines.append("# ")
lines.append(
"# Inter-atomic distance of (%s, %s) in (%2.2f, %2.2f) A"
% (kw["atomi"], kw["atomj"], kw["lb"], kw["ub"])
)
lines.append("")

if complete:
for dist in distlist:
try:
lines.append('%s-%s:\t%2.6f' % (dist[2].decode('utf-8'), dist[3].decode('utf-8'), dist[0]))
lines.append(
"%s-%s:\t%2.6f"
% (
dist[2].decode("utf-8"),
dist[3].decode("utf-8"),
dist[0],
)
)
except AttributeError:
lines.append('%s-%s:\t%2.6f' % (dist[2], dist[3], dist[0]))
lines.append("%s-%s:\t%2.6f" % (dist[2], dist[3], dist[0]))
else:
for dist in distlist:
try:
lines.append('%s-%s:\t%2.6f (%1.1e)' % (dist[2].decode('utf-8'), dist[3].decode('utf-8'), dist[0], dist[1]))
lines.append(
"%s-%s:\t%2.6f (%1.1e)"
% (
dist[2].decode("utf-8"),
dist[3].decode("utf-8"),
dist[0],
dist[1],
)
)
except AttributeError:
lines.append('%s-%s:\t%2.6f (%1.1e)' % (dist[2], dist[3], dist[0], dist[1]))
rv = '\n'.join(lines)
lines.append(
"%s-%s:\t%2.6f (%1.1e)"
% (dist[2], dist[3], dist[0], dist[1])
)
rv = "\n".join(lines)
return rv


def writeToFile(filename, rv):
f = open(filename, 'w', encoding="utf-8")
f = open(filename, "w", encoding="utf-8")
try:
rv = rv.decode('utf-8')
rv = rv.decode("utf-8")
f.write(rv)
f.close()
except AttributeError:
Expand All @@ -101,16 +134,18 @@ def writeToFile(filename, rv):
f.close()
pass


def main():
sysargv = sys.argv[1:]
strufile, atomi, atomj, lb, ub, complete, filename = sysargv
lb = float(lb)
ub = float(ub)
complete = ('1' == complete)
complete = "1" == complete
rv = calDistance(strufile, atomi, atomj, lb, ub, complete)
strv = formatResults(**rv)
writeToFile(filename, strv)
return

if __name__ == '__main__':

if __name__ == "__main__":
main()
16 changes: 8 additions & 8 deletions src/diffpy/distanceprinter/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,22 @@
##############################################################################


"""Definition of __version__, __date__, __gitsha__.
"""
"""Definition of __version__, __date__, __gitsha__."""

from pkg_resources import resource_filename
from ConfigParser import RawConfigParser

# obtain version information from the version.cfg file
cp = RawConfigParser(dict(version='', date='', commit='', timestamp=0))
if not cp.read(resource_filename(__name__, 'version.cfg')):
cp = RawConfigParser(dict(version="", date="", commit="", timestamp=0))
if not cp.read(resource_filename(__name__, "version.cfg")):
from warnings import warn

warn('Package metadata not found, execute "./setup.py egg_info".')

__version__ = cp.get('DEFAULT', 'version')
__date__ = cp.get('DEFAULT', 'date')
__gitsha__ = cp.get('DEFAULT', 'commit')
__timestamp__ = cp.getint('DEFAULT', 'timestamp')
__version__ = cp.get("DEFAULT", "version")
__date__ = cp.get("DEFAULT", "date")
__gitsha__ = cp.get("DEFAULT", "commit")
__timestamp__ = cp.getint("DEFAULT", "timestamp")

del cp

Expand Down
30 changes: 21 additions & 9 deletions tests/test_distanceprinter.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,26 +10,38 @@ def test_distanceprinter(monkeypatch):
# must be cif, stru, or xyz file
strufile = os.path.join(example_file_dir, "Ni-9008476.cif")
# atoms typically pull from list of elements in stru output of PDFFitStructure. 'all' is always first option
atomi = 'all'
atomj = 'all'
atomi = "all"
atomj = "all"
# lb and ub are lower an upper bound of distance to list in angstroms
lb = 1
ub = 10
# 1 means keep duplicate atom pairs with same inter-atomic distance, 0 means not to
comp = '1'
monkeypatch.setattr("sys.argv", ['distanceprinter', strufile, atomi, atomj, lb, ub, comp, 'tests\\outputs\\actual.res'])
comp = "1"
monkeypatch.setattr(
"sys.argv",
[
"distanceprinter",
strufile,
atomi,
atomj,
lb,
ub,
comp,
"tests\\outputs\\actual.res",
],
)
cwd = os.getcwd()
main()
generated_file_path = os.path.join(cwd, 'tests\\outputs\\actual.res')
f = open(generated_file_path, 'r', encoding="utf-8")
generated_file_path = os.path.join(cwd, "tests\\outputs\\actual.res")
f = open(generated_file_path, "r", encoding="utf-8")
rv0 = f.readlines()
rv0 = ''.join(rv0)
rv0 = "".join(rv0)
resultstr = rv0
output_file_dir = os.path.join(current_module_dir, "outputs")
example_output = os.path.join(output_file_dir, "expected.res")
f = open(example_output, 'r', encoding="utf-8")
f = open(example_output, "r", encoding="utf-8")
rv1 = f.readlines()
rv1 = ''.join(rv1)
rv1 = "".join(rv1)
teststr = rv1

# Remove structure file path when comparing as it may differ between machines
Expand Down