crossfilter icon indicating copy to clipboard operation
crossfilter copied to clipboard

Using reduce function to find max of group

Open cambridgemike opened this issue 14 years ago • 15 comments

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?

cambridgemike avatar May 11 '12 00:05 cambridgemike

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.

cambridgemike avatar May 21 '12 19:05 cambridgemike

Posted a possible solution on SO. Hopefully it's more efficient than re-running the group/reduce.

richardpoole avatar Jul 01 '12 13:07 richardpoole

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.

jasondavies avatar Feb 28 '13 23:02 jasondavies

Fixed in #61.

jasondavies avatar Mar 01 '13 11:03 jasondavies

On second thoughts, we can probably find a way to support this without having to recompute groups from scratch.

jasondavies avatar Mar 18 '13 08:03 jasondavies

@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

undefined

                                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 !

pdaems avatar Sep 14 '13 14:09 pdaems

Making reduceRemove optional never made it into a release, as it would be preferable to avoid recomputing groups from scratch.

jasondavies avatar Sep 14 '13 22:09 jasondavies

@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

pdaems avatar Sep 15 '13 08:09 pdaems

Ah, you need to pass null, e.g. .reduce(add, null, initial).

jasondavies avatar Sep 15 '13 09:09 jasondavies

@jasondavies Alright, thanks works great now !

pdaems avatar Sep 15 '13 09:09 pdaems

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.

esjewett avatar Sep 17 '13 19:09 esjewett

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.

jasondavies avatar Sep 18 '13 11:09 jasondavies

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.

esjewett avatar Sep 18 '13 14:09 esjewett

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.

gordonwoodhull avatar Feb 26 '16 21:02 gordonwoodhull

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!

RandomEtc avatar Mar 14 '16 02:03 RandomEtc