inFairness.utils.datautils module#
- inFairness.utils.datautils.convert_tensor_to_numpy(tensor)[source]#
Converts a PyTorch tensor to numpy array
If the provided tensor is not a PyTorch tensor, it returns the same object back with no modifications
- Parameters:
tensor (torch.Tensor) – Tensor to be converted to numpy array
- Returns:
array_np – Numpy array of the provided tensor
- Return type:
- inFairness.utils.datautils.generate_data_pairs(n_pairs, datasamples_1, datasamples_2=None, comparator=None)[source]#
Utility function to generate (in)comparable data pairs given data samples. Use case includes creating a dataset of comparable and incomparable data for the EXPLORE distance metric which learns from such data samples.
- Parameters:
n_pairs (int) – Number of pairs to construct
datasamples_1 (numpy.ndarray) – Array of data samples of shape (N_1, *)
datasamples_2 (numpy.ndarray) – (Optional) array of data samples of shape (N_2, *). If datasamples_2 is provided, then data pairs are constructed between datasamples_1 and datasamples_2. If datasamples_2 is not provided, then data pairs are constructed within datasamples_1
comparator (function) – A lambda function that given two samples returns True if they should be paired, and False if not. If comparator is not defined, then random data samples are paired together. Example: comparator = lambda x, y: (x == y)
- Returns:
idxs – A (n_pairs, 2) shaped array with indices of data sample pairs
- Return type:
- inFairness.utils.datautils.get_device(obj)[source]#
Returns a device (cpu/cuda) based on the type of the reference object
- Parameters:
obj (torch.Tensor) –
- inFairness.utils.datautils.include_exclude_terms(data_terms: Iterable[str], include: Iterable[str] = (), exclude: Iterable[str] = ())[source]#
given a set of data terms, return a resulting set depending on specified included and excluded terms.
- Parameters:
data_terms (string iterable) – set of terms to be filtered
include (string iterable) – set of terms to be included, if not specified all data_terms are included
exclude (string iterable) – set of terms to be excluded from data_terms
- Returns:
terms – resulting terms in alphabetical order.
- Return type:
list of strings