activityVisualisation.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # Copyright 2014 Modelling, Simulation and Design Lab (MSDL) at
  2. # McGill University and the University of Antwerp (http://msdl.cs.mcgill.ca/)
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. """
  16. Utility functions to visualize various kinds of data in a Cell DEVS way, that is: by creating a matrix containing single values. This matrix can then be processed by e.g. gnuplot to create a heatmap. Note that it is limited to 2D maps, as these are the most frequent and simplest to conceptually grasp.
  17. These functions are supposed to be used later on in development for the Activity-Aware part.
  18. """
  19. def visualizeLocations(kernel):
  20. """
  21. Visualize the locations in a Cell DEVS way
  22. :param kernel: a basesimulator object, to fetch the location of every model
  23. """
  24. location_map = [[0] * kernel.y_size for _ in range(kernel.x_size)]
  25. for i, loc in enumerate(kernel.destinations):
  26. try:
  27. model = kernel.model_ids[i]
  28. if isinstance(loc, int):
  29. locationMap[model.x][model.y] = loc
  30. else:
  31. locationMap[model.x][model.y] = kernel.name
  32. except AttributeError:
  33. pass
  34. visualizeMatrix(location_map, "%i", "locations-%f" % max(0, kernel.gvt))
  35. def visualizeActivity(sim):
  36. """
  37. Visualize the activity in a Cell DEVS way
  38. :param sim: the simulator object, to access the model and their activity
  39. """
  40. activities = []
  41. cached = {}
  42. import pypdevs.middleware as middleware
  43. for i in range(len(sim.server.proxies)):
  44. proxy = sim.controller.getProxy(i)
  45. cached.update(proxy.getTotalActivity((float('inf'), float('inf'))))
  46. for aDEVS in sim.model.component_set:
  47. model_id = aDEVS.model_id
  48. activities.append([cached[model_id], aDEVS])
  49. if sim.x_size > 0 and sim.y_size > 0:
  50. activity_map = [[0.0] * sim.y_size for i in range(sim.x_size)]
  51. for entry in activities:
  52. try:
  53. activity_map[entry[1].x][entry[1].y] = entry[0]
  54. except AttributeError:
  55. pass
  56. visualizeMatrix(activity_map, "%.6f", "activity")
  57. else:
  58. activities.sort(key=lambda i: i[1].getModelFullName())
  59. for entry in activities:
  60. print("%30s -- %.6f" % (entry[1].getModelFullName(), entry[0]))
  61. def visualizeMatrix(matrix, formatstring, filename):
  62. """
  63. Perform the actual visualisation in a matrix style
  64. :param matrix: the 2D matrix to visualize, should be a list of lists
  65. :param formatstring: the string to use to format the values, most likely something like "%f"
  66. :param filename: file to write the matrix to. Can be both a string to create a new file with that name, or an opened file handle.
  67. """
  68. if isinstance(filename, str):
  69. outfile = open(filename, 'w')
  70. openfile = False
  71. else:
  72. outfile = filename
  73. openfile = True
  74. formatstring = formatstring + " "
  75. for x in matrix:
  76. for y in x:
  77. outfile.write(formatstring % y)
  78. outfile.write("\n")
  79. if not openfile:
  80. outfile.close()