예제 #1
0
    def test_stack_agg(self):
        query = vl2asp({
            "mark": "bar",
            "encoding": {
                "x": {
                    "type": "nominal",
                    "field": "n1"
                },
                "y": {
                    "type": "quantitative",
                    "field": "q1",
                    "stack": "zero",
                    "aggregate": "sum",
                },
                "detail": {
                    "type": "nominal",
                    "field": "n2"
                },
                "color": {
                    "type": "quantitative",
                    "field": "q2",
                    "aggregate": "mean",
                },
            },
        })

        assert is_valid(data_schema + query, True) == True
예제 #2
0
파일: cli.py 프로젝트: uwdata/draco
def main():  # pragma: no cover
    parser = create_parser()
    args = parser.parse_args()

    if args.mode != Mode.optimize and (
        args.type == QueryType.draco or args.type == QueryType.cql
    ):
        print("Validation only works with full specs.", sys.stderr)
    else:
        logger.info(f"Processing query: {args.query.name} ...")
        if args.type == QueryType.asp:
            draco_query = args.query.read().split("\n")
        else:
            query_spec = json.load(args.query)
            if args.type == QueryType.vl:
                draco_query = vl2asp(query_spec)
            elif args.type == QueryType.cql:
                draco_query = cql2asp(query_spec)

            print(draco_query)

        if args.mode == Mode.violations:
            result = run(
                draco_query,
                debug=args.debug,
                files=["define.lp", "hard.lp", "soft.lp", "output.lp"],
                silence_warnings=True,
            )

            if result:
                print(result.violations, file=args.out)
        elif args.mode == Mode.valid:
            result = run(
                draco_query,
                debug=args.debug,
                files=["define.lp", "hard.lp", "output.lp"],
                silence_warnings=True,
            )

            print("valid" if result else "invalid", file=args.out)
        elif args.mode == Mode.optimize:
            result = run(draco_query, debug=args.debug)

            if result:
                print(json.dumps(result.as_vl()), file=args.out)
                logger.info(f"Cost: {result.cost}")
                outname = (
                    "stringIO" if isinstance(args.out, io.StringIO) else args.out.name
                )
                logger.info(f"Wrote Vega-Lite spec to {outname}")

    # close open files
    if args.query is not sys.stdin:
        args.query.close()

    if args.out is not sys.stdout:
        args.out.close()
예제 #3
0
    def test_one_bar(self):
        query = vl2asp({
            "mark": "bar",
            "encoding": {
                "y": {
                    "type": "quantitative",
                    "field": "q1"
                }
            }
        })

        assert is_valid(data_schema + query, True) == True
예제 #4
0
    def test_row_only(self):
        query = vl2asp({
            "mark": "point",
            "encoding": {
                "row": {
                    "type": "nominal",
                    "field": "n1"
                }
            }
        })

        assert is_valid(data_schema + query, True) == False
예제 #5
0
    def test_q_q_bar(self):
        query = vl2asp({
            "mark": "bar",
            "encoding": {
                "x": {
                    "type": "quantitative",
                    "field": "q1"
                },
                "y": {
                    "type": "quantitative",
                    "field": "q2"
                },
            },
        })

        assert is_valid(data_schema + query, True) == False
예제 #6
0
    def test_only_one_agg(self):
        query = vl2asp({
            "mark": "point",
            "encoding": {
                "x": {
                    "type": "quantitative",
                    "field": "q1"
                },
                "y": {
                    "type": "quantitative",
                    "field": "q2",
                    "aggregate": "mean"
                },
            },
        })

        assert is_valid(data_schema + query, True) == False
예제 #7
0
    def test_heatmap(self):
        query = vl2asp({
            "mark": "rect",
            "encoding": {
                "x": {
                    "type": "nominal",
                    "field": "n1"
                },
                "y": {
                    "type": "ordinal",
                    "field": "q1",
                    "bin": True
                },
            },
        })

        assert is_valid(data_schema + query, True) == True
예제 #8
0
    def test_hist(self):
        query = vl2asp({
            "mark": "bar",
            "encoding": {
                "x": {
                    "type": "quantitative",
                    "field": "q1",
                    "bin": True
                },
                "y": {
                    "type": "quantitative",
                    "aggregate": "count"
                },
            },
        })

        assert is_valid(data_schema + query, True) == True
예제 #9
0
    def test_scatter(self):
        query = vl2asp({
            "mark": "point",
            "encoding": {
                "x": {
                    "type": "quantitative",
                    "field": "q1"
                },
                "y": {
                    "type": "quantitative",
                    "field": "q2"
                },
                "color": {
                    "type": "nominal",
                    "field": "n2"
                },
                "size": {
                    "type": "quantitative",
                    "field": "q3"
                },
            },
        })

        assert is_valid(data_schema + query, True) == True
예제 #10
0
    def test_stack_q_q(self):
        query = vl2asp({
            "mark": "area",
            "encoding": {
                "x": {
                    "type": "quantitative",
                    "field": "q1",
                    "scale": {
                        "zero": False
                    },
                },
                "y": {
                    "type": "quantitative",
                    "field": "q2",
                    "stack": "zero"
                },
                "color": {
                    "type": "nominal",
                    "field": "n1"
                },
            },
        })

        assert is_valid(data_schema + query, True) == True
예제 #11
0
def run_spec(data_schema, spec, relax_hard=False):
    query = vl2asp(spec)
    return run(data_schema + query, relax_hard=relax_hard)
예제 #12
0
def run_spec(data_schema, spec):
    query = vl2asp(spec)
    return run(data_schema + query)