<jupyter_start><jupyter_text>![Astrofisica Computacional](../logo.png) --- 01. Data Analysis. Supernovae<NAME> (<EMAIL>)---  About this notebookIn this worksheet, we use a real ligth curve of a supernova to illustrate data analysis and the identification of the class of the event according to a theoretical model. ---  A Real Supernova LightcurveThe **A**merican **A**ssociation of **V**ariable **S**tars **O**bservers (**AAVSO**) collects information about variable stars, including nova and supernova events. From its webpage,https://www.aavso.orgit is possible to download the photometric data (lightcurves) of many objects. We will illustrate the data analysis using the supernova SN 2013E, located in the constellation Antlia. The data is given in a .txt file with comma separated values.  Reading the datasetThe dataset is a .txt file and we will use pandas to read it and take a look to its header and first elements.<jupyter_code>import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import warnings
warnings.filterwarnings('ignore')
df = pd.read_csv('aavsodata_5fea06dd7e52f-SN2013E.txt')

df.head()<jupyter_output><empty_output><jupyter_text>The `.describe()` attribute summarize the content of the dataset. Note that the column 'Magnitude' is not listed because its values are understood as string type variables.<jupyter_code>df.describe()<jupyter_output><empty_output><jupyter_text>The dataframe includes data from 171 observations of the supernova. The features (columns) include**JD** : Julian Date \**Magnitude** \**Uncertainty** \**Band**  --- We will work only with the data in the *V* band (visual). Therefore, we will select the relevant samples by imposing a filter to the dataframe,<jupyter_code>df[df['Band']=='V']<jupyter_output><empty_output><jupyter_text>The lightcurve will be defined using just the Julian Date and the Magnitude in the V band. These features are included in the definition of a new dataframe,<jupyter_code>lightcurve_df = df[df['Band']=='V'][['JD', 'Magnitude']]
lightcurve_df<jupyter_output><empty_output><jupyter_text>Note that we are left with only 121 datapoints. The type of the variables in this dataframe are<jupyter_code>lightcurve_df.dtypes<jupyter_output><empty_output><jupyter_text>Note that the 'Magnitude' column is not considered as a numerical variable. Then, we will use the function `pandas.to_numeric` to transform this column into float point numbers.<jupyter_code>lightcurve_df['Magnitude'] = pd.to_numeric(lightcurve_df['Magnitude'], downcast='float')
lightcurve_df
lightcurve_df.dtypes<jupyter_output><empty_output><jupyter_text>Now, the description of the dataframe gives the complete information about the magnitudes.<jupyter_code>lightcurve_df.describe()<jupyter_output><empty_output><jupyter_text>Ploting the DataIt is possible to use `pyplot` to plot directly from the dataframe. Note that we invert the y-axis (because magnitude is small for brigth objects).<jupyter_code>fig, ax = plt.subplots(figsize=(10,7))
ax.scatter(lightcurve_df['JD'], lightcurve_df['Magnitude'],  color='crimson', marker='.', label=r'SN 2013E')
ax.set_xlabel(r'$JD$')
ax.set_ylabel(r'$Magnitude$')
ax.set_ylim(19, 13)
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>--- Supernova TemplatesThe astrophysicist <NAME>, has developed a series of supernova templates based on theoretical models and observational data. These synthetic datasets are available athttps://c3.lbl.gov/nugent/nugent_templates.htmlPhotometric data included in this page correspond to the templates for supernovas of the following types- Type Ia Branch-normal (Bands: UBVRIJHK)- Type Ia 1991T-like (Bands: UBVRI)- Type Ia 1991bg-like (Bands: UBVRI)- Type Ib/c (Bands: UBVRI)- Type Ib/c high velocity - Hypernovae (Bands: V)- Type IIP (Bands: V)- Type IIL (Bands: V)- Type IIn (Bands: V)The datasets have no headers, the values are separated with spaces and the time is set to 0.0 at the explosion (maximum of the V-band magnitude) .We present here four of Nugent's templates:<jupyter_code># The delimiters in these file are spaces 
# There is no header in the file 
df_1a = pd.read_csv('sn1a_lc.v1.2.dat', header=None, delimiter=r'\s+', names=['days','U','B','V','R','I','J','H','K'] ) 
df_1bc = pd.read_csv('sn1bc_lc.v1.1.dat', header=None, delimiter=r'\s+', names=['days','U','B','V','R','I'] ) 
df_2p = pd.read_csv('sn2p_lc.v1.2.dat', header=None, delimiter=r'\s+', names=['days','V'] ) 
df_2n = pd.read_csv('sn2l_lc.v1.2.dat', header=None, delimiter=r'\s+', names=['days','V'] )<jupyter_output><empty_output><jupyter_text>The template for supernovas SNIa include the bands UBVRIJHK. Hence, we will define a new dataframe with only two columns, 'days' and 'V',<jupyter_code>template_df_1a = df_1a[['days','V']]
template_df_1a.describe()<jupyter_output><empty_output><jupyter_text>Similarly, the template for supernovas SNIb/c include the bands UBVRI. Hence, we will define a new dataframe with only two columns, 'days' and 'V',<jupyter_code>template_df_1bc = df_1bc[['days','V']]
template_df_1bc.describe()<jupyter_output><empty_output><jupyter_text>Templates for supernovas SNIIp and SNIIn only have information in the 'V' band. Now, we will look at the behavior of these templates,<jupyter_code>df_2p.describe()
df_2n.describe()
fig, ax = plt.subplots(figsize=(10,7))
ax.plot(template_df_1a['days'], template_df_1a['V'],  '--', 
        color='black', label=r'SNIa template')
ax.plot(template_df_1bc['days'], template_df_1bc['V'],  '--', 
        color='cornflowerblue', label=r'SNIb/c template')
ax.plot(df_2p['days'], df_2p['V'], '--', 
        color='mediumseagreen' , label=r'SNIIp template')
ax.plot(df_2n['days'], df_2n['V'], '--', 
        color='cadetblue' , label=r'SNIIn template')

ax.set_xlabel(r'$Days$')
ax.set_ylabel(r'$V$')
ax.set_ylim(10, -1)
plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Template FitNow, we will plot the supernova data and templates together,<jupyter_code>fig, ax = plt.subplots(figsize=(10,7))
ax.scatter(lightcurve_df['JD'], lightcurve_df['Magnitude'],  color='crimson', marker='.')

ax.plot(template_df_1a['days'], template_df_1a['V'],  '--', 
        color='black', label=r'SNIa template')
ax.plot(template_df_1bc['days'], template_df_1bc['V'],  '--', 
        color='cornflowerblue', label=r'SNIb/c template')
ax.plot(df_2p['days'], df_2p['V'], '--', 
        color='mediumseagreen' , label=r'SNIIp template')
ax.plot(df_2n['days'], df_2n['V'], '--', 
        color='cadetblue' , label=r'SNIIn template')

ax.set_xlabel(r'$JD - Days$')
ax.set_ylabel(r'$Magnitude$')
ax.set_ylim(19, -1)

plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Clearly the time scale is not appropiate. Since the SN data is given in a Julian Date timescale, we will rescale so that the maximum of brightness of the observational data occurs at day 0.0. First, we locate the maximum brightness (i.e. the minimum of Magnitude)<jupyter_code>Magnitude_min = lightcurve_df['Magnitude'].min()
Magnitude_min<jupyter_output><empty_output><jupyter_text>The corresponding sample is<jupyter_code>lightcurve_df[lightcurve_df['Magnitude']==Magnitude_min]<jupyter_output><empty_output><jupyter_text>The index corresponding to this sample is<jupyter_code>index_min = lightcurve_df[lightcurve_df['Magnitude']==Magnitude_min].index.tolist()
index_min<jupyter_output><empty_output><jupyter_text>and the corresponding Julian Date is<jupyter_code>JD_min = lightcurve_df['JD'][index_min[0]]
JD_min<jupyter_output><empty_output><jupyter_text>The rescaling of the time information gives<jupyter_code>lightcurve_df['JD'] = lightcurve_df['JD']- JD_min
lightcurve_df<jupyter_output><empty_output><jupyter_text>Now, we plot again,<jupyter_code>fig, ax = plt.subplots(figsize=(10,7))
ax.scatter(lightcurve_df['JD'], lightcurve_df['Magnitude'],  color='crimson', marker='.')

ax.plot(template_df_1a['days'], template_df_1a['V'],  '--', 
        color='black', label=r'SNIa template')
ax.plot(template_df_1bc['days'], template_df_1bc['V'],  '--', 
        color='cornflowerblue', label=r'SNIb/c template')
ax.plot(df_2p['days'], df_2p['V'], '--', 
        color='mediumseagreen' , label=r'SNIIp template')
ax.plot(df_2n['days'], df_2n['V'], '--', 
        color='cadetblue' , label=r'SNIIn template')

ax.set_xlabel(r'$Days$')
ax.set_ylabel(r'$Magnitude$')
ax.set_ylim(19, -1)

plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Now, we will re-scale the templates  using the maximum brightness of the lightcurve (i.e. the minimum Magnitude),<jupyter_code>template_df_1a['V_calib'] = template_df_1a['V'] + Magnitude_min
template_df_1bc['V_calib'] = template_df_1bc['V'] + Magnitude_min
df_2p['V_calib'] = df_2p['V'] + Magnitude_min
df_2n['V_calib'] = df_2n['V'] + Magnitude_min
template_df_1a
fig, ax = plt.subplots(figsize=(10,7))
ax.scatter(lightcurve_df['JD'], lightcurve_df['Magnitude'],  color='crimson', marker='.')

ax.plot(template_df_1a['days'], template_df_1a['V_calib'],  '--', 
        color='black', label=r'SNIa template')
ax.plot(template_df_1bc['days'], template_df_1bc['V_calib'],  '--', 
        color='cornflowerblue', label=r'SNIb/c template')
ax.plot(df_2p['days'], df_2p['V_calib'], '--', 
        color='mediumseagreen' , label=r'SNIIp template')
ax.plot(df_2n['days'], df_2n['V_calib'], '--', 
        color='cadetblue' , label=r'SNIIn template')

ax.set_xlabel(r'$Days$')
ax.set_ylabel(r'$Magnitude$')
ax.set_ylim(19, 13)

plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>From the plots above we recognize that- The SNIIp template does not fall sufficiently fast to fit the data.- The SNIIn template does fall too fast to fit the dataHence, the supernova SN 2013E may be of type SNIa or SNIb/c.<jupyter_code>fig, ax = plt.subplots(figsize=(10,7))
ax.scatter(lightcurve_df['JD'], lightcurve_df['Magnitude'],  color='crimson', marker='.')

ax.plot(template_df_1a['days'], template_df_1a['V_calib'],  '--', 
        color='black', label=r'SNIa template')
ax.plot(template_df_1bc['days'], template_df_1bc['V_calib'],  '--', 
        color='cornflowerblue', label=r'SNIb/c template')

ax.set_xlabel(r'$Days$')
ax.set_ylabel(r'$Magnitude$')
ax.set_ylim(19, 13)

plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>The SNIb/c template needs to be adjusted to fit the maximum value both in value and time. The value of the maximum of this template is<jupyter_code>template_1bc_min = template_df_1bc['V_calib'].min()
template_1bc_min<jupyter_output><empty_output><jupyter_text>The recalibration of the light curve gives<jupyter_code>template_df_1bc['V_calib2'] = template_df_1bc['V_calib'] + (Magnitude_min- template_1bc_min)
fig, ax = plt.subplots(figsize=(10,7))
ax.scatter(lightcurve_df['JD'], lightcurve_df['Magnitude'],  color='crimson', marker='.')

ax.plot(template_df_1a['days'], template_df_1a['V_calib'],  '--', 
        color='black', label=r'SNIa template')
ax.plot(template_df_1bc['days'], template_df_1bc['V_calib2'],  '--', 
        color='cornflowerblue', label=r'SNIb/c template')

ax.set_xlabel(r'$Days$')
ax.set_ylabel(r'$Magnitude$')
ax.set_ylim(19, 13)

plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Now, we shift the template in time by visual inspection to obtain<jupyter_code>template_df_1bc['days_calib2'] = template_df_1bc['days'] - 2
fig, ax = plt.subplots(figsize=(10,7))
ax.scatter(lightcurve_df['JD'], lightcurve_df['Magnitude'],  color='crimson', marker='.')

ax.plot(template_df_1a['days'], template_df_1a['V_calib'],  '--', 
        color='black', label=r'SNIa template')
ax.plot(template_df_1bc['days_calib2'], template_df_1bc['V_calib2'],  '--', 
        color='cornflowerblue', label=r'SNIb/c template')

ax.set_xlabel(r'$Days$')
ax.set_ylabel(r'$Magnitude$')
ax.set_ylim(19, 13)

plt.legend()
plt.show()<jupyter_output><empty_output><jupyter_text>Finally, using again a visual inspection of the plot, we will shift the templates in the brightness axis to obtain a better fit<jupyter_code>template_df_1bc['V_calib2'] = template_df_1bc['V_calib'] + (Magnitude_min- template_1bc_min)+0.1
template_df_1a['V_calib2'] = template_df_1a['V_calib'] +0.07
fig, ax = plt.subplots(figsize=(10,7))
ax.scatter(lightcurve_df['JD'], lightcurve_df['Magnitude'],  color='crimson', marker='.')

ax.plot(template_df_1a['days'], template_df_1a['V_calib2'],  '--', 
        color='black', label=r'SNIa template')
ax.plot(template_df_1bc['days_calib2'], template_df_1bc['V_calib2'],  '--', 
        color='cornflowerblue', label=r'SNIb/c template')

ax.set_xlabel(r'$Days$')
ax.set_ylabel(r'$Magnitude$')
ax.set_ylim(19, 13)

plt.legend()
plt.show()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.151452937460518, 'fraction_numerical': 0.016898294377763738, 'mean_word_length': 6.652567975830816, 'pattern_counts': {'":': 0, '<': 104, '<?xml version=': 0, '>': 104, 'https://': 2, 'lorem ipsum': 0, 'www.': 1, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '25', 'dir': 'jupyter-structured-clean-dedup', 'id': '452254', 'license': 'MIT', 'n_tokens_mistral': 4875, 'n_tokens_neox': 4580, 'n_words': 1009, 'path': '17. Data Analysis. Supernoavae/01.DataAnalysis-Supernovae.ipynb', 'repo_name': 'ashcat2005/AstrofisicaComputacional2022', 'size': '12696'}