-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathSeries.cs
More file actions
391 lines (342 loc) · 12.1 KB
/
Series.cs
File metadata and controls
391 lines (342 loc) · 12.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
//
// Series.cs
//
// Author:
// Tom Diethe <tom.diethe@bristol.ac.uk>
//
// Copyright (c) 2015 University of Bristol
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
namespace PythonPlotter
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
/// <summary>
/// The series interface.
/// </summary>
public interface ISeries
{
/// <summary>
/// Gets or sets the label.
/// </summary>
/// <value>The label.</value>
string Label { get; set; }
/// <summary>
/// Gets or sets the row index (for subplots)
/// </summary>
int Row { get; set; }
/// <summary>
/// Gets or sets the column index (for subplots)
/// </summary>
int Column { get; set; }
/// <summary>
/// Gets or sets the color.
/// </summary>
/// <value>The color.</value>
string Color { get; set; }
/// <summary>
/// Plot to the specified script.
/// </summary>
/// <param name="ax">The axis to plot to.</param>
/// <param name="script">Script.</param>
void Plot(string ax, StringBuilder script);
}
/// <summary>
/// Base class for series.
/// </summary>
public abstract class BaseSeries : ISeries
{
/// <summary>
/// Gets or sets the label.
/// </summary>
/// <value>The label.</value>
public string Label { get; set; }
/// <summary>
/// Gets or sets the row index (for subplots)
/// </summary>
public int Row { get; set; }
/// <summary>
/// Gets or sets the column index (for subplots)
/// </summary>
public int Column { get; set; }
/// <summary>
/// The color.
/// </summary>
public string Color { get; set; }
/// <summary>
/// Plot to the specified script.
/// </summary>
/// <param name="ax">The axis to plot to.</param>
/// <param name="script">Script.</param>
public virtual void Plot(string ax, StringBuilder script)
{
throw new NotImplementedException();
}
}
/// <summary>
/// Line series.
/// </summary>
public class LineSeries : BaseSeries
{
/// <summary>
/// Gets or sets the x values.
/// </summary>
public IEnumerable<double> X { get; set; }
/// <summary>
/// Gets or sets the y values.
/// </summary>
public IEnumerable<double> Y { get; set; }
/// <summary>
/// Gets or sets the line style
/// </summary>
public string LineStyle { get; set; }
/// <summary>
/// Plot to the specified script.
/// </summary>
/// <param name="ax">The axis to plot to.</param>
/// <param name="script">Script.</param>
public override void Plot(string ax, StringBuilder script)
{
string label = string.IsNullOrEmpty(Label) ? "" : $", label='{Label}'";
string color = string.IsNullOrEmpty(Color) ? "" : $", color={Color}";
string style = string.IsNullOrEmpty(LineStyle) ? "" : $", linestyle='{LineStyle}'";
if (Y == null)
{
script.AppendLine($"lines.extend({ax}.plot([{string.Join(", ", X)}]{label}{color}{style}))");
}
else
{
if (X == null)
{
throw new InvalidOperationException("X and Y should not both be null");
}
script.AppendLine(
$"lines.extend({ax}.plot([{string.Join(", ", X)}], [{string.Join(", ", Y)}]{label}{color}{style}))");
}
}
}
/// <summary>
/// Scatter Series
/// </summary>
public class ScatterSeries : LineSeries
{
/// <summary>
/// Plot to the specified script
/// </summary>
/// <param name="ax">The axis to plot to.</param>
/// <param name="script"></param>
public override void Plot(string ax, StringBuilder script)
{
string label = string.IsNullOrEmpty(Label) ? "" : $", label='{Label}'";
string color = string.IsNullOrEmpty(Color) ? "" : $", c={Color}";
if (Y == null)
{
script.AppendLine($"{ax}.scatter([{string.Join(", ", X)}]{label}{color})");
}
else
{
if (X == null)
{
throw new InvalidOperationException("X and Y should not both be null");
}
script.AppendLine($"{ax}.scatter([{string.Join(", ", X)}], [{string.Join(", ", Y)}]{label}{color})");
}
}
}
/// <summary>
/// Bar series
/// </summary>
public class BarSeries<T> : BaseSeries
{
/// <summary>
/// Gets or sets a value indicating whether this <see cref="BarSeries"/> is horizontal.
/// </summary>
/// <value><c>true</c> if horizontal; otherwise, <c>false</c>.</value>
public bool Horizontal { get; set; }
/// <summary>
/// Gets or sets the dependent values.
/// </summary>
/// <value>The dependent values.</value>
public IEnumerable<double> DependentValues { get; set; }
/// <summary>
/// Gets or sets the independent values.
/// </summary>
/// <value>The independent values.</value>
public IEnumerable<T> IndependentValues { get; set; }
/// <summary>
/// Gets or sets the error values.
/// </summary>
/// <value>The error values.</value>
public IEnumerable<double> ErrorValues { get; set; }
/// <summary>
/// Gets or sets the X tick labels.
/// </summary>
/// <value>The X tick labels.</value>
public IEnumerable<string> XTickLabels { get; set; }
/// <summary>
/// Gets or sets the width.
/// </summary>
/// <value>The width.</value>
public double Width { get; set; }
/// <summary>
/// Plot to the specified script.
/// </summary>
/// <param name="ax">The axis to plot to.</param>
/// <param name="script">Script.</param>
public override void Plot(string ax, StringBuilder script)
{
if (DependentValues == null)
{
throw new InvalidOperationException("DependentValues should not be null");
}
var command = Horizontal ? "barh" : "bar";
var errorString = Horizontal ? "xerr" : "yerr";
var errorValues = ErrorValues == null
? string.Empty
: $", {errorString}=[{string.Join(", ", ErrorValues)}]";
var independent = string.Join(", ", (typeof(T) == typeof(double))
? IndependentValues.Select(ia => ia.ToString())
: DependentValues.Select((ia, i) => i.ToString("D")));
var dependent = string.Join(", ", DependentValues);
var width = Math.Abs(Width) < double.Epsilon ? 1.0 : Width;
var color = string.IsNullOrEmpty(Color) ? "'b'" : Color;
var label = string.IsNullOrEmpty(Label) ? "" : $", label='{Label}'";
script.AppendLine(
$"lines.extend({ax}.{command}([{independent}], [{dependent}], {width}, color={color}{errorValues}{label}))");
if (IndependentValues != null && typeof(T) != typeof(double))
{
// script.AppendLine("ax = gca()");
script.AppendLine($"lines.extend({ax}.set_xticklabels(['{string.Join("', '", IndependentValues)}']))");
}
}
}
/// <summary>
/// Error line series.
/// </summary>
public class ErrorLineSeries : LineSeries
{
/// <summary>
/// The label for the error values.
/// </summary>
public string ErrorLabel { get; set; }
/// <summary>
/// Gets or sets the error values.
/// </summary>
/// <value>The error values.</value>
public IEnumerable<double> ErrorValues { get; set; }
/// <summary>
/// Gets or sets the alpha fill.
/// </summary>
/// <value>The alpha fill.</value>
public double AlphaFill { get; set; } = 0.1;
/// <summary>
/// Plot to the specified script.
/// </summary>
/// <param name="ax">The axis to plot to.</param>
/// <param name="script">Script.</param>
public override void Plot(string ax, StringBuilder script)
{
var label = string.IsNullOrEmpty(Label) ? "" : $", label='{Label}'";
var errorLabel = string.IsNullOrEmpty(Label) ? "" : $", label='{ErrorLabel}'";
var color = string.IsNullOrEmpty(Color) ? "" : $", color=c";
var x = X;
var y = Y;
if (Y == null)
{
if (X == null)
{
throw new InvalidOperationException("X and Y should not both be null");
}
x = Enumerable.Range(0, X.Count()).Select(ia => (double)ia);
y = X;
}
script.AppendLine($"x = array([{string.Join(", ", x)}])");
script.AppendLine($"y = array([{string.Join(", ", y)}])");
script.AppendLine($"e = array([{string.Join(", ", ErrorValues)}])");
script.AppendLine($"c = next(palette)");
script.AppendLine($"lines.extend({ax}.plot(x, y{label}{color}))");
script.AppendLine($"{ax}.fill_between(x, y-e, y+e, alpha={AlphaFill}{errorLabel}{color})");
}
}
/// <summary>
/// For use with matshow
/// </summary>
public class MatrixSeries : BaseSeries
{
/// <summary>
/// Gets or sets the values.
/// </summary>
public double[][] Values { get; set; }
/// <summary>
/// Gets or sets the color map.
/// </summary>
public string ColorMap { get; set; } = "gray";
/// <summary>
/// Gets or sets the values as a string.
/// </summary>
protected string ValuesAsString
{
get { return "[[" + string.Join("], [", Values.Select(ia => string.Join(", ", ia))) + "]]"; }
}
/// <summary>
/// Plot to the specified script.
/// </summary>
/// <param name="ax">The axis to plot to.</param>
/// <param name="script">Script.</param>
public override void Plot(string ax, StringBuilder script)
{
script.AppendLine($"x = array({ValuesAsString})");
script.AppendLine($"{ax}.matshow(x, cmap='{ColorMap}')");
}
}
/// <summary>
/// Hinton diagram. See http://tonysyu.github.io/mpltools/auto_examples/special/plot_hinton.html
/// </summary>
public class HintonSeries : MatrixSeries
{
/// <summary>
/// Plot to the specified script.
/// </summary>
/// <param name="ax">The axis to plot to.</param>
/// <param name="script">Script.</param>
public override void Plot(string ax, StringBuilder script)
{
// Note that the color map is ignored
// script.AppendLine("from mpltools import special");
script.AppendLine($"x = array({ValuesAsString})");
// script.AppendLine($"sca({ax})");
// script.AppendLine("special.hinton(x)");
script.AppendLine("max_weight = 2 ** np.ceil(np.log2(np.abs(x).max()))");
script.AppendLine("ax.patch.set_facecolor('gray')");
script.AppendLine("ax.set_aspect('equal', 'box')");
script.AppendLine("ax.xaxis.set_major_locator(plt.NullLocator())");
script.AppendLine("ax.yaxis.set_major_locator(plt.NullLocator())");
script.AppendLine("for (x, y), w in np.ndenumerate(x.T):");
script.AppendLine(" color = 'white' if w > 0 else 'black'");
script.AppendLine(" size = np.sqrt(abs(w) / max_weight)");
script.AppendLine(" rect = plt.Rectangle([x - size / 2, y - size / 2], size, size,");
script.AppendLine(" facecolor=color, edgecolor=color)");
script.AppendLine(" ax.add_patch(rect)");
script.AppendLine("ax.autoscale_view()");
script.AppendLine("ax.invert_yaxis()");
}
}
}