def plot_image(gaussians, title="", with_axis=True, xlim=None, ylim=None, colorschemes=color_schemes.get_colorbrewer_schemes(),legend_lw=2, *args, **kwargs): """ :param with_axis: if mathematical axis is shown or not :param images: List of List of 2D-images in rgb to plot :param gaussians: [gaussian_1, ... , gaussian_n] gaussians from which the image is calculated :param title: title of picture :return: """ fig, axis = plt.subplots(1, len(gaussians), sharex='col', sharey='row') limits = helper.get_limits(gaussians, xlim, ylim) extent = [limits.x_min, limits.x_max, limits.y_min, limits.y_max] z_list = helper.generate_distribution_grids(gaussians, limits=limits) z_min, z_max, z_sum = helper.generate_weights(z_list) colors = color_schemes.get_representiv_colors(colorschemes) names = [chr(i + 65) for i in range(len(gaussians))] custom_lines = [Line2D([0], [0], color=colors[i], lw=legend_lw) for i in range(len(colors))] for i, gaussian in enumerate(gaussians): img, _ = picture_contours.calculate_image([z_list[i]], z_min, z_max, z_sum, [colorschemes[i]], *args, **kwargs) axis[i].imshow(img, extent=extent, origin='lower') if title == "" and gaussians: title = str(gaussian.get_attributes()[4:-1]) axis[i].set_title(title) axis[i].legend([custom_lines[i]], [names[i]], frameon=False) if not with_axis: axis.axis("off") fig.subplots_adjust(wspace=0.1)
def test_generate_crosses_colors(self): dist_one_colors = [[0.77647059, 0.85882353, 0.9372549, 1.], [0.12941176, 0.44313725, 0.70980392, 1.], [.12941176, 0.44313725, 0.70980392, 1.], [0.77647059, 0.85882353, 0.9372549, 1.]] dist_two_colors = [[0.99607843, 0.92428677, 0.85887397, 1.], [0.99215686, 0.77615063, 0.57159781, 1.], [0.99215686, 0.77615063, 0.57159781, 1.], [0.99607843, 0.92428677, 0.85887397, 1.]] dist_1 = Gaussian(means=[1, 0], cov_matrix=[[3, 2], [2, 3]], x_min=self.x_min, x_max=self.x_max, y_min=self.y_min, y_max=self.y_max, size=self.size, weight=self.weight) dist_2 = Gaussian(means=[0, 1], cov_matrix=[[6, 2], [2, 6]], x_min=self.x_min, x_max=self.x_max, y_min=self.y_min, y_max=self.y_max, size=self.size, weight=self.weight) distributions = [dist_1, dist_2] # unpleasant.... limits = helper.get_limits(distributions) z_list = helper.generate_distribution_grids(distributions, limits=limits) z_min, z_max, z_sum = helper.generate_weights(z_list) colorschemes = color_schemes.get_colorbrewer_schemes() crosses = picture_cross.generate_crosses(distributions, z_list, z_min, z_max, colorschemes, method="normal", num_of_levels=1) np.testing.assert_almost_equal(dist_one_colors, crosses[0][2]) np.testing.assert_almost_equal(dist_one_colors, crosses[0][3]) np.testing.assert_almost_equal(dist_two_colors, crosses[1][2]) np.testing.assert_almost_equal(dist_two_colors, crosses[1][3])
def generate_random_points(distributions, colorschemes=None, z_list=None, z_min=None, z_max=None, xlim=None, ylim=None, *args, **kwargs): limits = helper.get_limits(distributions, xlim, ylim) if z_list is None: z_list = helper.generate_distribution_grids(distributions, limits=limits) if z_min is None: z_min, z_max, z_sum = helper.generate_weights(z_list) if colorschemes is None: colorschemes = color_schemes.get_colorbrewer_schemes() return __generate_random_points(distributions, z_list, z_min, z_max, colorschemes[:len(distributions)], *args, **kwargs)
def test_generate_crosses_iso_lines(self): dist_one_iso_lines = [0.25, 0.75, 0.75, 0.25] dist_two_iso_lines = [ 0.09889134000871382, 0.2966740200253193, 0.2966740200253193, 0.09889134000871382 ] dist_1 = Gaussian(means=[1, 0], cov_matrix=[[3, 2], [2, 3]], x_min=self.x_min, x_max=self.x_max, y_min=self.y_min, y_max=self.y_max, size=self.size, weight=self.weight) dist_2 = Gaussian(means=[0, 1], cov_matrix=[[6, 2], [2, 6]], x_min=self.x_min, x_max=self.x_max, y_min=self.y_min, y_max=self.y_max, size=self.size, weight=self.weight) distributions = [dist_1, dist_2] # unpleasant.... limits = helper.get_limits(distributions) z_list = helper.generate_distribution_grids(distributions, limits=limits) z_min, z_max, z_sum = helper.generate_weights(z_list) colorschemes = color_schemes.get_colorbrewer_schemes() crosses = picture_cross.generate_crosses(distributions, z_list, z_min, z_max, colorschemes, method="normal", num_of_levels=1) np.testing.assert_almost_equal(dist_one_iso_lines, crosses[0][4]) np.testing.assert_almost_equal(dist_two_iso_lines, crosses[1][4])
def input_image(ax, distribution, z_sum=None, num_of_pies_x=10, num_of_pies_y=0, angle=0, set_limit=False, iso_level=8, level_to_cut=1, contour_method="equal_density", colorschemes=color_schemes.get_colorbrewer_schemes(), modus="light", borders=None, scale=1., xlim=None, ylim=None): limits = helper.get_limits(distribution, xlim, ylim) if borders is None: if modus == "size": borders = [0.1, 1] else: borders = [.2, .9] if set_limit: ax.set_xlim([limits.x_min, limits.x_max]) ax.set_ylim([limits.y_min, limits.y_max]) if num_of_pies_y == 0: num_of_pies_y = get_distance_ratio(num_of_pies_x, limits) container, distances = container_size(num_of_pies_x, num_of_pies_y, limits) if z_sum is None: z_list = helper.generate_distribution_grids(distribution, limits) z_min, z_max, z_sum = helper.generate_weights(z_list) else: z_min = np.min(z_sum) z_max = np.max(z_sum) if iso_level: if 0 < level_to_cut <= iso_level: barrier = iso_lines.get_iso_levels(z_sum, contour_method, iso_level)[level_to_cut - 1] else: if level_to_cut > 0: logger.warning( "Point to cut[{}] is not in iso-level[{}]. Using pie-charts without point to cut" .format(level_to_cut, iso_level)) barrier = None else: barrier = None lw_borders = [ iso_lines.get_iso_levels( i.get_density_grid(i.size, limits.x_min, limits.x_max, limits.y_min, limits.y_max)[2])[0] for i in distribution ] for k in container[0][0]: for l in container[1]: middle_point = k, l[0] input_values = [] for j in range(len(distribution)): input_values.append(distribution[j].get_density(middle_point)) if not barrier: generate_pie(ax, middle_point, input_values, angle, distances, z_min, z_max, borders, modus, colorschemes, scale, lw_borders) elif sum(input_values) > barrier: generate_pie(ax, middle_point, input_values, angle, distances, z_min, z_max, borders, modus, colorschemes, scale, lw_borders)
def input_image( ax, distributions, z_list=None, z_min=None, z_max=None, z_sum=None, colorschemes=None, method="equal_density", num_of_levels=8, color_space="lab", use_c_implementation=False, mode="hierarchic", blending_operator=hierarchic_blending_operator.porter_duff_source_over, borders=None, min_gauss=False, lower_border=None, lower_border_to_cut=0, xlim=None, ylim=None): """ inputs the contours of distributions into an matplotlib axis object :param ax: matplotlib axis :param distributions: list of :class:`contour_visualization.Distribution.Distribution` :param z_list: list of densities each one with the same shape [density_1, ... , density_n] :param z_min: minimal density occurring in the z_list min([density_1, ... , density_n]) :param z_max: maximal density occurring in the z_list max([density_1, ... , density_n]) :param colorschemes: colorschemes to use for each density-grid :param method: method with which the distance between contour-lines is chosen :param num_of_levels: number of contour-lines to use :param color_space: colorspace to merge the images in "rgb" or "lab" :param use_c_implementation: run the merging process with the c-implementation :param mode: sets the mode to use. Defaults is hierarchic and defaults to hierarchic :param blending_operator: operator with which the pictures are merged :param borders: min and max color from colorspace which is used from 0. to 1. """ limits = helper.get_limits(distributions, xlim, ylim) if z_list is None: z_list = helper.generate_distribution_grids(distributions, limits=limits) if z_min is None: z_min, z_max, z_sum = helper.generate_weights(z_list) if colorschemes is None: colorschemes = color_schemes.get_colorbrewer_schemes() img, alpha = calculate_image(z_list, z_min, z_max, z_sum, colorschemes, method, num_of_levels, color_space, use_c_implementation, mode, blending_operator, borders, min_gauss=min_gauss, lower_border=lower_border, lower_border_to_cut=lower_border_to_cut) extent = [limits.x_min, limits.x_max, limits.y_min, limits.y_max] ax.imshow(img, extent=extent, origin='lower')
def plot_image( ax, distributions, contour_lines=False, contour_line_colorscheme=color_schemes. get_background_colorbrewer_scheme(), contour_lines_method="equal_density", contour_lines_weighted=True, contour_line_level=5, contour_line_borders=None, linewidth=2, contours=False, contour_colorscheme=color_schemes.get_colorbrewer_schemes(), contour_method="equal_density", contour_lvl=8, color_space="lab", use_c_implementation=True, contour_mode="hierarchic", blending_operator=hierarchic_blending_operator.porter_duff_source_over, contour_min_gauss=False, contour_lower_border_lvl=None, contour_lower_border_to_cut=0, contour_borders=None, crosses=False, cross_colorscheme=color_schemes.get_colorbrewer_schemes(), cross_width="5%", cross_same_broad=True, cross_length_multiplier=2. * np.sqrt(2.), cross_borders=None, cross_fill=True, cross_line_width=0., cross_blending_operator=hierarchic_blending_operator. porter_duff_source_over, cross_mode="hierarchic", pie_charts=False, pie_num=25, pie_angle=90, pie_chart_colors=None, pie_chart_modus="light", pie_chart_scale=1., pie_chart_borders=None, pie_chart_iso_level=40, pie_chart_level_to_cut=1, pie_chart_contour_method="equal_density", scatter_points=False, schatter_points_colors=None, scatter_points_num=1000, legend=False, legend_lw=2, legend_colors=None, legend_names=None, xlim=None, ylim=None, title="", xlabel="", ylabel="", *args, **kwargs): """ Plots an image at a given axe, can plot contour, contour-lines, crosses and pie-charts :param ax: axis to plot on :param distributions: list of distributions to plot [distribution_1, ... distribution_n] :param contour_lines: :param contour_line_colorscheme: :param contour_lines_method: :param contour_lines_weighted: :param contour_line_level: :param contour_line_borders: :param linewidth: :param contours: :param contour_colorscheme: :param contour_method: method with which the colored areas are calculated :param contour_lvl: :param color_space: :param use_c_implementation: :param contour_mode: sets the mode to use. Defaults is hierarchic and defaults to hierarchic :param blending_operator: blendingoperator to use. Only works for blending in python :param contour_min_gauss: if min of min gauss is used when True else from z_sum :param contour_lower_border_to_cut: defines the global lower border at which to cut the particular each image :param contour_lower_border_lvl: def at which level the iso-border gets cut :param contour_borders: :param crosses: :param cross_colorscheme: :param cross_width: :param cross_same_broad: if True calculates the broad of the crosses depending by the smaller cross :param cross_length_multiplier: is multiplied with the lenght to create bigger or smaller crosses :param cross_borders: :param cross_fill: if cross is filled with color or not :param cross_blending_operator: blending operotor for cross-intersections :param pie_charts: :param pie_num: :param pie_angle: where the pie-chart begins 0 is horizontal beginning on the right 90 beginns at the top :param pie_chart_colors: Colorscheme to use. Defaults is colorbrewer :param pie_chart_modus: "light" or "size" if "size" global density is coded with size elif "light" through the colorscheme :param pie_chart_scale: when light selected sets the size of the pies :param pie_chart_borders: [0.,1.] range of ether size or color lightness of the pies :param pie_chart_iso_level: :param pie_chart_level_to_cut: :param pie_chart_contour_method: :param legend: if a legend should be plotted or not :param legend_lw: :param legend_colors: plots colors as lines to legend if not chosen defaults to contour-colors :param legend_names: if set uses names instead of numbers :param title: title specified if not given non is plotted :param xlabel: :param ylabel: :return: """ if contours or contour_lines or pie_charts or crosses or scatter_points: limits = helper.get_limits(distributions, xlim, ylim) z_list = helper.generate_distribution_grids(distributions, limits=limits) z_min, z_max, z_sum = helper.generate_weights(z_list) if not contours: if isinstance(ax, type(plt)): ax.xlim((limits.x_min, limits.x_max)) ax.ylim((limits.y_min, limits.y_max)) else: ax.set_xlim((limits.x_min, limits.x_max), emit=False) ax.set_ylim((limits.y_min, limits.y_max), emit=False) if title: if isinstance(ax, type(plt)): ax.title(title) else: ax.set_title(title) if legend: if not legend_colors: legend_colors = color_schemes.get_representiv_colors( _evaluate_colors([ _evaluate_colors( [[ contour_line_colorscheme, ] if isinstance(contour_line_colorscheme, dict) else contour_line_colorscheme, [ contour_colorscheme, ] if isinstance(contour_colorscheme, dict) else contour_colorscheme, pie_chart_colors], len(distributions)), pie_chart_colors ], len(distributions)))[:len(distributions)] _generate_legend(ax, legend_colors, legend_names, legend_lw=legend_lw) if contours: if isinstance(contour_colorscheme, dict): picture_contours.input_image( ax, [distributions[0]], [z_sum], np.min(z_sum), np.max(z_sum), z_sum, [contour_colorscheme], contour_method, contour_lvl, color_space, use_c_implementation, contour_mode, blending_operator=blending_operator, borders=contour_borders, min_gauss=contour_min_gauss, lower_border=contour_lower_border_lvl, lower_border_to_cut=contour_lower_border_to_cut, xlim=xlim, ylim=ylim) else: picture_contours.input_image( ax, distributions, z_list, z_min, z_max, z_sum, contour_colorscheme, contour_method, contour_lvl, color_space, use_c_implementation, contour_mode, blending_operator=blending_operator, borders=contour_borders, min_gauss=contour_min_gauss, lower_border=contour_lower_border_lvl, lower_border_to_cut=contour_lower_border_to_cut, xlim=xlim, ylim=ylim) if crosses: picture_cross.input_crosses( ax, distributions, z_list, z_min, z_max, cross_colorscheme, cross_width, cross_same_broad, cross_length_multiplier, cross_borders, linewidth=cross_line_width, cross_fill=cross_fill, blending_operator=cross_blending_operator, mode=cross_mode, color_space=color_space, *args, **kwargs) if contour_lines: if isinstance(contour_line_colorscheme, dict): picture_contour_lines.generate_contour_lines( ax, z_sum, limits, contour_line_colorscheme, contour_lines_method, contour_lines_weighted, contour_line_level, contour_line_borders, linewidth) else: for z_values, scheme in zip(z_list, contour_line_colorscheme): picture_contour_lines.generate_contour_lines( ax, z_values, limits, scheme, contour_lines_method, contour_lines_weighted, contour_line_level, contour_line_borders, linewidth) if pie_charts: if pie_chart_colors is None: pie_chart_colors = contour_colorscheme pie_chart_vis.input_image(ax, distributions, z_sum, pie_num, angle=pie_angle, colorschemes=pie_chart_colors, modus=pie_chart_modus, borders=pie_chart_borders, iso_level=pie_chart_iso_level, level_to_cut=pie_chart_level_to_cut, contour_method=pie_chart_contour_method, scale=pie_chart_scale, set_limit=False, xlim=xlim, ylim=ylim) if scatter_points: logger.debug("Plotting scatter points") if schatter_points_colors is None: schatter_points_colors = contour_colorscheme draw_random_points.input_points( ax, distributions, colorschemes=schatter_points_colors, num=scatter_points_num, *args, **kwargs) if isinstance(ax, type(plt)): ax.xlim((limits.x_min, limits.x_max)) ax.ylim((limits.y_min, limits.y_max)) else: ax.set_xlim((limits.x_min, limits.x_max), emit=False) ax.set_ylim((limits.y_min, limits.y_max), emit=False) # # to avoid a stretched y-axis if isinstance(ax, type(plt)): pass # ax.aspect('equal', adjustable='box') else: ax.set_aspect('equal', adjustable='box') logger.debug("Axis-limits: {}".format(limits)) if xlabel: if isinstance(ax, type(plt)): ax.xlabel(xlabel) else: ax.set_xlabel(xlabel) if ylabel: if isinstance(ax, type(plt)): ax.ylabel(ylabel) else: ax.set_ylabel(ylabel)
def test_generate_crosses_crosses(self): dist_one_first_line = [((1., -4.), (5., 4.440892098500626e-16)), ((-0.181818, -2.818181), (3.818181, 1.181818)), ((-1., -2.), (3., 2.)), ((-1.818181, -1.181818), (2.181818, 2.818181)), ((-3., -4.440892098500626e-16), (1., 4.))] dist_one_second_line = [((-1.4721359549995792, -6.47213595499958), (-5.47213595499958, -2.472135954999579)), ((1.1162821280456323, -3.8837178719543686), (-2.8837178719543686, 0.11628212804563232)), ((3.0000000000000004, -2.0000000000000004), (-1.0000000000000004, 2.0000000000000004)), ((4.883717871954369, -0.11628212804563187), (0.8837178719543681, 3.883717871954369)), ((7.47213595499958, 2.472135954999579), (3.472135954999579, 6.47213595499958))] dist_two_first_line = [ ((0.0, -7.000000000000002), (8.000000000000002, 1.0)), ((-2.363636363636364, -4.636363636363638), (5.636363636363638, 3.363636363636364)), ((-4.000000000000001, -3.000000000000001), (4.000000000000001, 5.000000000000001)), ((-5.636363636363638, -1.3636363636363642), (2.363636363636364, 6.636363636363638)), ((-8.000000000000002, 1.0), (0.0, 9.000000000000002)) ] dist_two_second_line = [((-1.6568542494923806, -8.656854249492383), (-9.656854249492383, -0.6568542494923806)), ((1.6172644221835126, -5.382735577816489), (-6.382735577816489, 2.6172644221835126)), ((4.000000000000001, -3.000000000000001), (-4.000000000000001, 5.000000000000001)), ((6.382735577816488, -0.6172644221835135), (-1.6172644221835135, 7.382735577816488)), ((9.656854249492383, 2.6568542494923806), (1.6568542494923806, 10.656854249492383))] dist_1 = Gaussian(means=[1, 0], cov_matrix=[[3, 2], [2, 3]], x_min=self.x_min, x_max=self.x_max, y_min=self.y_min, y_max=self.y_max, size=self.size, weight=self.weight) dist_2 = Gaussian(means=[0, 1], cov_matrix=[[6, 2], [2, 6]], x_min=self.x_min, x_max=self.x_max, y_min=self.y_min, y_max=self.y_max, size=self.size, weight=self.weight) distributions = [dist_1, dist_2] # unpleasant.... limits = helper.get_limits(distributions) z_list = helper.generate_distribution_grids(distributions, limits=limits) z_min, z_max, z_sum = helper.generate_weights(z_list) colorschemes = color_schemes.get_colorbrewer_schemes() crosses = picture_cross.generate_crosses(distributions, z_list, z_min, z_max, colorschemes, method="normal", num_of_levels=1) np.testing.assert_almost_equal(dist_one_first_line, crosses[0][0], decimal=5) np.testing.assert_almost_equal(dist_one_second_line, crosses[0][1], decimal=5) np.testing.assert_almost_equal(dist_two_first_line, crosses[1][0], decimal=5) np.testing.assert_almost_equal(dist_two_second_line, crosses[1][1], decimal=5)