Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PowerSpectrum: improve diagnostic value power spectrum api #695

Merged
merged 3 commits into from
Dec 9, 2024

Conversation

JoepVanlier
Copy link
Member

@JoepVanlier JoepVanlier commented Sep 27, 2024

Why this PR?

  • A simpler constructor will allow easier construction of a PowerSpectrum. The benefits of this may not be evident in this PR, but a followup one will have pathological spectra constructed directly rather than ifft'ing the spectrum only to immediately fft it again.
  • Keeping track of the uncut data will allow us to plot what we excluded.
  • Keeping track of the high frequency data and performing the downsampling and exclusion/fit ranges lazily will allow us to do multiple analyses without the requirement to always recompute the expensive part (the FFT) or keep two separate power spectra around. It also means that we will be getting the full spectrum in the calibration routine, which will allow us to apply some improvements going forward.
  • Plotting the driving peak for an active calibration result gives a feel for how far above the noise floor it sits.
image

Tradeoffs

Increased size

Keeping the high frequency spectrum around does come at a cost, namely the increased memory footprint. For a typical calibration (100 kHz sample rate) this is about 4 MB worth of data per axis for passive, 8 for active.

Downsampling and frequency exclusion ranges

There is one annoying issue with showing the frequency exclusion ranges. Exclusion ranges are applied prior to downsampling, which means that downsampling can subsequently pull a point outside the exclusion ranges into it. This is not really a bug, as this allows us to make cuts smaller than the blocking size, but it can look weird.

image

@JoepVanlier JoepVanlier force-pushed the lazy_power branch 11 times, most recently from ba1a1a5 to aea2040 Compare October 2, 2024 16:40
@JoepVanlier JoepVanlier changed the title PowerSpectrum: simplify constructor and keep track of high frequency spectra PowerSpectrum: improve diagnostic value power spectrum api Oct 7, 2024
@JoepVanlier JoepVanlier marked this pull request as ready for review October 7, 2024 15:18
@JoepVanlier JoepVanlier requested review from a team as code owners October 7, 2024 15:18
@JoepVanlier JoepVanlier changed the base branch from main to expose_diode_model October 10, 2024 11:53
Base automatically changed from expose_diode_model to main October 18, 2024 14:20
@JoepVanlier JoepVanlier force-pushed the lazy_power branch 2 times, most recently from d07cc94 to 1c92eaf Compare December 2, 2024 17:20
Copy link
Contributor

@rpauszek rpauszek left a comment

Choose a reason for hiding this comment

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

Looks good, although I'm not gonna pretend that I got to deep into the intricacies of the math that was going on but looks mostly like rearranging and as always thoroughly tested. Just some minor nitpicks, mostly some extra comments would be good for those of us that don't know force calibration like the back of our hand

changelog.md Outdated
Comment on lines 18 to 19
* Allow showing the ranges that were excluded from a power spectrum by passing `show_excluded=True` to [`PowerSpectrum.plot()`](https://lumicks-pylake.readthedocs.io/en/latest/_api/lumicks.pylake.force_calibration.power_spectrum.PowerSpectrum.html#lumicks.pylake.force_calibration.power_spectrum.PowerSpectrum.plot).
* Allow plotting the ranges that were excluded from fitting and the active calibration peak when plotting a calibration result using `show_excluded=True` and `show_active_peak=True` in [`CalibrationResults.plot()`](https://lumicks-pylake.readthedocs.io/en/latest/_api/lumicks.pylake.force_calibration.power_spectrum_calibration.CalibrationResults.html#lumicks.pylake.force_calibration.power_spectrum_calibration.CalibrationResults.plot).
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
* Allow showing the ranges that were excluded from a power spectrum by passing `show_excluded=True` to [`PowerSpectrum.plot()`](https://lumicks-pylake.readthedocs.io/en/latest/_api/lumicks.pylake.force_calibration.power_spectrum.PowerSpectrum.html#lumicks.pylake.force_calibration.power_spectrum.PowerSpectrum.plot).
* Allow plotting the ranges that were excluded from fitting and the active calibration peak when plotting a calibration result using `show_excluded=True` and `show_active_peak=True` in [`CalibrationResults.plot()`](https://lumicks-pylake.readthedocs.io/en/latest/_api/lumicks.pylake.force_calibration.power_spectrum_calibration.CalibrationResults.html#lumicks.pylake.force_calibration.power_spectrum_calibration.CalibrationResults.plot).
* Show the ranges that were excluded from a power spectrum or calibration fit by passing `show_excluded=True` to [`PowerSpectrum.plot()`](https://lumicks-pylake.readthedocs.io/en/latest/_api/lumicks.pylake.force_calibration.power_spectrum.PowerSpectrum.html#lumicks.pylake.force_calibration.power_spectrum.PowerSpectrum.plot) or [`CalibrationResults.plot()`](https://lumicks-pylake.readthedocs.io/en/latest/_api/lumicks.pylake.force_calibration.power_spectrum_calibration.CalibrationResults.html#lumicks.pylake.force_calibration.power_spectrum_calibration.CalibrationResults.plot).
* Plot the active calibration peak for a calibration result using `show_active_peak=True` with [`CalibrationResults.plot()`](https://lumicks-pylake.readthedocs.io/en/latest/_api/lumicks.pylake.force_calibration.power_spectrum_calibration.CalibrationResults.html#lumicks.pylake.force_calibration.power_spectrum_calibration.CalibrationResults.plot).

import matplotlib.pyplot as plt

self.ps_data.plot(label="Data")
if show_active_peak and not hasattr(self.model, "output_power"):
raise ValueError("This is not an active calibration")
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: I wonder if this should be an AttributeError since the root problem comes from not having the output_power attribute. Could also include in the Raises section of the docstring but we're not super consistent with this

Copy link
Member Author

Choose a reason for hiding this comment

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

I was doubting about this too, but the error on the user side in this case is that they specified an incorrect value (they did not attempt to access this attribute themselves). That said, I will add a raises section, since it's generally a good idea to mention those.

Comment on lines +26 to +47
return (
np.insert(frequency, nan_insertion_points, np.nan),
np.insert(power, nan_insertion_points, np.nan),
frequency[break_idx - 1],
power[break_idx - 1],
)
Copy link
Contributor

Choose a reason for hiding this comment

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

it might be worthwhile commenting or adding to the docstring what exactly is returned from this function as it's not immediately clear from quickly inspecting the code

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep! Added.


self.total_samples_used = total_samples_used
self._raw_variance = variance
self._src_num_points_per_block = num_points_per_block
Copy link
Contributor

Choose a reason for hiding this comment

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

since this is private anyway, what's the src prefix for/mean?

Copy link
Member Author

Choose a reason for hiding this comment

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

It stands for source and was a reminder that that refers to the num points per block of the source material (pre blocking). That said, it is probably superfluous, since you can so clearly see it in the property already.

return self.sample_rate / self.total_sampled_used * self.num_points_per_block
return self.sample_rate / self.total_samples_used * self.num_points_per_block

def _apply_transforms(self, data, with_exclusions=True, downsample_data=True) -> np.ndarray:
Copy link
Contributor

Choose a reason for hiding this comment

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

I think a little docstring would be helpful here as it's not apparent to those of us that haven't delved deep into the intricacies of this math what transform needs to be applied or what it means

Copy link
Member Author

Choose a reason for hiding this comment

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

Fair point! I'll add a docstring.


def plot(self, **kwargs):
def plot(self, *, show_excluded=False, **kwargs):
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

plt.plot(self.frequency, self.power, **kwargs)
if show_excluded:
if self._downsampling_factor > 1:
plt.plot(self.unfiltered_frequency, self.unfiltered_power, label="_")
Copy link
Contributor

Choose a reason for hiding this comment

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

I had no idea that you could exclude labels from the legend like this 🤯

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah. It's pretty nice. :)

Three reasons for doing this
- A simpler constructor will allow easier construction of tests.
- Keeping track of the high frequency data and performing the downsampling and exclusion/fit ranges lazily will allow us to do multiple analyses without the requirement to always recompute the expensive part (the FFT).
- Keeping track of the uncut data will allow us to plot what we excluded.
allow plotting exclusion ranges and active calibration peak
@JoepVanlier JoepVanlier merged commit 382a72b into main Dec 9, 2024
8 checks passed
@JoepVanlier JoepVanlier deleted the lazy_power branch December 9, 2024 10:37
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.

2 participants