Skip to content

Commit

Permalink
b,ack
Browse files Browse the repository at this point in the history
  • Loading branch information
Jammy2211 committed Nov 13, 2024
1 parent f2ad520 commit 465c625
Show file tree
Hide file tree
Showing 8 changed files with 100 additions and 66 deletions.
13 changes: 8 additions & 5 deletions autolens/analysis/result.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,9 @@ def image_plane_multiple_image_positions(self) -> aa.Grid2DIrregular:

return aa.Grid2DIrregular(values=multiple_images)

def image_plane_multiple_image_positions_for_single_image_from(self, increments : int = 20) -> aa.Grid2DIrregular:
def image_plane_multiple_image_positions_for_single_image_from(
self, increments: int = 20
) -> aa.Grid2DIrregular:
"""
If the standard point solver only locates one multiple image, finds one or more additional images, which are
not technically multiple image in the point source regime, but are close enough to it they can be used
Expand All @@ -133,12 +135,14 @@ def image_plane_multiple_image_positions_for_single_image_from(self, increments
The number of increments the source-plane centre is moved to compute multiple images.
"""

logger.info("""
logger.info(
"""
Could not find multiple images for maximum likelihood lens model.
Incrementally moving source centre inwards towards centre of source-plane until caustic crossing occurs
and multiple images are formed.
""")
"""
)

grid = self.analysis.dataset.mask.derive_grid.all_false

Expand All @@ -150,8 +154,7 @@ def image_plane_multiple_image_positions_for_single_image_from(self, increments
)

for i in range(1, increments):

factor = 1.0 - (1.0 * (i/increments))
factor = 1.0 - (1.0 * (i / increments))

multiple_images = solver.solve(
tracer=self.max_log_likelihood_tracer,
Expand Down
75 changes: 51 additions & 24 deletions autolens/lens/sensitivity.py
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ def set_auto_filename(
return False

def subplot_sensitivity(self):

log_likelihoods = self.result.figure_of_merit_array(
use_log_evidences=False,
remove_zeros=True,
Expand All @@ -375,7 +374,7 @@ def subplot_sensitivity(self):
except TypeError:
log_evidences = np.zeros_like(log_likelihoods)

self.open_subplot_figure(number_subplots=8, subplot_shape=(2,4))
self.open_subplot_figure(number_subplots=8, subplot_shape=(2, 4))

plotter = aplt.Array2DPlotter(
array=self.data_subtracted,
Expand All @@ -398,10 +397,7 @@ def subplot_sensitivity(self):

above_threshold = np.where(log_likelihoods > 5.0, 1.0, 0.0)

above_threshold = aa.Array2D(
values=above_threshold,
mask=log_likelihoods.mask
)
above_threshold = aa.Array2D(values=above_threshold, mask=log_likelihoods.mask)

self.mat_plot_2d.plot_array(
array=above_threshold,
Expand All @@ -410,16 +406,32 @@ def subplot_sensitivity(self):
)

try:
log_evidences_base = self.result._array_2d_from(self.result.log_evidences_base)
log_evidences_perturbed = self.result._array_2d_from(self.result.log_evidences_perturbed)
log_evidences_base = self.result._array_2d_from(
self.result.log_evidences_base
)
log_evidences_perturbed = self.result._array_2d_from(
self.result.log_evidences_perturbed
)

log_evidences_base_min = np.nanmin(np.where(log_evidences_base == 0, np.nan, log_evidences_base))
log_evidences_base_max = np.nanmax(np.where(log_evidences_base == 0, np.nan, log_evidences_base))
log_evidences_perturbed_min = np.nanmin(np.where(log_evidences_perturbed == 0, np.nan, log_evidences_perturbed))
log_evidences_perturbed_max = np.nanmax(np.where(log_evidences_perturbed == 0, np.nan, log_evidences_perturbed))
log_evidences_base_min = np.nanmin(
np.where(log_evidences_base == 0, np.nan, log_evidences_base)
)
log_evidences_base_max = np.nanmax(
np.where(log_evidences_base == 0, np.nan, log_evidences_base)
)
log_evidences_perturbed_min = np.nanmin(
np.where(log_evidences_perturbed == 0, np.nan, log_evidences_perturbed)
)
log_evidences_perturbed_max = np.nanmax(
np.where(log_evidences_perturbed == 0, np.nan, log_evidences_perturbed)
)

self.mat_plot_2d.cmap.kwargs["vmin"] = np.min([log_evidences_base_min, log_evidences_perturbed_min])
self.mat_plot_2d.cmap.kwargs["vmax"] = np.max([log_evidences_base_max, log_evidences_perturbed_max])
self.mat_plot_2d.cmap.kwargs["vmin"] = np.min(
[log_evidences_base_min, log_evidences_perturbed_min]
)
self.mat_plot_2d.cmap.kwargs["vmax"] = np.max(
[log_evidences_base_max, log_evidences_perturbed_max]
)

self.mat_plot_2d.plot_array(
array=log_evidences_base,
Expand All @@ -431,21 +443,36 @@ def subplot_sensitivity(self):
array=log_evidences_perturbed,
visuals_2d=self.visuals_2d,
auto_labels=AutoLabels(title="Log Evidence Perturb"),

)
except TypeError:
pass

log_likelihoods_base = self.result._array_2d_from(self.result.log_likelihoods_base)
log_likelihoods_perturbed = self.result._array_2d_from(self.result.log_likelihoods_perturbed)

log_likelihoods_base_min = np.nanmin(np.where(log_likelihoods_base == 0, np.nan, log_likelihoods_base))
log_likelihoods_base_max = np.nanmax(np.where(log_likelihoods_base == 0, np.nan, log_likelihoods_base))
log_likelihoods_perturbed_min = np.nanmin(np.where(log_likelihoods_perturbed == 0, np.nan, log_likelihoods_perturbed))
log_likelihoods_perturbed_max = np.nanmax(np.where(log_likelihoods_perturbed == 0, np.nan, log_likelihoods_perturbed))
log_likelihoods_base = self.result._array_2d_from(
self.result.log_likelihoods_base
)
log_likelihoods_perturbed = self.result._array_2d_from(
self.result.log_likelihoods_perturbed
)

log_likelihoods_base_min = np.nanmin(
np.where(log_likelihoods_base == 0, np.nan, log_likelihoods_base)
)
log_likelihoods_base_max = np.nanmax(
np.where(log_likelihoods_base == 0, np.nan, log_likelihoods_base)
)
log_likelihoods_perturbed_min = np.nanmin(
np.where(log_likelihoods_perturbed == 0, np.nan, log_likelihoods_perturbed)
)
log_likelihoods_perturbed_max = np.nanmax(
np.where(log_likelihoods_perturbed == 0, np.nan, log_likelihoods_perturbed)
)

self.mat_plot_2d.cmap.kwargs["vmin"] = np.min([log_likelihoods_base_min, log_likelihoods_perturbed_min])
self.mat_plot_2d.cmap.kwargs["vmax"] = np.max([log_likelihoods_base_max, log_likelihoods_perturbed_max])
self.mat_plot_2d.cmap.kwargs["vmin"] = np.min(
[log_likelihoods_base_min, log_likelihoods_perturbed_min]
)
self.mat_plot_2d.cmap.kwargs["vmax"] = np.max(
[log_likelihoods_base_max, log_likelihoods_perturbed_max]
)

self.mat_plot_2d.plot_array(
array=log_likelihoods_base,
Expand Down
5 changes: 2 additions & 3 deletions autolens/point/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,10 @@ def info(self) -> str:
info += f"fluxes_noise_map : {self.fluxes_noise_map}\n"
return info

def extent_from(self, buffer : float = 0.1):

def extent_from(self, buffer: float = 0.1):
y_max = max(self.positions[:, 0]) + buffer
y_min = min(self.positions[:, 0]) - buffer
x_max = max(self.positions[:, 1]) + buffer
x_min = min(self.positions[:, 1]) - buffer

return [y_min, y_max, x_min, x_max]
return [y_min, y_max, x_min, x_max]
1 change: 0 additions & 1 deletion autolens/point/model/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@


class AnalysisPoint(AgAnalysis, AnalysisLens):

Visualizer = VisualizerPoint
Result = ResultPoint

Expand Down
12 changes: 5 additions & 7 deletions autolens/point/model/plotter_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@


class PlotterInterfacePoint(PlotterInterface):

def dataset_point(self, dataset: PointDataset):
"""
Output visualization of an `PointDataset` dataset, typically before a model-fit is performed.
Expand Down Expand Up @@ -54,7 +53,10 @@ def should_plot(name):
dataset_plotter.subplot_dataset()

def fit_point(
self, fit: FitPointDataset, during_analysis: bool, subfolders: str = "fit_dataset"
self,
fit: FitPointDataset,
during_analysis: bool,
subfolders: str = "fit_dataset",
):
"""
Visualizes a `FitPointDataset` object, which fits an imaging dataset.
Expand Down Expand Up @@ -104,7 +106,6 @@ def should_plot(name):
fit_plotter.subplot_fit()

if not during_analysis and should_plot("all_at_end_png"):

mat_plot_2d = self.mat_plot_2d_from(
subfolders=path.join("fit_dataset", "end"),
)
Expand All @@ -113,7 +114,4 @@ def should_plot(name):
fit=fit, mat_plot_2d=mat_plot_2d, include_2d=self.include_2d
)

fit_plotter.figures_2d(
positions=True,
fluxes=True
)
fit_plotter.figures_2d(positions=True, fluxes=True)
5 changes: 3 additions & 2 deletions autolens/point/model/visualizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@


class VisualizerPoint(af.Visualizer):

@staticmethod
def visualize_before_fit(
analysis,
Expand Down Expand Up @@ -76,7 +75,9 @@ def visualize(

tracer = fit.tracer

grid = ag.Grid2D.from_extent(extent=fit.dataset.extent_from(), shape_native=(100, 100))
grid = ag.Grid2D.from_extent(
extent=fit.dataset.extent_from(), shape_native=(100, 100)
)

plotter_interface.tracer(
tracer=tracer, grid=grid, during_analysis=during_analysis
Expand Down
45 changes: 28 additions & 17 deletions autolens/point/plot/fit_point_plotters.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,25 +41,36 @@ def figures_2d(self, positions: bool = False, fluxes: bool = False):
)

if self.mat_plot_2d.axis.kwargs.get("extent") is None:

buffer = 0.1

y_max = max(
max(self.fit.dataset.positions[:, 0]),
max(self.fit.positions.model_data[:, 0]),
) + buffer
y_min = min(
min(self.fit.dataset.positions[:, 0]),
min(self.fit.positions.model_data[:, 0]),
) - buffer
x_max = max(
max(self.fit.dataset.positions[:, 1]),
max(self.fit.positions.model_data[:, 1]),
) + buffer
x_min = min(
min(self.fit.dataset.positions[:, 1]),
min(self.fit.positions.model_data[:, 1]),
) - buffer
y_max = (
max(
max(self.fit.dataset.positions[:, 0]),
max(self.fit.positions.model_data[:, 0]),
)
+ buffer
)
y_min = (
min(
min(self.fit.dataset.positions[:, 0]),
min(self.fit.positions.model_data[:, 0]),
)
- buffer
)
x_max = (
max(
max(self.fit.dataset.positions[:, 1]),
max(self.fit.positions.model_data[:, 1]),
)
+ buffer
)
x_min = (
min(
min(self.fit.dataset.positions[:, 1]),
min(self.fit.positions.model_data[:, 1]),
)
- buffer
)

extent = [y_min, y_max, x_min, x_max]

Expand Down
10 changes: 3 additions & 7 deletions test_autolens/point/model/test_plotter_interface_point.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,16 @@ def make_plotter_interface_plotter_setup():
return path.join("{}".format(directory), "files")


def test__fit_point(
fit_point_dataset_x2_plane, include_2d_all, plot_path, plot_patch
):
def test__fit_point(fit_point_dataset_x2_plane, include_2d_all, plot_path, plot_patch):
if os.path.exists(plot_path):
shutil.rmtree(plot_path)

plotter_interface = PlotterInterfacePoint(image_path=plot_path)

plotter_interface.fit_point(
fit=fit_point_dataset_x2_plane, during_analysis=False
)
plotter_interface.fit_point(fit=fit_point_dataset_x2_plane, during_analysis=False)

assert path.join(plot_path, "subplot_fit.png") in plot_patch.paths

plot_path = path.join(plot_path, "fit_dataset")

assert path.join(plot_path, "fit_point_positions.png") in plot_patch.paths
assert path.join(plot_path, "fit_point_positions.png") in plot_patch.paths

0 comments on commit 465c625

Please sign in to comment.