Esempio n. 1
0
    def test_most_common_class_exclude(self):
        a = np.array([[10, 20, 30] * 6] * 6, dtype=np.uint8)

        expected = (10, 36)
        actual = most_common_class(a, exclude=(20, 30))

        self.assertEqual(expected, actual)
Esempio n. 2
0
    def test_most_common_class_with_valid(self):
        *_, gl30_10 = random_test_data()

        expected = 50
        actual = most_common_class(gl30_10)[0]

        self.assertEqual(expected, actual)
Esempio n. 3
0
    def test_most_common_class_with_fallback(self):
        a = np.array([[20, 20, 10] * 5] * 5, dtype=np.uint8)

        expected = 10
        actual = most_common_class(a)[0]

        self.assertEqual(expected, actual)
Esempio n. 4
0
    def test_most_common_class_with_zeros(self):
        a = np.zeros((10, 10), dtype=np.uint8)

        expected = None
        actual = most_common_class(a)

        self.assertEqual(expected, actual)
Esempio n. 5
0
def reclassify(driver, clustering=SETTINGS['clustering'],
               reject=SETTINGS['reject'], side_length=SETTINGS['buffer'], res=(1, 1)):
    """Reclassify pixels in proximate deforestation stratum.

    Approach: Cluster pixels with values ``clustering``; create square sized buffer around the cluster center;
    count most frequent class within the buffer by exclusion of ``reject``; reassign cluster to most frequent class

    Args:
        driver (ndarray): Proximate deforestation driver stratum.
        clustering (list(int): Values to cluster.
        reject (list(int): Values to reject for reclassification.
        side_length (int): Edge length of the buffer.
        res(int or tuple(int, int)): Cell size.

    Returns:
        ndarray: The reclassified stratum.
    """

    mask = np.isin(driver, clustering)

    clusters = []
    for cluster, _ in shapes(driver, mask=mask):
        polygon = Polygon(cluster['coordinates'][0])
        point = polygon.centroid
        center = int(point.y), int(point.x)

        LOGGER.debug('Cluster centroid at (%s, %s)', int(point.x), int(point.y))

        buffer = extract_square(driver, center, side_length, res)

        LOGGER.debug('Buffer size (%s, %s)', buffer.shape[0], buffer.shape[1])

        mc = most_common_class(buffer, reject)

        if mc:
            cls, count = mc
            clusters.append((cluster, cls))

    if clusters:
        return rasterize(clusters, out_shape=driver.shape, dtype=driver.dtype)

    return np.zeros(shape=driver.shape, dtype=driver.dtype)