compare_sex.py 4.36 KB
Newer Older
Jakob Knollmueller's avatar
MAP  
Jakob Knollmueller committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright(C) 2017-2018 Max-Planck-Society
# Author: Jakob Knollmueller
#
# Starblade is being developed at the Max-Planck-Institut fuer Astrophysik

import numpy as np
from astropy.io import fits
from matplotlib import pyplot as plt
import scipy.cluster.vq as sp
import starblade as sb

if __name__ == '__main__':
    #specifying location of the input file:
    path = '../data/hst_05195_01_wfpc2_f702w_pc_sci.fits'
    path = '../data/frame-i-004874-3-0692.fits'
    # path = '../data/frame-i-007812-6-0100.fits'
    # path = '../data/frame-g-002821-6-0141.fits'
    path = '../data/frame-i-000752-1-0432.fits'
    # path = '../data/frame-u-000752-1-0432.fits'
    # path = '../data/frame-i-006174-2-0094.fits'
    # path = 'data/frame-i-004858-1-0480.fits'

    # data = fits.open(path)[1].data
    # data = fits.open(path)[0].data#[750:,1000:]
    ##docker run --rm -i -t --name sex -v ~/Projects/starblade/demos/sextractor:/work chbrandt/sextractor my_data.fits -c default.se
    xx = 450
    yy = int(xx /0.75)
    x0 = 400#1050#770#450
    y0 = 1000#1550#200#1150
    data = fits.open(path)[0].data[x0:x0+xx,y0:y0+yy]
    data_unmod = data.copy()

    # sex = fits.open('check.fits')[0].data[500:500+xx,1200:1200+yy]

    data -= data.min() - 0.001
    # data = 1.-plt.imread('data/sdss.png').T[0]
    # data = fits.open(path)[1].data

    data = data.clip(min=0.0001)
    sex = fits.open('check.fits')[0].data

    hdu = fits.PrimaryHDU(data)
    hdul = fits.HDUList([hdu])
    hdul.writeto('my_data.fits',overwrite=True)

    data = np.ndarray.astype(data, float)
    vmin = np.log(data.min()+0.2)
    vmax = np.log(data.max())*0.4
    plt.gray()
    lin_max = data.max()*0.01
    lin_min=0.01
    plt.imsave('log_data.png', np.log(data),vmin=vmin,vmax=vmax)
    plt.imsave('data.png', (data), vmax = lin_max,vmin =lin_min)
    plt.imsave('sex.png', sex, vmax = lin_max, vmin=lin_min)
    plt.imsave('log_sex.png', np.log(sex), vmax = vmax, vmin=vmin)
    plt.imsave('point_sex.png', (data_unmod - sex), vmax = lin_max,vmin =lin_min)
    plt.imsave('log_point_sex.png', np.log((data_unmod - sex).clip(min=0.0001)), vmax = vmax,vmin =vmin)

    alpha = 1.4
Jakob Knollmueller's avatar
Jakob Knollmueller committed
73
    Starblade = sb.build_starblade(data, alpha=alpha, cg_steps=100, newton_steps=3)
Jakob Knollmueller's avatar
MAP  
Jakob Knollmueller committed
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
    for i in range(10):
        Starblade = sb.starblade_iteration(Starblade, samples=5)

        #plotting on logarithmic scale
        plt.imsave('log_diffuse_component.png', Starblade.s.val, vmin=vmin, vmax=vmax)
        plt.imsave('diffuse_component.png', np.exp(Starblade.s.val), vmin=lin_min, vmax=lin_max)

        plt.imsave('log_pointlike_component.png', Starblade.u.val, vmin=vmin, vmax=vmax)
        plt.imsave('pointlike_component.png', np.exp(Starblade.u.val), vmin=lin_min, vmax=lin_max)

        plt.figure()
        k_lenghts = Starblade.power_spectrum.domain[0].k_lengths
        plt.plot(k_lenghts, Starblade.power_spectrum.val)
        plt.title('power spectrum')
        plt.yscale('log')
        plt.xscale('log')
        plt.ylabel('power')
        plt.xlabel('harmonic mode')
        plt.savefig('power_spectrum.png')
        plt.close('all')
    hdu = fits.PrimaryHDU(np.exp(Starblade.u.val))
    hdul = fits.HDUList([hdu])
    hdul.writeto('my_points.fits',overwrite=True)
    hdu = fits.PrimaryHDU(np.exp(Starblade.s.val))
    hdul = fits.HDUList([hdu])
    hdul.writeto('my_diffuse.fits',overwrite=True)


    star_sex = np.log((data_unmod - sex).clip(min=0.0000000001))
    star_blade = Starblade.u.val
    diffuse_sex = np.log((sex).clip(min=0.0000000001))
    diffuse_blade = Starblade.s.val
    stars = np.empty((2,len(star_blade.flatten())))
    # stars = np.concatenate((star_blade.flatten(),star_sex.flatten()))
    stars[0] = star_blade.flatten()
    stars[1] = star_sex.flatten()
    # stars = sp.whiten(stars)
    sp.kmeans2(stars.T,2,iter=30)