diff --git a/doc/source/tutorial/abbreviations.txt b/doc/source/tutorial/abbreviations.txt index fbbeb6c..032313e 100644 --- a/doc/source/tutorial/abbreviations.txt +++ b/doc/source/tutorial/abbreviations.txt @@ -10,7 +10,7 @@ .. |tar.gz| replace:: :source-release:`tar.gz` .. Image substitutions -.. |images/extract_single_peak1.png| ge:: ../../examples/images/extract_single_peak1.png +.. |images/extract_single_peak1.png| image:: ../../examples/images/extract_single_peak1.png .. |images/extract_single_peak2.png| image:: ../../examples/images/extract_single_peak2.png .. |images/extract_single_peak3.png| image:: ../../examples/images/extract_single_peak3.png .. |images/parameter_summary1.png| image:: ../../examples/images/parameter_summary1.png diff --git a/news/entry-point-.rst b/news/entry-point-.rst new file mode 100644 index 0000000..698b5e4 --- /dev/null +++ b/news/entry-point-.rst @@ -0,0 +1,23 @@ +**Added:** + +* + +**Changed:** + +* + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* Configure entry point in pyproject.toml to run CLI commands + +**Security:** + +* diff --git a/news/fix-api-bug.rst b/news/fix-api-bug.rst new file mode 100644 index 0000000..4387e9a --- /dev/null +++ b/news/fix-api-bug.rst @@ -0,0 +1,23 @@ +**Added:** + +* + +**Changed:** + +* + +**Deprecated:** + +* + +**Removed:** + +* + +**Fixed:** + +* Update Python, matploblib API to run documentation CLI tutorials + +**Security:** + +* diff --git a/pyproject.toml b/pyproject.toml index 2044137..abb955f 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -45,6 +45,9 @@ template = "{tag}" dev_template = "{tag}" dirty_template = "{tag}" +[project.scripts] +srmise = "diffpy.srmise.applications.extract:main" + [tool.setuptools.packages.find] where = ["src"] # list of folders that contain the packages (["."] by default) include = ["*"] # package names should match these glob patterns (["*"] by default) diff --git a/src/diffpy/srmise/applications/extract.py b/src/diffpy/srmise/applications/extract.py index 9d08e9f..99157ff 100755 --- a/src/diffpy/srmise/applications/extract.py +++ b/src/diffpy/srmise/applications/extract.py @@ -419,52 +419,54 @@ def main(): from diffpy.srmise.pdfpeakextraction import PDFPeakExtraction from diffpy.srmise.srmiseerrors import SrMiseDataFormatError, SrMiseFileError - try: - options.peakfunction = eval("peaks." + options.peakfunction) - except Exception as err: - print(err) - print("Could not create peak function '%s'. Exiting." % options.peakfunction) - return - - try: - options.modelevaluator = eval("modelevaluators." + options.modelevaluator) - except Exception as err: - print(err) - print("Could not find ModelEvaluator '%s'. Exiting." % options.modelevaluator) - return - - if options.bcrystal is not None: + if options.peakfunction: + try: + options.peakfunction = eval("peaks." + options.peakfunction) + except Exception as err: + print(err) + print("Could not create peak function '%s'. Exiting." % options.peakfunction) + return + + if options.modelevaluator: + try: + options.modelevaluator = eval("modelevaluators." + options.modelevaluator) + except Exception as err: + print(err) + print("Could not find ModelEvaluator '%s'. Exiting." % options.modelevaluator) + return + + if options.bcrystal: from diffpy.srmise.baselines.polynomial import Polynomial bl = Polynomial(degree=1) options.baseline = parsepars(bl, [options.bcrystal, "0c"]) options.baseline.pars[0] = -4 * np.pi * options.baseline.pars[0] - elif options.bsrmise is not None: + elif options.bsrmise: # use baseline from existing file blext = PDFPeakExtraction() blext.read(options.bsrmise) options.baseline = blext.extracted.baseline - elif options.bpoly0 is not None: + elif options.bpoly0: from diffpy.srmise.baselines.polynomial import Polynomial bl = Polynomial(degree=0) options.baseline = parsepars(bl, [options.bpoly0]) - elif options.bpoly1 is not None: + elif options.bpoly1: from diffpy.srmise.baselines.polynomial import Polynomial bl = Polynomial(degree=1) options.baseline = parsepars(bl, options.bpoly1) - elif options.bpoly2 is not None: + elif options.bpoly2: from diffpy.srmise.baselines.polynomial import Polynomial bl = Polynomial(degree=2) options.baseline = parsepars(bl, options.bpoly2) - elif options.bseq is not None: + elif options.bseq: from diffpy.srmise.baselines.fromsequence import FromSequence bl = FromSequence(options.bseq) options.baseline = bl.actualize([], "internal") - elif options.bspherical is not None: + elif options.bspherical: from diffpy.srmise.baselines.nanospherical import NanoSpherical bl = NanoSpherical() @@ -472,6 +474,7 @@ def main(): try: options.baseline = eval("baselines." + options.baseline) + except Exception as err: print(err) print("Could not create baseline '%s'. Exiting." % options.baseline) @@ -479,7 +482,7 @@ def main(): filename = args[0] - if filename is not None: + if filename: ext = PDFPeakExtraction() try: ext.read(filename) @@ -487,14 +490,14 @@ def main(): ext.loadpdf(filename) pdict = {} - if options.peakfunction is not None: + if options.peakfunction: pdict["pf"] = [options.peakfunction] - if options.baseline is not None: + if options.baseline: pdict["baseline"] = options.baseline - if options.cres is not None: + if options.cres: pdict["cres"] = options.cres if options.dg_mode is None: - if options.dg is not None: + if options.dg: options.dg_mode = "absolute" elif ext.dy is None: options.dg_mode = "max-fraction" @@ -510,17 +513,17 @@ def main(): pdict["effective_dy"] = options.dg * ext.y.ptp() * np.ones(len(ext.y)) elif options.dg_mode == "dG-fraction": pdict["effective_dy"] = options.dg * ext.dy - if options.rng is not None: + if options.rng: pdict["rng"] = list(options.rng) - if options.qmax is not None: + if options.qmax: pdict["qmax"] = options.qmax if options.qmax == "automatic" else float(options.qmax) - if options.nyquist is not None: + if options.nyquist: pdict["nyquist"] = options.nyquist - if options.supersample is not None: + if options.supersample: pdict["supersample"] = options.supersample - if options.scale is not None: + if options.scale: pdict["scale"] = options.scale - if options.modelevaluator is not None: + if options.modelevaluator: pdict["error_method"] = options.modelevaluator if options.liveplot: @@ -532,15 +535,16 @@ def main(): cov = None if options.performextraction: cov = ext.extract() + print(cov) - if options.savefile is not None: + if options.savefile: try: ext.write(options.savefile) except SrMiseFileError as err: print(err) print("Could not save result to '%s'." % options.savefile) - if options.pwafile is not None: + if options.pwafile: try: ext.writepwa(options.pwafile) except SrMiseFileError as err: @@ -548,7 +552,7 @@ def main(): print("Could not save pwa summary to '%s'." % options.pwafile) print(ext) - if cov is not None: + if cov: print(cov) if options.plot: diff --git a/src/diffpy/srmise/applications/plot.py b/src/diffpy/srmise/applications/plot.py index 2663158..0399565 100755 --- a/src/diffpy/srmise/applications/plot.py +++ b/src/diffpy/srmise/applications/plot.py @@ -504,7 +504,7 @@ def on_draw(event): invisiblelabel.set_visible(True) visiblelabel = labeldict[fig] bbox = invisiblelabel.get_window_extent(invisiblelabel._renderer) - bbox = bbox.inverse_transformed(ax_main.transAxes) + bbox = bbox.transformed(ax_main.transAxes.inverted()) bbox = bbox.get_points() xpos = np.mean(np.transpose(bbox)[0]) diff --git a/src/diffpy/srmise/basefunction.py b/src/diffpy/srmise/basefunction.py index 1711138..7a955f0 100644 --- a/src/diffpy/srmise/basefunction.py +++ b/src/diffpy/srmise/basefunction.py @@ -100,6 +100,7 @@ def __init__( The class (not instance) which implements caching of BaseFunction evaluations. """ + self.parameterdict = parameterdict self.npars = len(self.parameterdict) @@ -114,9 +115,13 @@ def __init__( if not isinstance(p, str): emsg = "Argument parameterdict's keys must be strings." raise ValueError(emsg) - vals = self.parameterdict.values() + + # Convert values to list and sort + vals = list(self.parameterdict.values()) vals.sort() - if vals != range(self.npars): + + # Check if the sorted values match the sequence from 0 to npars-1 + if vals != list(range(self.npars)): emsg = ( "Argument parameterdict's values must uniquely specify " + "the index of each parameter defined by its keys." diff --git a/src/diffpy/srmise/pdfdataset.py b/src/diffpy/srmise/pdfdataset.py index 49fd168..eeb1280 100644 --- a/src/diffpy/srmise/pdfdataset.py +++ b/src/diffpy/srmise/pdfdataset.py @@ -202,7 +202,10 @@ def read(self, filename): self """ try: - self.readStr(open(filename, "rb").read()) + # Open the file in binary mode, read it, and decode to convert bytes to string + with open(filename, "rb") as file: + file_content = file.read().decode("utf-8") + self.readStr(file_content) except PDFDataFormatError as err: basename = os.path.basename(filename) emsg = ("Could not open '%s' due to unsupported file format " + "or corrupted data. [%s]") % (