# coding: utf-8

"""
    Mixpeek API

    This is the Mixpeek API, providing access to various endpoints for data processing and retrieval.

    The version of the OpenAPI document: 1.3.0
    Contact: info@mixpeek.com
    Generated by OpenAPI Generator (https://openapi-generator.tech)

    Do not edit the class manually.
"""  # noqa: E501


from __future__ import annotations
import pprint
import re  # noqa: F401
import json

from pydantic import BaseModel, ConfigDict, Field, StrictFloat, StrictInt
from typing import Any, ClassVar, Dict, List, Optional, Union
from typing_extensions import Annotated
from typing import Optional, Set
from typing_extensions import Self

class AlignmentMetrics(BaseModel):
    """
    Metrics measuring how well a ranking aligns with observed user behavior.  These metrics compare a candidate pipeline's ranking against ground truth derived from actual user interactions (clicks, purchases, etc.).
    """ # noqa: E501
    ndcg_at_k: Dict[str, Union[StrictFloat, StrictInt]] = Field(description="Normalized Discounted Cumulative Gain at various K values.")
    mean_rank_clicked: Union[StrictFloat, StrictInt] = Field(description="Average position of clicked items in the new ranking.")
    mean_rank_purchased: Optional[Union[StrictFloat, StrictInt]] = Field(default=None, description="Average position of purchased items (if any purchases observed).")
    recall_at_k: Dict[str, Union[StrictFloat, StrictInt]] = Field(description="Fraction of interacted items found in top K results.")
    avg_position_delta: Union[StrictFloat, StrictInt] = Field(description="Average change in position for interacted items (negative = promoted).")
    items_promoted: Annotated[int, Field(strict=True, ge=0)] = Field(description="Number of interacted items moved to higher positions.")
    items_demoted: Annotated[int, Field(strict=True, ge=0)] = Field(description="Number of interacted items moved to lower positions.")
    sessions_improved: Annotated[int, Field(strict=True, ge=0)] = Field(description="Sessions where candidate outperformed baseline.")
    sessions_degraded: Annotated[int, Field(strict=True, ge=0)] = Field(description="Sessions where candidate underperformed baseline.")
    sessions_neutral: Annotated[int, Field(strict=True, ge=0)] = Field(description="Sessions with no significant difference.")
    __properties: ClassVar[List[str]] = ["ndcg_at_k", "mean_rank_clicked", "mean_rank_purchased", "recall_at_k", "avg_position_delta", "items_promoted", "items_demoted", "sessions_improved", "sessions_degraded", "sessions_neutral"]

    model_config = ConfigDict(
        populate_by_name=True,
        validate_assignment=True,
        protected_namespaces=(),
    )


    def to_str(self) -> str:
        """Returns the string representation of the model using alias"""
        return pprint.pformat(self.model_dump(by_alias=True))

    def to_json(self) -> str:
        """Returns the JSON representation of the model using alias"""
        # TODO: pydantic v2: use .model_dump_json(by_alias=True, exclude_unset=True) instead
        return json.dumps(self.to_dict())

    @classmethod
    def from_json(cls, json_str: str) -> Optional[Self]:
        """Create an instance of AlignmentMetrics from a JSON string"""
        return cls.from_dict(json.loads(json_str))

    def to_dict(self) -> Dict[str, Any]:
        """Return the dictionary representation of the model using alias.

        This has the following differences from calling pydantic's
        `self.model_dump(by_alias=True)`:

        * `None` is only added to the output dict for nullable fields that
          were set at model initialization. Other fields with value `None`
          are ignored.
        """
        excluded_fields: Set[str] = set([
        ])

        _dict = self.model_dump(
            by_alias=True,
            exclude=excluded_fields,
            exclude_none=True,
        )
        return _dict

    @classmethod
    def from_dict(cls, obj: Optional[Dict[str, Any]]) -> Optional[Self]:
        """Create an instance of AlignmentMetrics from a dict"""
        if obj is None:
            return None

        if not isinstance(obj, dict):
            return cls.model_validate(obj)

        _obj = cls.model_validate({
            "ndcg_at_k": obj.get("ndcg_at_k"),
            "mean_rank_clicked": obj.get("mean_rank_clicked"),
            "mean_rank_purchased": obj.get("mean_rank_purchased"),
            "recall_at_k": obj.get("recall_at_k"),
            "avg_position_delta": obj.get("avg_position_delta"),
            "items_promoted": obj.get("items_promoted"),
            "items_demoted": obj.get("items_demoted"),
            "sessions_improved": obj.get("sessions_improved"),
            "sessions_degraded": obj.get("sessions_degraded"),
            "sessions_neutral": obj.get("sessions_neutral")
        })
        return _obj


