1

I am trying to use both the .std() and the .custom() function in the reductio library.

My code is as follows for the reductio part:

dims.theme1 = myCrossfilter.dimension(function(d) {return(d.theme1);});
groups.theme1 = dims.theme1.group();

var reducer = reductio()
    .custom({initial:reduceInit,add:reduceAdd,remove:reduceRemove})
    .std("pl");
reducer(groups.theme1);

My code for the custom functions is :

reduceAdd = function(p,v) {
    if (!p.fundsData.hasOwnProperty(v.AdyneAccount)) {
        p.fundsData[v.AdyneAccount]=0;
    }
    if (!p.stratsData.hasOwnProperty(v.Strategy)) {
        p.stratsData[v.Strategy]=0;
    }
    p.fundsData[v.AdyneAccount]+=+v.plfund;
    p.stratsData[v.Strategy]+=+v.plstrat;
    p.value+=+v.pl;
    return(p);
};
reduceRemove = function(p,v) {
    p.fundsData[v.AdyneAccount]-=+v.plfund;
    p.stratsData[v.Strategy]-=+v.plstrat;
    p.value-=+v.pl;
    return(p);
};
reduceInit = function(p,v) {
    return({
        value:0,
        fundsData:{},
        stratsData:{}
    });
};

I would expect my result (by doing groups.theme1.all()[0]) to look like the below (the values I have put in are random for this example) :

{
    "key": "theTheme",
    "value": {
        "value": 10,
        "fundsData": {
            "a": 10,
            "b": 5,
            "c": 4
        },
        "stratsData": {
            "somename": 8
        },
        "count": null,
        "sum": null,
        "sumOfSq": null,
        "std": 0
    }
}

And it does but it doesn't produce the values for count, sum and sumOfSq (so for std neither of course).

When I run .std("pl") by itself without my custom function on the exact same set of records, it works as expected. I do not understand why the addition of a custom function would prevent the correct calculation for the .std("pl") part.

All help welcome !

Chapo
  • 2,563
  • 3
  • 30
  • 60

1 Answers1

1

It looks to me like this is a bug in Reductio. The test names indicate that this shouldn't interfere with other aspects of reducers, but the content of the test makes clear that it does wipe them out.

https://github.com/crossfilter/reductio/blob/cd99f5043990a838b7e04ea86dbae3c1a1203119/test/custom.spec.js#L48

I've created an issue for this. No idea when I'll be able to get to it though. You'll probably want to just implement a normal Crossfilter custom reducer for this until I can fix it or someone sends a pull request.

Ethan Jewett
  • 6,002
  • 16
  • 25
  • Ok thank you for your answer. So define my custom reducer the 'normal' way and add the reductio object to that group? Or just create 2 distinct dimensions and groups? – Chapo Apr 09 '18 at 22:40
  • At the moment you'll have to create 2 distinct dimensions, or add whatever you have in your Reductio reducer to your "normal" custom reducer. Sorry about that :( – Ethan Jewett Apr 10 '18 at 17:00
  • No worries at all ! reductio is quite nice and I've already had to resort to the 2 dimension trick to get dc.js graph to behave properly anyway. – Chapo Apr 11 '18 at 02:05