diff --git a/install_requirements.sh b/install_requirements.sh new file mode 100755 index 0000000..eed81b9 --- /dev/null +++ b/install_requirements.sh @@ -0,0 +1,3 @@ +#/bin/bash +echo 'Installing requirements:' +sudo apt-get install sqlite3 default-jre octave \ No newline at end of file diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..55d6902 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,3 @@ +sudo apt-get install sqlite3 +default-jre +octave \ No newline at end of file diff --git a/scripts/SleepAnalysis.java b/scripts/Analyze.java similarity index 95% rename from scripts/SleepAnalysis.java rename to scripts/Analyze.java index d029fee..d346261 100644 --- a/scripts/SleepAnalysis.java +++ b/scripts/Analyze.java @@ -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"); @@ -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; diff --git a/scripts/GlobalAnalysisUntested.java b/scripts/GlobalAnalysisUntested.java new file mode 100644 index 0000000..8da1159 --- /dev/null +++ b/scripts/GlobalAnalysisUntested.java @@ -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; + } + } +} + diff --git a/scripts/histogram.m b/scripts/histogram.m index 0170e6c..297495d 100755 --- a/scripts/histogram.m +++ b/scripts/histogram.m @@ -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} diff --git a/scripts/plot_miband2.sh b/scripts/plot_miband2.sh index 68fa398..614d7a6 100755 --- a/scripts/plot_miband2.sh +++ b/scripts/plot_miband2.sh @@ -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/.dat +# ./read_gadgetbridge_db.sh $DB $USER > miband2.dat +tail -n ${NREC} ${FILENAME} > t.dat # +${REC} used to output all lines. gnuplot <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. diff --git a/scripts/read_gadgetbridge_db.sh b/scripts/read_gadgetbridge_db.sh index a0e7402..2313a57 100755 --- a/scripts/read_gadgetbridge_db.sh +++ b/scripts/read_gadgetbridge_db.sh @@ -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 @@ -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) @@ -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 <> ../../data/${OUTPUT} \ No newline at end of file