Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions corems/mass_spectrum/calc/NoiseCalc.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,20 +120,20 @@ def cut_mz_domain_noise(self):

if max_mz_noise > max_mz_whole_ms:
max_mz_noise = max_mz_whole_ms

#print(min_mz_noise, max_mz_noise)
low_mz_index = (where(self.mz_exp_profile >= min_mz_noise)[0][0])
#print(self.mz_exp_profile[low_mz_index])
# low_mz_index = (argmax(self.mz_exp_profile <= min_mz_noise))

high_mz_index = (where(self.mz_exp_profile <= max_mz_noise)[-1][-1])

#the following indexing relies on mz_exp_profile being ordered high mz to low mz
low_mz_index = (where(self.mz_exp_profile >= min_mz_noise)[-1][-1])
#print(self.mz_exp_profile[low_mz_index])
#low_mz_index = (argmax(self.mz_exp_profile <= min_mz_noise))

high_mz_index = (where(self.mz_exp_profile <= max_mz_noise)[0][0])
#print(self.mz_exp_profile[high_mz_index])
Comment on lines +124 to +130
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

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

The new where(...)[-1][-1] / [0][0] selection assumes mz_exp_profile is sorted high→low. If mz_exp_profile is low→high (which the if high_mz_index > low_mz_index branch suggests is possible, and can happen for array-based inputs), low_mz_index becomes the last element and the slice includes values outside [min_mz_noise, max_mz_noise], so min/max filtering breaks again. Consider making the index selection order-independent, e.g., build a boolean mask for (mz>=min)&(mz<=max) and slice using min(idx):max(idx)+1, which also lets you drop the order-dependent comment and the if/else.

Copilot uses AI. Check for mistakes.
#high_mz_index = (argmax(self.mz_exp_profile <= max_mz_noise))

if high_mz_index > low_mz_index:
# pyplot.plot(self.mz_exp_profile[low_mz_index:high_mz_index], self.abundance_profile[low_mz_index:high_mz_index])
# pyplot.show()
return self.mz_exp_profile[high_mz_index:low_mz_index], self.abundance_profile[low_mz_index:high_mz_index]
return self.mz_exp_profile[low_mz_index:high_mz_index], self.abundance_profile[low_mz_index:high_mz_index]
Comment on lines 133 to +136
Copy link

Copilot AI Mar 9, 2026

Choose a reason for hiding this comment

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

This change affects how noise ROI is selected for all non-'minima' threshold methods, but there doesn’t appear to be a unit test that asserts min_noise_mz/max_noise_mz actually change the computed noise baseline/STD (especially for both ascending and descending mz_exp_profile). Adding a focused test around cut_mz_domain_noise() (or run_noise_threshold_calc() with threshold_method='signal_noise') would prevent regressions like the original bug and the ordering edge case.

Copilot uses AI. Check for mistakes.
else:
# pyplot.plot(self.mz_exp_profile[high_mz_index:low_mz_index], self.abundance_profile[high_mz_index:low_mz_index])
# pyplot.show()
Expand Down