nodes: 0, module; 1, function_definition; 2, function_name:write_to_csv; 3, parameters; 4, block; 5, identifier:self; 6, default_parameter; 7, default_parameter; 8, default_parameter; 9, default_parameter; 10, default_parameter; 11, dictionary_splat_pattern; 12, expression_statement; 13, import_statement; 14, if_statement; 15, expression_statement; 16, comment:# check files before starting to write; 17, import_statement; 18, for_statement; 19, expression_statement; 20, import_from_statement; 21, expression_statement; 22, expression_statement; 23, expression_statement; 24, with_statement; 25, if_statement; 26, identifier:filename; 27, None; 28, identifier:extension; 29, string; 30, identifier:overwrite; 31, False; 32, identifier:stride; 33, integer:1; 34, identifier:chunksize; 35, None; 36, identifier:kw; 37, comment:""" write all data to csv with numpy.savetxt
  
          Parameters
          ----------
          filename : str, optional
              filename string, which may contain placeholders {itraj} and {stride}:
  
              * itraj will be replaced by trajetory index
              * stride is stride argument of this method
  
              If filename is not given, it is being tried to obtain the filenames
              from the data source of this iterator.
          extension : str, optional, default='.dat'
              filename extension of created files
          overwrite : bool, optional, default=False
              shall existing files be overwritten? If a file exists, this method will raise.
          stride : int
              omit every n'th frame
          chunksize: int, default=None
              how many frames to process at once
          kw : dict, optional
              named arguments passed into numpy.savetxt (header, seperator etc.)
  
          Example
          -------
          Assume you want to save features calculated by some FeatureReader to ASCII:
  
          >>> import numpy as np, pyemma
          >>> import os
          >>> from pyemma.util.files import TemporaryDirectory
          >>> from pyemma.util.contexts import settings
          >>> data = [np.random.random((10,3))] * 3
          >>> reader = pyemma.coordinates.source(data)
          >>> filename = "distances_{itraj}.dat"
          >>> with TemporaryDirectory() as td, settings(show_progress_bars=False):
          ...    out = os.path.join(td, filename)
          ...    reader.write_to_csv(out, header='', delimiter=';')
          ...    print(sorted(os.listdir(td)))
          ['distances_0.dat', 'distances_1.dat', 'distances_2.dat']
          """; 38, dotted_name; 39, not_operator; 40, block; 41, elif_clause; 42, else_clause; 43, call; 44, dotted_name; 45, identifier:f; 46, identifier:filenames; 47, block; 48, assignment; 49, dotted_name; 50, dotted_name; 51, assignment; 52, assignment; 53, call; 54, with_clause; 55, block; 56, comparison_operator:f is not None; 57, block; 58, string_content:.dat; 59, identifier:os; 60, identifier:filename; 61, assert_statement; 62, comment:#    raise RuntimeError("could not determine filenames"); 63, expression_statement; 64, for_statement; 65, call; 66, block; 67, block; 68, attribute; 69, argument_list; 70, identifier:errno; 71, try_statement; 72, identifier:f; 73, None; 74, identifier:pyemma; 75, identifier:_base; 76, identifier:progress; 77, identifier:ProgressReporter; 78, identifier:pg; 79, call; 80, identifier:it; 81, call; 82, attribute; 83, argument_list; 84, with_item; 85, with_item; 86, expression_statement; 87, for_statement; 88, identifier:f; 89, None; 90, expression_statement; 91, call; 92, assignment; 93, identifier:f; 94, attribute; 95, block; 96, identifier:isinstance; 97, argument_list; 98, expression_statement; 99, expression_statement; 100, raise_statement; 101, attribute; 102, identifier:debug; 103, binary_operator:"write_to_csv, filenames=%s" % filenames; 104, block; 105, except_clause; 106, identifier:ProgressReporter; 107, argument_list; 108, attribute; 109, argument_list; 110, identifier:pg; 111, identifier:register; 112, attribute; 113, string:"saving to csv"; 114, identifier:it; 115, call; 116, assignment; 117, identifier:X; 118, identifier:it; 119, block; 120, call; 121, identifier:hasattr; 122, argument_list; 123, identifier:filenames; 124, list; 125, identifier:self; 126, identifier:filenames; 127, expression_statement; 128, expression_statement; 129, identifier:filename; 130, identifier:str; 131, assignment; 132, assignment; 133, call; 134, identifier:self; 135, identifier:logger; 136, string:"write_to_csv, filenames=%s"; 137, identifier:filenames; 138, expression_statement; 139, raise_statement; 140, as_pattern; 141, block; 142, identifier:self; 143, identifier:iterator; 144, identifier:stride; 145, keyword_argument; 146, keyword_argument; 147, identifier:it; 148, identifier:n_chunks; 149, attribute; 150, argument_list; 151, identifier:oldtraj; 152, unary_operator; 153, if_statement; 154, expression_statement; 155, expression_statement; 156, expression_statement; 157, attribute; 158, argument_list; 159, identifier:self; 160, string; 161, assignment; 162, call; 163, identifier:filename; 164, call; 165, identifier:filenames; 166, list_comprehension; 167, identifier:TypeError; 168, argument_list; 169, assignment; 170, call; 171, identifier:OSError; 172, as_pattern_target; 173, if_statement; 174, raise_statement; 175, identifier:chunk; 176, identifier:chunksize; 177, identifier:return_trajindex; 178, False; 179, identifier:pg; 180, identifier:context; 181, integer:1; 182, comparison_operator:oldtraj != it.current_trajindex; 183, block; 184, call; 185, call; 186, call; 187, identifier:f; 188, identifier:close; 189, string_content:filenames; 190, pattern_list; 191, call; 192, attribute; 193, argument_list; 194, attribute; 195, argument_list; 196, call; 197, for_in_clause; 198, string:"filename should be str or None"; 199, identifier:st; 200, call; 201, identifier:OSError; 202, argument_list; 203, identifier:e; 204, comparison_operator:e.errno == errno.EEXIST; 205, block; 206, elif_clause; 207, identifier:oldtraj; 208, attribute; 209, if_statement; 210, expression_statement; 211, expression_statement; 212, expression_statement; 213, expression_statement; 214, attribute; 215, argument_list; 216, attribute; 217, argument_list; 218, attribute; 219, argument_list; 220, identifier:base; 221, identifier:_; 222, attribute; 223, argument_list; 224, identifier:filenames; 225, identifier:append; 226, binary_operator:base + extension; 227, identifier:filename; 228, identifier:replace; 229, string; 230, call; 231, attribute; 232, argument_list; 233, identifier:itraj; 234, call; 235, attribute; 236, argument_list; 237, attribute; 238, attribute; 239, attribute; 240, if_statement; 241, comparison_operator:e.errno == errno.ENOENT; 242, block; 243, identifier:it; 244, identifier:current_trajindex; 245, comparison_operator:f is not None; 246, block; 247, assignment; 248, call; 249, assignment; 250, assignment; 251, identifier:np; 252, identifier:savetxt; 253, identifier:f; 254, identifier:X; 255, dictionary_splat; 256, identifier:f; 257, identifier:flush; 258, identifier:pg; 259, identifier:update; 260, integer:1; 261, integer:0; 262, attribute; 263, identifier:splitext; 264, identifier:f; 265, identifier:base; 266, identifier:extension; 267, string_content:{stride}; 268, identifier:str; 269, argument_list; 270, identifier:filename; 271, identifier:replace; 272, string; 273, call; 274, identifier:range; 275, argument_list; 276, identifier:os; 277, identifier:stat; 278, identifier:f; 279, identifier:errno; 280, identifier:EEXIST; 281, identifier:e; 282, identifier:errno; 283, identifier:errno; 284, identifier:EEXIST; 285, identifier:overwrite; 286, block; 287, attribute; 288, attribute; 289, continue_statement; 290, identifier:f; 291, None; 292, expression_statement; 293, identifier:fn; 294, subscript; 295, attribute; 296, argument_list; 297, identifier:f; 298, call; 299, identifier:oldtraj; 300, attribute; 301, identifier:kw; 302, identifier:os; 303, identifier:path; 304, identifier:stride; 305, string_content:{itraj}; 306, identifier:str; 307, argument_list; 308, call; 309, continue_statement; 310, identifier:e; 311, identifier:errno; 312, identifier:errno; 313, identifier:ENOENT; 314, call; 315, identifier:filenames; 316, attribute; 317, attribute; 318, identifier:debug; 319, binary_operator:"opening file %s for writing csv." % fn; 320, identifier:open; 321, argument_list; 322, identifier:it; 323, identifier:current_trajindex; 324, identifier:itraj; 325, attribute; 326, argument_list; 327, attribute; 328, argument_list; 329, identifier:it; 330, identifier:current_trajindex; 331, identifier:self; 332, identifier:logger; 333, string:"opening file %s for writing csv."; 334, identifier:fn; 335, identifier:fn; 336, string; 337, identifier:self; 338, identifier:number_of_trajectories; 339, identifier:f; 340, identifier:close; 341, string_content:wb
code: def write_to_csv(self, filename=None, extension='.dat', overwrite=False,
                       stride=1, chunksize=None, **kw):
          """ write all data to csv with numpy.savetxt
  
          Parameters
          ----------
          filename : str, optional
              filename string, which may contain placeholders {itraj} and {stride}:
  
              * itraj will be replaced by trajetory index
              * stride is stride argument of this method
  
              If filename is not given, it is being tried to obtain the filenames
              from the data source of this iterator.
          extension : str, optional, default='.dat'
              filename extension of created files
          overwrite : bool, optional, default=False
              shall existing files be overwritten? If a file exists, this method will raise.
          stride : int
              omit every n'th frame
          chunksize: int, default=None
              how many frames to process at once
          kw : dict, optional
              named arguments passed into numpy.savetxt (header, seperator etc.)
  
          Example
          -------
          Assume you want to save features calculated by some FeatureReader to ASCII:
  
          >>> import numpy as np, pyemma
          >>> import os
          >>> from pyemma.util.files import TemporaryDirectory
          >>> from pyemma.util.contexts import settings
          >>> data = [np.random.random((10,3))] * 3
          >>> reader = pyemma.coordinates.source(data)
          >>> filename = "distances_{itraj}.dat"
          >>> with TemporaryDirectory() as td, settings(show_progress_bars=False):
          ...    out = os.path.join(td, filename)
          ...    reader.write_to_csv(out, header='', delimiter=';')
          ...    print(sorted(os.listdir(td)))
          ['distances_0.dat', 'distances_1.dat', 'distances_2.dat']
          """
          import os
          if not filename:
              assert hasattr(self, 'filenames')
              #    raise RuntimeError("could not determine filenames")
              filenames = []
              for f in self.filenames:
                  base, _ = os.path.splitext(f)
                  filenames.append(base + extension)
          elif isinstance(filename, str):
              filename = filename.replace('{stride}', str(stride))
              filenames = [filename.replace('{itraj}', str(itraj)) for itraj
                           in range(self.number_of_trajectories())]
          else:
              raise TypeError("filename should be str or None")
          self.logger.debug("write_to_csv, filenames=%s" % filenames)
          # check files before starting to write
          import errno
          for f in filenames:
              try:
                  st = os.stat(f)
                  raise OSError(errno.EEXIST)
              except OSError as e:
                  if e.errno == errno.EEXIST:
                      if overwrite:
                          continue
                  elif e.errno == errno.ENOENT:
                      continue
                  raise
          f = None
          from pyemma._base.progress import ProgressReporter
          pg = ProgressReporter()
          it = self.iterator(stride, chunk=chunksize, return_trajindex=False)
          pg.register(it.n_chunks, "saving to csv")
          with it, pg.context():
              oldtraj = -1
              for X in it:
                  if oldtraj != it.current_trajindex:
                      if f is not None:
                          f.close()
                      fn = filenames[it.current_trajindex]
                      self.logger.debug("opening file %s for writing csv." % fn)
                      f = open(fn, 'wb')
                      oldtraj = it.current_trajindex
                  np.savetxt(f, X, **kw)
                  f.flush()
                  pg.update(1, 0)
          if f is not None:
              f.close()