Commit 9707f3da authored by niklas.baumgarten's avatar niklas.baumgarten
Browse files

plotting for paper

parent be57e28e
import os
import subprocess
from datetime import datetime
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
......@@ -10,6 +10,12 @@ from mpp.python.mppy import Mpp, PROJECT_ROOT_DIR, PROJECT_PY_DATA_DIR
SPRNG5_DIR = os.path.abspath(os.path.join(PROJECT_ROOT_DIR, 'sprng5'))
font = {'family': 'DejaVu Sans',
'weight': 'normal',
'size': 14}
matplotlib.rc('font', **font)
class MLMCMpp(Mpp):
CONVERGENCE_HEAD = ' l M E[Qf-Qc] E[Qf] V[Qf-Qc]' \
......@@ -91,7 +97,7 @@ class MLMCMpp(Mpp):
eps = float(matches[0])
value = float(matches[1])
mlmc_cost = float(matches[2])
mc_cost = np.power(eps, -3) # Todo: adpat to problem
mc_cost = np.power(eps, -4)
rest_length = len(matches[3:])
levels = [int(match) for match in matches[3:3 + int(rest_length / 2)]]
......@@ -146,12 +152,12 @@ class MLMCMpp(Mpp):
axes[1].set_xticks(df.index)
axes[1].grid(which='major')
def plot_mlmc_table(self, df, axes):
def plot_mlmc_table(self, df, axes, show_mc=False):
if df.empty:
return
self.sample_vs_level_plot(axes, df)
self.eps_vs_cost_plot(axes, df)
self.eps_vs_cost_plot(axes, df, show_mc=show_mc)
@staticmethod
def sample_vs_level_plot(axes, df):
......@@ -169,16 +175,21 @@ class MLMCMpp(Mpp):
axes[0].grid(which='major')
@staticmethod
def eps_vs_cost_plot(axes, df):
eps_cost_mlmc = np.square(df.index) * df['MLMC cost']
def eps_vs_cost_plot(axes, df, show_mc=False, title=''):
eps_cost_mlmc = np.power(df.index, 2) * df['MLMC cost']
eps_cost_mc = np.power(df.index, 2) * df['MC cost']
axes[1].plot(df.index, eps_cost_mlmc, ls='--', label='MLMC cost',
linewidth=2, marker='o', markersize=6)
if show_mc:
axes[1].plot(df.index, eps_cost_mc, ls='-', label='MC cost',
linewidth=2, marker='o', markersize=6)
axes[1].set_title(r'Compared MLMC with idealized MC')
axes[1].set_title(r'MLMC cost scaled with $\epsilon^2$')
axes[1].set_ylabel(r'$\epsilon^2$ Cost')
axes[1].set_xlabel(r'$\epsilon$')
axes[1].set_xscale('log')
axes[1].set_yscale('log')
axes[1].set_ylim(1000, 1000000)
axes[1].legend()
axes[1].grid(which='major')
......@@ -205,8 +216,8 @@ class MLMCMpp(Mpp):
else:
plt.show()
def show_mlmc_table(self, dfs=None, labels=None, plot_name=None):
dfs = self.create_mlmc_table_from_log(dfs)
def show_mlmc_table(self, dfs=None, labels=None, plot_name=None, show_mc=False):
dfs = self.create_mlmc_table(dfs)
dfs = dfs if isinstance(dfs, list) else [dfs]
labels = labels if labels is not None else [None for df in dfs]
labels = labels if isinstance(labels, list) else [labels]
......@@ -215,10 +226,11 @@ class MLMCMpp(Mpp):
fig.set_figheight(5)
fig.set_figwidth(14)
for df, label in zip(dfs, labels):
self.plot_mlmc_table(df, axes)
self.plot_mlmc_table(df, axes, show_mc=show_mc)
if plot_name is not None:
path = PROJECT_PY_DATA_DIR + 'MLMC_{}_{}.png'.format(plot_name, self.now)
path = os.path.abspath(os.path.join(
PROJECT_PY_DATA_DIR, 'MLMC_{}_{}.png'.format(plot_name, self.now)))
plt.savefig(path)
else:
plt.show()
......@@ -236,7 +248,7 @@ class MLMCMpp(Mpp):
df = self.parse_convergence_table(log_content)
return df
def create_mlmc_table_from_log(self, df=None):
def create_mlmc_table(self, df=None):
if df is None:
log_content = self.read_log()
df = self.parse_mlmc_table(log_content)
......@@ -248,7 +260,7 @@ class MLMCMpp(Mpp):
print(df)
def print_mlmc_table(self, df):
self.create_mlmc_table_from_log(df)
self.create_mlmc_table(df)
self.prepare_pd_table()
print(df)
......@@ -258,17 +270,15 @@ class MLMCMpp(Mpp):
df.to_csv(path)
def save_mlmc_table(self, df, table_name=None):
self.create_mlmc_table_from_log(df)
self.create_mlmc_table(df)
path = PROJECT_PY_DATA_DIR + 'MLMC_{}_{}.csv'.format(table_name, self.now)
df.to_csv(path)
def clean_sprng5(self):
self.clean_directory(SPRNG5_DIR, True)
mpp = MLMCMpp()
if __name__ == "__main__":
mpp.clean_sprng5()
mpp.clean_build()
mpp.build()
path = os.path.dirname(os.path.realpath(__file__))
content = mpp.read_log(log_file='mlmc', log_dir=path)
mlmc_table = mpp.parse_mlmc_table(content)
mpp.show_mlmc_table(mlmc_table, plot_name='transport', show_mc=False)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment