Skip to content

Commit

Permalink
eci as cached_property
Browse files Browse the repository at this point in the history
  • Loading branch information
lbluque committed Apr 13, 2022
1 parent c35d02a commit ce2a82e
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions smol/cofe/expansion.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

from copy import deepcopy
from dataclasses import asdict, dataclass
from functools import cached_property

import numpy as np
from monty.json import MSONable, jsanitize
Expand Down Expand Up @@ -163,22 +164,20 @@ def __init__(self, cluster_subspace, coefficients, regression_data=None):
if regression_data is not None
else None
)
self._eci = None

@property
@cached_property
def eci(self):
"""Get the ECI for the cluster expansion.
This just divides coefficients by the corresponding multiplicities.
External terms are dropped since their fitted coefficients do not
represent ECI.
"""
if self._eci is None:
num_ext_terms = len(self._subspace.external_terms) # check for extra terms
coefs = self.coefs[:-num_ext_terms] if num_ext_terms else self.coefs[:]
self._eci = coefs.copy()
self._eci /= self._subspace.function_total_multiplicities
return self._eci
num_ext_terms = len(self._subspace.external_terms) # check for extra terms
coefs = self.coefs[:-num_ext_terms] if num_ext_terms else self.coefs[:]
eci = coefs.copy()
eci /= self._subspace.function_total_multiplicities
return eci

@property
def structure(self):
Expand Down Expand Up @@ -265,7 +264,8 @@ def prune(self, threshold=0, with_multiplicity=False):
self.coefs = self.coefs[ids_complement]
if self._feat_matrix is not None:
self._feat_matrix = self._feat_matrix[:, ids_complement]
self._eci = None # Reset
if hasattr(self, "eci"): # reset cache
del self.eci

def copy(self):
"""Return a copy of self."""
Expand Down

0 comments on commit ce2a82e

Please sign in to comment.