curate_LENS_data.py 3.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import numpy as np
  2. import argparse
  3. import neo
  4. import quantities as pq
  5. from utils.parse import parse_string2dict, none_or_float, none_or_str
  6. from utils.neo_utils import imagesequence_to_analogsignal, time_slice
  7. from utils.neo_utils import rotate_image, flip_image
  8. from utils.io_utils import write_neo
  9. import matplotlib.pyplot as plt
  10. if __name__ == '__main__':
  11. CLI = argparse.ArgumentParser(description=__doc__,
  12. formatter_class=argparse.RawDescriptionHelpFormatter)
  13. CLI.add_argument("--data", nargs='?', type=str, required=True,
  14. help="path to input data directory")
  15. CLI.add_argument("--output", nargs='?', type=str, required=True,
  16. help="path of output file")
  17. CLI.add_argument("--sampling_rate", nargs='?', type=none_or_float,
  18. default=None, help="sampling rate in Hz")
  19. CLI.add_argument("--spatial_scale", nargs='?', type=float, required=True,
  20. help="distance between electrodes or pixels in mm")
  21. CLI.add_argument("--data_name", nargs='?', type=str, default='None',
  22. help="chosen name of the dataset")
  23. CLI.add_argument("--annotations", nargs='+', type=none_or_str, default=None,
  24. help="metadata of the dataset")
  25. CLI.add_argument("--array_annotations", nargs='+', type=none_or_str,
  26. default=None, help="channel-wise metadata")
  27. CLI.add_argument("--kwargs", nargs='+', type=none_or_str, default=None,
  28. help="additional optional arguments")
  29. CLI.add_argument("--t_start", nargs='?', type=none_or_float, default=None,
  30. help="start time in seconds")
  31. CLI.add_argument("--t_stop", nargs='?', type=none_or_float, default=None,
  32. help="stop time in seconds")
  33. CLI.add_argument("--orientation_top", nargs='?', type=str, required=True,
  34. help="upward orientation of the recorded cortical region")
  35. CLI.add_argument("--orientation_right", nargs='?', type=str, required=True,
  36. help="right-facing orientation of the recorded cortical region")
  37. args, unknown = CLI.parse_known_args()
  38. # Load optical data
  39. nio = neo.io.tiffio.TiffIO(directory_path=args.data,
  40. sampling_rate=args.sampling_rate*pq.Hz,
  41. spatial_scale=args.spatial_scale*pq.mm,
  42. units='dimensionless')
  43. # loading the data flips the images vertically!
  44. block = nio.read_block()
  45. # change data orientation to be top=ventral, right=lateral
  46. imgseq = block.segments[0].imagesequences[0]
  47. imgseq = flip_image(imgseq, axis=-2) # vertical
  48. imgseq = rotate_image(imgseq, rotation=-90)
  49. block.segments[0].imagesequences[0] = imgseq
  50. # Transform into analogsignals
  51. asig = imagesequence_to_analogsignal(imgseq)
  52. asig = time_slice(asig, args.t_start, args.t_stop)
  53. if args.annotations is not None:
  54. asig.annotations.update(parse_string2dict(args.annotations))
  55. asig.annotations.update(orientation_top=args.orientation_top)
  56. asig.annotations.update(orientation_right=args.orientation_right)
  57. # # for debugging
  58. # fig, ax = plt.subplots()
  59. # ax.imshow(imgseq.as_array()[0], origin='lower')
  60. # breakpoint()
  61. # ToDo: add metadata
  62. block.name = args.data_name
  63. block.segments[0].name = 'Segment 1'
  64. block.segments[0].description = 'Loaded with neo.TiffIO (neo version {}). '\
  65. .format(neo.__version__)
  66. if asig.description is None:
  67. asig.description = ''
  68. asig.description += 'Ca+ imaging signal. '
  69. block.segments[0].analogsignals = [asig]
  70. # Save data
  71. write_neo(args.output, block)