> For the complete documentation index, see [llms.txt](https://docs.zama.org/concrete-ml/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.zama.org/concrete-ml/1.0-2/developer-guide/api/concrete.ml.pytest.utils.md).

# concrete.ml.pytest.utils.md

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.0.x/src/concrete/ml/pytest/utils.py#L0)

## module `concrete.ml.pytest.utils`

Common functions or lists for test files, which can't be put in fixtures.

### **Global Variables**

* **sklearn\_models\_and\_datasets**

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.0.x/src/concrete/ml/pytest/utils.py#L123)

### function `get_random_extract_of_sklearn_models_and_datasets`

```python
get_random_extract_of_sklearn_models_and_datasets()
```

Return a random sublist of sklearn\_models\_and\_datasets.

The sublist contains exactly one model of each kind.

**Returns:** the sublist

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.0.x/src/concrete/ml/pytest/utils.py#L150)

### function `instantiate_model_generic`

```python
instantiate_model_generic(model_class, **parameters)
```

Instantiate any Concrete ML model type.

**Args:**

* `model_class` (class): The type of the model to instantiate
* `parameters` (dict): Hyper-parameters for the model instantiation

**Returns:**

* `model_name` (str): The type of the model as a string
* `model` (object): The model instance

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.0.x/src/concrete/ml/pytest/utils.py#L193)

### function `get_torchvision_dataset`

```python
get_torchvision_dataset(param: Dict, train_set: bool)
```

Get train or testing data-set.

**Args:**

* `param` (Dict): Set of hyper-parameters to use based on the selected torchvision data-set.
* `It must contain`: data-set transformations (torchvision.transforms.Compose), and the data-set\_size (Optional\[int]).
* `train_set` (bool): Use train data-set if True, else testing data-set

**Returns:** A torchvision data-sets.

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.0.x/src/concrete/ml/pytest/utils.py#L221)

### function `data_calibration_processing`

```python
data_calibration_processing(data, n_sample: int, targets=None)
```

Reduce size of the given data-set.

**Args:**

* `data`: The input container to consider
* `n_sample` (int): Number of samples to keep if the given data-set
* `targets`: If `dataset` is a `torch.utils.data.Dataset`, it typically contains both the data and the corresponding targets. In this case, `targets` must be set to `None`. If `data` is instance of `torch.Tensor` or 'numpy.ndarray`,` targets\` is expected.

**Returns:**

* `Tuple[numpy.ndarray, numpy.ndarray]`: The input data and the target (respectively x and y).

**Raises:**

* `TypeError`: If the 'data-set' does not match any expected type.

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.0.x/src/concrete/ml/pytest/utils.py#L273)

### function `load_torch_model`

```python
load_torch_model(
    model_class: Module,
    state_dict_or_path: Optional[str, Path, Dict[str, Any]],
    params: Dict,
    device: str = 'cpu'
) → Module
```

Load an object saved with torch.save() from a file or dict.

**Args:**

* `model_class` (torch.nn.Module): A PyTorch or Brevitas network.
* `state_dict_or_path` (Optional\[Union\[str, Path, Dict\[str, Any]]]): Path or state\_dict
* `params` (Dict): Model's parameters
* `device` (str): Device type.

**Returns:**

* `torch.nn.Module`: A PyTorch or Brevitas network.

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.0.x/src/concrete/ml/pytest/utils.py#L303)

### function `values_are_equal`

```python
values_are_equal(value_1: Any, value_2: Any) → bool
```

Indicate if two values are equal.

This method takes into account objects of type None, numpy.ndarray, numpy.floating, numpy.integer, numpy.random.RandomState or any instance that provides a `__eq__` method.

**Args:**

* `value_2` (Any): The first value to consider.
* `value_1` (Any): The second value to consider.

**Returns:**

* `bool`: If the two values are equal.

***

[![](https://img.shields.io/badge/-source-cccccc?style=flat-square)](https://github.com/zama-ai/concrete-ml/blob/release/1.0.x/src/concrete/ml/pytest/utils.py#L347)

### function `check_serialization`

```python
check_serialization(
    object_to_serialize: Any,
    expected_type: Type,
    equal_method: Optional[Callable] = None,
    check_str: bool = True
)
```

Check that the given object can properly be serialized.

This function serializes all objects using the `dump`, `dumps`, `load` and `loads` functions from Concrete ML. If the given object provides a `dump` and `dumps` method, they are also serialized using these.

**Args:**

* `object_to_serialize` (Any): The object to serialize.
* `expected_type` (Type): The object's expected type.
* `equal_method` (Optional\[Callable]): The function to use to compare the two loaded objects. Default to `values_are_equal`.
* `check_str` (bool): If the JSON strings should also be checked. Default to True.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://docs.zama.org/concrete-ml/1.0-2/developer-guide/api/concrete.ml.pytest.utils.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
