예제 #1
0
def check_vertex_arrays(x_coordinates, y_coordinates, allow_nan=True):
    """Error-checks vertex arrays.

    V = number of vertices

    :param x_coordinates: length-V numpy array of x-coordinates.  May contain
        NaN's, where the first NaN separates the exterior from the first hole
        and the [k]th NaN separates the [k - 1]th hole from the [k]th hole.
        This method assumes that the first unbroken string of real values (not
        NaN) represents the exterior of the polygon.
    :param y_coordinates: Same but for y-coordinates.
    :param allow_nan: Boolean flag.  If True, this method allows NaN's in the
        coordinate arrays.
    :return: ValueError: if NaN's occur at different positions in the two
        arrays.
    """

    error_checking.assert_is_boolean(allow_nan)

    if allow_nan:
        error_checking.assert_is_real_numpy_array(x_coordinates)
        error_checking.assert_is_real_numpy_array(y_coordinates)
    else:
        error_checking.assert_is_numpy_array_without_nan(x_coordinates)
        error_checking.assert_is_numpy_array_without_nan(y_coordinates)

    error_checking.assert_is_numpy_array(x_coordinates, num_dimensions=1)

    these_expected_dim = numpy.array([len(x_coordinates)], dtype=int)
    error_checking.assert_is_numpy_array(y_coordinates,
                                         exact_dimensions=these_expected_dim)

    x_nan_indices = numpy.where(numpy.isnan(x_coordinates))[0]
    y_nan_indices = numpy.where(numpy.isnan(y_coordinates))[0]

    if not numpy.array_equal(x_nan_indices, y_nan_indices):
        error_string = ('x-coord ({0:s}) and y-coord ({1:s}) lists have NaN'
                        's at different'
                        ' locations.').format(str(x_nan_indices),
                                              str(y_nan_indices))

        raise ValueError(error_string)
예제 #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
    def test_assert_is_boolean_none(self):
        """Checks assert_is_boolean when input is None."""

        with self.assertRaises(TypeError):
            error_checking.assert_is_boolean(None)
    def test_assert_is_boolean_integer(self):
        """Checks assert_is_boolean when input is integer."""

        with self.assertRaises(TypeError):
            error_checking.assert_is_boolean(SINGLE_INTEGER)
    def test_assert_is_boolean_nan(self):
        """Checks assert_is_boolean when input is NaN."""

        with self.assertRaises(TypeError):
            error_checking.assert_is_boolean(numpy.nan)
    def test_assert_is_boolean_complex(self):
        """Checks assert_is_boolean when input is complex."""

        with self.assertRaises(TypeError):
            error_checking.assert_is_boolean(SINGLE_COMPLEX_NUMBER)
    def test_assert_is_boolean_true(self):
        """Checks assert_is_boolean when input is Boolean."""

        error_checking.assert_is_boolean(SINGLE_BOOLEAN)
    def test_assert_is_boolean_float(self):
        """Checks assert_is_boolean when input is float."""

        with self.assertRaises(TypeError):
            error_checking.assert_is_boolean(SINGLE_FLOAT)
    def test_assert_is_boolean_too_many_inputs(self):
        """Checks assert_is_boolean when input is array of Booleans."""

        with self.assertRaises(TypeError):
            error_checking.assert_is_boolean(BOOLEAN_NUMPY_ARRAY)