Exemplo n.º 1
0
def test_dimension_func(monkeypatch, ndx):
    monkeypatch.setattr(s, "DATA_COLUMNS", "{'close', 'open'}")

    dat = ndx

    fluctuation_eq = abs(VC("close") - VC("open"))

    month_dim = Dimension("month", group_type="sum", func=fluctuation_eq)

    assert 'Math.abs(d.close - d.open)' == month_dim.func

    expected_str = '''
    var month_dimension = facts.dimension(function(d){return d['month'];});
    
    var month_group = month_dimension.group().reduceSum(function(d){
        return Math.abs(d.close - d.open);
    });
    '''

    expected_str_replaced = str(expected_str).replace("\n", "").replace(
        " ", "").replace("\t", "")
    asserted_str_replaced = str(month_dim).replace("\n", "").replace(
        " ", "").replace("\t", "")

    assert expected_str_replaced == asserted_str_replaced
Exemplo n.º 2
0
def line_chart(monkeypatch, ndx):
    monkeypatch.setattr(s, "DATA_COLUMNS", "{'close', 'open'}")

    dat = ndx

    def get_month(x):
        return x.month

    dat["month"] = pd.to_datetime(dat.date)
    dat["month"] = dat.month.apply(get_month)

    total_eq = (VC("open") + VC("close")) / VC(2)
    total = VS("total", total_eq)

    avg_eq = round(VC("total") / VC("count"))
    avg = VS("avg", avg_eq)

    eqs = [total, avg]

    move_months_dim = NamedDimension(eqs,
                                     groupby=["month"],
                                     group_text="Monthly Index Average")

    line_chart = LineChart(
        "monthly-move-chart",
        move_months_dim,
        width=990,
        height=200,
        valueAccessor="avg",
    )

    return line_chart
Exemplo n.º 3
0
def test_avg_index_eq():

    avgIndex_eq = VC("sumIndex") / VC("count", fix_zero_division=True)
    avgIndex = VS("avgIndex", avgIndex_eq)

    expected_eq = 'p.count ? (p.sumIndex / p.count) : 0'
    expected = 'p.avgIndex = p.count ? (p.sumIndex / p.count) : 0;'

    assert expected_eq == str(avgIndex_eq)
    assert expected == str(avgIndex)
Exemplo n.º 4
0
def test_round_and_division_VS():
    avg_div = VC("total") / VC("count", fix_zero_division=True)
    avg_eq = round(avg_div)
    avg = VS("avg", avg_eq)

    expected_avg_div = 'p.count ? (p.total / p.count) : 0'
    expected_avg_eq = '(Math.round(p.count ? (p.total / p.count) : 0))'
    expected_avg = 'p.avg = Math.round(p.count ? (p.total / p.count) : 0);'

    assert str(avg_div) == expected_avg_div
    assert str(avg_eq) == expected_avg_eq
    assert str(avg) == expected_avg
Exemplo n.º 5
0
def test_move_months_dim(monkeypatch, ndx):
    monkeypatch.setattr(s, "DATA_COLUMNS", "{'close', 'open'}")

    expected = '''
    var month_group = month_dimension.group().reduce(
        function (p, v) {
            ++p.days;
            p.total += (v.open + v.close) / 2;
            p.avg = Math.round(p.total / p.days);
            return p;
        },
        function (p, v) {
            --p.days;
            p.total -= (v.open + v.close) / 2;
            p.avg = p.days ? Math.round(p.total / p.days) : 0;
            return p;
        },
        function () {
            return {days: 0, total: 0, avg: 0};
        }
    );
    '''

    dat = ndx

    def get_month(x):
        return x.month

    dat["month"] = pd.to_datetime(dat.date)
    dat["month"] = dat.month.apply(get_month)

    total_eq = (VC("open") + VC("close")) / VC(2)
    total = VS("total", total_eq)

    avg_eq = round(VC("total") / VC("count"))
    avg = VS("avg", avg_eq)

    eqs = [total, avg]

    move_months_dim = NamedDimension(eqs,
                                     groupby=["month"],
                                     group_text="Monthly Index Average")

    compare_strings(str(move_months_dim), expected)
Exemplo n.º 6
0
def test_nameddimension_VC(monkeypatch):
    monkeypatch.setattr(s, "DATA_COLUMNS", "{'close', 'open'}")
    absGain_eq = (VC("close") - VC("open")) * VC(100)
    fluctuation_eq = abs(VC("close") - VC("open"))
    sumIndex_eq = (VC("open") + VC("close")) / VC(2)
    avgIndex_eq = VC("sumIndex") / VC("count", fix_zero_division=True)
    percentageGain_eq = (VC("absGain") /
                         VC("avgIndex", fix_zero_division=True)) * VC(100)
    fluctuationPercentage_eq = (
        VC("fluctuation") / VC("avgIndex", fix_zero_division=True)) * VC(100)

    absGain = VS("absGain", absGain_eq)
    fluctuation = VS("fluctuation", fluctuation_eq)
    sumIndex = VS("sumIndex", sumIndex_eq)
    avgIndex = VS("avgIndex", avgIndex_eq)
    percentageGain = VS("percentageGain", percentageGain_eq)
    fluctuationPercentage = VS("fluctuationPercentage",
                               fluctuationPercentage_eq)

    eqs = [
        absGain,
        fluctuation,
        sumIndex,
        avgIndex,
        percentageGain,
        fluctuationPercentage,
    ]

    output = NamedDimension(columns=eqs, groupby=["year"])

    expected = """
            var year_group = year_dimension.group().reduce(
                /* callback for when data is added to the current filter results */
                function (p, v) {
                    ++p.count;
                    p.absGain += (v.close - v.open) * 100;
                    p.fluctuation += Math.abs(v.close - v.open);
                    p.sumIndex += (v.open + v.close) / 2;
                    p.avgIndex = p.count ? (p.sumIndex / p.count) : 0;
                    p.percentageGain = p.avgIndex ? (p.absGain / p.avgIndex) * 100 : 0;
                    p.fluctuationPercentage = p.avgIndex ? (p.fluctuation / p.avgIndex) * 100 : 0;
                    return p;
                },
                /* callback for when data is removed from the current filter results */
                function (p, v) {
                    --p.count;
                    p.absGain -= (v.close - v.open) * 100;
                    p.fluctuation -= Math.abs(v.close - v.open);
                    p.sumIndex -= (v.open + v.close) / 2;
                    p.avgIndex = p.count ? (p.sumIndex / p.count) : 0;
                    p.percentageGain = p.avgIndex ? (p.absGain / p.avgIndex) * 100 : 0;
                    p.fluctuationPercentage = p.avgIndex ? (p.fluctuation / p.avgIndex) * 100 : 0;
                    return p;
                },
                /* initialize p */
                function () {
                    return {
                        count: 0,
                        absGain: 0,
                        fluctuation: 0,
                        sumIndex: 0,
                        avgIndex: 0,
                        percentageGain: 0,
                        fluctuationPercentage: 0
                    };
                }
    );"""

    assert str(output).replace("\n", "").replace(" ", "").replace(
        "\t", "") == str(expected).replace("\n",
                                           "").replace(" ",
                                                       "").replace("\t", "")
Exemplo n.º 7
0
def test_VE_division_p_col(monkeypatch):
    monkeypatch.setattr(s, "DATA_COLUMNS", "{'close', 'open'}")
    output = VC("sumIndex") / VC("count", fix_zero_division=True)
    assert "p.count ? (p.sumIndex / p.count) : 0" == str(output)
Exemplo n.º 8
0
def test_VE_with_division(monkeypatch):
    monkeypatch.setattr(s, "DATA_COLUMNS", "{'close', 'open'}")
    output = (VC("open") + VC("close")) / VC(2)
    assert "((v.open + v.close) / 2)" == str(output)
Exemplo n.º 9
0
def test_bubble_chart(monkeypatch, ndx):
    monkeypatch.setattr(s, "DATA_COLUMNS", "{'close', 'open'}")

    dat = ndx

    absGain_eq = (VC("close") - VC("open")) * VC(100)
    fluctuation_eq = abs(VC("close") - VC("open"))
    sumIndex_eq = (VC("open") + VC("close")) / VC(2)
    avgIndex_eq = VC("sumIndex") / VC("count")
    percentageGain_eq = (VC("absGain") / VC("avgIndex")) * VC(100)
    fluctuationPercentage_eq = (VC("fluctuation") / VC("avgIndex")) * VC(100)

    absGain = VS("absGain", absGain_eq)
    fluctuation = VS("fluctuation2", fluctuation_eq)
    sumIndex = VS("sumIndex", sumIndex_eq)
    avgIndex = VS("avgIndex", avgIndex_eq)
    percentageGain = VS("percentageGain", percentageGain_eq)
    fluctuationPercentage = VS("fluctuationPercentage",
                               fluctuationPercentage_eq)

    columns = [
        absGain,
        fluctuation,
        sumIndex,
        avgIndex,
        percentageGain,
        fluctuationPercentage,
    ]

    # my_dict = {
    #     "absGain": absGain_eq,
    #     "fluctuation": fluctuation_eq,
    #     "sumIndex": sumIndex_eq,
    #     "avgIndex": avgIndex_eq,
    #     "percentageGain": percentageGain_eq,
    #     "fluctuationPercentage": fluctuationPercentage_eq,
    # }

    bubble_named_dimension = NamedDimension(columns=columns, groupby=["year"])

    def percentage(x):
        return f"percentage({x})"

    title = {
        "Index Gain: ": "absGain",
        "Index Gain in Percentage: ": percentage("percentageGain"),
        "Fluctuation / Index Ratio: ": percentage("fluctuationPercentage"),
    }

    bub_params = {
        "width": 990,
        "height": 250,
        "x": ScaleLinear([-2500, 2500]),
        "y": ScaleLinear([-100, 100]),
        "r": ScaleLinear([-0, 4000]),
        "colorAccessor": "absGain",
        "keyAccessor": "absGain",
        "valueAccessor": "percentageGain",
        "radiusValueAccessor": "fluctuationPercentage",
        "transitionDuration": 1500,
        "margins": Margin(10, 50, 30, 40),
        "maxBubbleRelativeSize": 0.3,
        "xAxisPadding": 500,
        "yAxisPadding": 100,
        "xAxisLabel": "Index Gain",
        "yAxisLabel": "Index Gain %",
        "label": Label("key"),
        "title": str(Title(title)),
        "yAxis": "tickFormat(function(v){return v+'%';})",
    }

    bubble_chart = BubbleChart("yearly-bubble-chart", bubble_named_dimension,
                               **bub_params)

    dc_documentation_string = r"""
        var bubble_chart_yearly_bubble_chart = dc.bubbleChart("#yearly-bubble-chart")
            .width(990) 
            .height(250)
            .transitionDuration(1500)
            .margins({top: 10, right: 50, bottom: 30, left: 40})
            .dimension(year_dimension)
            .group(year_group)
            .colors(d3.schemeRdYlGn[9])
            .colorDomain([-500, 500]) 
            .colorAccessor(function (d) {
                return d.value.absGain;
            })
            .keyAccessor(function (d) {
                return d.value.absGain;
            })
            .valueAccessor(function (d) {
                return d.value.percentageGain;
            })
            .radiusValueAccessor(function (d) {
                return d.value.fluctuationPercentage;
            })
            .maxBubbleRelativeSize(0.3)
            .x(d3.scaleLinear().domain([-2500, 2500]))
            .y(d3.scaleLinear().domain([-100, 100]))
            .r(d3.scaleLinear().domain([0, 4000]))
            .elasticX(true) 
            .elasticY(true)
            .xAxisPadding(500) 
            .yAxisPadding(100)
            .renderHorizontalGridLines(true) 
            .renderVerticalGridLines(true) 
            .xAxisLabel('Index Gain') 
            .yAxisLabel('Index Gain %') 
            .renderLabel(true)
            .label(function (d) {
                return d.key;
            }) 
            .renderTitle(true)
            .title(function (d) {
                return [
                    d.key,
                    'Index Gain: ' + numberFormat(d.value.absGain),
                    'Index Gain in Percentage: ' + numberFormat(d.value.percentageGain) + '%',
                    'Fluctuation / Index Ratio: ' + numberFormat(d.value.fluctuationPercentage) + '%'].join('\n')
            });
            
            bubble_chart_yearly_bubble_chart.yAxis()
            .tickFormat(function (v) {
                return v + '%';
            });
        """

    bubble_chart_replaced = (str(bubble_chart).replace("\n", "").replace(
        " ", "").replace("\t", ""))

    dc_documentation_string_replaced = (str(dc_documentation_string).replace(
        "\n", "").replace(" ", "").replace("\t", ""))

    assert bubble_chart_replaced == dc_documentation_string_replaced
Exemplo n.º 10
0
def test_VE_with_constant(monkeypatch):
    monkeypatch.setattr(s, "DATA_COLUMNS", "{'close', 'open'}")
    output = (VC("close") - VC("open")) * VC(100)
    assert str(output) == "((v.close - v.open) * 100)"
Exemplo n.º 11
0
def test_VE_with_abs(monkeypatch):
    monkeypatch.setattr(s, "DATA_COLUMNS", "{'close', 'open'}")
    output = abs(VC("close") - VC("open"))
    assert str(output) == "(Math.abs(v.close - v.open))"
Exemplo n.º 12
0
def test_absgain_VS_string(monkeypatch):
    monkeypatch.setattr(s, "DATA_COLUMNS", "{'close', 'open'}")
    absGain_eq = (VC("close") - VC("open")) * VC(100)
    absGain = VS("absGain", absGain_eq)
    expected = "p.absGain -= (v.close - v.open) * 100;"
    assert str(absGain) == expected
Exemplo n.º 13
0
def test_moveChart(monkeypatch, ndx):
    monkeypatch.setattr(s, "DATA_COLUMNS", "{'close', 'open'}")

    dat = ndx

    def get_month(x):
        return x.month

    dat["month"] = pd.to_datetime(dat.date)
    dat["month"] = dat.month.apply(get_month)

    total_eq = (VC("open") + VC("close")) / VC(2)
    total = VS("total", total_eq)

    avg_eq = round(VC("total") / VC("count"))
    avg = VS("avg", avg_eq)

    eqs = [total, avg]

    move_months_dim = NamedDimension(eqs,
                                     groupby=["month"],
                                     group_text="Monthly Index Average")

    line_chart = LineChart(
        "monthly-move-chart",
        move_months_dim,
        width=990,
        height=200,
        valueAccessor="avg",
        legend=Legend(x=800, y=10, itemHeight=13, gap=5),
        renderArea=True,
        margins=Margin(top=30, right=50, bottom=25, left=40),
        xUnits="d3.timeMonths",
    )

    dc_documentation_string = """
    var line_chart_monthly_move_chart = dc.lineChart("#monthly-move-chart")
        .renderArea(true)
        .width(990)
        .height(200)
        .transitionDuration(1000)
        .margins({top: 30, right: 50, bottom: 25, left: 40})
        .dimension(month_dimension)
        .mouseZoomable(true)
        .x(d3.scaleTime().domain([new Date(1985, 0, 1), new Date(2012, 11, 31)]))
        .round(d3.timeMonth.round)
        .xUnits(d3.timeMonths)
        .elasticY(true)
        .renderHorizontalGridLines(true)
        .legend(dc.legend().x(800).y(10).itemHeight(13).gap(5))
        .brushOn(false) 
        .group(month_group, 'Monthly Index Average')
        .valueAccessor(function (d) {
            return d.value.avg;
        }) 
        .stack(monthlyMoveGroup, 'Monthly Index Move', function (d) {
            return d.value;
        })
        .title(function (d) {
            var value = d.value.avg ? d.value.avg : d.value;
            if (isNaN(value)) {
                value = 0;
            }
            return dateFormat(d.key) + numberFormat(value);
        });
    """

    line_chart_replaced = (str(line_chart).replace("\n", "").replace(
        " ", "").replace("\t", ""))
    dc_documentation_string_replaced = (str(dc_documentation_string).replace(
        "\n", "").replace(" ", "").replace("\t", ""))

    assert line_chart_replaced == dc_documentation_string_replaced
Exemplo n.º 14
0
def test_absgain_VE_string(monkeypatch):
    monkeypatch.setattr(s, "DATA_COLUMNS", "{'close', 'open'}")
    absGain = (VC("close") - VC("open")) * VC(100)
    expected = "((v.close - v.open) * 100)"
    assert str(absGain) == expected
Exemplo n.º 15
0
    gap=1,
    centerBar=True,
    xUnits='d3.timeMonths',
    margins=Margin(0, 50, 20, 40),
    renderHorizontalGridLines=False,
    x='d3.scaleTime().domain([new Date(1985, 0, 1), new Date(2012, 11, 31)])')

# .margins({top: 0, right: 50, bottom: 20, left: 40})
# .x(d3.scaleTime().domain([new Date(1985, 0, 1), new Date(2012, 11, 31)]))
# .round(d3.timeMonth.round)
# .xUnits(d3.timeMonths);

dat["month"] = pd.to_datetime(dat.date)
dat["month"] = dat.month.dt.month

total_eq = (VC("open") + VC("close")) / VC(2)
total = VS("total", total_eq)

avg_eq = round(VC("total") / VC("count"))
avg = VS("avg", avg_eq)

eqs = [total, avg]

move_months_dim = NamedDimension(eqs,
                                 groupby=["month"],
                                 group_text="Monthly Index Average")

str(move_months_dim)

line_chart = LineChart("monthly-move-chart",
                       move_months_dim,