def gen_neighborhood(data, row, col):
    """
    Generate 3x3 neighborhood of selected pixel
    If near the edge generate the truncated neighborhood
    to avoid index out-of-bounds errors
    :param data: 2d numpy array
    :param row: integer row index
    :param col: integer col index
    :return data: sub-slice of original data array
    """
    min_row = 0
    min_col = 0
    max_row = data.shape[0] - 1
    max_col = data.shape[1] - 1
    if min_row < row < max_row and min_col < col < max_col:
        return data[row - 1:row + 2, col - 1:col + 2]
    elif (row, col) == (min_row, min_col):
        return data[row:row + 2, col:col + 2]
    elif (row, col) == (min_row, max_col):
        return data[row:row + 2, col - 1:col + 1]
    elif (row, col) == (max_row, max_col):
        return data[row - 1:row + 1, col - 1:col + 1]
    elif (row, col) == (max_row, min_col):
        return data[row - 1:row + 1, col:col + 2]
    elif row == min_row:
        return data[row:row + 2, col - 1:col + 2]
    elif col == max_col:
        return data[row - 1:row + 2, col - 1:col + 1]
    elif row == max_row:
        return data[row - 1:row + 1, col - 1:col + 2]
    else:
        return data[row - 1:row + 2, col:col + 2]
