-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
737 lines (514 loc) · 20.5 KB
/
main.py
File metadata and controls
737 lines (514 loc) · 20.5 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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
import np as np
import openpyxl
import tkinter.filedialog
import statistics
import scipy
#from matplotlib import pyplot as plt
#from matplotlib.widgets import Button
#import mpl_interactions.ipyplot as iplt
import pandas as pd # (version 1.0.0)
import plotly # (version 4.5.0)
import plotly.express as px
import dash # (version 1.8.0)
from dash import dcc
from dash import html
from dash.dependencies import Input, Output, State
#import mpl_interactions.ipyplot as iplt
import mpl_interactions.ipyplot as iplt
import matplotlib.pyplot as plt
import numpy as np
#x = np.linspace(0, np.pi, 100)
#tau = np.linspace(0.5, 10, 100)
#def f1(x, tau, beta):
# return np.sin(x * tau) * x * beta
#def f2(x, tau, beta):
# return np.sin(x * beta) * x * tau
#fig, ax = plt.subplots()
#controls = iplt.plot(x, f1, tau=tau, beta=(1, 10, 100), label="f1")
#iplt.plot(x, f2, controls=controls, label="f2")
#_ = plt.legend()
#plt.show()
"""
x = np.linspace(0, np.pi, 100)
p_val = np.linspace(0,10,100)
def graph1(x, p_val, mu):
return x * p_val * mu
#test
# ACCEPTANCE CRITERA
# AC01: if p value, have a list of proteins fitting the p value
# AC02: bar graph of each individual sample to see variation
# AC03: add typable p value
# NEW FUNCTIONALITY
def proteinMeanGraph(protein_name, mu):
fig, ax = plt.subplots()
controls = iplt.plot(x, graph1, p_val=p_val, mu = mu, label=protein_name)
#iplt.plot(x, graph1, controls=controls)
_ = plt.legend()
plt.show()
####################
# @params:
# choice: whether user wants to display graphical representation of results or not
# protein_name: name of protein
# desired_p_val: the value that the user entered for the desired p-value (i.e. 0.05, 0.01)
# mu: population mean
# TODO: improve p-value function
def pvalue_101(choice, protein_name, desired_p_val, mu, sigma, samp_size, samp_mean=0, deltam=0):
np.random.seed(1234)
s1 = np.random.normal(mu, sigma, samp_size) # simulate random normal distribution
if samp_mean > 0:
print(len(s1[s1>samp_mean]))
outliers = float(len(s1[s1>samp_mean])*100)/float(len(s1))
print('Percentage of numbers larger than {} is {}%'.format(samp_mean, outliers))
if deltam == 0:
deltam = abs(mu-samp_mean)
if deltam > 0 :
outliers = (float(len(s1[s1>(mu+deltam)]))
+float(len(s1[s1<(mu-deltam)])))*100.0/float(len(s1))
#print('Percentage of numbers further than the population mean of {} by +/-{} is {}% for {}'.format(mu, deltam, outliers, protein_name))
print('Percentage of numbers significantly larger than the control mean is {}% for {}'.format(outliers, protein_name))
percentage_needed = 1 - desired_p_val
if outliers < percentage_needed and choice == "y" and samp_size > 5:
fig, ax = plt.subplots(figsize=(8,8))
#fig.suptitle('Normal Distribution: population_mean={}'.format(mu) )
fig.suptitle('Normal Distribution: population_mean for {}'.format(protein_name))
plt.hist(s1)
plt.axvline(x=sigma, color='red')
#plt.axvline(x=mu-deltam, color='green')
plt.show()
return outliers
"""
# Set-up program
# File selection
root = tkinter.Tk()
root.withdraw
fileName1 = tkinter.filedialog.askopenfilename()
dataFrame = openpyxl.load_workbook(fileName1)
dataFrameReader = dataFrame.active
# Creating the data structures
arr = [] # goes down each column
sampleData = {} # goes across each row
sampleName = ""
flag = True
counter = 0
controlSampleNumbers = [] #column number of control samples
covidSampleNumbers = [] #column number of covid samples
covid2wkSampleNumbers = [] #column number of covid 2 week samples
covid6wkSampleNumbers = [] #column number of covid 6 week samples
for col in range(5, dataFrameReader.max_column):
arr.append([])
for row in dataFrameReader.iter_rows(0, dataFrameReader.max_row):
if flag:
sampleName = row[col].value
if sampleName.find("ctrl") != -1:
controlSampleNumbers.append(col)
if sampleName.find("Cov") != -1:
covidSampleNumbers.append(col)
if sampleName.find("2wk") != -1:
covid2wkSampleNumbers.append(col)
if sampleName.find("6wk") != -1:
covid6wkSampleNumbers.append(col)
flag = False
value = row[col].value
if not isinstance(value, str):
arr[counter].append(value)
arr[counter].sort()
print("Number of proteins with valid data in", sampleName, "=", len(arr[counter]))
counter += 1
flag = True
rowCounter = 0
# Set-up control values
# Goal: parse through just the control column and store data in dictionary
controlDataMap = {}
for row in dataFrameReader.iter_rows(1, dataFrameReader.max_row):
proteinName = row[2].value
controlDataMap[proteinName] = []
for col in controlSampleNumbers:
value = row[col].value
if not isinstance(value, str): # Filtered as value
controlDataMap[proteinName].append(value)
else:
controlDataMap[proteinName].append(50)
controlDataMappedToMean = {}
for key,val in controlDataMap.items():
if len(val) <= 0:
continue
mean = statistics.mean(val)
controlDataMappedToMean[key] = mean
# get data from left --> right
# key = proteinName
# value = list of data points
# Collect Sample Data
proteinName_mapped_to_p_value = {}
proteinName_mapped_to_mean = {}
numSamples_noCoV = len(controlSampleNumbers)
numSamples_coV = len(covidSampleNumbers)
numSamples_coV2wk = len(covid2wkSampleNumbers)
numSamples_coV6wk = len(covid6wkSampleNumbers)
for row in dataFrameReader.iter_rows(1, dataFrameReader.max_row):
proteinName = row[2].value
sampleData[proteinName] = []
coV_mean = 0
noCoV_mean = 0
coV2wk_mean = 0
coV6wk_mean = 0
coV_sum = 0
noCoV_sum = 0
coV2wk_sum = 0
coV6wk_sum = 0
ctrl_count = []
coV_count = []
coV2wk_count = []
coV6wk_count = []
count = 1
#Switch commented out parts to evaluate ctrl vs cov, cov2wks vs cov6wks
for col in range(5, dataFrameReader.max_column): # 6 = start of non-control groups
value = row[col].value
if not isinstance(value, str):
sampleData[proteinName].append(value)
"""
if col in controlSampleNumbers:
noCoV_sum += value
ctrl_count.append(count)
count += 1
elif col in covidSampleNumbers:
coV_sum += value
coV_count.append(count)
count += 1
"""
#"""
if col in covid2wkSampleNumbers:
coV2wk_sum += value
coV2wk_count.append(count)
count += 1
if col in covid6wkSampleNumbers:
coV6wk_sum += value
coV6wk_count.append(count)
count += 1
#"""
else:
sampleData[proteinName].append(0)
"""
if col in controlSampleNumbers:
noCoV_sum += 0
ctrl_count.append(count)
count += 1
elif col in covidSampleNumbers:
coV_sum += 0
coV_count.append(count)
count += 1
"""
#"""
if col in covid2wkSampleNumbers:
coV2wk_sum += 0
coV2wk_count.append(count)
count += 1
if col in covid6wkSampleNumbers:
coV6wk_sum += 0
coV6wk_count.append(count)
count += 1
#"""
noCoV_mean = noCoV_sum / numSamples_noCoV
coV_mean = coV_sum / numSamples_coV
coV2wk_mean = coV2wk_sum / numSamples_coV2wk
coV6wk_mean = coV6wk_sum / numSamples_coV6wk
#t-test and p-value mapping
a = []
b = []
"""
for col in ctrl_count:
a.append(sampleData[proteinName][col])
for col in coV_count:
b.append(sampleData[proteinName][col])
"""
#'''
for col in coV2wk_count:
a.append(sampleData[proteinName][col])
for col in coV6wk_count:
b.append(sampleData[proteinName][col])
#'''
res = scipy.stats.ttest_ind(a, b, equal_var=True)
proteinName_mapped_to_p_value[proteinName] = res.pvalue
proteinName_mapped_to_mean[proteinName] = "Non-Covid mean: " + str(noCoV_mean) + " | Covid mean: " + str(coV_mean) + " || p-value: " + str(res.pvalue)
#proteinName_mapped_to_mean[proteinName] = "Covid 2 wk mean: " + str(CoV2wk_mean) + " | Covid 6 wk mean: " + str(coV6wk_mean) + " || p-value: " + str(res.pvalue)
if numSamples_coV == 0 and numSamples_noCoV == 0:
proteinName_mapped_to_p_value[proteinName] = 0.99
proteinName_mapped_to_mean[proteinName] = "Insufficient Data for statistical significance"
rowCounter += 1
# Get Sample Names
sampleDataListSampleNames = []
ctrlSamples = []
coVSamples = []
coV2wkSamples = []
coV6wkSamples = []
colorMap = {}
for row in dataFrameReader.iter_rows(0, 1):
for col in range(5, dataFrameReader.max_column):
s = str(row[col].value)
#s = s.replace(".PG.Quantity", "")
s = s.split('_')[0]
sampleDataListSampleNames.append(s)
if "ctrl" in s:
colorMap[s] = "blue"
ctrlSamples.append(s)
if "Cov" in s:
colorMap[s] = "red"
coVSamples.append(s)
if "2wk" in s:
colorMap[s] = "purple"
coV2wkSamples.append(s)
if "6wk" in s:
colorMap[s] = "pink"
coV6wkSamples.append(s)
#sampleDataListCompare = [ctrlSamples, coVSamples]
sampleDataListCompare = [coV2wkSamples, coV6wkSamples]
proteinSampleDataMappedToMean = {}
#desired_p_value = (float)(input("Enter the desired p value: "))
desired_p_value = 0.05
#graphs = input("Would you like to see a graphical display of the statistics for statistically significant proteins? (y/n)")
graphs = "y"
temp_counter = 0
for key,val in sampleData.items():
# If entire data set is invalid (i.e. "Filtered) then ignore this sample
#if temp_counter >= 1:
# break
#temp_counter += 1
if len(val) <= 0:
continue
proteinSampleDataMappedToMean[key] = []
proteinSampleDataMappedToMean[key].append(statistics.mean(val))
#proteinSampleDataMappedToMean[key].append(pvalue_101(graphs, key, desired_p_value, controlDataMappedToMean[key], (int)(proteinSampleDataMappedToMean[key][0]), 100))
# proteinSampleDataMappedToMean[key].append(proteinMeanGraph(key, controlDataMappedToMean[key])) # Note: this allows GRAPHING
print("-----------------------------------------------------------")
proteinNamesList = list(sampleData.keys())
from dash import Dash, dcc, html, Input, Output
import plotly.express as px
app = Dash(__name__)
# List of elements on HTML webpage
app.layout = html.Div([
html.H2('Covid vs. Non-Covid Protein Expression Application || Property of the Gong Laboratory.'),
dcc.Dropdown(
id="dropdown",
options=[x for x in sampleData.keys()],
value=proteinNamesList[0],
clearable=False,
),
html.Label("Include results only from proteins with a p-value of less than or equal to: "),
dcc.Slider(0, 1, marks=None, value=0.05, id='slider', tooltip={"placement": "bottom", "always_visible": True}),
html.Label("Manually enter a p-value: "),
dcc.Input(type='number', value=0.05, id='manualinput'),
html.H3('Bar Chart'),
dcc.Graph(id="graph"),
html.H3('Violin Plot'),
dcc.Graph(id="violin"),
html.H3('Density Heatmap'),
dcc.Graph(id="heatmap"),
html.H3('Empirical Cumulative Distribution Plot'),
dcc.Graph(id="ECDF")
])
@app.callback(
Output("dropdown", "options"),
Input("slider", "value"))
def update_dropdown(pVal):
return [key for key,val in proteinName_mapped_to_p_value.items() if val <= pVal]
@app.callback(
Output("slider", "value"),
Input("manualinput", "value"))
def update_proteins(pVal):
if pVal is None:
return 0
return pVal
@app.callback(
Output("graph", "figure"),
Input("dropdown", "value"))
def update_bar_chart(proteinName):
print(proteinName + ": " + proteinName_mapped_to_mean[proteinName])
df = {'Samples': sampleDataListSampleNames, 'Detection Levels': sampleData[proteinName]}
fig = px.bar(df, x = 'Samples', y='Detection Levels', color="Samples", color_discrete_map=colorMap)
return fig
@app.callback(
Output("violin", "figure"),
Input("dropdown", "value"))
def update_violin_plot(proteinName):
df = {'Detection Levels': sampleData[proteinName], 'Sample': sampleDataListSampleNames}
fig = px.violin(df, y='Detection Levels', hover_data=['Sample'], color_discrete_map=colorMap, box=True, points="all")
return fig
@app.callback(
Output("heatmap", "figure"),
Input("dropdown", "value"))
def update_heatmap_plot(proteinName):
df = {'Samples': sampleDataListSampleNames, 'Detection Levels': sampleData[proteinName]}
fig = px.density_heatmap(df, y='Detection Levels', x='Samples', marginal_x="histogram", marginal_y="histogram")
return fig
@app.callback(
Output("ECDF", "figure"),
Input("dropdown", "value"))
def update_ECDF_plot(proteinName):
df = {'Detection Levels': sampleData[proteinName], 'Sample': sampleDataListSampleNames}
fig = px.ecdf(df, x='Detection Levels', hover_data=['Sample'], markers = True)
return fig
app.run_server(debug=True)
"""
app = dash.Dash(__name__)
# ---------------------------------------------------------------
df = pd.read_csv(
"C:\\Users\\antho\\Downloads\\DOHMH_New_York_City_Restaurant_Inspection_Results.csv") # https://drive.google.com/file/d/1jyvSiRjaNIeOCP59dUFQuZ0_N_StiQOr/view?usp=sharing
df['INSPECTION DATE'] = pd.to_datetime(df['INSPECTION DATE'])
df = df.groupby(['INSPECTION DATE', 'CUISINE DESCRIPTION', 'CAMIS'], as_index=False)['SCORE'].mean()
df = df.set_index('INSPECTION DATE')
df = df.loc['2016-01-01':'2019-12-31']
df = df.groupby([pd.Grouper(freq="M"), 'CUISINE DESCRIPTION'])['SCORE'].mean().reset_index()
# print (df[:5])
# ---------------------------------------------------------------
app.layout = html.Div([
html.Div([
dcc.Graph(id='our_graph')
], className='nine columns'),
html.Div([
html.Br(),
html.Label(['Choose 3 Cuisines to Compare:'], style={'font-weight': 'bold', "text-align": "center"}),
dcc.Dropdown(id='cuisine_one',
options=[{'label': x, 'value': x} for x in
proteinSampleDataMappedToMean.keys()],
value='African',
multi=False,
disabled=False,
clearable=True,
searchable=True,
placeholder='Choose Cuisine...',
className='form-dropdown',
style={'width': "90%"},
persistence='string',
persistence_type='memory'),
dcc.Dropdown(id='cuisine_two',
options=[{'label': x, 'value': x} for x in
df.sort_values('CUISINE DESCRIPTION')['CUISINE DESCRIPTION'].unique()],
value='Asian',
multi=False,
clearable=False,
persistence='string',
persistence_type='session'),
dcc.Dropdown(id='cuisine_three',
options=[{'label': x, 'value': x} for x in
df.sort_values('CUISINE DESCRIPTION')['CUISINE DESCRIPTION'].unique()],
value='Donuts',
multi=False,
clearable=False,
persistence='string',
persistence_type='local'),
], className='three columns'),
])
# ---------------------------------------------------------------
@app.callback(
Output('our_graph', 'figure'),
[Input('cuisine_one', 'value'),
Input('cuisine_two', 'value'),
Input('cuisine_three', 'value')]
)
def build_graph(first_cuisine, second_cuisine, third_cuisine):
dff = df[(df['CUISINE DESCRIPTION'] == first_cuisine) |
(df['CUISINE DESCRIPTION'] == second_cuisine) |
(df['CUISINE DESCRIPTION'] == third_cuisine)]
# print(dff[:5])
fig = px.line(dff, x="INSPECTION DATE", y="SCORE", color='CUISINE DESCRIPTION', height=600)
fig.update_layout(yaxis={'title': 'NEGATIVE POINT'},
title={'text': 'Restaurant Inspections in NYC',
'font': {'size': 28}, 'x': 0.5, 'xanchor': 'center'})
return fig
# ---------------------------------------------------------------
if __name__ == '__main__':
app.run_server(debug=False)
"""
"""
app = dash.Dash(__name__)
# ---------------------------------------------------------------
df = pd.read_csv(
"C:\\Users\\antho\\Downloads\\DOHMH_New_York_City_Restaurant_Inspection_Results.csv") # https://drive.google.com/file/d/1jyvSiRjaNIeOCP59dUFQuZ0_N_StiQOr/view?usp=sharing
df['INSPECTION DATE'] = pd.to_datetime(df['INSPECTION DATE'])
df = df.groupby(['INSPECTION DATE', 'CUISINE DESCRIPTION', 'CAMIS'], as_index=False)['SCORE'].mean()
df = df.set_index('INSPECTION DATE')
df = df.loc['2016-01-01':'2019-12-31']
df = df.groupby([pd.Grouper(freq="M"), 'CUISINE DESCRIPTION'])['SCORE'].mean().reset_index()
# print (df[:5])
df = px.data.iris()
# ---------------------------------------------------------------
app.layout = html.Div([
html.Div([
dcc.Graph(id='our_graph')
], className='nine columns'),
html.Div([
html.Br(),
html.Label(['Choose 3 Cuisines to Compare:'], style={'font-weight': 'bold', "text-align": "center"}),
dcc.Dropdown(id='cuisine_one',
options=[{'label': x, 'value': x} for x in
proteinSampleDataMappedToMean.keys()],
value='African',
multi=False,
disabled=False,
clearable=True,
searchable=True,
placeholder='Choose Cuisine...',
className='form-dropdown',
style={'width': "90%"},
persistence='string',
persistence_type='memory'),
dcc.Dropdown(id='cuisine_two',
options=[{'label': x, 'value': x} for x in
df.sort_values('CUISINE DESCRIPTION')['CUISINE DESCRIPTION'].unique()],
value='Asian',
multi=False,
clearable=False,
persistence='string',
persistence_type='session'),
dcc.Dropdown(id='cuisine_three',
options=[{'label': x, 'value': x} for x in
df.sort_values('CUISINE DESCRIPTION')['CUISINE DESCRIPTION'].unique()],
value='Donuts',
multi=False,
clearable=False,
persistence='string',
persistence_type='local'),
], className='three columns'),
])
# ---------------------------------------------------------------
@app.callback(
Output('our_graph', 'figure'),
[Input('cuisine_one', 'value'),
Input('cuisine_two', 'value'),
Input('cuisine_three', 'value')]
)
def build_graph(first_cuisine, second_cuisine, third_cuisine):
# dff = df[(df['CUISINE DESCRIPTION'] == first_cuisine) |
#(df['CUISINE DESCRIPTION'] == second_cuisine) |
#(df['CUISINE DESCRIPTION'] == third_cuisine)]
fig = px.scatter(df, x='sepal_length', y='sepal_width', color='species', size='petal_length')
return fig
# ---------------------------------------------------------------
if __name__ == '__main__':
app.run_server(debug=False)
"""
"""
from dash import Dash, dcc, html
from base64 import b64encode
import io
app = Dash(__name__)
buffer = io.StringIO()
df = px.data.iris() # replace with your own data source
fig = px.scatter(
df, x="sepal_width", y="sepal_length",
color="species")
fig.write_html(buffer)
html_bytes = buffer.getvalue().encode()
encoded = b64encode(html_bytes).decode()
app.layout = html.Div([
html.H4('Simple plot export options'),
html.P("↓↓↓ try downloading the plot as PNG ↓↓↓", style={"text-align": "right", "font-weight": "bold"}),
dcc.Graph(id="graph", figure=fig),
html.A(
html.Button("Download as HTML"),
id="download",
href="data:text/html;base64," + encoded,
download="plotly_graph.html"
)
])
app.run_server(debug=True)
"""