Skip to content

Commit

Permalink
Merge pull request #2 from kcwongaz/Refactor
Browse files Browse the repository at this point in the history
Refactor
  • Loading branch information
kcwongaz committed Jul 24, 2022
2 parents dd81a53 + 3507e1f commit e67e606
Show file tree
Hide file tree
Showing 16 changed files with 1,489 additions and 57 deletions.
91 changes: 91 additions & 0 deletions characteristics/loops/hist-chain.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

from loop_helpers import *


# --------------------------------------------------------------------------- #
# Target datetime range to plot flight on; UTC 16:00 is HKT 00:00
target_start = pd.to_datetime("2018-01-01 00:00", format=r"%Y-%m-%d %H:%M")
target_end = pd.to_datetime("2018-12-31 23:59", format=r"%Y-%m-%d %H:%M")
target_start_str = target_start.strftime(r"%Y%m%d_%H%M")
target_end_str = target_end.strftime(r"%Y%m%d_%H%M")
dstr = f"{target_start_str}-{target_end_str}"

datadir = f"/home/kc/Research/air_traffic/data/redirection"
savedir = f"/home/kc/Research/air_traffic/figures/2022-04-26"
mode = 2

plt.rcParams.update({
"font.family": "serif",
"font.sans-serif": ["Helvetica"]}
)

savefig = False


# --------------------------------------------------------------------------- #

chain = np.loadtxt(f"{datadir}/chain_all_{dstr}.txt")

fig, ax = plt.subplots()


edges = np.arange(-0.5, 16, 1)
centers = (edges[:-1] + edges[1:]) / 2

hist, _ = np.histogram(chain, bins=edges)
print(hist)
print(sum(hist))


# --------------------------------------------------------------------------- #
if mode == 1:
ax.bar(centers, hist, color="darkorange")
ax.set_xticks(centers)

for rect in ax.patches:
height = rect.get_height()
ax.annotate(f'{int(height)}', xy=(rect.get_x()+rect.get_width()/2, height),
xytext=(0, 3), textcoords='offset points',
ha='center', va='bottom', fontsize=8, color="darkorange")

# ax.set_ylim(0, 3500)
ax.set_xlabel("Length of Redirection Chain", fontsize=14)
ax.set_ylabel("Count", fontsize=14)

ax.set_title("Histogram of Redirection Chains (2017)", fontsize=16)

plt.tight_layout()

if savefig:
plt.savefig(f"{savedir}/hist_chains.png", dpi=300)
plt.show()


# --------------------------------------------------------------------------- #
elif mode == 2:

hist[1:-1] = hist[1:-1] - hist[2:]
print(np.sum(np.arange(len(hist)) * hist))

ax.bar(centers, hist, color="salmon")
ax.set_xticks(centers)

for rect in ax.patches:
height = rect.get_height()
ax.annotate(f'{int(height)}', xy=(rect.get_x()+rect.get_width()/2, height),
xytext=(0, 3), textcoords='offset points',
ha='center', va='bottom', fontsize=8, color="salmon")

# ax.set_ylim(0, 3500)
ax.set_xlabel("Length of Redirection Chain", fontsize=14)
ax.set_ylabel("Count", fontsize=14)

ax.set_title("Histogram of Unqiue Chains (2017)", fontsize=16)

plt.tight_layout()
if savefig:
plt.savefig(f"{savedir}/hist_unqiue_chains.png", dpi=300)
plt.show()
112 changes: 112 additions & 0 deletions characteristics/loops/hist-time_saved.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

from loop_helpers import *


# --------------------------------------------------------------------------- #
# Target datetime range to plot flight on; UTC 16:00 is HKT 00:00
target_start = pd.to_datetime("2018-01-01 00:00", format=r"%Y-%m-%d %H:%M")
target_end = pd.to_datetime("2018-12-31 23:59", format=r"%Y-%m-%d %H:%M")
target_start_str = target_start.strftime(r"%Y%m%d_%H%M")
target_end_str = target_end.strftime(r"%Y%m%d_%H%M")
dstr = f"{target_start_str}-{target_end_str}"

datadir = f"/home/kc/Research/air_traffic/data/redirection"
savedir = f"/home/kc/Research/air_traffic/figures/2022-04-26"

mode = 1

plt.rcParams.update({
"font.family": "serif",
"font.sans-serif": ["Helvetica"]}
)

savefig = False

# --------------------------------------------------------------------------- #

if mode == 1:
ts = np.loadtxt(f"{datadir}/ts_all_{dstr}.txt")
print(len(ts))
ts = ts[np.nonzero(ts)]
print(len(ts))

fig, ax = plt.subplots()

bins = np.arange(0, 121, 1)
ax.hist(ts, bins=bins, color="royalblue", rwidth=0.8)
ax.set_xticks(np.arange(0, 121, 10))

ax.set_xlabel("Total Time Saved $S$ (min)", fontsize=14)
ax.set_ylabel("Count")
ax.set_title("Histogram of Total Time Saved (2017)", fontsize=16)

print("Max: ", np.max(ts))
print("Min: ", np.min(ts))
print("Mean: ", np.mean(ts))
print("SD: ", np.std(ts), np.std(ts, ddof=1))


plt.tight_layout()
if savefig:
plt.savefig(f"{savedir}/hist_s_{dstr}.png", dpi=300)
plt.show()


elif mode == 2:
ts = np.loadtxt(f"{datadir}/ts1_all_{dstr}.txt")
ts = ts[np.nonzero(ts)]

fig, ax = plt.subplots()

bins = np.arange(0, 41, 1)
ax.hist(ts, bins=bins, color="mediumpurple", rwidth=0.8)
ax.set_xticks(np.arange(0, 41, 5))

ax.set_xlabel("First-step Time Saved $S_1$ (min)", fontsize=14)
ax.set_ylabel("Count")
ax.set_title("Histogram of First-step Time Saved (2017)", fontsize=16)

print("Max: ", np.max(ts))
print("Min: ", np.min(ts))
print("Mean: ", np.mean(ts))
print("SD: ", np.std(ts), np.std(ts, ddof=1))

plt.tight_layout()
if savefig:
plt.savefig(f"{savedir}/hist_s1_{dstr}.png", dpi=300)
plt.show()


elif mode == 3:
ts = np.loadtxt(f"{datadir}/ts_all_{dstr}.txt")
ts = ts[np.nonzero(ts)]

fig, ax = plt.subplots()

bins = np.arange(0, 251, 5)
centers = (bins[:-1] + bins[1:]) / 2

hist, _ = np.histogram(ts, bins=bins)

ind = np.nonzero(hist)
hist = hist[ind]
centers = centers[ind]

ax.plot(centers, hist, marker="o", color="royalblue", lw=1)

# ax.set_xticks(np.arange(0, 121, 10))
ax.set_xscale("log")
ax.set_yscale("log")

ax.set_xlabel("Total Time Saved $S$ (min)", fontsize=14)
ax.set_ylabel("Count")
ax.set_title("Histogram of Total Time Saved (2017)", fontsize=16)

print("Max: ", np.max(ts))
plt.tight_layout()
if savefig:
plt.savefig(f"{savedir}/hist_s_logscale.png", dpi=300)
plt.show()
Loading

0 comments on commit e67e606

Please sign in to comment.