Skip to content
32 changes: 15 additions & 17 deletions autofit/__init__.py
Original file line number Diff line number Diff line change
@@ -1,37 +1,32 @@
from autoconf import conf
from autofit.mapper.model import path_instances_of_class
from autofit.mapper.prior_model.attribute_pair import (
from .mapper.model import path_instances_of_class
from .mapper.prior_model.attribute_pair import (
cast_collection,
AttributeNameValue,
PriorNameValue,
InstanceNameValue,
)

dir(conf)
from . import exc
from autofit.optimize.non_linear.samples import AbstractSamples, MCMCSamples, NestedSamplerSamples
from .optimize.non_linear.samples import AbstractSamples, MCMCSamples, NestedSamplerSamples
from .aggregator import Aggregator, PhaseOutput
from .mapper import *
from .mapper import link
from .mapper.model import AbstractModel
from .mapper.model import ModelInstance
from .mapper.model import ModelInstance as Instance
from .mapper.model_mapper import ModelMapper
from .mapper.model_mapper import ModelMapper as Mapper
from .mapper.model_object import ModelObject
from .mapper.prior_model import *
from .mapper.prior_model.abstract import AbstractPriorModel
from .mapper.prior_model.annotation import AnnotationPriorModel
from .mapper.prior_model.collection import CollectionPriorModel
from .mapper.prior_model.collection import CollectionPriorModel as Collection
from autofit.mapper.prior.deferred import DeferredArgument
from autofit.mapper.prior.deferred import DeferredInstance
from .mapper.prior.deferred import DeferredArgument
from .mapper.prior.deferred import DeferredInstance
from .mapper.prior_model.dimension_type import DimensionType, map_types
from .mapper.prior_model.prior_model import PriorModel
from .mapper.prior_model.prior_model import PriorModel as Model
from .mapper.prior_model.util import PriorModelNameValue
from .optimize.grid_search import GridSearch as OptimizerGridSearch
from .optimize import *
from .optimize.non_linear.downhill_simplex import DownhillSimplex
from .optimize.non_linear.nested_sampling.dynesty import DynestyStatic, DynestyDynamic
from .optimize.grid_search import GridSearchResult
Expand All @@ -40,12 +35,11 @@
from .optimize.non_linear.non_linear import Analysis
from .optimize.non_linear.non_linear import NonLinearOptimizer
from .optimize.non_linear.emcee import Emcee
from autofit.optimize.non_linear.paths import Paths
from autofit.optimize.non_linear.paths import make_path
from autofit.optimize.non_linear.paths import convert_paths
from .optimize.non_linear.paths import Paths
from .optimize.non_linear.paths import make_path
from .optimize.non_linear.paths import convert_paths
from .optimize.non_linear.non_linear import Result
from .text import formatter, samples_text
from .tools import *
from .tools import path_util
from .tools.phase import AbstractPhase
from .tools.phase import Phase
Expand All @@ -54,8 +48,12 @@
from .tools.phase_property import PhaseProperty
from .tools.pipeline import Pipeline
from .tools.pipeline import ResultsCollection
from autofit.mapper.prior import AbstractPromise
from autofit.mapper.prior import last
from .mapper.prior import *
from .mapper.prior import AbstractPromise
from .mapper.prior import last
from .mapper.prior import GaussianPrior
from .mapper.prior import UniformPrior
from .mapper.prior import LogUniformPrior
from .mapper.prior import Prior
from .mapper import prior

__version__ = '0.58.0'
1 change: 1 addition & 0 deletions autofit/conf.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from autoconf.conf import *
2 changes: 1 addition & 1 deletion autofit/mapper/prior/prior.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import numpy as np
from scipy.special import erfcinv

from autoconf import conf
from autofit import conf
from autofit import exc
from autofit.mapper.model_object import ModelObject
from autofit.mapper.prior.arithmetic import ArithmeticMixin
Expand Down
129 changes: 129 additions & 0 deletions autofit/tools/edenise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
import re
import shutil
from os import walk
from uuid import uuid1


class Line:
def __init__(self, string):
if "*" in string:
print("Please ensure no imports in the __init__ contain a *")
exit(1)
self.string = string.replace("\n", "")
self.id = str(uuid1())

def __str__(self):
return f"{self.source} -> {self.target}"

def __repr__(self):
return f"<{self.__class__.__name__} {self}>"

def __len__(self):
return len(self.source)

def __lt__(self, other):
return len(self) < len(other)

def __gt__(self, other):
return len(self) > len(other)

@property
def is_import(self):
return self.string.startswith("from")

@property
def source(self):
match = re.match("from .* as (.+)", self.string)
if match is not None:
return match.group(1)
return re.match("from .* import (.+)", self.string).group(1)

@property
def target(self):
return self.string.replace(
f" as {self.source}",
""
).replace(
"from ",
""
).replace(
" import ",
"."
).lstrip(
"."
)


class Converter:
def __init__(self, prefix, lines):
self.prefix = prefix
self.lines = sorted(
filter(
lambda line: line.is_import,
lines
),
reverse=True
)

@classmethod
def from_prefix_and_source_directory(
cls,
prefix,
source_directory
):
source_directory = source_directory
with open(
f"{source_directory}/__init__.py"
) as f:
lines = map(Line, f.readlines())
return Converter(prefix, lines)

def convert(self, string):
for line in self.lines:
source = f"{self.prefix}.{line.source}"
string = string.replace(
source,
line.id
)
for line in self.lines:
target = f"{self.prefix}.{line.target}"
string = string.replace(
line.id,
target
)
return string


def edenise(
root_directory,
name,
prefix
):
target_directory = f"{root_directory}/../{name}_eden"

print(f"Creating {target_directory}...")
shutil.copytree(
root_directory,
target_directory,
symlinks=True
)

converter = Converter.from_prefix_and_source_directory(
prefix=prefix,
source_directory=f"{root_directory}/{name}"
)

for root, _, files in walk(f"{target_directory}/test_{name}"):
for file in files:
if file.endswith(".py"):
with open(f"{root}/{file}", "r+") as f:
string = f.read()
f.seek(0)
f.write(
converter.convert(
string
)
)
f.truncate()

open(f"{target_directory}/{name}/__init__.py", "w+").close()
23 changes: 23 additions & 0 deletions scripts/edenise.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/usr/bin/env python

from sys import argv

from autofit.tools import edenise


def main():
try:
root_directory, name, prefix = argv[1:]
edenise.edenise(
root_directory,
name,
prefix
)
except ValueError:
print("Usage: ./edenise.py root_directory project_name import_prefix")
print("e.g.: ./edenise.py /path/to/autofit autofit af")
exit(1)


if __name__ == "__main__":
main()
2 changes: 1 addition & 1 deletion test_autofit/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import pytest

import autofit as af
from autoconf import conf
from autofit import conf
import shutil

directory = path.dirname(path.realpath(__file__))
Expand Down
7 changes: 3 additions & 4 deletions test_autofit/unit/mapper/model/test_model_mapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import pytest

import autofit as af
from test_autofit.mock import MockClassGaussian
import test_autofit.mock
from autofit import exc
from autofit.text import formatter as frm
from test_autofit import mock
from test_autofit.mock import GeometryProfile
from test_autofit.mock import MockClassGaussian

dataset_path = "{}/../".format(os.path.dirname(os.path.realpath(__file__)))

Expand Down Expand Up @@ -108,7 +108,7 @@ def test_with_instance(self):

def test_with_promise(self):
mm = af.ModelMapper()
mm.promise = af.Promise(
mm.promise = af.prior.Promise(
af.Phase(
phase_name="phase",
analysis_class=None
Expand Down Expand Up @@ -528,7 +528,6 @@ def test_log_priors_from_vector(self):
assert log_priors == [0.125, 0.2]

def test_random_vector_from_prior_within_limits(self):

np.random.seed(1)

mapper = af.ModelMapper()
Expand Down Expand Up @@ -864,7 +863,7 @@ def make_promise_mapper():
mapper = af.ModelMapper()
mapper.galaxy = af.PriorModel(
mock.Galaxy,
redshift=af.Promise(
redshift=af.prior.Promise(
None,
None,
is_instance=False,
Expand Down
6 changes: 3 additions & 3 deletions test_autofit/unit/mapper/promise/test_iteration.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ def test_index_type(self, phase):
promise_0 = phase.result.model.collection[0]
promise_1 = phase.result.model.collection[1]

assert isinstance(promise_0, af.Promise)
assert isinstance(promise_1, af.Promise)
assert isinstance(promise_0, af.prior.Promise)
assert isinstance(promise_1, af.prior.Promise)

def test_index_populate_model(self, phase, prior_0, prior_1, results_collection):
promise_0 = phase.result.model.collection[0]
Expand All @@ -73,4 +73,4 @@ def test_iteration(self, phase):
promises = list(phase.result.model.collection)

assert len(promises) == 2
assert all([isinstance(promise, af.Promise) for promise in promises])
assert all([isinstance(promise, af.prior.Promise) for promise in promises])
10 changes: 5 additions & 5 deletions test_autofit/unit/mapper/promise/test_promise.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ def test_does_not_contribute_to_prior_count(
assert model.prior_count == 0

def test_model_promise(self, model_promise, phase):
assert isinstance(model_promise, af.Promise)
assert isinstance(model_promise, af.prior.Promise)
assert model_promise.path == ("one", "redshift")
assert model_promise.is_instance is False
assert model_promise._phase is phase
Expand All @@ -190,7 +190,7 @@ def test_optional_in_sub(self, collection, phase):
assert result is None

def test_instance_promise(self, instance_promise, phase):
assert isinstance(instance_promise, af.Promise)
assert isinstance(instance_promise, af.prior.Promise)
assert instance_promise.path == ("one", "redshift")
assert instance_promise.is_instance is True
assert instance_promise._phase is phase
Expand Down Expand Up @@ -239,13 +239,13 @@ def test_kwarg_promise(self, profile_promise, collection):
def test_embedded_results(self, phase, collection):
hyper_result = phase.result.hyper_result

assert isinstance(hyper_result, af.PromiseResult)
assert isinstance(hyper_result, af.prior.PromiseResult)

model_promise = hyper_result.model
instance_promise = hyper_result.instance

assert isinstance(model_promise.hyper_galaxy, af.Promise)
assert isinstance(instance_promise.hyper_galaxy, af.Promise)
assert isinstance(model_promise.hyper_galaxy, af.prior.Promise)
assert isinstance(instance_promise.hyper_galaxy, af.prior.Promise)

model = model_promise.populate(collection)
instance = instance_promise.populate(collection)
Expand Down
14 changes: 7 additions & 7 deletions test_autofit/unit/mapper/test_assertion.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,29 +119,29 @@ def make_model(collection):
class TestPromiseAssertion:
def test_less_than(self, promise_model, collection, model):
promise = promise_model.axis_ratio < promise_model.phi
assert isinstance(promise, af.GreaterThanLessThanAssertion)
assert isinstance(promise, af.prior.GreaterThanLessThanAssertion)

assertion = promise.populate(collection)
assert isinstance(assertion, af.GreaterThanLessThanAssertion)
assert isinstance(assertion, af.prior.GreaterThanLessThanAssertion)

def test_greater_than(self, promise_model, collection, model):
promise = promise_model.axis_ratio > promise_model.phi
assert isinstance(promise, af.GreaterThanLessThanAssertion)
assert isinstance(promise, af.prior.GreaterThanLessThanAssertion)

def test_greater_than_equal(self, promise_model, collection, model):
promise = promise_model.axis_ratio >= promise_model.phi
assert isinstance(promise, af.GreaterThanLessThanEqualAssertion)
assert isinstance(promise, af.prior.GreaterThanLessThanEqualAssertion)

def test_integer_promise_assertion(self, promise_model, collection, model):
promise = promise_model.axis_ratio > 1.0
assert isinstance(promise, af.GreaterThanLessThanAssertion)
assert isinstance(promise, af.prior.GreaterThanLessThanAssertion)

def test_compound_assertion(self, promise_model, collection, model):
promise = (1.0 < promise_model.axis_ratio) < 1.0
assert isinstance(promise, af.CompoundAssertion)
assert isinstance(promise, af.prior.CompoundAssertion)

assertion = promise.populate(collection)
assert isinstance(assertion, af.CompoundAssertion)
assert isinstance(assertion, af.prior.CompoundAssertion)


class TestModel:
Expand Down
Loading