Using reduce function to find max of group
Is it possible to use the reduce function to find the max of the group? It seems the problem would be that when records are removed, if the record removed happened to be that contributing the current maximum, you have have to re-run the the map/reduce in order to find the new maximum.
Is there a way to force the map/reduce to happen again?
I just wanted to check in and see if there is any suggestion on how to accomplish the above. I'm able to re-run the group/reduce but this is causing a performance nightmare.
Are there any other avenues for developers to get support? There is an open question on SO, http://stackoverflow.com/questions/10509478/using-crossfilter-is-it-possible-to-track-max-min-when-grouping but that's all I could find.
Posted a possible solution on SO. Hopefully it's more efficient than re-running the group/reduce.
You could keep a sorted array per group key, but then the overhead of maintaining this may be greater than simply recomputing the min/max per group key entirely from scratch. Perhaps the lack of a reduceRemove function could force each group to be recomputed from scratch if anything is removed from that group, while still allowing incremental updates when records are added.
Fixed in #61.
On second thoughts, we can probably find a way to support this without having to recompute groups from scratch.
@jasondavies
Im currently calculating the maximum of my group like:
var barGroup = datedimension.group().reduce(
function (p, v) {
p.values.push(+v[6]);
if (p.max < +v[6]) {
p.max = +v[6];
}
return p;
},
function (p, v) {
var index = p.values.indexOf(+v[6]);
if (index >= 0) {
p.values.splice(index, 1);
p.values.sort(function (a, b) { return a - b });
}
if (p.values.length > 0) {
p.max = p.values[p.values.length - 1];
} else {
p.max = 0;
}
return p;
},
function () {
return { max: 0, values: new Array() };
}
);
However the reduceRemoval takes a long time to process each time i use a filter on a dimension. Whenever i try and use the group.reduce function without reduceRemoval i get the following error

var barGroup = datedimension.group().reduce(
function (p, v) {
p.values.push(+v[6]);
if (p.max < +v[6]) {
p.max = +v[6];
}
return p;
},
function () {
return { max: 0, values: new Array() };
}
);
Any idea's ? Thanks in advance !
Making reduceRemove optional never made it into a release, as it would be preferable to avoid recomputing groups from scratch.
@jasondavies I get the same error by using the version with the optional reduceRemoval in it.
https://raw.github.com/square/crossfilter/6c20e31090a6f96962f25bcedc891b9827777a8c/crossfilter.js
Ah, you need to pass null, e.g. .reduce(add, null, initial).
@jasondavies Alright, thanks works great now !
Didn't see this and I think I've basically duplicated it in #92 . However, correct me if I'm wrong, but does this solution properly calculate a maximum if a group is created on a crossfilter that already has filters applied? I don't think so, but I'm not sure.
If you are referring to the optional reduceRemove in #61 then no, they are different. Making reduceRemove optional means that removing records (due to filters) forces a full rebuild of the group index (using reduceAdd only). Thus it will work correctly with filters, but we decided it would be preferable to find a performant solution instead for finding maxima (e.g. using a heap), since rebuilding the group index from scratch is slow.
I see. Yes, that is a different use-case - finding filtered max/min vs my need in #92 for finding unfiltered max/min for new groups after a filter has already been applied.
I provided an example here, which keeps a list of rows for each bin and then can compute min/max/median: http://dc-js.github.io/dc.js/examples/complex-reduce.html
Source here: https://github.com/dc-js/dc.js/blob/develop/web/examples/complex-reduce.html
It would be more efficient if crossfilter provided access to the underlying rows for each bin, instead of having to track those externally. But other than that, I think it's an okay solution to this problem.
As discussed in #151 an active fork is being developed in a new Crossfilter Organization. Please take further discussion there (if you haven't already) where it should be warmly welcomed by the new maintainers. Cheers!