From 24b2db31d1b752b07d582a10b40a78a0e16b9dbd Mon Sep 17 00:00:00 2001 From: Cameron Hargreaves Date: Thu, 14 Mar 2024 14:11:00 +0100 Subject: [PATCH] Added vector_to_formula class method --- ElMD/ElMD.py | 37 +++++++++++++++++++++++++++++++------ setup.py | 12 ++---------- 2 files changed, 33 insertions(+), 16 deletions(-) diff --git a/ElMD/ElMD.py b/ElMD/ElMD.py index 77138dc..f040f27 100644 --- a/ElMD/ElMD.py +++ b/ElMD/ElMD.py @@ -23,7 +23,7 @@ __copyright__ = "2019, Cameron Hargreaves" __credits__ = ["https://github.com/Zapaan", "Loïc Séguin-C. ", "https://github.com/Bowserinator/"] __license__ = "GPL" -__version__ = "0.5.11" +__version__ = "0.5.12" ''' import json @@ -55,12 +55,34 @@ def main(): print(x.elmd(y)) print(y.elmd(x)) + print(x.pretty_formula) + print(x.vec_to_formula(x.feature_vector)) + print(y.vec_to_formula(x.feature_vector)) + print(x.vec_to_formula(y.feature_vector)) + x = ElMD("CaTiO3", metric="fast") y = ElMD("NaCl", metric="fast") print(x.elmd(y)) print(y.elmd(x)) + print(x.pretty_formula) + print(x.vec_to_formula(x.feature_vector)) + print(y.vec_to_formula(x.feature_vector)) + print(x.vec_to_formula(y.feature_vector)) + + x = ElMD("CaTiO3", metric="mendeleev") + y = ElMD("NaCl", metric="mendeleev") + + print(x.elmd(y)) + print(y.elmd(x)) + + print(x.pretty_formula) + print(x.vec_to_formula(x.feature_vector)) + print(y.vec_to_formula(x.feature_vector)) + print(x.vec_to_formula(y.feature_vector)) + + print(time.time() - ts) @lru_cache(maxsize=16) @@ -178,7 +200,7 @@ def __init__(self, formula="", metric="mod_petti", strict_parsing=False, x=1): self.ratio_vector = self._gen_ratio_vector() self.petti_vector = self._gen_petti_vector() - self.pretty_formula = self._gen_pretty() + self.pretty_formula = self.vec_to_formula() self.feature_vector = self._gen_feature_vector() @@ -343,18 +365,21 @@ def _gen_feature_vector(self): return weighted_vector - def _gen_pretty(self): + def vec_to_formula(self, vector=None): ''' Return a normalized formula string ordered by the mod_petti dictionary ''' - inds = np.where(self.petti_vector != 0.0)[0] + if vector is None: + vector = self.petti_vector + + inds = np.where(vector != 0.0)[0] pretty_form = "" for i, ind in enumerate(inds): - if self.petti_vector[ind] == 1: + if vector[ind] == 1: pretty_form = pretty_form + f"{self.lookup[ind]}" else: - pretty_form = pretty_form + f"{self.lookup[ind]}{self.petti_vector[ind]:.3f}".strip('0') + ' ' + pretty_form = pretty_form + f"{self.lookup[ind]}{vector[ind]:.3f}".strip('0') + ' ' return pretty_form.strip() diff --git a/setup.py b/setup.py index 7ba0ed2..2f388c4 100644 --- a/setup.py +++ b/setup.py @@ -3,21 +3,13 @@ setup( name = 'ElMD', packages = ['ElMD'], -<<<<<<< HEAD - version = '0.5.5', -======= - version = '0.5.4', ->>>>>>> 0c4ba033856e760ba89e61b5390c791bff7be88f + version = '0.5.12', license='GPL3', description = 'An implementation of the Element movers distance for chemical similarity of ionic compositions', author = 'Cameron Hagreaves', author_email = 'cameron.h@rgreaves.me.uk', url = 'https://github.com/lrcfmd/ElMD/', -<<<<<<< HEAD - download_url = 'https://github.com/lrcfmd/ElMD/archive/v0.5.5.tar.gz', -======= - download_url = 'https://github.com/lrcfmd/ElMD/archive/v0.5.4.tar.gz', ->>>>>>> 0c4ba033856e760ba89e61b5390c791bff7be88f + download_url = 'https://github.com/lrcfmd/ElMD/archive/v0.5.12.tar.gz', keywords = ['ChemInformatics', 'Materials Science', 'Machine Learning', 'Materials Representation'], package_data={"elementFeatures": ["el_lookup/*.json"]}, include_package_data=True,