File size: 10,941 Bytes
1ee6ce7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""
UMAP Generator for arXiv papers
Creates 2D and 3D projections with density-weighted centroids
"""

import json
import numpy as np
import pandas as pd
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.decomposition import TruncatedSVD
import umap
import os
import shutil
from datetime import datetime
from collections import Counter

def load_papers(filename="arxiv_monthly_papers.json"):
    """Load papers from JSON file"""
    if not os.path.exists(filename):
        print(f"❌ File {filename} not found!")
        print("πŸ’‘ Run fetch_arxiv_api.py first")
        return None
    
    with open(filename, 'r', encoding='utf-8') as f:
        papers = json.load(f)
    
    print(f"πŸ“š {len(papers)} papers loaded from {filename}")
    return papers

def preprocess_papers(papers, sample_rate=5):
    """Preprocess papers and sample if necessary"""
    print(f"πŸ”„ Preprocessing papers...")
    
    # Filter papers with missing data
    valid_papers = []
    for paper in papers:
        if (paper.get('title') and 
            paper.get('summary') and 
            paper.get('primary_category')):
            valid_papers.append(paper)
    
    print(f"βœ… {len(valid_papers)} valid papers after filtering")
    
    # Sampling for performance (1 out of N)
    if sample_rate > 1:
        sampled_papers = valid_papers[::sample_rate]
        print(f"πŸ“Š Sampling 1/{sample_rate}: {len(sampled_papers)} papers retained")
        return sampled_papers
    
    return valid_papers

def create_embeddings(papers, max_features=5000, n_components=50):
    """Create TF-IDF + SVD embeddings of papers"""
    print(f"πŸ”’ Creating embeddings (max_features={max_features}, n_components={n_components})")
    
    # Combine title and summary
    texts = []
    for paper in papers:
        title = paper.get('title', '').strip()
        summary = paper.get('summary', '').strip()
        combined = f"{title} {summary}"
        texts.append(combined)
    
    # TF-IDF
    print("  πŸ“ TF-IDF vectorization...")
    tfidf = TfidfVectorizer(
        max_features=max_features,
        stop_words='english',
        ngram_range=(1, 2),
        min_df=2,
        max_df=0.95
    )
    
    tfidf_matrix = tfidf.fit_transform(texts)
    print(f"  βœ… TF-IDF: {tfidf_matrix.shape}")
    
    # Dimensionality reduction with SVD
    print(f"  πŸ”„ SVD reduction to {n_components} dimensions...")
    svd = TruncatedSVD(n_components=n_components, random_state=42)
    embeddings = svd.fit_transform(tfidf_matrix)
    
    print(f"  βœ… Final embeddings: {embeddings.shape}")
    print(f"  πŸ“Š Explained variance: {svd.explained_variance_ratio_.sum():.3f}")
    
    return embeddings

def map_to_families(papers):
    """Map categories to 9 main scientific families"""
    
    # Mapping to 9 scientific families
    domain_to_family = {
        'cs': 'Computer Science',
        'math': 'Mathematics', 
        'physics': 'Physics',
        'stat': 'Statistics',
        'q-bio': 'Biology',
        'eess': 'Engineering',
        'astro-ph': 'Astrophysics',
        'cond-mat': 'Condensed Matter',
        'nucl': 'Nuclear Physics'
    }
    
    families = []
    for paper in papers:
        primary_cat = paper.get('primary_category', '')
        if primary_cat:
            domain = primary_cat.split('.')[0]
            family = domain_to_family.get(domain, 'Other')
        else:
            family = 'Other'
        families.append(family)
    
    family_counts = Counter(families)
    print(f"πŸ“Š Distribution by family:")
    for family, count in family_counts.most_common():
        print(f"   {family}: {count} papers")
    
    return families

def generate_umap_projection(embeddings, families, n_neighbors=50, min_dist=0.1, spread=0.5, n_components=2):
    """Generate UMAP projection"""
    print(f"🎯 UMAP projection (n_neighbors={n_neighbors}, min_dist={min_dist}, spread={spread}, n_components={n_components})")
    
    # Configuration UMAP
    reducer = umap.UMAP(
        n_neighbors=n_neighbors,
        min_dist=min_dist, 
        spread=spread,
        n_components=n_components,
        random_state=42,
        metric='cosine'
    )
    
    # Projection
    projection = reducer.fit_transform(embeddings)
    print(f"βœ… Projection UMAP: {projection.shape}")
    
    return projection

def calculate_density_weighted_centroids(projection, families, families_list):
    """Calculate density-weighted centroids"""
    print("🎯 Calculating density-weighted centroids...")
    
    centroids = {}
    
    for family in families_list:
        # Points of this family
        family_mask = np.array(families) == family
        family_points = projection[family_mask]
        
        if len(family_points) < 30:  # Filter families too small
            continue
            
        if projection.shape[1] == 2:  # 2D
            # Calculate 2D density
            densities = []
            for point in family_points:
                distances = np.linalg.norm(family_points - point, axis=1)
                density = np.sum(distances < np.percentile(distances, 20))  # Local density
                densities.append(density)

            densities = np.array(densities)
            weights = densities / densities.sum()

            # Weighted centroid
            centroid_x = np.sum(family_points[:, 0] * weights)
            centroid_y = np.sum(family_points[:, 1] * weights)
            
            centroids[family] = {
                'x': float(centroid_x),
                'y': float(centroid_y),
                'count': len(family_points)
            }
            
        else:  # 3D
            # Calculate 3D density
            densities = []
            for point in family_points:
                distances = np.linalg.norm(family_points - point, axis=1)
                density = np.sum(distances < np.percentile(distances, 20))
                densities.append(density)

            densities = np.array(densities)
            weights = densities / densities.sum()

            # Weighted centroid
            centroid_x = np.sum(family_points[:, 0] * weights)
            centroid_y = np.sum(family_points[:, 1] * weights) 
            centroid_z = np.sum(family_points[:, 2] * weights)
            
            centroids[family] = {
                'x': float(centroid_x),
                'y': float(centroid_y), 
                'z': float(centroid_z),
                'count': len(family_points)
            }
    
    print(f"βœ… {len(centroids)} centroids calculated")
    return centroids

def save_visualization_data(papers, projection, families, centroids, output_prefix):
    """Save visualization data"""
    
    # Prepare data
    viz_data = []
    for i, paper in enumerate(papers):
        if projection.shape[1] == 2:  # 2D
            point = {
                'id': paper.get('id', f'paper_{i}'),
                'title': paper.get('title', ''),
                'summary': paper.get('summary', '')[:200] + '...',
                'authors': ', '.join(paper.get('authors', [])[:3]),  # Max 3 authors
                'category': paper.get('primary_category', ''),
                'family': families[i],
                'x': float(projection[i, 0]),
                'y': float(projection[i, 1])
            }
        else:  # 3D
            point = {
                'id': paper.get('id', f'paper_{i}'),
                'title': paper.get('title', ''),
                'summary': paper.get('summary', '')[:200] + '...',
                'authors': ', '.join(paper.get('authors', [])[:3]),
                'category': paper.get('primary_category', ''),
                'family': families[i],
                'x': float(projection[i, 0]),
                'y': float(projection[i, 1]),
                'z': float(projection[i, 2])
            }
        viz_data.append(point)
    
    # Add centroids
    viz_data_with_centroids = {
        'points': viz_data,
        'centroids': centroids,
        'metadata': {
            'total_papers': len(papers),
            'dimensions': projection.shape[1],
            'families': list(set(families)),
            'generated': datetime.now().isoformat()
        }
    }
    
    # Save
    output_file = f"{output_prefix}.json"
    with open(output_file, 'w', encoding='utf-8') as f:
        json.dump(viz_data_with_centroids, f, indent=2, ensure_ascii=False)
    
    size_mb = os.path.getsize(output_file) / 1024 / 1024
    print(f"πŸ’Ύ Data saved: {output_file} ({size_mb:.1f} MB)")
    
    return output_file

def main():
    """Main UMAP generation pipeline"""
    print("πŸš€ ArXiv UMAP Generator")
    print("=" * 40)
    
    # 1. Data loading
    papers = load_papers()
    if not papers:
        return
    
    # 2. Preprocessing
    papers = preprocess_papers(papers, sample_rate=5)  # 1 point out of 5
    
    # 3. Mapping to families
    families = map_to_families(papers)
    families_list = list(set(families))
    
    # 4. Embedding creation
    embeddings = create_embeddings(papers, max_features=3000, n_components=50)
    
    # 5. UMAP projection generation
    
    # UMAP 2D
    print("\n🎯 Generating 2D UMAP...")
    projection_2d = generate_umap_projection(
        embeddings, families,
        n_neighbors=50, min_dist=0.8, spread=1.0, n_components=2
    )
    
    centroids_2d = calculate_density_weighted_centroids(projection_2d, families, families_list)
    
    timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
    output_2d = save_visualization_data(
        papers, projection_2d, families, centroids_2d,
        f"arxiv_umap_viz_2d_{timestamp}"
    )
    
    # UMAP 3D
    print("\n🎯 Generating 3D UMAP...")
    projection_3d = generate_umap_projection(
        embeddings, families,
        n_neighbors=50, min_dist=0.8, spread=1.0, n_components=3
    )
    
    centroids_3d = calculate_density_weighted_centroids(projection_3d, families, families_list)
    
    output_3d = save_visualization_data(
        papers, projection_3d, families, centroids_3d,
        f"arxiv_umap_viz_3d_{timestamp}"
    )
    
    # Automatic copy to content/assets/data
    import shutil
    source_file = output_2d  # Use 2D by default
    target_dir = "../../assets/data"
    target_file = os.path.join(target_dir, "data.json")
    
    try:
        # Create directory if necessary
        os.makedirs(target_dir, exist_ok=True)
        shutil.copy2(source_file, target_file)
        print(f"\nβœ… AUTOMATIC COPY SUCCESSFUL!")
        print(f"πŸ“ {source_file} β†’ {target_file}")
    except Exception as e:
        print(f"\n⚠️  Automatic copy failed: {e}")
    
    print(f"\nπŸŽ‰ Generation completed!")
    print(f"πŸ“ Files created:")
    for f in [output_2d, output_3d]:
        if os.path.exists(f):
            size = os.path.getsize(f) / 1024 / 1024
            print(f"   - {f} ({size:.1f} MB)")

if __name__ == "__main__":
    main()