> 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.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.2.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**

* **MODELS\_AND\_DATASETS**
* **UNIQUE\_MODELS\_AND\_DATASETS**

***

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

### function `get_sklearn_linear_models_and_datasets`

```python
get_sklearn_linear_models_and_datasets(
    regressor: bool = True,
    classifier: bool = True,
    unique_models: bool = False,
    select: Optional[str, List[str]] = None,
    ignore: Optional[str, List[str]] = None
) → List
```

Get the pytest parameters to use for testing linear models.

**Args:**

* `regressor` (bool): If regressors should be selected.
* `classifier` (bool): If classifiers should be selected.
* `unique_models` (bool): If each models should be represented only once.
* `select` (Optional\[Union\[str, List\[str]]]): If not None, only return models which names (or a part of it) match the given string or list of strings. Default to None.
* `ignore` (Optional\[Union\[str, List\[str]]]): If not None, only return models which names (or a part of it) do not match the given string or list of strings. Default to None.

**Returns:**

* `List`: The pytest parameters to use for testing linear models.

***

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

### function `get_sklearn_tree_models_and_datasets`

```python
get_sklearn_tree_models_and_datasets(
    regressor: bool = True,
    classifier: bool = True,
    unique_models: bool = False,
    select: Optional[str, List[str]] = None,
    ignore: Optional[str, List[str]] = None
) → List
```

Get the pytest parameters to use for testing tree-based models.

**Args:**

* `regressor` (bool): If regressors should be selected.
* `classifier` (bool): If classifiers should be selected.
* `unique_models` (bool): If each models should be represented only once.
* `select` (Optional\[Union\[str, List\[str]]]): If not None, only return models which names (or a part of it) match the given string or list of strings. Default to None.
* `ignore` (Optional\[Union\[str, List\[str]]]): If not None, only return models which names (or a part of it) do not match the given string or list of strings. Default to None.

**Returns:**

* `List`: The pytest parameters to use for testing tree-based models.

***

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

### function `get_sklearn_neural_net_models_and_datasets`

```python
get_sklearn_neural_net_models_and_datasets(
    regressor: bool = True,
    classifier: bool = True,
    unique_models: bool = False,
    select: Optional[str, List[str]] = None,
    ignore: Optional[str, List[str]] = None
) → List
```

Get the pytest parameters to use for testing neural network models.

**Args:**

* `regressor` (bool): If regressors should be selected.
* `classifier` (bool): If classifiers should be selected.
* `unique_models` (bool): If each models should be represented only once.
* `select` (Optional\[Union\[str, List\[str]]]): If not None, only return models which names (or a part of it) match the given string or list of strings. Default to None.
* `ignore` (Optional\[Union\[str, List\[str]]]): If not None, only return models which names (or a part of it) do not match the given string or list of strings. Default to None.

**Returns:**

* `List`: The pytest parameters to use for testing neural network models.

***

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

### function `get_sklearn_neighbors_models_and_datasets`

```python
get_sklearn_neighbors_models_and_datasets(
    regressor: bool = True,
    classifier: bool = True,
    unique_models: bool = False,
    select: Optional[str, List[str]] = None,
    ignore: Optional[str, List[str]] = None
) → List
```

Get the pytest parameters to use for testing neighbor models.

**Args:**

* `regressor` (bool): If regressors should be selected.
* `classifier` (bool): If classifiers should be selected.
* `unique_models` (bool): If each models should be represented only once.
* `select` (Optional\[Union\[str, List\[str]]]): If not None, only return models which names (or a part of it) match the given string or list of strings. Default to None.
* `ignore` (Optional\[Union\[str, List\[str]]]): If not None, only return models which names (or a part of it) do not match the given string or list of strings. Default to None.

**Returns:**

* `List`: The pytest parameters to use for testing neighbor models.

***

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

### function `get_sklearn_all_models_and_datasets`

```python
get_sklearn_all_models_and_datasets(
    regressor: bool = True,
    classifier: bool = True,
    unique_models: bool = False,
    select: Optional[str, List[str]] = None,
    ignore: Optional[str, List[str]] = None
) → List
```

Get the pytest parameters to use for testing all models available in Concrete ML.

**Args:**

* `regressor` (bool): If regressors should be selected.
* `classifier` (bool): If classifiers should be selected.
* `unique_models` (bool): If each models should be represented only once.
* `select` (Optional\[Union\[str, List\[str]]]): If not None, only return models which names (or a part of it) match the given string or list of strings. Default to None.
* `ignore` (Optional\[Union\[str, List\[str]]]): If not None, only return models which names (or a part of it) do not match the given string or list of strings. Default to None.

**Returns:**

* `List`: The pytest parameters to use for testing all models available in Concrete ML.

***

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

### function `instantiate_model_generic`

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

Instantiate any Concrete ML model type.

**Args:**

* `model_class` (class): The type of the model to instantiate.
* `n_bits` (int): The number of quantization to use when initializing the model. For QNNs, default parameters are used based on whether `n_bits` is greater or smaller than 8.
* `parameters` (dict): Hyper-parameters for the model instantiation. For QNNs, these parameters will override the matching default ones.

**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.2.x/src/concrete/ml/pytest/utils.py#L423)

### 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.2.x/src/concrete/ml/pytest/utils.py#L484)

### 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.2.x/src/concrete/ml/pytest/utils.py#L514)

### 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.2.x/src/concrete/ml/pytest/utils.py#L558)

### 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.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.
