tasci_merger 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/lib/man_merger.rb +228 -231
  3. data/tasci_merger.gemspec +2 -2
  4. metadata +2 -2
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 954e2e96e4c21b8fb1f6e5b5c7ba7f5c3dac9dd5
4
- data.tar.gz: 52f83f1f802fe89c54a3f524675b04cbbdcc3488
3
+ metadata.gz: a7097b481a5ccd05ccbb429433cc122fcfedc655
4
+ data.tar.gz: 981ca465620a29860db631b0678d6c69e9e37810
5
5
  SHA512:
6
- metadata.gz: 10c2a5b74a095d0765cec187932112c089318eedbd0f4b918bac89b9401f13cb220cd83d0afee11c17d268efdc31341ae61ea3eda114f30f8f2a6ee3d9c485f6
7
- data.tar.gz: 8d314b9b0a46f8e449170c972945b00d5195cc1459e6ebe8e1f2d7f292dd00012737f870abc88cfa6c405ed236280daf80a90df2fa13a07bd7e890b216e514df
6
+ metadata.gz: e301450ff95d0764fee633acf430fb63f08bd5c953a265c5203cc1fc9e6e4bffa88597ec88939d467d5bd148185eb16d02ca532d4f6ac1cd146b2fe88e0603c4
7
+ data.tar.gz: 9c260f76bc1610525f46aa9884eb7c91f211d34f5d2e7d3120de5ff4870b28b5449d643755959ccaeb3850a49c77dad1fb3574466ff452699360bbf56bc3f332
data/lib/man_merger.rb CHANGED
@@ -3,297 +3,294 @@ require 'csv'
3
3
  ## CHANGELOG
4
4
  # 2173 Master file: change sp16,17,18 to *_rev
5
5
 
6
- module ETL
7
- class ManMerger
8
- LIST_DIR = "/usr/local/htdocs/access/lib/data/etl/klerman_merge_man_files/file_lists/"
9
- T_DRIVE_DIRS = ["/home/pwm4/Windows/tdrive/IPM/Modafinil_FD_42.85h/", "/home/pwm4/Windows/tdrive/IPM/NSBRI_65d_Entrainment/"]
10
- #T_DRIVE_DIR = "/home/pwm4/Windows/tdrive/IPM/Modafinil_FD_42.85h/"
11
- EPOCH_LENGTH = 30
12
-
13
- def merge_files
14
- subject_list = load_subject_list
15
- subject_list.each do |subject_code, file_list|
16
- merged_file = CSV.open("/usr/local/htdocs/access/lib/data/etl/klerman_merge_man_files/merged_files/#{subject_code}_merged.csv", "wb")
17
- merged_file << %w(SUBJECT_CODE LABTIME SLEEP_STAGE SLEEP_PERIOD SEM_FLAG)
18
- MY_LOG.info "---- #{subject_code}"
19
-
20
- previous_first_labtime = nil
21
- previous_last_labtime = nil
22
- subject_year = get_subject_year(file_list)
23
-
24
- file_list.each do |file_hash|
25
- matched_files = Dir.glob("#{T_DRIVE_DIRS[0]}#{subject_code}/PSG/SCORED/**/#{file_hash[:pattern]}.man", File::FNM_CASEFOLD)
26
- matched_files = Dir.glob("#{T_DRIVE_DIRS[1]}#{subject_code}/Sleep/#{file_hash[:pattern]}.man", File::FNM_CASEFOLD) if matched_files.length != 1
27
-
28
- ## Validate File List
29
- if matched_files.length != 1
30
- raise StandardError, "None or more than one matched file. #{file_hash[:pattern]} #{matched_files} #{matched_files.length} #{subject_code}"
31
- else
32
- man_file_path = matched_files[0]
33
- end
6
+ class ManMerger
7
+ LIST_DIR = "/usr/local/htdocs/access/lib/data/etl/klerman_merge_man_files/file_lists/"
8
+ T_DRIVE_DIRS = ["/home/pwm4/Windows/tdrive/IPM/Modafinil_FD_42.85h/", "/home/pwm4/Windows/tdrive/IPM/NSBRI_65d_Entrainment/"]
9
+ #T_DRIVE_DIR = "/home/pwm4/Windows/tdrive/IPM/Modafinil_FD_42.85h/"
10
+ EPOCH_LENGTH = 30
11
+
12
+ def merge_files
13
+ subject_list = load_subject_list
14
+ subject_list.each do |subject_code, file_list|
15
+ merged_file = CSV.open("/usr/local/htdocs/access/lib/data/etl/klerman_merge_man_files/merged_files/#{subject_code}_merged.csv", "wb")
16
+ merged_file << %w(SUBJECT_CODE LABTIME SLEEP_STAGE SLEEP_PERIOD SEM_FLAG)
17
+ MY_LOG.info "---- #{subject_code}"
18
+
19
+ previous_first_labtime = nil
20
+ previous_last_labtime = nil
21
+ subject_year = get_subject_year(file_list)
22
+
23
+ file_list.each do |file_hash|
24
+ matched_files = Dir.glob("#{T_DRIVE_DIRS[0]}#{subject_code}/PSG/SCORED/**/#{file_hash[:pattern]}.man", File::FNM_CASEFOLD)
25
+ matched_files = Dir.glob("#{T_DRIVE_DIRS[1]}#{subject_code}/Sleep/#{file_hash[:pattern]}.man", File::FNM_CASEFOLD) if matched_files.length != 1
26
+
27
+ ## Validate File List
28
+ if matched_files.length != 1
29
+ raise StandardError, "None or more than one matched file. #{file_hash[:pattern]} #{matched_files} #{matched_files.length} #{subject_code}"
30
+ else
31
+ man_file_path = matched_files[0]
32
+ end
34
33
 
35
- man_file = File.open(man_file_path)
36
- LOADER_LOGGER.info "--- Loading #{man_file_path}"
37
- file_info = {}
34
+ man_file = File.open(man_file_path)
35
+ LOADER_LOGGER.info "--- Loading #{man_file_path}"
36
+ file_info = {}
38
37
 
39
38
 
40
- ## Ignore Corrupted Files
41
- #next if tasci_file_path == "/home/pwm4/Windows/tdrive/IPM/AFOSR9_Slp_Restrict//24B7GXT3/PSG/TASCI_SEM/24b7gxt3_082907_wp19ap1_PID_24B7GXT3_082907_WP19AP1_RID_0_SEM.TASCI"
39
+ ## Ignore Corrupted Files
40
+ #next if tasci_file_path == "/home/pwm4/Windows/tdrive/IPM/AFOSR9_Slp_Restrict//24B7GXT3/PSG/TASCI_SEM/24b7gxt3_082907_wp19ap1_PID_24B7GXT3_082907_WP19AP1_RID_0_SEM.TASCI"
42
41
 
43
- # Date from file name
44
- matched_date = /_(\d\d)(\d\d)(\d\d)_/.match(man_file_path)
45
- file_info[:fn_date] = (matched_date ? Time.zone.local((matched_date[3].to_i > 30 ? matched_date[3].to_i + 1900 : matched_date[3].to_i + 2000), matched_date[1].to_i, matched_date[2].to_i) : nil)
42
+ # Date from file name
43
+ matched_date = /_(\d\d)(\d\d)(\d\d)_/.match(man_file_path)
44
+ file_info[:fn_date] = (matched_date ? Time.zone.local((matched_date[3].to_i > 30 ? matched_date[3].to_i + 1900 : matched_date[3].to_i + 2000), matched_date[1].to_i, matched_date[2].to_i) : nil)
46
45
 
47
- # read file
48
- lines = man_file.readlines("\r")
49
- # delete possible empty last line
50
- lines.pop if lines.last.blank?
46
+ # read file
47
+ lines = man_file.readlines("\r")
48
+ # delete possible empty last line
49
+ lines.pop if lines.last.blank?
51
50
 
52
- # get file first and last times
53
- matched_time = /(\d\d):(\d\d):(\d\d):(\d\d\d)/.match(lines.first)
54
- file_info[:first_time] = {hour: matched_time[1].to_i, min: matched_time[2].to_i, sec: matched_time[3].to_i}
55
- matched_time = /(\d\d):(\d\d):(\d\d):(\d\d\d)/.match(lines.last)
56
- file_info[:last_time] = {hour: matched_time[1].to_i, min: matched_time[2].to_i, sec: matched_time[3].to_i}
51
+ # get file first and last times
52
+ matched_time = /(\d\d):(\d\d):(\d\d):(\d\d\d)/.match(lines.first)
53
+ file_info[:first_time] = {hour: matched_time[1].to_i, min: matched_time[2].to_i, sec: matched_time[3].to_i}
54
+ matched_time = /(\d\d):(\d\d):(\d\d):(\d\d\d)/.match(lines.last)
55
+ file_info[:last_time] = {hour: matched_time[1].to_i, min: matched_time[2].to_i, sec: matched_time[3].to_i}
57
56
 
58
- # validate first/last times
59
- if file_hash[:start_time] != file_info[:first_time]
60
- MY_LOG.error "---- FIRST TIME MISMATCH ---\n#{man_file_path}\n#{file_hash[:start_time]} #{file_info[:first_time]}\n\n"
61
- end
62
- if file_hash[:last_line_time] != file_info[:last_time]
63
- MY_LOG.error "---- LAST TIME MISMATCH ----\n#{man_file_path}\n#{file_hash[:last_line_time]} #{file_info[:last_time]}\n\n"
64
- end
65
- if file_hash[:last_line_number] != lines.length
66
- MY_LOG.error "---- LINE COUNT MISMATCH ----\n#{man_file_path}\n#{file_hash[:last_line_number]} #{lines.length}\n\n"
67
- end
57
+ # validate first/last times
58
+ if file_hash[:start_time] != file_info[:first_time]
59
+ MY_LOG.error "---- FIRST TIME MISMATCH ---\n#{man_file_path}\n#{file_hash[:start_time]} #{file_info[:first_time]}\n\n"
60
+ end
61
+ if file_hash[:last_line_time] != file_info[:last_time]
62
+ MY_LOG.error "---- LAST TIME MISMATCH ----\n#{man_file_path}\n#{file_hash[:last_line_time]} #{file_info[:last_time]}\n\n"
63
+ end
64
+ if file_hash[:last_line_number] != lines.length
65
+ MY_LOG.error "---- LINE COUNT MISMATCH ----\n#{man_file_path}\n#{file_hash[:last_line_number]} #{lines.length}\n\n"
66
+ end
68
67
 
69
- ##
70
- # VALIDATION
71
- file_hash[:start_labtime] = Labtime.from_decimal(file_hash[:start_labtime], subject_year)
72
- file_hash[:last_line_labtime] = Labtime.from_decimal(file_hash[:last_line_labtime], subject_year)
68
+ ##
69
+ # VALIDATION
70
+ file_hash[:start_labtime] = Labtime.from_decimal(file_hash[:start_labtime], subject_year)
71
+ file_hash[:last_line_labtime] = Labtime.from_decimal(file_hash[:last_line_labtime], subject_year)
73
72
 
74
- start_realtime = file_hash[:start_labtime].to_time
75
- last_line_realtime = file_hash[:last_line_labtime].to_time
73
+ start_realtime = file_hash[:start_labtime].to_time
74
+ last_line_realtime = file_hash[:last_line_labtime].to_time
76
75
 
77
- first_realtime = file_hash[:start_labtime].time_zone.local(start_realtime.year, start_realtime.month, start_realtime.day, file_info[:first_time][:hour], file_info[:first_time][:min], file_info[:first_time][:sec])
78
- last_realtime = file_hash[:last_line_labtime].time_zone.local(last_line_realtime.year, last_line_realtime.month, last_line_realtime.day, file_info[:last_time][:hour], file_info[:last_time][:min], file_info[:last_time][:sec])
76
+ first_realtime = file_hash[:start_labtime].time_zone.local(start_realtime.year, start_realtime.month, start_realtime.day, file_info[:first_time][:hour], file_info[:first_time][:min], file_info[:first_time][:sec])
77
+ last_realtime = file_hash[:last_line_labtime].time_zone.local(last_line_realtime.year, last_line_realtime.month, last_line_realtime.day, file_info[:last_time][:hour], file_info[:last_time][:min], file_info[:last_time][:sec])
79
78
 
80
- file_info[:first_labtime] = Labtime.parse(first_realtime)
81
- file_info[:last_labtime] = Labtime.parse(last_realtime)
82
- predicted_last_labtime = Labtime.parse(file_info[:first_labtime].to_time + ((lines.length - 1) * 30).seconds)
79
+ file_info[:first_labtime] = Labtime.parse(first_realtime)
80
+ file_info[:last_labtime] = Labtime.parse(last_realtime)
81
+ predicted_last_labtime = Labtime.parse(file_info[:first_labtime].to_time + ((lines.length - 1) * 30).seconds)
83
82
 
84
- sep = false
85
- if (file_hash[:start_labtime].time_in_seconds - file_info[:first_labtime].time_in_seconds).abs > 2
86
- MY_LOG.error "---- FIRST LABTIME MISMATCH ----\n#{man_file_path}\n#{file_hash[:start_labtime].time_in_seconds - file_info[:first_labtime].time_in_seconds} | #{file_hash[:start_labtime].to_time}\n#{file_hash[:start_labtime]} | #{file_info[:first_labtime]}\n"
87
- sep = true
88
- end
83
+ sep = false
84
+ if (file_hash[:start_labtime].time_in_seconds - file_info[:first_labtime].time_in_seconds).abs > 2
85
+ MY_LOG.error "---- FIRST LABTIME MISMATCH ----\n#{man_file_path}\n#{file_hash[:start_labtime].time_in_seconds - file_info[:first_labtime].time_in_seconds} | #{file_hash[:start_labtime].to_time}\n#{file_hash[:start_labtime]} | #{file_info[:first_labtime]}\n"
86
+ sep = true
87
+ end
89
88
 
90
- # These checks fail if DST TRANSITION HAPPENS
91
- if last_line_realtime.dst? == start_realtime.dst?
92
- if (file_hash[:last_line_labtime].time_in_seconds - file_info[:last_labtime].time_in_seconds).abs > 2
93
- MY_LOG.error "---- LAST LABTIME MISMATCH ----\n#{man_file_path}\n#{file_hash[:last_line_labtime].time_in_seconds - file_info[:last_labtime].time_in_seconds} | #{file_hash[:last_line_labtime].to_time}\n#{file_hash[:last_line_labtime]} | #{file_info[:last_labtime]}\n"
94
- sep = true
95
- end
96
- if (file_info[:last_labtime].time_in_seconds - predicted_last_labtime.time_in_seconds).abs > 0
97
- MY_LOG.error "---- PRED LABTIME MISMATCH ----\n#{man_file_path}\n#{(file_info[:last_labtime].time_in_seconds - predicted_last_labtime.time_in_seconds)} | #{predicted_last_labtime.to_time}\nl: #{file_info[:last_labtime]} | #{predicted_last_labtime}\n"
98
- sep = true
99
- end
89
+ # These checks fail if DST TRANSITION HAPPENS
90
+ if last_line_realtime.dst? == start_realtime.dst?
91
+ if (file_hash[:last_line_labtime].time_in_seconds - file_info[:last_labtime].time_in_seconds).abs > 2
92
+ MY_LOG.error "---- LAST LABTIME MISMATCH ----\n#{man_file_path}\n#{file_hash[:last_line_labtime].time_in_seconds - file_info[:last_labtime].time_in_seconds} | #{file_hash[:last_line_labtime].to_time}\n#{file_hash[:last_line_labtime]} | #{file_info[:last_labtime]}\n"
93
+ sep = true
100
94
  end
101
-
102
- if (file_hash[:last_line_labtime].time_in_seconds - predicted_last_labtime.time_in_seconds).abs > 2
103
- MY_LOG.error "---- !PRED LABTIME MISMATCH ----\n#{man_file_path}\n#{(file_hash[:last_line_labtime].time_in_seconds - predicted_last_labtime.time_in_seconds)} | #{predicted_last_labtime.to_time}\nl: #{file_info[:last_line_labtime]} | #{predicted_last_labtime}\n"
95
+ if (file_info[:last_labtime].time_in_seconds - predicted_last_labtime.time_in_seconds).abs > 0
96
+ MY_LOG.error "---- PRED LABTIME MISMATCH ----\n#{man_file_path}\n#{(file_info[:last_labtime].time_in_seconds - predicted_last_labtime.time_in_seconds)} | #{predicted_last_labtime.to_time}\nl: #{file_info[:last_labtime]} | #{predicted_last_labtime}\n"
104
97
  sep = true
105
98
  end
99
+ end
106
100
 
107
- unless previous_first_labtime.nil? or previous_last_labtime.nil?
108
- MY_LOG.error "Start time is before previous end labtime for #{man_file_path}" if file_info[:first_labtime] < previous_last_labtime
109
- end
101
+ if (file_hash[:last_line_labtime].time_in_seconds - predicted_last_labtime.time_in_seconds).abs > 2
102
+ MY_LOG.error "---- !PRED LABTIME MISMATCH ----\n#{man_file_path}\n#{(file_hash[:last_line_labtime].time_in_seconds - predicted_last_labtime.time_in_seconds)} | #{predicted_last_labtime.to_time}\nl: #{file_info[:last_line_labtime]} | #{predicted_last_labtime}\n"
103
+ sep = true
104
+ end
110
105
 
111
- raise StandardError, "AHHHHH" if file_info[:first_labtime].sec != first_realtime.sec
112
- raise StandardError, "AHHHHH" if file_info[:last_labtime].sec != last_realtime.sec
106
+ unless previous_first_labtime.nil? or previous_last_labtime.nil?
107
+ MY_LOG.error "Start time is before previous end labtime for #{man_file_path}" if file_info[:first_labtime] < previous_last_labtime
108
+ end
109
+
110
+ raise StandardError, "AHHHHH" if file_info[:first_labtime].sec != first_realtime.sec
111
+ raise StandardError, "AHHHHH" if file_info[:last_labtime].sec != last_realtime.sec
113
112
 
114
- MY_LOG.info "-----------------------------------\n\n" if sep
113
+ MY_LOG.info "-----------------------------------\n\n" if sep
115
114
 
116
- last_labtime = nil
117
- ibob_flag = 0
115
+ last_labtime = nil
116
+ ibob_flag = 0
118
117
 
119
- lines.each_with_index do |line, line_number|
120
- #merged_file << %w(SUBJECT_CODE LABTIME SLEEP_STAGE SLEEP_PERIOD SEM_FLAG)
118
+ lines.each_with_index do |line, line_number|
119
+ #merged_file << %w(SUBJECT_CODE LABTIME SLEEP_STAGE SLEEP_PERIOD SEM_FLAG)
121
120
  =begin
122
121
  sleep man file:
123
- 0 undef/unscored
124
- 1 stage 1
125
- 2 stage 2
126
- 3 stage 3
127
- 4 stage 4
128
- 5 wake
129
- 6 REM
130
- 7 MVT
131
- 8 LOff and LOn
122
+ 0 undef/unscored
123
+ 1 stage 1
124
+ 2 stage 2
125
+ 3 stage 3
126
+ 4 stage 4
127
+ 5 wake
128
+ 6 REM
129
+ 7 MVT
130
+ 8 LOff and LOn
132
131
 
133
132
  wake man file:
134
- 0 undef/un
133
+ 0 undef/un
135
134
  cored
136
- 1 stage 1
137
- 2 stage 2
138
- 3 stage 3
139
- 4 stage 4
140
- 5 wake
141
- 6 REM
142
- 7 MVT
143
- 8 SEM
135
+ 1 stage 1
136
+ 2 stage 2
137
+ 3 stage 3
138
+ 4 stage 4
139
+ 5 wake
140
+ 6 REM
141
+ 7 MVT
142
+ 8 SEM
144
143
  =end
145
144
 
146
145
 
147
- line_labtime = file_info[:first_labtime].add_seconds(EPOCH_LENGTH * line_number)
148
- line_code = /(\d)\s\d\d:\d\d:\d\d:\d\d\d/.match(line)[1].to_i
149
-
150
- # Sleep Period Coding:
151
- # 1 Sleep Onset (Lights Off) (IN BED)
152
- # 2 Sleep Offset (Lights On) (OUT OF BED)
153
- if file_hash[:type] == :sleep and line_code == 8
154
- if ibob_flag == 0
155
- sleep_period = 1
156
- ibob_flag = 1
157
- else
158
- sleep_period = 2
159
- ibob_flag = 0
160
- end
161
- else
162
- sleep_period = nil
163
- end
164
-
165
- # Sleep Stage Coding:
166
- # 1 stage 1
167
- # 2 stage 2
168
- # 3 stage 3
169
- # 4 stage 4
170
- # 6 MT
171
- # 7 Undef
172
- # 5 REM
173
- # 9 Wake
174
- if line_code >= 1 and line_code <= 4
175
- line_event = line_code
176
- elsif line_code == 0
177
- line_event = 7
178
- elsif line_code == 5 or line_code == 8
179
- line_event = 9
180
- elsif line_code == 6
181
- line_event = 5
182
- elsif line_code == 7
183
- line_event = 6
184
- else
185
- raise StandardError, "Cannot map the following event: #{line_code}"
186
- end
146
+ line_labtime = file_info[:first_labtime].add_seconds(EPOCH_LENGTH * line_number)
147
+ line_code = /(\d)\s\d\d:\d\d:\d\d:\d\d\d/.match(line)[1].to_i
187
148
 
188
- # SEM Event Coding:
189
- # 1 Slow Eye Movement
190
- # 0 No Slow Eye Movement
191
- if file_hash[:type] == :wake and line_code == 8
192
- sem_event = 1
149
+ # Sleep Period Coding:
150
+ # 1 Sleep Onset (Lights Off) (IN BED)
151
+ # 2 Sleep Offset (Lights On) (OUT OF BED)
152
+ if file_hash[:type] == :sleep and line_code == 8
153
+ if ibob_flag == 0
154
+ sleep_period = 1
155
+ ibob_flag = 1
193
156
  else
194
- sem_event = 0
157
+ sleep_period = 2
158
+ ibob_flag = 0
195
159
  end
160
+ else
161
+ sleep_period = nil
162
+ end
196
163
 
197
- last_labtime = line_labtime
198
-
199
- output_line = [subject_code.upcase, line_labtime.to_decimal, line_event, sleep_period, sem_event]
200
- merged_file << output_line
164
+ # Sleep Stage Coding:
165
+ # 1 stage 1
166
+ # 2 stage 2
167
+ # 3 stage 3
168
+ # 4 stage 4
169
+ # 6 MT
170
+ # 7 Undef
171
+ # 5 REM
172
+ # 9 Wake
173
+ if line_code >= 1 and line_code <= 4
174
+ line_event = line_code
175
+ elsif line_code == 0
176
+ line_event = 7
177
+ elsif line_code == 5 or line_code == 8
178
+ line_event = 9
179
+ elsif line_code == 6
180
+ line_event = 5
181
+ elsif line_code == 7
182
+ line_event = 6
183
+ else
184
+ raise StandardError, "Cannot map the following event: #{line_code}"
201
185
  end
202
186
 
187
+ # SEM Event Coding:
188
+ # 1 Slow Eye Movement
189
+ # 0 No Slow Eye Movement
190
+ if file_hash[:type] == :wake and line_code == 8
191
+ sem_event = 1
192
+ else
193
+ sem_event = 0
194
+ end
203
195
 
204
- previous_first_labtime = file_info[:first_labtime]
205
- previous_last_labtime = last_labtime
196
+ last_labtime = line_labtime
206
197
 
198
+ output_line = [subject_code.upcase, line_labtime.to_decimal, line_event, sleep_period, sem_event]
199
+ merged_file << output_line
207
200
  end
208
- merged_file.close
209
- MY_LOG.info "---- end #{subject_code}\n\n"
201
+
202
+
203
+ previous_first_labtime = file_info[:first_labtime]
204
+ previous_last_labtime = last_labtime
210
205
 
211
206
  end
207
+ merged_file.close
208
+ MY_LOG.info "---- end #{subject_code}\n\n"
209
+
212
210
  end
211
+ end
213
212
 
214
- def load_subject_list
215
- subject_info = {}
216
- Dir.foreach(LIST_DIR) do |file|
217
- next if file == '.' or file == '..'
218
- #MY_LOG.info "#{file}"
219
- csv_file = CSV.open("#{LIST_DIR}#{file}", {headers: true})
220
-
221
- # Match and Validate File Name
222
- matched_sc = /(.*)SLEEP\.csv/i.match(File.basename(csv_file.path))
223
- if matched_sc
224
- subject_code = matched_sc[1].upcase
213
+ def load_subject_list
214
+ subject_info = {}
215
+ Dir.foreach(LIST_DIR) do |file|
216
+ next if file == '.' or file == '..'
217
+ #MY_LOG.info "#{file}"
218
+ csv_file = CSV.open("#{LIST_DIR}#{file}", {headers: true})
219
+
220
+ # Match and Validate File Name
221
+ matched_sc = /(.*)SLEEP\.csv/i.match(File.basename(csv_file.path))
222
+ if matched_sc
223
+ subject_code = matched_sc[1].upcase
224
+ else
225
+ next
226
+ end
227
+
228
+ subject_info[subject_code] = []
229
+ csv_file.each do |row|
230
+ file_info = {}
231
+ pattern = /(.*)\.man/i.match(row[0])
232
+
233
+ matched_time = /(\d\d):(\d\d):(\d\d):(\d\d\d)/.match(row[1])
234
+ if matched_time
235
+ file_info[:start_time] = {hour: matched_time[1].to_i, min: matched_time[2].to_i, sec: matched_time[3].to_i}
225
236
  else
237
+ MY_LOG.error "No Valid Start Time Found: #{row}"
226
238
  next
227
239
  end
228
240
 
229
- subject_info[subject_code] = []
230
- csv_file.each do |row|
231
- file_info = {}
232
- pattern = /(.*)\.man/i.match(row[0])
233
-
234
- matched_time = /(\d\d):(\d\d):(\d\d):(\d\d\d)/.match(row[1])
235
- if matched_time
236
- file_info[:start_time] = {hour: matched_time[1].to_i, min: matched_time[2].to_i, sec: matched_time[3].to_i}
237
- else
238
- MY_LOG.error "No Valid Start Time Found: #{row}"
239
- next
240
- end
241
-
242
- matched_time = /(\d\d):(\d\d):(\d\d):(\d\d\d)/.match(row[4])
243
- if matched_time
244
- file_info[:last_line_time] = {hour: matched_time[1].to_i, min: matched_time[2].to_i, sec: matched_time[3].to_i}
245
- else
246
- MY_LOG.error "No Valid End Time Found: #{row}"
247
- next
248
- end
241
+ matched_time = /(\d\d):(\d\d):(\d\d):(\d\d\d)/.match(row[4])
242
+ if matched_time
243
+ file_info[:last_line_time] = {hour: matched_time[1].to_i, min: matched_time[2].to_i, sec: matched_time[3].to_i}
244
+ else
245
+ MY_LOG.error "No Valid End Time Found: #{row}"
246
+ next
247
+ end
249
248
 
250
- file_info[:start_labtime] = row[2].to_f
251
- file_info[:last_line_number] = row[3].to_i
252
- file_info[:last_line_labtime] = row[5].to_f
249
+ file_info[:start_labtime] = row[2].to_f
250
+ file_info[:last_line_number] = row[3].to_i
251
+ file_info[:last_line_labtime] = row[5].to_f
253
252
 
254
- if pattern
255
- file_info[:pattern] = pattern[1]
256
- subject_info[subject_code] << file_info
253
+ if pattern
254
+ file_info[:pattern] = pattern[1]
255
+ subject_info[subject_code] << file_info
257
256
 
258
- # Determine if sleep or wake file
259
- raise StandardError, "CAN'T DETERMINE SP/WP (none match): #{pattern[1]}" unless (/_sp?\d/i.match(pattern[1]) or /_wp?\d/i.match(pattern[1]))
260
- raise StandardError, "CAN'T DETERMINE SP/WP (both match): #{pattern[1]}" if (/_sp?\d/i.match(pattern[1]) and /_wp?\d/i.match(pattern[1]))
257
+ # Determine if sleep or wake file
258
+ raise StandardError, "CAN'T DETERMINE SP/WP (none match): #{pattern[1]}" unless (/_sp?\d/i.match(pattern[1]) or /_wp?\d/i.match(pattern[1]))
259
+ raise StandardError, "CAN'T DETERMINE SP/WP (both match): #{pattern[1]}" if (/_sp?\d/i.match(pattern[1]) and /_wp?\d/i.match(pattern[1]))
261
260
 
262
- if /_sp?\d/i.match(pattern[1])
263
- file_info[:type] = :sleep
264
- elsif /_wp?\d/i.match(pattern[1])
265
- file_info[:type] = :wake
266
- else
267
- raise StandardError, "Didn't match any SP/WP..."
268
- end
261
+ if /_sp?\d/i.match(pattern[1])
262
+ file_info[:type] = :sleep
263
+ elsif /_wp?\d/i.match(pattern[1])
264
+ file_info[:type] = :wake
269
265
  else
270
- MY_LOG.info "No Valid File Name Found: #{row}"
271
- next
266
+ raise StandardError, "Didn't match any SP/WP..."
272
267
  end
268
+ else
269
+ MY_LOG.info "No Valid File Name Found: #{row}"
270
+ next
273
271
  end
274
- #MY_LOG.info subject_info[subject_code]
275
272
  end
276
- #MY_LOG.info subject_info.inspect
277
- subject_info
278
- end
279
-
280
- def get_subject_year(file_list)
281
- years = file_list.map do |h|
282
- matched_date = /_(\d\d)(\d\d)(\d\d)_/.match(h[:pattern])
283
- matched_date ? matched_date[3] : nil
284
- end
285
- years.delete_if {|x| x.nil? }
286
- years = years.uniq
287
-
288
- raise StandardError, "More than one unique year found in files: #{years}" if years.length > 1
289
- year = years.first.to_i
290
- year > 30 ? year + 1900 : year + 2000
273
+ #MY_LOG.info subject_info[subject_code]
291
274
  end
275
+ #MY_LOG.info subject_info.inspect
276
+ subject_info
292
277
  end
293
278
 
279
+ def get_subject_year(file_list)
280
+ years = file_list.map do |h|
281
+ matched_date = /_(\d\d)(\d\d)(\d\d)_/.match(h[:pattern])
282
+ matched_date ? matched_date[3] : nil
283
+ end
284
+ years.delete_if {|x| x.nil? }
285
+ years = years.uniq
294
286
 
287
+ raise StandardError, "More than one unique year found in files: #{years}" if years.length > 1
288
+ year = years.first.to_i
289
+ year > 30 ? year + 1900 : year + 2000
290
+ end
295
291
  end
296
292
 
293
+
297
294
  =begin
298
295
  path: /home/pwm4/Windows/tdrive/IPM/Modafinil_FD_42.85h/
299
296
  path: /usr/local/htdocs/access/lib/data/etl/klerman_merge_man_files/file_list
data/tasci_merger.gemspec CHANGED
@@ -1,7 +1,7 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'tasci_merger'
3
- s.version = '0.3.1'
4
- s.date = '2018-03-19'
3
+ s.version = '0.3.2'
4
+ s.date = '2018-07-12'
5
5
  s.summary = "Merger utility for TASCI scored sleep files."
6
6
  s.description = "Merger utility for TASCI scored sleep files, built for the Division of Sleep and Circadian Disorders at BWH."
7
7
  s.authors = ["Piotr Mankowski", "Chenxi Gao"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tasci_merger
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Piotr Mankowski
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2018-03-19 00:00:00.000000000 Z
12
+ date: 2018-07-12 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: activesupport