Class: Edfize::Edf

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

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.



48
49
50
51
52
53
54
55
# File 'lib/edfize/edf.rb', line 48

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.



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

def duration_of_a_data_record
  @duration_of_a_data_record
end

#filenameObject (readonly)

EDF File Path



6
7
8
# File 'lib/edfize/edf.rb', line 6

def filename
  @filename
end

#local_patient_identificationObject

Returns the value of attribute local_patient_identification.



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

def local_patient_identification
  @local_patient_identification
end

#local_recording_identificationObject

Returns the value of attribute local_recording_identification.



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

def local_recording_identification
  @local_recording_identification
end

#number_of_bytes_in_headerObject

Returns the value of attribute number_of_bytes_in_header.



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

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.



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

def number_of_data_records
  @number_of_data_records
end

#number_of_signalsObject

Returns the value of attribute number_of_signals.



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

def number_of_signals
  @number_of_signals
end

#reservedObject

Returns the value of attribute reserved.



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

def reserved
  @reserved
end

#signalsObject

Returns the value of attribute signals.



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

def signals
  @signals
end

#start_date_of_recordingObject

Returns the value of attribute start_date_of_recording.



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

def start_date_of_recording
  @start_date_of_recording
end

#start_time_of_recordingObject

Returns the value of attribute start_time_of_recording.



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

def start_time_of_recording
  @start_time_of_recording
end

#versionObject

Header Information



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

def version
  @version
end

Class Method Details

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

Yields:

  • (edf)


42
43
44
45
46
# File 'lib/edfize/edf.rb', line 42

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

Instance Method Details

#edf_sizeObject

Total File Size In Bytes



70
71
72
# File 'lib/edfize/edf.rb', line 70

def edf_size
  File.size(@filename)
end

#expected_data_sizeObject

Data Section Size In Bytes



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

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



79
80
81
# File 'lib/edfize/edf.rb', line 79

def expected_edf_size
  expected_data_size + size_of_header
end

#expected_size_of_headerObject



65
66
67
# File 'lib/edfize/edf.rb', line 65

def expected_size_of_header
  @number_of_bytes_in_header
end

#load_signalsObject



57
58
59
# File 'lib/edfize/edf.rb', line 57

def load_signals
  get_data_records
end


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/edfize/edf.rb', line 107

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



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

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

#section_units(section) ⇒ Object



87
88
89
90
91
92
93
94
95
# File 'lib/edfize/edf.rb', line 87

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

#section_value_to_string(section) ⇒ Object



83
84
85
# File 'lib/edfize/edf.rb', line 83

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

#size_of_headerObject



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

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