question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Using LombScargle with units crashes when calling autopower()

See original GitHub issue

Description

The LombScargle fails to compute autpower if Times and y values have been passed as Quantity to LombScargle . I’m using astropy Version: 4.1 and numpy Version: 1.18.1. See the snippet below

Code

window_ls = LombScargle(data["Time"] * u.s, np.ones(data["Time"].shape) * u.ct / u.s, fit_mean=False, nterms=1, center_data=False)
frequency_window, power_window = window_ls.autopower(minimum_frequency=minimum_frequency,
                                                         maximum_frequency=0, samples_per_peak=5)

Error

  File "/home/agurpide/anaconda3/lib/python3.7/site-packages/astropy/units/quantity.py", line 727, in to_value
    scale = self.unit._to(unit)
  File "/home/agurpide/anaconda3/lib/python3.7/site-packages/astropy/units/core.py", line 951, in _to
    f"'{self!r}' is not a scaled version of '{other!r}'")
astropy.units.core.UnitConversionError: 'Unit("s")' is not a scaled version of 'Unit(dimensionless)'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/agurpide/anaconda3/lib/python3.7/site-packages/astropy/units/quantity.py", line 1080, in __int__
    return int(self.to_value(dimensionless_unscaled))
  File "/home/agurpide/anaconda3/lib/python3.7/site-packages/astropy/units/quantity.py", line 730, in to_value
    value = self._to_value(unit, equivalencies)
  File "/home/agurpide/anaconda3/lib/python3.7/site-packages/astropy/units/quantity.py", line 661, in _to_value
    equivalencies=equivalencies)
  File "/home/agurpide/anaconda3/lib/python3.7/site-packages/astropy/units/core.py", line 987, in to
    return self._get_converter(other, equivalencies=equivalencies)(value)
  File "/home/agurpide/anaconda3/lib/python3.7/site-packages/astropy/units/core.py", line 918, in _get_converter
    raise exc
  File "/home/agurpide/anaconda3/lib/python3.7/site-packages/astropy/units/core.py", line 904, in _get_converter
    self, other, self._normalize_equivalencies(equivalencies))
  File "/home/agurpide/anaconda3/lib/python3.7/site-packages/astropy/units/core.py", line 888, in _apply_equivalencies
    unit_str, other_str))
astropy.units.core.UnitConversionError: 's' (time) and '' (dimensionless) are not convertible

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/agurpide/scripts/swift_scripts/lomb_scargle.py", line 190, in <module>
    samples_per_peak=samples_per_peak)
  File "/home/agurpide/anaconda3/lib/python3.7/site-packages/astropy/timeseries/periodograms/lombscargle/core.py", line 306, in autopower
    maximum_frequency=maximum_frequency)
  File "/home/agurpide/anaconda3/lib/python3.7/site-packages/astropy/timeseries/periodograms/lombscargle/core.py", line 249, in autofrequency
    Nf = 1 + int(np.round((maximum_frequency - minimum_frequency) / df))
  File "/home/agurpide/anaconda3/lib/python3.7/site-packages/astropy/units/quantity.py", line 1082, in __int__
    raise TypeError('only dimensionless scalar quantities can be '
TypeError: only dimensionless scalar quantities can be converted to Python scalars

Problem

in astropy.timeseries.periodograms.lombscargle.core.py

        df = 1.0 / baseline / samples_per_peak

        if minimum_frequency is None:
            minimum_frequency = 0.5 * df

        if maximum_frequency is None:
            avg_nyquist = 0.5 * n_samples / baseline
            maximum_frequency = nyquist_factor * avg_nyquist

        Nf = 1 + int(np.round((maximum_frequency - minimum_frequency) / df))

The problem I believe is that while df is a Quantity, maximum_frequency and minimum_frequency are not and thus it crashes. Perhaps a solution could be to allow autopower to accept minimum_frequency and maximum_frequency as Quantity too.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
mhvkcommented, Oct 29, 2020

It does seem a case where the docs just have to be clearer - if you use quantities in constructing LS, you should use quantities throughout…

1reaction
adrncommented, Oct 28, 2020

Not a timeseries maintainer, but a LombScargle user: This indeed looks like an oversight to me, and I think we should support this!

Read more comments on GitHub >

github_iconTop Results From Across the Web

LombScargle — Astropy v5.2
Compute Lomb-Scargle power at automatically-determined frequencies. Parameters. method str , optional. specify the lomb scargle implementation to use. Options ...
Read more >
Lomb-Scargle vs FFT power spectrum: crashes with evenly ...
I am trying to create some routines to compute power spectra for both evenly and unevenly sampled data, using the Lomb-Scargle periodogram ...
Read more >
LombScargle occasionally returns all-NaN powers #10070
Description I want to use LombScargle to calculate the spectral window function of ... ys).autopower(method='fast') np.isfinite(power).any().
Read more >
How to Interpret Lomb Scargle periodogram
The units will be based on whatever units are in your input. i.e. It is just the inverse of whatever you are giving...
Read more >
Fast Lomb-Scargle Periodograms in Python
predict(period * phase_fit, period=period) # Plot the phased data & model fig, ax = plt.subplots() ax.errorbar(phase, mag, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found