Skip to content
Snippets Groups Projects
Commit aff75b05 authored by dardela's avatar dardela
Browse files

Notebooks auxiliar

parent 036c2644
No related branches found
No related tags found
No related merge requests found
%% Cell type:code id:b6649867 tags:
``` python
from astropy.io import fits
import matplotlib.pyplot as plt
import numpy as np
import glob
import cv2
from PIL import Image
import copy
import os
```
%% Cell type:code id:ed76de12 tags:
``` python
ruta_fits = "RUTA_INPUT_FITS/"
ruta_guardar_mascaras_2_clases="RUTA_OUTPUT_MASCARAS/"
tam_fotos_x=512
tam_fotos_y=512
```
%% Cell type:code id:4ee87765 tags:
``` python
if not os.path.exists(ruta_guardar_mascaras_2_clases):
os.makedirs(ruta_guardar_mascaras_2_clases)
```
%% Cell type:markdown id:187a3d92 tags:
## Creamos máscaras de segmentación con 2 clases y de 3 clases
%% Cell type:code id:62e367b8 tags:
``` python
def get_numero_galaxia(nombre_galaxia:str):
numero_galaxia = nombre_galaxia.split('.')[0].split('_')[-1]
return numero_galaxia
```
%% Cell type:code id:25dc0528 tags:
``` python
#Leemos las galaxy convolved y las stream convolved
rutas_stream_convolved = glob.glob(ruta_fits+"stream_pix_above_surf_bright_limit_*.fits")
for ruta_stream in rutas_stream_convolved:
print(ruta_stream)
numero_galaxia = get_numero_galaxia(ruta_stream)
print(numero_galaxia)
#Hacemos el mismo proceso para la imagen de la cola de marea
stream_fits = fits.open(ruta_stream)
array_imagen_stream_fits = stream_fits[1].data
mascara_cola = np.zeros((tam_fotos_x,tam_fotos_y))
mascara_cola[array_imagen_stream_fits!=0] = 1
#Combinamos las mascaras
#Guardamos la mascara de la cola
cv2.imwrite(ruta_guardar_mascaras_2_clases+"mascara_"+str(numero_galaxia)+".png",mascara_cola)
#Mostramos la mascara
plt.imshow(mascara_cola,interpolation='none', origin="lower")
plt.show()
```
import glob
from astropy.io import fits
import numpy as np
import pandas as pd
import montage_wrapper as montage
import os.path
import pdb
input_folder = "./INPUT_FOLDER/"
output_folder = "./OUTPUT_FOLDER/"
vis_path = "./"
vis_image = "EUC_VIS_SWL-STK-000-000000-0000000__20210613T225647.742318Z.fits"
#I open the VIS image
hdu = fits.open(vis_path+vis_image)
img = hdu[1].data
hdr = hdu[1].header
dim1 = hdr["NAXIS1"]
dim2 = hdr["NAXIS2"]
if not os.path.exists(output_folder):
os.mkdir(output_folder)
#these are the only files I will accept as inputs
filename_sims = glob.glob(input_folder+"galaxy_and_stream_convolved_[!w]*.fits") #in the termina use ^ instead of !
for filename_sim in filename_sims:
print(filename_sim)
#I open each of the simulated galaxies
hdu_sim = fits.open(filename_sim)
img_sim = hdu_sim[1].data
hdr_sim = hdu_sim[1].header
dim1_sim = hdr_sim["NAXIS1"]
dim2_sim = hdr_sim["NAXIS2"]
flag_cutted = False
while flag_cutted == False:
#I obtain the new random centroid within the VIS image
x_center = np.random.randint(0, dim1)
y_center = np.random.randint(0, dim2)
#I will take a cutout in the new centroid position with the dimensions of the simulated galaxy
pix_in_x_halfsize = int(dim1_sim/2)
pix_in_y_halfsize = int(dim2_sim/2)
cutout = img[y_center-pix_in_y_halfsize:y_center+pix_in_y_halfsize, x_center-pix_in_x_halfsize:x_center+pix_in_x_halfsize]
#I will accept this new image if less than 10% of the pixels are zeros and the dimensions are all right
if np.count_nonzero(cutout == 0) >= (dim1_sim*dim2_sim)/10 or cutout.shape[0] != dim1_sim or cutout.shape[1] != dim2_sim:
flag_cutted = False
else:
flag_cutted = True
#I create the cutout itself
new_filename = output_folder+filename_sim[len(input_folder):-5]+"_in_"+vis_image[:-5]+".fits"
montage.mSubimage_pix(vis_path+vis_image,new_filename, x_center-pix_in_x_halfsize, y_center-pix_in_y_halfsize, hdu = 1, xpixsize = (pix_in_x_halfsize*2)-1, ypixsize = (pix_in_y_halfsize*2)-1 )
hdu_cutout = fits.open(new_filename)
img_cutout = hdu_cutout[0].data
hdr_cutout = hdu_cutout[0].header
fits.writeto(new_filename,img_cutout+img_sim,hdr_cutout,overwrite=True)
\ No newline at end of file
File added
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment