File size: 2,779 Bytes
99a0c40
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import json
from pathlib import Path


class PlotRegistry:
    def __init__(self, json_path: str):
        self.path = Path(json_path)
        with open(self.path, "r", encoding="utf-8") as f:
            self.registry = json.load(f)

    # -------------------------------------------------
    # Registry helpers
    # -------------------------------------------------
    def get_groups(self):
        return list(self.registry.keys())

    def get_plots_in_group(self, group: str):
        return self.registry[group]

    def get_plot(self, group: str, plot_key: str):
        return self.registry[group][plot_key]

    # -------------------------------------------------
    # Code generation (FINAL)
    # -------------------------------------------------
    def generate_code(
        self,
        group: str,
        plot_key: str,
        variables: dict,
        user_options: dict,
        figsize=(8, 5),
        title: str = "",
        xlabel: str = "",
        ylabel: str = "",
    ):
        plot = self.get_plot(group, plot_key)
        # Template artık sadece fonksiyon adını içerebilir: "sns.scatterplot"
        # Ancak mevcut JSON yapınızı bozmamak için string manipülasyonu yapacağız.
        base_func = plot["code_template"].split("(")[0] 

        # -----------------------------
        # Dinamik Argüman Oluşturma
        # -----------------------------
        exec_args = ["data=df", "ax=ax"]
        display_args = ["data=df"]

        # 1. Zorunlu ve Opsiyonel Inputlar (x, y, hue, size vb.)
        for name, value in variables.items():
            if value is not None:
                exec_args.append(f"{name}={name}")
                display_args.append(f"{name}='{value}'")

        # 2. Opsiyonel Ayarlar (alpha, bins, kde vb.)
        for k, v in user_options.items():
            if v not in ("", None):
                exec_args.append(f"{k}={k}")
                if isinstance(v, str):
                    display_args.append(f"{k}={repr(v)}")
                else:
                    display_args.append(f"{k}={v}")

        # -----------------------------
        # Final Kod Oluşturma
        # -----------------------------
        exec_plot_line = f"{base_func}({', '.join(exec_args)})"
        display_plot_line = f"{base_func}({', '.join(display_args)})"

        exec_code = f"""
# --- plotting ---
{exec_plot_line}
ax.set_title({repr(title)})
ax.set_xlabel({repr(xlabel)})
ax.set_ylabel({repr(ylabel)})
"""
        display_code = f"""
import matplotlib.pyplot as plt
import seaborn as sns

fig, ax = plt.subplots(figsize={figsize})
{display_plot_line}
ax.set_title({repr(title)})
ax.set_xlabel({repr(xlabel)})
ax.set_ylabel({repr(ylabel)})
plt.show()
""".strip()

        return exec_code.strip(), display_code