-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
105 lines (80 loc) · 2.9 KB
/
app.py
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
from flask import Flask, render_template, Response, send_file, redirect, request
from matplotlib.figure import Figure
import numpy as np
import matplotlib.pyplot as plt
from io import StringIO
from io import BytesIO
import base64
import time
app = Flask(__name__)
# Home Page
@app.route('/', methods=['POST', 'GET'])
def index():
return render_template('test.html')
# Show Histograms
@app.route('/data', methods=['POST', 'GET'])
def generate_plot():
img = BytesIO()
select = request.form.get("distributions") # Option from user
myTime = 0
start = time.time() #Start Time
#Uniform Distribution
if select == 'uniform':
#start = time.time()
# Generate random start and end points
low = np.random.randint(1000)
high = np.random.randint(low, 10000)
#Generate data points
uniformData = np.random.uniform(low,high, 10000)
#end = time.time()
#Plot
plt.hist(uniformData, rwidth=0.95)
plt.savefig(img, format='png')
plt.close()
img.seek(0)
plot_url = base64.b64encode(img.getvalue()).decode('utf8')
# Normal Distribution
elif select == 'normal':
#start = time.time()
# Generate data points with random mean and std
normalData = np.random.normal(np.random.randint(100), np.random.randint(100), 10000)
#end = time.time()
# Plot
plt.hist(normalData, rwidth=0.95)
plt.savefig(img, format='png')
plt.close()
img.seek(0)
plot_url = base64.b64encode(img.getvalue()).decode('utf8')
# Normal distribution with mean and std from user
elif select == 'normal_student':
# Get mean and std from user
desired_mean = request.form.get("mean")
desired_std = request.form.get("std")
# Convert to float
desired_mean = float(desired_mean)
desired_std = float(desired_std)
#start = time.time()
# Normal distribution with 0 mean and user std
studentData = np.random.normal(loc=0.0, scale=desired_std, size=10000)
actual_mean = np.mean(studentData)
# Samples with zero mean
zero_mean_samples = studentData - (actual_mean)
zero_mean_std = np.std(zero_mean_samples)
# Scale the samples with user std
scaled_samples = zero_mean_samples * (desired_std/zero_mean_std)
# add the user mean
final_samples = scaled_samples+desired_mean
#end = time.time()
# Plot
plt.hist(final_samples, rwidth=0.95)
plt.savefig(img, format='png')
plt.close()
img.seek(0)
plot_url = base64.b64encode(img.getvalue()).decode('utf8')
end = time.time() # End time
myTime = end-start # get time needed to run
myTime *= 1000 # get time in ms
myTime = round(myTime,2)
return render_template('data.html', plot_url=plot_url, myTime=myTime)
if __name__ == '__main__':
app.run()