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

UnitConversionError in plot_sensitivity_curve() #75

Closed
dbkeitel opened this issue Jan 6, 2022 · 7 comments · Fixed by #87
Closed

UnitConversionError in plot_sensitivity_curve() #75

dbkeitel opened this issue Jan 6, 2022 · 7 comments · Fixed by #87
Labels
bug Something isn't working

Comments

@dbkeitel
Copy link

dbkeitel commented Jan 6, 2022

Describe the bug
Following the visualisation tutorial, the plot_sensitivity_curve function doesn't work for me:

Example for reproducing

import legwork.visualisation as vis                                                                                                  
fig, ax = vis.plot_sensitivity_curve()
Traceback (most recent call last):
  File "/home/dkeitel/bin/venvs/legwork/lib/python3.9/site-packages/astropy/units/quantity.py", line 893, in to_value
    scale = self.unit._to(unit)
  File "/home/dkeitel/bin/venvs/legwork/lib/python3.9/site-packages/astropy/units/core.py", line 1098, in _to
    raise UnitConversionError(
astropy.units.core.UnitConversionError: 'Unit("Hz")' is not a scaled version of 'Unit(dimensionless)'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/dkeitel/bin/venvs/legwork/lib/python3.9/site-packages/astropy/units/quantity.py", line 1263, in __float__
    return float(self.to_value(dimensionless_unscaled))
  File "/home/dkeitel/bin/venvs/legwork/lib/python3.9/site-packages/astropy/units/quantity.py", line 896, in to_value
    value = self._to_value(unit, equivalencies)
  File "/home/dkeitel/bin/venvs/legwork/lib/python3.9/site-packages/astropy/units/quantity.py", line 803, in _to_value
    return self.unit.to(unit, self.view(np.ndarray),
  File "/home/dkeitel/bin/venvs/legwork/lib/python3.9/site-packages/astropy/units/core.py", line 1135, in to
    return self._get_converter(Unit(other),
  File "/home/dkeitel/bin/venvs/legwork/lib/python3.9/site-packages/astropy/units/core.py", line 1066, in _get_converter
    raise exc
  File "/home/dkeitel/bin/venvs/legwork/lib/python3.9/site-packages/astropy/units/core.py", line 1051, in _get_converter
    return self._apply_equivalencies(
  File "/home/dkeitel/bin/venvs/legwork/lib/python3.9/site-packages/astropy/units/core.py", line 1027, in _apply_equivalencies
    raise UnitConversionError(
astropy.units.core.UnitConversionError: 'Hz' (frequency) and '' (dimensionless) are not convertible

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/dkeitel/bin/venvs/legwork/lib/python3.9/site-packages/legwork/visualisation.py", line 314, in plot_sensitivity_curve
    ax.fill_between(frequency_range, 0, noise_amplitude, alpha=alpha, color=color)
  File "/home/dkeitel/bin/venvs/legwork/lib/python3.9/site-packages/matplotlib/__init__.py", line 1412, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "/home/dkeitel/bin/venvs/legwork/lib/python3.9/site-packages/matplotlib/axes/_axes.py", line 5252, in fill_between
    return self._fill_between_x_or_y(
  File "/home/dkeitel/bin/venvs/legwork/lib/python3.9/site-packages/matplotlib/axes/_axes.py", line 5225, in _fill_between_x_or_y
    pts[0] = start
  File "/home/dkeitel/bin/venvs/legwork/lib/python3.9/site-packages/astropy/units/quantity.py", line 1265, in __float__
    raise TypeError('only dimensionless scalar quantities can be '
TypeError: only dimensionless scalar quantities can be converted to Python scalars

Desktop (please complete the following information):

  • OS: Debian 11
  • Version: 0.1.6
  • installed in a venv
  • astropy 5.0
  • Run from: interactive python 3.9.2 session
  • same for a conda env with python 3.9.7
@dbkeitel dbkeitel added the bug Something isn't working label Jan 6, 2022
@dbkeitel
Copy link
Author

dbkeitel commented Jan 6, 2022

Interestingly, this code block from later in the tutorial does work for me:

# compare LISA and TianQin
fig, ax = vis.plot_sensitivity_curve(show=False, fill=False, instrument="LISA", label="LISA")
fig, ax = vis.plot_sensitivity_curve(show=False, fill=False, instrument="TianQin", label="TianQin",
                                     color=None, fig=fig, ax=ax)
ax.legend()
plt.show()

Seems like somethings' wrong with the defaults.

@dbkeitel
Copy link
Author

dbkeitel commented Jan 6, 2022

...but if I do fill=True in that second case, it fails again.

@TomWagg
Copy link
Collaborator

TomWagg commented Jan 6, 2022

Ah yes I believe this issue is the same as #57, it will be fixed by PR #72! 🙂

@TomWagg TomWagg closed this as completed Jan 6, 2022
@cmbiwer
Copy link

cmbiwer commented Jan 24, 2022

I am still seeing this error in the quick start tutorial. From LEGWORK main branch githash: 7e3d98b

Traceback (most recent call last):
  File "/Users/cmbiwer/anaconda3/envs/legwork2/lib/python3.7/site-packages/astropy/units/quantity.py", line 747, in to_value
    scale = self.unit._to(unit)
  File "/Users/cmbiwer/anaconda3/envs/legwork2/lib/python3.7/site-packages/astropy/units/core.py", line 1097, in _to
    f"'{self!r}' is not a scaled version of '{other!r}'")
astropy.units.core.UnitConversionError: 'Unit("Hz")' is not a scaled version of 'Unit(dimensionless)'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/cmbiwer/anaconda3/envs/legwork2/lib/python3.7/site-packages/astropy/units/quantity.py", line 1090, in __float__
    return float(self.to_value(dimensionless_unscaled))
  File "/Users/cmbiwer/anaconda3/envs/legwork2/lib/python3.7/site-packages/astropy/units/quantity.py", line 750, in to_value
    value = self._to_value(unit, equivalencies)
  File "/Users/cmbiwer/anaconda3/envs/legwork2/lib/python3.7/site-packages/astropy/units/quantity.py", line 670, in _to_value
    equivalencies=equivalencies)
  File "/Users/cmbiwer/anaconda3/envs/legwork2/lib/python3.7/site-packages/astropy/units/core.py", line 1134, in to
    equivalencies=equivalencies)(value)
  File "/Users/cmbiwer/anaconda3/envs/legwork2/lib/python3.7/site-packages/astropy/units/core.py", line 1064, in _get_converter
    raise exc
  File "/Users/cmbiwer/anaconda3/envs/legwork2/lib/python3.7/site-packages/astropy/units/core.py", line 1050, in _get_converter
    self, other, self._normalize_equivalencies(equivalencies))
  File "/Users/cmbiwer/anaconda3/envs/legwork2/lib/python3.7/site-packages/astropy/units/core.py", line 1026, in _apply_equivalencies
    f"{unit_str} and {other_str} are not convertible")
astropy.units.core.UnitConversionError: 'Hz' (frequency) and '' (dimensionless) are not convertible

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 35, in <module>
    vis.plot_sensitivity_curve(frequency_range=frequency_range, fig=fig, ax=right_ax)
  File "/Users/cmbiwer/src/LEGWORK/legwork/visualisation.py", line 314, in plot_sensitivity_curve
    ax.fill_between(frequency_range, 0 * noise_amplitude.unit, noise_amplitude, alpha=alpha, color=color)
  File "/Users/cmbiwer/anaconda3/envs/legwork2/lib/python3.7/site-packages/matplotlib/__init__.py", line 1412, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "/Users/cmbiwer/anaconda3/envs/legwork2/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 5254, in fill_between
    where=where, interpolate=interpolate, step=step, **kwargs)
  File "/Users/cmbiwer/anaconda3/envs/legwork2/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 5225, in _fill_between_x_or_y
    pts[0] = start
  File "/Users/cmbiwer/anaconda3/envs/legwork2/lib/python3.7/site-packages/astropy/units/quantity.py", line 1092, in __float__
    raise TypeError('only dimensionless scalar quantities can be '
TypeError: only dimensionless scalar quantities can be converted to Python scalars

@cmbiwer
Copy link

cmbiwer commented Jan 24, 2022

I also see this error for fig, ax = sources.plot_sources_on_sc(snr_cutoff=7, disttype="kde") at: https://legwork.readthedocs.io/en/latest/notebooks/Source.html

@cmbiwer
Copy link

cmbiwer commented Jan 24, 2022

Encountered another units error with

ax.fill_between(e_range, 0, t_merge, label="Default", alpha=0.2)

On https://legwork.readthedocs.io/en/latest/notebooks/Evolution.html

Traceback (most recent call last):
  File "/Users/cmbiwer/anaconda3/envs/legwork2/lib/python3.7/site-packages/astropy/units/quantity.py", line 747, in to_value
    scale = self.unit._to(unit)
  File "/Users/cmbiwer/anaconda3/envs/legwork2/lib/python3.7/site-packages/astropy/units/core.py", line 1097, in _to
    f"'{self!r}' is not a scaled version of '{other!r}'")
astropy.units.core.UnitConversionError: 'Unit("Gyr")' is not a scaled version of 'Unit(dimensionless)'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/cmbiwer/anaconda3/envs/legwork2/lib/python3.7/site-packages/astropy/units/quantity.py", line 1090, in __float__
    return float(self.to_value(dimensionless_unscaled))
  File "/Users/cmbiwer/anaconda3/envs/legwork2/lib/python3.7/site-packages/astropy/units/quantity.py", line 750, in to_value
    value = self._to_value(unit, equivalencies)
  File "/Users/cmbiwer/anaconda3/envs/legwork2/lib/python3.7/site-packages/astropy/units/quantity.py", line 670, in _to_value
    equivalencies=equivalencies)
  File "/Users/cmbiwer/anaconda3/envs/legwork2/lib/python3.7/site-packages/astropy/units/core.py", line 1134, in to
    equivalencies=equivalencies)(value)
  File "/Users/cmbiwer/anaconda3/envs/legwork2/lib/python3.7/site-packages/astropy/units/core.py", line 1064, in _get_converter
    raise exc
  File "/Users/cmbiwer/anaconda3/envs/legwork2/lib/python3.7/site-packages/astropy/units/core.py", line 1050, in _get_converter
    self, other, self._normalize_equivalencies(equivalencies))
  File "/Users/cmbiwer/anaconda3/envs/legwork2/lib/python3.7/site-packages/astropy/units/core.py", line 1026, in _apply_equivalencies
    f"{unit_str} and {other_str} are not convertible")
astropy.units.core.UnitConversionError: 'Gyr' (time) and '' (dimensionless) are not convertible

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test4.py", line 43, in <module>
    ax.fill_between(e_range, 0, t_merge, label="Default", alpha=0.2)
  File "/Users/cmbiwer/anaconda3/envs/legwork2/lib/python3.7/site-packages/matplotlib/__init__.py", line 1412, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "/Users/cmbiwer/anaconda3/envs/legwork2/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 5254, in fill_between
    where=where, interpolate=interpolate, step=step, **kwargs)
  File "/Users/cmbiwer/anaconda3/envs/legwork2/lib/python3.7/site-packages/matplotlib/axes/_axes.py", line 5225, in _fill_between_x_or_y
    pts[0] = start
  File "/Users/cmbiwer/anaconda3/envs/legwork2/lib/python3.7/site-packages/astropy/units/quantity.py", line 1092, in __float__
    raise TypeError('only dimensionless scalar quantities can be '
TypeError: only dimensionless scalar quantities can be converted to Python scalars

@TomWagg TomWagg reopened this Jan 24, 2022
@TomWagg
Copy link
Collaborator

TomWagg commented Jan 24, 2022

Thanks for pointing this out @cmbiwer, it seems ax.fill_between has become less tolerant of stray units. I've fixed this by adding quantity_support() in the relevant locations, so version 0.2.3 should work fine again.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants