I'm working on a project where I have to keep track of model performance across different version releases. I'm trying to create and write a text file that contains some lines of text that signal turbulences in the model performance. This is the function I wrote (it contains some auxiliary functions but these are not relevant for my question):
def alarm(cell_line, conc, full_model_id, metric, treshold):
v_model_results = get_model_across_versions(cell_line, conc, full_model_id)
v_model_results2 = v_model_results.reset_index()[metric]
percentages_list = calculate_percentages(v_model_results2)
length = len(percentages_list)
res = []
with open('results.txt', 'w') as f:
for i in range(length): if (percentages_list[i] < -treshold):
f.write("The percentage treshold between the versions v" + str(i+13) + " and v" + str(i+14)+ " is exceeded for the metric " + metric +". There is a drop of " + str(-percentages_list[i]) + " percent." + "\n")
print("The percentage treshold between the versions v" + str(i+13) + " and v" + str(i+14)+ " is exceeded for the metric " + metric +". There is a drop of " + str(-percentages_list[i]) + " percent.")
else:
print("No significant turbulence in performance between the versions v" + str(i+13) + " and v" + str(i+14)+ " for the metric " + metric + ".")
f.close()
When I apply the above function to an example all the messages are printed correctly and it indeed generates a text file results.txt
, but this file is empty. What am I doing wrong here?
open('results.txt', 'w')
will open the file and overwrite any data in it. This could be causing your problem.
Try open('results.txt', 'a')
. To append new text instead of overwritting.
Also, you don't need to include f.close()
. The with statement handles that all for you.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.