File size: 14,468 Bytes
54c99ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
import pandas as pd
import numpy as np
from collections import Counter
from typing import List, Dict
import re

def generate_dashboard(data):
    """Legacy function - kept for backwards compatibility"""
    return generate_comprehensive_dashboard(data, "Other")


def extract_items_from_field(data: List[Dict], field_name: str) -> List[str]:
    """Extract and split items from semicolon-separated field"""
    items = []
    for row in data:
        value = row.get(field_name, "")
        if value and isinstance(value, str):
            # Split by semicolon and clean
            parts = [p.strip() for p in value.split(';') if p.strip()]
            items.extend(parts)
    return items


def generate_comprehensive_dashboard(

    data: List[Dict],

    interviewee_type: str

) -> plt.Figure:
    """

    Generate comprehensive dashboard with multiple visualizations

    """
    
    if not data or len(data) == 0:
        # Return empty figure with message
        fig, ax = plt.subplots(figsize=(10, 6))
        ax.text(0.5, 0.5, 'No data available for visualization',
                ha='center', va='center', fontsize=14)
        ax.axis('off')
        return fig
    
    df = pd.DataFrame(data)
    
    # Determine number of subplots based on interviewee type
    if interviewee_type == "HCP":
        fig = create_hcp_dashboard(df)
    elif interviewee_type == "Patient":
        fig = create_patient_dashboard(df)
    else:
        fig = create_general_dashboard(df)
    
    plt.tight_layout()
    return fig


def create_hcp_dashboard(df: pd.DataFrame) -> plt.Figure:
    """Create dashboard for HCP interviews"""
    
    fig, axes = plt.subplots(2, 2, figsize=(14, 10))
    fig.suptitle('Healthcare Professional Interview Analysis', fontsize=16, fontweight='bold')
    
    # 1. Quality Score Distribution
    ax1 = axes[0, 0]
    if 'Quality Score' in df.columns:
        quality_scores = pd.to_numeric(df['Quality Score'], errors='coerce').dropna()
        if len(quality_scores) > 0:
            ax1.hist(quality_scores, bins=10, color='#3498db', edgecolor='black', alpha=0.7)
            ax1.axvline(quality_scores.mean(), color='red', linestyle='--', 
                       label=f'Mean: {quality_scores.mean():.2f}')
            ax1.set_xlabel('Quality Score')
            ax1.set_ylabel('Frequency')
            ax1.set_title('Transcript Quality Distribution')
            ax1.legend()
            ax1.grid(axis='y', alpha=0.3)
    
    # 2. Top Diagnoses
    ax2 = axes[0, 1]
    if 'Diagnoses' in df.columns:
        diagnoses = extract_items_from_field(df.to_dict('records'), 'Diagnoses')
        if diagnoses:
            diagnosis_counts = Counter(diagnoses)
            top_diagnoses = dict(diagnosis_counts.most_common(8))
            
            if top_diagnoses:
                labels = list(top_diagnoses.keys())
                # Truncate long labels
                labels = [label[:30] + '...' if len(label) > 30 else label for label in labels]
                values = list(top_diagnoses.values())
                
                bars = ax2.barh(labels, values, color='#2ecc71', edgecolor='black')
                ax2.set_xlabel('Frequency')
                ax2.set_title('Most Common Diagnoses')
                ax2.invert_yaxis()
                
                # Add value labels
                for i, bar in enumerate(bars):
                    width = bar.get_width()
                    ax2.text(width, bar.get_y() + bar.get_height()/2, 
                            f' {int(width)}', ha='left', va='center', fontsize=9)
    
    # 3. Prescription Analysis
    ax3 = axes[1, 0]
    if 'Prescriptions' in df.columns:
        prescriptions = extract_items_from_field(df.to_dict('records'), 'Prescriptions')
        if prescriptions:
            rx_counts = Counter(prescriptions)
            top_rx = dict(rx_counts.most_common(8))
            
            if top_rx:
                labels = list(top_rx.keys())
                labels = [label[:30] + '...' if len(label) > 30 else label for label in labels]
                values = list(top_rx.values())
                
                bars = ax3.barh(labels, values, color='#e74c3c', edgecolor='black')
                ax3.set_xlabel('Frequency')
                ax3.set_title('Most Mentioned Prescriptions')
                ax3.invert_yaxis()
                
                for i, bar in enumerate(bars):
                    width = bar.get_width()
                    ax3.text(width, bar.get_y() + bar.get_height()/2,
                            f' {int(width)}', ha='left', va='center', fontsize=9)
    
    # 4. Word Count by Transcript
    ax4 = axes[1, 1]
    if 'Word Count' in df.columns and 'Transcript ID' in df.columns:
        word_counts = pd.to_numeric(df['Word Count'], errors='coerce').dropna()
        transcript_ids = df['Transcript ID'][:len(word_counts)]
        
        if len(word_counts) > 0:
            bars = ax4.bar(range(len(word_counts)), word_counts, color='#9b59b6', 
                          edgecolor='black', alpha=0.7)
            ax4.set_xlabel('Transcript')
            ax4.set_ylabel('Word Count')
            ax4.set_title('Interview Length by Transcript')
            ax4.set_xticks(range(len(word_counts)))
            ax4.set_xticklabels(transcript_ids, rotation=45, ha='right')
            ax4.grid(axis='y', alpha=0.3)
            
            # Add mean line
            ax4.axhline(word_counts.mean(), color='red', linestyle='--',
                       label=f'Average: {int(word_counts.mean())}')
            ax4.legend()
    
    return fig


def create_patient_dashboard(df: pd.DataFrame) -> plt.Figure:
    """Create dashboard for Patient interviews"""
    
    fig, axes = plt.subplots(2, 2, figsize=(14, 10))
    fig.suptitle('Patient Interview Analysis', fontsize=16, fontweight='bold')
    
    # 1. Quality Score Distribution
    ax1 = axes[0, 0]
    if 'Quality Score' in df.columns:
        quality_scores = pd.to_numeric(df['Quality Score'], errors='coerce').dropna()
        if len(quality_scores) > 0:
            ax1.hist(quality_scores, bins=10, color='#3498db', edgecolor='black', alpha=0.7)
            ax1.axvline(quality_scores.mean(), color='red', linestyle='--',
                       label=f'Mean: {quality_scores.mean():.2f}')
            ax1.set_xlabel('Quality Score')
            ax1.set_ylabel('Frequency')
            ax1.set_title('Transcript Quality Distribution')
            ax1.legend()
            ax1.grid(axis='y', alpha=0.3)
    
    # 2. Top Symptoms
    ax2 = axes[0, 1]
    if 'Primary Symptoms' in df.columns:
        symptoms = extract_items_from_field(df.to_dict('records'), 'Primary Symptoms')
        if symptoms:
            symptom_counts = Counter(symptoms)
            top_symptoms = dict(symptom_counts.most_common(8))
            
            if top_symptoms:
                labels = list(top_symptoms.keys())
                labels = [label[:30] + '...' if len(label) > 30 else label for label in labels]
                values = list(top_symptoms.values())
                
                bars = ax2.barh(labels, values, color='#e67e22', edgecolor='black')
                ax2.set_xlabel('Frequency')
                ax2.set_title('Most Common Symptoms')
                ax2.invert_yaxis()
                
                for i, bar in enumerate(bars):
                    width = bar.get_width()
                    ax2.text(width, bar.get_y() + bar.get_height()/2,
                            f' {int(width)}', ha='left', va='center', fontsize=9)
    
    # 3. Patient Concerns
    ax3 = axes[1, 0]
    if 'Main Concerns' in df.columns:
        concerns = extract_items_from_field(df.to_dict('records'), 'Main Concerns')
        if concerns:
            concern_counts = Counter(concerns)
            top_concerns = dict(concern_counts.most_common(6))
            
            if top_concerns:
                # Create word cloud style pie chart
                labels = list(top_concerns.keys())
                labels = [label[:25] + '...' if len(label) > 25 else label for label in labels]
                sizes = list(top_concerns.values())
                colors_list = ['#ff6b6b', '#4ecdc4', '#45b7d1', '#f9ca24', '#6c5ce7', '#a29bfe']
                
                ax3.pie(sizes, labels=labels, autopct='%1.1f%%', startangle=90,
                       colors=colors_list[:len(sizes)])
                ax3.set_title('Distribution of Patient Concerns')
    
    # 4. Side Effects
    ax4 = axes[1, 1]
    if 'Side Effects' in df.columns:
        side_effects = extract_items_from_field(df.to_dict('records'), 'Side Effects')
        if side_effects:
            se_counts = Counter(side_effects)
            top_se = dict(se_counts.most_common(6))
            
            if top_se:
                labels = list(top_se.keys())
                labels = [label[:30] + '...' if len(label) > 30 else label for label in labels]
                values = list(top_se.values())
                
                bars = ax4.barh(labels, values, color='#e74c3c', edgecolor='black')
                ax4.set_xlabel('Frequency')
                ax4.set_title('Reported Side Effects')
                ax4.invert_yaxis()
                
                for i, bar in enumerate(bars):
                    width = bar.get_width()
                    ax4.text(width, bar.get_y() + bar.get_height()/2,
                            f' {int(width)}', ha='left', va='center', fontsize=9)
        else:
            ax4.text(0.5, 0.5, 'No side effects reported',
                    ha='center', va='center', transform=ax4.transAxes, fontsize=12)
            ax4.axis('off')
    
    return fig


def create_general_dashboard(df: pd.DataFrame) -> plt.Figure:
    """Create general dashboard"""
    
    fig, axes = plt.subplots(2, 2, figsize=(14, 10))
    fig.suptitle('General Interview Analysis', fontsize=16, fontweight='bold')
    
    # 1. Quality Score Distribution
    ax1 = axes[0, 0]
    if 'Quality Score' in df.columns:
        quality_scores = pd.to_numeric(df['Quality Score'], errors='coerce').dropna()
        if len(quality_scores) > 0:
            ax1.hist(quality_scores, bins=10, color='#3498db', edgecolor='black', alpha=0.7)
            ax1.axvline(quality_scores.mean(), color='red', linestyle='--',
                       label=f'Mean: {quality_scores.mean():.2f}')
            ax1.set_xlabel('Quality Score')
            ax1.set_ylabel('Frequency')
            ax1.set_title('Transcript Quality Distribution')
            ax1.legend()
            ax1.grid(axis='y', alpha=0.3)
    
    # 2. Word Count Distribution
    ax2 = axes[0, 1]
    if 'Word Count' in df.columns:
        word_counts = pd.to_numeric(df['Word Count'], errors='coerce').dropna()
        if len(word_counts) > 0:
            ax2.hist(word_counts, bins=15, color='#2ecc71', edgecolor='black', alpha=0.7)
            ax2.set_xlabel('Word Count')
            ax2.set_ylabel('Frequency')
            ax2.set_title('Interview Length Distribution')
            ax2.grid(axis='y', alpha=0.3)
    
    # 3. Processing Summary
    ax3 = axes[1, 0]
    if 'Quality Score' in df.columns:
        quality_scores = pd.to_numeric(df['Quality Score'], errors='coerce').dropna()
        
        categories = ['Excellent\n(>0.8)', 'Good\n(0.6-0.8)', 'Fair\n(0.4-0.6)', 'Poor\n(<0.4)']
        counts = [
            sum(quality_scores > 0.8),
            sum((quality_scores >= 0.6) & (quality_scores <= 0.8)),
            sum((quality_scores >= 0.4) & (quality_scores < 0.6)),
            sum(quality_scores < 0.4)
        ]
        
        colors_list = ['#2ecc71', '#f39c12', '#e67e22', '#e74c3c']
        bars = ax3.bar(categories, counts, color=colors_list, edgecolor='black', alpha=0.7)
        ax3.set_ylabel('Number of Transcripts')
        ax3.set_title('Quality Score Categories')
        ax3.grid(axis='y', alpha=0.3)
        
        # Add value labels
        for bar in bars:
            height = bar.get_height()
            if height > 0:
                ax3.text(bar.get_x() + bar.get_width()/2., height,
                        f'{int(height)}', ha='center', va='bottom', fontsize=10)
    
    # 4. Summary Statistics Table
    ax4 = axes[1, 1]
    ax4.axis('off')
    
    stats_data = []
    if 'Transcript ID' in df.columns:
        stats_data.append(['Total Transcripts', str(len(df))])
    
    if 'Quality Score' in df.columns:
        quality_scores = pd.to_numeric(df['Quality Score'], errors='coerce').dropna()
        if len(quality_scores) > 0:
            stats_data.append(['Avg Quality Score', f"{quality_scores.mean():.2f}"])
            stats_data.append(['Min Quality Score', f"{quality_scores.min():.2f}"])
            stats_data.append(['Max Quality Score', f"{quality_scores.max():.2f}"])
    
    if 'Word Count' in df.columns:
        word_counts = pd.to_numeric(df['Word Count'], errors='coerce').dropna()
        if len(word_counts) > 0:
            stats_data.append(['Avg Word Count', f"{int(word_counts.mean()):,}"])
            stats_data.append(['Total Words', f"{int(word_counts.sum()):,}"])
    
    if stats_data:
        table = ax4.table(cellText=stats_data, cellLoc='left',
                         colWidths=[0.5, 0.3], loc='center',
                         colLabels=['Metric', 'Value'])
        table.auto_set_font_size(False)
        table.set_fontsize(11)
        table.scale(1, 2)
        
        # Style the table
        for i in range(len(stats_data) + 1):
            if i == 0:
                table[(i, 0)].set_facecolor('#34495e')
                table[(i, 1)].set_facecolor('#34495e')
                table[(i, 0)].set_text_props(weight='bold', color='white')
                table[(i, 1)].set_text_props(weight='bold', color='white')
            else:
                if i % 2 == 0:
                    table[(i, 0)].set_facecolor('#ecf0f1')
                    table[(i, 1)].set_facecolor('#ecf0f1')
        
        ax4.set_title('Summary Statistics', fontsize=12, fontweight='bold', pad=20)
    
    return fig