Advertisement
Guest User

edfx hypnogram converter

a guest
Nov 19th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.88 KB | None | 0 0
  1. def convert_hypnograms(datadir):
  2. """
  3. This function is quite a hack to read the edf hypnogram as a byte array.
  4. I found no working reader for the hypnogram edfs.
  5. """
  6. print('Converting hypnograms')
  7. files = [x for x in os.listdir(datadir) if x.endswith('.hyp')]
  8. for file in files:
  9. file = os.path.join(datadir,file)
  10. hypnogram = []
  11. with open(file, mode='rb') as f: # b is important -> binary
  12.  
  13. raw_hypno = [x for x in str(f.read()).split('Sleep_stage_')][1:]
  14. for h in raw_hypno:
  15. stage = h[0]
  16. repeat = int(h.split('\\')[0][12:])//30 # no idea if this also works on linux
  17. hypnogram.extend(stage*repeat)
  18. with open(file[:-4] + '.csv', "w") as f:
  19. writer = csv.writer(f, lineterminator='\r')
  20. writer.writerows(hypnogram)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement