<jupyter_start><jupyter_text>Spectrum class exampleSimple example showing how the spectrum class works. Make sure you (pip) install the nasagamma module from: https://github.com/mauricioAyllon/NASA-gamma<jupyter_code>%matplotlib inline
from nasagamma import spectrum as sp
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd<jupyter_output><empty_output><jupyter_text>Load data and extract counts, channels, and energy.<jupyter_code># dataset 1
file = "data/SSR-mcnp.hdf"
df = pd.read_hdf(file, key='data')

# delete first (large) bin
df = df.iloc[1:,:]

cts_np = df.cts.to_numpy() * 1e8
erg = np.array(df.index)
chan = np.arange(0,len(cts_np),1)<jupyter_output><empty_output><jupyter_text>Instantiate a Spectrum object. Note that energy is an optional parameter. If it is not passed, channel numbers are infered.<jupyter_code># instantiate a Spectrum object
spect = sp.Spectrum(counts=cts_np, energies=erg)<jupyter_output><empty_output><jupyter_text>Plot using the built-in plotting routine<jupyter_code>spect.plot()<jupyter_output><empty_output><jupyter_text>We can also use some of the methods in the Spectrum class, such as rebin. Note that this conserves the area under the curve.<jupyter_code># rebin by 2 and by 4
ener2, cts2 = spect.rebin()
spect4 = sp.Spectrum(counts=cts2, energies=ener2)
ener4, cts4 = spect4.rebin()

plt.figure()
plt.plot(erg, cts_np, label="Original")
plt.plot(ener2, cts2, label="Rebinned by 2")
plt.plot(ener4, cts4, label="Rebinned by 4")
plt.yscale("log")
plt.legend()
plt.ylabel("cts/s/MeV")
plt.xlabel("Energy [MeV]")<jupyter_output><empty_output><jupyter_text>We can also apply the smooth function, which performs a moving average smoothing of the data. Note that this method does not necessarily conserve the area under the curve.<jupyter_code># smoothing every 8 points
spect_mv = sp.Spectrum(counts=cts_np, energies=erg)
n = 8
cts_mv = spect_mv.smooth(num=n)

plt.figure()
plt.plot(erg, cts_np, label="Original")
plt.plot(erg, cts_mv, label=f"Smoothing by {n}")
plt.yscale("log")
plt.legend()
plt.ylabel("cts/s/MeV")
plt.xlabel("Energy [MeV]")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1200750469043152, 'fraction_numerical': 0.01125703564727955, 'mean_word_length': 6.645161290322581, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '63629', 'license': 'MIT', 'n_tokens_mistral': 820, 'n_tokens_neox': 773, 'n_words': 207, 'path': 'examples/1.spectrum_example.ipynb', 'repo_name': 'mauricioAyllon/NASA-gamma', 'size': '2132'}