Skip to content

Commit c053ab7

Browse files
authored
Fix bugs to run tutorial CLI, update python, matplotlib API (#137)
* Add entry point to srmise app * Create entry-point-.rst * Fix abbreviation for image 1 * Evaluate method only after option is true * Fix matplotlib depreciate for inv_transform * Remove unnecessary check for lenght * Read .gr file, convert to utf-8: * Add fix api bug news * Apply pre-commit * Remove print func * Properly compare parameters, using a list
1 parent 2f8ef79 commit c053ab7

File tree

8 files changed

+101
-40
lines changed

8 files changed

+101
-40
lines changed

Diff for: doc/source/tutorial/abbreviations.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
.. |tar.gz| replace:: :source-release:`tar.gz`
1111

1212
.. Image substitutions
13-
.. |images/extract_single_peak1.png| ge:: ../../examples/images/extract_single_peak1.png
13+
.. |images/extract_single_peak1.png| image:: ../../examples/images/extract_single_peak1.png
1414
.. |images/extract_single_peak2.png| image:: ../../examples/images/extract_single_peak2.png
1515
.. |images/extract_single_peak3.png| image:: ../../examples/images/extract_single_peak3.png
1616
.. |images/parameter_summary1.png| image:: ../../examples/images/parameter_summary1.png

Diff for: news/entry-point-.rst

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* <news item>
4+
5+
**Changed:**
6+
7+
* <news item>
8+
9+
**Deprecated:**
10+
11+
* <news item>
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* Configure entry point in pyproject.toml to run CLI commands
20+
21+
**Security:**
22+
23+
* <news item>

Diff for: news/fix-api-bug.rst

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
**Added:**
2+
3+
* <news item>
4+
5+
**Changed:**
6+
7+
* <news item>
8+
9+
**Deprecated:**
10+
11+
* <news item>
12+
13+
**Removed:**
14+
15+
* <news item>
16+
17+
**Fixed:**
18+
19+
* Update Python, matploblib API to run documentation CLI tutorials
20+
21+
**Security:**
22+
23+
* <news item>

Diff for: pyproject.toml

+3
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ template = "{tag}"
4545
dev_template = "{tag}"
4646
dirty_template = "{tag}"
4747

48+
[project.scripts]
49+
srmise = "diffpy.srmise.applications.extract:main"
50+
4851
[tool.setuptools.packages.find]
4952
where = ["src"] # list of folders that contain the packages (["."] by default)
5053
include = ["*"] # package names should match these glob patterns (["*"] by default)

Diff for: src/diffpy/srmise/applications/extract.py

+39-35
Original file line numberDiff line numberDiff line change
@@ -419,82 +419,85 @@ def main():
419419
from diffpy.srmise.pdfpeakextraction import PDFPeakExtraction
420420
from diffpy.srmise.srmiseerrors import SrMiseDataFormatError, SrMiseFileError
421421

422-
try:
423-
options.peakfunction = eval("peaks." + options.peakfunction)
424-
except Exception as err:
425-
print(err)
426-
print("Could not create peak function '%s'. Exiting." % options.peakfunction)
427-
return
428-
429-
try:
430-
options.modelevaluator = eval("modelevaluators." + options.modelevaluator)
431-
except Exception as err:
432-
print(err)
433-
print("Could not find ModelEvaluator '%s'. Exiting." % options.modelevaluator)
434-
return
435-
436-
if options.bcrystal is not None:
422+
if options.peakfunction:
423+
try:
424+
options.peakfunction = eval("peaks." + options.peakfunction)
425+
except Exception as err:
426+
print(err)
427+
print("Could not create peak function '%s'. Exiting." % options.peakfunction)
428+
return
429+
430+
if options.modelevaluator:
431+
try:
432+
options.modelevaluator = eval("modelevaluators." + options.modelevaluator)
433+
except Exception as err:
434+
print(err)
435+
print("Could not find ModelEvaluator '%s'. Exiting." % options.modelevaluator)
436+
return
437+
438+
if options.bcrystal:
437439
from diffpy.srmise.baselines.polynomial import Polynomial
438440

439441
bl = Polynomial(degree=1)
440442
options.baseline = parsepars(bl, [options.bcrystal, "0c"])
441443
options.baseline.pars[0] = -4 * np.pi * options.baseline.pars[0]
442-
elif options.bsrmise is not None:
444+
elif options.bsrmise:
443445
# use baseline from existing file
444446
blext = PDFPeakExtraction()
445447
blext.read(options.bsrmise)
446448
options.baseline = blext.extracted.baseline
447-
elif options.bpoly0 is not None:
449+
elif options.bpoly0:
448450
from diffpy.srmise.baselines.polynomial import Polynomial
449451

450452
bl = Polynomial(degree=0)
451453
options.baseline = parsepars(bl, [options.bpoly0])
452-
elif options.bpoly1 is not None:
454+
elif options.bpoly1:
453455
from diffpy.srmise.baselines.polynomial import Polynomial
454456

455457
bl = Polynomial(degree=1)
456458
options.baseline = parsepars(bl, options.bpoly1)
457-
elif options.bpoly2 is not None:
459+
elif options.bpoly2:
458460
from diffpy.srmise.baselines.polynomial import Polynomial
459461

460462
bl = Polynomial(degree=2)
461463
options.baseline = parsepars(bl, options.bpoly2)
462-
elif options.bseq is not None:
464+
elif options.bseq:
463465
from diffpy.srmise.baselines.fromsequence import FromSequence
464466

465467
bl = FromSequence(options.bseq)
466468
options.baseline = bl.actualize([], "internal")
467-
elif options.bspherical is not None:
469+
elif options.bspherical:
468470
from diffpy.srmise.baselines.nanospherical import NanoSpherical
469471

470472
bl = NanoSpherical()
471473
options.baseline = parsepars(bl, options.bspherical)
472474

473475
try:
474476
options.baseline = eval("baselines." + options.baseline)
477+
475478
except Exception as err:
476479
print(err)
477480
print("Could not create baseline '%s'. Exiting." % options.baseline)
478481
return
479482

480483
filename = args[0]
481484

482-
if filename is not None:
485+
if filename:
483486
ext = PDFPeakExtraction()
484487
try:
485488
ext.read(filename)
486489
except (SrMiseDataFormatError, SrMiseFileError, Exception):
487490
ext.loadpdf(filename)
488491

489492
pdict = {}
490-
if options.peakfunction is not None:
493+
if options.peakfunction:
491494
pdict["pf"] = [options.peakfunction]
492-
if options.baseline is not None:
495+
if options.baseline:
493496
pdict["baseline"] = options.baseline
494-
if options.cres is not None:
497+
if options.cres:
495498
pdict["cres"] = options.cres
496499
if options.dg_mode is None:
497-
if options.dg is not None:
500+
if options.dg:
498501
options.dg_mode = "absolute"
499502
elif ext.dy is None:
500503
options.dg_mode = "max-fraction"
@@ -510,17 +513,17 @@ def main():
510513
pdict["effective_dy"] = options.dg * ext.y.ptp() * np.ones(len(ext.y))
511514
elif options.dg_mode == "dG-fraction":
512515
pdict["effective_dy"] = options.dg * ext.dy
513-
if options.rng is not None:
516+
if options.rng:
514517
pdict["rng"] = list(options.rng)
515-
if options.qmax is not None:
518+
if options.qmax:
516519
pdict["qmax"] = options.qmax if options.qmax == "automatic" else float(options.qmax)
517-
if options.nyquist is not None:
520+
if options.nyquist:
518521
pdict["nyquist"] = options.nyquist
519-
if options.supersample is not None:
522+
if options.supersample:
520523
pdict["supersample"] = options.supersample
521-
if options.scale is not None:
524+
if options.scale:
522525
pdict["scale"] = options.scale
523-
if options.modelevaluator is not None:
526+
if options.modelevaluator:
524527
pdict["error_method"] = options.modelevaluator
525528

526529
if options.liveplot:
@@ -532,23 +535,24 @@ def main():
532535
cov = None
533536
if options.performextraction:
534537
cov = ext.extract()
538+
print(cov)
535539

536-
if options.savefile is not None:
540+
if options.savefile:
537541
try:
538542
ext.write(options.savefile)
539543
except SrMiseFileError as err:
540544
print(err)
541545
print("Could not save result to '%s'." % options.savefile)
542546

543-
if options.pwafile is not None:
547+
if options.pwafile:
544548
try:
545549
ext.writepwa(options.pwafile)
546550
except SrMiseFileError as err:
547551
print(err)
548552
print("Could not save pwa summary to '%s'." % options.pwafile)
549553

550554
print(ext)
551-
if cov is not None:
555+
if cov:
552556
print(cov)
553557

554558
if options.plot:

Diff for: src/diffpy/srmise/applications/plot.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -504,7 +504,7 @@ def on_draw(event):
504504
invisiblelabel.set_visible(True)
505505
visiblelabel = labeldict[fig]
506506
bbox = invisiblelabel.get_window_extent(invisiblelabel._renderer)
507-
bbox = bbox.inverse_transformed(ax_main.transAxes)
507+
bbox = bbox.transformed(ax_main.transAxes.inverted())
508508
bbox = bbox.get_points()
509509
xpos = np.mean(np.transpose(bbox)[0])
510510

Diff for: src/diffpy/srmise/basefunction.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ def __init__(
100100
The class (not instance) which implements caching of BaseFunction
101101
evaluations.
102102
"""
103+
103104
self.parameterdict = parameterdict
104105
self.npars = len(self.parameterdict)
105106

@@ -114,9 +115,13 @@ def __init__(
114115
if not isinstance(p, str):
115116
emsg = "Argument parameterdict's keys must be strings."
116117
raise ValueError(emsg)
117-
vals = self.parameterdict.values()
118+
119+
# Convert values to list and sort
120+
vals = list(self.parameterdict.values())
118121
vals.sort()
119-
if vals != range(self.npars):
122+
123+
# Check if the sorted values match the sequence from 0 to npars-1
124+
if vals != list(range(self.npars)):
120125
emsg = (
121126
"Argument parameterdict's values must uniquely specify "
122127
+ "the index of each parameter defined by its keys."

Diff for: src/diffpy/srmise/pdfdataset.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,10 @@ def read(self, filename):
202202
self
203203
"""
204204
try:
205-
self.readStr(open(filename, "rb").read())
205+
# Open the file in binary mode, read it, and decode to convert bytes to string
206+
with open(filename, "rb") as file:
207+
file_content = file.read().decode("utf-8")
208+
self.readStr(file_content)
206209
except PDFDataFormatError as err:
207210
basename = os.path.basename(filename)
208211
emsg = ("Could not open '%s' due to unsupported file format " + "or corrupted data. [%s]") % (

0 commit comments

Comments
 (0)