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

Deal with warnings #361

Merged
merged 4 commits into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
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
5 changes: 4 additions & 1 deletion spectroscopy/code_src/keck_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,10 @@ def KeckDEIMOS_get_spec(sample_table, search_radius_arcsec):
# Prepare arrays
wave = spec["LAMBDA"][0] * u.angstrom
flux_cgs = spec["FLUX"][0] * 1e-17 * u.erg/u.second/u.centimeter**2/u.angstrom
error_cgs = np.sqrt(1 / spec["IVAR"][0]) * 1e-17 * u.erg/u.second/u.centimeter**2/u.angstrom
# Inverse variances may be zero, resulting in infinite error.
# We'll leave these in and ignore the "divide by zero" warning.
with np.errstate(divide='ignore'):
error_cgs = np.sqrt(1 / spec["IVAR"][0]) * flux_cgs.unit
Comment on lines +67 to +70
Copy link
Member

Choose a reason for hiding this comment

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

This maybe a good example for a docs of good practices, e.g. deal with expected Warnings that are due to the nature of the functionality (e.g. either this or the occasional no result warning coming from astroquery when we loop through a list of coordinates where we expect to have no matches!); and any warnings the end user should/can do nothing about.


# Create MultiIndex object
dfsingle = pd.DataFrame(dict(wave=[wave],
Expand Down
32 changes: 17 additions & 15 deletions spectroscopy/code_src/mast_functions.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import io
import os
import shutil
from contextlib import redirect_stdout
import warnings

import astropy.constants as const
import astropy.units as u
import astroquery.exceptions
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
Expand Down Expand Up @@ -96,6 +96,11 @@ def JWST_get_spec_helper(sample_table, search_radius_arcsec, datadir, verbose,

# Query results
search_coords = stab["coord"]
# If no results are found, this will raise a warning. We explicitly handle the no-results
# case below, so let's suppress the warning to avoid confusing notebook users.
warnings.filterwarnings("ignore", message='Query returned no results.',
category=astroquery.exceptions.NoResultsWarning,
module="astroquery.mast.discovery_portal")
query_results = Observations.query_criteria(
coordinates=search_coords, radius=search_radius_arcsec * u.arcsec,
dataproduct_type=["spectrum"], obs_collection=["JWST"], intentType="science",
Expand All @@ -122,13 +127,9 @@ def JWST_get_spec_helper(sample_table, search_radius_arcsec, datadir, verbose,
print("Nothing to download for source {}.".format(stab["label"]))
continue

# Download (suppress output)
trap = io.StringIO()
with redirect_stdout(trap):
download_results = Observations.download_products(
data_products_list_filter, download_dir=this_data_dir)
if verbose:
print(trap.getvalue())
# Download
download_results = Observations.download_products(
data_products_list_filter, download_dir=this_data_dir, verbose=False)

# Create table
# NOTE: `download_results` has NOT the same order as `data_products_list_filter`.
Expand Down Expand Up @@ -311,6 +312,11 @@ def HST_get_spec(sample_table, search_radius_arcsec, datadir, verbose,

# Query results
search_coords = stab["coord"]
# If no results are found, this will raise a warning. We explicitly handle the no-results
# case below, so let's suppress the warning to avoid confusing notebook users.
warnings.filterwarnings("ignore", message='Query returned no results.',
category=astroquery.exceptions.NoResultsWarning,
module="astroquery.mast.discovery_portal")
query_results = Observations.query_criteria(
coordinates=search_coords, radius=search_radius_arcsec * u.arcsec,
dataproduct_type=["spectrum"], obs_collection=["HST"], intentType="science",
Expand All @@ -336,12 +342,8 @@ def HST_get_spec(sample_table, search_radius_arcsec, datadir, verbose,
continue

# Download
trap = io.StringIO()
with redirect_stdout(trap):
download_results = Observations.download_products(
data_products_list_filter, download_dir=this_data_dir)
if verbose:
print(trap.getvalue())
download_results = Observations.download_products(
data_products_list_filter, download_dir=this_data_dir, verbose=False)

# Create table
# NOTE: `download_results` has NOT the same order as `data_products_list_filter`.
Expand Down
5 changes: 4 additions & 1 deletion spectroscopy/code_src/sdss_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ def SDSS_get_spec(sample_table, search_radius_arcsec, data_release):
# only one entry because we only search for one xid at a time. Could change that?
wave = 10**sp[0]["COADD"].data.loglam * u.angstrom
flux = sp[0]["COADD"].data.flux*1e-17 * u.erg/u.second/u.centimeter**2/u.angstrom
err = np.sqrt(1/sp[0]["COADD"].data.ivar)*1e-17 * flux.unit
# Inverse variances may be zero, resulting in infinite error.
# We'll leave these in and ignore the "divide by zero" warning.
with np.errstate(divide='ignore'):
err = np.sqrt(1/sp[0]["COADD"].data.ivar)*1e-17 * flux.unit

# Add to df_spec.
dfsingle = pd.DataFrame(dict(wave=[wave], flux=[flux], err=[err],
Expand Down
1 change: 1 addition & 0 deletions spectroscopy/spectra_generator.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ This cell will install them if needed:
```{code-cell} ipython3
# Uncomment the next line to install dependencies if needed.
# !pip install -r requirements_spectra_generator.txt
# !pip install --upgrade --pre astroquery # >=0.4.8.dev9474 needed for mast_functions
```

```{code-cell} ipython3
Expand Down