code: def volume_enforcement(
      prediction, ground_truth, weight_map=None, eps=0.001, hard=False
  ):
      """
      Computing a volume enforcement loss to ensure that the obtained volumes are
      close and avoid empty results when something is expected
      :param prediction:
      :param ground_truth: labels
      :param weight_map: potential weight map to apply
      :param eps: epsilon to use as regulariser
      :return:
      """
  
      prediction = tf.cast(prediction, tf.float32)
      if len(ground_truth.shape) == len(prediction.shape):
          ground_truth = ground_truth[..., -1]
      one_hot = labels_to_one_hot(
          ground_truth, tf.shape(prediction)[-1]
      )
  
      gt_red = tf.sparse_reduce_sum(one_hot, 0)
      pred_red = tf.reduce_sum(prediction, 0)
      if hard:
          pred_red = tf.sparse_reduce_sum(
              labels_to_one_hot(
                  tf.argmax(prediction, -1), tf.shape(prediction)[-1]
              ),
              0,
          )
  
      if weight_map is not None:
          n_classes = prediction.shape[1].value
          weight_map_nclasses = tf.tile(
              tf.expand_dims(tf.reshape(weight_map, [-1]), 1),
              [1, n_classes],
          )
          gt_red = tf.sparse_reduce_sum(
              weight_map_nclasses * one_hot, reduction_axes=[0]
          )
          pred_red = tf.reduce_sum(weight_map_nclasses * prediction, 0)
  
      return tf.reduce_mean(
          tf.sqrt(
              tf.square(
                  (gt_red + eps) / (pred_red + eps)
                  - (pred_red + eps) / (gt_red + eps)
              )
          )
      )
  
imports: from __future__ import absolute_import, print_function, division
  import numpy
  import tensorflow
  from niftynet.engine.application_factory import LossSegmentationFactory
  from niftynet.layer.base_layer import Layer
label_desc: Use image node_id 3 for calling a global function with example usage: volume_enforcement(prediction, ground_truth, weight_map, eps, hard) and returns: tf