code: def test_make_normalized_discounted_cumulative_gain_fn(self):
      with tf.Graph().as_default():
          scores = [[1.0, 3.0, 2.0], [1.0, 2.0, 3.0]]
          # Note that scores are ranked in descending order.
          ranks = [[3, 1, 2], [3, 2, 1]]
          labels = [[0.0, 0.0, 1.0], [0.0, 1.0, 2.0]]
          weights = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]]
          weights_3d = [[[1.0], [2.0], [3.0]], [[4.0], [5.0], [6.0]]]
          list_weights = [1.0, 0.0]
          list_weights_2d = [[1.0], [0.0]]
          weights_feature_name = "weights"
          weights_invalid_feature_name = "weights_invalid"
          weights_3d_feature_name = "weights_3d"
          list_weights_name = "list_weights"
          list_weights_2d_name = "list_weights_2d"
          features = {
              weights_feature_name: [weights[0]],
              weights_invalid_feature_name: weights[0],
              weights_3d_feature_name: [weights_3d[0]],
              list_weights_name: list_weights,
              list_weights_2d_name: list_weights_2d,
          }
          m = metrics_lib.make_ranking_metric_fn(
              metrics_lib.RankingMetricKey.NDCG
          )
  
          expected_ndcg = (
              _dcg(0.0, 1) + _dcg(1.0, 2) + _dcg(0.0, 3)
          ) / (_dcg(1.0, 1) + _dcg(0.0, 2) + _dcg(0.0, 3))
          self._check_metrics(
              [
                  (
                      m([labels[0]], [scores[0]], features),
                      expected_ndcg,
                  ),
              ]
          )
          expected_ndcg_1 = (
              _dcg(0.0, 1) + _dcg(1.0, 2) + _dcg(0.0, 3)
          ) / (_dcg(1.0, 1) + _dcg(0.0, 2) + _dcg(0.0, 3))
          expected_ndcg_2 = 1.0
          expected_ndcg = (expected_ndcg_1 + expected_ndcg_2) / 2.0
          self._check_metrics(
              [
                  (m(labels, scores, features), expected_ndcg),
              ]
          )
  
          # With item-wise weights.
          m_top = metrics_lib.make_ranking_metric_fn(
              metrics_lib.RankingMetricKey.NDCG,
              weights_feature_name=weights_feature_name,
              topn=1,
          )
          m_weight = metrics_lib.make_ranking_metric_fn(
              metrics_lib.RankingMetricKey.NDCG,
              weights_feature_name=weights_feature_name,
          )
          m_weights_3d = metrics_lib.make_ranking_metric_fn(
              metrics_lib.RankingMetricKey.NDCG,
              weights_feature_name=weights_3d_feature_name,
          )
          self._check_metrics(
              [
                  (
                      m_top([labels[0]], [scores[0]], features),
                      _dcg(0.0, 1, 2.0) / _dcg(1.0, 1, 3.0),
                  ),
                  (
                      m_weight([labels[0]], [scores[0]], features),
                      (
                          _dcg(0.0, 1, 2.0)
                          + _dcg(1.0, 2, 3.0)
                          + _dcg(0.0, 3, 1.0)
                      )
                      / (
                          _dcg(1.0, 1, 3.0)
                          + _dcg(0.0, 2, 1.0)
                          + _dcg(0.0, 3, 2.0)
                      ),
                  ),
                  (
                      m_weights_3d([labels[0]], [scores[0]], features),
                      (
                          _dcg(0.0, 1, 2.0)
                          + _dcg(1.0, 2, 3.0)
                          + _dcg(0.0, 3, 1.0)
                      )
                      / (
                          _dcg(1.0, 1, 3.0)
                          + _dcg(0.0, 2, 1.0)
                          + _dcg(0.0, 3, 2.0)
                      ),
                  ),
              ]
          )
          with self.assertRaises(ValueError):
              m_weight_invalid = metrics_lib.make_ranking_metric_fn(
                  metrics_lib.RankingMetricKey.NDCG,
                  weights_feature_name=weights_invalid_feature_name,
              )
              m_weight_invalid([labels[0]], [scores[0]], features)
  
          # With list-wise weights.
          m_list_weight = metrics_lib.make_ranking_metric_fn(
              metrics_lib.RankingMetricKey.NDCG,
              weights_feature_name=list_weights_name,
          )
          m_list_weight_2d = metrics_lib.make_ranking_metric_fn(
              metrics_lib.RankingMetricKey.NDCG,
              weights_feature_name=list_weights_2d_name,
          )
          self._check_metrics(
              [
                  (
                      m_list_weight(labels, scores, features),
                      (
                          _dcg(0.0, 1, 2.0)
                          + _dcg(1.0, 2, 3.0)
                          + _dcg(0.0, 3, 1.0)
                      )
                      / (
                          _dcg(1.0, 1, 3.0)
                          + _dcg(0.0, 2, 1.0)
                          + _dcg(0.0, 3, 2.0)
                      ),
                  ),
                  (
                      m_list_weight_2d(labels, scores, features),
                      (
                          _dcg(0.0, 1, 2.0)
                          + _dcg(1.0, 2, 3.0)
                          + _dcg(0.0, 3, 1.0)
                      )
                      / (
                          _dcg(1.0, 1, 3.0)
                          + _dcg(0.0, 2, 1.0)
                          + _dcg(0.0, 3, 2.0)
                      ),
                  ),
              ]
          )
  
          # Test different gain and discount functions.
          gain_fn = lambda rel: rel
          rank_discount_fn = lambda rank: 1.0 / rank
  
          def mod_dcg_fn(l, r):
              return _dcg(
                  l,
                  r,
                  gain_fn=gain_fn,
                  rank_discount_fn=rank_discount_fn,
              )
  
          m_mod = metrics_lib.make_ranking_metric_fn(
              metrics_lib.RankingMetricKey.NDCG,
              gain_fn=gain_fn,
              rank_discount_fn=rank_discount_fn,
          )
          list_size = len(scores[0])
          expected_modified_dcg_1 = sum(
              [
                  mod_dcg_fn(labels[0][ind], ranks[0][ind])
                  for ind in range(list_size)
              ]
          )
          self._check_metrics(
              [
                  (
                      m_mod([labels[0]], [scores[0]], features),
                      expected_modified_dcg_1,
                  ),
              ]
          )
  
imports: from __future__ import absolute_import
  from __future__ import division
  from __future__ import print_function
  import functools
  import math
  import tensorflow
  from tensorflow_ranking.python import metrics
label_desc: Use image node_id 11 for calling the MetricsTest obj's underlying member method code with example usage: obj.test_make_normalized_discounted_cumulative_gain_fn() and returns: _dcg