Coverage for lib/lib_plot.py: 100%

1 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2026-01-26 23:58 +0100

1 

2 

3 

4import os 

5 

6# [ ] VR TODO 26-5-23 : estimate mean and variance of each data 

7# [ ] VR TODO 26-5-23 : plot with u 1:i for size of data 

8# [ ] VR TODO : consolidate usage dev (commentaire exists !) USage : gère des tableaux de tuple ou des tableaux de float ou des dictionnaire 

9def plot_oned(data, in_file = "temp", 

10 map_illustration_abscisse = {}, 

11map_interval = {"x":None, "y" : None}, 

12 additionnal_command = None, 

13 using_list = [], prod = True, 

14 fact_snd_scale = None): # pragma no cover 

15 dirna = os.path.dirname(in_file) 

16 basen = os.path.basename(in_file) 

17 

18 gnuplot_data = os.path.join(dirna, basen + ".dat") 

19 gnuplot_cmd = os.path.join(dirna, basen + ".cmd") 

20 gnuplot_png = os.path.join(dirna, basen + ".png") 

21 

22 try : 

23 premieres_valeurs = [t[0] for t in data] 

24 dernieres_valeurs = [t[-1] for t in data] 

25 # Trouve le minimum et maximum des dernières valeurs 

26 min_abs = min(premieres_valeurs) 

27 max_abs = max(premieres_valeurs) 

28 

29 valeur_min = min(dernieres_valeurs) 

30 valeur_max = max(dernieres_valeurs) 

31 

32 print(f"Minimum de la dernière colonne: {valeur_min}") 

33 print(f"Maximum de la dernière colonne: {valeur_max}") 

34 valeur_max = 1.1 * (valeur_max - valeur_min) + valeur_min 

35 except Exception as e: 

36 print(str(e) + " : Impossible de trouver le minimum et maximum des dernières valeurs in plot_oned") 

37 # Trouve le minimum et maximum des dernières valeurs 

38 valeur_min = 0 

39 valeur_max = 0 

40 

41 is_dict = type(data) == type({}) 

42 

43 with open(gnuplot_data, "w") as f: 

44 if is_dict : 

45 for d in data : 

46 f.write(str(d) + " ") 

47 if type(data[d]) == type(()): 

48 for dd in data[d]: 

49 f.write(str(dd) + " ") 

50 f.write("\n") 

51 else : 

52 f.write(str(data[d]) + "\n") 

53 

54 f.write("\n") 

55 else : 

56 for d in data : 

57 if type(d) == type(()): 

58 for dd in d: 

59 f.write(str(dd) + " ") 

60 f.write("\n") 

61 else : 

62 f.write(str(d) + "\n") 

63 

64 if len(using_list) > 0 and len(map_illustration_abscisse) > 0: 

65 print("Incompatible options !") 

66 

67 using_args = [] 

68 if len(using_list) > 0: 

69 if type(using_list[0]) == list: 

70 print("Multiple plot") 

71 for one_using in using_list: 

72 title = "Data" 

73 if len(one_using) > 2: 

74 title = one_using[2] 

75 using_args.append("'" + gnuplot_data + "' using " + ":".join(one_using[:2]) + " title '" + title + "' w l lw 5 ") 

76 else: 

77 title = "Data" 

78 if len(using_list) > 2: 

79 title = using_list[2] 

80 # print("Unexpected type using_list") 

81 using_args.append("'" + gnuplot_data + "' using " + ":".join(using_list) + " title '" + title + "' w l lw 5") 

82 else: 

83 title = "Data" 

84 using_args.append("'" + gnuplot_data + "' w l title '" + title + "'") 

85 

86 

87 

88 with open(gnuplot_cmd, "w") as f: 

89 if additionnal_command != None: 

90 f.write(additionnal_command + "\n") 

91 

92 f.write("set grid\n") 

93 f.write("set timefmt \"%Y-%m-%dT%H:%M:%S\"\n") 

94 if "x" in map_interval and map_interval["x"] != None: 

95 f.write("set xrange [" + map_interval["x"][0] + ":" + map_interval["x"][1] + "]\n") 

96 if "y" in map_interval and map_interval["y"] != None: 

97 f.write("set yrange [" + map_interval["y"][0] + ":" + 1.2 * map_interval["y"][1] + "]\n") 

98 if fact_snd_scale != None: # and len(snd_scale) == 2: 

99# f.write("set y2range [" + str(snd_scale[0]) + ":" + str(snd_scale[1]) + "]\n") 

100 f.write("set y2tics\n") 

101 f.write(f"set link y2 via y/{fact_snd_scale} inverse y*{fact_snd_scale}\n") 

102 

103 if prod : 

104 f.write("set term png\n") 

105 f.write("set output '" + gnuplot_png + "'\n") 

106 

107 

108 f.write("plot " + ",".join(using_args)) 

109 

110 if len(map_illustration_abscisse) > 0 and valeur_min > 0 and valeur_max > 0: 

111 level_display = valeur_min - (valeur_max - valeur_min) / 3 

112 denum = (valeur_max - valeur_min) 

113 ratio = (max_abs - min_abs) / denum if denum > 0 else 1 

114 dx = 0.001 

115 dy = dx / ratio 

116 for abs in map_illustration_abscisse: 

117 png_file = map_illustration_abscisse[abs] 

118 f.write(" \\\n") 

119 f.write(", '" + png_file + "' binary filetype = png origin = (" + str(abs) + ", " + str(level_display) + ") dx=" +str(dx)+ " dy=" +str(dy)+ " with rgbimage notitle") 

120 f.write("\n") 

121 else : 

122 f.write("\n") 

123 

124# "android-logo.png" binary filetype = png origin = (1995, 165) dx = 1. / 10 dy = 1. / 220000 with rgbimage notitle,\ 

125 

126 if not prod : 

127 f.write("set term png\n") 

128 f.write("set output '" + gnuplot_png + "'\n") 

129 f.write("replot\n") 

130 else : 

131 f.write("# comment out the set term above and uncomment these !\n") 

132 f.write("# set term png\n") 

133 f.write("# set output '" + gnuplot_png + "'\n") 

134 f.write("# replot\n") 

135 

136 os.system("gnuplot '" + gnuplot_cmd + "' ") 

137 

138 return gnuplot_png 

139