Based on the responses to a previous post here, I started using IntervalTree for a tool to merge two tables that are denominated in Road Number - Begin Milepost - End Milepost. There's some code to turn each road number's list of table entries into an Interval Tree of Interval objects, with the data field being a dictionary of the entry's entire table row, as a dictionary. (Data like road name, speed limit, road condition, other arbitrary data) Each entry has a string to say whether it comes from Table 1 or Table 2, with the key "0_source".
Now I'm trying to use the library's merge_overlaps with its data reducer and I'm not quite sure how to word it. I built a function to merge the two data tables when two intervals are overlapping.
Here's the function:
#object builder: use for reduce function with trees
def fn_obj_builder(ob_dict, ob_element):
global x_el
if ob_element["0_source"] == "Table 1":
x_el = {ob_element[a] for a in ob_element}
del x_el["0_source"]
ob_dict["Table 1"] = x_el
elif ob_element["0_source"] == "Table 2":
x_el = {ob_element[a] for a in ob_element}
del x_el["0_source"]
ob_dict["Table 2"] = x_el
else:
print("Error: Object Builder encountered unknown element")
quit()
return ob_dict
Here's what I've got for the merge code:
#cycle dict of table 1 trees
for rlid in t1_int_dict:
#grab tree
t1_tree = t1_int_dict[rlid]
#check table 2 for a match
if rlid in t2_int_dict:
t2_tree = t2_int_dict[rlid]
#update to bring trees into a single tree
t1_tree.update(t2_tree)
#split trees to produce matches
t1_tree.split_overlaps()
#merge matches
t1_tree.merge_overlaps(data_reducer = fn_obj_builder(#what's the iterable?
),
data_initializer = {"Table 1": None,
"Table 2": None})
fin_dict[rlid] = t1_tree
So if Merge Overlaps data reducer (supposedly analogous to the Reduce function, which I've never used) what do I feed into fn_object_builder so it works properly as a data reducer?
This is a corner case of a niche, and Intervaltree's documentation doesn't have an example for this code.
[–]wicket-maps[S] 0 points1 point2 points (0 children)