10 Commits
Author SHA1 Message Date
marc 5ac9f5dcbe Merge branch 'master' into docker-merge 2023-04-21 12:16:10 +02:00
marc 8977a4f132 fix import 2023-04-21 11:10:10 +02:00
marc fc6909a802 fix dumb naming 2023-04-21 11:01:53 +02:00
marc 1ad392fee0 static dir 2023-04-21 10:52:52 +02:00
marc b5e3b0242d better dockerfile 2023-04-21 10:45:01 +02:00
marc 11bc0f7b53 push 2023-04-18 14:20:41 +02:00
marc a2e4b43e16 using env vars for efficiency 2023-04-18 14:18:58 +02:00
marc 5bfc480a4d makefile 2023-04-18 14:05:53 +02:00
marc b46663289c docker-compose 2023-04-18 14:05:49 +02:00
marc 91be71285a dockerfile 2023-04-18 14:05:44 +02:00
8 changed files with 131 additions and 5 deletions
+10
View File
@@ -0,0 +1,10 @@
FROM python
RUN pip install flask scipy matplotlib
# COPY chi_square.py kolmogorov_smirnov.py ./
COPY server.py ./
COPY templates templates
RUN mkdir static
# ENTRYPOINT ["python", "chi_square.py"]
ENTRYPOINT ["flask", "run"]
+8
View File
@@ -0,0 +1,8 @@
run: build
docker compose up
build:
docker compose build
push:
docker compose push
+2 -2
View File
@@ -104,11 +104,11 @@ def index():
plt.savefig('static/plot.png')
# Render template with plot image and results
return render_template('home.html', plot_path='static/plot.png', chisq_stat=chisq_stat, p_value=p_value)
return render_template('chi_square.html', plot_path='static/plot.png', chisq_stat=chisq_stat, p_value=p_value)
else:
# Render initial form
return render_template('home.html')
return render_template('chi_square.html')
if __name__ == '__main__':
app.run(debug=True)
+13
View File
@@ -0,0 +1,13 @@
version: "3.9"
services:
analytics_learning:
image: marcpartensky/analytics_learning
build: .
ports:
- "5000:5000"
environment:
FLASK_APP: server.py
# FLASK_APP: kolmogorov_smirnov.py
# FLASK_APP: chi_square.py
FLASK_RUN_HOST: "0.0.0.0"
FLASK_RUN_PORT: 5000
+3 -3
View File
@@ -103,11 +103,11 @@ def index():
plt.savefig('static/plot.png')
# Render template with plot
return render_template('index.html', plot_url='static/plot.png')
return render_template('kolmogorov_smirnov.html', plot_url='static/plot.png')
else:
return render_template('index.html', error=True)
return render_template('kolmogorov_smirnov.html', error=True)
else:
return render_template('index.html')
return render_template('kolmogorov_smirnov.html')
if __name__ == '__main__':
app.run(debug=True, port=8000)
+95
View File
@@ -0,0 +1,95 @@
#!/usr/bin/env python
from flask import Flask, render_template, request
import numpy as np
import scipy.stats as stats
from scipy.stats import expon, gamma, norm, poisson
import matplotlib.pyplot as plt
app = Flask(__name__)
# Define routes
@app.route('/chi_square', methods=['GET', 'POST'])
def chi_square():
if request.method == 'POST':
# Get distribution type from form submission
distribution_type = request.form['distribution_type']
# Generate random sample data for selected distribution
if distribution_type == 'normal':
data = np.random.normal(loc=0, scale=1, size=100)
elif distribution_type == 'exponential':
data = np.random.exponential(scale=1, size=100)
elif distribution_type == 'poisson':
data = np.random.poisson(lam=1, size=100)
elif distribution_type == 'gamma':
data = np.random.gamma(shape=2, scale=1, size=100)
# Calculate expected values for selected distribution
if distribution_type == 'normal':
expected = stats.norm(loc=np.mean(data), scale=np.std(data)).pdf(data)
elif distribution_type == 'exponential':
expected = stats.expon(scale=np.mean(data)).pdf(data)
elif distribution_type == 'poisson':
expected = stats.poisson(mu=np.mean(data)).pmf(data)
elif distribution_type == 'gamma':
expected = stats.gamma(a=np.mean(data)**2/np.var(data), scale=np.var(data)/np.mean(data)).pdf(data)
# Calculate Chi-square statistic and p-value
chisq_stat, p_value = stats.chisquare(f_obs=np.histogram(data, bins='auto')[0], f_exp=expected)
# Create histogram of sample data and plot expected distribution
plt.hist(data, bins='auto', density=True, alpha=0.7)
plt.plot(data, expected, 'r-', lw=2)
plt.title(f'{distribution_type.capitalize()} Distribution')
plt.xlabel('Value')
plt.ylabel('Density')
plt.savefig('static/plot.png')
# Render template with plot image and results
return render_template('chi_square.html', plot_path='static/plot.png', chisq_stat=chisq_stat, p_value=p_value)
else:
# Render initial form
return render_template('chi_square.html')
@app.route('/kolmogorov_smirnov', methods=['GET', 'POST'])
def kolmogorov_smirnov():
if request.method == 'POST':
# Get user input
distribution_type = request.form['distribution_type']
num_samples = int(request.form['num_samples'])
# Generate data
if distribution_type == 'normal':
data = norm.rvs(size=num_samples)
elif distribution_type == 'uniform':
data = np.random.uniform(size=num_samples)
elif distribution_type == 'standard':
data = np.random.randn(num_samples)
elif distribution_type == 'exponential':
data = expon.rvs(size=num_samples)
elif distribution_type == 'poisson':
data = poisson.rvs(mu=5, size=num_samples)
elif distribution_type == 'gamma':
data = gamma.rvs(a=5, size=num_samples)
else:
data = None
# Generate plot
if data is not None:
plt.hist(data, bins=30)
plt.title(f'{distribution_type.capitalize()} Distribution')
plt.xlabel('Value')
plt.ylabel('Frequency')
plt.savefig('static/plot.png')
# Render template with plot
return render_template('kolmogorov_smirnov.html', plot_url='static/plot.png')
else:
return render_template('kolmogorov_smirnov.html', error=True)
else:
return render_template('kolmogorov_smirnov.html')
if __name__ == '__main__':
app.run(debug=True)