Skip to content

Commit

Permalink
fix python2 to python3 to make pre-commit black hook work (#16)
Browse files Browse the repository at this point in the history
* fix python2 to python3 to make pre-commit black hook work

* add pyproject.toml and pre-commit work
  • Loading branch information
stevenhua0320 authored Jul 29, 2024
1 parent 75e5111 commit fd65cf6
Show file tree
Hide file tree
Showing 31 changed files with 1,035 additions and 1,093 deletions.
8 changes: 4 additions & 4 deletions devutils/makesdist
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ sys.path.insert(0, BASEDIR)
from setup import versiondata
timestamp = versiondata.getint('DEFAULT', 'timestamp')

print 'Run "setup.py sdist --formats=tar"',
print('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)
print "[done]"
print("[done]")

tarname = max(glob.glob(BASEDIR + '/dist/*.tar'), key=os.path.getmtime)

Expand All @@ -36,8 +36,8 @@ def fixtarinfo(tinfo):
return tinfo


print 'Filter %s --> %s.gz' % (2 * (os.path.basename(tarname),)),
print('Filter %s --> %s.gz' % (2 * (os.path.basename(tarname),)),)
for ti in tfin:
tfout.addfile(fixtarinfo(ti), tfin.extractfile(ti))
os.remove(tarname)
print "[done]"
print("[done]")
46 changes: 25 additions & 21 deletions devutils/prep.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

__basedir__ = os.getcwdu()

from numpy.compat import unicode

# Example imports


Expand All @@ -18,20 +20,21 @@ def __init__(self):

def test(self, call, *args, **kwds):
m = sys.modules[call.__module__]
testname = m.__name__+'.'+call.__name__
testname = m.__name__ + "." + call.__name__
path = os.path.dirname(m.__file__)
os.chdir(path)
try:
call(*args, **kwds)
self.messages.append("%s: success" %testname)
except Exception, e:
self.messages.append("%s: error, details below.\n%s" %(testname, e))
self.messages.append("%s: success" % testname)
except Exception as e:
self.messages.append("%s: error, details below.\n%s" % (testname, e))
finally:
os.chdir(__basedir__)

def report(self):
print '==== Results of Tests ===='
print '\n'.join(self.messages)
print("==== Results of Tests ====")
print("\n".join(self.messages))


def scrubeol(directory, filerestr):
"""Use unix-style endlines for files in directory matched by regex string.
Expand All @@ -50,11 +53,11 @@ def scrubeol(directory, filerestr):
text = unicode(original.read())
original.close()

updated = io.open(f, 'w', newline='\n')
updated = io.open(f, "w", newline="\n")
updated.write(text)
updated.close()

print "Updated %s to unix-style endlines." %f
print("Updated %s to unix-style endlines." % f)


def rm(directory, filerestr):
Expand All @@ -72,14 +75,13 @@ def rm(directory, filerestr):
for f in files:
os.remove(f)

print "Deleted %s." %f

print("Deleted %s." % f)


if __name__ == "__main__":

# Temporarily add examples to path
lib_path = os.path.abspath(os.path.join('..','doc','examples'))
lib_path = os.path.abspath(os.path.join("..", "doc", "examples"))
sys.path.append(lib_path)

# Delete existing files that don't necessarily have a fixed name.
Expand All @@ -88,14 +90,16 @@ def rm(directory, filerestr):

### Testing examples
examples = Test()
test_names = ["extract_single_peak",
"parameter_summary",
"fit_initial",
"query_results",
"multimodel_known_dG1",
"multimodel_known_dG2",
"multimodel_unknown_dG1",
"multimodel_unknown_dG2"]
test_names = [
"extract_single_peak",
"parameter_summary",
"fit_initial",
"query_results",
"multimodel_known_dG1",
"multimodel_known_dG2",
"multimodel_unknown_dG1",
"multimodel_unknown_dG2",
]

test_modules = []
for test in test_names:
Expand All @@ -107,7 +111,7 @@ def rm(directory, filerestr):
examples.report()

### Convert output of example files to Unix-style endlines for sdist.
if os.linesep != '\n':
print "==== Scrubbing Endlines ===="
if os.linesep != "\n":
print("==== Scrubbing Endlines ====")
# All *.srmise and *.pwa files in examples directory.
scrubeol("../doc/examples/output", r".*(\.srmise|\.pwa)")
45 changes: 12 additions & 33 deletions diffpy/srmise/applications/extract.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,7 @@ def main():
"--pf",
dest="peakfunction",
metavar="PF",
help="Fit peak function PF defined in "
"diffpy.srmise.peaks, e.g. "
"'GaussianOverR(maxwidth=0.7)'",
help="Fit peak function PF defined in " "diffpy.srmise.peaks, e.g. " "'GaussianOverR(maxwidth=0.7)'",
)
parser.add_option(
"--cres",
Expand Down Expand Up @@ -230,34 +228,30 @@ def main():
dest="bpoly0",
type="string",
metavar="a0[c]",
help="Use constant baseline given by y=a0. "
"Append 'c' to make parameter constant.",
help="Use constant baseline given by y=a0. " "Append 'c' to make parameter constant.",
)
group.add_option(
"--bpoly1",
dest="bpoly1",
type="string",
nargs=2,
metavar="a1[c] a0[c]",
help="Use baseline given by y=a1*x + a0. Append 'c' to "
"make parameter constant.",
help="Use baseline given by y=a1*x + a0. Append 'c' to " "make parameter constant.",
)
group.add_option(
"--bpoly2",
dest="bpoly2",
type="string",
nargs=3,
metavar="a2[c] a1[c] a0[c]",
help="Use baseline given by y=a2*x^2+a1*x + a0. Append "
"'c' to make parameter constant.",
help="Use baseline given by y=a2*x^2+a1*x + a0. Append " "'c' to make parameter constant.",
)
group.add_option(
"--bseq",
dest="bseq",
type="string",
metavar="FILE",
help="Use baseline interpolated from x,y values in FILE. "
"This baseline has no free parameters.",
help="Use baseline interpolated from x,y values in FILE. " "This baseline has no free parameters.",
)
group.add_option(
"--bspherical",
Expand Down Expand Up @@ -343,9 +337,7 @@ def main():
metavar="FILE",
help="Save result of extraction to FILE (.srmise " "format).",
)
group.add_option(
"--plot", "-p", action="store_true", dest="plot", help="Plot extracted peaks."
)
group.add_option("--plot", "-p", action="store_true", dest="plot", help="Plot extracted peaks.")
group.add_option(
"--liveplot",
"-l",
Expand All @@ -362,9 +354,7 @@ def main():
)
parser.add_option_group(group)

group = OptionGroup(
parser, "Verbosity Options", "Control detail printed to console."
)
group = OptionGroup(parser, "Verbosity Options", "Control detail printed to console.")
group.add_option(
"--informative",
"-i",
Expand Down Expand Up @@ -435,9 +425,7 @@ def main():
options.peakfunction = eval("peaks." + options.peakfunction)
except Exception as err:
print(err)
print(
"Could not create peak function '%s'. Exiting." % options.peakfunction
)
print("Could not create peak function '%s'. Exiting." % options.peakfunction)
return

if options.modelevaluator is not None:
Expand All @@ -447,9 +435,7 @@ def main():
options.modelevaluator = eval("modelevaluators." + options.modelevaluator)
except Exception as err:
print(err)
print(
"Could not find ModelEvaluator '%s'. Exiting." % options.modelevaluator
)
print("Could not find ModelEvaluator '%s'. Exiting." % options.modelevaluator)
return

if options.bcrystal is not None:
Expand Down Expand Up @@ -534,9 +520,7 @@ def main():
if options.rng is not None:
pdict["rng"] = list(options.rng)
if options.qmax is not None:
pdict["qmax"] = (
options.qmax if options.qmax == "automatic" else float(options.qmax)
)
pdict["qmax"] = options.qmax if options.qmax == "automatic" else float(options.qmax)
if options.nyquist is not None:
pdict["nyquist"] = options.nyquist
if options.supersample is not None:
Expand Down Expand Up @@ -624,10 +608,7 @@ def _format_text(self, text):
# the above is still the same
bits = text.split("\n")
formatted_bits = [
textwrap.fill(
bit, text_width, initial_indent=indent, subsequent_indent=indent
)
for bit in bits
textwrap.fill(bit, text_width, initial_indent=indent, subsequent_indent=indent) for bit in bits
]
result = "\n".join(formatted_bits) + "\n"
return result
Expand Down Expand Up @@ -665,9 +646,7 @@ def format_option(self, option):
help_lines.extend(textwrap.wrap(para, self.help_width))
# Everything is the same after here
result.append("%*s%s\n" % (indent_first, "", help_lines[0]))
result.extend(
["%*s%s\n" % (self.help_position, "", line) for line in help_lines[1:]]
)
result.extend(["%*s%s\n" % (self.help_position, "", line) for line in help_lines[1:]])
elif opts[-1] != "\n":
result.append("\n")
return "".join(result)
Expand Down
55 changes: 11 additions & 44 deletions diffpy/srmise/applications/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,7 @@ def makeplot(ppe_or_stability, ip=None, **kwds):
x = ppe.x[rangeslice]
y = ppe.y[rangeslice]
dy = ppe.effective_dy[rangeslice]
mcluster = ModelCluster(
ppe.initial_peaks, ppe.baseline, x, y, dy, None, ppe.error_method, ppe.pf
)
mcluster = ModelCluster(ppe.initial_peaks, ppe.baseline, x, y, dy, None, ppe.error_method, ppe.pf)
ext = mcluster
else:
ext = ppe.extracted
Expand Down Expand Up @@ -255,9 +253,7 @@ def makeplot(ppe_or_stability, ip=None, **kwds):
# Define the various data which will be plotted
r = ext.r_cluster
dr = (r[-1] - r[0]) / len(r)
rexpand = np.concatenate(
(np.arange(r[0] - dr, xlo, -dr)[::-1], r, np.arange(r[-1] + dr, xhi + dr, dr))
)
rexpand = np.concatenate((np.arange(r[0] - dr, xlo, -dr)[::-1], r, np.arange(r[-1] + dr, xhi + dr, dr)))
rfine = np.arange(r[0], r[-1], 0.1 * dr)
gr_obs = np.array(resample(ppe.x, ppe.y, rexpand)) * scale
# gr_fit = resample(r, ext.value(), rfine)
Expand Down Expand Up @@ -295,21 +291,10 @@ def makeplot(ppe_or_stability, ip=None, **kwds):
max_res = 0.0

# Derive various y limits based on all the offsets
rel_height = (
100.0
- top_offset
- dg_height
- cmp_height
- datatop_offset
- databottom_offset
- bottom_offset
)
rel_height = 100.0 - top_offset - dg_height - cmp_height - datatop_offset - databottom_offset - bottom_offset
abs_height = 100 * ((max_gr - min_gr) + (max_res - min_res)) / rel_height

yhi = (
max_gr
+ (top_offset + dg_height + cmp_height + datatop_offset) * abs_height / 100
)
yhi = max_gr + (top_offset + dg_height + cmp_height + datatop_offset) * abs_height / 100
ylo = yhi - abs_height

yhi = kwds.get("yhi", yhi)
Expand Down Expand Up @@ -353,13 +338,7 @@ def makeplot(ppe_or_stability, ip=None, **kwds):

# Remove labels above where insets begin
# ax_data.yaxis.set_ticklabels([str(int(loc)) for loc in ax_data.yaxis.get_majorticklocs() if loc < datatop])
ax_data.yaxis.set_ticks(
[
loc
for loc in ax_data.yaxis.get_majorticklocs()
if (loc < datatop and loc >= ylo)
]
)
ax_data.yaxis.set_ticks([loc for loc in ax_data.yaxis.get_majorticklocs() if (loc < datatop and loc >= ylo)])

# Dataset label
if datalabel is not None:
Expand All @@ -378,9 +357,7 @@ def makeplot(ppe_or_stability, ip=None, **kwds):

# Create new x axis at bottom edge of compare inset
ax_data.axis["top"].set_visible(False)
ax_data.axis["newtop"] = ax_data.new_floating_axis(
0, datatop, axis_direction="bottom"
) # "top" bugged?
ax_data.axis["newtop"] = ax_data.new_floating_axis(0, datatop, axis_direction="bottom") # "top" bugged?
ax_data.axis["newtop"].toggle(all=False, ticks=True)
ax_data.axis["newtop"].major_ticks.set_tick_out(True)
ax_data.axis["newtop"].minor_ticks.set_tick_out(True)
Expand All @@ -399,9 +376,7 @@ def makeplot(ppe_or_stability, ip=None, **kwds):
transform=ax_data.transAxes,
)
labeldict[fig] = newylabel # so we can find the correct text object
fig.canvas.mpl_connect(
"draw_event", on_draw
) # original label invisibility and updating
fig.canvas.mpl_connect("draw_event", on_draw) # original label invisibility and updating

# Compare extracted (and ideal, if provided) peak positions clearly.
if cmp_height > 0:
Expand Down Expand Up @@ -610,15 +585,9 @@ def main():
type="int",
help="Plot given model from set. Ignored if srmise_file is not a PeakStability file.",
)
parser.add_option(
"--show", action="store_true", help="execute pylab.show() blocking call"
)
parser.add_option(
"-o", "--output", type="string", help="save plot to the specified file"
)
parser.add_option(
"--format", type="string", default="eps", help="output format for plot saving"
)
parser.add_option("--show", action="store_true", help="execute pylab.show() blocking call")
parser.add_option("-o", "--output", type="string", help="save plot to the specified file")
parser.add_option("--format", type="string", default="eps", help="output format for plot saving")
parser.allow_interspersed_args = True
opts, args = parser.parse_args(sys.argv[1:])

Expand All @@ -636,9 +605,7 @@ def main():
try:
toplot.load(filename)
except Exception:
print(
"File '%s' is not a .srmise or PeakStability data file." % filename
)
print("File '%s' is not a .srmise or PeakStability data file." % filename)
return

if opts.model is not None:
Expand Down
Loading

0 comments on commit fd65cf6

Please sign in to comment.