1+ import warnings
2+ from types import SimpleNamespace
3+
4+ from numpy import allclose , arange , array , copy , isfinite , isnan
5+
6+ from corems .mass_spectrum .calc .PeakPicking import PeakPicking
7+
18# Tests for adpodization methods
29def test_hamming (bruker_transient ):
310 """Test the creation of a mass spectrum object with the Hamming apodization method"""
@@ -103,4 +110,98 @@ def test_mass_spectrum_properties(mass_spectrum_ftms):
103110 res = mass_spectrum_ftms .get_mz_and_abundance_peaks_tuples ()
104111 assert len (res ) == len (mass_spectrum_ftms )
105112 mass_spectrum_ftms .get_masses_count_by_nominal_mass ()
106- mass_spectrum_ftms .resolving_power_calc (12 , 1 )
113+ mass_spectrum_ftms .resolving_power_calc (12 , 1 )
114+
115+
116+ def _resolving_power_picker ():
117+ picker = PeakPicking ()
118+ picker .mspeaks_settings = SimpleNamespace (
119+ legacy_resolving_power = True ,
120+ legacy_centroid_polyfit = False ,
121+ )
122+ return picker
123+
124+
125+ def _spectrum_edge_warnings (caught ):
126+ needles = (
127+ "peak at low spectrum edge" ,
128+ "peak at high spectrum edge" ,
129+ "Zeroing the first 5 data points" ,
130+ "Zeroing the last 5 data points" ,
131+ "peak index minus adjacent to spectrum edge" ,
132+ "peak index plus adjacent to spectrum edge" ,
133+ )
134+ return [
135+ str (w .message )
136+ for w in caught
137+ if any (n in str (w .message ) for n in needles )
138+ ]
139+
140+
141+ def test_resolving_power_when_fwhm_is_measurable ():
142+ """Interior and padded-edge peaks get finite RP; abundance is not mutated."""
143+ picker = _resolving_power_picker ()
144+ cases = (
145+ (
146+ array ([100.0 , 100.1 , 100.2 , 100.3 , 100.4 ]),
147+ array ([0.0 , 50.0 , 100.0 , 50.0 , 0.0 ]),
148+ 2 ,
149+ 334.0 ,
150+ ),
151+ (
152+ 100.0 + 0.1 * arange (10 ),
153+ array ([0.0 , 0.0 , 0.0 , 100.0 , 50.0 , 0.0 , 10.0 , 10.0 , 10.0 , 10.0 ]),
154+ 3 ,
155+ None ,
156+ ),
157+ (
158+ 100.0 + 0.1 * arange (10 ),
159+ array ([10.0 , 10.0 , 10.0 , 10.0 , 0.0 , 50.0 , 100.0 , 50.0 , 0.0 , 0.0 ]),
160+ 6 ,
161+ None ,
162+ ),
163+ )
164+ with warnings .catch_warnings (record = True ) as caught :
165+ warnings .simplefilter ("always" )
166+ for massa , intes , apex , expected in cases :
167+ original = copy (intes )
168+ rp = picker .calculate_resolving_power (intes , massa , apex )
169+ assert isfinite (rp )
170+ assert rp > 0
171+ if expected is not None :
172+ assert allclose (rp , expected )
173+ assert allclose (intes , original )
174+ assert _spectrum_edge_warnings (caught ) == []
175+
176+
177+ def test_resolving_power_nan_when_fwhm_truncated ():
178+ """Missing half-max on either side yields silent nan and does not zero abundance."""
179+ picker = _resolving_power_picker ()
180+ cases = (
181+ (
182+ 100.0 + 0.1 * arange (8 ),
183+ array ([100.0 , 50.0 , 0.0 , 10.0 , 10.0 , 10.0 , 10.0 , 10.0 ]),
184+ 0 ,
185+ ),
186+ (
187+ 100.0 + 0.1 * arange (8 ),
188+ array ([10.0 , 10.0 , 10.0 , 10.0 , 10.0 , 0.0 , 50.0 , 100.0 ]),
189+ 7 ,
190+ ),
191+ (
192+ 100.0 + 0.1 * arange (12 ),
193+ array (
194+ [80.0 , 80.0 , 80.0 , 80.0 , 80.0 , 80.0 , 100.0 , 50.0 , 0.0 , 10.0 , 10.0 , 10.0 ]
195+ ),
196+ 6 ,
197+ ),
198+ )
199+ with warnings .catch_warnings (record = True ) as caught :
200+ warnings .simplefilter ("always" )
201+ for massa , intes , apex in cases :
202+ original = copy (intes )
203+ rp = picker .calculate_resolving_power (intes , massa , apex )
204+ assert isnan (rp )
205+ assert allclose (intes , original )
206+ assert _spectrum_edge_warnings (caught ) == []
207+
0 commit comments