File: partition.rb

package info (click to toggle)
ctioga 1.10-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 1,052 kB
  • ctags: 953
  • sloc: ruby: 9,306; sh: 504; makefile: 6
file content (271 lines) | stat: -rw-r--r-- 8,199 bytes parent folder | download | duplicates (2)
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
# partition.rb: segment partitioning functions
# Copyright (c) 2008 by Vincent Fourmond

  
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
  
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details (in the COPYING file).

require 'CTioga/utils'


module CTioga

  Version::register_svn_info('$Revision: 792 $', '$Date: 2008-05-23 00:19:31 +0200 (Fri, 23 May 2008) $')

  # A module for small convenience functions.
  module Utils

    # The following code is stolen from SciYAG/lib/utils.rb,
    # and is used to partition segments in natural-looking
    # subdivisions.

    
    # Our natural way to split decades
    NaturalDistances = Dobjects::Dvector[1, 2, 2.5, 5, 10]

    # Attempts to partition the given segment in at most _nb_
    # segments of equal size. The segments don't necessarily start on
    # the edge of the original segment
    def self.partition_segment(min, max, nb)
      if min > max             
        return partition_segment(max, min, nb)
      elsif min.nan? or max.nan?
        return [0] * nb
      elsif min == max
        return self.partition_segment(min * 0.7, min * 1.3, nb)
      end
      distance = max - min
      min_distance = distance/(nb + 1.0) # Why + 1.0 ? To account
      # for the space that could be left on the side.
      
      # The order of magnitude of the distance:
      order = min_distance.log10.floor
      
      # A distance which is within [1, 10 [ (but the latter is never reached.
      normalized_distance = min_distance * 10**(-order)
      final_distance = NaturalDistances.min_gt(normalized_distance) * 
        10**(order)
      #       puts "Distance: #{distance} in #{nb} : #{normalized_distance} #{final_distance}"
      
      # We're getting closer now: we found the natural distance between
      # ticks.
      
      start = (min/final_distance).ceil * final_distance
      retval = []
      val = start
      while val <= max
        retval << val
        # I use this to avoid potential cumulative addition
        # rounding errors
        val = start + final_distance * retval.size
      end
      return retval

    end

    # Our natural way to split decades - except that all successive
    # element now divide each other.
    NaturalDistancesNonLinear = Dobjects::Dvector[1, 2.5, 5, 10]

    # A class that handles a "natural distance". Create it by giving
    # the initial distance. You can then get its value, lower it,
    # increase it, find the first/last element of an interval that is
    # a multiple if it
    class NaturalDistance

      # Creates the biggest 'natural distance' that is smaller
      # than _distance_
      def initialize(distance)
        @order = distance.log10.floor
        normalized = distance * 10**(-@order)
        @index = 0
        for dist in NaturalDistances
          if dist > normalized
            break
          else
            @index += 1
          end
        end
      end

      # Returns the actual value of the distance
      def value
        return NaturalDistances[@index] * 10**(@order)
      end

      # Goes to the natural distance immediately under the current one
      def decrease
        if @index > 0
          @index -= 1
        else
          @index = NaturalDistances.size - 1
          @order -= 1
        end
      end

      # Goes to the natural distance immediately under the current one
      def increase
        if @index < NaturalDistances.size - 1
          @index += 1
        else
          @index = 0
          @order += 1
        end
      end

      # Find the minimum element inside the given interval
      # that is a multiple of this distance
      def find_minimum(x1, x2)
        x1,x2 = x2, x1 if x1 > x2

        dist = value
        v = (x1/dist).ceil * dist
        if v <= x2
          return v
        else
          return false
        end
      end

      # Returns the value of the next decade (ascending)
      def next_decade(x)
        decade = 10.**(@order + 1)
        if @index == 0          # We stop at 0.5 if the increase is 0.5
          decade *= 0.5
        end
        nb = Float((x/decade).ceil)
        if nb == x/decade
          return (nb + 1)*decade
        else
          return nb * decade
        end
      end

      # Returns the number of elements to #next_decade
      def nb_to_next_decade(x)
        dist = value
        dec = next_decade(x)
        return dec/dist - (x/dist).ceil + 1
      end

      # Returns a list of all the values corresponding to
      # the distance
      def to_next_decade(x)
        next_decade = next_decade(x)
        dist = value
        start = (x/dist).ceil * dist
        retval = []
        while start + retval.size * dist <= next_decade
          retval << start + retval.size * dist
        end
        return retval
      end

    end


    # Attempts to partition the segment image of _min_, _max_
    # by the Proc object _to_ into at most _nb_ elements. The
    # reverse of the transformation, _from_, has to be provided.
    def self.partition_nonlinear(to, from, x1, x2, nb)
      x1, x2 = x2, x1 if x1 > x2
      if x1.nan? or x2.nan?
        return [0] * nb
      elsif x1 == x2            # Nothing to do
        return self.partition_segment(x1 * 0.7, x2 * 1.3, nb)
      end

      xdist = x2 - x1
      xdist_min = xdist/(nb + 1.0) # Why + 1.0 ? To account
      # for the space that could be left on the side.

      y1 = to.call(x1)
      y2 = to.call(x2)
      
      # Make sure y1 < y2
      y1, y2 = y2, y1 if y1 > y2

      # We first need to check if the linear partitioning of
      # the target segment could be enough:
      
      candidate = self.partition_segment(y1, y2, nb)
      candidate_real = candidate.map(&from)

      # We inspect the segment: if one of the length deviates from the
      # average expected by more than 25%, we drop it

      length = []
      p candidate_real, xdist_min
      0.upto(candidate.size - 2) do |i|
        length << (candidate_real[i+1] - candidate_real[i]).abs/(xdist_min)
      end
      p length
      # If everything stays within 25% off, we keep that
      if length.min > 0.75 and length.max < 1.7
        return candidate
      end
      

      # We start with a geometric measure of the distance, that
      # will most likely scale better:
      ydist = y1 * (y2/y1).abs ** (1/(nb + 1.0))

      cur_dist = NaturalDistance.new(ydist)
      
      retval = []
      
      cur_y = y1
      # This flag is necessary to avoid infinite loops
      last_was_decrease = false
      
      distance_unchanged = 0
      last_real_distance = false
      while cur_y < y2
        candidates = cur_dist.to_next_decade(cur_y)
        # We now evaluate the distance in real
        real_distance = (from.call(cur_y) - from.call(candidates.last)).abs/
          candidates.size
        if last_real_distance && (real_distance == last_real_distance)
          distance_unchanged += 1
        else
          distance_unchanged = 0
        end
#         p [:cur_y=, cur_y, :y2=, y2, :real_distance, real_distance, 
#            :distance=, cur_dist, :xdist_min, xdist_min, 
#            :candidates=, *candidates]

        if (real_distance > 1.25 * xdist_min) && 
            (distance_unchanged < 3)
          cur_dist.decrease
          last_was_decrease = true
        elsif real_distance < 0.75 * xdist_min && 
            !last_was_decrease && (distance_unchanged < 3) && 
            candidates.last <= 10 * y2
          cur_dist.increase
          last_was_decrease = false
        else
          retval += candidates
          cur_y = candidates.last
          last_was_decrease = false
        end
        last_real_distance = real_distance
      end

      # We need to select them so
      return retval.select do |y|
        y >= y1 and y <= y2
      end

    end


  end

end