Exemplo n.º 1
0
def plot(adjacency_list: dict,
         elements: list,
         x_coordinates: list,
         y_coordinates: list,
         z_coordinates: list,
         plot_name: str = 'plot') -> None:
    """Creates a 3D scatter plot"""
    def atom_trace():
        """Creates an atom trace for the plot"""
        colors = [
            cpk_colors.get(element, cpk_color_rest) for element in elements
        ]
        markers = dict(color=colors,
                       line=dict(color='lightgray', width=2),
                       size=7,
                       symbol='circle',
                       opacity=0.8)
        trace = go.Scatter3d(x=x_coordinates,
                             y=y_coordinates,
                             z=z_coordinates,
                             mode='markers',
                             marker=markers,
                             text=elements)
        return trace

    def bond_trace():
        """"Creates a bond trace for the plot"""
        trace = go.Scatter3d(x=[],
                             y=[],
                             z=[],
                             hoverinfo='none',
                             mode='lines',
                             marker=dict(color='grey', size=7, opacity=1))
        adjascent_atoms = ((atom, neighbour)
                           for atom, neighbours in adjacency_list.items()
                           for neighbour in neighbours)
        for i, j in adjascent_atoms:
            trace['x'] += (x_coordinates[i], x_coordinates[j], None)
            trace['y'] += (y_coordinates[i], y_coordinates[j], None)
            trace['z'] += (z_coordinates[i], z_coordinates[j], None)
        return trace

    atoms = zip(elements, x_coordinates, y_coordinates, z_coordinates)
    annotations = [
        dict(text=element, x=x, y=y, z=z, showarrow=False, yshift=15)
        for element, x, y, z in atoms
    ]
    data = [atom_trace(), bond_trace()]
    axis_params = dict(showgrid=False,
                       showticklabels=False,
                       zeroline=False,
                       titlefont=dict(color='white'))
    layout = go.Layout(scene=dict(xaxis=axis_params,
                                  yaxis=axis_params,
                                  zaxis=axis_params,
                                  annotations=annotations),
                       margin=dict(r=0, l=0, b=0, t=0),
                       showlegend=False)
    fig = go.Figure(data=data, layout=layout)
    offline.plot(fig, show_link=False, filename=plot_name + '.html')
Exemplo n.º 2
0
def plot(ip_count_list):
    data = [
        go.Bar(x=[x[0] for x in ip_count_list],
               y=[x[1] for x in ip_count_list])
    ]

    py.plot(data, filename='freq.html')
Exemplo n.º 3
0
def interactive_plot_patients(df, df_label, fs_id1=1, fs_id2=2, class_patient=[1, 3, 5], period=0, display=True):
    """
    df is table_patients_mca
    df_label is df label

    This function plots the patients into the plan FS_id1, FS_id2 (factors resulting of the MCA)

    df_label is a serie which references the label (1=not at risk, 3=risk, 5=psychose) for each patient

    period (int) refers to the time of the diagnosis. 0 means for t0, 1 means 6 months, etc. It is linked to the index of
    the columns in the df_label_time_clear

    period (int, between 0 and 4) is the number of the consultation (0 = T0 ie the first label, 4=T(24mois) ie label_24mois )

    """
    data_ = []  # data for the plot

    fs = 'Factor'
    df_label_color = apply_color_label(df_label.iloc[:, period])
    df_label_copy = pd.DataFrame(df_label_color.iloc[:, period])

    df_color = pd.DataFrame(df_label_color.loc[:, 'color' + str(period)])

    # df_label_copy = pd.concat([df_label_copy, df_color])
    df_label_copy = df_label_copy.dropna()
    df_color = df_color.dropna()

    points_x = df.loc[(fs, fs_id1)].values
    points_y = df.loc[(fs, fs_id2)].values
    labels = df.columns.values  # index of patient (1,2,3,...)

    coordinates_max = max(max(abs(df.loc[(fs, fs_id1)].values)), max(abs(df.loc[(fs, fs_id2)].values)))
    for i in df_label_copy.index:

        if df_label_copy.loc[i,][0] in class_patient or str(
                int(df_label_copy.loc[i][0])) in class_patient:  # in order to select just the patient in a class

            trace = go.Scatter(x=[points_x[i]], y=[points_y[i]], hovertext=str(labels[i]),
                               mode='markers', name='patient {}'.format(i),
                               marker=dict(size=10, color=df_color.loc[i, 'color' + str(period)]))
            data_.append(trace)

    layout = go.Layout(
        title="Coordonnées des patients projetées dans le plan des facteurs " + str(fs_id1) + ' et ' + str(fs_id2),
        xaxis={"title": "facteur" + str(fs_id1), "range": [-coordinates_max - 0.1, coordinates_max + 0.1]},
        yaxis={"title": "facteur" + str(fs_id2), "range": [-coordinates_max - 0.1, coordinates_max + 0.1]})

    fig = go.Figure(data=data_, layout=layout)

    offline.plot(fig, filename='Images/Patients dans le plan des facteurs scores.html',
                 # to save the figure in the repertory
                 auto_open=False)

    if display:
        offline.iplot(fig)
        return None
    else:
        return data_, layout
Exemplo n.º 4
0
def gethtmlanalysis(category):

    #category=raw_input()

    df = pd.read_csv(os.path.join(settings.BASE_DIR, "muse/musedb.csv"),
                     sep=',',
                     error_bad_lines=False,
                     warn_bad_lines=False)

    df_cat = df[df["CATEGORY_DESC"] == category]

    del df

    sub = df_cat["SUBCATEGORY_DESC"].value_counts()
    l = sub.sum()
    reg = pd.DataFrame({
        'labels': sub.index.tolist(),
        'values': sub.values.tolist()
    })
    sub = sub[sub > (l * 0.008)]
    trace = go.Pie(labels=sub.index.tolist(), values=sub.values.tolist())
    data = go.Data([trace])
    layout = go.Layout(title="Top subcategories of  " + category +
                       " with total number of items sold")

    figure = go.Figure(data=data, layout=layout)

    offline.plot(figure,
                 filename=os.path.join(settings.BASE_DIR,
                                       "templates/tpl/cat-subcat.html"),
                 auto_open=False)

    brand = df_cat["BRAND_DESC"].value_counts()
    l = brand.sum()
    reg = pd.DataFrame({
        'labels': brand.index.tolist(),
        'values': brand.values.tolist()
    })
    brand = brand[brand > (l * 0.008)]
    trace = go.Pie(labels=brand.index.tolist(), values=brand.values.tolist())
    data = go.Data([trace])
    layout = go.Layout(title="Top Brands of  " + category +
                       " with total number of items sold")

    figure = go.Figure(data=data, layout=layout)

    offline.plot(figure,
                 filename=os.path.join(settings.BASE_DIR,
                                       "templates/tpl/cat-brand.html"),
                 auto_open=False)
Exemplo n.º 5
0
def graphNewcomers(repoPath, newcomers):
    # Sometimes we get bad data?
    newcomers = [x for x in newcomers if len(x.split('\t')) >= 4]

    issue = [x for x in newcomers if x.split('\t')[2].startswith('issue')]
    comment = [x for x in newcomers if x.split('\t')[2].startswith('comment')]
    pull = [x for x in newcomers if jsonIsPullRequest(x.split('\t')[2])]
    commentPR = [
        x for x in newcomers if jsonIsPullRequestComment(x.split('\t')[2])
    ]
    # For each line, pull out the filename (third item)
    data = [
        Bar(
            x=[
                'Opened an issue',
                'Commented on an issue<BR>opened by someone else',
                'Opened a pull request',
                'Commented on a pull request<BR>opened by someone else'
            ],
            y=[len(issue), len(comment),
               len(pull), len(commentPR)],
        )
    ]
    layout = Layout(title='First contribution types for<BR>' + repoPath, )
    fig = Figure(data=data, layout=layout)
    return offline.plot(fig,
                        show_link=False,
                        include_plotlyjs=False,
                        output_type='div')
Exemplo n.º 6
0
	def getHTML(self,params):
		df = self.getData(params)  # get data
		## create plotly chart data
		data = [
			go.Scatter(
				x=df['Date'],
				y=df['close'],
				name='Close'
				),
			go.Scatter(
				x=df['Date'],
				y=df['high'],
				name='High'
				),
			go.Scatter(
				x=df['Date'],
				y=df['low'],
				name='Low'
				)
			]
		## create plotly layout
		layout = go.Layout(
			autosize=False,
			width=700, height=500,
			title='{} Price'.format(self.company_name)
			)
		## create the figure
		fig = go.Figure(data=data, layout=layout)
		## generate the html
		html = offline.plot(fig, show_link=False, output_type='div', include_plotlyjs=False)
		return html
def plot_vals(objs, unc, preference, iteration, interaction_count, ideal,
              nadir, min, max):
    objs_orig = objs
    columns = ["f_" + str(i + 1) for i in range(np.shape(objs)[1])]
    range_plot = np.vstack((ideal, nadir))
    range_plot = np.hstack((range_plot, [[3], [3]]))
    if np.shape(objs)[0] > 0:
        unc_avg = np.mean(unc, axis=1)
        #unc_avg = (unc_avg-np.min(unc_avg))/(np.max(unc_avg)-np.min(unc_avg))
        unc_avg = (unc_avg - min) / (max - min)
        objs_col = unc_avg.reshape(-1, 1)
        objs = np.hstack((objs, objs_col))
    objs = np.vstack((objs, range_plot))
    objs = pd.DataFrame(objs, columns=columns + ["color"])
    if preference is not None:
        pref = pd.DataFrame(np.hstack((preference.reshape(1, -1), [[2]])),
                            columns=columns + ["color"])
        data_final = pd.concat([objs, pref])
    else:
        data_final = objs
    #color_scale_custom= [(0.0,'rgb(0,0,0)'),(0.5,'rgb(160,90,0)'),(0.5,'green'),(0.75,'green'),(0.75,'blue'),(1.0,'blue')]
    #color_scale_custom = [(0.0, 'rgb(36,86,104)'), (0.5, 'rgb(237,239,93)'), (0.5, 'red'), (0.75, 'red'), (0.75, 'lightgray'),
    #                      (1.0, 'lightgray')]
#    color_scale_custom = [(0.0, 'rgb(69,2,86)'), (0.5, 'rgb(249,231,33)'), (0.5, 'red'), (0.75, 'red'), (0.75, 'white'),
#                          (1.0, 'white')]
#    color_scale_custom = [(0.0, 'rgb(69,2,86)'), (0.125, 'rgb(59,28,140)'), (0.25, 'rgb(33,144,141)'),
#                          (0.375, 'rgb(90,200,101)'), (0.5, 'rgb(249,231,33)'),
#                          (0.5, 'red'), (0.75, 'red'), (0.75, 'white'),
#                          (1.0, 'white')]
    color_scale_custom = [(0.0, 'rgb(69,2,86)'), (0.083, 'rgb(59,28,140)'),
                          (0.167, 'rgb(33,144,141)'),
                          (0.25, 'rgb(90,200,101)'),
                          (0.334, 'rgb(249,231,33)'), (0.334, 'red'),
                          (0.7, 'red'), (0.7, 'white'), (1.0, 'white')]
    #color_scale_custom = ['Inferno', (1.5, 'green'), (2.5, 'green'), (2.5, 'white'),
    #                      (3.5, 'white')]
    #color_scale_custom = [(0.0,'red'),(1.5,'red'),(1.5, 'green'), (2.5, 'green'), (2.5, 'white'),
    #                      (3.5, 'white')]
    fig = ex.parallel_coordinates(data_final,
                                  dimensions=columns,
                                  color="color",
                                  color_continuous_scale=color_scale_custom,
                                  range_color=(0, 3))
    plot(fig,
         filename="testfile_" + str(iteration) + "_" + str(interaction_count) +
         ".html")
    print('Plotting done!!')
Exemplo n.º 8
0
def plot(trace1, trace2, annotations1 = None, annotations2 = None, plot_name = 'plot'):
    """Creates two 3D scatter plots"""
    if not annotations1:
        annotations1 = []
    if not annotations2:
        annotations2 = []
    fig = tools.make_subplots(rows=1, cols=2, specs=[[{'is_3d': True}, {'is_3d': True}]], print_grid=False,
                              horizontal_spacing=0.01, )
    fig.append_trace(trace1[0], 1, 1)
    fig.append_trace(trace1[1], 1, 1)
    fig.append_trace(trace2[0], 1, 2)
    fig.append_trace(trace2[1], 1, 2)
    axis_params = dict(showgrid=False, showticklabels=False, zeroline=False, titlefont=dict(color='white'))
    fig['layout'].update(showlegend=False, margin=dict(r=0, l=0, b=0, t=0))
    fig['layout']['scene1'].update(dict(xaxis=axis_params, yaxis=axis_params, zaxis=axis_params,
                                        annotations=annotations1))
    fig['layout']['scene2'].update(dict(xaxis=axis_params, yaxis=axis_params, zaxis=axis_params,
                                        annotations=annotations2))
    offline.plot(fig, show_link=False, filename=plot_name + '.html')
def plot_graph(jobs):

    # iterate over all jobs and creater scatters
    results = {}
    lines = []
    for job in jobs.keys():

        critical = []
        names = []
        for run in sorted(jobs[job]):

            if CRITICAL in jobs[job][run] and ALL_TESTS in jobs[job][run]:
                critical.append(jobs[job][run][CRITICAL][PERCENT])
                names.append("%s/%s" % (job, run))
        results[job] = {}
        results[job]['percent'] = critical
        results[job]['names'] = names

    # iterate and get greater
    size = 0
    for job in results:
        if size < len(results[job]['percent']):
            size = len(results[job]['percent'])

    # create lines
    for job in results:
        lines.append(
            go.Scatter(x=range(size - len(results[job]['percent']), size),
                       y=results[job]['percent'],
                       text=results[job]['names'],
                       name='%s - %s' % (job, CRITICAL),
                       mode="lines+markers"))

    data = go.Data(lines)
    layout = go.Layout(title="Regression Status",
                       xaxis={'title': 'Last Builds'},
                       yaxis={
                           'title': 'Percent',
                           'range': [0, 100]
                       })
    figure = go.Figure(data=data, layout=layout)
    plot(figure, filename='test.html', auto_open=True)
def visualize(max_x, max_y, barren_land_dicts):

    trace0 = go.Scatter(
        x=[1.5, 4.5],
        y=[0.75, 0.75],
    )
    data = [trace0]
    layout = {
        'xaxis': {
            'range': [0, max_x],
            'showgrid': True,
            'fixedrange': True
        },
        'yaxis': {
            'range': [0, max_y],
            'fixedrange': True
        },
        'shapes': barren_land_dicts
    }
    fig = go.Figure(data=data, layout=layout)
    py.plot(fig, filename='barren_plot.html', auto_open=True)
Exemplo n.º 11
0
def graphRampTime(deltas, nocontribs, graphtitle, xtitle, filename):
    data = [Histogram(x=deltas)]
    layout = Layout(
        title=graphtitle,
        yaxis=dict(title='Number of contributors'),
        xaxis=dict(title= xtitle +
                   '<br>Mean: ' + '{:.2f}'.format(statistics.mean(deltas)) + ' days, ' +
                   'Median: ' + '{:.2f}'.format(statistics.median(deltas)) + ' days' +
                   '<br>Number of contributors who did this: ' +
                   '{:,g}'.format(len(deltas)) +
                   '<br>Percentage of contributors who did this: ' +
                   '{:.2f}'.format(len(deltas)/(len(deltas)+len(nocontribs))*100) + '%')
    )
    fig = Figure(data=data, layout=layout)
    return offline.plot(fig, show_link=False, include_plotlyjs=False, output_type='div')
Exemplo n.º 12
0
def show_box_plot(city_name, col, Title):
    fig = tls.make_subplots(rows=1, cols=1)
    for city in city_name:
        for each in col:
            plot = go.Box(x=dataset[dataset['city'] == city]['city'],
                          y=dataset[dataset['city'] == city][each],
                          showlegend=True,
                          name=each + ' --- ' + city)

            fig.append_trace(plot, 1, 1)
    fig['layout'].update(title=Title)
    div = offline.plot(fig,
                       show_link=False,
                       output_type="div",
                       include_plotlyjs=False)

    return div
Exemplo n.º 13
0
def show_bar_plot(city_name, col, Title):
    fig = tls.make_subplots(rows=1, cols=1)
    for city in city_name:
        for each in col:
            plot = go.Bar(x=dataset[dataset['city'] == city]['city'],
                          y=dataset[dataset['city'] == city][each],
                          showlegend=True,
                          name=each + ' --- ' + city)

            fig.append_trace(plot, 1, 1)
    fig['layout'].update(title=Title)
    div = offline.plot(fig,
                       show_link=False,
                       output_type="div",
                       include_plotlyjs=False)

    return '<script src="https://cdn.plot.ly/plotly-1.2.0.min.js"></script>' + div
Exemplo n.º 14
0
def graphRampTime(deltas, nocontribs, graphtitle, xtitle, filename):
    data = [Histogram(x=deltas)]
    title = "<br><i>Not enough data available.</i>"
    if deltas:
        title = (
            '<br>Mean: {:.2f} days, Median: {:.2f} days'
            '<br>Number of contributors who did this: {:,g}'
            '<br>Percentage of contributors who did this: {:.2f}%').format(
                statistics.mean(deltas), statistics.median(deltas),
                len(deltas),
                len(deltas) / (len(deltas) + len(nocontribs)) * 100)
    layout = Layout(title=graphtitle,
                    yaxis=dict(title='Number of contributors'),
                    xaxis=dict(title=xtitle + title))
    fig = Figure(data=data, layout=layout)
    return offline.plot(fig,
                        show_link=False,
                        include_plotlyjs=False,
                        output_type='div')
Exemplo n.º 15
0
 def getHTML(self, params):
     df = self.getData(params)  # get data
     ## create plotly chart data
     data = [
         go.Scatter(x=df['Date'], y=df['close'], name='Close'),
         go.Scatter(x=df['Date'], y=df['high'], name='High'),
         go.Scatter(x=df['Date'], y=df['low'], name='Low')
     ]
     ## create plotly layout
     layout = go.Layout(autosize=False,
                        width=700,
                        height=500,
                        title='{} Price'.format(self.company_name))
     ## create the figure
     fig = go.Figure(data=data, layout=layout)
     ## generate the html
     html = offline.plot(fig,
                         show_link=False,
                         output_type='div',
                         include_plotlyjs=False)
     return html
Exemplo n.º 16
0
def show_city_frequency(number_of_city=10):
    plot_1 = go.Histogram(x=dataset[dataset.city.isin(
        city_count[:number_of_city].index.values)]['city'],
                          showlegend=False)

    ## Creating the grid for the above plot
    fig = tls.make_subplots(rows=1, cols=1)

    ## appending the plot on to the grid
    fig.append_trace(plot_1, 1, 1)

    ## displaying the figure
    fig['layout'].update(showlegend=True,
                         title="Frequency of cities in the dataset ")

    div = offline.plot(fig,
                       show_link=False,
                       output_type="div",
                       include_plotlyjs=False)

    return '<script src="https://cdn.plot.ly/plotly-1.2.0.min.js"></script>' + div
Exemplo n.º 17
0
def graphMergeDelay(coords):
    # Calculate, for each month, the average "age" of pull requests
    # (the average amount of time a pull request is open before being merged).
    # Discard all PRs opened after the end of the month
    # discard all PRs closed before the beginning of the month
    # in order to only look at PRs closed this month.
    #
    #       Jan                                 Feb
    # |---------1--------|
    #           |-------2---------|
    #               |---2---|
    #                           |-----------3------------|
    #
    # 1. If a pull request was opened before this month and closed this month,
    #    count the length of time from when it was opened to when it was closed.
    # 2. If a pull request was opened and closed in this month,
    #    count the length of time from when it was opened to when it was closed.
    # 3. If a pull request was opened this month but was not closed this month,
    #    count the length of time from when it was opened to the end of this month.
    #
    # (So essentially, from when it was open to when it was closed or EOM,
    # whichever is sooner)

    beg = coords[0][0]
    end = coords[-1][0]
    means = []
    bom = datetime(beg.year, beg.month, 1)
    while True:
        if bom.month + 1 <= 12:
            eom = datetime(bom.year, bom.month + 1, bom.day)
        else:
            eom = datetime(bom.year + 1, 1, bom.day)
        openpr = [(x, min(y, eom)) for (x, y) in coords if x < eom and y > bom]
        if not openpr:
            means.append((eom, 0))
            bom = eom
            continue
        lengths = [(y - x).total_seconds() / (60 * 60 * 24.)
                   for (x, y) in openpr]
        means.append((eom, numpy.average(lengths)))
        bom = eom
        if eom > end:
            break

    # Scatter chart - x is creation date, y is number of days open
    data = [
        Scatter(x=[x for (x, y) in coords],
                y=[(y - x).total_seconds() / (60 * 60 * 24.)
                   for (x, y) in coords],
                mode='markers',
                name='Pull requests<BR>by creation date'),
        Scatter(x=[x for (x, y) in means],
                y=[y for (x, y) in means],
                name='Average time open'),
    ]
    layout = Layout(
        title='Number of days a pull request is open',
        yaxis=dict(title='Number of days open'),
        xaxis=dict(title='Pull request creation date'),
    )
    fig = Figure(data=data, layout=layout)
    return offline.plot(fig,
                        show_link=False,
                        include_plotlyjs=False,
                        output_type='div')
Exemplo n.º 18
0
def stock_detail(request, s):  # 주식 그래프 생성
    code_df = pd.read_html(
        'http://kind.krx.co.kr/corpgeneral/corpList.do?method=download&searchType=13',
        header=0)[0]

    # 타입을 확인

    print(type(code_df))  # <class 'pandas.core.frame.DataFrame'>
    print(code_df.head())  # 데이터를 확인

    # code_df에 있는 '종목코드' 컬럼을 0을 채운 6자리 포멧으로 맞춰준다.

    code_df.종목코드 = code_df.종목코드.map('{:06d}'.format)

    # code_df를 회사명과 종목코드 컬럼만 뽑아낸다.

    #    ***참고*** pandas에서 컬럼을 선택 할 때

    #                   단일개 선택: df['컬럼명']   or   df.컬럼명

    #                   여러개 선택: df[['컬럼명', ... ,'컬럼명']]

    code_df = code_df[['회사명', '종목코드']]

    excel = code_df[[
        '회사명',
        '종목코드',
    ]]

    print(code_df)  # 데이터를 확인

    print('----------배열만들기-----------')
    code_df.to_csv("test.csv", encoding="UTF-8")  #csv로 저장해놓음
    first_test = code_df[['회사명', '종목코드']]  #호사명과 종목코드가 들어간것

    # 한글로된 컬럼명을 영어로 바꿔준다.

    code_df = code_df.rename(columns={'회사명': 'name', '종목코드': 'code'})

    def get_url(s, code_df):  #s 파라미터로, 데이터 프레임 반환

        # 코드를 가져오기 위한 처리.

        # 먼저 .query("name=='{}'".format(item_name))['code']는 name 컬럼에 item_name과 동일한 값의 code값을 반환한다는 뜻.

        # 즉, .query("쿼리".format(쿼리에 넣을 데이터))[얻을 자료]

        # .to_string(index = False)로 위에서 얻어진 값에 index를 빼고 string타입으로 바꿔준다.

        code = (code_df.query(
            "name=='{}'".format(s))['code'].to_string(index=False)).strip()

        # url은 일일 종가 시가 고가 저가 거래량을 보여주는 표이다.

        url = 'http://finance.naver.com/item/sise_day.nhn?code={code}'.format(
            code=code)

        print("요청 URL={}".format(url))

        return url

    print('----------------------------')

    # 회사명 입력 -> 그 회사 주식 가져옴.

    item_name = s

    url = get_url(item_name, code_df)

    df = pd.DataFrame()

    # 크롤링. 페이지 20까지 크롤링을 한다. 1->20 페이지 생성

    for page in range(1, 21):

        # 위에서 얻은 url에 page를 붙여줘서 url 포멧을 만들어준다.

        pg_url = '{url}&page={page}'.format(url=url, page=page)

        # pandas의 df에 위에서 얻은 url을 넣어줘서 우리가 구하고자 하는 데이터프레임을 만든다.

        # 데이터프레임을 만들 때 리스트에 [0]을 붙여줘서 만들 수 있음을 다시 확인.

        df = df.append(pd.read_html(pg_url, header=0)[0], ignore_index=True)

    # df.dropna()를 이용해 결측값(NaN) 있는 행을 제거한다.

    df = df.dropna()

    # 상위 5개 데이터 확인하기

    print(df.head())
    print("---------------head 이후 경계선------------")

    # 한글로 된 컬럼명을 영어로 바꿔준다.

    df = df.rename(
        columns={
            '날짜': 'date',
            '종가': 'close',
            '전일비': 'diff',
            '시가': 'open',
            '고가': 'high',
            '저가': 'low',
            '거래량': 'volume'
        })

    # 데이터의 타입을 int형으로 바꿔줌. \(역슬래쉬)는 뒤에 데이터가 이어진다는 의미이다. 한줄로 쓰면 \ 필요없음.

    df[['close', 'diff', 'open', 'high', 'low', 'volume']]\
        = df[['close', 'diff', 'open', 'high', 'low', 'volume']].astype(int)

    # 컬럼명 'date'의 타입을 date로 바꿔줌

    df['date'] = pd.to_datetime(df['date'])

    #  일자(date)를 기준으로 오름차순 정렬

    df = df.sort_values(by=['date'], ascending=False)

    print('---최근날짜것만 가져옴----')
    myDate = df.iloc[0]
    print(myDate)
    print(myDate.close)

    # 상위 5개 데이터 확인

    print(df.head())

    # plotly 라이브러리 사용법은 ' Python ▒ plotly 라이브러리 ' 참고

    trace = go.Scatter(x=df.date, y=df.close, name=item_name)
    data = [trace]

    # 그래프의 레이아웃을 설정한다. - 딕셔너리 타입으로 작성
    layout = dict(
        title='{}의 종가 시간 흐름'.format(item_name),
        xaxis=dict(rangeselector=dict(buttons=list([
            dict(count=1, label='1m', step='month', stepmode='backward'),
            dict(count=3, label='3m', step='month', stepmode='backward'),
            dict(count=6, label='6m', step='month', stepmode='backward'),
            dict(step='all')
        ])),
                   rangeslider=dict(),
                   type='date'))

    fig = go.Figure(data=data, layout=layout)
    # stock_img = offline.plot(fig, filename="C:/Users/Administrator/PycharmProjects/Crawling/img/inqury.html")
    stock_img = offline.plot(
        fig, filename="E:/ksy/python_project/bankproject/static/inqury.html")

    return redirect('stock:company_list2')
Exemplo n.º 19
0
def graphSentiment(repoPath, debug):
    sentimentDict = createSentimentDict(repoPath)
    commentSentiment = createSentimentCounts(sentimentDict)
    combinedIssueSentiment = createIssueSentiment(commentSentiment)

    if debug:
        print('Have', len(commentSentiment), 'sentiment json files')
    jsonDict = createJsonDict(repoPath, combinedIssueSentiment.keys(), True)

    # List: [date, issue path (for now), (combinedIssueSentiment 5 tuple)]
    coords = []
    for key, value in combinedIssueSentiment.items():
        try:
            path = os.path.join(repoPath, key, key + '.json')
            with open(path) as issueFile:
                issueJson = json.load(issueFile)
            url = issueJson['html_url']
            coords.append((jsonDict[path][0], key, value, url))
        except:
            key2 = os.path.join(repoPath, key, key + '.json')
            if debug:
                print(key, 'IS in combinedIssueSentiment dict')
                print(key2, 'NOT in jsonDict')
                if key2 not in sentimentDict.keys():
                    print(key2, 'NOT in sentimentDict')
                else:
                    print(key2, 'IS in sentimentDict')
                if key2 not in commentSentiment.keys():
                    print(key2, 'NOT in commentSentiment dict')
                else:
                    print(key2, 'IS in commentSentiment dict')
            #print(key, value, key.split(os.sep))
            pass
    if debug:
        print('coords len:', len(coords), 'number issues:',
              len(combinedIssueSentiment))

    coords = sorted(coords, key=lambda tup: tup[1])
    # Multiplier - what is the magnitude of positive comments you would have to receive vs negative comments
    # to have this issue "feel" positive?
    feelsMultipler = 2
    posCoords = [(date, issue, sentiment, url)
                 for (date, issue, sentiment, url) in coords
                 if (sentiment[4] * 2 + sentiment[3]) > feelsMultipler *
                 (sentiment[0] * 2 + sentiment[1])]
    negCoords = [(date, issue, sentiment, url)
                 for (date, issue, sentiment, url) in coords
                 if (sentiment[0] * 2 + sentiment[1]) > feelsMultipler *
                 (sentiment[4] * 2 + sentiment[3])]
    # Issues can have a lot of neutral comments (debate on code) and still "feel" negative or mixed.
    # If more than 20% of the comments are positive or neutral, it's a mixed thread.
    mixedPercent = .20
    neutralCoords = [(date, issue, sentiment, url) for (
        date, issue, sentiment, url
    ) in coords if (sentiment[2] > 0) and (mixedPercent > (
        (sentiment[0] * 2 + sentiment[1] + sentiment[3] + sentiment[4] * 2) /
        (sentiment[2]))) and (
            (date, issue, sentiment, url) not in posCoords) and (
                (date, issue, sentiment, url) not in negCoords)]
    mixedCoords = [
        (date, issue, sentiment, url)
        for (date, issue, sentiment, url) in coords
        if ((date, issue, sentiment, url) not in neutralCoords) and (
            (date, issue, sentiment, url) not in posCoords) and (
                (date, issue, sentiment, url) not in negCoords)
    ]
    sentCoords = [
        ('Neutral', 'rgba(0, 0, 0, .8)', neutralCoords),
        ('Positive', 'rgba(21, 209, 219, .8)', posCoords),
        ('Negative', 'rgba(250, 120, 80, .8)', negCoords),
        ('Mixed', 'rgba(130, 20, 160, .8)', mixedCoords),
    ]

    data = []
    for s in sentCoords:
        data.append(
            Scatter(
                x=[date for (date, issue, sentiment, url) in s[2]],
                y=[sentiment[2] for (date, issue, sentiment, url) in s[2]],
                error_y=dict(
                    type='data',
                    symmetric=False,
                    array=[
                        sentiment[3] + sentiment[4] * 2
                        for (date, issue, sentiment, url) in s[2]
                    ],
                    arrayminus=[
                        sentiment[1] + sentiment[0] * 2
                        for (date, issue, sentiment, url) in s[2]
                    ],
                    color=s[1],
                ),
                mode='markers',
                text=[url for (date, issue, sentiment, url) in s[2]],
                name=s[0] + ' community sentiment',
                marker=dict(color=s[1]),
            ))
    layout = Layout(
        title='Community sentiment',
        yaxis=dict(
            title='Number of + positive | neutral | - negative comments'),
        xaxis=dict(title='Issue or PR creation date'),
    )
    fig = Figure(data=data, layout=layout)
    return offline.plot(fig,
                        show_link=False,
                        auto_open=False,
                        include_plotlyjs=False,
                        output_type='div')
Exemplo n.º 20
0
def plot_map_station_with_plotly(station_control,
                                 station_id=None,
                                 width=800,
                                 height=600,
                                 offline_plot=False,
                                 return_plot=False,):
    """
    Affiche une cartographie de l'agglomération de Bordeaux avec toutes les stations Vcub et leurs états
    provenant des algorithmes (normal, inactive et anomaly).

    Si "station_id" est indiqué, alors la cartographie est focus sur la lat / lon de station (Numéro)

    Parameters
    ----------
    data : pd.DataFrame
        En provenance de station_control.csv (vcub_watcher)
    station_id : Int [opt]
        Numéro de station que l'on souhaite voir (en focus) sur la cartographie.
    width : int [opt]
        Largeur du graphique (en px).
    height : int [opt]
        Longeur du graphique (en px).
    offline_plot : bool [opt]
        Pour retourner le graphique et l'utilisé dans une application
    return_plot : bool [opt]
        Retourne le graphique pour être utilisé par le front.

    Returns
    -------
    None

    Examples
    --------

    plot_map_station_with_plotly(station_control=station_control, offline_plot=False)
    """
    # Param plot with a given station_id
    if station_id is not None:
        # On centre le graphique sur la lat / lon de la station
        center_lat = \
            station_control[station_control['station_id'] == station_id]['lat'].values[0]
        center_lon = \
            station_control[station_control['station_id'] == station_id]['lon'].values[0]
        zoom_plot = 15
    else:
        center_lat = 44.837794
        center_lon = -0.581662
        zoom_plot = 11

    # Preprocess avant graphique
    station_control['etat'] = 'normal'

    # Non monitoré
    station_control.loc[station_control['mean_activity'] < THRESHOLD_PROFILE_STATION,
                        'etat'] = 'non surveillée'

    # En anoamlie (HS prediction)
    station_control.loc[station_control['is_anomaly'] == 1, 'etat'] = 'anomaly'

    # Inactive
    station_control.loc[station_control['is_inactive'] == 1, 'etat'] = 'inactive'

    # Transform date to string
    try:
        station_control['anomaly_since_str'] = \
            station_control['anomaly_since'].dt.strftime(date_format='%Y-%m-%d %H:%M')
    except KeyError:
        # Pour vcub_watcher intégration
        # https://github.com/armgilles/vcub_keeper/issues/49#issuecomment-822504771
        station_control['anomaly_since_str'] = \
            station_control['En anomalie depuis'].dt.strftime(date_format='%Y-%m-%d %H:%M')

    station_control['anomaly_since_str'] = station_control['anomaly_since_str'].fillna('-')

    # Color for etat
    color_etat = {'anomaly': '#EB4D50',
                  'inactive': '#5E9BE6',
                  'normal': '#6DDE75',
                  'non surveillée': '#696A6A'}

    # To know when use add_trace after init fig
    wtf_compteur = 0

    for etat in station_control['etat'].unique():
        # Filter
        temp = station_control[station_control['etat'] == etat]

        # Building text
        texts = []
        for idx, station in temp.iterrows():
            text = str(station['NOM']) \
                + " <br />" + "station N° : " + str(station['station_id']) \
                + " <br />" + "Nombre de vélo dispo : " + str(station['available_bikes']) \
                + " <br />" + "Activité suspecte depuis : " + str(station['anomaly_since_str'])
            texts.append(text)

        if wtf_compteur == 0:
            fig = go.Figure(go.Scattermapbox(lat=temp['lat'],
                                             lon=temp['lon'],
                                             mode='markers',
                                             hoverinfo='text',
                                             hovertext=texts,
                                             marker_size=9,
                                             marker_color=color_etat[etat],
                                             name=etat))
        else:
            fig.add_trace(go.Scattermapbox(lat=temp['lat'],
                                           lon=temp['lon'],
                                           mode='markers',
                                           hoverinfo='text',
                                           hovertext=texts,
                                           marker_size=9,
                                           marker_color=color_etat[etat],
                                           name=etat))

        wtf_compteur = 1

    fig.update_layout(mapbox=dict(center=dict(lat=center_lat, lon=center_lon),
                                  accesstoken=MAPBOX_TOKEN,
                                  zoom=zoom_plot,
                                  style="light"),
                      showlegend=True,
                      width=width,
                      height=height,
                      legend=dict(orientation="h",
                                  yanchor="top",
                                  xanchor="center",
                                  y=1.1,
                                  x=0.5
                                  ))
    # To get map on Front
    if return_plot is True:
        return fig

    if offline_plot is False:
        iplot(fig)
    else:
        offline.plot(fig)
def animate_2d_init_(data: Union[np.ndarray, pd.DataFrame, list],
                     filename: str) -> dict:
    """Initiate a 2D scatter animation.

    Only for 2D data.

    Parameters
    ----------
    data : Union[np.ndarray, pd.DataFrame, list]
        Objective values
    filename : str
        Name of the file to which plot is saved

    Returns
    -------
    dict
        Plotly Figure Object
    """

    figure = {"data": [], "layout": {}, "frames": []}
    figure["layout"]["xaxis"] = {"autorange": True}
    figure["layout"]["yaxis"] = {"autorange": True}
    figure["layout"]["hovermode"] = "closest"
    figure["layout"]["sliders"] = {
        "args": ["transition", {
            "duration": 400,
            "easing": "cubic-in-out"
        }],
        "initialValue": "1952",
        "plotlycommand": "animate",
        "visible": True,
    }
    sliders_dict = {
        "active": 0,
        "yanchor": "top",
        "xanchor": "left",
        "currentvalue": {
            "font": {
                "size": 20
            },
            "prefix": "Iteration:",
            "visible": True,
            "xanchor": "right",
        },
        "transition": {
            "duration": 300,
            "easing": "cubic-in-out"
        },
        "pad": {
            "b": 10,
            "t": 50
        },
        "len": 0.9,
        "x": 0.1,
        "y": 0,
        "steps": [],
    }
    figure["layout"]["sliders"] = [sliders_dict]
    data_dict = {
        "x": list(data[:, 0]),
        "y": list(data[:, 1]),
        "mode": "markers",
        "marker": {
            "size": 5,
            "color": "rgba(255, 182, 193, .9)",
            "line": dict(width=2),
        },
    }
    figure["data"].append(data_dict)
    plot(figure, filename=filename)
    animate_2d_next_(data, figure, filename, 0)
    return figure
def animate_parallel_coords_next_(
    data: Union[np.ndarray, pd.DataFrame, list],
    figure: dict,
    filename: str,
    generation: int,
) -> dict:
    """Plot the next set of individuals in an animation.

    Plots parallel coordinate plot for 4D and up.

    Parameters
    ----------
    data : Union[np.ndarray, pd.DataFrame, list]
        The objective values to be plotted
    figure : dict
        Plotly figure object compatible dict
    filename : str
        Name of the file to which the plot is saved
    generation : int
        Iteration Number

    Returns
    -------
    dict
        Plotly Figure Object
    """
    frame = {"data": [], "name": str(generation)}
    objectives = pd.DataFrame(data)
    sliders_dict = figure["layout"]["sliders"][0]
    dimensions = [
        dict(
            range=[min(objectives[column]),
                   max(objectives[column])],
            label="f" + str(column),
            values=objectives[column],
        ) for column in objectives
    ]
    line = dict(
        color=objectives[0],
        colorscale="Viridis",
        showscale=True,
        cmin=min(objectives[objectives.columns[0]]),
        cmax=max(objectives[objectives.columns[0]]),
    )
    data_dict = go.Parcoords(line=line, dimensions=dimensions)
    frame["data"].append(data_dict)
    figure["frames"].append(frame)
    slider_step = {
        "args": [
            [generation],
            {
                "frame": {
                    "duration": 300,
                    "redraw": True
                },
                "mode": "immediate",
                "transition": {
                    "duration": 300
                },
            },
        ],
        "label":
        generation,
        "method":
        "animate",
    }
    sliders_dict["steps"].append(slider_step)
    figure["layout"]["sliders"] = [sliders_dict]
    plot(figure, auto_open=False, filename=filename)
    return figure
def animate_parallel_coords_init_(data: Union[np.ndarray, pd.DataFrame, list],
                                  filename: str) -> dict:
    """Plot the first (or zeroth) iteration of a population.

    Intended as a frames object. Plots parallel coordinate plot for >3D data.

    Parameters
    ----------
    data : Union[np.ndarray, pd.DataFrame, list]
        Contains the data to be plotted. Each row is an individual's objective values.
    filename : str
        Contains the name of the file to which the plot is saved.

    Returns
    -------
    dict
        Plotly figure object
    """
    figure = {"data": [], "layout": {}, "frames": []}
    objectives = pd.DataFrame(data)
    figure["layout"]["hovermode"] = "closest"
    figure["layout"]["sliders"] = {
        "args": ["transition", {
            "duration": 400,
            "easing": "cubic-in-out"
        }],
        "initialValue": "1952",
        "plotlycommand": "animate",
        "visible": True,
    }
    sliders_dict = {
        "active": 0,
        "yanchor": "top",
        "xanchor": "left",
        "currentvalue": {
            "font": {
                "size": 20
            },
            "prefix": "Iteration:",
            "visible": True,
            "xanchor": "right",
        },
        "transition": {
            "duration": 300,
            "easing": "cubic-in-out"
        },
        "pad": {
            "b": 10,
            "t": 50
        },
        "len": 0.9,
        "x": 0.1,
        "y": 0,
        "steps": [],
    }
    figure["layout"]["sliders"] = [sliders_dict]
    dimensions = [
        dict(
            range=[min(objectives[column]),
                   max(objectives[column])],
            label="f" + str(column),
            values=objectives[column],
        ) for column in objectives
    ]
    line = dict(
        color=objectives[0],
        colorscale="Viridis",
        showscale=True,
        cmin=min(objectives[objectives.columns[0]]),
        cmax=max(objectives[objectives.columns[0]]),
    )
    data_dict = go.Parcoords(line=line, dimensions=dimensions)
    figure["data"].append(data_dict)
    plot(figure, filename=filename)
    animate_parallel_coords_next_(data, figure, filename, 0)
    return figure
def animate_3d_next_(
    data: Union[np.ndarray, pd.DataFrame, list],
    figure: dict,
    filename: str,
    generation: int,
) -> dict:
    """Plot the next set of individuals in an animation.

    Plots scatter for 3D data.

    Parameters
    ----------
    data : Union[np.ndarray, pd.DataFrame, list]
        The objective values to be plotted
    figure : dict
        Plotly figure object compatible dict
    filename : str
        Name of the file to which the plot is saved
    generation : int
        Iteration Number

    Returns
    -------
    dict
        Plotly Figure Object
    """
    frame = {"data": [], "name": str(generation)}
    sliders_dict = figure["layout"]["sliders"][0]
    data_dict = go.Scatter3d(
        x=list(data[:, 0]),
        y=list(data[:, 1]),
        z=list(data[:, 2]),
        mode="markers",
        marker=dict(
            size=8,
            color=data[:, 2],
            colorscale="Viridis",
            opacity=0.5,
            line=dict(width=2, color="black", colorscale="Viridis"),
        ),
    )
    frame["data"].append(data_dict)
    figure["frames"].append(frame)
    slider_step = {
        "args": [
            [generation],
            {
                "frame": {
                    "duration": 300,
                    "redraw": True
                },
                "mode": "immediate",
                "transition": {
                    "duration": 300
                },
            },
        ],
        "label":
        generation,
        "method":
        "animate",
    }
    sliders_dict["steps"].append(slider_step)
    figure["layout"]["sliders"] = [sliders_dict]
    plot(figure, auto_open=False, filename=filename)
    return figure
def animate_3d_init_(data: Union[np.ndarray, pd.DataFrame, list],
                     filename: str) -> dict:
    """Plot the first (or zeroth) iteration of a population.

    Intended as a frames object. Plots Scatter 3D data.

    Parameters
    ----------
    data : Union[np.ndarray, pd.DataFrame, list]
        Contains the data to be plotted. Each row is an individual's objective values.
    filename : str
        Contains the name of the file to which the plot is saved.

    Returns
    -------
    dict
        Plotly figure object
    """
    figure = {"data": [], "layout": {}, "frames": []}
    figure["layout"]["hovermode"] = "closest"
    figure["layout"]["sliders"] = {
        "args": ["transition", {
            "duration": 400,
            "easing": "cubic-in-out"
        }],
        "initialValue": "1",
        "plotlycommand": "animate",
        "visible": True,
    }
    sliders_dict = {
        "active": 0,
        "yanchor": "top",
        "xanchor": "left",
        "currentvalue": {
            "font": {
                "size": 20
            },
            "prefix": "Iteration:",
            "visible": True,
            "xanchor": "right",
        },
        "transition": {
            "duration": 300,
            "easing": "cubic-in-out"
        },
        "pad": {
            "b": 10,
            "t": 50
        },
        "len": 0.9,
        "x": 0.1,
        "y": 0,
        "steps": [],
    }
    figure["layout"]["sliders"] = [sliders_dict]
    figure["layout"]["scene"] = dict(
        xaxis=dict(
            backgroundcolor="rgb(200, 200, 230)",
            gridcolor="rgb(255, 255, 255)",
            showbackground=True,
            zerolinecolor="rgb(255, 255, 255)",
        ),
        yaxis=dict(
            backgroundcolor="rgb(230, 200,230)",
            gridcolor="rgb(255, 255, 255)",
            showbackground=True,
            zerolinecolor="rgb(255, 255, 255)",
        ),
        zaxis=dict(
            backgroundcolor="rgb(230, 230,200)",
            gridcolor="rgb(255, 255, 255)",
            showbackground=True,
            zerolinecolor="rgb(255, 255, 255)",
        ),
    )

    data_dict = go.Scatter3d(
        x=list(data[:, 0]),
        y=list(data[:, 1]),
        z=list(data[:, 2]),
        mode="markers",
        marker=dict(
            size=8,
            color=data[:, 2],
            colorscale="Viridis",
            opacity=0.5,
            line=dict(width=2, color="black", colorscale="Viridis"),
        ),
    )
    figure["data"].append(data_dict)
    plot(figure, filename=filename)
    animate_3d_next_(data, figure, filename, 0)
    return figure
def animate_2d_next_(
    data: Union[np.ndarray, pd.DataFrame, list],
    figure: dict,
    filename: str,
    generation: int,
) -> dict:
    """Plot the next set of individuals in a 2D scatter animation.

    Parameters
    ----------
    data : Union[np.ndarray, pd.DataFrame, list]
        The objective values to be plotted
    figure : dict
        Plotly figure object compatible dict
    filename : str
        Name of the file to which the plot is saved
    generation : int
        Iteration Number

    Returns
    -------
    dict
        Plotly Figure Object
    """
    frame = {"data": [], "name": str(generation)}
    sliders_dict = figure["layout"]["sliders"][0]
    data_dict = {
        "x": list(data[:, 0]),
        "y": list(data[:, 1]),
        "mode": "markers",
        "marker": {
            "size": 5,
            "color": "rgba(255, 182, 193, .9)",
            "line": dict(width=2),
        },
    }
    frame["data"].append(data_dict)
    figure["frames"].append(frame)
    slider_step = {
        "args": [
            [generation],
            {
                "frame": {
                    "duration": 300,
                    "redraw": False
                },
                "mode": "immediate",
                "transition": {
                    "duration": 300
                },
            },
        ],
        "label":
        generation,
        "method":
        "animate",
    }
    sliders_dict["steps"].append(slider_step)
    figure["layout"]["sliders"] = [sliders_dict]
    plot(figure, auto_open=False, filename=filename)
    return figure
Exemplo n.º 27
0
def gen_plot_page(df):

    # Uniquify by real_time and op_group page 
    df = df[df.sub_type=='FullOp']
    df = df[['real_time','op_group']]
    df = df.drop_duplicates()
    df = df.sort_values(by="real_time",ascending=False)

    # Prepare addition columns for Pareto Chart 
    df['cumulative_sum'] = df.real_time.cumsum()
    df['cumulative_perc'] = 100*df.cumulative_sum/df.real_time.sum()
    df['demarcation'] = 80

    # Filter out until 80% 
    df = df.query('cumulative_perc < 80')

    # Prepare plotly data
    trace1 = Bar(
        x=df.op_group,
        y=df.real_time,
        name='Real Time',
        marker=dict(
             color='rgb(34,163,192)'
                    )
    )
    trace2 = Scatter(
        x=df.op_group,
        y=df.cumulative_perc,
        name='Cumulative Percentage',
        yaxis='y2',
        line=dict(
            color='rgb(243,158,115)',
            width=2.4
           )
    )
    trace3 = Scatter(
        x=df.op_group,
        y=df.demarcation,
        name='80%',
        yaxis='y2',
        line=dict(
            color='rgba(128,128,128,.45)',
            dash = 'dash',
            width=1.5
           )
    )
    data = [trace1, trace2, trace3]
    layout = Layout(
        title='Smartfill run time - Pareto Chart',
        titlefont=dict(
            color='',
            family='',
            size=0
        ),
        font=Font(
            color='rgb(128,128,128)',
            family='Balto, sans-serif',
            size=12
        ),
        width=1500,
        height=623,
        paper_bgcolor='rgb(240, 240, 240)',
        plot_bgcolor='rgb(240, 240, 240)',
        hovermode='compare',
        margin=dict(b=250,l=60,r=60,t=65),
        showlegend=True,
           legend=dict(
              x=.83,
              y=1.3,
              font=dict(
                family='Balto, sans-serif',
                size=12,
                color='rgba(128,128,128,.75)'
            ),
        ),
        annotations=[ dict(
                      text="Cumulative Percentage",
                      showarrow=False,
                      xref="paper", yref="paper",
                      textangle=90,
                      x=1.029, y=.75,
                      font=dict(
                      family='Balto, sans-serif',
                      size=14,
                      color='rgba(243,158,115,.9)'
                ),)],
        xaxis=dict(
          tickangle=-90
        ),
        yaxis=dict(
            title='Real Time',
            range=[0,30300],
          tickfont=dict(
                color='rgba(34,163,192,.75)'
            ),
          tickvals = [0,6000,12000,18000,24000,30000],
            titlefont=dict(
                    family='Balto, sans-serif',
                    size=14,
                    color='rgba(34,163,192,.75)')
        ),
        yaxis2=dict(
            range=[0,101],
            tickfont=dict(
                color='rgba(243,158,115,.9)'
            ),
            tickvals = [0,20,40,60,80,100],
            overlaying='y',
            side='right'
        )
    )
    
    fig = dict(data=data, layout=layout)
    
    # Gen plotly page 
    offline.plot(fig, auto_open=False, filename="pareto_chart.html")
Exemplo n.º 28
0
def plot_station_anomalies_with_score(data, clf, station_id,
                                      start_date='',
                                      end_date='',
                                      return_data=False,
                                      offline_plot=False,
                                      display_title=True,
                                      return_plot=False):
    """
    Plot Time Series activty and anomaly score
    Parameters
    ----------
    data : pd.DataFrame
        Tableau temporelle de l'activité des stations Vcub
    clf : Pipeline Scikit Learn
        Estimator already fit
    station_id : Int
        ID station
    start_date : str [opt]
        Date de début du graphique yyyy-mm-dd
    end_date : str [opt]
        Date de fin du graphique yyyy-mm-dd
    return_data : bool [opt]
        Retour le DataFrame lié à la station demandé et au contraintes de date si remplie.
    offline_plot : bool [opt]
        Pour exporter le graphique
    display_title : bool [opt]
        Afin d'afficher le titre du graphique
    offline_plot : bool [opt]
        Pour retourner le graphique et l'utiliser dans une application

    Returns
    -------
    data : pd.DataFrame
        Could return it if return_data is True.

    Examples
    --------

    plot_station_anomalies_with_score(data=ts_activity, clf=clf, station_id=22)
    """

    # Filter on station_id
    data_station = data[data['station_id'] == station_id].copy()

    if 'consecutive_no_transactions_out' not in data.columns:
        # Some features
        data_station = get_transactions_in(data_station)
        data_station = get_transactions_out(data_station)
        data_station = get_transactions_all(data_station)
        data_station = get_consecutive_no_transactions_out(data_station)

    data_pred = predict_anomalies_station(data=data_station,
                                          clf=clf,
                                          station_id=station_id)
    
    data_pred['anomaly_score'] = \
        logistic_predict_proba_from_model(clf.decision_function(data_pred[FEATURES_TO_USE_CLUSTER])) * 100

    if start_date != '':
        data_pred = data_pred[data_pred['date'] >= start_date]

    if end_date != '':
        data_pred = data_pred[data_pred['date'] <= end_date]
        
    # Figure
    
    if display_title:
        title = "Détection d'anomalies sur la stations N° " + str(station_id)
    else:
        title = None
    
    fig = make_subplots(rows=2, cols=1,
                        shared_xaxes=True,
                        specs=[[{"secondary_y": True}],
                               [{"secondary_y": True}]],
                        row_heights=[0.82, 0.18],
                        vertical_spacing=0.01,
                        x_title=title,
                       )
    # Row 1

    # Axe 1
    fig.add_trace(go.Scatter(x=data_pred['date'],
                             y=data_pred['available_bikes'],
                             mode='lines',
                             line={'width': 2},
                             name="Vélo disponible"),
                  row=1, col=1)
                

    # Axe 2
    fig.add_trace(go.Scatter(x=data_pred['date'],
                             y=data_pred['consecutive_no_transactions_out'],
                             mode='lines',
                             line={'width': 1,
                                   'dash': 'dot',
                                   'color': 'rgba(189,189,189,1)'},
                             #yaxis='y2',
                             name='Absence consécutive de prise de vélo'),
                  row=1, col=1, secondary_y=True)

    # For shape hoverdata anomaly
    data_pred['ano_hover_text'] = np.NaN
    data_pred.loc[data_pred['anomaly'] == -1,
                  'ano_hover_text'] = data_pred['available_bikes']
    fig.add_trace(go.Scatter(x=data_pred['date'],
                             y=data_pred['ano_hover_text'],
                             mode='lines',
                             text='x',
                             connectgaps=False,
                             line={'width': 2,
                                   'color': 'red'},
                             name='anomaly'),
                 row=1, col=1) 
    
    # Row 2
    fig.add_trace(go.Scatter(x=data_pred['date'],
                             y=data_pred['anomaly_score'],
                             line={'width': 1, 'color': 'black'},
                             fill="tozeroy",
                             mode='lines', #'lines' #'none'
                             name="Score d'anomalie"),
                  row=2, col=1)

    # Shapes anomaly
    shapes = []
    # https://github.com/armgilles/vcub_keeper/issues/38
    data_pred['no_anomalie'] = (data_pred['anomaly'] == 1)
    data_pred['anomaly_grp'] = data_pred['no_anomalie'].cumsum()

    grp = \
        data_pred[data_pred['anomaly'] == -1].groupby('anomaly_grp',
                                                      as_index=False)['date'].agg({'min': 'min',
                                                                                   'max': 'max'})

    max_value = data_pred['available_bikes'].max()
    for idx, row in grp.iterrows():
        shapes.append(dict(type="rect",
                           xref="x",
                           yref="y",
                           x0=row['min'],
                           y0=0,
                           x1=row['max'],
                           y1=max_value,
                           fillcolor="red",
                           opacity=0.7,
                           layer="below",
                           line_width=0
                           ))

    data_pred = data_pred.drop(['no_anomalie', 'anomaly_grp'], axis=1)


    # Design graph
    layout = dict(
        showlegend=True,
        legend=dict(orientation="h",
                    yanchor="top",
                    xanchor="center",
                    y=1.2,
                    x=0.5
                    ),
        xaxis=dict(
                rangeslider=dict(
                    visible=False
                ),
                type='date',
                tickformat='%a %Y-%m-%d %H:%M',
                hoverformat='%a %Y-%m-%d %H:%M'
        ),
        yaxis={'title': 'Nombre de vélo disponible',
               'title_font': {'color': 'rgba(100, 111, 251, 1)'},
               'tickfont': {'color': 'rgba(100, 111, 251, 1)'}
                },
        yaxis2={'title': 'Absence consécutive de prise de vélo',
                'title_font': {'color': 'rgba(122, 122, 122, 1)'},
                #'overlaying': 'y',
                'side': 'right',
                'showgrid': False,
                'visible': True,
                'tickfont': {'color': 'rgba(122, 122, 122, 1)'}
               },
        xaxis2=dict(
                rangeslider=dict(
                    visible=False
                ),
                type='date',
                hoverformat='%a %Y-%m-%d %H:%M'
        ),
        yaxis3={'title': 'Score',
                'title_font': {'color': 'rgba(0, 0, 0, 0.8)'},
                'range': [0, 100],
                #'gridwidth': 25
                'tickmode': 'linear',
                'tick0': 0.0,
                'dtick': 25
               },
        template='plotly_white',
        hovermode='x',
        shapes=shapes
    )

    fig.update_layout(layout)
    
    # Horizontal line for anomaly score
    fig.add_shape(go.layout.Shape(type="line",
                                  name='test',
                                  x0=data_pred['date'].min(),
                                  y0=50,
                                  x1=data_pred['date'].max(),
                                  y1=50,
                                  line=dict(color='Red', width=1,
                                            dash='dot'),
                                  #xref='x',
                                  #yref='y'
                                 ),
                    row=2, col=1)
    
    if return_plot is True:
        return fig
    if offline_plot is False:
        iplot(fig)
    else:
        offline.plot(fig)

    if return_data is True:
        return data_pred
Exemplo n.º 29
0
def interactive_plot_patient_time_3d(list_patients_to_keep, df, df_label, fs_id1=1, fs_id2=2, fs_id3=3,
                                     class_patient=[1, 3, 5], display=True, ):
    """
    :param fs_id1:
    :param fs_id2:
    :param fs_id3:
    :param df: table_patients_mca
    :param df_label: df_label
    :param class_patient:
    :param display:
    :param list_patients_to_keep: table_patients_mca.columns.to_list()
    :return:
    """
    df_copy = deepcopy(df)  # deepcopy because some columns may be deleted

    # delete columns (patients) accroding to the list_patients_to_keep
    list_to_delete = select_list_to_delete_from_list_to_keep(df_copy, list_patients_to_keep)
    df_copy = df_copy.drop(list_to_delete, axis=1)

    df_label_color = apply_color_label(df_label)
    fs = 'Factor'
    points_x = df_copy.loc[(fs, fs_id1)].values
    points_y = df_copy.loc[(fs, fs_id2)].values
    points_z = df_copy.loc[(fs, fs_id3)].values
    labels = df_copy.columns.values
    coordinates_max = max(max(abs(df.loc[(fs, fs_id1)].values)),
                          max(abs(df.loc[(fs, fs_id2)].values)),
                          max(abs(df.loc[(fs, fs_id3)].values)))

    fig = go.Figure()
    dic_nb_patients = {}
    for step in np.arange(0, 5):

        df_label_copy = pd.DataFrame(df_label_color.iloc[:, step])
        df_color = pd.DataFrame(df_label_color.loc[:, 'color' + str(step)])
        df_label_copy = df_label_copy.dropna()
        df_color = df_color.dropna()
        data_ = []
        dic_nb_patients[step] = 0
        for i, i_patient in enumerate(df_copy.columns):
            if df_label_copy.loc[i_patient,][0] in class_patient or str(
                    int(df_label_copy.loc[i_patient][0])) in class_patient:
                trace = go.Scatter3d(
                    visible=False,
                    mode='markers',
                    marker=dict(size=10, color=df_color.loc[i_patient, 'color' + str(step)]),
                    name='patient {}'.format(i_patient),
                    x=[points_x[i]],
                    y=[points_y[i]],
                    z=[points_z[i]],
                    hovertext=str(labels[i]))
                fig.add_trace(trace)
                dic_nb_patients[step] = dic_nb_patients[step] + 1

    # Make 10th trace visible
    for i in range(dic_nb_patients[0]):
        fig.data[i].visible = True

    # Create and add slider
    steps = []
    p_min = 0  # count the number of patient by time
    p_max = 0
    for i in range(5):
        nb_patients = dic_nb_patients[i]  # nb of points (graphs) for a period of time
        p_max = p_min + nb_patients
        step = dict(
            method="restyle",
            args=["visible", [False] * len(fig.data)],  # intitialize all to false
        )

        for j in range(p_min, p_max):
            step["args"][1][j] = True  # Toggle i'th trace to "visible"
        steps.append(step)
        p_min = p_max

    sliders = [dict(
        active=0,
        currentvalue={"prefix": "Durée de suivi "},
        pad={"t": 50},
        steps=steps
    )]

    fig.update_layout(
        sliders=sliders,
        title="Coordonnées des patients projetés dans le plan 3D des facteurs " + str(fs_id1) + ', ' + str(
            fs_id2) + ' et ' + str(fs_id3),
        scene=dict(
            xaxis={"title": "facteur" + str(fs_id1), "range": [-coordinates_max - 0.1, coordinates_max + 0.1]},
            yaxis={"title": "facteur" + str(fs_id2), "range": [-coordinates_max - 0.1, coordinates_max + 0.1]},
            zaxis={"title": "facteur" + str(fs_id3), "range": [-coordinates_max - 0.1, coordinates_max + 0.1]}),
        height=600
    )

    offline.plot(fig, filename='Images/Patients en fonction du temps dans le plan des facteurs scores.html',
                 # to save the figure in the repertory
                 auto_open=False)

    if display:
        offline.iplot(fig)
        return None
    else:
        return fig['data'], fig['layout']
Exemplo n.º 30
0
def graphFrequency(data, graphtitle, xtitle, filename):
    botNames = getBots()
    data = sorted(data, key=lambda tup: tup[2], reverse=True)
    # Filter out any bots
    bots = [x for x in data if x[3] in botNames]
    nobots = [x for x in data if not (x[3] in botNames)]
    # Filter out contributors who have been inactive for a year
    recent = nobots[0][4]
    for x in nobots:
        if x[4] > recent:
            recent = x[4]
    recent = datetime(recent.year - 1, recent.month, recent.day, recent.hour,
                      recent.minute, recent.second, recent.microsecond)
    inactive = [x for x in nobots if x[4] < recent]
    active = [x for x in nobots if x[4] >= recent]

    # Divide the remaining list into roughly fourths
    # to get 25th, 50th, 75th percentiles
    # Up to 3 extra people may end up in the last quartile
    # FIXME: I'm sure there's a more Pythonic way to do this
    quartiles = []
    chunks = int(len(active) / 4)
    for i in range(0, 4):
        if i != 3:
            quartiles.append(active[(chunks * i):(chunks * (i + 1))])
        else:
            quartiles.append(active[(chunks * i):len(active) + 1])
    data = []
    labels = [
        ('rgba(213, 94, 0, .8)', 'Top 25% of active contributors'),
        ('rgba(230, 159, 0, .8)', 'Above average active contributors'),
        ('rgba(86, 180, 233, .8)', 'Somewhat active contributors'),
        ('rgba(0, 114, 178, .8)', 'Least active contributors'),
    ]
    for i in range(4):
        data.append(
            Scatter(x=[coord[0] for coord in quartiles[i]],
                    y=[coord[1] for coord in quartiles[i]],
                    name=labels[i][1],
                    mode='markers',
                    text=[coord[3] for coord in quartiles[i]],
                    marker=dict(color=labels[i][0])))
    if inactive:
        data.append(
            Scatter(x=[coord[0] for coord in inactive],
                    y=[coord[1] for coord in inactive],
                    name='Inactive for more than 1 year',
                    mode='markers',
                    text=[coord[3] for coord in inactive],
                    marker=dict(color='rgba(0, 0, 0, .8)')))
    if bots:
        data.append(
            Scatter(x=[coord[0] for coord in bots],
                    y=[coord[1] for coord in bots],
                    name='Bots',
                    mode='markers',
                    text=[coord[3] for coord in bots],
                    marker=dict(color='rgba(240, 228, 66, .8)')))
    layout = Layout(
        title=graphtitle,
        yaxis=dict(title='Number of contributions'),
        xaxis=dict(title=xtitle),
    )
    fig = Figure(data=data, layout=layout)
    return offline.plot(fig,
                        show_link=False,
                        include_plotlyjs=False,
                        output_type='div')
Exemplo n.º 31
0
def offline_plot(graph_object: go):
    """Plot a graph object to an html file"""
    fig = go.Figure()
    fig.add_trace(graph_object)
    offline.plot(fig, filename="chart.html")