예제 #1
0
파일: lpc.py 프로젝트: epp-warwick/lpcm
  def __init__(self, start_points_generator = lpcRandomStartPoints(), **params):
    '''
    Arguments
    ---------
    start_points_generator: a type callable with a single argument, x0, also implements the function setScaleParameters(h) 
    (see doc for function self.resetScaleParameters)

    h : 1-dim numpy.array, length m or float, bandwidth. May be either specified as a single number, then
        the same bandwidth is used in all dimensions, or as an
        m-dimensional bandwidth vector (where m is the dimension of feature points). 
        The default setting is 10 percent of the range in each direction. If scaled =TRUE
        then, if specified, the bandwidth has to be specified in fractions of the
        data range, e.g. h = [0.2, 0.1, 0.3], rather than absolute values.

    t0 : float, scalar step length. Default setting is 't0 = h' if 'h' is a
        scalar, and 't0 = mean(h)' if 'h' is a vector.

    depth (NOT USED): int, maximum depth of branches,  restricted to the
        values 1,2 or 3 (The original LPC branch has depth 1.  If,
        along this curve, a point features a high second local PC,
        this launches a new starting point, and the resulting branch
        has depth 2.  If, along this branch, a point features a high
        second local PC, this launches a new starting point, and the
        resulting branch has depth 3. )
 
    way: "one": go only in direction of the first local eigenvector,
        "back": go only in opposite direction, "two": go from
        starting point in both directions.

    scaled: bool, if True, scales each variable by dividing through its range
      
    pen: power used for angle penalization. If set to 0, the
        angle penalization is switched off.
          
    it : maximum number of iterations on either side of the starting
        point within each branch.
    
    cross:  If True, curves are stopped when they come
        too close to an existing branch. Used in the self-coverage
        function.
    
    boundary: This boundary correction [2] reduces the bandwidth adaptively
        once the relative difference of parameter values between two
        centers of mass falls below the given threshold. This measure
        delays convergence and enables the curve to proceed further
        into the end points. If set to 0, this boundary correction is
        switched off.
  
    convergence_at: this forces the curve to stop if the relative
        difference of parameter values between two centers of mass
        falls below the given threshold.  If set to 0, then the curve
        will always stop after exactly "iter" iterations.
  
    mult: integer which enforces a fixed number of starting
        points.  If the number given here is larger than the number
        of starting points provided at "x0", then the missing points
        will be set at random (For example, if d=2, mult=3, and
        x0=c(58.5, 17.8, 80,20), then one gets the starting points
        (58.5, 17.8), (80,20), and a randomly chosen third one.
        Another example for such a situation is "x0=NULL" with
        "mult=1", in which one random starting point is chosen). If
        the number given here is smaller the number of starting
        points provided at x0, then only the first "mult" starting
        points will be used.
    
    pruning_thresh (NOT USED) : float, used to remove non-dense, depth > 1 branches 
    
    rho0: float, steers the birth process of higher-depth starting points
        by acting as a threshold for definition of high_rho_pts. 
        Usually, between 0.3 and 0.4
                
    gapsize (NOT USED): float, sets scaling of t0 which is applied when 
        starting depth > 1 branches
    
    binary: If True, weights supplied to self.lpc are ignored and each point has equal weight in calculation of the lpc curve. (Default: True)
    '''
    super(LPCImpl, self).__init__()
    self._lpcParameters = { 'h': None, 
                            't0': None,
                            'way': 'two',
                            'scaled': True,
                            'pen': 2,
                            'depth': 1,
                            'it': 100,
                            'cross': True,
                            'boundary': 0.005,
                            'convergence_at': 1e-6,
                            'mult': None, #set this to None to allow exactly the number of local density modes to be returned from MeanShift  
                            'pruning_thresh': 0.0,
                            'rho0': 0.4,
                            'gapsize': 1.5,
                            'binary': True 
                          }
    self._prm_list = [self._lpcParameters] 
    self.user_prm = None #extension of parameter set disallowed
    self._type_check.update({ 'h': lambda x: (x == None) or LPCImpl._positivityCheck(x) or (isinstance(x, list) and all(map(LPCImpl._positivityCheck, x)) ) , 
                              't0': lambda x: (x == None) or LPCImpl._positivityCheck,
                              'way': lambda x: x in ('one', 'two', 'back'),
                              'scaled': (bool,),
                              'pen': lambda x: (x == 0) or LPCImpl._positivityCheck,
                              'depth': lambda x: x in (1,2,3),
                              'it': lambda x: isinstance(x, int) and x > 9,
                              'cross': (bool,),
                              'convergence_at': LPCImpl._positivityCheck,
                              'boundary':  lambda x: LPCImpl._positivityCheck(x), #TODO - no assertion boundary > convergence.at (add to _update)
                              'mult': lambda x: (x == None) or (isinstance(x, int) and x > 0),
                              'pruning_thresh': LPCImpl._positivityCheck,
                              'rho0': LPCImpl._positivityCheck,
                              'gapsize': LPCImpl._positivityCheck,
                              'binary': (bool,)
                            })
    self.set(**params)
    self.Xi = None
    self.x0 = None #set equal to the return value of _selectStartPoints
    self._dataRange = None
    self._weights = None
    self._curve = None
    if not iscallable(start_points_generator):
      raise TypeError, 'Start points generator must be callable'
    self._startPointsGenerator = start_points_generator
예제 #2
0
 def testlpcRandomStartPoints1(self):
   print 'Generate 1 random start point from line, with no explicitly defined start points'
   lpcRSP = lpcRandomStartPoints()
   sp = lpcRSP(self._line, 1)
   print sp