Class: Edfize::Edf

Inherits:
Object
  • Object
show all
Defined in:
lib/edfize/edf.rb

Overview

Class used to load and manipulate EDFs

Constant Summary collapse

HEADER_CONFIG =
{
  version:                        { size:  8, after_read: :to_i,  name: 'Version' },
  local_patient_identification:   { size: 80, after_read: :strip, name: 'Local Patient Identification' },
  local_recording_identification: { size: 80, after_read: :strip, name: 'Local Recording Identification' },
  start_date_of_recording:        { size:  8,                     name: 'Start Date of Recording', description: '(dd.mm.yy)' },
  start_time_of_recording:        { size:  8,                     name: 'Start Time of Recording', description: '(hh.mm.ss)'},
  number_of_bytes_in_header:      { size:  8, after_read: :to_i,  name: 'Number of Bytes in Header' },
  reserved:                       { size: 44,                     name: 'Reserved' },
  number_of_data_records:         { size:  8, after_read: :to_i,  name: 'Number of Data Records' },
  duration_of_a_data_record:      { size:  8, after_read: :to_i,  name: 'Duration of a Data Record', units: 'second' },
  number_of_signals:              { size:  4, after_read: :to_i,  name: 'Number of Signals' }
}
HEADER_OFFSET =
HEADER_CONFIG.collect{|k,h| h[:size]}.inject(:+)
SIZE_OF_SAMPLE_IN_BYTES =
2
RESERVED_SIZE =

Used by tests

HEADER_CONFIG[:reserved][:size]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filename) ⇒ Edf

Returns a new instance of Edf.



52
53
54
55
56
57
58
59
# File 'lib/edfize/edf.rb', line 52

def initialize(filename)
  @filename = filename
  @signals = []

  read_header
  read_signal_header
  self
end

Instance Attribute Details

#duration_of_a_data_recordObject

Returns the value of attribute duration_of_a_data_record.



21
22
23
# File 'lib/edfize/edf.rb', line 21

def duration_of_a_data_record
  @duration_of_a_data_record
end

#filenameObject (readonly)

EDF File Path



10
11
12
# File 'lib/edfize/edf.rb', line 10

def filename
  @filename
end

#local_patient_identificationObject

Returns the value of attribute local_patient_identification.



14
15
16
# File 'lib/edfize/edf.rb', line 14

def local_patient_identification
  @local_patient_identification
end

#local_recording_identificationObject

Returns the value of attribute local_recording_identification.



15
16
17
# File 'lib/edfize/edf.rb', line 15

def local_recording_identification
  @local_recording_identification
end

#number_of_bytes_in_headerObject

Returns the value of attribute number_of_bytes_in_header.



18
19
20
# File 'lib/edfize/edf.rb', line 18

def number_of_bytes_in_header
  @number_of_bytes_in_header
end

#number_of_data_recordsObject

Returns the value of attribute number_of_data_records.



20
21
22
# File 'lib/edfize/edf.rb', line 20

def number_of_data_records
  @number_of_data_records
end

#number_of_signalsObject

Returns the value of attribute number_of_signals.



22
23
24
# File 'lib/edfize/edf.rb', line 22

def number_of_signals
  @number_of_signals
end

#reservedObject

Returns the value of attribute reserved.



19
20
21
# File 'lib/edfize/edf.rb', line 19

def reserved
  @reserved
end

#signalsObject

Returns the value of attribute signals.



24
25
26
# File 'lib/edfize/edf.rb', line 24

def signals
  @signals
end

#start_date_of_recordingObject

Returns the value of attribute start_date_of_recording.



16
17
18
# File 'lib/edfize/edf.rb', line 16

def start_date_of_recording
  @start_date_of_recording
end

#start_time_of_recordingObject

Returns the value of attribute start_time_of_recording.



17
18
19
# File 'lib/edfize/edf.rb', line 17

def start_time_of_recording
  @start_time_of_recording
end

#versionObject

Header Information



13
14
15
# File 'lib/edfize/edf.rb', line 13

def version
  @version
end

Class Method Details

.create(filename) {|edf| ... } ⇒ Object

Yields:

  • (edf)


46
47
48
49
50
# File 'lib/edfize/edf.rb', line 46

def self.create(filename, &block)
  edf = new(filename)
  yield edf if block_given?
  edf
end

Instance Method Details

#edf_sizeObject

Total File Size In Bytes



81
82
83
# File 'lib/edfize/edf.rb', line 81

def edf_size
  File.size(@filename)
end

#expected_data_sizeObject

Data Section Size In Bytes



86
87
88
# File 'lib/edfize/edf.rb', line 86

def expected_data_size
  @signals.collect(&:samples_per_data_record).inject(:+).to_i * @number_of_data_records * SIZE_OF_SAMPLE_IN_BYTES
end

#expected_edf_sizeObject



90
91
92
# File 'lib/edfize/edf.rb', line 90

def expected_edf_size
  expected_data_size + size_of_header
end

#expected_size_of_headerObject



76
77
78
# File 'lib/edfize/edf.rb', line 76

def expected_size_of_header
  @number_of_bytes_in_header
end

#load_epoch(epoch_number, epoch_size) ⇒ Object

Epoch Number is Zero Indexed, and Epoch Size is in Seconds (Not Data Records)



66
67
68
69
70
# File 'lib/edfize/edf.rb', line 66

def load_epoch(epoch_number, epoch_size)
  # reset_signals!
  load_digital_signals_by_epoch(epoch_number, epoch_size)
  calculate_physical_values!
end

#load_signalsObject



61
62
63
# File 'lib/edfize/edf.rb', line 61

def load_signals
  get_data_records
end

#parse_integer(string) ⇒ Object



153
154
155
156
157
# File 'lib/edfize/edf.rb', line 153

def parse_integer(string)
  Integer(format('%g', string))
rescue
  nil
end


116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'lib/edfize/edf.rb', line 116

def print_header
  puts "\nEDF                            : #{@filename}"
  puts "Total File Size                : #{edf_size} bytes"
  puts "\nHeader Information"
  HEADER_CONFIG.each do |section, hash|
    puts "#{hash[:name]}#{' '*(31 - hash[:name].size)}: " + section_value_to_string(section) + section_units(section) + section_description(section)
  end
  puts "\nSignal Information"
  signals.each_with_index do |signal, index|
    puts "\n  Position                     : #{index + 1}"
    signal.print_header
  end
  puts "\nGeneral Information"
  puts "Size of Header (bytes)         : #{size_of_header}"
  puts "Size of Data   (bytes)         : #{data_size}"
  puts "Total Size     (bytes)         : #{edf_size}"

  puts "Expected Size of Header (bytes): #{expected_size_of_header}"
  puts "Expected Size of Data   (bytes): #{expected_data_size}"
  puts "Expected Total Size     (bytes): #{expected_edf_size}"
end

#section_description(section) ⇒ Object



107
108
109
110
111
112
113
114
# File 'lib/edfize/edf.rb', line 107

def section_description(section)
  description = HEADER_CONFIG[section][:description].to_s
  if description == ''
    ''
  else
    " #{description}"
  end
end

#section_units(section) ⇒ Object



98
99
100
101
102
103
104
105
# File 'lib/edfize/edf.rb', line 98

def section_units(section)
  units = HEADER_CONFIG[section][:units].to_s
  if units == ''
    ''
  else
    " #{units}" + (instance_variable_get("@#{section}") == 1 ? '' : 's')
  end
end

#section_value_to_string(section) ⇒ Object



94
95
96
# File 'lib/edfize/edf.rb', line 94

def section_value_to_string(section)
  instance_variable_get("@#{section}").to_s
end

#size_of_headerObject



72
73
74
# File 'lib/edfize/edf.rb', line 72

def size_of_header
  HEADER_OFFSET + ns * Signal::SIGNAL_CONFIG.collect { |_k, h| h[:size] }.inject(:+)
end

#start_dateObject



138
139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'lib/edfize/edf.rb', line 138

def start_date
  (dd, mm, yy) = start_date_of_recording.split('.')
  dd = parse_integer(dd)
  mm = parse_integer(mm)
  yy = parse_integer(yy)
  yyyy = if yy && yy >= 85
           yy + 1900
         else
           yy + 2000
         end
  Date.strptime("#{mm}/#{dd}/#{yyyy}", '%m/%d/%Y')
rescue
  nil
end