def test_to_from_dict(): """Test the to/from dict methods.""" mesh2d = Mesh2D.from_grid(num_x=2, num_y=2) mesh3d = Mesh3D.from_mesh2d(mesh2d) data = [0, 1, 2, 3] graphic_con = GraphicContainer(data, mesh3d.min, mesh3d.max) graphic_con_dict = graphic_con.to_dict() new_graphic_con = GraphicContainer.from_dict(graphic_con_dict) assert new_graphic_con.to_dict() == graphic_con_dict
def create_graphic_container(_season, _data, _study_mesh, _legend_par): """Creates the Ladybug 'Graphic' Object from the result data Copied from Ladybug 'IncidentRadiation' Component Arguments: _season: (str) 'Winter' or 'Summer'. Used in the title. _data: (list: float:) A list of the result data to use to color / style the output _study_mesh: (ladybug_geometry.geometry3d.Mesh3D) The joined Mesh used in the analysis _legend_par: Ladybug Legend Parameters Returns: (tuple) graphic: (ladybug.graphic.GraphicContainer) The Ladybug Graphic Object title: The text title """ graphic = GraphicContainer(_data, _study_mesh.min, _study_mesh.max, _legend_par) graphic.legend_parameters.title = 'kWh' title = text_objects('{} Incident Radiation'.format(_season), graphic.lower_title_location, graphic.legend_parameters.text_height * 1.5, graphic.legend_parameters.font) return graphic, title
def test_init_graphic_con(): """Test the initialization of GraphicContainer objects.""" mesh2d = Mesh2D.from_grid(num_x=2, num_y=2) mesh3d = Mesh3D.from_mesh2d(mesh2d) data = [0, 1, 2, 3] graphic_con = GraphicContainer(data, mesh3d.min, mesh3d.max) str(graphic_con) # Test the GraphicContainer representation assert len(graphic_con) == 4 assert graphic_con[0] == 0 assert graphic_con[-1] == 3 for item in graphic_con: assert isinstance(item, (float, int)) assert len(graphic_con.values) == 4 assert isinstance(graphic_con.legend, Legend) assert graphic_con.value_colors == graphic_con.legend.value_colors assert graphic_con.legend_parameters.is_base_plane_default is False assert graphic_con.legend_parameters.is_segment_height_default is False assert graphic_con.legend_parameters.is_segment_width_default is True assert graphic_con.legend_parameters.is_text_height_default is True assert graphic_con.legend_parameters.base_plane != Plane() assert isinstance(graphic_con.lower_title_location, Plane) assert isinstance(graphic_con.upper_title_location, Plane) assert graphic_con.lower_title_location != Plane() assert graphic_con.upper_title_location != Plane()
def test_init_graphic_con_invalid(self): """Test the initialization of GraphicContainer objects with invalid inputs.""" mesh2d = Mesh2D.from_grid(num_x=2, num_y=2) mesh3d = Mesh3D.from_mesh2d(mesh2d) data = [0, 1, 2, 3, 4] with pytest.raises(Exception): GraphicContainer(data, mesh3d.min, mesh3d.max, data_type=Temperature(), unit='NotAUnit')
def test_init_graphic_con_data_type(self): """Test the initialization of GraphicContainer objects with a DataType.""" mesh2d = Mesh2D.from_grid(num_x=2, num_y=2) mesh3d = Mesh3D.from_mesh2d(mesh2d) data = [-1, 0, 1, 2] graphic_con = GraphicContainer(data, mesh3d.min, mesh3d.max, data_type=Temperature()) assert graphic_con.legend_parameters.is_title_default is False assert graphic_con.legend_parameters.title == 'C' legend_par = LegendParameters() legend_par.vertical = False graphic_con = GraphicContainer(data, mesh3d.min, mesh3d.max, legend_par, data_type=Temperature()) assert graphic_con.legend_parameters.is_title_default is False assert graphic_con.legend_parameters.title == 'Temperature (C)'
def graphic_container(self): """Get a ladybug GraphicContainer that relates to this object. The GraphicContainer possesses almost all things needed to visualize the ColorFace object including the legend, value_colors, lower_title_location, upper_title_location, etc. """ return GraphicContainer(self.matched_values, self.min_point, self._max_point, self.legend_parameters, self.data_type, str(self.unit))
def test_init_graphic_con_vertex_based(): """Test the initialization of ResultMesh objects with vertex-based input.""" mesh2d = Mesh2D.from_grid(num_x=2, num_y=2) mesh3d = Mesh3D.from_mesh2d(mesh2d) data = [0, 1, 2, 3, 4, 5, 6, 7, 8] graphic_con = GraphicContainer(data, mesh3d.min, mesh3d.max) assert len(graphic_con) == 9 assert graphic_con[0] == 0 assert graphic_con[-1] == 8 assert len(graphic_con.values) == 9 assert isinstance(graphic_con.legend_parameters, LegendParameters) assert isinstance(graphic_con.legend, Legend) assert graphic_con.value_colors == graphic_con.legend.value_colors
def test_init_graphic_con_data_type_ordinal(self): """Test the ResultMesh objects with a DataType with unit_descr.""" mesh2d = Mesh2D.from_grid(num_x=2, num_y=2) mesh3d = Mesh3D.from_mesh2d(mesh2d) data = [-1, 0, 1, 2] graphic_con = GraphicContainer(data, mesh3d.min, mesh3d.max, data_type=PredictedMeanVote(), unit='PMV') assert graphic_con.legend_parameters.min == -3 assert graphic_con.legend_parameters.max == 3 assert graphic_con.legend_parameters.segment_count == 7 assert graphic_con.legend_parameters.is_title_default is False assert graphic_con.legend_parameters.title == 'PMV' assert graphic_con.legend.segment_text == ['Cold', 'Cool', 'Slightly Cool', 'Neutral', 'Slightly Warm', 'Warm', 'Hot']
def graphic_container(self): """Get a ladybug GraphicContainer that relates to this object. The GraphicContainer possesses almost all things needed to visualize the ColorRooms object including the legend, value_colors, etc. """ # produce a range of values from the collected attributes attr_dict = {i: val for i, val in enumerate(self._attributes_unique)} attr_dict_rev = {val: i for i, val in attr_dict.items()} values = tuple(attr_dict_rev[r_attr] for r_attr in self._attributes) # produce legend parameters with an ordinal dict for the attributes l_par = self.legend_parameters.duplicate() l_par.segment_count = len(self._attributes_unique) l_par.ordinal_dictionary = attr_dict if l_par.is_title_default: l_par.title = self.attr_name_end.replace('_', ' ').title() return GraphicContainer(values, self.min_point, self._max_point, l_par)
def test_graphic_con_data_type_ordinal_all_same(): """Test the GraphicContainer with a DataType with unit_descr and all equal values.""" mesh2d = Mesh2D.from_grid(num_x=2, num_y=2) mesh3d = Mesh3D.from_mesh2d(mesh2d) data = [0] * 3 graphic_con = GraphicContainer(data, mesh3d.min, mesh3d.max, data_type=PredictedMeanVote(), unit='PMV') assert graphic_con.legend_parameters.min == -3 assert graphic_con.legend_parameters.max == 3 assert graphic_con.legend_parameters.segment_count == 7 assert not graphic_con.legend_parameters.is_title_default assert graphic_con.legend_parameters.title == 'PMV' assert graphic_con.legend.segment_text == [ 'Cold', 'Cool', 'Slightly Cool', 'Neutral', 'Slightly Warm', 'Warm', 'Hot' ]
def test_init_graphic_con_legend_parameters(): """Test the initialization of ResultMesh objects with a LegendParameters.""" mesh2d = Mesh2D.from_grid(num_x=2, num_y=2) mesh3d = Mesh3D.from_mesh2d(mesh2d) data = [-1, 0, 1, 2] legend_par = LegendParameters(base_plane=Plane(o=Point3D(2, 2, 0))) legend_par.vertical = False legend_par.segment_height = 0.25 legend_par.segment_width = 0.5 legend_par.text_height = 0.15 graphic_con = GraphicContainer(data, mesh3d.min, mesh3d.max, legend_par) assert graphic_con.legend_parameters.is_base_plane_default is False assert graphic_con.legend_parameters.is_segment_height_default is False assert graphic_con.legend_parameters.is_segment_width_default is False assert graphic_con.legend_parameters.is_text_height_default is False assert graphic_con.legend_parameters.vertical is False assert graphic_con.legend_parameters.base_plane.o == Point3D(2, 2, 0) assert graphic_con.legend_parameters.segment_height == 0.25 assert graphic_con.legend_parameters.segment_width == 0.5 assert graphic_con.legend_parameters.text_height == 0.15
for ival, ang in zip(int_vals, angles)) weight_result = sum(r * w for r, w in zip(w_res, patch_wghts)) results.append(weight_result * 100 / vec_count) else: if patch_wghts: for int_list in int_matrix: weight_result = sum(r * w for r, w in zip(int_list, patch_wghts)) results.append(weight_result * 100 / vec_count) else: results = [ sum(int_list) * 100 / vec_count for int_list in int_matrix ] # create the mesh and legend outputs graphic = GraphicContainer(results, study_mesh.min, study_mesh.max, legend_par_) graphic.legend_parameters.title = '%' if legend_par_ is None or legend_par_.are_colors_default: graphic.legend_parameters.colors = Colorset.view_study() title_txt = vt_str if vt_str in ('Sky Exposure', 'Sky View') else \ '{} View'.format(vt_str) title = text_objects(title_txt, graphic.lower_title_location, graphic.legend_parameters.text_height * 1.5, graphic.legend_parameters.font) # create all of the visual outputs study_mesh.colors = graphic.value_colors mesh = from_mesh3d(study_mesh) legend = legend_objects(graphic.legend)
elif period_ is not None: new_data = [[ data.filter_by_analysis_period(period_) for data in data_list ] for data_list in data_mtx] values = [data.average for data_list in new_data for data in data_list] time_text = period_ else: values = [data.average for data_list in data_mtx for data in data_list] time_text = header.analysis_period # generate Ladybug objects for the graphic lb_meshes = [to_mesh3d(mesh) for mesh in _mesh] lb_mesh = Mesh3D.join_meshes(lb_meshes) graphic = GraphicContainer(values, lb_mesh.min, lb_mesh.max, legend_par_, data_type=header.data_type, unit=header.unit) # set titles and set default colors and color ranges if graphic.legend_parameters.are_colors_default: graphic.legend_parameters.colors = colors_from_data_type( header.data_type) if isinstance(header.data_type, TemperatureDelta) and graphic.legend.is_min_default \ and graphic.legend.is_max_default: graphic.legend_parameters.min = -5 graphic.legend_parameters.max = 5 graphic.legend_parameters.title = header.unit global_title = '{}\n{}'.format(header.data_type.name, time_text) title = text_objects(global_title, graphic.lower_title_location, graphic.legend_parameters.text_height * 1.5,
# move the center point so sun paths are not on top of one another fac = i * radius * 3 center_pt_i = Point2D(center_pt.x + fac, center_pt.y) center_pt3d_i = Point3D(center_pt3d.x + fac, center_pt3d.y, center_pt3d.z) # create the ladybug compass object lb_compass = Compass(radius, center_pt_i, north_) # create a graphic container to generate colors and legends n_data = data.filter_by_moys( moys) # filter data collection by sun-up hours graphic = GraphicContainer(n_data.values, lb_compass.min_point3d(z), lb_compass.max_point3d(z), lpar, n_data.header.data_type, n_data.header.unit) all_legends.append(legend_objects(graphic.legend)) title.append( text_objects(title_text(n_data), graphic.lower_title_location, graphic.legend_parameters.text_height, graphic.legend_parameters.font)) # create points, analemmas, daily arcs, and compass geometry sun_pts_init = draw_sun_positions(suns, radius, center_pt3d_i) analemma_i, daily_i = draw_analemma_and_arcs( sp, datetimes, radius, center_pt3d_i) compass_i = compass_objects(lb_compass, z, None, projection_, graphic.legend_parameters.font) all_analemma.append(analemma_i)
from ladybug_rhino.togeometry import to_mesh3d from ladybug_rhino.fromgeometry import from_mesh3d from ladybug_rhino.fromobjects import legend_objects from ladybug_rhino.text import text_objects from ladybug_rhino.color import color_to_color from ladybug_rhino.grasshopper import all_required_inputs except ImportError as e: raise ImportError('\nFailed to import ladybug_rhino:\n\t{}'.format(e)) if all_required_inputs(ghenv.Component): # generate Ladybug objects lb_mesh = to_mesh3d(_mesh) if offset_dom_: dom_st, dom_end = offset_dom_ lb_mesh = lb_mesh.height_field_mesh(_values, (dom_st, dom_end)) graphic = GraphicContainer(_values, lb_mesh.min, lb_mesh.max, legend_par_) # generate titles if legend_title_ is not None: graphic.legend_parameters.title = legend_title_ if global_title_ is not None: title = text_objects(global_title_, graphic.lower_title_location, graphic.legend_parameters.text_height * 1.5, graphic.legend_parameters.font) # draw rhino objects lb_mesh.colors = graphic.value_colors mesh = from_mesh3d(lb_mesh) legend = legend_objects(graphic.legend) colors = [color_to_color(col) for col in lb_mesh.colors] legend_par = graphic.legend_parameters
def draw_dome(dome_data, center, dome_name, legend_par): """Draw the dome mesh, compass, legend, and title for a sky dome. Args: dome_data: List of radiation values for the dome data center: Point3D for the center of the sun path. dome_name: Text for the dome name, which will appear in the title. legend_par: Legend parameters to be used for the dome Returns: dome_mesh: A colored mesh for the dome based on dome_data. dome_compass: A compass for the dome. dome_legend: A leend for the colored dome mesh. dome_title: A title for the dome. values: A list of radiation values that align with the dome_mesh faces. """ # create the dome mesh and ensure patch values align with mesh faces if len(dome_data) == 145: # tregenza sky lb_mesh = view_sphere.tregenza_dome_mesh_high_res.scale(radius) values = [] # high res dome has 3 x 3 faces per patch; we must convert tot_i = 0 # track the total number of patches converted for patch_i in view_sphere.TREGENZA_PATCHES_PER_ROW: row_vals = [] for val in dome_data[tot_i:tot_i + patch_i]: row_vals.extend([val] * 3) for i in range(3): values.extend(row_vals) tot_i += patch_i values = values + [dome_data[-1] ] * 18 # last patch has triangular faces else: #reinhart sky lb_mesh = view_sphere.reinhart_dome_mesh.scale(radius) values = dome_data + [dome_data[-1] ] * 11 # last patch has triangular faces # move and/or rotate the mesh as needed if north != 0: lb_mesh = lb_mesh.rotate_xy(math.radians(north), Point3D()) if center != Point3D(): lb_mesh = lb_mesh.move(Vector3D(center.x, center.y, center.z)) # project the mesh if requested if projection_ is not None: if projection_.title() == 'Orthographic': pts = (Compass.point3d_to_orthographic(pt) for pt in lb_mesh.vertices) elif projection_.title() == 'Stereographic': pts = (Compass.point3d_to_stereographic(pt, radius, center) for pt in lb_mesh.vertices) else: raise ValueError( 'Projection type "{}" is not recognized.'.format(projection_)) pts3d = tuple(Point3D(pt.x, pt.y, z) for pt in pts) lb_mesh = Mesh3D(pts3d, lb_mesh.faces) # output the dome visualization, including legend and compass move_fac = radius * 0.15 min_pt = lb_mesh.min.move(Vector3D(-move_fac, -move_fac, 0)) max_pt = lb_mesh.max.move(Vector3D(move_fac, move_fac, 0)) graphic = GraphicContainer(values, min_pt, max_pt, legend_par) graphic.legend_parameters.title = 'kWh/m2' lb_mesh.colors = graphic.value_colors dome_mesh = from_mesh3d(lb_mesh) dome_legend = legend_objects(graphic.legend) dome_compass = compass_objects( Compass(radius, Point2D(center.x, center.y), north), z, None, projection_, graphic.legend_parameters.font) # construct a title from the metadata st, end = metadata[2], metadata[3] time_str = '{} - {}'.format(st, end) if st != end else st title_txt = '{} Radiation\n{}\n{}'.format( dome_name, time_str, '\n'.join([dat for dat in metadata[4:]])) dome_title = text_objects(title_txt, graphic.lower_title_location, graphic.legend_parameters.text_height, graphic.legend_parameters.font) return dome_mesh, dome_compass, dome_legend, dome_title, values