|
12 | 12 | import sys
|
13 | 13 | from pathlib import Path
|
14 | 14 |
|
| 15 | +#### |
| 16 | +# TODO: |
| 17 | +# [ ] - If the credential is two, I think we need to set it to zero. We probably want to remove |
| 18 | +# two with saturation (never going negative). |
| 19 | +# |
| 20 | +# [ ] - The port size should also be reduced by 1 with saturation (never going negative). |
| 21 | +# |
| 22 | +#### |
| 23 | + |
15 | 24 |
|
16 | 25 | if(len(sys.argv) == 1):
|
17 | 26 | print("Please provide a filename")
|
@@ -81,6 +90,82 @@ def explain_r2(r2):
|
81 | 90 |
|
82 | 91 |
|
83 | 92 |
|
| 93 | +for predicted_attribute,name in [("best_instr", "instructions"), ("best_cycles", "cycles")]: |
| 94 | + print("=====================================") |
| 95 | + print("predicting ", predicted_attribute, " (", name, ")", " without credentials" ) |
| 96 | + regressor = LinearRegression(fit_intercept=False, positive=True) |
| 97 | + for protocol in range(8): |
| 98 | + print() |
| 99 | + thisframe = data[data["protocol"]==protocol] |
| 100 | + if thisframe.empty: |
| 101 | + continue |
| 102 | + thisframe = thisframe[thisframe["credential"]<=2] |
| 103 | + if thisframe.empty: |
| 104 | + continue |
| 105 | + print("protocol = ", protocol, " ", protocols[protocol]) |
| 106 | + print("number of entries: ", len(thisframe.index)) |
| 107 | + x = thisframe[predictors] |
| 108 | + y = thisframe[[predicted_attribute]] |
| 109 | + regressor.fit(x, y) |
| 110 | + r2 = regressor.score(x,y) |
| 111 | + print("R2 = ", r2, " (", explain_r2(r2), ")") |
| 112 | + print("Coefficients = ", regressor.coef_) |
| 113 | + for i in zip(predictors, regressor.coef_[0]): |
| 114 | + if i[1] > 0.000001: |
| 115 | + print("weight for ", i[0], " = ", i[1]) |
| 116 | + print("Intercept = ", regressor.intercept_) |
| 117 | + |
| 118 | + with PdfPages(dirname+protocols[protocol]+'_predicted_'+name+'_nocredentials.pdf') as pdf: |
| 119 | + fig,ax = plt.subplots() |
| 120 | + ax.plot(thisframe['input_size'], thisframe[predicted_attribute], label="measured", linestyle='none', marker='.', markerfacecolor='blue', markersize=4) |
| 121 | + ax.plot(thisframe['input_size'], regressor.predict(x), label="predicted", linestyle='none', marker='x', markerfacecolor='red', markersize=4) |
| 122 | + |
| 123 | + ax.set_xlabel("URL size (bytes)") |
| 124 | + ax.set_ylabel(name) |
| 125 | + ax.spines[['right', 'top']].set_visible(False) |
| 126 | + ax.legend(loc='best', frameon=False) |
| 127 | + pdf.savefig(fig) |
| 128 | + |
| 129 | + |
| 130 | + |
| 131 | + |
| 132 | +for predicted_attribute,name in [("best_instr", "instructions"), ("best_cycles", "cycles")]: |
| 133 | + print("=====================================") |
| 134 | + print("predicting ", predicted_attribute, " (", name, ")", " with credentials" ) |
| 135 | + regressor = LinearRegression(fit_intercept=False, positive=True) |
| 136 | + for protocol in range(8): |
| 137 | + print() |
| 138 | + thisframe = data[data["protocol"]==protocol] |
| 139 | + if thisframe.empty: |
| 140 | + continue |
| 141 | + thisframe = thisframe[thisframe["credential"]>2] |
| 142 | + if thisframe.empty: |
| 143 | + continue |
| 144 | + print("protocol = ", protocol, " ", protocols[protocol]) |
| 145 | + print("number of entries: ", len(thisframe.index)) |
| 146 | + x = thisframe[predictors] |
| 147 | + y = thisframe[[predicted_attribute]] |
| 148 | + regressor.fit(x, y) |
| 149 | + r2 = regressor.score(x,y) |
| 150 | + print("R2 = ", r2, " (", explain_r2(r2), ")") |
| 151 | + print("Coefficients = ", regressor.coef_) |
| 152 | + for i in zip(predictors, regressor.coef_[0]): |
| 153 | + if i[1] > 0.000001: |
| 154 | + print("weight for ", i[0], " = ", i[1]) |
| 155 | + print("Intercept = ", regressor.intercept_) |
| 156 | + |
| 157 | + with PdfPages(dirname+protocols[protocol]+'_predicted_'+name+'_withcredentials.pdf') as pdf: |
| 158 | + fig,ax = plt.subplots() |
| 159 | + ax.plot(thisframe['input_size'], thisframe[predicted_attribute], label="measured", linestyle='none', marker='.', markerfacecolor='blue', markersize=4) |
| 160 | + ax.plot(thisframe['input_size'], regressor.predict(x), label="predicted", linestyle='none', marker='x', markerfacecolor='red', markersize=4) |
| 161 | + |
| 162 | + ax.set_xlabel("URL size (bytes)") |
| 163 | + ax.set_ylabel(name) |
| 164 | + ax.spines[['right', 'top']].set_visible(False) |
| 165 | + ax.legend(loc='best', frameon=False) |
| 166 | + pdf.savefig(fig) |
| 167 | + |
| 168 | + |
84 | 169 | if plots:
|
85 | 170 | print ("Plotting...")
|
86 | 171 | data["best_instructions_per_cycle"] = data["best_instr"]/data["best_cycles"]
|
|
0 commit comments