hbp_d571_example2.py 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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, spatial_scale=0.05 * mm).read()
  11. #data = TiffIO(data_path).read(units=dimensionless, sampling_rate=25 * Hz, spatial_scale=0.05 * mm)
  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. print(images.min(), images.max(), images.mean()) ###
  18. # preprocessing
  19. background = np.mean(images, axis=0)
  20. print(background.min(), background.max(), background.mean())
  21. preprocessed_images = images - background
  22. print(preprocessed_images.min(), preprocessed_images.max(), preprocessed_images.mean(), preprocessed_images.shape, preprocessed_images.dtype)
  23. np.save("preprocessed_images_orig.npy", preprocessed_images.magnitude)
  24. plt.subplot(2, 2, 2)
  25. plt.imshow(preprocessed_images[100], cmap='gray')
  26. plt.title("Subtracted background (frame 100)")
  27. # defining ROI and extracting signal
  28. roi = CircularRegionOfInterest(x=50, y=50, radius=10)
  29. circle = plt.Circle(roi.centre, roi.radius, color='b', fill=False)
  30. ax = plt.gca()
  31. ax.add_artist(circle)
  32. central_signal = preprocessed_images.signal_from_region(roi)[0]
  33. plt.subplot(2, 2, 3)
  34. plt.plot(central_signal.times, central_signal, lw=0.8)
  35. plt.title("Mean signal from ROI")
  36. plt.xlabel("Time [s]")
  37. # calculating power spectrum
  38. freqs, psd = el.spectral.welch_psd(central_signal,
  39. fs=central_signal.sampling_rate,
  40. freq_res=0.1 * Hz,
  41. overlap=0.8)
  42. plt.subplot(2, 2, 4)
  43. plt.plot(freqs, np.mean(psd, axis=0), lw=0.8)
  44. plt.title("Average power spectrum")
  45. plt.xlabel("frequency [Hz]")
  46. plt.ylabel("Fourier signal")
  47. plt.tight_layout()
  48. plt.show() # see Figure 2