In [1]:
%matplotlib inline
from matplotlib import rcParams
rcParams["savefig.dpi"] = 150

In [191]:
import h5py
import json
import fitsio
import numpy as np
import pandas as pd
from astropy.wcs import WCS
from itertools import product
import matplotlib.pyplot as pl
from scipy.linalg import cho_factor, cho_solve
from matplotlib.colors import LinearSegmentedColormap
from sklearn.decomposition import FastICA, RandomizedPCA

In [198]:
with open("k2.wcs.json", "r") as f:
    wcs = WCS(json.load(f))

In [199]:
df = pd.read_csv("K2_var.dat", delim_whitespace=True, names=[
    "ID", "RA", "DEC", "X", "Y", "Mag_Sch", "Mag_K2", "Phot_K2", "Clas_K2", "Period",
    "ID_Nar", "ID_Hu", "ID_Hu", "ID_Kim", "ID_Mei", "ID_Moc", "B", "V", "R",
    "J_2MASS", "H_2MASS", "K_2MASS", 
])
df = df[df.Clas_K2 == 1]

In [228]:
ra = np.array(df.RA)
dec = np.array(df.DEC)
x, y = wcs.all_world2pix(ra, dec, 1)
df["k2_cx"] = x
df["k2_cy"] = y
print(x.min(), x.max(), y.min(), y.max())


6.47309067761 842.618822315 17.4438291402 649.500747629

In [469]:
df[df.J_2MASS > 14].sort("J_2MASS")


Out[469]:
ID RA DEC X Y Mag_Sch Mag_K2 Phot_K2 Clas_K2 Period ... ID_Mei ID_Moc B V R J_2MASS H_2MASS K_2MASS k2_cx k2_cy
151 3464 91.770158 23.988426 3401.7183 352.9890 -12.6841 15.2568 2 1 0.401891 ... -1 -1 16.0587 15.3444 15.1896 14.001 13.753 13.718 782.405927 449.615643
953 12644 92.198202 24.161670 1765.7593 1065.8515 -12.3598 15.4538 2 1 5.473107 ... 288 -1 16.6910 15.7034 15.4137 14.005 13.555 13.443 402.605991 374.294605
1287 16544 92.245494 24.244804 1584.0434 1412.0240 -11.9662 15.7424 5 1 8.136031 ... 395 -1 17.3839 16.1911 15.7205 14.005 13.442 13.251 347.789200 309.393765
1852 22268 92.273183 24.375511 1476.2322 1957.1279 -12.4245 15.3617 2 1 1.364675 ... -1 -1 16.4789 15.6601 15.3895 14.008 13.628 13.501 299.229632 198.911003
1509 18834 92.284050 24.295713 1436.3789 1623.8882 -12.3544 15.4722 2 1 -99.000000 ... -1 -1 16.6882 15.7187 15.3984 14.008 13.512 13.345 306.525392 271.414315
2430 28549 92.124969 24.532769 2036.1012 2616.1339 -10.9766 16.6461 5 1 -99.000000 ... 441 -1 19.1329 17.4720 16.7141 14.009 13.264 13.029 386.272111 32.632768
1400 17633 92.180705 24.268592 1830.1069 1512.4586 -12.3513 15.4375 5 1 6.027691 ... 304 -1 16.8077 15.7532 15.4238 14.018 13.616 13.462 395.128251 276.556664
756 9986 92.258859 24.114334 1535.6657 867.2116 -12.9653 15.0517 2 1 1.286762 ... -1 -1 16.1758 15.1027 15.5310 14.018 13.878 13.709 363.207221 427.139138
1666 20531 92.306929 24.333111 1348.6826 1779.6302 -12.2156 15.5688 2 1 5.359926 ... -1 -1 16.8809 15.9151 15.5257 14.019 13.573 13.486 280.610338 242.532858
2306 27020 92.258722 24.491952 1528.9874 2443.3553 -12.3849 15.6322 2 1 4.166096 ... 252 -1 16.8366 15.9042 15.4877 14.024 13.491 13.380 287.381780 93.344403
2172 25408 92.070805 24.450470 2243.8061 2273.7671 -12.3995 15.4783 2 1 5.835201 ... 302 -1 16.6718 15.7408 15.3784 14.032 13.622 13.517 446.407578 95.474562
229 4518 92.152879 24.015482 1941.6000 456.4941 -12.5009 15.3650 2 1 8.850414 ... -1 -1 16.5580 15.5469 15.2751 14.032 13.593 13.547 468.597300 495.360268
1772 21563 91.887085 24.355485 2944.7674 1881.7420 -12.8485 15.1417 2 1 0.035920 ... -1 -1 15.7941 15.1780 15.0716 14.034 13.796 13.710 612.964807 145.675130
798 10552 92.508463 24.124252 584.6690 905.3221 -13.1386 14.8561 2 1 1.933229 ... 166 -1 15.3220 14.9152 14.8446 14.035 13.920 13.835 159.550514 463.358712
1056 13786 92.215570 24.184767 1699.1501 1161.9473 -12.8788 15.1251 2 1 14.456852 ... -1 -1 15.6216 15.3087 15.6673 14.037 13.902 13.811 383.964150 357.028235
1127 14692 92.272262 24.204919 1482.9190 1245.1047 -12.2704 15.5509 2 1 5.768951 ... 294 -1 16.8196 15.8261 15.4913 14.041 13.570 13.541 334.237297 349.509836
1931 22924 91.879693 24.389085 2971.8601 2022.1701 -12.5501 15.3848 2 1 0.089988 ... -1 -1 16.2794 15.5124 15.3118 14.042 13.740 13.624 611.954063 114.524147
1637 20273 92.210127 24.327208 1716.9538 1756.5790 -11.8039 15.9066 5 1 7.956304 ... -1 -1 17.5704 16.3860 15.8489 14.049 13.472 13.355 359.640849 230.094906
1556 19265 92.340828 24.305806 1220.2184 1665.1671 -12.1996 15.5693 2 1 5.862149 ... 301 -1 16.9066 15.9418 15.5658 14.049 13.624 13.571 258.794798 272.821687
1089 14312 92.260795 24.196174 1526.7382 1208.7891 -12.5900 15.2976 2 1 1.114799 ... -1 -1 16.1712 15.4332 15.2680 14.049 13.844 13.779 345.232710 355.157560
300 5360 91.963012 24.031831 2665.0429 528.9217 -12.4659 15.4185 2 1 -99.000000 ... -1 -1 16.4241 15.6142 15.3529 14.054 13.744 13.634 618.346702 446.397574
1025 13490 92.289057 24.179138 1419.4490 1137.2232 -12.2607 15.6041 2 1 0.089924 ... -1 -1 16.7497 15.8106 15.5534 14.064 13.738 13.568 325.865642 375.338051
1113 14569 91.981876 24.200914 2588.5219 1234.2098 -12.2564 15.6047 2 1 -99.000000 ... 312 -1 16.7455 15.8139 15.4984 14.078 13.602 13.505 568.671000 300.085122
1346 17164 92.106525 24.257715 2112.6246 1468.5107 -12.7393 15.1966 2 1 0.059760 ... -1 -1 16.0174 15.3393 15.1708 14.081 13.897 13.718 456.964249 272.626394
323 5640 92.188155 24.038767 1806.6185 553.0225 -12.1232 15.6622 2 1 0.941615 ... -1 -1 17.0300 15.9527 15.5857 14.087 13.613 13.422 435.437615 481.150672
113 3052 92.351435 23.982165 1185.1424 314.0770 -11.9209 15.7869 5 1 4.718687 ... -1 -1 17.3953 16.2206 15.7977 14.090 13.535 13.412 314.770101 560.595449
609 8616 91.900902 24.090343 2900.0877 774.7326 -11.7639 15.9940 5 1 6.224134 ... -1 90 17.4502 16.4061 15.9797 14.101 13.685 13.553 656.354567 383.207161
362 6158 92.488364 24.050030 662.1383 595.6950 -12.4518 15.4445 2 1 4.950436 ... -1 -1 16.3282 15.5844 15.4130 14.108 13.903 13.824 190.462907 525.237502
2176 25492 91.799613 24.450704 3274.2892 2281.5612 -11.3142 16.3755 5 1 11.519587 ... -1 -1 18.4688 17.1090 16.3659 14.109 13.481 13.237 663.200556 45.013409
2449 28770 92.139330 24.539140 1981.4264 2642.4406 -12.0462 15.6625 2 1 -99.000000 ... -1 -1 16.9121 15.9535 15.6160 14.110 13.728 13.585 373.481747 29.646278
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
1008 13310 92.485180 24.175806 672.7254 1120.7572 -7.4589 19.8754 5 1 1.555366 ... -1 -1 27.7506 21.6034 20.3719 16.942 16.471 15.632 168.196904 413.691010
1710 20958 91.871884 24.341303 3003.0050 1822.9545 -9.5133 18.3585 5 1 3.779865 ... -1 -1 19.5493 18.5705 18.2115 16.945 17.007 15.276 628.053570 155.433965
806 10606 92.174804 24.123967 1855.6755 908.9088 -7.1995 20.1127 5 1 36.286699 ... -1 -1 27.7319 22.0259 20.6029 16.950 16.365 16.053 429.056292 403.386154
1192 15518 91.762178 24.219818 3424.3425 1319.0362 -7.5621 19.9021 5 1 -99.000000 ... -1 -1 22.4860 21.1949 20.1827 16.958 16.399 16.321 740.990646 242.876289
1302 16741 92.002844 24.248057 2507.4379 1430.4820 -8.5440 19.2051 5 1 9.996336 ... -1 -1 21.0790 19.7236 19.0767 16.958 16.166 15.966 542.207316 262.180521
1347 17168 92.470265 24.259311 728.4466 1469.4800 -8.5557 19.1023 5 1 8.322637 ... -1 -1 21.7633 20.0392 19.1573 16.969 16.033 15.538 163.709251 337.316729
1876 22446 92.277390 24.380299 1460.1508 1977.0441 -8.2439 19.2690 5 1 0.920982 ... -1 -1 27.8191 20.6894 19.5280 16.970 16.347 16.007 294.885392 195.447545
812 10692 92.357497 24.126209 1159.7068 915.2807 -8.3222 19.4310 5 1 4.234154 ... -1 -1 21.3242 19.8964 19.3468 16.975 16.577 15.876 281.205211 434.470027
2412 28249 92.346284 24.525294 1195.8454 2581.2000 -8.8426 18.8730 5 1 6.277976 ... -1 -1 20.5484 19.3700 18.7758 16.978 16.169 15.716 210.420767 79.928412
384 6381 92.634573 24.054590 104.8355 613.4956 -9.1217 18.5062 5 1 9.474334 ... -1 -1 -99.0000 -99.0000 -99.0000 16.984 16.068 15.973 71.054727 547.332111
22 1228 92.395453 23.939479 1017.9231 135.2852 -8.7453 18.8794 5 1 0.603605 ... -1 -1 20.7928 19.4319 18.9098 16.992 16.378 16.035 287.587723 606.177070
136 3296 92.448945 23.988045 813.2128 337.3702 -8.5496 19.0547 5 1 -99.000000 ... -1 -1 20.9233 19.6201 19.0671 16.997 16.353 16.259 234.632854 572.878379
392 6413 92.473072 24.054972 720.3632 616.4846 -7.9240 19.6544 5 1 14.690971 ... -1 -1 22.3103 20.6272 19.6342 16.998 16.249 16.219 201.870877 518.137643
1599 19841 92.449161 24.318703 807.9580 1717.6196 -7.3755 20.1285 5 1 0.836485 ... -1 -1 27.7497 21.3146 20.3828 17.003 16.022 15.922 168.942963 281.083863
889 11808 92.403031 24.147004 985.9508 1001.4800 -9.0924 18.5380 5 1 7.018704 ... -1 -1 20.3093 19.1068 18.6256 17.005 15.957 15.642 240.287254 424.318559
1251 16093 92.599964 24.235864 235.1196 1370.4485 -8.8886 18.7504 5 1 4.749568 ... -1 -1 20.5371 19.3104 18.7782 17.008 16.173 17.291 63.562704 381.352111
397 6447 92.307667 24.055106 1350.7689 619.2031 -7.1550 20.1746 5 1 0.567689 ... -1 -1 27.7225 22.0575 20.7181 17.016 16.044 16.266 335.640517 488.291461
1456 18227 91.962048 24.280605 2661.7934 1567.3030 -7.2419 20.2638 5 1 0.702006 ... -1 -1 27.7288 21.4617 20.6407 17.037 16.166 15.942 568.286566 225.856324
1947 23027 92.360133 24.394324 1145.3598 2034.3624 -9.3842 18.5846 5 1 2.646732 ... -1 -1 19.8340 18.7972 18.4097 17.042 16.289 16.257 225.547171 198.146154
45 1995 92.203382 23.957428 1750.2565 213.2051 -8.8386 18.9347 5 1 6.399771 ... -1 -1 20.4682 19.3074 18.8432 17.047 16.535 16.303 439.471723 555.821852
273 4966 92.349003 24.025620 1193.7062 495.5093 -7.3092 19.6916 5 1 15.640819 ... -1 -1 24.4879 21.7274 20.4229 17.072 16.413 15.790 308.096272 521.779424
176 3754 91.990008 23.996779 2563.0730 381.9546 -7.6015 19.9319 5 1 0.656654 ... -1 -1 23.7818 21.2524 20.2798 17.084 16.160 15.821 603.740010 482.359215
849 11302 91.740761 24.134195 3508.8205 962.3113 -9.1585 18.6996 5 1 8.872054 ... -1 -1 20.0273 19.0039 18.5734 17.093 16.314 16.133 775.885330 314.887106
10 646 92.395224 23.925249 1019.0106 75.8847 -7.4739 19.8075 1 1 3.453461 ... -1 -1 27.7895 20.7461 19.8923 17.100 16.479 16.163 290.591396 618.699470
443 6928 92.172444 24.062845 1865.9853 653.8223 -7.2032 20.4342 5 1 0.327409 ... -1 -1 23.1401 21.9249 20.7112 17.111 15.867 16.867 443.271339 457.013329
242 4671 92.309559 24.019043 1344.1904 468.6378 -7.7795 19.5673 5 1 0.121186 ... -1 -1 22.3666 21.0417 20.0178 17.159 15.871 15.890 341.306205 520.493032
2142 25122 91.898211 24.442391 2899.8719 2244.1559 -8.6120 19.3283 5 1 9.499136 ... -1 -1 20.7823 19.6665 19.1317 17.159 16.294 16.230 586.156290 70.708725
102 2901 91.794578 23.975685 3309.0041 299.0842 -8.0554 19.5603 5 1 1.434257 ... -1 -1 22.3820 20.4354 19.6159 17.169 16.164 15.706 765.394418 465.389938
2005 23605 91.883039 24.405056 2958.6548 2088.7353 -8.4406 19.1544 5 1 1.720564 ... -1 -1 21.3010 20.0219 19.2301 17.250 16.114 16.063 605.984599 100.987416
1171 15215 92.236122 24.215719 1620.2873 1290.7830 -7.8661 19.5119 5 1 2.204538 ... -1 -1 27.8024 20.7036 19.7339 17.268 16.073 15.924 361.183866 333.399900

1008 rows × 24 columns


In [195]:
with h5py.File("data/k2.h5", "r") as f:
    time = f["time"][:]
    i = np.arange(len(time))[np.isnan(time)][-1] + 100
    time = time[i+1:]
    data = f["frames"][i+1:, :, :]
data[data == 0] = np.nan

In [196]:
m = np.isfinite(time) & np.any(np.isfinite(data), axis=(1, 2))
time = time[m]
data = data[m]

In [206]:
pl.imshow(np.log(data[-1]), cmap="gray", interpolation="nearest")
pl.plot(x, y, ".r", ms=2);



In [67]:
scaled = data  # / np.median(data, axis=0)[None, :, :]

In [68]:
data.shape


Out[68]:
(1724, 550, 800)

In [538]:
X, Y = np.meshgrid(range(scaled.shape[1]), range(scaled.shape[2]), indexing="ij")
m = np.any(np.isfinite(scaled), axis=0)
pred = np.zeros_like(scaled)
count = np.zeros_like(scaled[0], dtype=int)
hw = 5
ex = hw + 10

cen = [258.794798, 272.821687]
xmn, xmx = int(cen[1] - 5), int(cen[1] + 5)
ymn, ymx = int(cen[0] - 4), int(cen[0] + 4)

# xmn, xmx = 575, 585
# ymn, ymx = 515, 525

# for i, j in product(range(0, scaled.shape[1], hw+1), range(0, scaled.shape[1], hw+1)):
for i, j in product(range(xmn, xmx, hw+1), range(ymn, ymx, hw+1)):
    # Select target and predictor pixels.
    window = m & (i - hw <= X) & (X <= i + hw) & (j - hw <= Y) & (Y <= j + hw)
    r2 = (X-i)**2 + (Y-j)**2
    others = m & ((i - ex > X) | (X > i + ex) | (j - ex > Y) | (Y > j + ex)) & (r2 < 25**2)
    print(i, j, window.sum(), others.sum())

    if not (np.any(window) and np.any(others)):
        continue
    
    A = np.concatenate((scaled[:, others], np.ones((len(scaled), 1))), axis=1)
    ATA = np.dot(A.T, A)
    ATA[np.diag_indices_from(ATA)] += 1e-2
    factor = cho_factor(ATA, overwrite_a=True)
    # y = np.concatenate((scaled[:, window], np.zeros((len(emp), window.sum()))), axis=0)
    y = scaled[:, window]
    w = cho_solve(factor, np.dot(A.T, y))

    pred[:, window] += np.dot(A[:len(scaled)], w)
    count[window] += 1


267 254 121 980
267 260 121 980
273 254 121 980
273 260 121 980

In [539]:
pred[:, count > 0] /= count[count > 0]

In [540]:
d = (scaled[:, xmn:xmx, ymn:ymx] - pred[:, xmn:xmx, ymn:ymx])
np.unravel_index(np.argmax(np.abs(d)), d.shape)


Out[540]:
(1649, 9, 2)

In [541]:
n = 100
pl.imshow(data[n, xmn:xmx, ymn:ymx] - pred[n, xmn:xmx, ymn:ymx], cmap="gray", interpolation="nearest")
pl.colorbar()


Out[541]:
<matplotlib.colorbar.Colorbar at 0x14b9f1048>

In [542]:
d = data[:, xmn:xmx, ymn:ymx] - pred[:, xmn:xmx, ymn:ymx]
block = d.reshape((len(d), -1))
# block = block / np.median(block, axis=0) - 1

In [543]:
pl.plot(time, np.sum(block, axis=1), ".k")
# pl.plot(time, np.sum(block, axis=1), ".k")


Out[543]:
[<matplotlib.lines.Line2D at 0x12a5f27b8>]

In [550]:
model = FastICA(n_components=2)
model.fit(block.T)


Out[550]:
FastICA(algorithm='parallel', fun='logcosh', fun_args=None, max_iter=200,
    n_components=2, random_state=None, tol=0.0001, w_init=None,
    whiten=True)

In [551]:
n = len(model.components_)
print(n)


2

In [552]:
pl.figure(figsize=(5, 10))
pl.plot(model.components_.T + 1e-4*np.arange(n));



In [553]:
fig, axes = pl.subplots(1, 2, figsize=(8, 4))
v = model.components_[1]
comp = np.abs((np.dot(v, block)).reshape(d[0].shape))

axes[0].imshow(np.log(data[0, xmn:xmx, ymn:ymx]), cmap="gray_r", interpolation="nearest")
axes[1].imshow(comp, cmap="gray_r", interpolation="nearest")


Out[553]:
<matplotlib.image.AxesImage at 0x143f1bd30>

In [557]:
c = np.dot(v, block)
c /= np.sum(c)

t = time % 5.862149

# y = np.sum(block, axis=1)
# pl.plot(t, y / np.median(np.abs(np.diff(y))), ".g")

y = np.sum(c * block, axis=1)
pl.plot(t, y / np.median(np.abs(np.diff(y))), ".k")


Out[557]:
[<matplotlib.lines.Line2D at 0x12972e358>]

In [556]:
pl.figure()
pl.imshow(d[0], cmap="gray_r", interpolation="nearest")
pl.figure()
pl.imshow(d[0], cmap="gray_r", interpolation="nearest")
pl.imshow(comp, cmap="Blues", interpolation="nearest", alpha=0.5);



In [ ]:


In [ ]: