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()
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: input_task = AspTask(args.query.read()) else: # load a task from a spec provided by the user query_spec = json.load(args.query) d = args.base or os.path.dirname(args.query.name) if args.type == QueryType.draco: input_task = Task.from_obj(query_spec, d) elif args.type == QueryType.cql: input_task = Task.from_cql(query_spec, d) elif args.type == QueryType.vl: input_task = Task.from_vegalite(query_spec, d) if args.mode == Mode.violations: task = run(input_task, debug=args.debug, files=['define.lp', 'hard.lp', 'soft.lp', 'output.lp'], silence_warnings=True) if task: print(task.violations, file=args.out) elif args.mode == Mode.valid: task = run(input_task, debug=args.debug, files=['define.lp', 'hard.lp', 'output.lp'], silence_warnings=True) print('valid' if task else 'invalid', file=args.out) elif args.mode == Mode.optimize: task = run(input_task, debug=args.debug) if task: print(task.to_vegalite_json(), file=args.out) logger.info(f'Cost: {task.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()
def test_output_schema(self): json_files = [ os.path.join(EXAMPLES_DIR, fname) for fname in os.listdir(EXAMPLES_DIR) if fname.endswith(".json") and not fname.endswith(".vl.json") ] with open("js/node_modules/vega-lite/build/vega-lite-schema.json") as sf: schema = json.load(sf) for fname in json_files: with open(fname, "r") as f: query_spec = json.load(f) data = None if "url" in query_spec["data"]: data = read_data_to_asp( os.path.join( os.path.dirname(f.name), query_spec["data"]["url"] ) ) elif "values" in query_spec["data"]: data = read_data_to_asp(query_spec["data"]["values"]) else: raise Exception("no data found in spec") print(data) query = cql2asp(query_spec) program = query + data result = run(program) validate(result.as_vl(), schema)
def test_output_schema(self, file): query_spec = { "data": { "url": file + ".csv" }, "mark": "?", "encodings": [{ "channel": "x", "field": "?" }, { "channel": "y", "field": "?" }] } data = read_data_to_asp( os.path.join(PATH_INPUT, "csv", query_spec["data"]["url"])) query = cql2asp(query_spec) program = query + data result = run(program) try: with open(os.path.join(PATH_OUPUT, file + ".json"), 'w') as f: json.dump(result.as_vl(), f) print("success") except: print(f"!!!!!Error predict: {file}!!!!!")
def count_violations(task: Task, debug=False) -> Optional[Dict[str, int]]: ''' Get a dictionary of violations for a full spec. Args: task: a task spec object Returns: a dictionary storing violations of soft rules ''' out_task = run(task, files=['define.lp', 'soft.lp', 'output.lp'], silence_warnings=True, debug=debug) if out_task is not None: return out_task.violations else: return None
def test_output_schema(self): json_files = [ os.path.join(EXAMPLES_DIR, fname) for fname in os.listdir(EXAMPLES_DIR) if fname.endswith('.json') and not fname.endswith('.vl.json') ] with open('node_modules/vega-lite/build/vega-lite-schema.json') as sf: schema = json.load(sf) for fname in json_files: with open(fname, 'r') as f: query_spec = json.load(f) input_task = Task.from_obj(query_spec, os.path.dirname(f.name)) task = run(input_task) validate(task.to_vegalite(), schema)
def generate_visual_pairs(partial_full_data, weights): # Generate pairs that can be visualized by bug finders result = {} result["headers"] = { "first": { "title": "Draco", "subtitle": "Draco Prediction" }, "second": { "title": "CQL", "subtitle": "Compassql Prediction" } } result["specs"] = [] for case in partial_full_data: partial_spec, full_spec = partial_full_data[case] draco_rec = run(Task.from_cql(partial_spec), constants=weights) if draco_rec is None: logger.warning(f'Could not find a spec for {partial_spec}') result["specs"].append({ "first": None, "second": full_spec, "properties": { "input": partial_spec } }) continue result["specs"].append({ "first": draco_rec.to_vegalite(), "second": full_spec, "properties": { "input": partial_spec } }) return result
def discriminative_learning(train_data, initial_weights, learning_rate=0.01, max_iter=100): """ discriminative learning for mln from partial and full specs """ weights = {} for k in initial_weights: weights[k] = initial_weights[k] * 50 logging.disable(logging.CRITICAL) t = 0 while t < max_iter: print("[Iteration] {}".format(t)) for case in train_data: partial_spec, full_spec = train_data[case][0], train_data[case][1] draco_rec = run(partial_spec, constants=weights, silence_warnings=True) map_state = count_violations(draco_rec) truth_state = count_violations(full_spec) # get the names of violated rules in two specs violated_rules = set( list(map_state.keys()) + list(truth_state.keys())) for r in violated_rules: # get the num violations of the rule r n1 = map_state.get(r, 0) n2 = truth_state.get(r, 0) # since our weights are costs and we want to minimize the loss weights[r + "_weight"] += n1 - n2 # the solution generated by visrec solution # print(draco_rec.to_vegalite_json()) break t += 1
def get_rec(data, query): query = Query.from_obj(query) input_task = Task(data, query) return run(input_task)
def run_spec(data, spec): query = Query.from_vegalite(spec) input_task = Task(data, query) return run(input_task)
def get_rec(data_schema, spec, relax_hard=False): query = cql2asp(spec) return run(data_schema + query, relax_hard=relax_hard)
def run_spec(data_schema, spec, relax_hard=False): query = vl2asp(spec) return run(data_schema + query, relax_hard=relax_hard)
def get_rec(data_schema, spec): query = cql2asp(spec) return run(data_schema + query)
def run_spec(data_schema, spec): query = vl2asp(spec) return run(data_schema + query)