def test_inject_context_IR(self): a = Alist( **{ tt.ID: '1', tt.SUBJECT: 'Ghana', tt.PROPERTY: 'P1082', tt.OBJECT: '', tt.OPVAR: '?x', tt.COST: 1 }) ctx1 = [{ ctx.nationality: 'United Kingdom' }, { ctx.place: 'England', ctx.device: 'phone', ctx.datetime: '2020-04-30 12:00:00' }, { 's': { 'wikidata': 'url://wikidata', 'worldbank': 'wbankID' }, 't': '2020' }] a.set(tt.CONTEXT, ctx1) alist = frank.context.inject_retrieval_context(a, 'worldbank') self.assertEqual(alist.get(tt.TIME), '2020')
def test_graph_add_nodes(self): graph = InferenceGraph() alist1 = Alist( **{ tt.ID: '1', tt.SUBJECT: '$y', tt.PROPERTY: 'P1082', tt.OBJECT: '?x', tt.TIME: '2010', tt.OPVAR: '?x', tt.COST: 1, '$y': 'Ghana' }) alist2 = Alist( **{ tt.ID: '101', tt.SUBJECT: 'Africa', tt.PROPERTY: 'P1082', tt.OBJECT: '?x', tt.TIME: '2010', tt.OPVAR: '?x', tt.COST: 1 }) graph.add_alists_from([alist1, alist2]) nodes = graph.nodes() print(nodes) self.assertTrue(len(nodes) == 2)
def reduce(alist: Alist, children: List[Alist], G: InferenceGraph): # do comparisons vars_to_compare = alist.get(tt.OPVAR).split(' ') # propagate projection vars to parent5 propagate.projections(alist, tuple(children)) response_var = "?_lte_" if len(vars_to_compare) == 0: alist.set(response_var, "false") return alist result = True if len(vars_to_compare) > 1 and utils.is_numeric( alist.instantiation_value(vars_to_compare[0])): for x in vars_to_compare[1:]: if utils.is_numeric( alist.instantiation_value(x)) and utils.is_numeric( alist.instantiation_value(x)): result = (utils.get_number( alist.instantiation_value( vars_to_compare[0]), 0) <= utils.get_number( alist.instantiation_value(x), 0)) and result else: result = False break else: result = False alist.set(response_var, str(result).lower()) # alist.instantiate_variable(tt.COV, estimate_uncertainty( # children, True, alist.get(tt.OP), len(children) # )) return alist
def test_inject_context_inference(self): G = InferenceGraph() a = Alist( **{ tt.ID: '1', tt.SUBJECT: 'Ghana', tt.PROPERTY: 'P1082', tt.TIME: '2023', tt.OBJECT: '', tt.OPVAR: '?x', tt.COST: 1 }) ctx1 = [{ ctx.nationality: 'United Kingdom', ctx.accuracy: 'low', ctx.speed: 'low' }, { ctx.place: 'United Kingdom', ctx.device: 'phone', ctx.datetime: '2020-07-27 11:00:00' }, {}] a.set(tt.CONTEXT, ctx1) G.add_alist(a) op_alist = Temporal().decompose(a, G) self.assertEqual(op_alist.get(tt.OP), 'regress')
def find_propert_time(alist: Alist): alist_arr = [] results = find_recording(artist=alist.get(tt.SUBJECT), title=alist.get(tt.OBJECT), date=None) # parse date formats and sort in reverse FORMATS = ['%Y', '%Y-%m-%d'] for r in results: date = '' for fmt in FORMATS: try: date = datetime.strptime(r['date'], fmt) r['date'] = date.strftime('%Y') except: pass results_sorted = [k for k in sorted(results, key=lambda x: x['date'])] for item in results_sorted: data_alist = alist.copy() data_alist.set(tt.TIME, item['date']) data_alist.data_sources = list( set(data_alist.data_sources + ['musicbrainz'])) alist_arr.append(data_alist) break # greedy; take only the first answer returned return alist_arr
def decompose(self, alist: A, G: InferenceGraph): # check for comparison operations: eq, lt, gt, lte, gte and for multiple variables in operation variable if alist.get(tt.OP).lower() in ['eq', 'lt', 'gt', 'lte', 'gte'] \ and len(alist.get(tt.OPVAR).split(' ')) > 1: opvars = alist.get(tt.OPVAR).split(' ') op_alist = alist.copy() # higher cost makes this decomposition more expensive op_alist.cost = alist.cost + 1 op_alist.branch_type = br.OR op_alist.parent_decomposition = 'comparison' op_alist.node_type = nt.HNODE # op_alist.state = states.EXPLORED # alist.link_child(op_alist) G.link(alist, op_alist, op_alist.parent_decomposition) for p in opvars: pval = alist.get(p) child = Alist() child.set(tt.OP, "value") child.set(tt.OPVAR, p) child.set(p, pval) child.cost = op_alist.cost + 1 child.node_type = nt.ZNODE child.set(tt.CONTEXT, op_alist.get(tt.CONTEXT)) # op_alist.link_child(child) G.link(op_alist, child, op_alist.parent_decomposition) else: return None return op_alist
def get_map_strategy(self, alist: Alist): """ Get decomposition rules to apply to an alist Args ---- alist : Alist Return ------ ops : A list of reduce functions for aggregating alists """ # TODO: learn to predict best strategy given path of root from # node and attributes in alist self.last_heartbeat = time.time() if alist.get(tt.OP).lower() in ['eq', 'lt', 'gt', 'lte', 'gte']: return [(frank.map.map_wrapper.get_mapper_fn("comparison"), "comparison")] # if compound frame (i.e nesting point in frame), then normalize elif alist.uninstantiated_nesting_variables(): return [(frank.map.map_wrapper.get_mapper_fn("normalize"), "normalize")] else: ops = [] for allowed_op in config.config["base_decompositions"]: try: ops.append( (frank.map.map_wrapper.get_mapper_fn(allowed_op), allowed_op)) except Exception as ex: print("Error in decomposition mapper: " + str(ex)) random.shuffle(ops) return ops
def test_context_composition(self): G = InferenceGraph() a = Alist( **{ tt.ID: '1', tt.SUBJECT: 'Ghana', tt.PROPERTY: 'P1082', tt.TIME: '2023', tt.OBJECT: '', tt.OPVAR: '?x', tt.COST: 1 }) ctx1 = [{ ctx.nationality: 'United Kingdom' }, { ctx.place: 'United Kingdom', ctx.device: 'computer', ctx.datetime: '2010-07-27 11:00:00' }, {}] a.set(tt.CONTEXT, ctx1) G.add_alist(a) query_ctx = frank.context.inject_query_context # query context should infer the ctx.accuracy from ctx.device op_alist = Temporal().decompose(query_ctx(a), G) self.assertEqual( (op_alist.get(tt.OP), len(G.child_alists(op_alist.id))), ('gpregress', 19))
def create_graph2(self): graph = InferenceGraph() parent = Alist( **{ tt.ID: '1', tt.SUBJECT: 'Africa', tt.PROPERTY: 'P1082', tt.OBJECT: '', tt.TIME: '2010', tt.OPVAR: '?x', tt.COST: 1 }) child = Alist( **{ tt.ID: '1', tt.SUBJECT: 'Ghana', tt.PROPERTY: 'P1082', tt.OBJECT: '', tt.TIME: '2010', tt.OPVAR: '?x', tt.COST: 2 }) child2 = Alist( **{ tt.ID: '1', tt.SUBJECT: 'a_Ghana', tt.PROPERTY: 'P1082', tt.OBJECT: '', tt.TIME: '2010', tt.OPVAR: '?x', tt.COST: 5 }) grandchild = Alist( **{ tt.ID: '1', tt.SUBJECT: 'b_Ghana', tt.PROPERTY: 'P1082', tt.OBJECT: '', tt.TIME: '2010', tt.OPVAR: '?x', tt.COST: 3 }) ggrandchild = Alist( **{ tt.ID: '1', tt.SUBJECT: 'c_Ghana', tt.PROPERTY: 'P1082', tt.OBJECT: '', tt.TIME: '2010', tt.OPVAR: '?x', tt.COST: 4 }) # ggrandchild.state = states.EXPLORED graph.add_alists_from([parent]) graph.link(parent, child, edge_label='TP') graph.link(parent, child2, edge_label='GS') graph.link(child, grandchild, edge_label='GS') graph.link(grandchild, ggrandchild, edge_label='GS') return graph
def part_of_relation_subject(alist: Alist): results = [] for r in find_relation_subject(alist.get(tt.OBJECT), "location"): factAlist = alist.copy() factAlist.data_sources.add('wikidata') factAlist.set(tt.SUBJECT, r) results.append(factAlist) return results
def part_of_relation_object(alist: Alist): results = [] for r in _part_of_relation_object(alist.get(tt.SUBJECT), "location"): fact_alist = alist.copy() fact_alist.data_sources = list( set(fact_alist.data_sources + ['wikidata'])) fact_alist.set(tt.OBJECT, r) results.append(fact_alist) return results
def projections(parent: Alist, alists_to_propagate): ''' propagate projection from selected child alist to its parent [in place]''' # copy projection vars of min alist to parent for c in alists_to_propagate: projVars = c.projection_variables() if projVars: for pvkey, pv in projVars.items(): parent.instantiate_variable( pvkey, c.instantiation_value(pvkey), insert_missing=True)
def reduce(alist: Alist, children: List[Alist], G: InferenceGraph): y_predict = None X = [] y = [] data_pts = [] for c in children: opVarValue = c.instantiation_value(c.get(tt.OPVAR)) if utils.is_numeric(opVarValue) and utils.is_numeric(c.get(tt.TIME)): x_val = utils.get_number(c.get(tt.TIME), None) y_val = utils.get_number(opVarValue, None) X.append([x_val]) y.append(y_val) data_pts.append([x_val, y_val]) X = np.array(X) y = np.array(y) reg = LinearRegression().fit(X, y) x_predict = utils.get_number(alist.get(tt.TIME), None) y_predict = reg.predict(np.array([[x_predict]]))[0] prediction = [x_predict, y_predict] coeffs = [v for v in reg.coef_] coeffs.insert(0, reg.intercept_) fnStr = 'LIN;' + ';'.join([str(v) for v in reg.coef_]) fnAndData = \ """{{"function":{coeffs}, "data":{data_pts}, "prediction":{prediction}}}""".format( coeffs=coeffs, data_pts=data_pts, prediction=prediction) alist.instantiate_variable(alist.get(tt.OPVAR), y_predict) alist.set(tt.FNPLOT, fnAndData) alist.instantiate_variable( tt.COV, estimate_uncertainty(children, len(data_pts) == len(children), alist.get(tt.OP), len(children))) return alist
def part_of_geopolitical_subject(alist: Alist): results = [] geopolitical_type = alist.get(tt.PROPERTY).split(':') for r in find_geopolitical_subelements(alist.get(tt.OBJECT), geopolitical_type[-1]): fact_alist = alist.copy() fact_alist.data_sources = list( set(fact_alist.data_sources + ['wikidata'])) fact_alist.set(tt.SUBJECT, r) results.append(fact_alist) return results
def setUp(self): G = InferenceGraph() self.infer = Infer(G) self.alist = Alist(**{tt.ID: '1', tt.SUBJECT: 'Ghana', tt.PROPERTY: 'P1082', tt.OBJECT: '?x', tt.TIME: '2010', tt.OPVAR: '?x', tt.COST: 1}) self.c1 = Alist(**{tt.ID: '2', tt.SUBJECT: 'Ghana', tt.PROPERTY: 'P1082', tt.OBJECT: '?x', tt.TIME: '2010', tt.OPVAR: '?x', tt.COST: 1}) self.c1.attributes['?x'] = '' self.c1.instantiate_variable('?x', '120') self.c1.state = states.REDUCIBLE self.c1.data_sources = ['wikidata', 'worldbank']
def find_property_subject(alist: Alist): alist_arr = [] results = find_recording(artist=None, title=alist.get(tt.OBJECT), date=alist.get(tt.TIME)) for item in results: data_alist = alist.copy() data_alist.set(tt.SUBJECT, item['artist']) data_alist.data_sources = list( set(data_alist.data_sources + ['musicbrainz'])) alist_arr.append(data_alist) return alist_arr
def flush(alist: Alist, items) -> Alist: """ Flush query context that whose corresponding alist attribute value is different """ for k in items: try: if k in alist.get(tt.CONTEXT)[2] and alist.get( tt.CONTEXT)[2][k] != alist.get(k): del alist.get(tt.CONTEXT)[2][k] except: pass return alist
def test_getNestingVars(self): alist = Alist( **{ tt.ID: '1', tt.SUBJECT: '$y', tt.PROPERTY: 'P1082', tt.OBJECT: '?x', tt.TIME: '2010', tt.OPVAR: '?x', tt.COST: 1 }) alist.set('$y', {"$filter": [{"p": "type", "o": "country"}]}) self.assertTrue(alist.nesting_variables())
def test_getVariables(self): alist = Alist( **{ tt.ID: '1', tt.SUBJECT: 'Africa', tt.PROPERTY: 'P1082', tt.OBJECT: '?x', tt.TIME: '2010', tt.OPVAR: '?x', tt.COST: 1 }) alist.set('#d', 34) variables = alist.variables() self.assertTrue(len(variables) == 3, "should have 3 items in dict")
def test_getNonInstantiated(self): alist = Alist( **{ tt.ID: '1', tt.SUBJECT: '$y', tt.PROPERTY: 'P1082', tt.OBJECT: '?x', tt.TIME: '2010', tt.OPVAR: '?x', tt.COST: 1, '$y': 'Ghana' }) result = alist.uninstantiated_attributes() self.assertTrue('s' not in result)
def test_isInstantiated(self): alist = Alist( **{ tt.ID: '1', tt.SUBJECT: '$y', tt.PROPERTY: 'P1082', tt.OBJECT: '?x', tt.TIME: '2010', tt.OPVAR: '?x', tt.COST: 1, '$y': 'Ghana' }) result = alist.is_instantiated(tt.SUBJECT) self.assertTrue(result)
def test_getAlistJsonWithMetadata(self): alist = Alist( **{ tt.ID: '1', tt.SUBJECT: '$y', tt.PROPERTY: 'P1082', tt.OBJECT: '?x', tt.TIME: '2010', tt.OPVAR: '?x', tt.COST: 1, '$y': 'Ghana' }) result = alist.get_alist_json_with_metadata() self.assertTrue('id' in result)
def test_graph_add_nodes_and_edges(self): graph = InferenceGraph() alist1 = Alist( **{ tt.ID: '1', tt.SUBJECT: '$y', tt.PROPERTY: 'P1082', tt.OBJECT: '?x', tt.TIME: '2010', tt.OPVAR: '?x', tt.COST: 1, '$y': 'Ghana' }) alist2 = Alist( **{ tt.ID: '101', tt.SUBJECT: 'Africa', tt.PROPERTY: 'P1082', tt.OBJECT: '?x', tt.TIME: '2010', tt.OPVAR: '?x', tt.COST: 1 }) alist3 = Alist( **{ tt.ID: '102', tt.SUBJECT: 'Africa', tt.PROPERTY: 'P1082', tt.OBJECT: '?x', tt.TIME: '2010', tt.OPVAR: '?x', tt.COST: 1 }) graph.add_alists_from([alist1]) # plt.ion() # # plt.plot() # fig = plt.figure() # plt.show() # graph.display() graph.plot_plotly("Graph 1") # plt.pause(0.3) graph.link(alist1, alist2, edge_label='TP') graph.link(alist1, alist3, edge_label='GS') edges = graph.edges() # plt.clf() # graph.display() # plt.pause(2) graph.plot_plotly("Graph 2") self.assertTrue(len(edges) > 0)
def find_property_values(self, alist: Alist, search_element: str): if search_element == tt.OBJECT: subject = alist.instantiation_value(tt.SUBJECT) nodes = self._get_nodes(subject) results = [] for node in nodes: try: data_alist = alist.copy() data_alist.set(tt.OBJECT, node[alist.get(tt.PROPERTY)]) data_alist.data_sources = list( set(data_alist.data_sources + [self.name])) results.append(data_alist) except: pass return results
def link(self, parent: Alist, child: Alist, edge_label='', create_new_id=True): if parent: succ = self.successors(parent.id) succ_nodes = [self.nodes[x] for x in succ] if create_new_id: child.depth = parent.depth + 1 child.id = f"{parent.depth + 1}{parent.id}{len(succ_nodes) + 1}" self.add_alist(child) self.add_edge(parent.id, child.id, **{'label': edge_label}) else: self.add_alist(child)
def test_getVariableRefs(self): alist = Alist( **{ tt.ID: '1', tt.SUBJECT: 'Africa', tt.PROPERTY: 'P1082', tt.OBJECT: '?x', tt.TIME: '2010', tt.OPVAR: '?x', tt.COST: 1 }) varRefs = alist.variable_references('?x') self.assertEqual(sorted(list(varRefs.keys())), sorted([tt.OBJECT, tt.OPVAR]), "should be OBJECT and OPVAR.")
def api_start(self, alist_obj, session_id, inference_graphs): a = Alist(**alist_obj) t = threading.Thread(target=self.start, args=( a, session_id, inference_graphs)) t.start() return session_id
def test_normalize_filter_with_location(self): alist = Alist( **{ tt.ID: '1', tt.SUBJECT: '$y', tt.PROPERTY: 'P1082', tt.OBJECT: '?x', tt.TIME: '2010', tt.OPVAR: '?x', tt.COST: 1, '$y': { "$filter": [{ "p": "type", "o": "country" }, { "p": "location", "o": "Africa" }] } }) G = InferenceGraph() G.add_alist(alist) normalize = Normalize() results = normalize.decompose(alist, G) self.assertTrue(len(G.child_alists(alist.id)) > 0)
def test_eq(self): a = Alist(**{ tt.ID: '1', tt.OPVAR: '$x $y', '$x': '?x1', '$y': '?y1', '?_eq_': '' }) b = Alist(**{tt.ID: '2', tt.OPVAR: '?x1', '?x1': 20}) c = Alist(**{tt.ID: '3', tt.OPVAR: '?y1', '?y1': 20}) G = InferenceGraph() G.add_alist(a) G.link(a, b) G.link(a, c) result = frank.reduce.eq.reduce(a, [b, c], G) self.assertTrue(True if result.instantiation_value('?_eq_') == 'true' else False)
def reduce(alist: Alist, children: List[Alist], G: InferenceGraph): variables = alist.variables() data = [x.instantiation_value(alist.get(tt.OPVAR)) for x in children if (x not in list(variables.keys()) and x not in list(variables.values()))] alist.instantiate_variable(alist.get(tt.OPVAR), len(data)) alist.instantiate_variable(tt.COV, estimate_uncertainty( children, False, alist.get(tt.OP), len(children) )) return alist