A Discrete-Event Network Simulator
API
gnuplot-aggregator.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2013 University of Washington
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License version 2 as
6  * published by the Free Software Foundation;
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  * GNU General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
16  *
17  * Author: Mitch Watrous (watrous@u.washington.edu)
18  */
19 
20 #include "gnuplot-aggregator.h"
21 
22 #include "ns3/abort.h"
23 #include "ns3/log.h"
24 
25 #include <fstream>
26 #include <iostream>
27 #include <string>
28 
29 namespace ns3
30 {
31 
32 NS_LOG_COMPONENT_DEFINE("GnuplotAggregator");
33 
34 NS_OBJECT_ENSURE_REGISTERED(GnuplotAggregator);
35 
36 TypeId
38 {
39  static TypeId tid =
40  TypeId("ns3::GnuplotAggregator").SetParent<DataCollectionObject>().SetGroupName("Stats");
41 
42  return tid;
43 }
44 
45 GnuplotAggregator::GnuplotAggregator(const std::string& outputFileNameWithoutExtension)
46  : m_outputFileNameWithoutExtension(outputFileNameWithoutExtension),
47  m_graphicsFileName(m_outputFileNameWithoutExtension + ".png"),
48  m_title("Data Values"),
49  m_xLegend("X Values"),
50  m_yLegend("Y Values"),
51  m_titleSet(false),
52  m_xAndYLegendsSet(false),
53  m_gnuplot(m_graphicsFileName)
54 {
55  NS_LOG_FUNCTION(this);
56 }
57 
59 {
60  NS_LOG_FUNCTION(this);
61  if (!m_titleSet)
62  {
63  NS_LOG_WARN("Warning: The plot title was not set for the gnuplot aggregator");
64  }
65  if (!m_xAndYLegendsSet)
66  {
67  NS_LOG_WARN("Warning: The axis legends were not set for the gnuplot aggregator");
68  }
69 
70  std::string dataFileName = m_outputFileNameWithoutExtension + ".dat";
71  std::string plotFileName = m_outputFileNameWithoutExtension + ".plt";
72  std::string scriptFileName = m_outputFileNameWithoutExtension + ".sh";
73 
74  // Open the gnuplot plot and data files.
75  std::ofstream plotFile;
76  plotFile.open(plotFileName);
77  std::ofstream dataFile;
78  dataFile.open(dataFileName);
79 
80  // Skip any NaN's that appear in data.
81  m_gnuplot.AppendExtra("set datafile missing \"-nan\"");
82 
83  // Write the gnuplot plot and data files.
84  m_gnuplot.GenerateOutput(plotFile, dataFile, dataFileName);
85 
86  // Close the gnuplot plot and data files.
87  plotFile.close();
88  dataFile.close();
89 
90  // Open the shell script file.
91  std::ofstream scriptFile;
92  scriptFile.open(scriptFileName);
93 
94  // Write the shell script file.
95  scriptFile << "#!/bin/sh" << std::endl;
96  scriptFile << std::endl;
97  scriptFile << "gnuplot " << plotFileName << std::endl;
98 
99  // Close the shell script file.
100  scriptFile.close();
101 }
102 
103 void
104 GnuplotAggregator::Write2d(std::string context, double x, double y)
105 {
106  NS_LOG_FUNCTION(this << context << x << y);
107 
108  if (m_2dDatasetMap.count(context) == 0)
109  {
110  NS_ABORT_MSG("Dataset " << context << " has not been added");
111  }
112 
113  if (m_enabled)
114  {
115  // Add this 2D data point to its dataset.
116  m_2dDatasetMap[context].Add(x, y);
117  }
118 }
119 
120 void
122  double x,
123  double y,
124  double errorDelta)
125 {
126  NS_LOG_FUNCTION(this << context << x << y << errorDelta);
127 
128  if (m_2dDatasetMap.count(context) == 0)
129  {
130  NS_ABORT_MSG("Dataset " << context << " has not been added");
131  }
132 
133  if (m_enabled)
134  {
135  // Add this 2D data point with its error bar to its dataset.
136  m_2dDatasetMap[context].Add(x, y, errorDelta);
137  }
138 }
139 
140 void
142  double x,
143  double y,
144  double errorDelta)
145 {
146  NS_LOG_FUNCTION(this << context << x << y << errorDelta);
147 
148  if (m_2dDatasetMap.count(context) == 0)
149  {
150  NS_ABORT_MSG("Dataset " << context << " has not been added");
151  }
152 
153  if (m_enabled)
154  {
155  // Add this 2D data point with its error bar to its dataset.
156  m_2dDatasetMap[context].Add(x, y, errorDelta);
157  }
158 }
159 
160 void
162  double x,
163  double y,
164  double xErrorDelta,
165  double yErrorDelta)
166 {
167  NS_LOG_FUNCTION(this << context << x << y << xErrorDelta << yErrorDelta);
168 
169  if (m_2dDatasetMap.count(context) == 0)
170  {
171  NS_ABORT_MSG("Dataset " << context << " has not been added");
172  }
173 
174  if (m_enabled)
175  {
176  // Add this 2D data point with its error bar to its dataset.
177  m_2dDatasetMap[context].Add(x, y, xErrorDelta, yErrorDelta);
178  }
179 }
180 
181 void
182 GnuplotAggregator::SetTerminal(const std::string& terminal)
183 {
184  // Change the extension for the graphics file.
186 
187  // Update the gnuplot, too.
188  m_gnuplot.SetTerminal(terminal);
190 }
191 
192 void
193 GnuplotAggregator::SetTitle(const std::string& title)
194 {
195  NS_LOG_FUNCTION(this << title);
196  m_gnuplot.SetTitle(title);
197  m_titleSet = true;
198 }
199 
200 void
201 GnuplotAggregator::SetLegend(const std::string& xLegend, const std::string& yLegend)
202 {
203  NS_LOG_FUNCTION(this << xLegend << yLegend);
204  m_gnuplot.SetLegend(xLegend, yLegend);
205  m_xAndYLegendsSet = true;
206 }
207 
208 void
209 GnuplotAggregator::SetExtra(const std::string& extra)
210 {
211  NS_LOG_FUNCTION(this << extra);
212  m_gnuplot.SetExtra(extra);
213 }
214 
215 void
216 GnuplotAggregator::AppendExtra(const std::string& extra)
217 {
218  NS_LOG_FUNCTION(this << extra);
219  m_gnuplot.AppendExtra(extra);
220 }
221 
222 void
223 GnuplotAggregator::Add2dDataset(const std::string& dataset, const std::string& title)
224 {
225  NS_LOG_FUNCTION(this << dataset << title);
226 
227  if (m_2dDatasetMap.count(dataset) > 0)
228  {
229  NS_ABORT_MSG("Dataset " << dataset << " has already been added");
230  }
231 
232  // Add this dataset to the map so that its values can be saved.
233  Gnuplot2dDataset gnuplot2dDataset(title);
234  m_2dDatasetMap[dataset] = gnuplot2dDataset;
235 
236  // Add this dataset to the plot so that its values can be plotted.
238 }
239 
240 void
242 {
243  NS_LOG_FUNCTION(extra);
245 }
246 
247 void
248 GnuplotAggregator::Set2dDatasetExtra(const std::string& dataset, const std::string& extra)
249 {
250  NS_LOG_FUNCTION(this << dataset << extra);
251  if (m_2dDatasetMap.count(dataset) == 0)
252  {
253  NS_ABORT_MSG("Dataset " << dataset << " has not been added");
254  }
255 
256  // Set the extra parameters for the dataset.
257  m_2dDatasetMap[dataset].SetExtra(extra);
258 }
259 
260 void
262 {
263  NS_LOG_FUNCTION(this << dataset);
264  if (m_2dDatasetMap.count(dataset) == 0)
265  {
266  NS_ABORT_MSG("Dataset " << dataset << " has not been added");
267  }
268 
269  if (m_enabled)
270  {
271  // Add an empty line to the dataset.
272  m_2dDatasetMap[dataset].AddEmptyLine();
273  }
274 }
275 
276 void
278 {
279  NS_LOG_FUNCTION(style);
281 }
282 
283 void
285 {
286  NS_LOG_FUNCTION(this << dataset << style);
287  if (m_2dDatasetMap.count(dataset) == 0)
288  {
289  NS_ABORT_MSG("Dataset " << dataset << " has not been added");
290  }
291 
292  // Set the style for the dataset.
293  m_2dDatasetMap[dataset].SetStyle(style);
294 }
295 
296 void
298 {
299  NS_LOG_FUNCTION(errorBars);
301 }
302 
303 void
304 GnuplotAggregator::Set2dDatasetErrorBars(const std::string& dataset,
305  Gnuplot2dDataset::ErrorBars errorBars)
306 {
307  NS_LOG_FUNCTION(this << dataset << errorBars);
308  if (m_2dDatasetMap.count(dataset) == 0)
309  {
310  NS_ABORT_MSG("Dataset " << dataset << " has not been added");
311  }
312 
313  // Set the error bars for the dataset.
314  m_2dDatasetMap[dataset].SetErrorBars(errorBars);
315 }
316 
317 void
319 {
320  NS_LOG_FUNCTION(this << keyLocation);
321  // Set the specified key location.
322  switch (keyLocation)
323  {
324  case NO_KEY:
325  m_gnuplot.AppendExtra("set key off");
326  break;
327  case KEY_ABOVE:
328  m_gnuplot.AppendExtra("set key outside center above");
329  break;
330  case KEY_BELOW:
331  m_gnuplot.AppendExtra("set key outside center below");
332  break;
333  default:
334  m_gnuplot.AppendExtra("set key inside");
335  break;
336  }
337 }
338 
339 } // namespace ns3
Base class for data collection framework objects.
bool m_enabled
Object's activation state.
Class to represent a 2D points plot.
Definition: gnuplot.h:118
ErrorBars
Whether errorbars should be used for this dataset.
Definition: gnuplot.h:138
static void SetDefaultStyle(enum Style style)
Change default style for all newly created objects.
Definition: gnuplot.cc:340
Style
The plotting style to use for this dataset.
Definition: gnuplot.h:123
static void SetDefaultErrorBars(enum ErrorBars errorBars)
Change default errorbars style for all newly created objects.
Definition: gnuplot.cc:351
void Write2dDatasetEmptyLine(const std::string &dataset)
Add an empty line in the data output sequence.
GnuplotAggregator(const std::string &outputFileNameWithoutExtension)
void Write2dWithXErrorDelta(std::string context, double x, double y, double errorDelta)
Writes a 2D value to a 2D gnuplot dataset with error bars in the x direction.
static void Set2dDatasetDefaultExtra(const std::string &extra)
Change extra formatting style parameters for newly created objects.
Gnuplot m_gnuplot
Used to create gnuplot files.
void Write2d(std::string context, double x, double y)
Writes a 2D value to a 2D gnuplot dataset.
std::string m_graphicsFileName
The graphics file name with its extension.
bool m_xAndYLegendsSet
Set equal to true after setting the x and y legends.
void Set2dDatasetExtra(const std::string &dataset, const std::string &extra)
Add extra formatting parameters to this dataset.
void AppendExtra(const std::string &extra)
void Write2dWithYErrorDelta(std::string context, double x, double y, double errorDelta)
Writes a 2D value to a 2D gnuplot dataset with error bars in the y direction.
std::string m_outputFileNameWithoutExtension
The output file name without any extension.
static TypeId GetTypeId()
Get the type ID.
bool m_titleSet
Set equal to true after setting the title.
static void Set2dDatasetDefaultStyle(Gnuplot2dDataset::Style style)
Change default style for all newly created objects.
void Write2dWithXYErrorDelta(std::string context, double x, double y, double xErrorDelta, double yErrorDelta)
Writes a 2D value to a 2D gnuplot dataset with error bars in the x and y directions.
KeyLocation
The location of the key in the plot.
void SetTerminal(const std::string &terminal)
void Add2dDataset(const std::string &dataset, const std::string &title)
Adds a 2D dataset to the plot.
std::map< std::string, Gnuplot2dDataset > m_2dDatasetMap
Maps context strings to 2D datasets.
static void Set2dDatasetDefaultErrorBars(Gnuplot2dDataset::ErrorBars errorBars)
Change default errorbars style for all newly created objects.
void SetLegend(const std::string &xLegend, const std::string &yLegend)
void Set2dDatasetErrorBars(const std::string &dataset, Gnuplot2dDataset::ErrorBars errorBars)
Set the error bars to use for this dataset.
void Set2dDatasetStyle(const std::string &dataset, Gnuplot2dDataset::Style style)
Set the style of plotting to use for this dataset.
void SetKeyLocation(KeyLocation keyLocation)
Set the location of the key in the plot.
void SetTitle(const std::string &title)
void SetExtra(const std::string &extra)
static void SetDefaultExtra(const std::string &extra)
Change extra formatting style parameters for newly created objects.
Definition: gnuplot.cc:147
void AddDataset(const GnuplotDataset &dataset)
Definition: gnuplot.cc:759
void SetLegend(const std::string &xLegend, const std::string &yLegend)
Definition: gnuplot.cc:739
void SetTerminal(const std::string &terminal)
Definition: gnuplot.cc:727
void AppendExtra(const std::string &extra)
Definition: gnuplot.cc:752
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition: gnuplot.cc:765
void SetExtra(const std::string &extra)
Definition: gnuplot.cc:746
void SetTitle(const std::string &title)
Definition: gnuplot.cc:733
void SetOutputFilename(const std::string &outputFilename)
Definition: gnuplot.cc:706
a unique identifier for an interface.
Definition: type-id.h:59
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition: type-id.cc:931
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition: abort.h:49
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition: log.h:202
#define NS_LOG_FUNCTION(parameters)
If log level LOG_FUNCTION is enabled, this macro will output all input parameters separated by ",...
#define NS_LOG_WARN(msg)
Use NS_LOG to output a message of level LOG_WARN.
Definition: log.h:261
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition: object-base.h:46
Every class exported by the ns3 library is enclosed in the ns3 namespace.