Пример #1
0
def topic_fig(series, n):
    labels = [f'Topic {i}' for i in range(n)]
    fig = go.Figure(data=[go.Pie(labels=labels, values=percentages, hole=0.6)])
    fig.update_traces(textposition='outside', textinfo='percent+label')
    return fig
Пример #2
0
if len(plot_serviceprovider_active) > 0: 
    for i in range(len(plot_serviceprovider_active)):
        fig.add_trace(
            go.Scattergeo(
                locationmode = 'ISO-3',
                lon = [plot_serviceprovider_active['SP_lng'][i], plot_serviceprovider_active['DL_lng'][i]],
                lat = [plot_serviceprovider_active['SP_lat'][i], plot_serviceprovider_active['DL_lat'][i]],
                mode = 'lines',
                line = dict(width = 1,color = 'green'),
                showlegend=False
                #opacity = float(df_flight_paths_2['cnt'][i]) / float(df_flight_paths_2['cnt'].max()),
            )
        )

fig.update_layout(
    title_text = 'Optimierte Bezugsplanung für das Ersatzteil Test-1 <br>(Mouseover für Detailinformationen zum jeweiligen Standort)',
    showlegend = True,
    geo = go.layout.Geo(
        scope = 'world',
        projection_type = 'natural earth',
        showland = True,
        landcolor = 'rgb(243, 243, 243)',
        countrycolor = 'rgb(204, 204, 204)',
    ),
)

fig.add_trace(go.Pie(values=[2, 3, 1]),
              row=2, col=1)

fig.show()
Пример #3
0
def update_graph_live(n):
    devices_on = 0
    for key, value in ecosystem_status.items():
        # print(key, value)
        if value == 1:
            devices_on += 1
    overview_devices_on.append(devices_on)
    overview_smartflow_status.append(int(ecosystem_status['smartflow_status']))

    # Create the graph with subplots
    fig = plotly.subplots.make_subplots(
        rows=3,
        cols=10,
        specs=[[{
            'type': 'domain',
            "colspan": 3,
            "rowspan": 3
        }, None, None, {
            "colspan": 6
        }, None, None, None, None, None, None],
               [
                   None, None, None, {
                       "colspan": 6,
                       "rowspan": 2
                   }, None, None, None, None, None, None
               ], [None, None, None, None, None, None, None, None, None,
                   None]],
        subplot_titles=("", "Status", "Active Devices", "", "", "", "", "", "",
                        "", "", "", "", "", "", "", "", "", "", ""),
        shared_xaxes=True)
    fig['layout']['margin'] = {'l': 100, 'r': 10, 'b': 0, 't': 50}
    fig['layout']['legend'] = {
        'orientation': 'v',
        'borderwidth': 2,
        'x': -0.01,
        'y': 1
    }
    device_bar = go.Bar(x=list(overview_times),
                        y=list(overview_devices_on),
                        name='Devices Active',
                        text=list(overview_devices_on),
                        textposition='outside',
                        marker=dict(color='#0000FF'))
    labels = ['Anomalous', 'Normal']
    normal = 0
    anomalous = 0
    for entry in list(overview_smartflow_status):
        if entry is 0:
            normal += 1
        elif entry is 1:
            anomalous += 1
    print(list(overview_smartflow_status))
    values = [anomalous, normal]

    colors = ['#FF0000', '#32CD32']
    system_pie = go.Pie(labels=labels,
                        hole=.3,
                        values=values,
                        hoverinfo='label+percent',
                        marker=dict(colors=colors))

    fig.append_trace(
        {
            'x':
            list(overview_times),
            'y':
            list(overview_smartflow_status),
            'yaxis':
            'y2',
            'name':
            'Smartflow Status',
            'mode':
            'markers+lines',
            'fill':
            'tozeroy',
            'marker':
            dict(size=15,
                 color=(set_color(
                     list(overview_smartflow_status)[
                         len(list(overview_smartflow_status)) - 1]))),
            'type':
            'scatter',
        }, 1, 4)
    fig.append_trace(system_pie, 1, 1)
    fig.append_trace(device_bar, 2, 4)

    fig.update_layout(title_text='[---OVERVIEW---]',
                      font=dict(family='Courier New, monospace',
                                color='#1A1A1A'),
                      paper_bgcolor='#C2C5CC',
                      plot_bgcolor='#C2C5CC',
                      height=300,
                      yaxis=dict(range=(0, 1)),
                      yaxis2=dict(range=(0, 10)))
    return fig
Пример #4
0
def get_pie_chart(labels, sizes):

    fig = go.Figure(data=[go.Pie(labels=labels, values=sizes)])

    return fig.to_html(full_html=False, default_height=800, default_width=1000)
Пример #5
0
def twitter_page(n_clicks, twitterhandle):
    if n_clicks > 0:
        # Extract 1000 tweets from twitter user
        posts = api.user_timeline(screen_name=twitterhandle,
                                  count=10000,
                                  lang="en",
                                  tweet_mode="extended")

        # Create dataframe
        df = pd.DataFrame([tweet.full_text for tweet in posts],
                          columns=['Tweets'])

        # Clean text
        # Create function to clean tweets
        def cleanTxt(text):
            text = re.sub(r'@[A-Za-z0-9]+', '', text)  #removes @mentions
            text = re.sub(r'#', '', text)  #removes '#'
            text = re.sub(r'RT[\s]+', '', text)  # removes RT
            text = re.sub(r'https?:\/\/\S+', '', text)  #removes links
            return text

        df['Text'] = df['Tweets'].apply(cleanTxt)

        # Create function to get the subjectivity
        def getSubjectivity(text):
            return TextBlob(text).sentiment.subjectivity

        # Create function to get the polarity
        def getPolarity(text):
            return TextBlob(text).sentiment.polarity

        # Create two new columns
        df['Subjectivity'] = df['Text'].apply(getSubjectivity)
        df['Polarity'] = df['Text'].apply(getPolarity)

        # Calculate the negative, neutral and positive analysis
        def getAnalysis(score):
            if score < 0:
                return 'Negative'
            elif score == 0:
                return 'Neutral'
            else:
                return 'Positive'

        df['Sentiment'] = df['Polarity'].apply(getAnalysis)

        #Create Pie Chart
        labels = df['Sentiment'].value_counts().index
        values = df['Sentiment'].value_counts()
        fig = go.Figure(data=[go.Pie(labels=labels, values=values)])

        # Clean the text
        allWords = ' '.join([twts for twts in df['Text']])
        allWords_lower = allWords.lower()
        stop = set(stopwords.words('english') + list(string.punctuation))
        new_stopwords = ['...', '``', "''", '’', "'s", "n't"]
        new_stopwords_list = stop.union(new_stopwords)

        text_tokens = nltk.word_tokenize(allWords_lower)
        text_no_stop_words_punct = [
            t for t in text_tokens
            if t not in new_stopwords_list and t not in string.punctuation
        ]

        filtered_string = (" ").join(text_no_stop_words_punct)

        # Convert the long string to a dictionary with frequency counts.
        def word_count(str):
            counts = dict()
            words = str.split()

            for word in words:
                if word in counts:
                    counts[word] += 1
                else:
                    counts[word] = 1

            return counts

        twitter_wordcloud = (word_count(filtered_string))

        # Create Word Cloud
        wc = WordCloud().generate_from_frequencies(
            frequencies=twitter_wordcloud)
        wc_img = wc.to_image()
        with BytesIO() as buffer:
            wc_img.save(buffer, 'png')
            img2 = base64.b64encode(buffer.getvalue()).decode()

        #Display Pie Chart and Word Cloud
        twitter_results = html.Div([
            html.Div(dcc.Graph(id='graph1', figure=fig),
                     style={
                         'width': '49%',
                         'display': 'inline-block'
                     }),
            html.Div(children=[
                html.Img(src="data:image/png;base64," + img2,
                         style={
                             'height': '50%',
                             'width': '50%'
                         })
            ],
                     style={
                         'width': '49%',
                         'display': 'block',
                         'textAlign': 'center'
                     }),
        ])

        return twitter_results
Пример #6
0
                          header=dict(values=['Country', 'Year', 'Sex', 'Age', 'Population', 'Suicides/100k',
                                              'Generation', 'GDP per capita', 'Continent'],
                                      align='left'),
                          cells=dict(values=[suicides.country, suicides.year, suicides.sex, suicides.age,
                                             suicides.population, suicides['suicides/100k'],
                                             suicides.generation, suicides.gdp_per_capita, suicides.continent],
                                     align='left'))
                      ])),
                  className='ten columns')
          ]),
 html.Div(className='row',
          children=[
              html.Div(
                  dcc.Graph(
                      id='pie-chart-continent',
                      figure = go.Figure(data=[go.Pie(labels=grouped_by_continent['continent'], values=grouped_by_continent['suicides/100k'],
                                                      textposition='inside')])
                  ), className='six columns'
              ),
              html.Div(
                  dcc.Graph(
                      id='gender-graph',
                      figure=px.bar(grouped_by_gender, x='year', y='suicides/100k', color='sex')
                  ), className='six columns'
              )
          ]),
 html.Div(
     className='row',
     children=[
         html.Div(
             dcc.Graph(
                 id='age-graph',
Пример #7
0
           name='Low Satisfaction'))
fig_job_satisfaction.update_layout(title="Job Satisfaction (%)")

# Unemployment
fig_unemployment = px.line(unemployment_data,
                           x="country",
                           y="unemp_rate",
                           title='Rate of Unemployment',
                           labels={"unemp_rate": "Unemployment rate"})

############################################ Legal ############################################

# Crime rate
fig_crime_rate = go.Figure(data=[
    go.Pie(labels=crime_data['country'],
           values=crime_data['prct_rpt_crime'],
           hole=.3,
           title="Crime Report Rate")
])

# police_trust_rating,legal_trust_rating

fig_legal_trust_subplot = go.Figure()
fig_legal_trust_subplot.add_trace(
    go.Scatter(x=euro_stats_data['country'],
               y=euro_stats_data['police_trust_rating'],
               mode='lines',
               line=dict(color='darkslateblue'),
               name='Police Trust Rating'))
fig_legal_trust_subplot.add_trace(
    go.Scatter(x=euro_stats_data['country'],
               y=euro_stats_data['legal_trust_rating'],
 def country_gender(self,df, country):
     x = ["male", "female"]
     onlycountry = df.loc[df["country"] == country]
     year_group = onlycountry.groupby("year")["suicides_no"].sum()
     plot_x = year_group.index
     plot_y = year_group
     age_group= onlycountry.groupby("age")["suicides_no"].sum()
     pie_lables = age_group.index
     pie_values = age_group
     y = []
     for i in x:
         gender = onlycountry.loc[onlycountry["sex"] == i]
         y.append(gender["suicides_no"].sum())
     trace = [go.Scatter(x=plot_x, y=plot_y, mode='lines', visible=True), go.Bar(x=x, y=y, visible=False),go.Pie(labels=pie_lables,values=pie_values,visible=False)]
     layout = go.Layout(title="Detail sucide count of " + country)
     fig = go.Figure(data=trace, layout=layout)
     fig.update_layout(
         updatemenus=[
             dict(type="buttons",
                  direction="left",
                  buttons=[
                      dict(
                          label="All Years",
                          method="update",
                          args=[{"type": ["scatter", "bar","pie"], "mode": ["lines", "", ""],
                                 "visible": [True, False,False]}],
                      ), dict(
                          label="Gender",
                          method="update",
                          args=[{"type": ["scatter", "bar","pie"], "mode": ["lines", "", ""],
                                 "visible": [False, True,False]}],
                      ),
                      dict(
                          label="Age",
                          method="update",
                          args=[{"type": ["scatter", "bar","pie"], "mode": ["lines", "", ""],
                                 "visible": [False, False, True]}],
                      )
                  ],
                  pad={"r": 10, "t": 0}, showactive=True, x=0.11, xanchor="left", y=1.1, yanchor="top"
                  ),
         ]
     )
     return fig
Пример #9
0
def qsphere(state,
            state_labels=True,
            state_labels_kind='bits',
            as_widget=False):
    """Plots a statevector of qubits using the qsphere
    representation.

    Parameters:
        state (ndarray): Statevector as 1D NumPy array.
        state_labels (bool): Show state labels.
        state_labels_kind (str): 'bits' (default) or 'ints'.
        as_widget (bool): Return a widget instance.

    Returns:
        PlotlyFigure or PlotlyWidget: Figure instance.

    Raises:
        KaleidoscopeError: Invalid statevector input.

    Example:

        .. jupyter-execute::

            from qiskit import QuantumCircuit
            from qiskit.quantum_info import Statevector
            import kaleidoscope.qiskit
            from kaleidoscope.interactive import qsphere

            qc = QuantumCircuit(3)
            qc.h(range(3))
            qc.ch(0,1)
            qc.s(2)
            qc.cz(2,1)
            state = qc.statevector()

            qsphere(state)
    """
    if state.__class__.__name__ in ['Statevector'] \
            and 'qiskit' in state.__class__.__module__:
        state = state.data

    if state.__class__.__name__ in ['DensityMatrix'] \
            and 'qiskit' in state.__class__.__module__:

        if not abs(1 - state.data.dot(state.data).trace().real) < 1e-6:
            raise KaleidoscopeError(
                'Input density matrix is not a pure state.')
        # pylint: disable=unexpected-keyword-arg
        _, evecs = la.eig(state.data)
        state = evecs[0].ravel()

    if len(state.shape) == 2:
        if not abs(1 - state.dot(state).trace().real) < 1e-6:
            raise KaleidoscopeError(
                'Input density matrix is not a pure state.')
        # pylint: disable=unexpected-keyword-arg
        _, evecs = la.eig(state.data)
        state = evecs[0].ravel()

    if len(state.shape) != 1:
        raise KaleidoscopeError('Input state is not 1D array.')

    if np.log2(state.shape[0]) % 1:
        raise KaleidoscopeError('Input is not a valid statevector of qubits.')

    eps = 1e-8
    norm = mpl.colors.Normalize(vmin=0, vmax=2 * np.pi)
    cmap = cc.cm.CET_C1s
    num_qubits = int(np.log2(state.shape[0]))

    xvals = []
    yvals = []
    zvals = []
    colors = []
    bases = []
    probs = []
    marker_sizes = []

    for idx in range(2**num_qubits):
        prob = (state[idx] * state[idx].conj()).real
        if prob > eps:
            elem = bin(idx)[2:].zfill(num_qubits)
            weight = elem.count("1")
            zvalue = -2 * weight / num_qubits + 1
            number_of_divisions = spsp.comb(num_qubits, weight)
            weight_order = _bit_string_index(elem)
            angle = (float(weight) / num_qubits) * (np.pi * 2) + \
                    (weight_order * 2 * (np.pi / number_of_divisions))

            if (weight > num_qubits / 2) or (
                ((weight == num_qubits / 2) and
                 (weight_order >= number_of_divisions / 2))):
                angle = np.pi - angle - (2 * np.pi / number_of_divisions)

            xvalue = np.sqrt(1 - zvalue**2) * np.cos(angle)
            yvalue = np.sqrt(1 - zvalue**2) * np.sin(angle)

            bases.append(elem)
            probs.append(prob)
            xvals.append(xvalue)
            yvals.append(yvalue)
            zvals.append(zvalue)

            phase = np.arctan2(state[idx].imag, state[idx].real)
            phase = phase if phase >= 0 else phase + 2 * np.pi
            colors.append(mpl.colors.rgb2hex(cmap(norm(phase))))
            marker_sizes.append(np.sqrt(prob) * 40)

    if state_labels_kind == 'ints':
        bases = [int(kk, 2) for kk in bases]

    # Output figure instance
    fig = make_subplots(rows=5,
                        cols=5,
                        specs=[[{
                            "type": "scene",
                            "rowspan": 5,
                            "colspan": 5
                        }, None, None, None, None],
                               [None, None, None, None, None],
                               [None, None, None, None, None],
                               [None, None, None, None, None],
                               [
                                   None, None, None, None, {
                                       "rowspan": 1,
                                       "colspan": 1,
                                       "type": "domain"
                                   }
                               ]])

    figsize = (350, 350)

    # List for vector annotations, if any
    fig_annotations = []

    fig.add_trace(BSPHERE(), row=1, col=1)

    # latitudes
    for kk in _qsphere_latitudes(zvals):
        fig.add_trace(kk, row=1, col=1)

    fig.add_trace(go.Scatter3d(
        x=[0],
        y=[0],
        z=[0],
        mode='markers',
        opacity=0.6,
        marker=dict(size=4, color='#555555'),
    ),
                  row=1,
                  col=1)

    for kk, _ in enumerate(xvals):
        fig.add_trace(go.Scatter3d(x=[0, xvals[kk]],
                                   y=[0, yvals[kk]],
                                   z=[0, zvals[kk]],
                                   mode="lines",
                                   hoverinfo=None,
                                   opacity=0.5,
                                   line=dict(color=colors[kk], width=3)),
                      row=1,
                      col=1)

        if state_labels:
            xanc = 'center'
            if xvals[kk] != 0:
                if xvals[kk] < 0:
                    xanc = 'right'
                else:
                    pass

            yanc = 'middle'
            if zvals[kk] != 0:
                if zvals[kk] < 0:
                    yanc = 'top'
                else:
                    yanc = 'bottom'

            fig_annotations.append(
                dict(
                    showarrow=False,
                    x=xvals[kk] * 1.1,
                    y=yvals[kk] * 1.1,
                    z=zvals[kk] * 1.1,
                    text="<b>|{}\u3009</b>".format(bases[kk]),
                    align='left',
                    opacity=0.7,
                    xanchor=xanc,
                    yanchor=yanc,
                    xshift=10,
                    bgcolor="#ffffff",
                    font=dict(
                        size=10,
                        color="#000000",
                    ),
                ))

    fig.add_trace(go.Scatter3d(
        x=xvals,
        y=yvals,
        z=zvals,
        mode='markers',
        opacity=1,
        marker=dict(size=marker_sizes, color=colors),
    ),
                  row=1,
                  col=1)

    slices = 128
    labels = [''] * slices
    values = [1] * slices

    phase_colors = [
        mpl.colors.rgb2hex(cmap(norm(2 * np.pi * kk / slices)))
        for kk in range(slices)
    ]

    fig.add_trace(go.Pie(labels=labels,
                         values=values,
                         hole=.6,
                         showlegend=False,
                         textinfo='none',
                         hoverinfo='none',
                         textposition="outside",
                         rotation=90,
                         textfont_size=12,
                         marker=dict(colors=phase_colors)),
                  row=5,
                  col=5)

    pie_x = fig.data[-1]['domain']['x']
    pie_y = fig.data[-1]['domain']['y']

    fig['layout'].update(annotations=[
        dict(
            xref='paper',
            yref='paper',
            x=(pie_x[1] - pie_x[0]) / 2 + pie_x[0],
            y=(pie_y[1] - pie_y[0]) / 2 + pie_y[0],
            text='Phase',
            xanchor="center",
            yanchor="middle",
            showarrow=False,
            font=dict(size=9),
        ),
        dict(
            xref='paper',
            yref='paper',
            x=pie_x[0] - 0.03,
            y=(pie_y[1] - pie_y[0]) / 2 + pie_y[0],
            text='\U0001D70B',
            xanchor="left",
            yanchor="middle",
            showarrow=False,
            font=dict(size=14),
        ),
        dict(
            xref='paper',
            yref='paper',
            x=pie_x[1] + 0.03,
            y=(pie_y[1] - pie_y[0]) / 2 + pie_y[0],
            text='0',
            xanchor="right",
            yanchor="middle",
            showarrow=False,
            font=dict(size=12),
        ),
        dict(
            xref='paper',
            yref='paper',
            x=(pie_x[1] - pie_x[0]) / 2 + pie_x[0],
            y=pie_y[1] + 0.05,
            text='\U0001D70B/2',
            xanchor="center",
            yanchor="top",
            showarrow=False,
            font=dict(size=12),
        ),
        dict(
            xref='paper',
            yref='paper',
            x=(pie_x[1] - pie_x[0]) / 2 + pie_x[0],
            y=pie_y[0] - 0.05,
            text='3\U0001D70B/2',
            xanchor="center",
            yanchor="bottom",
            showarrow=False,
            font=dict(size=12),
        )
    ])

    fig.update_layout(width=figsize[0],
                      height=figsize[1],
                      autosize=False,
                      hoverdistance=50,
                      showlegend=False,
                      scene_aspectmode='cube',
                      margin=dict(r=15, b=15, l=15, t=15),
                      scene=dict(annotations=fig_annotations,
                                 xaxis=dict(showbackground=False,
                                            range=[-1.2, 1.2],
                                            showspikes=False,
                                            visible=False),
                                 yaxis=dict(showbackground=False,
                                            range=[-1.2, 1.2],
                                            showspikes=False,
                                            visible=False),
                                 zaxis=dict(showbackground=False,
                                            range=[-1.2, 1.2],
                                            showspikes=False,
                                            visible=False)),
                      scene_camera=dict(eye=dict(x=0, y=-1.4, z=0.3)))

    if as_widget:
        return PlotlyWidget(fig)

    return PlotlyFigure(fig, modebar=True)
Пример #10
0
def get_donut_graphs(df, label):
    non_main = 1 - df.values[0]
    labels =["main", label]
    values =[non_main, df]
    fig = go.Figure(data=[go.Pie(labels=labels, values=values, hole= 0.499)])
    return fig
Пример #11
0
import plotly.graph_objects as go
from plotly.offline import plot

fig = go.Figure(
    data=[go.Pie(labels=['bob', 'boba', 'boban'], values=[1000, 1000, 1000])])
plot(fig)
Пример #12
0
    def generateReport(self):
        self.determineSelection()
        self.determineDateRange()
        self.determineStyleSettings()
        self.determineLocationsToTrack()

        conn = sqlite3.connect('vmc_tap.db')
        conn_results = []

        title = self.title

        substr = ''

        for i, location in enumerate(self.location_list):
            if i != (len(self.location_list) - 1):
                substr += location + '\' or location = \''
            else:
                substr += location

        conn_string_sql = "select " + self.group_by + ", count(" + self.selection + ") from visits where (location = \'" + substr + "\') and check_in_date >= \'" + self.from_time.strftime(
            '%Y-%m-%d') + "\' and check_in_date <= \'" + self.to_time.strftime(
                '%Y-%m-%d') + "\' group by " + self.group_by + ";"
        print('location_list: ', self.location_list)

        # conn_string_sql = "select location, count(" + self.selection + ") from visits group by location;"

        print('conn_string_sql', conn_string_sql)
        #       print('conn.execute: ', conn.execute(conn_string_sql))

        for d in conn.execute(conn_string_sql):
            conn_results.append(d)

        conn.close()

        # Rotates 2D array to work w/ plotly
        conn_results_rotated = list(zip(*conn_results[::-1]))

        print('Conn results_rotated:', conn_results_rotated)

        app = DjangoDash('Graph')  # replaces dash.Dash

        # print('conn_results_rotated: ', conn_results_rotated)

        if conn_results_rotated == []:
            x_axis = [1, 2, 3, 4, 5, 6, 7, 8]
            y_axis = [1, 2, 3, 4, 5, 6, 7, 8]
        # raise emptyList("List is empty")
        else:
            x_axis = conn_results_rotated[0]
            y_axis = conn_results_rotated[1]
            # for tuple in conn_results:
            #    x_axis.append(tuple[0])
            #   y_axis.append(tuple[1])

            print('x_axis: ', x_axis)
            print(y_axis)

        layout = Layout(title=title)
        fig = go.Figure(data=[
            go.Pie(labels=x_axis, values=y_axis, textinfo=self.textinfo)
        ],
                        layout=layout)

        # graph = [Bar(x=x_axis,y=y_axis)]
        # layout = Layout(title='Length of Visits',xaxis=dict(title='Length (min)'),yaxis=dict(title='# of Visits'))
        # fig = Figure(data=graph,layout=layout)
        # plot_div = plot(fig,output_type='div',show_link=False,link_text="")

        # Dash instance for includng a table
        if self.include_table == 'Yes':
            header = ['Row Labels', 'Count of Location']
            x_list = list(x_axis)
            y_list = list(y_axis)
            values = [x_list, y_list]
            print('values: ', values)

            total = 0
            for count in y_axis:
                total += count

            x_list.append('<b>Grand Total</b>')
            y_list.append(total)
            table = go.Figure(data=[
                go.Table(header=dict(values=header), cells=dict(values=values))
            ])

            app.layout = html.Div(children=[
                dcc.Graph(id='table', figure=table),
                dcc.Graph(id='figure',
                          figure=fig,
                          style={
                              'height': '80vh',
                              'width': '80vw'
                          }),
            ],
                                  style={
                                      'height': '40vh',
                                      'width': '70vw'
                                  })
        else:
            app.layout = html.Div(children=[
                dcc.Graph(id='figure', figure=fig, style={'height': '90vh'}),
            ],
                                  style={
                                      'height': '70vh',
                                      'width': '70vw'
                                  })

        return app, title
Пример #13
0
def output_html(
    all_cve_data: Dict[ProductInfo, CVEData],
    scanned_dir: str,
    filename: str,
    theme_dir: str,
    total_files: int,
    products_with_cve: int,
    products_without_cve: int,
    merge_report: Union[None, MergeReports],
    logger: LOGGER,
    outfile,
):
    """Returns a HTML report for CVE's"""

    # Step 1: Load all the templates

    # Root folder where html_reports is present
    root = os.path.dirname(os.path.abspath(__file__))

    # Template Directory contains all the html files
    templates_dir = os.path.join(root, "html_reports")
    templates_env = Environment(
        loader=FileSystemLoader(templates_dir),
        autoescape=select_autoescape(enabled_extensions=("html"),
                                     disabled_extensions=("css,js")),
    )

    temp_base = "templates/base.html"
    temp_dash = "templates/dashboard.html"
    temp_product = "templates/row_product.html"
    temp_cve = "templates/row_cve.html"
    temp_intermediate = "templates/intermediate.html"

    base = templates_env.get_template(temp_base)
    dashboard = templates_env.get_template(temp_dash)
    cve_row = templates_env.get_template(temp_cve)
    product_row = templates_env.get_template(temp_product)
    # Load merge template if the report is generated from intermediate reports
    if merge_report:
        merged = templates_env.get_template(temp_intermediate)
        (
            products_trace,
            total_files_trace,
            intermediate_timeline,
            severity_trace,
        ) = load_timeline_from_merged(merge_report)

        # Intermediate Graphs Rendering
        intermediate = merged.render(
            products_trace=products_trace.to_html(full_html=False,
                                                  include_plotlyjs=False),
            total_files_trace=total_files_trace.to_html(
                full_html=False, include_plotlyjs=False),
            intermediate_timeline=intermediate_timeline.to_html(
                full_html=False, include_plotlyjs=False),
            severity_trace=severity_trace.to_html(full_html=False,
                                                  include_plotlyjs=False),
        )
    else:
        intermediate = None

    # Step 2: Prepare Charts
    # Start generating graph with the data

    # dash graph1: Products Vulnerability Graph
    product_pie = go.Figure(data=[
        go.Pie(
            labels=["Vulnerable", "No Known Vulnerability"],
            values=[products_with_cve, products_without_cve],
            hole=0.4,
        )
    ])

    # Chart configuration for product_pie
    product_pie.update_layout(
        autosize=True,
        legend_orientation="h",
    )
    product_pie.update_traces(
        hoverinfo="label+percent",
        textinfo="value",
        textfont_size=14,
        marker=dict(
            colors=["#d80032", "#1a936f"],
            line=dict(color="white", width=2),
        ),
    )

    # dash graph2: Product CVE's Graph
    cve_bar = go.Figure()
    for product_info, cve_data in all_cve_data.items():
        # Check if product contains CVEs
        if cve_data["cves"]:
            if product_info.vendor != "UNKNOWN":
                cve_bar.add_trace(
                    go.Bar(
                        x=[
                            f"{product_info.vendor}-{product_info.product}({product_info.version})"
                        ],
                        y=[
                            0 if cve_data["cves"][0][1] == "UNKNOWN" else len(
                                cve_data["cves"])
                        ],
                        name=f"{product_info.product}-{product_info.version}",
                    ))
            else:
                cve_bar.add_trace(
                    go.Bar(
                        x=[f"{product_info.product}({product_info.version})"],
                        y=[
                            0 if cve_data["cves"][0][1] == "UNKNOWN" else len(
                                cve_data["cves"])
                        ],
                        name=f"{product_info.product}-{product_info.version}",
                    ))

    # Chart configuration for cve_bar
    cve_bar.update_layout(yaxis_title="Number of CVE's", )

    all_paths = defaultdict(list)

    star_warn = ""
    products_found = []
    # List of Products
    for product_info, cve_data in all_cve_data.items():
        # Check if product contains CVEs
        if cve_data["cves"]:

            # group product wise cves on the basis of remarks
            cve_by_remark = group_cve_by_remark(cve_data["cves"])

            # hid is unique for each product
            if product_info.vendor != "UNKNOWN":
                hid = f"{product_info.vendor}{product_info.product}{''.join(product_info.version.split('.'))}"
            else:
                hid = (
                    f"{product_info.product}{''.join(product_info.version.split('.'))}"
                )
            new_cves = render_cves(
                hid,
                cve_row,
                "NEW",
                cve_by_remark[Remarks.NewFound],
            )
            mitigated_cves = render_cves(
                hid,
                cve_row,
                "MITIGATED",
                cve_by_remark[Remarks.Mitigated],
            )
            confirmed_cves = render_cves(
                hid,
                cve_row,
                "CONFIRMED",
                cve_by_remark[Remarks.Confirmed],
            )
            unexplored_cves = render_cves(
                hid,
                cve_row,
                "UNEXPLORED",
                cve_by_remark[Remarks.Unexplored],
            )
            ignored_cves = render_cves(
                hid,
                cve_row,
                "IGNORED",
                cve_by_remark[Remarks.Ignored],
            )

            analysis_data = Counter(cve.severity for cve in cve_data["cves"])

            # initialize a figure object for Analysis Chart
            analysis_pie = go.Figure(data=[
                go.Pie(
                    labels=list(analysis_data.keys()),
                    values=list(analysis_data.values()),
                    hole=0.4,
                )
            ])
            colors_avail = {
                "CRITICAL": "#f25f5c",
                "HIGH": "#ee6c4d",
                "MEDIUM": "#f4d35e",
                "LOW": "#90a955",
                "UNKNOWN": "#808080",
            }
            colors = [colors_avail[label] for label in analysis_data.keys()]
            analysis_pie.update_traces(
                hoverinfo="label+percent",
                textinfo="value",
                textfont_size=14,
                marker=dict(
                    colors=colors,
                    line=dict(color="white", width=2),
                ),
            )
            analysis_pie.update_layout(
                autosize=True,
                height=300,
                legend_orientation="h",
                margin=dict(l=0, r=20, t=0, b=0),
                # paper_bgcolor="LightSteelBlue",
            )

            products_found.append(
                product_row.render(
                    vendor=product_info.vendor,
                    name=product_info.product,
                    version=product_info.version,
                    cve_count=0 if cve_data["cves"][0][1] == "UNKNOWN" else
                    len(cve_data["cves"]),
                    severity_analysis=analysis_pie.to_html(
                        full_html=False, include_plotlyjs=False),
                    fix_id=hid,
                    paths=cve_data["paths"],
                    len_paths=len(cve_data["paths"]),
                    new_cves=new_cves,
                    mitigated_cves=mitigated_cves,
                    confirmed_cves=confirmed_cves,
                    unexplored_cves=unexplored_cves,
                    ignored_cves=ignored_cves,
                ))

            if "*" in product_info.vendor:
                star_warn = "* vendors guessed by the tool"

            # update all_paths
            for path in cve_data["paths"]:
                all_paths[path].append(hid)

    # Dashboard Rendering
    dashboard = dashboard.render(
        graph_cves=cve_bar.to_html(full_html=False, include_plotlyjs=False),
        graph_products=product_pie.to_html(full_html=False,
                                           include_plotlyjs=False),
        directory=scanned_dir,
        total_files=total_files,
        products_with_cve=products_with_cve,
        products_without_cve=products_without_cve,
    )

    # try to load the bigger files just before the generation of report

    # css template names
    css_main = "css/main.css"
    css_bootstrap = "css/bootstrap.css"

    style_main = templates_env.get_template(css_main)
    style_bootstrap = templates_env.get_template(css_bootstrap)

    # js template names
    js_main = "js/main.js"
    js_bootstrap = "js/bootstrap.js"
    js_plotly = "js/plotly.js"
    js_jquery = "js/jquery.js"

    script_main = templates_env.get_template(js_main)
    script_bootstrap = templates_env.get_template(js_bootstrap)
    script_plotly = templates_env.get_template(js_plotly)
    script_jquery = templates_env.get_template(js_jquery)

    # Render the base html to generate report
    outfile.write(
        base.render(
            date=datetime.now().strftime("%d %b %Y"),
            dashboard=dashboard,
            intermediate=intermediate,
            scanned_dir=scanned_dir,
            all_paths=all_paths,
            print_mode=html_print_mode(
                all_cve_data,
                scanned_dir,
                products_with_cve,
                products_without_cve,
                total_files,
                star_warn,
                merge_report,
                version=VERSION,
                full_html=False,
            ),
            products_found="".join(products_found),
            version=VERSION,
            star_warn=star_warn,
            style_main=style_main.render(),
            style_bootstrap=style_bootstrap.render(),
            script_main=script_main.render(),
            script_jquery=script_jquery.render(),
            script_bootstrap=script_bootstrap.render(),
            script_plotly=script_plotly.render(),
        ))
Пример #14
0
def createFigures():
    ## make layout of plotly
    fig = make_subplots(
        rows=3, cols=3,
        specs=[[{"type": "domain"},{"type": "xy", "colspan": 2}, None],
                [{"type": "xy"}, {"type": "domain"}, {"type": "xy"}],
            [{"type": "table", "rowspan": 1, "colspan" : 3}, None, None]], print_grid=True,
        subplot_titles=("2019 Homeruns by Team", "Winker vs Suarez", "Reds Hitters By wRC+", "2019 Stolen Bases by Team", 
                        "HR vs RBI Correlation", "Reds Hitters Roster Info")
    )

    ## league homer query and visualization 
    database.execute("SELECT Team, HR FROM League")
    result = database.fetchall()

    labels = []
    values = []

    for line in result:
        labels.append(line[0])
        values.append(line[1])

    fig.add_trace(
        go.Pie(labels=labels, values=values, showlegend=False, title_font_size=25, hoverinfo='label+percent+value', textinfo='label', textfont_size=10,
                    marker=dict(line=dict(color='#000000', width=2))),
        row=1, col=1
    )
    ## end league homer query and visualization 

    ## Winker vs Suarez query + visualization
    database.execute("""select o.LastName, FLOOR(datediff(curdate(), r.DOB)/365) as 'Age', o.R, o.HR, o.RBI
                        from Outfielders o, Reds r
                        where o.LastName = 'Winker' and r.LastName = 'Winker'
                        UNION
                        Select t.LastName, FLOOR(datediff(curdate(), r.DOB)/365) as 'Age', t.R, t.HR, t.RBI
                        from ThirdBasemen t, Reds r
                        where t.LastName = 'Suarez' and r.LastName = 'Suarez'""")

    result = database.fetchall()
    names = []
    age = []
    runs = []
    hr = []
    rbi = []
    count = 0

    for line in result:
        names.append(line[0])
        age.append(int(line[1]))
        runs.append(int(line[2]))
        hr.append(int(line[3]))
        rbi.append(int(line[4]))

    while (count < 2):  
        fig.add_trace(
            go.Bar(name='', xaxis='x1', hoverinfo="text+y", x=["Age", "Runs", "HR", "RBI"], y=[age[count], runs[count], hr[count], rbi[count]], text=names[count]),
            row=1, col=2
        )
        count +=1


    ## end Winker vs Suarez query + visualization

    ## hitters v wRC query + visualization
    database.execute("""select o.LastName, o.wRC
                    from Outfielders o
                    UNION
                    Select t.LastName, t.wRC
                    from ThirdBasemen t
                    UNION
                    select f.LastName, f.wRC
                    from FirstBasemen f
                    UNION
                    select c.LastName, c.wRC
                    from Catchers c
                    UNION
                    select s.LastName, s.wRC
                    from SecondBasemen s
                    UNION 
                    select ss.LastName, ss.wRC
                    from Shortstops ss;""")

    result = database.fetchall()
    names=[]
    wrc = []
    count = 0

    for line in result:
        names.append(line[0])
        wrc.append(line[1])
        
    while (count < 16):
        fig.add_trace(
        go.Bar(name='', x=[wrc[count]], xaxis='x2',
            y=[names[count]],
            orientation='h'),
            row=2, col=1
        )
        count += 1

    ## end hitters v wRC query + visualization

    ## SB by team query + visualization
    database.execute("Select Team, SB FROM League;")
    result= database.fetchall()

    labels = []
    values = []

    for line in result:
        labels.append(line[0])
        values.append(line[1])

    fig.add_trace(
        go.Pie(name='',labels=labels, values=values, showlegend=False, title_font_size=25, hole=.3, textinfo='label', textfont_size=8),
        row=2, col=2
    )

    ## end SB by team query + visualization

    ## HR v RBI Correlation query + visualization

    database.execute("""select o.LastName, o.HR, o.RBI
                    from Outfielders o
                    UNION
                    Select t.LastName, t.HR, t.RBI
                    from ThirdBasemen t
                    UNION
                    select f.LastName, f.HR, f.RBI
                    from FirstBasemen f
                    UNION
                    select c.LastName, c.HR, c.RBI
                    from Catchers c
                    UNION
                    select s.LastName, s.HR, s.RBI
                    from SecondBasemen s
                    UNION 
                    select ss.LastName, ss.HR, ss.RBI
                    from Shortstops ss;""")
    result= database.fetchall()
    names=[]
    hr = []
    rbi= []

    for line in result:
        names.append(line[0])
        hr.append(int(line[1]))
        rbi.append(int(line[2]))

    fig.add_trace(
        go.Scatter(name="", xaxis='x3',
            x=hr, y=rbi,
            text=names,
            textfont_size=10,
            mode='markers',
            marker_symbol="diamond-cross-open",
            marker_size=10,
        ),
        row=2, col=3
    )
    ## end HR v RBI Correlation query + visualization

    ## roster info query + table ##
    database.execute("select FirstName, LastName, date_format(DOB, '%c/%e/%Y'), Height, Weight, Country, Position from Reds;")
    result = database.fetchall()
    name = []
    dob = []
    height = []
    weight = []
    country = []
    position = []

    for line in result:
        name.append(line[0]+ ' ' + line[1])
        dob.append((line[2]))
        height.append(line[3])
        weight.append(line[4])
        country.append(line[5])
        position.append(line[6])

    ## creates table to display roster info ##
    fig.add_trace(
        go.Table(header=dict(values=['Name', 'Date of Birth', 'Height', 'Weight', 'Country', 'Position']),
                    cells=dict(values=[name, dob, height, weight, country, position]), name="Reds 2019 Hitter Info")
                        ,
        row=3, col=1
    )

    fig.update_layout(autosize=True,height=1650, width=1550, showlegend=False,
                    title={
                        'text': "2019 Reds Hitters Visualization",
                        'y': 0.99,
                        'x': 0.5,
                        'xanchor': 'center',
                        'yanchor': 'top',
                        'font_size': 50
                    
    })
    ## end roster info ##
    fig.update_xaxes(title_text="wRC+", row=2, col=1)
    fig.update_xaxes(title_text="Home Runs", row=2, col=3)
    fig.update_yaxes(title_text="Runs Batted In", row=2, col=3)

    #display figures
    fig.show()
Пример #15
0
def vague_seuil_px():

    nb_individu = 1000 # recommandé : 1000
    variance_population = 5
    rayon_contamination = 2
    infectiosite = 0.8 # proba d'être infecté
    p = 0.35 # proba d'être immunisé
    d = 0.2 # proba de décès

    # NOTE : si les courbes restent constantes, augmentez le rayon de contamination
    # si le virus est trés mortel il n'y aura pas beaucoup de propagation

    # Bleu : '#636EFA'
    # Rouge : '#EF553B'
    # Vert : '#00CC96'
    # Violet : '#AB63FA'

    # création des figures
    fig = go.Figure()
    fig = make_subplots(rows=2, cols=2,column_widths=[0.8, 0.2],row_heights=[0.5,0.5],subplot_titles=["population","",""],specs=[[{'type': 'xy'},{'type': 'domain'}],[{'type': 'xy', 'colspan' : 2},None]],horizontal_spacing = 0.05,vertical_spacing=0.05)

    # création des courbes finales
    courbe_sains = []
    courbe_infectes = []
    courbe_immunises = []
    courbe_deces = []

    # dataset
    x, y = make_blobs(n_samples=nb_individu, centers=2, cluster_std=variance_population)  # création du dataset
    taille_pop = len(x)

    numero_infecte_1 = rd.randint(0, taille_pop-1)  # on choisit le premier individu infecté au hasard
    coord_1er_infecte = [x[:, 0][numero_infecte_1], x[:, 1][numero_infecte_1]]  # coordonnées du 1er infecté
    courbe_sains.append(taille_pop - 1)
    courbe_infectes.append(1)
    courbe_immunises.append(0)
    courbe_deces.append(0)

    # 1er vague
    coord_infectes = []  # cette liste sera implémentée des nouveaux cas
    coord_sains = []  # liste des cas sains
    for k in range(taille_pop):
        if [x[:, 0][k], x[:, 1][k]] == coord_1er_infecte:
            coord_infectes.append(coord_1er_infecte)
        elif distance(coord_1er_infecte, [x[:, 0][k], x[:, 1][k]]) < rayon_contamination and chance_infecte(
                infectiosite):
            coord_infectes.append([x[:, 0][k], x[:, 1][k]])
        else:
            coord_sains.append([x[:, 0][k], x[:, 1][k]])
    # actualisation des courbes finales
    courbe_sains.append(len(coord_sains))
    courbe_infectes.append(len(coord_infectes))
    courbe_immunises.append(0)
    courbe_deces.append(0)

    # vagues 2 à la fin
    coord_immunises = []  # on initialise
    coord_deces = []
    i = 1
    while len(courbe_infectes)!=0 and courbe_sains[i-1] > courbe_sains[i] : # conditions d'état stationnaire
        non_sains = []
        coord_infectes1, coord_immunises = immuniser(coord_infectes, coord_immunises, p) # on sépare immunisés et infectés
        coord_infectes, coord_deces = deces(coord_infectes1, coord_deces, coord_immunises, d) # on sépare les décès des infectés qu'ils restent

        for k in range(len(coord_infectes)):
            for j in range(len(coord_sains)):
                if distance(np.array(coord_infectes)[k, :],
                            np.array(coord_sains)[j, :]) < rayon_contamination and np.array(coord_sains)[j,:] not in np.array(coord_infectes) and chance_infecte(infectiosite):
                    coord_infectes.append(list(np.array(coord_sains)[j, :]))
                    non_sains.append(list(np.array(coord_sains)[j, :]))
        coord_sains = remove_(coord_sains, non_sains)
        # pour les courbes finales
        courbe_sains.append(len(coord_sains))
        courbe_infectes.append(len(coord_infectes))
        courbe_immunises.append(len(coord_immunises))
        courbe_deces.append(len(coord_deces))
        i += 1 # vague suivante
        
    # on trace les individus de la dernière vague si il y en a 
    if coord_sains != []:
        fig.add_trace(go.Scatter(x=np.array(coord_sains)[:, 0], y=np.array(coord_sains)[:, 1],name="sain", mode="markers", marker=dict(color='#636EFA'), showlegend=False),1,1)
    if coord_infectes != []:
        fig.add_trace(go.Scatter(x=np.array(coord_infectes)[:, 0], y=np.array(coord_infectes)[:, 1],name="infecté", mode="markers",marker=dict(color='#EF553B'), showlegend=False),1,1)
    if coord_immunises != []:
        fig.add_trace(go.Scatter(x=np.array(coord_immunises)[:, 0], y=np.array(coord_immunises)[:, 1],name='immunisé', mode="markers",marker=dict(color='#00CC96'), showlegend=False),1,1)
    if coord_deces != []:
        fig.add_trace(go.Scatter(x=np.array(coord_deces)[:, 0], y=np.array(coord_deces)[:, 1],name="décédé", mode="markers", marker=dict(color='#AB63FA'), showlegend=False),1,1)
    
    fig.update_traces(hoverinfo="name") # affichage du curseur de la souris
    fig.update_xaxes(showgrid=False, visible=False, row=1, col=1) # on supprime les axes
    fig.update_yaxes(showgrid=False, visible=False, row=1, col=1)

    # Pie finale
    labels = ["sains","infectés","immunisés","décédés"]
    fig.add_trace(go.Pie(values=[len(coord_sains),len(coord_infectes) ,len(coord_immunises), len(coord_deces)], labels=labels, sort=False),1,2)
    
    # on trace les courbes finales
    x_courbe = list(np.arange(0, len(courbe_sains))) # abscisses, correspond aux vagues
    fig.add_trace(go.Scatter(x=x_courbe, y=courbe_sains, marker=dict(color='#636EFA'), showlegend=False, name="sains", yaxis="y",),2,1)
    fig.add_trace(go.Scatter(x=x_courbe, y=courbe_infectes,  marker=dict(color='#EF553B'), showlegend=False, name="infectés", yaxis="y2",),2,1)
    fig.add_trace(go.Scatter(x=x_courbe, y=courbe_immunises, marker=dict(color='#00CC96'), showlegend=False, name="immunisés", yaxis="y3",),2,1)
    fig.add_trace(go.Scatter(x=x_courbe, y=courbe_deces, marker=dict(color='#AB63FA'), showlegend=False, name="décédés", yaxis="y4",),2,1)
    fig.update_xaxes(title_text="vague", row=2, col=1) # nom abscisses
    fig.update_yaxes(title_text="nombre d'individus", row=2, col=1) # nom ordonnées
    fig.add_annotation(text="Maximum d'infectés", x=courbe_infectes.index(max(courbe_infectes)), # ajouter un texte avec une flèche au pic d'infectés
                   y=max(courbe_infectes)+0.05*taille_pop, arrowhead=1, showarrow=True,row=2,col=1)
    fig.update_traces(
        hoverinfo="name+x+y", # affichage du curseur de la souris sur le point
        line={"width": 1.2},
        marker={"size": 6},
        mode="lines+markers",
        showlegend=False, row=2, col=1)
    fig.update_layout(hovermode="x")
    fig.update_layout(title_text="simulation virus") # titre du plot
    fig.update_layout(title_font_color='#EF553B',) # couleur du titre
    
    plot(fig)
Пример #16
0
                sizes[others_idx] += int(
                    round(df[(df.NUTZUNG_LEVEL2 == label)
                             & is_district].FLAECHE.sum()))
        else:
            labels_used.append(label)
            sizes.append(
                int(
                    round(df[(df.NUTZUNG_LEVEL2 == label)
                             & is_district].FLAECHE.sum())))

    explode = list(0 for label in labels_used)
    street_idx = labels_used.index('Straßenraum u. Parkplätze')
    explode[street_idx] = 0.1
    fig = go.Figure(data=[
        go.Pie(labels=labels_used,
               values=sizes,
               pull=explode,
               marker_colors=[label_colors[i] for i in labels_used])
    ])
    fig.update_traces(hoverinfo='label+text',
                      text=[f'{size:,} Quadratmeter' for size in sizes],
                      textinfo='percent',
                      textfont_size=14)
    fig.update_layout(legend=dict(
        x=0, y=-1, traceorder="normal", xanchor='left', yanchor='bottom'))
    fig.update_layout(autosize=True)
    pio.write_html(fig,
                   file=str(district) + '/index.html',
                   auto_open=False,
                   include_plotlyjs="cdn")  # , include_mathjax="cdn")
    fig.show()
Пример #17
0
def virus_px():
    
    #paramètres
    nb_individu = 1000 
    variance_population = 5
    rayon_contamination = 2
    infectiosite = 0.7
    p = 0.4 
    d = 0.2 #proba de décès

    fig = go.Figure() #création de la figure
    
    x, y = make_blobs(n_samples=nb_individu, centers=2, cluster_std=variance_population, shuffle=True) #dataset population
    taille_pop = len(x)
    
    # création des subplots - 'xy' pour des courbes et 'domain' pour des pies | colspan pour étendre un subplot sur 2 colonnes
    fig = make_subplots(rows=6, cols=2,column_widths=[0.8, 0.2],specs=[[{'type':'xy'},{'type':'domain'}],[{'type':'xy'},{'type':'domain'}],[{'type':'xy'},{'type':'domain'}],[{'type':'xy'},{'type':'domain'}],[{'type':'xy'},{'type':'domain'}],[{'type':'xy', "colspan": 2},None]])
    
    # initialisation des courbes finales
    courbe_sains = []
    courbe_infectes = []
    courbe_immunises = []
    courbe_deces = []
    
    "1er infecté"
    numero_infecte_1 = rd.randint(0, taille_pop) # au hasard un individu dans la population
    coord_1er_infecte = [x[:, 0][numero_infecte_1], x[:, 1][numero_infecte_1]]
    fig.add_trace(go.Scatter(x=x[:, 0],y=x[:, 1],mode="markers",marker=dict(color="Blue"),showlegend=False),1,1) # on trace les individus sains
    fig.add_trace(go.Scatter(x=[x[:, 0][numero_infecte_1]],y=[x[:, 1][numero_infecte_1]],mode="markers",marker=dict(color="Red"),showlegend=False),1,1) # on trace l'individu infecté
    
    "pie"
    labels = ["sains","infectés","immunisés","décédés"]
    fig.add_trace(go.Pie(labels=labels, values=[taille_pop-1,1,0,0],sort=False),1,2) # création du pie (graphe fromage)
    fig.update_xaxes(showgrid=False, visible= False, row=1, col=1)
    fig.update_yaxes(showgrid=False, visible= False, row=1, col=1)
    #actualisation des courbes
    courbe_sains.append(taille_pop - 1)
    courbe_infectes.append(1)
    courbe_immunises.append(0)
    courbe_deces.append(0)
    
    "1er vague"
    coord_infectes = []  # cette liste sera implémentée des nouveaux cas
    coord_sains = []  # liste des cas sains
    for k in range(taille_pop):
        if [x[:, 0][k], x[:, 1][k]] == coord_1er_infecte:
            coord_infectes.append(coord_1er_infecte)
        elif distance(coord_1er_infecte, [x[:, 0][k], x[:, 1][k]]) < rayon_contamination and chance_infecte(infectiosite):
            fig.add_trace(go.Scatter(x=[x[:, 0][k]], y=[x[:, 1][k]], mode="markers", marker=dict(color="DarkOrange"), showlegend=False),2,1)
            coord_infectes.append([x[:, 0][k], x[:, 1][k]])
        else:
            fig.add_trace(go.Scatter(x=[x[:, 0][k]], y=[x[:, 1][k]], mode="markers", marker=dict(color="Blue"), showlegend=False),2,1)
            coord_sains.append([x[:, 0][k], x[:, 1][k]])
            fig.add_trace(go.Scatter(x=[x[:, 0][numero_infecte_1]], y=[x[:, 1][numero_infecte_1]], mode="markers",marker=dict(color="Red"), showlegend=False),2,1)
    fig.update_xaxes(showgrid=False, visible=False, row=1, col=1) # on supprime les axes
    fig.update_yaxes(showgrid=False, visible=False, row=1, col=1)
    
    "pie"
    labels = ["sains","infectés","immunisés","décédés"]
    fig.add_trace(go.Pie(values=[ len(coord_sains),len(coord_infectes),0,0], labels=labels,sort=False),2,2)
    courbe_sains.append(len(coord_sains))
    courbe_infectes.append(len(coord_infectes))
    courbe_immunises.append(0)
    courbe_deces.append(0)
    
    "2e vague"
    non_sains = []
    coord_infectes1, coord_immunises = immuniser(coord_infectes, [], p)
    coord_infectes, coord_deces = deces(coord_infectes1, [], coord_immunises, d)
    for k in range(len(coord_infectes)):
        for j in range(len(coord_sains)):
            if distance(np.array(coord_infectes)[k, :], np.array(coord_sains)[j, :]) < rayon_contamination and list(np.array(coord_sains)[j, :]) not in coord_infectes:
                coord_infectes.append(list(np.array(coord_sains)[j, :]))
                non_sains.append(list(np.array(coord_sains)[j, :]))

    coord_sains = remove_(coord_sains, non_sains)
    if coord_sains != []:
        fig.add_trace(go.Scatter(x=np.array(coord_sains)[:, 0], y=np.array(coord_sains)[:, 1], mode="markers", marker=dict(color="Blue"), showlegend=False),3,1)
    if coord_infectes != []:
        fig.add_trace(go.Scatter(x=np.array(coord_infectes)[:, 0], y=np.array(coord_infectes)[:, 1], mode="markers",marker=dict(color="Red"), showlegend=False),3,1)
    if coord_immunises != []:
        fig.add_trace(go.Scatter(x=np.array(coord_immunises)[:, 0], y=np.array(coord_immunises)[:, 1], mode="markers",marker=dict(color="Green"), showlegend=False),3,1)
    if coord_deces != []:
        fig.add_trace(go.Scatter(x=np.array(coord_deces)[:, 0], y=np.array(coord_deces)[:, 1], mode="markers",marker=dict(color="Purple"), showlegend=False),3,1)
    fig.update_xaxes(showgrid=False, visible=False, row=2, col=1)
    fig.update_yaxes(showgrid=False, visible=False, row=2, col=1)
    
    "pie"
    labels = ["sains","infectés","immunisés","décédés"]
    fig.add_trace(go.Pie(values=[len(coord_sains),len(coord_infectes) ,len(coord_immunises), len(coord_deces)], labels=labels,sort=False),3,2)
    courbe_sains.append(len(coord_sains))
    courbe_infectes.append(len(coord_infectes))
    courbe_immunises.append(len(coord_immunises))
    courbe_deces.append(len(coord_deces))
    
    "3e vague"
    non_sains = []
    coord_infectes1, coord_immunises = immuniser(coord_infectes, coord_immunises, p)
    coord_infectes, coord_deces = deces(coord_infectes1, coord_deces, coord_immunises, d)
    for k in range(len(coord_infectes)):
        for j in range(len(coord_sains)):
            if distance(np.array(coord_infectes)[k, :], np.array(coord_sains)[j, :]) < rayon_contamination and list(
                    np.array(coord_sains)[j, :]) \
                    not in coord_infectes and chance_infecte(infectiosite):
                coord_infectes.append(list(np.array(coord_sains)[j, :]))
                non_sains.append(list(np.array(coord_sains)[j, :]))
    coord_sains = remove_(coord_sains, non_sains) # on enlève les individus sains qui sont maintenant infectés
    if coord_sains != []:
        fig.add_trace(go.Scatter(x=np.array(coord_sains)[:, 0], y=np.array(coord_sains)[:, 1], mode="markers", marker=dict(color="Blue"), showlegend=False),4,1)
    if coord_infectes != []:
        fig.add_trace(go.Scatter(x=np.array(coord_infectes)[:, 0], y=np.array(coord_infectes)[:, 1], mode="markers",marker=dict(color="Red"), showlegend=False),4,1)
    if coord_immunises != []:
        fig.add_trace(go.Scatter(x=np.array(coord_immunises)[:, 0], y=np.array(coord_immunises)[:, 1], mode="markers",marker=dict(color="Green"), showlegend=False),4,1)
    if coord_deces != []:
        fig.add_trace(go.Scatter(x=np.array(coord_deces)[:, 0], y=np.array(coord_deces)[:, 1], mode="markers",marker=dict(color="Purple"), showlegend=False),4,1)
    fig.update_xaxes(showgrid=False, visible=False, row=3, col=1)
    fig.update_yaxes(showgrid=False, visible=False, row=3, col=1)
    
    "pie"
    labels = ["sains","infectés","immunisés","décédés"]
    fig.add_trace(go.Pie(values=[len(coord_sains),len(coord_infectes) ,len(coord_immunises), len(coord_deces)], labels=labels,sort=False),4,2)
    courbe_sains.append(len(coord_sains))
    courbe_infectes.append(len(coord_infectes))
    courbe_immunises.append(len(coord_immunises))
    courbe_deces.append(len(coord_deces))
    
    "4e vague"
    non_sains = []
    coord_infectes1, coord_immunises = immuniser(coord_infectes, coord_immunises, p)
    coord_infectes, coord_deces = deces(coord_infectes1, coord_deces, coord_immunises, d)
    for k in range(len(coord_infectes)):
        for j in range(len(coord_sains)):
            if distance(np.array(coord_infectes)[k, :], np.array(coord_sains)[j, :]) < rayon_contamination and list(
                    np.array(coord_sains)[j, :]) not in \
                    coord_infectes and chance_infecte(infectiosite):
                coord_infectes.append(list(np.array(coord_sains)[j, :]))
                non_sains.append(list(np.array(coord_sains)[j, :]))
    coord_sains = remove_(coord_sains, non_sains)
    if coord_sains != []:
        fig.add_trace(go.Scatter(x=np.array(coord_sains)[:, 0], y=np.array(coord_sains)[:, 1], mode="markers", marker=dict(color="Blue"), showlegend=False),5,1)
    if coord_infectes != []:
        fig.add_trace(go.Scatter(x=np.array(coord_infectes)[:, 0], y=np.array(coord_infectes)[:, 1], mode="markers",marker=dict(color="Red"), showlegend=False),5,1)
    if coord_immunises != []:
        fig.add_trace(go.Scatter(x=np.array(coord_immunises)[:, 0], y=np.array(coord_immunises)[:, 1], mode="markers",marker=dict(color="Green"), showlegend=False),5,1)
    if coord_deces != []:
        fig.add_trace(go.Scatter(x=np.array(coord_deces)[:, 0], y=np.array(coord_deces)[:, 1], mode="markers",marker=dict(color="Purple"), showlegend=False),5,1)
    fig.update_xaxes(showgrid=False, visible=False, row=4, col=1)
    fig.update_yaxes(showgrid=False, visible=False, row=4, col=1)
    
    "pie"
    labels = ["sains","infectés","immunisés","décédés"]
    fig.add_trace(go.Pie(values=[len(coord_sains),len(coord_infectes) ,len(coord_immunises), len(coord_deces)], labels=labels,sort=False),5,2)
    courbe_sains.append(len(coord_sains))
    courbe_infectes.append(len(coord_infectes))
    courbe_immunises.append(len(coord_immunises))
    courbe_deces.append(len(coord_deces))
    
    "courbes"
    x_courbe = list(np.arange(0, len(courbe_sains)))
    # on trace les courbes finales
    fig.add_trace(go.Scatter(x=x_courbe,y=courbe_sains,marker=dict(color="Blue"), name="sain",showlegend=False),6,1)
    fig.add_trace(go.Scatter(x=x_courbe,y=courbe_infectes,marker=dict(color="Red"), name="infecté",showlegend=False),6,1)
    fig.add_trace(go.Scatter(x=x_courbe,y=courbe_immunises,marker=dict(color="Green"), name="immunisé", showlegend=False),6,1)
    fig.add_trace(go.Scatter(x=x_courbe,y=courbe_deces,marker=dict(color="Black"),name="décédé",showlegend=False),6,1)
    fig.update_traces(
        hoverinfo="name+x+y", # affichage du curseur avec la souris
        line={"width": 1.2},
        marker={"size": 6},
        mode="lines+markers",
        showlegend=False, row=2, col=1) 
    fig.update_layout(hovermode="x")
    
    plot(fig)
        & (db2015.copy().drop(['iso3'], axis=1)['countryname'] == country1)]

if age_new == 'All':
    table2 = db2015.copy().drop(
        ['iso3'], axis=1).rename(columns=dic_cor)[(db2015.copy().drop(
            ['iso3'], axis=1)['countryname'] == country2)]
else:
    table2 = db2015.copy().drop(['iso3'], axis=1).rename(columns=dic_cor)[
        (db2015.copy().drop(['iso3'], axis=1)['age'] == age_new)
        & (db2015.copy().drop(['iso3'], axis=1)['countryname'] == country2)]

table1 = pd.DataFrame(table1.drop(['age'], axis=1).mean()).reset_index()
table2 = pd.DataFrame(table2.drop(['age'], axis=1).mean()).reset_index()

labels1 = table1['index']
values1 = table1[0]
fig_new1 = go.Figure(data=[
    go.Pie(labels=labels1, values=values1, direction='clockwise', sort=False)
])

labels2 = table2['index']
values2 = table2[0]
fig_new2 = go.Figure(data=[
    go.Pie(labels=labels2, values=values2, direction='clockwise', sort=False)
])
st.markdown(f"Nutrition in {country1}")
st.plotly_chart(fig_new1)
st.markdown(f"Nutrition in {country2}")
st.plotly_chart(fig_new2)
########################################################################################################################
Пример #19
0
def metrics_of_tweet(
    df,
    retweet_col='retweet',
    conversation_id_col='conversation_id',
    id_col='id',
    retweet_id_col='retweet_id',
    user_id_col='user_id',
    user_rt_id_col='user_rt_id',
):

    df = df.drop_duplicates(
        subset=[conversation_id_col, id_col, retweet_id_col])
    number_tweets = df.shape[0]
    number_users = len(
        set(i for i in list(df[user_id_col].unique()) +
            list(df[user_rt_id_col].unique())))
    number_tweets_orig = sum((df[conversation_id_col] == df[id_col])
                             & (df[retweet_col] == False))
    number_answers = sum((df[conversation_id_col] != df[id_col])
                         & (df[retweet_col] == False))
    number_retweets = sum(df[retweet_col] == True)

    labels = ['Orig. Tweets', 'Answers', 'Retweets']
    values = [number_tweets_orig, number_answers, number_retweets]

    fig = go.Figure()

    fig.add_trace(
        go.Indicator(mode="number",
                     value=number_tweets,
                     domain={
                         'x': [0, 0.5],
                         'y': [0.75, 1]
                     },
                     title='Number of Tweets'))

    fig.add_trace(
        go.Indicator(mode="number",
                     value=number_users,
                     domain={
                         'x': [0, 0.5],
                         'y': [0.12, 0.3]
                     },
                     title='Number of Users'))

    fig.add_trace(
        go.Indicator(mode="gauge",
                     gauge={
                         'shape': "bullet",
                         'axis': {
                             'range': [None, number_tweets]
                         }
                     },
                     value=number_users,
                     domain={
                         'x': [0, 0.5],
                         'y': [0, 0.1]
                     }))

    fig.add_trace(
        go.Pie(labels=labels,
               values=values,
               textinfo='none',
               hole=0.7,
               domain={
                   'x': [0.65, 1],
                   'y': [0, 1]
               },
               title='Types of Tweets'))

    fig.update_layout(
        grid={
            'rows': 1,
            'columns': 1,
            'pattern': "independent"
        },
        legend={
            'x': 1,
            'y': 0.5,
            'orientation': 'v',
            'yanchor': 'middle'
        },
    )

    return fig
Пример #20
0
def getPlots(mm):
    wordAxisText = [np.asarray(mc[0][1]), np.asarray(mcw[0])]
    arrV = calcTicks(maxval, 6)
    arrT = [str(int(abs(x))) for x in arrV]

    pieM = go.Pie(values=[totalcount[0], totalcount[2]],
                  labels=[chatters[0].split()[0], chatters[1].split()[0]],
                  hole=0.4,
                  pull=0.01,
                  hovertemplate='%{label} %{percent}' +
                  '<br>%{value} messages</br><extra></extra>',
                  marker=dict(colors=chattercolor),
                  domain=dict(x=[0.77, 0.965], y=[0.075, 0.375]),
                  rotation=90,
                  textfont=dict(family='Roboto'))
    pieW = go.Pie(values=[totalcount[1], totalcount[3]],
                  labels=[chatters[0].split()[0], chatters[1].split()[0]],
                  hole=0.4,
                  pull=0.01,
                  hovertemplate='%{label} %{percent}' +
                  '<br>%{value} words</br><extra></extra>',
                  marker=dict(colors=chattercolor),
                  domain=dict(x=[0.77, 0.965], y=[0.575, 0.875]),
                  rotation=90,
                  textfont=dict(family='Roboto'))

    polH1 = go.Scatterpolar(cliponaxis=True,
                            connectgaps=True,
                            subplot='polar1',
                            hoveron="points",
                            name=chatters[0].split(' ')[0],
                            r=hourBuckets[mm],
                            theta=hours,
                            hovertemplate=chatters[0].split(' ')[0] + '<br>' +
                            dictionary[mm] + ': %{r}</br>' +
                            'Hour: %{theta}<extra></extra>',
                            fill='toself',
                            line=dict(color=chattercolor[0]))

    polH2 = go.Scatterpolar(cliponaxis=True,
                            connectgaps=True,
                            subplot='polar1',
                            hoveron="points",
                            r=hourBuckets[mm + 2],
                            theta=hours,
                            hovertemplate=chatters[1].split(' ')[0] + '<br>' +
                            dictionary[mm] + ': %{r}</br>' +
                            'Hour: %{theta}<extra></extra>',
                            fill='toself',
                            customdata=['1:23pm'],
                            line=dict(color=chattercolor[1]))

    polW1 = go.Scatterpolar(cliponaxis=True,
                            connectgaps=True,
                            subplot='polar2',
                            hoveron="points",
                            name=chatters[0],
                            r=weekdayBuckets[mm],
                            theta=weekdays,
                            hovertemplate=chatters[0].split(' ')[0] + '<br>' +
                            dictionary[mm] + ': %{r}</br>' +
                            'Day: %{theta}<extra></extra>',
                            fill='toself',
                            line=dict(color=chattercolor[0]))

    polW2 = go.Scatterpolar(cliponaxis=True,
                            connectgaps=True,
                            subplot='polar2',
                            hoveron="points",
                            name=chatters[1],
                            r=weekdayBuckets[mm + 2],
                            theta=weekdays,
                            hovertemplate=chatters[1].split(' ')[0] + '<br>' +
                            dictionary[mm] + ': %{r}</br>' +
                            'Day: %{theta}<extra></extra>',
                            fill='toself',
                            line=dict(color=chattercolor[1]))

    barL = go.Bar(y=mcw[0],
                  x=mc[0][1],
                  xaxis='x2',
                  yaxis='y2',
                  orientation='h',
                  text=-1 * wordAxisText[0].astype('int'),
                  hovertemplate=chatters[0].split(' ')[0] +
                  '<br>Word: %{y}</br>' + 'Count: %{text}<extra></extra>',
                  marker=dict(color=chattercolor[0]))

    barR = go.Bar(y=mcw[0],
                  x=mc[1][1],
                  orientation='h',
                  xaxis='x2',
                  yaxis='y2',
                  text=wordAxisText[1].astype(str),
                  textposition='outside',
                  texttemplate=' %{y}',
                  hovertemplate=chatters[1].split(' ')[0] +
                  '<br>Word: %{y}</br>' + 'Count: %{x}<extra></extra>',
                  textfont=dict(family='Open Sans, light'),
                  marker=dict(color=chattercolor[1]))

    layout = go.Layout(
        autosize=True,
        margin=dict(t=0, b=0, l=0, r=0, pad=0),
        template='plotly_dark',
        polar1=dict(domain=dict(x=[0.035, 0.23], y=[0.075, 0.375]),
                    radialaxis=dict(visible=True,
                                    showticklabels=False,
                                    showline=False,
                                    angle=45,
                                    tickangle=45,
                                    tickmode='auto',
                                    nticks=4,
                                    tickfont=dict(size=10, family='Roboto')),
                    angularaxis=dict(rotation=90,
                                     direction='clockwise',
                                     type='category',
                                     tickfont=dict(family='Roboto'))),
        polar2=dict(domain=dict(x=[0.035, 0.23], y=[0.575, 0.875]),
                    radialaxis=dict(visible=True,
                                    showticklabels=False,
                                    showline=False,
                                    angle=45,
                                    tickangle=45,
                                    tickmode='auto',
                                    nticks=5,
                                    tickfont=dict(size=8, family='Roboto')),
                    angularaxis=dict(rotation=90,
                                     direction='clockwise',
                                     type='category',
                                     tickfont=dict(family='Roboto'))),
        xaxis2=dict(domain=[0.25, 0.75],
                    tickfont=dict(family='Roboto'),
                    position=0,
                    anchor='free',
                    range=[-maxval * 1.1, maxval * 1.1],
                    uirevision='n',
                    tickvals=arrV,
                    ticktext=arrT,
                    nticks=6),
        yaxis2=dict(domain=[0, 0.93],
                    tickfont=dict(family='Roboto'),
                    showticklabels=False,
                    type='category'),
        showlegend=False,
        barmode='overlay',
        transition={
            'duration': 500,
            'ordering': "traces first"
        },
        annotations=[
            dict(text=dictionary[mm] + '<br>per weekday',
                 showarrow=False,
                 font=dict(size=35),
                 xref="paper",
                 yref="paper",
                 xanchor='center',
                 x=0.1328,
                 y=1,
                 height=100,
                 width=250,
                 align='center'),
            dict(text=dictionary[mm] + '<br>per hour',
                 showarrow=False,
                 font=dict(size=35),
                 xref="paper",
                 yref="paper",
                 xanchor='center',
                 x=0.1328,
                 y=0.45,
                 height=500,
                 width=250,
                 align='center'),
            dict(text='Most used words',
                 showarrow=False,
                 font=dict(size=40),
                 xref="paper",
                 yref="paper",
                 xanchor='center',
                 x=0.5,
                 y=1,
                 width=400),
            dict(text='Words<br>per person',
                 showarrow=False,
                 font=dict(size=35),
                 xref="paper",
                 yref="paper",
                 xanchor='center',
                 x=0.8672,
                 y=1,
                 height=100,
                 width=250,
                 align='center'),
            dict(text='Messages<br>per person',
                 showarrow=False,
                 font=dict(size=35),
                 xref="paper",
                 yref="paper",
                 xanchor='center',
                 x=0.8672,
                 y=0.45,
                 height=500,
                 width=250,
                 align='center')
        ])

    if totalcount[mm] >= totalcount[mm + 2]:
        data = [pieM, pieW, polH1, polH2, polW1, polW2, barL, barR]
    else:
        data = [pieM, pieW, polH2, polH1, polW2, polW1, barL, barR]

    fig = go.Figure(data=data, layout=layout)
    return fig
# it shows the difference of booking cancellations for th total amount of book canceled or not and the percentage(resort and city hotels)
resort_hotel = hotel_booked[hotel_booked["hotel"] == "Resort Hotel"]
city_hotel = hotel_booked[hotel_booked["hotel"] == "City Hotel"]

cancel_reso = pd.DataFrame(resort_hotel["is_canceled"].value_counts())
cancel_reso.rename(columns={"is_canceled": "booking_cancellations"}, index =({0: "No Canceled", 1:"Yes Canceled"}), inplace=True)
cancel_reso["Status"] = cancel_reso.index

cancel_city = pd.DataFrame(city_hotel["is_canceled"].value_counts())
cancel_city.rename(columns={"is_canceled": "booking_cancellations"}, index =({0: "No Canceled", 1:"Yes Canceled"}), inplace=True)
cancel_city["Status"] = cancel_city.index

# make a pie chart to show the differece book canceled for those hotels

fig = make_subplots(rows=1, cols=2, specs=[[{'type':'domain'}, {'type':'domain'}]], subplot_titles = ["City Hotel", "Resort Hotel"])
fig.add_trace(go.Pie(values = cancel_city["booking_cancellations"], labels = cancel_city["Status"]),1,1)
fig.add_trace(go.Pie(values = cancel_reso["booking_cancellations"], labels = cancel_reso["Status"]),1,2)
fig.update_traces(textposition='inside', textinfo='value+percent+label')
fig.update_layout(title_text = "Type Of Booking Cancellations For City Hotel And Resort Hotel")
fig.show()

# scatter plot between reservation_status and lead_time
px.scatter(hotel_booked, x='reservation_status', y='lead_time', color='reservation_status', opacity=0.1)

# add new feature engineering from existing adr column
# make a new column called total overnight cost in each hotel for guest
hotel_booked["adr_pp"] =hotel_booked["adr"] / (hotel_booked["adults"] + hotel_booked["children"])
room_guest = hotel_booked[hotel_booked["reservation_status"]== 0]
room_cost = room_guest[["hotel", "adr_pp", "reserved_room_type"]].sort_values("reserved_room_type")

plt.figure(figsize= (13,9))
import plotly.graph_objects as go

labels = ['Oxygen','Hydrogen','Carbon_Dioxide','Nitrogen']
values = [4500, 2500, 1053, 500]

fig = go.Figure(data=[go.Pie(labels=labels, values=values)])
fig.show()
Пример #23
0
def reddit_radios(n_clicks, subreddit):
    if n_clicks > 0:
        #return 'You have selected "{}"'.format(subreddit)

        #Define function to pull up historical Reddit post information
        def load_results(lower_bound_timestamp, upper_bound_timestamp,
                         target_result_size, target_subreddit,
                         score_threshold):
            headline_collection = set()

            reddit_data_url = f"https://api.pushshift.io/reddit/submission/search/?after={lower_bound_timestamp}&before={upper_bound_timestamp}&sort_type=score&sort=desc&subreddit={target_subreddit}&limit={target_result_size}&score={score_threshold}"

            try:
                with urllib.request.urlopen(reddit_data_url) as url:
                    data = json.loads(url.read().decode())

                    for submission in data['data']:
                        headline_collection.add(submission['title'])

                return headline_collection
            except urllib.error.HTTPError as e:
                print(e.__dict__)
                return set()
            except urllib.error.URLError as e:
                print(e.__dict__)
                return set()

        # Get Reddit posts
        headlines = set()

        time_now = datetime.datetime.now()

        limit_delta = 7
        limit_lower_delta = 6

        result_size = 1000
        score_limit = ">0"

        for i in range(0, 8):
            previous_timestamp = int(
                (time_now - datetime.timedelta(days=limit_delta)).timestamp())
            current_timestamp = int(
                (time_now -
                 datetime.timedelta(days=limit_lower_delta)).timestamp())

            full_collection = load_results(previous_timestamp,
                                           current_timestamp, result_size,
                                           subreddit, score_limit)
            headlines = headlines.union(full_collection)

            limit_delta = limit_delta - 1
            limit_lower_delta = limit_lower_delta - 1

            display.clear_output()

        # Calculate polarity to get the sentiment
        sia = SentimentIntensityAnalyzer()
        results = []

        for line in headlines:
            pol_score = sia.polarity_scores(line)
            pol_score['headline'] = line
            results.append(pol_score)

        #Convert the results to a dataframe
        df = pd.DataFrame.from_records(results)

        #Label the results accordingly
        df['label'] = 'Neutral'
        df.loc[df['compound'] > 0.1, 'label'] = 'Positive'
        df.loc[df['compound'] < -0.1, 'label'] = 'Negative'

        #Create Pie Chart
        labels = df['label'].value_counts().index
        values = df['label'].value_counts()
        fig = go.Figure(data=[go.Pie(labels=labels, values=values)])
        reddit_pie = html.Div([dcc.Graph(figure=fig)])

        # Clean the text
        allWords = ' '.join([rdf for rdf in df['headline']])
        allWords_lower = allWords.lower()
        stop = set(stopwords.words('english') + list(string.punctuation))
        new_stopwords = ['...', '``', "''", '’', "'s", "n't"]
        new_stopwords_list = stop.union(new_stopwords)

        text_tokens = nltk.word_tokenize(allWords_lower)
        text_no_stop_words_punct = [
            t for t in text_tokens
            if t not in new_stopwords_list and t not in string.punctuation
        ]

        filtered_string = (" ").join(text_no_stop_words_punct)

        # Convert the long string to a dictionary with frequency counts.
        def word_count(str):
            counts = dict()
            words = str.split()

            for word in words:
                if word in counts:
                    counts[word] += 1
                else:
                    counts[word] = 1

            return counts

        reddit_wordcloud = (word_count(filtered_string))

        # Create Word Cloud
        wc = WordCloud().generate_from_frequencies(
            frequencies=reddit_wordcloud)
        wc_img = wc.to_image()
        with BytesIO() as buffer:
            wc_img.save(buffer, 'png')
            img2 = base64.b64encode(buffer.getvalue()).decode()

        #Display Pie Chart and Word Cloud
        reddit_results = html.Div([
            html.Div(dcc.Graph(id='graph1', figure=fig),
                     style={
                         'width': '49%',
                         'display': 'inline-block'
                     }),
            html.Div(children=[
                html.Img(src="data:image/png;base64," + img2,
                         style={
                             'height': '50%',
                             'width': '50%'
                         })
            ],
                     style={
                         'width': '49%',
                         'display': 'block',
                         'textAlign': 'center'
                     }),
        ])

        return reddit_results
Пример #24
0
import plotly.graph_objects as go
from dash.dependencies import Input, Output

import load_data as ld
import site_info as si
import log_writer as lw

site_top_df = ld.load_data_site()
site_top_df.sort_values('Визиты', inplace=True)
site_label1 = site_top_df.sort_values(
    'Посетители', ascending=False).reset_index().drop('index', axis=1)

el_b_df = ld.load_data_eb()
fig_site_top3 = go.Figure(data=[
    go.Pie(labels=el_b_df['site_page'],
           values=el_b_df['Глубина просмотра'],
           hole=.3)
])
fig_site_top3.update_layout(
    title_text='Глубина просмотра раздела "Электронный бюджет"',
    autosize=True,
    piecolorway=[
        '#26205b', '#3257af', '#c8abd5', '#f9c5d8', '#b83e74', '#8d0837',
        '#9456ef'
    ],
    paper_bgcolor='#ebecf1',
    plot_bgcolor='#ebecf1')

# ------------------------------------- start load data block ------------------------------------------------------

etsp_df = ld.LoadEtspData()
Пример #25
0
    fig_3 = make_subplots(rows=1, cols=len(choice), subplot_titles=choice)
    if breakdown_type == 'Bar plot':
        for i in range(1):
            for j in range(len(choice)):
                fig_3.add_trace(
                    go.Bar(x=plot_sentiment(choice[j]).Sentiment, y=plot_sentiment(choice[j]).Tweets, showlegend=False),
                    row=i+1, col=j+1
                )
        fig_3.update_layout(height=600, width=800)
        st.plotly_chart(fig_3)
    else:
        fig_3 = make_subplots(rows=1, cols=len(choice), specs=[[{'type':'domain'}]*len(choice)], subplot_titles=choice)
        for i in range(1):
            for j in range(len(choice)):
                fig_3.add_trace(
                    go.Pie(labels=plot_sentiment(choice[j]).Sentiment, values=plot_sentiment(choice[j]).Tweets, showlegend=True),
                    i+1, j+1
                )
        fig_3.update_layout(height=600, width=800)
        st.plotly_chart(fig_3)
st.sidebar.subheader("Breakdown airline by sentiment")
choice = st.sidebar.multiselect('Pick airlines', ('US Airways','United','American','Southwest','Delta','Virgin America'), key=0)
if len(choice) > 0:
    choice_data = data[data.airline.isin(choice)]
    fig_0 = px.histogram(
                        choice_data, x='airline', y='airline_sentiment',
                         histfunc='count', color='airline_sentiment',
                         facet_col='airline_sentiment', labels={'airline_sentiment':'tweets'},
                          height=600, width=800)
    st.plotly_chart(fig_0)
Пример #26
0
def update_figure_user(start_date_user, end_date_user, choosen_month,
                       choosen_week, choice_type_period):
    if choice_type_period == 'm':
        period_choice = True
        week_choice = True
        month_choice = False
        lw.log_writer(f'User choice "month = {choosen_month}"')

        if int(choosen_month) > 1:
            etsp_prev_filt_df = etsp_df[etsp_df['month_open'] == (
                int(choosen_month) - 1)]
            sue_prev_filt_df = sue_df[sue_df['month_open'] == (
                int(choosen_month) - 1)]
            osp_prev_filt_df = osp_df[osp_df['month_open'] == (
                int(choosen_month) - 1)]
        else:
            etsp_prev_filt_df = etsp_df[etsp_df['month_open'] == 12]
            sue_prev_filt_df = sue_df[sue_df['month_open'] == 12]
            osp_prev_filt_df = osp_df[osp_df['month_open'] == 12]

        etsp_filtered_df = etsp_df[etsp_df['month_open'] == int(choosen_month)]
        sue_filtered_df = sue_df[sue_df['month_open'] == int(choosen_month)]
        osp_filtered_df = osp_df[osp_df['month_open'] == int(choosen_month)]
        sue_incidents_filtered_df = sue_incidents_df[
            sue_incidents_df['month_open'] == int(choosen_month)]
        print(sue_incidents_filtered_df)

        start_date_metrika = ld.GetMonthPeriod(ld.current_year,
                                               choosen_month)[0]
        end_date_metrika = ld.GetMonthPeriod(ld.current_year, choosen_month)[1]

        filtered_metrika_df = si.get_site_info(start_date_metrika,
                                               end_date_metrika)

    elif choice_type_period == 'p':
        period_choice = False
        week_choice = True
        month_choice = True
        lw.log_writer(
            f'User choice "range period start = {start_date_user}, end = {end_date_user}"'
        )

        if int(start_date_user[5:7]) > 1:
            etsp_prev_filt_df = etsp_df[etsp_df['month_open'] == (
                int(start_date_user[5:7]) - 1)]
            sue_prev_filt_df = sue_df[sue_df['month_open'] == (
                int(start_date_user[5:7]) - 1)]
            osp_prev_filt_df = osp_df[osp_df['month_open'] == (
                int(start_date_user[5:7]) - 1)]
        else:
            etsp_prev_filt_df = etsp_df[etsp_df['month_open'] == 12]
            sue_prev_filt_df = sue_df[sue_df['month_open'] == 12]
            osp_prev_filt_df = osp_df[osp_df['month_open'] == 12]

        etsp_filtered_df = etsp_df[(etsp_df['start_date'] >= start_date_user)
                                   & (etsp_df['start_date'] <= end_date_user)]
        sue_filtered_df = sue_df[(sue_df['start_date'] >= start_date_user)
                                 & (sue_df['start_date'] <= end_date_user)]
        osp_filtered_df = osp_df[(osp_df['start_date'] >= start_date_user)
                                 & (osp_df['start_date'] <= end_date_user)]
        sue_incidents_filtered_df = sue_incidents_df[
            (sue_incidents_df['Дата обращения'] >= start_date_user)
            & (sue_incidents_df['Дата обращения'] <= end_date_user)]

        start_date_metrika = start_date_user
        end_date_metrika = end_date_user

        filtered_metrika_df = si.get_site_info(start_date_metrika,
                                               end_date_metrika)

    else:
        period_choice = True
        week_choice = False
        month_choice = True
        lw.log_writer(
            f'User choice "week = {choosen_week} ({ld.GetPeriod(ld.current_year, choosen_week)})"'
        )

        if int(choosen_week) > 1:
            etsp_prev_filt_df = etsp_df[etsp_df['week_open'] == (
                int(choosen_week) - 1)]
            sue_prev_filt_df = sue_df[sue_df['week_open'] == (
                int(choosen_week) - 1)]
            osp_prev_filt_df = osp_df[osp_df['week_open'] == (
                int(choosen_week) - 1)]
        else:
            etsp_prev_filt_df = etsp_df[etsp_df['week_open'] == 52]
            sue_prev_filt_df = sue_df[sue_df['week_open'] == 52]
            osp_prev_filt_df = osp_df[osp_df['week_open'] == 52]

        etsp_filtered_df = etsp_df[etsp_df['week_open'] == int(choosen_week)]
        sue_filtered_df = sue_df[sue_df['week_open'] == int(choosen_week)]
        osp_filtered_df = osp_df[osp_df['week_open'] == int(choosen_week)]
        sue_incidents_filtered_df = sue_incidents_df[
            sue_incidents_df['week_open'] == int(choosen_week)]

        start_date_metrika = ld.GetPeriod(ld.current_year, choosen_week,
                                          's')[0]
        end_date_metrika = ld.GetPeriod(ld.current_year, choosen_week, 's')[1]

        filtered_metrika_df = si.get_site_info(start_date_metrika,
                                               end_date_metrika)

    etsp_count_tasks = etsp_filtered_df['count_task'].sum()
    sue_count_tasks = sue_filtered_df['count_task'].sum()
    osp_count_tasks = osp_filtered_df['count_task'].sum()

    etsp_prev_count_tasks = etsp_prev_filt_df['count_task'].sum()
    sue_prev_count_tasks = sue_prev_filt_df['count_task'].sum()
    osp_prev_count_tasks = osp_prev_filt_df['count_task'].sum()

    etsp_avg_time = ld.CountMeanTime(etsp_filtered_df)
    sue_avg_time = ld.CountMeanTime(sue_filtered_df)
    osp_avg_time = ld.CountMeanTime(osp_filtered_df)

    visits = str(int(filtered_metrika_df['visits'][0]))
    users = str(int(filtered_metrika_df['users'][0]))
    pageviews = str(int(filtered_metrika_df['pageviews'][0]))
    bounceRate = ''.join(
        [str(round(filtered_metrika_df['bounceRate'][0], 2)), "%"])
    pageDepth = str(round(filtered_metrika_df['pageDepth'][0], 2))
    avgVisitDurSec = str(
        dt.timedelta(seconds=round(
            filtered_metrika_df['avgVisitDurationSeconds'][0], 0)))[2:]
    site_stat_data = [{
        'Визиты': visits,
        'Посетители': users,
        'Просмотры': pageviews,
        'Отказы': bounceRate,
        'Глубина просмотра': pageDepth,
        'Время на сайте': avgVisitDurSec
    }]

    fig_support = go.Figure(
        go.Bar(y=[etsp_count_tasks, sue_count_tasks, osp_count_tasks],
               x=['ЕЦП', 'СУЭ', 'ОСП'],
               base=0,
               marker=dict(color=['#a92b2b', '#37a17c', '#a2d5f2']),
               text=[etsp_count_tasks, sue_count_tasks, osp_count_tasks],
               textposition='auto'))
    fig_support.update_layout(autosize=True,
                              legend=dict(orientation="h",
                                          yanchor="bottom",
                                          y=0.2,
                                          xanchor="right",
                                          x=0.5),
                              paper_bgcolor='#ebecf1',
                              plot_bgcolor='#ebecf1')
    fig_support.update_xaxes(ticks="inside", tickson="boundaries")

    total_curr_tasks = etsp_count_tasks + sue_count_tasks + osp_count_tasks
    total_prev_tasks = etsp_prev_count_tasks + sue_prev_count_tasks + osp_prev_count_tasks
    diff_tasks = total_curr_tasks - total_prev_tasks

    if diff_tasks > 0:
        style_tasks = {'font-size': '2em', 'color': 'green'}
        diff_tasks = '+ ' + str(diff_tasks)
    elif diff_tasks == 0:
        style_tasks = {'font-size': '2em'}
        diff_tasks = str(diff_tasks)
    else:
        style_tasks = {'font-size': '2em', 'color': 'red'}
        diff_tasks = str(diff_tasks)

    total_tasks = ''.join([str(total_curr_tasks), ' ( ', diff_tasks, ' )'])

    total_curr_users = len(etsp_filtered_df['user'].unique()) + len(
        sue_filtered_df['user'].unique()) + len(
            osp_filtered_df['user'].unique())
    total_prev_users = len(etsp_prev_filt_df['user'].unique()) + len(
        sue_prev_filt_df['user'].unique()) + len(
            osp_prev_filt_df['user'].unique())
    diff_users = total_curr_users - total_prev_users

    if diff_users > 0:
        style_users = {'font-size': '2em', 'color': 'green'}
        diff_users = '+ ' + str(diff_users)
    elif diff_users == 0:
        style_users = {'font-size': '2em'}
        diff_users = str(diff_users)
    else:
        style_users = {'font-size': '2em', 'color': 'red'}
        diff_users = str(diff_users)

    total_users = ''.join([str(total_curr_users), ' ( ', diff_users, ' )'])

    labels_figure_support = ["ЕЦП", "СУЭ", "ОСП"]
    values_figure_support = [
        etsp_filtered_df['count_task'].sum(),
        sue_filtered_df['count_task'].sum(),
        osp_filtered_df['count_task'].sum()
    ]
    colors = ['#a92b2b', '#37a17c', '#a2d5f2']

    fig = go.Figure(
        go.Pie(labels=labels_figure_support,
               values=values_figure_support,
               marker_colors=colors))
    fig.update_traces(hoverinfo="label+percent+name")

    fig.update_layout(paper_bgcolor='#ebecf1', showlegend=True)

    etsp_top_user_filtered_df = ld.TopUser(etsp_filtered_df)

    sue_top_user_filtered_df = ld.TopUser(sue_filtered_df)

    if len(sue_incidents_filtered_df) > 0:
        style_data = dict(width='20%', backgroundColor='#ff847c')
        tooltip_data = [{
            column: {
                'value': str(value),
                'type': 'markdown'
            }
            for column, value in row.items()
        } for row in sue_incidents_filtered_df.to_dict('records')]

    else:
        style_data = dict(width='20%', backgroundColor='#c4fbdb')
        sue_incidents_filtered_df = ld.NoIncidents()
        tooltip_data = sue_incidents_filtered_df.to_dict('records')

    return (period_choice, month_choice, week_choice, fig_support, total_tasks,
            style_tasks, total_users, style_users, etsp_avg_time,
            sue_avg_time, osp_avg_time, fig,
            sue_incidents_filtered_df.to_dict('records'), style_data,
            etsp_top_user_filtered_df.to_dict('records'),
            sue_top_user_filtered_df.to_dict('records'), site_stat_data,
            tooltip_data)
Пример #27
0
def Aff_pies_func(input):
    aff_data = input
    if sum(aff_data.Count) < 2:
        pi_plural = "PI"
    else:
        pi_plural = "PIs"
    colours = np.array([""] * len(aff_data["PI_aff"]), dtype=object)
    for i in aff_data["PI_aff"]:
        colours[
            np.where(aff_data["PI_aff"] == i)
        ] = FACILITY_USER_AFFILIATION_COLOUR_OFFICIAL_ABB[str(i)]
    fig = go.Figure(
        go.Pie(
            #    aff_data,
            values=aff_data["Count"],
            labels=aff_data["PI_aff"],
            hole=0.6,
            # color=aff_data["PI_aff"],
            marker=dict(colors=colours, line=dict(color="#000000", width=1)),
            direction="clockwise",
            sort=True,
        )
    )
    # fig = px.pie(
    #     aff_data,
    #     values="Count",
    #     names="PI_aff",
    #     hole=0.6,
    #     color="PI_aff",
    #     color_discrete_map=FACILITY_USER_AFFILIATION_COLOUR_OFFICIAL_ABB,
    # )

    fig.update_traces(
        textposition="outside",
        texttemplate="%{label} (%{percent:.1%f})",
        # the :.1%f does to 1 decimal place. do .0 to round to whole numbers. HOWEVER! whole numbers tend to give 0% as a value. Wouldnt recommend
    )  # textinfo="percent+label")
    fig.update_layout(
        margin=dict(
            l=100, r=100, b=100, t=100
        ),  # original values l=200, r=200 b=100 t=300
        font=dict(size=34),  # 36 works for most
        annotations=[
            dict(
                showarrow=False,
                text="{} {}".format(sum(aff_data.Count), pi_plural),
                font=dict(size=50),  # should work for all centre bits
                x=0.5,
                y=0.5,
            )
        ],
        showlegend=False,
        width=1000,
        height=1000,
        autosize=False,
    )
    if not os.path.isdir("Plots/Aff_Pies/"):
        os.mkdir("Plots/Aff_Pies/")
    if sum(aff_data.Count) > 0:
        fig.write_image(
            "Plots/Aff_Pies/{}_{}_affs.svg".format(
                input["Unit"][input["Unit"].first_valid_index()],
                input["Year"][input["Year"].first_valid_index()],
            )
        )
    else:
        print(
            "Warning: not all unit year combinations have data - check whether this is expected"
        )
Пример #28
0
cursor.execute("""
Select Count(flower_id)/(select count(*) from sepal), spicies_name from sepal
LEFT join Vid_irisa on sepal.species_id=Vid_irisa.species_id 
group by spicies_name
""")

name = []
count = []

for row in cursor:
    print("Вид ириса ", str(row[1]) + " Количество в выборке: " + str(row[0]) )
    name += [row[1]]
    count += [row[0]]


pie = go.Pie(labels=name, values=count)
ohyt = py.plot([pie])






"...............................Creating DashBoar..........................."

my_dboard = dashboard.Dashboard()

vovin_diagram = fileId_from_url(ohyt3)
voin_kryg = fileId_from_url(ohyt2)
vovini_tochki = fileId_from_url(ohyt)
Пример #29
0
import plotly.graph_objects as go
import numpy as np

np.random.seed(42)

# declaring size of arr
size = 7

x = [f'Product {i}' for i in range(size)]
y = np.random.randint(low=0, high=100, size=size)

# creating a Pie Chart
fig = go.Figure(data=[go.Pie(labels=x, values=y)])

# Adjusting width and height of the image
fig.layout.template = 'plotly_dark'
# To display labels with the percentage
fig.update_traces(textposition='inside', textinfo='percent+label')
fig.update_layout(title='Pie Chart',
                  xaxis_title='X Axis Title',
                  yaxis_title='Y Axis Title',
                  autosize=False,
                  width=600,
                  height=600,
                  margin=dict(l=50, r=50, b=100, t=100, pad=4))
fig.show()
Пример #30
0
def plot_stock_data(ticker="AAPL"):
    import dash
    import dash_core_components as dcc
    import dash_html_components as html
    import plotly.graph_objects as go
    from plotly.offline import plot
    from plotly.subplots import make_subplots
    import pandas as pd
    from datetime import datetime
    from dash.dependencies import Input, Output
    import json
    import plotly.express as px

    ticker_list, ticker_names = get_data.get_tickers_list_SP500()

    ticker_list = [{
        "label": name,
        "value": ticker
    } for name, ticker in zip(ticker_names, ticker_list)]
    df = pd.read_csv(f"tickers_data/{ticker}.csv")
    fig = make_subplots(specs=[[{"secondary_y": True}]])
    fig.add_trace(go.Candlestick(x=df["datetime"],
                                 open=df["open"],
                                 high=df["high"],
                                 low=df["low"],
                                 close=df["close"]),
                  secondary_y=False)
    fig.add_trace(go.Bar(x=df["datetime"], y=df["volume"], opacity=1),
                  secondary_y=False)
    fig.layout.yaxis2.showgrid = False
    fig.update_layout(title=f"{ticker}", clickmode="event+select")

    optimal_weights = portfolio_optimization()
    #optimal_weights = pd.DataFrame({"names": optimal_weights.keys(), "values": optimal_weights.values()})
    #print(optimal_weights.head())
    portfolio_fig = make_subplots()
    portfolio_fig.add_trace(
        go.Pie(labels=list(optimal_weights.keys()),
               values=list(optimal_weights.values())))
    # DASH APP
    app = dash.Dash("Stock Data")
    '''
    "data": [{"x": df["datetime"], "open": df["open"], "high": df["high"], "low": df["low"],
                          "close": df["close"],
                          "name":ticker,
                          "type": "candlestick"
                          },
                         {"x": df["datetime"], "y": df["volume"], "type": "bar"}
                         ]
    '''
    app.layout = html.Div([
        dcc.Graph(id="basic-interactions", figure=fig),
        html.Div(children=[
            dcc.Dropdown(id="input-stock", options=ticker_list, value='AAPL')
        ]),
        dcc.Graph(id="portfolio-weights", figure=portfolio_fig)
    ])

    @app.callback(Output('basic-interactions', 'figure'),
                  Input('input-stock', 'value'))
    def change_plot(ticker):
        df = pd.read_csv(f"tickers_data/{ticker}.csv")
        fig = make_subplots(specs=[[{"secondary_y": True}]])
        fig.add_trace(go.Candlestick(x=df["datetime"],
                                     open=df["open"],
                                     high=df["high"],
                                     low=df["low"],
                                     close=df["close"]),
                      secondary_y=True)
        fig.add_trace(go.Bar(x=df["datetime"], y=df["volume"]),
                      secondary_y=False)
        fig.layout.yaxis2.showgrid = False
        fig.update_layout(title=f"{ticker}", clickmode="event+select")
        return fig

    app.run_server(debug=True)