Scheduled service maintenance on November 22


On Friday, November 22, 2024, between 06:00 CET and 18:00 CET, GIN services will undergo planned maintenance. Extended service interruptions should be expected. We will try to keep downtimes to a minimum, but recommend that users avoid critical tasks, large data uploads, or DOI requests during this time.

We apologize for any inconvenience.

tools.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. # -*- coding: utf-8 -*-
  2. """
  3. Common tools that are useful for neo.io object tests
  4. """
  5. # needed for python 3 compatibility
  6. from __future__ import absolute_import
  7. import logging
  8. import os
  9. import shutil
  10. import tempfile
  11. from neo.core import Block, Segment
  12. from neo.test.generate_datasets import generate_from_supported_objects
  13. def close_object_safe(obj):
  14. """
  15. Close an object safely, ignoring errors
  16. For some io types, like HDF5IO, the file should be closed before being
  17. opened again in a test. Call this after the test is done to make sure
  18. the file is closed.
  19. """
  20. try:
  21. obj.close()
  22. except:
  23. pass
  24. def cleanup_test_file(mode, path, directory=None):
  25. """
  26. Remove test files or directories safely. mode is the mode of the io class,
  27. either 'file' or 'directory'. It can also be an io class object, or any
  28. other object with a 'mode' attribute. If that is the case, use the
  29. 'mode' attribute from the object.
  30. If directory is not None and path is not an absolute path already,
  31. use the file from the given directory.
  32. """
  33. if directory is not None and not os.path.isabs(path):
  34. path = os.path.join(directory, path)
  35. if hasattr(mode, 'mode'):
  36. mode = mode.mode
  37. if mode == 'file':
  38. if os.path.exists(path):
  39. os.remove(path)
  40. elif mode == 'dir':
  41. if os.path.exists(path):
  42. shutil.rmtree(path)
  43. def get_test_file_full_path(ioclass, filename=None,
  44. directory=None, clean=False):
  45. """
  46. Get the full path for a file of the given filename.
  47. If filename is None, create a filename.
  48. If filename is a list, get the full path for each item in the list.
  49. If return_path is True, also return the full path to the file.
  50. If directory is not None and path is not an absolute path already,
  51. use the file from the given directory.
  52. If return_path is True, return the full path of the file along with
  53. the io object. return reader, path. Default is False.
  54. If clean is True, try to delete existing versions of the file
  55. before creating the io object. Default is False.
  56. """
  57. # create a filename if none is provided
  58. if filename is None:
  59. filename = 'Generated0_%s' % ioclass.__name__
  60. if (ioclass.mode == 'file' and len(ioclass.extensions) >= 1):
  61. filename += '.' + ioclass.extensions[0]
  62. elif not hasattr(filename, 'lower'):
  63. return [get_test_file_full_path(ioclass, filename=fname,
  64. directory=directory, clean=clean) for
  65. fname in filename]
  66. # if a directory is provided add it
  67. if directory is not None and not os.path.isabs(filename):
  68. filename = os.path.join(directory, filename)
  69. if clean:
  70. cleanup_test_file(ioclass, filename)
  71. return filename
  72. # prevent this being treated as a test if imported into a test file
  73. get_test_file_full_path.__test__ = False
  74. def create_generic_io_object(ioclass, filename=None, directory=None,
  75. return_path=False, clean=False):
  76. """
  77. Create an io object in a generic way that can work with both
  78. file-based and directory-based io objects
  79. If filename is None, create a filename.
  80. If return_path is True, also return the full path to the file.
  81. If directory is not None and path is not an absolute path already,
  82. use the file from the given directory.
  83. If return_path is True, return the full path of the file along with
  84. the io object. return reader, path. Default is False.
  85. If clean is True, try to delete existing versions of the file
  86. before creating the io object. Default is False.
  87. """
  88. filename = get_test_file_full_path(ioclass, filename=filename,
  89. directory=directory, clean=clean)
  90. try:
  91. # actually create the object
  92. if ioclass.mode == 'file':
  93. ioobj = ioclass(filename=filename)
  94. elif ioclass.mode == 'dir':
  95. ioobj = ioclass(dirname=filename)
  96. else:
  97. ioobj = None
  98. except:
  99. print(filename)
  100. raise
  101. # return the full path if requested, otherwise don't
  102. if return_path:
  103. return ioobj, filename
  104. return ioobj
  105. def iter_generic_io_objects(ioclass, filenames, directory=None,
  106. return_path=False, clean=False):
  107. """
  108. Return an iterable over the io objects created from a list of filenames.
  109. The objects are automatically cleaned up afterwards.
  110. If directory is not None and path is not an absolute path already,
  111. use the file from the given directory.
  112. If return_path is True, yield the full path of the file along with
  113. the io object. yield reader, path. Default is False.
  114. If clean is True, try to delete existing versions of the file
  115. before creating the io object. Default is False.
  116. """
  117. for filename in filenames:
  118. ioobj, path = create_generic_io_object(ioclass, filename=filename,
  119. directory=directory,
  120. return_path=True,
  121. clean=clean)
  122. if ioobj is None:
  123. continue
  124. if return_path:
  125. yield ioobj, path
  126. else:
  127. yield ioobj
  128. close_object_safe(ioobj)
  129. def create_generic_reader(ioobj, target=None, readall=False):
  130. """
  131. Create a function that can read the target object from a file.
  132. If target is None, use the first supported_objects from ioobj
  133. If target is False, use the 'read' method.
  134. If target is the Block or Segment class, use read_block or read_segment,
  135. respectively.
  136. If target is a string, use 'read_'+target.
  137. If readall is True, use the read_all_ method instead of the read_ method.
  138. Default is False.
  139. """
  140. if target is None:
  141. target = ioobj.supported_objects[0].__name__
  142. if target == Block:
  143. if readall:
  144. return ioobj.read_all_blocks
  145. return ioobj.read_block
  146. elif target == Segment:
  147. if readall:
  148. return ioobj.read_all_segments
  149. return ioobj.read_segment
  150. elif not target:
  151. if readall:
  152. raise ValueError('readall cannot be True if target is False')
  153. return ioobj.read
  154. elif hasattr(target, 'lower'):
  155. if readall:
  156. return getattr(ioobj, 'read_all_%ss' % target.lower())
  157. return getattr(ioobj, 'read_%s' % target.lower())
  158. def iter_generic_readers(ioclass, filenames, directory=None, target=None,
  159. return_path=False, return_ioobj=False,
  160. clean=False, readall=False):
  161. """
  162. Iterate over functions that can read the target object from a list of
  163. filenames.
  164. If target is None, use the first supported_objects from ioobj
  165. If target is False, use the 'read' method.
  166. If target is the Block or Segment class, use read_block or read_segment,
  167. respectively.
  168. If target is a string, use 'read_'+target.
  169. If directory is not None and path is not an absolute path already,
  170. use the file from the given directory.
  171. If return_path is True, return the full path of the file along with
  172. the reader object. return reader, path.
  173. If return_ioobj is True, return the io object as well as the reader.
  174. return reader, ioobj. Default is False.
  175. If both return_path and return_ioobj is True,
  176. return reader, path, ioobj. Default is False.
  177. If clean is True, try to delete existing versions of the file
  178. before creating the io object. Default is False.
  179. If readall is True, use the read_all_ method instead of the read_ method.
  180. Default is False.
  181. """
  182. for ioobj, path in iter_generic_io_objects(ioclass=ioclass,
  183. filenames=filenames,
  184. directory=directory,
  185. return_path=True,
  186. clean=clean):
  187. res = create_generic_reader(ioobj, target=target, readall=readall)
  188. if not return_path and not return_ioobj:
  189. yield res
  190. else:
  191. res = (res,)
  192. if return_path:
  193. res = res + (path,)
  194. if return_ioobj:
  195. res = res + (ioobj,)
  196. yield res
  197. def create_generic_writer(ioobj, target=None):
  198. """
  199. Create a function that can write the target object to a file using the
  200. neo io object ioobj.
  201. If target is None, use the first supported_objects from ioobj
  202. If target is False, use the 'write' method.
  203. If target is the Block or Segment class, use write_block or write_segment,
  204. respectively.
  205. If target is a string, use 'write_'+target.
  206. """
  207. if target is None:
  208. target = ioobj.supported_objects[0].__name__
  209. if target == Block:
  210. return ioobj.write_block
  211. elif target == Segment:
  212. return ioobj.write_segment
  213. elif not target:
  214. return ioobj.write
  215. elif hasattr(target, 'lower'):
  216. return getattr(ioobj, 'write_' + target.lower())
  217. def read_generic(ioobj, target=None, lazy=False, readall=False,
  218. return_reader=False):
  219. """
  220. Read the target object from a file using the given neo io object ioobj.
  221. If target is None, use the first supported_objects from ioobj
  222. If target is False, use the 'write' method.
  223. If target is the Block or Segment class, use write_block or write_segment,
  224. respectively.
  225. If target is a string, use 'write_'+target.
  226. The lazy parameters is passed to the reader. Defaults is True.
  227. If readall is True, use the read_all_ method instead of the read_ method.
  228. Default is False.
  229. If return_reader is True, yield the io reader function as well as the
  230. object. yield obj, reader. Default is False.
  231. """
  232. obj_reader = create_generic_reader(ioobj, target=target, readall=readall)
  233. obj = obj_reader(lazy=lazy)
  234. if return_reader:
  235. return obj, obj_reader
  236. return obj
  237. def iter_read_objects(ioclass, filenames, directory=None, target=None,
  238. return_path=False, return_ioobj=False,
  239. return_reader=False, clean=False, readall=False,
  240. lazy=False):
  241. """
  242. Iterate over objects read from a list of filenames.
  243. If target is None, use the first supported_objects from ioobj
  244. If target is False, use the 'read' method.
  245. If target is the Block or Segment class, use read_block or read_segment,
  246. respectively.
  247. If target is a string, use 'read_'+target.
  248. If directory is not None and path is not an absolute path already,
  249. use the file from the given directory.
  250. If return_path is True, yield the full path of the file along with
  251. the object. yield obj, path.
  252. If return_ioobj is True, yield the io object as well as the object.
  253. yield obj, ioobj. Default is False.
  254. If return_reader is True, yield the io reader function as well as the
  255. object. yield obj, reader. Default is False.
  256. If some combination of return_path, return_ioobj, and return_reader
  257. is True, they are yielded in the order: obj, path, ioobj, reader.
  258. If clean is True, try to delete existing versions of the file
  259. before creating the io object. Default is False.
  260. The lazy parameter is passed to the reader. Defaults is True.
  261. If readall is True, use the read_all_ method instead of the read_ method.
  262. Default is False.
  263. """
  264. for obj_reader, path, ioobj in iter_generic_readers(ioclass, filenames,
  265. directory=directory,
  266. target=target,
  267. return_path=True,
  268. return_ioobj=True,
  269. clean=clean,
  270. readall=readall):
  271. obj = obj_reader(lazy=lazy)
  272. if not return_path and not return_ioobj and not return_reader:
  273. yield obj
  274. else:
  275. obj = (obj,)
  276. if return_path:
  277. obj = obj + (path,)
  278. if return_ioobj:
  279. obj = obj + (ioobj,)
  280. if return_reader:
  281. obj = obj + (obj_reader,)
  282. yield obj
  283. def write_generic(ioobj, target=None, obj=None, return_writer=False):
  284. """
  285. Write the target object to a file using the given neo io object ioobj.
  286. If target is None, use the first supported_objects from ioobj
  287. If target is False, use the 'write' method.
  288. If target is the Block or Segment class, use write_block or write_segment,
  289. respectively.
  290. If target is a string, use 'write_'+target.
  291. obj is the object to write. If obj is None, an object is created
  292. automatically for the io class.
  293. If return_writer is True, yield the io writer function as well as the
  294. object. yield obj, writer. Default is False.
  295. """
  296. if obj is None:
  297. supported_objects = ioobj.supported_objects
  298. obj = generate_from_supported_objects(supported_objects)
  299. obj_writer = create_generic_writer(ioobj, target=target)
  300. obj_writer(obj)
  301. if return_writer:
  302. return obj, obj_writer
  303. return obj