Draco Helper Class#
Draco can check whether a visualization specification is valid. If it isn’t valid, it can also compute what constrains are being violated. These methods check a specification against the standard set of constraints in Draco. If you define your own constraints, you can pass different ASP programs to the constructor or instead use the lower-level Draco run API.
Class Documentation#
- class draco.Draco(define=programs.define, constraints=programs.constraints, helpers=programs.helpers, generate=programs.generate, hard=programs.hard, soft=programs.soft, optimize=programs.optimize, weights=draco_weights)#
A class for holding all the programs used by Draco.
- check_spec(spec)#
Checks the spec against the hard constraints.
Internally, Draco checks against the definitions, constraints, helpers, and hard constraints.
- Parameters:
spec (
Union
[Iterable
[str
],str
]) – The specification to check- Return type:
bool
- complete_spec(spec, models=1)#
Get optimal completions for the partial input specification.
- Parameters:
spec (
Union
[Iterable
[str
],str
]) – The partial specification to complete.models – The number of completions to return, defaults to 1
- count_preferences(spec)#
Get a dictionary from preferences to show how often a given specification violates the preference. Returns None if the problem is not satisfiable.
Internally, Draco checks against the definitions, constraints, helpers, and soft constraints.
- Parameters:
spec (
Union
[Iterable
[str
],str
]) – The specification to check
- get_violations(spec)#
Get the list of violations for a given specification. Returns None if the problem is not satisfiable.
Internally, Draco checks against the definitions, constraints (without the constraint that disallows violations), helpers, and hard constraints.
- Parameters:
spec (
Union
[Iterable
[str
],str
]) – The specification to check
Usage Example#
from draco import Draco
d = Draco()
valid_spec = """
attribute(number_rows,root,100).
entity(field,root,f1).
attribute((field,name),f1,temperature).
attribute((field,type),f1,number).
entity(view,root,v).
entity(mark,v,m1).
attribute((mark,type),m1,tick).
entity(encoding,m1,e1).
attribute((encoding,channel),e1,x).
attribute((encoding,field),e1,f1).
entity(scale,v,sx).
attribute((scale,channel),sx,x).
attribute((scale,type),sx,linear).
"""
d.check_spec(valid_spec)
False
d.get_violations(valid_spec)
['existing_field', 'invalid_domain']
invalid_spec = """
attribute(number_rows,root,100).
entity(field,root,f1).
attribute((field,name),f1,temperature).
attribute((field,type),f1,number).
entity(view,root,v).
entity(mark,v,m1).
% invalid mark
attribute((mark,type),m1,invalid).
entity(encoding,m1,e1).
attribute((encoding,channel),e1,x).
attribute((encoding,field),e1,f1).
entity(scale,v,sx).
attribute((scale,channel),sx,x).
attribute((scale,type),sx,linear).
"""
d.check_spec(invalid_spec)
False
d.get_violations(invalid_spec)
['existing_field', 'invalid_domain']