def test_validation2():
    h = '8928308280fffff'

    with pytest.raises(H3ResolutionError):
        h3.h3_to_children(h, 17)

    assert not h3.h3_indexes_are_neighbors(h, h)
def test_validation():
    h = '8a28308280fffff'  # invalid hex

    with pytest.raises(H3CellError):
        h3.h3_get_base_cell(h)

    with pytest.raises(H3CellError):
        h3.h3_get_resolution(h)

    with pytest.raises(H3CellError):
        h3.h3_to_parent(h, 9)

    with pytest.raises(H3CellError):
        h3.h3_distance(h, h)

    with pytest.raises(H3CellError):
        h3.k_ring(h, 1)

    with pytest.raises(H3CellError):
        h3.hex_ring(h, 1)

    with pytest.raises(H3CellError):
        h3.h3_to_children(h, 11)

    with pytest.raises(H3CellError):
        h3.compact({h})

    with pytest.raises(H3CellError):
        h3.uncompact({h}, 10)
def test_children():
    expected = {
        '8a28308280c7fff', '8a28308280cffff', '8a28308280d7fff',
        '8a28308280dffff', '8a28308280e7fff', '8a28308280effff',
        '8a28308280f7fff'
    }

    out = h3.h3_to_children('8928308280fffff', 10)

    assert out == expected

    expected = set()
    out = h3.h3_to_children('8928308280fffff', 8)

    assert out == expected
Example #4
0
def test_children():
    h = '8928308280fffff'

    # one above should raise an exception
    with pytest.raises(H3ResolutionError):
        h3.h3_to_children(h, 8)

    # same resolution is set of just cell itself
    out = h3.h3_to_children(h, 9)
    assert out == {h}

    # one below should give children
    expected = {
        '8a28308280c7fff',
        '8a28308280cffff',
        '8a28308280d7fff',
        '8a28308280dffff',
        '8a28308280e7fff',
        '8a28308280effff',
        '8a28308280f7fff'
    }
    out = h3.h3_to_children(h, 10)
    assert out == expected

    # finest resolution cell should return error for children
    h = '8f04ccb2c45e225'
    with pytest.raises(H3ResolutionError):
        h3.h3_to_children(h)
    def get_reward_scale(self,
                         hex_densities,
                         target_hex_unclipped,
                         whitelist_hexs,
                         normalize=False):
        """

        :param hex_densities: dict of densiteis of each occupied hex at all levels
        :param whitelist_hexs: dict of densities of whitelisted hexs
        :param target_hex_unclipped: dict of res R hex as keys and raw count of interactive hexs as values
            this could be regenerated pretty easily if desired (O(|hotspots|)) if is_interactive is O(1) and fast
        :return:
        """
        reward_scales = dict()
        whitelist_density = 0
        for whex in whitelist_hexs:
            whitelist_density += hex_densities.get(whex, 0)
        for h in self.hotspots:

            # initialize scale initially set to clipped/unclipped count for target res
            hspot_hex = h3.h3_to_parent(h['location'], self.chain_vars['R'])
            scale = hex_densities[hspot_hex] / target_hex_unclipped[
                h3.h3_to_parent(h['location'], self.chain_vars['R'])]

            for parent_res in range(self.chain_vars['R'] - 1, -1, -1):
                if hspot_hex in whitelist_hexs:
                    break
                parent = h3.h3_to_parent(h['location'], parent_res)
                children_sum = 0

                for child in h3.h3_to_children(parent, parent_res + 1):
                    children_sum += hex_densities.get(child, 0)
                # multiply scale by ratio of clipped values
                scale *= hex_densities[parent] / children_sum
                hspot_hex = parent

            # if we stopped an arent at a whitelisted hex, this hex gets 0 rewards
            if hspot_hex not in whitelist_hexs:
                scale = 0

            reward_scales[h['address']] = scale

        if normalize:
            # will set mean of all scales to 1 for ease of understanding
            scale_sum = 0
            for v in reward_scales.values():
                scale_sum += v
            scale_avg = scale_sum / len(reward_scales)
            for k in reward_scales.keys():
                reward_scales[k] /= scale_avg

        return reward_scales
Example #6
0
 def children(self, res=None):
     """Return the children of a given node with given res."""
     if res is None:
         res = self.res + 1
     return h3.h3_to_children(self.hex_id, res)
Example #7
0
#!/usr/bin/env python3

import h3

base_cells = h3.get_res0_indexes()

all_cells = []
for base_cell in base_cells:
    res5_cells = h3.h3_to_children(base_cell, 4)
    all_cells += res5_cells

with open("h3_4_index.txt", "w") as fd:
    for cell in all_cells:
        fd.write(f"{cell}\n")
Example #8
0
def test_h3_to_children():
    h = '8828308281fffff'
    children = h3.h3_to_children(h, 9)

    assert len(children) == 7