Skip to content

Commit

Permalink
Merge pull request #696 from rhayes777/feature/analysis_combined_visu…
Browse files Browse the repository at this point in the history
…alize

Feature/analysis combined visualize
  • Loading branch information
Jammy2211 authored Apr 17, 2023
2 parents c682c69 + 421e7d6 commit d92120d
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 0 deletions.
1 change: 1 addition & 0 deletions autofit/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
from .non_linear.abstract_search import NonLinearSearch
from .non_linear.abstract_search import PriorPasser
from .non_linear.analysis.analysis import Analysis
from .non_linear.analysis.combined import CombinedAnalysis
from .non_linear.grid.grid_search import GridSearchResult
from .non_linear.initializer import InitializerBall
from .non_linear.initializer import InitializerPrior
Expand Down
5 changes: 5 additions & 0 deletions autofit/graphical/declarative/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ def visualize(
instance,
during_analysis
)
model_factor.visualize_combined(
paths,
instance,
during_analysis
)

@property
def global_prior_model(self) -> Collection:
Expand Down
5 changes: 5 additions & 0 deletions autofit/graphical/declarative/factor/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ def visualize(
instance,
during_analysis
)
self.analysis.visualize_combined(
paths,
instance,
during_analysis
)

def log_likelihood_function(
self,
Expand Down
4 changes: 4 additions & 0 deletions autofit/non_linear/abstract_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
FactorApproximation,
)
from autofit.graphical.utils import Status
from autofit.graphical.utils import Status
from autofit.mapper.prior_model.collection import Collection
from autofit.non_linear.initializer import Initializer
from autofit.non_linear.parallel import SneakyPool
Expand Down Expand Up @@ -768,6 +769,9 @@ def perform_update(self, model, analysis, during_analysis):
analysis.visualize(
paths=self.paths, instance=instance, during_analysis=during_analysis
)
analysis.visualize_combined(
analyses=None, paths=self.paths, instance=instance, during_analysis=during_analysis
)

if self.should_profile:
self.logger.debug("Profiling Maximum Likelihood Model")
Expand Down
3 changes: 3 additions & 0 deletions autofit/non_linear/analysis/analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ def log_likelihood_function(self, instance):
def visualize(self, paths: AbstractPaths, instance, during_analysis):
pass

def visualize_combined(self, analyses, paths: AbstractPaths, instance, during_analysis):
pass

def save_attributes_for_aggregator(self, paths: AbstractPaths):
pass

Expand Down
22 changes: 22 additions & 0 deletions autofit/non_linear/analysis/combined.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,28 @@ def func(child_paths, analysis):

self._for_each_analysis(func, paths)

def visualize_combined(self, analyses : List["Analysis"], paths: AbstractPaths, instance, during_analysis):
"""
Visualise the instance using images and quantities which are shared across all analyses.
For example, each Analysis may have a different dataset, where the fit to each dataset is intended to all
be plotted on the same matplotlib subplot. This function can be overwritten to allow the visualization of such
a plot.
Only the first analysis is used to visualize the combined results, where it is assumed that it uses the
`analyses` property to access the other analyses and perform visualization.
Parameters
----------
paths
An object describing the paths for saving data (e.g. hard-disk directories or entries in sqlite database).
instance
The maximum likelihood instance of the model so far in the non-linear search.
during_analysis
Is this visualisation during analysis?
"""
self.analyses[0].visualize_combined(analyses=self.analyses, paths=paths, instance=instance, during_analysis=during_analysis)

def profile_log_likelihood_function(
self, paths: AbstractPaths, instance,
):
Expand Down
3 changes: 3 additions & 0 deletions autofit/non_linear/analysis/indexed.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ def log_likelihood_function(self, instance):
def visualize(self, paths: AbstractPaths, instance, during_analysis):
return self.analysis.visualize(paths, instance[self.index], during_analysis)

def visualize_combined(self, analyses, paths: AbstractPaths, instance, during_analysis):
return self.analysis.visualize_combined(paths, instance[self.index], during_analysis)

def profile_log_likelihood_function(self, paths: AbstractPaths, instance):
return self.profile_log_likelihood_function(paths, instance[self.index])

Expand Down
Binary file not shown.
16 changes: 16 additions & 0 deletions test_autofit/non_linear/test_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
class Analysis(af.Analysis):
def __init__(self):
self.did_visualise = False
self.did_visualise_combined = False
self.did_profile = False

def log_likelihood_function(self, instance):
Expand All @@ -23,6 +24,10 @@ def visualize(self, paths: AbstractPaths, instance, during_analysis):
os.makedirs(paths.image_path)
open(f"{paths.image_path}/image.png", "w+").close()

def visualize_combined(self, analyses, paths: AbstractPaths, instance, during_analysis):

self.did_visualise_combined = True

def profile_log_likelihood_function(self, paths: AbstractPaths, instance):
self.did_profile = True

Expand All @@ -37,6 +42,17 @@ def test_visualise():
assert analysis_2.did_visualise is True


def test_visualise_combined():
analysis_1 = Analysis()
analysis_2 = Analysis()

(analysis_1 + analysis_2).visualize_combined(af.DirectoryPaths(), None, None)

assert analysis_1.did_visualise_combined is True
assert analysis_2.did_visualise_combined is False



def test__profile_log_likelihood():
analysis_1 = Analysis()
analysis_2 = Analysis()
Expand Down

0 comments on commit d92120d

Please sign in to comment.