code: def get_gender_precision_fairness_indicator(
      train_metadata,
      test_metadata,
      train_features,
      test_features,
      apply_l2_norm=False,
      k_values=None,
  ):
      k_values = k_values or [1, 5, 10, 50]
      if apply_l2_norm:
          print("Applying L2 norm...")
          from torch import nn
  
          train_features = torch.from_numpy(train_features).float()
          test_features = torch.from_numpy(test_features).float()
          train_features = nn.functional.normalize(
              train_features, dim=1, p=2
          )
          test_features = nn.functional.normalize(
              test_features, dim=1, p=2
          )
          train_features = train_features.numpy()
          test_features = test_features.numpy()
  
      similarity = test_features.dot(train_features.T)
      print(f"Similarity: {similarity.shape}")
  
      test_gt_gender_values = [
          item["gender"] for item in list(test_metadata.values())
      ]
      train_gender_values = [
          item["gender"] for item in list(train_metadata.values())
      ]
      print(
          f"test/train ground truth gender list: {len(test_gt_gender_values)}, train_gender_values: {len(train_gender_values)}"
      )
      print(
          f"unique gender values -> test: {set(test_gt_gender_values)}, train: {set(train_gender_values)}"
      )
  
      neighbor_ranks = np.argsort(-similarity, axis=1)
      num_test_images = neighbor_ranks.shape[0]
      # num_train_images = neighbor_ranks.shape[1]
      predicted_gender_matrix = []
      for row_num in range(num_test_images):
          neighbors_gender_values = [
              train_gender_values[idx]
              for idx in neighbor_ranks[row_num]
          ]
          predicted_gender_matrix.append(neighbors_gender_values)
      predicted_gender_matrix = np.vstack(predicted_gender_matrix)
      print(f"neighbors matrix: {predicted_gender_matrix.shape}")
  
      (
          output_gender_precision_fairness_indicator,
          output_gender_precision_attributes_count_map,
      ) = (
          {},
          {},
      )
      for k_val in k_values:
          key_str = f"precision@{k_val}"
          (
              output_gender_precision_fairness_indicator[key_str],
              output_gender_precision_attributes_count_map[key_str],
          ) = ({}, {})
  
          to_predict_attributes = list(
              list(test_metadata.values())[0].keys()
          )
          to_predict_attributes.remove("feature")
          preds_values = predicted_gender_matrix[:, :k_val]
  
          # compute overall precision@k per attribute
          (
              output_attributes_precision_map,
              output_attributes_count_map,
          ) = ({}, {})
  
          # compute the overall precision@k
          total_correct = 0
          for idx in range(num_test_images):
              total_correct += len(
                  np.where(
                      np.array(preds_values[idx])
                      == test_gt_gender_values[idx]
                  )[0]
              )
          output_attributes_precision_map["overall"] = total_correct / (
              k_val * num_test_images
          )
          for attribute in to_predict_attributes:
              attribute_values = [
                  item[attribute]
                  for item in list(test_metadata.values())
              ]
              attribute_correct_incorrect = {}
              for idx in range(num_test_images):
                  num_correct = len(
                      np.where(
                          np.array(preds_values[idx])
                          == test_gt_gender_values[idx]
                      )[0]
                  )
                  if (
                      attribute_values[idx]
                      in attribute_correct_incorrect
                  ):
                      attribute_correct_incorrect[
                          attribute_values[idx]
                      ]["freq"] = (
                          attribute_correct_incorrect[
                              attribute_values[idx]
                          ]["freq"]
                          + 1
                      )
                      attribute_correct_incorrect[
                          attribute_values[idx]
                      ]["num_correct"] = (
                          attribute_correct_incorrect[
                              attribute_values[idx]
                          ]["num_correct"]
                          + num_correct
                      )
                  else:
                      attribute_correct_incorrect[
                          attribute_values[idx]
                      ] = {}
                      attribute_correct_incorrect[
                          attribute_values[idx]
                      ]["freq"] = 1
                      attribute_correct_incorrect[
                          attribute_values[idx]
                      ]["num_correct"] = num_correct
              attribute_accuracy_map, attribute_count_map = {}, {}
              for key, val in attribute_correct_incorrect.items():
                  attribute_accuracy_map[key] = val["num_correct"] / (
                      val["freq"] * k_val
                  )
                  attribute_count_map[key] = val["freq"]
              output_attributes_precision_map[
                  attribute
              ] = attribute_accuracy_map
              output_attributes_count_map[
                  attribute
              ] = attribute_count_map
          output_gender_precision_fairness_indicator[
              key_str
          ] = output_attributes_precision_map
          output_gender_precision_attributes_count_map[
              key_str
          ] = output_attributes_count_map
      return (
          output_gender_precision_fairness_indicator,
          output_gender_precision_attributes_count_map,
      )
  
file_path: vissl/projects/fairness_indicators/similarity_search/inference_casual_conversations.py
imports: import numpy
  import pandas
  import torch
  from vissl.utils.io import load_file
label_desc: Use image node_id 7 for calling a global function with example usage: get_gender_precision_fairness_indicator(train_metadata, test_metadata, train_features, test_features, apply_l2_norm, k_values) and returns: output_gender_precision_fairness_indicator,  output_gender_precision_attributes_count_map