concrete.ml.pytest.utils
Common functions or lists for test files, which can't be put in fixtures.
MODELS_AND_DATASETS
UNIQUE_MODELS_AND_DATASETS
get_sklearn_linear_models_and_datasets
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.
get_sklearn_tree_models_and_datasets
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.
get_sklearn_neural_net_models_and_datasets
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.
get_sklearn_neighbors_models_and_datasets
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.
get_sklearn_all_models_and_datasets
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.
instantiate_model_generic
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.
data_calibration_processing
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.
load_torch_model
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.
values_are_equal
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.
check_serialization
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.
get_random_samples
Select n_sample
random elements from a 2D NumPy array.
Args:
x
(numpy.ndarray): The 2D NumPy array from which random rows will be selected.
n_sample
(int): The number of rows to randomly select.
Returns:
numpy.ndarray
: A new 2D NumPy array containing the randomly selected rows.
Raises:
AssertionError
: If n_sample
is not within the range (0, x.shape[0]) or if x
is not a 2D array.