| """ |
| Image Processing Module - Utilities for parsing and analyzing matplotlib plots. |
| |
| The module exposes the ``Plotprocess`` class, which provides tools for: |
| - Identifying plot types (pie, bar, scatter, heatmap, kde, violin, line), including |
| step histograms (``histtype='step'/'stepfilled'``) detected via Polygon patches |
| - Extracting plot data and visual parameters (colors, titles, axis labels, tick |
| labels, legend labels) through per-type ``parse_*`` methods |
| - Processing single or multiple subplots via ``plot_process``, which filters out |
| colorbar/inset axes, saves extracted data to a ``.npy`` array, and writes figure |
| parameters to a ``.json`` file |
| |
| Reference: https://github.com/yiyihum/da-code/tree/main/da_agent/configs/scripts/image.py |
| """ |
|
|
| import matplotlib.pyplot as plt |
| import json |
| import os |
| import random, string |
| import numpy as np |
| import matplotlib.colors as mcolors |
| from matplotlib.image import AxesImage |
| from matplotlib.patches import Wedge, Rectangle, Polygon |
| from matplotlib.collections import PathCollection, QuadMesh, PolyCollection, LineCollection |
| from matplotlib.contour import QuadContourSet |
|
|
| class Plotprocess: |
| |
| @classmethod |
| def identify_plot_type(cls, ax): |
| |
| for patch in ax.patches: |
| if isinstance(patch, Wedge): |
| return 'pie' |
| |
| for patch in ax.patches: |
| if isinstance(patch, Rectangle) and patch.get_width() != patch.get_height(): |
| return 'bar' |
| |
| for collection in ax.collections: |
| if isinstance(collection, PathCollection) and len(collection.get_offsets()) > 0: |
| return 'scatter' |
| |
| for collection in ax.collections: |
| if isinstance(collection, QuadMesh): |
| return 'heatmap' |
| |
| if any(isinstance(c, QuadContourSet) for c in ax.collections): |
| return 'kde' |
| |
| |
| has_poly = any(isinstance(c, PolyCollection) for c in ax.collections) |
| if any(isinstance(c, LineCollection) for c in ax.collections) and not has_poly: |
| return 'kde' |
| |
| if any(hasattr(c, "get_paths") for c in ax.collections) and len(ax.collections) > 0: |
| lines = ax.get_lines() |
| if len(lines) == 0 and not Plotprocess.is_symmetric_poly(ax): |
| |
| return 'kde' |
| flag = True |
| for line in ax.get_lines(): |
| if len(line.get_xdata()) > 5 and len(line.get_ydata()) > 5: |
| flag = False |
| if flag: |
| return 'violin' |
| |
| lines = ax.get_lines() |
| for line in lines: |
| if len(line.get_xdata()) > 1 and len(line.get_ydata()) > 1: |
| return 'line' |
| |
| for patch in ax.patches: |
| if isinstance(patch, Polygon) and cls._get_step_hist_heights(patch): |
| return 'bar' |
| |
| for image in ax.images: |
| if isinstance(image, AxesImage): |
| data = image.get_array() |
| if data is not None and np.asarray(data).ndim >= 2: |
| return 'heatmap' |
| return '' |
| |
| @classmethod |
| def is_symmetric_poly(cls, ax): |
| for c in ax.collections: |
| if hasattr(c, "get_paths"): |
| for path in c.get_paths(): |
| verts = path.vertices |
| x = verts[:, 0] |
|
|
| mid = np.mean(x) |
| left = x[x < mid] |
| right = x[x > mid] |
|
|
| if len(left) == 0 or len(right) == 0: |
| continue |
|
|
| if len(left) == len(right) and np.allclose( |
| np.sort(mid - left), |
| np.sort(right - mid), |
| atol=1e-1 |
| ): |
| return True |
| return False |
| |
| @classmethod |
| def is_numeric(cls, arr): |
| if arr is None: |
| return False |
| arr = np.asarray(arr) |
| if arr.size == 0: |
| return False |
| if not np.issubdtype(arr.dtype, np.number): |
| return False |
| return True |
| |
| @classmethod |
| def parse_bar(cls, ax): |
| result_data = {'width': [], 'height': []} |
| colors = set() |
| results = [] |
| |
| for patch in ax.patches: |
| if isinstance(patch, Rectangle): |
| width, height = patch.get_width(), patch.get_height() |
| result_data['width'].append(width) |
| result_data['height'].append(height) |
| color = patch.get_facecolor() if isinstance(patch.get_facecolor(), str) \ |
| else tuple(patch.get_facecolor()) |
| colors.add(color) |
| |
| data_type = max(result_data, key=lambda k: len(set(result_data[k]))) |
| coord_type = 'x' if data_type == 'height' else 'y' |
| last_coord = -1000 |
| result = [] |
| |
| for patch in ax.patches: |
| if not isinstance(patch, Rectangle): |
| continue |
| |
| width = patch.get_width() if data_type == 'height' else patch.get_height() |
| |
| if width == 0: |
| continue |
| |
| coord = patch.get_x() if coord_type == 'x' else patch.get_y() |
| |
| if coord < last_coord: |
| results.append(result) |
| result = [] |
| |
| result.append(patch.get_height() if data_type == 'height' else patch.get_width()) |
| |
| last_coord = coord |
| |
| if result: |
| results.append(result) |
|
|
| for patch in ax.patches: |
| if not isinstance(patch, Polygon): |
| continue |
|
|
| heights = cls._get_step_hist_heights(patch) |
| if not heights: |
| continue |
|
|
| results.append(heights) |
| facecolor = patch.get_facecolor() |
| edgecolor = patch.get_edgecolor() |
| color = edgecolor if facecolor[-1] == 0 else facecolor |
| colors.add(tuple(color)) |
|
|
| return results, colors |
| |
| @classmethod |
| def parse_line(cls, ax): |
| colors = set() |
| results = [] |
| lines = ax.get_lines() |
| for line in lines: |
| xdata, ydata = line.get_xdata(), line.get_ydata() |
| |
| if len(xdata) > 1 and len(ydata) > 1: |
| |
| if not cls.is_numeric(ydata): |
| continue |
| if np.isnan(ydata).all(): |
| continue |
| |
| results.append(ydata) |
| color = line.get_color() if isinstance(line.get_color(), str) \ |
| else tuple(line.get_color()) |
| colors.add(color) |
| |
| |
| return results, colors |
| @classmethod |
| def parse_pie(cls, ax): |
| result = [] |
| colors = set() |
| for patch in ax.patches: |
| if isinstance(patch, Wedge): |
| sector_proportion = abs(patch.theta2 - patch.theta1) / 360 |
| result.append(sector_proportion) |
| color = patch.get_facecolor() if isinstance(patch.get_facecolor(), str)\ |
| else tuple(patch.get_facecolor()) |
| colors.add(color) |
| |
| |
| return [result], colors |
| |
| @classmethod |
| def parse_scatter(cls, ax): |
| result = [] |
| colors = set() |
| scatters = [child for child in ax.get_children() if isinstance(child, PathCollection) and len(child.get_offsets()) > 0] |
| for scatter in scatters: |
| scatter_data = scatter.get_offsets() |
| scatter_data = scatter_data.reshape(-1, 1) if scatter_data.ndim == 1 else scatter_data |
| for data in scatter_data: |
| result.append(data) |
| scatter_colors = scatter.get_facecolor() |
| for color in scatter_colors: |
| color = color if isinstance(color, str) else tuple(color) |
| colors.add(color) |
| |
| return result, colors |
|
|
| @classmethod |
| def parse_heatmap(cls, ax): |
| results = [] |
| colors = set() |
|
|
| for collection in ax.collections: |
| if isinstance(collection, QuadMesh): |
| data = collection.get_array() |
| if data is not None: |
| results.append(data) |
|
|
| cmap = collection.cmap |
| colors.add(str(cmap.name)) |
|
|
| for image in ax.images: |
| if isinstance(image, AxesImage): |
| data = image.get_array() |
| if data is not None: |
| results.append(data) |
|
|
| cmap = image.get_cmap() |
| if cmap is not None: |
| colors.add(str(cmap.name)) |
| return results, colors |
| |
| @classmethod |
| def parse_violin(cls, ax): |
| results = [] |
| colors = set() |
|
|
| for collection in ax.collections: |
| paths = collection.get_paths() |
| for path in paths: |
| vertices = path.vertices |
| results.append(vertices[:, 1]) |
|
|
| facecolors = collection.get_facecolor() |
| for color in facecolors: |
| color = color if isinstance(color, str) else tuple(color) |
| colors.add(color) |
|
|
| return results, colors |
| |
| @classmethod |
| def parse_kde(cls, ax): |
| all_vertices = [] |
| colors = set() |
| for collection in ax.collections: |
| if isinstance(collection, QuadContourSet): |
| facecolors = collection.get_facecolor() |
| for color in facecolors: |
| color = color if isinstance(color, str) else tuple(color) |
| colors.add(color) |
| for path in collection.get_paths(): |
| all_vertices.append(path.vertices) |
| |
| results = [v for vertices in all_vertices for v in vertices.tolist()] |
| return results, colors |
|
|
| @classmethod |
| def handle_result(cls, results): |
| try: |
| results = np.array(results) if results else np.array([]) |
| except Exception as e: |
| max_length = max(len(x) for x in results) |
| results = [np.pad(x, (0, max_length - len(x)), 'constant') for x in results] |
| results = np.array(results) |
|
|
| return results |
| |
| @classmethod |
| def generate_random_string(cls, length=4): |
| letters = string.ascii_letters |
| return ''.join(random.choice(letters) for _ in range(length)) |
|
|
| @staticmethod |
| def _get_step_hist_heights(patch): |
| try: |
| xy = np.asarray(patch.get_xy()) |
| except Exception: |
| return [] |
|
|
| if xy.ndim != 2 or xy.shape[0] < 4 or xy.shape[1] < 2: |
| return [] |
|
|
| x = xy[:, 0] |
| y = xy[:, 1] |
| dx = np.diff(x) |
| dy = np.diff(y) |
|
|
| |
| if not np.all(np.isclose(dx, 0) | np.isclose(dy, 0)): |
| return [] |
|
|
| baseline = y[0] |
| heights = [] |
| for idx in range(len(dx)): |
| if np.isclose(dx[idx], 0) or not np.isclose(dy[idx], 0): |
| continue |
| if np.isclose(y[idx], baseline): |
| continue |
| heights.append(y[idx]) |
|
|
| return heights |
| |
| @staticmethod |
| def _is_colorbar_axes(ax): |
| if ax.get_label() == '<colorbar>': |
| return True |
| if getattr(ax, '_colorbar', None) is not None: |
| return True |
| return False |
|
|
| @staticmethod |
| def _is_inset_axes(ax): |
| locator = ax.get_axes_locator() |
| if locator is None: |
| return False |
| try: |
| subplotspec = ax.get_subplotspec() |
| except Exception: |
| subplotspec = None |
| return subplotspec is None |
|
|
| @classmethod |
| def plot_process(cls, fig, image_file_name): |
| """处理单个或多个子图的图形""" |
| axes = [ |
| ax for ax in fig.get_axes() |
| if not cls._is_colorbar_axes(ax) and not cls._is_inset_axes(ax) |
| ] |
|
|
| if not axes: |
| return None |
|
|
| all_parameters = {} |
| all_results = {} |
| for idx, ax in enumerate(axes): |
| ax_params = {} |
| gt_graph = cls.identify_plot_type(ax) |
| if not gt_graph: |
| continue |
|
|
| parse_func = getattr(cls, f"parse_{gt_graph}", None) |
| if not parse_func: |
| continue |
|
|
| results, colors = parse_func(ax) |
| results = cls.handle_result(results) |
| colors = [c if isinstance(c, str) else str(mcolors.to_hex(c)) for c in colors] |
| legend = ax.get_legend() |
| graph_title = ax.get_title() if ax.get_title() else '' |
| legend_title = legend.get_title().get_text() if legend and legend.get_title() else '' |
| labels = [text.get_text() for text in legend.get_texts()] if legend else [] |
| x_label = ax.get_xlabel() if ax.get_xlabel() else '' |
| y_label = ax.get_ylabel() if ax.get_ylabel() else '' |
| xtick_labels = [label.get_text() for label in ax.get_xticklabels()] |
| ytick_labels = [label.get_text() for label in ax.get_yticklabels()] |
|
|
| ax_params['type'] = gt_graph |
| ax_params['color'] = colors |
| ax_params['graph_title'] = graph_title |
| ax_params['legend_title'] = legend_title |
| ax_params['labels'] = labels |
| ax_params['x_label'] = x_label |
| ax_params['y_label'] = y_label |
| ax_params['xtick_labels'] = xtick_labels |
| ax_params['ytick_labels'] = ytick_labels |
|
|
| |
| if len(results) > 0: |
| all_results[f'subplot_{idx}'] = results |
|
|
| all_parameters[f'subplot_{idx}'] = ax_params |
|
|
| |
| if all_results: |
| npy_path = os.path.splitext(image_file_name)[0] + '.npy' |
| np.save(npy_path, all_results) |
|
|
| |
| fig_size = fig.get_size_inches() |
| all_parameters['figsize'] = list(fig_size) |
| all_parameters['total_subplots'] = len(axes) |
|
|
| output_path = os.path.splitext(image_file_name)[0] + '.json' |
| with open(output_path, 'w') as js: |
| json.dump(all_parameters, js) |
|
|
| |
| |
|
|