File size: 13,910 Bytes
49eef15
 
 
 
 
 
 
 
 
 
 
681177e
 
268cdd3
150a64c
681177e
49eef15
 
 
681177e
29833ea
681177e
29833ea
681177e
49eef15
681177e
49eef15
681177e
 
 
 
 
 
49eef15
 
681177e
 
49eef15
681177e
49eef15
 
 
 
 
681177e
49eef15
 
681177e
49eef15
 
 
681177e
49eef15
 
681177e
 
49eef15
 
681177e
 
 
49eef15
681177e
49eef15
681177e
49eef15
681177e
49eef15
 
f4174ca
 
49eef15
 
 
f4174ca
49eef15
 
f4174ca
49eef15
 
 
f4174ca
49eef15
 
 
681177e
49eef15
 
 
681177e
49eef15
 
 
 
 
681177e
49eef15
681177e
49eef15
 
 
 
 
f4174ca
49eef15
681177e
49eef15
 
681177e
fccb8d7
681177e
fccb8d7
681177e
 
49eef15
 
681177e
 
 
 
 
 
49eef15
 
681177e
 
 
 
49eef15
 
 
681177e
 
 
49eef15
 
 
 
681177e
49eef15
681177e
49eef15
 
681177e
 
49eef15
 
 
 
 
681177e
7cced23
 
681177e
49eef15
 
 
 
681177e
49eef15
 
 
 
 
 
 
681177e
49eef15
681177e
49eef15
 
 
 
 
 
681177e
49eef15
 
681177e
49eef15
681177e
c52095c
 
681177e
 
 
 
c52095c
 
 
 
 
 
 
 
 
 
 
 
 
 
681177e
c52095c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7e21172
c52095c
 
681177e
c52095c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
681177e
 
 
 
 
 
 
 
 
 
 
 
9dc1a68
681177e
 
 
 
 
c52095c
681177e
c52095c
 
 
f4174ca
c52095c
 
 
 
 
f4174ca
c52095c
 
 
 
 
f4174ca
681177e
977aecc
681177e
977aecc
681177e
 
 
 
 
 
ea7a0c4
977aecc
681177e
 
 
 
 
 
 
 
 
 
 
 
ea7a0c4
681177e
49eef15
681177e
49eef15
681177e
 
49eef15
681177e
49eef15
681177e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49eef15
d25177f
681177e
 
 
 
 
 
 
 
49eef15
681177e
 
 
 
 
 
 
 
 
 
49eef15
80b3c94
681177e
 
 
 
 
 
 
49eef15
80b3c94
 
 
 
 
 
 
 
 
 
 
f52d276
 
681177e
 
 
 
f177fd2
 
681177e
 
c891cbe
681177e
 
 
 
 
 
c891cbe
681177e
 
f177fd2
9dc1a68
681177e
9dc1a68
c891cbe
681177e
 
f177fd2
681177e
f177fd2
681177e
 
 
f177fd2
8c00937
f52d276
 
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
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
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
import subprocess
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.ticker import FuncFormatter
import gradio as gr
import tempfile
import logging
from PIL import Image
import os
import io
import numpy as np
from sklearn.linear_model import LinearRegression
from sklearn.metrics import r2_score
import base64

# Initialize logging
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)

def plot_driver_analysis(results_df, average_value, title):
    """
    Plot driver analysis results with factors instead of trust buckets.
    """
    logger.info("Plotting driver analysis with title '%s'.", title)
    try:
        # Define color scheme for factors
        color_map = {
            "Factor 1": "#375570",
            "Factor 2": "#E3B05B",
            "Factor 3": "#C63F48",
            "Factor 4": "#418387",
            "Factor 5": "#DF8859",
            "Factor 6": "#6D93AB",
        }

        # Define the order
        order = ["Factor 1", "Factor 2", "Factor 3", "Factor 4", "Factor 5", "Factor 6"]

        # Apply categorical ordering
        results_df["Predictor"] = pd.Categorical(
            results_df["Predictor"], categories=order, ordered=True
        )
        results_df.sort_values("Predictor", ascending=False, inplace=True)

        # Create the figure
        fig, ax = plt.subplots(figsize=(10, 8))

        # Set x-axis formatter
        formatter = FuncFormatter(lambda x, _: f"{x:.0f}%")
        ax.xaxis.set_major_formatter(formatter)

        # Determine x-axis range
        actual_min = results_df["Importance_percent"].min()
        actual_max = results_df["Importance_percent"].max()
        x_min = 0
        x_max = actual_max + 5
        plt.xlim(x_min, x_max)

        # Set x-axis ticks and grid
        x_ticks = np.arange(np.floor(x_min), np.ceil(x_max) + 5, 5)
        ax.set_xticks(x_ticks)
        for tick in x_ticks:
            ax.axvline(x=tick, color="grey", linestyle="--", linewidth=0.5, zorder=2)

        # Create bars
        for i, row in enumerate(results_df.itertuples(index=False)):
            color = color_map.get(row.Predictor, "#cccccc")
            ax.barh(
                row.Predictor,
                row.Importance_percent,
                left=0,
                color=color,
                edgecolor="white",
                height=0.6,
                zorder=3,
            )
            ax.text(
                row.Importance_percent + 0.5,
                i,
                f"{row.Importance_percent:.1f}%",
                va="center",
                ha="left",
                color="#8c8b8c",
            )

        # Draw average line
        ax.axvline(average_value, color="black", linewidth=1, linestyle="-", zorder=3)
        plt.title(title, fontsize=14)

        # Style the plot
        ax.spines[["left", "top", "right"]].set_color("none")
        ax.tick_params(axis="y", colors="#8c8b8c", length=0)
        ax.set_axisbelow(True)
        plt.tight_layout()

        # Save to image
        img_data = io.BytesIO()
        plt.savefig(img_data, format="png", facecolor=fig.get_facecolor(), edgecolor="none")
        img_data.seek(0)
        img = Image.open(img_data)
        plt.close(fig)

        return img

    except Exception as e:
        logger.error("Error plotting driver analysis: %s", e)
        raise

def plot_factor_performance(driver_df, title):
    """
    Plot factor performance (agreement scores).
    """
    factors = ["Factor 1", "Factor 2", "Factor 3", "Factor 4", "Factor 5", "Factor 6"]
    
    try:
        color_map = {
            "Factor 1": "#375570",
            "Factor 2": "#E3B05B",
            "Factor 3": "#C63F48",
            "Factor 4": "#418387",
            "Factor 5": "#DF8859",
            "Factor 6": "#6D93AB",
        }

        # Calculate mean scores
        results_df = (driver_df[factors].mean()).reset_index()
        results_df.columns = ["Factor", "Agreement_Score"]
        
        fig, ax = plt.subplots(figsize=(10, 8))

        ax.bar(
            results_df["Factor"],
            results_df["Agreement_Score"],
            color=[color_map[factor] for factor in results_df["Factor"]],
            edgecolor="white",
            zorder=2,
        )

        # Add values on top
        for i, row in enumerate(results_df.itertuples(index=False, name=None)):
            factor, score = row
            ax.text(
                i,
                score + 0.1,
                f"{score:.1f}",
                ha="center",
                va="bottom",
                color="#8c8b8c",
            )

        # Set y-axis
        plt.ylim(1, 10)
        plt.yticks(range(1, 11))
        plt.ylabel("Agreement Score")
        plt.title(title, fontsize=14)

        ax.spines[["top", "right"]].set_color("none")

        # Add grid
        y_ticks = ax.get_yticks()
        for y_tick in y_ticks:
            ax.axhline(y=y_tick, color="grey", linestyle="--", linewidth=0.5, zorder=1)

        ax.set_axisbelow(True)
        plt.tight_layout()

        # Save to image
        img_data = io.BytesIO()
        plt.savefig(img_data, format="png", facecolor=fig.get_facecolor(), edgecolor="none")
        img_data.seek(0)
        img = Image.open(img_data)
        plt.close(fig)

        return img
    except Exception as e:
        logger.error("Error plotting factor performance: %s", e)
        raise

def calculate_r2_image(r2_percent):
    """
    Create R² visualization.
    """
    categories = [
        ("<40%: Deficient", "#b03c3c"),
        (">50%: Gaps", "#bdd8da"),
        (">60%: Proven", "#89b7bc"),
        (">70%: Robust", "#375a5e"),
    ]
    labels = [c[0] for c in categories]
    colors = [c[1] for c in categories]

    fig, ax = plt.subplots(figsize=(3.6, 3.6), subplot_kw=dict(aspect="equal"))

    wedges, _ = ax.pie(
        [1] * 4,
        startangle=90,
        counterclock=False,
        colors=colors,
        wedgeprops=dict(width=0.35)
    )

    # Add outer labels
    for i, wedge in enumerate(wedges):
        angle = (wedge.theta2 + wedge.theta1) / 2
        x = 1.5 * np.cos(np.deg2rad(angle))
        y = 1.5 * np.sin(np.deg2rad(angle))
        ax.text(
            x, y, labels[i],
            ha='center', va='center',
            fontsize=9,
            color='black'
        )

    # Center R² text
    ax.text(
        0, 0, f"{int(round(r2_percent))}%",
        ha='center', va='center',
        fontsize=19, fontweight='bold'
    )

    ax.set_title("Model Validity", fontsize=11, pad=10)
    ax.axis('off')
    fig.patch.set_facecolor('none')
    ax.patch.set_facecolor('none')
    plt.tight_layout()

    buf = io.BytesIO()
    plt.savefig(buf, format='png', transparent=True, dpi=200)
    plt.close(fig)
    buf.seek(0)
    img_base64 = base64.b64encode(buf.read()).decode("utf-8")

    return f"""
    <div style='display: flex; justify-content: center; align-items: center;'>
        <img src='data:image/png;base64,{img_base64}' style='max-width: 240px; height: auto;'/>
    </div>
    """

def create_avg_target_display(avg_target):
    """
    Create average target (Purchase Consideration) visualization.
    """
    fig, ax = plt.subplots(figsize=(3.6, 3.6))
    
    # Create circular display
    circle = plt.Circle((0.5, 0.5), 0.4, color='#4CAF50', alpha=0.3)
    ax.add_patch(circle)
    
    ax.text(0.5, 0.5, f"{avg_target:.1f}", 
            ha='center', va='center', fontsize=24, fontweight='bold')
    ax.text(0.5, 0.2, "Scale: 1-6", 
            ha='center', va='center', fontsize=10, color='gray')
    
    ax.set_xlim(0, 1)
    ax.set_ylim(0, 1)
    ax.set_title("Avg Target", fontsize=11, pad=10)
    ax.axis('off')
    
    fig.patch.set_facecolor('none')
    ax.patch.set_facecolor('none')
    plt.tight_layout()

    buf = io.BytesIO()
    plt.savefig(buf, format='png', transparent=True, dpi=200)
    plt.close(fig)
    buf.seek(0)
    img_base64 = base64.b64encode(buf.read()).decode("utf-8")

    return f"""
    <div style='display: flex; justify-content: center; align-items: center;'>
        <img src='data:image/png;base64,{img_base64}' style='max-width: 240px; height: auto;'/>
    </div>
    """

def call_r_script_simplified(input_file, csv_output_path):
    """
    Call R script for Shapley regression analysis on Consideration.
    """
    command = [
        "Rscript",
        "process_data.R",
        input_file,
        csv_output_path
    ]

    try:
        subprocess.run(command, check=True)
    except subprocess.CalledProcessError as e:
        logger.error("R script failed with error: %s", e)
        # For demo purposes, create mock data if R script fails
        mock_data = pd.DataFrame({
            'Predictor': ['Factor 1', 'Factor 2', 'Factor 3', 'Factor 4', 'Factor 5', 'Factor 6'],
            'Importance': [0.15, 0.22, 0.18, 0.20, 0.13, 0.12]
        })
        mock_data.to_csv(csv_output_path, index=False)
    except Exception as e:
        logger.error("Error calling R script: %s", e)
        raise

def analyze_prospects_data(file_path):
    """
    Analyze prospects data focusing on Purchase Consideration as target.
    """
    logger.info("Analyzing prospects file: %s", file_path)
    
    try:
        # Load Excel file
        df = pd.read_excel(file_path, sheet_name="Driver", header=3)
        
        # Map column names from trust buckets to factors
        column_mapping = {
            "Stability": "Factor 1",
            "Development": "Factor 2", 
            "Relationship": "Factor 3",
            "Benefit": "Factor 4",
            "Vision": "Factor 5",
            "Competence": "Factor 6"
        }
        
        # Create a copy with renamed columns for analysis
        df_analysis = df.copy()
        for old_name, new_name in column_mapping.items():
            if old_name in df_analysis.columns:
                df_analysis.rename(columns={old_name: new_name}, inplace=True)
        
        # Check if Consideration column exists
        if "Consideration" not in df.columns:
            logger.error("Consideration column not found in dataset")
            return None, None, None, None
        
        # Calculate R² for Consideration model
        factors = list(column_mapping.values())
        X = df_analysis[factors].dropna()
        y = df.loc[X.index, "Consideration"]  # Use Consideration as target
        
        model = LinearRegression()
        model.fit(X, y)
        r2 = r2_score(y, model.predict(X))
        r2_percent = r2 * 100
        
        # Calculate average target (Consideration)
        avg_target = df["Consideration"].mean()
        
        # Create visualizations
        r2_html = calculate_r2_image(r2_percent)
        avg_target_html = create_avg_target_display(avg_target)
        
        # Factor performance plot
        factor_performance_img = plot_factor_performance(df_analysis, "Factor Performance (Agreement Scores)")
        
        # Run Shapley analysis on Consideration
        temp_dir = tempfile.mkdtemp()
        csv_output_path = os.path.join(temp_dir, "consideration_results.csv")
        
        # Call R script or create mock results
        call_r_script_simplified(file_path, csv_output_path)
        
        # Load results with renamed predictors
        results_df = pd.read_csv(csv_output_path)
        
        # Map predictor names if they come from R script with original names
        if "Predictor" in results_df.columns:
            results_df["Predictor"] = results_df["Predictor"].map(
                lambda x: column_mapping.get(x, x)
            )
        
        results_df["Importance_percent"] = results_df["Importance"] * 100
        average_value = results_df["Importance_percent"].mean()
        
        # Create driver analysis plot
        driver_analysis_img = plot_driver_analysis(
            results_df, 
            average_value,
            "Shapley Driver Analysis - Purchase Consideration"
        )
        
        # Clean up
        os.remove(csv_output_path)
        os.rmdir(temp_dir)
        
        return r2_html, avg_target_html, factor_performance_img, driver_analysis_img
        
    except Exception as e:
        logger.error(f"Error analyzing data: {e}")
        raise

# Gradio interface with light theme
css = """
.metric-container {
    display: flex;
    justify-content: space-around;
    margin: 20px 0;
}
"""

# JavaScript to force light theme
js = """
function refresh() {
    const url = new URL(window.location);
    if (url.searchParams.get('__theme') !== 'light') {
        url.searchParams.set('__theme', 'light');
        window.location.href = url.href;
    }
}
"""

# Create the Gradio interface with light theme
with gr.Blocks(css=css, js=js, theme=gr.themes.Soft()) as demo:
    gr.Markdown("""
        <h2 style="text-align: center; font-size: 2.25rem; font-weight: 600;">
            Driver Analysis Demo - Purchase Consideration
        </h2>
    """)
    
    gr.Markdown("### Prospects Analysis")
    gr.Markdown("Analysis showing what drives Purchase Consideration among prospects")
    
    # Metrics row
    with gr.Row(equal_height=True):
        with gr.Column(scale=1):
            r2_output = gr.HTML()
        with gr.Column(scale=1):
            avg_target_output = gr.HTML()
    
    # Charts
    with gr.Row():
        with gr.Column():
            factor_performance_plot = gr.Image(show_label=False)
        with gr.Column():
            driver_analysis_plot = gr.Image(show_label=False)
    
    # Hidden state for file path
    prospects_file = gr.State(value="example_files/Volkswagen Non Customers.xlsx")
    
    # Auto-load on page load
    demo.load(
        fn=analyze_prospects_data,
        inputs=[prospects_file],
        outputs=[r2_output, avg_target_output, factor_performance_plot, driver_analysis_plot]
    )

# Launch without the theme parameter
demo.launch(server_name="0.0.0.0", share=False)