asciiimageio.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from .baseio import BaseIO
  2. from neo.core import ImageSequence, Segment, Block
  3. import numpy as np
  4. class AsciiImageIO(BaseIO):
  5. """
  6. IO class for reading ImageSequence in a text file
  7. *Usage*:
  8. >>> from neo import io
  9. >>> import quantities as pq
  10. >>> r = io.AsciiImageIO(file_name='File_asciiimage_1.txt',nb_frame=511, nb_row=100,
  11. ... nb_column=100,units='mm', sampling_rate=1.0*pq.Hz,
  12. ... spatial_scale=1.0*pq.mm)
  13. >>> block = r.read_block()
  14. read block
  15. creating segment
  16. returning block
  17. >>> block
  18. Block with 1 segments
  19. file_origin: 'File_asciiimage_1.txt
  20. # segments (N=1)
  21. 0: Segment with 1 imagesequences # analogsignals (N=0)
  22. """
  23. name = 'AsciiImage IO'
  24. description = "Neo IO module for optical imaging data stored as a folder of TIFF images."
  25. _prefered_signal_group_mode = 'group-by-same-units'
  26. is_readable = True
  27. is_writable = False
  28. supported_objects = [Block, Segment, ImageSequence]
  29. readable_objects = supported_objects
  30. writeable_object = []
  31. support_lazy = False
  32. read_params = {}
  33. write_params = {}
  34. extensions = []
  35. mode = 'file'
  36. def __init__(self, file_name=None, nb_frame=None, nb_row=None, nb_column=None, units=None, sampling_rate=None,
  37. spatial_scale=None, **kwargs):
  38. BaseIO.__init__(self, file_name, **kwargs)
  39. self.nb_frame = nb_frame
  40. self.nb_row = nb_row
  41. self.nb_column = nb_column
  42. self.units = units
  43. self.sampling_rate = sampling_rate
  44. self.spatial_scale = spatial_scale
  45. def read_block(self, lazy=False, **kwargs):
  46. file = open(self.filename, 'r')
  47. data = file.read()
  48. print("read block")
  49. liste_value = []
  50. record = []
  51. for i in range(len(data)):
  52. if data[i] == "\n" or data[i] == "\t":
  53. t = "".join(str(e) for e in record)
  54. liste_value.append(t)
  55. record = []
  56. else:
  57. record.append(data[i])
  58. data = []
  59. nb = 0
  60. for i in range(self.nb_frame):
  61. data.append([])
  62. for y in range(self.nb_row):
  63. data[i].append([])
  64. for x in range(self.nb_column):
  65. data[i][y].append(liste_value[nb])
  66. nb += 1
  67. image_sequence = ImageSequence(np.array(data, dtype='float'), units=self.units,
  68. sampling_rate=self.sampling_rate, spatial_scale=self.spatial_scale)
  69. file.close()
  70. print("creating segment")
  71. segment = Segment(file_origin=self.filename)
  72. segment.imagesequences = [image_sequence]
  73. block = Block(file_origin=self.filename)
  74. segment.block = block
  75. block.segments.append(segment)
  76. print("returning block")
  77. return block