Skip to content

Commit

Permalink
more stats.
Browse files Browse the repository at this point in the history
  • Loading branch information
ripsawridge committed Dec 9, 2014
1 parent 2003f3e commit 11a6439
Show file tree
Hide file tree
Showing 2 changed files with 9,121 additions and 15 deletions.
143 changes: 128 additions & 15 deletions climbstats/climbstats.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,119 @@ function getData(data_in, tabletop) {
if (err) {
console.log(err);
} else {
ProcessData(data);
ProcessData(data_in);
}
});
}


function CreateStats(array) {
var res = {
mean: 0.0,
max: 0.0
};
var accum = 0.0;

// Compute the mean
for (var i = 0; i < array.length; i++) {
accum += array[i];
}
res.mean = accum / array.length;

// max
array.sort();
if (array.length > 0) {
res.max = array[array.length - 1];
}

return res;
}


function DateSort(a, b) {
a = new Date(a.date);
b = new Date(b.date);
if (a < b) return -1;
else if (a > b) return 1;
return 0;
}

// Creates an array of objects with { date, values[] }, sorted by date.
function AggregateIndexesByDate(data, filter_function) {
var aggregates = [];
// Aggregate data
for (var i = 0; i < data.length; i++) {
var entry = data[i];
var choose = true;
if (filter_function !== undefined) {
choose = filter_function(entry);
}

if (choose === true) {
if (aggregates[entry.date] === undefined) {
aggregates[entry.date] = [];
}

aggregates[entry.date].push(entry.index);
}
}

// Interpolate holes.
for (var i = 0; i < data.length; i++) {
var entry = data[i];
if (aggregates[entry.date] === undefined) {
// If you didn't climb anything clean on this day, you get credit for
// having climbed one grade 4 UIAA climb.
var index = ClimbGrades.ToIndex("4", ClimbGrades.GRADE.UIAA);
aggregates[entry.date] = [index];
}
}

var result = [];
for (i in aggregates) {
var obj = {};
obj.date = i;
obj.values = aggregates[i].slice(0);
result.push(obj);
}

result.sort(DateSort);
return result;
}


function AggregateData(data, filter_function) {
// aggregates will be an array, sorted by date with objects
// { date, values } where values are all that passed the filter
// function.
var aggregates = AggregateIndexesByDate(data, filter_function);
var stats = aggregates.map(function(entry) {
var obj = CreateStats(entry.values);
obj.date = entry.date;
return obj;
});
return stats;
}


function FromIndex(index) {
index = Math.round(index);
return ClimbGrades.FromIndex(index, ClimbGrades.GRADE.UIAA);
}

// stats is a sorted array of objects like { date, max, mean, done_max, done_mean }
function PrintStats(stats) {
for (var i = 0; i < stats.length; i++) {
var entry = stats[i];
var max = FromIndex(entry.max);
var done_max = FromIndex(entry.done_max);
console.log(entry.date + " " + FromIndex(entry.mean) + " " +
max + " " + FromIndex(entry.done_mean) + " " +
done_max);
}
}


function ProcessData(data) {
// First, turn the climbing grades into a proper ordering.
data = data.map(function(entry) {
Expand All @@ -34,24 +142,29 @@ function ProcessData(data) {
// mean grade (climbed or attempted).
// median grade (climbed or attempted).

// Order by date.
data.sort(function(a, b) {
a = new Date(a.date);
b = new Date(b.date);
if (a < b) return -1;
else if (a > b) return 1;
return 0;
var stats_full = AggregateData(data);

var stats_done = AggregateData(data, function(entry) {
var choose = entry.notes.trim() === "";
return choose;
});


// Print out simple data.
for (var i = 0; i < data.length; i++) {
var entry = data[i];
if (entry.notes.trim() === "") {
console.log(entry.date + " " + entry.index);
}
// merge done data with all attempt data.
for (var i = 0; i < stats_full.length; i++) {
var entry = stats_full[i];
var done_entry = stats_done[i];
entry.done_mean = done_entry.mean;
entry.done_max = done_entry.max;
}

// We only want to print data from the last 6 months.
var filterDate = new Date();
filterDate.setMonth(filterDate.getMonth() - 6);
stats_done = stats_full.filter(function(entry) {
var d = new Date(entry.date);
return d > filterDate;
});
PrintStats(stats_full);
}


Expand Down
Loading

0 comments on commit 11a6439

Please sign in to comment.