def test_transforms(): # aggregate transform agg1 = alt.AggregatedFieldDef(**{"as": "x1", "op": "mean", "field": "y"}) agg2 = alt.AggregatedFieldDef(**{"as": "x2", "op": "median", "field": "z"}) chart = alt.Chart().transform_aggregate([agg1], ["foo"], x2="median(z)") kwds = dict(aggregate=[agg1, agg2], groupby=["foo"]) assert chart.transform == [alt.AggregateTransform(**kwds)] # bin transform chart = alt.Chart().transform_bin("binned", field="field", bin=True) kwds = {"as": "binned", "field": "field", "bin": True} assert chart.transform == [alt.BinTransform(**kwds)] # calcualte transform chart = alt.Chart().transform_calculate("calc", "datum.a * 4") kwds = {"as": "calc", "calculate": "datum.a * 4"} assert chart.transform == [alt.CalculateTransform(**kwds)] # density transform chart = alt.Chart().transform_density("x", as_=["value", "density"]) kwds = {"as": ["value", "density"], "density": "x"} assert chart.transform == [alt.DensityTransform(**kwds)] # filter transform chart = alt.Chart().transform_filter("datum.a < 4") assert chart.transform == [alt.FilterTransform(filter="datum.a < 4")] # flatten transform chart = alt.Chart().transform_flatten(["A", "B"], ["X", "Y"]) kwds = {"as": ["X", "Y"], "flatten": ["A", "B"]} assert chart.transform == [alt.FlattenTransform(**kwds)] # fold transform chart = alt.Chart().transform_fold(["A", "B", "C"], as_=["key", "val"]) kwds = {"as": ["key", "val"], "fold": ["A", "B", "C"]} assert chart.transform == [alt.FoldTransform(**kwds)] # impute transform chart = alt.Chart().transform_impute("field", "key", groupby=["x"]) kwds = {"impute": "field", "key": "key", "groupby": ["x"]} assert chart.transform == [alt.ImputeTransform(**kwds)] # joinaggregate transform chart = alt.Chart().transform_joinaggregate(min="min(x)", groupby=["key"]) kwds = { "joinaggregate": [alt.JoinAggregateFieldDef(field="x", op="min", **{"as": "min"})], "groupby": ["key"], } assert chart.transform == [alt.JoinAggregateTransform(**kwds)] # loess transform chart = alt.Chart().transform_loess("x", "y", as_=["xx", "yy"]) kwds = {"on": "x", "loess": "y", "as": ["xx", "yy"]} assert chart.transform == [alt.LoessTransform(**kwds)] # lookup transform (data) lookup_data = alt.LookupData(alt.UrlData("foo.csv"), "id", ["rate"]) chart = alt.Chart().transform_lookup("a", from_=lookup_data, as_="a", default="b") kwds = {"from": lookup_data, "as": "a", "lookup": "a", "default": "b"} assert chart.transform == [alt.LookupTransform(**kwds)] # lookup transform (selection) lookup_selection = alt.LookupSelection(key="key", selection="sel") chart = alt.Chart().transform_lookup("a", from_=lookup_selection, as_="a", default="b") kwds = {"from": lookup_selection, "as": "a", "lookup": "a", "default": "b"} assert chart.transform == [alt.LookupTransform(**kwds)] # pivot transform chart = alt.Chart().transform_pivot("x", "y") assert chart.transform == [alt.PivotTransform(pivot="x", value="y")] # quantile transform chart = alt.Chart().transform_quantile("x", as_=["prob", "value"]) kwds = {"quantile": "x", "as": ["prob", "value"]} assert chart.transform == [alt.QuantileTransform(**kwds)] # regression transform chart = alt.Chart().transform_regression("x", "y", as_=["xx", "yy"]) kwds = {"on": "x", "regression": "y", "as": ["xx", "yy"]} assert chart.transform == [alt.RegressionTransform(**kwds)] # sample transform chart = alt.Chart().transform_sample() assert chart.transform == [alt.SampleTransform(1000)] # stack transform chart = alt.Chart().transform_stack("stacked", "x", groupby=["y"]) assert chart.transform == [ alt.StackTransform(stack="x", groupby=["y"], **{"as": "stacked"}) ] # timeUnit transform chart = alt.Chart().transform_timeunit("foo", field="x", timeUnit="date") kwds = {"as": "foo", "field": "x", "timeUnit": "date"} assert chart.transform == [alt.TimeUnitTransform(**kwds)] # window transform chart = alt.Chart().transform_window(xsum="sum(x)", ymin="min(y)", frame=[None, 0]) window = [ alt.WindowFieldDef(**{ "as": "xsum", "field": "x", "op": "sum" }), alt.WindowFieldDef(**{ "as": "ymin", "field": "y", "op": "min" }), ] # kwargs don't maintain order in Python < 3.6, so window list can # be reversed assert chart.transform == [ alt.WindowTransform(frame=[None, 0], window=window) ] or chart.transform == [ alt.WindowTransform(frame=[None, 0], window=window[::-1]) ]
def test_transforms(): # aggregate transform agg1 = alt.AggregatedFieldDef(**{'as': 'x1', 'op': 'mean', 'field': 'y'}) agg2 = alt.AggregatedFieldDef(**{'as': 'x2', 'op': 'median', 'field': 'z'}) chart = alt.Chart().transform_aggregate([agg1], ['foo'], x2='median(z)') kwds = dict(aggregate=[agg1, agg2], groupby=['foo']) assert chart.transform == [alt.AggregateTransform(**kwds)] # bin transform chart = alt.Chart().transform_bin("binned", field="field", bin=True) kwds = {'as': 'binned', 'field': 'field', 'bin': True} assert chart.transform == [alt.BinTransform(**kwds)] # calcualte transform chart = alt.Chart().transform_calculate("calc", "datum.a * 4") kwds = {'as': 'calc', 'calculate': 'datum.a * 4'} assert chart.transform == [alt.CalculateTransform(**kwds)] # density transform chart = alt.Chart().transform_density('x', as_=['value', 'density']) kwds = {'as': ['value', 'density'], 'density': 'x'} assert chart.transform == [alt.DensityTransform(**kwds)] # filter transform chart = alt.Chart().transform_filter("datum.a < 4") assert chart.transform == [alt.FilterTransform(filter="datum.a < 4")] # flatten transform chart = alt.Chart().transform_flatten(['A', 'B'], ['X', 'Y']) kwds = {'as': ['X', 'Y'], 'flatten': ['A', 'B']} assert chart.transform == [alt.FlattenTransform(**kwds)] # fold transform chart = alt.Chart().transform_fold(['A', 'B', 'C'], as_=['key', 'val']) kwds = {'as': ['key', 'val'], 'fold': ['A', 'B', 'C']} assert chart.transform == [alt.FoldTransform(**kwds)] # impute transform chart = alt.Chart().transform_impute("field", "key", groupby=["x"]) kwds = {"impute": "field", "key": "key", "groupby": ["x"]} assert chart.transform == [alt.ImputeTransform(**kwds)] # joinaggregate transform chart = alt.Chart().transform_joinaggregate(min='min(x)', groupby=['key']) kwds = { 'joinaggregate': [ alt.JoinAggregateFieldDef( field='x', op='min', **{'as': 'min'}) ], 'groupby': ['key'] } assert chart.transform == [alt.JoinAggregateTransform(**kwds)] # loess transform chart = alt.Chart().transform_loess('x', 'y', as_=['xx', 'yy']) kwds = {'on': 'x', 'loess': 'y', 'as': ['xx', 'yy']} assert chart.transform == [alt.LoessTransform(**kwds)] # lookup transform (data) lookup_data = alt.LookupData(alt.UrlData('foo.csv'), 'id', ['rate']) chart = alt.Chart().transform_lookup('a', from_=lookup_data, as_='a', default='b') kwds = {'from': lookup_data, 'as': 'a', 'lookup': 'a', 'default': 'b'} assert chart.transform == [alt.LookupTransform(**kwds)] # lookup transform (selection) lookup_selection = alt.LookupSelection(key='key', selection='sel') chart = alt.Chart().transform_lookup('a', from_=lookup_selection, as_='a', default='b') kwds = {'from': lookup_selection, 'as': 'a', 'lookup': 'a', 'default': 'b'} assert chart.transform == [alt.LookupTransform(**kwds)] # pivot transform chart = alt.Chart().transform_pivot('x', 'y') assert chart.transform == [alt.PivotTransform(pivot='x', value='y')] # quantile transform chart = alt.Chart().transform_quantile('x', as_=['prob', 'value']) kwds = {'quantile': 'x', 'as': ['prob', 'value']} assert chart.transform == [alt.QuantileTransform(**kwds)] # regression transform chart = alt.Chart().transform_regression('x', 'y', as_=['xx', 'yy']) kwds = {'on': 'x', 'regression': 'y', 'as': ['xx', 'yy']} assert chart.transform == [alt.RegressionTransform(**kwds)] # sample transform chart = alt.Chart().transform_sample() assert chart.transform == [alt.SampleTransform(1000)] # stack transform chart = alt.Chart().transform_stack('stacked', 'x', groupby=['y']) assert chart.transform == [alt.StackTransform(stack='x', groupby=['y'], **{'as': 'stacked'})] # timeUnit transform chart = alt.Chart().transform_timeunit("foo", field="x", timeUnit="date") kwds = {'as': 'foo', 'field': 'x', 'timeUnit': 'date'} assert chart.transform == [alt.TimeUnitTransform(**kwds)] # window transform chart = alt.Chart().transform_window(xsum='sum(x)', ymin='min(y)', frame=[None, 0]) window = [alt.WindowFieldDef(**{'as': 'xsum', 'field': 'x', 'op': 'sum'}), alt.WindowFieldDef(**{'as': 'ymin', 'field': 'y', 'op': 'min'})] # kwargs don't maintain order in Python < 3.6, so window list can # be reversed assert (chart.transform == [alt.WindowTransform(frame=[None, 0], window=window)] or chart.transform == [alt.WindowTransform(frame=[None, 0], window=window[::-1])])