-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.py
More file actions
277 lines (244 loc) · 9.88 KB
/
app.py
File metadata and controls
277 lines (244 loc) · 9.88 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
import base64
import io
import pandas as pd
import numpy as np
from scipy.optimize import curve_fit
import dash_extendable_graph as deg
from dash import Dash, dcc, html, Input, Output, State, dash_table, callback_context, no_update
from dash.exceptions import PreventUpdate
# Initialize the Dash app
app = Dash(__name__)
server = app.server
# Fit functions
def linear_func(x, a, b): return a*x + b
def quad_func(x, a, b, c): return a*x**2 + b*x + c
def sqrt_func(x, a, b): return a*np.sqrt(x) + b
def sine_func(x, a, b, c, d): return a*np.sin(b*x + c) + d
def cos_func(x, a, b, c, d): return a*np.cos(b*x + c) + d
def tan_func(x, a, b, c, d): return a*np.tan(b*x + c) + d
FIT_FUNCTIONS = {
'Linear': (linear_func, ['a','b']),
'Quadratic': (quad_func, ['a','b','c']),
'Square Root': (sqrt_func, ['a','b']),
'Sine': (sine_func, ['a','b','c','d']),
'Cosine': (cos_func, ['a','b','c','d']),
'Tangent': (tan_func, ['a','b','c','d']),
}
# Initial scatter figure
INITIAL_FIG = {
'data': [{'x':[], 'y':[], 'mode':'markers'}],
'layout': {
'template':'plotly_white',
'showlegend':False,
'xaxis':{'title':'X','showgrid':True,'gridcolor':'lightgrey','linecolor':'white','zerolinecolor':'black'},
'yaxis':{'title':'Y','showgrid':True,'gridcolor':'lightgrey','linecolor':'white','zerolinecolor':'black'},
'margin':{'l':40,'r':10,'t':40,'b':40}
}
}
# Helper: blank row
def blank_row():
return {'x': None, 'y': None}
INITIAL_ROWS = [blank_row()]
INITIAL_COLUMNS = [{'name': 'x', 'id': 'x', 'type': 'numeric'}, {'name': 'y', 'id': 'y', 'type': 'numeric'}]
app.index_string = '''
<!DOCTYPE html>
<html>
<head>
{%metas%}
<title>Mr.Angeles' Lite Physics Modeling</title>
{%favicon%}
{%css%}
<style>
html, body {
margin: 0;
padding: 0;
height: 100%;
overflow: hidden;
}
</style>
</head>
<body>
{%app_entry%}
<footer>
{%config%}
{%scripts%}
{%renderer%}
</footer>
</body>
</html>
'''
# Layout
app.layout = html.Div(style={'display':'flex', 'flexDirection':'column', 'height':'100vh','boxSizing':'border-box'}, children=[
html.Div('Mr. Angeles\' Lite Physics Modeling', style={
'flex': '0 0 5%',
'textAlign':'left',
'fontSize':'2rem',
'fontWeight':'bold',
'margin':'0',
'padding':'0.5rem',
'borderBottom':'2px solid #ccc',
'background-color':'rgba(120,169,153,0.35)'
}),
html.Div(
style={'flex':'1','display':'flex','fontFamily':'Helvetica, Arial'},
children=[
dcc.Store(id='prev-count', data=0),
dcc.Store(id='change-in-rows', data=0),
# Left panel: table and fit controls
html.Div(style={'width':'33%','padding':'0rem 1rem','borderRight':'1px solid #ccc','display':'flex','flexDirection':'column','background-color':'rgba(85,117,203,0.10)'}, children=[
# Table section
html.Div(style={'flex':'1', 'overflowY':'auto', 'paddingBottom':'1rem'}, children=[
html.H3('Data Entry'),
dcc.Upload(id='upload-csv', children=html.Div([html.Strong('📄 Drag & Drop or '), html.A('Select Data', style={'color':'#007bff', 'textDecoration':'underline'})]),
style={'width':'95%',
'height':'3rem',
'lineHeight':'3rem',
'borderWidth':'1px',
'borderStyle':'dashed',
'borderRadius':'6px',
'textAlign':'center',
'marginBottom':'1rem',
'cursor':'pointer',
'transition':'background-color 0.2s ease-in-out'},
multiple=False),
html.Div(style={'marginBottom':'1rem'}, children=[
html.Button('Clear Data', id='clear-btn', n_clicks=0),
html.Button('Add Row', id='add-row', n_clicks=0, style={'marginLeft':'0.5rem'})
]),
dash_table.DataTable(id='data-table',
columns=INITIAL_COLUMNS,
data=INITIAL_ROWS.copy(),
editable=True,
row_deletable=True,
style_table={'minWidth':'0','maxWidth':'95%'})
]),
# Fit controls section
html.Div(style={'flex':'1','paddingTop':'1rem','borderTop':'1px solid #ccc','overflowY':'auto'}, children=[
html.H4('Curve Fitting'),
dcc.Dropdown(id='fit-method', options=[{'label':'No Fit','value':'None'}] + [{'label':k,'value':k} for k in FIT_FUNCTIONS], value='None'),
html.Div(id='fit-output', style={'marginTop':'1rem','whiteSpace':'pre-wrap','fontFamily':'monospace'})
])
]),
# Right panel: extendable graph
html.Div(style={'width':'67%','padding':'0rem 1rem'}, children=[
html.H3('Graph'),
deg.ExtendableGraph(
id='scatter-plot',
figure=INITIAL_FIG,
extendData=None,
config={'scrollZoom':True,'doubleClick':'reset','modeBarButtonsToRemove':['zoom2d','toggleSpikelines','hoverCompareCartesian'],},
style={'height':'80vh'})
])
])
])
# Callbacks
# Callback: Handle CSV uploads
@app.callback(
Output('data-table','data'),
Input('upload-csv','contents'),
Input('clear-btn','n_clicks'),
Input('add-row','n_clicks'),
State('data-table','data')
)
def modify_table(contents, clear_n, add_n, rows):
ctx = callback_context
if not ctx.triggered:
raise PreventUpdate
trig = ctx.triggered[0]['prop_id'].split('.')[0]
if trig == 'upload-csv' and contents:
_,b64=contents.split(',')
decoded = base64.b64decode(b64)
df=pd.read_csv(io.BytesIO(decoded))
if {'x','y'}.issubset(df.columns):
df = df[['x','y']]
else:
df = df.iloc[:,:2]
df.columns=['x','y']
record_dict = df.to_dict('records')
return record_dict
if trig == 'clear-btn':
return INITIAL_ROWS.copy()
if trig == 'add-row':
rows.append(blank_row())
return rows
return rows
# Callback: Extend plot to stop jitter
@app.callback(
Output('scatter-plot','extendData'),
Output('change-in-rows','data'),
Output('prev-count','data'),
Input('data-table','data'),
State('prev-count','data')
)
def extend_graph(rows, prev_count):
df = pd.DataFrame(rows).dropna()
current_count = len(df)
# Clear graph
if current_count == 0:
return ([{'x': [], 'y': []}], [0], None), current_count - prev_count, 0
# Add data point
if current_count > prev_count:
new = df.iloc[prev_count:]
return ([{'x':new['x'].tolist(),'y':new['y'].tolist()}],[0],None), current_count - prev_count, current_count
# Erase data point
if current_count < prev_count:
return ([{'x':df['x'].tolist(),'y':df['y'].tolist()}],[0],None), current_count - prev_count, current_count
return no_update, no_update, no_update
# Callback: Overlay curve on graph
@app.callback(
Output('scatter-plot','figure'),
Output('fit-output','children'),
Output('change-in-rows','data', allow_duplicate=True),
Input('clear-btn', 'n_clicks'),
Input('fit-method','value'),
Input('scatter-plot','extendData'),
State('data-table','data'),
State('scatter-plot','figure'),
State('change-in-rows','data'),
prevent_initial_call=True
)
def update_fit(clear_n, method, ext_data, data, fig, change_in_rows):
ctx = callback_context
if not ctx.triggered:
raise PreventUpdate
trig = ctx.triggered[0]['prop_id'].split('.')[0]
if trig == 'clear-btn':
return INITIAL_FIG, '', 0
df = pd.DataFrame(data).dropna()
out=''
if trig == 'scatter-plot' and change_in_rows < 0:
scatter_trace = {'x': df['x'].tolist(), 'y': df['y'].tolist(), 'mode': 'markers'}
layout = fig.get('layout', INITIAL_FIG['layout'])
fig = {
'data': [scatter_trace],
'layout': layout
}
if method in FIT_FUNCTIONS and not df.empty:
func, params = FIT_FUNCTIONS[method]
x,y=df['x'].values, df['y'].values
p0=np.ones(len(params))
try:
popt,_ = curve_fit(func, x, y, p0=p0, maxfev=2000)
except RuntimeError:
return fig, 'Could not fit curve to data.', 0
except TypeError:
return fig, 'Not enough data. Please input at least 5 data points.', 0
# prepare fit line
xf = np.linspace(x.min(), x.max(),200)
yf = func(xf, *popt)
# update fig data: keep scatter at idx0, replace/append fit at idx1
newdata = [fig['data'][0]] + [dict(x=xf.tolist(), y=yf.tolist(), mode='lines', name=f'{method} Fit')]
fig['data'] = newdata
# compute R2
ss_res = np.sum((y - func(x, *popt))**2)
ss_tot = np.sum((y - y.mean())**2)
r2 = 1-ss_res/ss_tot if ss_tot else np.nan
# format output
eq = f"{method} fit parameters:\n"+"; ".join([f"{p}={v:.3f}" for p,v in zip(params,popt)])+f"\nR²={r2:.3f}"
out = eq
else:
# remove fit trace if exists
fig['data'] = [fig['data'][0]]
return fig, out, 0
if __name__=='__main__':
app.run(debug=True,port=8050)