diff --git a/autofit/non_linear/abstract.py b/autofit/non_linear/abstract.py index 44dd5637e..7e5d85e23 100644 --- a/autofit/non_linear/abstract.py +++ b/autofit/non_linear/abstract.py @@ -1,19 +1,19 @@ import configparser -from abc import ABC, abstractmethod import logging -import pickle -import numpy as np import multiprocessing as mp +import pickle +from abc import ABC, abstractmethod from time import sleep from typing import Dict -from copy import copy + +import numpy as np from autoconf import conf from autofit.mapper import model_mapper as mm from autofit.non_linear.paths import Paths, convert_paths from autofit.text import formatter -from autofit.text import samples_text from autofit.text import model_text +from autofit.text import samples_text logging.basicConfig() logger = logging.getLogger(__name__) # TODO: Logging issue @@ -67,7 +67,7 @@ def __init__( paths.non_linear_name = self.config("tag", "name", str) if paths.non_linear_tag is "": - paths.non_linear_tag = self.tag + paths.non_linear_tag_function = lambda: self.tag log_file = conf.instance.general.get("output", "log_file", str).replace(" ", "") self.paths = paths @@ -452,7 +452,7 @@ def initial_points_from_model(self, number_of_points, model): An object that represents possible instances of some model with a given dimensionality which is the number of free dimensions of the model. """ - + init_pos = np.zeros(shape=(number_of_points, model.prior_count)) if self.initialize_method in "ball": @@ -514,7 +514,6 @@ def make_pool(self): class Analysis: def __init__(self, log_likelihood_cap=None): - self.log_likelihood_cap = log_likelihood_cap def log_likelihood_function(self, instance): @@ -630,8 +629,9 @@ def init(queue): global idx idx = queue.get() + def f(x): global idx process = mp.current_process() sleep(1) - return (idx, process.pid, x * x) \ No newline at end of file + return (idx, process.pid, x * x) diff --git a/autofit/non_linear/paths.py b/autofit/non_linear/paths.py index 0cb255b35..a0a261daf 100644 --- a/autofit/non_linear/paths.py +++ b/autofit/non_linear/paths.py @@ -52,12 +52,16 @@ def wrapper(self, *args, **kwargs): non_linear_instance = kwargs["non_linear_class"]() non_linear_name = non_linear_instance.config("tag", "name", str) - non_linear_tag = non_linear_instance.tag + + def non_linear_tag_function(): + return non_linear_instance.tag else: non_linear_name = None - non_linear_tag = None + + def non_linear_tag_function(): + return "" func( self, @@ -67,7 +71,7 @@ def wrapper(self, *args, **kwargs): folders=kwargs.pop("phase_folders", tuple()), path_prefix=kwargs.pop("phase_path", None), non_linear_name=non_linear_name, - non_linear_tag=non_linear_tag, + non_linear_tag_function=non_linear_tag_function, remove_files=remove_files, ), **kwargs, @@ -84,7 +88,7 @@ def __init__( folders=tuple(), path_prefix=None, non_linear_name=None, - non_linear_tag=None, + non_linear_tag_function=lambda: "", remove_files=False, ): """Manages the path structure for non-linear search output, for analyses both not using and using the phase @@ -134,9 +138,27 @@ def __init__( self.name = name or "" self.tag = tag or "" self.non_linear_name = non_linear_name or "" - self.non_linear_tag = non_linear_tag or "" + self.non_linear_tag_function = non_linear_tag_function self.remove_files = remove_files + def __getstate__(self): + state = self.__dict__.copy() + state["non_linear_tag"] = state.pop("non_linear_tag_function")() + return state + + def __setstate__(self, state): + non_linear_tag = state.pop( + "non_linear_tag" + ) + self.non_linear_tag_function = lambda: non_linear_tag + self.__dict__.update( + state + ) + + @property + def non_linear_tag(self): + return self.non_linear_tag_function() + @property def path(self): return link.make_linked_folder(self.sym_path) @@ -212,6 +234,7 @@ def name_folder(self): return "/".join((conf.instance.output_path, self.path_prefix, self.name)) @property + @make_path def sym_path(self) -> str: return "{}/{}/{}/{}/{}/samples".format( conf.instance.output_path, self.path_prefix, self.name, self.tag, self.non_linear_tag diff --git a/test_autofit/unit/tools/test_paths.py b/test_autofit/unit/tools/test_paths.py index 877934312..f6bfaedb9 100644 --- a/test_autofit/unit/tools/test_paths.py +++ b/test_autofit/unit/tools/test_paths.py @@ -8,6 +8,15 @@ directory = path.dirname(path.realpath(__file__)) +def test_dynamic(phase): + paths = phase.paths + non_linear_tag = paths.non_linear_tag + + phase.search.nwalkers += 1 + + assert paths.non_linear_tag != non_linear_tag + + class PatchPaths(af.Paths): @property def path(self): @@ -34,7 +43,6 @@ def make_paths(): def test_backup_zip_remove(paths): - os.mkdir(paths.sym_path) os.mkdir(paths.path)