A Discrete-Event Network Simulator
API
Loading...
Searching...
No Matches
gnuplot-aggregator.cc
Go to the documentation of this file.
1/*
2 * Copyright (c) 2013 University of Washington
3 *
4 * SPDX-License-Identifier: GPL-2.0-only
5 *
6 * Author: Mitch Watrous (watrous@u.washington.edu)
7 */
8
10
11#include "ns3/abort.h"
12#include "ns3/log.h"
13
14#include <fstream>
15#include <iostream>
16#include <string>
17
18namespace ns3
19{
20
21NS_LOG_COMPONENT_DEFINE("GnuplotAggregator");
22
23NS_OBJECT_ENSURE_REGISTERED(GnuplotAggregator);
24
25TypeId
27{
28 static TypeId tid =
29 TypeId("ns3::GnuplotAggregator").SetParent<DataCollectionObject>().SetGroupName("Stats");
30
31 return tid;
32}
33
34GnuplotAggregator::GnuplotAggregator(const std::string& outputFileNameWithoutExtension)
35 : m_outputFileNameWithoutExtension(outputFileNameWithoutExtension),
36 m_graphicsFileName(m_outputFileNameWithoutExtension + ".png"),
37 m_title("Data Values"),
38 m_xLegend("X Values"),
39 m_yLegend("Y Values"),
40 m_titleSet(false),
41 m_xAndYLegendsSet(false),
42 m_gnuplot(m_graphicsFileName)
43{
44 NS_LOG_FUNCTION(this);
45}
46
48{
49 NS_LOG_FUNCTION(this);
50 if (!m_titleSet)
51 {
52 NS_LOG_WARN("Warning: The plot title was not set for the gnuplot aggregator");
53 }
55 {
56 NS_LOG_WARN("Warning: The axis legends were not set for the gnuplot aggregator");
57 }
58
59 std::string dataFileName = m_outputFileNameWithoutExtension + ".dat";
60 std::string plotFileName = m_outputFileNameWithoutExtension + ".plt";
61 std::string scriptFileName = m_outputFileNameWithoutExtension + ".sh";
62
63 // Open the gnuplot plot and data files.
64 std::ofstream plotFile;
65 plotFile.open(plotFileName);
66 std::ofstream dataFile;
67 dataFile.open(dataFileName);
68
69 // Skip any NaN's that appear in data.
70 m_gnuplot.AppendExtra("set datafile missing \"-nan\"");
71
72 // Write the gnuplot plot and data files.
73 m_gnuplot.GenerateOutput(plotFile, dataFile, dataFileName);
74
75 // Close the gnuplot plot and data files.
76 plotFile.close();
77 dataFile.close();
78
79 // Open the shell script file.
80 std::ofstream scriptFile;
81 scriptFile.open(scriptFileName);
82
83 // Write the shell script file.
84 scriptFile << "#!/bin/sh" << std::endl;
85 scriptFile << std::endl;
86 scriptFile << "gnuplot " << plotFileName << std::endl;
87
88 // Close the shell script file.
89 scriptFile.close();
90}
91
92void
93GnuplotAggregator::Write2d(std::string context, double x, double y)
94{
95 NS_LOG_FUNCTION(this << context << x << y);
96
97 if (m_2dDatasetMap.count(context) == 0)
98 {
99 NS_ABORT_MSG("Dataset " << context << " has not been added");
100 }
101
102 if (m_enabled)
103 {
104 // Add this 2D data point to its dataset.
105 m_2dDatasetMap[context].Add(x, y);
106 }
107}
108
109void
111 double x,
112 double y,
113 double errorDelta)
114{
115 NS_LOG_FUNCTION(this << context << x << y << errorDelta);
116
117 if (m_2dDatasetMap.count(context) == 0)
118 {
119 NS_ABORT_MSG("Dataset " << context << " has not been added");
120 }
121
122 if (m_enabled)
123 {
124 // Add this 2D data point with its error bar to its dataset.
125 m_2dDatasetMap[context].Add(x, y, errorDelta);
126 }
127}
128
129void
131 double x,
132 double y,
133 double errorDelta)
134{
135 NS_LOG_FUNCTION(this << context << x << y << errorDelta);
136
137 if (m_2dDatasetMap.count(context) == 0)
138 {
139 NS_ABORT_MSG("Dataset " << context << " has not been added");
140 }
141
142 if (m_enabled)
143 {
144 // Add this 2D data point with its error bar to its dataset.
145 m_2dDatasetMap[context].Add(x, y, errorDelta);
146 }
147}
148
149void
151 double x,
152 double y,
153 double xErrorDelta,
154 double yErrorDelta)
155{
156 NS_LOG_FUNCTION(this << context << x << y << xErrorDelta << yErrorDelta);
157
158 if (m_2dDatasetMap.count(context) == 0)
159 {
160 NS_ABORT_MSG("Dataset " << context << " has not been added");
161 }
162
163 if (m_enabled)
164 {
165 // Add this 2D data point with its error bar to its dataset.
166 m_2dDatasetMap[context].Add(x, y, xErrorDelta, yErrorDelta);
167 }
168}
169
170void
171GnuplotAggregator::SetTerminal(const std::string& terminal)
172{
173 // Change the extension for the graphics file.
175
176 // Update the gnuplot, too.
177 m_gnuplot.SetTerminal(terminal);
179}
180
181void
182GnuplotAggregator::SetTitle(const std::string& title)
183{
184 NS_LOG_FUNCTION(this << title);
185 m_gnuplot.SetTitle(title);
186 m_titleSet = true;
187}
188
189void
190GnuplotAggregator::SetLegend(const std::string& xLegend, const std::string& yLegend)
191{
192 NS_LOG_FUNCTION(this << xLegend << yLegend);
193 m_gnuplot.SetLegend(xLegend, yLegend);
194 m_xAndYLegendsSet = true;
195}
196
197void
198GnuplotAggregator::SetExtra(const std::string& extra)
199{
200 NS_LOG_FUNCTION(this << extra);
201 m_gnuplot.SetExtra(extra);
202}
203
204void
205GnuplotAggregator::AppendExtra(const std::string& extra)
206{
207 NS_LOG_FUNCTION(this << extra);
208 m_gnuplot.AppendExtra(extra);
209}
210
211void
212GnuplotAggregator::Add2dDataset(const std::string& dataset, const std::string& title)
213{
214 NS_LOG_FUNCTION(this << dataset << title);
215
216 if (m_2dDatasetMap.count(dataset) > 0)
217 {
218 NS_ABORT_MSG("Dataset " << dataset << " has already been added");
219 }
220
221 // Add this dataset to the map so that its values can be saved.
222 Gnuplot2dDataset gnuplot2dDataset(title);
223 m_2dDatasetMap[dataset] = gnuplot2dDataset;
224
225 // Add this dataset to the plot so that its values can be plotted.
227}
228
229void
235
236void
237GnuplotAggregator::Set2dDatasetExtra(const std::string& dataset, const std::string& extra)
238{
239 NS_LOG_FUNCTION(this << dataset << extra);
240 if (m_2dDatasetMap.count(dataset) == 0)
241 {
242 NS_ABORT_MSG("Dataset " << dataset << " has not been added");
243 }
244
245 // Set the extra parameters for the dataset.
246 m_2dDatasetMap[dataset].SetExtra(extra);
247}
248
249void
251{
252 NS_LOG_FUNCTION(this << dataset);
253 if (m_2dDatasetMap.count(dataset) == 0)
254 {
255 NS_ABORT_MSG("Dataset " << dataset << " has not been added");
256 }
257
258 if (m_enabled)
259 {
260 // Add an empty line to the dataset.
261 m_2dDatasetMap[dataset].AddEmptyLine();
262 }
263}
264
265void
271
272void
274{
275 NS_LOG_FUNCTION(this << dataset << style);
276 if (m_2dDatasetMap.count(dataset) == 0)
277 {
278 NS_ABORT_MSG("Dataset " << dataset << " has not been added");
279 }
280
281 // Set the style for the dataset.
282 m_2dDatasetMap[dataset].SetStyle(style);
283}
284
285void
291
292void
295{
296 NS_LOG_FUNCTION(this << dataset << errorBars);
297 if (m_2dDatasetMap.count(dataset) == 0)
298 {
299 NS_ABORT_MSG("Dataset " << dataset << " has not been added");
300 }
301
302 // Set the error bars for the dataset.
303 m_2dDatasetMap[dataset].SetErrorBars(errorBars);
304}
305
306void
308{
309 NS_LOG_FUNCTION(this << keyLocation);
310 // Set the specified key location.
311 switch (keyLocation)
312 {
313 case NO_KEY:
314 m_gnuplot.AppendExtra("set key off");
315 break;
316 case KEY_ABOVE:
317 m_gnuplot.AppendExtra("set key outside center above");
318 break;
319 case KEY_BELOW:
320 m_gnuplot.AppendExtra("set key outside center below");
321 break;
322 default:
323 m_gnuplot.AppendExtra("set key inside");
324 break;
325 }
326}
327
328} // 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:105
static void SetDefaultStyle(Style style)
Change default style for all newly created objects.
Definition gnuplot.cc:342
ErrorBars
Whether errorbars should be used for this dataset.
Definition gnuplot.h:126
static void SetDefaultErrorBars(ErrorBars errorBars)
Change default errorbars style for all newly created objects.
Definition gnuplot.cc:354
Style
The plotting style to use for this dataset.
Definition gnuplot.h:111
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:143
void AddDataset(const GnuplotDataset &dataset)
Definition gnuplot.cc:785
void SetLegend(const std::string &xLegend, const std::string &yLegend)
Definition gnuplot.cc:765
void SetTerminal(const std::string &terminal)
Definition gnuplot.cc:753
void AppendExtra(const std::string &extra)
Definition gnuplot.cc:778
void GenerateOutput(std::ostream &os)
Writes gnuplot commands and data values to a single output stream.
Definition gnuplot.cc:791
void SetExtra(const std::string &extra)
Definition gnuplot.cc:772
void SetTitle(const std::string &title)
Definition gnuplot.cc:759
void SetOutputFilename(const std::string &outputFilename)
Definition gnuplot.cc:726
a unique identifier for an interface.
Definition type-id.h:48
TypeId SetParent(TypeId tid)
Set the parent TypeId.
Definition type-id.cc:1001
#define NS_ABORT_MSG(msg)
Unconditional abnormal program termination with a message.
Definition abort.h:38
#define NS_LOG_COMPONENT_DEFINE(name)
Define a Log component with a specific name.
Definition log.h:191
#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:250
#define NS_OBJECT_ENSURE_REGISTERED(type)
Register an Object subclass with the TypeId system.
Definition object-base.h:35
Every class exported by the ns3 library is enclosed in the ns3 namespace.