hbp_d571_example.py 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. import os
  2. import matplotlib.pyplot as plt
  3. import numpy as np
  4. from quantities import Hz, mm, dimensionless
  5. from neo.core import CircularRegionOfInterest
  6. from neo.io import TiffIO
  7. import elephant as el
  8. data_path = os.path.expanduser("~/Data/WaveScalES/LENS/170110_mouse2_deep/t1")
  9. # loading data
  10. data = TiffIO(data_path, units=dimensionless, sampling_rate=25 * Hz,
  11. spatial_scale=0.05 * mm).read()
  12. images = data[0].segments[0].imagesequences[0]
  13. images /= images.max()
  14. plt.subplot(2, 2, 1)
  15. plt.imshow(images[100], cmap='gray')
  16. plt.title("Original image (frame 100)")
  17. # preprocessing
  18. background = np.mean(images, axis=0)
  19. preprocessed_images = images - background
  20. plt.subplot(2, 2, 2)
  21. plt.imshow(preprocessed_images[100], cmap='gray')
  22. plt.title("Subtracted background (frame 100)")
  23. # defining ROI and extracting signal
  24. roi = CircularRegionOfInterest(x=50, y=50, radius=10)
  25. circle = plt.Circle(roi.centre, roi.radius, color='b', fill=False)
  26. ax = plt.gca()
  27. ax.add_artist(circle)
  28. central_signal = preprocessed_images.signal_from_region(roi)[0]
  29. plt.subplot(2, 2, 3)
  30. plt.plot(central_signal.times, central_signal, lw=0.8)
  31. plt.title("Mean signal from ROI")
  32. plt.xlabel("Time [s]")
  33. # calculating power spectrum
  34. freqs, psd = el.spectral.welch_psd(central_signal,
  35. fs=central_signal.sampling_rate,
  36. freq_res=0.1 * Hz,
  37. overlap=0.8)
  38. plt.subplot(2, 2, 4)
  39. plt.plot(freqs, np.mean(psd, axis=0), lw=0.8)
  40. plt.title("Average power spectrum")
  41. plt.xlabel("frequency [Hz]")
  42. plt.ylabel("Fourier signal")
  43. plt.tight_layout()
  44. plt.show() # see Figure 2