Coverage for pySDC/projects/soft_failure/visualization_helper.py: 100%
54 statements
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-20 14:51 +0000
« prev ^ index » next coverage.py v7.6.9, created at 2024-12-20 14:51 +0000
1import os
3import matplotlib
4import numpy as np
5from matplotlib import rc
7from pySDC.helpers.stats_helper import filter_stats
9matplotlib.use('Agg')
10import matplotlib.pyplot as plt
13def show_residual_across_simulation(stats, fname):
14 """
15 Helper routine to visualize the residuals dependent on the number of iterations across the simulation
17 Args:
18 stats (dict): statistics object
19 fname (str): filename
20 """
22 # get residuals of the run
23 extract_stats = filter_stats(stats, type='residual_post_iteration')
25 # find boundaries for x-,y-axis as well as arrays
26 maxiter = 0
27 for k, _ in extract_stats.items():
28 maxiter = max(maxiter, k.iter)
30 # grep residuals and put into array
31 residual = np.zeros(maxiter)
32 residual[:] = -99
33 for k, v in extract_stats.items():
34 if k.iter != -1:
35 residual[k.iter - 1] = np.log10(v)
37 # Set up latex stuff and fonts
38 rc('font', **{"sans-serif": ["Arial"], "size": 30})
39 rc('legend', fontsize='small')
40 rc('xtick', labelsize='small')
41 rc('ytick', labelsize='small')
43 # create plot and save
44 fig, ax = plt.subplots(figsize=(15, 10))
46 ax.set_xlabel('iteration')
47 ax.set_ylabel('log10(residual)')
49 plt.axis([0, 14, -12, 3])
50 plt.plot(np.linspace(1, maxiter, num=maxiter), residual)
52 plt.savefig(fname)
54 assert os.path.isfile(fname), 'ERROR: plotting did not create PNG file'
57def show_min_max_residual_across_simulation(fname, minres, maxres, meanres, medianres, maxiter):
58 """
59 Helper routine to visualize the minimal, maximal, mean, median residual vectors dependent on the
60 number of iterations across the simulation
62 Args:
63 stats (dict): statistics object
64 fname (str): filename
65 minres: minimal residual vector
66 maxres: maximal residual vector
67 meanres: mean residual vector
68 medianres: median residual vector
69 maxiter (int): length of residual vectors, maximal iteration index
70 """
72 # Set up latex stuff and fonts
73 rc('font', **{"sans-serif": ["Arial"], "size": 30})
74 rc('legend', fontsize='small')
75 rc('xtick', labelsize='small')
76 rc('ytick', labelsize='small')
78 # create plot and save
79 fig, ax = plt.subplots(figsize=(15, 10))
81 ax.set_xlabel('iteration')
82 ax.set_ylabel('log10(residual)')
84 plt.plot(np.linspace(1, maxiter, num=maxiter), np.log10(minres), 'ob--', label='min')
85 plt.plot(np.linspace(1, maxiter, num=maxiter), np.log10(maxres), 'og--', label='max')
86 plt.plot(np.linspace(1, maxiter, num=maxiter), np.log10(meanres), 'or--', label='mean')
87 plt.plot(np.linspace(1, maxiter, num=maxiter), np.log10(medianres), 'oy--', label='median')
88 plt.fill_between(
89 np.linspace(1, maxiter, num=maxiter), np.log10(minres), np.log10(maxres), color='grey', alpha=0.3, label='range'
90 )
91 plt.axis([0, 14, -12, 3])
92 plt.legend()
94 plt.savefig(fname)
96 assert os.path.isfile(fname), 'ERROR: plotting did not create PNG file'
99def show_iter_hist(fname, maxiter, nruns):
100 """
101 Helper routine to visualize the maximal iteration number across the simulation in a histogram
103 Args:
104 stats (dict): statistics object
105 fname (str): filename
106 maxiter: maximal iterations per run
107 nruns: number of runs
108 """
110 # create plot and save
111 fig, ax = plt.subplots(figsize=(15, 10))
113 plt.hist(maxiter, bins=np.arange(min(maxiter), max(maxiter) + 2, 1), align='left', rwidth=0.9)
115 # with correction allowed: axis instead of xticks
116 # plt.axis([12, 51, 0, nruns+1])
117 plt.xticks([13, 15, 20, 25, 30, 35, 40, 45, 50])
119 ax.set_xlabel('iterations until convergence')
121 plt.hlines(nruns, min(maxiter), max(maxiter), colors='red', linestyle='dashed')
123 # with correction allowed: no logscale
124 plt.yscale('log')
126 plt.savefig(fname)
128 assert os.path.isfile(fname), 'ERROR: plotting did not create PNG file'