import numpy as np
import lightkurve
from sympy.plotting import plot
from sklearn.decomposition import FastICA, PCA
import matplotlib.pyplot as plt
tpfs = lightkurve.search_tesscut(ticid).download(cutout_size=11)
ff = tpfs.to_lightcurve()
##Perform ICA
n_components = 10
X = np.ascontiguousarray(np.nan_to_num(tpfs.flux), np.float64)
X_flat = X.reshape(len(tpfs.flux),-1) #turns three dimensional into two dimensional
ica = FastICA(n_components=n_components) #define n_components
S_ = ica.fit_transform(X_flat)
A_ = ica.mixing_ #combine x_flat to get x
#print(S_.shape, A_.shape, tpfs.flux.shape)
## Plot components
for i in range(n_components):
plt.plot(S_[:,i]+0.2*i)
## Make pixel weighting arrays
A_useful = A_.reshape(11,11,n_components).T #reshape from 2d to 3d
print(len(A_useful))
for i in range(n_components):
plt.imshow(A_useful[i])
plt.colorbar()
plt.plot(S_[:,1]) #plot one principal component
plt.ylim(-0.1, 0.1)
plt.imshow(A_useful[1]) #plot corresponding weight map
plt.colorbar()