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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
"""
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):
        # Check for pie plots
        for patch in ax.patches:
            if isinstance(patch, Wedge):
                return 'pie'
        # Check for bar plots
        for patch in ax.patches:
            if isinstance(patch, Rectangle) and patch.get_width() != patch.get_height():
                return 'bar'
        # Check for scatter plots
        for collection in ax.collections:
            if isinstance(collection, PathCollection) and len(collection.get_offsets()) > 0:
                return 'scatter'
        # heatmap (sns.heatmap)
        for collection in ax.collections:
            if isinstance(collection, QuadMesh):
                return 'heatmap'
        # kde plot (sns.kdeplot with fill=True, 2D uses QuadContourSet)
        if any(isinstance(c, QuadContourSet) for c in ax.collections):
            return 'kde'
        # In newer matplotlib/seaborn, 2D KDE contour lines are stored as LineCollection
        # Only treat as kde if no PolyCollection present (violinplot also uses LineCollection)
        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'
        # PolyCollection-based plots: distinguish filled KDE from violin
        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):
                # PolyCollection with no lines → filled 1D KDE (sns.kdeplot fill=True)
                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'
        # Check for line plots
        lines = ax.get_lines()
        for line in lines:
            if len(line.get_xdata()) > 1 and len(line.get_ydata()) > 1:
                return 'line'
        # Step histograms (histtype='step'/'stepfilled') are stored as Polygon patches.
        for patch in ax.patches:
            if isinstance(patch, Polygon) and cls._get_step_hist_heights(patch):
                return 'bar'
        # heatmap/image matrix (plt.imshow, ax.imshow, matshow). 
        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()  # Initialize colors set
        results = []  # Initialize the results list
        # Collect width and height data from Rectangle patches
        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)
        # Determine which dimension has the most variety to identify orientation
        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 = []
        # Loop through patches and group based on coordinates
        for patch in ax.patches:
            if not isinstance(patch, Rectangle):
                continue
            # Get the relevant dimension based on the identified data_type
            width = patch.get_width() if data_type == 'height' else patch.get_height()
            # Skip patches with zero width/height
            if width == 0:
                continue
            # Determine the current coordinate based on the type of data (x or y)
            coord = patch.get_x() if coord_type == 'x' else patch.get_y()
            # If the current coordinate is smaller than the previous one, start a new group
            if coord < last_coord:
                results.append(result)
                result = []
            # Append the relevant height or width to the current group
            result.append(patch.get_height() if data_type == 'height' else patch.get_width())
            # Update the last coordinate for comparison in the next iteration
            last_coord = coord
        # Append the final result group if it exists
        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()  # Initialize the set to store colors
        results = []  # Initialize results list
        lines = ax.get_lines()  # Get the lines from the axes
        for line in lines:
            xdata, ydata = line.get_xdata(), line.get_ydata()
            # Ensure that both x and y have more than 1 data point
            if len(xdata) > 1 and len(ydata) > 1:
                # Check if xdata and ydata are numeric, skip if not
                if not cls.is_numeric(ydata):
                    continue
                if np.isnan(ydata).all():
                    continue
                # Append the ydata to results
                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)
        # flatten to a single list of [x, y] points
        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)

        # Matplotlib step histograms are axis-aligned polygons.
        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

        # 保存所有子图数据到一个npy文件
        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)

# fig = plt.gcf()
# Plotprocess.plot_process(fig)