Coverage for pySDC/projects/soft_failure/visualization_helper.py: 100%

54 statements  

« prev     ^ index     » next       coverage.py v7.5.0, created at 2024-04-29 09:02 +0000

1import os 

2 

3import matplotlib 

4import numpy as np 

5from matplotlib import rc 

6 

7from pySDC.helpers.stats_helper import filter_stats 

8 

9matplotlib.use('Agg') 

10import matplotlib.pyplot as plt 

11 

12 

13def show_residual_across_simulation(stats, fname): 

14 """ 

15 Helper routine to visualize the residuals dependent on the number of iterations across the simulation 

16 

17 Args: 

18 stats (dict): statistics object 

19 fname (str): filename 

20 """ 

21 

22 # get residuals of the run 

23 extract_stats = filter_stats(stats, type='residual_post_iteration') 

24 

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) 

29 

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) 

36 

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') 

42 

43 # create plot and save 

44 fig, ax = plt.subplots(figsize=(15, 10)) 

45 

46 ax.set_xlabel('iteration') 

47 ax.set_ylabel('log10(residual)') 

48 

49 plt.axis([0, 14, -12, 3]) 

50 plt.plot(np.linspace(1, maxiter, num=maxiter), residual) 

51 

52 plt.savefig(fname) 

53 

54 assert os.path.isfile(fname), 'ERROR: plotting did not create PNG file' 

55 

56 

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 

61 

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 """ 

71 

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') 

77 

78 # create plot and save 

79 fig, ax = plt.subplots(figsize=(15, 10)) 

80 

81 ax.set_xlabel('iteration') 

82 ax.set_ylabel('log10(residual)') 

83 

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() 

93 

94 plt.savefig(fname) 

95 

96 assert os.path.isfile(fname), 'ERROR: plotting did not create PNG file' 

97 

98 

99def show_iter_hist(fname, maxiter, nruns): 

100 """ 

101 Helper routine to visualize the maximal iteration number across the simulation in a histogram 

102 

103 Args: 

104 stats (dict): statistics object 

105 fname (str): filename 

106 maxiter: maximal iterations per run 

107 nruns: number of runs 

108 """ 

109 

110 # create plot and save 

111 fig, ax = plt.subplots(figsize=(15, 10)) 

112 

113 plt.hist(maxiter, bins=np.arange(min(maxiter), max(maxiter) + 2, 1), align='left', rwidth=0.9) 

114 

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]) 

118 

119 ax.set_xlabel('iterations until convergence') 

120 

121 plt.hlines(nruns, min(maxiter), max(maxiter), colors='red', linestyle='dashed') 

122 

123 # with correction allowed: no logscale 

124 plt.yscale('log') 

125 

126 plt.savefig(fname) 

127 

128 assert os.path.isfile(fname), 'ERROR: plotting did not create PNG file'