<jupyter_start><jupyter_text>Specviz Demonstration NotebookThis notebook demonstrates the Specviz API in the Notebook setting. UI equivalents for these actions, as well as additional documentation about Specviz, can be found here: https://jdaviz.readthedocs.io/en/latest/specviz/ Setup<jupyter_code>import astropy.units as u
# Suppress warnings
import warnings

with warnings.catch_warnings():
    warnings.simplefilter("ignore")<jupyter_output><empty_output><jupyter_text>Create Specviz via Helper<jupyter_code>from jdaviz import Specviz
specviz = Specviz()<jupyter_output><empty_output><jupyter_text>Display Specviz<jupyter_code>specviz.app<jupyter_output><empty_output><jupyter_text>Load a file to Specviz<jupyter_code>from astropy.utils.data import download_file
fn = download_file('https://data.sdss.org/sas/dr14/sdss/spectro/redux/26/spectra/0751/spec-0751-52251-0160.fits', cache=True)
specviz.load_spectrum(fn, "myfile", format="SDSS-III/IV spec")<jupyter_output><empty_output><jupyter_text>Subset RetrievalUser Task: Select a subset in the viewer aboveAfter defining regions in your spectra, you can retrieve your subsets through different means. To retrieve your subset by name:<jupyter_code># Returns a version of the whole spectra, with a mask applied
specviz.get_spectra('Subset 1')<jupyter_output><empty_output><jupyter_text>Or, if you've defined multiple regions, you can retrieve all defined regions/subsets via:<jupyter_code>specviz.app.get_subsets_from_viewer('spectrum-viewer')<jupyter_output><empty_output><jupyter_text>Screenshot Saving<jupyter_code>specviz.app.get_viewer("spectrum-viewer").figure.save_png()<jupyter_output><empty_output><jupyter_text>Panning/Zooming in Specviz Limit MethodsYou can use the methods x_limits() and y_limits() to modify the field of view of Specviz. You can provide it a scalar (which assumes the units of the loaded spectra), an Astropy Quantity, or 'auto' to automatically scale.<jupyter_code>specviz.x_limits()
specviz.x_limits(650*u.nm,750*u.nm)
specviz.y_limits('auto', 110.0)<jupyter_output><empty_output><jupyter_text>Disable Scientific NotationScientific notation is used in the axes of the viewers by default. To deactivate it, run the following code:<jupyter_code># axis 1 corresponds to the Y-axis and axis 0 to the X-axis
# fmt can be '0.1e' to set scientific notation or '0.2f' to turn it off
specviz.set_spectrum_tick_format(fmt="0.2f", axis=1)<jupyter_output><empty_output><jupyter_text>Autoscale MethodsYou can also quickly return to the default zoom using autoscale_x() and autoscale_y()<jupyter_code>specviz.autoscale_x()
specviz.autoscale_y()<jupyter_output><empty_output><jupyter_text>Extracting ModelsIf models were added using the model fitting plugin, they can be extracted using the following property<jupyter_code>specviz.fitted_models<jupyter_output><empty_output><jupyter_text>If the name of a particular model is known, it can be extracted from the fitted_models property<jupyter_code>specviz.fitted_models['Model']<jupyter_output><empty_output><jupyter_text>Alternatively, the following getter can be used<jupyter_code>models = specviz.get_models()
models<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.08933080808080808, 'fraction_numerical': 0.012941919191919192, 'mean_word_length': 8.29325513196481, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 53, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '495062', 'license': 'MIT', 'n_tokens_mistral': 1101, 'n_tokens_neox': 1055, 'n_words': 264, 'path': 'notebooks/SpecvizExample.ipynb', 'repo_name': 'check-spelling/jdaviz', 'size': '3168'}