<jupyter_start><jupyter_text>Load data<jupyter_code>path_perfect_sims_phot = "/sps/lsst/users/bbiswas/data/kilonova_datasets/RESSPECT/RESSPECT_PERFECT_LIGHTCURVE_WITH_KN.csv"
path_perfect_sims_head = "/sps/lsst/users/bbiswas/data/kilonova_datasets/RESSPECT/RESSPECT_PERFECT_HEAD_WITH_KN.csv"
data_ob = load_RESSPECT_data(phot_df_file_path=path_perfect_sims_phot, meta_df_file_path=path_perfect_sims_head)

from src.Generate_PC_utils import get_event_distirb
event_distrib = get_event_distirb(data_ob)
print(event_distrib)
from kndetect.utils import get_data_dir_path
data_dir = get_data_dir_path()<jupyter_output><empty_output><jupyter_text>Filter objects with readings every 2 days<jupyter_code>from src.Generate_PC_utils import get_ids_of_eqally_spaced_objects
equally_spaced_ids = get_ids_of_eqally_spaced_objects(data_ob)
event_distrib = get_event_distirb(data_ob, equally_spaced_ids)
print(event_distrib)
if save_results:
    np.save(os.path.join(data_dir,"equally_spaced_ids"), equally_spaced_ids)
equally_spaced_ids = np.load(os.path.join(data_dir,"equally_spaced_ids.npy"))
event_distrib = get_event_distirb(data_ob, equally_spaced_ids)
print(event_distrib)<jupyter_output>{'Ibc_V19': 584, 'IIn': 381, 'II_V19': 567, 'II-NMF': 621, 'Iax': 601, 'Ia': 849, '91bg': 558, 'Ibc-MOSFIT': 248, 'KN': 1000}<jupyter_text>Generating PCs  Create train data with specific distribution of Data<jupyter_code>from src.Generate_PC_utils import get_ids_for_target_distrib
target_distrib = {'91bg': 125, 'II-NMF': 125, 'II_V19': 125, 'IIn': 125, 'Ia': 125, 'Iax': 125, 'Ibc-MOSFIT': 125, 'Ibc_V19': 125, 'KN': 1000}
target_ids = get_ids_for_target_distrib(data_ob, target_distrib, equally_spaced_ids)
event_distrib = get_event_distirb(data_ob, target_ids)
print(event_distrib)<jupyter_output>{'II-NMF': 125, 'II_V19': 125, '91bg': 125, 'Iax': 125, 'Ibc_V19': 125, 'KN': 1000, 'Ia': 125, 'IIn': 125, 'Ibc-MOSFIT': 125}<jupyter_text>Generate PCS<jupyter_code>from src.Generate_PC_utils import stack_training_data, gen_components
train_data = stack_training_data(data_ob, object_ids=target_ids, num_days_tolerance=2, plot_results=False)
plt.close('all')
if save_results:

    np.save(os.path.join(data_dir,"PC_generation_dataset_mixed.npy"), train_data)
PC_dict, PC_var_ratio = gen_components(train_data)
print(PC_var_ratio)
band = 'all'
PCs= PC_dict[band]
markers = ['o','s','D','*','x']

band = 'all'

print(band+ " band")
PCs= PC_dict[band]
fig = plt.figure(figsize=(10,5))
for i in range(3):
    PC = PCs[i]
    x = np.arange(0,102,2)-50
    plt.plot(x,PC,marker=markers[i],label = "PC "+str(i+1))

plt.xlabel("days since maximum", fontsize = 25)
plt.ylabel("PCs", fontsize=25)

plt.rc('xtick', labelsize=17)    
plt.rc('ytick', labelsize=17) 

plt.rc('legend', fontsize=15)
plt.legend()
plt.tight_layout()
plt.show()
if save_results:

        np.save(os.path.join(data_dir, 'pc_var_ratio_mixed_pcs.npy'), PC_var_ratio)
        np.save(os.path.join(data_dir, "mixed_pcs"),PC_dict)<jupyter_output><empty_output><jupyter_text>Now interpolate<jupyter_code>import matplotlib.pyplot as plt
from src.io_utils import get_pcs
num_pc_components=3
bands=['g','r']
pcs = get_pcs(num_pc_components, pcs_choice='non-interpolated', normalize_pcs=False)
import numpy as np
fig = plt.figure(figsize=(10,5))

for i in range(num_pc_components):
    PC = pcs[i]
    x = np.arange(0,102,2)-50
    plt.scatter(x,PC,label = "PC "+str(i+1))
plt.legend()
x = np.linspace(-50, 50, num=51, endpoint=True)
xnew = np.linspace(-50, 50, num=401, endpoint=True)
np.shape(x)
from scipy.interpolate import interp1d
PC_inter_func = []

PC_new=[]
for i in range(3):
    func = interp1d(x, pcs[i], kind='quadratic')
    PC_inter_func.append(func)
    PC_new.append(func(xnew))
PC_new = np.array(PC_new)
np.shape(PC_new)
import numpy as np
fig = plt.figure(figsize=(10,5))
pc_names = ["PC1", "PC2", "PC3"]
colors = ['#F5622E', '#15284F', '#3C8DFF'] 
markers = ['o','s','D','*','x']
for i in range(num_pc_components):
    max_val = np.amax(np.abs(PC_new[i]))
    PC_new[i] = PC_new[i]/max_val
    PC = PC_new[i]
    plt.plot(xnew,PC,label = pc_names[i], marker=markers[i], ms=5, color=colors[i])
plt.xlabel("days since maximum", fontsize=25)
plt.ylabel("normalized PCs", fontsize=25)
ax = plt. gca()

leg = ax.legend()

plt.rc('xtick', labelsize=17)    
plt.rc('ytick', labelsize=17) 

plt.rc('legend', loc='lower right', fontsize=15)
plt.legend()
plt.tight_layout()
#plt.savefig("results/PC_plots/interpolated_pcs.pdf")
if save_results:
    np.save(os.path.join(data_dir, 'interpolated_mixed_pcs.npy'), PC_new)<jupyter_output><empty_output>
StarcoderdataJupyterStructuredCleanDedup