-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathGlobalAnalysisUntested.java
More file actions
103 lines (83 loc) · 2.58 KB
/
Copy pathGlobalAnalysisUntested.java
File metadata and controls
103 lines (83 loc) · 2.58 KB
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
import java.io.*;
import java.text.*;
/**
* Analyze MiBand2 data, but globally; not just the sleep hours.
*/
public class SleepAnalysis {
// Date stamp, eg 20170703
private static SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
// Just the hour
private static SimpleDateFormat hf = new SimpleDateFormat("H");
private static final int SLEEP = 112;
public static void main (String[] arg) throws Exception {
String line, date, lastDate = '';
int activityType, mainSleep = 0, napSleep = 0, hour;
int intensity, steps, hr;
int sigma_hr = 0;
int sigma_hr2 = 0;
int sigma_intensity = 0;
int sigma_steps = 0;
int hr_min = 999;
int hr_max = 0;
int hr_count = 0;
long time;
LineNumberReader r = new LineNumberReader(new FileReader(arg[0]));
System.out.println ("# date main_sleep_hours nap_hours hr_mean hr_sd hr_min hr_max");
while ( (line = r.readLine()) != null) {
String[] p = line.split(" ");
time = Long.parseLong(p[0]);
date = df.format(time*1000);
hour = Integer.parseInt(hf.format(time*1000));
if (!date.equals(lastDate) && lastDate.length()>0) {
// MiBand records are every 1 minute
double mainSleepHours = (double)mainSleep / 60.0;
double napSleepHours = (double)napSleep / 60.0;
double n = (double)hr_count;
double hr_mean = (double)sigma_hr / n;
double hr_sd = Math.sqrt( (sigma_hr2 - (sigma_hr*sigma_hr)/n) / (n-1) );
double meanSleepIntensity = mainSleep == 0 ? 0 : (double)sigma_intensity / (double)mainSleep;
System.out.println (
(df.parse(lastDate).getTime()/1000 + 6*3600)
+ " " + lastDate
+ " " + String.format("%.1f",mainSleepHours)
+ " " + String.format("%.1f",napSleepHours)
+ " " + String.format("%.1f",hr_mean)
+ " " + String.format("%.2f",hr_sd)
+ " " + hr_min
+ " " + hr_max
+ " " + String.format("%.2f", meanSleepIntensity)
+ " " + sigma_steps
);
mainSleep = napSleep = 0;
sigma_hr = sigma_hr2 = 0;
hr_count = 0;
hr_min = 999;
hr_max = 0;
sigma_intensity = 0;
sigma_steps = 0;
}
intensity = Integer.parseInt(p[3]);
steps = Integer.parseInt(p[4]);
activityType = Integer.parseInt(p[5]);
hr = Integer.parseInt(p[6]);
sigma_steps += steps;
if (activityType == SLEEP) {
mainSleep++;
if (hr != 255) {
// Measure mean and stddev heart rate during main sleep
sigma_hr += hr;
sigma_hr2 += hr*hr;
if (hr > hr_max) {
hr_max = hr;
}
if (hr < hr_min) {
hr_min = hr;
}
hr_count++;
}
sigma_intensity += intensity;
}
lastDate = date;
}
}
}