Guest User

Untitled

a guest
Oct 20th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. """
  2. Generic example of experiment that compares two methods
  3. COS, SIN in two different experiments EXPERIMENT1, EXPERIMENT2
  4.  
  5. Experiments can be in general long so one can choose to SAVE the
  6. files so that the plots can be generated again when the
  7. appropriate RUNTAG is set and the PLOT program option is chosen
  8. """
  9. import os
  10.  
  11. import matplotlib.pyplot as plt
  12. import numpy as np
  13.  
  14.  
  15. class Method:
  16. COS = "cosine"
  17. SIN = "sine"
  18.  
  19.  
  20. class Program:
  21. EXPERIMENT1 = 1
  22. EXPERIMENT2 = 2
  23. PLOT = 3
  24.  
  25. RUNTAG = 'run1'
  26. FILETAG = 'experiment'
  27. OUTPUTFOLDER = 'outputs'
  28. STEPS = 5000
  29. STEP_SIZE = 1e-3
  30. SAVE = True
  31.  
  32. PROGRAM = Program.PLOT
  33.  
  34.  
  35. class NameGen(object):
  36. def __init__(self, filetag, output_folder):
  37. self.output_folder = output_folder
  38. self.filetag = filetag
  39.  
  40. def get_output_prefix(self, exptag, label):
  41. return os.path.join(self.output_folder,
  42. '{}_{}{}'.format(self.filetag, label, exptag))
  43.  
  44. def get_output_filename(self, exptag, label, ext):
  45. return '{}.{}'.format(self.get_output_prefix(exptag, label), ext)
  46.  
  47. def save_plot(self, tag, label='', dpi=300, ext='png'):
  48. plt.draw()
  49. plt.savefig(self.get_output_filename(tag, label, ext), dpi=dpi)
  50.  
  51.  
  52. def factory(method_name, **kwargs):
  53. if method_name == Method.COS:
  54. t = kwargs.pop('term', 0)
  55. return lambda x: np.cos(x + t)
  56. elif method_name == Method.SIN:
  57. f = kwargs.pop('factor', 1)
  58. return lambda x: np.sin(f * x)
  59. else:
  60. raise ValueError("Method: '{}' is not valid.".format(method_name))
  61.  
  62.  
  63. def plot_result(y, label):
  64. plt.plot(y, label=label)
  65.  
  66.  
  67. def run(func, label):
  68. y = func(np.arange(STEPS) * STEP_SIZE)
  69. plot_result(y, label)
  70.  
  71. if SAVE:
  72. namegen = NameGen(FILETAG, OUTPUTFOLDER)
  73. npz_filename = namegen.get_output_filename(RUNTAG, label, 'npz')
  74. np.savez(npz_filename, y=y)
  75.  
  76.  
  77. def finalize_plot():
  78. plt.legend(loc='best', prop={'size': 12})
  79.  
  80. plt.xlabel('Simulation steps')
  81. plt.ylabel('Output')
  82.  
  83. namegen = NameGen(FILETAG, OUTPUTFOLDER)
  84. namegen.save_plot(RUNTAG, "")
  85. plt.clf()
  86.  
  87.  
  88. def main_example1():
  89. for method_name in [Method.COS, Method.SIN]:
  90. run(factory(method_name), label=method_name)
  91.  
  92.  
  93. def main_example2():
  94. for method_name in [Method.COS, Method.SIN]:
  95. run(factory(method_name, factor=2, term=1), label=method_name)
  96.  
  97.  
  98. def main_plot():
  99. namegen = NameGen(FILETAG, OUTPUTFOLDER)
  100.  
  101. for label in [Method.COS, Method.SIN]:
  102. npz_filename = namegen.get_output_filename(RUNTAG, label, 'npz')
  103. y = np.load(npz_filename)['y']
  104. plot_result(y, label)
  105.  
  106. if __name__ == '__main__':
  107. if PROGRAM == Program.EXPERIMENT1:
  108. main_example1()
  109. elif PROGRAM == Program.EXPERIMENT2:
  110. main_example2()
  111. elif PROGRAM == Program.PLOT:
  112. main_plot()
  113.  
  114. finalize_plot()
Add Comment
Please, Sign In to add comment