-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomparison_metrics.py
More file actions
434 lines (357 loc) · 15.9 KB
/
Copy pathcomparison_metrics.py
File metadata and controls
434 lines (357 loc) · 15.9 KB
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
"""
Comparison metrics for measuring information loss (Objective 4 / RQ3).
Compares topic models trained on full text vs summarized text.
"""
import numpy as np
import pandas as pd
from typing import List, Dict, Tuple, Set
from bertopic import BERTopic
from sklearn.metrics.pairwise import cosine_similarity
from scipy.stats import spearmanr, kendalltau
from scipy.spatial.distance import jensenshannon
import warnings
warnings.filterwarnings('ignore')
class TopicComparisonMetrics:
"""
Metrics for comparing two topic models (full text vs summary).
"""
def __init__(self, full_model: BERTopic, summary_model: BERTopic,
full_docs: List[str], summary_docs: List[str]):
"""
Initialize comparison metrics.
Parameters:
-----------
full_model : BERTopic
Topic model trained on full text
summary_model : BERTopic
Topic model trained on summaries
full_docs : List[str]
Full text documents
summary_docs : List[str]
Summary documents
"""
self.full_model = full_model
self.summary_model = summary_model
self.full_docs = full_docs
self.summary_docs = summary_docs
def calculate_topic_count_diff(self) -> Dict[str, int]:
"""
Compare number of topics detected.
Returns:
--------
Dict[str, int]
Topic counts and difference
"""
full_topics = [t for t in set(self.full_model.topics_) if t != -1]
summary_topics = [t for t in set(self.summary_model.topics_) if t != -1]
# Cap ratio at 1.0 - having more topics doesn't mean better preservation
# It could indicate overfitting or fragmentation
raw_ratio = len(summary_topics) / len(full_topics) if full_topics else 0
capped_ratio = min(raw_ratio, 1.0)
return {
'full_text_topics': len(full_topics),
'summary_topics': len(summary_topics),
'difference': len(full_topics) - len(summary_topics),
'ratio': capped_ratio,
'raw_ratio': raw_ratio # Keep raw ratio for analysis
}
def calculate_topic_overlap(self, similarity_threshold: float = 0.7) -> Dict[str, float]:
"""
Calculate topic overlap based on keyword similarity.
Parameters:
-----------
similarity_threshold : float
Threshold for considering topics as "same"
Returns:
--------
Dict[str, float]
Overlap metrics
"""
full_topics = self.full_model.get_topics()
summary_topics = self.summary_model.get_topics()
# Remove outlier topics
full_topics = {k: v for k, v in full_topics.items() if k != -1}
summary_topics = {k: v for k, v in summary_topics.items() if k != -1}
if not full_topics or not summary_topics:
return {'overlap_count': 0, 'overlap_ratio': 0.0, 'similarity_scores': []}
# Get top words for each topic
full_words_dict = {
k: set([word for word, _ in v[:20]])
for k, v in full_topics.items()
}
summary_words_dict = {
k: set([word for word, _ in v[:20]])
for k, v in summary_topics.items()
}
# Calculate Jaccard similarity between all topic pairs
matches = 0
all_similarities = []
for full_id, full_words in full_words_dict.items():
max_similarity = 0
for summary_id, summary_words in summary_words_dict.items():
# Jaccard similarity
intersection = len(full_words & summary_words)
union = len(full_words | summary_words)
similarity = intersection / union if union > 0 else 0
all_similarities.append(similarity)
max_similarity = max(max_similarity, similarity)
if max_similarity >= similarity_threshold:
matches += 1
overlap_ratio = matches / len(full_topics) if full_topics else 0
return {
'overlap_count': matches,
'overlap_ratio': overlap_ratio,
'avg_similarity': np.mean(all_similarities) if all_similarities else 0,
'max_similarity': np.max(all_similarities) if all_similarities else 0
}
def calculate_vocabulary_preservation(self) -> Dict[str, float]:
"""
Calculate how much of the important vocabulary is preserved.
Returns:
--------
Dict[str, float]
Vocabulary preservation metrics
"""
# Get all important words from full text topics
full_topics = self.full_model.get_topics()
summary_topics = self.summary_model.get_topics()
full_words = set()
for topic_id, words in full_topics.items():
if topic_id != -1:
full_words.update([word for word, _ in words[:20]])
summary_words = set()
for topic_id, words in summary_topics.items():
if topic_id != -1:
summary_words.update([word for word, _ in words[:20]])
if not full_words:
return {'preserved_ratio': 0.0, 'lost_words': 0}
preserved = full_words & summary_words
lost = full_words - summary_words
return {
'preserved_ratio': len(preserved) / len(full_words),
'preserved_count': len(preserved),
'lost_count': len(lost),
'new_words': len(summary_words - full_words)
}
def calculate_document_assignment_consistency(self) -> Dict[str, float]:
"""
Calculate consistency of document-to-topic assignments.
Checks if documents are assigned to "similar" topics in both models.
Returns:
--------
Dict[str, float]
Assignment consistency metrics
"""
full_assignments = self.full_model.topics_
summary_assignments = self.summary_model.topics_
if len(full_assignments) != len(summary_assignments):
print("Warning: Document counts differ between models")
return {'consistency': 0.0}
# Build topic similarity matrix
full_topics = self.full_model.get_topics()
summary_topics = self.summary_model.get_topics()
# For each document, check if assigned to "similar" topics
consistent_assignments = 0
for i in range(len(full_assignments)):
full_topic = full_assignments[i]
summary_topic = summary_assignments[i]
# Skip if either is outlier
if full_topic == -1 or summary_topic == -1:
continue
# Get topic words
if full_topic in full_topics and summary_topic in summary_topics:
full_words = set([word for word, _ in full_topics[full_topic][:20]])
summary_words = set([word for word, _ in summary_topics[summary_topic][:20]])
# Calculate similarity
intersection = len(full_words & summary_words)
union = len(full_words | summary_words)
similarity = intersection / union if union > 0 else 0
if similarity >= 0.3: # Lower threshold for individual docs
consistent_assignments += 1
total_docs = len([t for t in full_assignments if t != -1])
consistency = consistent_assignments / total_docs if total_docs > 0 else 0
return {
'consistency': consistency,
'consistent_docs': consistent_assignments,
'total_docs': total_docs
}
def calculate_emerging_topics_preservation(self, full_emerging: pd.DataFrame,
summary_emerging: pd.DataFrame) -> Dict[str, float]:
"""
Calculate how well emerging topics are preserved.
Parameters:
-----------
full_emerging : pd.DataFrame
Emerging topics from full text
summary_emerging : pd.DataFrame
Emerging topics from summaries
Returns:
--------
Dict[str, float]
Preservation metrics
"""
if full_emerging.empty or summary_emerging.empty:
return {
'emerging_overlap_count': 0,
'emerging_overlap_ratio': 0.0,
'emergence_score_correlation': 0.0
}
full_topics_set = set(full_emerging['Topic'].values)
summary_topics_set = set(summary_emerging['Topic'].values)
# Direct ID overlap (may not be meaningful if topics renumbered)
direct_overlap = len(full_topics_set & summary_topics_set)
# Compare emergence scores correlation
if len(full_emerging) > 0 and len(summary_emerging) > 0:
# Match topics by similarity (simplified - assumes same IDs for now)
common_topics = list(full_topics_set & summary_topics_set)
if common_topics:
full_scores = full_emerging[full_emerging['Topic'].isin(common_topics)]['Emergence_Score'].values
summary_scores = summary_emerging[summary_emerging['Topic'].isin(common_topics)]['Emergence_Score'].values
if len(full_scores) == len(summary_scores) and len(full_scores) > 1:
correlation, _ = spearmanr(full_scores, summary_scores)
else:
correlation = 0
else:
correlation = 0
else:
correlation = 0
# Cap emerging overlap ratio at 1.0 (can't preserve more than 100%)
raw_overlap_ratio = direct_overlap / len(full_topics_set) if full_topics_set else 0
capped_overlap_ratio = min(raw_overlap_ratio, 1.0)
return {
'emerging_overlap_count': direct_overlap,
'emerging_overlap_ratio': capped_overlap_ratio,
'emergence_score_correlation': correlation,
'raw_overlap_ratio': raw_overlap_ratio # Keep for analysis
}
def calculate_burst_preservation(self, full_bursts: pd.DataFrame,
summary_bursts: pd.DataFrame) -> Dict[str, float]:
"""
Calculate how well burst patterns are preserved.
Parameters:
-----------
full_bursts : pd.DataFrame
Burst summary from full text
summary_bursts : pd.DataFrame
Burst summary from summaries
Returns:
--------
Dict[str, float]
Burst preservation metrics
"""
if full_bursts.empty or summary_bursts.empty or len(full_bursts) == 0 or len(summary_bursts) == 0:
return {
'burst_topic_overlap': 0.0,
'burst_count_correlation': 0.0
}
full_topics = set(full_bursts['Topic'].values)
summary_topics = set(summary_bursts['Topic'].values)
overlap = len(full_topics & summary_topics)
# Compare consensus burst counts for common topics
common_topics = list(full_topics & summary_topics)
if common_topics:
full_consensus = full_bursts[full_bursts['Topic'].isin(common_topics)]['Consensus_Bursts'].values
summary_consensus = summary_bursts[summary_bursts['Topic'].isin(common_topics)]['Consensus_Bursts'].values
if len(full_consensus) == len(summary_consensus) and len(full_consensus) > 0:
# Correlation of burst counts
if len(full_consensus) > 1:
correlation, _ = spearmanr(full_consensus, summary_consensus)
else:
correlation = 1.0 if full_consensus[0] == summary_consensus[0] else 0.0
else:
correlation = 0
else:
correlation = 0
# Cap burst overlap at 1.0 (can't preserve more than 100%)
raw_burst_overlap = overlap / len(full_topics) if full_topics else 0
capped_burst_overlap = min(raw_burst_overlap, 1.0)
return {
'burst_topic_overlap': capped_burst_overlap,
'burst_count_correlation': correlation,
'raw_burst_overlap': raw_burst_overlap # Keep for analysis
}
def calculate_information_loss_score(self, full_coherence: float,
summary_coherence: float,
similarity_threshold: float = 0.7) -> Dict[str, float]:
"""
Calculate comprehensive information loss score.
Parameters:
-----------
full_coherence : float
Coherence score for full text model
summary_coherence : float
Coherence score for summary model
similarity_threshold : float
Threshold for topic matching
Returns:
--------
Dict[str, float]
Comprehensive loss metrics
"""
# Topic count preservation
topic_diff = self.calculate_topic_count_diff()
topic_count_preservation = topic_diff['ratio']
# Topic overlap
overlap = self.calculate_topic_overlap(similarity_threshold)
topic_overlap_score = overlap['overlap_ratio']
# Vocabulary preservation
vocab = self.calculate_vocabulary_preservation()
vocab_preservation = vocab['preserved_ratio']
# Coherence preservation (cap at 1.0 - higher coherence doesn't mean better preservation)
raw_coherence_ratio = summary_coherence / full_coherence if full_coherence > 0 else 0
coherence_preservation = min(raw_coherence_ratio, 1.0)
# Overall information loss (0 = total loss, 1 = no loss)
preservation_score = (
0.3 * topic_count_preservation +
0.3 * topic_overlap_score +
0.2 * vocab_preservation +
0.2 * coherence_preservation
)
information_loss = 1.0 - preservation_score
return {
'information_loss': information_loss,
'preservation_score': preservation_score,
'topic_count_preservation': topic_count_preservation,
'topic_overlap_score': topic_overlap_score,
'vocab_preservation': vocab_preservation,
'coherence_preservation': coherence_preservation
}
def generate_comparison_report(self, full_coherence: float,
summary_coherence: float,
full_emerging: pd.DataFrame = None,
summary_emerging: pd.DataFrame = None,
full_bursts: pd.DataFrame = None,
summary_bursts: pd.DataFrame = None) -> Dict:
"""
Generate comprehensive comparison report.
Returns:
--------
Dict
Complete comparison metrics
"""
report = {
'topic_counts': self.calculate_topic_count_diff(),
'topic_overlap': self.calculate_topic_overlap(),
'vocabulary': self.calculate_vocabulary_preservation(),
'document_consistency': self.calculate_document_assignment_consistency(),
'coherence': {
'full_text': full_coherence,
'summary': summary_coherence,
'difference': full_coherence - summary_coherence,
'ratio': summary_coherence / full_coherence if full_coherence > 0 else 0
},
'information_loss': self.calculate_information_loss_score(
full_coherence, summary_coherence
)
}
if (full_emerging is not None and summary_emerging is not None and
not full_emerging.empty and not summary_emerging.empty):
report['emerging_topics'] = self.calculate_emerging_topics_preservation(
full_emerging, summary_emerging
)
if (full_bursts is not None and summary_bursts is not None and
not full_bursts.empty and not summary_bursts.empty):
report['bursts'] = self.calculate_burst_preservation(
full_bursts, summary_bursts
)
return report