Changeset 1464:e1a27fb4e3ef in orange-bioinformatics


Ignore:
Timestamp:
07/24/11 23:26:25 (3 years ago)
Author:
markotoplak
Branch:
default
Convert:
097dae9ed22223481595fc8cd5c8225d20dcbc39
Message:

obiDifscale: replaced cumpute_area with a 10x faster version.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • obiDifscale.py

    r1450 r1464  
    131131    return serie_0 
    132132 
    133 def compute_area(serie, tempi): 
    134     """ Function that calculates the area between 2 time series """ 
    135     # if the ith point and the next point have the same sign it computes the 
    136     # area by the integral with the trapezoidal method, else it computes the 
    137     # area of the two triangles found with linear interpolation 
    138     area_pieces = [] 
    139     for i in range(len(serie)-1): 
    140         if sign(serie[i]) == sign(serie[i+1]): 
    141             area_pieces.append(abs(numpy.trapz([serie[i], serie[i+1]], [tempi[i], tempi[i+1]]))) 
    142         else: 
    143             area_triangle_1 = float(serie[i] * (tempi[i+1] - tempi[i])) / \ 
    144                               (serie[i] - serie[i+1]) * abs(serie[i]) / 2 
    145             area_triangle_2 = float(serie[i+1] * (tempi[i] - tempi[i+1])) / \ 
    146                               (serie[i] - serie[i+1]) * abs(serie[i+1]) / 2 
    147             area_pieces.append(area_triangle_1 + area_triangle_2) 
    148  
    149     area = sum(area_pieces); 
    150     return area 
     133def compute_area(vals, t, baseline=0.): 
     134    return sum(a_pair((t[i], vals[i]), (t[i+1], vals[i+1]), baseline) \ 
     135        for i in xrange(len(vals)-1)) 
     136 
     137def a_pair(p1, p2, baseline): 
     138    """Area under the line bounded by a pair of two points (x,y) with 
     139    respect to baseline. Both parts around the diagonal are 
     140    positive.""" 
     141    x1,y1 = p1 
     142    x2,y2 = p2 
     143    x2 = x2-x1 #same start 
     144    x1 = 0.0 
     145    a = y1-baseline 
     146    b = y2-baseline 
     147    if a*b >= 0: #both on one side 
     148        return abs(x2*(b+a)/2.0) 
     149    else: 
     150        xp = -a * x2 / float(y2-y1) 
     151        return (abs(xp * a) + abs((x2-xp)*b)) / 2.0 
    151152 
    152153def uniform_time_scale(attr_set): 
Note: See TracChangeset for help on using the changeset viewer.