From 0cc25035175f7044cb6e84655482244fd818bd2b Mon Sep 17 00:00:00 2001 From: James Nightingale Date: Mon, 25 Jul 2022 15:28:52 +0100 Subject: [PATCH 1/5] LINE_LENGTH now uses config --- autofit/config/general.ini | 1 + autofit/mapper/prior_model/abstract.py | 6 ++++-- autofit/text/samples_text.py | 10 ++++++++-- autofit/text/text_util.py | 19 ++++++++++--------- test_autofit/config/general.ini | 1 + 5 files changed, 24 insertions(+), 13 deletions(-) diff --git a/autofit/config/general.ini b/autofit/config/general.ini index 4d2d5cb36..d8831acf7 100644 --- a/autofit/config/general.ini +++ b/autofit/config/general.ini @@ -4,6 +4,7 @@ log_file=output.log log_level=INFO samples_to_csv=False model_results_decimal_places=3 +info_line_length=80 remove_files=False force_pickle_overwrite=False identifier_version=4 diff --git a/autofit/mapper/prior_model/abstract.py b/autofit/mapper/prior_model/abstract.py index eb136cad3..dd70f05c7 100644 --- a/autofit/mapper/prior_model/abstract.py +++ b/autofit/mapper/prior_model/abstract.py @@ -32,8 +32,6 @@ __name__ ) -LINE_LENGTH = 80 - class Limits: @staticmethod @@ -1554,6 +1552,8 @@ def info(self) -> str: parameter of the overall model. This information is extracted from each priors *model_info* property. """ + LINE_LENGTH = conf.instance["general"]["output"]["info_line_length"] + formatter = TextFormatter( line_length=LINE_LENGTH ) @@ -1602,6 +1602,8 @@ def parameterization(self) -> str: """ from .prior_model import PriorModel + LINE_LENGTH = conf.instance["general"]["output"]["info_line_length"] + formatter = TextFormatter( line_length=LINE_LENGTH ) diff --git a/autofit/text/samples_text.py b/autofit/text/samples_text.py index 970b0ce33..8e0b0be4a 100644 --- a/autofit/text/samples_text.py +++ b/autofit/text/samples_text.py @@ -1,10 +1,11 @@ import logging +from autoconf import conf + from autofit.text import formatter as frm logger = logging.getLogger(__name__) - def values_from_samples(samples, median_pdf_model): if median_pdf_model: @@ -15,7 +16,8 @@ def values_from_samples(samples, median_pdf_model): def summary( samples, sigma=3.0, median_pdf_model=True, indent=1, line_length=None ) -> str: - """ Create a string summarizing the results of the `NonLinearSearch` at an input sigma value. + """ + Create a string summarizing the results of the `NonLinearSearch` at an input sigma value. This function is used for creating the model.results files of a non-linear search. @@ -24,6 +26,8 @@ def summary( sigma The sigma within which the PDF is used to estimate errors (e.g. sigma = 1.0 uses 0.6826 of the PDF).""" + LINE_LENGTH = conf.instance["general"]["output"]["info_line_length"] + values = values_from_samples(samples=samples, median_pdf_model=median_pdf_model) values_at_sigma = samples.vector_at_sigma(sigma=sigma) @@ -32,6 +36,8 @@ def summary( if line_length is None: line_length = len(max(parameter_names, key=len)) + 8 + line_length = line_length or LINE_LENGTH + sigma_formatter = frm.TextFormatter(indent=indent, line_length=line_length) for i, prior_path in enumerate(samples.model.unique_prior_paths): diff --git a/autofit/text/text_util.py b/autofit/text/text_util.py index 6ab72f922..fc4e162df 100644 --- a/autofit/text/text_util.py +++ b/autofit/text/text_util.py @@ -1,7 +1,6 @@ from autoconf import conf from autofit.text import formatter as frm, samples_text - def padding(item, target=6): string = str(item) difference = target - len(string) @@ -15,6 +14,8 @@ def result_info_from(samples) -> str: sigma confidence and information on the maximum log likelihood. """ + LINE_LENGTH = conf.instance["general"]["output"]["info_line_length"] + results = [] if hasattr(samples, "log_evidence"): @@ -24,7 +25,7 @@ def result_info_from(samples) -> str: frm.add_whitespace( str0="Bayesian Evidence ", str1="{:.8f}".format(samples.log_evidence), - whitespace=90 + whitespace=LINE_LENGTH ) ] results += ["\n"] @@ -33,7 +34,7 @@ def result_info_from(samples) -> str: frm.add_whitespace( str0="Maximum Log Likelihood ", str1="{:.8f}".format(max(samples.log_likelihood_list)), - whitespace=90 + whitespace=LINE_LENGTH ) ] results += ["\n"] @@ -41,7 +42,7 @@ def result_info_from(samples) -> str: frm.add_whitespace( str0="Maximum Log Posterior ", str1="{:.8f}".format(max(samples.log_posterior_list)), - whitespace=90 + whitespace=LINE_LENGTH ) ] results += ["\n"] @@ -50,7 +51,7 @@ def result_info_from(samples) -> str: results += ["Maximum Log Likelihood Model:\n\n"] - formatter = frm.TextFormatter() + formatter = frm.TextFormatter(line_length=LINE_LENGTH) for i, prior_path in enumerate(samples.model.unique_prior_paths): formatter.add( @@ -62,9 +63,9 @@ def result_info_from(samples) -> str: if samples.pdf_converged: - results += samples_text.summary(samples=samples, sigma=3.0, indent=4, line_length=90) + results += samples_text.summary(samples=samples, sigma=3.0, indent=4, line_length=LINE_LENGTH) results += ["\n"] - results += samples_text.summary(samples=samples, sigma=1.0, indent=4, line_length=90) + results += samples_text.summary(samples=samples, sigma=1.0, indent=4, line_length=LINE_LENGTH) else: @@ -72,11 +73,11 @@ def result_info_from(samples) -> str: "\n WARNING: The samples have not converged enough to compute a PDF and model errors. \n " "The model below over estimates errors. \n\n" ] - results += samples_text.summary(samples=samples, sigma=1.0, indent=4, line_length=90) + results += samples_text.summary(samples=samples, sigma=1.0, indent=4, line_length=LINE_LENGTH) results += ["\n\ninstances\n"] - formatter = frm.TextFormatter() + formatter = frm.TextFormatter(line_length=LINE_LENGTH) for t in samples.model.path_float_tuples: formatter.add(*t) diff --git a/test_autofit/config/general.ini b/test_autofit/config/general.ini index bc81dbc7c..2fd4726b2 100644 --- a/test_autofit/config/general.ini +++ b/test_autofit/config/general.ini @@ -4,6 +4,7 @@ log_file=output.log log_level=INFO samples_to_csv=False model_results_decimal_places=3 +info_line_length=80 remove_files=True force_pickle_overwrite=False identifier_version=3 From d9d75fb5bfa3e7aa9499db2c026c99aeb31f7674 Mon Sep 17 00:00:00 2001 From: James Nightingale Date: Mon, 25 Jul 2022 15:29:33 +0100 Subject: [PATCH 2/5] fixed test_util test --- test_autofit/text/test_text_util.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_autofit/text/test_text_util.py b/test_autofit/text/test_text_util.py index 5df23f0bd..6b7eed16d 100644 --- a/test_autofit/text/test_text_util.py +++ b/test_autofit/text/test_text_util.py @@ -37,7 +37,7 @@ def test__results_to_file(samples): samples=samples, ) - assert "Maximum Log Likelihood 1.00000000\n" in result_info + assert "Maximum Log Likelihood 1.00000000\n" in result_info def test__search_summary_to_file(model): file_search_summary = path.join(text_path, "search.summary") From d7d571e3da65ff79ac7b19925969329f875d4e07 Mon Sep 17 00:00:00 2001 From: James Nightingale Date: Mon, 25 Jul 2022 15:32:15 +0100 Subject: [PATCH 3/5] renamed info_line_length to info_whitespace_length --- autofit/config/general.ini | 2 +- autofit/mapper/prior_model/abstract.py | 4 ++-- autofit/text/samples_text.py | 2 +- autofit/text/text_util.py | 2 +- test_autofit/config/general.ini | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/autofit/config/general.ini b/autofit/config/general.ini index d8831acf7..d9ca45a14 100644 --- a/autofit/config/general.ini +++ b/autofit/config/general.ini @@ -4,7 +4,7 @@ log_file=output.log log_level=INFO samples_to_csv=False model_results_decimal_places=3 -info_line_length=80 +info_whitespace_length=80 remove_files=False force_pickle_overwrite=False identifier_version=4 diff --git a/autofit/mapper/prior_model/abstract.py b/autofit/mapper/prior_model/abstract.py index dd70f05c7..591a8db47 100644 --- a/autofit/mapper/prior_model/abstract.py +++ b/autofit/mapper/prior_model/abstract.py @@ -1552,7 +1552,7 @@ def info(self) -> str: parameter of the overall model. This information is extracted from each priors *model_info* property. """ - LINE_LENGTH = conf.instance["general"]["output"]["info_line_length"] + LINE_LENGTH = conf.instance["general"]["output"]["info_whitespace_length"] formatter = TextFormatter( line_length=LINE_LENGTH @@ -1602,7 +1602,7 @@ def parameterization(self) -> str: """ from .prior_model import PriorModel - LINE_LENGTH = conf.instance["general"]["output"]["info_line_length"] + LINE_LENGTH = conf.instance["general"]["output"]["info_whitespace_length"] formatter = TextFormatter( line_length=LINE_LENGTH diff --git a/autofit/text/samples_text.py b/autofit/text/samples_text.py index 8e0b0be4a..76811e165 100644 --- a/autofit/text/samples_text.py +++ b/autofit/text/samples_text.py @@ -26,7 +26,7 @@ def summary( sigma The sigma within which the PDF is used to estimate errors (e.g. sigma = 1.0 uses 0.6826 of the PDF).""" - LINE_LENGTH = conf.instance["general"]["output"]["info_line_length"] + LINE_LENGTH = conf.instance["general"]["output"]["info_whitespace_length"] values = values_from_samples(samples=samples, median_pdf_model=median_pdf_model) values_at_sigma = samples.vector_at_sigma(sigma=sigma) diff --git a/autofit/text/text_util.py b/autofit/text/text_util.py index fc4e162df..cb27c2809 100644 --- a/autofit/text/text_util.py +++ b/autofit/text/text_util.py @@ -14,7 +14,7 @@ def result_info_from(samples) -> str: sigma confidence and information on the maximum log likelihood. """ - LINE_LENGTH = conf.instance["general"]["output"]["info_line_length"] + LINE_LENGTH = conf.instance["general"]["output"]["info_whitespace_length"] results = [] diff --git a/test_autofit/config/general.ini b/test_autofit/config/general.ini index 2fd4726b2..26040ce94 100644 --- a/test_autofit/config/general.ini +++ b/test_autofit/config/general.ini @@ -4,7 +4,7 @@ log_file=output.log log_level=INFO samples_to_csv=False model_results_decimal_places=3 -info_line_length=80 +info_whitespace_length=80 remove_files=True force_pickle_overwrite=False identifier_version=3 From 1c43ab14d8a5789b4ee730c1395e1abda0c8bf38 Mon Sep 17 00:00:00 2001 From: Richard Date: Fri, 29 Jul 2022 09:24:13 +0100 Subject: [PATCH 4/5] fixed test --- test_autofit/non_linear/test_analysis.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test_autofit/non_linear/test_analysis.py b/test_autofit/non_linear/test_analysis.py index fb9579da5..4a40954e5 100644 --- a/test_autofit/non_linear/test_analysis.py +++ b/test_autofit/non_linear/test_analysis.py @@ -213,7 +213,7 @@ def test_set_number_of_cores( multi_analysis.n_cores = 1 assert multi_analysis._log_likelihood_function.__name__ == "_summed_log_likelihood" - multi_analysis.n_cores = 1 + multi_analysis.n_cores = 2 assert isinstance( multi_analysis._log_likelihood_function, AnalysisPool From 895d9ee85aedb0c86ecbaf5aff190df539ca8f3d Mon Sep 17 00:00:00 2001 From: James Nightingale Date: Fri, 29 Jul 2022 15:03:04 +0100 Subject: [PATCH 5/5] info whitespace config call now function --- autofit/mapper/prior_model/abstract.py | 8 +++----- autofit/text/samples_text.py | 6 ------ autofit/text/text_util.py | 20 +++++++++----------- autofit/tools/util.py | 5 +++++ 4 files changed, 17 insertions(+), 22 deletions(-) diff --git a/autofit/mapper/prior_model/abstract.py b/autofit/mapper/prior_model/abstract.py index 591a8db47..acd53b1cd 100644 --- a/autofit/mapper/prior_model/abstract.py +++ b/autofit/mapper/prior_model/abstract.py @@ -27,6 +27,7 @@ from autofit.text import formatter as frm from autofit.text.formatter import TextFormatter from autofit.tools.util import split_paths +from autofit.tools.util import info_whitespace logger = logging.getLogger( __name__ @@ -1552,10 +1553,9 @@ def info(self) -> str: parameter of the overall model. This information is extracted from each priors *model_info* property. """ - LINE_LENGTH = conf.instance["general"]["output"]["info_whitespace_length"] formatter = TextFormatter( - line_length=LINE_LENGTH + line_length=info_whitespace() ) for t in self.path_instance_tuples_for_class( @@ -1602,10 +1602,8 @@ def parameterization(self) -> str: """ from .prior_model import PriorModel - LINE_LENGTH = conf.instance["general"]["output"]["info_whitespace_length"] - formatter = TextFormatter( - line_length=LINE_LENGTH + line_length=info_whitespace() ) for t in self.path_instance_tuples_for_class( diff --git a/autofit/text/samples_text.py b/autofit/text/samples_text.py index 76811e165..cb47cabd9 100644 --- a/autofit/text/samples_text.py +++ b/autofit/text/samples_text.py @@ -1,7 +1,5 @@ import logging -from autoconf import conf - from autofit.text import formatter as frm logger = logging.getLogger(__name__) @@ -26,8 +24,6 @@ def summary( sigma The sigma within which the PDF is used to estimate errors (e.g. sigma = 1.0 uses 0.6826 of the PDF).""" - LINE_LENGTH = conf.instance["general"]["output"]["info_whitespace_length"] - values = values_from_samples(samples=samples, median_pdf_model=median_pdf_model) values_at_sigma = samples.vector_at_sigma(sigma=sigma) @@ -36,8 +32,6 @@ def summary( if line_length is None: line_length = len(max(parameter_names, key=len)) + 8 - line_length = line_length or LINE_LENGTH - sigma_formatter = frm.TextFormatter(indent=indent, line_length=line_length) for i, prior_path in enumerate(samples.model.unique_prior_paths): diff --git a/autofit/text/text_util.py b/autofit/text/text_util.py index cb27c2809..ce8a7f55a 100644 --- a/autofit/text/text_util.py +++ b/autofit/text/text_util.py @@ -1,5 +1,6 @@ from autoconf import conf from autofit.text import formatter as frm, samples_text +from autofit.tools.util import info_whitespace def padding(item, target=6): string = str(item) @@ -13,9 +14,6 @@ def result_info_from(samples) -> str: Output the full model.results file, which include the most-likely model, most-probable model at 1 and 3 sigma confidence and information on the maximum log likelihood. """ - - LINE_LENGTH = conf.instance["general"]["output"]["info_whitespace_length"] - results = [] if hasattr(samples, "log_evidence"): @@ -25,7 +23,7 @@ def result_info_from(samples) -> str: frm.add_whitespace( str0="Bayesian Evidence ", str1="{:.8f}".format(samples.log_evidence), - whitespace=LINE_LENGTH + whitespace=info_whitespace() ) ] results += ["\n"] @@ -34,7 +32,7 @@ def result_info_from(samples) -> str: frm.add_whitespace( str0="Maximum Log Likelihood ", str1="{:.8f}".format(max(samples.log_likelihood_list)), - whitespace=LINE_LENGTH + whitespace=info_whitespace() ) ] results += ["\n"] @@ -42,7 +40,7 @@ def result_info_from(samples) -> str: frm.add_whitespace( str0="Maximum Log Posterior ", str1="{:.8f}".format(max(samples.log_posterior_list)), - whitespace=LINE_LENGTH + whitespace=info_whitespace() ) ] results += ["\n"] @@ -51,7 +49,7 @@ def result_info_from(samples) -> str: results += ["Maximum Log Likelihood Model:\n\n"] - formatter = frm.TextFormatter(line_length=LINE_LENGTH) + formatter = frm.TextFormatter(line_length=info_whitespace()) for i, prior_path in enumerate(samples.model.unique_prior_paths): formatter.add( @@ -63,9 +61,9 @@ def result_info_from(samples) -> str: if samples.pdf_converged: - results += samples_text.summary(samples=samples, sigma=3.0, indent=4, line_length=LINE_LENGTH) + results += samples_text.summary(samples=samples, sigma=3.0, indent=4, line_length=info_whitespace()) results += ["\n"] - results += samples_text.summary(samples=samples, sigma=1.0, indent=4, line_length=LINE_LENGTH) + results += samples_text.summary(samples=samples, sigma=1.0, indent=4, line_length=info_whitespace()) else: @@ -73,11 +71,11 @@ def result_info_from(samples) -> str: "\n WARNING: The samples have not converged enough to compute a PDF and model errors. \n " "The model below over estimates errors. \n\n" ] - results += samples_text.summary(samples=samples, sigma=1.0, indent=4, line_length=LINE_LENGTH) + results += samples_text.summary(samples=samples, sigma=1.0, indent=4, line_length=info_whitespace()) results += ["\n\ninstances\n"] - formatter = frm.TextFormatter(line_length=LINE_LENGTH) + formatter = frm.TextFormatter(line_length=info_whitespace()) for t in samples.model.path_float_tuples: formatter.add(*t) diff --git a/autofit/tools/util.py b/autofit/tools/util.py index ca69f7b00..37f848733 100644 --- a/autofit/tools/util.py +++ b/autofit/tools/util.py @@ -8,6 +8,7 @@ import numpy as np +from autoconf import conf def split_paths(func): """ @@ -141,3 +142,7 @@ def numpy_array_from_json(file_path: str): """ with open(file_path, "r") as f: return np.asarray(json.load(f)) + + +def info_whitespace(): + return conf.instance["general"]["output"]["info_whitespace_length"] \ No newline at end of file