numplot 0.0.3 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2d354b2cc2c06f15dfb5130ea2ee5c48f1f157f2
4
- data.tar.gz: 04ddb3b2c06dc8f02b43d9920b019df571abc151
3
+ metadata.gz: cdbe4f92ef197a8a2d1a10ef3a349209c40cda37
4
+ data.tar.gz: 5464bd3ae6a2871a484d46fd059eb5e719e20a14
5
5
  SHA512:
6
- metadata.gz: 7aad96c6901b48822dcc03bc3ae5dd6bc2f0c90e5662c3dab090295d553096e9a08321e56b050bbdac9586f0265646a66ee885b6a5045c033bb213e1d768a444
7
- data.tar.gz: e9acde2f4a6165047da57a44de4f223a751a89c084d284fe7508f2467fffcf81ee598d64897f812517483a95c749976b94fa0decbd22813d3f3597b81156867d
6
+ metadata.gz: cac8b0b03249cc8d1fcf580751c0c7f5d0e1f99a8d7d5bb7ec36ed480c42473fb5d220367dd6d20b8022d472e85fd5e9de38bed0bd5e970f861a78a0aa94fd09
7
+ data.tar.gz: 7ea8b225b4d3cfd3d14670266e95ad6735a431f99946920386b455d628939ab7e5411e44ef65edbf290df02ad9272e6081cfa8e2edd4aa0b93e421611bb8cb80
data/LICENSE ADDED
@@ -0,0 +1,9 @@
1
+ Copyright (c) 2013, Ippei Obayashi
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
5
+
6
+ * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
7
+ * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
8
+
9
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,29 @@
1
+ # NumPlot
2
+
3
+ This library is a wrapper library of the gnuplot command.
4
+
5
+ ## Install
6
+
7
+ * Install gnuplot command
8
+ * gem install numplot
9
+
10
+ ## Usage
11
+ In your scirpt,
12
+
13
+ require 'numplot'
14
+
15
+ and use NumPlot module.
16
+ Please see {file:sample/sample1.rb} as an example.
17
+
18
+ ## URLs
19
+ * [bitbucket repository](https://bitbucket.org/ohai/numplot)
20
+
21
+ ## Author
22
+ Ippei Obayashi <ohai@kmc.gr.jp>
23
+
24
+ ## Copyright
25
+ Copyright (c) 2013, Ippei Obayashi
26
+ All rights reserved.
27
+
28
+ The software is licensed under the BSD 2-Clause License.
29
+ Please see the {file:LICENSE} for more information.
@@ -17,17 +17,31 @@ module NumPlot
17
17
  def convert_range(range)
18
18
  case range
19
19
  when String then range
20
- when Range then "[#{range.begin}:#{range.end}]"
20
+ when Range then range_pair(range.begin, range.end)
21
21
  when Array
22
22
  if range.size != 2
23
23
  raise TypeError, "Cannot convert #{range.inspect} to gnuplot's range"
24
24
  end
25
- "[#{range[0]}:#{range[1]}]"
25
+ range_pair(range[0], range[1])
26
26
  else
27
27
  raise TypeError, "Cannot convert #{range.inspect} to gnuplot's range"
28
28
  end
29
29
  end
30
30
 
31
+ # Convert a pair of lower and upper bounds to the gnuplot "range"
32
+ # parmameter stirng.
33
+ # This method is used for converting ranges and arrays of two elements
34
+ # into strings.
35
+ def range_pair(lower, upper)
36
+ "[#{range_bound(lower)}:#{range_bound(upper)}]"
37
+ end
38
+
39
+ def range_bound(x)
40
+ return "" if Float === x && x.infinite?
41
+ return "" if x.nil?
42
+ x.to_s
43
+ end
44
+
31
45
  def convert_color(color)
32
46
  case color
33
47
  when RGB then color.to_gnuplot_string
@@ -49,8 +63,22 @@ module NumPlot
49
63
  def quote(str)
50
64
  "\"#{str}\""
51
65
  end
52
- end
53
66
 
67
+ def escape_label(s)
68
+ s.gsub(/[\^_\{\}@&\\~]/){|s| "\\#{s}"}
69
+ end
70
+
71
+ def quote_label(s, enhanced)
72
+ return "'#{s.text}'" if s.kind_of?(EnhancedText)
73
+
74
+ if enhanced
75
+ "'#{escape_label(s)}'"
76
+ else
77
+ "\"#{s}\""
78
+ end
79
+ end
80
+ end
81
+
54
82
  # The class representing colors in gnuplot.
55
83
  #
56
84
  # You can use three types of color specifications like the following
@@ -210,7 +238,7 @@ module NumPlot
210
238
  # plotter = NumPlot::Plotter.new
211
239
  # # Set parameters
212
240
  # plotter.set("term", "png")
213
- # plotter.set("output", "\"sin.png\"") # Need to quote
241
+ # plotter.output("sin.png")
214
242
  # plotter.xrange -2.0 .. 2.0
215
243
  # # Set datasets
216
244
  # plotter.datasets << NumPlot::Dataset.rows(data, title: "sin(x)", with: line)
@@ -280,14 +308,16 @@ module NumPlot
280
308
  # @overload $0(range, *opts)
281
309
  # Set $0 parameter.
282
310
  # You can specify the range with one of three styles.
311
+ #
283
312
  # * a string such as: "[-1.0:1.0]" or "[:2.0]"
284
313
  # * a range such as: -1.0 .. 1.0 or -Float::INFINITY .. 2.0
285
314
  # * an array such as: [-1.0, 1.0] or [nil, 2.0]
315
+ #
286
316
  # You can also specify the following options as args:
287
- # * "reverse"
288
- # * "no reverse"
289
- # * "writeback"
290
- # * "no writeback"
317
+ #
318
+ # * reverse: or noreverse: true or false
319
+ # * writeback: or nowriteback: true or false
320
+ #
291
321
  # @param range[String,Range,Array] the range of x axis
292
322
  # @param opts[Array<String>] options
293
323
  # @return [self]
@@ -316,15 +346,29 @@ module NumPlot
316
346
  # @!visibility private
317
347
  RANGE_OPTIONS = [YesNoOption[:reverse], YesNoOption[:writeback]]
318
348
 
349
+ # Enable an enhanced text mode of gnuplot.
350
+ def enable_enhanced_text_mode
351
+ @enhanced = true
352
+ set("termoption", "enhanced")
353
+ end
354
+
355
+ # Disable an enhanced text mode of gnuplot.
356
+ def disable_enhanced_text_mode
357
+ @enhanced = false
358
+ set("termoption", "noenhanced")
359
+ end
360
+
319
361
  # @!macro [new] label
320
362
  # @overload $0(label, opts={})
321
363
  # Set $0 parameter.
322
364
  # You can specify the following options as a hash:
323
- # * offset: OFFSET
324
- # * font: FONT
325
- # * textcolor: COLOR (tc: is also available)
326
- # * enhanced: true OR noenhanced: true
365
+ #
366
+ # * offset: the label is moved by the given offset
367
+ # * font: font info {Font}
368
+ # * textcolor: color {RGB} (tc: is also available)
369
+ # * enhanced: or noenhanced: true or false
327
370
  # * rotate: (TODO: FIX IT) OR norotate: true
371
+ #
328
372
  # @param label[String] label string
329
373
  # @param opts[{Symbol => Object}] options
330
374
  # @return [self]
@@ -341,16 +385,29 @@ module NumPlot
341
385
  def y2label(*args); _label("y2label", *args); end
342
386
 
343
387
  def _label(type, *args)
344
- set(type, LabelParser.parse(*args))
388
+ set(type, LabelParser.parse(@enhanced, *args))
345
389
  end
346
390
  private :_label
347
391
 
392
+ # Set output target.
393
+ # You can specify target file name.
394
+ # @param fname[String,nil] name of target file
395
+ # @return [void]
396
+ def output(fname)
397
+ if fname.nil?
398
+ set("output")
399
+ else
400
+ set("output", "\"#{fname}\"")
401
+ end
402
+ end
403
+
348
404
  # Plot given datasets with given parameters.
349
405
  # @overload plot(opts={})
350
406
  # Open a gnuplot process and write commands and data to the process.
351
407
  # You get the result of plotting on a window or an image file
352
408
  #
353
409
  # You can specify the following options:
410
+ #
354
411
  # * :persist - Add -persist to gnuplot command.
355
412
  # The default value is true.
356
413
  # * :waiting - Wait a gnuplot process to be terminate.
@@ -390,7 +447,7 @@ module NumPlot
390
447
  @commands.each{|cmd| pr.puts(cmd) }
391
448
  pr.write(@plot_command)
392
449
  pr.write(" ")
393
- pr.write(@datasets.map{|e| e.to_plot_arg }.join(", "))
450
+ pr.write(@datasets.map{|e| e.to_plot_arg(@enhanced) }.join(", "))
394
451
  pr.write("\n")
395
452
  pr.write(@datasets.map{|e| e.to_plot_dataset }.compact.join("e\n"))
396
453
  end
@@ -400,6 +457,7 @@ module NumPlot
400
457
  # The dataset class plotted by {Plotter}.
401
458
  #
402
459
  # You can construct a dataset object using one of the following three methods:
460
+ #
403
461
  # * {.columns}: construct a dataset from columns
404
462
  # * {.rows}: construct a dataset from rows
405
463
  # * {.histogram}: construct a histogram dataset
@@ -410,12 +468,14 @@ module NumPlot
410
468
  # The dataset object is registered to {Plotter} object using
411
469
  # {Plotter#add_dataset}.
412
470
  #
413
- # == Parameters for a dataset
471
+ # # Parameters for a dataset
472
+ #
414
473
  # When you call {.columns}, {.rows}, or {.histogram},
415
474
  # you can give some parameters to determine the style of
416
475
  # plotting.
417
476
  # The parameters are as follows. If you know the details
418
477
  # of these parameters, please read the gnuplot documents.
478
+ #
419
479
  # * title: "TITLE" or notitle: true
420
480
  # * with: "points", "dots", etc.
421
481
  # * axis:
@@ -476,31 +536,51 @@ module NumPlot
476
536
  new(data.map(&:to_a), opts)
477
537
  end
478
538
 
479
-
539
+ # Construct a dataset object from a sequence of sequences of rows
540
+ #
541
+ # If your data is like
542
+ # [[[x00, y00] [x01, y01], ... ], [[x10, y10], [x11, y11], ...], ..]
543
+ # you should use this method.
544
+ #
545
+ # You can give some parameters as a hash
546
+ # to set a plotting style. Please see
547
+ # {Dataset} for details.
548
+ # @param data[Enumerable<Enumerable<Numeric>>]
549
+ # @param opts[{Symbol => object}] options
550
+ # @return [Dataset]
551
+ def self.lines(data, opts={})
552
+ data = data.map{|lines|
553
+ lines.to_a + [[]]
554
+ }.flatten(1)
555
+ p data
556
+ new(data, opts)
557
+ end
558
+
480
559
  # Construct a histogram dataset object.
481
560
  #
482
561
  # You need to give some parameters to construct a histogram.
562
+ #
483
563
  # * dim: the dimension of your data, now only 1-dim data is supported
484
564
  # (default is 1)
485
565
  # * binsize: the bin size (required, Numeric)
486
- # * base: the coordinate of a left side of a bin (default is 0.0).
566
+ # * base: the coordinate of a center of a bin (default is 0.0).
487
567
  # If binsize = 1.0 and base = 0.0, the bins are
488
- # ..., [-1.0, 0.0), [0.0, 1.0), ... .
489
- # If binsize = 1.0 and base = 0.5, the bins are
490
568
  # ..., [-1.5, -0.5), [-0.5, 0.5), [0.5, 1.5), ... .
569
+ # If binsize = 1.0 and base = 0.5, the bins are
570
+ # ..., [-1.0, 0.0), [0.0, 1.0), ... .
491
571
  # * histogram_style: You can choose one of the following normalization
492
572
  # scheme:
493
- # * :count not normalized
494
- # * :ratio normailized to the number of data points
495
- # * :density normalized to (the number of data points)*(binsize).
496
- # You choose this option if you want to show the probability density
497
- # function from the data.
573
+ # * :count not normalized
574
+ # * :ratio normailized to the number of data points
575
+ # * :density normalized to (the number of data points)*(binsize).
576
+ # You choose this option if you want to show the probability density
577
+ # function from the data.
498
578
  # * cumulative: construct a cumulative histogram if true.
499
579
  # If the cumulative option is true, the meaning of
500
580
  # histogram_style option is changed.
501
- # * :count not normalized
502
- # * :ratio, :density normalized by the number of data, this means that
503
- # the maximum value of a cumulative histogram is normalized to 1.
581
+ # * :count not normalized
582
+ # * :ratio, :density normalized by the number of data, this means that
583
+ # the maximum value of a cumulative histogram is normalized to 1.
504
584
  #
505
585
  # You can also give some other parameters to determine
506
586
  # a plotting style. See {Dataset} for details.
@@ -583,7 +663,8 @@ module NumPlot
583
663
  # Return a stirng for gnuplot's "plot" or "splot" like "'-' with dots"
584
664
  # @return [String]
585
665
  # @!visibility private
586
- def to_plot_arg
666
+ def to_plot_arg(in_enhanced_mode)
667
+ @opts[:_in_enhanced_text_mode] = in_enhanced_mode
587
668
  ret = "'-'"
588
669
  OPTIONS.each do |opt|
589
670
  ret << opt.plot_arg(@opts)
@@ -600,13 +681,15 @@ module NumPlot
600
681
 
601
682
  # @!visibility private
602
683
  class Title
684
+ include Conversion
685
+
603
686
  def plot_arg(h)
604
687
  if h[:notitle]
605
688
  " notitle"
606
689
  elsif h.has_key?(:title)
607
- " title \'#{h[:title]}\'"
690
+ " title #{quote_label(h[:title], h[:_in_enhanced_text_mode])}"
608
691
  else
609
- ""
692
+ " notitle"
610
693
  end
611
694
  end
612
695
  end
@@ -633,7 +716,7 @@ module NumPlot
633
716
 
634
717
  # @!visibility private
635
718
  class LabelParser
636
- def self.parse(*args)
719
+ def self.parse(enhanced, *args)
637
720
  if Hash === args.last
638
721
  opts = args.pop
639
722
  else
@@ -645,7 +728,7 @@ module NumPlot
645
728
  raise ArgumentError, "Unknown label argument #{args.inspect}" if !args.empty?
646
729
  raise ArgumentError, "No label text" if label.nil?
647
730
 
648
- gnuplot_string = Conversion.quote(label)
731
+ gnuplot_string = Conversion.quote_label(label, enhanced)
649
732
 
650
733
  OPTIONS.each do |opt|
651
734
  gnuplot_string << opt.plot_arg(opts)
@@ -662,6 +745,28 @@ module NumPlot
662
745
  BoolOption[:norotate],
663
746
  ]
664
747
  end
665
-
666
748
 
749
+ # Represents "enhanced text" in enhanced text mode
750
+ # All unmarked texts are handled as "non enhanced text" and
751
+ # all special characters are escaped in enhanced text mode.
752
+ class EnhancedText
753
+ # Create an EnhancedText object.
754
+ #
755
+ # You can use the short-cut method {NumPlot#ET}.
756
+ def initialize(text)
757
+ @text = text
758
+ end
759
+
760
+ # @return [String] a text
761
+ attr_reader :text
762
+ end
763
+
764
+ # Mark a text as "enhanced text" in enhanced text mode
765
+ #
766
+ # @return [EnhancedText]
767
+ # @param text[String] an text handled by "enhanced text mode"
768
+ def ET(text)
769
+ EnhancedText.new(text)
770
+ end
771
+ module_function :ET
667
772
  end
@@ -22,7 +22,7 @@ plotter <<
22
22
  normal_random_dist = Array.new(10000){ rand_normal }
23
23
  plotter <<
24
24
  NumPlot::Dataset.histo(normal_random_dist,
25
- binsize: 0.1, histogram_style: :density,
25
+ binsize: 0.07, histogram_style: :density,
26
26
  title: "a normal distribution")
27
27
 
28
28
  plotter.xrange(-1.0 .. 1.0)
@@ -0,0 +1,23 @@
1
+ require 'numplot'
2
+
3
+ require 'test-unit'
4
+
5
+ class TestConversion < Test::Unit::TestCase
6
+ include NumPlot
7
+
8
+ def test_convert_range
9
+ inf = Float::INFINITY
10
+ assert_equal("[1:2]", Conversion.convert_range([1,2]))
11
+ assert_equal("[1:2]", Conversion.convert_range(1..2))
12
+ assert_equal("[1:2]", Conversion.convert_range(1...2))
13
+ assert_equal("[:]", Conversion.convert_range(-inf .. inf))
14
+ assert_equal("[2.0:]", Conversion.convert_range(2.0 .. inf))
15
+ assert_equal("[:2.0]", Conversion.convert_range([nil, 2.0]))
16
+ assert_equal("[:2.0]", Conversion.convert_range([-inf, 2.0]))
17
+ end
18
+
19
+ def test_escape_label
20
+ assert_equal('\{\{/Symbol q\}\_n\}\^r\~a\{.8-\}',
21
+ Conversion.escape_label("{{/Symbol q}_n}^r~a{.8-}"))
22
+ end
23
+ end
@@ -6,9 +6,11 @@ class TestRGB < Test::Unit::TestCase
6
6
  include NumPlot
7
7
 
8
8
  def test_s_parse
9
- assert_equal("\"foo\"", LabelParser.parse("foo"))
9
+ assert_equal("\"foo\"", LabelParser.parse(false, "foo"))
10
10
  assert_equal("\"foo\" offset 32 font \"bar,14\"",
11
- LabelParser.parse("foo", offset: 32, font: Font["bar",14]))
12
-
11
+ LabelParser.parse(false, "foo", offset: 32, font: Font["bar",14]))
12
+ assert_equal("'foo'", LabelParser.parse(true, "foo"))
13
+ assert_equal("'x\\_y'", LabelParser.parse(true, "x_y"))
14
+ assert_equal("'x_y'", LabelParser.parse(true, ET("x_y")))
13
15
  end
14
16
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: numplot
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.3
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ippei Obayashi
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-03-13 00:00:00.000000000 Z
11
+ date: 2013-07-17 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: test-unit
@@ -35,12 +35,16 @@ files:
35
35
  - lib/numplot/option.rb
36
36
  - sample/sample1.rb
37
37
  - test/run_test.rb
38
+ - test/test_conversion.rb
38
39
  - test/test_labelparser.rb
39
40
  - test/test_options.rb
40
41
  - test/test_plotter.rb
41
42
  - test/test_rgb.rb
43
+ - README.md
44
+ - LICENSE
42
45
  homepage: http://www.kmc.gr.jp
43
- licenses: []
46
+ licenses:
47
+ - 2-clause BSDL
44
48
  metadata: {}
45
49
  post_install_message:
46
50
  rdoc_options: []
@@ -58,9 +62,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
58
62
  version: '0'
59
63
  requirements: []
60
64
  rubyforge_project:
61
- rubygems_version: 2.0.0
65
+ rubygems_version: 2.0.3
62
66
  signing_key:
63
67
  specification_version: 4
64
68
  summary: An wrapper for gnuplot
65
69
  test_files: []
66
- has_rdoc: