Skip to content

Instantly share code, notes, and snippets.

View ShuaiGuo16's full-sized avatar
🎯
Focusing

Shuai Guo ShuaiGuo16

🎯
Focusing
View GitHub Profile
@ShuaiGuo16
ShuaiGuo16 / Plot_trajectory.py
Last active December 27, 2020 22:51
Plot a single trajectory
# Load trajectory data
traj_X = np.genfromtxt('./Dataset/traj_X.csv', delimiter=',')
traj_Y = np.genfromtxt('./Dataset/traj_Y.csv', delimiter=',')
# Display a single trajectory
fig, ax = plt.subplots(figsize=(8,3))
pick_traj = 5
ax.plot(traj_X[pick_traj,:],traj_Y[pick_traj,:],
color='b', lw=2, linestyle='--')
@ShuaiGuo16
ShuaiGuo16 / Animate_trajectory.py
Last active December 28, 2020 09:28
Animate a single trajectory
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import animation
from IPython.display import HTML
from celluloid import Camera
%matplotlib inline
# Animate a single trajectory
pick_traj = 5 # Select a trajectory to simulate
@ShuaiGuo16
ShuaiGuo16 / Animate_trajectores.py
Last active December 28, 2020 12:05
Animate multiple trajectories
# Create individual frames
for i in range(0,30):
for j in range(1,traj_X.shape[1]+1):
# Projectile's trajectory
x = traj_X[i][0:j]
y = traj_Y[i][0:j]
# Show Projectile's location
ax.plot(x[-1], y[-1], marker='o', markersize=12,
@ShuaiGuo16
ShuaiGuo16 / histogram_edges.py
Created December 28, 2020 12:14
Obtain edges of bins for plotting histogram
# Extract range data
Range = traj_X[:,-1]
# Extract bins
n, bins, patches = plt.hist(Range, bins=10, edgecolor='black')
plt.close()
@ShuaiGuo16
ShuaiGuo16 / Animate_histogram.py
Created December 28, 2020 12:23
Animate histogram
# Set up the histogram axis
ax_hist = ax.twinx() # Make an identical copy
ax_hist.set_ylim(0, 10.2)
ax_hist.set_yticks(list(range(0,10,2)))
ax_hist.set_ylabel('Count', fontsize=15)
# Initiate camera
camera = Camera(fig)
# A list of range values
# Data analysis
import pandas as pd
import numpy as np
# Simulate SIR model
from scipy.integrate import odeint
# Sampling
from pyDOE import lhs
from scipy.stats.distributions import norm
@ShuaiGuo16
ShuaiGuo16 / UQ_SIR_model.py
Created January 13, 2021 10:26
SIR model
def SIR_model(beta, gamma, t, N, I0, R0):
"""The function solves the SIR model to simulate
the disease spreading"""
S0 = N - I0 - R0
def deriv(y, t, N, beta, gamma):
S, I, R = y
dSdt = -beta * S * I / N
dIdt = beta * S * I / N - gamma * I
@ShuaiGuo16
ShuaiGuo16 / UQ_SIR_Simulation.py
Last active January 13, 2021 14:51
Simulate SIR model
# Assign infection and recovery rate parameters
beta, gamma = 0.22, 0.1
# Grid of time points (in days)
t = np.arange(0, 101, 2)
# Run SIR model to predict epidemic evolution
S, I, R = SIR_model(beta, gamma, t, I0=8, N=1000, R0=0)
# Extract output of interest
@ShuaiGuo16
ShuaiGuo16 / UQ_Uniform.py
Created January 13, 2021 11:37
Generate uniform samples
# Specify random sample number
sample_num = 1000
# Using Latin Hypercube method to generate uniform distribution
uni_samples = lhs(n=2, samples=sample_num, criterion='maximin')
@ShuaiGuo16
ShuaiGuo16 / UQ_Standard.py
Created January 13, 2021 11:41
Turn uniform to standard normal
# Transforming to standard normal distribution
std_norm_samples = np.zeros_like(uni_samples)
for i in range(2):
std_norm_samples[:,i] = norm(loc=0, scale=1).ppf(uni_samples[:,i])