Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions install_requirements.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#/bin/bash
echo 'Installing requirements:'
sudo apt-get install sqlite3 default-jre octave
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
sudo apt-get install sqlite3
default-jre
octave
7 changes: 3 additions & 4 deletions scripts/SleepAnalysis.java → scripts/Analyze.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
* grouped by day. Main sleep is sleep counted betweeen 00:00 and
* 12:00, naps at any other time of day.
*/
public class SleepAnalysis {
public class Analyze {

// Date stamp, eg 20170703
private static SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
Expand All @@ -18,9 +18,8 @@ public class SleepAnalysis {

public static void main (String[] arg) throws Exception {


String line,date,lastDate="";
int activityType,mainSleep=0,napSleep=0, hour;
String line, date, lastDate = '';
int activityType, mainSleep = 0, napSleep = 0, hour;
int intensity, steps, hr;
int sigma_hr = 0;
int sigma_hr2 = 0;
Expand Down
103 changes: 103 additions & 0 deletions scripts/GlobalAnalysisUntested.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
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;
}
}
}

5 changes: 2 additions & 3 deletions scripts/histogram.m
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
# Command line:
# octave histogram.m data.dat > /dev/null
#
# Joe Desbonnet,
# Original script by Joe Desbonnet,
# jdesbonnet@gmail.com
# 3 Jul 2017

# July 3rd, 2017

datain = argv(){1}
dataout = argv(){2}
Expand Down
37 changes: 20 additions & 17 deletions scripts/plot_miband2.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
#!/bin/bash

DB=$1
USER=$2
NREC=$3
# Input: .dat file location with MiBand2 data, Number of records to plot

./read_gadgetbridge_db.sh $DB $USER > miband2.dat
tail -n ${NREC} miband2.dat > t.dat
FILENAME=$1
NREC=$2

# Following command is valid if the data hasn't been extracted yet. To follow my standard,
# load the data already from ../../data/<file_name>.dat
# ./read_gadgetbridge_db.sh $DB $USER > miband2.dat
tail -n ${NREC} ${FILENAME} > t.dat # +${REC} used to output all lines.

gnuplot <<EOF

set title "Activity and sleeping heart rate data from Mi Band 2"
set title "Activity and Heart Rate data from Mi Band 2"

set terminal pngcairo size 1400,600
set output "sleep.png"
Expand All @@ -18,20 +21,20 @@ set grid
set xdata time
set timefmt "%s"
set format x "%H:%M\n%a"
set yrange [0:130]
set ylabel "Beats per minute (bpm)"

set yrange [0:200]
set ylabel "Beats Per Minute (bpm)"
set datafile missing '255'

# Note: lines don't work with the NaNs
plot 't.dat' using 1:((\$7<40||\$7>150)?NaN:\$7) with linespoints title "Sleeping heart rate (bpm)" , \
'' using 1:4 with impulses lt 3 title 'Activity' , \
'' using 1:5 with impulses lt 2 title 'Steps', \
'' using 1:6
#plot 't.dat' using 1:7 with linespoints title "Sleeping heart rate (bpm)" , '' using 1:4 with impulses lt 3 title 'Activity'
#plot 't.dat' using 1:((\$7<40||\$7>150)?NaN:\$7) with linespoints title "bpm" , \
#'' using 1:4 with impulses lt 3 title 'Activity' , \
#'' using 1:5 with impulses lt 2 title 'Steps', \
#'' using 1:6
#plot 't.dat' using 1:7 with linespoints title "bpm" , '' using 1:4 with impulses lt 3 title 'Activity'

EOF

java SleepAnalysis miband2.dat > sleep.dat
gnuplot sleep_summary.gnuplot
plot 't.dat' using 1:((\$7<40||\$7>150)?NaN:\$7) with linespoints title "bpm" , '' using 1:4 with impulses lt 3 title 'Activity'
EOF
java Analyze ${FILENAME} > sleep.dat
#gnuplot sleep_summary.gnuplot. Uncommented because I don't use the sleep summary.

12 changes: 7 additions & 5 deletions scripts/read_gadgetbridge_db.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
#
# Extract Mi Band 2 data from SQLite DB generated by GadgetBridge app.
#
# Parameter: SQLLite database file
# Input: SQLLite database file, DEVICE ID (in my case, DEVICE_ID = 1).
# I checked it by looking at the database file, under the DEVICE_ATTRIBUTES table.
# Output: tab separated output sent to stdout
#
# Tested with GadgetBridge version 0.19.2 (May 2017). The GadgetBridge
Expand All @@ -13,7 +14,6 @@
# Source tarball can be downloaded from here:
# https://f-droid.org/packages/nodomain.freeyourgadget.gadgetbridge/
#
#
# Output is a tab separated file with columns:
#
# Column 1: time (unix epoch)
Expand All @@ -32,12 +32,14 @@
#
# Joe Desbonnet
# 2 July 2017
# Modified by Ignacio Guillermo Martínez Rincón,
# 23caj23@gmail.com
# October 3rd, 2018

DB=$1
DEVICE_ID=$2


# I expect all device ID's to be = 1.
sqlite3 $DB <<EOF
.separator " "
SELECT * FROM MI_BAND_ACTIVITY_SAMPLE WHERE DEVICE_ID=${DEVICE_ID} ORDER BY TIMESTAMP;
SELECT * FROM MI_BAND_ACTIVITY_SAMPLE WHERE DEVICE_ID=1 ORDER BY TIMESTAMP;
EOF
8 changes: 0 additions & 8 deletions scripts/sleep_mean.m

This file was deleted.

96 changes: 0 additions & 96 deletions scripts/sleep_summary.gnuplot

This file was deleted.

31 changes: 31 additions & 0 deletions wrapper_scripts/read_db.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/bash

# Input: database file name + output file name
# Example: ./read_db 1 1

DB=$1
OUTPUT=$2
DATEXTENSION='.dat'
DBEXTENSION='.db'

echo 'Checking formats...'

if [[ ${OUTPUT} = *".dat" ]]
then
echo 'Found .dat file'
echo ${OUTPUT}
else
OUTPUT=${OUTPUT}${DATEXTENSION}
echo ${OUTPUT}
fi

if [[ ${DB} = *'.db' ]]
then
echo 'Found .db file'
echo ${DB}
else
DB=${DB}${DBEXTENSION}
echo ${DB}
fi

../scripts/read_gadgetbridge_db.sh ../../databases/${DB} >> ../../data/${OUTPUT}