Beispiel #1
0
def save_asymmetries(filt='f160w'):

    HFF = Table.read('output/tables/nbCGs.fits')

    asymmetries = []
    for cluster, ID in zip(HFF['cluster'], HFF['ID']):
        sci = core.open_cutout('{}/cutouts/{}_ID_{}_{}.fits'.format(
            cluster, cluster, ID, filt),
                               simple=True)

        err = core.open_cutout('{}/cutouts/{}_ID_{}_{}_noise.fits'.format(
            cluster, cluster, ID, filt),
                               simple=True)

        segmap = core.open_cutout('{}/cutouts/{}_ID_{}_segmap.fits'.format(
            cluster, cluster, ID),
                                  simple=True)

        morph = statmorph.source_morphology(sci,
                                            segmap,
                                            weightmap=err,
                                            label=ID)
        asymmetries.append(morph.asymmetry)

    HFF['asymmetry'] = asymmetries
    HFF.write('output/tables/nbCGs_asymmetries_NEW.fits')

    return
Beispiel #2
0
def test1():
    """
    Check values for a randomly chosen galaxy.
    """
    curdir = os.path.dirname(__file__)
    hdulist = fits.open('%s/data_slice.fits' % (curdir))
    image = hdulist[0].data
    segmap = hdulist[1].data
    mask = np.bool8(hdulist[2].data)
    gain = 1.0
    source_morphs = statmorph.source_morphology(image,
                                                segmap,
                                                mask=mask,
                                                gain=gain)
    morph = source_morphs[0]

    #for key in correct_values:
    #    print("'%s':%.14f," % (key, morph[key]))

    # Check results
    assert morph['flag'] == 0
    assert morph['flag_sersic'] == 0
    for key in correct_values:
        value = morph[key]
        value0 = correct_values[key]
        relative_error = np.abs((value - value0) / value0)
        assert relative_error < 1e-6
Beispiel #3
0
 def test_no_psf(self, print_values=False):
     source_morphs = statmorph.source_morphology(self.image,
                                                 self.segmap,
                                                 mask=self.mask,
                                                 gain=self.gain)
     morph = source_morphs[0]
     for key in self.correct_values:
         assert_allclose(morph[key],
                         self.correct_values[key],
                         err_msg="%s value did not match." % (key, ))
         if print_values:
             print("'%s': %.14g," % (key, morph[key]))
Beispiel #4
0
 def test_psf(self):
     # Try delta-like PSF, which should give the same results as no PSF.
     psf = np.array([[0, 0, 0], [0, 1, 0], [0, 0, 0]], dtype=np.float64)
     source_morphs = statmorph.source_morphology(self.image,
                                                 self.segmap,
                                                 mask=self.mask,
                                                 gain=self.gain,
                                                 psf=psf)
     morph = source_morphs[0]
     for key in self.correct_values:
         assert_allclose(morph[key],
                         self.correct_values[key],
                         err_msg="%s value did not match." % (key, ))
def test_make_figure():
    curdir = os.path.dirname(__file__)
    hdulist = fits.open('%s/../../tests/data/slice.fits' % (curdir, ))
    image = hdulist[0].data
    segmap = hdulist[1].data
    mask = np.bool8(hdulist[2].data)
    gain = 1.0
    source_morphs = statmorph.source_morphology(image,
                                                segmap,
                                                mask=mask,
                                                gain=gain)
    morph = source_morphs[0]
    fig = make_figure(morph)
    assert isinstance(fig, matplotlib.figure.Figure)
Beispiel #6
0
 def test_weightmap(self):
     # Manually create weight map instead of using the gain argument.
     weightmap = np.sqrt(
         np.abs(self.image) / self.gain +
         self.correct_values['sky_sigma']**2)
     source_morphs = statmorph.source_morphology(self.image,
                                                 self.segmap,
                                                 mask=self.mask,
                                                 weightmap=weightmap)
     morph = source_morphs[0]
     for key in self.correct_values:
         assert_allclose(morph[key],
                         self.correct_values[key],
                         err_msg="%s value did not match." % (key, ))
Beispiel #7
0
def get_morphology_measures(
        image: torch.Tensor,
        gain: float = 10000.0,  # assume average of 10,000 electrons per pixel
        silent: bool = True):
    """ return the morphology measures of a galaxy in an RGB image """
    if len(image.shape) == 4:
        image = image[0]
    image = image.transpose(0, 2)
    image = image.detach().cpu().numpy()
    image_bw = RGB2BW(image)
    segmap = get_segmentation_map(image_bw)
    with ExitStack() as stack:
        if silent:
            stack.enter_context(redirect_stdout(StringIO()))
            stack.enter_context(redirect_stderr(StringIO()))
        morphology_measures = statmorph.source_morphology(image_bw,
                                                          segmap,
                                                          gain=gain)
    return morphology_measures[0]
Beispiel #8
0
    def run_statmorph(self,
                      flux_type="flux_ellip",
                      rad_type="rhalf_ellip",
                      overwrite=False):
        '''
        Requires: gain, mask, psf, segmap
        '''
        assert_properties(["gain", "zp", "exptime"], self.data)
        assert_data(["img", "segmap", "mask", "psf"], self.data)
        if not clear_overwrite("statmorph", self.data, overwrite): return

        morphology = statmorph.source_morphology(
            image=self.data["img"][()] - self.data.attrs["sky_mean"],
            segmap=self.data["segmap"][()],
            mask=np.array(self.data["mask"][()], dtype=bool),
            psf=self.data["psf"][()],
            gain=self.data.attrs["gain"])[0]

        output = _morphology_dict(morphology,
                                  self.survey,
                                  self.data.attrs["zp"],
                                  self.data.attrs["exptime"],
                                  self.data.attrs["pxscale"],
                                  flux_type=flux_type,
                                  rad_type=rad_type)

        #### Save the diagnostic figure as RGB array
        diag_fig = sm_make_figure(morphology)
        canvas = FigureCanvas(diag_fig)
        canvas.draw()
        diag_fig_arr = np.fromstring(canvas.tostring_rgb(), dtype='uint8')
        diag_fig_arr = diag_fig_arr.reshape(
            diag_fig.canvas.get_width_height()[::-1] + (3, ))
        plt.close()

        #### Store in the node
        self.data["statmorph"] = diag_fig_arr
        for key, value in output.items():
            self.data["statmorph"].attrs[key] = value
Beispiel #9
0
plt.show()
#%%
# Keep only the largest segment
label = np.argmax(segm.areas) + 1
segmap = segm.data == label
plt.imshow(segmap, origin='lower', cmap='gray')
plt.show()

segmap_float = ndi.uniform_filter(np.float64(segmap), size=10)
segmap = segmap_float > 0.5
plt.imshow(segmap, origin='lower', cmap='gray')
plt.show()

#%%Running statmorph
start = time.time()
source_morphs = statmorph.source_morphology(image, segmap, gain=gain, psf=psf)
print('Time: %g s.' % (time.time() - start))

morph = source_morphs[0]

print('xc_centroid =', morph.xc_centroid)
print('yc_centroid =', morph.yc_centroid)
print('ellipticity_centroid =', morph.ellipticity_centroid)
print('elongation_centroid =', morph.elongation_centroid)
print('orientation_centroid =', morph.orientation_centroid)
print('xc_asymmetry =', morph.xc_asymmetry)
print('yc_asymmetry =', morph.yc_asymmetry)
print('ellipticity_asymmetry =', morph.ellipticity_asymmetry)
print('elongation_asymmetry =', morph.elongation_asymmetry)
print('orientation_asymmetry =', morph.orientation_asymmetry)
print('rpetro_circ =', morph.rpetro_circ)
def statmorphWrapper(index_pairs):
    
    df = pd.read_csv('NGVSgalaxies.csv')

    # Iterate through rows in csv file containing measurements for each galaxy
    for row in df.iloc[index_pairs[0]:index_pairs[1]].itertuples(index=True, name='Pandas'):
        galaxy = row.Official_name
        base = 'https://www.cadc-ccda.hia-iha.nrc-cnrc.gc.ca/files/vault/ngvs/data/NGVS/galaxies/'
        galaxyPath = f'{galaxy}/{galaxy}_G'

        # Create check file for current galaxy
        print(f'checking {galaxy}')
        writeFile = open(f'/mnt/scratch/check/{galaxy}.txt', 'w') 
        writeFile.write('checking')
        writeFile.close()


        startcopy = time.time()
        # Try to copy galaxy files
        os.system(f'vcp vos:ngvs/data/NGVS/galaxies/{galaxy}/{galaxy}_G.fits /mnt/scratch/temp_galaxy_storage')
        os.system(f'vcp vos:ngvs/data/NGVS/galaxies/{galaxyPath}_iso_model.fits /mnt/scratch/temp_galaxy_storage')
        os.system(f'vcp vos:ngvs/data/NGVS/galaxies/{galaxyPath}_galfit_model.fits /mnt/scratch/temp_galaxy_storage')
        os.system(f'vcp vos:ngvs/data/NGVS/galaxies/{galaxyPath}_mask.fits /mnt/scratch/temp_galaxy_storage')
        os.system(f'vcp vos:ngvs/data/NGVS/galaxies/{galaxyPath}_psf.fits /mnt/scratch/temp_galaxy_storage')
        os.system(f'vcp vos:ngvs/data/NGVS/galaxies/{galaxyPath}_sig.fits /mnt/scratch/temp_galaxy_storage')
        os.system(f'vcp vos:ngvs/data/NGVS/galaxies/{galaxyPath}_iso_residual.fits /mnt/scratch/temp_galaxy_storage')
        os.system(f'vcp vos:ngvs/data/NGVS/galaxies/{galaxyPath}_galfit_residual.fits /mnt/scratch/temp_galaxy_storage')
        endcopy = time.time() - startcopy

        # If one of the required files is missing, continue to next galaxy                                                           
        if(not path.isfile(f'/mnt/scratch/temp_galaxy_storage/{galaxy}_G.fits') or (not path.isfile(f'/mnt/scratch/temp_galaxy_storage/{galaxy}_G_iso_model.fits') and not \
        path.isfile(f'/mnt/scratch/temp_galaxy_storage/{galaxy}_G_galfit_model.fits')) or not path.isfile(f'/mnt/scratch/temp_galaxy_storage/{galaxy}_G_mask.fits') or not \
        path.isfile(f'/mnt/scratch/temp_galaxy_storage/{galaxy}_G_psf.fits') or not path.isfile(f'/mnt/scratch/temp_galaxy_storage/{galaxy}_G_sig.fits') or (not \
        path.isfile(f'/mnt/scratch/temp_galaxy_storage/{galaxy}_G_iso_residual.fits') and not path.isfile(f'/mnt/scratch/temp_galaxy_storage/{galaxy}_G_galfit_residual.fits'))):
            print(f'missing {galaxy}')
            writeFile = open(f'/mnt/scratch/missing/{galaxy}.txt', 'w')     
            writeFile.write(f'{galaxy}\n')
            if(not path.isfile(f'/mnt/scratch/temp_galaxy_storage/{galaxy}_G.fits')):
                writeFile.write('missing original\n')
            if(not path.isfile(f'/mnt/scratch/temp_galaxy_storage/{galaxy}_G_iso_model.fits')):
                writeFile.write('missing iso model\n')
            if(not path.isfile(f'/mnt/scratch/temp_galaxy_storage/{galaxy}_G_galfit_model.fits')):
                writeFile.write('missing galfit model\n')
            if(not path.isfile(f'/mnt/scratch/temp_galaxy_storage/{galaxy}_G_mask.fits')):
                writeFile.write('missing mask\n')
            if(not path.isfile(f'/mnt/scratch/temp_galaxy_storage/{galaxy}_G_psf.fits')):
                writeFile.write('missing psf\n')
            if(not path.isfile(f'/mnt/scratch/temp_galaxy_storage/{galaxy}_G_sig.fits')):
                writeFile.write('missing sig\n')
            if(not path.isfile(f'/mnt/scratch/temp_galaxy_storage/{galaxy}_G_iso_residual.fits')):
                writeFile.write('missing iso residual\n')
            if(not path.isfile(f'/mnt/scratch/temp_galaxy_storage/{galaxy}_G_galfit_residual.fits')):
                writeFile.write('missing galfit residual\n')
            
            writeFile.close()
            clearTempFiles(galaxy)
            continue

        # ONLY PROCESS SMALLER FILES
        #---------------------------------------------------------------------------------------
        if(os.path.getsize(f'/mnt/scratch/temp_galaxy_storage/{galaxy}_G.fits') >= 300000000):
            writeFile = open(f'/mnt/scratch/largefile/{galaxy}.txt', 'w')     
            writeFile.write(f'{galaxy}\n')
            writeFile.close()
            clearTempFiles(galaxy)
            continue
        #---------------------------------------------------------------------------------------

        # If any of the galaxy files are empty then create galaxy corrupt file and continue to next galaxy
        if(checkCorrupt(galaxy)):
            writeFile = open(f'/mnt/scratch/corrupt/{galaxy}.txt', 'w')     
            writeFile.write(f'corrupt\n')
            writeFile.close()
            clearTempFiles(galaxy)
            continue

        # Beginning of segmentation map creation

        startseg = time.time()

        hdu = fits.open(f'/mnt/scratch/temp_galaxy_storage/{galaxy}_G.fits')
        im_header = hdu[0].header
        im_data = hdu[0].data

         # Sky subtraction from original image
        sky_data = np.zeros(np.shape(im_data))
        sky_data += row.SKY
        im_sky_subtracted = im_data - sky_data

        # Calculate nucleus center
        ra = row.NGVS_ra
        dec = row.NGVS_dec
        mywcs = wcs.WCS(im_header)
        xCenter, yCenter = mywcs.all_world2pix([[ra, dec]], 0)[0]

        mask_data = fits.getdata(f'/mnt/scratch/temp_galaxy_storage/{galaxy}_G_mask.fits')

        # If a iso model exists then use that file for the original model data, otherwise use galfit
        if path.isfile(f'/mnt/scratch/temp_galaxy_storage/{galaxy}_G_iso_model.fits'):
            original_model_data = fits.getdata(f'/mnt/scratch/temp_galaxy_storage/{galaxy}_G_iso_model.fits')
        else:
            original_model_data = fits.getdata(f'/mnt/scratch/temp_galaxy_storage/{galaxy}_G_galfit_model.fits')

        # If nucleus exists then mask nucleus
        if(row.Nuc_Flag == 1):
            for i in range(len(mask_data)):
                for j in range(len(mask_data)):
                    # TODO: Change radius of nucleus mask
                    if(((i-xCenter)**2) + ((j-yCenter)**2) <= (5**2)):
                        mask_data[i][j] = 100
                    
        
        ellipse_data = np.zeros(np.shape(original_model_data))
        # Calculate median of original model values within 10 pixel radius from nucleus center
        pixelList = []
        for i in range(len(original_model_data)):
            for j in range(len(original_model_data)):
                    if(((i-xCenter)**2) + ((j-yCenter)**2) <= (10**2) and mask_data[i][j] != 100):
                        pixelList.append(original_model_data[i][j])   

        median = statistics.median(pixelList)

        # Create Segmentation Map
        seg_data = np.zeros(np.shape(original_model_data))
        ellipse_data = np.zeros(np.shape(original_model_data))
        
        # isEmpty flag is used for checking if a segmentation map is valid for processing. If segmentation map 2D list is all 0's then script crashes.
        isEmpty = True

        # if median is greater than 2*sky value then create segmentation map from original model values greater than 1.4*sky value within ellipse area
        if(median > 2*row.SKY):
            for i in range(len(original_model_data)):   
                for j in range(len(original_model_data)):
                        if(inEllipse(i,j,xCenter,yCenter,row.Size,row.AxisRatio,row.PA)):
                            ellipse_data[i][j] = 100
                            if(original_model_data[i][j] > (1.4*row.SKY)):
                                seg_data[i][j] = 100
                                isEmpty = False
        # If median is less than 2*sky value then create segmentation map from original model values greater than 1.1*sky value within ellipse area
        else:
            for i in range(len(original_model_data)):
                for j in range(len(original_model_data)):
                        if(inEllipse(i,j,xCenter,yCenter,row.Size,row.AxisRatio,row.PA)):
                            ellipse_data[i][j] = 100
                            if(original_model_data[i][j] > (1.1*row.SKY)):
                                seg_data[i][j] = 100
                                isEmpty = False

        psf = fits.getdata(f'/mnt/scratch/temp_galaxy_storage/{galaxy}_G_psf.fits')
        weightmap = fits.getdata(f'/mnt/scratch/temp_galaxy_storage/{galaxy}_G_sig.fits')
        mask_data = np.array(mask_data, dtype=bool)

        endseg = time.time() - startseg
 
        # End of segmentation map creation


        # If the galaxy's segmentation map is empty with no area of interest, then create empty galaxy file and continue to next galaxy
        if(isEmpty):    
            writeFile = open(f'/mnt/scratch/emptyseg/{galaxy}.txt', 'w')     
            writeFile.write('empty')
            writeFile.close()
            clearTempFiles(galaxy)
            continue

        start_time = time.time()

        # run statmorph on current galaxy
        source_morphs = statmorph.source_morphology(im_sky_subtracted, seg_data, mask=mask_data, weightmap=weightmap, psf=psf)
        end_time = time.time() - start_time

        morph = source_morphs[0]

        startmodelcreate = time.time()

        # create model from statmorph results
        ny, nx = im_sky_subtracted.shape
        y, x = np.mgrid[0:ny, 0:nx]
        fitted_model = statmorph.ConvolvedSersic2D(
            amplitude=morph.sersic_amplitude,
            r_eff=morph.sersic_rhalf,
            n=morph.sersic_n,
            x_0=morph.sersic_xc,
            y_0=morph.sersic_yc,
            ellip=morph.sersic_ellip,
            theta=morph.sersic_theta)
        fitted_model.set_psf(psf)
        output_model_data = fitted_model(x, y)

        endmodelcreate = time.time() - startmodelcreate
        
        if path.isfile(f'/mnt/scratch/temp_galaxy_storage/{galaxy}_G_iso_residual.fits'):
            original_res_data = fits.getdata(f'/mnt/scratch/temp_galaxy_storage/{galaxy}_G_iso_residual.fits')
        else:
            original_res_data = fits.getdata(f'/mnt/scratch/temp_galaxy_storage/{galaxy}_G_galfit_residual.fits')


        startfig = time.time()

        # normalize images, models, segmentation map
        output_res_data = im_sky_subtracted - output_model_data
        p1 = 10. ; p2 = 90.

        im_p1 = np.percentile(im_sky_subtracted.ravel(), p1)
        im_p2 = np.percentile(im_sky_subtracted.ravel(), p2)
        normSky = ImageNormalize(im_sky_subtracted, vmin=im_p1, vmax=im_p2)

        im_p1 = np.percentile(output_model_data.ravel(), p1)
        im_p2 = np.percentile(output_model_data.ravel(), p2)
        normOutputMod = ImageNormalize(output_model_data, vmin=im_p1, vmax=im_p2)

        im_p1 = np.percentile(original_model_data.ravel(), p1)
        im_p2 = np.percentile(original_model_data.ravel(), p2)
        normOriginalMod = ImageNormalize(original_model_data, vmin=im_p1, vmax=im_p2)

        im_p1 = np.percentile(output_res_data.ravel(), p1)
        im_p2 = np.percentile(output_res_data.ravel(), p2)
        normOutputRes = ImageNormalize(output_res_data, vmin=im_p1, vmax=im_p2)

        im_p1 = np.percentile(original_res_data.ravel(), p1)
        im_p2 = np.percentile(original_res_data.ravel(), p2)
        normOriginalRes = ImageNormalize(original_res_data, vmin=im_p1, vmax=im_p2)

        # create figures for images, models, segmentation map
        gs = gridspec.GridSpec(2, 4, width_ratios=[1, 1, 1, 1],
         wspace=0.2, hspace=0, top=0.7, bottom=0.05, left=0.1, right=0.5)

        fig = plt.figure(figsize=(30,10))

        ax= plt.subplot(gs[0,0])
        ax.imshow(im_sky_subtracted, norm=normSky, cmap='gray', origin='lower')
        ax.set_title('Sky Subtracted Image', fontsize=15)

        ax= plt.subplot(gs[0,1])
        ax.imshow(original_model_data, norm=normOriginalMod, cmap='gray', origin='lower')
        ax.set_title('Original Model', fontsize=15)

        ax= plt.subplot(gs[0,2])
        ax.imshow(output_model_data, norm=normOutputMod, cmap='gray', origin='lower')
        ax.set_title('Output Model', fontsize=15)

        ax= plt.subplot(gs[0,3])
        ax.imshow(mask_data, cmap='gray', origin='lower')
        ax.set_title('Mask', fontsize=15)

        ax= plt.subplot(gs[1,0])
        ax.imshow(seg_data, cmap='gray', origin='lower')
        ax.set_title('Segmap', fontsize=15)

        ax= plt.subplot(gs[1,1])
        ax.imshow(ellipse_data, cmap='gray', origin='lower')
        ax.set_title('Ellipse Area', fontsize=15)

        ax= plt.subplot(gs[1,2])
        ax.imshow(original_res_data, norm=normOriginalRes, cmap='gray', origin='lower')
        ax.set_title('Original Residual', fontsize=15)

        ax= plt.subplot(gs[1,3])
        ax.imshow(output_res_data, norm=normOutputRes, cmap='gray', origin='lower')
        ax.set_title('Output Residual', fontsize=15)

        endfig = time.time() - startfig

        # save figures as PNG image to output directory
        fig.savefig(f'/mnt/scratch/output/{galaxy}_sourcemorph:{round(end_time, 2)}_seg={round(endseg, 2)}_RE={round(row.Size, 3)}_mag={round(row.principleg_mag_cg, 3)}.png', facecolor='w', edgecolor='w', transparent=False, bbox_inches='tight')
        plt.close(fig)

        
        # UNCOMMENT TO SAVE AS MULTI EXTENSION FITS FILE INSTEAD OF PNG 
        #------------------------------------------------------------------------------------------------------------------------ 
        # primary_hdu = fits.PrimaryHDU(im_sky_subtracted, header=im_header)
        # image_hdu = fits.ImageHDU(output_model_data)
        # image_hdu2 = fits.ImageHDU(output_res_data)
        # hdul = fits.HDUList([primary_hdu, image_hdu, image_hdu2])

        # upload fits file to VOSpace
        # hdul.writeto(f'/mnt/scratch/output/{galaxy}_output.fits', overwrite=True)
        # os.system(f'vcp /mnt/scratch/output/{galaxy}_output.fits vos:ngvs/data/STATMORPH/FITS_output/{galaxy}_output.fits')
        # if path.isfile(f'/mnt/scratch/output/{galaxy}_output.fits'):
        #    os.system(f'rm /mnt/scratch/output/{galaxy}_output.fits')
        #------------------------------------------------------------------------------------------------------------------------
        
        # UPLOAD PNG FILE WITH FLAGS
        # os.system(f'vcp /mnt/scratch/output/{galaxy}_time:{round(end_time, 2)}_Flag={morph.flag}_SersicFlag={morph.flag_sersic}.png \
        # vos:ngvs/data/STATMORPH/filesize_bug/{galaxy}_time:{round(end_time, 2)}_Flag={morph.flag}_SersicFlag={morph.flag_sersic}.png')
        # if path.isfile(f'/mnt/scratch/output/{galaxy}_time:{round(end_time, 2)}_Flag={morph.flag}_SersicFlag={morph.flag_sersic}.png'):
        #    os.system(f'rm /mnt/scratch/output/{galaxy}_time:{round(end_time, 2)}_Flag={morph.flag}_SersicFlag={morph.flag_sersic}.png')


        # UPLOAD PNG FILE WITH MEDIAN & SKY VALUES
        # os.system(f'vcp /mnt/scratch/output/{galaxy}_time:{round(end_time, 2)}_size={row.Size}_median={median}_2*sky={2*row.SKY}sky={row.SKY}.png \
        # vos:ngvs/data/STATMORPH/memory_bug/{galaxy}_time:{round(end_time, 2)}_size={row.Size}_median={median}_2*sky={2*row.SKY}sky={row.SKY}.png')
        # if path.isfile(f'/mnt/scratch/output/{galaxy}_time:{round(end_time, 2)}_size={row.Size}_median={median}_2*sky={2*row.SKY}sky={row.SKY}.png'):
        #    os.system(f'rm /mnt/scratch/output/{galaxy}_time:{round(end_time, 2)}_size={row.Size}_median={median}_2*sky={2*row.SKY}sky={row.SKY}.png')

        # UPLOAD PNG FILE WITH RUNNING TIMES & RE FACTOR & MAGNITUDE
        os.system(f'vcp /mnt/scratch/output/{galaxy}_sourcemorph:{round(end_time, 2)}_seg={round(endseg, 2)}_RE={round(row.Size, 3)}_mag={round(row.principleg_mag_cg, 3)}.png \
        vos:ngvs/data/STATMORPH/dec17/{galaxy}_sourcemorph:{round(end_time, 2)}_seg={round(endseg, 2)}_RE={round(row.Size, 3)}_mag={round(row.principleg_mag_cg, 3)}.png')
        if path.isfile(f'/mnt/scratch/output/{galaxy}_sourcemorph:{round(end_time, 2)}_seg={round(endseg, 2)}_RE={round(row.Size, 3)}_mag={round(row.principleg_mag_cg, 3)}.png'):
            os.system(f'rm /mnt/scratch/output/{galaxy}_sourcemorph:{round(end_time, 2)}_seg={round(endseg, 2)}_RE={round(row.Size, 3)}_mag={round(row.principleg_mag_cg, 3)}.png')

        hdu.close()
        clearTempFiles(galaxy)
        print(f'complete {galaxy}')
Beispiel #11
0
    sigma_clip = SigmaClip(sigma=3., iters=10)
    bkg_estimator = MedianBackground()
    try:
        bkg = Background2D(
            camera_data, (np.shape(camera_data)[0], np.shape(camera_data)[1]),
            edge_method='pad',
            filter_size=(3, 3),
            sigma_clip=sigma_clip,
            bkg_estimator=bkg_estimator)
    except ValueError:
        continue

    # call statmorph (cite Vicente Rodriguez-Gomez)
    source_morphs = statmorph.source_morphology(
        camera_data, segmap, weightmap=camera_data_sigma,
        skybox_size=20)  #,weightmap=camera_data_sigma)#psf=psf,
    try:
        morph = source_morphs[0]
    except IndexError:
        print('STATMORPH FAIL')
        continue

    morph = source_morphs[0]

    if morph.sn_per_pixel < 2.5:
        # This is how to weed out galaxies with too low of S/N per pixel:
        # Vicente says that below a certain level the predictors got wonky and I found
        # this to be true at <S/N> < 2.5
        continue
    if morph.flag == 1:
def main(calculate_morphologies=False, download=False):

    import matplotlib.pyplot as plt

    GAMA = Table.read(GAMA_path)

    RAs = GAMA['RA']
    Decs = GAMA['DEC']
    redshifts = GAMA['Z']

    # scale = cosmo.arcsec_per_kpc_proper(redshifts)*scale_of_interest/pixel_scale
    # determine number of pixels that correspond to 15 kpc in projected
    # size, to highlight AGN driven features like bubbles and cavities
    # kpc_per_pixel = scale_of_interest/scale

    files = download_images(RAs, Decs, download=download)

    if calculate_morphologies == True:
        asymmetries = []
        smoothnesses = []
        ginis = []
        M20s = []
        for i in range(len(files)):
            try:
                with fits.open('SDSS_images/' + files[i]) as hdu:
                    image = hdu[0].data
                    image[image < 0] = 0
                    sigma = np.sqrt(image)
                    # plt.imshow(image)

                    threshold = photutils.detect_threshold(image, 1.5)
                    npixels = 5
                    segm = photutils.detect_sources(image, threshold, npixels)
                    label = np.argmax(segm.areas) + 1
                    segmap = segm.data == label
                    segmap_float = ndimage.uniform_filter(np.float64(segmap),
                                                          size=10)
                    segmap = segmap_float > 0.5
                    # plt.imshow(segmap, origin='lower', cmap='gray')

                    source_morphs = statmorph.source_morphology(
                        image, segmap, weightmap=sigma)
                    morph = source_morphs[0]
                    # params = [morph.gini, morph.m20, morph.asymmetry, morph.smoothness]
                    asymmetries.append(morph.asymmetry)
                    smoothnesses.append(morph.smoothness)
                    ginis.append(morph.gini)
                    M20s.append(morph.m20)

                    from statmorph.utils.image_diagnostics import make_figure
                    make_figure(morph,
                                'SDSS_images/PNGs/' + files[i][:-5] + '.png')
            except:
                asymmetries.append(-99)
                smoothnesses.append(-99)
                ginis.append(-99)
                M20s.append(-99)

    GAMA['Asymm'] = asymmetries
    GAMA['Smooth'] = smoothnesses
    GAMA['Gini'] = ginis
    GAMA['M20'] = M20s

    GAMA.write(
        'catalogs/CARS_GAMA/GAMA_allObjects_logMass10_complete_0-1364_morphologies.fits'
    )

    # files = []
    # for i in range(len(small)) :
    #     file = ('cutout_%.4f_%.4f.fits') % (RAs[i], Decs[i])
    #     files.append(file)

    # as_params(files, scale.value)

    return