Ejemplo n.º 1
0
def _check_polygons(polygon_objects_grid_coords, num_panel_rows,
                    num_panel_columns, panel_row_by_polygon,
                    panel_column_by_polygon):
    """Error-checks list of polygons.

    :param polygon_objects_grid_coords: See doc for
        `polygons_from_pixel_to_grid_coords`.
    :param num_panel_rows: Same.
    :param num_panel_columns: Same.
    :param panel_row_by_polygon: Same.
    :param panel_column_by_polygon: Same.
    """

    error_checking.assert_is_integer(num_panel_rows)
    error_checking.assert_is_greater(num_panel_rows, 0)
    error_checking.assert_is_integer(num_panel_columns)
    error_checking.assert_is_greater(num_panel_columns, 0)

    num_polygons = len(polygon_objects_grid_coords)
    if num_polygons == 0:
        return

    error_checking.assert_is_numpy_array(numpy.array(
        polygon_objects_grid_coords, dtype=object),
                                         num_dimensions=1)

    these_expected_dim = numpy.array([num_polygons], dtype=int)

    error_checking.assert_is_integer_numpy_array(panel_row_by_polygon)
    error_checking.assert_is_numpy_array(panel_row_by_polygon,
                                         exact_dimensions=these_expected_dim)
    error_checking.assert_is_geq_numpy_array(panel_row_by_polygon, 0)
    error_checking.assert_is_less_than_numpy_array(panel_row_by_polygon,
                                                   num_panel_rows)

    error_checking.assert_is_integer_numpy_array(panel_column_by_polygon)
    error_checking.assert_is_numpy_array(panel_column_by_polygon,
                                         exact_dimensions=these_expected_dim)
    error_checking.assert_is_geq_numpy_array(panel_column_by_polygon, 0)
    error_checking.assert_is_less_than_numpy_array(panel_column_by_polygon,
                                                   num_panel_columns)
Ejemplo n.º 2
0
def pixel_columns_to_grid_columns(pixel_column_by_vertex, num_pixel_columns,
                                  num_panel_columns, num_grid_columns,
                                  assert_same_panel):
    """Converts pixel columns to grid columns.

    V = number of vertices in object

    :param pixel_column_by_vertex: length-V numpy array with column coordinates
        of vertices in pixel space.
    :param num_pixel_columns: Total number of pixel columns in image.
    :param num_panel_columns: Total number of panel columns in image.
    :param num_grid_columns: Total number of columns in grid (one grid per
        panel).
    :param assert_same_panel: Boolean flag.  If True, all vertices must be in
        the same panel.
    :return: grid_column_by_vertex: length-V numpy array with column coordinates
        (floats) of vertices in grid space.
    :return: panel_column_by_vertex: length-V numpy array with column
        coordinates (integers) of vertices in panel space.
    """

    error_checking.assert_is_integer(num_pixel_columns)
    error_checking.assert_is_greater(num_pixel_columns, 0)
    error_checking.assert_is_integer(num_panel_columns)
    error_checking.assert_is_greater(num_panel_columns, 0)
    error_checking.assert_is_integer(num_grid_columns)
    error_checking.assert_is_greater(num_grid_columns, 0)
    error_checking.assert_is_boolean(assert_same_panel)

    error_checking.assert_is_numpy_array(pixel_column_by_vertex,
                                         num_dimensions=1)

    pixel_column_by_vertex += 0.5
    error_checking.assert_is_geq_numpy_array(pixel_column_by_vertex, 0.)
    error_checking.assert_is_leq_numpy_array(pixel_column_by_vertex,
                                             num_pixel_columns)

    panel_column_to_first_px_column = {}
    for j in range(num_panel_columns):
        panel_column_to_first_px_column[j] = (j * float(num_pixel_columns) /
                                              num_panel_columns)

    panel_column_by_vertex = numpy.floor(pixel_column_by_vertex *
                                         float(num_panel_columns) /
                                         num_pixel_columns).astype(int)

    panel_column_by_vertex[panel_column_by_vertex ==
                           num_panel_columns] = num_panel_columns - 1

    if assert_same_panel and len(numpy.unique(panel_column_by_vertex)) > 1:
        error_string = (
            'Object is in multiple panels.  Panel columns listed below.\n{0:s}'
        ).format(str(panel_column_by_vertex))

        raise ValueError(error_string)

    num_vertices = len(pixel_column_by_vertex)
    for i in range(num_vertices):
        pixel_column_by_vertex[i] = (
            pixel_column_by_vertex[i] -
            panel_column_to_first_px_column[panel_column_by_vertex[i]])

    grid_column_by_vertex = -0.5 + (pixel_column_by_vertex * float(
        num_grid_columns * num_panel_columns) / num_pixel_columns)

    return grid_column_by_vertex, panel_column_by_vertex
Ejemplo n.º 3
0
def polygons_to_mask(polygon_objects_grid_coords, num_grid_rows,
                     num_grid_columns, num_panel_rows, num_panel_columns,
                     panel_row_by_polygon, panel_column_by_polygon):
    """Converts list of polygons to one binary mask for each panel.

    M = number of rows in grid
    N = number of columns in grid
    J = number of panel rows in image
    K = number of panel columns in image

    :param polygon_objects_grid_coords: See doc for
        `polygons_from_pixel_to_grid_coords`.
    :param num_grid_rows: Same.
    :param num_grid_columns: Same.
    :param num_panel_rows: Same.
    :param num_panel_columns: Same.
    :param panel_row_by_polygon: Same.
    :param panel_column_by_polygon: Same.
    :return: mask_matrix: J-by-K-by-M-by-N numpy array of Boolean flags.  If
        mask_matrix[j, k, m, n] == True, grid point [m, n] in panel [j, k] is
        in/on at least one of the polygons.
    """

    error_checking.assert_is_integer(num_grid_rows)
    error_checking.assert_is_greater(num_grid_rows, 0)
    error_checking.assert_is_integer(num_grid_columns)
    error_checking.assert_is_greater(num_grid_columns, 0)

    _check_polygons(polygon_objects_grid_coords=polygon_objects_grid_coords,
                    num_panel_rows=num_panel_rows,
                    num_panel_columns=num_panel_columns,
                    panel_row_by_polygon=panel_row_by_polygon,
                    panel_column_by_polygon=panel_column_by_polygon)

    mask_matrix = numpy.full(
        (num_panel_rows, num_panel_columns, num_grid_rows, num_grid_columns),
        False,
        dtype=bool)

    num_polygons = len(polygon_objects_grid_coords)
    if num_polygons == 0:
        return mask_matrix

    panel_coord_matrix = numpy.hstack(
        (numpy.reshape(panel_row_by_polygon, (num_polygons, 1)),
         numpy.reshape(panel_column_by_polygon, (num_polygons, 1))))

    panel_coord_matrix = numpy.unique(panel_coord_matrix.astype(int), axis=0)

    for i in range(panel_coord_matrix.shape[0]):
        this_panel_row = panel_coord_matrix[i, 0]
        this_panel_column = panel_coord_matrix[i, 1]

        these_polygon_indices = numpy.where(
            numpy.logical_and(panel_row_by_polygon == this_panel_row,
                              panel_column_by_polygon == this_panel_column))[0]

        these_polygon_objects = [
            polygon_objects_grid_coords[k] for k in these_polygon_indices
        ]

        mask_matrix[this_panel_row, this_panel_column,
                    ...] = (_polygons_to_mask_one_panel(
                        polygon_objects_grid_coords=these_polygon_objects,
                        num_grid_rows=num_grid_rows,
                        num_grid_columns=num_grid_columns))

    return mask_matrix
    def test_assert_is_positive_nan_banned(self):
        """Checks assert_is_greater; input_variable = NaN, allow_nan = False."""

        with self.assertRaises(ValueError):
            error_checking.assert_is_greater(numpy.nan, 0, allow_nan=False)
    def test_assert_is_positive_nan_allowed(self):
        """Checks assert_is_greater; input_variable = NaN, allow_nan = True."""

        error_checking.assert_is_greater(numpy.nan, 0, allow_nan=True)
    def test_assert_is_positive_true(self):
        """Checks assert_is_greater with base_value = 0, input_variable > 0."""

        error_checking.assert_is_greater(SINGLE_POSITIVE, 0)
    def test_assert_is_positive_zero(self):
        """Checks assert_is_greater with base_value = 0, input_variable = 0."""

        with self.assertRaises(ValueError):
            error_checking.assert_is_greater(SINGLE_ZERO, 0)
    def test_assert_is_positive_negative(self):
        """Checks assert_is_greater with base_value = 0, input_variable < 0."""

        with self.assertRaises(ValueError):
            error_checking.assert_is_greater(SINGLE_NEGATIVE, 0)