ViewTools.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Fri Oct 12 08:56:46 2018
  5. @author: galizia
  6. """
  7. import numpy as np
  8. #import FID_names as FID_names
  9. from time import time
  10. from datetime import timedelta
  11. #import datetime
  12. #import numpy as np
  13. #import pandas as pd
  14. #import os
  15. #from ggplot import *
  16. # import struct
  17. import matplotlib.pyplot as plt
  18. #from matplotlib.backends.backend_pdf import PdfPages
  19. from moviepy.editor import VideoClip
  20. from moviepy.video.io.bindings import mplfig_to_npimage
  21. def calc_timetrace(imaging_Data, mask):
  22. ##section to flatten array with mask
  23. xpixels, ypixels, frames = imaging_Data.shape
  24. timetrace = np.zeros(frames)
  25. mask_sum = mask.sum()
  26. for i in range(frames): #I'm sure there is an inbuild way to get the time trace, I'll search for it later
  27. # np.mean(array, axis=(1,2))
  28. # temp = mask * imaging_Data[:,:,i]
  29. temp = np.multiply(mask,imaging_Data[:,:,i]) #is this multiplication better?
  30. timetrace[i] = temp.sum()/mask_sum
  31. return timetrace
  32. #end calc_timetrace
  33. def secondsToStr(t):
  34. return str(timedelta(seconds=t))
  35. def log(s='start counting', elapsed=None):
  36. start = time()
  37. line = "="*42
  38. print(line)
  39. print(secondsToStr(time()), '-', s)
  40. if elapsed:
  41. print("Elapsed time:", elapsed)
  42. print(line)
  43. print()
  44. return start
  45. def endlog(start, s="End Program"):
  46. end = time()
  47. elapsed = end-start
  48. log(s, secondsToStr(elapsed))
  49. def now():
  50. return secondsToStr(time())
  51. def save_movie_file_xyt(dataMtrx, fps=24, bitrate="256k", movie_filename=''):
  52. #Oct18: procedure adapted from FID_out
  53. # dataMtrx has shape x,y,t
  54. # lcl_flags needed for filename, if filename is given, ignore lcl_flags
  55. start = log("save_movie_file_test2")
  56. if movie_filename == '':
  57. movie_filename = 'dummyMovie.mp4'
  58. zlen = dataMtrx.shape[2]
  59. # scale matrix to min, max
  60. scaleMin = dataMtrx.min()
  61. scaleMax = dataMtrx.max()
  62. duration = (zlen-1)/fps #frames/fps
  63. fig = plt.figure()
  64. ax = fig.add_subplot(111)
  65. fig.tight_layout(pad=1.0)
  66. def make_frame(t):
  67. #gives frame at time t
  68. ax.clear() #without this, it is very slow
  69. ax.axis('off')
  70. #ax.set_title("Frame " + str(int(zlen/duration*t)) + "/" + str(zlen))
  71. ax.imshow(dataMtrx[:,:,int(zlen/duration*t)], clim=(scaleMin, scaleMax))
  72. #fig.colorbar()
  73. return mplfig_to_npimage(fig)
  74. animation = VideoClip(make_frame, duration=duration)
  75. # export as a video file
  76. # animation.write_gif(movie_filename+'.gif', fps=fps) #gif is larger
  77. animation.write_videofile(movie_filename, fps=fps, bitrate=bitrate)
  78. plt.close()
  79. endlog(start, "save_movie_file_xyt")