You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.py 2.4KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import os
  2. import numpy as np
  3. # implemented with list because of memory issues
  4. def read_cascades(name: str, with_cascade_id=False):
  5. num_of_nodes = -1
  6. cascades = []
  7. finish_time = 0
  8. if os.path.isfile('./{}'.format(name)):
  9. with open(name, 'r') as outfile:
  10. for line in outfile:
  11. if line.strip() == '':
  12. pass
  13. elif ';' in line:
  14. cascade = []
  15. if with_cascade_id:
  16. stripped = line.strip().split(';')[1].split(',')
  17. for i in range(len(stripped)):
  18. if i % 2 == 0:
  19. cascade += [(int(stripped[i]), float(stripped[i + 1]))]
  20. if cascade[-1][1] > finish_time:
  21. finish_time = cascade[-1][1]
  22. else:
  23. # cascades += [[(int(event.split(',')[0]), float(event.split(',')[1])) for event in
  24. # line.strip().split(';')]]
  25. for event in line.strip().split(';'):
  26. cascade += [(int(event.split(',')[0]), float(event.split(',')[1]))]
  27. if cascade[-1][1] > finish_time:
  28. finish_time = cascade[-1][1]
  29. cascades += [cascade]
  30. else:
  31. num_of_nodes += 1
  32. print(cascades)
  33. print(num_of_nodes)
  34. print(finish_time)
  35. return num_of_nodes, finish_time, cascades
  36. def read_result(name: str, num_of_nodes: int = 32):
  37. mat = np.zeros([num_of_nodes, num_of_nodes])
  38. if os.path.isfile('./{}'.format(name)):
  39. with open(name, 'r') as outfile:
  40. for line in outfile:
  41. if '.' in line:
  42. stripped = line.split(',')
  43. i = int(stripped[0])
  44. j = int(stripped[1])
  45. mat[i][j] = float(stripped[-1])
  46. return mat
  47. def print_mat(mat, num_of_nodes=32):
  48. for i in range(num_of_nodes):
  49. for j in range(num_of_nodes):
  50. if mat[i][j] != 0:
  51. print((i, j, mat[i][j]), end=' ')
  52. print()
  53. def new_print(mat):
  54. for x in mat:
  55. print(x)
  56. def normalize(mat):
  57. mean = np.mean(mat)
  58. return np.vectorize(lambda val: 1. if val >= mean else 0.)(np.repeat(mat, 1, axis=1))