Ejemplo n.º 1
0
    def __init__(self, band1, band2, expand = False):
        """
        @param band1    First band (numpy masked array)
        @param band2    Second band (numpy masked array)
        @param expand   If the param is True, use union of categories of the bands and compute NxN crosstable
        """
        QObject.__init__(self)

        if not sizes_equal(band1, band2):
            raise CrossTabError('Sizes of rasters are not equal!')

        band1, band2 = masks_identity(band1, band2, dtype=np.uint8)

        self.X = np.ma.compressed(band1).flatten()
        self.Y = np.ma.compressed(band2).flatten()

        # Compute gradations of the bands
        self.graduation_x = get_gradations(self.X)
        self.graduation_y = get_gradations(self.Y)
        if expand:
            self.graduation_x = list(set(self.graduation_x + self.graduation_y))
            self.graduation_y = self.graduation_x

        rows, cols = len(self.graduation_x), len(self.graduation_y)
        self.shape = (rows, cols)

        self._T = None       # Crosstable
        self.n  = None       # Count of elements in the crosstable
Ejemplo n.º 2
0
def woe(factor, sites, unit_cell=1):
    '''Weight of evidence method (multiclass form).

    @param factor     Multiclass pattern array used for prediction of point objects (sites).
    @param sites      Array layer consisting of the locations at which the point objects are known to occur.
    @param unit_cell  Method parameter, pixelsize of resampled rasters.

    @return masked array  Array of total weights of each factor.
    '''

    # Get list of categories from the factor raster
    categories = get_gradations(factor.compressed())

    # Try to binarize sites:
    sCategories = get_gradations(sites.compressed())
    if len(sCategories) != 2:
        raise WoeError('Site raster must be binary!')
    sites = binaryzation(sites, [sCategories[1]])

    # List of the weights of evidence:
    # weights[0] is (wPlus, wMinus) for the first category, weights[1] is (wPlus, wMinus) for the second category, ...
    weights = []
    if len(categories) >= 2:
        for cat in categories:
            fct = binaryzation(factor, [cat])
            weights.append(_binary_woe(fct, sites, unit_cell))
    else:
        raise WoeError('Wrong count of categories in the factor raster!')

    wTotalMin = sum([w[1] for w in weights])
    # List of total weights of evidence of the categories:
    # wMap[0] is the total weight of the first category, wMap[1] is the total weight of the second category, ...
    wMap = [w[0] + wTotalMin - w[1] for w in weights]

    # If len(categories) = 2, then [w[0] + wTotalMin - w[1] for w in weights] increases the answer.
    # In this case:
    if len(categories) == 2:
        wMap = [w/2 for w in wMap]

    resultMap =np.zeros(ma.shape(factor))
    for i,cat in enumerate(categories):
        resultMap[factor==cat] = wMap[i]

    resultMap = ma.array(data=resultMap, mask=factor.mask)
    result = {'map': resultMap, 'categories': categories, 'weights': wMap}
    return result
Ejemplo n.º 3
0
 def getBandGradation(self, bandNo):
     '''
     Return list of categories of raster's band
     '''
     try:
         res = self.bandgradation[bandNo]
     except KeyError:
         band = self.getBand(bandNo)
         res = get_gradations(band.compressed())
         self.bandgradation[bandNo] = res
     return res