From 28eb27812033f471dc57ab060eb5afe5f9b373b3 Mon Sep 17 00:00:00 2001 From: Dan Howe Date: Wed, 28 Aug 2019 17:02:49 +1000 Subject: [PATCH] Add 'sharing_wp.py' --- daqviewer/sharing_wp.py | 161 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 161 insertions(+) create mode 100644 daqviewer/sharing_wp.py diff --git a/daqviewer/sharing_wp.py b/daqviewer/sharing_wp.py new file mode 100644 index 0000000..3e16227 --- /dev/null +++ b/daqviewer/sharing_wp.py @@ -0,0 +1,161 @@ +import json +import numpy as np +import dash +import dash_table +import pandas as pd +import plotly.graph_objs as go +import dash_table +import dash_core_components as dcc +import dash_html_components as html +from dash.dependencies import Input, Output, State + +app = dash.Dash(__name__) + +app.layout = html.Div([ + html.Div(dcc.Graph(id='graph')), + html.Div(id='div-table'), + html.Div(id='datatable-row-ids-container'), + html.Div([ + html.Button('Load', id='button'), + html.Div(id='storage', style={'display': 'none'}) + ]) +]) + +# app.css.append_css('style.css') + + +@app.callback( + [Output('storage', 'children'), + Output('div-table', 'children')], + [Input('button', 'n_clicks')], +) +def load_data(n_clicks): + csv_name = 'test_20_WP.csv' + + # Check instrument type + inst_type = 'WP' + if inst_type == 'WP': + df = pd.read_csv(csv_name, index_col=0, header=5, skiprows=[6]) + + # Rename columns based on probe locations + suffixes = ['P1', 'P2', 'P3', 'incident', 'reflected'] + col_names = list(df.columns) + for i, col in enumerate(col_names[:-4]): + if ('.' not in col) and (col_names[i + 4] == col + '.4'): + for j, suffix in enumerate(suffixes): + col_names[i + j] = '{}-{}'.format(col, suffix) + df.columns = col_names + else: + df = pd.read_csv(csv_name, index_col=0, header=3, skiprows=[4]) + + # Zero time series based on first 5s + df -= df[:5].mean() + + df = df[:100] + + # Round floats + df = df.round(2) + + variables = { + 'location': '', + 'H_sig': 'o', + 'H_1%': 'x', + 'H_max': '□', + 'Hm0': '◊', + 'Tp': '', + 'Tp1': '', + } + + stats = df.describe().T + s1 = stats.index.to_series() + s1.name = 'Name' + stats = pd.concat([s1, stats], axis=1) + + datatable = dash_table.DataTable( + id='datatable-row-ids', + # columns=[{ + # 'name': [val, key], + # 'id': key + # } for key, val in variables.items()], + columns=[{ + 'name': x, + 'id': x + } for x in stats.columns], + data=stats.to_dict('records'), + editable=False, + sort_action='native', + sort_mode='multi', + row_selectable='multi', + row_deletable=False, + selected_rows=np.arange(stats.shape[0]), + style_as_list_view=True, + style_cell={ + 'minWidth': '100px', + 'width': '100px', + 'maxWidth': '100px', + }, + ) + + return df.to_json(orient='table'), [datatable] + + +@app.callback( + Output('graph', 'figure'), + [Input('storage', 'children')], +) +def update_graph(json_data): + + if json_data is None: + return {'data': []} + + df = pd.read_json(json_data, orient='table') + + ts = [] + for col in df.columns: + trace = go.Scatter(x=df.index, y=df[col], name=col, opacity=0.8) + ts.append(trace) + + layout = {'title': 'basename', 'xaxis': {'rangeslider': {}}} + timeseries = {'data': ts, 'layout': layout} + + return timeseries + + +# @app.callback(Output('datatable-row-ids-container', 'children'), [ +# Input('datatable-row-ids', 'derived_virtual_row_ids'), +# Input('datatable-row-ids', 'selected_row_ids'), +# Input('datatable-row-ids', 'active_cell') +# ]) +# def update_graphs(row_ids, selected_row_ids, active_cell): +# # When the table is first rendered, `derived_virtual_data` and +# # `derived_virtual_selected_rows` will be `None`. This is due to an +# # idiosyncracy in Dash (unsupplied properties are always None and Dash +# # calls the dependent callbacks when the component is first rendered). +# # So, if `rows` is `None`, then the component was just rendered +# # and its value will be the same as the component's dataframe. +# # Instead of setting `None` in here, you could also set +# # `derived_virtual_data=df.to_rows('dict')` when you initialize +# # the component. +# selected_id_set = set(selected_row_ids or []) +# +# if selected_id_set: +# dff = df.loc[selected_id_set] +# else: +# dff = df +# +# spec = [] +# for i, row in dff.iterrows(): +# x = np.arange(10) +# y = x * row['H_sig'] +# +# trace = go.Scatter(x=x, y=y, name=i) +# spec.append(trace) +# +# energy = dict(data=spec) +# +# graph = dcc.Graph(id='time-series', figure=energy) +# +# return graph + +if __name__ == '__main__': + app.run_server(debug=True)