Skip to content

Commit

Permalink
Merge pull request #964 from CitrineInformatics/bugfix/powers-type
Browse files Browse the repository at this point in the history
Adjust type of ChemicalFormulaFeaturizer.powers.
  • Loading branch information
anoto-moniz authored Sep 19, 2024
2 parents 9e15919 + 497ba9f commit 0e454fe
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/citrine/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "3.5.4"
__version__ = "3.6.0"
27 changes: 25 additions & 2 deletions src/citrine/informatics/predictors/chemical_formula_featurizer.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import List, Optional
from typing import List, Optional, Union
from warnings import warn

from citrine._rest.resource import Resource
from citrine._serialization import properties
Expand Down Expand Up @@ -133,7 +134,7 @@ class ChemicalFormulaFeaturizer(Resource["ChemicalFormulaFeaturizer"], Predictor
input_descriptor = properties.Object(ChemicalFormulaDescriptor, 'input')
features = properties.List(properties.String, 'features')
excludes = properties.List(properties.String, 'excludes', default=[])
powers = properties.List(properties.Integer, 'powers')
_powers = properties.List(properties.Float, 'powers')

typ = properties.String('type', default='ChemicalFormulaFeaturizer', deserializable=False)

Expand All @@ -152,5 +153,27 @@ def __init__(self,
self.excludes = excludes if excludes is not None else []
self.powers = powers if powers is not None else [1]

@property
def powers(self) -> List[int]:
"""The list of powers when computing generalized weighted means of element properties."""
warn("The type of 'powers' will change to a list of floats in v4.0.0. To retrieve them as "
"floats now, use 'powers_as_float'.")
truncated = [int(p) for p in self._powers]
if truncated != self._powers:
diffs = [f"{x} => {y}" for x, y in zip(self._powers, truncated) if x != y]
warn(f"The following powers were cast to ints: {'; '.join(diffs)}.")
return truncated

@powers.setter
def powers(self, value: List[Union[int, float]]):
self._powers = value

@property
def powers_as_float(self) -> List[float]:
"""Powers when computing generalized weighted means of element properties."""
warn("'powers_as_float' will be deprecated in v4.0.0 for 'powers', and removed in v5.0.0",
PendingDeprecationWarning)
return self._powers

def __str__(self):
return '<ChemicalFormulaFeaturizer {!r}>'.format(self.name)
11 changes: 10 additions & 1 deletion tests/informatics/test_predictors.py
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,10 @@ def test_chemical_featurizer(chemical_featurizer):
assert chemical_featurizer.input_descriptor == ChemicalFormulaDescriptor("formula")
assert chemical_featurizer.features == ["standard"]
assert chemical_featurizer.excludes == []
assert chemical_featurizer.powers == [1, 2]
with pytest.warns(UserWarning):
assert chemical_featurizer.powers == [1, 2]
with pytest.warns(PendingDeprecationWarning):
assert chemical_featurizer.powers_as_float == [1.0, 2.0]

assert str(chemical_featurizer) == "<ChemicalFormulaFeaturizer 'Chemical featurizer'>"

Expand All @@ -272,6 +275,12 @@ def test_chemical_featurizer(chemical_featurizer):
'powers': [1, 2],
'type': 'ChemicalFormulaFeaturizer'
}

chemical_featurizer.powers = [0.5, -1]
with pytest.warns(PendingDeprecationWarning):
assert chemical_featurizer.powers_as_float == [0.5, -1.0]
with pytest.warns(UserWarning):
assert chemical_featurizer.powers == [0, -1]


def test_auto_ml(auto_ml):
Expand Down

0 comments on commit 0e454fe

Please sign in to comment.