How to use the autofit.optimize.non_linear function in autofit

To help you get started, we’ve selected a few autofit examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Jammy2211 / PyAutoLens / test / pipeline / test_pipeline.py View on Github external
def run(self, data, previous_results, mask=None, positions=None):
        self.data = data
        self.previous_results = previous_results
        self.mask = mask
        self.positions = positions
        return non_linear.Result(model_mapper.ModelInstance(), 1)
github Jammy2211 / PyAutoLens / test_autolens / unit / pipeline / phase / extensions / test_phase_extensions.py View on Github external
def __init__(self):
        self.paths = autofit.optimize.non_linear.paths.Paths(
            phase_name="phase_name",
            phase_path="phase_path",
            phase_folders=("",),
            phase_tag="",
        )
        self.optimizer = MockOptimizer(paths=self.paths)
        self.model = af.ModelMapper()
github Jammy2211 / PyAutoLens / workspace / howtolens / chapter_4_inversions / tutorial_8_pipeline.py View on Github external
# We can customize the inversion's priors like we do our light and mass profiles.

            self.lens_galaxies.lens = previous_results[0].variable.lens
            self.source_galaxies.source.pixelization.shape_0 = mm.UniformPrior(lower_limit=20.0, upper_limit=40.0)
            self.source_galaxies.source.pixelization.shape_1 = mm.UniformPrior(lower_limit=20.0, upper_limit=40.0)

            # The expected value of the regularization coefficient depends on the details of the data reduction and
            # source galaxy. A broad log-uniform prior is thus an appropriate way to sample the large range of
            # possible values.
            self.source_galaxies.source.regularization.coefficients_0 = mm.LogUniformPrior(lower_limit=1.0e-6,
                                                                                           upper_limit=10000.0)

    phase2 = InversionPhase(lens_galaxies=dict(lens=gm.GalaxyModel(mass=mp.EllipticalIsothermal)),
                           source_galaxies=dict(source=gm.GalaxyModel(pixelization=pix.Rectangular,
                                                                      regularization=reg.Constant)),
                           optimizer_class=nl.MultiNest, phase_name=pipeline_name + '/phase_2_inversion')

    phase2.optimizer.sampling_efficiency = 0.3
    phase2.optimizer.const_efficiency_mode = True

    return pipeline.PipelineImaging(pipeline_name, phase1, phase2)
github Jammy2211 / PyAutoLens / workspace / pipelines / examples / mask_and_positions.py View on Github external
#    case 0.3". This is the equivalent of making a circular_annular mask with inner_radius_arcsec=0.3, however
    #    because it is a phase input variable we can turn this masking on and off for different phases.
    # 2) Don't specify anything about using positions, given this phase is a lens-only phase with no mass model.

    def mask_function(image):
        return msk.Mask.circular(shape=image.shape, pixel_scale=image.pixel_scale, radius_arcsec=2.5)

    class LensPhase(ph.LensPlanePhase):

        def pass_priors(self, previous_results):

            self.lens_galaxies.lens.light.centre_0 = prior.GaussianPrior(mean=0.0, sigma=0.1)
            self.lens_galaxies.lens.light.centre_1 = prior.GaussianPrior(mean=0.0, sigma=0.1)

    phase1 = LensPhase(lens_galaxies=dict(lens=gm.GalaxyModel(light=lp.EllipticalSersic)),
                       optimizer_class=nl.MultiNest, mask_function=mask_function,
                       inner_circular_mask_radii=0.3,
                       phase_name=pipeline_path + '/phase_1_lens_light_only_mask_uses_inner_radii_input')

    # You'll see these lines throughout all of the example pipelines. They are used to make MultiNest sample the \
    # non-linear parameter space faster (if you haven't already, checkout the tutorial '' in howtolens/chapter_2).

    phase1.optimizer.const_efficiency_mode = True
    phase1.optimizer.n_live_points = 30
    phase1.optimizer.sampling_efficiency = 0.3

    ### PHASE 2 ###

    # In phase 2, we will:

    # 1) Use a lens-subtracted image using a model lens galaxy image from phase 1.
    # 2) Initialize the priors on the centre of the lens galaxy's mass-profile by linking them to those inferred for \
github Jammy2211 / PyAutoLens / workspace / tools / analysis / phase.py View on Github external
# We're going to model our lens galaxy using a light profile (an elliptical Sersic) and mass profile
# (a singular isothermal ellipsoid). We load these profiles from the 'light_profiles (lp)' and 'mass_profiles (mp)'.

# To setup our model galaxies, we use the 'galaxy_model' module and GalaxyModel class.
# A GalaxyModel represents a galaxy where the parameters of its associated profiles are
# variable and fitted for by the analysis.
lens_galaxy_model = gm.GalaxyModel(light=lp.EllipticalSersic, mass=mp.EllipticalIsothermal)
source_galaxy_model = gm.GalaxyModel(light=lp.EllipticalSersic)

# To perform the analysis, we set up a phase using the 'phase' module (imported as 'ph').
# A phase takes our galaxy models and fits their parameters using a non-linear search (in this case, MultiNest).
phase = ph.LensSourcePlanePhase(lens_galaxies=dict(lens=gm.GalaxyModel(light=lp.EllipticalSersic,
                                                                       mass=mp.EllipticalIsothermal,
                                                                       shear=mp.ExternalShear)),
                                 source_galaxies=dict(source=gm.GalaxyModel(light=lp.EllipticalSersic)),
                                 optimizer_class=nl.MultiNest,
                                 phase_name='example/phase_non_pipeline')

# You'll see these lines throughout all of the example pipelines. They are used to make MultiNest sample the \
# non-linear parameter space faster (if you haven't already, checkout the tutorial '' in howtolens/chapter_2).

phase.optimizer.const_efficiency_mode = True
phase.optimizer.n_live_points = 50
phase.optimizer.sampling_efficiency = 0.5

# We run the phase on the image, print the results and plot the fit.
result = phase.run(data=ccd_data)
lens_fit_plotters.plot_fit_subplot(fit=result.most_likely_fit)
github Jammy2211 / PyAutoLens / workspace / howtolens / chapter_2_lens_modeling / scripts / tutorial_8_masking_and_positions.py View on Github external
exposure_time=300.0, psf=psf, background_sky_level=0.1, add_noise=True)

    return ccd_simulated

ccd_data = simulate_two_source_galaxies()
ccd_plotters.plot_ccd_subplot(ccd_data=ccd_data)

# To specify the positions, we break the positions list into two cells. They will be plotted in different colours to
# represent the fact they trace from different source galaxies.
ccd_plotters.plot_ccd_subplot(ccd_data=ccd_data, positions=[[[2.65, 0.0], [-0.55, 0.0]], [[-2.65, 0.0], [0.55, 0.0]]])

# Again, we tell our phase to use the positions and pass this list of pixels to our phase when we run it.
phase_with_x2_positions = ph.LensSourcePlanePhase(lens_galaxies=dict(lens=gm.GalaxyModel()),
                                source_galaxies=dict(source=gm.GalaxyModel()),
                                use_positions=True, # <- We use the positionos here
                                optimizer_class=nl.MultiNest, phase_name='phase')
github Jammy2211 / PyAutoLens / workspace / pipelines / no_lens_light / subhalo / from_power_law / lens_pl_shear_subhalo_source_sersic.py View on Github external
results.from_phase('phase_1_lens_pl_shear_source_sersic').variable.source.light.sersic_index

            self.source_galaxies.source.light.axis_ratio = \
                results.from_phase('phase_1_lens_pl_shear_source_sersic').variable.source.light.axis_ratio

            self.source_galaxies.source.light.phi = \
                results.from_phase('phase_1_lens_pl_shear_source_sersic').variable.source.light.phi


    phase2 = GridPhase(phase_name='phase_2_subhalo_search', phase_folders=phase_folders,
                       tag_phases=tag_phases,
                       lens_galaxies=dict(lens=gm.GalaxyModel(mass=mp.EllipticalPowerLaw,
                                                              shear=mp.ExternalShear),
                                          subhalo=gm.GalaxyModel(mass=mp.SphericalTruncatedNFWChallenge)),
                       source_galaxies=dict(source=gm.GalaxyModel(light=lp.EllipticalSersic)),
                       optimizer_class=nl.MultiNest,
                       sub_grid_size=sub_grid_size, bin_up_factor=bin_up_factor,
                       positions_threshold=positions_threshold, inner_mask_radii=inner_mask_radii,
                       interp_pixel_scale=interp_pixel_scale,
                       number_of_steps=4)

    phase2.optimizer.const_efficiency_mode = True
    phase2.optimizer.n_live_points = 50
    phase2.optimizer.sampling_efficiency = 0.5

    class SubhaloPhase(ph.LensSourcePlanePhase):

        def pass_priors(self, results):

            ### Lens Mass, PL -> PL, Shear -> Shear ###

            self.lens_galaxies.lens = results.from_phase('phase_1_lens_pl_shear_source_sersic').variable.lens
github Jammy2211 / PyAutoLens / workspace / howtolens / chapter_3_pipelines / tutorial_2_pipeline_x2_lens_galaxies.py View on Github external
class LeftLensPhase(ph.LensPlanePhase):

        def pass_priors(self, previous_results):

            # Lets restrict the prior's on the centres around the pixel we know the galaxy's light centre peaks.

            self.lens_galaxies.left_lens.light.centre_0 = prior.GaussianPrior(mean=0.0, sigma=0.05)
            self.lens_galaxies.left_lens.light.centre_1 = prior.GaussianPrior(mean=-1.0, sigma=0.05)

            # Given we are only fitting the very central region of the lens galaxy, we don't want to let a parameter 
            # like th Sersic index vary. Lets fix it to 4.0.

            self.lens_galaxies.left_lens.light.sersic_index = 4.0

    phase1 = LeftLensPhase(lens_galaxies=dict(left_lens=gm.GalaxyModel(light=lp.EllipticalSersic)),
                           optimizer_class=nl.MultiNest, mask_function=mask_function,
                           phase_name=pipeline_path + '/phase_1_left_lens_light')

    phase1.optimizer.const_efficiency_mode = True
    phase1.optimizer.n_live_points = 30
    phase1.optimizer.sampling_efficiency = 0.5

    ### PHASE 2 ###

    # Now do the exact same with the lens galaxy on the right at (0.0", 1.0")

    def mask_function(image):
        return msk.Mask.circular(image.shape, pixel_scale=image.pixel_scale, radius_arcsec=0.5, centre=(0.0, 1.0))

    class RightLensPhase(ph.LensPlanePhase):

        def pass_priors(self, previous_results):
github Jammy2211 / PyAutoLens / workspace / pipelines / examples / lens_light_and_source_inversion.py View on Github external
#    its light profile in phase 1.

    class LensSubtractedPhase(ph.LensSourcePlanePhase):

        def modify_image(self, image, previous_results):
            return image - previous_results[0].unmasked_lens_plane_model_image

        def pass_priors(self, previous_results):

            self.lens_galaxies.lens.mass.centre_0 = previous_results[0].variable.lens.light.centre_0
            self.lens_galaxies.lens.mass.centre_1 = previous_results[0].variable.lens.light.centre_1

    phase2 = LensSubtractedPhase(lens_galaxies=dict(lens=gm.GalaxyModel(mass=mp.EllipticalIsothermal,
                                                                        shear=mp.ExternalShear)),
                                 source_galaxies=dict(source=gm.GalaxyModel(light=lp.EllipticalSersic)),
                                 optimizer_class=nl.MultiNest, mask_function=mask_function_annular,
                                 phase_name=pipeline_path + '/phase_2_source_only')

    phase2.optimizer.const_efficiency_mode = True
    phase2.optimizer.n_live_points = 60
    phase2.optimizer.sampling_efficiency = 0.2


    ### PHASE 3 ###

    # In phase 3, we will fit simultaneously the lens and source galaxies, where we:

    # 1) Initialize the lens's light, mass, shear and source's light using the results of phases 1 and 2.

    class LensSourcePhase(ph.LensSourcePlanePhase):

        def pass_priors(self, previous_results):