Skip to content

Client

Valor client object for interacting with the api.

Parameters:

Name Type Description Default
connection ClientConnection

Option to use an existing connection object.

None
Source code in valor/coretypes.py
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
class Client:
    """
    Valor client object for interacting with the api.

    Parameters
    ----------
    connection : ClientConnection, optional
        Option to use an existing connection object.
    """

    def __init__(self, connection: Optional[ClientConnection] = None):
        if not connection:
            connection = get_connection()
        self.conn = connection

    @classmethod
    def connect(
        cls,
        host: str,
        access_token: Optional[str] = None,
        reconnect: bool = False,
    ) -> Client:
        """
        Establishes a connection to the Valor API.

        Parameters
        ----------
        host : str
            The host to connect to. Should start with "http://" or "https://".
        access_token : str
            The access token for the host (if the host requires authentication).
        """
        connect(host=host, access_token=access_token, reconnect=reconnect)
        return cls(get_connection())

    def get_labels(
        self,
        filters: Optional[Filter] = None,
    ) -> List[Label]:
        """
        Gets all labels using an optional filter.

        Parameters
        ----------
        filters : Filter, optional
            Optional constraints to filter by.

        Returns
        ------
        List[valor.Label]
            A list of labels.
        """
        filters = filters if filters is not None else Filter()
        return [
            Label(**label) for label in self.conn.get_labels(filters.to_dict())
        ]

    def get_labels_from_dataset(
        self, dataset: Union[Dataset, str]
    ) -> List[Label]:
        """
        Get all labels associated with a dataset's ground truths.

        Parameters
        ----------
        dataset : valor.Dataset
            The dataset to search by.

        Returns
        ------
        List[valor.Label]
            A list of labels.
        """
        dataset_name = (
            dataset.name if isinstance(dataset, Dataset) else dataset
        )
        return [
            Label(**label)
            for label in self.conn.get_labels_from_dataset(dataset_name)  # type: ignore
        ]

    def get_labels_from_model(self, model: Union[Model, str]) -> List[Label]:
        """
        Get all labels associated with a model's ground truths.

        Parameters
        ----------
        model : valor.Model
            The model to search by.

        Returns
        ------
        List[valor.Label]
            A list of labels.
        """
        model_name = model.name if isinstance(model, Model) else model
        return [
            Label(**label)
            for label in self.conn.get_labels_from_model(model_name)  # type: ignore
        ]

    def create_dataset(
        self,
        dataset: Union[Dataset, dict],
    ) -> None:
        """
        Creates a dataset.

        Parameters
        ----------
        dataset : valor.Dataset
            The dataset to create.
        """
        if isinstance(dataset, Dataset):
            dataset = dataset.encode_value()
        self.conn.create_dataset(dataset)

    def create_groundtruths(
        self,
        dataset: Dataset,
        groundtruths: List[GroundTruth],
        ignore_existing_datums: bool = False,
        timeout: Optional[float] = None,
    ):
        """
        Creates ground truths.

        Parameters
        ----------

        dataset : valor.Dataset
            The dataset to create the ground truth for.
        groundtruths : List[valor.GroundTruth]
            The ground truths to create.
        timeout : float, optional
            The number of seconds the client should wait until raising a timeout.
        ignore_existing_datums : bool, default=False
            If True, will ignore datums that already exist in the backend.
            If False, will raise an error if any datums already exist.
            Default is False.
        """
        groundtruths_json = []
        for groundtruth in groundtruths:
            if not isinstance(groundtruth, GroundTruth):
                raise TypeError(
                    f"Expected ground truth to be of type 'valor.GroundTruth' not '{type(groundtruth)}'."
                )
            if not isinstance(groundtruth.annotations._value, list):
                raise TypeError
            groundtruth_dict = groundtruth.encode_value()
            groundtruth_dict["dataset_name"] = dataset.name
            groundtruths_json.append(groundtruth_dict)
        self.conn.create_groundtruths(
            groundtruths_json,
            timeout=timeout,
            ignore_existing_datums=ignore_existing_datums,
        )

    def get_groundtruth(
        self,
        dataset: Union[Dataset, str],
        datum: Union[Datum, str],
    ) -> Union[GroundTruth, None]:
        """
        Get a particular ground truth.

        Parameters
        ----------
        dataset : Union[Dataset, str]
            The dataset the datum belongs to.
        datum : Union[Datum, str]
            The desired datum.

        Returns
        ----------
        Union[GroundTruth, None]
            The matching ground truth or 'None' if it doesn't exist.
        """
        dataset_name = (
            dataset.name if isinstance(dataset, Dataset) else dataset
        )
        datum_uid = datum.uid if isinstance(datum, Datum) else datum
        try:
            resp = self.conn.get_groundtruth(
                dataset_name=dataset_name, datum_uid=datum_uid  # type: ignore
            )
            resp.pop("dataset_name")
            return GroundTruth.decode_value(resp)
        except ClientException as e:
            if e.status_code == 404:
                return None
            raise e

    def finalize_dataset(self, dataset: Union[Dataset, str]) -> None:
        """
        Finalizes a dataset such that new ground truths cannot be added to it.

        Parameters
        ----------
        dataset : str
            The dataset to be finalized.
        """
        dataset_name = (
            dataset.name if isinstance(dataset, Dataset) else dataset
        )
        return self.conn.finalize_dataset(name=dataset_name)  # type: ignore

    def get_dataset(
        self,
        name: str,
    ) -> Union[Dataset, None]:
        """
        Gets a dataset by name.

        Parameters
        ----------
        name : str
            The name of the dataset to fetch.

        Returns
        -------
        Union[Dataset, None]
            A Dataset with a matching name, or 'None' if one doesn't exist.
        """
        dataset = Dataset.decode_value(
            {
                **self.conn.get_dataset(name),
                "connection": self.conn,
            }
        )
        return dataset

    def get_datasets(
        self,
        filters: Optional[Filter] = None,
    ) -> List[Dataset]:
        """
        Get all datasets, with an option to filter results according to some user-defined parameters.

        Parameters
        ----------
        filters : Filter, optional
            Optional constraints to filter by.

        Returns
        ------
        List[valor.Dataset]
            A list of datasets.
        """
        dataset_list = []
        filters = filters if filters is not None else Filter()
        for kwargs in self.conn.get_datasets(filters.to_dict()):
            dataset = Dataset.decode_value({**kwargs, "connection": self.conn})
            dataset_list.append(dataset)
        return dataset_list

    def get_datums(
        self,
        filters: Optional[Filter] = None,
    ) -> List[Datum]:
        """
        Get all datums using an optional filter.

        Parameters
        ----------
        filters : Filter, optional
            Optional constraints to filter by.

        Returns
        -------
        List[valor.Datum]
            A list datums.
        """

        filters = filters if filters is not None else Filter()
        return [
            Datum.decode_value(datum)
            for datum in self.conn.get_datums(filters.to_dict())
        ]

    def get_datum(
        self,
        dataset: Union[Dataset, str],
        uid: str,
    ) -> Union[Datum, None]:
        """
        Get datum.
        `GET` endpoint.

        Parameters
        ----------
        dataset : valor.Dataset
            The dataset the datum belongs to.
        uid : str
            The UID of the datum.

        Returns
        -------
        valor.Datum
            The requested datum or 'None' if it doesn't exist.
        """
        dataset_name = (
            dataset.name if isinstance(dataset, Dataset) else dataset
        )
        resp = self.conn.get_datum(dataset_name=dataset_name, uid=uid)  # type: ignore
        return Datum.decode_value(resp)

    def get_dataset_status(
        self,
        name: str,
    ) -> Union[TableStatus, None]:
        """
        Get the state of a given dataset.

        Parameters
        ----------
        name : str
            The name of the dataset we want to fetch the state of.

        Returns
        ------
        TableStatus | None
            The state of the dataset, or 'None' if the dataset does not exist.
        """
        try:
            return self.conn.get_dataset_status(name)
        except ClientException as e:
            if e.status_code == 404:
                return None
            raise e

    def get_dataset_summary(
        self,
        name: str,
        *_,
        timeout: Optional[float] = None,
    ) -> DatasetSummary:
        """
        Gets the summary of a dataset.

        Parameters
        ----------
        name : str
            The name of the dataset to create a summary for.

        Returns
        -------
        DatasetSummary
            A dataclass containing the dataset summary.
        """
        return DatasetSummary(
            **self.conn.get_dataset_summary(name, timeout=timeout)
        )

    def delete_dataset(self, name: str, timeout: int = 0) -> None:
        """
        Deletes a dataset.

        Parameters
        ----------
        name : str
            The name of the dataset to be deleted.
        timeout : int
            The number of seconds to wait in order to confirm that the dataset was deleted.
        """
        self.conn.delete_dataset(name)
        if timeout:
            for _ in range(timeout):
                try:
                    self.get_dataset(name)
                except DatasetDoesNotExistError:
                    break
                time.sleep(1)
            else:
                raise TimeoutError(
                    "Dataset wasn't deleted within timeout interval"
                )

    def create_model(
        self,
        model: Union[Model, dict],
    ):
        """
        Creates a model.

        Parameters
        ----------
        model : valor.Model
            The model to create.
        """
        if isinstance(model, Model):
            model = model.encode_value()
        self.conn.create_model(model)

    def create_predictions(
        self,
        dataset: Dataset,
        model: Model,
        predictions: List[Prediction],
        timeout: Optional[float] = None,
    ) -> None:
        """
        Creates predictions.

        Parameters
        ----------
        dataset : valor.Dataset
            The dataset that is being operated over.
        model : valor.Model
            The model making the prediction.
        predictions : List[valor.Prediction]
            The predictions to create.
        timeout : float, optional
            The number of seconds the client should wait until raising a timeout.
        """
        predictions_json = []
        for prediction in predictions:
            if not isinstance(prediction, Prediction):
                raise TypeError(
                    f"Expected prediction to be of type 'valor.Prediction' not '{type(prediction)}'."
                )
            if not isinstance(prediction.annotations._value, list):
                raise TypeError
            prediction_dict = prediction.encode_value()
            prediction_dict["dataset_name"] = dataset.name
            prediction_dict["model_name"] = model.name
            predictions_json.append(prediction_dict)
        self.conn.create_predictions(predictions_json, timeout=timeout)

    def get_prediction(
        self,
        dataset: Union[Dataset, str],
        model: Union[Model, str],
        datum: Union[Datum, str],
    ) -> Union[Prediction, None]:
        """
        Get a particular prediction.

        Parameters
        ----------
        dataset : Union[Dataset, str]
            The dataset the datum belongs to.
        model : Union[Model, str]
            The model that made the prediction.
        datum : Union[Datum, str]
            The desired datum.

        Returns
        ----------
        Union[Prediction, None]
            The matching prediction or 'None' if it doesn't exist.
        """
        dataset_name = (
            dataset.name if isinstance(dataset, Dataset) else dataset
        )
        model_name = model.name if isinstance(model, Model) else model
        datum_uid = datum.uid if isinstance(datum, Datum) else datum

        resp = self.conn.get_prediction(
            dataset_name=dataset_name,  # type: ignore
            model_name=model_name,  # type: ignore
            datum_uid=datum_uid,  # type: ignore
        )
        resp.pop("dataset_name")
        resp.pop("model_name")
        return Prediction.decode_value(resp)

    def finalize_inferences(
        self, dataset: Union[Dataset, str], model: Union[Model, str]
    ) -> None:
        """
        Finalizes a model-dataset pairing such that new predictions cannot be added to it.
        """
        dataset_name = (
            dataset.name if isinstance(dataset, Dataset) else dataset
        )
        model_name = model.name if isinstance(model, Model) else model
        return self.conn.finalize_inferences(
            dataset_name=dataset_name,  # type: ignore
            model_name=model_name,  # type: ignore
        )

    def get_model(
        self,
        name: str,
    ) -> Union[Model, None]:
        """
        Gets a model by name.

        Parameters
        ----------
        name : str
            The name of the model to fetch.

        Returns
        -------
        Union[valor.Model, None]
            A Model with matching name or 'None' if one doesn't exist.
        """
        return Model.decode_value(
            {
                **self.conn.get_model(name),
                "connection": self.conn,
            }
        )

    def get_models(
        self,
        filters: Optional[Filter] = None,
    ) -> List[Model]:
        """
        Get all models using an optional filter.

        Parameters
        ----------
        filters : Filter, optional
            Optional constraints to filter by.

        Returns
        ------
        List[valor.Model]
            A list of models.
        """
        model_list = []
        filters = filters if filters is not None else Filter()
        for kwargs in self.conn.get_models(filters.to_dict()):
            model = Model.decode_value({**kwargs, "connection": self.conn})
            model_list.append(model)
        return model_list

    def get_model_status(
        self,
        dataset_name: str,
        model_name: str,
    ) -> Optional[TableStatus]:
        """
        Get the state of a given model over a dataset.

        Parameters
        ----------
        dataset_name : str
            The name of the dataset that the model is operating over.
        model_name : str
            The name of the model we want to fetch the state of.

        Returns
        ------
        Union[TableStatus, None]
            The state of the model or 'None' if the model doesn't exist.
        """
        try:
            return self.conn.get_model_status(dataset_name, model_name)
        except ClientException as e:
            if e.status_code == 404:
                return None
            raise e

    def get_model_eval_requests(
        self, model: Union[Model, str]
    ) -> List[Evaluation]:
        """
        Get all evaluations that have been created for a model.

        This does not return evaluation results.

        `GET` endpoint.

        Parameters
        ----------
        model : str
            The model to search by.

        Returns
        -------
        List[Evaluation]
            A list of evaluations.
        """
        model_name = model.name if isinstance(model, Model) else model
        return [
            Evaluation(**evaluation, connection=self.conn)
            for evaluation in self.conn.get_model_eval_requests(model_name)  # type: ignore
        ]

    def delete_model(self, name: str, timeout: int = 0) -> None:
        """
        Deletes a model.

        Parameters
        ----------
        name : str
            The name of the model to be deleted.
        timeout : int
            The number of seconds to wait in order to confirm that the model was deleted.
        """
        self.conn.delete_model(name)
        if timeout:
            for _ in range(timeout):
                try:
                    self.get_model(name)
                except ModelDoesNotExistError:
                    break
                time.sleep(1)
            else:
                raise TimeoutError(
                    "Model wasn't deleted within timeout interval"
                )

    def get_evaluations(
        self,
        *,
        evaluation_ids: Optional[List[int]] = None,
        models: Union[List[Model], List[str], None] = None,
        datasets: Union[List[Dataset], List[str], None] = None,
        metrics_to_sort_by: Optional[
            Dict[str, Union[Dict[str, str], str]]
        ] = None,
        timeout: Optional[float] = None,
    ) -> List[Evaluation]:
        """
        Returns all evaluations associated with user-supplied dataset and/or model names.

        Parameters
        ----------
        evaluation_ids : List[int], optional.
            A list of job IDs to return metrics for.
        models : Union[List[valor.Model], List[str]], optional
            A list of model names that we want to return metrics for.
        datasets : Union[List[valor.Dataset], List[str]], optional
            A list of dataset names that we want to return metrics for.
        metrics_to_sort_by : dict[str, str | dict[str, str]], optional
            An optional dict of metric types to sort the evaluations by.
        timeout : float, optional
            The number of seconds the client should wait until raising a timeout.

        Returns
        -------
        List[valor.Evaluation]
            A list of evaluations.
        """
        if isinstance(datasets, list):
            datasets = [  # type: ignore
                element.name if isinstance(element, Dataset) else element
                for element in datasets
            ]
        if isinstance(models, list):
            models = [  # type: ignore
                element.name if isinstance(element, Model) else element
                for element in models
            ]
        return [
            Evaluation(connection=self.conn, **evaluation)
            for evaluation in self.conn.get_evaluations(
                evaluation_ids=evaluation_ids,
                models=models,  # type: ignore
                datasets=datasets,  # type: ignore
                metrics_to_sort_by=metrics_to_sort_by,
                timeout=timeout,
            )
        ]

    def evaluate(
        self,
        request: EvaluationRequest,
        *_,
        allow_retries: bool = False,
        timeout: Optional[float] = None,
    ) -> List[Evaluation]:
        """
        Creates as many evaluations as necessary to fulfill the request.

        Parameters
        ----------
        request : schemas.EvaluationRequest
            The requested evaluation parameters.
        allow_retries : bool, default = False
            Option to retry previously failed evaluations.
        timeout : float, optional
            The number of seconds the client should wait until raising a timeout.

        Returns
        -------
        List[Evaluation]
            A list of evaluations that meet the parameters.
        """
        return [
            Evaluation(**evaluation)
            for evaluation in self.conn.evaluate(
                request.to_dict(),
                allow_retries=allow_retries,
                timeout=timeout,
            )
        ]

    def delete_evaluation(self, evaluation_id: int, timeout: int = 0) -> None:
        """
        Deletes an evaluation.

        Parameters
        ----------
        evaluation_id : int
            The id of the evaluation to be deleted.
        timeout : int, default=0
            The number of seconds to wait in order to confirm that the model was deleted.
        """
        self.conn.delete_evaluation(evaluation_id)
        if timeout:
            for _ in range(timeout):
                try:
                    self.get_evaluations(evaluation_ids=[evaluation_id])
                except EvaluationDoesNotExist:
                    break
                time.sleep(1)
            else:
                raise TimeoutError(
                    "Evaluation wasn't deleted within timeout interval"
                )

Functions

valor.Client.connect(host, access_token=None, reconnect=False) classmethod

Establishes a connection to the Valor API.

Parameters:

Name Type Description Default
host str

The host to connect to. Should start with "http://" or "https://".

required
access_token str

The access token for the host (if the host requires authentication).

None
Source code in valor/coretypes.py
@classmethod
def connect(
    cls,
    host: str,
    access_token: Optional[str] = None,
    reconnect: bool = False,
) -> Client:
    """
    Establishes a connection to the Valor API.

    Parameters
    ----------
    host : str
        The host to connect to. Should start with "http://" or "https://".
    access_token : str
        The access token for the host (if the host requires authentication).
    """
    connect(host=host, access_token=access_token, reconnect=reconnect)
    return cls(get_connection())

valor.Client.create_dataset(dataset)

Creates a dataset.

Parameters:

Name Type Description Default
dataset Dataset

The dataset to create.

required
Source code in valor/coretypes.py
def create_dataset(
    self,
    dataset: Union[Dataset, dict],
) -> None:
    """
    Creates a dataset.

    Parameters
    ----------
    dataset : valor.Dataset
        The dataset to create.
    """
    if isinstance(dataset, Dataset):
        dataset = dataset.encode_value()
    self.conn.create_dataset(dataset)

valor.Client.create_groundtruths(dataset, groundtruths, ignore_existing_datums=False, timeout=None)

Creates ground truths.

Parameters:

Name Type Description Default
dataset Dataset

The dataset to create the ground truth for.

required
groundtruths List[GroundTruth]

The ground truths to create.

required
timeout float

The number of seconds the client should wait until raising a timeout.

None
ignore_existing_datums bool

If True, will ignore datums that already exist in the backend. If False, will raise an error if any datums already exist. Default is False.

False
Source code in valor/coretypes.py
def create_groundtruths(
    self,
    dataset: Dataset,
    groundtruths: List[GroundTruth],
    ignore_existing_datums: bool = False,
    timeout: Optional[float] = None,
):
    """
    Creates ground truths.

    Parameters
    ----------

    dataset : valor.Dataset
        The dataset to create the ground truth for.
    groundtruths : List[valor.GroundTruth]
        The ground truths to create.
    timeout : float, optional
        The number of seconds the client should wait until raising a timeout.
    ignore_existing_datums : bool, default=False
        If True, will ignore datums that already exist in the backend.
        If False, will raise an error if any datums already exist.
        Default is False.
    """
    groundtruths_json = []
    for groundtruth in groundtruths:
        if not isinstance(groundtruth, GroundTruth):
            raise TypeError(
                f"Expected ground truth to be of type 'valor.GroundTruth' not '{type(groundtruth)}'."
            )
        if not isinstance(groundtruth.annotations._value, list):
            raise TypeError
        groundtruth_dict = groundtruth.encode_value()
        groundtruth_dict["dataset_name"] = dataset.name
        groundtruths_json.append(groundtruth_dict)
    self.conn.create_groundtruths(
        groundtruths_json,
        timeout=timeout,
        ignore_existing_datums=ignore_existing_datums,
    )

valor.Client.create_model(model)

Creates a model.

Parameters:

Name Type Description Default
model Model

The model to create.

required
Source code in valor/coretypes.py
def create_model(
    self,
    model: Union[Model, dict],
):
    """
    Creates a model.

    Parameters
    ----------
    model : valor.Model
        The model to create.
    """
    if isinstance(model, Model):
        model = model.encode_value()
    self.conn.create_model(model)

valor.Client.create_predictions(dataset, model, predictions, timeout=None)

Creates predictions.

Parameters:

Name Type Description Default
dataset Dataset

The dataset that is being operated over.

required
model Model

The model making the prediction.

required
predictions List[Prediction]

The predictions to create.

required
timeout float

The number of seconds the client should wait until raising a timeout.

None
Source code in valor/coretypes.py
def create_predictions(
    self,
    dataset: Dataset,
    model: Model,
    predictions: List[Prediction],
    timeout: Optional[float] = None,
) -> None:
    """
    Creates predictions.

    Parameters
    ----------
    dataset : valor.Dataset
        The dataset that is being operated over.
    model : valor.Model
        The model making the prediction.
    predictions : List[valor.Prediction]
        The predictions to create.
    timeout : float, optional
        The number of seconds the client should wait until raising a timeout.
    """
    predictions_json = []
    for prediction in predictions:
        if not isinstance(prediction, Prediction):
            raise TypeError(
                f"Expected prediction to be of type 'valor.Prediction' not '{type(prediction)}'."
            )
        if not isinstance(prediction.annotations._value, list):
            raise TypeError
        prediction_dict = prediction.encode_value()
        prediction_dict["dataset_name"] = dataset.name
        prediction_dict["model_name"] = model.name
        predictions_json.append(prediction_dict)
    self.conn.create_predictions(predictions_json, timeout=timeout)

valor.Client.delete_dataset(name, timeout=0)

Deletes a dataset.

Parameters:

Name Type Description Default
name str

The name of the dataset to be deleted.

required
timeout int

The number of seconds to wait in order to confirm that the dataset was deleted.

0
Source code in valor/coretypes.py
def delete_dataset(self, name: str, timeout: int = 0) -> None:
    """
    Deletes a dataset.

    Parameters
    ----------
    name : str
        The name of the dataset to be deleted.
    timeout : int
        The number of seconds to wait in order to confirm that the dataset was deleted.
    """
    self.conn.delete_dataset(name)
    if timeout:
        for _ in range(timeout):
            try:
                self.get_dataset(name)
            except DatasetDoesNotExistError:
                break
            time.sleep(1)
        else:
            raise TimeoutError(
                "Dataset wasn't deleted within timeout interval"
            )

valor.Client.delete_evaluation(evaluation_id, timeout=0)

Deletes an evaluation.

Parameters:

Name Type Description Default
evaluation_id int

The id of the evaluation to be deleted.

required
timeout int

The number of seconds to wait in order to confirm that the model was deleted.

0
Source code in valor/coretypes.py
def delete_evaluation(self, evaluation_id: int, timeout: int = 0) -> None:
    """
    Deletes an evaluation.

    Parameters
    ----------
    evaluation_id : int
        The id of the evaluation to be deleted.
    timeout : int, default=0
        The number of seconds to wait in order to confirm that the model was deleted.
    """
    self.conn.delete_evaluation(evaluation_id)
    if timeout:
        for _ in range(timeout):
            try:
                self.get_evaluations(evaluation_ids=[evaluation_id])
            except EvaluationDoesNotExist:
                break
            time.sleep(1)
        else:
            raise TimeoutError(
                "Evaluation wasn't deleted within timeout interval"
            )

valor.Client.delete_model(name, timeout=0)

Deletes a model.

Parameters:

Name Type Description Default
name str

The name of the model to be deleted.

required
timeout int

The number of seconds to wait in order to confirm that the model was deleted.

0
Source code in valor/coretypes.py
def delete_model(self, name: str, timeout: int = 0) -> None:
    """
    Deletes a model.

    Parameters
    ----------
    name : str
        The name of the model to be deleted.
    timeout : int
        The number of seconds to wait in order to confirm that the model was deleted.
    """
    self.conn.delete_model(name)
    if timeout:
        for _ in range(timeout):
            try:
                self.get_model(name)
            except ModelDoesNotExistError:
                break
            time.sleep(1)
        else:
            raise TimeoutError(
                "Model wasn't deleted within timeout interval"
            )

valor.Client.evaluate(request, *_, allow_retries=False, timeout=None)

Creates as many evaluations as necessary to fulfill the request.

Parameters:

Name Type Description Default
request EvaluationRequest

The requested evaluation parameters.

required
allow_retries bool

Option to retry previously failed evaluations.

= False
timeout float

The number of seconds the client should wait until raising a timeout.

None

Returns:

Type Description
List[Evaluation]

A list of evaluations that meet the parameters.

Source code in valor/coretypes.py
def evaluate(
    self,
    request: EvaluationRequest,
    *_,
    allow_retries: bool = False,
    timeout: Optional[float] = None,
) -> List[Evaluation]:
    """
    Creates as many evaluations as necessary to fulfill the request.

    Parameters
    ----------
    request : schemas.EvaluationRequest
        The requested evaluation parameters.
    allow_retries : bool, default = False
        Option to retry previously failed evaluations.
    timeout : float, optional
        The number of seconds the client should wait until raising a timeout.

    Returns
    -------
    List[Evaluation]
        A list of evaluations that meet the parameters.
    """
    return [
        Evaluation(**evaluation)
        for evaluation in self.conn.evaluate(
            request.to_dict(),
            allow_retries=allow_retries,
            timeout=timeout,
        )
    ]

valor.Client.finalize_dataset(dataset)

Finalizes a dataset such that new ground truths cannot be added to it.

Parameters:

Name Type Description Default
dataset str

The dataset to be finalized.

required
Source code in valor/coretypes.py
def finalize_dataset(self, dataset: Union[Dataset, str]) -> None:
    """
    Finalizes a dataset such that new ground truths cannot be added to it.

    Parameters
    ----------
    dataset : str
        The dataset to be finalized.
    """
    dataset_name = (
        dataset.name if isinstance(dataset, Dataset) else dataset
    )
    return self.conn.finalize_dataset(name=dataset_name)  # type: ignore

valor.Client.finalize_inferences(dataset, model)

Finalizes a model-dataset pairing such that new predictions cannot be added to it.

Source code in valor/coretypes.py
def finalize_inferences(
    self, dataset: Union[Dataset, str], model: Union[Model, str]
) -> None:
    """
    Finalizes a model-dataset pairing such that new predictions cannot be added to it.
    """
    dataset_name = (
        dataset.name if isinstance(dataset, Dataset) else dataset
    )
    model_name = model.name if isinstance(model, Model) else model
    return self.conn.finalize_inferences(
        dataset_name=dataset_name,  # type: ignore
        model_name=model_name,  # type: ignore
    )

valor.Client.get_dataset(name)

Gets a dataset by name.

Parameters:

Name Type Description Default
name str

The name of the dataset to fetch.

required

Returns:

Type Description
Union[Dataset, None]

A Dataset with a matching name, or 'None' if one doesn't exist.

Source code in valor/coretypes.py
def get_dataset(
    self,
    name: str,
) -> Union[Dataset, None]:
    """
    Gets a dataset by name.

    Parameters
    ----------
    name : str
        The name of the dataset to fetch.

    Returns
    -------
    Union[Dataset, None]
        A Dataset with a matching name, or 'None' if one doesn't exist.
    """
    dataset = Dataset.decode_value(
        {
            **self.conn.get_dataset(name),
            "connection": self.conn,
        }
    )
    return dataset

valor.Client.get_dataset_status(name)

Get the state of a given dataset.

Parameters:

Name Type Description Default
name str

The name of the dataset we want to fetch the state of.

required

Returns:

Type Description
TableStatus | None

The state of the dataset, or 'None' if the dataset does not exist.

Source code in valor/coretypes.py
def get_dataset_status(
    self,
    name: str,
) -> Union[TableStatus, None]:
    """
    Get the state of a given dataset.

    Parameters
    ----------
    name : str
        The name of the dataset we want to fetch the state of.

    Returns
    ------
    TableStatus | None
        The state of the dataset, or 'None' if the dataset does not exist.
    """
    try:
        return self.conn.get_dataset_status(name)
    except ClientException as e:
        if e.status_code == 404:
            return None
        raise e

valor.Client.get_dataset_summary(name, *_, timeout=None)

Gets the summary of a dataset.

Parameters:

Name Type Description Default
name str

The name of the dataset to create a summary for.

required

Returns:

Type Description
DatasetSummary

A dataclass containing the dataset summary.

Source code in valor/coretypes.py
def get_dataset_summary(
    self,
    name: str,
    *_,
    timeout: Optional[float] = None,
) -> DatasetSummary:
    """
    Gets the summary of a dataset.

    Parameters
    ----------
    name : str
        The name of the dataset to create a summary for.

    Returns
    -------
    DatasetSummary
        A dataclass containing the dataset summary.
    """
    return DatasetSummary(
        **self.conn.get_dataset_summary(name, timeout=timeout)
    )

valor.Client.get_datasets(filters=None)

Get all datasets, with an option to filter results according to some user-defined parameters.

Parameters:

Name Type Description Default
filters Filter

Optional constraints to filter by.

None

Returns:

Type Description
List[Dataset]

A list of datasets.

Source code in valor/coretypes.py
def get_datasets(
    self,
    filters: Optional[Filter] = None,
) -> List[Dataset]:
    """
    Get all datasets, with an option to filter results according to some user-defined parameters.

    Parameters
    ----------
    filters : Filter, optional
        Optional constraints to filter by.

    Returns
    ------
    List[valor.Dataset]
        A list of datasets.
    """
    dataset_list = []
    filters = filters if filters is not None else Filter()
    for kwargs in self.conn.get_datasets(filters.to_dict()):
        dataset = Dataset.decode_value({**kwargs, "connection": self.conn})
        dataset_list.append(dataset)
    return dataset_list

valor.Client.get_datum(dataset, uid)

Get datum. GET endpoint.

Parameters:

Name Type Description Default
dataset Dataset

The dataset the datum belongs to.

required
uid str

The UID of the datum.

required

Returns:

Type Description
Datum

The requested datum or 'None' if it doesn't exist.

Source code in valor/coretypes.py
def get_datum(
    self,
    dataset: Union[Dataset, str],
    uid: str,
) -> Union[Datum, None]:
    """
    Get datum.
    `GET` endpoint.

    Parameters
    ----------
    dataset : valor.Dataset
        The dataset the datum belongs to.
    uid : str
        The UID of the datum.

    Returns
    -------
    valor.Datum
        The requested datum or 'None' if it doesn't exist.
    """
    dataset_name = (
        dataset.name if isinstance(dataset, Dataset) else dataset
    )
    resp = self.conn.get_datum(dataset_name=dataset_name, uid=uid)  # type: ignore
    return Datum.decode_value(resp)

valor.Client.get_datums(filters=None)

Get all datums using an optional filter.

Parameters:

Name Type Description Default
filters Filter

Optional constraints to filter by.

None

Returns:

Type Description
List[Datum]

A list datums.

Source code in valor/coretypes.py
def get_datums(
    self,
    filters: Optional[Filter] = None,
) -> List[Datum]:
    """
    Get all datums using an optional filter.

    Parameters
    ----------
    filters : Filter, optional
        Optional constraints to filter by.

    Returns
    -------
    List[valor.Datum]
        A list datums.
    """

    filters = filters if filters is not None else Filter()
    return [
        Datum.decode_value(datum)
        for datum in self.conn.get_datums(filters.to_dict())
    ]

valor.Client.get_evaluations(*, evaluation_ids=None, models=None, datasets=None, metrics_to_sort_by=None, timeout=None)

Returns all evaluations associated with user-supplied dataset and/or model names.

Parameters:

Name Type Description Default
evaluation_ids List[int], optional.

A list of job IDs to return metrics for.

None
models Union[List[Model], List[str]]

A list of model names that we want to return metrics for.

None
datasets Union[List[Dataset], List[str]]

A list of dataset names that we want to return metrics for.

None
metrics_to_sort_by dict[str, str | dict[str, str]]

An optional dict of metric types to sort the evaluations by.

None
timeout float

The number of seconds the client should wait until raising a timeout.

None

Returns:

Type Description
List[Evaluation]

A list of evaluations.

Source code in valor/coretypes.py
def get_evaluations(
    self,
    *,
    evaluation_ids: Optional[List[int]] = None,
    models: Union[List[Model], List[str], None] = None,
    datasets: Union[List[Dataset], List[str], None] = None,
    metrics_to_sort_by: Optional[
        Dict[str, Union[Dict[str, str], str]]
    ] = None,
    timeout: Optional[float] = None,
) -> List[Evaluation]:
    """
    Returns all evaluations associated with user-supplied dataset and/or model names.

    Parameters
    ----------
    evaluation_ids : List[int], optional.
        A list of job IDs to return metrics for.
    models : Union[List[valor.Model], List[str]], optional
        A list of model names that we want to return metrics for.
    datasets : Union[List[valor.Dataset], List[str]], optional
        A list of dataset names that we want to return metrics for.
    metrics_to_sort_by : dict[str, str | dict[str, str]], optional
        An optional dict of metric types to sort the evaluations by.
    timeout : float, optional
        The number of seconds the client should wait until raising a timeout.

    Returns
    -------
    List[valor.Evaluation]
        A list of evaluations.
    """
    if isinstance(datasets, list):
        datasets = [  # type: ignore
            element.name if isinstance(element, Dataset) else element
            for element in datasets
        ]
    if isinstance(models, list):
        models = [  # type: ignore
            element.name if isinstance(element, Model) else element
            for element in models
        ]
    return [
        Evaluation(connection=self.conn, **evaluation)
        for evaluation in self.conn.get_evaluations(
            evaluation_ids=evaluation_ids,
            models=models,  # type: ignore
            datasets=datasets,  # type: ignore
            metrics_to_sort_by=metrics_to_sort_by,
            timeout=timeout,
        )
    ]

valor.Client.get_groundtruth(dataset, datum)

Get a particular ground truth.

Parameters:

Name Type Description Default
dataset Union[Dataset, str]

The dataset the datum belongs to.

required
datum Union[Datum, str]

The desired datum.

required

Returns:

Type Description
Union[GroundTruth, None]

The matching ground truth or 'None' if it doesn't exist.

Source code in valor/coretypes.py
def get_groundtruth(
    self,
    dataset: Union[Dataset, str],
    datum: Union[Datum, str],
) -> Union[GroundTruth, None]:
    """
    Get a particular ground truth.

    Parameters
    ----------
    dataset : Union[Dataset, str]
        The dataset the datum belongs to.
    datum : Union[Datum, str]
        The desired datum.

    Returns
    ----------
    Union[GroundTruth, None]
        The matching ground truth or 'None' if it doesn't exist.
    """
    dataset_name = (
        dataset.name if isinstance(dataset, Dataset) else dataset
    )
    datum_uid = datum.uid if isinstance(datum, Datum) else datum
    try:
        resp = self.conn.get_groundtruth(
            dataset_name=dataset_name, datum_uid=datum_uid  # type: ignore
        )
        resp.pop("dataset_name")
        return GroundTruth.decode_value(resp)
    except ClientException as e:
        if e.status_code == 404:
            return None
        raise e

valor.Client.get_labels(filters=None)

Gets all labels using an optional filter.

Parameters:

Name Type Description Default
filters Filter

Optional constraints to filter by.

None

Returns:

Type Description
List[Label]

A list of labels.

Source code in valor/coretypes.py
def get_labels(
    self,
    filters: Optional[Filter] = None,
) -> List[Label]:
    """
    Gets all labels using an optional filter.

    Parameters
    ----------
    filters : Filter, optional
        Optional constraints to filter by.

    Returns
    ------
    List[valor.Label]
        A list of labels.
    """
    filters = filters if filters is not None else Filter()
    return [
        Label(**label) for label in self.conn.get_labels(filters.to_dict())
    ]

valor.Client.get_labels_from_dataset(dataset)

Get all labels associated with a dataset's ground truths.

Parameters:

Name Type Description Default
dataset Dataset

The dataset to search by.

required

Returns:

Type Description
List[Label]

A list of labels.

Source code in valor/coretypes.py
def get_labels_from_dataset(
    self, dataset: Union[Dataset, str]
) -> List[Label]:
    """
    Get all labels associated with a dataset's ground truths.

    Parameters
    ----------
    dataset : valor.Dataset
        The dataset to search by.

    Returns
    ------
    List[valor.Label]
        A list of labels.
    """
    dataset_name = (
        dataset.name if isinstance(dataset, Dataset) else dataset
    )
    return [
        Label(**label)
        for label in self.conn.get_labels_from_dataset(dataset_name)  # type: ignore
    ]

valor.Client.get_labels_from_model(model)

Get all labels associated with a model's ground truths.

Parameters:

Name Type Description Default
model Model

The model to search by.

required

Returns:

Type Description
List[Label]

A list of labels.

Source code in valor/coretypes.py
def get_labels_from_model(self, model: Union[Model, str]) -> List[Label]:
    """
    Get all labels associated with a model's ground truths.

    Parameters
    ----------
    model : valor.Model
        The model to search by.

    Returns
    ------
    List[valor.Label]
        A list of labels.
    """
    model_name = model.name if isinstance(model, Model) else model
    return [
        Label(**label)
        for label in self.conn.get_labels_from_model(model_name)  # type: ignore
    ]

valor.Client.get_model(name)

Gets a model by name.

Parameters:

Name Type Description Default
name str

The name of the model to fetch.

required

Returns:

Type Description
Union[Model, None]

A Model with matching name or 'None' if one doesn't exist.

Source code in valor/coretypes.py
def get_model(
    self,
    name: str,
) -> Union[Model, None]:
    """
    Gets a model by name.

    Parameters
    ----------
    name : str
        The name of the model to fetch.

    Returns
    -------
    Union[valor.Model, None]
        A Model with matching name or 'None' if one doesn't exist.
    """
    return Model.decode_value(
        {
            **self.conn.get_model(name),
            "connection": self.conn,
        }
    )

valor.Client.get_model_eval_requests(model)

Get all evaluations that have been created for a model.

This does not return evaluation results.

GET endpoint.

Parameters:

Name Type Description Default
model str

The model to search by.

required

Returns:

Type Description
List[Evaluation]

A list of evaluations.

Source code in valor/coretypes.py
def get_model_eval_requests(
    self, model: Union[Model, str]
) -> List[Evaluation]:
    """
    Get all evaluations that have been created for a model.

    This does not return evaluation results.

    `GET` endpoint.

    Parameters
    ----------
    model : str
        The model to search by.

    Returns
    -------
    List[Evaluation]
        A list of evaluations.
    """
    model_name = model.name if isinstance(model, Model) else model
    return [
        Evaluation(**evaluation, connection=self.conn)
        for evaluation in self.conn.get_model_eval_requests(model_name)  # type: ignore
    ]

valor.Client.get_model_status(dataset_name, model_name)

Get the state of a given model over a dataset.

Parameters:

Name Type Description Default
dataset_name str

The name of the dataset that the model is operating over.

required
model_name str

The name of the model we want to fetch the state of.

required

Returns:

Type Description
Union[TableStatus, None]

The state of the model or 'None' if the model doesn't exist.

Source code in valor/coretypes.py
def get_model_status(
    self,
    dataset_name: str,
    model_name: str,
) -> Optional[TableStatus]:
    """
    Get the state of a given model over a dataset.

    Parameters
    ----------
    dataset_name : str
        The name of the dataset that the model is operating over.
    model_name : str
        The name of the model we want to fetch the state of.

    Returns
    ------
    Union[TableStatus, None]
        The state of the model or 'None' if the model doesn't exist.
    """
    try:
        return self.conn.get_model_status(dataset_name, model_name)
    except ClientException as e:
        if e.status_code == 404:
            return None
        raise e

valor.Client.get_models(filters=None)

Get all models using an optional filter.

Parameters:

Name Type Description Default
filters Filter

Optional constraints to filter by.

None

Returns:

Type Description
List[Model]

A list of models.

Source code in valor/coretypes.py
def get_models(
    self,
    filters: Optional[Filter] = None,
) -> List[Model]:
    """
    Get all models using an optional filter.

    Parameters
    ----------
    filters : Filter, optional
        Optional constraints to filter by.

    Returns
    ------
    List[valor.Model]
        A list of models.
    """
    model_list = []
    filters = filters if filters is not None else Filter()
    for kwargs in self.conn.get_models(filters.to_dict()):
        model = Model.decode_value({**kwargs, "connection": self.conn})
        model_list.append(model)
    return model_list

valor.Client.get_prediction(dataset, model, datum)

Get a particular prediction.

Parameters:

Name Type Description Default
dataset Union[Dataset, str]

The dataset the datum belongs to.

required
model Union[Model, str]

The model that made the prediction.

required
datum Union[Datum, str]

The desired datum.

required

Returns:

Type Description
Union[Prediction, None]

The matching prediction or 'None' if it doesn't exist.

Source code in valor/coretypes.py
def get_prediction(
    self,
    dataset: Union[Dataset, str],
    model: Union[Model, str],
    datum: Union[Datum, str],
) -> Union[Prediction, None]:
    """
    Get a particular prediction.

    Parameters
    ----------
    dataset : Union[Dataset, str]
        The dataset the datum belongs to.
    model : Union[Model, str]
        The model that made the prediction.
    datum : Union[Datum, str]
        The desired datum.

    Returns
    ----------
    Union[Prediction, None]
        The matching prediction or 'None' if it doesn't exist.
    """
    dataset_name = (
        dataset.name if isinstance(dataset, Dataset) else dataset
    )
    model_name = model.name if isinstance(model, Model) else model
    datum_uid = datum.uid if isinstance(datum, Datum) else datum

    resp = self.conn.get_prediction(
        dataset_name=dataset_name,  # type: ignore
        model_name=model_name,  # type: ignore
        datum_uid=datum_uid,  # type: ignore
    )
    resp.pop("dataset_name")
    resp.pop("model_name")
    return Prediction.decode_value(resp)