|
| 1 | +from functools import partial |
| 2 | +from typing import Iterable, Iterator, List, Optional, Union |
| 3 | +from uuid import UUID |
| 4 | + |
| 5 | +from citrine.informatics.executions.predictor_evaluation import PredictorEvaluation, \ |
| 6 | + PredictorEvaluationRequest, PredictorEvaluatorsResponse |
| 7 | +from citrine.informatics.predictor_evaluator import PredictorEvaluator |
| 8 | +from citrine.informatics.predictors import GraphPredictor |
| 9 | +from citrine.resources.predictor import LATEST_VER as LATEST_PRED_VER |
| 10 | +from citrine._rest.collection import Collection |
| 11 | +from citrine._rest.resource import PredictorRef |
| 12 | +from citrine._session import Session |
| 13 | + |
| 14 | + |
| 15 | +class PredictorEvaluationCollection(Collection[PredictorEvaluation]): |
| 16 | + """Represents the collection of predictor evaluations. |
| 17 | +
|
| 18 | + Parameters |
| 19 | + ---------- |
| 20 | + project_id: UUID |
| 21 | + the UUID of the project |
| 22 | +
|
| 23 | + """ |
| 24 | + |
| 25 | + _api_version = 'v1' |
| 26 | + _path_template = '/projects/{project_id}/predictor-evaluations' |
| 27 | + _individual_key = None |
| 28 | + _resource = PredictorEvaluation |
| 29 | + _collection_key = 'response' |
| 30 | + |
| 31 | + def __init__(self, project_id: UUID, session: Session): |
| 32 | + self.project_id = project_id |
| 33 | + self.session: Session = session |
| 34 | + |
| 35 | + def build(self, data: dict) -> PredictorEvaluation: |
| 36 | + """Build an individual predictor evaluation.""" |
| 37 | + evaluation = PredictorEvaluation.build(data) |
| 38 | + evaluation._session = self.session |
| 39 | + evaluation._project_id = self.project_id |
| 40 | + return evaluation |
| 41 | + |
| 42 | + def _list_base(self, |
| 43 | + *, |
| 44 | + per_page: int = 100, |
| 45 | + predictor_id: Optional[UUID] = None, |
| 46 | + predictor_version: Optional[Union[int, str]] = None, |
| 47 | + archived: Optional[bool] = None |
| 48 | + ) -> Iterator[PredictorEvaluation]: |
| 49 | + params = {"archived": archived} |
| 50 | + if predictor_id is not None: |
| 51 | + params["predictor_id"] = str(predictor_id) |
| 52 | + if predictor_version is not None: |
| 53 | + params["predictor_version"] = predictor_version |
| 54 | + |
| 55 | + fetcher = partial(self._fetch_page, additional_params=params) |
| 56 | + return self._paginator.paginate(page_fetcher=fetcher, |
| 57 | + collection_builder=self._build_collection_elements, |
| 58 | + per_page=per_page) |
| 59 | + |
| 60 | + def list_all(self, |
| 61 | + *, |
| 62 | + per_page: int = 100, |
| 63 | + predictor_id: Optional[UUID] = None, |
| 64 | + predictor_version: Optional[Union[int, str]] = None |
| 65 | + ) -> Iterable[PredictorEvaluation]: |
| 66 | + """List all predictor evaluations.""" |
| 67 | + return self._list_base(per_page=per_page, |
| 68 | + predictor_id=predictor_id, |
| 69 | + predictor_version=predictor_version) |
| 70 | + |
| 71 | + def list(self, |
| 72 | + *, |
| 73 | + per_page: int = 100, |
| 74 | + predictor_id: Optional[UUID] = None, |
| 75 | + predictor_version: Optional[Union[int, str]] = None |
| 76 | + ) -> Iterable[PredictorEvaluation]: |
| 77 | + """List non-archived predictor evaluations.""" |
| 78 | + return self._list_base(per_page=per_page, |
| 79 | + predictor_id=predictor_id, |
| 80 | + predictor_version=predictor_version, |
| 81 | + archived=False) |
| 82 | + |
| 83 | + def list_archived(self, |
| 84 | + *, |
| 85 | + per_page: int = 100, |
| 86 | + predictor_id: Optional[UUID] = None, |
| 87 | + predictor_version: Optional[Union[int, str]] = None |
| 88 | + ) -> Iterable[PredictorEvaluation]: |
| 89 | + """List archived predictor evaluations.""" |
| 90 | + return self._list_base(per_page=per_page, |
| 91 | + predictor_id=predictor_id, |
| 92 | + predictor_version=predictor_version, |
| 93 | + archived=True) |
| 94 | + |
| 95 | + def archive(self, uid: Union[UUID, str]): |
| 96 | + """Archive an evaluation.""" |
| 97 | + url = self._get_path(uid, action="archive") |
| 98 | + result = self.session.put_resource(url, {}, version=self._api_version) |
| 99 | + return self.build(result) |
| 100 | + |
| 101 | + def restore(self, uid: Union[UUID, str]): |
| 102 | + """Restore an archived evaluation.""" |
| 103 | + url = self._get_path(uid, action="restore") |
| 104 | + result = self.session.put_resource(url, {}, version=self._api_version) |
| 105 | + return self.build(result) |
| 106 | + |
| 107 | + def default_from_config(self, config: GraphPredictor) -> List[PredictorEvaluator]: |
| 108 | + """Retrieve the default evaluators for an arbitrary (but valid) predictor config. |
| 109 | +
|
| 110 | + See :func:`~citrine.resources.PredictorEvaluationCollection.default_evaluators` for details |
| 111 | + on the resulting evaluators. |
| 112 | + """ |
| 113 | + path = self._get_path(action="default-from-config") |
| 114 | + payload = config.dump()["instance"] |
| 115 | + result = self.session.post_resource(path, json=payload, version=self._api_version) |
| 116 | + return PredictorEvaluatorsResponse.build(result).evaluators |
| 117 | + |
| 118 | + def default(self, |
| 119 | + *, |
| 120 | + predictor_id: Union[UUID, str], |
| 121 | + predictor_version: Union[int, str] = LATEST_PRED_VER |
| 122 | + ) -> List[PredictorEvaluator]: |
| 123 | + """Retrieve the default evaluators for a stored predictor. |
| 124 | +
|
| 125 | + The current default evaluators perform 5-fold, 3-trial cross-validation on all valid |
| 126 | + predictor responses. Valid responses are those that are **not** produced by the |
| 127 | + following predictors: |
| 128 | +
|
| 129 | + * :class:`~citrine.informatics.predictors.generalized_mean_property_predictor.GeneralizedMeanPropertyPredictor` |
| 130 | + * :class:`~citrine.informatics.predictors.mean_property_predictor.MeanPropertyPredictor` |
| 131 | + * :class:`~citrine.informatics.predictors.ingredient_fractions_predictor.IngredientFractionsPredictor` |
| 132 | + * :class:`~citrine.informatics.predictors.ingredients_to_simple_mixture_predictor.IngredientsToSimpleMixturePredictor` |
| 133 | + * :class:`~citrine.informatics.predictors.ingredients_to_formulation_predictor.IngredientsToFormulationPredictor` |
| 134 | + * :class:`~citrine.informatics.predictors.label_fractions_predictor.LabelFractionsPredictor` |
| 135 | + * :class:`~citrine.informatics.predictors.molecular_structure_featurizer.MolecularStructureFeaturizer` |
| 136 | + * :class:`~citrine.informatics.predictors.simple_mixture_predictor.SimpleMixturePredictor` |
| 137 | +
|
| 138 | + Parameters |
| 139 | + ---------- |
| 140 | + predictor_id: UUID |
| 141 | + Unique identifier of the predictor to evaluate |
| 142 | + predictor_version: Option[Union[int, str]] |
| 143 | + The version of the predictor to evaluate |
| 144 | +
|
| 145 | + Returns |
| 146 | + ------- |
| 147 | + PredictorEvaluation |
| 148 | +
|
| 149 | + """ # noqa: E501,W505 |
| 150 | + path = self._get_path(action="default") |
| 151 | + payload = PredictorRef(uid=predictor_id, version=predictor_version).dump() |
| 152 | + result = self.session.post_resource(path, json=payload, version=self._api_version) |
| 153 | + return PredictorEvaluatorsResponse.build(result).evaluators |
| 154 | + |
| 155 | + def trigger(self, |
| 156 | + *, |
| 157 | + predictor_id: Union[UUID, str], |
| 158 | + predictor_version: Union[int, str] = LATEST_PRED_VER, |
| 159 | + evaluators: List[PredictorEvaluator]) -> PredictorEvaluation: |
| 160 | + """Evaluate a predictor using the provided evaluators. |
| 161 | +
|
| 162 | + Parameters |
| 163 | + ---------- |
| 164 | + predictor_id: UUID |
| 165 | + Unique identifier of the predictor to evaluate |
| 166 | + predictor_version: Option[Union[int, str]] |
| 167 | + The version of the predictor to evaluate. Defaults to the latest trained version. |
| 168 | + evaluators: List[PredictorEvaluator] |
| 169 | + The evaluators to use to measure predictor performance. |
| 170 | +
|
| 171 | + Returns |
| 172 | + ------- |
| 173 | + PredictorEvaluation |
| 174 | +
|
| 175 | + """ |
| 176 | + path = self._get_path("trigger") |
| 177 | + payload = PredictorEvaluationRequest(evaluators=evaluators, |
| 178 | + predictor_id=predictor_id, |
| 179 | + predictor_version=predictor_version).dump() |
| 180 | + result = self.session.post_resource(path, payload, version=self._api_version) |
| 181 | + return self.build(result) |
| 182 | + |
| 183 | + def trigger_default(self, |
| 184 | + *, |
| 185 | + predictor_id: Union[UUID, str], |
| 186 | + predictor_version: Union[int, str] = LATEST_PRED_VER |
| 187 | + ) -> PredictorEvaluation: |
| 188 | + """Evaluate a predictor using the default evaluators. |
| 189 | +
|
| 190 | + See :func:`~citrine.resources.PredictorCollection.default_evaluators` for details on the evaluators. |
| 191 | +
|
| 192 | + Parameters |
| 193 | + ---------- |
| 194 | + predictor_id: UUID |
| 195 | + Unique identifier of the predictor to evaluate |
| 196 | + predictor_version: Option[Union[int, str]] |
| 197 | + The version of the predictor to evaluate |
| 198 | +
|
| 199 | + Returns |
| 200 | + ------- |
| 201 | + PredictorEvaluation |
| 202 | +
|
| 203 | + """ # noqa: E501,W505 |
| 204 | + path = self._get_path("trigger-default") |
| 205 | + payload = PredictorRef(uid=predictor_id, version=predictor_version).dump() |
| 206 | + result = self.session.post_resource(path, json=payload, version=self._api_version) |
| 207 | + return self.build(result) |
| 208 | + |
| 209 | + def register(self, model: PredictorEvaluation) -> PredictorEvaluation: |
| 210 | + """Cannot register an evaluation.""" |
| 211 | + raise NotImplementedError("Cannot register a PredictorEvaluation.") |
| 212 | + |
| 213 | + def update(self, model: PredictorEvaluation) -> PredictorEvaluation: |
| 214 | + """Cannot update an evaluation.""" |
| 215 | + raise NotImplementedError("Cannot update a PredictorEvaluation.") |
| 216 | + |
| 217 | + def delete(self, uid: Union[UUID, str]): |
| 218 | + """Cannot delete an evaluation.""" |
| 219 | + raise NotImplementedError("Cannot delete a PredictorEvaluation.") |
0 commit comments