masskit.utils.textalloc package

Submodules

masskit.utils.textalloc.candidates module

masskit.utils.textalloc.candidates.generate_candidates(w: float, h: float, x: float, y: float, xmindistance: float, ymindistance: float, xmaxdistance: float, ymaxdistance: float, nbr_candidates: int) ndarray

Generates 36 candidate boxes

Args:

w (float): width of box h (float): height of box x (float): xmin of box y (float): ymin of box xmindistance (float): fraction of the x-dimension to use as margins for text bboxes ymindistance (float): fraction of the y-dimension to use as margins for text bboxes xmaxdistance (float): fraction of the x-dimension to use as max distance for text bboxes ymaxdistance (float): fraction of the y-dimension to use as max distance for text bboxes nbr_candidates (int): nbr of candidates to use. If <1 or >36 uses all 36

Returns:

np.ndarray: candidate boxes array

masskit.utils.textalloc.non_overlapping_boxes module

masskit.utils.textalloc.non_overlapping_boxes.get_non_overlapping_boxes(original_boxes: ndarray, xlims: Tuple[float, float], ylims: Tuple[float, float], margin: float, min_distance: float, max_distance: float, verbose: bool, nbr_candidates: int, draw_all: bool, scatter_xy: ndarray = None, lines_xyxy: ndarray = None) Tuple[List[Tuple[float, float, float, float, str, int]], List[int]]

Finds boxes that do not have an overlap with any other objects.

Args:

original_boxes (np.ndarray): original boxes containing texts. xlims (Tuple[float, float]): x-limits of plot gotten from ax.get_ylim() ylims (Tuple[float, float]): y-limits of plot gotten from ax.get_ylim() margin (float): parameter for margins between objects. Increase for larger margins to points and lines. min_distance (float): parameter for max distance between text and origin. max_distance (float): parameter for max distance between text and origin. verbose (bool): prints progress using tqdm. nbr_candidates (int): Sets the number of candidates used. draw_all (bool): Draws all texts after allocating as many as possible despit overlap. scatter_xy (np.ndarray, optional): 2d array of scattered points in plot. lines_xyxy (np.ndarray, optional): 2d array of line segments in plot.

Returns:

Tuple[List[Tuple[float, float, float, float, str, int]], List[int]]: data of non-overlapping boxes and indices of overlapping boxes.

masskit.utils.textalloc.overlap_functions module

masskit.utils.textalloc.overlap_functions.ccw(x1y1: ndarray, x2y2: ndarray, x3y3: ndarray, cand: bool) ndarray

CCW used in line intersect

Args:

x1y1 (np.ndarray): x2y2 (np.ndarray): x3y3 (np.ndarray): cand (bool): using candidate positions (different broadcasting)

Returns:

np.ndarray:

masskit.utils.textalloc.overlap_functions.inside_plot(xmin_bound: float, ymin_bound: float, xmax_bound: float, ymax_bound: float, candidates: ndarray) ndarray

Finds candidates that are inside the plot bounds

Args:

xmin_bound (float): ymin_bound (float): xmax_bound (float): ymax_bound (float): candidates (np.ndarray): candidate boxes

Returns:

np.ndarray: Boolean array of shape (K,) with True for non-overlapping candidates with boxes.

masskit.utils.textalloc.overlap_functions.line_intersect(cand_xyxy: ndarray, lines_xyxy: ndarray) ndarray

Checks if line segments intersect for all line segments and candidates.

Args:

cand_xyxy (np.ndarray): line segments in candidates lines_xyxy (np.ndarray): line segments plotted

Returns:

np.ndarray: Boolean array with True for non-overlapping candidate segments with line segments.

masskit.utils.textalloc.overlap_functions.non_overlapping_with_boxes(box_arr: ndarray, candidates: ndarray, xmargin: float, ymargin: float) ndarray

Finds candidates not overlapping with allocated boxes.

Args:

box_arr (np.ndarray): array with allocated boxes candidates (np.ndarray): candidate boxes xmargin (float): fraction of the x-dimension to use as margins for text boxes ymargin (float): fraction of the y-dimension to use as margins for text boxes

Returns:

np.ndarray: Boolean array of shape (K,) with True for non-overlapping candidates with boxes.

masskit.utils.textalloc.overlap_functions.non_overlapping_with_lines(lines_xyxy: ndarray, candidates: ndarray, xmargin: float, ymargin: float) ndarray

Finds candidates not overlapping with lines

Args:

lines_xyxy (np.ndarray): line segments candidates (np.ndarray): candidate boxes xmargin (float): fraction of the x-dimension to use as margins for text boxes ymargin (float): fraction of the y-dimension to use as margins for text boxes

Returns:

np.ndarray: Boolean array of shape (K,) with True for non-overlapping candidates with lines.

masskit.utils.textalloc.overlap_functions.non_overlapping_with_points(scatter_xy: ndarray, candidates: ndarray, xmargin: float, ymargin: float) ndarray

Finds candidates not overlapping with points.

Args:

scatter_xy (np.ndarray): Array of shape (N,2) containing coordinates for all scatter-points candidates (np.ndarray): Array of shape (K,4) with K candidate boxes xmargin (float): fraction of the x-dimension to use as margins for text boxes ymargin (float): fraction of the y-dimension to use as margins for text boxes

Returns:

np.ndarray: Boolean array of shape (K,) with True for non-overlapping candidates with points

Module contents

masskit.utils.textalloc.allocate_text(ax, x: Union[ndarray, List[float]], y: Union[ndarray, List[float]], text_list: List[str], x_scatter: Union[ndarray, List[float]] = None, y_scatter: Union[ndarray, List[float]] = None, x_lines: List[Union[ndarray, List[float]]] = None, y_lines: List[Union[ndarray, List[float]]] = None, textsize: int = 10, margin: float = 0.01, min_distance: float = 0.015, max_distance: float = 0.07, verbose: bool = False, draw_lines: bool = True, linecolor: str = 'r', draw_all: bool = True, nbr_candidates: int = 100, linewidth: float = 1, textcolor: str = 'k', ylims=None)

Main function of allocating text-boxes in matplotlib plot

Args:

fig (_type_): matplotlib figure used for rendering textbox-sizes. ax (_type_): matplotlib axes used for plotting. x (Union[np.ndarray, List[float]]): x-coordinates of texts 1d array/list. y (Union[np.ndarray, List[float]]): y-coordinates of texts 1d array/list. text_list (List[str]): list of texts. x_scatter (Union[np.ndarray, List[float]], optional): x-coordinates of all scattered points in plot 1d array/list. Defaults to None. y_scatter (Union[np.ndarray, List[float]], optional): y-coordinates of all scattered points in plot 1d array/list. Defaults to None. x_lines (List[Union[np.ndarray, List[float]]], optional): x-coordinates of all lines in plot list of 1d arrays/lists. Defaults to None. y_lines (List[Union[np.ndarray, List[float]]], optional): y-coordinates of all lines in plot list of 1d arrays/lists. Defaults to None. textsize (int, optional): size of text. Defaults to 10. margin (float, optional): parameter for margins between objects. Increase for larger margins to points and lines. Defaults to 0.01. min_distance (float, optional): parameter for min distance between text and origin. Defaults to 0.015. max_distance (float, optional): parameter for max distance between text and origin. Defaults to 0.07. verbose (bool, optional): prints progress using tqdm. Defaults to False. draw_lines (bool, optional): draws lines from original points to textboxes. Defaults to True. linecolor (str, optional): color code of the lines between points and text-boxes. Defaults to “r”. draw_all (bool, optional): Draws all texts after allocating as many as possible despit overlap. Defaults to True. nbr_candidates (int, optional): Sets the number of candidates used. Defaults to 0. linewidth (float, optional): width of line. Defaults to 1. textcolor (str, optional): color code of the text. Defaults to “k”.

masskit.utils.textalloc.find_nearest_point_on_box(xmin: float, ymin: float, w: float, h: float, x: float, y: float) Tuple[float, float]

Finds nearest point on box from point. Returns None,None if point inside box

Args:

xmin (float): xmin of box ymin (float): ymin of box w (float): width of box h (float): height of box x (float): x-coordinate of point y (float): y-coordinate of point

Returns:

Tuple[float, float]: x,y coordinate of nearest point

masskit.utils.textalloc.lines_to_segments(x_lines: List[ndarray], y_lines: List[ndarray]) ndarray

Sets up

Args:

x_lines (List[np.ndarray]): x-coordinates of all lines in plot list of 1d arrays y_lines (List[np.ndarray]): y-coordinates of all lines in plot list of 1d arrays

Returns:

np.ndarray: 2d array of line segments