read_proxy_with_lazy_load.py 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. """
  2. This is an example demonstrate the lazy load and proxy objects.
  3. """
  4. import urllib
  5. import neo
  6. import quantities as pq
  7. import numpy as np
  8. url_repo = 'https://web.gin.g-node.org/NeuralEnsemble/ephy_testing_data/raw/master/'
  9. # Get Plexon files
  10. distantfile = url_repo + 'micromed/File_micromed_1.TRC'
  11. localfile = './File_micromed_1.TRC'
  12. urllib.request.urlretrieve(distantfile, localfile)
  13. # create a reader
  14. reader = neo.MicromedIO(filename='File_micromed_1.TRC')
  15. reader.parse_header()
  16. lim0, lim1 = -20 * pq.ms, +20 * pq.ms
  17. def apply_my_fancy_average(sig_list):
  18. """basic average along triggers and then channels
  19. here we go back to numpy with magnitude
  20. to be able to use np.stack
  21. """
  22. sig_list = [s.magnitude for s in sig_list]
  23. sigs = np.stack(sig_list, axis=0)
  24. return np.mean(np.mean(sigs, axis=0), axis=1)
  25. seg = reader.read_segment(lazy=False)
  26. triggers = seg.events[0]
  27. anasig = seg.analogsignals[0] # here anasig contain the whole recording in memory
  28. all_sig_chunks = []
  29. for t in triggers.times:
  30. t0, t1 = (t + lim0), (t + lim1)
  31. anasig_chunk = anasig.time_slice(t0, t1)
  32. all_sig_chunks.append(anasig_chunk)
  33. m1 = apply_my_fancy_average(all_sig_chunks)
  34. seg = reader.read_segment(lazy=True)
  35. triggers = seg.events[0].load(time_slice=None) # this load all trigers in memory
  36. anasigproxy = seg.analogsignals[0] # this is a proxy
  37. all_sig_chunks = []
  38. for t in triggers.times:
  39. t0, t1 = (t + lim0), (t + lim1)
  40. anasig_chunk = anasigproxy.load(time_slice=(t0, t1)) # here real data are loaded
  41. all_sig_chunks.append(anasig_chunk)
  42. m2 = apply_my_fancy_average(all_sig_chunks)
  43. print(m1)
  44. print(m2)