import numpy as np
import matplotlib.pyplot as plt

from properimage import single_image as s

%matplotlib inline

pixel = np.random.random((128,128))*5.
# Add some stars to it
star = [[35, 38, 35],
        [38, 90, 39],
        [35, 39, 34]]
for i in range(25):
    x, y = np.random.randint(120, size=2)
    pixel[x:x+3,y:y+3] = star

mask = np.random.randint(2, size=(128,128)) 
for i in range(10):
    mask = mask & np.random.randint(2, size=(128,128))
    
img  = s.SingleImage(pixel, mask)

"""
Explanation: Tutorial - Part #1 - The SingleImage Class
In this first tutorial the basics of the methods and properties of the
SingleImage class are explained.
This object class represents the basic unit of data which we will manipulate.
Basically it starts containing pixel data, with its mask.
First lets create an instance of this class with a numpy array.
End of explanation
"""


print(img)

"""
Explanation: We can see that the img object created automatically produces an output
displaying the number of sources found.
This just accounts for sources good enough for PSF estimation, which is
the first step for any processing ProperImage is intended for.
If we try to print the instance, (or obtain the representation output) we find
that the explicit origin of the data is being displayed
End of explanation
"""


img.data

"""
Explanation: If you would like to acces the data inside the object img just ask for data.
End of explanation
"""


plt.figure(figsize=(6,6))
plt.imshow(img.data, cmap='Greys')

"""
Explanation: As can be seen it is a numpy masked array, with bad pixels flagged.
End of explanation
"""


img.best_sources[['x', 'y', 'cflux']]

"""
Explanation: We can check the best sources extracted.
End of explanation
"""


a_fields, psf_basis = img.get_variable_psf()

"""
Explanation: And also obtain the estimation of PSF.
As the PSF may vary across the field, we use the method described in Lauer T. (2002) for spatially variant PSF.
The methodology returns a series of image-sized coefficients $a_i$ and a series of basis PSF elements $p_i$.
End of explanation
"""


len(psf_basis), psf_basis[0].shape

a_fields

"""
Explanation: As in our simple example we don't vary the PSF we obtain only a PSF element, and a None coefficient.
End of explanation
"""


plt.imshow(psf_basis[0])

"""
Explanation: We may check the looks of the psf_basis single element.
End of explanation
"""


