def __sub__(self, other): query = self.anchor_on( other)._get_anchor_asp() + self._get_asp_complete() files = ['compare.lp'] result = draco(query, files=files, silence_warnings=True) return result.graphscape_list
def _get_draco_sol(self): partial = self._get_asp_partial() anchor = self._get_anchor_asp() query = partial + anchor sol = draco(query) return sol
def run_draco(query, lp_files, num_models): """ :param query: :param lp_files: :param num_models: :return: draco result set """ return draco(query, files=lp_files, topk=True, k=num_models, silence_warnings=True)
def _get_topk_from_anchor(self): draco_query = self._get_full_query() graphscape_query = self._get_full_query() best_graphscape = draco(graphscape_query, files=Chart.OPT_GRAPHSCAPE_FILES, topk=True, k=Chart.K, silence_warnings=True) best_draco = draco(draco_query, files=Chart.OPT_DRACO_THEN_GRAPHSCAPE_FILES, topk=True, k=Chart.K, silence_warnings=True) good = filter_sols(best_draco, best_graphscape, self._name) if (len(good) == 1): return [(good[0], 0)] d = [v.d for v in good] g = [v.g for v in good] dz = None gz = None if (len(set(d)) == 1): dz = [0 for _ in d] else: dz = normalize(d) if (len(set(g)) == 1): gz = [0 for _ in g] else: gz = normalize(g) combined = [(v, dz[i] + gz[i], d[i]) for i, v in enumerate(good)] combined.sort(key=lambda x: (x[1], x[2])) return combined
def _get_draco_sol(self): if (self._solved): return self._sol sol = None if (self._anchor): topk = self._get_topk_from_anchor() sol = topk[0][0] else: query = self._get_full_query() sol = draco(query) self._solved = True self._sol = sol return sol
def see(self, *fields, anchor=None, name=None): if (name is not None): viewname = '\"{0}\"'.format(name) else: viewname = Chart.DEFAULT_NAME fields = list(fields) if (not fields): fields = self._recent_fields elif (all([x[0] == '+' for x in fields])): fields = [x[1:] for x in fields] + self._recent_fields elif (all([x[0] == '-' for x in fields])): fields = filter(lambda x : x not in [y[1:] for y in fields], self._recent_fields) self._recent_fields = fields partial, asp = self._query(fields, anchor, viewname) # print('\n'.join(asp)) self._sol = draco(asp) if (name is not None): self._anchors[viewname] = anchor_spec(partial, self._sol.props[viewname], viewname) return VegaLite(self._sol.as_vl(viewname), self._data)
scale='zero') # get the default view and schema asp. print('\n\n---- HERE IS THE DEFAULT VIEW, SCHEMA PLUS THE QUERY ----') query = gapminder1._get_full_query() for l in query: print(l) print('\n\n---- HERE IS THE VL FOR THE OPTIMAL MODEL ----') vl = gapminder1._get_vegalite() vl_json = json.dumps(vl) print(vl_json) # r = gapminder1._get_render() # for display in Jupyter # call Draco directly with the query, for a single optimal cold recommendation result = draco(query) print_draco_result( result, '---- HERE IS THE SINGLE_RESULT DIRECT FROM DRACO ----') # call draco to get top 5 models from query results = draco(query, topk=True, k=5, silence_warnings=True) print('\n\n---- HERE ARE RESULTS FROM topk=5 ----') for i, result in enumerate(results): print_draco_result(result, f'model {i+1}') # try explicit field to encoding assignments in query query_expl = [ 'encoding(v_v,e0).', 'encoding(v_v,e1).', ':- not field(v_v,e0,"pop").', # ':- field(v_v,e0,"pop"), not zero(v_v,e0).',
def _get_stats(self, anchor=None): evalk = 1000 own_sol = None own_draco_score = None own_graphscape_score = None own_spec = None query = None if (anchor): query = self.clone().anchor_on(anchor)._get_full_query() else: if self._anchor is None: raise Exception("cold recommendation requires anchor") query = self._get_full_query() topk_draco = draco(query, files=Chart.OPT_DRACO_THEN_GRAPHSCAPE_FILES, topk=True, k=evalk, silence_warnings=True) if (anchor): top = draco(query, files=Chart.OPT_DRACO_THEN_GRAPHSCAPE_FILES, topk=True, k=Chart.K, silence_warnings=True) own_sol = top[0] else: if self._anchor is None: raise Exception("cold recommendation requires anchor") own_sol = self._get_draco_sol() own_draco_score = own_sol.d own_graphscape_score = own_sol.g own_spec = json.dumps(sorted(own_sol.props[Chart.DEFAULT_NAME])) actual_k_draco = len(topk_draco) topk_draco_scores = [x.d for x in topk_draco] max_draco_score = max(topk_draco_scores) min_draco_score = min(topk_draco_scores) norm_draco_score = None if (max_draco_score == min_draco_score): if own_draco_score == max_draco_score: norm_draco_score = 0 else: norm_draco_score = None else: norm_draco_score = (own_draco_score - min_draco_score) / ( max_draco_score - min_draco_score) topk_draco_specs = { json.dumps(sorted(c.props[Chart.DEFAULT_NAME])): rank for rank, c in enumerate(topk_draco) } for rank, c in enumerate(topk_draco): if (rank > 0): prev = topk_draco[rank - 1] if prev.d == c.d: topk_draco_specs[json.dumps( sorted( c.props[Chart.DEFAULT_NAME]))] = topk_draco_specs[ json.dumps( sorted(prev.props[Chart.DEFAULT_NAME]))] draco_rank = None draco_of = None if (own_spec in topk_draco_specs): draco_rank = topk_draco_specs[own_spec] draco_of = actual_k_draco else: draco_rank = None draco_of = actual_k_draco ## Graphscape # graphscape_query = None # if (anchor): # graphscape_query = self.clone().anchor_on(anchor)._get_full_query() # else: # if self._anchor is None: # raise Exception("cold recommendation requires anchor") # graphscape_query = self._get_full_query() topk_graphscape = draco(query, files=Chart.OPT_GRAPHSCAPE_FILES, topk=True, k=evalk, silence_warnings=True) actual_k_graphscape = len(topk_graphscape) topk_graphscape_scores = [x.d for x in topk_graphscape] max_graphscape_score = max(topk_graphscape_scores) min_graphscape_score = min(topk_graphscape_scores) norm_graphscape_score = None if (max_graphscape_score == min_graphscape_score): if own_graphscape_score == max_graphscape_score: norm_graphscape_score = 0 else: norm_graphscape_score = None else: norm_graphscape_score = ( own_graphscape_score - min_graphscape_score) / ( max_graphscape_score - min_graphscape_score) topk_graphscape_specs = { json.dumps(sorted(c.props[Chart.DEFAULT_NAME])): rank for rank, c in enumerate(topk_graphscape) } graphscape_rank = None graphscape_of = None for rank, c in enumerate(topk_graphscape): if (rank > 0): prev = topk_graphscape[rank - 1] if prev.g == c.g: topk_graphscape_specs[json.dumps( sorted(c.props[Chart.DEFAULT_NAME]) )] = topk_graphscape_specs[json.dumps( sorted(prev.props[Chart.DEFAULT_NAME]))] if (own_spec in topk_graphscape_specs): graphscape_rank = topk_graphscape_specs[own_spec] graphscape_of = actual_k_graphscape else: graphscape_rank = None graphscape_of = actual_k_graphscape return { 'draco_score': own_draco_score, 'norm_draco_score': norm_draco_score, 'draco_rank': draco_rank, 'draco_of': draco_of, 'graphscape_score': own_graphscape_score, 'norm_graphscape_score': norm_graphscape_score, 'graphscape_rank': graphscape_rank, 'graphscape_of': graphscape_of, }