in distill/analytics/graph/graph.py [0:0]
def funnel(edges_segmentN,
user_specification,
node_labels=False):
"""
Creates Funnel Graph from defined edge list and optional user-provided labels
:param edges_segmentN: List of Tuples
:param user_specification: String of Target of interest e.g. #document
:param node_labels: Optional Dictionary of key default values, value replacements
:return: A Funnel graph
"""
## Removing duplicates
edge_list_temp = []
for row in edges_segmentN:
if row[0] != row[1]:
edge_list_temp.append(row)
edge_list = edge_list_temp
## Convert from list of 2s to list of 1s
edgelist_list = []
length = len(edge_list) - 1
for i in edge_list:
if edge_list.index(i) != length:
edgelist_list.append(i[0])
else:
edgelist_list.append(i[0])
edgelist_list.append(i[1])
# Remove the None values
funnel_targets_temp = []
for item in edgelist_list:
if item != None:
funnel_targets_temp.append(item)
funnel_targets = funnel_targets_temp
## Convert that list into a list of 3s
edge_list = []
for i in range(len(funnel_targets)):
if i == (len(funnel_targets) - 2):
break
else:
edge_list.append((funnel_targets[i], funnel_targets[i + 1], funnel_targets[i + 2]))
## Convert the list of 3s to a counter
edge_list_counter = collections.Counter(edge_list)
first_rung = user_specification
new_edge_list = []
for i in edge_list:
if i[0] == user_specification:
new_edge_list.append((i[0], i[1], i[2]))
new_edge_list_counter = collections.Counter(new_edge_list)
new_edge_list_counter.most_common(1)
first_rung = new_edge_list_counter.most_common(1)[0][0][0]
second_rung = new_edge_list_counter.most_common(1)[0][0][1]
third_rung = new_edge_list_counter.most_common(1)[0][0][2]
counter1 = 0
counter2 = 0
counter3 = 0
for i in edge_list:
if i[0] == first_rung:
counter1 += 1
if i[1] == second_rung:
counter2 += 1
if i[2] == third_rung:
counter3 += 1
# Numbers are how many times each target occured
# Edges are the targets
numbers = [counter1, counter2, counter3]
edges = [first_rung, second_rung, third_rung]
# If node labels was given as an argument, replaces the targets with the provided names
if node_labels:
new_edges = []
for edge in edges:
if edge in node_labels:
new_edges.append(node_labels[edge])
else:
new_edges.append(edge)
edges = new_edges
# Plotting labels from the list with the values from the dictionary
data = dict(
number=numbers,
edge=edges)
# Plotting the figure
fig = go.Figure(go.Funnel(
y=edges,
x=numbers,
textposition="inside",
textinfo="value+percent initial",
opacity=0.65, marker={"color": ["deepskyblue", "lightsalmon", "tan"],
"line": {"width": [2]}},
connector={"line": {"color": "lime", "dash": "dot", "width": 5}})
)
fig.show()
return fig