def to_widget( self, width=700, height=500, margins=None, align_link_types=False, link_label_format="", link_label_min_width=5, debugging=False, ): if SankeyWidget is None: raise RuntimeError("ipysankeywidget is required") if margins is None: margins = { "top": 25, "bottom": 10, "left": 130, "right": 130, } value = self.to_json(format="widget") widget = SankeyWidget( nodes=value["nodes"], links=value["links"], order=value["order"], groups=value["groups"], align_link_types=align_link_types, linkLabelFormat=link_label_format, linkLabelMinWidth=link_label_min_width, layout=Layout(width=str(width), height=str(height)), margins=margins, ) if debugging: output = Output() def callback(_, d): with output: clear_output() if not d: return link = [ l for l in self.links if l.source == d["source"] and l.target == d["target"] and l.type == d["type"] ] assert len(link) == 1 link = link[0] with output: display("Flows in dataset contributing to this link:") if self.dataset: display(self.dataset._table.loc[link.original_flows]) else: display(link.original_flows) widget.on_link_clicked(callback) return VBox([widget, output]) else: return widget
def to_widget(self, width=700, height=500, margins=None, align_link_types=False, link_label_format='', link_label_min_width=5, debugging=False): if SankeyWidget is None: raise RuntimeError('ipysankeywidget is required') if margins is None: margins = { 'top': 25, 'bottom': 10, 'left': 130, 'right': 130, } value = self.to_json(format='widget') widget = SankeyWidget(nodes=value['nodes'], links=value['links'], order=value['order'], groups=value['groups'], align_link_types=align_link_types, linkLabelFormat=link_label_format, linkLabelMinWidth=link_label_min_width, layout=Layout(width=str(width), height=str(height)), margins=margins) if debugging: output = Output() def callback(_, d): with output: clear_output() if not d: return link = [ l for l in self.links if l.source == d['source'] and l.target == d['target'] and l.type == d['type'] ] assert len(link) == 1 link = link[0] with output: display('Flows in dataset contributing to this link:') if self.dataset: display(self.dataset._table.loc[link.original_flows]) else: display(link.original_flows) widget.on_link_clicked(callback) return VBox([widget, output]) else: return widget
def diagram(self, variable=None, varlabel=None, *, minsenss=0, maxlinks=20, top=0, bottom=0, left=230, right=140, width=1000, height=400, showconstraints=True): "creates links and an ipython widget to show them" margins = dict(top=top, bottom=bottom, left=left, right=right) self.minsenss = minsenss self.maxlinks = maxlinks self.showconstraints = showconstraints for key in self.solution.name_collision_varkeys(): key.descr["necessarylineage"] = True if variable: if not varlabel: varlabel = variable.str_without(["unnecessary lineage"]) if len(varlabel) > 20: varlabel = variable.str_without(["lineage"]) self.nodes[varlabel] = {"id": varlabel, "title": varlabel} csetnode = self.add_node(varlabel, self.csetlabel) if variable.key in self.solution["sensitivities"]["cost"]: costnode = self.add_node(varlabel, "[cost function]") self.links[costnode, varlabel] = \ self.solution["sensitivities"]["cost"][variable.key] else: csetnode = self.csetlabel self.nodes[self.csetlabel] = {"id": self.csetlabel, "title": self.csetlabel} total_sens = self.link(self.cset, csetnode, variable) if variable: self.links[csetnode, varlabel] = total_sens links, nodes = self._links_and_nodes() out = SankeyWidget(nodes=nodes, links=links, margins=margins, layout=Layout(width=str(width), height=str(height))) filename = self.csetlabel if variable: filename += "_" + variable.str_without(["unnecessary lineage", "units"]) if not os.path.isdir("sankey_autosaves"): os.makedirs("sankey_autosaves") filename = "sankey_autosaves" + os.path.sep + cleanfilename(filename) out.auto_save_png(filename + ".png") out.auto_save_svg(filename + ".svg") out.on_node_clicked(self.onclick) out.on_link_clicked(self.onclick) for key in self.solution.name_collision_varkeys(): del key.descr["necessarylineage"] return out
def sankey(margin_top=10, **value): """Show SankeyWidget with default values for size and margins""" return SankeyWidget(layout=layout, margins=dict(top=margin_top, bottom=0, left=30, right=60), **value)
def diagram(self, variables=None, *, flowright=False, width=900, height=400, top=0, bottom=0, left=120, right=55, printing=True): "creates links and an ipython widget to show them" margins = dict(top=top, bottom=bottom, left=left, right=right) self.__init__(self.gp) if not variables: self.constrlinks(self.gp[0]) else: if not getattr(variables, "__len__", False): variables = [variables] for var in variables: self.varlinks(self.gp[0], var.key, printing=printing) # if var_eqs were found, label them on the diagram lookup = { key: i for i, key in enumerate(sorted(map(str, self.var_eqs))) } for node in self.nodes: if node["id"] in lookup: # use inverted circled numbers to id the variables... node["title"] += " " + chr(0x2776 + lookup[node["id"]]) elif "passthrough" in node: cn = node.pop("passthrough") l_idx = lookup[str(cn.left.hmap.keys()[0].keys()[0])] r_idx = lookup[str(cn.right.hmap.keys()[0].keys()[0])] op = {"=": "=", ">=": u"\u2265", "<=": u"\u2264"}[cn.oper] # ...so that e.g. (1) >= (2) can label the constraints node["title"] = (node["id"] + u"\u2009" + chr(l_idx + 0x2776) + op + chr(r_idx + 0x2776)) if flowright: r, l = margins["right"], margins["left"] margins["left"], margins["right"] = r, l links = [] maxflow = np.abs(self.links.values()).max() for (source, target), value in self.links.items(): if not flowright: source, target = target, source links.append({ "source": source, "target": target, "value": max(abs(value), maxflow / 1e5), "color": getcolor(value) }) return SankeyWidget(nodes=self.nodes, links=links, layout=Layout(width=str(width), height=str(height)), margins=margins)
def show_sankey(sankey_definition, dataset, palette=None, width=700, height=500, margins=None, align_link_types=False, measure='value', agg_measures=None, hue=None, override_node_layout=None, override_link_layout=None): if SankeyWidget is None: raise RuntimeError('ipysankeywidget is required') if margins is None: margins = {'top': 25, 'bottom': 10, 'left': 130, 'right': 130} if isinstance(hue, str): if hue == measure: hue_func = lambda data: data[hue] else: hue_func = lambda data: data['measures'][hue] elif callable(hue): hue_func = hue elif hue is None: hue_func = None else: raise ValueError('hue must be attribute name, callable, or None') G, groups = sankey_view(sankey_definition, dataset, measure, agg_measures) value = graph_to_sankey(G, groups, palette=palette, hue=hue_func) if align_link_types: value['alignLinkTypes'] = True # XXX experimental overrides if override_node_layout is not None: for node in value['nodes']: override = override_node_layout.get(node['id'], {}) if 'y' in override: node['forceY'] = override['y'] if override_link_layout is not None: value['overrideLinks'] = override_link_layout return SankeyWidget(nodes=value['nodes'], links=value['links'], order=value['order'], align_link_types=align_link_types, layout=Layout(width=str(width), height=str(height)), margins=margins)
def plot_conf_sankey(ln, labels=None): conf = get_confusions(ln) df = pd.DataFrame(columns=['source', 'target', 'type', 'value']) for (i, j), val in np.ndenumerate(conf): sou_label = 'true ' + (str(i) if labels is None else labels[i]) pred_label = 'pred ' + (str(j) if labels is None else labels[j]) df = df.append( { 'source': sou_label, 'target': pred_label, 'value': val, 'type': 'cor' if i == j else 'other' }, ignore_index=True) return SankeyWidget(links=df.to_dict('records'))
def draw_sankey(pop_size, hri_estimate, human_estimate): from ipysankeywidget import SankeyWidget from ipywidgets import Layout layout = Layout(width="800", height="300") n_high_risk = hri_estimate n_low_risk = pop_size - n_high_risk n_selected = human_estimate n_not_selected = n_high_risk - n_selected links = [ { 'source': 'Population', 'target': 'High Risk', 'value': n_high_risk, 'color': 'rgb(181, 57, 65)' }, { 'source': 'Population', 'target': 'Low Risk', 'value': n_low_risk, 'color': 'rgb(192, 172, 97)' }, { 'source': 'High Risk', 'target': 'Not Selected', 'value': n_not_selected, 'color': 'rgb(60, 92, 160)' }, { 'source': 'High Risk', 'target': 'Selected', 'value': n_selected, 'color': 'rgb(71, 155, 85)' }, ] w = SankeyWidget(links=links, layout=layout, margins=dict(top=0, bottom=0, left=100, right=100)) display(w)
def show_variance(flows, sdd, normed=False, vlim=None, width=800, palette=None): if palette is None: palette = sequential.Reds_9.mpl_colormap value, vmin, vmax = _calc_variance(flows, sdd, normed, vlim, palette) w = SankeyWidget(nodes=value['nodes'], links=value['links'], order=value['order'], layout=ipywidgets.Layout(width=str(width), height='400'), margins=dict(top=15, bottom=10, left=100, right=100)) colorbar( palette, vmin, vmax, 'Credible interval width' + (' (normalised)' if normed else ' [Mt]')) return w
def plot_sankey(flows, com_coord): com_coord = com_coord.append({'comarca':'Catalunya','latitude':0,'longitude':0},ignore_index=True) SankeyWidget(links=flows.to_dict('records')) nodes = { 'Comarcas_productoras': ProcessGroup(list(com_coord.comarca.unique())), 'Comarcas_entrega' : ProcessGroup(list(com_coord.comarca.unique())), } # productoras on the left, entrega on the right ordering = sorted([[key] for key,_ in nodes.items()],reverse=True) bundles = [ Bundle(sorted(list(nodes),reverse=True)[0], sorted(list(nodes),reverse=True)[1]), ] comarcas = Partition.Simple('process',list(com_coord.comarca.unique())) # Update the ProcessGroup nodes to use the partitions nodes['Comarcas_productoras'].partition = comarcas nodes['Comarcas_entrega'].partition = comarcas sdd = SankeyDefinition(nodes, bundles, ordering) return(sdd)
import pandas as pd #from plotly.utils import pandas flows = pd.read_csv('simple_fruit_sales.csv') flows from ipysankeywidget import SankeyWidget SankeyWidget(links=flows.to_dict('records'))
'color': 'violet' }, { 'source': 'NO*', 'target': 'NO', 'value': rate[12], 'color': 'orange' }, ] rank_sets = [ { 'type': 'same', 'nodes': ['N2', 'N2O', 'NO'] }, ] Pt211_500network = SankeyWidget(links=links, margins=dict(top=150, bottom=150, left=150, right=150), rank_sets=rank_sets) # In[ ]: Pt211_500network # In[ ]: # Pt211_500network.save_svg('Pt211_500network.svg')
def create_sankey_traffic_flow(filename): ''' Create sankey diagram to visualize traffic flow among stations in starting and ending on the date given. Input: Date :given in day,month,year.(User provided) starting: List of four letter code strings that traffic flow start from.(User provided) ending: List of four letter code strings that traffic flow end at.(User provided) filename: hourly data file. Make sure the years are equal.(User provided) Output: Opens svg daigram in browser. ''' assert isinstance(filename, str) #import libraries import pandas as pd import collections from ipysankeywidget import SankeyWidget #--- starting = ['EMBR', 'CIVC', 'MONT', 'POWL', '12TH'] ending = ['DUBL', 'BALB', '24TH', 'PHIL', 'DELN', 'WCRK', 'FRMT'] year = 2018 month = 8 day = 22 #read data fo = pd.read_csv(filename, index_col=0, header=None, names=['Date', 'Hour', 'Departing', 'Arriving', 'Count']) #find data on the date given fo_find_date = fo.loc['{}-0{}-{}'.format(year, month, day)] select_date = collections.OrderedDict() for i in starting: select_date[i] = fo_find_date[fo_find_date.Departing == i] #filter data based on starting and ending lists specific_start_end = collections.OrderedDict() startend_number = {} for start in starting: for end in ending: specific_start_end[start + end] = select_date[start][ select_date[start].Arriving == end] add_up = 0 for num in list(specific_start_end[start + end].Count): add_up += num startend_number[start + end] = add_up #create sankey diagram traffic_flow = [] for key in specific_start_end.keys(): sankey_dict = {} sankey_dict['source'] = specific_start_end[key]['Departing'].iloc[0] sankey_dict['target'] = specific_start_end[key]['Arriving'].iloc[0] sankey_dict['value'] = specific_start_end[key]['Count'].sum() traffic_flow.append(sankey_dict) color = ['grey', 'darkgrey', 'dimgrey', 'lightgrey', 'silver'] for col in color: for element in traffic_flow: for i in range(5): if element['source'] == starting[i]: element['color'] = color[i] s = SankeyWidget(links=traffic_flow, margins=dict(top=0, bottom=0, left=100, right=100)) return s