-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
110 lines (91 loc) · 3.38 KB
/
main.py
File metadata and controls
110 lines (91 loc) · 3.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import base64
import io
import dash_bootstrap_components as dbc
import numpy as np
import pandas as pd
import plotly.express as px
from dash import Dash, html, dcc, callback, Output, Input, callback_context, no_update, State
from dash.exceptions import PreventUpdate
from pca import run_pca
app = Dash(__name__)
app.layout = html.Div([
html.H1(children='PCA Analysis', style={'textAlign': 'center'}),
dbc.Row(
[dcc.Upload(id='upload-data',
children=['Drag and Drop or ',
html.A('Select Hyperspectral File')]
, style={
'width': '50%',
'height': '60px',
'lineHeight': '60px',
'borderWidth': '1px',
'borderStyle': 'dashed',
'borderRadius': '5px',
'textAlign': 'center'
},
multiple=False,
max_size=-1,
)
]
),
dbc.Row([dcc.Input(id="height-input", type="number", placeholder="enter image height"),
dcc.Input(id="width-input", type="number", placeholder="enter image width")]),
dbc.Row(html.Button('Run PCA', id='pca-button', n_clicks=0)),
dbc.Row(dcc.Loading(
id="loading-1",
type="default",
children=html.Div(id="loading-output-1")
)),
dbc.Row(dcc.Graph(id='spectral-content'))
])
@callback(
[Output("upload-data", "children"),
Output("loading-output-1", "children")],
[Input("upload-data", 'contents'),
Input("pca-button", 'n_clicks'),
Input("height-input", "value"),
Input("width-input", "value")],
State('upload-data', 'filename'),
)
def render_pca(upload_file, pca_button, height, width, filename):
changed_id = [p['prop_id'] for p in callback_context.triggered][0]
if upload_file is None:
raise PreventUpdate
if "pca-button" not in changed_id:
return filename, no_update
else:
content_type, content_string = upload_file.split(",")
decoded = base64.b64decode(content_string)
hsi_data = np.genfromtxt(io.StringIO(decoded.decode("utf-8")), delimiter='\t')
run_pca(hsi_data, height, width)
global pca_im
global wavelengths
global hsi_im
pca_im = np.load('output/pca_im.npy')
hsi_im = np.load('output/hsi_im.npy')
wavelengths = hsi_data[0]
graphs_children = [
dbc.Col([dcc.Dropdown([i for i in range(1, pca_im.shape[2] + 1)], 1,
id='dropdown-selection'),
dcc.Graph(id='graph-content',
hoverData={'points': [{'y': 0, 'x': 0}]}
)], width="auto")
]
return filename, graphs_children
@callback(
Output('graph-content', 'figure'),
Input('dropdown-selection', 'value')
)
def update_pca_im(value):
pca_sl = pca_im[:, :, value - 1]
return px.imshow(pca_sl)
@callback(
Output('spectral-content', 'figure'),
Input('graph-content', 'hoverData')
)
def update_spectral_graph(hoverData):
df = pd.DataFrame({'wavelength': wavelengths,
'intensity': hsi_im[hoverData['points'][0]['y'], hoverData['points'][0]['x'], :]})
return px.line(df, x='wavelength', y='intensity', title='Hyperspectral Data of Pixel')
if __name__ == '__main__':
app.run_server(debug=True, threaded=True)