~ubuntu-branches/ubuntu/oneiric/ctioga2/oneiric

« back to all changes in this revision

Viewing changes to lib/ctioga2/data/filters.rb

  • Committer: Bazaar Package Importer
  • Author(s): Vincent Fourmond
  • Date: 2011-01-24 21:36:06 UTC
  • Revision ID: james.westby@ubuntu.com-20110124213606-9ettx0ugl83z0bzp
Tags: upstream-0.1
ImportĀ upstreamĀ versionĀ 0.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# stack.rb: the data stack
 
2
# copyright (c) 2009 by Vincent Fourmond
 
3
  
 
4
# This program is free software; you can redistribute it and/or modify
 
5
# it under the terms of the GNU General Public License as published by
 
6
# the Free Software Foundation; either version 2 of the License, or
 
7
# (at your option) any later version.
 
8
  
 
9
# This program is distributed in the hope that it will be useful,
 
10
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
# GNU General Public License for more details (in the COPYING file).
 
13
 
 
14
require 'ctioga2/utils'
 
15
require 'ctioga2/data/datacolumn'
 
16
require 'ctioga2/data/backends/backends'
 
17
require 'ctioga2/data/backends/factory'
 
18
 
 
19
# This module contains all the classes used by ctioga
 
20
module CTioga2
 
21
 
 
22
  Version::register_svn_info('$Revision: 194 $', '$Date: 2010-11-22 10:26:54 +0100 (Mon, 22 Nov 2010) $')
 
23
 
 
24
 
 
25
  module Data
 
26
 
 
27
    # A series of commands that can be used as "filters", as they act
 
28
    # upon the last Dataset pushed unto the stack.
 
29
    module Filters
 
30
 
 
31
      FiltersGroup =  
 
32
        CmdGroup.new('filter', "Filters",
 
33
                     "The commands in this group act upon the last 
 
34
dataset pushed unto the data stack: they can be viewed as filters.", 
 
35
                     101)
 
36
      
 
37
      SortOperation = 
 
38
        Cmd.new("sort-last", nil, "--sort-last", 
 
39
                [], {}) do |plotmaker, opts|
 
40
        plotmaker.data_stack.last.sort!
 
41
      end
 
42
      
 
43
      SortOperation.describe("Sorts the last dataset according to X values",
 
44
                             <<EOH, FiltersGroup)
 
45
Sorts the last dataset pushed unto the stack according to X values. Can be
 
46
used as a filter.
 
47
 
 
48
See also {command: sort}.
 
49
EOH
 
50
 
 
51
      SortFilter = 
 
52
        Cmd.new("sort", nil, "--sort", 
 
53
                [], {}) do |plotmaker, opts|
 
54
        plotmaker.data_stack.add_to_dataset_hook('sort-last()')
 
55
      end
 
56
      
 
57
      SortFilter.describe("Systematically sort subsequent datasets",
 
58
                          <<EOH, FiltersGroup)
 
59
Install the {command: sort-last} command as a dataset hook (see {command:
 
60
dataset-hook}): all subsequent datasets will be sorted according to
 
61
their X values.
 
62
EOH
 
63
 
 
64
      TrimOperation = 
 
65
        Cmd.new("trim-last", nil, "--trim-last", 
 
66
                [CmdArg.new('integer')], {}) do |plotmaker, number, opts|
 
67
        plotmaker.data_stack.last.trim!(number)
 
68
      end
 
69
      
 
70
      TrimOperation.describe("Only keeps every n points in the last dataset",
 
71
                             <<EOH, FiltersGroup)
 
72
Only keeps one every ? data point on the last dataset pushed unto the
 
73
data stack. Useful when data have too many points to avoid creating
 
74
heavy PDF files that take ages to display with no additional benefits.
 
75
 
 
76
This operation is very crude and does not average data.
 
77
 
 
78
See also {command: trim}.
 
79
EOH
 
80
 
 
81
      TrimFilter = 
 
82
        Cmd.new("trim", nil, "--trim", 
 
83
                [CmdArg.new('integer')], {}) do |plotmaker, number, opts|
 
84
        ## @todo There should be a way to add commands in a type-safe
 
85
        ## way, without having to convert to string first.
 
86
        plotmaker.data_stack.add_to_dataset_hook("trim-last(#{number})")
 
87
      end
 
88
      
 
89
      TrimFilter.describe("Systematically trim subsequent datasets",
 
90
                          <<EOH, FiltersGroup)
 
91
Install the {command: trim-last} command as a dataset hook (see {command:
 
92
dataset-hook}): all subsequent datasets will be trimmed to keep only
 
93
every n point.
 
94
EOH
 
95
 
 
96
 
 
97
      CherryPickOperation = 
 
98
        Cmd.new("cherry-pick-last", nil, "--cherry-pick-last", 
 
99
                [CmdArg.new('text')], {}) do |plotmaker, formula|
 
100
        plotmaker.data_stack.last.select_formula!(formula)
 
101
      end
 
102
      
 
103
      CherryPickOperation.describe("Removes data from the last dataset for which the formula is false",
 
104
                                   <<EOH, FiltersGroup)
 
105
 
 
106
Removes the data from the last dataset in the data stack for which the
 
107
formula returns false.
 
108
 
 
109
See also the {command: cherry-pick} command to apply the selection to
 
110
all datasets.
 
111
 
 
112
You might find it much easier to use the /where option of the
 
113
{command: plot} or {command: load} commands.
 
114
EOH
 
115
 
 
116
      CherryPickFilter = 
 
117
        Cmd.new("cherry-pick", nil, "--cherry-pick", 
 
118
                [CmdArg.new('text')], {}) do |plotmaker, formula|
 
119
        plotmaker.data_stack.add_to_dataset_hook("cherry-pick-last(#{formula})")
 
120
      end
 
121
      
 
122
      CherryPickFilter.describe("Systematicallly remove data for which the formula is false",
 
123
                                <<EOH, FiltersGroup)
 
124
Install the {command: cherry-pick-last} command as a dataset hook (see
 
125
{command: dataset-hook}): all points for which the formula returns
 
126
false for subsequent datasets will be removed.
 
127
EOH
 
128
 
 
129
      AverageDupOperation = 
 
130
        Cmd.new("avg-dup-last", nil, "--avg-dup-last", 
 
131
                [], {}) do |plotmaker|
 
132
        plotmaker.data_stack.last.average_duplicates!
 
133
      end
 
134
      
 
135
      AverageDupOperation.describe("Average successive elements with identical X values",
 
136
                                   <<EOH, FiltersGroup)
 
137
Averages successive points with identical X values. This algorithm is
 
138
naive with respect to the min/max values and averages them just as
 
139
well, whereas one might expect something more clever.
 
140
 
 
141
To average over identical X values when they are not successive in the
 
142
dataset, you might want to hand it over to {command: sort-last} first.
 
143
 
 
144
EOH
 
145
 
 
146
      AverageDupFilter = 
 
147
        Cmd.new("avg-dup", nil, "--avg-dup", 
 
148
                [], {}) do |plotmaker, formula|
 
149
        plotmaker.data_stack.add_to_dataset_hook("avg-dup-last()")
 
150
      end
 
151
      
 
152
      AverageDupFilter.describe("Systematicallly average successive elements with identical X values",
 
153
                                <<EOH, FiltersGroup)
 
154
Install the {command: avg-dup-last} command as a dataset hook (see
 
155
{command: dataset-hook}): all datasets acquired after this is on will
 
156
be averaged if they have identical successive values of X.
 
157
EOH
 
158
 
 
159
 
 
160
      SmoothOperation = 
 
161
        Cmd.new("smooth-last", nil, "--smooth-last", 
 
162
                [CmdArg.new('integer')], {}) do |plotmaker, number|
 
163
        plotmaker.data_stack.last.naive_smooth!(number)
 
164
      end
 
165
      
 
166
      SmoothOperation.describe("Smooths data using a gaussian filter",
 
167
                               <<EOH, FiltersGroup)
 
168
Smooth the data using a simple (naive even) gaussian filter. Good for
 
169
producing 'lines to guide the eye'
 
170
EOH
 
171
 
 
172
 
 
173
 
 
174
 
 
175
 
 
176
    end
 
177
  end
 
178
end
 
179