TorchCraftAI
A bot for machine learning research on StarCraft: Brood War
visdom.h
1 /*
2  * Copyright (c) 2017-present, Facebook, Inc.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #pragma once
9 
10 #include <autogradpp/autograd.h>
11 #include <mapbox/variant.hpp>
12 
13 #include <string>
14 #include <unordered_map>
15 
16 namespace visdom {
17 
19  std::string server;
20  int port = 8097;
21  bool ipv6 = true; // unused
22  bool proxy = false; // unused
23 
24  ConnectionParams() : server("http://localhost") {}
26  std::string server,
27  int port = 8097,
28  bool ipv6 = true,
29  bool proxy = false)
30  : server(std::move(server)), port(port), ipv6(ipv6), proxy(proxy) {}
31 };
32 
33 using StringList = std::vector<std::string>;
34 using StringListMap = std::unordered_map<int, StringList>;
35 using OptionValue = mapbox::util::variant<bool,
36  double,
37  std::string,
38  StringList,
40  torch::Tensor>;
41 using Options = std::unordered_map<std::string, OptionValue>;
42 
43 struct OptionPair {
44  OptionPair(std::string key, OptionValue&& value)
45  : first(std::move(key)), second(std::move(value)) {}
46  OptionPair(std::string key, char const* value)
47  : first(std::move(key)), second(std::string(value)) {}
48  OptionPair(std::string key, bool value)
49  : first(std::move(key)), second(value) {}
50  OptionPair(std::string key, double value)
51  : first(std::move(key)), second(value) {}
52  OptionPair(std::string key, float value)
53  : first(std::move(key)), second(double(value)) {}
54  OptionPair(std::string key, int value)
55  : first(std::move(key)), second(double(value)) {}
56  OptionPair(std::string key, std::initializer_list<std::string> value)
57  : first(std::move(key)), second(std::vector<std::string>(value)) {}
58 
59  std::string first;
61 };
62 
63 inline Options makeOpts(std::initializer_list<OptionPair> init) {
64  Options opts;
65  for (auto const& it : init) {
66  opts[it.first] = it.second;
67  }
68  return opts;
69 }
70 
71 enum class UpdateMethod {
72  None,
73  Append,
74  Insert,
75  Replace,
76  Remove,
77 };
78 
79 class VisdomImpl;
80 class Visdom {
81  public:
82  Visdom(
84  std::string env = "main",
85  bool send = true);
86  ~Visdom();
87 
88  /**
89  * This funciton allows the user to save envs that are alive on the Tornado
90  * server.
91  * The envs can be specified as a list of env ids.
92  */
93  std::string save(std::vector<std::string> const& envs);
94 
95  /**
96  * This function closes a specific window.
97  * Use `win={}` to close all windows in an env.
98  */
99  std::string close(
100  std::string const& win = std::string(),
101  std::string const& env = std::string());
102 
103  /**
104  * This funciton prints text in a box.
105  * It takes as input an `text` string. No specific `opts` are currently
106  * supported.
107  */
108  std::string text(std::string const& txt, Options const& opts = Options()) {
109  return text(txt, {}, {}, opts);
110  }
111  std::string text(
112  std::string const& txt,
113  std::string const& win,
114  Options const& opts = Options()) {
115  return text(txt, win, {}, opts);
116  }
117  std::string text(
118  std::string const& txt,
119  std::string const& win,
120  std::string const& env,
121  Options const& opts = Options());
122 
123  /**
124  * This function draws a heatmap.
125  * It takes as input an `NxM` tensor `X` that specifies the value at each
126  * location in the heatmap.
127  *
128  * The following `opts` are supported:
129  * - `opts.colormap`: colormap (string; default = "Viridis")
130  * - `opts.xmin`: clip minimum value (number, default = `X:min()`)
131  * - `opts.xmax`: clip maximum value (number, default = `X:max()`)
132  * - `opts.columnnames`: vector of strings containing x-axis labels
133  * - `opts.rownames`: vector of strings containing y-axis labels
134  */
135  std::string heatmap(torch::Tensor tensor, Options const& opts = Options()) {
136  return heatmap(tensor, {}, {}, opts);
137  }
138  std::string heatmap(
139  torch::Tensor tensor,
140  std::string const& win,
141  Options const& opts = Options()) {
142  return heatmap(tensor, win, {}, opts);
143  }
144  std::string heatmap(
145  torch::Tensor tensor,
146  std::string const& win,
147  std::string const& env,
148  Options const& opts = Options());
149 
150  /**
151  * This function draws a 2D or 3D scatter plot. It takes in an `Nx2` or `Nx3`
152  * tensor `X` that specifies the locations of the `N` points in the scatter
153  * plot. An optional `N` tensor `Y` containing discrete labels that range
154  * between `1` and `K` can be specified as well -- the labels will be
155  * reflected in the colors of the markers.
156  *
157  * `update` can be used to efficiently update the data of an existing line.
158  * Use 'append' to append data, 'replace' to use new data. Update data that
159  * is all NaN is ignored (can be used for masking update).
160  *
161  * The following `opts` are supported:
162  *
163  * - `opts.colormap` : colormap (`string`; default = `'Viridis'`)
164  * - `opts.markersymbol`: marker symbol (`string`; default = `'dot'`)
165  * - `opts.markersize` : marker size (`number`; default = `'10'`)
166  * - `opts.markercolor` : marker color (`np.array`; default = `None`)
167  * - `opts.legend` : `table` containing legend names
168  */
169  std::string scatter(
170  torch::Tensor X,
171  Options const& opts = Options(),
172  UpdateMethod update = UpdateMethod::None) {
173  return scatter(X, torch::Tensor(), {}, {}, {}, opts, update);
174  }
175  std::string scatter(
176  torch::Tensor X,
177  std::string const& win,
178  Options const& opts = Options(),
179  UpdateMethod update = UpdateMethod::None) {
180  return scatter(X, torch::Tensor(), win, {}, {}, opts, update);
181  }
182  std::string scatter(
183  torch::Tensor X,
184  std::string const& win,
185  std::string const& env,
186  Options const& opts = Options(),
187  UpdateMethod update = UpdateMethod::None) {
188  return scatter(X, torch::Tensor(), win, env, {}, opts, update);
189  }
190  std::string scatter(
191  torch::Tensor X,
192  std::string const& win,
193  std::string const& env,
194  std::string const& name,
195  Options const& opts = Options(),
196  UpdateMethod update = UpdateMethod::None) {
197  return scatter(X, torch::Tensor(), win, env, name, opts, update);
198  }
199  std::string scatter(
200  torch::Tensor X,
201  torch::Tensor Y,
202  Options const& opts = Options(),
203  UpdateMethod update = UpdateMethod::None) {
204  return scatter(X, Y, {}, {}, {}, opts, update);
205  }
206  std::string scatter(
207  torch::Tensor X,
208  torch::Tensor Y,
209  std::string const& win,
210  Options const& opts = Options(),
211  UpdateMethod update = UpdateMethod::None) {
212  return scatter(X, Y, win, {}, {}, opts, update);
213  }
214  std::string scatter(
215  torch::Tensor X,
216  torch::Tensor Y,
217  std::string const& win,
218  std::string const& env,
219  Options const& opts = Options(),
220  UpdateMethod update = UpdateMethod::None) {
221  return scatter(X, Y, win, env, {}, opts, update);
222  }
223  std::string scatter(
224  torch::Tensor X,
225  torch::Tensor Y,
226  std::string const& win,
227  std::string const& env,
228  std::string const& name,
229  Options const& opts = Options(),
231 
232  /**
233  * This function draws a line plot. It takes in an `N` or `NxM` tensor `Y`
234  * that specifies the values of the `M` lines (that connect `N` points) to
235  * plot. It also takes an optional `X` tensor that specifies the corresponding
236  * x-axis values; `X` can be an `N` tensor (in which case all lines will share
237  * the same x-axis values) or have the same size as `Y`.
238  *
239  * `update` can be used to efficiently update the data of an existing line.
240  * Use 'append' to append data, 'replace' to use new data. Update data that
241  * is all NaN is ignored (can be used for masking update).
242  *
243  * The following `opts` are supported:
244  *
245  * - `opts.fillarea` : fill area below line (`boolean`)
246  * - `opts.colormap` : colormap (`string`; default = `'Viridis'`)
247  * - `opts.markers` : show markers (`boolean`; default = `false`)
248  * - `opts.markersymbol`: marker symbol (`string`; default = `'dot'`)
249  * - `opts.markersize` : marker size (`number`; default = `'10'`)
250  * - `opts.legend` : `table` containing legend names
251  *
252  * If `update` is specified, the figure will be updated without
253  * creating a new plot -- this can be used for efficient updating.
254  */
255  std::string line(
256  torch::Tensor Y,
257  Options const& opts = Options(),
258  UpdateMethod update = UpdateMethod::None) {
259  return line(Y, torch::Tensor(), {}, {}, {}, opts, update);
260  }
261  std::string line(
262  torch::Tensor Y,
263  std::string const& win,
264  Options const& opts = Options(),
265  UpdateMethod update = UpdateMethod::None) {
266  return line(Y, torch::Tensor(), win, {}, {}, opts, update);
267  }
268  std::string line(
269  torch::Tensor Y,
270  std::string const& win,
271  std::string const& env,
272  Options const& opts = Options(),
273  UpdateMethod update = UpdateMethod::None) {
274  return line(Y, torch::Tensor(), win, env, {}, opts, update);
275  }
276  std::string line(
277  torch::Tensor Y,
278  std::string const& win,
279  std::string const& env,
280  std::string const& name,
281  Options const& opts = Options(),
282  UpdateMethod update = UpdateMethod::None) {
283  return line(Y, torch::Tensor(), win, env, name, opts, update);
284  }
285  std::string line(
286  torch::Tensor Y,
287  torch::Tensor X,
288  Options const& opts = Options(),
289  UpdateMethod update = UpdateMethod::None) {
290  return line(Y, X, {}, {}, {}, opts, update);
291  }
292  std::string line(
293  torch::Tensor Y,
294  torch::Tensor X,
295  std::string const& win,
296  Options const& opts = Options(),
297  UpdateMethod update = UpdateMethod::None) {
298  return line(Y, X, win, {}, {}, opts, update);
299  }
300  std::string line(
301  torch::Tensor Y,
302  torch::Tensor X,
303  std::string const& win,
304  std::string const& env,
305  Options const& opts = Options(),
306  UpdateMethod update = UpdateMethod::None) {
307  return line(Y, X, win, env, {}, opts, update);
308  }
309  std::string line(
310  torch::Tensor Y,
311  torch::Tensor X,
312  std::string const& win,
313  std::string const& env,
314  std::string const& name,
315  Options const& opts = Options(),
317 
318  private:
319  VisdomImpl* d_;
320 };
321 
322 } // namespace visdom
std::string scatter(torch::Tensor X, Options const &opts=Options(), UpdateMethod update=UpdateMethod::None)
This function draws a 2D or 3D scatter plot.
Definition: visdom.h:169
std::string line(torch::Tensor Y, std::string const &win, Options const &opts=Options(), UpdateMethod update=UpdateMethod::None)
Definition: visdom.h:261
Definition: checkpointer.h:12
Definition: visdom.h:80
std::string line(torch::Tensor Y, std::string const &win, std::string const &env, std::string const &name, Options const &opts=Options(), UpdateMethod update=UpdateMethod::None)
Definition: visdom.h:276
std::string heatmap(torch::Tensor tensor, std::string const &win, Options const &opts=Options())
Definition: visdom.h:138
std::string line(torch::Tensor Y, torch::Tensor X, std::string const &win, Options const &opts=Options(), UpdateMethod update=UpdateMethod::None)
Definition: visdom.h:292
std::string line(torch::Tensor Y, std::string const &win, std::string const &env, Options const &opts=Options(), UpdateMethod update=UpdateMethod::None)
Definition: visdom.h:268
std::string heatmap(torch::Tensor tensor, Options const &opts=Options())
This function draws a heatmap.
Definition: visdom.h:135
Definition: visdom.h:18
STL namespace.
Definition: visdom.cpp:400
std::string scatter(torch::Tensor X, torch::Tensor Y, std::string const &win, std::string const &env, Options const &opts=Options(), UpdateMethod update=UpdateMethod::None)
Definition: visdom.h:214
std::string scatter(torch::Tensor X, torch::Tensor Y, Options const &opts=Options(), UpdateMethod update=UpdateMethod::None)
Definition: visdom.h:199
UpdateMethod
Definition: visdom.h:71
OptionPair(std::string key, double value)
Definition: visdom.h:50
std::string scatter(torch::Tensor X, torch::Tensor Y, std::string const &win, Options const &opts=Options(), UpdateMethod update=UpdateMethod::None)
Definition: visdom.h:206
mapbox::util::variant< bool, double, std::string, StringList, StringListMap, torch::Tensor > OptionValue
Definition: visdom.h:40
std::string first
Definition: visdom.h:59
int port
Definition: visdom.h:20
OptionPair(std::string key, OptionValue &&value)
Definition: visdom.h:44
ConnectionParams()
Definition: visdom.h:24
ConnectionParams(std::string server, int port=8097, bool ipv6=true, bool proxy=false)
Definition: visdom.h:25
std::string server
Definition: visdom.h:19
std::string line(torch::Tensor Y, torch::Tensor X, std::string const &win, std::string const &env, Options const &opts=Options(), UpdateMethod update=UpdateMethod::None)
Definition: visdom.h:300
std::string scatter(torch::Tensor X, std::string const &win, Options const &opts=Options(), UpdateMethod update=UpdateMethod::None)
Definition: visdom.h:175
OptionPair(std::string key, float value)
Definition: visdom.h:52
std::string line(torch::Tensor Y, Options const &opts=Options(), UpdateMethod update=UpdateMethod::None)
This function draws a line plot.
Definition: visdom.h:255
OptionPair(std::string key, std::initializer_list< std::string > value)
Definition: visdom.h:56
OptionValue second
Definition: visdom.h:60
std::string text(std::string const &txt, std::string const &win, Options const &opts=Options())
Definition: visdom.h:111
std::vector< std::string > StringList
Definition: visdom.h:33
std::string line(torch::Tensor Y, torch::Tensor X, Options const &opts=Options(), UpdateMethod update=UpdateMethod::None)
Definition: visdom.h:285
std::unordered_map< int, StringList > StringListMap
Definition: visdom.h:34
OptionPair(std::string key, char const *value)
Definition: visdom.h:46
Definition: visdom.h:43
std::unordered_map< std::string, OptionValue > Options
Definition: visdom.h:41
std::string scatter(torch::Tensor X, std::string const &win, std::string const &env, std::string const &name, Options const &opts=Options(), UpdateMethod update=UpdateMethod::None)
Definition: visdom.h:190
bool ipv6
Definition: visdom.h:21
bool proxy
Definition: visdom.h:22
OptionPair(std::string key, int value)
Definition: visdom.h:54
std::string scatter(torch::Tensor X, std::string const &win, std::string const &env, Options const &opts=Options(), UpdateMethod update=UpdateMethod::None)
Definition: visdom.h:182
std::string text(std::string const &txt, Options const &opts=Options())
This funciton prints text in a box.
Definition: visdom.h:108
Options makeOpts(std::initializer_list< OptionPair > init)
Definition: visdom.h:63
OptionPair(std::string key, bool value)
Definition: visdom.h:48