import numpy as np

# Paths to the .npy files
theta_path = "/drive2/Kuntal/Pysindy-experiment/M1-output/messidor_all_thetas.npy"
ids_path   = "/drive2/Kuntal/Pysindy-experiment/M1-output/messidor_theta_ids.npy"

# Load the data
thetas = np.load(theta_path)  # shape: (N_samples, 3, 20)
ids    = np.load(ids_path)    # shape: (N_samples,)

# Confidence interval bounds
# min_bound, max_bound = -0.014539650469939325, 0.014539650469939325  # Fixed en-dash to minus sign
# min_bound, max_bound = -0.00042219943335775056, 0.00042219943335775056
min_bound, max_bound = -0.0596098734708454, 0.0596098734708454
# Create masks for inside and outside the interval
outside_mask = (thetas < min_bound) | (thetas > max_bound)
inside_mask  = ~outside_mask

# Summary counts
total_values        = thetas.size
num_outside_values  = np.sum(outside_mask)
num_inside_values   = np.sum(inside_mask)

print(f"Total theta values:           {total_values}")
print(f"Values outside interval:      {num_outside_values}")
print(f"Values inside interval:       {num_inside_values}")
print(f"Percent outside:              {100 * num_outside_values/total_values:.2f}%")
print(f"Percent inside:               {100 * num_inside_values/total_values:.2f}%\n")

# Find samples (by index and ID) that have any out-of-bound values
sample_out_idx = np.where(np.any(outside_mask.reshape(thetas.shape[0], -1), axis=1))[0]
sample_in_idx  = np.where(np.all( inside_mask.reshape(thetas.shape[0], -1), axis=1))[0]

print(f"Number of samples with any outside values:  {len(sample_out_idx)}")
print(f"Sample IDs with outside values:             {ids[sample_out_idx]}\n")

print(f"Number of samples fully inside interval:     {len(sample_in_idx)}")
print(f"Sample IDs fully inside interval:            {ids[sample_in_idx]}")

# (Optional) Extract the actual inside and outside values if needed:
inside_values  = thetas[inside_mask]   # 1D array of all the values inside
outside_values = thetas[outside_mask]  # 1D array of all the values outside
