From 9509a0753b8ec62a67c346c9133f6a0f0a6dae89 Mon Sep 17 00:00:00 2001 From: Zhi Ming Xu Date: Sat, 16 Aug 2025 02:23:52 -0400 Subject: [PATCH 1/3] chore: embed DistancePrinter under src/diffpy --- src/diffpy/__init__.py | 15 +++ src/diffpy/distanceprinter/__init__.py | 22 ++++ src/diffpy/distanceprinter/distanceprinter.py | 116 ++++++++++++++++++ src/diffpy/distanceprinter/version.py | 35 ++++++ 4 files changed, 188 insertions(+) create mode 100644 src/diffpy/__init__.py create mode 100644 src/diffpy/distanceprinter/__init__.py create mode 100644 src/diffpy/distanceprinter/distanceprinter.py create mode 100644 src/diffpy/distanceprinter/version.py diff --git a/src/diffpy/__init__.py b/src/diffpy/__init__.py new file mode 100644 index 0000000..5138d34 --- /dev/null +++ b/src/diffpy/__init__.py @@ -0,0 +1,15 @@ +#!/usr/bin/env python +############################################################################## +# +# DistancePrinter by Simon J. L. Billinge group +# (c) 2013 Trustees of the Columbia University +# in the City of New York. All rights reserved. +# +# File coded by: Xiaohao Yang +# +# See AUTHORS.txt for a list of people who contributed. +# See LICENSENOTICE.txt for license information. +# +############################################################################## + +# Placeholder until this gets scikit-packaged later \ No newline at end of file diff --git a/src/diffpy/distanceprinter/__init__.py b/src/diffpy/distanceprinter/__init__.py new file mode 100644 index 0000000..5dc714a --- /dev/null +++ b/src/diffpy/distanceprinter/__init__.py @@ -0,0 +1,22 @@ +#!/usr/bin/env python +############################################################################## +# +# DistancePrinter by Simon J. L. Billinge group +# (c) 2013 Trustees of the Columbia University +# in the City of New York. All rights reserved. +# +# File coded by: Xiaohao Yang +# +# See AUTHORS.txt for a list of people who contributed. +# See LICENSENOTICE.txt for license information. +# +############################################################################## + +""" +Blank namespace package. +""" + +__import__('pkg_resources').declare_namespace(__name__) + + +# End of file diff --git a/src/diffpy/distanceprinter/distanceprinter.py b/src/diffpy/distanceprinter/distanceprinter.py new file mode 100644 index 0000000..7a653ca --- /dev/null +++ b/src/diffpy/distanceprinter/distanceprinter.py @@ -0,0 +1,116 @@ +#!/usr/bin/env python +############################################################################## +# +# DistancePrinter by Simon J. L. Billinge group +# (c) 2013 Trustees of the Columbia University +# in the City of New York. All rights reserved. +# +# File coded by: Xiaohao Yang +# +# See AUTHORS.txt for a list of people who contributed. +# See LICENSENOTICE.txt for license information. +# +############################################################################## + +import sys +import numpy as np + +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)] + 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]: + distlist[i] = (dist, dd, ele[ij[1]], ele[ij[0]]) + else: + distlist[i] = (dist, dd, ele[ij[0]], ele[ij[1]]) + 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 + 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('# ') + strustr = stru.__str__().splitlines() + 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('') + + 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])) + except AttributeError: + 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])) + except AttributeError: + 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") + try: + rv = rv.decode('utf-8') + f.write(rv) + f.close() + except AttributeError: + # No need to decode in python 3 + f.write(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) + rv = calDistance(strufile, atomi, atomj, lb, ub, complete) + strv = formatResults(**rv) + writeToFile(filename, strv) + return + +if __name__ == '__main__': + main() diff --git a/src/diffpy/distanceprinter/version.py b/src/diffpy/distanceprinter/version.py new file mode 100644 index 0000000..486d6b7 --- /dev/null +++ b/src/diffpy/distanceprinter/version.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python +############################################################################## +# +# DistancePrinter by Simon J. L. Billinge group +# (c) 2013 Trustees of the Columbia University +# in the City of New York. All rights reserved. +# +# File coded by: Xiaohao Yang +# +# See AUTHORS.txt for a list of people who contributed. +# See LICENSENOTICE.txt for license information. +# +############################################################################## + + +"""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')): + 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') + +del cp + +# End of file From ea265682cc591a1843691af3dddf629dc81503d5 Mon Sep 17 00:00:00 2001 From: Zhi Ming Xu Date: Sat, 16 Aug 2025 02:24:02 -0400 Subject: [PATCH 2/3] chore: change distanceprinter imports --- tests/test_distanceprinter.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_distanceprinter.py b/tests/test_distanceprinter.py index 565a03e..9d1d618 100644 --- a/tests/test_distanceprinter.py +++ b/tests/test_distanceprinter.py @@ -1,7 +1,7 @@ import os from io import open -from DistancePrinter.distanceprinter import main +from diffpy.distanceprinter.distanceprinter import main def test_distanceprinter(monkeypatch): From e62950562cd010b2521f3c3fb50900d250fbeea7 Mon Sep 17 00:00:00 2001 From: Zhi Ming Xu Date: Sat, 16 Aug 2025 02:24:22 -0400 Subject: [PATCH 3/3] chore: change distanceprinter references in pyproject.toml --- DistancePrinter/__init__.py | 22 ------ DistancePrinter/distanceprinter.py | 116 ----------------------------- DistancePrinter/version.py | 35 --------- pyproject.toml | 4 +- 4 files changed, 2 insertions(+), 175 deletions(-) delete mode 100644 DistancePrinter/__init__.py delete mode 100644 DistancePrinter/distanceprinter.py delete mode 100644 DistancePrinter/version.py diff --git a/DistancePrinter/__init__.py b/DistancePrinter/__init__.py deleted file mode 100644 index 5dc714a..0000000 --- a/DistancePrinter/__init__.py +++ /dev/null @@ -1,22 +0,0 @@ -#!/usr/bin/env python -############################################################################## -# -# DistancePrinter by Simon J. L. Billinge group -# (c) 2013 Trustees of the Columbia University -# in the City of New York. All rights reserved. -# -# File coded by: Xiaohao Yang -# -# See AUTHORS.txt for a list of people who contributed. -# See LICENSENOTICE.txt for license information. -# -############################################################################## - -""" -Blank namespace package. -""" - -__import__('pkg_resources').declare_namespace(__name__) - - -# End of file diff --git a/DistancePrinter/distanceprinter.py b/DistancePrinter/distanceprinter.py deleted file mode 100644 index 7a653ca..0000000 --- a/DistancePrinter/distanceprinter.py +++ /dev/null @@ -1,116 +0,0 @@ -#!/usr/bin/env python -############################################################################## -# -# DistancePrinter by Simon J. L. Billinge group -# (c) 2013 Trustees of the Columbia University -# in the City of New York. All rights reserved. -# -# File coded by: Xiaohao Yang -# -# See AUTHORS.txt for a list of people who contributed. -# See LICENSENOTICE.txt for license information. -# -############################################################################## - -import sys -import numpy as np - -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)] - 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]: - distlist[i] = (dist, dd, ele[ij[1]], ele[ij[0]]) - else: - distlist[i] = (dist, dd, ele[ij[0]], ele[ij[1]]) - 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 - 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('# ') - strustr = stru.__str__().splitlines() - 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('') - - 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])) - except AttributeError: - 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])) - except AttributeError: - 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") - try: - rv = rv.decode('utf-8') - f.write(rv) - f.close() - except AttributeError: - # No need to decode in python 3 - f.write(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) - rv = calDistance(strufile, atomi, atomj, lb, ub, complete) - strv = formatResults(**rv) - writeToFile(filename, strv) - return - -if __name__ == '__main__': - main() diff --git a/DistancePrinter/version.py b/DistancePrinter/version.py deleted file mode 100644 index 486d6b7..0000000 --- a/DistancePrinter/version.py +++ /dev/null @@ -1,35 +0,0 @@ -#!/usr/bin/env python -############################################################################## -# -# DistancePrinter by Simon J. L. Billinge group -# (c) 2013 Trustees of the Columbia University -# in the City of New York. All rights reserved. -# -# File coded by: Xiaohao Yang -# -# See AUTHORS.txt for a list of people who contributed. -# See LICENSENOTICE.txt for license information. -# -############################################################################## - - -"""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')): - 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') - -del cp - -# End of file diff --git a/pyproject.toml b/pyproject.toml index ecaf836..45c8729 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -33,7 +33,7 @@ classifiers = [ ] [project.scripts] -distanceprinter = 'DistancePrinter.distanceprinter:main' +distanceprinter = 'diffpy.distanceprinter.distanceprinter:main' [project.urls] Homepage = "https://github.com/diffpy/diffpy.distanceprinter/" @@ -46,7 +46,7 @@ dev_template = "{tag}" dirty_template = "{tag}" [tool.setuptools.packages.find] -where = ["DistancePrinter"] # list of folders that contain the packages (["."] by default) +where = ["src"] # list of folders that contain the packages (["."] by default) include = ["*"] # package names should match these glob patterns (["*"] by default) exclude = [] # exclude packages matching these glob patterns (empty by default) namespaces = false # to disable scanning PEP 420 namespaces (true by default)