Exemplo n.º 1
0
def display_file(file_content, file_name, dropdown_value, dropdown_value_2,
                 dropdown_value_3):

    if file_content is None:
        return html.Div(['Choose a file to process it.'])

    data_ini = parse_file(file_content)

    if ('{}'.format(dropdown_value_2) == '(sg)'):
        window = golay_window.window(10.0, data_ini)
        # Apply the Savintzky - Golay filter with window = 31 and polynomial parameter = 6
        data = sav_golay.golay(data_ini, 31, 6)
        res = data[:, 1:4] - data_ini[:, 1:4]

    elif ('{}'.format(dropdown_value_2) == '(tma)'):
        data = triple_moving_average.generate_filtered_data(data_ini, 3)
        res = data[:, 1:4] - data_ini[:, 1:4]

    elif ('{}'.format(dropdown_value_2) == '(bo)'):
        data = sav_golay.golay(data_ini, 31, 6)
        data = triple_moving_average.generate_filtered_data(data, 3)
        res = data[:, 1:4] - data_ini[:, 1:4]

    elif ('{}'.format(dropdown_value_2) == '(nf)'):
        data = data_ini
        res = data[:, 1:4] - data_ini[:, 1:4]

    #else:
    #data=data_ini

    if (data.shape[0] > 0):
        data_dict = [{
            'No.': '{:03d}'.format(i + 1),
            't (s)': data[i][0],
            'x ' + '{}'.format(dropdown_value): data[i][1],
            'y ' + '{}'.format(dropdown_value): data[i][2],
            'z ' + '{}'.format(dropdown_value): data[i][3]
        } for i in range(data.shape[0])]

        if ('{}'.format(dropdown_value_3) == '(ef)'):
            kep, res = e_fit.determine_kep(data[:, 1:])

        elif ('{}'.format(dropdown_value_3) == '(gm)'):
            # Apply gibbs method
            kep_gibbs = gibbsMethod.gibbs_get_kep(data[:, 1:])
            kep_final_gibbs = lamberts_kalman.kalman(kep_gibbs, 0.01**2)
            kep_final_gibbs = np.transpose(kep_final_gibbs)
            kep = np.resize(kep_final_gibbs, ((7, 1)))

        elif ('{}'.format(dropdown_value_3) == '(in)'):
            # Apply the interpolation method
            kep_inter = interpolation.main(data)
            # Apply Kalman filters, estimate of measurement variance R = 0.01 ** 2
            kep_final_inter = lamberts_kalman.kalman(kep_inter, 0.01**2)
            kep = np.transpose(kep_final_inter)
            kep = np.resize(kep, ((7, 1)))

        elif ('{}'.format(dropdown_value_3) == '(lk)'):
            # Apply Lambert's solution for the filtered data set
            kep_lamb = lamberts_kalman.create_kep(data)
            # Apply Kalman filters, estimate of measurement variance R = 0.01 ** 2
            kep_final_lamb = lamberts_kalman.kalman(kep_lamb, 0.01**2)
            kep = np.transpose(kep_final_lamb)
            kep = np.resize(kep, ((7, 1)))

        # Visuals
        orbit_points = data[:, 1:] + res
        earth_points = gen_sphere()

        xyz_org = go.Scatter3d(name='Original Data',
                               legendgroup='org',
                               showlegend=False,
                               x=data[:, 1],
                               y=data[:, 2],
                               z=data[:, 3],
                               mode='markers',
                               marker={
                                   'size': 1,
                                   'color': 'black'
                               })

        xyz_fit = go.Scatter3d(name='Fitted Ellipse',
                               legendgroup='fit',
                               showlegend=False,
                               x=orbit_points[:, 0],
                               y=orbit_points[:, 1],
                               z=orbit_points[:, 2],
                               mode='lines',
                               line={
                                   'width': 5,
                                   'color': 'red'
                               })

        xyz_cur = go.Scatter3d(name='Position at Epoch',
                               legendgroup='cur',
                               showlegend=False,
                               x=[orbit_points[0, 0]],
                               y=[orbit_points[0, 1]],
                               z=[orbit_points[0, 2]],
                               mode='markers',
                               marker={
                                   'size': 3,
                                   'color': 'blue'
                               })

        earth_top = go.Mesh3d(x=earth_points[0],
                              y=earth_points[1],
                              z=earth_points[2],
                              color='#1f77b4',
                              opacity=0.5,
                              hoverinfo='skip',
                              showlegend=False)

        earth_bottom = go.Mesh3d(x=earth_points[0],
                                 y=earth_points[1],
                                 z=-earth_points[2],
                                 color='#1f77b4',
                                 opacity=0.5,
                                 hoverinfo='skip',
                                 showlegend=False)

        xy_org = go.Scatter(name='Original Data',
                            legendgroup='org',
                            showlegend=True,
                            x=data[:, 1],
                            y=data[:, 2],
                            mode='markers',
                            marker={
                                'size': 5,
                                'color': 'black'
                            })

        xy_fit = go.Scatter(name='Fitted Ellipse',
                            legendgroup='fit',
                            showlegend=True,
                            x=orbit_points[:, 0],
                            y=orbit_points[:, 1],
                            mode='lines',
                            line={
                                'width': 2,
                                'color': 'red'
                            })

        xy_cur = go.Scatter(name='Position at Epoch',
                            legendgroup='cur',
                            showlegend=True,
                            x=[orbit_points[0, 0]],
                            y=[orbit_points[0, 1]],
                            mode='markers',
                            marker={
                                'size': 10,
                                'color': 'blue'
                            })

        yz_org = go.Scatter(name='Original Data',
                            legendgroup='org',
                            showlegend=False,
                            x=data[:, 2],
                            y=data[:, 3],
                            mode='markers',
                            marker={
                                'size': 5,
                                'color': 'black'
                            })

        yz_fit = go.Scatter(name='Fitted Ellipse',
                            legendgroup='fit',
                            showlegend=False,
                            x=orbit_points[:, 1],
                            y=orbit_points[:, 2],
                            mode='lines',
                            line={
                                'width': 2,
                                'color': 'red'
                            })

        yz_cur = go.Scatter(name='Position at Epoch',
                            legendgroup='cur',
                            showlegend=False,
                            x=[orbit_points[0, 1]],
                            y=[orbit_points[0, 2]],
                            mode='markers',
                            marker={
                                'size': 10,
                                'color': 'blue'
                            })

        xz_org = go.Scatter(name='Original Data',
                            legendgroup='org',
                            showlegend=False,
                            x=data[:, 1],
                            y=data[:, 3],
                            mode='markers',
                            marker={
                                'size': 5,
                                'color': 'black'
                            })

        xz_fit = go.Scatter(name='Fitted Ellipse',
                            legendgroup='fit',
                            showlegend=False,
                            x=orbit_points[:, 0],
                            y=orbit_points[:, 2],
                            mode='lines',
                            line={
                                'width': 2,
                                'color': 'red'
                            })

        xz_cur = go.Scatter(name='Position at Epoch',
                            legendgroup='cur',
                            showlegend=False,
                            x=[orbit_points[0, 0]],
                            y=[orbit_points[0, 2]],
                            mode='markers',
                            marker={
                                'size': 10,
                                'color': 'blue'
                            })

        xyz_fig = tools.make_subplots(rows=2,
                                      cols=2,
                                      specs=[[{}, {}], [{}, {
                                          'is_3d': True
                                      }]])
        xyz_fig.append_trace(xz_org, 1, 1)
        xyz_fig.append_trace(xz_fit, 1, 1)
        xyz_fig.append_trace(xz_cur, 1, 1)
        xyz_fig.append_trace(yz_org, 1, 2)
        xyz_fig.append_trace(yz_fit, 1, 2)
        xyz_fig.append_trace(yz_cur, 1, 2)
        xyz_fig.append_trace(xy_org, 2, 1)
        xyz_fig.append_trace(xy_fit, 2, 1)
        xyz_fig.append_trace(xy_cur, 2, 1)
        xyz_fig.append_trace(xyz_org, 2, 2)
        xyz_fig.append_trace(xyz_fit, 2, 2)
        xyz_fig.append_trace(xyz_cur, 2, 2)
        xyz_fig.append_trace(earth_top, 2, 2)
        xyz_fig.append_trace(earth_bottom, 2, 2)

        xyz_fig['layout']['xaxis1'].update(title='x ' +
                                           '{}'.format(dropdown_value))
        xyz_fig['layout']['yaxis1'].update(title='z ' +
                                           '{}'.format(dropdown_value),
                                           scaleanchor='x')
        xyz_fig['layout']['xaxis2'].update(title='y ' +
                                           '{}'.format(dropdown_value),
                                           scaleanchor='x')
        xyz_fig['layout']['yaxis2'].update(title='z ' +
                                           '{}'.format(dropdown_value),
                                           scaleanchor='x2')
        xyz_fig['layout']['xaxis3'].update(title='x ' +
                                           '{}'.format(dropdown_value),
                                           scaleanchor='x')
        xyz_fig['layout']['yaxis3'].update(title='y ' +
                                           '{}'.format(dropdown_value),
                                           scaleanchor='x3')

        xyz_fig['layout']['scene1']['xaxis'].update(
            showticklabels=True,
            showspikes=False,
            title='x ' + '{}'.format(dropdown_value))
        xyz_fig['layout']['scene1']['yaxis'].update(
            showticklabels=True,
            showspikes=False,
            title='y ' + '{}'.format(dropdown_value))
        xyz_fig['layout']['scene1']['zaxis'].update(
            showticklabels=True,
            showspikes=False,
            title='z ' + '{}'.format(dropdown_value))

        xyz_fig['layout'].update(width=1050, height=700, margin={'t': 50})
        xyz_fig['layout']['legend'].update(orientation='h')

        rel_time = data[:, 0] - data[0, 0]

        xt_org = go.Scatter(name='Original Data',
                            legendgroup='org',
                            x=rel_time,
                            y=data[:, 1],
                            mode='markers',
                            marker={
                                'size': 5,
                                'color': 'black'
                            })

        xt_fit = go.Scatter(name='Fitted Ellipse',
                            legendgroup='fit',
                            x=rel_time,
                            y=orbit_points[:, 0],
                            mode='lines',
                            line={
                                'width': 2,
                                'color': 'red'
                            })

        xt_cur = go.Scatter(name='Position at Epoch',
                            legendgroup='cur',
                            x=[rel_time[0]],
                            y=[orbit_points[0, 0]],
                            mode='markers',
                            marker={
                                'size': 10,
                                'color': 'blue'
                            })

        yt_org = go.Scatter(name='Original Data',
                            legendgroup='org',
                            showlegend=False,
                            x=rel_time,
                            y=data[:, 2],
                            mode='markers',
                            marker={
                                'size': 5,
                                'color': 'black'
                            })

        yt_fit = go.Scatter(name='Fitted Ellipse',
                            legendgroup='fit',
                            showlegend=False,
                            x=rel_time,
                            y=orbit_points[:, 1],
                            mode='lines',
                            line={
                                'width': 2,
                                'color': 'red'
                            })

        yt_cur = go.Scatter(name='Position at Epoch',
                            legendgroup='cur',
                            showlegend=False,
                            x=[rel_time[0]],
                            y=[orbit_points[0, 1]],
                            mode='markers',
                            marker={
                                'size': 10,
                                'color': 'blue'
                            })

        zt_org = go.Scatter(name='Original Data',
                            legendgroup='org',
                            showlegend=False,
                            x=rel_time,
                            y=data[:, 3],
                            mode='markers',
                            marker={
                                'size': 5,
                                'color': 'black'
                            })

        zt_fit = go.Scatter(name='Fitted Ellipse',
                            legendgroup='fit',
                            showlegend=False,
                            x=rel_time,
                            y=orbit_points[:, 2],
                            mode='lines',
                            line={
                                'width': 2,
                                'color': 'red'
                            })

        zt_cur = go.Scatter(name='Position at Epoch',
                            legendgroup='cur',
                            showlegend=False,
                            x=[rel_time[0]],
                            y=[orbit_points[0, 2]],
                            mode='markers',
                            marker={
                                'size': 10,
                                'color': 'blue'
                            })

        rt_org = go.Scatter(name='Original Data',
                            legendgroup='org',
                            showlegend=False,
                            x=rel_time,
                            y=(data[:, 1]**2 + data[:, 2]**2 +
                               data[:, 3]**2)**0.5,
                            mode='markers',
                            marker={
                                'size': 5,
                                'color': 'black'
                            })

        rt_fit = go.Scatter(
            name='Fitted Ellipse',
            legendgroup='fit',
            showlegend=False,
            x=rel_time,
            y=((orbit_points[:, 0])**2 + (orbit_points[:, 1])**2 +
               (orbit_points[:, 2])**2)**0.5,
            mode='lines',
            line={
                'width': 2,
                'color': 'red'
            })

        rt_cur = go.Scatter(
            name='Position at Epoch',
            legendgroup='cur',
            showlegend=False,
            x=[rel_time[0]],
            y=[((orbit_points[0, 0])**2 + (orbit_points[0, 1])**2 +
                (orbit_points[0, 2])**2)**0.5],
            mode='markers',
            marker={
                'size': 10,
                'color': 'blue'
            })

        t_fig = tools.make_subplots(rows=4, cols=1, shared_xaxes=True)
        t_fig.append_trace(xt_org, 1, 1)
        t_fig.append_trace(xt_fit, 1, 1)
        t_fig.append_trace(xt_cur, 1, 1)
        t_fig.append_trace(yt_org, 2, 1)
        t_fig.append_trace(yt_fit, 2, 1)
        t_fig.append_trace(yt_cur, 2, 1)
        t_fig.append_trace(zt_org, 3, 1)
        t_fig.append_trace(zt_fit, 3, 1)
        t_fig.append_trace(zt_cur, 3, 1)
        t_fig.append_trace(rt_org, 4, 1)
        t_fig.append_trace(rt_fit, 4, 1)
        t_fig.append_trace(rt_cur, 4, 1)

        t_fig['layout']['xaxis1'].update(title='t (s)')
        t_fig['layout']['yaxis1'].update(title='x ' +
                                         '{}'.format(dropdown_value))
        t_fig['layout']['yaxis2'].update(title='y ' +
                                         '{}'.format(dropdown_value),
                                         scaleanchor='y')
        t_fig['layout']['yaxis3'].update(title='z ' +
                                         '{}'.format(dropdown_value),
                                         scaleanchor='y')
        t_fig['layout']['yaxis4'].update(title='|r| ' +
                                         '{}'.format(dropdown_value),
                                         scaleanchor='y',
                                         scaleratio=100)

        t_fig['layout'].update(width=1050, height=700, margin={'t': 50})
        t_fig['layout']['legend'].update(orientation='h')

        res_x = go.Histogram(name='Δx', x=res[:, 0])
        res_y = go.Histogram(name='Δy', x=res[:, 1])
        res_z = go.Histogram(name='Δx', x=res[:, 2])

        res_fig = tools.make_subplots(rows=1, cols=3, shared_yaxes=True)
        res_fig.append_trace(res_x, 1, 1)
        res_fig.append_trace(res_y, 1, 2)
        res_fig.append_trace(res_z, 1, 3)

        res_fig['layout']['yaxis1'].update(title='Frequency')
        res_fig['layout']['xaxis1'].update(title='Δx ' +
                                           '{}'.format(dropdown_value))
        res_fig['layout']['xaxis2'].update(title='Δy ' +
                                           '{}'.format(dropdown_value))
        res_fig['layout']['xaxis3'].update(title='Δz ' +
                                           '{}'.format(dropdown_value))

        res_fig['layout'].update(margin={'t': 50}, showlegend=False)

        coords_ecef, coord_times = ground_track(kep, data[0][0])

        track = go.Scattergeo(name='Ground Trace',
                              lat=coords_ecef[:, 0],
                              lon=coords_ecef[:, 1],
                              text=coord_times,
                              mode='lines',
                              line={'color': 'red'})

        track_cur = go.Scattergeo(name='Position at Epoch',
                                  lat=[coords_ecef[0, 0]],
                                  lon=[coords_ecef[0, 1]],
                                  text=coord_times[0],
                                  marker={
                                      'size': 10,
                                      'color': 'blue'
                                  })

        track_fig = go.Figure(data=[track, track_cur])
        track_fig['layout'].update(height=600, width=1050, margin={'t': 50})
        track_fig['layout']['geo']['lataxis'].update(showgrid=True,
                                                     dtick=30,
                                                     gridcolor='#ccc')
        track_fig['layout']['geo']['lonaxis'].update(showgrid=True,
                                                     dtick=60,
                                                     gridcolor='#ccc')
        track_fig['layout']['legend'].update(orientation='h')

        return [
            dcc.Markdown('''File Name: **''' + file_name + '''**'''),
            html.Details([
                html.Summary('''File Contents'''),
                dt.DataTable(rows=data_dict, editable=False)
            ],
                         open=False),
            html.Details([
                html.Summary('''Computed Keplerian Elements'''),
                dt.DataTable(rows=[
                    {
                        'Element': 'Semi-major Axis',
                        'Value': str(kep[0][0]) + '{}'.format(dropdown_value)
                    },
                    {
                        'Element': 'Eccentricity',
                        'Value': str(kep[1][0])
                    },
                    {
                        'Element': 'Inclination',
                        'Value': str(kep[2][0]) + ' °'
                    },
                    {
                        'Element': 'Argument of Periapsis',
                        'Value': str(kep[3][0]) + ' °'
                    },
                    {
                        'Element': 'Right Ascension of Ascending Node',
                        'Value': str(kep[4][0]) + ' °'
                    },
                    {
                        'Element': 'True Anomaly',
                        'Value': str(kep[5][0]) + ' °'
                    },
                ],
                             editable=False)
            ],
                         open=True),
            html.Details([
                html.Summary('''XYZ Plots'''),
                dcc.Graph(id='xyz-plot', figure=xyz_fig)
            ],
                         open=True),
            html.Details([
                html.Summary('''Ground Track'''),
                dcc.Graph(id='track-plot', figure=track_fig)
            ],
                         open=True),
            html.Details([
                html.Summary('''Time Plots'''),
                dcc.Graph(id='t-plot', figure=t_fig)
            ],
                         open=True),
            html.Details([
                html.Summary('''Residuals'''),
                dcc.Graph(id='res-plot', figure=res_fig),
                dt.DataTable(rows=[{
                    ' ':
                    'Maximum',
                    'Δx ' + '{}'.format(dropdown_value):
                    np.max(res[:, 0]),
                    'Δy ' + '{}'.format(dropdown_value):
                    np.max(res[:, 1]),
                    'Δz ' + '{}'.format(dropdown_value):
                    np.max(res[:, 2])
                }, {
                    ' ':
                    'Minimum',
                    'Δx ' + '{}'.format(dropdown_value):
                    np.min(res[:, 0]),
                    'Δy ' + '{}'.format(dropdown_value):
                    np.min(res[:, 1]),
                    'Δz ' + '{}'.format(dropdown_value):
                    np.min(res[:, 2])
                }, {
                    ' ':
                    'Average',
                    'Δx ' + '{}'.format(dropdown_value):
                    np.average(res[:, 0]),
                    'Δy ' + '{}'.format(dropdown_value):
                    np.average(res[:, 1]),
                    'Δz ' + '{}'.format(dropdown_value):
                    np.average(res[:, 2])
                }, {
                    ' ':
                    'Standard Deviation',
                    'Δx ' + '{}'.format(dropdown_value):
                    np.std(res[:, 0]),
                    'Δy ' + '{}'.format(dropdown_value):
                    np.std(res[:, 1]),
                    'Δz ' + '{}'.format(dropdown_value):
                    np.std(res[:, 2])
                }],
                             editable=False)
            ],
                         open=True),
        ]
    else:
        return html.Div(children=[
            html.Div(
                '''There was an error processing this file.Try uploading another file in required format.'''
            )
        ])
Exemplo n.º 2
0
 def update_figure(invar, invar_2, invar_3, outvar, invar1_log, invar2_log, invar3_log, outvar_log, param_slider,
                   graph_type, color_use, color_dd, error_use, error_dd, filter_active, fit_use, fit_dd, fit_num, fit_conf, add_noise_var, fit_color,
                   fit_opacity, fit_sampling, id_type, param_center, param_log):
     for i in range(len(param_slider)):
         if param_log[i] == ['log']:
             param_slider[i] = [10**val for val in param_slider[i]]
             param_center[i] = 10**param_center[i]
     if invar is None:
         return go.Figure()
     sel_y = np.full((len(outdata),), True)
     dds_value = []
     for iteration, values in enumerate(param_slider):
         dds_value.append(invars[id_type[iteration]['index']])
         # filter for minimum
         sel_y_min = np.array(indata[dds_value[iteration]] >= param_slider[iteration][0])
         # filter for maximum
         sel_y_max = np.array(indata[dds_value[iteration]] <= param_slider[iteration][1])
         # print('iter ', iteration, 'filer', filter_active[iteration][0])
         if filter_active != [[]]:
             if filter_active[iteration] == ['act']:
                 sel_y = sel_y_min & sel_y_max & sel_y
     if graph_type == '1D':
         fig = go.Figure(
             data=[go.Scatter(
                 x=indata[invar][sel_y],
                 y=outdata[outvar][sel_y],
                 mode='markers',
                 name='data',
                 error_y=dict(type='data', array=outdata[error_dd][sel_y], visible= error_use == ['true']),
                 # text=[(invar, outvar) for i in range(len(indata[invar][sel_y]))],
                 # hovertemplate=" %{text} <br> %{x} <br> %{y}",
             )],
             layout=go.Layout(xaxis=dict(title=invar, rangeslider=dict(visible=True)), yaxis=dict(title=outvar))
         )
         if fit_use == ['show']:
             mesh_in, mesh_out, mesh_out_std, fit_dd_values = mesh_fit(param_slider, id_type, fit_dd, fit_num,
                                                                       param_center, [invar], [invar1_log],
                                                                       outvar, fit_sampling, add_noise_var)
             for i in range(len(fit_dd_values)):
                 fig.add_trace(go.Scatter(
                     x=mesh_in[i][invars.index(invar)],
                     y=mesh_out[i],
                     mode='lines',
                     name=f'fit: {fit_dd}={fit_dd_values[i]:.1e}',
                     line_color=colormap(indata[fit_dd].min(), indata[fit_dd].max(), fit_dd_values[i]),
                     marker_line=dict(coloraxis="coloraxis2"),
                 ))
                 fig.add_trace(go.Scatter(
                     x=np.hstack((mesh_in[i][invars.index(invar)], mesh_in[i][invars.index(invar)][::-1])),
                     y=np.hstack((mesh_out[i] + fit_conf * mesh_out_std[i], mesh_out[i][::-1] - fit_conf * mesh_out_std[i][::-1])),
                     showlegend=False,
                     fill='toself',
                     line_color=colormap(indata[fit_dd].min(), indata[fit_dd].max(), fit_dd_values[i]),
                     marker_line=dict(coloraxis="coloraxis2"),
                     opacity=fit_opacity,
                 ))
     elif graph_type == '2D':
         fig = go.Figure(
             data=[go.Scatter3d(
                 x=indata[invar][sel_y],
                 y=indata[invar_2][sel_y],
                 z=outdata[outvar][sel_y],
                 mode='markers',
                 name='Data',
                 error_z=dict(type='data', array=outdata[error_dd][sel_y], visible=error_use == ['true'], width= 10)
             )],
             layout=go.Layout(scene=dict(xaxis_title=invar, yaxis_title=invar_2, zaxis_title=outvar))
         )
         if fit_use == ['show'] and invar != invar_2:
             mesh_in, mesh_out, mesh_out_std, fit_dd_values = mesh_fit(param_slider, id_type, fit_dd, fit_num,
                                                                       param_center, [invar, invar_2],
                                                                       [invar1_log, invar2_log], outvar,
                                                                       fit_sampling, add_noise_var)
             for i in range(len(fit_dd_values)):
                 fig.add_trace(go.Surface(
                     x=mesh_in[i][invars.index(invar)].reshape((fit_sampling, fit_sampling)),
                     y=mesh_in[i][invars.index(invar_2)].reshape((fit_sampling, fit_sampling)),
                     z=mesh_out[i].reshape((fit_sampling, fit_sampling)),
                     name=f'fit: {fit_dd}={fit_dd_values[i]:.2f}',
                     surfacecolor=fit_dd_values[i] * np.ones([fit_sampling, fit_sampling])
                         if fit_color == 'multi-fit' else
                         (mesh_in[i][invars.index(color_dd)].reshape((fit_sampling, fit_sampling))
                          if (fit_color == 'marker-color' and color_dd in invars) else
                          mesh_out[i].reshape((fit_sampling, fit_sampling))),
                     opacity=fit_opacity,
                     coloraxis="coloraxis2" if (fit_color == 'multi-fit' or
                         (fit_color == 'output' and (color_dd != outvar and color_dd != 'OUTPUT'))) else "coloraxis",
                     showlegend=True if len(invars) > 2 else False,
                 ))
                 if fit_conf > 0:
                     fig.add_trace(go.Surface(
                         x=mesh_in[i][invars.index(invar)].reshape((fit_sampling, fit_sampling)),
                         y=mesh_in[i][invars.index(invar_2)].reshape((fit_sampling, fit_sampling)),
                         z=mesh_out[i].reshape((fit_sampling, fit_sampling)) + fit_conf * mesh_out_std[i].reshape((fit_sampling, fit_sampling)),
                         showlegend=False,
                         name=f'fit+v: {fit_dd}={fit_dd_values[i]:.2f}',
                         surfacecolor=fit_dd_values[i] * np.ones([fit_sampling, fit_sampling])
                             if fit_color == 'multi-fit' else
                             (mesh_in[i][invars.index(color_dd)].reshape((fit_sampling, fit_sampling))
                              if (fit_color == 'marker-color' and color_dd in invars) else
                              mesh_out[i].reshape((fit_sampling, fit_sampling))),
                         opacity=fit_opacity,
                         coloraxis="coloraxis2" if (fit_color == 'multi-fit' or
                             (fit_color == 'output' and (color_dd != outvar and color_dd != 'OUTPUT')))
                             else "coloraxis",
                     ))
                     fig.add_trace(go.Surface(
                         x=mesh_in[i][invars.index(invar)].reshape((fit_sampling, fit_sampling)),
                         y=mesh_in[i][invars.index(invar_2)].reshape((fit_sampling, fit_sampling)),
                         z=mesh_out[i].reshape((fit_sampling, fit_sampling)) - fit_conf * mesh_out_std[i].reshape((fit_sampling, fit_sampling)),
                         showlegend=False,
                         name=f'fit-v: {fit_dd}={fit_dd_values[i]:.2f}',
                         surfacecolor=fit_dd_values[i] * np.ones([fit_sampling, fit_sampling])
                             if fit_color == 'multi-fit' else
                             (mesh_in[i][invars.index(color_dd)].reshape((fit_sampling, fit_sampling))
                              if (fit_color == 'marker-color' and color_dd in invars) else
                              mesh_out[i].reshape((fit_sampling, fit_sampling))),
                         opacity=fit_opacity,
                         coloraxis="coloraxis2" if (fit_color == 'multi-fit' or
                             (fit_color == 'output' and (color_dd != outvar and color_dd != 'OUTPUT')))
                             else "coloraxis",
                     ))
             fig.update_layout(coloraxis2=dict(
                 colorbar=dict(title=outvar if fit_color == 'output' else fit_dd),
                 cmin=min(fit_dd_values) if fit_color == 'multi-fit' else None,
                 cmax=max(fit_dd_values) if fit_color == 'multi-fit' else None,
             ))
     elif graph_type == '2D contour':
         mesh_in, mesh_out, mesh_out_std, fit_dd_values = mesh_fit(param_slider, id_type, fit_dd, fit_num,
                                                                   param_center, [invar, invar_2],
                                                                   [invar1_log, invar2_log], outvar,
                                                                   fit_sampling, add_noise_var)
         data_x = mesh_in[0][invars.index(invar)]
         data_y = mesh_in[0][invars.index(invar_2)]
         fig = go.Figure()
         if min(data_x) != max(data_x):
             if min(data_y) != max(data_y):
                 fig.add_trace(go.Scatter(
                     x=indata[invar][sel_y],
                     y=indata[invar_2][sel_y],
                     mode='markers',
                     name='Data',
                 ))
                 fig.add_trace(go.Contour(
                     x=mesh_in[0][invars.index(invar)],
                     y=mesh_in[0][invars.index(invar_2)],
                     z=mesh_out[0],
                     contours_coloring='heatmap',
                     contours_showlabels=True,
                     coloraxis='coloraxis2',
                     name='fit',
                 ))
                 fig.update_xaxes(
                     range=[log10(min(fig.data[1]['x'])), log10(max(fig.data[1]['x']))] if invar1_log == ['log']
                     else [min(fig.data[1]['x']), max(fig.data[1]['x'])])
                 fig.update_yaxes(
                     range=[log10(min(fig.data[1]['y'])), log10(max(fig.data[1]['y']))] if invar2_log == ['log']
                     else [min(fig.data[1]['y']), max(fig.data[1]['y'])])
                 fig.update_layout(xaxis_title=invar,
                                   yaxis_title=invar_2,
                                   coloraxis2=dict(colorbar=dict(title=outvar),
                                                   colorscale='solar',
                                                   cmin=min(fig.data[1]['z']),
                                                   cmax=max(fig.data[1]['z'])))
             else:
                 fig.update_layout(title="y-data is constant, no contour-plot possible")
         else:
             fig.update_layout(title="x-data is constant, no contour-plot possible")
     elif graph_type == '3D':
         fig = go.Figure(
             data=go.Scatter3d(
                 x=indata[invar][sel_y],
                 y=indata[invar_2][sel_y],
                 z=indata[invar_3][sel_y],
                 mode='markers',
                 marker=dict(
                         color=outdata[outvar][sel_y],
                         coloraxis="coloraxis2",
                     ),
                 name='Data',
             ),
             layout=go.Layout(scene=dict(xaxis_title=invar, yaxis_title=invar_2, zaxis_title=invar_3)),
         )
         fig.update_layout(coloraxis2=dict(
             colorbar=dict(title=outvar),
         ))
         if fit_use == ['show'] and len({invar, invar_2, invar_3}) == 3:
             mesh_in, mesh_out, mesh_out_std, fit_dd_values = mesh_fit(param_slider, id_type, fit_dd, fit_num,
                                                                       param_center, [invar, invar_2, invar_3],
                                                                       [invar1_log, invar2_log, invar3_log], outvar,
                                                                       fit_sampling, add_noise_var)
             for i in range(len(fit_dd_values)):
                 fig.add_trace(
                     go.Isosurface(
                         x=mesh_in[i][invars.index(invar)],
                         y=mesh_in[i][invars.index(invar_2)],
                         z=mesh_in[i][invars.index(invar_3)],
                         value=mesh_out[i],
                         surface_count=fit_num,
                         coloraxis="coloraxis2",
                         isomin=mesh_out[i].min() * 1.1,
                         isomax=mesh_out[i].max() * 0.9,
                         caps=dict(x_show=False, y_show=False, z_show=False),
                         opacity=fit_opacity,
                     ),
                 )
     else:
         fig = go.Figure()
     fig.update_layout(legend=dict(xanchor="left", x=0.01))
     # log scale
     log_dict = {'1D': (invar1_log, outvar_log),
                 '2D': (invar1_log, invar2_log, outvar_log),
                 '2D contour': (invar1_log, invar2_log),
                 '3D': (invar1_log, invar2_log, invar3_log),}
     log_list = ['linear' if log is None or len(log) == 0 else log[0] for log in log_dict[graph_type]]
     log_key = ['xaxis', 'yaxis', 'zaxis']
     comb_dict = dict(zip(log_key, [{'type': log} for log in log_list]))
     if len(log_list) < 3 :
         fig.update_layout(**comb_dict)
     else:
         fig.update_scenes(**comb_dict)
     # color
     if color_use == ['true']: # TODO: trigger-detection no new fig just update
         if fit_use == ['show'] and (graph_type=='2D' and (fit_color=='multi-fit' and color_dd==fit_dd)):
             fig.update_traces(
                 marker=dict(
                     coloraxis="coloraxis2",
                     color=indata[color_dd][sel_y] if color_dd in indata.dtype.names else outdata[color_dd][sel_y],
                 ),
                 selector=dict(mode='markers'),
             )
         elif graph_type == '3D':
             fig.update_traces(
                 marker=dict(
                     coloraxis="coloraxis2",
                     color=outdata[outvar][sel_y],
                 ),
                 selector=dict(mode='markers'),
             )
         elif graph_type=='1D':
             fig.update_traces(
                 marker=dict(
                     coloraxis="coloraxis2",
                     color=outdata[outvar][sel_y] if color_dd == 'OUTPUT' else
                     (indata[color_dd][sel_y] if color_dd in indata.dtype.names else outdata[color_dd][sel_y]),
                 ),
                 selector=dict(mode='markers'),
             )
             if color_dd==fit_dd:
                 fig.update_layout(coloraxis2=dict(colorscale='cividis', colorbar=dict(title=fit_dd)))
             elif color_dd == 'OUTPUT':
                 fig.update_layout(coloraxis2=dict(colorscale='plasma', colorbar=dict(title=outvar)))
             else:
                 fig.update_layout(coloraxis2=dict(colorscale='plasma', colorbar=dict(title=color_dd)))
         elif graph_type =='2D contour':
             fig.update_traces(
                 marker=dict(
                     coloraxis="coloraxis",
                     color=outdata[outvar][sel_y] if color_dd == 'OUTPUT' else
                     (indata[color_dd][sel_y] if color_dd in indata.dtype.names else outdata[color_dd][sel_y]),
                 ),
                 selector=dict(mode='markers'),
             )
             if color_dd == outvar or color_dd == 'OUTPUT':
                 fig.update_traces(marker_coloraxis="coloraxis2", selector=dict(mode='markers'))
             else:
                 fig.update_layout(coloraxis=dict(colorbar=dict(title=color_dd, x=1.1),
                                                  colorscale='ice'))
         else:
             fig.update_traces(
                 marker=dict(
                     coloraxis="coloraxis",
                     color=outdata[outvar][sel_y] if color_dd == 'OUTPUT' else
                     (indata[color_dd][sel_y] if color_dd in indata.dtype.names else outdata[color_dd][sel_y]),
                 ),
                 selector=dict(mode='markers'),
             )
             fig.update_layout(coloraxis=dict(
                 colorbar=dict(title=outvar if color_dd == 'OUTPUT' else color_dd, x=1.1),
                 colorscale='viridis',
             ))
     fig.update_layout(height=graph_height)
     return fig
Exemplo n.º 3
0
Arquivo: 3d.py Projeto: mcneela/Retina
import fovea_plot as fp
import plotly.graph_objs as go

import numpy as np

x, y, z = np.random.multivariate_normal(np.array([0,0,0]), np.eye(3), 200).transpose()
trace1 = go.Scatter3d(
    x=x,
    y=y,
    z=z,
    mode='markers',
    marker=dict(
        size=12,
        line=dict(
            color='rgba(217, 217, 217, 0.14)',
            width=0.5
        ),
        opacity=0.8
    )
)

x2, y2, z2 = np.random.multivariate_normal(np.array([0,0,0]), np.eye(3), 200).transpose()
trace2 = go.Scatter3d(
    x=x2,
    y=y2,
    z=z2,
    mode='markers',
    marker=dict(
        color='rgb(127, 127, 127)',
        size=12,
        symbol='circle',
Exemplo n.º 4
0
def plot_lab(pix, img):
    '''
    Plots colors in CIELAB color space in plotly 3D Scatter plot
    '''
    colors = [
        'rgb(' + str(r) + ',' + str(g) + ',' + str(b) + ')'
        for (r, g, b) in pix
    ]
    x, y, z = [], [], []
    for item in pix:
        R, G, B = item[0] / 255, item[1] / 255, item[2] / 255
        if R > 0.04045:
            R = math.pow(((R + 0.055) / 1.055), 2.4)
        else:
            R = R / 12.92
        if G > 0.04045:
            G = math.pow(((G + 0.055) / 1.055), 2.4)
        else:
            G = G / 12.92
        if B > 0.04045:
            B = math.pow(((B + 0.055) / 1.055), 2.4)
        else:
            B = B / 12.92
        R *= 100
        G *= 100
        B *= 100

        X = (R * 0.4124) + (G * 0.3576) + (B * 0.1805)
        Y = (R * 0.2126) + (G * 0.7152) + (B * 0.0722)
        Z = (R * 0.0193) + (G * 0.1192) + (B * 0.9505)

        X = X / 95.047
        Y = Y / 100
        Z = Z / 108.883

        if X > 0.008856:
            fx = (math.pow(X, 1 / 3))
        else:
            fx = ((903.3 * X) + 16) / 116
        if Y > 0.008856:
            fy = (math.pow(Y, 1 / 3))
        else:
            fy = ((903.3 * Y) + 16) / 116
        if Z > 0.008856:
            fz = (math.pow(Z, 1 / 3))
        else:
            fz = ((903.3 * Z) + 16) / 116

        L = (116 * fy) - 16
        a = 500 * (fx - fy)
        b = 200 * (fy - fz)

        x.append(L)
        y.append(a)
        z.append(b)

    trace0 = go.Scatter3d(x=x,
                          y=y,
                          z=z,
                          mode='markers',
                          marker=dict(size=4, color=colors, opacity=0.8))
    data = [trace0]
    layout = go.Layout(title=img,
                       scene=dict(
                           xaxis=dict(title="L*", range=[0, 100]),
                           yaxis=dict(title="a*", range=[-86.185, 98.254]),
                           zaxis=dict(title="b*", range=[-107.863, 94.482]),
                       ),
                       margin=dict(l=0, r=0, b=25, t=50))
    fig = go.Figure(data=data, layout=layout)
    plotly.offline.plot(fig, filename=img + 'Lab')
def plot_3Dwaypoint_neighbors_center_goal_start():
    X = ( -2.5, -2.5, -2.5, -2.5, -2.300000191, -2.699999809, )
    Y = ( -3.900000095, -3.900000095, -4.099999905, -3.700000286, -3.900000095, -3.900000095, )
    Z = ( 3.100000143, 3.499999762, 3.299999952, 3.299999952, 3.299999952, 3.299999952, )
    X_A = ( -2.599999905, -2.5, -2.700000048, )
    Y_A = ( -3.799999952, -4.099999905, -3.900000095, )
    Z_A = ( 3, 3.299999952, 3.299999952, )
    X_UNKNOWN = ( -2.5, -2.5, -2.300000191, )
    Y_UNKNOWN = ( -3.900000095, -3.700000286, -3.900000095, )
    Z_UNKNOWN = ( 3.499999762, 3.299999952, 3.299999952, )



    trace_neighbors_unknown = go.Scatter3d(
        x= X_UNKNOWN,
        y= Y_UNKNOWN,
        z= Z_UNKNOWN,
        mode='markers',
        name = "Unknown",
        marker=dict(
            size=12,
        )
    )
    trace_neighbors_raw = go.Scatter3d(
        x= X,
        y= Y,
        z= Z,
        mode='markers',
        name = "Raw",
        marker=dict(
            size=12,
        )
    )
    trace_neighbors_centers = go.Scatter3d(
        x= X_A,
        y= Y_A,
        z= Z_A,
        mode='markers',
        name = "Centers",
        marker=dict(
            size=12,
        )
    )
    trace_center = go.Scatter3d(
        x=(-2.5, ),
        y=(-3.9, ),
        z=(3.3, ) ,
        mode='markers',
        name = "Start center",
        marker=dict(
            size=12,
        )
    )
    goal_point_coordinates = (2.0, 5.2, 1.2)
    trace_end_point = go.Scatter3d(
        x=(goal_point_coordinates[0], ),
        y=(goal_point_coordinates[1], ),
        z=(goal_point_coordinates[2], ) ,
        mode='markers',
        name = "End POINT",
        marker=dict(
            size=12,
        )
    )

    traces = [trace_neighbors_centers, trace_neighbors_raw, trace_center, trace_neighbors_unknown, trace_end_point]
    layout = go.Layout(
    )
    plotly.offline.plot({
            "data": traces,
            "layout": layout,        
            },
            filename= 'plots/3d_neighbors.html')
Exemplo n.º 6
0
means_k.fit(X)
labels = means_k.labels_
centroids = means_k.cluster_centers_


# In[125]:


#Create a 3d plot to view the data sepparation made by Kmeans
trace1 = go.Scatter3d(
    x= X['Spending Score (1-100)'],
    y= X['Annual Income (k$)'],
    z= X['Age'],
    mode='markers',
     marker=dict(
        color = labels, 
        size= 10,
        line=dict(
            color= labels,
        ),
        opacity = 0.9
     )
)
layout = go.Layout(
    title= 'Clusters',
    scene = dict(
            xaxis = dict(title  = 'Spending_score'),
            yaxis = dict(title  = 'Annual_income'),
            zaxis = dict(title  = 'Age')
        )
)
fig = go.Figure(data=trace1, layout=layout)
Exemplo n.º 7
0
import plotly.offline as offline
import pdb

X = empty((0, 3), float)
for i in range(0, 180):
    phi = deg2rad(i)
    theta = deg2rad(20 * i)
    x = array([cos(theta), sin(theta), phi])
    X = vstack([X, x])

# pdb.set_trace()

trace = go.Scatter3d(x=X[:, 0],
                     y=X[:, 1],
                     z=X[:, 2],
                     marker=dict(
                         size=2,
                         color='#DC143C',
                         colorscale='Viridis',
                     ))
# trace2 = go.Scatter3d(
#     x=wave2[:,0], y=wave2[:,1], z=wave2[:,2],
#     marker=dict(
#         size=2,
#         color='#32CD32',
#         colorscale='Viridis',
#     )
# )
# data = [trace,trace2]
data = [trace]
layout = dict(
    title='Iris dataset',
Exemplo n.º 8
0
def draw3Dplotly(x_name, y_name, z_name, algorithm, output_type = 'div'):      
    tags = pickle.loads(rds.get(tags_map[algorithm]))
    data = []
    for i in pd.Series(tags).sort_values().unique():
        ww = tags == i
        ddd = df[ww]
        xyz_info = ('<br>此產品'
                    '<br>'+ to_chinese[x_name] +': ' + ddd[x_name].astype(str) +
                    '<br>'+ to_chinese[y_name] +': ' + ddd[y_name].astype(str) +
                    '<br>'+ to_chinese[z_name] +': ' + ddd[z_name].astype(str)    )
        if ((algorithm == 'DBSCAN') & (i == -1))|((algorithm == 'WaveCluster') & (i == 0)):
            trace = go.Scatter3d(
                name = '噪音點',    
                x = ddd[x_name],
                y = ddd[y_name],
                z = ddd[z_name],
                mode = 'markers',
                hoverinfo = 'text',
                text = ddd.index + '<br>是噪音點,總共 {} 個'.format(len(ddd)) + xyz_info,
                marker=dict(
                    size = 5,
                    opacity=0.5
                )
            )
            data.append(trace)
        
        else:
            trace = go.Scatter3d(
                name = '群 {}'.format(i),    
                x = ddd[x_name],
                y = ddd[y_name],
                z = ddd[z_name],
                mode = 'markers',
                hoverinfo = 'text',
                text = ddd.index + '<br>位於第 {} 群<br>群的大小: {}'.format(i, len(ddd)) + xyz_info,
                marker=dict(
                    size = 5,
                    opacity=0.5
                )
            )
            data.append(trace)
        
    layout= go.Layout(
			height=720,
#        title= '{} 與 {} 與 {} 之分布'.format(to_chinese[x_name], to_chinese[y_name], to_chinese[z_name]),
#        titlefont=dict(
#            size=30
#        ),
        margin=dict(
            l=0,
            r=0,
            b=0,
            t=50
        ),    
        scene=dict(        
            xaxis=dict(
                title= to_chinese[x_name],
                titlefont=dict(
                    size=18
                ),
                range=[0, df[x_name].max()]
            ),
            yaxis=dict(
                title= to_chinese[y_name],
                titlefont=dict(
                    size=18
                ),
                range=[0, df[y_name].max()]
            ),
            zaxis=dict(
                title= to_chinese[z_name],
                titlefont=dict(
                    size=18
                ),
                range=[0, df[z_name].max()]
            )     
        )
    )
    
    fig= {'data':data,'layout':layout}
    div = plot(fig, output_type = output_type, config=conf)
    return(div)
Exemplo n.º 9
0
def generate_comparison_plot(plot_data, data_keys, field_names=None):
    print(field_names)
    if field_names is None:
        field_names = data_keys
    ptitle = 'MegaQC Comparison Plot'
    plot_x = []
    plot_y = []
    plot_z = []
    plot_col = []
    plot_size = []
    plot_names = []
    annotations = go.Annotations([])
    for s_name in plot_data:
        plot_names.append(s_name)
        try:
            plot_x.append(plot_data[s_name][data_keys['x']])
            plot_y.append(plot_data[s_name][data_keys['y']])
        except KeyError:
            print("Couldn't find key {} (available: {})".format(plot_data[s_name].keys(), data_keys))
        try:
            plot_z.append(plot_data[s_name][data_keys['z']])
        except KeyError:
            plot_z.append(None)
        try:
            plot_col.append(plot_data[s_name][data_keys['col']])
        except KeyError:
            plot_col.append(None)
        try:
            plot_size.append(plot_data[s_name][data_keys['size']])
        except KeyError:
            plot_size.append(None)

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

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

    plot_height = 600
    if all([x == None for x in plot_z]):
        fig = go.Scatter(
            x = plot_x,
            y = plot_y,
            mode = 'markers',
            marker = markers,
            text = plot_names
        )
    else:
        markers.update({'opacity':0.8})
        fig = go.Scatter3d(
            x = plot_x,
            y = plot_y,
            z = plot_z,
            mode = 'markers',
            marker = markers,
            text = plot_names
        )
        plot_height = 800
    # Make the plot
    layout = go.Layout(
        title = ptitle,
        # For 2D plots
        xaxis = dict(
            title = field_names['x']
        ),
        yaxis = dict(
            title = field_names['y']
        ),
        # For 3D plots
        scene = dict(
            xaxis = dict(
                title = field_names['x']
            ),
            yaxis = dict(
                title = field_names['y']
            ),
            zaxis = dict(
                title = field_names['z']
            ),
        ),
        annotations = annotations,
        height = plot_height
    )
    plot_div = py.plot(
        go.Figure(data = [fig], layout = layout),
        output_type = 'div',
        show_link = False,
        config = dict(
            modeBarButtonsToRemove = [
                'sendDataToCloud',
                'resetScale2d',
                'hoverClosestCartesian',
                'hoverCompareCartesian',
                'toggleSpikelines'
            ],
            displaylogo = False
        )
    )
    return plot_div
Exemplo n.º 10
0
def display(m, wireframe=True, smooth=True, data=None):
    """ The display function shows an interactive presentation of the Manifold, m, inside
        a Jupyter Notebook. wireframe=True means that a wireframe view of the mesh is
        superimposed on the 3D model. If smooth=True, the mesh is rendered with vertex
        normals. Otherwise, the mesh is rendered with face normals. If data=None, the
        mesh is shown in a light grey color. If data contains an array of scalar values
        per vertex, these are mapped to colors used to color the mesh. Finally, note that
        m can also be a Graph. In that case the display function just draws the edges as
        black lines. """
    mesh_data = []
    if isinstance(m, hmesh.Manifold):
        xyz = array([p for p in m.positions()])
        m_tri = hmesh.Manifold(m)
        hmesh.triangulate(m_tri)
        ijk = array([[idx for idx in m_tri.circulate_face(f, 'v')]
                     for f in m_tri.faces()])
        mesh = go.Mesh3d(x=xyz[:, 0],
                         y=xyz[:, 1],
                         z=xyz[:, 2],
                         i=ijk[:, 0],
                         j=ijk[:, 1],
                         k=ijk[:, 2],
                         color='#dddddd',
                         flatshading=not smooth)
        if data is not None:
            mesh['intensity'] = data
        mesh_data += [mesh]
        if wireframe:
            pos = m.positions()
            xyze = []
            for h in m.halfedges():
                if h < m.opposite_halfedge(h):
                    p0 = pos[m.incident_vertex(m.opposite_halfedge(h))]
                    p1 = pos[m.incident_vertex(h)]
                    xyze.append(array(p0))
                    xyze.append(array(p1))
                    xyze.append(array([None, None, None]))
            xyze = array(xyze)
            trace1 = go.Scatter3d(x=xyze[:, 0],
                                  y=xyze[:, 1],
                                  z=xyze[:, 2],
                                  mode='lines',
                                  line=dict(color='rgb(125,0,0)', width=1),
                                  hoverinfo='none')
            mesh_data += [trace1]
    elif isinstance(m, graph.Graph):
        pos = m.positions()
        xyze = []
        for v in m.nodes():
            for w in m.neighbors(v):
                if v < w:
                    p0 = pos[v]
                    p1 = pos[w]
                    xyze.append(array(p0))
                    xyze.append(array(p1))
                    xyze.append(array([None, None, None]))
        xyze = array(xyze)
        trace1 = go.Scatter3d(x=xyze[:, 0],
                              y=xyze[:, 1],
                              z=xyze[:, 2],
                              mode='lines',
                              line=dict(color='rgb(0,0,0)', width=1),
                              hoverinfo='none')
        mesh_data += [trace1]

    lyt = go.Layout(width=850, height=800)
    lyt.scene.aspectmode = "data"
    if EXPORT_MODE:
        py.iplot(dict(data=mesh_data, layout=lyt))
    else:
        return go.FigureWidget(mesh_data, lyt)
Exemplo n.º 11
0
from plotly.offline import init_notebook_mode
import plotly.graph_objs as go
plotly.offline.init_notebook_mode(connected=True)


# In[29]:

x, y, z = np.random.multivariate_normal(np.array([0,0,0]), np.eye(3), 200).transpose()
trace0 = go.Scatter3d(
    x=np.array(t1_0),
    y=np.array(t2_0),
    z=np.array(t3_0),
    #colors = np.array(labels),
    mode='markers',
    marker=dict(
        size=12,
        #color='rgb(255, 0, 255)',
        line=dict(
            color='rgba(217, 217, 217, 0.14)',
            width=0.1
        ),
        opacity=0.7
    )
)
trace1 = go.Scatter3d(
    x=np.array(t1_1),
    y=np.array(t2_1),
    z=np.array(t3_1),
    mode='markers',
    marker=dict(
        size=12,
        color='rgb(127, 127, 127)',
Exemplo n.º 12
0
Arquivo: Plot.py Projeto: Deep-MI/LaPy
def plot_tria_mesh(tria, vfunc=None, plot_edges=None, plot_levels=False, tfunc=None,
                   edge_color='rgb(50,50,50)', tic_color='rgb(50,200,10)', html_output=False,
                   width=800, height=800, flatshading=False,
                   xrange=None, yrange=None, zrange=None, 
                   showcaxis=False, caxis=None):
    # interesting example codes:
    # https://plot.ly/~empet/14749/mesh3d-with-intensities-and-flatshading/#/
    #
    if type(tria).__name__ != "TriaMesh":
        raise ValueError('plot_tria_mesh works only on TriaMesh class')

    x, y, z = zip(*tria.v)
    i, j, k = zip(*tria.t)

    vlines = []
    if vfunc is None:
        if tfunc is None:
            triangles = go.Mesh3d(x=x, y=y, z=z, i=i, j=j, k=k, flatshading=flatshading)
        elif tfunc.ndim == 1 or (tfunc.ndim == 2 and np.min(tfunc.shape) == 1):
            # scalar tfunc
            min_fcol = np.min(tfunc)
            max_fcol = np.max(tfunc)
            # special treatment for constant functions
            if np.abs(min_fcol - max_fcol) < 0.0001:
                if np.abs(max_fcol) > 0.0001:
                    min_fcol = -np.abs(min_fcol)
                    max_fcol = np.abs(max_fcol)
                else:  # both are zero
                    min_fcol = -1
                    max_fcol = 1
            # if min_fcol >= 0 and max_fcol <= 1:
            #    min_fcol = 0
            #    max_fcol = 1
            # colormap = cm.RdBu
            colormap = _get_colorscale(min_fcol, max_fcol)
            facecolor = [_map_z2color(zz, colormap, min_fcol, max_fcol) for zz in tfunc]
            # for tria colors overwrite flatshading to be true:
            triangles = go.Mesh3d(x=x, y=y, z=z, i=i, j=j, k=k,
                                  facecolor=facecolor,
                                  flatshading=True)
        elif tfunc.ndim == 2 and np.min(tfunc.shape) == 3:
            # vector tfunc
            s = 0.7 * tria.avg_edge_length()
            centroids = (1.0/3.0) * (tria.v[tria.t[:, 0], :] + tria.v[tria.t[:, 1], :]
                                     + tria.v[tria.t[:, 2], :])
            xv = np.column_stack(
                (centroids[:, 0], centroids[:, 0] + s * tfunc[:, 0],
                 np.full(tria.t.shape[0], np.nan))).reshape(-1)
            yv = np.column_stack(
                (centroids[:, 1], centroids[:, 1] + s * tfunc[:, 1],
                 np.full(tria.t.shape[0], np.nan))).reshape(-1)
            zv = np.column_stack(
                (centroids[:, 2], centroids[:, 2] + s * tfunc[:, 2],
                 np.full(tria.t.shape[0], np.nan))).reshape(-1)
            vlines = go.Scatter3d(x=xv, y=yv, z=zv, mode='lines',
                                  line=dict(color=tic_color, width=2))
            triangles = go.Mesh3d(x=x, y=y, z=z, i=i, j=j, k=k, flatshading=flatshading)
        else:
            raise ValueError('tfunc should be scalar (face color) or 3d for each triangle')

    elif vfunc.ndim == 1 or (vfunc.ndim == 2 and np.min(vfunc.shape) == 1):
        # scalar vfunc
        if plot_levels:
            colorscale = _get_color_levels()
        else: 
            colorscale = _get_colorscale(min(vfunc), max(vfunc))
            
        triangles = go.Mesh3d(x=x, y=y, z=z, i=i, j=j, k=k,
                              intensity=vfunc,
                              colorscale=colorscale,
                              flatshading=flatshading)
    elif vfunc.ndim == 2 and np.min(vfunc.shape) == 3:
        # vector vfunc
        s = 0.7 * tria.avg_edge_length()
        xv = np.column_stack(
            (tria.v[:, 0], tria.v[:, 0] + s * vfunc[:, 0],
             np.full(tria.v.shape[0], np.nan))).reshape(-1)
        yv = np.column_stack(
            (tria.v[:, 1], tria.v[:, 1] + s * vfunc[:, 1],
             np.full(tria.v.shape[0], np.nan))).reshape(-1)
        zv = np.column_stack(
            (tria.v[:, 2], tria.v[:, 2] + s * vfunc[:, 2],
             np.full(tria.v.shape[0], np.nan))).reshape(-1)
        vlines = go.Scatter3d(x=xv, y=yv, z=zv, mode='lines', line=dict(color=tic_color, width=2))
        triangles = go.Mesh3d(x=x, y=y, z=z, i=i, j=j, k=k, flatshading=flatshading)
    else:
        raise ValueError('vfunc should be scalar or 3d for each vertex')

    if plot_edges:
        # 4 points = three edges for each tria, nan to separate triangles
        # this plots every edge twice (except boundary edges)
        xe = np.column_stack(
            (tria.v[tria.t[:, 0], 0], tria.v[tria.t[:, 1], 0], tria.v[tria.t[:, 2], 0],
             tria.v[tria.t[:, 0], 0], np.full(tria.t.shape[0], np.nan))).reshape(-1)
        ye = np.column_stack(
            (tria.v[tria.t[:, 0], 1], tria.v[tria.t[:, 1], 1], tria.v[tria.t[:, 2], 1],
             tria.v[tria.t[:, 0], 1], np.full(tria.t.shape[0], np.nan))).reshape(-1)
        ze = np.column_stack(
            (tria.v[tria.t[:, 0], 2], tria.v[tria.t[:, 1], 2], tria.v[tria.t[:, 2], 2],
             tria.v[tria.t[:, 0], 2], np.full(tria.t.shape[0], np.nan))).reshape(-1)

        # define the lines to be plotted
        lines = go.Scatter3d(x=xe,
                             y=ye,
                             z=ze,
                             mode='lines',
                             line=dict(color=edge_color, width=1.5)
                             )

        data = [triangles, lines]

    else:
        data = [triangles]

    if vlines:
        data.append(vlines)

    # line_marker = dict(color='#0066FF', width=2)

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

    layout = go.Layout(
        width=width,
        height=height,
        scene=dict(
            xaxis=noaxis,
            yaxis=noaxis,
            zaxis=noaxis
            )
        )

    if xrange is not None:
        layout.scene.xaxis.update(range=xrange)
    if yrange is not None:
        layout.scene.yaxis.update(range=yrange)
    if zrange is not None:
        layout.scene.zaxis.update(range=zrange)

    data[0].update(showscale=showcaxis)

    if caxis is not None:
        data[0].update(cmin=caxis[0])
        data[0].update(cmax=caxis[1])

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

    if not html_output:
        plotly.offline.iplot(fig)
    else:
        plotly.offline.plot(fig)
Exemplo n.º 13
0
def plot(df, title, colorscale="blues", color_on="speed"):
    """
    Args:
        df and title    trivial
        colorscale      `str`: the scale to use for coloring
        color_one       `str`: one of "density", "time", "speed" - select
                        the dimension getting colored

    Upon success a plotly-JSON-encoded graph is returned.
    """
    # prepare bins
    try:
        bin_max = int(df.Download.max()) + 2
    except ValueError:  # the case of only Nan
        bin_max = 2

    # prepare grouping by a given time unit
    df["Time"] = [
        ts.replace(hour=0, minute=0, second=0, microsecond=0)
        for ts in df["tslocal"]
    ]

    # append bins for Download values
    df["Speed"] = pd.cut(
        x=df.Download,
        bins=range(bin_max),
        labels=range(bin_max - 1),
        right=False,
    )

    # append a density column to aggregate
    df["Density"] = 1

    # cleanup before grouping
    d3 = df.drop(["Download", "Upload"], axis=1)

    # GroupBy with dimensions time, speed, density
    g3 = d3.groupby(by=[d3.Time, d3.Speed], as_index=False).sum()

    # make the graph
    fig = go.Figure()

    fig.add_trace(
        go.Scatter3d(
            x=[0] * len(df.Density),
            y=df.tslocal,
            z=df.Download,
            name="rug",
            mode="markers",
            marker=dict(size=2),
        ))

    # default marker, accentuating speed in blues
    marker = dict(size=6, opacity=0.9, color=g3.Speed)
    marker["colorscale"] = colorscale

    if color_on == "density":
        marker["color"] = g3.Density
    elif color_on == "time":
        marker["color"] = g3.Time - g3.Time.min()
    # else the above default is used

    fig.add_trace(
        go.Scatter3d(
            x=g3.Density,
            y=g3.Time,
            z=g3.Speed,
            name="density",
            mode="markers",
            marker=marker,
        ))

    # TODO Graph width/ height: plotly only accepts px values so far,
    #      so set these with care and watch out for API improvements.
    fig.layout = dict(
        title_text=title,
        yaxis=go.layout.YAxis(type="date"),
        scene_camera_eye=dict(x=0.6, y=2, z=-0.1),
        scene=dict(
            xaxis_title="density (no unit)",
            yaxis_title="time",
            zaxis_title="speed (Mbit/s)",
        ),
        height=768,
    )

    return json.dumps(fig, cls=plotly.utils.PlotlyJSONEncoder)
Exemplo n.º 14
0
def create_image(
    request,
    numb,
):  # Representación Nube de puntos.

    document = Document.objects.filter(user=request.user).get(numb=numb)
    filename = document.document.path
    nombre = document.Nombre

    pcd = read_point_cloud(filename)
    pcd_array = (np.asarray(pcd.points))

    x = []
    y = []
    z = []

    for row in pcd_array:
        x.append(float(row[0]))
        y.append(float(row[1]))
        z.append(float(row[2]))

    trace1 = go.Scatter3d(
        x=x,
        y=y,
        z=z,
        mode='markers',
        marker=dict(
            size=0.75,
            color=z,
            colorscale='Earth',
            symbol='circle',
        ),
    )

    data = [trace1]
    layout = go.Layout(
        paper_bgcolor='rgba(40,57,90,.8)',

        #paper_bgcolor='rgba(40,57,90,.9)',
        scene=dict(
            xaxis=dict(
                showline=False,
                showticklabels=False,
                showgrid=False,
                zeroline=False,
                range=[-125, 125],
            ),
            yaxis=dict(
                showline=False,
                showticklabels=False,
                showgrid=False,
                zeroline=False,
                range=[-125, 125],
            ),
            zaxis=dict(
                showline=False,
                showticklabels=False,
                showgrid=False,
                zeroline=False,
                range=[-125, 125],
            ),
        ),
        width=950,
        height=750,
        showlegend=False)

    fig = go.Figure(
        data=data,
        layout=layout,
    )
    plot_div = plot(fig, output_type='div', include_plotlyjs=False)
    form = DescriptionForm(request.POST or None)

    if request.method == 'POST':
        message = get_object_or_404(Document, numb=numb)
        form = DescriptionForm(request.POST, instance=message)
        if form.is_valid():
            form.save()

    context = {
        "form": form,
        "plot": plot_div,
        "filename": nombre,
        "Document": document,
    }
    return render(request, 'Document/plot.html', context)
def user_scatter_plot(user_features):

    job_groups = get_job_groups()
    # Scatter latitude (x), longitude(y), and Age(z), and color code by occupation
    lat = np.array(
        [fhf.get_zip_data(z, 'latitude') for z in user_features.zip_code])
    lon = np.array(
        [fhf.get_zip_data(z, 'longitude') for z in user_features.zip_code])
    x = lat / 90
    y = lon / 180
    z = np.array(user_features.normed_age *
                 130)  #Age was normalized by 130, un-normalize for plot

    # Generate a list of colors for plotting, one for each job group
    color_list = fhf.get_N_HexCol(len(job_groups.keys()), 0.6, 0.75)

    # Generate the employment bins, and for each one, generate its scatter plot data
    traces = []
    for ei, e in enumerate(job_groups.keys()):

        # Generate the employment bins
        e_idx = []
        for jobs in e:
            e_idx.extend(np.where(user_features[jobs] == 1)[0].tolist())
        e_idx = np.array(e_idx)

        # Generate the scatter data for each bin, and append it to the 'traces' list
        # which we will then plot
        x_i = x[e_idx]
        y_i = y[e_idx]
        z_i = z[e_idx]
        traces.append(
            go.Scatter3d(x=x_i,
                         y=y_i,
                         z=z_i,
                         name=job_groups[e],
                         mode='markers',
                         marker=dict(color=color_list[ei],
                                     size=5,
                                     line=dict(color='rgba(58, 71, 80, 1.0)',
                                               width=0.5),
                                     opacity=0.8)))

    # Set up the 3d plot's camera position
    camera = dict(
        #up=dict(x=0, y=0, z=1),
        #center=dict(x=0, y=0, z=0),
        eye=dict(x=1.35, y=1.35, z=0.65))

    # Set up the plot's layout
    layout = dict(
        #height = 650,
        height=500,
        width=780,
        #width = 900,
        scene=dict(camera=camera,
                   xaxis=dict(title='Latitude', ),
                   yaxis=dict(title='Longitude', ),
                   zaxis=dict(title='Age', )),
        margin=dict(
            l=30,
            r=30,
            t=30,
            b=30,
        ),
    )

    # Get plotting traces and layout
    return traces, layout
Exemplo n.º 16
0
def scat3d(dfxyz=None,
           xyzcols=None,
           tagcol=None,
           title='plot',
           height=800,
           width=800):
    """
    plotly 3D scatter plot
    
    ARGUMENTS:
        dfxyz   dataframe with x,y,z coordinates
        colxyz  column names for x,y,z in the dataframe
      
    TODO: add group tags(for the legend) and neuronID tags(hoverinfo) to the dataframe
    """

    tag = 'tag'
    tfont = dict(family='Courier New, monospace', size=18, color='#3f3f3f')

    lblX = xyzcols[0]
    lblY = xyzcols[1]
    lblZ = xyzcols[2]

    autosize = False
    opacity = 1  #0.99    # opacity=1 required for true 3d rendering! (but opacity=1 breaks hoverinfo)
    lw = 2

    #ad_marker=dict(size=4,color='grey',opacity=0.3)
    margin = dict(l=10, r=100, b=10, t=50)

    pltdata = []
    if tagcol is not None:
        for tag in dfxyz[tagcol].unique():
            dfi = dfxyz[dfxyz[tagcol] == tag]
            pltfmt = dict(line=dict(width=lw),
                          opacity=opacity,
                          mode='markers',
                          name=tag)
            pltfmt['legendgroup'] = tag
            pltfmt['hoverinfo'] = 'none'
            pltfmt['marker'] = dict(size=5,
                                    line=dict(color='rgb(110, 110, 110)',
                                              width=1))
            #pltfmt['showlegend'] = True #if i==0 else False
            pltdata.append(
                go.Scatter3d(x=dfi[lblX], y=dfi[lblY], z=dfi[lblZ], **pltfmt))
    else:
        tag = 'tag'
        pltfmt = dict(line=dict(width=lw),
                      opacity=opacity,
                      mode='markers',
                      name=tag)
        pltfmt['legendgroup'] = tag
        pltfmt['hoverinfo'] = 'none'
        #pltfmt['showlegend'] = True #if i==0 else False
        pltdata = [
            go.Scatter3d(x=dfxyz[lblX], y=dfxyz[lblY], z=dfxyz[lblZ], **pltfmt)
        ]

    #== camera and layout
    #camera_xy = dict(
    #up=dict(x=0, y=0, z=1),
    #center=dict(x=0, y=0, z=0),
    #eye=dict(x=0.0, y=-0.0, z=2.5)
    #)

    xaxis = dict(title=lblX, titlefont=tfont)
    yaxis = dict(title=lblY, titlefont=tfont)
    zaxis = dict(title=lblZ, titlefont=tfont)

    layout = go.Layout(
        title=title,
        scene=dict(
            xaxis=xaxis,
            yaxis=yaxis,
            zaxis=zaxis,
            aspectmode='data',
            #camera=camera_xy,
        ),
        showlegend=True,
        autosize=autosize,
        width=width,
        height=height,
        margin=margin,
        #updatemenus=list([ dict( x=0.55, y=1, yanchor='top',  buttons=butlst, ) ]),
    )
    fig3d = go.Figure(data=pltdata, layout=layout)

    return fig3d
Exemplo n.º 17
0
def graph():
    nodes, links = get_graph_db()
    N = len(nodes)
    L = len(links)
    Edges = [(links[k]['source'], links[k]['target']) for k in range(L)]
    # Graph
    G = ig.Graph(Edges, directed=True)
    # Getting name and group
    labels = []
    group = []
    for node in nodes:
        labels.append(node['name'])
        group.append(node['group'])
    # Layout style
    layt = G.layout_fruchterman_reingold(grid=True, dim=3)
    # Getting coordinates
    Xn = [layt[k][0] for k in range(N)]  # x-coordinates of nodes
    Yn = [layt[k][1] for k in range(N)]  # y-coordinates
    Zn = [layt[k][2] for k in range(N)]  # z-coordinates
    Xe = []
    Ye = []
    Ze = []
    for e in Edges:
        Xe += [layt[e[0]][0], layt[e[1]][0], None]
        Ye += [layt[e[0]][1], layt[e[1]][1], None]
        Ze += [layt[e[0]][2], layt[e[1]][2], None]
    # Traces
    trace1 = go.Scatter3d(
        x=Xe,
        y=Ye,
        z=Ze,
        mode='lines',
        line=dict(color='rgb(125,125,125)', width=1),
        hoverinfo='skip',
    )
    trace2 = go.Scatter3d(x=Xn,
                          y=Yn,
                          z=Zn,
                          mode='markers',
                          name='actors',
                          opacity=1,
                          marker=dict(symbol='circle',
                                      size=4,
                                      color=group,
                                      colorscale='haline',
                                      line=dict(color='rgb(50,50,50)',
                                                width=0.5)),
                          text=labels,
                          hoverinfo='text')
    # Layout figure
    axis = dict(showbackground=False,
                showline=False,
                zeroline=False,
                showgrid=False,
                showticklabels=False,
                title='')
    layout = go.Layout(title="Authors",
                       showlegend=False,
                       scene=dict(
                           xaxis=dict(axis),
                           yaxis=dict(axis),
                           zaxis=dict(axis),
                       ),
                       margin=dict(l=0, r=0, t=0, b=0),
                       hovermode='closest',
                       hoverlabel=dict(bgcolor='white'))
    data = [trace1, trace2]
    fig = go.Figure(data=data, layout=layout)
    fig.update_layout(title={
        'y': 0.9,
        'x': 0.5,
        'xanchor': 'center',
        'yanchor': 'top'
    },
                      autosize=True)

    config = {'responsive': True, "displaylogo": False, \
        'modeBarButtonsToRemove': ['pan3d', 'lasso3d', 'zoom3d', 'orbitRotation', 'tableRotation', \
            'resetCameraDefault3d', 'hoverClosest3d', 'resetCameraLastSave3d']}
    graph = dcc.Graph(figure=fig,
                      id="graph",
                      config=config,
                      style={
                          "height": "92vh",
                          "width": "100%"
                      })
    return graph
###### Plotly Graph #######

## Making a plotly graph to show category separation from only top three features

#Top three features as shown by random forest
a = train_original["concave.points_mean"]
b = train_original["perimeter_worst"]
c = train_original["texture_worst"]
 
#Importing packages and setting credentials
import plotly
import plotly.plotly as py
import plotly.graph_objs as go
plotly.tools.set_credentials_file(username='******', api_key='CueWpz8W4OROnmTR9mko')

#Mapping colors onto original training data
size_mapping = {'B': "blue",'M': "red"}
train_original['color'] = train_original['diagnosis'].map(size_mapping)

#Create the 3D scatter plot
trace1 = go.Scatter3d(x=a, y=b, z=c, mode='markers', marker=dict(size=4, color=train_original["color"], opacity=0.8))

#Set the layout
data = [trace1]
layout = go.Layout(title='Plot Title', xaxis=dict(title='Growth Mean Radius'), yaxis=dict(title='Growth Mean Texture'))

#Plot the figure to my plotly account
fig = go.Figure(data=data, layout=layout)
py.iplot(fig, filename='3d-scatter-tumor')

Exemplo n.º 19
0
grid_y = 2*np.sin(2*grid_ang*np.pi/16)
points = np.array(list(zip(grid_x, grid_y, grid_z)))

# プロットのためにxyzごとにリストに分解
xs = points[:,0]
ys = points[:,1]
zs = points[:,2]

data = []
# マーカー
data.append(go.Scatter3d(
        x=xs,
        y=ys,
        z=zs,
        mode='markers',
        marker=dict(
            color='rgb(100,100,200)',
            size=2,
            opacity=0.8
        )
    ))
# 線分
for x, y, z in points:
    data.append(go.Scatter3d(
            x=[x, x+x/2],
            y=[y, y+y/2],
            z=[z, z],
            mode='lines',
            marker=dict(
                color='rgb(100,100,200)',
                size=5,
Exemplo n.º 20
0
def Plot_3D(df_meta_input=None, n_comps=10, dim=3):
    try:
        df_QC_report = pd.read_csv('QC_report.csv', index_col=0)

        # Import metadata; if not provided only QC will be used
        try:
            df_meta_plot = pd.read_csv(df_meta_input, index_col=0)
            df_meta_plot = df_meta_plot.select_dtypes('object').merge(
                df_QC_report[['recommendation', 'Outlier']],
                left_index=True,
                right_index=True)
            df_meta_plot = df_meta_plot.astype(str)

        except FileNotFoundError:
            print('No metadata file found')
            df_meta_plot = df_QC_report[['recommendation', 'Outlier']]

        df_plot = pd.read_csv('./counts/TPM_counts.csv',
                              index_col=0)[df_meta_plot.index]

        # Compute PCA results for plotiing
        data_array = df_plot.apply(lambda x: np.log2(x + 1)).values.T
        pca = PCA(n_components=min(
            n_comps,
            len(df_QC_report) - 1))  # PC number can not exceed sample size -1
        PCA_result = pca.fit_transform(data_array)
        Ratio = pca.explained_variance_ratio_
        mu = PCA_result.mean(axis=0)
        PCA_result = PCA_result - mu
        X = [xx[0] for xx in PCA_result]
        Y = [xx[1] for xx in PCA_result]
        Z = [xx[2] for xx in PCA_result]

        # generate PCA - QC correlation matrix heatmap here
        df_PCA = pd.DataFrame(
            PCA_result,
            columns=[f'PC_{i+1}' for i in range(PCA_result.shape[1])],
            index=df_QC_report.index)
        corr_matrix = np.zeros(
            [int(df_QC_report.shape[1] - 3),
             int(df_PCA.shape[1])])
        p_matrix = np.zeros(
            [int(df_QC_report.shape[1] - 3),
             int(df_PCA.shape[1])])
        for i, col in enumerate(df_QC_report.columns[:-3]):
            for j, PC in enumerate(df_PCA.columns):
                corr_matrix[i, j] = spearmanr(df_QC_report[col].values,
                                              df_PCA[PC].values).correlation
                p_matrix[i, j] = spearmanr(df_QC_report[col].values,
                                           df_PCA[PC].values).pvalue
        _, ax = plt.subplots(figsize=(25, 6))
        sns.heatmap(pd.DataFrame(
            p_matrix, index=df_QC_report.columns[:-3],
            columns=df_PCA.columns).apply(lambda x: -np.log10(x)).T,
                    cmap='GnBu')
        for j in range(len(pd.DataFrame(corr_matrix).T.columns)):
            for i in range(len(pd.DataFrame(corr_matrix).T.index)):
                text = ax.text(j + 0.5,
                               i + 0.5,
                               pd.DataFrame(corr_matrix).T.applymap(
                                   lambda x: '%.2f' % x).values[i, j],
                               ha="center",
                               va="center",
                               color="red")
        plt.savefig(f'QC_plots/PCA_QC_correlation.png',
                    format='png',
                    bbox_inches='tight')

        # Back to plot 3D funciton
        df_PCA = pd.DataFrame({
            'sample': df_meta_plot.index.values,
            'X': X,
            'Y': Y,
            'Z': Z
        }).set_index('sample')
        group_factors = df_meta_plot.columns

        scatter_data = []
        Scatter_data_length = []
        if dim == 3:
            for select_factor in group_factors:
                trace = []
                for N in sorted(set(df_meta_plot[select_factor])):
                    trace.append(
                        go.Scatter3d(
                            x=df_PCA[df_meta_plot[select_factor] ==
                                     N]['X'].values,
                            y=df_PCA[df_meta_plot[select_factor] ==
                                     N]['Y'].values,
                            z=df_PCA[df_meta_plot[select_factor] ==
                                     N]['Z'].values,
                            mode='markers',
                            name=N,
                            text=df_meta_plot[df_meta_plot[select_factor] ==
                                              N].index.values,
                            hoverinfo='text',
                            marker=dict(size=4,
                                        line=dict(width=0.5),
                                        opacity=0.8)))

                scatter_data += trace
                Scatter_data_length.append(len(trace))

        if dim == 2:
            for select_factor in group_factors:
                trace = []
                for N in sorted(set(df_meta_plot[select_factor])):
                    trace.append(
                        go.Scatter(
                            x=df_PCA[df_meta_plot[select_factor] ==
                                     N]['X'].values,
                            y=df_PCA[df_meta_plot[select_factor] ==
                                     N]['Y'].values,
                            mode='markers',
                            name=N,
                            text=df_meta_plot[df_meta_plot[select_factor] ==
                                              N].index.values,
                            hoverinfo='text',
                            marker=dict(size=4,
                                        line=dict(width=0.5),
                                        opacity=0.8)))

                scatter_data += trace
                Scatter_data_length.append(len(trace))

        Vis_table = []
        for i in np.arange(len(Scatter_data_length)):
            Vis_list = []
            for j, elements in enumerate(Scatter_data_length):
                if j == i:
                    Vis_list.append([True] * elements)
                else:
                    Vis_list.append([False] * elements)
            Vis_table.append(
                [item for sublist in Vis_list for item in sublist])
        dict_Vis = dict(zip(group_factors, Vis_table))

        updatemenus = [
            dict(active=0,
                 buttons=list([
                     dict(label=Group_factor,
                          method='update',
                          args=[{
                              'visible': dict_Vis[Group_factor] + [True]
                          }, {
                              'title': str(Group_factor)
                          }]) for Group_factor in group_factors
                 ]))
        ]

        layout = go.Layout(
            legend=dict(x=-0.15, y=0),
            scene=dict(
                xaxis=dict(title='PC1  ' + '%.2f' % Ratio[0],
                           titlefont=dict(family='Courier New, monospace',
                                          size=18,
                                          color='#7f7f7f')),
                yaxis=dict(title='PC2  ' + '%.2f' % Ratio[1],
                           titlefont=dict(family='Courier New, monospace',
                                          size=18,
                                          color='#7f7f7f')),
                zaxis=dict(title='PC3  ' + '%.2f' % Ratio[2],
                           titlefont=dict(family='Courier New, monospace',
                                          size=18,
                                          color='#7f7f7f')),
            ),
            updatemenus=updatemenus)
        fig = go.Figure(data=scatter_data, layout=layout)
        py.plot(fig, filename='check_QC_PCA.html', auto_open=False)

    except FileNotFoundError:
        print('Please generate QC report')
Exemplo n.º 21
0
    def get_score_plot(
        self,
        ordinate,
        abscissa,
        applicate,
        color_by_labels,
        include_db_index,
        label_by_labels,
        encircle_by,
        plot_centroid,
        plot_medoid,
        graph_only=False,
        theme=None
    ) -> Union[dcc.Graph, Tuple[dcc.Graph, Union[dash_table.DataTable,
                                                 html.Div]]]:
        is_3d = (applicate is not None) and (applicate != -1)
        theme = theme or 'plotly_white'
        color_by_labels = color_by_labels or []
        encircle_by = encircle_by or []
        label_by_labels = label_by_labels or []
        color_label = ','.join(color_by_labels) if color_by_labels else 'All'
        label_df = self._processed_label_df.reset_index()
        color_labels = label_df[color_by_labels].apply(lambda x: ','.join(x.apply(str)), axis=1) if color_by_labels \
            else pd.Series(['All' for _ in range(0, len(label_df))])
        color_names = list(color_labels.unique())
        color_indices = [
            label_df.index[color_labels == color_name]
            for color_name in color_names
        ]

        label_labels = label_df[label_by_labels].apply(lambda x: ','.join(x.apply(str)), axis=1) if label_by_labels \
            else None

        if len(color_names) > 1 and include_db_index:
            db_df = self._get_scores(color_label, color_labels, True)
            style_header = {
                'backgroundColor': '#303030'
            } if theme == 'plotly_dark' else {}
            style_cell = {
                'backgroundColor': '#444444'
            } if theme == 'plotly_dark' else {}
            table = dash_table.DataTable(columns=[{
                'name': val,
                'id': val
            } for val in db_df.columns],
                                         data=db_df.to_dict('rows'),
                                         style_as_list_view=True,
                                         style_header=style_header,
                                         style_cell=style_cell)
        else:
            table = html.Div()

        graph_data = [
            go.Scatter(  # dummy series to use as stand-in for legend title
                x=[0],
                y=[0],
                name=color_label,
                mode='markers',
                marker={
                    'opacity': 0,
                    'size': 0,
                    'color': 'rgba(0,0,0,0)'
                }) if not is_3d else go.Scatter3d(x=[0],
                                                  y=[0],
                                                  z=[0],
                                                  name=color_label,
                                                  mode='markers',
                                                  marker={
                                                      'opacity': 0,
                                                      'size': 0,
                                                      'color': 'rgba(0,0,0,0)'
                                                  })
        ]
        shapes = []
        annotations = []

        if len(color_indices) > len(
                DEFAULT_PLOTLY_COLORS):  # repeat default color list
            colors = []
            while len(colors) < len(color_indices):
                colors += DEFAULT_PLOTLY_COLORS
        else:
            colors = DEFAULT_PLOTLY_COLORS
        colors = colors[:len(color_indices)]

        for inds, name, color in zip(color_indices, color_names, colors):
            graph_data.append(
                go.Scatter(x=self._scores[inds, abscissa],
                           y=self._scores[inds, ordinate],
                           text=[
                               '<br>'.join([
                                   f'{label}: {label_df[label][ind]}'
                                   for label in label_df.columns
                               ]) for ind in inds
                           ],
                           name=name,
                           mode='markers',
                           marker={
                               'size': 15,
                               'opacity': 0.5,
                               'line': {
                                   'width': 0.5,
                                   'color': 'white'
                               },
                               'color': color
                           }) if not is_3d else go.
                Scatter3d(x=self._scores[inds, abscissa],
                          y=self._scores[inds, ordinate],
                          z=self._scores[inds, applicate],
                          text=[
                              '<br>'.join([
                                  f'{label}: {label_df[label][ind]}'
                                  for label in label_df.columns
                              ]) for ind in inds
                          ],
                          name=name,
                          mode='markers',
                          marker={
                              'size': 5,
                              'opacity': 0.5,
                              'line': {
                                  'width': 0.5,
                                  'color': 'white'
                              },
                              'color': color
                          }))
            if plot_centroid:
                graph_data.append(
                    go.Scatter(x=[np.mean(self._scores[inds, abscissa])],
                               y=[np.mean(self._scores[inds, ordinate])],
                               text=f'{name} centroid',
                               name=f'{name} centroid',
                               mode='markers',
                               marker={
                                   'size': 20,
                                   'opacity': 0.85,
                                   'line': {
                                       'width': 0.5,
                                       'color': 'white'
                                   },
                                   'color': color,
                                   'symbol': 'x'
                               }) if not is_3d else go.
                    Scatter3d(x=[np.mean(self._scores[inds, abscissa])],
                              y=[np.mean(self._scores[inds, ordinate])],
                              z=[np.mean(self._scores[inds, applicate])],
                              text=f'{name} centroid',
                              name=f'{name} centroid',
                              mode='markers',
                              marker={
                                  'size': 5,
                                  'opacity': 0.85,
                                  'line': {
                                      'width': 0.5,
                                      'color': 'white'
                                  },
                                  'color': color,
                                  'symbol': 'x'
                              }))
            if plot_medoid:
                graph_data.append(
                    go.Scatter(x=[np.median(self._scores[inds, abscissa])],
                               y=[np.median(self._scores[inds, ordinate])],
                               text=f'{name} medoid',
                               name=f'{name} medoid',
                               mode='markers',
                               marker={
                                   'size': 20,
                                   'opacity': 0.85,
                                   'line': {
                                       'width': 0.5,
                                       'color': 'white'
                                   },
                                   'color': color,
                                   'symbol': 'cross'
                               }) if not is_3d else go.
                    Scatter3d(x=[np.median(self._scores[inds, abscissa])],
                              y=[np.median(self._scores[inds, ordinate])],
                              z=[np.median(self._scores[inds, applicate])],
                              text=f'{name} medoid',
                              name=f'{name} medoid',
                              mode='markers',
                              marker={
                                  'size': 5,
                                  'opacity': 0.85,
                                  'line': {
                                      'width': 0.5,
                                      'color': 'white'
                                  },
                                  'color': color,
                                  'symbol': 'cross'
                              }))
            if label_labels is not None:
                annotations += [{
                    'x': self._scores[ind, abscissa],
                    'y': self._scores[ind, ordinate],
                    'xref': 'x',
                    'yref': 'y',
                    'text': label_labels[ind],
                    'showarrow': False
                } for ind in inds] if not is_3d else [
                    {
                        'x': self._scores[ind, abscissa],
                        'y': self._scores[ind, ordinate],
                        'xref': 'x',
                        'yref': 'y',
                        'text': label_labels[ind],
                        'showarrow': False
                    } for ind in inds
                ]
            if not is_3d:
                for metric in encircle_by:
                    if metric.endswith('std'):
                        x_std = np.std(self._scores[inds, abscissa], ddof=1)
                        y_std = np.std(self._scores[inds, ordinate], ddof=1)
                        x_mean = np.mean(self._scores[inds, abscissa])
                        y_mean = np.mean(self._scores[inds, ordinate])
                        x0 = x_mean - x_std * float(metric[0])
                        x1 = x_mean + x_std * float(metric[0])
                        y0 = y_mean - y_std * float(metric[0])
                        y1 = y_mean + y_std * float(metric[0])
                    elif metric.endswith('sem'):
                        x_sem = stats.sem(self._scores[inds, abscissa], ddof=1)
                        y_sem = stats.sem(self._scores[inds, ordinate], ddof=1)
                        x_mean = np.mean(self._scores[inds, abscissa])
                        y_mean = np.mean(self._scores[inds, ordinate])
                        x0 = x_mean - x_sem * float(metric[0])
                        x1 = x_mean + x_sem * float(metric[0])
                        y0 = y_mean - y_sem * float(metric[0])
                        y1 = y_mean + y_sem * float(metric[0])
                    elif metric == 'range':
                        x0 = np.min(self._scores[inds, abscissa])
                        x1 = np.max(self._scores[inds, abscissa])
                        y0 = np.min(self._scores[inds, ordinate])
                        y1 = np.max(self._scores[inds, ordinate])
                    elif metric == '95percentile':
                        x_ptile = np.percentile(self._scores[inds, abscissa],
                                                95)
                        y_ptile = np.percentile(self._scores[inds, ordinate],
                                                95)
                        x_median = np.median(self._scores[inds, abscissa])
                        y_median = np.median(self._scores[inds, ordinate])
                        x0 = x_median - x_ptile
                        x1 = x_median + x_ptile
                        y0 = y_median - y_ptile
                        y1 = y_median + y_ptile
                    elif metric.endswith('conf'):
                        conf = float(metric[:2]) / 100.0
                        n = len(self._scores[inds, abscissa])
                        x_mean = np.mean(self._scores[inds, abscissa])
                        y_mean = np.mean(self._scores[inds, ordinate])
                        x_h = stats.sem(self._scores[inds, abscissa],
                                        ddof=1) * stats.t.ppf(
                                            (1 + conf) / 2, n - 1)
                        y_h = stats.sem(self._scores[inds, ordinate],
                                        ddof=1) * stats.t.ppf(
                                            (1 + conf) / 2, n - 1)
                        x0 = x_mean - x_h
                        x1 = x_mean + x_h
                        y0 = y_mean - y_h
                        y1 = y_mean + y_h
                    else:
                        x0 = 0
                        x1 = 0
                        y0 = 0
                        y1 = 0

                    shapes.append({
                        'type': 'circle',
                        'xref': 'x',
                        'yref': 'y',
                        'x0': x0,
                        'y0': y0,
                        'x1': x1,
                        'y1': y1,
                        'opacity': 0.25,
                        'fillcolor': color,
                        'line': {
                            'color': color
                        }
                    })
        axis_line_style = {
            'zerolinecolor': '#375A7F',  # darkly primary
            'gridcolor': '#444444'  # darkly secondary
        } if theme == 'plotly_dark' else {
            'zerolinecolor': '#2C3E50',  # flatly primary
            'gridcolor': '#95A5A6'  # flatly secondary
        }
        layout = go.Layout(shapes=shapes,
                           annotations=annotations,
                           height=700,
                           font={'size': 16},
                           margin={
                               't': 25,
                               'l': 25,
                               'b': 25,
                               'r': 25
                           },
                           template=theme,
                           plot_bgcolor='rgba(0,0,0,0)',
                           paper_bgcolor='rgba(0,0,0,0)',
                           xaxis={
                               'title': f'PC{abscissa + 1}',
                               **axis_line_style
                           },
                           yaxis={
                               'title': f'PC{ordinate + 1}',
                               **axis_line_style
                           }) if not is_3d else go.Layout(
                               annotations=annotations,
                               height=700,
                               font={'size': 14},
                               margin={
                                   't': 25,
                                   'l': 25,
                                   'b': 25,
                                   'r': 25
                               },
                               template=theme,
                               plot_bgcolor='rgba(0,0,0,0)',
                               paper_bgcolor='rgba(0,0,0,0)',
                               scene={
                                   'xaxis': {
                                       'title': f'PC{abscissa + 1}',
                                       **axis_line_style
                                   },
                                   'yaxis': {
                                       'title': f'PC{ordinate + 1}',
                                       **axis_line_style
                                   },
                                   'zaxis': {
                                       'title': f'PC{applicate + 1}',
                                       **axis_line_style
                                   }
                               })
        graph = dcc.Graph(figure={'data': graph_data, 'layout': layout})
        if graph_only:
            return graph
        return graph, table
Exemplo n.º 22
0
 def __init__(self, x, y, z, mode='lines', **kwargs):
     super().__init__(data=go.Scatter3d(x=x, y=y, z=z, mode=mode, **kwargs))
def plot_pointVisualization_eurocRun1(X, Y, Z, X_A, Y_A, Z_A, prev_s_coord, current_s_coord, 
    start_coordinates, goal_point_coordinates, s_stream, run, open_X, open_Y, open_Z ):
    s_X = []
    s_Y = []
    s_Z = []
    for s in s_stream:
        s_X.append(s[0])
        s_Y.append(s[1])
        s_Z.append(s[2])
    trace_s_stream = go.Scatter3d(
        x=s_X,
        y=s_Y,
        z=s_Z,
        mode='markers',
        name = "S stream",
        marker=dict(
            size=12,
        )
    )
    trace_neighbors_raw = go.Scatter3d(
        x=X,
        y=Y,
        z=Z,
        mode='markers',
        name = "Raw neighbors",
        marker=dict(
            size=12,
        )
    )
    trace_neighbors_centers = go.Scatter3d(
        x=X_A,
        y=Y_A,
        z=Z_A,
        mode='markers',
        name = "Voxel centers",
        marker=dict(
            size=12,
        )
    )
    # trace_prev_s_neighbors_raw = go.Scatter3d(
    #     x=prev_s_X,
    #     y=prev_s_Y,
    #     z=prev_s_Z,
    #     mode='markers',
    #     name = "Previous s raw neighbors",
    #     marker=dict(
    #         size=12,
    #     )
    # )
    trace_open = go.Scatter3d(
        x=open_X,
        y=open_Y,
        z=open_Z,
        mode='markers',
        name = "Top nodes on open ",
        marker=dict(
            size=12,
        )
    )
    trace_prev_s = go.Scatter3d(
        x=(prev_s_coord[0], ),
        y=(prev_s_coord[1], ),
        z=(prev_s_coord[2], ) ,
        mode='markers',
        name = "Previous s",
        marker=dict(
            size=12,
        )
    )
    trace_current_s = go.Scatter3d(
        x=(current_s_coord[0], ),
        y=(current_s_coord[1], ),
        z=(current_s_coord[2], ) ,
        mode='markers',
        name = "Current s",
        marker=dict(
            size=12,
        )
    )
    # trace_target = go.Scatter3d(
    #     x=(correct_neighbor_coordinates['point'][0], correct_neighbor_coordinates['center'][0]),
    #     y=(correct_neighbor_coordinates['point'][1], correct_neighbor_coordinates['center'][1]),
    #     z=(correct_neighbor_coordinates['point'][2], correct_neighbor_coordinates['center'][2]) ,
    #     mode='markers',
    #     name = "Correct neighbor for path",
    #     marker=dict(
    #         size=12,
    #     )
    # )
    trace_start_center = go.Scatter3d(
        x=(start_coordinates[0], ),
        y=(start_coordinates[1], ),
        z=(start_coordinates[2], ) ,
        mode='markers',
        name = "Start center",
        marker=dict(
            size=12,
        )
    )
    #the distance takes into account the final point, not it's center
    trace_end_point = go.Scatter3d(
        x=(goal_point_coordinates[0], ),
        y=(goal_point_coordinates[1], ),
        z=(goal_point_coordinates[2], ) ,
        mode='markers',
        name = "End POINT",
        marker=dict(
            size=12,
        )
    )
    traces = [trace_s_stream, trace_start_center, trace_end_point, trace_open, trace_neighbors_raw, trace_neighbors_centers, trace_prev_s, trace_current_s]
    layout = go.Layout(
    )
    plotly.offline.plot({
            "data": traces,
            "layout": layout,        
            },
            filename= 'plots/voxelSizeDistribution_'+run+'.html')
                    zs.append(z)

print(pd.DataFrame({'x': xs, 'y': ys, 'z': zs}))

xs = np.array(xs)
ys = np.array(ys)
zs = np.array(zs)

plot = go.Scatter3d(x=xs,
                    y=ys,
                    z=zs,
                    mode='markers',
                    marker=dict(size=1,
                                # size=np.e ** np.sqrt(xs ** 2 + ys ** 2 + zs ** 2),
                                color=np.sqrt(xs ** 2 + ys ** 2 + zs ** 2),
                                # line=dict(
                                #     color='rgb(204, 204, 204)',
                                #     width=1
                                # ),
                                colorscale='Viridis',
                                ),
                    opacity=.7,
                    )

data = [plot]

layout = go.Layout(
    xaxis=go.layout.XAxis(
        title='x'
    ),
    yaxis=go.layout.YAxis(
Exemplo n.º 25
0
    def do_plot(self):
        max_lateral_distance = np.max(
            [self._plot_data['z_coord'], self._plot_data['cum_disp_dist']])
        true_north_seq = [x for x in range(int(max_lateral_distance))]

        # Assign colors by perforations
        perforation_flag = np.ndarray(shape=(self._processed_data.shape[0], ))
        flag = 1
        for x in range(self._processed_data.shape[0]):
            flag = 0
            depth = self._processed_data['MD'][x]
            for y in self._perf_meta_data[['Top', 'Btm']].copy().as_matrix():
                if depth >= y[0] and depth <= y[1]:
                    perforation_flag[x] = -1
                    flag = 1
                    continue
            if flag == 0: perforation_flag[x] = depth

        well_path = go.Scatter3d(x=self._plot_data['cum_disp_dist'],
                                 y=self._plot_data['z_coord'],
                                 z=self._plot_data['cum_tvd'],
                                 marker=dict(color=self._plot_data['cum_tvd'],
                                             size=6,
                                             symbol='circle',
                                             colorscale='Viridis',
                                             line=dict(color='#1f77b4',
                                                       width=.1),
                                             opacity=.2))

        perforations = go.Scatter3d(
            x=self._plot_data['cum_disp_dist'].iloc[np.where(
                perforation_flag == -1)[0]],
            y=self._plot_data['z_coord'].iloc[np.where(
                perforation_flag == -1)[0]],
            z=self._plot_data['cum_tvd'].iloc[np.where(
                perforation_flag == -1)[0]],
            mode='lines',
            line=dict(color='#FF4500', width=6))

        data = [well_path, perforations]

        layout = dict(
            title='Well Model',
            showlegend=False,
            margin=go.Margin(l=50, r=50, b=100, t=100, pad=4),
            scene=dict(xaxis=dict(title='Distance',
                                  gridcolor='rgb(255, 255, 255)',
                                  zerolinecolor='rgb(255, 255, 255)',
                                  showbackground=True,
                                  backgroundcolor='rgb(230, 230,230)'),
                       yaxis=dict(title='',
                                  gridcolor='rgb(255, 255, 255)',
                                  zerolinecolor='rgb(255, 255, 255)',
                                  showbackground=True,
                                  backgroundcolor='rgb(230, 230,230)',
                                  autorange=True,
                                  showgrid=False,
                                  zeroline=False,
                                  showline=False,
                                  ticks='',
                                  showticklabels=False),
                       zaxis=dict(title='Depth',
                                  gridcolor='rgb(255, 255, 255)',
                                  zerolinecolor='rgb(255, 255, 255)',
                                  showbackground=True,
                                  backgroundcolor='rgb(230, 230,230)'),
                       camera=dict(up=dict(x=1, y=1, z=1),
                                   center=dict(x=0, y=0, z=0),
                                   eye=dict(x=3, y=5.5, z=.75))),
        )

        fig = go.Figure(data=data, layout=layout)
        fig['layout'].update(scene=dict(aspectmode="data"))
        return fig
Exemplo n.º 26
0
Zn = [pos[k][2] for k in range(0, G.number_of_nodes())]  # y-coordinates

# populate node edges
Edges = G.edges()
Xe = []
Ye = []
Ze = []
for e in Edges:
    Xe += [pos[e[0]][0], pos[e[1]][0], None]  # x-coordinates of edge ends
    Ye += [pos[e[0]][1], pos[e[1]][1], None]
    Ze += [pos[e[0]][2], pos[e[1]][2], None]

# add trace for edges
edge_trace = go.Scatter3d(x=Xe,
                          y=Ye,
                          z=Ze,
                          line=go.Line(width=0.5, color='#888'),
                          hoverinfo='none',
                          mode='lines')

# add trace for members in the house
house_trace = go.Scatter3d(
    x=Xn[0:3],
    y=Yn[0:3],
    z=Zn[0:3],
    mode='markers',
    name='Member of the House',
    text=[
        'Name: Representative James P. McGovern',
        'Name: Representative Trent Franks',
        'Name: Represenatative Christopher H. Smith'
    ],
Exemplo n.º 27
0
def plot_interest(gen_sample, subsummary, RG_info, subset_col, Names, code= {},ref_keep= [0,1,2],
                                                    gp_names_dict= {}, ID_col= 'IRIS_ID',
                                                    include= [], Sn= 400, Sm= 5000, gp_cols= {}, sample= True):
    
    if sample:
        gen_sample, subsummary, code_vec, code_lib, Nsample, Msample= geno_subset_random(gen_sample,summary, RG_info, 
                                                                                         ID_col,subset_col, Names,code=code, include= Names_select, 
                                                                                         Sn= Sn, Sm= Sm)
    else:
        Nsample= list(range(len(Names)))

    PCsel= 1

    Names_subsel= [Names[x] for x in Nsample]
    ###


    gp_cols= {z:'rgb({})'.format(','.join([str(x) for x in g])) for z,g in gp_cols.items()}

    ref_dict= list(RG_info['IRIS_ID'])
    ref_dict= [ref_dict.index(x) for x in Names_subsel]
    ref_code= [RG_info['Initial_subpop'][x] for x in ref_dict]

    ref_dict= [['admx',x][int(x in code.keys())] for x in ref_code]
    ref_dict= [code[x] for x in ref_dict]

    keep_vec= [x for x in range(len(ref_dict)) if ref_dict[x] in ref_keep or Names_subsel[x] in Names_select]
    names_kept= [Names_subsel[x] for x in keep_vec]
    Names_idx= [names_kept.index(x) for x in Names_select]
    ref_dict= [ref_dict[x] for x in keep_vec]


    ref_dict= {z: [x for x in range(len(ref_dict)) if ref_dict[x]==z] for z in set(ref_dict)}
    ###
    pca = PCA(n_components=3)

    pca.fit(gen_sample[keep_vec])

    pc_data= pca.transform(gen_sample[keep_vec])

    ###

    fig= [go.Scatter3d(
        x= pc_data[g,0],
        y= pc_data[g,PCsel],
        z= pc_data[g,PCsel+1],
        text= [Names_subsel[x] for x in g],
        mode= "markers",
        name= gp_names_dict[z],
        marker= dict(
            size= 5,
            color= gp_cols[z]
        )
    ) for z,g in ref_dict.items()]

    fig.append(go.Scatter3d(
        x= pc_data[Names_idx[:5],0],
        y= pc_data[Names_idx[:5],PCsel],
        z= pc_data[Names_idx[:5],PCsel+1],
        mode= "markers",
        name= 'interest subtrop',
        text= Names_select[:5],
        marker= dict(
            size= 5,
            color= 'rgb(30,144,255)',
            symbol= 'x',
            line= dict(width= 1)
        )
        )
    )

    fig.append(go.Scatter3d(
        x= pc_data[Names_idx[5:10],0],
        y= pc_data[Names_idx[5:10],PCsel],
        z= pc_data[Names_idx[5:10],PCsel+1],
        mode= "markers",
        name= 'control subtrop',
        text= Names_select[5:10],
        marker= dict(
            size= 5,
            color= 'rgb(255,215,0)',
            symbol= 'cross',
            line= dict(width= 2)
        )
        )
    )

    fig.append(go.Scatter3d(
        x= pc_data[Names_idx[10:],0],
        y= pc_data[Names_idx[10:],PCsel],
        z= pc_data[Names_idx[10:],PCsel+1],
        mode= "markers",
        name= 'control temp',
        text= Names_select[10:],
        marker= dict(
            size= 5,
            color= 'rgb(173,255,47)',
            symbol= 'diamond',
            line= dict(width= 2)
        )
    ))



    layout= go.Layout()

    Figure= go.Figure(data= fig,layout= layout)
    iplot(Figure)
Exemplo n.º 28
0
def _draw_streamlines(figure, sls, dimensions, color, name, cbv=None,
                      cbv_lims=[None, None]):
    color = np.asarray(color)

    plotting_shape = (sls._data.shape[0] + sls._offsets.shape[0])
    # dtype object so None can be stored
    x_pts = np.zeros(plotting_shape)
    y_pts = np.zeros(plotting_shape)
    z_pts = np.zeros(plotting_shape)

    if cbv is not None:
        if cbv_lims[0] is None:
            cbv_lims[0] = 0
        if cbv_lims[1] is None:
            cbv_lims[1] = cbv.max()

        customdata = np.zeros(plotting_shape)
        line_color = np.zeros((plotting_shape, 3))
        color_constant = (color / color.max())\
            * (1.4 / (cbv_lims[1] - cbv_lims[0])) + cbv_lims[0]
    else:
        customdata = np.zeros(plotting_shape)
        line_color = np.zeros((plotting_shape, 3))
        color_constant = color

    for sl_index, plotting_offset in enumerate(sls._offsets):
        sl_length = sls._lengths[sl_index]
        sl = sls._data[plotting_offset:plotting_offset + sl_length]

        # add sl to lines
        total_offset = plotting_offset + sl_index
        x_pts[total_offset:total_offset + sl_length] = sl[:, 0]
        # don't draw between streamlines
        x_pts[total_offset + sl_length] = np.nan
        y_pts[total_offset:total_offset + sl_length] = sl[:, 1]
        y_pts[total_offset + sl_length] = np.nan
        z_pts[total_offset:total_offset + sl_length] = sl[:, 2]
        z_pts[total_offset + sl_length] = np.nan

        if cbv is not None:
            brightness = cbv[
                sl[:, 0].astype(int),
                sl[:, 1].astype(int),
                sl[:, 2].astype(int)
            ]

            line_color[total_offset:total_offset + sl_length, :] = \
                np.outer(brightness, color_constant)
            customdata[total_offset:total_offset + sl_length] = brightness
        else:
            line_color[total_offset:total_offset + sl_length, :] = \
                color_constant
            customdata[total_offset:total_offset + sl_length] = 1

            line_color[total_offset + sl_length, :] = [0, 0, 0]
            customdata[total_offset + sl_length] = 0

    figure.add_trace(
        go.Scatter3d(
            x=dimensions[0] - x_pts,
            y=y_pts,
            z=z_pts,
            name=name,
            marker=dict(
                size=0.0001,
                color=_color_arr2str(color)
            ),  # this is necessary to add color to legend
            line=dict(
                width=8,
                color=line_color,
            ),
            hovertext=customdata,
            hoverinfo='all'
        )
    )
def Coincidences_3D_plot(df, data_sets, window):
    # Declare max and min count
    min_count = 0
    max_count = np.inf
    # Perform initial filters
    df = df[(df.wCh != -1) & (df.gCh != -1)]
    df = filter_ce_clusters(window, df)
    if data_sets == 'mvmelst_039.zip':
        df = df[df.Time < 1.5e12]
    # Initiate 'voxel_id -> (x, y, z)'-mapping
    detector_vec = get_detector_mappings()
    # Initiate border lines
    b_traces = initiate_detector_border_lines(detector_vec)
    # Calculate 3D histogram
    H, edges = np.histogramdd(df[['wCh', 'gCh', 'Bus']].values,
                              bins=(80, 40, 9),
                              range=((0, 80), (80, 120), (0, 9)))
    # Insert results into an array
    hist = [[], [], [], []]
    loc = 0
    labels = []
    detector_names = ['ILL', 'ESS_CLB', 'ESS_PA']
    for wCh in range(0, 80):
        for gCh in range(80, 120):
            for bus in range(0, 9):
                detector = detector_vec[bus // 3]
                over_min = H[wCh, gCh - 80, bus] > min_count
                under_max = H[wCh, gCh - 80, bus] <= max_count
                if over_min and under_max:
                    coord = detector[flip_bus(bus % 3), gCh, flip_wire(wCh)]
                    hist[0].append(coord['x'])
                    hist[1].append(coord['y'])
                    hist[2].append(coord['z'])
                    hist[3].append(H[wCh, gCh - 80, bus])
                    loc += 1
                    labels.append('Detector: ' + detector_names[(bus // 3)] +
                                  '<br>' + 'Module: ' + str(bus) + '<br>' +
                                  'WireChannel: ' + str(wCh) + '<br>' +
                                  'GridChannel: ' + str(gCh) + '<br>' +
                                  'Counts: ' + str(H[wCh, gCh - 80, bus]))
    # Produce 3D histogram plot
    MG_3D_trace = go.Scatter3d(
        x=hist[2],
        y=hist[0],
        z=hist[1],
        mode='markers',
        marker=dict(
            size=5,
            color=np.log10(hist[3]),
            colorscale='Jet',
            opacity=1,
            colorbar=dict(thickness=20, title='log10(counts)'),
            #cmin=0,
            #cmax=2.5
        ),
        text=labels,
        name='Multi-Grid',
        scene='scene1')
    # Introduce figure and put everything together
    fig = py.tools.make_subplots(rows=1, cols=1, specs=[[{'is_3d': True}]])
    # Insert histogram
    fig.append_trace(MG_3D_trace, 1, 1)
    # Insert vessel borders
    for b_trace in b_traces:
        fig.append_trace(b_trace, 1, 1)
    # Assign layout with axis labels, title and camera angle
    a = 1
    camera = dict(up=dict(x=0, y=0, z=1),
                  center=dict(x=0, y=0, z=0),
                  eye=dict(x=-2 * a, y=-0.5 * a, z=1.3 * a))
    fig['layout']['scene1']['xaxis'].update(title='z [m]')
    fig['layout']['scene1']['yaxis'].update(title='x [m]')
    fig['layout']['scene1']['zaxis'].update(title='y [m]')
    fig['layout'].update(title='Coincidences (3D)<br>' + str(data_sets))
    fig['layout']['scene1']['camera'].update(camera)
    fig.layout.showlegend = False
    # If in plot He3-tubes histogram, return traces, else save HTML and plot
    if data_sets == '':
        return b_traces, hist[0], hist[1], hist[2], np.log10(hist[3])
    else:
        py.offline.plot(fig,
                        filename='../Results/HTML_files/Ce3Dhistogram.html',
                        auto_open=True)
        pio.write_image(fig, '../Results/HTML_files/Ce3Dhistogram.pdf')
Exemplo n.º 30
0
plt.show()  #Show the Clustering map

### isualizing'Agw'&'Region_code'&'Policy_Sales_Channel'
feature_cols4 = ['Age', 'Region_Code', 'Policy_Sales_Channel']
train4 = train[feature_cols4].values  # Features
km4 = KMeans(n_clusters=5,
             init='k-means++',
             max_iter=300,
             n_init=10,
             random_state=0)
km4.fit(train4)
train['labels'] = km4.labels_
# 绘制3D图
trace1 = go.Scatter3d(x=train['Age'],
                      y=train['Region_Code'],
                      z=train['Policy_Sales_Channel'],
                      mode='markers',
                      marker=dict(color=train['labels'],
                                  size=10,
                                  line=dict(color=train['labels'], width=12),
                                  opacity=0.8))
df_3dfid = [trace1]

layout = go.Layout(margin=dict(l=0, r=0, b=0, t=0),
                   scene=dict(xaxis=dict(title='Age'),
                              yaxis=dict(title='Region_Code'),
                              zaxis=dict(title='Policy_Sales_Channel')))

fig = go.Figure(data=df_3dfid, layout=layout)
py.offline.plot(fig)