append-experiment-results.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import sys
  2. # Define a function to extract variables from the data file
  3. def extract_variables_from_file(file_path):
  4. variables_list = []
  5. with open(file_path, 'r') as file:
  6. header = file.readline().strip()
  7. for line in file:
  8. columns = line.strip().split()
  9. ds, op, is_optimized, heapsize, y, pm, y_err = columns
  10. variables_dict = {
  11. 'DS': ds,
  12. 'OP': op,
  13. 'is_optimized': is_optimized,
  14. 'heapsize': heapsize,
  15. 'y': float(y), # Convert 'y' to a float to handle averages
  16. 'pm': pm,
  17. 'y_err': float(y_err) # Convert 'y_err' to a float to handle averages
  18. }
  19. variables_list.append(variables_dict)
  20. return variables_list
  21. # Define a function to update the data based on input and add new rows if needed
  22. def update_data(file_path, ds, op, is_optimized, heapsize, y0):
  23. extracted_variables = extract_variables_from_file(file_path)
  24. # Find the row with matching 'DS', 'OP', 'is_optimized', and 'heapsize'
  25. matching_row = None
  26. for variables in extracted_variables:
  27. if (variables['DS'] == ds and
  28. variables['OP'] == op and
  29. variables['is_optimized'] == is_optimized and
  30. variables['heapsize'] == heapsize):
  31. matching_row = variables
  32. break
  33. if matching_row:
  34. # Update 'y' and 'y_err' based on 'y0'
  35. matching_row['y'] = (matching_row['y'] + y0) / 2
  36. matching_row['y_err'] = abs(matching_row['y'] - y0)
  37. else:
  38. # If no matching row found, add a new row
  39. new_row = {
  40. 'DS': ds,
  41. 'OP': op,
  42. 'is_optimized': is_optimized,
  43. 'heapsize': heapsize,
  44. 'y': y0,
  45. 'pm': '±',
  46. 'y_err': 0 # Since it's a new row, we set y_err to 0
  47. }
  48. extracted_variables.append(new_row)
  49. # Write the updated data back to the file
  50. with open(file_path, 'w') as file:
  51. header_line = "DS OP is_optimized heapsize y pm y-err\n"
  52. file.write(header_line)
  53. for variables in extracted_variables:
  54. row = f"{variables['DS']} {variables['OP']} {variables['is_optimized']} {variables['heapsize']} {variables['y']} {variables['pm']} {variables['y_err']}\n"
  55. file.write(row)
  56. # Check if the user provided the required input
  57. #if len(sys.argv) != 6:
  58. # print("Usage: python3 script.py <file_path> <DS> <OP> <is_optimized> <heapsize> <y0>")
  59. # sys.exit(1)
  60. # Get the input values from the command-line arguments
  61. file_path = sys.argv[1]
  62. ds = sys.argv[2]
  63. op = sys.argv[3]
  64. is_optimized = sys.argv[4]
  65. heapsize = sys.argv[5]
  66. y0 = float(sys.argv[6]) # Convert y0 to a float
  67. # Call the function to update the data based on input
  68. update_data(file_path, ds, op, is_optimized, heapsize, y0)