def test_assert_is_non_negative_numpy_array_non_negative_with_nan_allowed(
            self):
        """Checks assert_is_geq_numpy_array; base_value = 0, inputs >= 0.

        In this case, input array contains NaN's and allow_nan = True.
        """

        error_checking.assert_is_geq_numpy_array(
            NON_NEGATIVE_NUMPY_ARRAY_WITH_NANS, 0, allow_nan=True)
    def test_assert_is_non_negative_numpy_array_positive_with_nan_banned(self):
        """Checks assert_is_geq_numpy_array; base_value = 0, inputs > 0.

        In this case, input array contains NaN's and allow_nan = False.
        """

        with self.assertRaises(ValueError):
            error_checking.assert_is_geq_numpy_array(
                POSITIVE_NUMPY_ARRAY_WITH_NANS, 0, allow_nan=False)
Beispiel #3
0
def _polygons_to_mask_one_panel(polygon_objects_grid_coords, num_grid_rows,
                                num_grid_columns):
    """Converts list of polygons to binary mask.

    M = number of rows in grid
    N = number of columns in grid

    :param polygon_objects_grid_coords: See doc for
        `polygons_from_pixel_to_grid_coords`.
    :param num_grid_rows: Same.
    :param num_grid_columns: Same.
    :return: mask_matrix: M-by-N numpy array of Boolean flags.  If
        mask_matrix[i, j] == True, grid point [i, j] is in/on at least one of
        the polygons.
    """

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

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

    # TODO(thunderhoser): This triple for-loop is probably inefficient.
    for k in range(num_polygons):
        these_grid_columns = numpy.array(
            polygon_objects_grid_coords[k].exterior.xy[0])

        error_checking.assert_is_geq_numpy_array(these_grid_columns, -0.5)
        error_checking.assert_is_leq_numpy_array(these_grid_columns,
                                                 num_grid_columns - 0.5)

        these_grid_rows = numpy.array(
            polygon_objects_grid_coords[k].exterior.xy[1])

        error_checking.assert_is_geq_numpy_array(these_grid_rows, -0.5)
        error_checking.assert_is_leq_numpy_array(these_grid_rows,
                                                 num_grid_rows - 0.5)

        for i in range(num_grid_rows):
            for j in range(num_grid_columns):
                if mask_matrix[i, j]:
                    continue

                mask_matrix[i, j] = polygons.point_in_or_on_polygon(
                    polygon_object=polygon_objects_grid_coords[k],
                    query_x_coordinate=j,
                    query_y_coordinate=i)

    return mask_matrix
Beispiel #4
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)
Beispiel #5
0
def write_points(output_file_name,
                 grid_row_by_point,
                 grid_column_by_point,
                 panel_row_by_point,
                 panel_column_by_point,
                 full_storm_id_string=None,
                 storm_time_unix_sec=None):
    """Writes human points of interest for one image to NetCDF file.

    K = number of points of interest

    :param output_file_name: Path to output (NetCDF) file.
    :param grid_row_by_point: length-K numpy array of row indices in data grid
        (floats).
    :param grid_column_by_point: length-K numpy array of column indices in data
        grid (floats).
    :param panel_row_by_point: length-K numpy array of row indices in panel grid
        (integers).
    :param panel_column_by_point: length-K numpy array of column indices in
        panel grid (integers).
    :param full_storm_id_string: See doc for `write_polygons`.
    :param storm_time_unix_sec: Same.
    """

    is_composite = (full_storm_id_string is None
                    and storm_time_unix_sec is None)

    if is_composite:
        full_storm_id_string = DUMMY_STORM_ID_STRING
        storm_time_unix_sec = -1

    error_checking.assert_is_string(full_storm_id_string)
    error_checking.assert_is_integer(storm_time_unix_sec)

    # 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)

    error_checking.assert_is_numpy_array(grid_row_by_point, num_dimensions=1)
    error_checking.assert_is_geq_numpy_array(grid_row_by_point, -0.5)
    # error_checking.assert_is_leq_numpy_array(
    #     grid_row_by_point, num_grid_rows - 0.5)

    num_points = len(grid_row_by_point)
    these_expected_dim = numpy.array([num_points], dtype=int)

    error_checking.assert_is_numpy_array(grid_column_by_point,
                                         exact_dimensions=these_expected_dim)
    error_checking.assert_is_geq_numpy_array(grid_column_by_point, -0.5)
    # error_checking.assert_is_leq_numpy_array(
    #     grid_column_by_point, num_grid_columns - 0.5)

    error_checking.assert_is_numpy_array(panel_row_by_point,
                                         exact_dimensions=these_expected_dim)
    error_checking.assert_is_integer_numpy_array(panel_row_by_point)
    error_checking.assert_is_geq_numpy_array(panel_row_by_point, 0)

    error_checking.assert_is_numpy_array(panel_column_by_point,
                                         exact_dimensions=these_expected_dim)
    error_checking.assert_is_integer_numpy_array(panel_column_by_point)
    error_checking.assert_is_geq_numpy_array(panel_column_by_point, 0)

    file_system_utils.mkdir_recursive_if_necessary(file_name=output_file_name)
    dataset_object = netCDF4.Dataset(output_file_name,
                                     'w',
                                     format='NETCDF3_64BIT_OFFSET')

    dataset_object.setncattr(STORM_ID_KEY, full_storm_id_string)
    dataset_object.setncattr(STORM_TIME_KEY, storm_time_unix_sec)
    dataset_object.createDimension(POINT_DIMENSION_KEY, num_points)

    dataset_object.createVariable(GRID_ROW_BY_POINT_KEY,
                                  datatype=numpy.float32,
                                  dimensions=POINT_DIMENSION_KEY)
    dataset_object.variables[GRID_ROW_BY_POINT_KEY][:] = grid_row_by_point

    dataset_object.createVariable(GRID_COLUMN_BY_POINT_KEY,
                                  datatype=numpy.float32,
                                  dimensions=POINT_DIMENSION_KEY)
    dataset_object.variables[
        GRID_COLUMN_BY_POINT_KEY][:] = grid_column_by_point

    dataset_object.createVariable(PANEL_ROW_BY_POINT_KEY,
                                  datatype=numpy.int32,
                                  dimensions=POINT_DIMENSION_KEY)
    dataset_object.variables[PANEL_ROW_BY_POINT_KEY][:] = panel_row_by_point

    dataset_object.createVariable(PANEL_COLUMN_BY_POINT_KEY,
                                  datatype=numpy.int32,
                                  dimensions=POINT_DIMENSION_KEY)
    dataset_object.variables[
        PANEL_COLUMN_BY_POINT_KEY][:] = panel_column_by_point

    dataset_object.close()
Beispiel #6
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_non_negative_numpy_array_mixed_sign(self):
        """assert_is_geq_numpy_array; base_value = 0, inputs mixed sign."""

        with self.assertRaises(ValueError):
            error_checking.assert_is_geq_numpy_array(MIXED_SIGN_NUMPY_ARRAY, 0)
    def test_assert_is_non_negative_numpy_array_non_positive(self):
        """Checks assert_is_geq_numpy_array; base_value = 0, inputs <= 0."""

        with self.assertRaises(ValueError):
            error_checking.assert_is_geq_numpy_array(NON_POSITIVE_NUMPY_ARRAY,
                                                     0)
    def test_assert_is_non_negative_numpy_array_positive(self):
        """Checks assert_is_geq_numpy_array; base_value = 0, inputs > 0."""

        error_checking.assert_is_geq_numpy_array(POSITIVE_NUMPY_ARRAY, 0)