Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust type of ChemicalFormulaFeaturizer.powers. #964

Merged
merged 4 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
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