def test_mesh2d_init_from_face_vertices():
    """Test the initalization of Mesh2D from_face_vertices."""
    face_1 = (Point2D(0, 0), Point2D(0, 2), Point2D(2, 2), Point2D(2, 0))
    face_2 = (Point2D(2, 2), Point2D(2, 0), Point2D(4, 0))
    mesh_1 = Mesh2D.from_face_vertices([face_1, face_2])
    mesh_2 = Mesh2D.from_face_vertices([face_1, face_2], False)

    assert len(mesh_1.vertices) == 5
    assert len(mesh_2.vertices) == 7
    assert len(mesh_1.faces) == len(mesh_2.faces) == 2
    assert mesh_1.area == mesh_2.area == 6

    assert mesh_1.min == mesh_2.min == Point2D(0, 0)
    assert mesh_1.max == mesh_2.max == Point2D(4, 2)
    assert mesh_1.center == mesh_2.center == Point2D(2, 1)
    assert mesh_1.centroid.x == mesh_2.centroid.x == pytest.approx(1.56,
                                                                   rel=1e-2)
    assert mesh_1.centroid.y == mesh_2.centroid.y == pytest.approx(0.89,
                                                                   rel=1e-2)

    assert len(mesh_1.face_areas) == len(mesh_2.face_areas) == 2
    assert mesh_1.face_areas[0] == mesh_2.face_areas[0] == 4
    assert mesh_1.face_areas[1] == mesh_2.face_areas[1] == 2
    assert len(mesh_1.face_centroids) == len(mesh_2.face_centroids) == 2
    assert mesh_1.face_centroids[0] == mesh_2.face_centroids[0] == Point2D(
        1, 1)
    assert mesh_1.face_centroids[1].x == mesh_2.face_centroids[
        1].x == pytest.approx(2.67, rel=1e-2)
    assert mesh_1.face_centroids[1].y == mesh_2.face_centroids[
        1].y == pytest.approx(0.67, rel=1e-2)

    assert mesh_1._is_color_by_face is mesh_1._is_color_by_face is False
    assert mesh_1.colors is mesh_1.colors is None
Exemple #2
0
    def colored_mesh(self):
        """Get the colored Mesh2D for this graphic.

        Returns:
            A Mesh2D of the wind rose plot.
        """
        assert self._number_of_directions > 2, 'The number of directions must be ' \
            'greater then three to plot the wind rose. Currently the ' \
            'number_of_directions parameter is: {}'.format(self._number_of_directions)

        # Reset computed graphics to account for changes to cached viz properties
        self._compass = None
        self._container = None

        max_bar_radius = self.mesh_radius
        min_bar_radius = self._zero_mesh_radius
        zero_mesh_array, zero_color_array = [], []

        if self.show_zeros:
            # Compute the array for calm rose
            zero_data = [[0] for _ in self.histogram_data]
            zero_data_stacked = [[0] for _ in self.histogram_data]
            zero_mesh_array, zero_color_array = WindRose._compute_colored_mesh_array(
                zero_data,
                zero_data_stacked,
                self.bin_vectors,
                0,
                min_bar_radius,
                show_freq=False)

        # Calculate regular mesh
        if self.show_freq:
            histogram_data_stacked = WindRose._histogram_data_nested(
                self.histogram_data, self.frequency_hours)
        else:
            histogram_data_stacked = [[sum(h) / len(h)] if len(h) > 0 else [0]
                                      for h in self.histogram_data]
            # Don't round radius since there are no frequency intervals.
            max_bar_radius = self.real_freq_max / self.frequency_hours
            max_bar_radius *= self.frequency_spacing_distance
            max_bar_radius += self._zero_mesh_radius

        mesh_array, color_array = WindRose._compute_colored_mesh_array(
            self.histogram_data, histogram_data_stacked, self.bin_vectors,
            min_bar_radius, max_bar_radius, self.show_freq)

        mesh_array += zero_mesh_array
        color_array += zero_color_array

        mesh = Mesh2D.from_face_vertices(mesh_array, purge=True)

        # Assign colors
        _color_range = self.color_range
        mesh.colors = tuple(_color_range.color(val) for val in color_array)

        # Scale up unit circle to windrose radius (and other transforms)
        return self._transform(mesh)
Exemple #3
0
    def colored_mesh(self):
        """Get a colored Mesh2D for this graphic.
        """
        max_bar_radius = 1.0
        min_bar_radius = 0.0
        zero_mesh_array, zero_color_array = [], []

        # Calculate ytick nums for the wind mesh
        ytick_num = self.legend.legend_parameters.segment_count  # total
        max_bar_num = self.real_freq_max
        ytick_num_mesh = ytick_num  # default

        if self.show_zeros:
            # Calculate radius of zero rose

            max_bar_num += self.zeros_per_bin
            min_bar_radius = self.zeros_per_bin / max_bar_num * max_bar_radius
            ytick_num_mesh = max_bar_num / self.frequency_maximum * ytick_num

            # Update yticks
            zero_ytick_num_mesh = self._ytick_zero_inc()
            ytick_num_mesh -= zero_ytick_num_mesh

            # Compute the array for calm rose
            zero_data = [[0] for _ in self.histogram_data]
            zero_data_stacked = [[0] for _ in self.histogram_data]
            zero_mesh_array, zero_color_array = WindRose._compute_colored_mesh_array(
                zero_data,
                zero_data_stacked,
                self.bin_vectors,
                zero_ytick_num_mesh,
                0,
                min_bar_radius,
                show_stack=False)

        # Calculate regular mesh
        if self.show_stack:
            histogram_data_stacked = WindRose._histogram_data_stacked(
                self.histogram_data, ytick_num)
        else:
            histogram_data_stacked = [[sum(h) / len(h)] if len(h) > 0 else [0]
                                      for h in self.histogram_data]
        mesh_array, color_array = WindRose._compute_colored_mesh_array(
            self.histogram_data, histogram_data_stacked, self.bin_vectors,
            ytick_num_mesh, min_bar_radius, max_bar_radius, self.show_stack)

        mesh_array += zero_mesh_array
        color_array += zero_color_array

        mesh = Mesh2D.from_face_vertices(mesh_array, purge=True)
        mesh.colors = color_array

        return mesh.scale(self.wind_radius).move(self.base_point)
Exemple #4
0
    def colored_mesh(self):
        """Get the colored Mesh2D for this graphic.

        Returns:
            A Mesh2D of the wind rose plot.
        """
        assert self._number_of_directions > 2, 'The number of directions must be ' \
            'greater then three to plot the wind rose. Currently the ' \
            'number_of_directions parameter is: {}'.format(self._number_of_directions)

        # Reset computed graphics to account for changes to cached viz properties
        self._compass = None
        self._container = None

        zero_poly_array, zero_color_array = [], []
        max_bar_radius = self.mesh_radius
        min_bar_radius = self._zero_mesh_radius

        if self.show_zeros and self.zero_count > 0:
            # Compute the array for calm rose
            zero_data = [[0] for _ in self.histogram_data]
            zero_data_stacked = [[[0]] for _ in self.histogram_data]
            zero_poly_array, zero_color_array = WindRose._compute_colored_mesh_array(
                zero_data,
                zero_data_stacked,
                self.bin_vectors,
                0,
                min_bar_radius,
                show_freq=False)

        # Calculate stacked_data
        flat_data = [b for a in self.histogram_data for b in a]
        max_data = max(flat_data)
        min_data = min(
            self.analysis_values) if self.show_zeros else min(flat_data)
        bin_count = self.legend_parameters.segment_count
        data_range = (min_data, max_data)
        histogram_data_stacked, bin_range = WindRose._histogram_data_nested(
            self.histogram_data, data_range, bin_count)
        data_step = bin_range[1] - bin_range[0]

        if not self.show_freq:
            for i in range(self._number_of_directions):
                stack = histogram_data_stacked[i]
                vals = [b for a in stack for b in a]
                if len(vals) > 0:
                    mean_val = sum(vals) / float(len(vals))
                    histogram_data_stacked[i] = [[mean_val for b in a]
                                                 for a in stack]
        poly_array, color_array = WindRose._compute_colored_mesh_array(
            self.histogram_data, histogram_data_stacked, self.bin_vectors,
            min_bar_radius, max_bar_radius, self.show_freq)

        # Compute colors
        # If show_freq, assign colors to intervals. Else keep averages.
        if self.show_freq:
            for i, mean_val in enumerate(color_array):
                for j in range(len(bin_range) - 1):
                    if bin_range[j] <= mean_val < bin_range[j + 1]:
                        color_array[i] = j
                        break
            if self.show_zeros:
                color_array = [c + 1 for c in color_array]

            # convert bin legend interval to the average interval (interval midpoint)
            color_array = [(c * data_step) + min_data for c in color_array]
        poly_array += zero_poly_array
        color_array += zero_color_array

        # Store colors and polygons before processing
        self._poly_array = poly_array
        self._color_array = color_array  # for testing

        # Assign colors
        _color_range = self.color_range
        _color_range = [_color_range.color(val) for val in color_array]
        mesh = Mesh2D.from_face_vertices(poly_array, purge=True)
        mesh.colors = tuple(_color_range)

        # Scale up unit circle to windrose radius (and other transforms)
        return self._transform(mesh)