Example #1
0
 def make_annotations(pos, text, font_size=10, font_color='rgb(250,250,250)'):
     L=len(pos)
     if len(text)!=L:
         raise ValueError('The lists pos and text must have the same len')
     annotations = go.Annotations()
     for k in range(L):
         annotations.append(
             go.Annotation(
             text=labels[k], # or replace labels with a different list for the text within the circle
             x=pos[k][0], y=2*M-position[k][1],
             xref='x1', yref='y1',
             font=dict(color=font_color, size=font_size),
             showarrow=False)
         )
     return annotations
Example #2
0
def create_annotated_heatmap(x=None, y=None, z=None):
    data = go.Heatmap(z=z, x=x, y=y, colorscale='Viridis')

    annotations = go.Annotations()
    for n, row in enumerate(z):
        for m, val in enumerate(row):
            annotations.append(go.Annotation(text=str(z[n][m]), x=x[m], y=y[n], xref='x1', yref='y1', showarrow=False))

    fig = go.Figure(data=go.Data([data]))
    fig['layout'].update(
        annotations=annotations,
        xaxis=go.XAxis(ticks='', title='y_pred', side='bottom'),
        yaxis=go.YAxis(ticks='', title='y_true', ticksuffix='  '),  # ticksuffix is a workaround to add a bit of padding
        autosize=False
    )
    return json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
Example #3
0
def update_graph4(product_selected1, product_selected2, product_selected3):

    z = confusion_matrix(y_test, y_pred)

    trace15 = go.Heatmap(z=z,
                         x=['0', '1'],
                         y=['1', '0'],
                         colorscale=["#ab6d41", "#ffb785"])

    xx = [0, 1]
    yy = [1, 0]

    annotations = go.Annotations()
    for n, row in enumerate(z):
        for m, val in enumerate(row):
            annotations.append(
                go.Annotation(text=str(z[n][m]),
                              x=xx[m],
                              y=yy[n],
                              xref='x1',
                              yref='y1',
                              showarrow=False))

    return {
        'data': [trace15],
        'layout':
        go.Layout(
            font=dict(family="Arial Narrow, sans-serif",
                      color="black",
                      size=16),
            annotations=annotations,
            width=400,
            height=400,
            autosize=False,
            showlegend=False,
            yaxis=dict(tickmode='array',
                       tickvals=[-0.5, 0, 0.5, 1, 1.5],
                       ticktext=['', 'Self-harm', '', 'No<br>self-harm', '']),
            xaxis=dict(tickmode='array',
                       tickvals=[-0.5, 0, 0.5, 1, 1.5],
                       ticktext=['', 'No self-harm', '', 'Self-harm', '']),
            title='<b>Confusion Matrix</b>',
            yaxis_title_text='<b>actual</b>',
            xaxis_title_text='<b>predicted</b>',
            paper_bgcolor='#F5F5F7')
    }
Example #4
0
    def make_annotations(pos, info, font_size=8, font_color='rgb(0,0,0)'):
        L = len(pos)
        if len(info) != L:
            raise ValueError(
                'make_annotations()::The lists pos and info must have the same len'
            )

        annotations = go.Annotations()
        for k in range(L):
            item = go.Annotation(
                text=info[k],  # text to be shown within the node  
                x=pos[k][0],
                y=pos[k][1],
                xref='x1',
                yref='y1',
                font=dict(color=font_color, size=font_size),
                showarrow=False)
            annotations.append(item)

        return annotations
 def make_annotations(pos, text, font_size=14, font_color='#fff'):
     if len(text) != L:
         raise ValueError('The lists pos and text must have the same len')
     labels = go.Annotations()
     for k in pos:
         labels.append(
             go.Annotation(
                 text=annotations[vertex_to_i[k]],
                 x=pos[k][0],
                 y=2 * M - position[k][1],
                 xref='x1',
                 yref='y1',
                 font=dict(color=font_color, size=font_size),
                 showarrow=False,
                 bgcolor='#6175c1',
                 bordercolor='#c7c7c7',
                 borderwidth=1,
                 borderpad=2,
             ), )
     return labels
Example #6
0
def MakeAnnotations(positions,
                    texts,
                    font_size=10,
                    font_color='rgb(250,250,250)'):
    posLength = len(positions)
    textLength = len(texts)
    if (posLength != textLength):
        raise ValueError('Position and texts mismatch.')
    annotations = objs.Annotations()
    for k in range(posLength):
        currentColor = font_color
        currentText = texts[k]
        if (texts[k] == "None"):
            currentText = "X"
            currentColor = "red"
        annotations.append(
            objs.Annotation(text=currentText,
                            x=positions[k][0],
                            y=positions[k][1],
                            xref='x1',
                            yref='y1',
                            font=dict(color=currentColor, size=font_size),
                            showarrow=False))
    return annotations
Example #7
0
# Sums the Global sales of a game's genre and platform, and transforms
# it into a matrix for the heatmap
sales_df = games[['Genre', 'Platform', 'Global_Sales']]
Sales = sales_df.groupby(['Genre', 'Platform']).sum().unstack(fill_value=0)

# Average sales ṕer title
Popularity = Sales.values / Titles.values
Popularity = np.nan_to_num(Popularity)
Popularity = np.round(Popularity, 2)

# Extracts the labels for the legend
genres = list(Titles.index.values)
platforms = list(Titles.columns.values)

# Label of heatmap cells
annotations = go.Annotations()
for i, row in enumerate(Sales.values):
    for j, val in enumerate(row):
        annotations.append(
            go.Annotation(text=str(Popularity[i][j]),
                          x=platforms[j],
                          y=genres[i],
                          xref='x1',
                          yref='y1',
                          showarrow=False,
                          font={
                              'size': 11,
                              'color': 'white'
                          } if Popularity[i][j] < 4.5 else {
                              'size': 11,
                              'color': 'black'
Example #8
0
def plot_stacked_barchart_cath(stats_df, title, plot_out=None):

    plot_dict = {}
    for cath in np.unique(stats_df['cath_topology']):

        plot_dict[cath] = {}
        for dataset in np.unique(stats_df['dataset']):
            plot_dict[cath]['Set ' + str(dataset)] = len(stats_df.query('cath_topology == @cath and dataset == @dataset'))

    plot_df = pd.DataFrame(plot_dict)
    plot_df_relative = plot_df.apply(lambda x: x/np.sum(x), axis=1)
    plot_df.loc['Total'] = plot_df.sum(axis=0).tolist()
    plot_df_relative.loc['Total'] = plot_df.loc['Total'] / plot_df.loc['Total'].sum(axis=0)

    #add bar for every group == cath
    data = []
    for cath in plot_df.columns:
        data.append(
            go.Bar(
                x=plot_df_relative.index.tolist(),
                y=plot_df_relative[cath],
                showlegend=True,
                name=cath
            )
        )

    #add annotation for every bar
    y=0
    annotations_list = []
    for cath in plot_df.columns:
        y += plot_df_relative[cath]['Total']
        for dataset in plot_df.index.tolist():
            annotations_list.append(
                go.Annotation(
                    x=dataset,
                    y=y-0.1,
                    text=str(plot_df[cath][dataset]),
                    showarrow=False,
                    font=dict(color='#ffffff')
                )
            )

    plot = {
        "data": data,
        "layout": go.Layout(
            barmode="stack",
            title=title,
            yaxis=dict(
                title="Proportion of CATH classes",
                exponentformat="e",
                showexponent='All'
            ),
            annotations=go.Annotations(annotations_list),
            legend=dict(orientation="h"),
            font=dict(size=16)
        )
    }

    if title=="":
        plot['layout']['margin']['t'] = 10


    plotly_plot(plot, filename=plot_out, auto_open=False)
Example #9
0
def plot_scatter(apc_mat, ec_mat, plot_file):

    indices_i, indices_j = np.triu_indices(apc_mat.shape[0], k=1)
    apc = apc_mat[indices_i, indices_j]
    ec = ec_mat[indices_i, indices_j]

    text = [
        "i: {0}<br>j: {1}<br>apc:{2}<br>ec:{3}".format(i, j, apc_mat[i, j],
                                                       ec_mat[i, j])
        for i, j in zip(indices_i, indices_j)
    ]

    scatter_data = go.Scatter(x=apc,
                              y=ec,
                              mode='markers',
                              marker=dict(color="black"),
                              text=text,
                              showlegend=False)

    diagonal = go.Scatter(x=[0, np.max(list(apc) + list(ec))],
                          y=[0, np.max(list(apc) + list(ec))],
                          mode="lines",
                          line=dict(color="darkgrey", width=4, dash="dot"),
                          showlegend=False)

    pearson_r = pearsonr(apc, ec)

    data = []
    data.append(diagonal)
    data.append(scatter_data)

    fig = {
        "data":
        data,
        "layout":
        go.Layout(hovermode="text",
                  font=dict(size=24),
                  yaxis=dict(title="Entropy Correction",
                             exponentformat="e",
                             showexponent='All',
                             scaleratio=1,
                             scaleanchor='x'),
                  xaxis=dict(title="Average Product Correction",
                             exponentformat="e",
                             showexponent='All',
                             scaleratio=1,
                             scaleanchor='y'),
                  annotations=go.Annotations([
                      go.Annotation(x=0.05,
                                    y=0.95,
                                    showarrow=False,
                                    text='Pearson r = {0}'.format(
                                        np.round(pearson_r[0], decimals=3)),
                                    font=dict(color="black", size=24),
                                    xref='paper',
                                    yref='paper')
                  ]),
                  margin=dict(t=10),
                  width="550",
                  height="500")
    }

    plotly_plot(fig, filename=plot_file, auto_open=False, show_link=False)
def plot_slot_avail(node_info, sec_since_epoch=None):
    """Plot "slot" availability.

    Parameters
    ----------
    node_info : pandas.DataFrame
    sec_since_epoch : numeric, optional
        If provided, a date-time stamp will be displayed on the plots
        indicating the time the information displayed was generated.

    """
    if DEBUG:
        t0 = time.time()
        print(">> plot_slot_avail")

    updated_at = ""
    if sec_since_epoch is not None:
        datetime_stamp, _ = make_datetime_stamp(sec_since_epoch,
                                                human_readable=True)
        updated_at = "Updated {}".format(datetime_stamp)

    slot_cores = np.array(
        [1, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 24, 28, 32, 40])
    slot_mem = 2**np.arange(0, 11)

    cluster_node_info = OrderedDict([
        ("ACI CPU", node_info.query('cluster == "aci"')),
        (
            "CyberLAMP CPU-Only",
            node_info.query('(cluster == "cyberlamp") & (subgroup != "phi")'),
        ),
        (
            "CyberLAMP Single-GPU",
            node_info.query('(cluster == "cyberlamp") & (subgroup != "phi")'),
        ),
    ])

    for cluster_name, cni in cluster_node_info.items():
        print(">>   Working on {} cluster".format(cluster_name))
        # TODO: cyberlamp has different CPUs available on the 1-GPU nodes for
        # GPU vs. non-GPU jobs. Knowing which cores are "taken" (or if it's
        # just a count, not particular cores that are dedicatd to job type) is
        # not possible as the pbsnodes parsing script stands (might need data
        # from another source about actual jobs that are running on each node
        # and what they call out).

        sc = slot_cores[slot_cores <= cni.cores.max()]
        sm = slot_mem[slot_mem <= cni.mem_tot.max()]
        if "gpu" in cluster_name.lower():
            sg = 1
            sc = sc[sc <= 20]
            sm = sm[sm <= 242]
        else:
            sg = 0

        slots = compute_open_slots(node_info=cni,
                                   slot_cores=sc,
                                   slot_mem=sm,
                                   slot_gpus=sg)

        if np.all(slots <= 3):
            log_slots = slots
        else:
            with np.errstate(divide="ignore"):
                log_slots = np.log10(slots)
            neginf_mask = np.isinf(log_slots)
            neginf_fill_val = -np.max(log_slots) / 6
            log_slots[neginf_mask] = neginf_fill_val

        min_slots = slots.min()
        max_slots = slots.max()

        lin_tickvals = np.array(
            [1, 3, 10, 30, 100, 300, 1e3, 3e3, 1e4, 3e4, 1e5, 3e5, 1e6],
            dtype=int)
        lin_tickvals = lin_tickvals[(lin_tickvals > min_slots)
                                    & (lin_tickvals < max_slots)]

        if np.all(slots <= 3):
            lin_tickvals = np.array([0, max_slots], dtype=int)
            tickvals = lin_tickvals
        else:
            if len(lin_tickvals) < 3:
                order = np.floor(np.log10(min_slots))
                lin_tickvals = np.arange(
                    10**np.floor(np.log10(min_slots)),
                    max_slots + 1,
                    10**order,
                    dtype=int,
                )

            lin_tickvals = np.array(sorted(
                set(lin_tickvals.tolist() + [max_slots])),
                                    dtype=int)
            tickvals = np.log10(lin_tickvals)

            lin_tickvals = sorted(set([0] + lin_tickvals.tolist()))
            tickvals = [neginf_fill_val] + tickvals.tolist()

        ticktext = ["{:d}".format(tv) for tv in lin_tickvals]

        text = np.empty_like(slots, dtype=str).tolist()
        for (core_i, cores), (mem_i, mem) in product(enumerate(sc),
                                                     enumerate(sm)):
            if cores == 1:
                ct = "core"
            else:
                ct = "cores"
            val = slots[core_i, mem_i]
            if val == 1:
                slt = "slot"
            else:
                slt = "slots"
            text[core_i][
                mem_i] = "{:d} {:s} & {:d} GiB mem : {:d} {:s}".format(
                    cores, ct, mem, val, slt)

        # print(f"slots\n{slots}")
        # print(f"log_slots\n{log_slots}")
        # print(f"tickvals\n{tickvals}")

        trace = graph_objs.Heatmap(
            z=log_slots,
            x=[str(m) + " GiB" for m in sm],
            y=[str(sc[0]) + " core"] + [str(c) + " cores" for c in sc[1:]],
            zsmooth=False,
            xgap=1,
            ygap=1,
            colorscale="Viridis",
            colorbar=dict(
                outlinewidth=0,
                tickvals=tickvals,
                ticktext=ticktext,
            ),
            hoverinfo="text",
            text=text,
        )
        data = [trace]
        layout = graph_objs.Layout(
            title="{} Job Slots Available".format(cluster_name),
            annotations=graph_objs.Annotations([
                graph_objs.Annotation(
                    x=0.5,
                    y=1.07,
                    showarrow=False,
                    text=updated_at,
                    xref="paper",
                    yref="paper",
                ),
            ]),
            xaxis=dict(ticks="", nticks=36),
            yaxis=dict(ticks=""),
        )
        fig = graph_objs.Figure(data=data, layout=layout)
        plotly.plot(fig, filename=cluster_name, auto_open=False)

    if DEBUG:
        print(">>   plot_slot_avail:", time.time() - t0)
Example #11
0
def update_graph(occupation_data, skill_data):
    """
	 Creates the 3D movable graph to visualize 
	 the common skills between respective roles
	
	 """

    input_data = occupation_data + skill_data
    input_data = [a.lower() for a in input_data]

    if input_data == []:

        fix_position_list = [
            'web developer', 'ict project manager', 'ict business analyst',
            'software developer', 'ict network engineer',
            'database administrator', 'ict consultant', 'data analyst',
            'ict application developer', 'mobile app developer'
        ]

        for key, value in graph_visualization_data.items():
            if key == 'nodes':
                nodes_new = [
                    k for k in value if k['id'].lower() in fix_position_list
                ]
            if key == 'links':
                links_new = [
                    k for k in value
                    if k['source'].lower() in fix_position_list
                    or k['target'].lower() in fix_position_list
                ]

        new_graph_data = {'nodes': nodes_new, 'links': links_new}

        G = nx.json_graph.node_link_graph(new_graph_data)
        pos = nx.spring_layout(G, dim=3)

        Xn_role = [pos[k][0] for k in pos if k in occupations]
        Yn_role = [pos[k][1] for k in pos if k in occupations]
        Zn_role = [pos[k][2] for k in pos if k in occupations]

        role_labels = [k for k in pos if k in occupations]

        Xn_skill = [pos[k][0] for k in pos if k in skills_graph]
        Yn_skill = [pos[k][1] for k in pos if k in skills_graph]
        Zn_skill = [pos[k][2] for k in pos if k in skills_graph]

        skill_labels = [k for k in pos if k in skills_graph]

        Xe = []
        Ye = []
        Ze = []
        for e in G.edges():
            Xe.extend([pos[e[0]][0], pos[e[1]][0], None])
            Ye.extend([pos[e[0]][1], pos[e[1]][1], None])
            Ze.extend([pos[e[0]][2], pos[e[1]][2], None])

        trace_nodes_role = go.Scatter3d(x=Xn_role,
                                        y=Yn_role,
                                        z=Zn_role,
                                        mode='markers',
                                        marker=dict(size='18',
                                                    symbol='dot',
                                                    color='rgb(255,140,0)'),
                                        text=role_labels,
                                        hoverinfo='text')

        trace_nodes_skill = go.Scatter3d(x=Xn_skill,
                                         y=Yn_skill,
                                         z=Zn_skill,
                                         mode='markers',
                                         marker=dict(symbol='dot',
                                                     color='rgb(33,188,235)'),
                                         text=skill_labels,
                                         hoverinfo='text')

        trace_edges = go.Scatter3d(mode='lines',
                                   x=Xe,
                                   y=Ye,
                                   z=Ze,
                                   line=dict(width=0.6,
                                             color='rgb(119,136,153)'),
                                   hoverinfo='none')

        axis = dict(
            showline=False,  # hide axis line, grid, ticklabels and  title
            zeroline=False,
            showgrid=False,
            showticklabels=False,
            showbackground=False,
            title='')

        layout_skill_graph = go.Layout(
            title="Skill tree for Jobs",
            titlefont=dict(color='white'),
            showlegend=False,
            scene=go.Scene(
                xaxis=go.XAxis(axis),
                yaxis=go.YAxis(axis),
                zaxis=go.ZAxis(axis),
            ),
            margin=dict(l=0, r=0, t=40, b=0),
            hovermode='closest',
            plot_bgcolor='#131827',
            paper_bgcolor='#131827',  #set background color            
            annotations=go.Annotations([
                go.Annotation(showarrow=False,
                              text="",
                              xref='paper',
                              yref='paper',
                              x=0,
                              y=0.1,
                              xanchor='left',
                              yanchor='bottom',
                              font=go.Font(size=14))
            ]))

        data = go.Data([trace_edges, trace_nodes_role, trace_nodes_skill])
        fig = go.Figure(data=data, layout=layout_skill_graph)

        return fig

    else:

        for key, value in graph_visualization_data.items():
            if key == 'nodes':
                nodes_new = [k for k in value if k['id'].lower() in input_data]
            if key == 'links':
                links_new = [
                    k for k in value if k['source'].lower() in input_data
                    or k['target'].lower() in input_data
                ]

        new_graph_data = {'nodes': nodes_new, 'links': links_new}

        G = nx.json_graph.node_link_graph(new_graph_data)
        pos = nx.spring_layout(G, dim=3)

        Xn_role = [pos[k][0] for k in pos if k in occupations]
        Yn_role = [pos[k][1] for k in pos if k in occupations]
        Zn_role = [pos[k][2] for k in pos if k in occupations]

        role_labels = [k for k in pos if k in occupations]

        Xn_skill = [pos[k][0] for k in pos if k in skills_graph]
        Yn_skill = [pos[k][1] for k in pos if k in skills_graph]
        Zn_skill = [pos[k][2] for k in pos if k in skills_graph]

        skill_labels = [k for k in pos if k in skills_graph]

        Xe = []
        Ye = []
        Ze = []
        for e in G.edges():
            Xe.extend([pos[e[0]][0], pos[e[1]][0], None])
            Ye.extend([pos[e[0]][1], pos[e[1]][1], None])
            Ze.extend([pos[e[0]][2], pos[e[1]][2], None])

        trace_nodes_role = go.Scatter3d(x=Xn_role,
                                        y=Yn_role,
                                        z=Zn_role,
                                        mode='markers',
                                        marker=dict(size='18',
                                                    symbol='dot',
                                                    color='rgb(255,140,0)'),
                                        text=role_labels,
                                        hoverinfo='text')

        trace_nodes_skill = go.Scatter3d(x=Xn_skill,
                                         y=Yn_skill,
                                         z=Zn_skill,
                                         mode='markers',
                                         marker=dict(symbol='dot',
                                                     color='rgb(33,188,235)'),
                                         text=skill_labels,
                                         hoverinfo='text')

        trace_edges = go.Scatter3d(mode='lines',
                                   x=Xe,
                                   y=Ye,
                                   z=Ze,
                                   line=dict(width=0.6,
                                             color='rgb(119,136,153)'),
                                   hoverinfo='none')

        axis = dict(
            showline=False,  # hide axis line, grid, ticklabels and  title
            zeroline=False,
            showgrid=False,
            showticklabels=False,
            showbackground=False,
            title='')

        layout_skill_graph = go.Layout(
            title="Skill tree for Jobs",
            titlefont=dict(color='white'),
            showlegend=False,
            scene=go.Scene(
                xaxis=go.XAxis(axis),
                yaxis=go.YAxis(axis),
                zaxis=go.ZAxis(axis),
            ),
            margin=dict(l=0, r=0, t=40, b=0),
            hovermode='closest',
            plot_bgcolor='#131827',
            paper_bgcolor='#131827',  #set background color            
            annotations=go.Annotations([
                go.Annotation(showarrow=False,
                              text="",
                              xref='paper',
                              yref='paper',
                              x=0,
                              y=0.1,
                              xanchor='left',
                              yanchor='bottom',
                              font=go.Font(size=14))
            ]))

        data = go.Data([trace_edges, trace_nodes_role, trace_nodes_skill])
        fig = go.Figure(data=data, layout=layout_skill_graph)

        return fig
def plot_boxplot(statistics_dict, key, plot_out):

    methods=sorted(statistics_dict.keys())

    data = []
    annotations_list = []
    max_value=-np.inf
    min_value=np.inf
    for method in methods:
        values = statistics_dict[method][key]
        proteins = statistics_dict[method]['protein']
        target_neff = statistics_dict[method]['target neff']
        sample_neff = statistics_dict[method]['sample neff']

        box = go.Box(
            y=values,
            boxmean=True,
            pointpos=1.8,
            jitter=0.4,
            boxpoints='all',
            name=method,
            marker=dict(opacity=1),
            text=[proteins[i] + "<br>target neff: " + str(target_neff[i]) + "<br>sample neff: " + str(sample_neff[i]) for i in range(len(values))],
            hoverinfo='all',
            orientation='v',
            showlegend=False
        )

        data.append(box)

        max_value = np.max([max_value, np.max(values)])
        min_value = np.min([min_value, np.min(values)])

    for method in methods:
        annotations_list.append(
                go.Annotation(
                    x=method,
                    y=max_value + (max_value-min_value)/10.0,
                    text=str(len(statistics_dict[method][key])),
                    showarrow=False
                )
            )


    plot = {
        "data": data,
        "layout": go.Layout(
            yaxis=dict(
                exponentformat='e',
                showexponent='All'
            ),
            annotations=go.Annotations(annotations_list),
            font=dict(size=18)
        )
    }

    if key == "neff_difference":
        plot['layout']['title'] = "Neff Difference between original Pfam and synthetic alignment"
        plot['layout']['yaxis']['title'] = "Pfam Neff - synthetic Neff"
    if key == "mutation_rate":
        plot['layout']['title'] = "Mutation rate used for Sampling"
        plot['layout']['yaxis']['title'] = "mutation rate"

    plotly_plot(plot, filename=plot_out, auto_open=False)
Example #13
0
    def get_plotly_networkx_3d(self, varset="all", include_plotlyjs=False):
        import networkx as nx
        g, edge_labels = nx.Graph(), {}
        for i, (name, var) in enumerate(self.items()):
            #if i == 5: break
            if varset != "all" and var.varset != varset: continue
            g.add_node(var, name=name)
            for parent in var.get_parents():
                #print(parent, "is parent of ", name)
                parent = self[parent]
                g.add_edge(parent, var)

                # TODO: Add getters! What about locked nodes!
                #i = [dep.node for dep in child.deps].index(task)
                #edge_labels[(task, child)] = " ".join(child.deps[i].exts)

        # Get positions for all nodes using layout_type.
        # e.g. pos = nx.spring_layout(g)
        #pos = getattr(nx, layout_type + "_layout")(g) #, scale=100000, iterations=30)
        pos = nx.spring_layout(g, dim=3)

        import plotly.graph_objs as go
        trace1 = go.Scatter3d(x=[],
                              y=[],
                              z=[],
                              mode='lines',
                              line=go.Line(width=2.0, color='#888'), #, dash="dot"),
                              hoverinfo='none',
                       )

        for edge in g.edges():
            x0, y0, z0 = pos[edge[0]]
            x1, y1, z1 = pos[edge[1]]
            trace1['x'] += [x0, x1, None]
            trace1['y'] += [y0, y1, None]
            trace1['z'] += [z0, z1, None]

        trace2 = go.Scatter3d(
                       x=[],
                       y=[],
                       z=[],
                       name='variable',
                       marker=go.Marker(symbol='dot',
                                     size=10,
                                     #color=group,
                                     colorscale='Viridis',
                                     line=go.Line(width=2)
                                     ),
                       text=[v.name for v in g],
                       textposition='center',
                       mode='markers+text',
                       hoverinfo='text',
                       hovertext=[v.mnemonics for v in g.nodes()],
                       )

        for node in g.nodes():
            x, y, z = pos[node]
            trace2['x'].append(x)
            trace2['y'].append(y)
            trace2['z'].append(z)

        axis=dict(showbackground=False,
                  showline=False,
                  zeroline=False,
                  showgrid=False,
                  showticklabels=False,
                  title=''
                  )

        layout = go.Layout(
            title="Abinit variables (3D visualization)",
            width=1000,
            height=1000,
            showlegend=False,
            scene=go.Scene(
                xaxis=go.XAxis(axis),
                yaxis=go.YAxis(axis),
                zaxis=go.ZAxis(axis),
            ),
            margin=go.Margin(t=100),
            hovermode='closest',
            annotations=go.Annotations([go.Annotation(
                showarrow=False,
                #text="Data source: <a href='http://bost.ocks.org/mike/miserables/miserables.json'>[1]</a>",
                text="Hello",
                xref='paper',
                yref='paper',
                x=0,
                y=0.1,
                xanchor='left',
                yanchor='bottom',
                font=go.Font(size=14),
                )]),
            )

        data = go.Data([trace1, trace2])
        fig = go.Figure(data=data, layout=layout)

        import plotly
        s = plotly.offline.plot(fig, include_plotlyjs=include_plotlyjs, output_type='div')
        return s
Example #14
0
data = [go.Histogram(x=ingredients_count)]
layout = layout = go.Layout(
    title='How many ingredients make up a dinner recipe?',
    xaxis=dict(title='Number of ingredients in each recipe'),
    yaxis=dict(title='Number of recipes with x number of ingredients'),
    bargap=0.1,
    bargroupgap=0.1,
    annotations=go.Annotations([
        go.Annotation(
            x=0,
            y=0,
            showarrow=False,
            text='<a href="https://twitter.com/rikunert">@rikunert</a>',
            xanchor='left',
            xref='paper',
            yref='paper'),
        go.Annotation(x=1,
                      y=0,
                      showarrow=False,
                      text='source: <a href="https://cookpad.com">cookpad</a>',
                      xanchor='right',
                      xref='paper',
                      yref='paper')
    ]))
fig = go.Figure(data=data, layout=layout)
po.plot(fig, filename='file.html')
#py.iplot(data, filename='basic histogram')

# let's get an idea of how many times different ingredients actually occur
ingredients_recipe = [r['ingredients'] for r in recipe_info]
ingredients = [
    showline=True,
    zeroline=True,
    mirror="ticks",
    ticks="inside",
    linewidth=2,
    tickwidth=2,
    zerolinewidth=2,
)

bandlayout = go.Layout(
    width=700,
    height=500,
    title="Bands diagram",
    xaxis=bandxaxis,
    yaxis=bandyaxis,
    annotations=go.Annotations(annotations)
)

bandfig = go.Figure(data=bandTraces + vlines, layout=bandlayout)
plot_url = iplot(bandfig, filename="Bands")


# In[14]:


# plot DOS and bands together
dosbandfig = make_subplots(rows=1, cols=2, shared_yaxes=True)
# add the bands
for btrace in bandTraces:
    dosbandfig.append_trace(btrace, 1, 1)
# add vlines for specific k-points
Example #16
0
def generate_comparison_plot(plot_data, data_keys, field_names=None):
    print(field_names)
    if field_names is None:
        field_names = data_keys
    ptitle = 'MegaQC Comparison Plot'
    plot_x = []
    plot_y = []
    plot_z = []
    plot_col = []
    plot_size = []
    plot_names = []
    annotations = go.Annotations([])
    for s_name in plot_data:
        plot_names.append(s_name)
        try:
            plot_x.append(plot_data[s_name][data_keys['x']])
            plot_y.append(plot_data[s_name][data_keys['y']])
        except KeyError:
            print("Couldn't find key {} (available: {})".format(plot_data[s_name].keys(), data_keys))
        try:
            plot_z.append(plot_data[s_name][data_keys['z']])
        except KeyError:
            plot_z.append(None)
        try:
            plot_col.append(plot_data[s_name][data_keys['col']])
        except KeyError:
            plot_col.append(None)
        try:
            plot_size.append(plot_data[s_name][data_keys['size']])
        except KeyError:
            plot_size.append(None)

    # Colour with a colour scale
    markers = {}
    if not all([x == None for x in plot_col]):
        markers['color'] = plot_col
        markers['colorscale'] = 'Viridis'
        markers['showscale'] = True
        annotations.append(go.Annotation(
            text = field_names['col'],
            x = 1.02,
            y = 0.5,
            textangle= - 90,
            xref = 'paper',
            yref = 'paper',
            showarrow = False
        ))

    # Scale the marker size according to a variable
    if not all([x == None for x in plot_size]):
        smax = max([x for x in plot_size if type(x) is float])
        smin = min([x for x in plot_size if type(x) is float])
        srange = smax - smin
        if srange > 0:
            norm_plot_size = []
            for x in plot_size:
                if type(x) is float:
                    norm_plot_size.append((((x - smin)/srange)*35)+2)
                else:
                    norm_plot_size.append(2)
            markers['size'] = norm_plot_size
            ptitle += '<br><span style="font-size:0.7rem">Marker Size represents "{}"</span>'.format(field_names['size'])

    plot_height = 600
    if all([x == None for x in plot_z]):
        fig = go.Scatter(
            x = plot_x,
            y = plot_y,
            mode = 'markers',
            marker = markers,
            text = plot_names
        )
    else:
        markers.update({'opacity':0.8})
        fig = go.Scatter3d(
            x = plot_x,
            y = plot_y,
            z = plot_z,
            mode = 'markers',
            marker = markers,
            text = plot_names
        )
        plot_height = 800
    # Make the plot
    layout = go.Layout(
        title = ptitle,
        # For 2D plots
        xaxis = dict(
            title = field_names['x']
        ),
        yaxis = dict(
            title = field_names['y']
        ),
        # For 3D plots
        scene = dict(
            xaxis = dict(
                title = field_names['x']
            ),
            yaxis = dict(
                title = field_names['y']
            ),
            zaxis = dict(
                title = field_names['z']
            ),
        ),
        annotations = annotations,
        height = plot_height
    )
    plot_div = py.plot(
        go.Figure(data = [fig], layout = layout),
        output_type = 'div',
        show_link = False,
        config = dict(
            modeBarButtonsToRemove = [
                'sendDataToCloud',
                'resetScale2d',
                'hoverClosestCartesian',
                'hoverCompareCartesian',
                'toggleSpikelines'
            ],
            displaylogo = False
        )
    )
    return plot_div
def plot_scatter_comparison(title,
                            x_axis_title,
                            y_axis_title,
                            mat_1,
                            mat_2,
                            plot_out,
                            color_vector=None,
                            qqplot=False):

    L = mat_1.shape[0]
    upper_triangular_indices = np.triu_indices(L, k=1)

    score_1 = mat_1[upper_triangular_indices]
    score_2 = mat_2[upper_triangular_indices]

    lin_reg_x = list(
        np.arange(np.min([np.min(score_1), np.min(score_2)]),
                  np.max([np.max(score_1), np.max(score_2)]), 0.05))
    slope, intercept, rvalue, pvalue, stderr = linregress(score_1, score_2)
    lin_reg_y = [intercept + slope * x for x in lin_reg_x]

    text = [
        "i: " + str(i + 1) + ", j: " + str(j + 1) for i, j in zip(
            upper_triangular_indices[0], upper_triangular_indices[1])
    ]

    if color_vector is not None:
        text = [
            text[ij] + ", sum_nia * sum_njb: " + str(color_vector[ij])
            for ij in range(len(text))
        ]
        color_scale = cl.interp(cl.scales['3']['seq']['Reds'], 400)
        color_vector = [color_scale[i - 1] for i in color_vector]
        opacity = 1
    else:
        color_vector = 'rgb(31,120,180)'
        opacity = 1

    data = []

    data.append(
        go.Scattergl(x=[
            np.min([np.min(score_1), np.min(score_2)]),
            np.min([np.max(score_1), np.max(score_2)])
        ],
                     y=[
                         np.min([np.min(score_1),
                                 np.min(score_2)]),
                         np.min([np.max(score_1),
                                 np.max(score_2)])
                     ],
                     mode='lines',
                     line=dict(color='lightgrey', width=3, dash='dash'),
                     showlegend=False))

    data.append(
        go.Scattergl(x=score_1,
                     y=score_2,
                     text=text,
                     mode='markers',
                     marker=dict(opacity=opacity, color=color_vector),
                     hoverinfo="x+y+text",
                     showlegend=False))

    if qqplot:

        index_sorted_i = np.argsort(score_1)
        index_sorted_j = np.argsort(score_2)

        text_sorted = [
            "i: " + str(i + 1) + ", j: " + str(j + 1)
            for i, j in zip(upper_triangular_indices[0][index_sorted_i],
                            upper_triangular_indices[1][index_sorted_j])
        ]

        data.append(
            go.Scattergl(x=sorted(score_1),
                         y=sorted(score_2),
                         text=text_sorted,
                         mode='markers',
                         marker=dict(color="rgb(255,127,0)"),
                         hoverinfo="x+y+text",
                         showlegend=False))

    data.append(
        go.Scattergl(
            x=lin_reg_x,
            y=lin_reg_y,
            mode='lines',
            line=dict(color="black",
                      width=4),  #"rgb(166,206,227)"),#"rgb(51,160,44)"),
            showlegend=False))

    plot = {
        "data":
        data,
        "layout":
        go.Layout(annotations=go.Annotations([
            go.Annotation(x=np.percentile(lin_reg_x, 95),
                          y=np.percentile(lin_reg_y, 95),
                          showarrow=True,
                          ax=30,
                          ay=50,
                          arrowcolor='black',
                          arrowside="start",
                          text='y = {0} + {1}x'.format(
                              np.round(intercept, decimals=3),
                              np.round(slope, decimals=3)),
                          font=dict(color="black", size=18),
                          xref='x1',
                          yref='y1')
        ]),
                  title=title,
                  font=dict(size=18),
                  yaxis1=dict(
                      title=y_axis_title,
                      exponentformat="e",
                      showexponent='All',
                      scaleratio=1.0,
                      scaleanchor='x',
                  ),
                  xaxis1=dict(title=x_axis_title,
                              exponentformat="e",
                              showexponent='All',
                              scaleratio=1.0,
                              scaleanchor='y'))
    }

    plotly_plot(plot, filename=plot_out, auto_open=False)
Example #18
0
def corrPlot(
        x,  # 1D data vector or list of 1D dsata vectors
        y,  # 1D data vector or list of 1D dsata vectors
        z=None,  # optional colors for the lines
        names=None,  # names of x, y (ex:['A', 'B']
        maxdata=2000,  # max # of points to plot above histogram (if too high, it will be slow)
        addCorr=True,  # whether to add correlation statistics into plot (R2, spearmanR2, Pvals, & y=mx+b)
        addCorrLine=True,  # whether to plot correlation line
        addXYline=False,  # whether to plot y=x line
        text=None,  # whether to add additional text to each point
        plot=True,  # if false, just returns plotly json object
        title='Correlation',  # title of plot
        xlbl='',  #
        ylbl='',
        markersize=None,  # either None or #. If None, will automatically determine best
):
    """
    Plots x , y data and their trendline using plotly

    EX: plot diff between two series
        corrPlot(x, y, xlbl='A', ylbl='B', addCorr=False, addCorrLine=False, addXYline=True)
    """
    #TODO: remove outliers

    # 1st convert t ndarray

    # 1st convert t ndarray
    if type(x) != np.ndarray: x = np.array(x)
    if type(y) != np.ndarray: y = np.array(y)

    # (1) get N
    if np.issubdtype(x.dtype, np.number):  # given an np array
        x = np.atleast_2d(x)
        y = np.atleast_2d(y)
        N, Lx = x.shape
    else:  # given a data array
        N = len(x)

    # (2) remove NaNs
    tmpx, tmpy = [], []
    for n in range(N):
        bad = np.atleast_2d(np.isnan(x[n]) | np.isnan(y[n]))
        tmpx += [x[n][~bad[0]]]
        tmpy += [y[n][~bad[0]]]
    x = np.array(tmpx)
    y = np.array(tmpy)

    # (3) get Lx
    if np.issubdtype(x.dtype, np.number):  # given an np array
        N, Lx = x.shape
        Lx = np.tile(Lx, N)
    else:  # given a data array
        Lx = [len(l) for l in x]
        Ly = [len(l) for l in y]
        if Lx != Ly:
            raise ValueError('All x & y vectors must be same length!!!')

    # if data has too many points, remove some for speed
    Iplot = [
        np.arange(Lx[n]) if Lx[n] < maxdata else np.random.choice(
            Lx[n], size=maxdata, replace=False) for n in range(N)
    ]

    if names is None:
        names = ['Line ' + str(i) for i in range(N)]
    if isinstance(names, str):
        names = [names]

    traces = []

    # determine scatterpoint colors
    if z is not None:
        assert N == 1, 'So far coloring only works w/ 1 data series'
        if type(z) != np.ndarray: z = np.array(z)
        z = np.atleast_2d(z)
        cols = z
        showleg = False
        showscale = True
        line_col = ['black']
        lg = [None]
        scattertext = ['z=%d' % (i)
                       for i in range(Lx[0])] if text is None else text
    else:
        if N > 1:
            lg = names
            showleg = False
            cols = cl.scales[str(max(3, N))]['qual']['Set1']
        else:
            lg = [None]
            showleg = True
            cols = ['blue']
        line_col = cols
        showscale = False
        if text is None:
            scattertext = ''
        else:
            scattertext = text

    # scale markersize
    Lxp = np.min([max(Lx), maxdata])
    if markersize is None:
        if Lxp > 5000:
            markersize = 1
        elif Lxp > 2000:
            markersize = 2
        elif Lxp > 1000:
            markersize = 3
        elif Lxp > 200:
            markersize = 4
        elif Lxp > 80:
            markersize = 5
        elif Lxp > 25:
            markersize = 7
        else:
            markersize = 9

    scatPlot = [
        go.Scatter(x=x[n][Iplot[n]],
                   y=y[n][Iplot[n]],
                   name=names[n],
                   legendgroup=lg[n],
                   mode='markers',
                   opacity=.5,
                   text=scattertext,
                   marker={
                       'size': markersize,
                       'color': cols[n],
                       'showscale': showscale,
                       'colorscale': 'Portland'
                   }) for n in range(N)
    ]
    traces += scatPlot

    annots = []
    if addCorr:
        for n in range(N):
            slope, intercept, R2, p_val, std_err = sp.stats.linregress(
                x[n], y[n])
            R2sp, p_val_sp = sp.stats.spearmanr(x[n], y[n])
            corrtext = 'Pearson [R2, P]=[%.2f,%.2f] <br> ' \
                       'Spearman [R2, P]=[%.2f,%.2f] <br> ' \
                       'y=%.2fx+%.2f' \
                       % (R2, p_val, R2sp, p_val_sp, slope, intercept)
            #if only 1 data record print stats on graph
            if N == 1:
                annots = go.Annotations([
                    go.Annotation(x=0.05,
                                  y=0.95,
                                  showarrow=False,
                                  text=corrtext,
                                  xref='paper',
                                  yref='paper')
                ])

            if addCorrLine:
                x_rng = [np.min(x[0]), np.max(x[0])]
                dx_rng = x_rng[1] - x_rng[0]
                shift = .03  # shift from edges
                xc = np.array(
                    [x_rng[0] + dx_rng * shift, x_rng[1] - dx_rng * shift])
                yc = slope * xc + intercept
                corrline = [
                    go.Scatter(x=xc,
                               y=yc,
                               name=names[n] + ' corr',
                               legendgroup=lg[n],
                               showlegend=showleg,
                               mode='lines',
                               line={'color': line_col[n]},
                               hovertext=corrtext,
                               hoverinfo='name+text')
                ]
                traces += corrline

    if addXYline:
        x_rng = [np.min(x[0]), np.max(x[0])]
        dx_rng = x_rng[1] - x_rng[0]
        shift = .03  # shift from edges
        xc = np.array([x_rng[0] + dx_rng * shift, x_rng[1] - dx_rng * shift])
        xyline = [
            go.Scatter(x=xc,
                       y=xc,
                       name='X=Y',
                       showlegend=True,
                       mode='lines',
                       line={'color': 'black'})
        ]
        traces += xyline

    showleg = False if N == 1 else True

    layout = go.Layout(
        title=title,
        annotations=annots,
        xaxis={'title': xlbl},
        yaxis={'title': ylbl},
        hovermode='closest',
        showlegend=showleg,
    )
    fig = go.Figure(data=traces, layout=layout)

    return plotOut(fig, plot)
Example #19
0
def main():
    # Centrality for graph representation.
    centrality = Centrality.Centrality(Graph(),
                    "../FacebookNetwork/dataset/SplitedData/train.edges",
                    "../FacebookNetwork/dataset/SplitedData/val.edges",
                    "../FacebookNetwork/dataset/SplitedData/test.edges",
                    "../FacebookNetwork/dataset/SplitedData/neg_train.edges",
                    "../FacebookNetwork/dataset/SplitedData/neg_val.edges",
                    "../FacebookNetwork/dataset/SplitedData/neg_test.edges")

    centrality.arg.LoadEdgesData()
    betweenness = centrality.BetweenCentrality()
    closeness = centrality.ClosenessCentrality()
    eigenVec = centrality.EigenVector_Centrality()

    #PredictionModels, Split positive and negative data
    pos_train = DataReader.DataReader(Graph(),
                    "../FacebookNetwork/dataset/SplitedData/train.edges",
                    "../FacebookNetwork/dataset/SplitedData/val.edges",
                    "../FacebookNetwork/dataset/SplitedData/test.edges",
                    "../FacebookNetwork/dataset/SplitedData/neg_train.edges",
                    "../FacebookNetwork/dataset/SplitedData/neg_val.edges",
                    "../FacebookNetwork/dataset/SplitedData/neg_test.edges")
    neg_train = DataReader.DataReader(Graph(),
                    "../FacebookNetwork/dataset/SplitedData/train.edges",
                    "../FacebookNetwork/dataset/SplitedData/val.edges",
                    "../FacebookNetwork/dataset/SplitedData/test.edges",
                    "../FacebookNetwork/dataset/SplitedData/neg_train.edges",
                    "../FacebookNetwork/dataset/SplitedData/neg_val.edges",
                    "../FacebookNetwork/dataset/SplitedData/neg_test.edges")
    pos_val = DataReader.DataReader(Graph(),
                    "../FacebookNetwork/dataset/SplitedData/train.edges",
                    "../FacebookNetwork/dataset/SplitedData/val.edges",
                    "../FacebookNetwork/dataset/SplitedData/test.edges",
                    "../FacebookNetwork/dataset/SplitedData/neg_train.edges",
                    "../FacebookNetwork/dataset/SplitedData/neg_val.edges",
                    "../FacebookNetwork/dataset/SplitedData/neg_test.edges")
    neg_val = DataReader.DataReader(Graph(),
                    "../FacebookNetwork/dataset/SplitedData/train.edges",
                    "../FacebookNetwork/dataset/SplitedData/val.edges",
                    "../FacebookNetwork/dataset/SplitedData/test.edges",
                    "../FacebookNetwork/dataset/SplitedData/neg_train.edges",
                    "../FacebookNetwork/dataset/SplitedData/neg_val.edges",
                    "../FacebookNetwork/dataset/SplitedData/neg_test.edges")

    #Load Edges for each graph
    pos_train.LoadEdgesData()
    neg_train.LoadEdgesData()
    pos_val.LoadEdgesData()
    neg_val.LoadEdgesData()

    #Split into negative and positive files.
    pos_train.GenerateTrain() #we will get pos and neg files.
    pos_val.GenerateValidation() #we will get pos and neg files.

    #LabelData into dictionnary for files
    pos_train.LoadGeneratedFiles(pos_train.fluxTr)
    neg_train.LoadGeneratedFiles(neg_train.fluxTr_neg)
    pos_val.LoadGeneratedFiles(pos_val.fluxV)
    neg_val.LoadGeneratedFiles(neg_val.fluxVal_neg)

    X_train_pos = pos_train.LabelData()
    X_train_neg = neg_train.LabelData()

    X_val_pos = pos_val.LabelData()
    X_val_neg = neg_val.LabelData()

    print('----------------- Spliting and labeling data X & Y------------------------- \n')
    Y_train_pos = np.full(shape=(X_train_pos.shape[0],1), fill_value=1)
    Y_train_neg = np.full(shape=(X_train_neg.shape[0],1), fill_value=0)

    Y_val_pos = np.full(shape=(X_val_pos.shape[0],1), fill_value=1)
    Y_val_neg = np.full(shape=(X_val_neg.shape[0],1), fill_value=0)

    X_train = np.append(X_train_pos,X_train_neg,axis=0)
    y_train = np.append(Y_train_pos,Y_train_neg,axis=0)

    X_val = np.append(X_val_pos, X_val_neg, axis=0)
    y_val = np.append(Y_val_pos, Y_val_neg, axis=0)

    np.random.shuffle(X_train)
    np.random.shuffle(y_train)
    np.random.shuffle(X_val)
    np.random.shuffle(y_val)
    print('----------------- Done ------------------------- \n ')
    print('\n----------------- Linear Model Predictions ------------------------- \n')

    reg = linear_model.Ridge (alpha = .5)
    reg.fit(X=X_train[:-1],y=y_train[:-1])

    reg.predict(X_train[-1:])
    len(reg.predict(X_val))

    np.mean((reg.predict(X_val) - y_val)**2)
    print('Log loss ',log_loss(y_val,reg.predict(X_val)))

    print('\n ----------------- Linear LinearRegression ------------------------- \n')
    regressor = LinearRegression()
    regressor.fit(X_train, y_train)
    print('Slope',regressor.intercept_)
    y_pred = regressor.predict(X_val)
    df = pd.DataFrame({'Actual': y_val.flatten(), 'Predicted': y_pred.flatten()})
    #print(df)
    print('\n ----------------- SVM ------------------------- \n')
    clf_svm = svm.SVC()
    clf_svm.fit(X=X_train[:-1],y=y_train[:-1])
    print(log_loss(y_val,clf_svm.predict(X_val)))

    print('\n ------------------------ Implementing Kernel SVM | Polynomial  ------------------------ \n')
    svclassifier2 = svm.SVC(kernel='poly', degree=8,C=150) # this is the degre of the polynomial.
    svclassifier2.fit(X_train, y_train)

    #making prediction
    y_predp = svclassifier2.predict(X_val)

    #evaluating the poly svm
    print(confusion_matrix(y_val, y_predp))
    print(classification_report(y_val, y_predp))
    print('\n --------------------------- Implementing Kernel SVM | Linear -------------------------- \n')

    svclassifier1 = svm.SVC(kernel='linear')
    svclassifier1.fit(X_train, y_train)
    #Make predict
    y_pred = svclassifier1.predict(X_val)
    #Evaluating the Algorithm
    print(svclassifier1.score(X_val, y_val))
    print(confusion_matrix(y_val,y_pred))
    print(classification_report(y_val,y_pred))

    print('\n ------------------------ Implementing Kernel SVM | Sigmoid ------------------------ \n')
    svclassifier4 = svm.SVC(kernel='sigmoid')
    svclassifier4.fit(X_train, y_train)
    #making predict
    y_preds = svclassifier4.predict(X_val)
    #Evaluating Algorithm
    print(confusion_matrix(y_val, y_preds))
    print(classification_report(y_val, y_preds))

    print('\n------------------------ Implementing Kernel SVM | Gaussian ------------------------\n')
    svclassifier3 = svm.SVC(kernel='rbf')
    svclassifier3.fit(X_train, y_train)
    #making predit
    y_predg = svclassifier3.predict(X_val)
    #Evaluating Algorithm
    print(confusion_matrix(y_val, y_predg))
    print(classification_report(y_val, y_predg))

    print('\n ------------------------ KNN ------------------------ \n')
    sc = StandardScaler()
    sc.fit(X_train)
    X_train = sc.transform(X_train)
    X_val = sc.transform(X_val)
    print('Value for K Math.sqrt(len of X_train) -------> ',math.sqrt(len(X_train)))
    print("Please wait for graph representation ....")

    accuracy = [] #We agregate the Accuracy averages for 18 neighbors.
    f1_scores = [] #Metrics ...
    index = range(3,81)
    for i in index:
        classifier = KNeighborsClassifier(n_neighbors = i, metric= 'euclidean', weights='uniform', leaf_size= 30) #27 classifiers
        classifier.fit(X_train, y_train)
        y_pred = classifier.predict(X_val) # Predict the class labels for the provided data
        conf_matrix = confusion_matrix(y_val, y_pred) # What we predit <VS> what actually is on test data.
        res = (conf_matrix[0, 0] + conf_matrix[1, 1]) / sum(sum(conf_matrix)) # Calculate Accuracy of our predit.
        accuracy.append(res)
        f1_scores.append(list(zip(y_val, y_pred)))

    print('In the range of 3 to 39 we have this values of accuracy')
    print(accuracy)

    # Evaluate the Model.
    print('We evaluate the Matrix of Confusion')
    mc = confusion_matrix(y_val, y_pred)
    print(classification_report(y_val, y_pred))
    print(mc)
    # Graph representation

    plt.figure(figsize=(10, 6), num='Knn Algorithm Facebook Network Prediction')
    plt.plot(index, accuracy, color='green', linestyle='dashed', marker='o',
             markerfacecolor='blue', markersize=10)
    plt.title('Accuracy ratio according to K values')
    plt.xlabel('K Values')
    plt.ylabel('Accuracy average')
    plt.show()

    #LoadLabels.
    nodesLabels = []
    edges = []
    #nodesLabels  = [nodesLabels.append(eachEgo['name']) for eachEgo in centrality.arg.graph.vs]
    for eachEgo in centrality.arg.graph.vs:
        nodesLabels.append(eachEgo['name'])
    #edges = [edges.append(edge.tuple) for edge in centrality.arg.graph.es] #ça marche pas je ne sais pas pourquoi.
    for e in centrality.arg.graph.es:
        edges.append(e.tuple)

    layout = centrality.arg.graph.layout('kk', dim=3)

    #Prepare coordinates for Nodes and Edges.
    Xn=[layout[n][0] for n in range(len(centrality.arg.graph.vs))]# x-coordinates of nodes
    Yn=[layout[n][1] for n in range(len(centrality.arg.graph.vs))]# y-coordinates
    Zn=[layout[n][2] for n in range(len(centrality.arg.graph.vs))]# z-coordinates

    #Lists of edges.
    Xe=[]
    Ye=[]
    Ze=[]
    for e in edges:
        Xe+=[layout[e[0]][0],layout[e[1]][0], None]# x-coordinates of edge ends
        Ye+=[layout[e[0]][1],layout[e[1]][1], None]
        Ze+=[layout[e[0]][2],layout[e[1]][2], None]

    trace1=go.Scatter3d(x=Xe,
                   y=Ye,
                   z=Ze,
                   mode='lines',
                   line=go.Line(color='rgb(125,125,125)', width=1),
                   hoverinfo='none'
                   )

    trace2=go.Scatter3d(x=Xn,
                  y=Yn,
                  z=Zn,
                  mode='markers',
                  name='Alters',
                  marker=go.Marker(symbol='circle',
                                color=eigenVec,
                                size=10,colorbar=go.ColorBar(
                   title='Node Degree'
               ),
                                colorscale='Viridis',
                                line=go.Line(color='rgb(158,18,130)', width=0.5)
                                ),
                  text=nodesLabels,
                  hoverinfo='text'
                  )

    axis=dict(showbackground=True,
             showline=True,
             zeroline=False,
             showgrid=True,
             showticklabels=True,
             title=''
             )
    plan = go.Layout(
             title="Facebook Ego-Network",
             width=1000,
             height=1000,
             showlegend=True,
             scene=go.Scene(
             xaxis=go.XAxis(axis),
             yaxis=go.YAxis(axis),
             zaxis=go.ZAxis(axis),
            ),
         margin=go.Margin(t=100),
        hovermode='closest',
        annotations=go.Annotations([
               go.Annotation(
                showarrow=True,
                xref='paper',
                yref='paper',
                x=0,
                y=0.1,
                xanchor='left',
                yanchor='top',
                font=go.Font(size=14)
                )
            ]),)

    data=go.Data([trace1, trace2])
    fig=go.Figure(data=data, layout=plan)
    fig.show()
Example #20
0
def getAnnotations(df,annotations,kind='lines',theme=None,**kwargs):
	"""
	Generates an annotations object

	Parameters:
	-----------
		df : DataFrame
			Original DataFrame of values
		annotations : dict or list
			Dictionary of annotations
			{x_point : text}
			or
			List of Plotly annotations
	"""
	


	

	for key in list(kwargs.keys()):
		if key not in __ANN_KWARGS:
			raise Exception("Invalid keyword : '{0}'".format(key))

	theme_data = getTheme(theme)

	kwargs['fontcolor']=kwargs.pop('fontcolor',theme_data['annotations']['fontcolor'])
	kwargs['arrowcolor']=kwargs.pop('arrowcolor',theme_data['annotations']['arrowcolor'])
	kwargs['fontsize']=kwargs.pop('fontsize',12)

	def check_ann(annotation):
		local_list=[]
		try:
			_annotation=dict_replace_keyword({},'font',annotation,False)
			_annotation=dict_replace_keyword(_annotation,'font',kwargs,False)
			local_list.append(go.Annotation(_annotation))
			
		except:
			if 'title' in annotation:
				local_list.append(
						go.Annotation(	
								text=annotation['title'],
								showarrow=False,
								x=0,
								y=1,
								xref='paper',
								yref='paper',
								font={'size':24 if not 'fontsize' in kwargs else kwargs['fontsize']}
							)
					)
				del annotation['title']
		
			for k,v in list(annotation.items()):
				if kind in ('candlestick','ohlc','candle'):
					d=ta._ohlc_dict(df)
					maxv=df[d['high']].ix[k]
					yref='y2'
				else:
					maxv=df.ix[k].sum() if k in df.index else 0
					yref='y1'
				ann=go.Annotation(
								x=k,
								y=maxv,
								xref='x',
								yref=yref,
								text=v,
								showarrow=True,
								arrowhead=7,
								ax=0,
								ay=-100,
								textangle=-90
								)
				local_list.append(ann)

			_l=[]
			for i in local_list:
				_l.append(dict_replace_keyword(i,'font',kwargs,True))
			
			local_list=_l

		return local_list

	if not isinstance(annotations,list):
		annotations=[annotations]	
	_list_ann=[]
	for ann in annotations:
		_list_ann.extend(check_ann(ann))
	return go.Annotations(_list_ann)
Example #21
0
def get_player_trend(_player, _nb_games, _season, _season_type):

    pid = _player['PLAYER_ID']
    name = _player['PLAYER_NAME']
    team = _player['TEAM_ABBR']
    last_n_games_fp_avg = _player['LAST_N_GAMES_NBA_FANTASY_PTS']
    last_n_games_ttfl_avg = _player['LAST_N_GAMES_TTFL_SCORE']
    ov_fp_avg = _player['NBA_FANTASY_PTS']
    ov_ttfl_avg = _player['TTFL_SCORE']
    ov_gp = _player['GP']

    PLAYER_HEADSHOT_URL = 'https://ak-static.cms.nba.com/wp-content/uploads/headshots/nba/latest/260x190/'
    PLAYER_HEADSHOT_EXT = '.png'

    TEAM_LOGO_URL = 'https://www.nba.com/.element/img/1.0/teamsites/logos/teamlogos_500x500/'
    TEAM_LOGO_EXT = '.png'

    # ---

    print "Fetching %s log..." % name

    (game_matchup, game_date, game_fp,
     game_ttfl) = get_player_log(pid, _nb_games, _season, _season_type)

    print "%s log fetched..." % name

    # ---

    (ng_date, player_team, vs_team, location) = get_player_next_game(pid)
    if location == 'H':
        ng_matchup = 'vs.'
    else:
        ng_matchup = '@'

    ng_text = '%s <br> %s %s %s' % (ng_date, player_team, ng_matchup, vs_team)

    inj_status = _player['INJ_STATUS']
    if inj_status == 'True':
        injury_text = 'OUT (%s from %s)' % (_player['INJ_TYPE'],
                                            _player['INJ_DATE'])
    else:
        injury_text = ''

    # ---

    print "Tracing %s rating trend..." % name

    # trace plots for nba fp and ttfl score trends
    xaxis = []
    for (matchup, date) in zip(game_matchup, game_date):
        xaxis.append("%s<br>%s" % (date, matchup))

    trace1 = pgo.Scatter(x=xaxis,
                         y=game_fp,
                         mode='lines+markers',
                         name='nba fp trend',
                         line=dict(shape='spline', ),
                         hoverinfo='y')

    trace2 = pgo.Scatter(x=xaxis,
                         y=game_ttfl,
                         mode='lines+markers',
                         name='ttfl score trend',
                         line=dict(shape='spline', ),
                         hoverinfo='y')

    trace3 = pgo.Scatter(x=xaxis,
                         y=[last_n_games_fp_avg] * len(xaxis),
                         mode='lines',
                         name='last %d games nba fp avg' % len(xaxis),
                         line=dict(dash='dash', ),
                         hoverinfo='y')

    trace4 = pgo.Scatter(x=xaxis,
                         y=[last_n_games_ttfl_avg] * len(xaxis),
                         mode='lines',
                         name='last %d games ttfl avg' % len(xaxis),
                         line=dict(dash='dash', ),
                         hoverinfo='y')

    trace5 = pgo.Scatter(x=xaxis,
                         y=[ov_fp_avg] * len(xaxis),
                         mode='lines',
                         name='overall (%d) nba fp avg' % ov_gp,
                         line=dict(dash='dash', ),
                         hoverinfo='y')

    trace6 = pgo.Scatter(x=xaxis,
                         y=[ov_ttfl_avg] * len(xaxis),
                         mode='lines',
                         name='overall (%d) ttfl avg' % ov_gp,
                         line=dict(dash='dash', ),
                         hoverinfo='y')

    data = [trace1, trace2, trace3, trace4, trace5, trace6]

    layout = pgo.Layout(
        title='%s %s %s Ratings Trend' % (name, _season, _season_type),
        titlefont=dict(size=18, ),
        xaxis=dict(tickangle=45, ),
        yaxis=dict(range=[0, 100], ),
        images=[
            dict(
                source=PLAYER_HEADSHOT_URL + str(pid) + PLAYER_HEADSHOT_EXT,
                xref="paper",
                yref="paper",
                x=0.1,
                y=1.1,
                sizex=0.25,
                sizey=0.25,
                xanchor="center",
                yanchor="top",
                layer="below",
            ),
            dict(
                source=TEAM_LOGO_URL + team.lower() + TEAM_LOGO_EXT,
                xref="paper",
                yref="paper",
                x=0.5,
                y=0.5,
                sizex=1,
                sizey=1,
                xanchor="center",
                yanchor="middle",
                opacity=0.2,
                layer="below",
            ),
        ],
        legend=dict(x=0.8,
                    y=1.05,
                    font=dict(size=10, ),
                    bordercolor='#000000',
                    borderwidth=1),
        annotations=pgo.Annotations([
            pgo.Annotation(
                xref='paper',
                yref='paper',
                x=0.2,
                y=1.0,
                showarrow=False,
                text='Next Game: %s' % ng_text,
            ),
            pgo.Annotation(
                xref='paper',
                yref='paper',
                x=0.5,
                y=1.0,
                showarrow=False,
                text=injury_text,
                font=dict(size=12, color='#D62728'),
            ),
        ]),
    )

    fig = pgo.Figure(data=data, layout=layout)

    fname = './tmp/%d_ratings.html' % (pid)
    po.plot(fig, filename=fname)

    print "%s rating trend traced..." % name

    print "----------"
Example #22
0
    def draw_text(self, **props):
        """Create an annotation dict for a text obj.

        Currently, plotly uses either 'page' or 'data' to reference
        annotation locations. These refer to 'display' and 'data',
        respectively for the 'coordinates' key used in the Exporter.
        Appropriate measures are taken to transform text locations to
        reference one of these two options.

        props.keys() -- [
        'text',         (actual content string, not the text obj)
        'position',     (an x, y pair, not an mpl Bbox)
        'coordinates',  ('data', 'axes', 'figure', 'display')
        'text_type',    ('title', 'xlabel', or 'ylabel')
        'style',        (style dict, see below)
        'mplobj'        (actual mpl text object)
        ]

        props['style'].keys() -- [
        'alpha',        (opacity of text)
        'fontsize',     (size in points of text)
        'color',        (hex color)
        'halign',       (horizontal alignment, 'left', 'center', or 'right')
        'valign',       (vertical alignment, 'baseline', 'center', or 'top')
        'rotation',
        'zorder',       (precedence of text when stacked with other objs)
        ]

        """
        self.msg += "    Attempting to draw an mpl text object\n"
        if not mpltools.check_corners(props['mplobj'], self.mpl_fig):
            warnings.warn(
                "Looks like the annotation(s) you are trying \n"
                "to draw lies/lay outside the given figure size.\n\n"
                "Therefore, the resulting Plotly figure may not be \n"
                "large enough to view the full text. To adjust \n"
                "the size of the figure, use the 'width' and \n"
                "'height' keys in the Layout object. Alternatively,\n"
                "use the Margin object to adjust the figure's margins.")
        align = props['mplobj']._multialignment
        if not align:
            align = props['style']['halign']  # mpl default
        if 'annotations' not in self.plotly_fig['layout']:
            self.plotly_fig['layout']['annotations'] = go.Annotations()
        if props['text_type'] == 'xlabel':
            self.msg += "      Text object is an xlabel\n"
            self.draw_xlabel(**props)
        elif props['text_type'] == 'ylabel':
            self.msg += "      Text object is a ylabel\n"
            self.draw_ylabel(**props)
        elif props['text_type'] == 'title':
            self.msg += "      Text object is a title\n"
            self.draw_title(**props)
        else:  # just a regular text annotation...
            self.msg += "      Text object is a normal annotation\n"
            if props['coordinates'] is not 'data':
                self.msg += "        Text object isn't linked to 'data' " \
                            "coordinates\n"
                x_px, y_px = props['mplobj'].get_transform().transform(
                    props['position'])
                x, y = mpltools.display_to_paper(
                    x_px, y_px, self.plotly_fig['layout']
                )
                xref = 'paper'
                yref = 'paper'
                xanchor = props['style']['halign']  # no difference here!
                yanchor = mpltools.convert_va(props['style']['valign'])
            else:
                self.msg += "        Text object is linked to 'data' " \
                            "coordinates\n"
                x, y = props['position']
                axis_ct = self.axis_ct
                xaxis = self.plotly_fig['layout']['xaxis{0}'.format(axis_ct)]
                yaxis = self.plotly_fig['layout']['yaxis{0}'.format(axis_ct)]
                if (xaxis['range'][0] < x < xaxis['range'][1]
                        and yaxis['range'][0] < y < yaxis['range'][1]):
                    xref = 'x{0}'.format(self.axis_ct)
                    yref = 'y{0}'.format(self.axis_ct)
                else:
                    self.msg += "            Text object is outside " \
                                "plotting area, making 'paper' reference.\n"
                    x_px, y_px = props['mplobj'].get_transform().transform(
                        props['position'])
                    x, y = mpltools.display_to_paper(x_px, y_px,
                                                     self.plotly_fig['layout'])
                    xref = 'paper'
                    yref = 'paper'
                xanchor = props['style']['halign']  # no difference here!
                yanchor = mpltools.convert_va(props['style']['valign'])
            annotation = go.Annotation(
                text=(str(props['text']) if
                      isinstance(props['text'], six.string_types) else
                      props['text']),
                opacity=props['style']['alpha'],
                x=x,
                y=y,
                xref=xref,
                yref=yref,
                align=align,
                xanchor=xanchor,
                yanchor=yanchor,
                showarrow=False,  # change this later?
                font=go.layout.annotation.Font(
                    color=props['style']['color'],
                    size=props['style']['fontsize']
                )
            )
            self.plotly_fig['layout']['annotations'] += annotation,
            self.msg += "    Heck, yeah I drew that annotation\n"
Example #23
0
def conditionalHist(x,
                    y,
                    Nbins=50,
                    std=True,
                    plot=False,
                    xlbl='X',
                    ylbl='Y',
                    stats=None):
    """
    gives E[y|x]
    :param x:
    :param y:
    :param Nbins: number of bins in the hist
    :param std: whether to calc/plot standard deviation of the hist
    :param plot: 1/0
    :param xlbl:
    :param ylbl:
    :param stats: whether to add trendline to plot
    :return:
    """
    x = np.array(x)
    y = np.array(y)

    # calc min/max xrange (ie make robust to outliers
    maxstd = 8  # if max above this many stddevs from mean, it is clipped
    percclip = [5, 95]  # percentile above which it is clipped
    meanx, stdx, minx, maxx = np.mean(x), np.std(x), np.min(x), np.max(x)
    xrange = np.percentile(
        x, percclip
    ) if meanx + maxstd * stdx < maxx or meanx - maxstd * stdx > minx else [
        minx, maxx
    ]

    bins = np.linspace(*xrange, Nbins + 2)
    bins = bins[1:-1]  # remove edge effects
    dig = np.digitize(x, bins)
    #remove values outside of range
    Igood = list(dig != 0) and list(dig < Nbins)
    condHist = accum(dig[Igood], y[Igood], func=np.mean, size=Nbins)

    if std:
        condStd = accum(dig[Igood], y[Igood], func=np.std)

    if plot:
        traces = []
        traces += [go.Scatter(x=bins, y=condHist, name='E[Y|X]')]
        if std:
            ploterror_top = go.Scatter(
                x=bins,
                y=condHist + condStd,
                fill='none',
                fillcolor='rgba(200,100,80,0.2)',
                mode='lines',
                marker=dict(color="444"),
                line=dict(width=0),
                showlegend=True,
                legendgroup='bounds',
                name='var[Y|X]',
                opacity=.7,
            )
            ploterror_bottom = go.Scatter(
                x=bins,
                y=condHist - condStd,
                fill='tonexty',
                fillcolor='rgba(200,100,80,0.2)',
                mode='lines',
                marker=dict(color="444"),
                line=dict(width=0),
                showlegend=False,
                legendgroup='bounds',
                name='lower bound',
                opacity=.7,
            )
            traces += [ploterror_top, ploterror_bottom]
        if stats:
            slope, intercept, R2, p_val, std_err = sp.stats.linregress(x, y)
            R2sp, p_val_sp = sp.stats.spearmanr(x, y)
            corrtext = 'Pearson [R2, P]=[%.2f,%.2f] <br> ' \
                       'Spearman [R2, P]=[%.2f,%.2f] <br> ' \
                       'y=%.2fx+%.2f' \
                       % (R2, p_val, R2sp, p_val_sp, slope, intercept)
            print(corrtext)
            annots = go.Annotations([
                go.Annotation(x=0.05,
                              y=0.95,
                              showarrow=False,
                              text=corrtext,
                              xref='paper',
                              yref='paper')
            ])
        else:
            annots = []

        title = 'Conditional Histogram of ' + xlbl + ' | ' + ylbl
        layout = go.Layout(title=title,
                           xaxis={'title': xlbl},
                           yaxis={'title': ylbl},
                           annotations=annots)
        if in_notebook():
            pyo.iplot({'data': traces, 'layout': layout})
        else:
            pyo.plot({
                'data': traces,
                'layout': layout
            },
                     filename='getStimOnOffTimes.html')

    if std:
        return condHist, bins, condStd
    else:
        return condHist, bins