ctv_handlers.py 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. from ..ctvs import get_ctv_function
  2. import numpy as np
  3. import pandas as pd
  4. from ..flags import FlagsManager
  5. class PixelWiseCTVHandler(object):
  6. def __init__(self, flags: FlagsManager, p1):
  7. super().__init__()
  8. try:
  9. ctv_method_file = flags.get_ctv_method_file()
  10. except FileNotFoundError as fnfe:
  11. ctv_method_file = None
  12. self.ctv_method = get_ctv_function(flags["CTV_Method"], ctv_method_file)
  13. self.ctv_firstframe = flags["CTV_firstframe"]
  14. self.ctv_lastframe = flags["CTV_lastframe"]
  15. self.sampling_period = p1.metadata.trial_ticks
  16. stim_on_times_td = p1.pulsed_stimuli_handler.get_pulse_start_times()
  17. if len(stim_on_times_td) > 0:
  18. self.stim_on_times = stim_on_times_td / pd.Timedelta("1ms")
  19. else:
  20. self.stim_on_times = None
  21. stim_off_times_td = p1.pulsed_stimuli_handler.get_pulse_end_times()
  22. if len(stim_off_times_td) > 0:
  23. self.stim_off_times = stim_off_times_td / pd.Timedelta("1ms")
  24. else:
  25. self.stim_off_times = None
  26. self.stimulus_number = flags["CTV_StimulusNumber"]
  27. self.flags = flags
  28. self.p1 = p1
  29. def apply(self, data):
  30. """
  31. Apply the CTV specified in self.flags during initialization pixel wise to generate overview frames
  32. :param numpy.ndarray data: 3D, XYT
  33. :rtype: numpy.ndarray
  34. :return: dimensions: features-X-Y
  35. """
  36. result_frame = None
  37. for x_ind, y_ind in np.ndindex(*data.shape[:2]):
  38. features = self.apply_pixel(data[x_ind, y_ind, :])
  39. if result_frame is None:
  40. result_frame = np.empty((len(features), *data.shape[:2]))
  41. result_frame[:, x_ind, y_ind] = features
  42. return result_frame # dimensions: features-X-Y
  43. def apply_pixel(self, timetrace):
  44. return self.ctv_method(
  45. time_trace=timetrace,
  46. first_frame=self.ctv_firstframe,
  47. last_frame=self.ctv_lastframe,
  48. sampling_period=self.sampling_period,
  49. stim_on_times=self.stim_on_times,
  50. stim_off_times=self.stim_off_times,
  51. stimulus_number=self.stimulus_number,
  52. flags=self.flags,
  53. p1=self.p1)
  54. def get_ctv_handler(flags, p1):
  55. if flags["SO_Method"] == 0:
  56. return PixelWiseCTVHandler(flags=flags, p1=p1)
  57. else:
  58. raise NotImplementedError(
  59. f"Features with 'SO_Method' set to {flags['SO_Method']} have not yet been implemented\n")