File size: 11,314 Bytes
5dde370
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
## 1. Data Preprocessing: Convert Raw Medical Text Data to Standardized Q&A Pairs
```bash
python Back-end/data_processing/process_qa.py
```

### Data Preprocessing Instructions

#### Feature Description
- Clean and convert raw medical text data into standardized Question-Answer (QA) pairs format
- Support processing data from multiple sources (Healthline articles, MedQA dataset, etc.)
- Use TF-IDF algorithm for keyword extraction
- Generate inverted index for QA pairs to facilitate retrieval
- Automatically remove duplicate and invalid QA pairs

#### Data Requirements
The data directory structure should be as follows:
```
./Data/
β”œβ”€β”€ Raw/
β”‚   β”œβ”€β”€ Healthline/
β”‚   β”‚   └── healthline_articles_text.csv
β”‚   └── MedQA/
β”‚       └── *.csv
└── Processed/
    └── cleaned_qa/
```

#### Input File Format
1. Healthline Article Data (CSV format):
   - Required columns: 'title' (as question), 'content' (as answer)

2. MedQA Data (CSV format):
   - Required columns: 'Question'/'question', 'Answer'/'answer'

#### Output Files
Processed data will be saved in `./Data/Processed/cleaned_qa/` and `keywords/` directories:
1. `qa_database.json`: Contains all processed QA pairs
   ```json
   [
     {
       "id": "unique_id",
       "source": "data_source",
       "question": "cleaned_question",
       "answer": "cleaned_answer",
       "keywords": ["keyword1", "keyword2", ...]
     },
     ...
   ]
   ```
2. `keyword_index.json`: Keyword inverted index
   ```json
   {
     "keyword1": ["qa_id1", "qa_id2", ...],
     "keyword2": ["qa_id3", "qa_id4", ...],
     ...
   }
   ```

#### Data Processing Steps
1. Text Cleaning:
   - Remove HTML tags
   - Normalize punctuation
   - Remove excess whitespace
   - Convert to lowercase

2. Keyword Extraction:
   - Use NLTK for tokenization
   - Remove stopwords (including general and medical domain-specific stopwords)
   - Use TF-IDF algorithm to extract important keywords

3. Quality Control:
   - Filter invalid QA pairs
   - Remove duplicate content
   - Generate unique identifiers

#### Dependency Installation
```bash
pip install nltk scikit-learn pandas tqdm
```

## 2. Text Vectorization: Convert QA Pairs to High-dimensional Vectors and Build Vector Database
```bash
python Back-end/models/vectorize.py
```

### Vectorization Process Instructions

#### Feature Description
- Use medical domain pre-trained model BioBERT-MNLI to convert QA pairs into high-dimensional vectors
- Build efficient vector database supporting semantic similarity search
- Implement vector caching mechanism to avoid repeated computation
- Multi-threaded parallel processing to improve data processing efficiency
- Support incremental updates and data persistence

#### System Architecture
1. Model Configuration:
   - Pre-trained model: `pritamdeka/BioBERT-mnli-snli-scinli-scitail-mednli-stsb`
   - Vector dimension: 768
   - Device support: Automatic GPU/CPU detection
   - Memory optimization: Batch processing mechanism

2. Vectorization Process:
   - Data loading and preprocessing
   - Text vector generation (with caching support)
   - Vector normalization (L2 regularization)
   - Batch processing (BATCH_SIZE=1024)
   - Multi-threaded parallel insertion (MAX_WORKERS=8)

3. Database Optimization:
   - Use ChromaDB for vector storage
   - HNSW index configuration:
     - Space metric: cosine
     - Build parameters: ef=100
     - Search parameters: ef=128
     - Graph connectivity: M=32/64
   - Batch persistence (40000 entries/batch)

#### Data Directory Structure
```
./Data/
β”œβ”€β”€ database/          # ChromaDB persistent storage
β”œβ”€β”€ Embeddings/        # Vector cache directory
└── Processed/
    β”œβ”€β”€ keywords/      # Keyword index
    └── cleaned_qa/    # Preprocessed QA data
```

#### Vectorization Process
1. Data Preparation:
   - Load QA data and keyword index
   - Merge questions, answers, and keywords into unified text
   - Build metadata (source, ID, keywords, etc.)

2. Vector Generation:
   - Calculate data hash for cache validation
   - Check and load cached vectors
   - Generate new text vectors in batches
   - Automatically save vectors to cache directory

3. Database Construction:
   - Create temporary in-memory database
   - Multi-threaded parallel data insertion
   - Batch persistence to disk
   - Automatic progress tracking and memory monitoring

#### Performance Metrics
- Batch size: 1024 entries/batch
- Insertion batch: 1024 entries/batch
- Persistence batch: 40000 entries/batch
- Parallel threads: 8
- Vector dimension: 768
- Memory usage monitoring: Real-time tracking

#### Dependency Requirements
```bash
pip install torch sentence-transformers chromadb tqdm numpy psutil
```

#### Hardware Recommendations
- Recommended configuration: NVIDIA GPU (8GB+ VRAM)
- Minimum configuration: 8GB system memory (CPU mode)

#### Usage Instructions
1. Run vectorization processing:
```bash
python Back-end/models/vectorize.py
```

2. Test database:
```bash
python Back-end/models/test_db.py
```

The test program will:
- Verify database integrity
- Display random sample data
- Execute example queries
- Show similarity scores

Example query result:
```
Results for query term 'diabetes':

Result 1:
----------------------------------------
Similarity score: 0.6597

Document content:
Question: what are the treatments for diabetes
Answer: diabetes is a very serious disease over time diabetes that is not well managed causes serious damage to the eyes kidneys nerves and heart gums and teeth if you have diabetes you are more likely than someone who does not have diabetes to have heart disease or a stroke people with diabetes also tend to develop heart disease or stroke at an earlier age than others the best way to protect yourself from the serious complications of diabetes is to manage your blood glucose blood pressure and cholesterol and avoid smoking it is not always easy but people who make an ongoing effort to manage their diabetes can greatly improve their overall health
Keywords: diabetes, heart, serious, blood, manage, people, stroke, best, complication, damage

Metadata:
{
    'item_id': 'MedicalQuestionAnswering_5480',
    'keywords': 'diabetes, heart, serious, blood, manage, people, stroke, best, complication, damage',
    'source': 'diabetes',
    'type': 'qa'
}
```

This example demonstrates:
- Semantic similarity search effectiveness
- Results including complete QA pairs
- Related metadata information
- Keyword extraction results

## 3. Topic Clustering: Perform Topic Clustering on Vectorized QA Pairs
```bash
python Back-end/models/cluster_topic.py
```

### Topic Clustering Instructions

#### Feature Description
- Use UMAP for high-dimensional vector dimensionality reduction
- Apply HDBSCAN algorithm for density clustering
- Support GPU acceleration (if available)
- Automatically update clustering results to vector database
- Clustering results persistence and caching mechanism

#### System Architecture
1. Dimensionality Reduction Configuration:
   - Algorithm: UMAP (Uniform Manifold Approximation and Projection)
   - Output dimension: 50
   - Number of neighbors: 50
   - Minimum distance: 0.2
   - Distance metric: cosine
   - GPU support: automatic detection

2. Clustering Configuration:
   - Algorithm: HDBSCAN
   - Minimum cluster size: 100
   - Minimum samples: 10
   - Distance metric: euclidean
   - Cluster selection method: EOM (Excess of Mass)
   - Parallel processing: multi-core support

3. Data Flow:
   - Load vectors from ChromaDB
   - UMAP dimensionality reduction
   - HDBSCAN clustering
   - Write results back to database

#### Data Directory Structure
```
./Data/
β”œβ”€β”€ database/          # ChromaDB storage
β”œβ”€β”€ Embeddings/        # Dimensionality reduction result cache
└── Processed/
    └── clusters/      # Clustering results
```

#### Performance Optimization
- Automatic GPU acceleration support
- Dimensionality reduction result caching
- Parallel computation optimization
- Memory usage optimization

#### Dependency Requirements
```bash
pip install umap-learn hdbscan joblib
# GPU acceleration (optional)
pip install cupy cuml
```

#### Usage Instructions
1. Run clustering processing:
```bash
python Back-end/models/cluster_topic.py
```

2. Clustering Results:
- Each document will be assigned a cluster label
- Label format: `cluster_N` (N is cluster number)
- Noise points marked as `noise`
- Results stored in vector database metadata

Example query result:
```json
{
    "id": "doc_id",
    "metadata": {
        "cluster": "cluster_1",
        "source": "original_source",
        "keywords": ["keyword1", "keyword2", ...]
    }
}
```

## 4. Topic Classification: Train Topic Classifier and Make Predictions
```bash
python Back-end/models/topic_classification.py
```

### Topic Classification Instructions

#### Feature Description
- Train random forest classifier based on clustering results
- Use 5-fold cross-validation to evaluate model performance
- Support model persistence and version management
- Provide topic prediction functionality for new documents
- Automatic integration with vector database system

#### System Architecture
1. Classifier Configuration:
   - Algorithm: Random Forest
   - Number of trees: 100
   - Parallel processing: multi-core support
   - Evaluation metrics: accuracy, macro-F1, weighted-F1
   - Cross-validation: 5-fold stratified validation

2. Training Process:
   - Load vectors and labels from ChromaDB
   - Data preprocessing and cleaning
   - Model training and validation
   - Performance evaluation and reporting
   - Save optimal model

3. Prediction Process:
   - Load trained model
   - New document vectorization
   - Topic prediction
   - Update results to database

#### Data Directory Structure
```
./Data/
β”œβ”€β”€ database/          # ChromaDB storage
└── models/            # Model storage directory
    └── topic_classifier_*.joblib  # Timestamped model files
```

#### Performance Metrics
- Cross-validation folds: 5
- Evaluation metrics:
  - Accuracy
  - Macro-average F1
  - Weighted-average F1
- Model version control: timestamp naming

#### Dependency Requirements
```bash
pip install scikit-learn joblib numpy
```

#### Usage Instructions
1. Train classifier:
```bash
python Back-end/models/topic_classification.py
```

2. Model Output:
- Detailed performance report for each fold during training
- Final model saved in `./models/` directory
- Filename format: `topic_classifier_YYYYMMDD_HHMMSS.joblib`

Example output:
```
Loading data...
Data loading complete, feature shape: (N, 768)
Number of classes: K

Starting 5-fold cross-validation...
Fold 1 validation:
Classification report:
              precision    recall  f1-score 
   cluster_0       0.85      0.83      0.84       
   cluster_1       0.82      0.80      0.81      
...

Overall performance:
Average accuracy: 0.8234 Β± 0.0256
Average macro F1 score: 0.8156 Β± 0.0278
Average weighted F1 score: 0.8245 Β± 0.0267

Model saved to: ./models/topic_classifier_20240315_143022.joblib
```

This classification system will:
- Automatically process new medical QA documents
- Maintain consistency with existing clustering system
- Support incremental updates and model iteration
- Provide reliable topic prediction service