Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minor test coverage improvements #269

Merged
merged 4 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions smol/cofe/wrangling/wrangler.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,10 @@ def get_matching_corr_duplicate_indices(
self.structures[c[0]], self.structures[c[1]], symmetric=True
)
]
overlaps = list(filter(lambda s: s[0] & s[1], combinations(matches, 2)))
while overlaps: # change to := walrus when commiting to 3.8 only

while overlaps := list(
filter(lambda s: s[0] & s[1], combinations(matches, 2))
):
all_overlaps = [o for overlap in overlaps for o in overlap]
# keep only disjoint sets
matches = [s for s in matches if s not in all_overlaps]
Expand Down
17 changes: 0 additions & 17 deletions smol/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,6 @@
from typing import Any, Dict


def _repr(instance: object, **fields: Dict[str, Any]) -> str:
"""Create object representation.

A helper function for repr overloading in classes.
"""
attrs = []

for key, field in fields.items():
attrs.append(f"{key}={field!r}")

return (
(f"<{instance.__class__.__name__} {hex(id(instance))}({','.join(attrs)})>")
if len(attrs) == 0
else f"<{instance.__class__.__name__} {hex(id(instance))}>"
)


def class_name_from_str(class_str):
"""Return a class name based on given string.

Expand Down
34 changes: 34 additions & 0 deletions tests/test_cofe/test_cluster.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import json
import os
from itertools import combinations

import numpy as np
import pytest
from ruamel import yaml

from smol.cofe.space import Cluster
from smol.cofe.space.domain import get_site_spaces
Expand Down Expand Up @@ -55,3 +58,34 @@ def test_msonable(cluster):
_ = repr(cluster)
_ = str(cluster)
assert_msonable(cluster, skip_keys=["sites"])


def test_to_from(cluster, tmpdir):
yml = cluster.to("yaml")
js = cluster.to("json")

cluster2 = Cluster.from_str(yml, "yaml")
assert cluster == cluster2
cluster2 = Cluster.from_str(js, "json")
assert cluster == cluster2

with open(os.path.join(tmpdir, "cluster.yaml"), "w") as f:
yaml.dump(yml, f)
with open(os.path.join(tmpdir, "cluster.json"), "w") as f:
json.dump(js, f)

# cluster2 = Cluster.from_file(os.path.join(tmpdir, "cluster.yaml"))
# assert cluster == cluster2
# cluster2 = Cluster.from_file(os.path.join(tmpdir, "cluster.json"))
# assert cluster == cluster2

# cluster.to("yaml", os.path.join(tmpdir, "cluster.yaml"))
# cluster.to("json", os.path.join(tmpdir, "cluster.json"))

# cluster2 = Cluster.from_file(os.path.join(tmpdir, "cluster.yaml"))
# assert cluster == cluster2
# cluster2 = Cluster.from_file(os.path.join(tmpdir, "cluster.json"))
# assert cluster == cluster2

with pytest.raises(ValueError):
cluster.to("bad_format")
12 changes: 12 additions & 0 deletions tests/test_cofe/test_expansion.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ def test_regression_data(cluster_subspace, rng):
assert reg_data.parameters == reg.get_params()
assert_msonable(expansion)

# test bad feature matrix shape
reg_data = RegressionData.from_sklearn(
reg, feature_matrix=feat_matrix[:, :-1], property_vector=prop_vec
)
with pytest.raises(AttributeError):
expansion = ClusterExpansion(cluster_subspace, coeffs, reg_data)
# test bad coeff length
with pytest.raises(AttributeError):
expansion = ClusterExpansion(cluster_subspace, coeffs[:-1], reg_data)


def test_predict(cluster_expansion, rng):
subspace = cluster_expansion.cluster_subspace
Expand Down Expand Up @@ -112,6 +122,8 @@ def test_prune(cluster_expansion):


def test_msonable(cluster_expansion):
_ = repr(cluster_expansion)
_ = str(cluster_expansion)
_ = str(cluster_expansion)
d = cluster_expansion.as_dict()
ce1 = ClusterExpansion.from_dict(d)
Expand Down
4 changes: 4 additions & 0 deletions tests/test_moca/test_mcushers.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ def test_bad_propabilities(mcmcusher):
mcmcusher.sublattice_probabilities = [0.6, 0.1]
with pytest.raises(AttributeError):
mcmcusher.sublattice_probabilities = [0.5, 0.2, 0.3]
with pytest.raises(AttributeError):
_ = Flip(mcmcusher.sublattices, [0.5])
with pytest.raises(ValueError):
_ = Flip(mcmcusher.sublattices, [0.5, 0.2])


def test_propose_step(mcmcusher, rand_occu):
Expand Down