code: def chi_square_calc(classes, table, TOP, P, POP):
      """
      Calculate Chi-squared.
  
      :param classes: confusion matrix classes
      :type classes: list
      :param table: input confusion matrix
      :type table: dict
      :param TOP: number of positives in predict vector per class
      :type TOP: dict
      :param P: number of actual positives per class
      :type P: dict
      :param POP: population or total number of samples per class
      :type POP: dict
      :return: chi-squared as float
      """
      try:
          result = 0
          for i in classes:
              for j in classes:
                  expected = (TOP[j] * P[i]) / (POP[i])
                  result += ((table[i][j] - expected) ** 2) / expected
          return result
      except Exception:
          return "None"
  
imports: from __future__ import division
  import math
  import operator
  from functools import reduce
  from .pycm_interpret import *
  from .pycm_ci import kappa_SE_calc, CI_calc, SE_calc
  from .pycm_util import complement
label_desc: Use image node_id 31 for calling a global function with example usage: chi_square_calc(classes, table, TOP, P, POP) and returns: result, str