We've updated our privacy policy.

Rewrite EDF Header Start Date of Recording

Rewrite EDF Header Start Date of Recording

In this tutorial, you will learn how to:

  1. Rewrite an invalid signal start date of recording in the EDF Header

The Requirements for running this tutorial are described after the discussion of the tutorial script.

The script

require 'rubygems'
require 'colorize'
require 'edfize'
require 'nsrr'
require 'nsrr/commands/download'

Nsrr::Commands::Download.run(%w(download learn/polysomnography/edfs/learn-nsrr01.edf --token=public))
Nsrr::Commands::Download.run(%w(download learn/polysomnography/edfs/invalid/learn-nsrr01-invalid-date.edf --token=public))

puts "EDFs Downloaded: #{Edfize.edf_paths.count}"

INVALID_DATE = '00.00.00'
CLIPPING_DATE = '01.01.85'

Edfize.edfs do |edf|
  if edf.start_date_of_recording == INVALID_DATE
    initial_date = edf.start_date_of_recording
    edf.update(start_date_of_recording: CLIPPING_DATE)
    puts initial_date.colorize(:red) + " to " + edf.start_date_of_recording.colorize(:green) + " for #{edf.filename}"
  else
    puts "   OK".colorize(:green) + "       #{edf.start_date_of_recording} for #{edf.filename}"
  end
end

puts "\nCongrats! You have completed your third tutorial!".colorize(:green).on_white

download script

Breaking down the script

The script loads two EDFs from the NSRR using the Nsrr::Commands::Download.run method, described in Tutorial 01. The two EDFs are identical, except that the invalid EDF has its date set to an invalid date, specifically 00.00.00.

Next, the script sets two constants, INVALID_DATE, and CLIPPING_DATE.

INVALID_DATE = '00.00.00'
CLIPPING_DATE = '01.01.85'

The INVALID_DATE constant is used to identify EDFs to be rewritten, and the CLIPPING_DATE constant is the new valid date that is written to the EDF.

edf.update(start_date_of_recording: CLIPPING_DATE)

The script then iterates through all the EDFs in the current folder and subfolders and checks if the edf.start_date_of_recording is invalid. For EDFs with invalid start dates, the update method is called. The update method accepts a hash of key value pairs that correspond to the attribute being updated and the new value being assigned.

Try These

  1. This tutorial is based on a real issue that existed with EDFs hosted on the NSRR that is described in this blog post. While the NSRR mainly had invalid "Zero Dates" in the EDFs, modify the script to rewrite ANY invalid date to the clipping date.
  2. The NSRR maintains the original start time of the recording to help researchers interested in comparing EDFs based on the start time, however it can be useful to set all the start times to midnight as well. Modify the script to change the start_time_of_recording to midnight (00.00.00).

If you completed this tutorial, please leave a review and check out the next tutorial in the series:

Tutorial 04 - Compute EDF Signal Averages

Requirements

This tutorial requires that you have Ruby 2.2 or above installed, preferably Ruby 2.3 or higher.

Extract

Extract the script to your desktop to a folder of the same name. Open your console (Command Prompt on Windows or Terminal on Mac/Linux) and navigate to your tutorial folder.

MacOS/Linux

cd ~/Desktop/tutorial_03

Windows

cd %USERPROFILE%\Desktop\tutorial_03

Notes

  • cd stands for "Change Directory"
  • ~, and %USERPROFILE% represents your user home directory

Installing the required gems.

This script requires you to have the colorize, nsrr, and edfize gems installed. The colorize gem is used to color command line output. The nsrr gem is used to easily download sets of files from the National Sleep Research Resource (https://sleepdata.org). The edfize gem is used to read and write EDF files using Ruby.

In your console type:

gem install colorize nsrr edfize --no-document

This command will get you the latest stable release of the colorize, nsrr, and edfize gem from RubyGems.org. The --no-document flag specifies to skip the installation of the documentation for the gems.

Running the script.

In your console and from the tutorial_03 directory, type:

ruby tutorial_03.rb

Questions?

Remember, if you have any questions on this script, please let us know on the NSRR forum here: https://sleepdata.org/forum, or email us at support@sleepdata.org.