1
1
module GnuplotRB
2
2
##
3
- # === Overview
4
3
# Dataset keeps control of Datablock or String (some math functions like
5
4
# this 'x*sin(x)' or filename) and options related to original dataset
6
5
# in gnuplot (with, title, using etc).
7
6
#
8
- # === Options
7
+ # == Options
9
8
# Dataset options are explained in
10
9
# {gnuplot docs}[http://www.gnuplot.info/docs_5.0/gnuplot.pdf](pp. 80-101).
11
10
# Several common options:
@@ -28,6 +27,8 @@ class Dataset
28
27
# Order is significant for some options
29
28
OPTION_ORDER = %w( index using axes title )
30
29
30
+ private_constant :OPTION_ORDER
31
+
31
32
##
32
33
# Hash of init handlers for data given in
33
34
# different containers.
@@ -41,41 +42,39 @@ class Dataset
41
42
) if defined? Daru
42
43
43
44
##
44
- # ====== Overview
45
- # Creates new dataset out of given string with
46
- # math function or filename. If *data* isn't a string
47
- # it will create datablock to store data.
48
- # ====== Parameters
49
- # * *data* - String, Datablock or something acceptable by
50
- # Datablock.new as data (e.g. [x,y] where x and y are arrays)
51
- # * *options* - hash of options specific for gnuplot
52
- # dataset, and some special options ('file: true' will
53
- # make data to be stored inside temporary file).
54
- # ====== Examples
55
- # Math function:
45
+ # Create new dataset out of given string with math function or filename.
46
+ # If *data* isn't a string it will create datablock to store data.
47
+ #
48
+ # @param data [String, Datablock, #to_gnuplot_points] String, Datablock or something acceptable
49
+ # by Datablock.new as data (e.g. [x,y] where x and y are arrays)
50
+ # @param options [Hash] options specific for gnuplot
51
+ # dataset (see Dataset top level doc), and some special options ('file: true' will
52
+ # make data to be stored inside temporary file)
53
+ #
54
+ # @example Math function:
56
55
# Dataset.new('x*sin(x)', with: 'lines', lw: 4)
57
- # File with points:
56
+ # @example File with points:
58
57
# Dataset.new('points.data', with: 'lines', title: 'Points from file')
59
- # Some data (creates datablock stored in memory):
58
+ # @example Some data (creates datablock stored in memory):
60
59
# x = (0..5000).to_a
61
60
# y = x.map {|xx| xx*xx }
62
61
# points = [x, y]
63
62
# Dataset.new(points, with: 'points', title: 'Points')
64
- # The same data but datablock stores it in temp file:
63
+ # @example The same data but datablock stores it in temp file:
65
64
# Dataset.new(points, with: 'points', title: 'Points', file: true)
66
65
def initialize ( data , **options )
67
66
# run method by name
68
67
send ( INIT_HANDLERS [ data . class ] , data , options )
69
68
end
70
69
71
70
##
72
- # ====== Overview
73
- # Converts Dataset to string containing gnuplot dataset.
74
- # ====== Parameters
75
- # * *terminal* - must be given if data given as Datablock and
71
+ # Convert Dataset to string containing gnuplot dataset.
72
+ #
73
+ # @param terminal [Terminal] must be given if data given as Datablock and
76
74
# it does not use temp file so data should be piped out
77
- # to gnuplot via terminal before use.
78
- # ====== Examples
75
+ # to gnuplot via terminal before use
76
+ # @return [String] gnuplot dataset
77
+ # @example
79
78
# Dataset.new('points.data', with: 'lines', title: 'Points from file').to_s
80
79
# #=> "'points.data' with lines title 'Points form file'"
81
80
# Dataset.new(points, with: 'points', title: 'Points').to_s
@@ -85,26 +84,29 @@ def to_s(terminal = nil)
85
84
end
86
85
87
86
##
88
- # ====== Overview
89
- # Creates new dataset with updated data (given
90
- # data is appended to existing) and merged options .
87
+ # Create new dataset with updated data and merged options.
88
+ #
89
+ # Given data is appended to existing.
91
90
# Data is updated only if Dataset stores it in Datablock.
92
91
# Method does nothing if no options given and data isn't stored
93
92
# in in-memory Datablock.
94
- # ====== Parameters
95
- # * *data* - data to append to existing
96
- # * *options* - hash to merge with existing options
97
- # ====== Examples
98
- # Updating dataset with Math formula or filename given:
93
+ #
94
+ # @param data [#to_gnuplot_points] data to append to existing
95
+ # @param options [Hash] new options to merge with existing options
96
+ # @return self if dataset corresponds to math formula or file
97
+ # (filename or temporary file if datablock)
98
+ # @return [Dataset] new dataset if data is stored in 'in-memory' Datablock
99
+ # @example Updating dataset with Math formula or filename given:
99
100
# dataset = Dataset.new('file.data')
100
101
# dataset.update(data: 'asd')
101
102
# #=> nothing updated
102
103
# dataset.update(data: 'asd', title: 'File')
103
104
# #=> Dataset.new('file.data', title: 'File')
104
- # Updating dataset with data stored in Datablock:
105
+ # @example Updating dataset with data stored in Datablock (in-memory) :
105
106
# in_memory_points = Dataset.new(points, title: 'Old one')
106
107
# in_memory_points.update(data: some_update, title: 'Updated')
107
108
# #=> Dataset.new(points + some_update, title: 'Updated')
109
+ # @example Updating dataset with data stored in Datablock (in-file):
108
110
# temp_file_points = Dataset.new(points, title: 'Old one', file: true)
109
111
# temp_file_points.update(data: some_update)
110
112
# #=> data updated but no new dataset created
@@ -123,14 +125,44 @@ def update(data = nil, **options)
123
125
end
124
126
end
125
127
128
+ ##
129
+ # Update Dataset with new data and options.
130
+ #
131
+ # Given data is appended to existing.
132
+ # Data is updated only if Dataset stores it in Datablock.
133
+ # Method does nothing if no options given and data isn't stored
134
+ # in in-memory Datablock.
135
+ #
136
+ # @param data [#to_gnuplot_points] data to append to existing
137
+ # @param options [Hash] new options to merge with existing options
138
+ # @return self
139
+ # @example Updating dataset with Math formula or filename given:
140
+ # dataset = Dataset.new('file.data')
141
+ # dataset.update!(data: 'asd')
142
+ # #=> nothing updated
143
+ # dataset.update!(data: 'asd', title: 'File')
144
+ # dataset.title
145
+ # #=> 'File' # data isn't updated
146
+ # @example Updating dataset with data stored in Datablock (in-memory):
147
+ # in_memory_points = Dataset.new(points, title: 'Old one')
148
+ # in_memory_points.update!(data: some_update, title: 'Updated')
149
+ # in_memory_points.data
150
+ # #=> points + some_update
151
+ # in_memory_points.title
152
+ # #=> 'Updated'
153
+ # @example Updating dataset with data stored in Datablock (in-file):
154
+ # temp_file_points = Dataset.new(points, title: 'Old one', file: true)
155
+ # temp_file_points.update!(data: some_update)
156
+ # #=> data updated but no new dataset created
157
+ # temp_file_points.update!(data: some_update, title: 'Updated')
158
+ # #=> data and options updated
126
159
def update! ( data = nil , **options )
127
160
@data . update! ( data ) if data
128
161
options! ( options )
129
162
self
130
163
end
131
164
132
165
##
133
- # ====== Overview
134
166
# Own implementation of #clone. Creates new Dataset if
135
167
# data stored in datablock and calls super otherwise.
136
168
def clone
@@ -142,11 +174,13 @@ def clone
142
174
end
143
175
144
176
##
145
- # ====== Overview
146
- # Creates new Plot object with only one Dataset given - self.
177
+ # Create new Plot object with only one Dataset given - self.
147
178
# Calls #plot on created Plot. All arguments given to this #plot
148
179
# will be sent to Plot#plot instead.
149
- # ====== Example
180
+ # @param args sequence of arguments all of which will be passed to Plot#plot,
181
+ # see docs there
182
+ # @return [Plot] new Plot object with only one Dataset - self
183
+ # @example
150
184
# sin = Dataset.new('sin(x)')
151
185
# sin.plot(term: [qt, size: [300, 300]])
152
186
# #=> shows qt window 300x300 with sin(x)
@@ -159,13 +193,13 @@ def plot(*args)
159
193
private
160
194
161
195
##
162
- # ====== Overview
163
- # Creates new dataset with existing options merged with
196
+ # Create new dataset with existing options merged with
164
197
# the given ones. Does nothing if no options given.
165
- # ====== Parameters
166
- # * *options* - hash to merge with existing options
167
- # ====== Examples
168
- # Updating dataset with Math formula or filename given:
198
+ #
199
+ # @param options [Hash] new options to merge with existing options
200
+ # @return [Dataset] self if options empty
201
+ # @return [Dataset] new Dataset with updated options otherwise
202
+ # @example Updating dataset with Math formula or filename given:
169
203
# dataset = Dataset.new('file.data')
170
204
# dataset.update_options(title: 'File')
171
205
# #=> Dataset.new('file.data', title: 'File')
@@ -178,32 +212,37 @@ def update_options(**options)
178
212
end
179
213
180
214
##
181
- # ====== Overview
182
215
# Create string from own options
216
+ # @return [String] options converted to Gnuplot format
183
217
def options_to_string
184
218
options . sort_by { |key , _ | OPTION_ORDER . find_index ( key . to_s ) || 999 }
185
219
. map { |key , value | OptionHandling . option_to_string ( key , value ) }
186
220
. join ( ' ' )
187
221
end
188
222
189
223
##
190
- # Needed by OptionHandling to create new object when
191
- # options are changed.
224
+ # Needed by OptionHandling to create new object when options are changed.
192
225
def new_with_options ( options )
193
226
self . class . new ( @data , options )
194
227
end
195
228
229
+ ##
230
+ # Initialize Dataset from given String
196
231
def init_string ( data , options )
197
232
@type , @data = File . exist? ( data ) ? [ :datafile , "'#{ data } '" ] : [ :math_function , data . clone ]
198
233
@options = Hamster . hash ( options )
199
234
end
200
235
236
+ ##
237
+ # Initialize Dataset from given Datablock
201
238
def init_dblock ( data , options )
202
239
@type = :datablock
203
240
@data = data . clone
204
241
@options = Hamster . hash ( options )
205
242
end
206
243
244
+ ##
245
+ # Create new value for 'using' option based on column count
207
246
def get_daru_columns ( data , cnt )
208
247
new_opt = ( 2 ..cnt ) . to_a . join ( ':' )
209
248
if data . index [ 0 ] . is_a? ( DateTime ) || data . index [ 0 ] . is_a? ( Numeric )
@@ -213,6 +252,8 @@ def get_daru_columns(data, cnt)
213
252
end
214
253
end
215
254
255
+ ##
256
+ # Initialize Dataset from given Daru::DataFrame
216
257
def init_daru_frame ( data , options )
217
258
options [ :title ] ||= data . name
218
259
if options [ :using ]
@@ -229,12 +270,16 @@ def init_daru_frame(data, options)
229
270
init_default ( data , options )
230
271
end
231
272
273
+ ##
274
+ # Initialize Dataset from given Daru::Vector
232
275
def init_daru_vector ( data , options )
233
276
options [ :using ] ||= get_daru_columns ( data , 2 )
234
277
options [ :title ] ||= data . name
235
278
init_default ( data , options )
236
279
end
237
280
281
+ ##
282
+ # Initialize Dataset from given data with #to_gnuplot_points method
238
283
def init_default ( data , file : false , **options )
239
284
@type = :datablock
240
285
@data = Datablock . new ( data , file )
0 commit comments