-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #982 from CitrineInformatics/feature/pne-6367-feat…
…ure-effects [PNE-6367] Add support for feature effects.
- Loading branch information
Showing
6 changed files
with
153 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
__version__ = "3.11.6" | ||
__version__ = "3.12.0" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
from typing import Dict | ||
from uuid import UUID | ||
|
||
from citrine._rest.resource import Resource | ||
from citrine._serialization import properties | ||
|
||
|
||
class ShapleyMaterial(Resource): | ||
"""The feature effect of a material.""" | ||
|
||
material_id = properties.UUID('material_id', serializable=False) | ||
value = properties.Float('value', serializable=False) | ||
|
||
|
||
class ShapleyFeature(Resource): | ||
"""All feature effects for this feature by material.""" | ||
|
||
feature = properties.String('feature', serializable=False) | ||
materials = properties.List(properties.Object(ShapleyMaterial), 'materials', | ||
serializable=False) | ||
|
||
@property | ||
def material_dict(self) -> Dict[UUID, float]: | ||
"""Presents the feature's effects as a dictionary by material.""" | ||
return {material.material_id: material.value for material in self.materials} | ||
|
||
|
||
class ShapleyOutput(Resource): | ||
"""All feature effects for this output by feature.""" | ||
|
||
output = properties.String('output', serializable=False) | ||
features = properties.List(properties.Object(ShapleyFeature), 'features', serializable=False) | ||
|
||
@property | ||
def feature_dict(self) -> Dict[str, Dict[UUID, float]]: | ||
"""Presents the output's feature effects as a dictionary by feature.""" | ||
return {feature.feature: feature.material_dict for feature in self.features} | ||
|
||
|
||
class FeatureEffects(Resource): | ||
"""Captures information about the feature effects associated with a predictor.""" | ||
|
||
predictor_id = properties.UUID('metadata.predictor_id', serializable=False) | ||
predictor_version = properties.Integer('metadata.predictor_version', serializable=False) | ||
status = properties.String('metadata.status', serializable=False) | ||
failure_reason = properties.Optional(properties.String(), 'metadata.failure_reason', | ||
serializable=False) | ||
|
||
outputs = properties.Optional(properties.List(properties.Object(ShapleyOutput)), 'resultobj', | ||
serializable=False) | ||
|
||
@classmethod | ||
def _pre_build(cls, data: dict) -> Dict: | ||
shapley = data["result"] | ||
material_ids = shapley["materials"] | ||
|
||
outputs = [] | ||
for output, feature_dict in shapley["outputs"].items(): | ||
features = [] | ||
for feature, values in feature_dict.items(): | ||
items = zip(material_ids, values) | ||
materials = [{"material_id": mid, "value": value} for mid, value in items] | ||
features.append({ | ||
"feature": feature, | ||
"materials": materials | ||
}) | ||
|
||
outputs.append({"output": output, "features": features}) | ||
|
||
data["resultobj"] = outputs | ||
return data | ||
|
||
@property | ||
def as_dict(self) -> Dict[str, Dict[str, Dict[UUID, float]]]: | ||
"""Presents the feature effects as a dictionary by output.""" | ||
return {output.output: output.feature_dict for output in self.outputs} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters