-
Notifications
You must be signed in to change notification settings - Fork 2
/
bp.rb
executable file
·254 lines (176 loc) · 5.56 KB
/
bp.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#!/usr/bin/env ruby
################################################
###
## File: bp.rb
## Desc: quick and dirty (really dirty)
## Extract bp data from eMail and format into report
#
require 'pathname' # STDLIB
require 'amazing_print' # Pretty print Ruby objects with proper indentation and colors
require 'date' # STDLIB
require 'debug_me' # A tool to print the labeled value of variables.
include DebugMe
class BloodPressureMeasurement
attr_accessor :systolic
attr_accessor :diastolic
attr_accessor :rate
attr_accessor :comment
def initialize( systolic, diastolic, rate, comment )
@systolic = systolic
@diastolic = diastolic
@rate = rate
@comment = comment
# debug_me{[ :systolic, :diastolic, :rate, :comment ]}
end
def to_s
"#{@systolic} / #{@diastolic} @#{@rate} #{@comment}"
end
end # of class BloodPressureMeasurement
BP_ENTRY_REGEX = /^(\d+)\D(\d+)\D+(\d+)\s*(.*)$/
class String
def to_bpm
data = self.match(BP_ENTRY_REGEX).to_a
# debug_me{[ :data ]}
BloodPressureMeasurement.new( data[1].to_i, data[2].to_i, data[3].to_i, data[4] ) unless data.nil?
end
end # end of class String
if ARGV.empty? or '-h' == ARGV.first or '--help' == ARGV.first
puts <<~EOS
Usage: np.rb file_path
Where: file_path is a path to a file of eMail messages
EOS
exit(-1)
end
email_path = Pathname.new ARGV.first
unless email_path.exist?
puts <<~EOS
ERROR: File does not exist.
#{email_path}
EOS
exit(-1)
end
observations = Hash.new
datetime = nil
email_path.readlines.each do |a_line|
a_line.chomp!.strip!
if a_line.start_with?('Date')
datetime = DateTime.parse(a_line[6,99]) # strings index at zero, 7th character begins the data string
observations[datetime] = nil
end
if 100 < a_line[0,3].to_i
observations[datetime] = a_line.to_bpm
end
end # email_path.readlines.each do |a_line|
out_hash = Hash.new
observations.each_pair do |k,v|
dow = k.strftime('%a')
o_date, o_time = k.to_time.to_s.split(' ')
key = "#{o_date} #{dow} #{o_time}"
out_hash[key] = v
end # observations.each_pair do |k,v|
out_hash.sort.each do |entry|
puts "#{entry.join(' ')}"
end # out_hash.sort.each do |entry|
print "\n\n"
#################################################
## Draw a graph
require 'daru' # Data Analysis in RUby
require 'daru/plotly' # Data Analysis in RUby
include Daru::Plotly::Methods
require 'linefit' # LineFit is a linear regression math class.
# title:string The plot title
# data_points:array and array of [x,y] entries where x is data-time, y is BloodPressureMeasurement
# filter:proc used to filter data_points
# names:array array of symbols used to extract data from BloodPressureMeasurement
def generate_plot( title, data_points, filter, *names )
# debug_me{[ :title, :names ]}
line_fit = LineFit.new
line_fit2 = LineFit.new
x_labels = Hash.new
x_values = Array.new
y_values = Hash.new
names.each do |series_name|
y_values[series_name] = Array.new
end
e_no = 0
data_points.each do |entry|
# debug_me{[ :entry ]} if entry.last.rate.nil? || entry.last.rate < 50
a_date = entry.first
x_value = ((a_date.year - 2000)*100.0 + a_date.yday.to_f + a_date.hour.to_f / 24.0).round(2)
y_value = entry.last.send(names.first)
if filter.call(a_date)
x_labels[e_no] = x_value # a_date.strftime('%a')[0,1].downcase
x_values << e_no # x_value
e_no += 1
names.each do |series_name|
y_values[series_name] << entry.last.send(series_name)
end
end # if filter.call(a_date)
end
line_fit.setData( x_values, y_values[names[0]] )
line_fit2.setData( x_values, y_values[names[1]] )
y_values[:trend] = Array.new
y_values[:trend2] = Array.new
x_values.each do |x|
y_values[:trend] << line_fit.forecast(x)
y_values[:trend2] << line_fit2.forecast(x)
end
# TODO: replace data stuff with a data_frame from "daru"
d8_index = Daru::DateTimeIndex.date_range(
start: '2018-4-1',
end: '2018-5-31',
freq: 'D'
)
values = y_values
values[:x] = x_values
cat_index = Daru::CategoricalIndex.new(x_values)
data_frame = Daru::DataFrame.new(values, index: cat_index)
puts data_frame.data
graph = plot(
data_frame,
x: :x,
y: names+[:trend, :trend2],
layout: {
title: title,
xaxis: { title: 'Pbservations in DateTime Order' },
yaxis: { title: 'Measurement' }
},
)
graph.generate_html
end # of def generate_plot( title, data_points, filter, *names )
everything = Proc.new do |d8_time|
true
end
morning = Proc.new do |d8_time|
d8_time.hour <= 12
end
afternoon = Proc.new do |d8_time|
(12..18).include? d8_time.hour
end
evening = Proc.new do |d8_time|
d8_time.hour >= 19
end
# observations is a hash, turn it into an sorted array
my_data = observations.sort
generate_plot( 'BP History',
my_data,
everything,
:systolic, :diastolic, :rate
)
=begin
generate_plot( 'BP History Morning',
my_data,
morning,
:systolic, :diastolic, :rate
)
generate_plot( 'BP History Afternoon',
my_data,
afternoon,
:systolic, :diastolic, :rate
)
generate_plot( 'BP History Evening',
my_data,
evening,
:systolic, :diastolic, :rate
)
=end