Skip to content

Rapidata results

RapidataResults #

Bases: dict

A specialized dictionary class for handling Rapidata API results. Extends the built-in dict class with specialized methods.

to_pandas #

to_pandas(split_details: bool = False) -> DataFrame
Warning

This method is currently under development. The structure of the results may change in the future.

Converts the results to a pandas DataFrame.

For Compare results (exactly 2 assets), creates standardized A_/B_ columns for each metric (plus Both_/Neither_ when those options are present). For Ranking results with N assets, creates one <asset>_<metric> column per asset. For Classification results, creates one row per datapoint and answer option, with option, count, ratio, summedUserScore and summedUserScoreRatio columns. For other results, flattens nested dictionaries into columns with underscore-separated names.

Parameters:

Name Type Description Default
split_details bool

If True, splits each datapoint by its detailed results, creating a row for each response with global metrics copied.

False

Returns:

Type Description
DataFrame

pd.DataFrame: A DataFrame containing the processed results

Raises:

Type Description
ValueError

If split_details is True but no detailed results are found

Source code in src/rapidata/rapidata_client/results/rapidata_results.py
def to_pandas(self, split_details: bool = False) -> "pd.DataFrame":
    """
    Warning:
        This method is currently under development. The structure of the results may change in the future.

    Converts the results to a pandas DataFrame.

    For Compare results (exactly 2 assets), creates standardized ``A_``/``B_``
    columns for each metric (plus ``Both_``/``Neither_`` when those options
    are present). For Ranking results with N assets, creates one
    ``<asset>_<metric>`` column per asset. For Classification results,
    creates one row per datapoint and answer option, with ``option``,
    ``count``, ``ratio``, ``summedUserScore`` and ``summedUserScoreRatio``
    columns. For other results, flattens nested dictionaries into columns
    with underscore-separated names.

    Args:
        split_details: If True, splits each datapoint by its detailed results,
                      creating a row for each response with global metrics copied.

    Returns:
        pd.DataFrame: A DataFrame containing the processed results

    Raises:
        ValueError: If split_details is True but no detailed results are found
    """
    import pandas as pd
    from pandas.core.indexes.base import Index

    if "results" not in self or not self["results"]:
        return pd.DataFrame()

    task_type = self._task_type()
    if task_type is None:
        managed_print(
            "Warning: Results are old and Order type is not specified. Dataframe might be wrong."
        )

    if split_details and not self._has_detailed_results():
        raise ValueError("No detailed results found in the data")

    # Compare/Ranking have an asset-aware shape; route them to the
    # compare-specific path even when split_details=True so we keep the
    # A_/B_ (or per-asset) column splitting.
    if task_type in ("Compare", "Ranking"):
        return self._compare_to_pandas(split_details=split_details)

    if split_details:
        return self._to_pandas_with_detailed_results()

    if task_type == "Classification":
        return self._classification_to_pandas()

    # Get the structure from first item
    first_item = self["results"][0]
    columns = []
    path_map = {}  # Maps flattened column names to paths to reach the values

    # Build the column structure once
    self._build_column_structure(first_item, columns, path_map)

    # Extract data using the known structure
    data = []
    for item in self["results"]:
        row = []
        for path in path_map.values():
            value = self._get_value_from_path(item, path)
            row.append(value)
        data.append(row)

    return pd.DataFrame(data, columns=Index(columns))

to_json #

to_json(path: str = './results.json') -> None

Saves the results to a JSON file.

Parameters:

Name Type Description Default
path str

The file path where the JSON should be saved. Defaults to "./results.json".

'./results.json'
Source code in src/rapidata/rapidata_client/results/rapidata_results.py
def to_json(self, path: str = "./results.json") -> None:
    """
    Saves the results to a JSON file.

    Args:
        path: The file path where the JSON should be saved. Defaults to "./results.json".
    """
    with open(path, "w") as f:
        json.dump(self, f)