Exemple #1
0
def test_exclude_identifier_fields():
    other = ExcludeClass(three=4)
    assert Identifier(other) == Identifier(ExcludeClass())

    other.__exclude_identifier_fields__ = tuple()

    assert Identifier(other) != Identifier(ExcludeClass())
Exemple #2
0
def test_identifier_version():
    set_version(1)
    identifier = Identifier(af.Gaussian())

    set_version(2)
    assert identifier != Identifier(af.Gaussian())

    assert identifier == Identifier(af.Gaussian(), version=1)
Exemple #3
0
def test_change_class():
    gaussian_0 = af.Model(af.Gaussian,
                          normalization=af.UniformPrior(lower_limit=1e-6,
                                                        upper_limit=1e6))
    gaussian_1 = af.Model(af.Gaussian,
                          normalization=af.LogUniformPrior(lower_limit=1e-6,
                                                           upper_limit=1e6))

    assert Identifier(gaussian_0) != Identifier(gaussian_1)
Exemple #4
0
def test_constructor_only():
    attribute = AttributeClass()
    attribute.attribute = 1

    assert Identifier(
        AttributeClass()
    ) == Identifier(
        attribute
    )
Exemple #5
0
def test_tiny_change():
    # noinspection PyTypeChecker
    instance = Class(one=1.0)
    identifier = str(Identifier(instance))

    instance.one += 1e-9
    print(instance.one)

    assert identifier == Identifier(instance)
Exemple #6
0
def test_identifier_fields():
    other = Class(three=4)
    assert Identifier(
        Class()
    ) == Identifier(
        other
    )

    other.__identifier_fields__ = ("one", "two", "three")
    assert Identifier(
        Class()
    ) != Identifier(
        other
    )
Exemple #7
0
def test_missing_field():
    instance = Class()
    instance.__identifier_fields__ = ("five",)

    with pytest.raises(
            AssertionError
    ):
        Identifier(
            instance
        )
    def identifier(self):
        if None in (self.model, self.search):
            logger.warn("Both model and search should be set")

        if self._identifier is None:
            identifier_list = [self.search, self.model]

            if self.unique_tag is not None:
                identifier_list.append(self.unique_tag)
            self._identifier = str(Identifier(identifier_list))
        return self._identifier
Exemple #9
0
    def identifier(self):
        if None in (self.model, self.search):
            logger.warn("Both model and search should be set")

        if self._identifier is None:
            identifier_list = [self.search, self.model]

            if self.unique_tag is not None:
                identifier_list.append(self.unique_tag)
            identifier = Identifier(identifier_list)
            self._identifier = str(identifier)
            with open(f"{self._sym_path}/.identifier", "w+") as f:
                f.write(identifier.description)

        return self._identifier
Exemple #10
0
def test__identifier_description():

    model = af.CollectionPriorModel(gaussian=af.PriorModel(
        af.Gaussian,
        centre=af.UniformPrior(lower_limit=0.0, upper_limit=1.0),
        normalization=af.LogUniformPrior(lower_limit=0.001, upper_limit=0.01),
        sigma=af.GaussianPrior(
            mean=0.5, sigma=2.0, lower_limit=-1.0, upper_limit=1.0),
    ))

    identifier = Identifier([model])

    description = identifier.description.splitlines()

    i = 0

    assert description[i] == "CollectionPriorModel"
    i += 1
    assert description[i] == "item_number"
    i += 1
    assert description[i] == "0"
    i += 1
    assert description[i] == "gaussian"
    i += 1
    assert description[i] == "PriorModel"
    i += 1
    assert description[i] == "cls"
    i += 1
    assert description[i] == "autofit.example.model.Gaussian"
    i += 1
    assert description[i] == "centre"
    i += 1
    assert description[i] == "UniformPrior"
    i += 1
    assert description[i] == "lower_limit"
    i += 1
    assert description[i] == "0.0"
    i += 1
    assert description[i] == "upper_limit"
    i += 1
    assert description[i] == "1.0"
    i += 1
    assert description[i] == "normalization"
    i += 1
    assert description[i] == "LogUniformPrior"
    i += 1
    assert description[i] == "lower_limit"
    i += 1
    assert description[i] == "0.001"
    i += 1
    assert description[i] == "upper_limit"
    i += 1
    assert description[i] == "0.01"
    i += 1
    assert description[i] == "sigma"
    i += 1
    assert description[i] == "GaussianPrior"
    i += 1
    assert description[i] == "lower_limit"
    i += 1
    assert description[i] == "-1.0"
    i += 1
    assert description[i] == "upper_limit"
    i += 1
    assert description[i] == "1.0"
    i += 1
    assert description[i] == "mean"
    i += 1
    assert description[i] == "0.5"
    i += 1
    assert description[i] == "sigma"
    i += 1
    assert description[i] == "2.0"
    i += 1
Exemple #11
0
def test_hash_list():
    identifier = Identifier(Class())
    assert identifier.hash_list == ["Class", "one", "1", "two", "2"]
Exemple #12
0
def test_exclude_does_no_effect_constructor():
    attribute = AttributeClass()
    attribute.__exclude_identifier_fields__ = tuple()
    attribute.attribute = 1

    assert Identifier(AttributeClass()) == Identifier(attribute)
Exemple #13
0
def test_numpy_array():
    identifier = Identifier(np.array([0]))
    assert identifier.hash_list == []
Exemple #14
0
def test_config_error():
    model = af.Model(SomeWeirdClass)

    with pytest.raises(ConfigException):
        print(Identifier([model]))
Exemple #15
0
def test__identifier_description__after_take_attributes():

    model = af.CollectionPriorModel(gaussian=af.PriorModel(
        af.Gaussian,
        centre=af.UniformPrior(lower_limit=0.0, upper_limit=1.0),
        normalization=af.LogUniformPrior(lower_limit=0.001, upper_limit=0.01),
        sigma=af.GaussianPrior(
            mean=0.5, sigma=2.0, lower_limit=-1.0, upper_limit=1.0),
    ))

    model.take_attributes(source=model)

    identifier = Identifier([model])

    description = identifier.description.splitlines()

    # THIS TEST FAILS DUE TO THE BUG DESCRIBED IN A GITHUB ISSUE.

    i = 0

    assert description[i] == "CollectionPriorModel"
    i += 1
    assert description[i] == "item_number"
    i += 1
    assert description[i] == "0"
    i += 1
    assert description[i] == "gaussian"
    i += 1
    assert description[i] == "PriorModel"
    i += 1
    assert description[i] == "cls"
    i += 1
    assert description[i] == "autofit.example.model.Gaussian"
    i += 1
    assert description[i] == "centre"
    i += 1
    assert description[i] == "UniformPrior"
    i += 1
    assert description[i] == "lower_limit"
    i += 1
    assert description[i] == "0.0"
    i += 1
    assert description[i] == "upper_limit"
    i += 1
    assert description[i] == "1.0"
    i += 1
    assert description[i] == "normalization"
    i += 1
    assert description[i] == "LogUniformPrior"
    i += 1
    assert description[i] == "lower_limit"
    i += 1
    assert description[i] == "0.001"
    i += 1
    assert description[i] == "upper_limit"
    i += 1
    assert description[i] == "0.01"
    i += 1
    assert description[i] == "sigma"
    i += 1
    assert description[i] == "GaussianPrior"
    i += 1
    assert description[i] == "lower_limit"
    i += 1
    assert description[i] == "-1.0"
    i += 1
    assert description[i] == "upper_limit"
    i += 1
    assert description[i] == "1.0"
    i += 1
    assert description[i] == "mean"
    i += 1
    assert description[i] == "0.5"
    i += 1
    assert description[i] == "sigma"
    i += 1
    assert description[i] == "2.0"
    i += 1
Exemple #16
0
def test_class_path():
    identifier = Identifier(Class, version=3)
    string, = identifier.hash_list
    assert "test_autofit.database.identifier.test_identifiers.Class" in string
Exemple #17
0
def test_private_not_included():
    instance = PrivateClass(argument="one")
    identifier = str(Identifier(instance))

    instance._argument = "two"
    assert Identifier(instance) == identifier
Exemple #18
0
def test_infinity():
    # noinspection PyTypeChecker
    instance = Class(one=float("inf"))
    str(Identifier(instance))
Exemple #19
0
def test__identifier_description__after_model_and_instance():

    model = af.CollectionPriorModel(gaussian=af.PriorModel(
        af.Gaussian,
        centre=af.UniformPrior(lower_limit=0.0, upper_limit=1.0),
        normalization=af.LogUniformPrior(lower_limit=0.001, upper_limit=0.01),
        sigma=af.GaussianPrior(
            mean=0.5, sigma=2.0, lower_limit=-1.0, upper_limit=1.0),
    ))

    max_log_likelihood_instance = model.instance_from_prior_medians()

    samples = af.m.MockSamples(
        max_log_likelihood_instance=max_log_likelihood_instance,
        gaussian_tuples=[(1.0, 2.0), (3.0, 4.0), (5.0, 6.0)])
    search = af.m.MockSearch(prior_passer=af.PriorPasser(
        sigma=1.0, use_errors=True, use_widths=False))

    result = af.Result(samples=samples, model=model, search=search)

    model.gaussian.centre = result.model.gaussian.centre
    model.gaussian.normalization = result.instance.gaussian.normalization

    identifier = Identifier([model])

    description = identifier.description.splitlines()

    # THIS TEST FAILS DUE TO THE BUG DESCRIBED IN A GITHUB ISSUE.

    i = 0

    assert description[i] == "CollectionPriorModel"
    i += 1
    assert description[i] == "item_number"
    i += 1
    assert description[i] == "0"
    i += 1
    assert description[i] == "gaussian"
    i += 1
    assert description[i] == "PriorModel"
    i += 1
    assert description[i] == "cls"
    i += 1
    assert description[i] == "autofit.example.model.Gaussian"
    i += 1
    assert description[i] == "centre"
    i += 1
    assert description[i] == "GaussianPrior"
    i += 1
    assert description[i] == "lower_limit"
    i += 1
    assert description[i] == "0.0"
    i += 1
    assert description[i] == "upper_limit"
    i += 1
    assert description[i] == "1.0"
    i += 1
    assert description[i] == "mean"
    i += 1
    assert description[i] == "1.0"
    i += 1
    assert description[i] == "sigma"
    i += 1
    assert description[i] == "2.0"
    i += 1
    assert description[i] == "normalization"
    i += 1
    assert description[i] == "0.00316228"
    i += 1
    assert description[i] == "sigma"
    i += 1
    assert description[i] == "GaussianPrior"
    i += 1
    assert description[i] == "lower_limit"
    i += 1
    assert description[i] == "-1.0"
    i += 1
    assert description[i] == "upper_limit"
    i += 1
    assert description[i] == "1.0"
    i += 1
    assert description[i] == "mean"
    i += 1
    assert description[i] == "0.5"
    i += 1
    assert description[i] == "sigma"
    i += 1
    assert description[i] == "2.0"
    i += 1