tiffio.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. """
  2. Neo IO module for optical imaging data stored as a folder of TIFF images.
  3. """
  4. import os
  5. try:
  6. from PIL import Image
  7. have_pil = True
  8. except ImportError:
  9. have_pil = False
  10. import numpy as np
  11. from neo.core import ImageSequence, Segment, Block
  12. from .baseio import BaseIO
  13. import glob
  14. import re
  15. class TiffIO(BaseIO):
  16. """
  17. Neo IO module for optical imaging data stored as a folder of TIFF images.
  18. *Usage*:
  19. >>> from neo import io
  20. >>> import quantities as pq
  21. >>> r = io.TiffIO("dir_tiff",spatial_scale=1.0*pq.mm, units='V',
  22. ... sampling_rate=1.0*pq.Hz)
  23. >>> block = r.read_block()
  24. read block
  25. creating segment
  26. returning block
  27. >>> block
  28. Block with 1 segments
  29. file_origin: 'test'
  30. # segments (N=1)
  31. 0: Segment with 1 imagesequences
  32. annotations: {'tiff_file_names': ['file_tif_1_.tiff',
  33. 'file_tif_2.tiff',
  34. 'file_tif_3.tiff',
  35. 'file_tif_4.tiff',
  36. 'file_tif_5.tiff',
  37. 'file_tif_6.tiff',
  38. 'file_tif_7.tiff',
  39. 'file_tif_8.tiff',
  40. 'file_tif_9.tiff',
  41. 'file_tif_10.tiff',
  42. 'file_tif_11.tiff',
  43. 'file_tif_12.tiff',
  44. 'file_tif_13.tiff',
  45. 'file_tif_14.tiff']}
  46. # analogsignals (N=0)
  47. """
  48. name = 'TIFF IO'
  49. description = "Neo IO module for optical imaging data stored as a folder of TIFF images."
  50. _prefered_signal_group_mode = 'group-by-same-units'
  51. is_readable = True
  52. is_writable = False
  53. supported_objects = [Block, Segment, ImageSequence]
  54. readable_objects = supported_objects
  55. writeable_objects = []
  56. support_lazy = False
  57. read_params = {}
  58. write_params = {}
  59. extensions = []
  60. mode = 'dir'
  61. def __init__(self, directory_path=None, units=None, sampling_rate=None,
  62. spatial_scale=None, **kwargs):
  63. if not have_pil:
  64. raise Exception("Please install the pillow package to use TiffIO")
  65. BaseIO.__init__(self, directory_path, **kwargs)
  66. self.units = units
  67. self.sampling_rate = sampling_rate
  68. self.spatial_scale = spatial_scale
  69. def read_block(self, lazy=False, **kwargs):
  70. # to sort file
  71. def natural_sort(l):
  72. convert = lambda text: int(text) if text.isdigit() else text.lower()
  73. alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
  74. return sorted(l, key=alphanum_key)
  75. # find all the images in the given directory
  76. file_name_list = []
  77. # name of extensions to track
  78. types = ["*.tif", "*.tiff"]
  79. for file in types:
  80. file_name_list.append(glob.glob(self.filename+"/"+file))
  81. # flatten list
  82. file_name_list = [item for sublist in file_name_list for item in sublist]
  83. # delete path in the name of file
  84. file_name_list = [file_name[len(self.filename)+1::] for file_name in file_name_list]
  85. # sorting file
  86. file_name_list = natural_sort(file_name_list)
  87. list_data_image = []
  88. for file_name in file_name_list:
  89. list_data_image.append(
  90. np.array(Image.open(self.filename + "/" + file_name), dtype=np.float))
  91. list_data_image = np.array(list_data_image)
  92. if len(list_data_image.shape) == 4:
  93. list_data_image = []
  94. for file_name in file_name_list:
  95. list_data_image.append(
  96. np.array(Image.open(self.filename + "/" + file_name).convert('L'), dtype=np.float))
  97. print("read block")
  98. image_sequence = ImageSequence(np.stack(list_data_image),
  99. units=self.units,
  100. sampling_rate=self.sampling_rate,
  101. spatial_scale=self.spatial_scale)
  102. print("creating segment")
  103. segment = Segment(file_origin=self.filename)
  104. segment.annotate(tiff_file_names=file_name_list)
  105. segment.imagesequences = [image_sequence]
  106. block = Block(file_origin=self.filename)
  107. segment.block = block
  108. block.segments.append(segment)
  109. print("returning block")
  110. return block