Skip to content

Commit

Permalink
Merge pull request #64 from WMD-group/test_updates
Browse files Browse the repository at this point in the history
Test updates
  • Loading branch information
AntObi authored Jul 7, 2023
2 parents 8dfcddf + c1d1816 commit 31c227f
Show file tree
Hide file tree
Showing 10 changed files with 356 additions and 68 deletions.
37 changes: 35 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,41 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
<!-- insertion marker -->
## Unreleased

<small>[Compare with latest](https://github.com/WMD-group/ElementEmbeddings/compare/v0.1...HEAD)</small>
<small>[Compare with latest](https://github.com/WMD-group/ElementEmbeddings/compare/v0.1.1...HEAD)</small>

### Added

- Added dev & doc reqs and updated version no. ([e9278c5](https://github.com/WMD-group/ElementEmbeddings/commit/e9278c579a031643576f137196aad34e0f5ea98f) by Anthony Onwuli).
- Added test for dimension plotter in 3D ([ef37daf](https://github.com/WMD-group/ElementEmbeddings/commit/ef37daff6aa824c3d9917fa1ba26fc37b95a9951) by Anthony Onwuli).
- Added test for plotting euclidean heatmap ([e9cc5ed](https://github.com/WMD-group/ElementEmbeddings/commit/e9cc5ed5508e624420b1330973425572ff5b1628) by Anthony Onwuli).
- Added deprecation warning to old plotting function ([542e2f2](https://github.com/WMD-group/ElementEmbeddings/commit/542e2f2e6bd96b0f0e1624192cb9a9a98fb3dfcc) by Anthony Onwuli).
- Added functions to test PCA and UMAP for Embedding ([b7ccc8f](https://github.com/WMD-group/ElementEmbeddings/commit/b7ccc8f41384e5e6095090aa016088279b5a0439) by Anthony Onwuli).
- Added test function for tSNE for Embedding class ([aaa1472](https://github.com/WMD-group/ElementEmbeddings/commit/aaa147279ba609984482813df2ce9530420da2be) by Anthony Onwuli).
- Added more tests for compute metrics function ([c4055bc](https://github.com/WMD-group/ElementEmbeddings/commit/c4055bcdad6e5bd7832a8568767ced72cd9cdfd9) by Anthony Onwuli).
- Added test for computing spearman's rank ([5f715aa](https://github.com/WMD-group/ElementEmbeddings/commit/5f715aaa3ba339b5e01012cb0a40c44652481b55) by Anthony Onwuli).
- Added more dataframe test functions ([edeeb87](https://github.com/WMD-group/ElementEmbeddings/commit/edeeb8714ae80b194159738b562606819ffc3ccb) by Anthony Onwuli).
- Added test for removing multiple elements ([b69d806](https://github.com/WMD-group/ElementEmbeddings/commit/b69d80699cad211166ff1b112886d19d387890b5) by Anthony Onwuli).
- Added test function for removing elements ([664cbbf](https://github.com/WMD-group/ElementEmbeddings/commit/664cbbf1846757b7d018c199745b6227465c0268) by Anthony Onwuli).
- Added setUpClass to test_core.py ([7cb8ab6](https://github.com/WMD-group/ElementEmbeddings/commit/7cb8ab6d3b731d04831cdfe83a90b926ab1e2a1b) by Anthony Onwuli).
- Added tests for `as_dataframe` method ([7256c23](https://github.com/WMD-group/ElementEmbeddings/commit/7256c23d8d2840b77983424ee9247a90f1caaded) by Anthony Onwuli).
- Added more tests to the Embedding loading function ([7f9f87b](https://github.com/WMD-group/ElementEmbeddings/commit/7f9f87b987a77f1d4b73cf9fed289a5d9a028417) by Anthony Onwuli).
- Added test functions for loading csv and json ([87ba958](https://github.com/WMD-group/ElementEmbeddings/commit/87ba9581c506bc16aa377961da28c0cbe60e80de) by Anthony Onwuli).
- Added test embedding json file ([188aea4](https://github.com/WMD-group/ElementEmbeddings/commit/188aea48e21b3c3d5a1b9624a9885b94f14b2fcc) by Anthony Onwuli).
- Added test embedding csv file ([173bcee](https://github.com/WMD-group/ElementEmbeddings/commit/173bcee057173ec1a48cdc7bb3141406236119ce) by Anthony Onwuli).

### Fixed

- Fixed spelling error for extras_require setup.py ([8e28e9a](https://github.com/WMD-group/ElementEmbeddings/commit/8e28e9a09550bfcaf21ec4d95989cd031d717596) by Anthony Onwuli).

### Removed

- Removed outdated installation instructions ([c69817f](https://github.com/WMD-group/ElementEmbeddings/commit/c69817fef331e203fb3861e603c7c0176097e51f) by Anthony Onwuli).
- Removed an else block from `load_data` ([5532de6](https://github.com/WMD-group/ElementEmbeddings/commit/5532de6d050580382f0fa9688be96f0e9cd231ec) by Anthony Onwuli).

<!-- insertion marker -->
## [v0.1.1](https://github.com/WMD-group/ElementEmbeddings/releases/tag/v0.1.1) - 2023-07-05

<small>[Compare with v0.1](https://github.com/WMD-group/ElementEmbeddings/compare/v0.1...v0.1.1)</small>

### Added

Expand All @@ -20,7 +54,6 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.

- removed pandas, pytest and pytest-subtests in reqs ([cd1bf77](https://github.com/WMD-group/ElementEmbeddings/commit/cd1bf776220250377bb7cd48cca6b08e9a968f1d) by Anthony Onwuli).

<!-- insertion marker -->
## [v0.1](https://github.com/WMD-group/ElementEmbeddings/releases/tag/v0.1) - 2023-06-30

<small>[Compare with first commit](https://github.com/WMD-group/ElementEmbeddings/compare/262c7e99a438a3527fb73866093ae8cb1ee85ee6...v0.1)</small>
Expand Down
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,5 @@ keywords:
- element representation
license: MIT
commit: d3f30602abf825ba3dcd5f247694a174a358ef49
version: '0.1'
date-released: '2023-06-30'
version: '0.2.0'
date-released: '2023-07-07'
10 changes: 6 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ ElementEmbeddings
[![documentation](https://img.shields.io/badge/docs-mkdocs%20material-blue.svg?style=flat)](https://wmd-group.github.io/ElementEmbeddings/)
![python version](https://img.shields.io/pypi/pyversions/elementembeddings)


The **Element Embeddings** package provides high-level tools for analysing elemental
embeddings data. This primarily involves visualising the correlation between
embedding schemes using different statistical measures.
Expand All @@ -37,14 +36,18 @@ importing the class.

Installation
--------

The latest stable release can be installed via pip using:

```bash
pip install ElementEmbeddings
```
The latest version can be installed using:

For installing the development or documentation dependencies via pip:

```bash
pip install git+git://github.com/WMD-group/ElementEmbeddings.git
pip install "ElementEmbeddings[dev]"
pip install "ElementEmbeddings[docs]"
```

For development, you can clone the repository and install the package in editable mode.
Expand Down Expand Up @@ -80,4 +83,3 @@ We can access some of the properties of the `Embedding` class. For example, we c
The magpie representation has embeddings of dimension 22
['H', 'He', 'Li', 'Be', 'B', 'C', 'N', 'O', 'F', 'Ne', 'Na', 'Mg', 'Al', 'Si', 'P', 'S', 'Cl', 'Ar', 'K', 'Ca', 'Sc', 'Ti', 'V', 'Cr', 'Mn', 'Fe', 'Co', 'Ni', 'Cu', 'Zn', 'Ga', 'Ge', 'As', 'Se', 'Br', 'Kr', 'Rb', 'Sr', 'Y', 'Zr', 'Nb', 'Mo', 'Tc', 'Ru', 'Rh', 'Pd', 'Ag', 'Cd', 'In', 'Sn', 'Sb', 'Te', 'I', 'Xe', 'Cs', 'Ba', 'La', 'Ce', 'Pr', 'Nd', 'Pm', 'Sm', 'Eu', 'Gd', 'Tb', 'Dy', 'Ho', 'Er', 'Tm', 'Yb', 'Lu', 'Hf', 'Ta', 'W', 'Re', 'Os', 'Ir', 'Pt', 'Au', 'Hg', 'Tl', 'Pb', 'Bi', 'Po', 'At', 'Rn', 'Fr', 'Ra', 'Ac', 'Th', 'Pa', 'U', 'Np', 'Pu', 'Am', 'Cm', 'Bk']
```

22 changes: 21 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

module_dir = os.path.dirname(os.path.abspath(__file__))

VERSION = "0.1.1"
VERSION = "0.2.0"
DESCRIPTION = "Element Embeddings"
with open(os.path.join(module_dir, "README.md"), encoding="utf-8") as f:
LONG_DESCRIPTION = f.read()
Expand Down Expand Up @@ -37,6 +37,26 @@
"umap-learn==0.5.3",
"adjustText==0.8",
],
extras_require={
"dev": [
"pre-commit==2.20.0",
"black==23.3.0",
"isort==5.12.0",
"pytest==7.2.1",
"pytest-subtests==0.10.0",
"nbqa==1.5.3",
"pyupgrade==3.3.1",
"flake8==6.0.0",
"autopep8==2.0.1",
"pytest-cov==4.1.0",
],
"docs": [
"mkdocs==1.4.3",
"mkdocs-material==9.1.17",
"mkdocstrings ==0.21.2",
"mkdocstrings-python == 1.0.0",
],
},
classifiers=[
"Programming Language :: Python",
"Programming Language :: Python :: 3.8",
Expand Down
13 changes: 8 additions & 5 deletions src/elementembeddings/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,11 +151,6 @@ def load_data(embedding_name: Optional[str] = None):
_json = path.join(data_directory, _cbfv_files[embedding_name])
with open(_json) as f:
embedding_data = json.load(f)

# Load a json file from a file specified in the input
else:
with open(embedding_name) as f:
embedding_data = json.load(f)
else:
raise (
ValueError(
Expand Down Expand Up @@ -853,6 +848,10 @@ def plot_PCA_2D(
ax (matplotlib.axes.Axes): An Axes object with the PCA plot

"""
warnings.warn(
"This method is deprecated and will be removed in a future release. ",
DeprecationWarning,
)
embeddings_array = np.array(list(self.embeddings.values()))
element_array = np.array(self.element_list)

Expand Down Expand Up @@ -913,6 +912,10 @@ def plot_tSNE(


"""
warnings.warn(
"This method is deprecated and will be removed in a future release. ",
DeprecationWarning,
)
embeddings_array = np.array(list(self.embeddings.values()))
element_array = np.array(self.element_list)

Expand Down
4 changes: 4 additions & 0 deletions src/elementembeddings/tests/files/test_embedding.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
element,0,1,2,3,4,5,6,7,8,9
H,1,0,0,0,0,0,0,0,0,0
He,0,1,0,0,0,0,0,0,0,0
Li,0,0,1,0,0,0,0,0,0,0
1 change: 1 addition & 0 deletions src/elementembeddings/tests/files/test_embedding.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"H":[1,0,0,0,0,0,0,0,0,0], "He":[0,1,0,0,0,0,0,0,0,0],"Li":[0,0,1,0,0,0,0,0,0,0]}
96 changes: 74 additions & 22 deletions src/elementembeddings/tests/test_composition.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,53 @@
class TestComposition(unittest.TestCase):
"""Test the composition module."""

def setUp(self):
"""Set up the test class."""
self.formulas = [
"Sr3Sc2(GeO4)3",
"Fe2O3",
"Li7La3ZrO12",
"CsPbI3",
]

def test_formula_parser(self):
"""Test the formula_parser function."""
LLZO_parsed = composition.formula_parser("Li7La3ZrO12")
LLZO_parsed = composition.formula_parser(self.formulas[2])
assert isinstance(LLZO_parsed, dict)
assert "Zr" in LLZO_parsed
assert LLZO_parsed["Li"] == 7

def test_formula_parser_with_parentheses(self):
"""Test the formula_parser function with parentheses."""
SrScGeO4_parsed = composition.formula_parser(self.formulas[0])
assert isinstance(SrScGeO4_parsed, dict)
assert "Sr" in SrScGeO4_parsed
assert SrScGeO4_parsed["Ge"] == 3

def test_formula_parser_with_invalid_formula(self):
"""Test the formula_parser function with an invalid formula."""
with self.assertRaises(ValueError):
composition.formula_parser("Sr3Sc2(GeO4)3)")

def test__get_fractional_composition(self):
"""Test the _get_fractional_composition function."""
CsPbI3_frac = composition._get_fractional_composition("CsPbI3")
CsPbI3_frac = composition._get_fractional_composition(self.formulas[3])
assert isinstance(CsPbI3_frac, dict)
assert "Pb" in CsPbI3_frac
assert CsPbI3_frac["I"] == 0.6

def test_Composition_class(self):
"""Test the Composition class."""
Fe2O3_magpie = composition.CompositionalEmbedding(
formula="Fe2O3", embedding="magpie"
)
assert isinstance(Fe2O3_magpie.embedding, core.Embedding)
assert Fe2O3_magpie.formula == "Fe2O3"
assert Fe2O3_magpie.embedding_name == "magpie"
assert isinstance(Fe2O3_magpie.composition, dict)
assert {"Fe": 2, "O": 3} == Fe2O3_magpie.composition
assert Fe2O3_magpie._natoms == 5
assert Fe2O3_magpie.fractional_composition == {"Fe": 0.4, "O": 0.6}
assert isinstance(Fe2O3_magpie._mean_feature_vector(), np.ndarray)
# Test that the feature vector function works
stats = [

class TestCompositionalEmbedding(unittest.TestCase):
"""Test the CompositionalEmbedding class."""

def setUp(self):
"""Set up the test formulas."""
self.formulas = ["Sr3Sc2(GeO4)3", "Fe2O3", "Li7La3ZrO12", "CsPbI3", "CsPbI-3"]
self.valid_magpie_compositions = [
composition.CompositionalEmbedding(formula=formula, embedding="magpie")
for formula in self.formulas[:3]
]
self.stats = [
"mean",
"variance",
"minpool",
Expand All @@ -49,16 +67,50 @@ def test_Composition_class(self):
"geometric_mean",
"harmonic_mean",
]
assert isinstance(Fe2O3_magpie.feature_vector(stats=stats), np.ndarray)

def test_CompositionalEmbedding_attributes(self):
"""Test the Composition class."""
Fe2O3_magpie = self.valid_magpie_compositions[1]
assert isinstance(Fe2O3_magpie.embedding, core.Embedding)
assert Fe2O3_magpie.formula == "Fe2O3"
assert Fe2O3_magpie.embedding_name == "magpie"
assert isinstance(Fe2O3_magpie.composition, dict)
assert {"Fe": 2, "O": 3} == Fe2O3_magpie.composition
assert Fe2O3_magpie.num_atoms == 5
assert Fe2O3_magpie.fractional_composition == {"Fe": 0.4, "O": 0.6}
assert Fe2O3_magpie.embedding.dim == 22

def test_CompositionalEmbedding_negative_formula(self):
"""Test the Composition class with a negative formula."""
with self.assertRaises(ValueError):
composition.CompositionalEmbedding(
formula=self.formulas[4], embedding="magpie"
)

def test__mean_feature_vector(self):
"""Test the _mean_feature_vector function."""
assert isinstance(
self.valid_magpie_compositions[1]._mean_feature_vector(), np.ndarray
)
# Test that the feature vector function works

def test_feature_vector(self):
"""Test the feature_vector function."""
assert isinstance(
self.valid_magpie_compositions[0].feature_vector(stats=self.stats),
np.ndarray,
)
assert len(
Fe2O3_magpie.feature_vector(stats=stats)
) == Fe2O3_magpie.embedding.dim * len(stats)
self.valid_magpie_compositions[0].feature_vector(stats=self.stats)
) == self.valid_magpie_compositions[0].embedding.dim * len(self.stats)
# Test that the feature vector function works with a single stat
assert isinstance(Fe2O3_magpie.feature_vector(stats="mean"), np.ndarray)
assert isinstance(
self.valid_magpie_compositions[0].feature_vector(stats="mean"), np.ndarray
)

def test_composition_featuriser(self):
"""Test the composition featuriser function."""
formulas = ["Fe2O3", "Li7La3ZrO12", "CsPbI3"]
formulas = self.formulas[:3]
formula_df = pd.DataFrame(formulas, columns=["formula"])
assert isinstance(composition.composition_featuriser(formula_df), pd.DataFrame)
assert composition.composition_featuriser(formula_df).shape == (3, 2)
Expand Down
Loading

0 comments on commit 31c227f

Please sign in to comment.