From 0974ca1d1dc396c24546533e664190498adfa69e Mon Sep 17 00:00:00 2001 From: Caden Myers Date: Mon, 22 Jul 2024 16:33:50 -0600 Subject: [PATCH 1/5] add flake8, pre-commit, pyproject files --- .flake8 | 11 +++++++++++ .pre-commit-config.yaml | 43 +++++++++++++++++++++++++++++++++++++++++ pyproject.toml | 22 +++++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 .flake8 create mode 100644 .pre-commit-config.yaml create mode 100644 pyproject.toml diff --git a/.flake8 b/.flake8 new file mode 100644 index 00000000..2d2cb168 --- /dev/null +++ b/.flake8 @@ -0,0 +1,11 @@ +[flake8] +exclude = + .git, + __pycache__, + build, + dist, + doc/source/conf.py +max-line-length = 115 +# Ignore some style 'errors' produced while formatting by 'black' +# https://black.readthedocs.io/en/stable/guides/using_black_with_other_tools.html#labels-why-pycodestyle-warnings +extend-ignore = E203 diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 00000000..c4588061 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,43 @@ +default_language_version: + python: python3 +ci: + autofix_commit_msg: | + [pre-commit.ci] auto fixes from pre-commit hooks + autofix_prs: true + autoupdate_branch: 'pre-commit-autoupdate' + autoupdate_commit_msg: '[pre-commit.ci] pre-commit autoupdate' + autoupdate_schedule: monthly + skip: [no-commit-to-branch] + submodules: false +repos: + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.6.0 + hooks: + - id: check-yaml + - id: end-of-file-fixer + - id: trailing-whitespace + exclude: '\.(rst|txt)$' + - repo: https://github.com/psf/black + rev: 24.4.2 + hooks: + - id: black + - repo: https://github.com/pycqa/flake8 + rev: 7.0.0 + hooks: + - id: flake8 + - repo: https://github.com/pycqa/isort + rev: 5.13.2 + hooks: + - id: isort + args: ["--profile", "black"] + - repo: https://github.com/kynan/nbstripout + rev: 0.7.1 + hooks: + - id: nbstripout + - repo: https://github.com/pre-commit/pre-commit-hooks + rev: v4.4.0 + hooks: + - id: no-commit-to-branch + name: Prevent Commit to Main Branch + args: ["--branch", "main"] + stages: [pre-commit] diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 00000000..e537e24a --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,22 @@ +[tool.black] +line-length = 115 +include = '\.pyi?$' +exclude = ''' +/( + \.git + | \.hg + | \.mypy_cache + | \.tox + | \.venv + | \.rst + | \.txt + | _build + | buck-out + | build + | dist + + # The following are specific to Black, you probably don't want those. + | blib2to3 + | tests/data +)/ +''' From 6eb7215b1032d5911127e346e58613a532733993 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Mon, 22 Jul 2024 22:34:37 +0000 Subject: [PATCH 2/5] [pre-commit.ci] auto fixes from pre-commit hooks --- conda-recipe/expandpdfguibase.py | 1 + devutils/makesdist | 38 +- setup.py | 10 +- src/diffpy/pdfgui/applications/pdfgui.py | 4 +- src/diffpy/pdfgui/control/calculation.py | 10 +- src/diffpy/pdfgui/control/constraint.py | 6 +- src/diffpy/pdfgui/control/fitdataset.py | 34 +- src/diffpy/pdfgui/control/fitstructure.py | 30 +- src/diffpy/pdfgui/control/fitting.py | 24 +- src/diffpy/pdfgui/control/organizer.py | 6 +- src/diffpy/pdfgui/control/parameter.py | 8 +- src/diffpy/pdfgui/control/pdfdataset.py | 17 +- src/diffpy/pdfgui/control/pdfguicontrol.py | 25 +- src/diffpy/pdfgui/control/pdfguimacros.py | 2 +- src/diffpy/pdfgui/control/pdfstructure.py | 8 +- src/diffpy/pdfgui/control/plotter.py | 29 +- src/diffpy/pdfgui/control/structureviewer.py | 7 +- .../pdfgui/doc/manual/extractEquations.py | 2 +- src/diffpy/pdfgui/doc/manual/fixHTMLCode.py | 2 +- src/diffpy/pdfgui/doc/manual/pdfgui.html | 76 +- src/diffpy/pdfgui/doc/manual/pdfgui.texinfo | 2 +- src/diffpy/pdfgui/doc/tutorial/1050K.gr | 14 +- src/diffpy/pdfgui/doc/tutorial/1100K.gr | 14 +- src/diffpy/pdfgui/doc/tutorial/1150K.gr | 14 +- src/diffpy/pdfgui/doc/tutorial/300K.gr | 14 +- src/diffpy/pdfgui/doc/tutorial/550K.gr | 14 +- src/diffpy/pdfgui/doc/tutorial/650K.gr | 14 +- src/diffpy/pdfgui/doc/tutorial/700K.gr | 14 +- src/diffpy/pdfgui/doc/tutorial/720K.gr | 14 +- src/diffpy/pdfgui/doc/tutorial/730K.gr | 14 +- src/diffpy/pdfgui/doc/tutorial/740K.gr | 14 +- src/diffpy/pdfgui/doc/tutorial/750K.gr | 14 +- src/diffpy/pdfgui/doc/tutorial/800K.gr | 14 +- src/diffpy/pdfgui/doc/tutorial/880K.gr | 14 +- src/diffpy/pdfgui/doc/tutorial/980K.gr | 14 +- src/diffpy/pdfgui/doc/tutorial/CdSe-3nm.gr | 9004 ++++++++-------- src/diffpy/pdfgui/doc/tutorial/CdSe-bulk.gr | 9006 ++++++++--------- src/diffpy/pdfgui/doc/tutorial/Ni-neutron.gr | 14 +- src/diffpy/pdfgui/doc/tutorial/Ni-xray.gr | 4006 ++++---- src/diffpy/pdfgui/doc/tutorial/Ni.stru | 6 +- .../doc/tutorial/tui_mno_bond_lengths.py | 4 +- src/diffpy/pdfgui/gui/aboutdialog.py | 37 +- src/diffpy/pdfgui/gui/adddatapanel.py | 1 + src/diffpy/pdfgui/gui/addphasepanel.py | 13 +- src/diffpy/pdfgui/gui/blankpanel.py | 1 + src/diffpy/pdfgui/gui/bondangledialog.py | 9 +- src/diffpy/pdfgui/gui/bondlengthdialog.py | 7 +- src/diffpy/pdfgui/gui/calculationpanel.py | 59 +- .../pdfgui/gui/datasetconfigurepanel.py | 81 +- .../pdfgui/gui/datasetconstraintpanel.py | 29 +- src/diffpy/pdfgui/gui/datasetpanel.py | 5 +- src/diffpy/pdfgui/gui/datasetresultspanel.py | 23 +- src/diffpy/pdfgui/gui/dopingseriespanel.py | 27 +- src/diffpy/pdfgui/gui/errorreportdialog.py | 23 +- .../gui/errorreportdialog_control_fix.py | 15 +- src/diffpy/pdfgui/gui/errorwrapper.py | 10 +- src/diffpy/pdfgui/gui/extendedplotframe.py | 18 +- src/diffpy/pdfgui/gui/fitnotebookpanel.py | 3 +- src/diffpy/pdfgui/gui/fittree.py | 45 +- src/diffpy/pdfgui/gui/journalpanel.py | 2 + src/diffpy/pdfgui/gui/mainframe.py | 162 +- src/diffpy/pdfgui/gui/outputpanel.py | 5 +- src/diffpy/pdfgui/gui/parameterspanel.py | 36 +- src/diffpy/pdfgui/gui/pdfguiglobals.py | 1 + src/diffpy/pdfgui/gui/pdfpanel.py | 2 +- src/diffpy/pdfgui/gui/phaseconfigurepanel.py | 115 +- .../pdfgui/gui/phaseconstraintspanel.py | 127 +- src/diffpy/pdfgui/gui/phasenotebookpanel.py | 17 +- src/diffpy/pdfgui/gui/phasepanelutils.py | 9 +- src/diffpy/pdfgui/gui/phaseresultspanel.py | 80 +- src/diffpy/pdfgui/gui/plotpanel.py | 29 +- src/diffpy/pdfgui/gui/preferencespanel.py | 29 +- src/diffpy/pdfgui/gui/resultspanel.py | 1 + src/diffpy/pdfgui/gui/rseriespanel.py | 21 +- src/diffpy/pdfgui/gui/sgconstraindialog.py | 6 +- src/diffpy/pdfgui/gui/sgstructuredialog.py | 22 +- src/diffpy/pdfgui/gui/supercelldialog.py | 4 +- .../pdfgui/gui/temperatureseriespanel.py | 18 +- src/diffpy/pdfgui/gui/tooltips.py | 8 +- src/diffpy/pdfgui/gui/welcomepanel.py | 9 +- .../gui/wxextensions/autowidthlabelsgrid.py | 8 +- .../pdfgui/gui/wxextensions/listctrls.py | 34 +- .../pdfgui/gui/wxextensions/validators.py | 3 +- src/diffpy/pdfgui/gui/wxextensions/wx12.py | 5 +- src/diffpy/pdfgui/tests/__init__.py | 3 +- src/diffpy/pdfgui/tests/debug.py | 1 + src/diffpy/pdfgui/tests/run.py | 3 +- src/diffpy/pdfgui/tests/rundeps.py | 1 + .../pdfgui/tests/showphasenotebookpanel.py | 2 +- src/diffpy/pdfgui/tests/testaboutdialog.py | 4 +- src/diffpy/pdfgui/tests/testcalculation.py | 4 +- src/diffpy/pdfgui/tests/testconstraint.py | 3 +- src/diffpy/pdfgui/tests/testdata/300K.gr | 14 +- src/diffpy/pdfgui/tests/testdata/550K.gr | 14 +- .../pdfgui/tests/testdata/Ni_2-8.chi.gr | 4006 ++++---- .../pdfgui/tests/testdopingseriespanel.py | 3 +- .../pdfgui/tests/testextendedplotframe.py | 2 +- src/diffpy/pdfgui/tests/testfitdataset.py | 1 + src/diffpy/pdfgui/tests/testfitstructure.py | 8 +- .../pdfgui/tests/testinsertrowsdialog.py | 1 + src/diffpy/pdfgui/tests/testloadproject.py | 2 +- src/diffpy/pdfgui/tests/testparameter.py | 3 +- .../pdfgui/tests/testparameterspanel.py | 2 +- src/diffpy/pdfgui/tests/testpdfstructure.py | 7 +- .../pdfgui/tests/testphaseconfigurepanel.py | 11 +- .../pdfgui/tests/teststructureviewer.py | 4 +- .../tests/testtemperatureseriespanel.py | 3 +- src/diffpy/pdfgui/tests/testutils.py | 5 +- src/diffpy/pdfgui/utils.py | 2 +- src/diffpy/pdfgui/version.py | 7 +- 110 files changed, 13676 insertions(+), 14176 deletions(-) diff --git a/conda-recipe/expandpdfguibase.py b/conda-recipe/expandpdfguibase.py index e97614d1..84381557 100644 --- a/conda-recipe/expandpdfguibase.py +++ b/conda-recipe/expandpdfguibase.py @@ -1,6 +1,7 @@ #!/usr/bin/env python import sys + from pkg_resources import Requirement, resource_filename pkg = Requirement.parse("diffpy.pdfgui") diff --git a/devutils/makesdist b/devutils/makesdist index 6aaae616..e77257ea 100755 --- a/devutils/makesdist +++ b/devutils/makesdist @@ -1,51 +1,57 @@ #!/usr/bin/env python -'''Create source distribution tar.gz archive, where each file belongs +"""Create source distribution tar.gz archive, where each file belongs to a root user and modification time is set to the git commit time. -''' +""" -import sys +import glob +import gzip import os import subprocess -import glob +import sys import tarfile -import gzip BASEDIR = os.path.dirname(os.path.dirname(os.path.realpath(__file__))) sys.path.insert(0, BASEDIR) -from setup import versiondata, FALLBACK_VERSION -timestamp = versiondata.getint('DEFAULT', 'timestamp') +from setup import FALLBACK_VERSION, versiondata -vfb = versiondata.get('DEFAULT', 'version').split('.post')[0] + '.post0' +timestamp = versiondata.getint("DEFAULT", "timestamp") + +vfb = versiondata.get("DEFAULT", "version").split(".post")[0] + ".post0" emsg = "Invalid FALLBACK_VERSION. Expected %r got %r." assert vfb == FALLBACK_VERSION, emsg % (vfb, FALLBACK_VERSION) + def inform(s): sys.stdout.write(s) sys.stdout.flush() return + inform('Run "setup.py sdist --formats=tar" ') -cmd_sdist = [sys.executable] + 'setup.py sdist --formats=tar'.split() -ec = subprocess.call(cmd_sdist, cwd=BASEDIR, stdout=open(os.devnull, 'w')) -if ec: sys.exit(ec) +cmd_sdist = [sys.executable] + "setup.py sdist --formats=tar".split() +ec = subprocess.call(cmd_sdist, cwd=BASEDIR, stdout=open(os.devnull, "w")) +if ec: + sys.exit(ec) inform("[done]\n") -tarname = max(glob.glob(BASEDIR + '/dist/*.tar'), key=os.path.getmtime) +tarname = max(glob.glob(BASEDIR + "/dist/*.tar"), key=os.path.getmtime) tfin = tarfile.open(tarname) -fpout = gzip.GzipFile(tarname + '.gz', 'w', mtime=0) -tfout = tarfile.open(fileobj=fpout, mode='w') +fpout = gzip.GzipFile(tarname + ".gz", "w", mtime=0) +tfout = tarfile.open(fileobj=fpout, mode="w") + def fixtarinfo(tinfo): tinfo.uid = tinfo.gid = 0 - tinfo.uname = tinfo.gname = 'root' + tinfo.uname = tinfo.gname = "root" tinfo.mtime = timestamp tinfo.mode &= ~0o022 return tinfo -inform('Filter %s --> %s.gz ' % (2 * (os.path.basename(tarname),))) + +inform("Filter %s --> %s.gz " % (2 * (os.path.basename(tarname),))) for ti in tfin: tfout.addfile(fixtarinfo(ti), tfin.extractfile(ti)) diff --git a/setup.py b/setup.py index 713b4c31..1738fd08 100755 --- a/setup.py +++ b/setup.py @@ -11,7 +11,8 @@ import os import re import sys -from setuptools import setup, find_packages + +from setuptools import find_packages, setup # Use this version when git data are not available, like in git zip archive. # Update when tagging a new release. @@ -28,7 +29,7 @@ def gitinfo(): - from subprocess import Popen, PIPE + from subprocess import PIPE, Popen kw = dict(stdout=PIPE, cwd=MYDIR, universal_newlines=True) proc = Popen(["git", "describe", "--tags", "--match=v[[:digit:]]*"], **kw) @@ -67,10 +68,7 @@ def getversioncfg(): cp = RawConfigParser() cp.read(versioncfgfile) d = cp.defaults() - rewrite = not d or ( - g["commit"] - and (g["version"] != d.get("version") or g["commit"] != d.get("commit")) - ) + rewrite = not d or (g["commit"] and (g["version"] != d.get("version") or g["commit"] != d.get("commit"))) if rewrite: cp.set("DEFAULT", "version", g["version"]) cp.set("DEFAULT", "commit", g["commit"]) diff --git a/src/diffpy/pdfgui/applications/pdfgui.py b/src/diffpy/pdfgui/applications/pdfgui.py index b6bace7c..0c1f0b48 100755 --- a/src/diffpy/pdfgui/applications/pdfgui.py +++ b/src/diffpy/pdfgui/applications/pdfgui.py @@ -34,9 +34,9 @@ from __future__ import print_function -import sys -import os import getopt +import os +import sys def usage(): diff --git a/src/diffpy/pdfgui/control/calculation.py b/src/diffpy/pdfgui/control/calculation.py index 0bd6d6c9..2cab84a3 100644 --- a/src/diffpy/pdfgui/control/calculation.py +++ b/src/diffpy/pdfgui/control/calculation.py @@ -19,11 +19,13 @@ import copy import math -from diffpy.pdfgui.control.controlerrors import ControlConfigError -from diffpy.pdfgui.control.controlerrors import ControlKeyError -from diffpy.pdfgui.control.controlerrors import ControlValueError +from diffpy.pdfgui.control.controlerrors import ( + ControlConfigError, + ControlKeyError, + ControlValueError, +) from diffpy.pdfgui.control.pdfcomponent import PDFComponent -from diffpy.pdfgui.utils import safeCPickleDumps, pickle_loads +from diffpy.pdfgui.utils import pickle_loads, safeCPickleDumps class Calculation(PDFComponent): diff --git a/src/diffpy/pdfgui/control/constraint.py b/src/diffpy/pdfgui/control/constraint.py index 1ba0487f..fac6082f 100644 --- a/src/diffpy/pdfgui/control/constraint.py +++ b/src/diffpy/pdfgui/control/constraint.py @@ -17,8 +17,8 @@ constraints will be stored in { variable : constraint } dictionary """ -import re import math +import re from diffpy.pdfgui.control.controlerrors import ControlSyntaxError @@ -135,9 +135,7 @@ def __setattr__(self, name, value): raise ControlSyntaxError(message) # few more checks of the formula: if newformula.find("**") != -1: - emsg = ( - "invalid constraint formula '{}', " "operator '**' not supported." - ).format(newformula) + emsg = ("invalid constraint formula '{}', " "operator '**' not supported.").format(newformula) raise ControlSyntaxError(emsg) # checks checked self.__dict__["formula"] = newformula diff --git a/src/diffpy/pdfgui/control/fitdataset.py b/src/diffpy/pdfgui/control/fitdataset.py index 2e07edea..eb3d6a80 100644 --- a/src/diffpy/pdfgui/control/fitdataset.py +++ b/src/diffpy/pdfgui/control/fitdataset.py @@ -17,11 +17,12 @@ """ import copy + import numpy -from diffpy.pdfgui.control.pdfdataset import PDFDataSet -from diffpy.pdfgui.control.parameter import Parameter from diffpy.pdfgui.control.controlerrors import ControlStatusError +from diffpy.pdfgui.control.parameter import Parameter +from diffpy.pdfgui.control.pdfdataset import PDFDataSet class FitDataSet(PDFDataSet): @@ -112,9 +113,7 @@ def getYNames(self): returns list of strings """ - ynames = ["Gobs", "Gcalc", "Gdiff", "Gtrunc", "dGcalc", "crw"] + list( - self.constraints.keys() - ) + ynames = ["Gobs", "Gcalc", "Gdiff", "Gtrunc", "dGcalc", "crw"] + list(self.constraints.keys()) return ynames def getXNames(self): @@ -321,10 +320,7 @@ def writeCalcStr(self): Gdiff = self.Gdiff drcalc = 0.0 for i in range(len(self.rcalc)): - lines.append( - "%g %g %.1f %g %g" - % (self.rcalc[i], self.Gcalc[i], drcalc, self.dGcalc[i], Gdiff[i]) - ) + lines.append("%g %g %.1f %g %g" % (self.rcalc[i], self.Gcalc[i], drcalc, self.dGcalc[i], Gdiff[i])) # lines are ready here datastring = "\n".join(lines) + "\n" return datastring @@ -636,9 +632,7 @@ def _set_fitrmin(self, value): self._fitrmin = float(value) return - fitrmin = property( - _get_fitrmin, _set_fitrmin, doc="Lower boundary for simulated PDF curve." - ) + fitrmin = property(_get_fitrmin, _set_fitrmin, doc="Lower boundary for simulated PDF curve.") # fitrmax @@ -650,9 +644,7 @@ def _set_fitrmax(self, value): self._fitrmax = float(value) return - fitrmax = property( - _get_fitrmax, _set_fitrmax, doc="Upper boundary for simulated PDF curve." - ) + fitrmax = property(_get_fitrmax, _set_fitrmax, doc="Upper boundary for simulated PDF curve.") # fitrstep @@ -664,9 +656,7 @@ def _set_fitrstep(self, value): self._fitrstep = float(value) return - fitrstep = property( - _get_fitrstep, _set_fitrstep, doc="R-step used for simulated PDF curve." - ) + fitrstep = property(_get_fitrstep, _set_fitrstep, doc="R-step used for simulated PDF curve.") # rcalc @@ -707,9 +697,7 @@ def _set_dGcalc(self, value): self._dGcalc = value return - dGcalc = property( - _get_dGcalc, _set_dGcalc, doc="List of standard deviations of Gcalc." - ) + dGcalc = property(_get_dGcalc, _set_dGcalc, doc="List of standard deviations of Gcalc.") # Gtrunc @@ -757,9 +745,7 @@ def _get_Gdiff(self): rv = [] return rv - Gdiff = property( - _get_Gdiff, doc="Difference between observed and calculated PDF on rcalc grid." - ) + Gdiff = property(_get_Gdiff, doc="Difference between observed and calculated PDF on rcalc grid.") # crw def _get_crw(self): diff --git a/src/diffpy/pdfgui/control/fitstructure.py b/src/diffpy/pdfgui/control/fitstructure.py index d38f2022..f643c994 100644 --- a/src/diffpy/pdfgui/control/fitstructure.py +++ b/src/diffpy/pdfgui/control/fitstructure.py @@ -18,12 +18,13 @@ import copy import re + import numpy -from diffpy.pdfgui.control.pdfstructure import PDFStructure from diffpy.pdfgui.control.constraint import Constraint -from diffpy.pdfgui.control.parameter import Parameter from diffpy.pdfgui.control.controlerrors import ControlTypeError, ControlValueError +from diffpy.pdfgui.control.parameter import Parameter +from diffpy.pdfgui.control.pdfstructure import PDFStructure from diffpy.structure import Atom @@ -309,12 +310,7 @@ def expandSuperCell(self, mno): # back to business acd = self._popAtomConstraints() mnofloats = numpy.array(mno[:3], dtype=float) - ijklist = [ - (i, j, k) - for i in range(mno[0]) - for j in range(mno[1]) - for k in range(mno[2]) - ] + ijklist = [(i, j, k) for i in range(mno[0]) for j in range(mno[1]) for k in range(mno[2])] # build a list of new atoms newatoms = [] for a in self.initial: @@ -389,9 +385,7 @@ def getSpaceGroupList(self): existing_names[sg.short_name] = True # sort by International Tables number, stay compatible with 2.3 n_sg = [(sg.number % 1000, sg) for sg in unique_named_list] - n_sg = sorted( - n_sg, key=lambda x: x[0] - ) # sort by the first element of tuple. + n_sg = sorted(n_sg, key=lambda x: x[0]) # sort by the first element of tuple. FitStructure.sorted_standard_space_groups = [sg for n, sg in n_sg] sglist = list(FitStructure.sorted_standard_space_groups) if self.custom_spacegroup: @@ -438,9 +432,7 @@ def expandAsymmetricUnit(self, spacegroup, indices, sgoffset=[0, 0, 0]): coreatoms = [self.initial[i] for i in ruindices] corepos = [a.xyz for a in coreatoms] coreUijs = [a.U for a in coreatoms] - eau = ExpandAsymmetricUnit( - spacegroup, corepos, coreUijs, sgoffset=sgoffset, eps=self.symposeps - ) + eau = ExpandAsymmetricUnit(spacegroup, corepos, coreUijs, sgoffset=sgoffset, eps=self.symposeps) # build a nested list of new atoms: newatoms = [] for i in range(len(coreatoms)): @@ -469,9 +461,7 @@ def expandAsymmetricUnit(self, spacegroup, indices, sgoffset=[0, 0, 0]): self._restoreAtomConstraints(acd) return - def applySymmetryConstraints( - self, spacegroup, indices, posflag, Uijflag, sgoffset=[0, 0, 0] - ): + def applySymmetryConstraints(self, spacegroup, indices, posflag, Uijflag, sgoffset=[0, 0, 0]): """Generate symmetry constraints for positions and thermal factors. Both positions and thermal factors may get corrected to reflect space group symmetry. Old positional and thermal constraints get @@ -511,9 +501,7 @@ def applySymmetryConstraints( selatoms = [self.initial[i] for i in uindices] selpos = [a.xyz for a in selatoms] selUijs = [a.U for a in selatoms] - symcon = SymmetryConstraints( - spacegroup, selpos, selUijs, sgoffset=sgoffset, eps=self.symposeps - ) + symcon = SymmetryConstraints(spacegroup, selpos, selUijs, sgoffset=sgoffset, eps=self.symposeps) # deal with positions if posflag: # fix positions: @@ -762,8 +750,8 @@ def load(self, z, subpath): subpath -- path to its own storage within project file """ # subpath = projname/fitname/structure/myname/ - from diffpy.pdfgui.utils import asunicode from diffpy.pdfgui.control.pdfguicontrol import CtrlUnpickler + from diffpy.pdfgui.utils import asunicode subs = subpath.split("/") rootDict = z.fileTree[subs[0]][subs[1]][subs[2]][subs[3]] diff --git a/src/diffpy/pdfgui/control/fitting.py b/src/diffpy/pdfgui/control/fitting.py index a11cbb71..8e24aed5 100644 --- a/src/diffpy/pdfgui/control/fitting.py +++ b/src/diffpy/pdfgui/control/fitting.py @@ -18,11 +18,13 @@ import threading import time +from diffpy.pdfgui.control.controlerrors import ( + ControlError, + ControlStatusError, + ControlValueError, +) from diffpy.pdfgui.control.organizer import Organizer -from diffpy.pdfgui.control.controlerrors import ControlError -from diffpy.pdfgui.control.controlerrors import ControlStatusError -from diffpy.pdfgui.control.controlerrors import ControlValueError -from diffpy.pdfgui.utils import safeCPickleDumps, pickle_loads +from diffpy.pdfgui.utils import pickle_loads, safeCPickleDumps # helper routines to deal with PDFfit2 exceptions @@ -30,11 +32,11 @@ def getEngineExceptions(): """Return a tuple of possible exceptions from diffpy.pdffit2.pdffit2.""" from diffpy.pdffit2.pdffit2 import ( - dataError, - unassignedError, + calculationError, constraintError, + dataError, structureError, - calculationError, + unassignedError, ) engine_exceptions = ( @@ -212,9 +214,7 @@ def load(self, z, subpath): self.parameters = CtrlUnpickler.loads(z.read(subpath + "parameters")) if "steps" in rootDict: - self.itemIndex, self.dataNameDict, self.snapshots = pickle_loads( - z.read(subpath + "steps") - ) + self.itemIndex, self.dataNameDict, self.snapshots = pickle_loads(z.read(subpath + "steps")) if "result" in rootDict: self.rw, self.res = pickle_loads(z.read(subpath + "result")) @@ -582,9 +582,7 @@ def close(self, force=False): # way while user choose to stop forcefully else: if self.isThreadRunning(): - raise ControlStatusError( - "Fitting: Fitting %s is still running" % self.name - ) + raise ControlStatusError("Fitting: Fitting %s is still running" % self.name) if self.thread is not None: self.thread.join() diff --git a/src/diffpy/pdfgui/control/organizer.py b/src/diffpy/pdfgui/control/organizer.py index 4746877f..31a5a095 100644 --- a/src/diffpy/pdfgui/control/organizer.py +++ b/src/diffpy/pdfgui/control/organizer.py @@ -13,11 +13,11 @@ # ############################################################################## -from diffpy.pdfgui.control.pdfcomponent import PDFComponent -from diffpy.pdfgui.control.fitdataset import FitDataSet -from diffpy.pdfgui.control.fitstructure import FitStructure from diffpy.pdfgui.control.calculation import Calculation from diffpy.pdfgui.control.controlerrors import ControlTypeError +from diffpy.pdfgui.control.fitdataset import FitDataSet +from diffpy.pdfgui.control.fitstructure import FitStructure +from diffpy.pdfgui.control.pdfcomponent import PDFComponent class Organizer(PDFComponent): diff --git a/src/diffpy/pdfgui/control/parameter.py b/src/diffpy/pdfgui/control/parameter.py index 6ed68d6d..cf8bbe94 100644 --- a/src/diffpy/pdfgui/control/parameter.py +++ b/src/diffpy/pdfgui/control/parameter.py @@ -20,10 +20,10 @@ import six from diffpy.pdfgui.control.controlerrors import ( - ControlTypeError, + ControlError, ControlKeyError, ControlRuntimeError, - ControlError, + ControlTypeError, ) @@ -143,9 +143,7 @@ def __getLinkedValue(self): try: srcpar = srcfit.parameters[srcidx] except KeyError: - raise ControlKeyError( - "Fitting '%s' has no parameter %s" % (fitname, srcidx) - ) + raise ControlKeyError("Fitting '%s' has no parameter %s" % (fitname, srcidx)) if srcpar.refined is not None: value = srcpar.refined diff --git a/src/diffpy/pdfgui/control/pdfdataset.py b/src/diffpy/pdfgui/control/pdfdataset.py index 4159ae77..6caa978a 100644 --- a/src/diffpy/pdfgui/control/pdfdataset.py +++ b/src/diffpy/pdfgui/control/pdfdataset.py @@ -16,14 +16,14 @@ """class PDFDataSet for experimental PDF data. """ +import copy import os.path import re -import copy import time from getpass import getuser +from diffpy.pdfgui.control.controlerrors import ControlFileError, ControlKeyError from diffpy.pdfgui.control.pdfcomponent import PDFComponent -from diffpy.pdfgui.control.controlerrors import ControlKeyError, ControlFileError class PDFDataSet(PDFComponent): @@ -146,10 +146,10 @@ def read(self, filename): self.readStr(fp.read()) except PDFDataFormatError as err: basename = os.path.basename(filename) - emsg = ( - "Could not open '%s' due to unsupported file format " - + "or corrupted data. [%s]" - ) % (basename, err) + emsg = ("Could not open '%s' due to unsupported file format " + "or corrupted data. [%s]") % ( + basename, + err, + ) raise ControlFileError(emsg) self.filename = os.path.abspath(filename) return self @@ -330,10 +330,7 @@ def writeStr(self): lines.append("##### start data") lines.append("#L r(A) G(r) d_r d_Gr") for i in range(len(self.robs)): - lines.append( - "%g %g %g %g" - % (self.robs[i], self.Gobs[i], self.drobs[i], self.dGobs[i]) - ) + lines.append("%g %g %g %g" % (self.robs[i], self.Gobs[i], self.drobs[i], self.dGobs[i])) # that should be it datastring = "\n".join(lines) + "\n" return datastring diff --git a/src/diffpy/pdfgui/control/pdfguicontrol.py b/src/diffpy/pdfgui/control/pdfguicontrol.py index e0816b74..bb39baa8 100644 --- a/src/diffpy/pdfgui/control/pdfguicontrol.py +++ b/src/diffpy/pdfgui/control/pdfguicontrol.py @@ -15,22 +15,25 @@ from __future__ import print_function -import sys import os +import sys import threading import time + import six import six.moves.cPickle as pickle -from diffpy.pdfgui.control.pdflist import PDFList -from diffpy.pdfgui.control.fitting import Fitting from diffpy.pdfgui.control.calculation import Calculation +from diffpy.pdfgui.control.controlerrors import ( + ControlError, + ControlFileError, + ControlTypeError, +) from diffpy.pdfgui.control.fitdataset import FitDataSet -from diffpy.pdfgui.control.organizer import Organizer from diffpy.pdfgui.control.fitstructure import FitStructure -from diffpy.pdfgui.control.controlerrors import ControlError -from diffpy.pdfgui.control.controlerrors import ControlFileError -from diffpy.pdfgui.control.controlerrors import ControlTypeError +from diffpy.pdfgui.control.fitting import Fitting +from diffpy.pdfgui.control.organizer import Organizer +from diffpy.pdfgui.control.pdflist import PDFList from diffpy.pdfgui.utils import asunicode, quote_plain @@ -434,9 +437,9 @@ def save(self, projfile=None): # self.projfile is unset here only due to a bug. assert self.projfile is not None - import zipfile import shutil import tempfile + import zipfile projbase = os.path.basename(self.projfile) projName = os.path.splitext(projbase)[0] @@ -492,9 +495,7 @@ def start(self, IDlist): self.redirectStdout() fits = [ID for ID in IDlist if isinstance(ID, Fitting)] # only add calcs which is not in fits, because fits will automatically run calcs under it anyway - calcs = [ - ID for ID in IDlist if isinstance(ID, Calculation) and ID.owner not in fits - ] + calcs = [ID for ID in IDlist if isinstance(ID, Calculation) and ID.owner not in fits] for calc in calcs: calc.start() self.enqueue(fits) @@ -516,7 +517,7 @@ def redirectStdout(self): This redirect engine output to StringIO if not done yet. """ - from diffpy.pdffit2 import redirect_stdout, output + from diffpy.pdffit2 import output, redirect_stdout if output.stdout is sys.stdout: redirect_stdout(six.StringIO()) diff --git a/src/diffpy/pdfgui/control/pdfguimacros.py b/src/diffpy/pdfgui/control/pdfguimacros.py index 91d78ed2..e91abe76 100644 --- a/src/diffpy/pdfgui/control/pdfguimacros.py +++ b/src/diffpy/pdfgui/control/pdfguimacros.py @@ -17,8 +17,8 @@ from __future__ import print_function -import os import copy +import os from diffpy.pdfgui.control.controlerrors import ControlValueError from diffpy.pdfgui.control.fitdataset import FitDataSet diff --git a/src/diffpy/pdfgui/control/pdfstructure.py b/src/diffpy/pdfgui/control/pdfstructure.py index 12e383c4..a63f8692 100644 --- a/src/diffpy/pdfgui/control/pdfstructure.py +++ b/src/diffpy/pdfgui/control/pdfstructure.py @@ -16,9 +16,9 @@ import copy import re -from diffpy.structure import PDFFitStructure +from diffpy.pdfgui.control.controlerrors import ControlFileError, ControlKeyError from diffpy.pdfgui.control.pdfcomponent import PDFComponent -from diffpy.pdfgui.control.controlerrors import ControlKeyError, ControlFileError +from diffpy.structure import PDFFitStructure class PDFStructure(PDFComponent, PDFFitStructure): @@ -73,9 +73,7 @@ def copy(self, other=None): # dictionary of allowed keys from self.pdffit dictionary, # that can be used in setvar and getvar methods. - _allowed_pdffit_vars = dict.fromkeys( - ("spdiameter", "stepcut", "delta1", "delta2", "sratio", "rcut") - ) + _allowed_pdffit_vars = dict.fromkeys(("spdiameter", "stepcut", "delta1", "delta2", "sratio", "rcut")) def setvar(self, var, value): """assign to data member using PdfFit-style variable diff --git a/src/diffpy/pdfgui/control/plotter.py b/src/diffpy/pdfgui/control/plotter.py index d475e2a2..77134fb1 100644 --- a/src/diffpy/pdfgui/control/plotter.py +++ b/src/diffpy/pdfgui/control/plotter.py @@ -13,9 +13,8 @@ # ############################################################################## +from diffpy.pdfgui.control.controlerrors import ControlConfigError, ControlStatusError from diffpy.pdfgui.control.pdfcomponent import PDFComponent -from diffpy.pdfgui.control.controlerrors import ControlConfigError -from diffpy.pdfgui.control.controlerrors import ControlStatusError from diffpy.pdfgui.gui.extendedplotframe import ExtendedPlotFrame # Preset plotting style @@ -226,19 +225,16 @@ def validate(self): # The logic below make sure only one of them can be true. if bItemIsVector: if self.bMultiData or self.bMultiStep: - emsg = ( - "({}, {}) can't be plotted with multiple " "refinements/steps" - ).format(self.xStr, self.yStr) + emsg = ("({}, {}) can't be plotted with multiple " "refinements/steps").format( + self.xStr, self.yStr + ) raise ControlConfigError(emsg) else: if not self.bMultiData and not self.bMultiStep: - raise ControlConfigError( - "(%s, %s) is a single point" % (self.xStr, self.yStr) - ) + raise ControlConfigError("(%s, %s) is a single point" % (self.xStr, self.yStr)) elif self.bMultiData and self.bMultiStep: emsg = ( - "({}, {}) can't be plotted with both multiple " - "refinements and multiple steps" + "({}, {}) can't be plotted with both multiple " "refinements and multiple steps" ).format(self.xStr, self.yStr) raise ControlConfigError(emsg) @@ -351,9 +347,7 @@ def draw(self): hMin = min(rs) hMax = max(rs) - self.plotwnd.insertCurve( - [hMin, hMax], [self.offset, self.offset], baselineStyle - ) + self.plotwnd.insertCurve([hMin, hMax], [self.offset, self.offset], baselineStyle) else: # update only self.plotwnd.updateData(self.ref, xs, ys) @@ -497,9 +491,7 @@ def _addCurve(dataIds): if y in ("Gdiff", "crw") and group == -1 and len(yset) > 0: _offset = shift # Create curve, get data for it and update it in the plot - curve = Plotter.Curve( - legend, self.window, xName, y, step, dataIds, _offset, style - ) + curve = Plotter.Curve(legend, self.window, xName, y, step, dataIds, _offset, style) self.curves.append(curve) return @@ -623,7 +615,8 @@ def export(self, filename): # Check if any curve if len(self.curves) == 0: return - import time, getpass + import getpass + import time outfile = open(filename, "w") header = "# Generated on %s by %s.\n" % (time.ctime(), getpass.getuser()) @@ -661,7 +654,7 @@ def _exportCompactData(fp, xylist, xynames=None): datasets = [] datanames = [] xt2idx = {} - for ((x, y), (xn, yn)) in zip(xylist, xynames): + for (x, y), (xn, yn) in zip(xylist, xynames): if x is None or not len(x): continue if y is None or not len(y): diff --git a/src/diffpy/pdfgui/control/structureviewer.py b/src/diffpy/pdfgui/control/structureviewer.py index 5a064222..d58def97 100644 --- a/src/diffpy/pdfgui/control/structureviewer.py +++ b/src/diffpy/pdfgui/control/structureviewer.py @@ -18,10 +18,10 @@ from __future__ import print_function -import sys import os.path -import tempfile import shutil +import sys +import tempfile from diffpy.pdfgui.control.controlerrors import ControlConfigError @@ -151,6 +151,7 @@ def __del__(self): # short circuit if nothing has been created if self._tmpdir is None: return + # Function for showing unremovable files def onerror(fnc, path, error): print(("Cannot remove %s - %s" % (path, error)), file=sys.stderr) @@ -176,8 +177,8 @@ def _getArgumentList(self, strupath): Return list of arguments (not including the viewer executable). """ - import shlex import re + import shlex # make sure shlex.split is not called with None, because # it would read standard input diff --git a/src/diffpy/pdfgui/doc/manual/extractEquations.py b/src/diffpy/pdfgui/doc/manual/extractEquations.py index 99d833a4..4c32814e 100644 --- a/src/diffpy/pdfgui/doc/manual/extractEquations.py +++ b/src/diffpy/pdfgui/doc/manual/extractEquations.py @@ -31,9 +31,9 @@ ############################################################################## # business -import sys import os import shutil +import sys def loadEquations(): diff --git a/src/diffpy/pdfgui/doc/manual/fixHTMLCode.py b/src/diffpy/pdfgui/doc/manual/fixHTMLCode.py index 86fa6b2e..0e410070 100644 --- a/src/diffpy/pdfgui/doc/manual/fixHTMLCode.py +++ b/src/diffpy/pdfgui/doc/manual/fixHTMLCode.py @@ -16,9 +16,9 @@ ############################################################################## # business -import sys import os import re +import sys eqcnt = 0 diff --git a/src/diffpy/pdfgui/doc/manual/pdfgui.html b/src/diffpy/pdfgui/doc/manual/pdfgui.html index 849b22fe..24219aeb 100644 --- a/src/diffpy/pdfgui/doc/manual/pdfgui.html +++ b/src/diffpy/pdfgui/doc/manual/pdfgui.html @@ -9,7 +9,7 @@ indicated in each source file). -->