reab5555 commited on
Commit
5251bf1
·
verified ·
1 Parent(s): 07990a2

Update visualization.py

Browse files
Files changed (1) hide show
  1. visualization.py +64 -81
visualization.py CHANGED
@@ -1,91 +1,74 @@
1
- import plotly.graph_objects as go
2
-
3
- def create_placeholder_chart(title="Error"):
4
- fig = go.Figure()
5
- fig.add_annotation(text="Error generating chart", xref="paper", yref="paper", showarrow=False, font=dict(size=20, color="red"))
6
- fig.update_layout(title=title)
7
- return fig
8
 
9
  def create_charts(results):
10
  charts = {}
11
  explanations = {}
12
 
13
- for i, speaker_data in enumerate(results['speaker_analyses']):
14
- charts[i] = {}
15
- explanations[i] = {}
16
 
17
  # Attachment Styles
18
- try:
19
- attachment_data = speaker_data['attachment_styles']
20
- labels = ['Secured', 'Anxious-Preoccupied', 'Dismissive-Avoidant', 'Fearful-Avoidant']
21
- values = [attachment_data[label] for label in labels]
22
- colors = ['blue', 'orange', 'green', 'red']
23
-
24
- fig = go.Figure(data=[go.Bar(x=labels, y=values, marker_color=colors)])
25
- fig.update_layout(title=f'Speaker {i+1}: Attachment Styles', yaxis_range=[0, 1])
26
-
27
- charts[i]['attachment'] = fig
28
- explanations[i]['attachment'] = attachment_data['Explanation']
29
- except Exception as e:
30
- charts[i]['attachment'] = create_placeholder_chart(f'Speaker {i+1}: Attachment Styles')
31
- explanations[i]['attachment'] = f"Error in attachment data: {str(e)}"
32
-
33
- # Attachment Dimensions (Radar Chart)
34
- try:
35
- dimensions_data = speaker_data['attachment_styles']
36
- labels = ['Self', 'Others', 'Anxiety', 'Avoidance']
37
- values = [dimensions_data[label] for label in labels]
38
-
39
- fig = go.Figure(data=go.Scatterpolar(
40
- r=values,
41
- theta=labels,
42
- fill='toself'
43
- ))
44
- fig.update_layout(
45
- polar=dict(
46
- radialaxis=dict(visible=True, range=[0, 10])
47
- ),
48
- showlegend=False,
49
- title=f'Speaker {i+1}: Attachment Dimensions'
50
- )
51
-
52
- charts[i]['dimensions'] = fig
53
- except Exception as e:
54
- charts[i]['dimensions'] = create_placeholder_chart(f'Speaker {i+1}: Attachment Dimensions')
55
-
56
  # Big Five Traits
57
- try:
58
- bigfive_data = speaker_data['big_five_traits']
59
- labels = ['Extraversion', 'Agreeableness', 'Conscientiousness', 'Neuroticism', 'Openness']
60
- values = [bigfive_data[label] for label in labels]
61
-
62
- fig = go.Figure(data=[go.Bar(x=labels, y=values, marker_color='rgb(55, 83, 109)')])
63
- fig.update_layout(title=f'Speaker {i+1}: Big Five Traits', yaxis_range=[0, 10])
64
-
65
- charts[i]['bigfive'] = fig
66
- explanations[i]['bigfive'] = bigfive_data['Explanation']
67
- except Exception as e:
68
- charts[i]['bigfive'] = create_placeholder_chart(f'Speaker {i+1}: Big Five Traits')
69
- explanations[i]['bigfive'] = f"Error in big five data: {str(e)}"
70
-
71
  # Personality Disorders
72
- try:
73
- personality_data = speaker_data['personality_disorders']
74
- labels = [k for k in personality_data.keys() if k != 'Explanation']
75
- values = [personality_data[label] for label in labels]
76
-
77
- fig = go.Figure(data=[go.Bar(x=labels, y=values, marker_color='rgb(26, 118, 255)')])
78
- fig.update_layout(
79
- title=f'Speaker {i+1}: Personality Disorders',
80
- yaxis_range=[0, 5],
81
- xaxis_tickangle=-45
82
- )
83
-
84
- charts[i]['personality'] = fig
85
- explanations[i]['personality'] = personality_data['Explanation']
86
- except Exception as e:
87
- charts[i]['personality'] = create_placeholder_chart(f'Speaker {i+1}: Personality Disorders')
88
- explanations[i]['personality'] = f"Error in personality data: {str(e)}"
89
-
90
- return charts, explanations
91
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import plotly.graph_objs as go
2
+ from plotly.subplots import make_subplots
 
 
 
 
 
3
 
4
  def create_charts(results):
5
  charts = {}
6
  explanations = {}
7
 
8
+ for speaker_id, data in results.items():
9
+ charts[speaker_id] = {}
10
+ explanations[speaker_id] = {}
11
 
12
  # Attachment Styles
13
+ attachment_data = data['attachments']
14
+ labels = ['Secured', 'Anxious-Preoccupied', 'Dismissive-Avoidant', 'Fearful-Avoidant']
15
+ values = [getattr(attachment_data, 'Secured', 0),
16
+ getattr(attachment_data, 'Anxious_Preoccupied', 0),
17
+ getattr(attachment_data, 'Dismissive_Avoidant', 0),
18
+ getattr(attachment_data, 'Fearful_Avoidant', 0)]
19
+ colors = ['blue', 'orange', 'green', 'red']
20
+
21
+ fig = go.Figure(data=[go.Bar(x=labels, y=values, marker_color=colors)])
22
+ fig.update_layout(title=f'{speaker_id}: Attachment Styles', yaxis_range=[0, 1])
23
+ charts[speaker_id]['attachment'] = fig
24
+ explanations[speaker_id]['attachment'] = attachment_data.Explanation
25
+
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
26
  # Big Five Traits
27
+ bigfive_data = data['bigfive']
28
+ labels = ['Extraversion', 'Agreeableness', 'Conscientiousness', 'Neuroticism', 'Openness']
29
+ values = [bigfive_data.Extraversion, bigfive_data.Agreeableness,
30
+ bigfive_data.Conscientiousness, bigfive_data.Neuroticism, bigfive_data.Openness]
31
+ colors = ['blue', 'green', 'red', 'purple', 'orange']
32
+
33
+ fig = go.Figure(data=[go.Bar(x=labels, y=values, marker_color=colors)])
34
+ fig.update_layout(title=f'{speaker_id}: Big Five Traits', yaxis_range=[0, 10])
35
+ charts[speaker_id]['bigfive'] = fig
36
+ explanations[speaker_id]['bigfive'] = bigfive_data.Explanation
37
+
 
 
 
38
  # Personality Disorders
39
+ personality_data = data['personalities']
40
+ labels = ['Antisocial', 'Narcissistic', 'Depressed', 'Anxious-Avoidant',
41
+ 'Obsessive', 'Paranoid', 'Borderline', 'Dependent', 'Schizoid-Schizotypal']
42
+ values = [personality_data.Antisocial_Psychopathic, personality_data.Narcissistic,
43
+ personality_data.Depressed, personality_data.Anxious_Avoidant,
44
+ personality_data.Obsessional, personality_data.Paranoid,
45
+ personality_data.Borderline_Dysregulated, personality_data.Dependent_Victimized,
46
+ personality_data.Schizoid_Schizotypal]
47
+ colors = ['black', 'orange', 'gray', 'green', 'brown', 'purple', 'red', 'cyan', 'gold']
48
+
49
+ fig = go.Figure(data=[go.Bar(x=labels, y=values, marker_color=colors)])
50
+ fig.update_layout(title=f'{speaker_id}: Personality Disorders', yaxis_range=[0, 5])
51
+ charts[speaker_id]['personality'] = fig
52
+ explanations[speaker_id]['personality'] = personality_data.Explanation
 
 
 
 
 
53
 
54
+ # Attachment Dimensions (Radar Chart)
55
+ dimensions_data = data['attachments']
56
+ labels = ['Self', 'Others', 'Anxiety', 'Avoidance']
57
+ values = [dimensions_data.Self, dimensions_data.Others,
58
+ dimensions_data.Anxiety, dimensions_data.Avoidance]
59
+
60
+ fig = go.Figure(data=go.Scatterpolar(
61
+ r=values,
62
+ theta=labels,
63
+ fill='toself'
64
+ ))
65
+ fig.update_layout(
66
+ polar=dict(
67
+ radialaxis=dict(visible=True, range=[0, 10])
68
+ ),
69
+ showlegend=False,
70
+ title=f'{speaker_id}: Attachment Dimensions'
71
+ )
72
+ charts[speaker_id]['dimensions'] = fig
73
+
74
+ return charts, explanations