docs: def obj_to_file(obj, filename, filetype='auto', ndarray_to_list=False, squeeze=True):
      '''Writes annotation in file.
  
      :param filetype:
          auto
          yaml
          pkl, pickle
          pklz, picklezip
      :param ndarray_to_list: convert ndarrays in obj to lists
      :param squeeze: squeeze ndarray
  
      '''
      # import json
      # with open(filename, mode='w') as f:
      #    json.dump(annotation,f)
      if ndarray_to_list:
          obj = ndarray_to_list_in_structure(obj, squeeze=squeeze)
  
      # write to yaml
      d = os.path.dirname(os.path.abspath(filename))
      if not os.path.exists(d):
          os.makedirs(d)
  
      if filetype == 'auto':
          _, ext = os.path.splitext(filename)
          filetype = ext[1:]
  
      if filetype in ('yaml', 'yml'):
          # import yaml
          from ruamel.yaml import YAML
          yaml = YAML(typ="unsafe")
          with open(filename, 'wt', encoding="utf-8") as f:
              yaml.dump(obj, f)
          # if sys.version_info.major == 2:
          #     with open(filename, 'wb') as f:
          #         yaml.dump(obj, f, encoding="utf-8")
          # else:
          #     with open(filename, "w", encoding="utf-8") as f:
          #         yaml.dump(obj, f)
      elif filetype in ('pickle', 'pkl'):
          f = open(filename, 'wb')
          logger.info("filename " + filename)
          # if sys.version_info[0] < 3: import cPickle as pickle
          # else: import _pickle as pickle
          import pickle
          pickle.dump(obj, f, -1)
          f.close
      elif filetype in ('streamingpicklezip', 'spklz'):
          # this is not working :-(
          import gzip
          import sPickle as pickle
          f = gzip.open(filename, 'wb', compresslevel=1)
          # f = open(filename, 'wb')
          pickle.s_dump(obj, f)
          f.close
      elif filetype in ('picklezip', 'pklz'):
          import gzip
          if sys.version_info[0] < 3: import cPickle as pickle
          else: import _pickle as pickle
          f = gzip.open(filename, 'wb', compresslevel=1)
          # f = open(filename, 'wb')
          pickle.dump(obj, f)
          f.close
      elif filetype in('mat'):
  
          import scipy.io as sio
          sio.savemat(filename, obj)
      else:
          logger.error('Unknown filetype ' + filetype)