Skip to content

Commit

Permalink
Added vector_to_formula class method
Browse files Browse the repository at this point in the history
  • Loading branch information
SurgeArrester committed Mar 14, 2024
1 parent d096cd4 commit 24b2db3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 16 deletions.
37 changes: 31 additions & 6 deletions ElMD/ElMD.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
__copyright__ = "2019, Cameron Hargreaves"
__credits__ = ["https://github.com/Zapaan", "Loïc Séguin-C. <[email protected]>", "https://github.com/Bowserinator/"]
__license__ = "GPL"
__version__ = "0.5.11"
__version__ = "0.5.12"
'''
import json
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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()

Expand Down
12 changes: 2 additions & 10 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '[email protected]',
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,
Expand Down

0 comments on commit 24b2db3

Please sign in to comment.