Esempio n. 1
0
def run_tests(random_seed,
              outfile,
              config=None,
              gsparams=None,
              wmult=None,
              logger=None,
              fail_value=-666.):
    """Run a full set of tests, writing pickled tuple output to outfile.
    """
    import sys
    import cPickle
    import numpy as np
    import galsim
    import galaxy_sample
    # Load up the comparison_utilities module from the parent directory
    sys.path.append('..')
    import comparison_utilities

    if config is None:
        use_config = False
        if gsparams is None:
            import warnings
            warnings.warn("No gsparams provided to run_tests?")
        if wmult is None:
            raise ValueError("wmult must be set if config=None.")
    else:
        use_config = True
        if gsparams is not None:
            import warnings
            warnings.warn(
                "gsparams is provided as a kwarg but the config['image']['gsparams'] will take "
                + "precedence.")
        if wmult is not None:
            import warnings
            warnings.warn(
                "wmult is provided as a kwarg but the config['image']['wmult'] will take "
                + "precedence.")
    # Get galaxy sample
    n_cosmos, hlr_cosmos, gabs_cosmos = galaxy_sample.get()
    # Only take the first NOBS objects
    n_cosmos = n_cosmos[0:NOBS]
    hlr_cosmos = hlr_cosmos[0:NOBS]
    gabs_cosmos = gabs_cosmos[0:NOBS]
    ntest = len(SERSIC_N_TEST)
    # Setup a UniformDeviate
    ud = galsim.UniformDeviate(random_seed)
    # Open the output file and write a header:
    fout = open(outfile, 'wb')
    fout.write(
        '#  g1obs_draw g2obs_draw sigma_draw delta_g1obs delta_g2obs delta_sigma '
        + 'err_g1obs err_g2obs err_sigma\n')
    # Start looping through the sample objects and collect the results
    for i, hlr, gabs in zip(range(NOBS), hlr_cosmos, gabs_cosmos):
        print "Testing galaxy #"+str(i+1)+"/"+str(NOBS)+\
              " with (hlr, |g|) = "+str(hlr)+", "+str(gabs)
        random_theta = 2. * np.pi * ud()
        g1 = gabs * np.cos(2. * random_theta)
        g2 = gabs * np.sin(2. * random_theta)
        for j, sersic_n in zip(range(ntest), SERSIC_N_TEST):
            print "Exploring Sersic n = " + str(sersic_n)
            if use_config:
                # Increment the random seed so that each test gets a unique one
                config['image'][
                    'random_seed'] = random_seed + i * NOBS * ntest + j * ntest + 1
                config['gal'] = {
                    "type": "Sersic",
                    "n": sersic_n,
                    "half_light_radius": hlr,
                    "ellip": {
                        "type": "G1G2",
                        "g1": g1,
                        "g2": g2
                    }
                }
                config['psf'] = {
                    "type": "Airy",
                    "lam_over_diam": PSF_LAM_OVER_DIAM
                }
                try:
                    results = comparison_utilities.compare_dft_vs_photon_config(
                        config,
                        abs_tol_ellip=TOL_ELLIP,
                        abs_tol_size=TOL_SIZE,
                        logger=logger)
                    test_ran = True
                except RuntimeError as err:
                    test_ran = False
                    pass
                # Uncomment lines below to ouput a check image
                #import copy
                #checkimage = galsim.config.BuildImage(copy.deepcopy(config))[0] #im = first element
                #checkimage.write('junk_'+str(i + 1)+'_'+str(j + 1)+'.fits')
            else:
                test_gsparams = galsim.GSParams(maximum_fft_size=MAX_FFT_SIZE)
                galaxy = galsim.Sersic(sersic_n,
                                       half_light_radius=hlr,
                                       gsparams=test_gsparams)
                galaxy.applyShear(g1=g1, g2=g2)
                psf = galsim.Airy(lam_over_diam=PSF_LAM_OVER_DIAM,
                                  gsparams=test_gsparams)
                try:
                    results = comparison_utilities.compare_dft_vs_photon_object(
                        galaxy,
                        psf_object=psf,
                        rng=ud,
                        pixel_scale=PIXEL_SCALE,
                        size=IMAGE_SIZE,
                        abs_tol_ellip=TOL_ELLIP,
                        abs_tol_size=TOL_SIZE,
                        n_photons_per_trial=NPHOTONS,
                        wmult=wmult)
                    test_ran = True
                except RuntimeError, err:
                    test_ran = False
                    pass

            if not test_ran:
                import warnings
                warnings.warn('RuntimeError encountered for galaxy ' +
                              str(i + 1) + '/' + str(NOBS) + ' with ' +
                              'Sersic n = ' + str(sersic_n) + ': ' + str(err))
                fout.write('%e %e %e %e %e %e %e %e %e %e %e %e %e\n' %
                           (fail_value, fail_value, fail_value, fail_value,
                            fail_value, fail_value, fail_value, fail_value,
                            fail_value, fail_value, fail_value, fail_value,
                            fail_value))
                fout.flush()
            else:
                fout.write('%e %e %e %e %e %e %e %e %e %e %e %e %e\n' %
                           (results.g1obs_draw, results.g2obs_draw,
                            results.sigma_draw, results.delta_g1obs,
                            results.delta_g2obs, results.delta_sigma,
                            results.err_g1obs, results.err_g2obs,
                            results.err_sigma, sersic_n, hlr, g1, g2))
                fout.flush()
def run_tests(use_interpolants, nitems=test_interpolants.default_nitems):
    """Run the tests for the specified interpolants."""

    import sys
    # Import the Sersic galaxy sample module
    try:
        import galaxy_sample
    except ImportError:
        import sys
        sys.path.append('../external/test_sersic_highn')
        import galaxy_sample

    # Get the COSMOS galaxy sample parameters
    ns_cosmos, hlrs_cosmos, gobss_cosmos = galaxy_sample.get()
    # Only use the first nitems galaxies in these lists, starting at test_interpolants.first_index
    istart = test_interpolants.default_first_index
    iend = istart + nitems
    ns_cosmos = ns_cosmos[istart: iend]
    hlrs_cosmos = hlrs_cosmos[istart: iend]
    gobss_cosmos = gobss_cosmos[istart: iend]

    # Draw a whole load of images of Sersic profiles at random orientations using these params
    sersic_images = draw_sersic_images(
        ns_cosmos, hlrs_cosmos, gobss_cosmos, random_seed=test_interpolants.rseed, nmin=0.3,
        nmax=4.2, image_size=SERSIC_IMAGE_SIZE, pixel_scale=test_interpolants.pixel_scale)

    # Calculate the reference results for g1obs, g2obs and sigma for these reference images
    g1_list = []
    g2_list = []
    sigma_list = []
    print "Calculating reference g1, g2 & sigma for "+str(len(sersic_images))+" Sersic images"
    for sersic_image in sersic_images:
        shape = test_interpolants.CatchAdaptiveMomErrors(sersic_image)
        if isinstance(shape, float):
            g1_list.append(-10)
            g2_list.append(-10)
            sigma_list.append(-10)
        elif isinstance(shape, galsim.hsm.ShapeData):
            g1_list.append(shape.observed_shape.g1)
            g2_list.append(shape.observed_shape.g2)
            sigma_list.append(shape.moments_sigma)
        else:
            raise TypeError("Unexpected output from test_interpolants.CatchAdaptiveMomErrors().")
    g1_list = np.asarray(g1_list)
    g2_list = np.asarray(g2_list)
    sigma_list = np.asarray(sigma_list)

    # Then start the interpolant tests...
    # Define a dict storing PSFs to iterate over along with the appropriate test pixel scale and
    # filename
    psf_dict = {
        "delta" : (
            galsim.Gaussian(1.e-8), test_interpolants.pixel_scale, DELTA_FILENAME),
        "original" : (
            None, test_interpolants.pixel_scale, ORIGINAL_FILENAME),
    }
    print''
    # Then we start the grand loop producing output in a similar fashion to test_interpolants.py
    for image_type in ("delta", "original"):
 
        # Get the correct PSF and test image pixel scale
        psf = psf_dict[image_type][0]
        dx_test = psf_dict[image_type][1]
        outfile = open(psf_dict[image_type][2], 'wb')
        print "Writing test results to "+str(outfile)
        for padding in test_interpolants.padding_list:

            print "Using padding = "+str(padding)
            for interpolant in use_interpolants:

                print "Using interpolant: "+str(interpolant)
                print 'Running Angle tests'
                for angle in test_interpolants.angle_list: # Possible rotation angles

                    sys.stdout.write('.')
                    sys.stdout.flush()
                    dataXint = calculate_interpolated_image_g1g2sigma(
                        sersic_images, psf=psf, dx_input=test_interpolants.pixel_scale,
                        dx_test=dx_test, shear=None, magnification=None, angle=angle*galsim.degrees,
                        shift=None, x_interpolant=interpolant, padding=padding,
                        image_type=image_type)
                    test_interpolants.print_results(
                        outfile, g1_list, g2_list, sigma_list, dataXint)
                    dataKint = calculate_interpolated_image_g1g2sigma(
                        sersic_images, psf=psf, dx_input=test_interpolants.pixel_scale,
                        dx_test=dx_test, shear=None, magnification=None, angle=angle*galsim.degrees,
                        shift=None, k_interpolant=interpolant, padding=padding,
                        image_type=image_type)
                    test_interpolants.print_results(
                        outfile, g1_list, g2_list, sigma_list, dataKint)
                sys.stdout.write('\n')
 
                print 'Running Shear/Magnification tests'
                for (g1, g2, mag) in test_interpolants.shear_and_magnification_list:

                    sys.stdout.write('.')
                    sys.stdout.flush()
                    dataXint = calculate_interpolated_image_g1g2sigma(
                        sersic_images, psf=psf, dx_input=test_interpolants.pixel_scale,
                        dx_test=dx_test, shear=(g1, g2), magnification=mag, angle=None,
                        shift=None, x_interpolant=interpolant, padding=padding,
                        image_type=image_type)
                    test_interpolants.print_results(
                        outfile, g1_list, g2_list, sigma_list, dataXint)
                    dataKint = calculate_interpolated_image_g1g2sigma(
                        sersic_images, psf=psf, dx_input=test_interpolants.pixel_scale,
                        dx_test=dx_test, shear=(g1, g2), magnification=mag, angle=None,
                        shift=None, k_interpolant=interpolant, padding=padding,
                        image_type=image_type)
                    test_interpolants.print_results(
                        outfile, g1_list, g2_list, sigma_list, dataKint)
                sys.stdout.write('\n')

                print 'Running Shift tests'
                for shift in test_interpolants.shift_list:

                    sys.stdout.write('.')
                    sys.stdout.flush()
                    dataXint = calculate_interpolated_image_g1g2sigma(
                        sersic_images, psf=psf, dx_input=test_interpolants.pixel_scale,
                        dx_test=dx_test, shear=None, magnification=None, angle=None,
                        shift=shift, x_interpolant=interpolant, padding=padding,
                        image_type=image_type)
                    test_interpolants.print_results(
                        outfile, g1_list, g2_list, sigma_list, dataXint)
                    dataKint = calculate_interpolated_image_g1g2sigma(
                        sersic_images, psf=psf, dx_input=test_interpolants.pixel_scale,
                        dx_test=dx_test, shear=None, magnification=None, angle=None,
                        shift=shift, k_interpolant=interpolant, padding=padding,
                        image_type=image_type)
                    test_interpolants.print_results(
                        outfile, g1_list, g2_list, sigma_list, dataKint)
                sys.stdout.write('\n')

                print ''

        print "Finished tests for image_type: "+str(image_type) 
        print ""
        outfile.close()
Esempio n. 3
0
def run_tests(random_seed, outfile, config=None, gsparams=None, wmult=None, logger=None,
              fail_value=-666.):
    """Run a full set of tests, writing pickled tuple output to outfile.
    """
    import cPickle
    import numpy as np
    import galsim
    import galaxy_sample
    
    if config is None:
        use_config = False
        if gsparams is None:
            import warnings
            warnings.warn("No gsparams provided to run_tests?")
        if wmult is None:
            raise ValueError("wmult must be set if config=None.")
    else:
        use_config = True
        if gsparams is not None:
            import warnings
            warnings.warn(
                "gsparams is provided as a kwarg but the config['image']['gsparams'] will take "+
                "precedence.")
        if wmult is not None:
            import warnings
            warnings.warn(
                "wmult is provided as a kwarg but the config['image']['wmult'] will take "+
                "precedence.")
    # Get galaxy sample
    n_cosmos, hlr_cosmos, gabs_cosmos = galaxy_sample.get()
    # Only take the first NOBS objects
    n_cosmos = n_cosmos[0: NOBS]
    hlr_cosmos = hlr_cosmos[0: NOBS]
    gabs_cosmos = gabs_cosmos[0: NOBS]
    # Setup a UniformDeviate
    ud = galsim.UniformDeviate(random_seed)
    # Open the output file and write a header:
    fout = open(outfile, 'wb')
    fout.write(
        '#  g1obs_draw g2obs_draw sigma_draw delta_g1obs delta_g2obs delta_sigma '+
        'err_g1obs err_g2obs err_sigma\n')
    # Start looping through the sample objects and collect the results
    for i, hlr, gabs in zip(range(NOBS), hlr_cosmos, gabs_cosmos):
        print "Testing galaxy #"+str(i+1)+"/"+str(NOBS)+\
              " with (hlr, |g|) = "+str(hlr)+", "+str(gabs)
        random_theta = 2. * np.pi * ud()
        g1 = gabs * np.cos(2. * random_theta)
        g2 = gabs * np.sin(2. * random_theta)
        if use_config:
            # Increment the random seed so that each test gets a unique one
            config['image']['random_seed'] = random_seed + i * NOBS + 1
            config['gal'] = {
                "type" : "Gaussian" , "half_light_radius" : hlr ,
                "ellip" : {
                    "type" : "G1G2" , "g1" : g1 , "g2" : g2
                }
            }
            config['psf'] = {"type" : "Airy" , "lam_over_diam" : PSF_LAM_OVER_DIAM }
            try:
                results = galsim.utilities.compare_dft_vs_photon_config(
                    config, abs_tol_ellip=TOL_ELLIP, abs_tol_size=TOL_SIZE, logger=logger)
                test_ran = True
            except RuntimeError as err:
                test_ran = False
                pass
            # Uncomment lines below to ouput a check image
            #import copy
            #checkimage = galsim.config.BuildImage(copy.deepcopy(config))[0] #im = first element
            #checkimage.write('junk_'+str(i + 1)+'_'+str(j + 1)+'.fits')
        else:
            test_gsparams = galsim.GSParams(maximum_fft_size=MAX_FFT_SIZE)
            galaxy = galsim.Gaussian(half_light_radius=hlr, gsparams=test_gsparams)
            galaxy.applyShear(g1=g1, g2=g2)
            psf = galsim.Airy(lam_over_diam=PSF_LAM_OVER_DIAM, gsparams=test_gsparams)
            try:
                results = galsim.utilities.compare_dft_vs_photon_object(
                    galaxy, psf_object=psf, rng=ud, pixel_scale=PIXEL_SCALE, size=IMAGE_SIZE,
                    abs_tol_ellip=TOL_ELLIP, abs_tol_size=TOL_SIZE,
                    n_photons_per_trial=NPHOTONS, wmult=wmult)
                test_ran = True
            except RuntimeError, err:
                test_ran = False
                pass

        if not test_ran:
            import warnings
            warnings.warn(
                'RuntimeError encountered for galaxy '+str(i + 1)+'/'+str(NOBS)+': '+str(err))
            fout.write(
                '%e %e %e %e %e %e %e %e %e %e %e %e\n' % (
                    fail_value, fail_value, fail_value, fail_value, fail_value, fail_value,
                    fail_value, fail_value, fail_value, fail_value, fail_value, fail_value,
                )
            )
            fout.flush()
        else:
            fout.write(
                '%e %e %e %e %e %e %e %e %e %e %e %e\n' % (
                    results.g1obs_draw, results.g2obs_draw, results.sigma_draw,
                    results.delta_g1obs, results.delta_g2obs, results.delta_sigma,
                    results.err_g1obs, results.err_g2obs, results.err_sigma, hlr, g1, g2
                )
            )
            fout.flush()
Esempio n. 4
0
def run_tests(use_interpolants, nitems=test_interpolants.default_nitems):
    """Run the tests for the specified interpolants."""

    import sys
    # Import the Sersic galaxy sample module
    try:
        import galaxy_sample
    except ImportError:
        import sys
        sys.path.append('../external/test_sersic_highn')
        import galaxy_sample

    # Get the COSMOS galaxy sample parameters
    ns_cosmos, hlrs_cosmos, gobss_cosmos = galaxy_sample.get()
    # Only use the first nitems galaxies in these lists, starting at test_interpolants.first_index
    istart = test_interpolants.default_first_index
    iend = istart + nitems
    ns_cosmos = ns_cosmos[istart:iend]
    hlrs_cosmos = hlrs_cosmos[istart:iend]
    gobss_cosmos = gobss_cosmos[istart:iend]

    # Draw a whole load of images of Sersic profiles at random orientations using these params
    sersic_images = draw_sersic_images(
        ns_cosmos,
        hlrs_cosmos,
        gobss_cosmos,
        random_seed=test_interpolants.rseed,
        nmin=0.3,
        nmax=4.2,
        image_size=SERSIC_IMAGE_SIZE,
        pixel_scale=test_interpolants.pixel_scale)

    # Calculate the reference results for g1obs, g2obs and sigma for these reference images
    g1_list = []
    g2_list = []
    sigma_list = []
    print "Calculating reference g1, g2 & sigma for " + str(
        len(sersic_images)) + " Sersic images"
    for sersic_image in sersic_images:
        shape = test_interpolants.CatchAdaptiveMomErrors(sersic_image)
        if isinstance(shape, float):
            g1_list.append(-10)
            g2_list.append(-10)
            sigma_list.append(-10)
        elif isinstance(shape, galsim.hsm.ShapeData):
            g1_list.append(shape.observed_shape.g1)
            g2_list.append(shape.observed_shape.g2)
            sigma_list.append(shape.moments_sigma)
        else:
            raise TypeError(
                "Unexpected output from test_interpolants.CatchAdaptiveMomErrors()."
            )
    g1_list = np.asarray(g1_list)
    g2_list = np.asarray(g2_list)
    sigma_list = np.asarray(sigma_list)

    # Then start the interpolant tests...
    # Define a dict storing PSFs to iterate over along with the appropriate test pixel scale and
    # filename
    psf_dict = {
        "delta": (galsim.Gaussian(1.e-8), test_interpolants.pixel_scale,
                  DELTA_FILENAME),
        "original": (None, test_interpolants.pixel_scale, ORIGINAL_FILENAME),
    }
    print ''
    # Then we start the grand loop producing output in a similar fashion to test_interpolants.py
    for image_type in ("delta", "original"):

        # Get the correct PSF and test image pixel scale
        psf = psf_dict[image_type][0]
        dx_test = psf_dict[image_type][1]
        outfile = open(psf_dict[image_type][2], 'wb')
        print "Writing test results to " + str(outfile)
        for padding in test_interpolants.padding_list:

            print "Using padding = " + str(padding)
            for interpolant in use_interpolants:

                print "Using interpolant: " + str(interpolant)
                print 'Running Angle tests'
                for angle in test_interpolants.angle_list:  # Possible rotation angles

                    sys.stdout.write('.')
                    sys.stdout.flush()
                    dataXint = calculate_interpolated_image_g1g2sigma(
                        sersic_images,
                        psf=psf,
                        dx_input=test_interpolants.pixel_scale,
                        dx_test=dx_test,
                        shear=None,
                        magnification=None,
                        angle=angle * galsim.degrees,
                        shift=None,
                        x_interpolant=interpolant,
                        padding=padding,
                        image_type=image_type)
                    test_interpolants.print_results(outfile, g1_list, g2_list,
                                                    sigma_list, dataXint)
                    dataKint = calculate_interpolated_image_g1g2sigma(
                        sersic_images,
                        psf=psf,
                        dx_input=test_interpolants.pixel_scale,
                        dx_test=dx_test,
                        shear=None,
                        magnification=None,
                        angle=angle * galsim.degrees,
                        shift=None,
                        k_interpolant=interpolant,
                        padding=padding,
                        image_type=image_type)
                    test_interpolants.print_results(outfile, g1_list, g2_list,
                                                    sigma_list, dataKint)
                sys.stdout.write('\n')

                print 'Running Shear/Magnification tests'
                for (g1, g2,
                     mag) in test_interpolants.shear_and_magnification_list:

                    sys.stdout.write('.')
                    sys.stdout.flush()
                    dataXint = calculate_interpolated_image_g1g2sigma(
                        sersic_images,
                        psf=psf,
                        dx_input=test_interpolants.pixel_scale,
                        dx_test=dx_test,
                        shear=(g1, g2),
                        magnification=mag,
                        angle=None,
                        shift=None,
                        x_interpolant=interpolant,
                        padding=padding,
                        image_type=image_type)
                    test_interpolants.print_results(outfile, g1_list, g2_list,
                                                    sigma_list, dataXint)
                    dataKint = calculate_interpolated_image_g1g2sigma(
                        sersic_images,
                        psf=psf,
                        dx_input=test_interpolants.pixel_scale,
                        dx_test=dx_test,
                        shear=(g1, g2),
                        magnification=mag,
                        angle=None,
                        shift=None,
                        k_interpolant=interpolant,
                        padding=padding,
                        image_type=image_type)
                    test_interpolants.print_results(outfile, g1_list, g2_list,
                                                    sigma_list, dataKint)
                sys.stdout.write('\n')

                print 'Running Shift tests'
                for shift in test_interpolants.shift_list:

                    sys.stdout.write('.')
                    sys.stdout.flush()
                    dataXint = calculate_interpolated_image_g1g2sigma(
                        sersic_images,
                        psf=psf,
                        dx_input=test_interpolants.pixel_scale,
                        dx_test=dx_test,
                        shear=None,
                        magnification=None,
                        angle=None,
                        shift=shift,
                        x_interpolant=interpolant,
                        padding=padding,
                        image_type=image_type)
                    test_interpolants.print_results(outfile, g1_list, g2_list,
                                                    sigma_list, dataXint)
                    dataKint = calculate_interpolated_image_g1g2sigma(
                        sersic_images,
                        psf=psf,
                        dx_input=test_interpolants.pixel_scale,
                        dx_test=dx_test,
                        shear=None,
                        magnification=None,
                        angle=None,
                        shift=shift,
                        k_interpolant=interpolant,
                        padding=padding,
                        image_type=image_type)
                    test_interpolants.print_results(outfile, g1_list, g2_list,
                                                    sigma_list, dataKint)
                sys.stdout.write('\n')

                print ''

        print "Finished tests for image_type: " + str(image_type)
        print ""
        outfile.close()