birl.utilities.drawing module¶
Function for drawing and visualisations
Copyright (C) 2017-2019 Jiri Borovec <jiri.borovec@fel.cvut.cz>
- class birl.utilities.drawing.RadarChart(df, steps=5, fig=None, rect=None, fill_alpha=0.05, colors='nipy_spectral', *args, **kwargs)[source]¶
Bases:
object
>>> import pandas as pd >>> df = pd.DataFrame(np.random.random((5, 3)), columns=list('abc')) >>> RadarChart(df) <...>
draw a dataFrame with scaled axis
- Parameters
- __draw_curve(idx, row, fill_alpha=0.05, *args, **kw)[source]¶
draw particular curve
- Parameters
idx (str) – name
row – data with values
fill_alpha – transparency of filled region
args – optional arguments
kw – optional key arguments
- birl.utilities.drawing._list_colors(colors, nb)[source]¶
sample color space
- Parameters
colors (str|list) –
nb (int) –
- Return list
>>> _list_colors('jet', 2) [(0.0, 0.0, 0.5, 1.0), (0.5, 0.0, 0.0, 1.0)] >>> _list_colors(plt.cm.jet, 3) [(0.0, 0.0, 0.5, 1.0), (0.0, 0.0, 0.5..., 1.0), (0.0, 0.0, 0.5..., 1.0)] >>> _list_colors([(255, 0, 0), (0, 255, 0)], 1) [(255, 0, 0), (0, 255, 0)]
- birl.utilities.drawing.create_figure(im_size, figsize_max=18)[source]¶
create an empty figure of image size maximise maximal size
>>> fig, ax = create_figure((100, 150)) >>> isinstance(fig, plt.Figure) True
- birl.utilities.drawing.draw_heatmap(data, row_labels=None, col_labels=None, ax=None, cbar_kw=None, cbar_label='', **kwargs)[source]¶
Create a draw_heatmap from a numpy array and two lists of labels.
- Parameters
data – A 2D numpy array of shape (N,M)
row_labels – A list or array of length N with the labels for the rows
col_labels – A list or array of length M with the labels for the columns
ax – A matplotlib.axes.Axes instance to which the draw_heatmap is plotted. If not provided, use current axes or create a new one.
cbar_kw – A dictionary with arguments to
matplotlib.Figure.colorbar()
.cbar_label – The label for the colorbar
- birl.utilities.drawing.draw_image_points(image, points, color='green', marker_size=5, shape='o')[source]¶
draw marker in the image and add to each landmark its index
- Parameters
- Returns
np.ndarray
>>> image = np.zeros((10, 10, 3)) >>> points = np.array([[9, 1], [2, 2], [5, 5]]) >>> img = draw_image_points(image, points, marker_size=1, shape='s') >>> img.shape == (10, 10, 3) # Windows x64 returns (10L, 10L, 3L) True >>> np.round(img[:, :, 1], 2) array([[ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0.5, 0.5], [ 0. , 0.5, 0.5, 0.5, 0. , 0. , 0. , 0. , 0.5, 0. ], [ 0. , 0.5, 0. , 0.5, 0. , 0. , 0. , 0. , 0.5, 0.5], [ 0. , 0.5, 0.5, 0.5, 0. , 0. , 0. , 0. , 0. , 0. ], [ 0. , 0. , 0. , 0. , 0.5, 0.5, 0.5, 0. , 0. , 0. ], [ 0. , 0. , 0. , 0. , 0.5, 0. , 0.5, 0. , 0. , 0. ], [ 0. , 0. , 0. , 0. , 0.5, 0.5, 0.5, 0. , 0. , 0. ], [ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ], [ 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. , 0. ]]) >>> img = draw_image_points(None, points, marker_size=1)
- birl.utilities.drawing.draw_images_warped_landmarks(image_target, image_source, points_init, points_target, points_warped, fig_size_max=18)[source]¶
composed form several functions - images overlap + landmarks + legend
- Parameters
image_target (ndarray) – np.array<height, with, dim>
image_source (ndarray) – np.array<height, with, dim>
points_target (ndarray) – np.array<nb_points, dim>
points_init (ndarray) – np.array<nb_points, dim>
points_warped (ndarray) – np.array<nb_points, dim>
fig_size_max (float) – maximal figure size for major image dimension
- Returns
object
>>> image = np.random.random((50, 50, 3)) >>> points = np.array([[20, 30], [40, 10], [15, 25], [5, 50], [10, 60]]) >>> fig = draw_images_warped_landmarks(image, 1 - image, points, points + 1, points - 1) >>> isinstance(fig, plt.Figure) True >>> fig = draw_images_warped_landmarks(None, None, points, points + 1, points - 1) >>> isinstance(fig, plt.Figure) True >>> draw_images_warped_landmarks(image, None, points, points + 1, points - 1) <...> >>> draw_images_warped_landmarks(None, image, points, points + 1, points - 1) <...>
- birl.utilities.drawing.draw_landmarks_origin_target_warped(ax, points_origin, points_target, points_warped=None, marker='o')[source]¶
visualisation of transforming points, presenting 3 set of points: original points, targeting points, and the estimate of target points
scenario 1: original - moving landmarks target - reference landmarks estimate - transformed landmarks
scenario 2: original - reference landmarks target - moving landmarks estimate - transformed landmarks
- Parameters
ax – matplotlib figure
points_origin (ndarray) – np.array<nb_points, dim>
points_target (ndarray) – np.array<nb_points, dim>
points_warped (ndarray) – np.array<nb_points, dim>
marker (str) – set the marker shape
>>> points = np.array([[20, 30], [40, 10], [15, 25]]) >>> draw_landmarks_origin_target_warped(plt.figure().gca(), ... points, points + 1, points - 1)
- birl.utilities.drawing.draw_matrix_user_ranking(df_stat, higher_better=False, fig=None, cmap='tab20')[source]¶
show matrix as image, sorted per column and unique colour per user
- Parameters
- Return Figure
>>> import pandas as pd >>> df = pd.DataFrame(np.random.random((5, 3)), columns=list('abc')) >>> draw_matrix_user_ranking(df) <...>
- birl.utilities.drawing.draw_scatter_double_scale(df, colors='nipy_spectral', ax_decs=None, idx_markers=('o', 'd'), xlabel='', figsize=None, legend_style=None, plot_style=None, x_spread=(0.4, 5))[source]¶
Draw a scatter with double scales on left and right
- Parameters
df (DF) – dataframe
cmap (func) – color mapping
ax_decs (dict) – dictionary with names of left and right axis
idx_markers (tuple) –
xlabel (str) – title of x axis
legend_style (dict) – legend configuration
plot_style (dict) – extra plot configuration
x_spread (tuple(float,int)) – range of spreads and number of samples
- Return tuple
figure and both axis
>>> import pandas as pd >>> df = pd.DataFrame(np.random.random((10, 3)), columns=['col1', 'col2', 'col3']) >>> fig, axs = draw_scatter_double_scale(df, ax_decs={'name': None}, xlabel='X') >>> axs {...} >>> # just the selected columns >>> fig, axs = draw_scatter_double_scale(df, ax_decs={'name1': ['col1', 'col2'], ... 'name2': ['col3']}) >>> fig <...> >>> # for the "name2" use all remaining columns >>> fig, axs = draw_scatter_double_scale(df, ax_decs={'name1': ['col1', 'col2'], ... 'name2': None}) >>> fig <...>
- birl.utilities.drawing.effective_decimals(num)[source]¶
find the first effective decimal
- Parameters
num (float) – number
- Return int
number of the first effective decimals
- birl.utilities.drawing.export_figure(path_fig, fig)[source]¶
export the figure and close it afterwords
- Parameters
path_fig (str) – path to the new figure image
fig – object
>>> path_fig = './sample_figure.jpg' >>> export_figure(path_fig, plt.figure()) >>> os.remove(path_fig)
- birl.utilities.drawing.overlap_two_images(image1, image2, transparent=0.5)[source]¶
merge two images together with transparency level
- Parameters
image1 (ndarray) – np.array<height, with, dim>
image2 (ndarray) – np.array<height, with, dim>
transparent (float) – level ot transparency in range (0, 1) with 1 to see only first image nad 0 to see the second one
- Returns
np.array<height, with, dim>
>>> img1 = np.ones((5, 6, 1)) * 0.2 >>> img2 = np.ones((6, 5, 1)) * 0.8 >>> overlap_two_images(img1, img2, transparent=0.5)[:, :, 0] array([[ 0.5, 0.5, 0.5, 0.5, 0.5, 0.1], [ 0.5, 0.5, 0.5, 0.5, 0.5, 0.1], [ 0.5, 0.5, 0.5, 0.5, 0.5, 0.1], [ 0.5, 0.5, 0.5, 0.5, 0.5, 0.1], [ 0.5, 0.5, 0.5, 0.5, 0.5, 0.1], [ 0.4, 0.4, 0.4, 0.4, 0.4, 0. ]])