group_utils

fairdiverse.recommendation.utils.group_utils.Build_Adjecent_Matrix(config)[source]

Builds an adjacency matrix based on the group-item mapping, initializing it with ones, and adjusting rows with no connections.

This function uses the Init_Group_AdjcentMatrix to retrieve a mapping of item IDs (iid) to product IDs (pid), constructs an adjacency matrix, and ensures that rows with no connections are assigned a default value.

Parameters:

config – A configuration dictionary containing dataset information and matrix dimensions.

Returns:

A tuple containing: - A 2D NumPy array representing the adjacency matrix. - A dictionary mapping item IDs (iid) to product IDs (pid).

fairdiverse.recommendation.utils.group_utils.Init_Group_AdjcentMatrix(dataset_name)[source]

Initializes the group adjacency matrix by loading the processed dataset corresponding to the given dataset name.

This function checks if the processed dataset exists for the provided dataset name, and if not, raises an error. It then loads the iid2pid.json file, converts the keys and values to integers, and returns the result.

Parameters:

dataset_name – The name of the dataset to load.

Returns:

A dictionary mapping item IDs (iid) to product IDs (pid).

fairdiverse.recommendation.utils.group_utils.convert_keys_values_to_int(data)[source]

Recursively converts dictionary keys and values, as well as string digits, into integers.

This function will process a dictionary or list and convert all keys and values to integers where applicable. If the data is a string that represents a digit, it will be converted to an integer.

Parameters:

data – The input data to be processed, which can be a dictionary, list, or string.

Returns:

The input data with keys and values converted to integers if applicable.

fairdiverse.recommendation.utils.group_utils.get_cos_similar_torch(v1, v2, device='cuda')[source]

Computes the cosine similarity between two vectors using PyTorch.

This function calculates the cosine similarity between two input vectors, v1 and v2, using the specified device (either cuda for GPU or cpu). The function utilizes PyTorch’s cosine_similarity function and returns the result as a NumPy array.

Parameters:
  • v1 – The first vector for cosine similarity calculation.

  • v2 – The second vector for cosine similarity calculation.

Returns:

The cosine similarity between v1 and v2.

fairdiverse.recommendation.utils.group_utils.get_iid2text(dataset_name)[source]

Loads and returns the mapping of item IDs (iid) to text descriptions from the processed dataset.

This function checks if the processed dataset exists for the given dataset name, and if not, raises an error. It then loads the iid2text.json file, converts the keys and values to integers, and returns the result.

Parameters:

dataset_name – The name of the dataset to load.

Returns:

A dictionary mapping item IDs (iid) to their corresponding text descriptions.

fairdiverse.recommendation.utils.group_utils.load_json(file_path)[source]

Loads a JSON file from the specified file path.

This function attempts to open and parse a JSON file. If successful, it returns the parsed data. If the file is not found, or the file content is not valid JSON, an error message is printed, and None is returned.

Parameters:

file_path – The path to the JSON file to be loaded.

Returns:

The parsed data from the JSON file if successful, otherwise None.