Skip to content

fix: fix load single peak in py3 support#178

Merged
sbillinge merged 12 commits intodiffpy:mainfrom
stevenhua0320:fix-single-peak
Apr 9, 2026
Merged

fix: fix load single peak in py3 support#178
sbillinge merged 12 commits intodiffpy:mainfrom
stevenhua0320:fix-single-peak

Conversation

@stevenhua0320
Copy link
Copy Markdown
Contributor

@stevenhua0320 stevenhua0320 commented Apr 8, 2026

@sbillinge ready to review. Note that there are a lot of py2 legacy in this package. (My bad, I did not clean it up comprehensively on 2024). Now it would give a plot when I run the command srmise docs/examples/data/Ag-nyquist-qmax30.gr --plot --range 2 3.5 --baseline "Polynomial(degree=1)" it would give me the plot below.
Also there are some messages for the fitting process:

The input PDF appears to be missing information: The sampling interval of the input PDF (0.10471976036036036) is larger than the Nyquist interval (0.10471975511965977) defined by qmax=30.0.  This information is irretrievable.
2.90030e+00 (2.30611e-03)  2.62623e-01 (5.94556e-03)  9.64654e+00 (2.24194e-01)
-3.87207e-01 (3.80378e-01)  -2.92455e-01 (1.41472e-01)
effective_dy: [0.2765272 0.3758883 0.3993384 0.4062425 0.4092704 0.4108721 0.4118211
 0.4124392 0.4128434 0.4131494 0.4133937 0.4135703 0.41365   0.4140212
 0.4138731 0.4139608 0.4140158 0.4140681 0.4140688 0.4141887 0.4141705
 0.4142069 0.4142    0.4142505 0.4142869 0.414276  0.414235  0.4143683
 0.4143157 0.4142939 0.4143335 0.4143717 0.4143361 0.4143605 0.4143777
 0.414216  0.4145213 0.4143604 0.4143672 0.414408  0.4143034 0.4145074
 0.4143658 0.4144454 0.4143957 0.4144191 0.41442   0.4144247 0.4143801
 0.4144333 0.4144703 0.4144277 0.414347  0.4144955 0.4144083 0.41444
 0.4143993 0.4144537 0.4144685 0.4143536 0.4145365 0.4144362 0.4143902
 0.4144907 0.4144324 0.4143906 0.4144803 0.4144227 0.4143587 0.414509
 0.4144201 0.4144356 0.4144505 0.4144687 0.414469  0.4144434 0.4144111
 0.4145219 0.4143796 0.4144955 0.4144445 0.414446  0.4144502 0.4143988
 0.4145152 0.4144222 0.4144654 0.4144388 0.4144548 0.4144297 0.4144823
 0.4144444 0.4144652 0.4144644 0.4144417 0.4144549 0.4144527 0.4144868
 0.4144561 0.4144608 0.4144841 0.4144248 0.4144966 0.4144239 0.4144633
 0.4144556 0.4144629 0.4144406 0.4144775 0.4144525 0.4144847 0.4144663
 0.4144317 0.4145045 0.4144493 0.414473  0.4144754 0.4144237 0.4144983
 0.4144333 0.414449  0.4144681 0.4144388 0.4144618 0.4144581 0.4144738
 0.4144686 0.4144742 0.4144435 0.414468  0.4144499 0.4144547 0.4144545
 0.4144739 0.4144597 0.4144296 0.4144873 0.4144529 0.4144721 0.4144343
 0.4144819 0.4144488 0.4144636 0.4144588 0.4144802 0.4144567 0.4144705
 0.4144523 0.4144734 0.4144571 0.4144744 0.4144514 0.4144552 0.414459
 0.4144734 0.414468  0.41448   0.4144769 0.4144631 0.4144793 0.414472
 0.4144708 0.4144635 0.4144717 0.4144609 0.4144541 0.4144635 0.4144642
 0.4144741 0.414465  0.4144646 0.4144802 0.4144664 0.4144843 0.4144721
 0.4144568 0.4144656 0.4144651 0.4144719 0.4144805 0.4144722 0.4144764
 0.4144649 0.4144721 0.4144765 0.4144656 0.4144695 0.4144606 0.4144579
 0.4144769 0.4144748 0.4144622 0.4144701 0.4144719 0.4144673 0.4144699
 0.4144735 0.4144827 0.4144778 0.4144763 0.4144695 0.4144653 0.4144726
 0.4144633 0.4144626 0.4144595 0.4144623 0.4144694 0.4144665 0.4144765
 0.4144772 0.4144741 0.4144802 0.4144756 0.4144667 0.4144744 0.414475
 0.4144755 0.4144746 0.4144769 0.4144813 0.4144749 0.4144805 0.4144797
 0.4144777 0.4144789 0.4144813 0.4144795 0.4144831 0.4144789 0.4144732
 0.414472  0.4144689 0.4144738 0.4144672 0.4144618 0.4144653 0.4144676
 0.4144723 0.4144764 0.4144796 0.4144753 0.4144743 0.4144714 0.4144631
 0.4144645 0.4144619 0.4144606 0.4144628 0.4144652 0.4144678 0.4144644
 0.4144702 0.4144717 0.4144741 0.4144759 0.4144742 0.414472  0.414475
 0.414476  0.4144714 0.4144684 0.4144675 0.4144691 0.4144731 0.4144762
 0.4144751 0.4144731 0.4144694 0.4144698 0.4144737 0.4144747 0.4144762
 0.414476  0.4144759 0.4144753 0.4144751 0.4144704 0.4144712 0.4144691
 0.4144694 0.4144679 0.4144692 0.4144697 0.4144709 0.4144724 0.4144726
 0.4144725 0.4144727 0.4144744 0.4144709 0.41447   0.4144686 0.4144733
 0.4144777 0.414476  0.4144723 0.414469  0.414467  0.4144685 0.414471
 0.4144731 0.4144743 0.4144733 0.4144693 0.4144691 0.4144692 0.4144668
 0.4144673 0.414468  0.4144688 0.4144693 0.4144716 0.414471  0.4144728
 0.4144734 0.4144724 0.4144769 0.4144768 0.4144725 0.4144718 0.4144688
 0.4144638 0.4144631 0.4144619 0.4144626 0.4144662 0.4144635 0.4144605
 0.4144651 0.4144694 0.414472  0.4144754 0.4144737]
rng: [2.0, 3.5]
pf: [<diffpy.srmise.peaks.gaussianoverr.GaussianOverR object at 0x109d7e900>]
initial_peaks: 
baseline: [-0.60176135  0.        ]
cres: 0.10471975511965977
error_method: <class 'diffpy.srmise.modelevaluators.aic.AIC'>
qmax: 30.0
supersample: 4.0
nyquist: True
scale: False
Extraction type: extract
--- Extracted ---
Slice: slice(None, 15, None)
Quality: 573.557582474722
Baseline: [-0.60176135  0.        ]
Peaks:
[ 2.89549619  0.54157962 18.57789434]


2.90030e+00 (2.30611e-03)  2.62623e-01 (5.94556e-03)  9.64654e+00 (2.24194e-01)
-3.87207e-01 (3.80378e-01)  -2.92455e-01 (1.41472e-01)
Screenshot 2026-04-07 at 10 14 52 PM Closes #5 #147

@codecov
Copy link
Copy Markdown

codecov bot commented Apr 8, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 76.47%. Comparing base (d9e435d) to head (36e6733).
⚠️ Report is 3 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #178   +/-   ##
=======================================
  Coverage   76.47%   76.47%           
=======================================
  Files           3        3           
  Lines          51       51           
=======================================
  Hits           39       39           
  Misses         12       12           
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@stevenhua0320
Copy link
Copy Markdown
Contributor Author

@sbillinge I believe we have no tests to cover the issue here (since the tests only contain the easiest one written on 2024 summer by you and me), so it needs some human wisdom to decide if it is correct or not.

@sbillinge
Copy link
Copy Markdown
Contributor

yah, something seems rather wrong here. The fit is not working. It should end up looking like the fit in @bobleesj from the other issue.

@sbillinge
Copy link
Copy Markdown
Contributor

@stevenhua0320 let me know when you figure this out and want a review.

@stevenhua0320
Copy link
Copy Markdown
Contributor Author

stevenhua0320 commented Apr 9, 2026

@sbillinge ready to review. Finally, I figured out that it is because of the regularization of aic aicc in base is wrong, in the skpkg step in 2024 we changed the logic from checking true or false to if it is None. Therefore, we run into the issue. Now, with other py2 legacy cleanup, we managed to reproduce the correct result and the plot.

(diffpy.srmise_env) huarundong@Stevens-MacBook-Pro-405 diffpy.srmise % srmise docs/examples/data/Ag-nyquist-qmax30.gr --plot --range 2 3.5 --baseline "Polynomial(degree=1)"                                                                       
The input PDF appears to be missing information: The sampling interval of the input PDF (0.10471976036036036) is larger than the Nyquist interval (0.10471975511965977) defined by qmax=30.0.  This information is irretrievable.
2.90075e+00 (1.90429e-03)  2.67213e-01 (4.91303e-03)  9.84389e+00 (1.86615e-01)
-5.10705e-01 (3.12242e-01)  9.01918e-03 (1.16242e-01)
effective_dy: [0.2765272 0.3758883 0.3993384 0.4062425 0.4092704 0.4108721 0.4118211
 0.4124392 0.4128434 0.4131494 0.4133937 0.4135703 0.41365   0.4140212
 0.4138731 0.4139608 0.4140158 0.4140681 0.4140688 0.4141887 0.4141705
 0.4142069 0.4142    0.4142505 0.4142869 0.414276  0.414235  0.4143683
 0.4143157 0.4142939 0.4143335 0.4143717 0.4143361 0.4143605 0.4143777
 0.414216  0.4145213 0.4143604 0.4143672 0.414408  0.4143034 0.4145074
 0.4143658 0.4144454 0.4143957 0.4144191 0.41442   0.4144247 0.4143801
 0.4144333 0.4144703 0.4144277 0.414347  0.4144955 0.4144083 0.41444
 0.4143993 0.4144537 0.4144685 0.4143536 0.4145365 0.4144362 0.4143902
 0.4144907 0.4144324 0.4143906 0.4144803 0.4144227 0.4143587 0.414509
 0.4144201 0.4144356 0.4144505 0.4144687 0.414469  0.4144434 0.4144111
 0.4145219 0.4143796 0.4144955 0.4144445 0.414446  0.4144502 0.4143988
 0.4145152 0.4144222 0.4144654 0.4144388 0.4144548 0.4144297 0.4144823
 0.4144444 0.4144652 0.4144644 0.4144417 0.4144549 0.4144527 0.4144868
 0.4144561 0.4144608 0.4144841 0.4144248 0.4144966 0.4144239 0.4144633
 0.4144556 0.4144629 0.4144406 0.4144775 0.4144525 0.4144847 0.4144663
 0.4144317 0.4145045 0.4144493 0.414473  0.4144754 0.4144237 0.4144983
 0.4144333 0.414449  0.4144681 0.4144388 0.4144618 0.4144581 0.4144738
 0.4144686 0.4144742 0.4144435 0.414468  0.4144499 0.4144547 0.4144545
 0.4144739 0.4144597 0.4144296 0.4144873 0.4144529 0.4144721 0.4144343
 0.4144819 0.4144488 0.4144636 0.4144588 0.4144802 0.4144567 0.4144705
 0.4144523 0.4144734 0.4144571 0.4144744 0.4144514 0.4144552 0.414459
 0.4144734 0.414468  0.41448   0.4144769 0.4144631 0.4144793 0.414472
 0.4144708 0.4144635 0.4144717 0.4144609 0.4144541 0.4144635 0.4144642
 0.4144741 0.414465  0.4144646 0.4144802 0.4144664 0.4144843 0.4144721
 0.4144568 0.4144656 0.4144651 0.4144719 0.4144805 0.4144722 0.4144764
 0.4144649 0.4144721 0.4144765 0.4144656 0.4144695 0.4144606 0.4144579
 0.4144769 0.4144748 0.4144622 0.4144701 0.4144719 0.4144673 0.4144699
 0.4144735 0.4144827 0.4144778 0.4144763 0.4144695 0.4144653 0.4144726
 0.4144633 0.4144626 0.4144595 0.4144623 0.4144694 0.4144665 0.4144765
 0.4144772 0.4144741 0.4144802 0.4144756 0.4144667 0.4144744 0.414475
 0.4144755 0.4144746 0.4144769 0.4144813 0.4144749 0.4144805 0.4144797
 0.4144777 0.4144789 0.4144813 0.4144795 0.4144831 0.4144789 0.4144732
 0.414472  0.4144689 0.4144738 0.4144672 0.4144618 0.4144653 0.4144676
 0.4144723 0.4144764 0.4144796 0.4144753 0.4144743 0.4144714 0.4144631
 0.4144645 0.4144619 0.4144606 0.4144628 0.4144652 0.4144678 0.4144644
 0.4144702 0.4144717 0.4144741 0.4144759 0.4144742 0.414472  0.414475
 0.414476  0.4144714 0.4144684 0.4144675 0.4144691 0.4144731 0.4144762
 0.4144751 0.4144731 0.4144694 0.4144698 0.4144737 0.4144747 0.4144762
 0.414476  0.4144759 0.4144753 0.4144751 0.4144704 0.4144712 0.4144691
 0.4144694 0.4144679 0.4144692 0.4144697 0.4144709 0.4144724 0.4144726
 0.4144725 0.4144727 0.4144744 0.4144709 0.41447   0.4144686 0.4144733
 0.4144777 0.414476  0.4144723 0.414469  0.414467  0.4144685 0.414471
 0.4144731 0.4144743 0.4144733 0.4144693 0.4144691 0.4144692 0.4144668
 0.4144673 0.414468  0.4144688 0.4144693 0.4144716 0.414471  0.4144728
 0.4144734 0.4144724 0.4144769 0.4144768 0.4144725 0.4144718 0.4144688
 0.4144638 0.4144631 0.4144619 0.4144626 0.4144662 0.4144635 0.4144605
 0.4144651 0.4144694 0.414472  0.4144754 0.4144737]
rng: [2.0, 3.5]
pf: [<diffpy.srmise.peaks.gaussianoverr.GaussianOverR object at 0x1082bacf0>]
initial_peaks: 
baseline: [-0.60176135  0.        ]
cres: 0.10471975511965977
error_method: <class 'diffpy.srmise.modelevaluators.aic.AIC'>
qmax: 30.0
supersample: 4.0
nyquist: True
scale: False
Extraction type: extract
--- Extracted ---
Slice: slice(None, 15, None)
Quality: 8.142532170140099
Baseline: [-0.5107055   0.00901918]
Peaks:
[2.90074808 0.26721295 9.84388525]


2.90075e+00 (1.90429e-03)  2.67213e-01 (4.91303e-03)  9.84389e+00 (1.86615e-01)
-5.10705e-01 (3.12242e-01)  9.01918e-03 (1.16242e-01)
Screenshot 2026-04-09 at 12 17 32 AM

self.chisq = self.chi_squared(fit.value(), fit.y_cluster, fit.error_cluster)

self.stat = self.chisq + self.parpenalty(k, n)
self.stat = self.chisq + self.parpenalty(k)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed, otherwise, it would have a unneeded positional argument, Moreover, since we are doing AIC, we don't need n here by the AIC formula.

self.x[idx] - self.x[self.clusters[int(near_idx) - 1, 1]],
self.x[idx] - self.x[self.clusters[int(near_idx), 0]],
self.x[idx] - self.x[self.clusters[near_idx - 1, 1]],
self.x[idx] - self.x[self.clusters[near_idx, 0]],
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is to revert the incorrect fix done on summer 2024.

# Enable "dg" as alias for "effective_dy"
if "dg" in args and "effective_dy" not in args:
nargs.add("effective_dy")
nargs.append("effective_dy")
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now, we have nargs as a list, so we need append method here

new_dr = (new_r[-1] - r[0]) / (len(new_r) - 1)

yfft = np.imag(np.fft.fft(new_y))[: len(new_y) / 2]
yfft = np.imag(np.fft.fft(new_y))[: len(new_y) // 2]
Copy link
Copy Markdown
Contributor Author

@stevenhua0320 stevenhua0320 Apr 9, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In py2 it doesn't matter but in py3, since the slicing number must be an integer, so it must be // to get an integer value.

Copy link
Copy Markdown
Contributor

@sbillinge sbillinge left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh wow. Super! Great job!

@sbillinge sbillinge merged commit 2a30b00 into diffpy:main Apr 9, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot extract single peak

2 participants