forked from arielf/weight-loss
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lifestyle-csv2vw
executable file
·90 lines (76 loc) · 2.17 KB
/
lifestyle-csv2vw
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
#!/usr/bin/perl -w
#
# Generate a VW training-set from our raw data CSV
#
use Scalar::Util qw(looks_like_number);
my $SepPat = qr{(?:\s*,\s*|\s+)};
my $Interactive = -t STDOUT;
# Default days-window
my $NDays = 1;
sub process_args() {
for my $arg (@ARGV) {
if (! -f $arg and $arg =~ /^\d$/) {
$NDays = $arg;
} else {
push(@files, $arg);
}
}
@ARGV = @files;
}
sub process_input() {
my $prev_weight;
my @daily = ();
while (<>) {
# Skip comments or header-line
next if (/^[#A-Za-z]/);
chomp;
# Windows line endings, just in case...
tr/\015//d;
my ($date, $weight, @factors) = split($SepPat);
next unless ((defined $date) and $date =~ /^\d/);
# Only generate a training set if everything is defined and
# we have a prior day weight to compare to
unless ((defined $weight) and looks_like_number($weight)) {
$weight = '' unless (defined $weight);
if ($Interactive) {
warn "$ARGV:$. weight: '$weight' is not a number - line ignored\n";
}
undef $prev_weight;
next;
}
#
# -- collect daily (gain + factors) data points in @daily
#
if ((defined $prev_weight) && scalar(@factors) > 0) {
my $gain = $weight - $prev_weight;
my @day_list = ($gain, @factors);
push(@daily, \@day_list);
}
$prev_weight = $weight;
}
#
# Output vw training-set
#
for (my $i = 0; $i < @daily; $i++) {
my $start = $i;
my $end = $start + $NDays - 1;
my $sum_gain = 0.0;
my @sum_factors = ();
for (my $j = $i; $j <= $end; $j++) {
#
# -- Aggregate consecutive days gains and factors - up to $NDays
#
my $day_list = $daily[$i];
my @gain_factors = @$day_list;
my ($gain, @factors) = @gain_factors;
$sum_gain += $gain;
push(@sum_factors, @factors);
printf "%.2f | @sum_factors\n", $sum_gain;
}
}
}
#
# -- main
#
process_args();
process_input();