def dual_norm_wparam(model, N, T0, mode, idx, aval, tkey, tval, fd=None): import numpy as np import matplotlib.pyplot as plt import matplotlib.ticker as mticker import operator import math from setup.gtfpath import gtfpath from aux.create_dir import create_dir from aux.cffdic import cffdic from aux.rdatatoarray import rdatatoarray from aux.gptr import gptr from figsetup.style import style from figsetup.text import text from fig_helpers.set_ax import set_ax from fig_helpers.set_pltparams import set_pltparams from save_helpers.mysave import mysave style(1) text() T0 = int(T0) print(model, N, T0, mode, fd) target_dir = './dual_norm/' create_dir(target_dir) search_dir = '../' + model + '_info/dual_norm' if mode == 'all': root, filenames = gtfpath(search_dir, '^.*_' + N + 'nb_.*$') else: root, filenames = gtfpath( search_dir, '^.*_' + N + 'nb_*_h10_(?!.*-90|.*-80|.*-70).*$') ptr = gptr(model, N, T0, mode, fd) files_dict = cffdic(filenames, ptr, idx) dict_final = sorted(files_dict.items(), key=operator.itemgetter(0)) data = rdatatoarray(dict_final, 'dual_norm') solver = model.upper() plot_params = set_pltparams('dual_norm', solver, N, T0, fd) fig, ax = plt.subplots(1, tight_layout=True) set_ax(ax, 'dual_norm', tkey, tval) ax.plot(data[:, 0], data[:, 1], **plot_params) aidx = np.where(data[:, 0] == aval[idx]) ylim_exp = math.ceil(math.log10(min(data[:, 1]))) - 1 ax.set_ylim([10**ylim_exp, None]) ax.plot(aval[idx], data[aidx, 1], 'ro', label='Anchor point') ax.legend(loc=0) #f = mticker.ScalarFormatter(useOffset=False, useMathText=True) #g = lambda x,pos : "${}$".format(f._formatSciNotation('%1.10e' % x)) #plt.gca().yaxis.set_major_formatter(mticker.FuncFormatter(g)) #plt.ticklabel_format(axis="y", style="sci", scilimits=(0,0)) header = tkey[0] + ',' + 'dual_norm' mysave(fig, target_dir, data, 'dual_norm', header, N, fd)
def temp_mrelerr_wparam(model, N, T0, mode, idx, aval, tkey, tval, fd=None): import numpy as np import matplotlib.pyplot as plt import operator from aux.cffdic import cffdic from aux.gptr import gptr from aux.create_dir import create_dir from setup.gtfpath import gtfpath from fig_helpers.set_ax import set_ax from fig_helpers.set_pltparams import set_pltparams from aux.rdatatoarray import rdatatoarray from save_helpers.mysave import mysave from figsetup.style import style from figsetup.text import text style(1) text() T0 = int(T0) target_dir = './temp_mrelerr/' create_dir(target_dir) search_dir = '../' + model + '_info/temp_mrelerr' if mode == 'all': root, filenames = gtfpath(search_dir, '^.*_' + N + 'nb_.*$') else: root, filenames = gtfpath( search_dir, '^.*_' + N + 'nb_.*_h10_(?!.*-90|.*-80|.*-70).*$') ptr = gptr(model, N, T0, mode, fd) files_dict = cffdic(filenames, ptr, idx) dict_final = sorted(files_dict.items(), key=operator.itemgetter(0)) data = rdatatoarray(dict_final, 'mrelerr') solver = model.upper() plot_params1 = set_pltparams('rom_mrelerr', solver, N, T0, fd) plot_params2 = set_pltparams('proj_mrelerr', solver, N, T0, fd) fig, ax = plt.subplots(1, tight_layout=True) set_ax(ax, 'temp_mrelerr', tkey, tval) ax.plot(data[:, 0], data[:, 1], **plot_params1) ax.plot(data[:, 0], data[:, 2], **plot_params2) aidx = np.where(data[:, 0] == aval[idx]) ax.plot(aval[idx], data[aidx, 1], 'ro', label='Anchor point') ax.legend(loc=0, ncol=1) header = tkey[0] + ',' + 'rom_mrelerr' + ',' + 'proj_mrelerr' mysave(fig, target_dir, data, 'mrelerr', header, N, fd)
def grep_feature(filenames, feature, ptr, sdir, tdir): import os import re from aux.create_dir import create_dir fdir = os.path.join(tdir, feature) create_dir(fdir) for fname in filenames: target_dir = os.path.join(fdir, fname+'_'+feature) ft = open(target_dir, 'w') with open(os.path.join(sdir, fname), 'r') as f: for line in f: if re.search(ptr, line): ft.write(line) ft.close() return
def setup_info(src_dir, N=None): """setup info directory argv[1]: source directory argv[2]: N """ from aux.create_dir import create_dir from setup.cfpdic import cfpdic from setup.load_features import load_features from setup.grep_files import grep_files from setup.grep_feature import grep_feature rom_dir = src_dir + '_info' create_dir(rom_dir) root, filenames = grep_files(src_dir, N) sprt_features = cfpdic() features = load_features() for feature in features.keys(): print("---------------------------------------------") grep_feature(filenames, feature, sprt_features[feature], src_dir, rom_dir) print("---------------------------------------------")
def parametric(model, N, T0, mode, fd=None, ifsetup=False): import os import yaml from aux.create_dir import create_dir from setup.setup_info import setup_info from dual_norm.dual_norm_wparam import dual_norm_wparam from dual_norm.vel_dual_norm_wparam import vel_dual_norm_wparam from dual_norm.temp_dual_norm_wparam import temp_dual_norm_wparam from mrelerr.mrelerr_wparam import mrelerr_wparam from mabserr.mabserr_wparam import mabserr_wparam from mrelerr.vel_mrelerr_wparam import vel_mrelerr_wparam from mabserr.vel_mabserr_wparam import vel_mabserr_wparam from mrelerr.temp_mrelerr_wparam import temp_mrelerr_wparam from mabserr.temp_mabserr_wparam import temp_mabserr_wparam from nu_1st_2nd_momentum.nu_1st2nd_momentum_wparam import nu_1st2nd_momentum_wparam # This python script is used to plot parametric results. # You must specify the model and the POD mdoe N. if ifsetup: setup_info(model, N) with open('./parametric.yaml') as f: ainfo = yaml.load(f, Loader=yaml.FullLoader) akey = list(ainfo['anchor'].keys()) akey = [i.split('^', 1)[0] for i in akey] aval = list(ainfo['anchor'].values()) aval = [i for i in aval] idx = akey.index(list(ainfo['train_set'].keys())[0]) al = '_'.join([a + str(b) for a, b in zip(akey, aval)]) tkey = list(ainfo['train_set'].keys()) tval = list(ainfo['train_set'].values()) tdir = './'+model+'_parameter_'+al create_dir(tdir) with open('./features.yaml') as f: features = yaml.load(f, Loader=yaml.FullLoader) fkey = list(features.keys()) os.chdir(tdir) if 'dual_norm' in fkey: dual_norm_wparam(model, N, T0, mode, idx, aval, tkey, tval, fd) if 'nu_1st2nd' in fkey: nu_1st2nd_momentum_wparam(model, N, T0, mode, idx, aval, tkey, tval, fd, iffom=True) if 'vel_dual_norm' in fkey: vel_dual_norm_wparam(model, N, T0, mode, idx, aval, tkey, tval, fd) if 'temp_dual_norm' in fkey: temp_dual_norm_wparam(model, N, T0, mode, idx, aval, tkey, tval, fd) for sol in ['', 'vel_', 'temp_']: for norm in ['h1', 'h10', 'l2']: if sol+'mrelerr_'+norm in fkey: mrelerr_wparam(model, N, T0, mode, idx, aval, tkey, tval, norm, fd) if sol+'mabserr_'+norm in fkey: mabserr_wparam(model, N, T0, mode, idx, aval, tkey, tval, norm, fd) if 'vel_mrelerr' in fkey: vel_mrelerr_wparam(model, N, T0, mode, idx, aval, tkey, tval, norm, fd) if 'vel_mabserr' in fkey: vel_mabserr_wparam(model, N, T0, mode, idx, aval, tkey, tval, norm, fd) if 'temp_mrelerr' in fkey: temp_mrelerr_wparam(model, N, T0, mode, idx, aval, tkey, tval, norm, fd) if 'temp_mabserr' in fkey: temp_mabserr_wparam(model, N, T0, mode, idx, aval, tkey, tval, norm, fd)
def hg_compare(argv): import numpy as np import matplotlib.pyplot as plt import re import os import sys import pandas as pd from aux.helpers import find_files from figsetup.style import style from figsetup.text import text from figsetup.color import color from aux.create_dir import create_dir from compare.helpers import set_ax style(1) markers = ['o', 'v', '*', 'x'] text() print("This is the name of the program:", sys.argv[0]) print("Argument List:", str(sys.argv)) os.chdir(str(sys.argv[1])) feature = str(sys.argv[2]) ncand = str(sys.argv[3]) itr = str(sys.argv[4]) fname = feature+'_'+ncand+'_itr'+itr+'.csv' fnames = find_files(fname, './') ctr = 0 fig, ax = plt.subplots(1, tight_layout=True) for f in fnames: data = pd.read_csv(f) ptr = re.split('/', f) for i in ptr: if 'online' in i: model = i.split('_')[1] ax.plot(data.iloc[:, 0], data.iloc[:, 1], 'b-', marker=markers[ctr], mfc='None', label=model) ctr += 1 ax.legend(loc=0) set_ax(ax, feature, itr) if feature == 'nu_m': fom_mnu_p = [] p = [] fname = 'nus_mom.csv' fnames = find_files(fname, '../') for f in fnames: par = float(re.split('[/]',f)[1].split('_')[1]) if par <= max(data.iloc[:, 0]) and par >= min(data.iloc[:, 0]): dd = pd.read_csv(f) p.append(par) fom_mnu_p.append(dd['mean']) p, fom_mnu_p = zip(*sorted(zip(p, fom_mnu_p))) ax.plot(p, fom_mnu_p, 'ko', label='FOM') ax.legend(loc=0) elif feature == 'nu_std': fom_nustd_p = [] p = [] fname = 'nus_mom.csv' fnames = find_files(fname, '../') for f in fnames: par = float(re.split('[/]',f)[1].split('_')[1]) if par <= max(data.iloc[:, 0]) and par >= min(data.iloc[:, 0]): dd = pd.read_csv(f) p.append(par) fom_nustd_p.append(dd[' std']) p, fom_nustd_p = zip(*sorted(zip(p, fom_nustd_p))) ax.plot(p, fom_nustd_p, 'ko', label='FOM') ax.legend(loc=0) elif feature == 'mtke': list1 = [] p = [] fname = 'tmtke' fnames = find_files(fname, '../') for f in fnames: par = float(re.split('[/]',f)[1].split('_')[1]) if par <= max(data.iloc[:, 0]) and par >= min(data.iloc[:, 0]): dd = np.loadtxt(f) p.append(par) list1.append(dd) p, fom_nustd_p = zip(*sorted(zip(p, list1))) ax.plot(p, list1, 'ko', label='FOM') ax.legend(loc=0) elif feature == 'mtfluc': list1 = [] p = [] fname = 'tmtfluc' fnames = find_files(fname, '../') for f in fnames: par = float(re.split('[/]',f)[1].split('_')[1]) if par <= max(data.iloc[:, 0]) and par >= min(data.iloc[:, 0]): dd = np.loadtxt(f) p.append(par) list1.append(dd) p, fom_nustd_p = zip(*sorted(zip(p, list1))) ax.plot(p, list1, 'ko', label='FOM') ax.legend(loc=0) elif feature == 'rom_fldrelerr': fname = 'proj_fldrelerr'+'_'+ncand+'_itr'+itr+'.csv' fnames = find_files(fname, './') ctr = 0 for f in fnames: data = pd.read_csv(f) ptr = re.split('/', f) for i in ptr: if 'online' in i: model = i.split('_')[1] ax.plot(data.iloc[:, 0], data.iloc[:, 1], 'b--', marker=markers[ctr], mfc='None', label=model+' projection') ctr += 1 ax.legend(loc=0) ax.set_yscale('log') tdir = './hg_compare/' create_dir(tdir) fig.savefig(tdir+feature+'_'+ncand+'_'+itr+'.png') return
def dual_norm_wN(model, T0, anchor=None, fd=None): import numpy as np import matplotlib.pyplot as plt import sys import operator sys.path.append('/Users/bigticket0501/Developer/PyMOR/code/plot_helpers/') import setup import yaml from mor import ROM from snapshot import Snapshot from aux.cffdic import cffdic from aux.gptr import gptr from aux.create_dir import create_dir from aux.sort import sort from setup.gtfpath import gtfpath from figsetup.style import style from figsetup.text import text style(1) text() T0 = int(T0) # print("---------------------------------------------") # print("This is the name of the program:", sys.argv[0]) # print("Argument List:", str(sys.argv)) # os.chdir(str(sys.argv[1])) # model = str(sys.argv[2]) # deg = str(int(sys.argv[3])-90) # print("---------------------------------------------") if anchor is None: with open('../anchor.yaml') as f: features = yaml.load(f, Loader=yaml.FullLoader) akey = list(features.keys()) aval = list(features.values()) aval = [str(i) for i in aval] aval[0] = str(int(aval[0]) - 90) al = '_'.join(aval) target_dir = './dual_norm/' create_dir(target_dir) search_dir = '../' + model + '_info/dual_norm' root, filenames = gtfpath(search_dir, '^.*_h10_' + al + '_.*$') ptr = gptr(model, None, T0, None, fd) files_dict = cffdic(filenames, ptr, 0) dict_final = sorted(files_dict.items(), key=operator.itemgetter(0)) roms = [] for nb, fnames in dict_final: for fname in fnames: print(fname) rom = ROM(fname) rom.DTAR() rom.anchor('theta') print(rom.anchor('theta')) roms.append(rom) data = sort(roms, 'nb', 'dtar') solver = rom.info['method'] anchor = str(int(rom.info['anchor'])) print(rom.anchor('anchor')) 1 / o fig, ax = plt.subplots(1, tight_layout=True) plot_params = { 'c': 'k', 'marker': 'o', 'mfc': 'None', 'label': solver + ' with ' + r'$\theta^*_g = ' + anchor + '$' } ax.set(ylabel=r'$\triangle(\theta_g=' + str(int(deg) + 90) + ')$', xlabel=r'$N$', ylim=[10**np.floor(np.log10(min(data[:, 1]))), 1], xlim=[1, max(data[:, 0])]) ax.semilogy(data[:, 0], data[:, 1], **plot_params) print("---------------------------------------------") fig.savefig('.' + target_dir + 'dual_norm_theta_' + str(int(deg) + 90) + '.png') print("---------------------------------------------") plt.show() header = tkey[0] + ',' + 'dual_norm' np.savetxt('.' + target_dir + 'N_list_' + str(int(deg) + 90) + '.dat', data[:, 0]) np.savetxt('.' + target_dir + 'erri_theta_' + str(int(deg) + 90) + '.dat', data[:, 1])
def conv_compare(argv): import yaml import os import re import pandas as pd import numpy as np import matplotlib.pyplot as plt from figsetup.style import style from figsetup.text import text from aux.create_dir import create_dir sys.path.append('/home/pht2/Developer/PyROM/code/plot_helpers/') import reader style(1) text() models = [] for i in range(len(argv) - 1): models.append(argv[i]) feature = argv[-1] fig, ax = plt.subplots(1, tight_layout=True) for model in models: dir1 = model + '_reproduction' with open('reproduction.yaml') as f: info = yaml.load(f, Loader=yaml.FullLoader) for key, value in info['parameters'].items(): al = '_'.join([str(key), str(value)]) dir1 = '_'.join([dir1, al]) if model == 'l-rom': fd = info['perc'].replace('p', '.') fd = str(int(float(fd) * 100)) solver = model.upper() + ' with ' + str( fd) + ' percentage filtered' fname = feature + '_' + info['perc'] + '.csv' elif model == 'l-rom-df': fd = info['fwidth'].replace('p', '.') solver = model.upper() + r' with filter width $\delta=$ ' + str(fd) fname = feature + '_' + info['fwidth'] + '.csv' else: solver = model.upper() fname = feature + '.csv' tpath = reader.find_files(fname, './') print(tpath) for f in tpath: if model in re.split('[/_]', f): fn = f # tpath = os.path.join(dir1, feature, fname) data = pd.read_csv(fn) ax.plot(data.iloc[:, 0], data.iloc[:, 1], '-o', label=solver) ax.legend(loc=0) anc_lb = [] for key, value in info['parameters'].items(): if key == 'theta': anc_lb.append('\\' + str(key) + '^*_g=' + str(value)) else: anc_lb.append(str(key) + '^*=' + str(value)) anc_lb = ', '.join(anc_lb) if feature == 'mrelerr_h1': title = 'Relative error in the predicted mean flow at ' + '$' + anc_lb + '$' ax.set( xlabel=r'$N$', ylabel= r'$\frac{\|\langle \bf{u} - \bf{\tilde{u}} \rangle\|_{H^1}}{\|\langle \bf{u} \rangle\|_{H^1}}$', ylim=[1e-3, 1], title=title) ax.set_yscale('log') elif feature == 'mtke': title = 'Predicted mean TKE at ' + '$' + anc_lb + '$' ax.set(xlabel=r'$N$', ylabel=r'$\langle TKE \rangle_g$', title=title) ax.set_yscale('log') mtke_fom = np.loadtxt('../qoi/tmtke') fom_params = {'c': 'k', 'marker': 'o', 'label': 'FOM'} ax.plot(data.iloc[:, 0], mtke_fom * np.ones(len(data.iloc[:, 0])), **fom_params) elif feature == 'dual_norm': title = 'Dual norm at ' + '$' + anc_lb + '$' ax.set(xlabel=r'$N$', ylabel=r'$\triangle$', title=title, ylim=[1e-4, 1]) ax.set_yscale('log') elif feature == 'mtfluc': title = 'Predicted mean fluctuation in temperature at ' + '$' + anc_lb + '$' ax.set(xlabel=r'$N$', ylabel=r'$\langle T_{fluc} \rangle_s$', title=title) mtfluc_fom = np.loadtxt('../qoi/tmtfluc') fom_params = {'c': 'k', 'marker': 'o', 'label': 'FOM'} ax.plot(data.iloc[:, 0], mtfluc_fom * np.ones(len(data.iloc[:, 0])), **fom_params) elif feature == 'mnu': filename = './fom/nus_mom.csv' fom = pd.read_csv(filename).to_numpy() title = 'Predicted mean Nu at ' + '$' + anc_lb + '$' ax.set(xlabel=r'$N$', ylabel=r'$\langle Nu \rangle_s$', title=title) fom_params = {'c': 'k', 'marker': 'o', 'label': 'FOM'} ax.plot(data.iloc[:, 0], fom[0][0] * np.ones(len(data.iloc[:, 0])), **fom_params) elif feature == 'stdnu': filename = './fom/nus_mom.csv' fom = pd.read_csv(filename).to_numpy() title = 'Predicted std(Nu) at ' + '$' + anc_lb + '$' ax.set(xlabel=r'$N$', ylabel=r'Std(Nu)', title=title) fom_params = {'c': 'k', 'marker': 'o', 'label': 'FOM'} ax.plot(data.iloc[:, 0], fom[0][1] * np.ones(len(data.iloc[:, 0])), **fom_params) elif feature == 'mnu_err': title = 'Relative error in mean Nu at ' + '$' + anc_lb + '$' ax.set(xlabel=r'$N$', title=title) ax.set_yscale('log') ax.legend(loc=0) tdir = './compare/' create_dir(tdir) fig.savefig(tdir + feature + '_conv_compare.png') return
def nu_1st2nd_momentum_wparam(model, N, T0, mode, idx, aval, tkey, tval, fd=None, iffom=False): import numpy as np import matplotlib.pyplot as plt import operator import math from aux.cffdic import cffdic from aux.gptr import gptr from aux.create_dir import create_dir from setup.gtfpath import gtfpath from fig_helpers.set_ax import set_ax from fig_helpers.set_pltparams import set_pltparams from aux.rdatatoarray import rdatatoarray from fom_qois.fom_qois import fom_qois from save_helpers.mysave import mysave from figsetup.style import style from figsetup.text import text style(1) text() T0 = int(T0) target_dir = './nu/' create_dir(target_dir) search_dir = '../' + model + '_info/nu' if mode == 'all': root, filenames = gtfpath(search_dir, '^.*_' + N + 'nb_.*$') else: root, filenames = gtfpath( search_dir, '^.*_' + N + 'nb_.*_h10_(?!.*-90|.*-80|.*-70).*$') ptr = gptr(model, N, T0, mode, fd) files_dict = cffdic(filenames, ptr, idx) dict_final = sorted(files_dict.items(), key=operator.itemgetter(0)) rom_data = rdatatoarray(dict_final, 'nu_1st2nd', T0) solver = model.upper() if iffom: params = [int(i[0]) for i in dict_final] fom_data = fom_qois(params, 'nu_1st2nd') fig1, ax1 = plt.subplots(1, tight_layout=True) set_ax(ax1, 'mnu', tkey, tval) plot_params = set_pltparams('nu_1st2nd', solver, N, T0, fd) ax1.plot(rom_data[:, 0], rom_data[:, 1], **plot_params) if iffom: FOM_params = set_pltparams('FOM', solver, N, T0, fd) ax1.plot(fom_data[:, 0], fom_data[:, 1], **FOM_params) aidx = np.where(rom_data[:, 0] == aval[idx]) ylim_exp = math.ceil(math.log10(min(rom_data[:, 1]))) - 1 ax1.set_ylim([10**ylim_exp, None]) ax1.plot(aval[idx], rom_data[aidx, 1], 'ro', label='Anchor point') ax1.legend(loc=1) fig2, ax2 = plt.subplots(1, tight_layout=True) set_ax(ax2, 'std_nu', tkey, tval) plot_params = set_pltparams('nu_1st2nd', solver, N, T0, fd) ax2.plot(rom_data[:, 0], rom_data[:, 2], **plot_params) if iffom: ax2.plot(fom_data[:, 0], fom_data[:, 2], **FOM_params) ax2.plot(aval[idx], rom_data[aidx, 2], 'ro', label='Anchor point') ax2.legend(loc=1) if iffom: mnu_relerr = abs(fom_data[:, 1] - rom_data[:, 1]) / abs(fom_data[:, 1]) stdnu_relerr = abs(fom_data[:, 2] - rom_data[:, 2]) / abs(fom_data[:, 2]) fig3, ax3 = plt.subplots(1, tight_layout=True) set_ax(ax3, 'mnurelerr', tkey, tval) plot_params = set_pltparams('mnurelerr', solver, N, T0, fd) ax3.semilogy(fom_data[:, 0], mnu_relerr, **plot_params) aidx = np.where(fom_data[:, 0] == aval[idx]) ylim_exp = math.ceil(math.log10(min(mnu_relerr))) - 1 ax3.set_ylim([10**ylim_exp, None]) ax3.semilogy(aval[idx], mnu_relerr[aidx], 'ro', label='Anchor point') ax3.legend(loc=1) header = tkey[0] + ',' + 'mnurelerr' mysave(fig3, target_dir, np.stack([fom_data[:, 0], mnu_relerr], axis=-1), 'mnurelerr', header, N, fd) header = tkey[0] + ',' + 'mnu' mysave(fig1, target_dir, np.stack([rom_data[:, 0], rom_data[:, 1]], axis=-1), 'mnu', header, N, fd) header = tkey[0] + ',' + 'std_nu' mysave(fig2, target_dir, np.stack([rom_data[:, 0], rom_data[:, 2]], axis=-1), 'std_nu', header, N, fd)