rairo commited on
Commit
3df5109
·
verified ·
1 Parent(s): 6ab7a27

Update sozo_gen.py

Browse files
Files changed (1) hide show
  1. sozo_gen.py +25 -0
sozo_gen.py CHANGED
@@ -172,7 +172,32 @@ class ChartGenerator:
172
  # In the new architecture, we cannot fall back to a placeholder. We must fail.
173
  raise ValueError(f"Failed to generate a valid chart specification for '{description}'.") from e
174
 
 
175
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
176
  # --- Main Business Logic Functions ---
177
 
178
  # In sozo_gen.py
 
172
  # In the new architecture, we cannot fall back to a placeholder. We must fail.
173
  raise ValueError(f"Failed to generate a valid chart specification for '{description}'.") from e
174
 
175
+ # PASTE THIS CODE INTO YOUR sozo_gen.py FILE
176
 
177
+ def execute_chart_spec(spec: ChartSpecification, df: pd.DataFrame, output_path: Path) -> bool:
178
+ try:
179
+ plot_data = prepare_plot_data(spec, df)
180
+ fig, ax = plt.subplots(figsize=(12, 8)); plt.style.use('default')
181
+ if spec.chart_type == "bar": ax.bar(plot_data.index.astype(str), plot_data.values, color='#2E86AB', alpha=0.8); ax.set_xlabel(spec.x_col); ax.set_ylabel(spec.y_col); ax.tick_params(axis='x', rotation=45)
182
+ elif spec.chart_type == "pie": ax.pie(plot_data.values, labels=plot_data.index, autopct='%1.1f%%', startangle=90); ax.axis('equal')
183
+ elif spec.chart_type == "line": ax.plot(plot_data.index, plot_data.values, marker='o', linewidth=2, color='#A23B72'); ax.set_xlabel(spec.x_col); ax.set_ylabel(spec.y_col); ax.grid(True, alpha=0.3)
184
+ elif spec.chart_type == "scatter": ax.scatter(plot_data.iloc[:, 0], plot_data.iloc[:, 1], alpha=0.6, color='#F18F01'); ax.set_xlabel(spec.x_col); ax.set_ylabel(spec.y_col); ax.grid(True, alpha=0.3)
185
+ elif spec.chart_type == "hist": ax.hist(plot_data.values, bins=20, color='#C73E1D', alpha=0.7, edgecolor='black'); ax.set_xlabel(spec.x_col); ax.set_ylabel('Frequency'); ax.grid(True, alpha=0.3)
186
+ ax.set_title(spec.title, fontsize=14, fontweight='bold', pad=20); plt.tight_layout()
187
+ plt.savefig(output_path, dpi=300, bbox_inches='tight', facecolor='white'); plt.close()
188
+ return True
189
+ except Exception as e: logging.error(f"Static chart generation failed for '{spec.title}': {e}"); return False
190
+
191
+ def prepare_plot_data(spec: ChartSpecification, df: pd.DataFrame) -> pd.Series:
192
+ if spec.x_col not in df.columns or (spec.y_col and spec.y_col not in df.columns): raise ValueError(f"Invalid columns in chart spec: {spec.x_col}, {spec.y_col}")
193
+ if spec.chart_type in ["bar", "pie"]:
194
+ if not spec.y_col: return df[spec.x_col].value_counts().nlargest(spec.top_n or 10)
195
+ grouped = df.groupby(spec.x_col)[spec.y_col].agg(spec.agg_method or 'sum')
196
+ return grouped.nlargest(spec.top_n or 10)
197
+ elif spec.chart_type == "line": return df.set_index(spec.x_col)[spec.y_col].sort_index()
198
+ elif spec.chart_type == "scatter": return df[[spec.x_col, spec.y_col]].dropna()
199
+ elif spec.chart_type == "hist": return df[spec.x_col].dropna()
200
+ return df[spec.x_col]
201
  # --- Main Business Logic Functions ---
202
 
203
  # In sozo_gen.py