Refactor code

This commit is contained in:
2023-03-17 21:49:53 +00:00
parent 1b00fb5253
commit 3b631a94f8
8 changed files with 8 additions and 9 deletions
+1 -1
View File
@@ -13,7 +13,7 @@
"import matplotlib.pyplot as plt\n",
"import scipy\n",
"from glob import glob\n",
"from notebooks.lib import read_xyz_alt, read_load, linear, mean_across"
"from lib.lib import read_xyz_alt, read_load, linear, mean_across"
]
},
{
-26
View File
@@ -1,26 +0,0 @@
import numpy as np
import scipy
from matplotlib import pyplot as plt
from notebooks.lib import linear
def cr_n(df, ignore_prefix=0, data_color="tab:blue", fit_color="tab:red"):
p, pcov = scipy.optimize.curve_fit(linear, np.log(df.cr[df.N > ignore_prefix]), np.log(df.N[df.N > ignore_prefix]))
linear_extent = np.linspace(0, np.max(np.log(df.cr)))
plt.scatter(
np.log(df.cr),
np.log(df.N),
s=1, marker='.', color=data_color
)
plt.plot(
linear_extent,
linear(linear_extent, *p),
color=fit_color
)
plt.xlabel("$\\log r_{max}$")
plt.ylabel("$\\log N$")
return p, pcov
-160
View File
@@ -1,160 +0,0 @@
import os
from glob import glob
from pathlib import Path
import numpy as np
import pandas as pd
def read_xy(path: str):
df = pd.read_csv(path, skipinitialspace=True)
df['N'] = df.index + 1
df['r'] = (df.x ** 2 + df.y ** 2) ** 0.5
df['cr'] = df.r.cummax()
df['fd'] = np.log(df.N) / np.log(df.cr)
df['run'] = os.path.splitext(Path(path).name)[0]
return df.replace([np.inf, -np.inf], np.nan).dropna()
def read_xy_alt(path: str):
df = pd.read_csv(path)
df['N'] = df.index + 1
# Find the outermost corner of this object
df['r'] = (
(df.x.abs() + np.sqrt(0.5)) ** 2 +
(df.y.abs() + np.sqrt(0.5)) ** 2
) ** 0.5
df['cr'] = df.r.cummax()
df['fd'] = np.log(df.N) / np.log(df.cr)
df['run'] = os.path.splitext(Path(path).name)[0]
return df
def read_xyz(path: str):
df = pd.read_csv(path)
df['N'] = df.index + 1
df['r'] = (df.x ** 2 + df.y ** 2 + df.z ** 2) ** 0.5
df['cr'] = df.r.cummax()
df['fd'] = np.log(df.N) / np.log(df.cr)
df['run'] = os.path.splitext(Path(path).name)[0]
return df
def read_xyz_alt(path: str):
df = pd.read_csv(path)
df['N'] = df.index + 1
# Find the outermost corner of this object
df['r'] = (
(df.x.abs() + np.sqrt(0.5)) ** 2 +
(df.y.abs() + np.sqrt(0.5)) ** 2 +
(df.z.abs() + np.sqrt(0.5)) ** 2
) ** 0.5
df['cr'] = df.r.cummax()
df['fd'] = np.log(df.N) / np.log(df.cr)
df['run'] = os.path.splitext(Path(path).name)[0]
return df
def read_load(load_dir: str, reader=read_xy_alt):
paths = glob(f'{load_dir}/*.csv')
return pd.concat([reader(path) for path in paths])
def augment_read_with_sp(inner_reader):
def hoc(path: str):
probability = float(Path(path).parent.name)
df = inner_reader(path)
df['probability'] = probability
return df
return hoc
def read_sp_xy(specific_probability_dir: str):
probability = float(Path(specific_probability_dir).name)
df = read_load(specific_probability_dir)
df['probability'] = probability
return df
def read_sp(sp_dir: str, inner_reader=read_xy_alt):
if not Path(sp_dir).exists():
raise Exception("Root does not exist")
reader = augment_read_with_sp(inner_reader)
return pd.concat([
read_load(specific_probability_dir, reader)
for specific_probability_dir in glob(f'{sp_dir}/*')
])
def convergent_tail_index(series, tol):
diffs = np.abs(np.ediff1d(series))
for i in range(0, len(diffs)):
if np.max(diffs[i:]) <= tol:
return i
# No convergence found
return None
def mean_of_tail(series, tol=0.05):
tail_index = convergent_tail_index(series, tol)
if tail_index is None:
raise Exception("No convergence found.")
return np.mean(series[tail_index:])
def std_of_tail(series, tol=0.05):
tail_index = convergent_tail_index(series, tol)
if tail_index is None:
raise Exception("No convergence found.")
return np.std(series[tail_index:])
def fd_stats(dfs):
fds = [mean_of_tail(df.fd, 0.1) for df in dfs]
fds_clean = [f for f in fds if f < np.inf]
return np.mean(fds_clean), np.mean(fds_clean) / np.sqrt(fds_clean.length())
def linear(x, a, b):
return x * a + b
def mean_across(df):
runs = df.run.unique().size
data = df.groupby('N').agg({'fd': ['mean', 'std', ['stderr', lambda fd: np.std(fd) / np.sqrt(runs)]]}) \
.reset_index() \
.replace([np.inf, -np.inf], np.nan)
return data
def aggregate_sp_fd(df):
by_run = df.groupby(['probability', 'N'])
by_probability = by_run.agg(
overall_fd=('fd', lambda fd: np.mean(fd[-100:])),
overall_fd_std=('fd', 'std')
).reset_index().groupby('probability')
data = by_probability.agg(
fd=('overall_fd', 'mean'),
# TODO Check stats
fd_std=('overall_fd_std', lambda std: np.sqrt(np.mean(np.square(std))))
)
return data
-36
View File
@@ -1,36 +0,0 @@
import numpy as np
import matplotlib.pyplot as plt
from notebooks.lib import read_load
alpha = read_load("../data/alpha")
meaned_by_N = alpha.groupby('N').agg({'fd': ['mean', 'std']}) \
.reset_index() \
.replace([np.inf, -np.inf], np.nan)
without_prefix = meaned_by_N[50:]
fig, ax = plt.subplots(figsize=(6, 6))
plt.fill_between(
without_prefix.N,
# TODO Check error math here
(without_prefix['fd']['mean'] - without_prefix['fd']['std'] / np.sqrt(20)),
(without_prefix['fd']['mean'] + without_prefix['fd']['std'] / np.sqrt(20)),
alpha=0.2, label=f"Standard error band"
)
plt.plot(
without_prefix.N,
without_prefix['fd']['mean'],
color='tab:blue', label='fd mean, seeds = 20'
)
plt.plot([50, 10000], [1.71, 1.71], color='red', label='Theory')
plt.fill_between(without_prefix.N, 1.71 - 0.01, 1.71 + 0.01, alpha=0.2, label='Theory error band')
plt.xlabel("$N_C$")
plt.ylabel("$fd$ (instantaneous)")
plt.legend()
plt.savefig('../figures/nc-fd-convergence.svg')
plt.savefig('../figures/nc-fd-convergence.png')
plt.show()
-15
View File
@@ -1,15 +0,0 @@
import matplotlib.pyplot as plt
from notebooks.graphs import cr_n
from notebooks.lib import read_load
alpha = read_load("../data/alpha")
cr_n(
alpha,
ignore_prefix=50
)
plt.savefig('../figures/rmax-n.svg')
plt.savefig('../figures/rmax-n.png')
plt.show()
-18
View File
@@ -1,18 +0,0 @@
from matplotlib import pyplot as plt
from notebooks.lib import read_sp, read_xyz_alt, aggregate_sp_fd
data_3d_sp = read_sp("../data/rust-3d-offaxis-sp", read_xyz_alt)
sp_fd_data = aggregate_sp_fd(data_3d_sp)
# %%
plt.fill_between(sp_fd_data.index, sp_fd_data.fd - sp_fd_data.fd_std, sp_fd_data.fd + sp_fd_data.fd_std, alpha=0.2, label=f"Standard error band")
plt.plot(sp_fd_data.index, sp_fd_data.fd, color='tab:blue', label='fd mean, seeds = 100')
plt.xlabel("$p_{stick}$")
plt.ylabel("$fd$")
plt.legend()
plt.savefig('../figures/sp-fd-3d.svg')
plt.savefig('../figures/sp-fd-3d.png')
plt.show()
-53
View File
@@ -1,53 +0,0 @@
import numpy as np
from matplotlib import pyplot as plt
from notebooks.lib import read_sp, aggregate_sp_fd
c_sp = read_sp("../data/stick-probability")
rust_sp = read_sp("../data/rust-sticking-probability")
# %%
c_data = aggregate_sp_fd(c_sp)
rust_data = aggregate_sp_fd(rust_sp)
# %%
# plt.fill_between(
# c_data.index,
# c_data.fd - c_data.fd_std,
# c_data.fd + c_data.fd_std,
# alpha=0.2,
# color='tab:blue',
# label=f"IPC + PS, Standard error band"
# )
plt.plot(
c_data.index,
c_data.fd,
color='tab:blue',
label='IPC + PS, fd mean, seeds = 100'
)
# plt.fill_between(
# rust_data.index,
# rust_data.fd - rust_data.fd_std,
# rust_data.fd + rust_data.fd_std,
# alpha=0.2,
# color='tab:orange',
# label=f"NF, Standard error band"
# )
plt.plot(
rust_data.index,
rust_data.fd,
color='tab:orange',
label='NF, fd mean, seeds = 100'
)
plt.xlabel("$p_{stick}$")
plt.ylabel("$fd$")
plt.legend()
plt.savefig('../figures/sp-fd-rust-vs-c.svg')
plt.savefig('../figures/sp-fd-rust-vs-c.png')
plt.show()
-18
View File
@@ -1,18 +0,0 @@
from matplotlib import pyplot as plt
from notebooks.lib import read_sp, aggregate_sp_fd
data_2d_sp = read_sp("../data/stick-probability")
sp_fd_data = aggregate_sp_fd(data_2d_sp)
# %%
plt.fill_between(sp_fd_data.index, sp_fd_data.fd - sp_fd_data.fd_std, sp_fd_data.fd + sp_fd_data.fd_std, alpha=0.2, label=f"Standard error band")
plt.plot(sp_fd_data.index, sp_fd_data.fd, color='tab:blue', label='fd mean, seeds = 100')
plt.xlabel("$p_{stick}$")
plt.ylabel("$fd$")
plt.legend()
plt.savefig('../figures/sp-fd.svg')
plt.savefig('../figures/sp-fd.png')
plt.show()