import itertools


def sparse_dict_from_array(array, magnitude_threshold=0):
  """Converts a array to a dict of nonzero-entries keyed by index-tuple."""
  ret = {}
  for index_tuple in itertools.product(*(map(range, array.shape))):
    v = array[index_tuple]
    if abs(v) > magnitude_threshold:
      ret[index_tuple] = v
  return ret