def test_segment_text(): """Test the segment_text property.""" legend = Legend(range(10), LegendParameters(segment_count=6)) assert len(legend.segment_text_location) == len(legend.segment_text) == 6 assert legend.segment_text == [ '0.00', '1.80', '3.60', '5.40', '7.20', '9.00' ] for pl in legend.segment_text_location: assert isinstance(pl, Plane) for pt in legend.segment_text_location_2d: assert isinstance(pt, Point2D) seg_num = [0, 1.8, 3.6, 5.4, 7.2, 9] for i, num in enumerate(legend.segment_numbers): assert num == pytest.approx(seg_num[i], rel=1e-3) legend = Legend(range(10), LegendParameters(segment_count=6)) legend.legend_parameters.vertical = False assert len(legend.segment_text_location) == len(legend.segment_text) == 6 assert legend.segment_text == [ '0.00', '1.80', '3.60', '5.40', '7.20', '9.00' ] for pl in legend.segment_text_location: assert isinstance(pl, Plane)
def test_legend_title(): """Test the title property.""" legend = Legend(range(5)) assert legend.title == '' assert legend.legend_parameters.is_title_default is True assert legend.title_location.o == Point3D(0, 11.25, 0) assert legend.title_location_2d == Point2D(0, 11.25) legend = Legend(range(5), LegendParameters(segment_count=5, title='C')) assert legend.title == 'C' assert legend.legend_parameters.is_title_default is False assert legend.title_location.o == Point3D(0, 5.25, 0) assert legend.title_location_2d == Point2D(0, 5.25)
def test_segment_text_ordinal_dictionary(self): """Test the segment_text property with ordinal dictionary.""" results = list(range(3000)) ordinal_dict = {300: 'low', 1150: 'desired', 2000: 'too much'} legend = Legend(results, LegendParameters(min=300, max=2000, segment_count=3)) legend.legend_parameters.ordinal_dictionary = ordinal_dict assert len(legend.segment_text_location) == len(legend.segment_text) == 3 assert legend.segment_text == ['low', 'desired', 'too much'] assert len(set(legend.value_colors)) > 100 legend = Legend(results, LegendParameters(min=300, max=2000, segment_count=3)) legend.legend_parameters.ordinal_dictionary = ordinal_dict legend.legend_parameters.continuous_colors = False assert len(legend.segment_text_location) == len(legend.segment_text) == 3 assert legend.segment_text == ['low', 'desired', 'too much'] assert len(set(legend.value_colors)) == 3
def test_init_legend_with_parameter(): """Test the initialization of Legend with LegendParameter objects.""" legend = Legend([0, 10], LegendParameters(2, 8)) assert len(legend.values) == 2 assert legend.legend_parameters.min == 2 assert legend.legend_parameters.max == 8 assert legend.is_min_default is False assert legend.is_max_default is False
def test_init_legend(): """Test the initialization of Legend objects.""" legend = Legend([0, 10]) str(legend) # Test the Legend representation hash(legend) assert len(legend) == 2 assert legend[0] == 0 assert legend[1] == 10 for item in legend: assert isinstance(item, (float, int)) assert len(legend.values) == 2 assert legend.legend_parameters.min == 0 assert legend.legend_parameters.max == 10 assert legend.is_min_default assert legend.is_max_default assert legend.legend_parameters.is_segment_count_default assert legend.legend_parameters.is_title_default assert legend.legend_parameters.is_base_plane_default assert legend.legend_parameters.is_segment_height_default assert legend.legend_parameters.is_segment_width_default assert legend.legend_parameters.is_text_height_default legend_copy = legend.duplicate() assert legend_copy.values == legend.values assert legend_copy.legend_parameters.min == legend.legend_parameters.min assert legend_copy.legend_parameters.max == legend.legend_parameters.max assert legend_copy.legend_parameters.segment_count == \ legend.legend_parameters.segment_count assert legend_copy.is_min_default assert legend_copy.is_max_default assert legend_copy.legend_parameters.is_segment_count_default assert legend_copy.legend_parameters.is_title_default assert legend_copy.legend_parameters.is_base_plane_default assert legend_copy.legend_parameters.is_segment_height_default assert legend_copy.legend_parameters.is_segment_width_default assert legend_copy.legend_parameters.is_text_height_default assert legend_copy == legend legend_copy.legend_parameters.segment_count = 3 assert legend_copy != legend
def test_legend_value_colors(): """Test the color_range, value_colors, and segment_colors property.""" legend = Legend(range(5), LegendParameters(segment_count=10)) for i, color in enumerate(legend.color_range): assert color == ColorRange()[i] assert legend.color_range.domain == ColorRange(domain=(0, 4)).domain assert len(legend.values) == 5 assert len(legend.value_colors) == 5 assert legend.value_colors[0] == Colorset.original()[0] assert legend.segment_colors == Colorset.original()
def test_segment_text_ordinal_dictionary2(self): """Test the segment_text property with another ordinal dictionary.""" results = list(range(-4, 5)) ordinal_dict = PredictedMeanVote().unit_descr legend = Legend(results, LegendParameters(min=-3, max=3, segment_count=7)) legend.legend_parameters.ordinal_dictionary = ordinal_dict assert legend.segment_text == ['Cold', 'Cool', 'Slightly Cool', 'Neutral', 'Slightly Warm', 'Warm', 'Hot'] legend.legend_parameters.min = -2 legend.legend_parameters.max = 2 legend.legend_parameters.segment_count = 5 assert legend.segment_text == ['Cool', 'Slightly Cool', 'Neutral', 'Slightly Warm', 'Warm']
def test_categorized_category_names(): """Test the LegendParametersCategorized category_names property.""" data = [100, 300, 500, 1000, 2000, 3000] leg_colors = [Color(0, 0, 255), Color(0, 255, 0), Color(255, 0, 0)] legend_par = LegendParametersCategorized([300, 2000], leg_colors) cat_names = ['low', 'desired', 'too much'] legend_par.category_names = cat_names legend = Legend(data, legend_par) assert legend_par.category_names == tuple(cat_names) assert legend.segment_text == tuple(cat_names) with pytest.raises(AssertionError): legend_par.category_names = [ 'low', 'desired', 'too much', 'not a category' ]
def test_categorized_colors(): """Test the LegendParametersCategorized colors property.""" data = [100, 300, 500, 1000, 2000, 3000] leg_colors = [Color(0, 0, 255), Color(0, 255, 0), Color(255, 0, 0)] legend_par = LegendParametersCategorized([300, 2000], leg_colors) legend = Legend(data, legend_par) assert legend.segment_colors == tuple(leg_colors) assert legend.value_colors == \ (Color(0, 0, 255), Color(0, 255, 0), Color(0, 255, 0), Color(0, 255, 0), Color(0, 255, 0), Color(255, 0, 0)) legend.legend_parameters.continuous_colors = True assert legend.value_colors == \ (Color(0, 0, 255), Color(0, 0, 255), Color(0, 60, 195), Color(0, 210, 45), Color(255, 0, 0), Color(255, 0, 0))
def test_segment_mesh_2d(): """Test the segment_mesh_2d property.""" legend = Legend(range(10), LegendParameters(segment_count=6)) assert isinstance(legend.segment_mesh_2d, Mesh2D) assert len(legend.segment_mesh_2d.faces) == 6 assert len(legend.segment_mesh_2d.vertices) == 14 legend.legend_parameters.vertical = False assert len(legend.segment_mesh_2d.faces) == 6 assert len(legend.segment_mesh_2d.vertices) == 14 legend.legend_parameters.continuous_legend = True assert len(legend.segment_mesh_2d.faces) == 5 assert len(legend.segment_mesh_2d.vertices) == 12 legend.legend_parameters.vertical = True assert len(legend.segment_mesh_2d.faces) == 5 assert len(legend.segment_mesh_2d.vertices) == 12
def test_legend_to_from_dict(): """Test the to/from dict methods.""" legend = Legend([0, 10], LegendParameters(2, 8)) legend_dict = legend.to_dict() new_legend = Legend.from_dict(legend_dict) assert new_legend.to_dict() == legend_dict
except ImportError as e: raise ImportError('\nFailed to import ladybug:\n\t{}'.format(e)) try: from ladybug_rhino.togeometry import to_plane from ladybug_rhino.fromobjects import legend_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): # set default values legend_par_ = legend_par_.duplicate() if legend_par_ is not None else \ LegendParameters() if _base_plane_: legend_par_.base_plane = to_plane(_base_plane_) legend_par_.title = title_ # create the legend legend = Legend(_values, legend_par_) # separate all of the outputs from this component rhino_objs = legend_objects(legend) mesh = rhino_objs[0] title_obj = rhino_objs[1] label_objs = rhino_objs[2:] label_text = legend.segment_text colors = [color_to_color(col) for col in legend.value_colors]