<jupyter_start><jupyter_text>Processing perturbation coefficients using `sandy` Let's start by importing the python packages `sandy` and `pandas` for data processing.<jupyter_code>import sandy
import pandas as pd<jupyter_output><empty_output><jupyter_text>Then, we import some more packages for plotting purposes.<jupyter_code>import matplotlib.pyplot as plt
import seaborn as sns
# %matplotlib notebook   # uncomment this if you are in a jupyter notebook<jupyter_output><empty_output><jupyter_text>We define a multigroup perturbation as a series of tabulated pairs `(energy value, perturbation coefficient)`, where the energy values are the right bounds of the energy bins that define the perturbation coefficient as a step function.<jupyter_code>grid   = [1e-5, 1e-4, 1e-3, 1e-2, 1e-1, 1e0, 1e5, 1e6, 1e7, 2e7] # right bounds of each energy interval
values = [   1,  1.4,  1.3,  1.2,  1.1, 0.6, 1.8, 1.9, 1.9, 1.6]
series = pd.Series(values, index=grid)
pert = sandy.Pert(series)
pert<jupyter_output><empty_output><jupyter_text>The `pert` variable is a `sandy.Pert` instance and it contains perturbation coefficients into a `pandas.Series` with a `pandas.IntervalIndex` that defines the energy bins. For plotting and analysis purposes we are often interested in tabulated values that are not indexed by energy bins, but by scalar energy values.By calling attribute `.right` the instance returns a `pandas.Series` with perturbation coefficients as `.values` and the right bounds of the energy bins as `.index`.Try also `pert.left` and `pert.mid`.<jupyter_code>pert.right<jupyter_output><empty_output><jupyter_text>We can resort to the outputs produced by attributes `.right` and `.left` to plot the perturbation coefficients.*Notice that for a correct rendering of the output of `.left` 'steps-post' should be assigned to the drawstyle argument*.<jupyter_code>pert.right.plot(logx=True, drawstyle="steps")
plt.show()<jupyter_output><empty_output><jupyter_text>Perturbation objects can also be printed to a `.csv` or text file with columns divided by a specific separator.<jupyter_code>pert.right.to_csv("pert.csv", sep=",", header=False)
with open("pert.csv") as f:
    text = f.read()
print(text)<jupyter_output>1e-05,1.0
0.0001,1.4
0.001,1.3
0.01,1.2
0.1,1.1
1.0,0.6
100000.0,1.8
1000000.0,1.9
10000000.0,1.9
20000000.0,1.6<jupyter_text>Perturbations could even be imported into `sandy` directly from a text file.<jupyter_code>sandy.Pert.from_file("pert.csv", sep=",")<jupyter_output><empty_output><jupyter_text>Eventually, a common request is to reshape the perturbation coefficients according the energy structure of, say, a cross section to directly implement a vector-vector multiplication.This can be done using method `.reshape` to which the new energy grid is provided.<jupyter_code>new_grid = [1e6, 2e6, 5e6, 1e7, 2e7]
pert2 = pert.reshape(new_grid)
pert2<jupyter_output><empty_output><jupyter_text>The reshaped `pert2` objects contains the perturbation coefficients defined over a union between the original and the new energy grids. Of course the two plots overlap as the information does not change.<jupyter_code>pert.right.plot(logx=True, drawstyle="steps", label="original")
pert2.right.plot(logx=True, drawstyle="steps", label="reshaped")
plt.legend(loc="best")
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10213919855378126, 'fraction_numerical': 0.03766194636938837, 'mean_word_length': 6.0638297872340425, 'pattern_counts': {'":': 0, '<': 36, '<?xml version=': 0, '>': 36, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '9', 'dir': 'jupyter-structured-clean-dedup', 'id': '275092', 'license': 'MIT', 'n_tokens_mistral': 1160, 'n_tokens_neox': 1077, 'n_words': 358, 'path': 'notebooks/pert_notebook.ipynb', 'repo_name': 'nicoloabrate/sandy', 'size': '3319'}