import collections


def _Sanitize(data):
  """Converts data to a format acceptable by scipy.io.savemat.

  The scipy.io.savemat function cannot handle Booleans, NoneTypes, or
  unicode strings.

  Args:
    data: Dictionary returned by json.load.

  Returns:
    Sanitized dictionary that is compatible with scipy.io.savemat.
  """
  if isinstance(data, collections.OrderedDict):
    return collections.OrderedDict([(str(k), _Sanitize(v))
                                    for k, v in data.items()])
  if isinstance(data, dict):
    return {str(k): _Sanitize(v) for k, v in data.items()}
  elif isinstance(data, list):
    return [_Sanitize(x) for x in data]
  elif data is None:
    return []
  elif isinstance(data, bool):
    return 1 if data else 0
  else:
    return data