<jupyter_start><jupyter_text>Project 3: Astronomical Source Detection <NAME>------------------------------------------- Part 1 : SEP tutorial  Start by importing numpy as per usual and, this time we will import SEP which helps us detect sources in an image and perform certain calculations<jupyter_code>import numpy as np
import sep<jupyter_output><empty_output><jupyter_text>Instead of using fitsio, I will use astropy which means some adjustments will have to  be made.------------ Will import fits from  astropy.io<jupyter_code>from astropy.io import fits # import fits from astropy.io which will allow us to open fits files
import matplotlib.pyplot as plt # import this to plot whatever we may need to plot
from matplotlib import rcParams # allows us to vary certain parameters that will be used later
%matplotlib inline

rcParams['figure.figsize'] = [10., 8.] # Set the size: do it once here, and never have to do it again in later in the 
                                        #code<jupyter_output><empty_output><jupyter_text>Open the fits file, which was downloaded from SEP GitHub account<jupyter_code>fname = "image.fits" # already in same folder
hdu_list = fits.open(fname)
hdu_list.info()<jupyter_output><empty_output><jupyter_text>Image information typically in PRIMARY block, accessed by indexing hdu_list<jupyter_code>image_data = hdu_list["PRIMARY"].data<jupyter_output><empty_output><jupyter_text>Now we can close FITS file because stored as a variable<jupyter_code>hdu_list.close()<jupyter_output><empty_output><jupyter_text>Showing the Data<jupyter_code># show the image
m, s = np.mean(image_data), np.std(image_data)
plt.imshow(image_data, interpolation='nearest', cmap='gray', vmin=m-s, vmax=m+s, origin='lower')
plt.colorbar();
plt.savefig('skyview.png' , bbox_inches="tight",dpi=600)<jupyter_output><empty_output><jupyter_text>Background subtraction time, taking away outliers, black is the outlier<jupyter_code>bkg = sep.Background(image_data)
print(bkg.globalback)
print(bkg.globalrms)

bkg_image = bkg.back()
# bkg_image = np.array(bkg) # equivalent to above<jupyter_output><empty_output><jupyter_text>Show the Background, as well have save the figure<jupyter_code>plt.imshow(bkg_image, interpolation='nearest', cmap='gray', origin='lower')
plt.colorbar();
plt.savefig('background.png' , bbox_inches="tight",dpi=600)<jupyter_output><empty_output><jupyter_text>Treat background noise as a 2-d array.<jupyter_code># evaluate the background noise as 2-d array, same size as original image
bkg_rms = bkg.rms()
# show the background noise
plt.imshow(bkg_rms, interpolation='nearest', cmap='gray', origin='lower')
plt.colorbar();
plt.savefig('noise.png' , bbox_inches="tight",dpi=600)<jupyter_output><empty_output><jupyter_text>Now we subtract the background<jupyter_code># subtract the background
data_sub = image_data - bkg<jupyter_output><empty_output><jupyter_text>Now we do object detection note that the number "1.5" refers to a sigma value or threshold value<jupyter_code>objects = sep.extract(data_sub, 1.5, err=bkg.globalrms)


len(objects) # how many objects were detected


from matplotlib.patches import Ellipse

# plot background-subtracted image
fig, ax = plt.subplots()
m, s = np.mean(data_sub), np.std(data_sub)
im = ax.imshow(data_sub, interpolation='nearest', cmap='gray',
               vmin=m-s, vmax=m+s, origin='lower')

# plot an ellipse for each object
for i in range(len(objects)):
    e = Ellipse(xy=(objects['x'][i], objects['y'][i]), # gives us the coordinates
                width=6*objects['a'][i],
                height=6*objects['b'][i],
                angle=objects['theta'][i] * 180. / np.pi)
    e.set_facecolor('none')
    e.set_edgecolor('red')
    ax.add_artist(e)
    
plt.savefig('tutorialfinal.png' , bbox_inches="tight",dpi=600) # saves the figure<jupyter_output><empty_output><jupyter_text>The objects function has several uses/information which can be extracted and displayed<jupyter_code># available fields
objects.dtype.names<jupyter_output><empty_output><jupyter_text>Now we will show/calculate the fluxes of 9 objects in the image<jupyter_code>flux, fluxerr, flag = sep.sum_circle(data_sub, objects['x'], objects['y'],
                                     3.0, err=bkg.globalrms, gain=1.0)


# show the first 10 objects results:
for i in range(10):
    print("object {:d}: flux = {:f} +/- {:f}".format(i, flux[i], fluxerr[i]))<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.12556458897922312, 'fraction_numerical': 0.008355916892502258, 'mean_word_length': 5.075445816186557, 'pattern_counts': {'":': 0, '<': 54, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '506697', 'license': 'MIT', 'n_tokens_mistral': 1523, 'n_tokens_neox': 1448, 'n_words': 387, 'path': 'astr-119-project-3-tutorial.ipynb', 'repo_name': 'ericgc10/astr-119-project-3', 'size': '4442'}