-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDataProcessor.py
More file actions
1330 lines (1136 loc) · 77.3 KB
/
DataProcessor.py
File metadata and controls
1330 lines (1136 loc) · 77.3 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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import sys
import time
import threading
import pandas as pd
from dotenv import load_dotenv
from Main.modules import (
ensure_dir,
find_files,
find_data_folders,
save_statistics,
save_comprehensive_statistics,
merge_databases_simple,
merge_databases_enhanced,
compare_merge_methods,
create_result_folder
)
from datetime import datetime
from colorama import Fore, Style
class ProcessIndicator:
"""Process indicator for showing progress"""
busy = False
delay = 0.1
@staticmethod
def indicator():
while 1:
for cursor in '|/-\\':
yield cursor
def __init__(self, description="Processing"):
self.description = description
self.generator = self.indicator()
self.busy = False
self.visible = False
def write_next(self):
with self._screen_lock:
if not self.visible:
sys.stdout.write(f"\r{self.description:<50}")
self.visible = True
sys.stdout.write(next(self.generator))
sys.stdout.flush()
sys.stdout.write('\b')
def remove_indicator(self, cleanup=False):
with self._screen_lock:
if self.visible:
sys.stdout.write('\b')
self.visible = False
if cleanup:
sys.stdout.write(f"\r{self.description:<50}[Completed]\n")
sys.stdout.flush()
def indicator_task(self):
while self.busy:
self.write_next()
time.sleep(self.delay)
def __enter__(self):
if sys.stdout.isatty():
self._screen_lock = threading.Lock()
self.busy = True
self.thread = threading.Thread(target=self.indicator_task)
self.thread.start()
def __exit__(self, exc_type, exc_val, exc_tb):
if sys.stdout.isatty():
self.busy = False
time.sleep(self.delay)
self.remove_indicator(cleanup=True)
def process_wos_data(input_file: str, output_file: str) -> tuple[bool, pd.DataFrame, dict]:
"""Process WoS data"""
try:
from Main.wos2xlsx import save_to_excel
success = save_to_excel(input_file, output_file)
if success:
with ProcessIndicator("Converting WoS data to Excel format") as indicator:
df = pd.read_excel(output_file)
stats = {
'Record Count': len(df),
'Column Count': len(df.columns),
'Non-Empty Columns': df.count().to_dict()
}
return True, df, stats
return False, None, {}
except Exception as e:
sys.stderr.write(f"\nError: {str(e)}\n")
return False, None, {}
def process_scopus_data(input_file: str, output_file: str) -> tuple[bool, pd.DataFrame, dict]:
"""Process Scopus data"""
try:
from Main.scp2xlsx import save_to_excel
success = save_to_excel(input_file, output_file)
if success:
with ProcessIndicator("Converting Scopus data to Excel format") as indicator:
df = pd.read_excel(output_file)
stats = {
'Record Count': len(df),
'Column Count': len(df.columns),
'Non-Empty Columns': df.count().to_dict()
}
return True, df, stats
return False, None, {}
except Exception as e:
sys.stderr.write(f"\nError: {str(e)}\n")
return False, None, {}
def merge_txt_files(data_dir: str) -> str:
"""Merge all txt files in data directory into wos_raw.txt in merged_raw folder"""
txt_files = find_files(data_dir, "txt")
if not txt_files:
raise ValueError("No WoS files (txt) found in Data folder.")
# Create merged_raw directory if not exists
merged_raw_dir = os.path.join(data_dir, "merged_raw")
os.makedirs(merged_raw_dir, exist_ok=True)
output_file = os.path.join(merged_raw_dir, "wos_raw.txt")
with open(output_file, 'w', encoding='utf-8') as outfile:
for txt_file in txt_files:
with open(txt_file, 'r', encoding='utf-8') as infile:
outfile.write(infile.read())
outfile.write('\n') # Add newline between files
return output_file
def merge_csv_files(data_dir: str) -> str:
"""Merge all csv files in data directory into scp_raw.csv in merged_raw folder"""
csv_files = find_files(data_dir, "csv")
if not csv_files:
raise ValueError("No Scopus files (csv) found in Data folder.")
# Create merged_raw directory if not exists
merged_raw_dir = os.path.join(data_dir, "merged_raw")
os.makedirs(merged_raw_dir, exist_ok=True)
output_file = os.path.join(merged_raw_dir, "scp_raw.csv")
if len(csv_files) == 1:
# If only one CSV file, just copy it
import shutil
shutil.copy2(csv_files[0], output_file)
else:
# Read and combine all CSV files
all_data = []
for csv_file in csv_files:
df = pd.read_csv(csv_file, encoding='utf-8')
all_data.append(df)
# Concatenate all dataframes
combined_df = pd.concat(all_data, ignore_index=True)
# Save combined data
combined_df.to_csv(output_file, index=False, encoding='utf-8')
return output_file
def main():
try:
# Load environment variables
load_dotenv()
print("Database Merge Tool")
print("------------------")
# Find project folders
workspace_dir = "Workspace"
if not os.path.exists(workspace_dir):
os.makedirs(workspace_dir)
project_folders = [d for d in os.listdir(workspace_dir) if os.path.isdir(os.path.join(workspace_dir, d))]
if not project_folders:
sys.stderr.write("\nError: No project folder found in Workspace.\n")
return
print("\nProject Folders:")
for i, folder in enumerate(project_folders, 1):
print(f"{i}. {folder}")
# Project selection
while True:
try:
choice = int(input("\nSelect project number: "))
if 1 <= choice <= len(project_folders):
project_dir = os.path.join(workspace_dir, project_folders[choice-1])
break
else:
sys.stderr.write("Error: Invalid selection.\n")
except ValueError:
sys.stderr.write("Error: Invalid input. Please enter a number.\n")
# Create Data directory if not exists
data_dir = os.path.join(project_dir, "Data")
if not os.path.exists(data_dir):
os.makedirs(data_dir)
print(f"\nSelected Project: {project_folders[choice-1]}")
# Create unique analysis directory
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
analysis_dir = os.path.join(project_dir, f"Analysis_{timestamp}")
os.makedirs(analysis_dir, exist_ok=True)
# Create subfolders
text_files_dir = os.path.join(analysis_dir, "Text_Files")
cell_files_dir = os.path.join(analysis_dir, "Cell_Files")
os.makedirs(text_files_dir, exist_ok=True)
os.makedirs(cell_files_dir, exist_ok=True)
# Set output files in analysis directory
wos_output = os.path.join(cell_files_dir, "WoS.xlsx")
scopus_output = os.path.join(cell_files_dir, "Scopus.xlsx")
merged_bib = os.path.join(cell_files_dir, "Merged_Bib.xlsx")
merged_vos = os.path.join(text_files_dir, "Merged_Vos.txt")
api_enriched_bib = os.path.join(cell_files_dir, "Merged_API_Enriched_Bib.xlsx")
ml_enriched_bib = os.path.join(cell_files_dir, "Merged_ML_Enriched_Bib.xlsx")
api_enriched_vos = os.path.join(text_files_dir, "Merged_API_Enriched_Vos.txt")
ml_enriched_vos = os.path.join(text_files_dir, "Merged_ML_Enriched_Vos.txt")
stats_excel = os.path.join(analysis_dir, "Statistics.xlsx")
api_log = os.path.join(analysis_dir, "Api_Log.txt")
api_updates = os.path.join(analysis_dir, "Api_Update.xlsx")
ml_updates = os.path.join(analysis_dir, "ML_Update.xlsx")
# Check if merged_raw directory exists and contains required files
merged_raw_dir = os.path.join(data_dir, "merged_raw")
wos_raw = os.path.join(merged_raw_dir, "wos_raw.txt")
scp_raw = os.path.join(merged_raw_dir, "scp_raw.csv")
use_existing = False
if os.path.exists(merged_raw_dir) and os.path.exists(wos_raw) and os.path.exists(scp_raw):
while True:
choice = input("\nPreviously merged raw files found. What would you like to do?\n"
"1. Use previously merged files\n"
"2. Perform new merge\n"
"Your choice (1/2): ")
if choice in ['1', '2']:
use_existing = (choice == '1')
break
else:
sys.stderr.write("Invalid choice. Please enter 1 or 2.\n")
if not use_existing:
if os.path.exists(merged_raw_dir):
print("\nDeleting old merged files...")
import shutil
shutil.rmtree(merged_raw_dir)
# Merge txt files into wos_raw.txt
print("\nMerging process in progress...")
try:
wos_input = merge_txt_files(data_dir)
print(f"WoS files merged: {os.path.basename(wos_input)}")
except ValueError as e:
sys.stderr.write(f"\nError: {str(e)}\n")
return
# Merge csv files into scp_raw.csv
print("\nMerging Scopus files...")
try:
scopus_input = merge_csv_files(data_dir)
print(f"Scopus files merged: {os.path.basename(scopus_input)}")
except ValueError as e:
sys.stderr.write(f"\nError: {str(e)}\n")
return
else:
print("\nUsing existing merged files...")
wos_input = wos_raw
scopus_input = scp_raw
# Process WoS data
print("\nProcessing WoS data...")
wos_success, wos_df, wos_stats = process_wos_data(wos_input, wos_output)
if not wos_success:
sys.stderr.write("\nError: Failed to process WoS data.\n")
return
print("WoS data processing completed.")
# Process Scopus data
print("\nProcessing Scopus data...")
scopus_success, scopus_df, scopus_stats = process_scopus_data(scopus_input, scopus_output)
if not scopus_success:
sys.stderr.write("\nError: Failed to process Scopus data.\n")
return
print("Scopus data processing completed.")
# Enhanced merge
print("\nMerging process in progress...")
enhanced_success, enhanced_stats, enhanced_df = merge_databases_enhanced(
wos_df,
scopus_df,
merged_bib,
result_dir=analysis_dir
)
print("\nConverting data to VosViewer format...")
from Main.xlsx2vos import convert_excel_to_wos
convert_excel_to_wos(merged_bib, merged_vos)
print("VosViewer conversion completed.")
if enhanced_success:
# Ask user for enrichment preference
while True:
print("\nEnrichment Options:")
print("1. API Enrichment")
print("2. ML Enrichment (Experimental)")
print("3. API + ML Enrichment")
print("4. No Enrichment")
choice = input("Select enrichment method (1/2/3/4): ")
if choice == "1":
# API Enrichment
print("\nPerforming API enrichment...")
from Main.modules.api_utils import extract_metadata
# Read the merged file
merged_df = pd.read_excel(merged_bib)
# API supported fields and which APIs support which fields
api_supported_fields = {
'DT': {'name': 'Document Type', 'apis': ['CrossRef', 'OpenAlex', 'Scopus', 'DataCite', 'EuropePMC']},
'TI': {'name': 'Title', 'apis': ['CrossRef', 'OpenAlex', 'Scopus', 'DataCite', 'EuropePMC', 'SemanticScholar']},
'AU': {'name': 'Author', 'apis': ['CrossRef', 'OpenAlex', 'Scopus', 'DataCite', 'EuropePMC', 'SemanticScholar']},
'AF': {'name': 'Author-Affiliation', 'apis': ['OpenAlex']},
'C1': {'name': 'Author Addresses', 'apis': ['OpenAlex', 'DataCite']},
'PY': {'name': 'Publication Year', 'apis': ['CrossRef', 'OpenAlex', 'Scopus', 'DataCite', 'EuropePMC', 'SemanticScholar']},
'SO': {'name': 'Publication Name', 'apis': ['CrossRef', 'OpenAlex', 'Scopus', 'DataCite', 'EuropePMC']},
'JI': {'name': 'ISO Source Abbreviation', 'apis': ['Scopus']},
'VL': {'name': 'Volume', 'apis': ['Scopus', 'EuropePMC']},
'IS': {'name': 'Issue', 'apis': ['Scopus', 'EuropePMC']},
'BP': {'name': 'Beginning Page', 'apis': ['Scopus', 'EuropePMC']},
'EP': {'name': 'Ending Page', 'apis': ['Scopus', 'EuropePMC']},
'PU': {'name': 'Publisher', 'apis': ['CrossRef', 'OpenAlex', 'Scopus']},
'PA': {'name': 'Publisher Address', 'apis': ['CrossRef']},
'SN': {'name': 'ISSN', 'apis': ['CrossRef', 'OpenAlex', 'Scopus', 'EuropePMC']},
'AB': {'name': 'Abstract', 'apis': ['CrossRef', 'OpenAlex', 'Scopus', 'DataCite', 'EuropePMC', 'SemanticScholar']},
'DE': {'name': 'Author Keywords', 'apis': ['CrossRef', 'DataCite', 'SemanticScholar']},
'ID': {'name': 'Keywords Plus', 'apis': ['DataCite']},
'SC': {'name': 'Research Areas', 'apis': ['CrossRef', 'DataCite', 'SemanticScholar']},
'WC': {'name': 'Web of Science Categories', 'apis': ['CrossRef', 'DataCite', 'SemanticScholar']},
'TC': {'name': 'Times Cited', 'apis': ['OpenAlex', 'Scopus', 'EuropePMC', 'SemanticScholar']},
'RC': {'name': 'Reference Count', 'apis': ['SemanticScholar']},
'CR': {'name': 'Cited References', 'apis': ['OpenAlex', 'Scopus', 'EuropePMC', 'SemanticScholar']},
'OA': {'name': 'Open Access', 'apis': ['OpenAlex', 'EuropePMC']},
'LI': {'name': 'License', 'apis': ['CrossRef']},
'UR': {'name': 'URL', 'apis': ['CrossRef', 'OpenAlex', 'Scopus', 'EuropePMC', 'SemanticScholar']},
'AI': {'name': 'Additional Information', 'apis': ['EuropePMC']},
'EI': {'name': 'External IDs', 'apis': ['SemanticScholar']}
}
print(f"\n{Fore.CYAN}Checking empty fields in supported areas:{Style.RESET_ALL}")
print("-" * 50)
print(f"{Fore.WHITE}Field Code | Field Name | Supporting APIs | Empty Records{Style.RESET_ALL}")
print("-" * 50)
# Show empty field statistics
empty_stats = {}
dois_with_empty_fields = set()
for field_code, field_info in api_supported_fields.items():
if field_code in merged_df.columns:
empty_mask = (merged_df[field_code].isna()) | (merged_df[field_code].astype(str).str.strip() == '')
empty_count = empty_mask.sum()
empty_percentage = (empty_count / len(merged_df)) * 100
# Choose color based on empty percentage
if empty_percentage == 0:
status_color = Fore.GREEN
elif empty_percentage < 10:
status_color = Fore.CYAN
elif empty_percentage < 30:
status_color = Fore.YELLOW
else:
status_color = Fore.RED
print(f"{Fore.WHITE}{field_code:^10} | {field_info['name']:<30} | {', '.join(field_info['apis']):<50} | {status_color}{empty_count:>5} ({empty_percentage:.2f}%){Style.RESET_ALL}")
empty_stats[field_code] = {
'Initial Empty': empty_count,
'Initial %': empty_percentage,
'Supporting APIs': field_info['apis']
}
if empty_count > 0:
valid_dois = merged_df.loc[
empty_mask &
merged_df['DI'].notna() &
(merged_df['DI'].astype(str).str.strip() != ''),
'DI'
].tolist()
dois_with_empty_fields.update(valid_dois)
print("-" * 50)
total_dois = len(dois_with_empty_fields)
print(f"\n{Fore.GREEN}Found {total_dois} DOIs with empty fields that can be enriched{Style.RESET_ALL}")
# Ask for confirmation
while True:
confirm = input(f"\n{Fore.YELLOW}Do you want to proceed with API enrichment? (Y/N): {Style.RESET_ALL}").strip().upper()
if confirm in ['Y', 'N']:
break
print("Invalid input. Please enter Y or N.")
if confirm == 'Y':
# Perform API enrichment
enriched_df = merged_df.copy()
total_enriched = 0
# Only process DOIs that have empty fields
dois_to_process = list(dois_with_empty_fields)
for idx, row in merged_df.iterrows():
if pd.notna(row.get('DI')) and row['DI'] in dois_to_process:
doi = row['DI']
db_source = row['DB']
print(f"\n{Fore.CYAN}Processing DOI ({total_enriched + 1}/{total_dois}): {Fore.WHITE}{doi}{Style.RESET_ALL}")
print(f"{Fore.YELLOW}Source Database: {db_source}{Style.RESET_ALL}")
# Show empty fields before enrichment
empty_fields = []
for field_code, field_info in api_supported_fields.items():
if field_code in merged_df.columns:
current_value = row[field_code]
if pd.isna(current_value) or str(current_value).strip() == '':
empty_fields.append(f"{field_info['name']} ({field_code}) - Supported by: {', '.join(field_info['apis'])}")
if empty_fields:
print(f"\n{Fore.YELLOW}Empty Fields:{Style.RESET_ALL}")
for field in empty_fields:
print(f" - {field}")
# Perform API enrichment
print(f"\n{Fore.CYAN}Querying APIs...{Style.RESET_ALL}")
enriched_metadata = extract_metadata(doi, row.to_dict())
# Show API sources used
if 'API_Sources' in enriched_metadata:
print(f"\n{Fore.GREEN}API Sources Used:{Style.RESET_ALL}")
for field, source in enriched_metadata['API_Sources'].items():
if field in api_supported_fields:
print(f" - {api_supported_fields[field]['name']} ({field}): {source}")
# Update and track changes
updated_fields = []
for field_code, field_info in api_supported_fields.items():
if field_code in merged_df.columns:
current_value = row[field_code]
new_value = enriched_metadata.get(field_code)
if (pd.isna(current_value) or str(current_value).strip() == '') and pd.notna(new_value) and str(new_value).strip() != '':
enriched_df.loc[idx, field_code] = new_value
api_source = enriched_metadata.get('API_Sources', {}).get(field_code, 'Unknown API')
updated_fields.append(f"{field_info['name']} ({field_code}) from {api_source}")
if updated_fields:
total_enriched += 1
print(f"\n{Fore.GREEN}Updated Fields:{Style.RESET_ALL}")
for field in updated_fields:
print(f" + {field}")
else:
print(f"\n{Fore.YELLOW}No fields were updated{Style.RESET_ALL}")
print("-" * 50)
# Show final enrichment statistics
print(f"\n{Fore.CYAN}Field Enrichment Summary:{Style.RESET_ALL}")
print("-" * 100)
print(f"{Fore.WHITE}Field Code | Field Name | Supporting APIs | Initial Empty | Final Empty | Improvement{Style.RESET_ALL}")
print("-" * 100)
for field_code, field_info in api_supported_fields.items():
if field_code in merged_df.columns:
initial_empty = empty_stats[field_code]['Initial Empty']
initial_percentage = empty_stats[field_code]['Initial %']
final_empty_mask = (enriched_df[field_code].isna()) | (enriched_df[field_code].astype(str).str.strip() == '')
final_empty = final_empty_mask.sum()
final_percentage = (final_empty / len(enriched_df)) * 100
improvement = initial_empty - final_empty
improvement_rate = ((initial_empty - final_empty) / initial_empty * 100) if initial_empty > 0 else 0
print(f"{field_code:^10} | {field_info['name']:<30} | {', '.join(field_info['apis']):<50} | {initial_empty:>5} ({initial_percentage:.2f}%) | {final_empty:>5} ({final_percentage:.2f}%) | {improvement:>5} ({improvement_rate:.2f}%)")
print("-" * 100)
print(f"\n{Fore.GREEN}API enrichment completed. {total_enriched} records updated.{Style.RESET_ALL}")
# Save API enriched results
enriched_df.to_excel(api_enriched_bib, index=False)
convert_excel_to_wos(api_enriched_bib, api_enriched_vos)
# Save API updates to Excel
api_updates_df = pd.DataFrame(columns=['DOI', 'Field', 'Original Value', 'New Value', 'API Source'])
for idx, row in merged_df.iterrows():
doi = row['DI']
for field_code, field_info in api_supported_fields.items():
if field_code in merged_df.columns:
original_value = row[field_code]
new_value = enriched_df.loc[idx, field_code]
if pd.notna(new_value) and (pd.isna(original_value) or str(original_value).strip() == ''):
api_source = enriched_metadata.get('API_Sources', {}).get(field_code, 'Unknown API')
api_updates_df = pd.concat([api_updates_df, pd.DataFrame({
'DOI': [doi],
'Field': [f"{field_info['name']} ({field_code})"],
'Original Value': [original_value],
'New Value': [new_value],
'API Source': [api_source]
})], ignore_index=True)
api_updates_df.to_excel(api_updates, index=False)
# Save API enrichment statistics
stats_df = pd.DataFrame(columns=['Field Code', 'Field Name', 'Initial Empty', 'Final Empty', 'Improvement', 'Improvement Rate', 'Supporting APIs'])
for field_code, field_info in api_supported_fields.items():
if field_code in merged_df.columns:
initial_empty = empty_stats[field_code]['Initial Empty']
initial_percentage = empty_stats[field_code]['Initial %']
final_empty_mask = (enriched_df[field_code].isna()) | (enriched_df[field_code].astype(str).str.strip() == '')
final_empty = final_empty_mask.sum()
final_percentage = (final_empty / len(enriched_df)) * 100
improvement = initial_empty - final_empty
improvement_rate = ((initial_empty - final_empty) / initial_empty * 100) if initial_empty > 0 else 0
stats_df = pd.concat([stats_df, pd.DataFrame({
'Field Code': [field_code],
'Field Name': [field_info['name']],
'Initial Empty': [initial_empty],
'Initial %': [initial_percentage],
'Final Empty': [final_empty],
'Final %': [final_percentage],
'Improvement': [improvement],
'Improvement Rate': [f"{improvement_rate:.2f}%"],
'Supporting APIs': [', '.join(field_info['apis'])]
})], ignore_index=True)
# Save comprehensive statistics for enriched data
enriched_stats = {
'WoS Statistics': wos_stats,
'Scopus Statistics': scopus_stats,
'Merge Statistics': enhanced_stats,
'API Enrichment Statistics': {
'Total Records': len(enriched_df),
'Enriched Records': total_enriched,
'Field Statistics': stats_df.to_dict('records')
}
}
# Save statistics to Excel
with pd.ExcelWriter(stats_excel, engine='openpyxl') as writer:
# Original statistics
pd.DataFrame([wos_stats]).to_excel(writer, sheet_name='WoS Stats', index=False)
pd.DataFrame([scopus_stats]).to_excel(writer, sheet_name='Scopus Stats', index=False)
pd.DataFrame([enhanced_stats]).to_excel(writer, sheet_name='Merge Stats', index=False)
# API enrichment statistics
stats_df.to_excel(writer, sheet_name='API Enrichment Stats', index=False)
api_updates_df.to_excel(writer, sheet_name='API Updates', index=False)
# Save API log
with open(api_log, 'w', encoding='utf-8') as f:
f.write("API Enrichment Log\n")
f.write("=================\n\n")
f.write(f"Total Records: {len(enriched_df)}\n")
f.write(f"Enriched Records: {total_enriched}\n\n")
f.write("Field Enrichment Summary:\n")
f.write("----------------------\n")
for _, row in stats_df.iterrows():
f.write(f"\nField: {row['Field Name']} ({row['Field Code']})\n")
f.write(f"Initial Empty: {row['Initial Empty']} ({row['Initial %']:.2f}%)\n")
f.write(f"Final Empty: {row['Final Empty']} ({row['Final %']:.2f}%)\n")
f.write(f"Improvement: {row['Improvement']} records ({row['Improvement Rate']})\n")
f.write(f"Supporting APIs: {row['Supporting APIs']}\n")
f.write("-" * 50 + "\n")
# After API enrichment is complete, ask about ML enrichment
while True:
ml_confirm = input(f"\n{Fore.YELLOW}Would you like to perform ML enrichment after API enrichment? (Y/N): {Style.RESET_ALL}").strip().upper()
if ml_confirm in ['Y', 'N']:
break
print("Invalid input. Please enter Y or N.")
if ml_confirm == 'Y':
# Proceed with ML enrichment using original merged file
print(f"\nPerforming ML enrichment using original merged data...")
merged_df = pd.read_excel(merged_bib) # Always use original data
# Show ML enrichment capabilities
print(f"\n{Fore.CYAN}ML Enrichment Capabilities:{Style.RESET_ALL}")
print("-" * 50)
print(f"{Fore.WHITE}Field | Training Data | Empty Records | Model Type{Style.RESET_ALL}")
print("-" * 50)
# Check training data availability
de_train = len(merged_df[merged_df['DE'].notna()])
id_train = len(merged_df[merged_df['ID'].notna()]) # Add ID training data count
sc_train = len(merged_df[merged_df['SC'].notna()])
de_empty = merged_df['DE'].isna().sum()
id_empty = merged_df['ID'].isna().sum() # Add ID empty count
sc_empty = merged_df['SC'].isna().sum()
# Show DE stats with color coding
if de_train > 100:
train_color = Fore.GREEN
elif de_train > 50:
train_color = Fore.YELLOW
else:
train_color = Fore.RED
if de_empty == 0:
empty_color = Fore.GREEN
elif de_empty < len(merged_df) * 0.3:
empty_color = Fore.YELLOW
else:
empty_color = Fore.RED
print(f"{Fore.WHITE}DE | {train_color}{de_train:>5} records{Style.RESET_ALL} | {empty_color}{de_empty:>5} records{Style.RESET_ALL} | TF-IDF + RandomForest")
# Show ID stats with color coding
if id_train > 100:
train_color = Fore.GREEN
elif id_train > 50:
train_color = Fore.YELLOW
else:
train_color = Fore.RED
if id_empty == 0:
empty_color = Fore.GREEN
elif id_empty < len(merged_df) * 0.3:
empty_color = Fore.YELLOW
else:
empty_color = Fore.RED
print(f"{Fore.WHITE}ID | {train_color}{id_train:>5} records{Style.RESET_ALL} | {empty_color}{id_empty:>5} records{Style.RESET_ALL} | TF-IDF + RandomForest")
# Show SC stats with color coding
if sc_train > 100:
train_color = Fore.GREEN
elif sc_train > 50:
train_color = Fore.YELLOW
else:
train_color = Fore.RED
if sc_empty == 0:
empty_color = Fore.GREEN
elif sc_empty < len(merged_df) * 0.3:
empty_color = Fore.YELLOW
else:
empty_color = Fore.RED
print(f"{Fore.WHITE}SC | {train_color}{sc_train:>5} records{Style.RESET_ALL} | {empty_color}{sc_empty:>5} records{Style.RESET_ALL} | TF-IDF + RandomForest")
print("-" * 50)
# Show warning if training data is insufficient
if de_train < 10 and id_train < 10 and sc_train < 10: # Updated condition to include ID
print(f"\n{Fore.RED}Warning: Insufficient training data for all fields.{Style.RESET_ALL}")
print("ML enrichment requires at least 10 records with known values.")
print("Consider using API enrichment instead.")
continue
# Ask for confirmation
while True:
ml_confirm = input(f"\n{Fore.YELLOW}Do you want to proceed with ML enrichment? (Y/N): {Style.RESET_ALL}").strip().upper()
if ml_confirm in ['Y', 'N']:
break
print("Invalid input. Please enter Y or N.")
if ml_confirm == 'Y':
try:
# Perform ML enrichment with progress indicator
with ProcessIndicator("Training and applying ML models") as indicator:
enriched_df, ml_stats = enrich_metadata_ml(merged_df)
# Save ML enriched results
enriched_df.to_excel(ml_enriched_bib, index=False)
convert_excel_to_wos(ml_enriched_bib, ml_enriched_vos)
# Create ML statistics DataFrame
ml_stats_df = pd.DataFrame([{
'Metric': 'Total Records',
'Value': ml_stats['total_records']
}, {
'Metric': 'Original Empty Keywords (DE)',
'Value': ml_stats['original_empty_keywords']
}, {
'Metric': 'Original Empty Keywords Plus (ID)',
'Value': ml_stats['original_empty_id']
}, {
'Metric': 'Original Empty Subjects (SC)',
'Value': ml_stats['original_empty_subjects']
}, {
'Metric': 'Remaining Empty Keywords (DE)',
'Value': ml_stats['enriched_empty_keywords']
}, {
'Metric': 'Remaining Empty Keywords Plus (ID)',
'Value': ml_stats['enriched_empty_id']
}, {
'Metric': 'Remaining Empty Subjects (SC)',
'Value': ml_stats['enriched_empty_subjects']
}, {
'Metric': 'Keywords (DE) Filled',
'Value': ml_stats['keywords_filled']
}, {
'Metric': 'Keywords Plus (ID) Filled',
'Value': ml_stats['id_filled']
}, {
'Metric': 'Subjects (SC) Filled',
'Value': ml_stats['subjects_filled']
}, {
'Metric': 'Keywords Fill Rate (%)',
'Value': round((ml_stats['keywords_filled'] / ml_stats['original_empty_keywords'] * 100), 2) if ml_stats['original_empty_keywords'] > 0 else 0
}, {
'Metric': 'Keywords Plus Fill Rate (%)',
'Value': round((ml_stats['id_filled'] / ml_stats['original_empty_id'] * 100), 2) if ml_stats['original_empty_id'] > 0 else 0
}, {
'Metric': 'Subjects Fill Rate (%)',
'Value': round((ml_stats['subjects_filled'] / ml_stats['original_empty_subjects'] * 100), 2) if ml_stats['original_empty_subjects'] > 0 else 0
}])
# Save ML statistics to Excel
ml_stats_df.to_excel(ml_updates, index=False)
# Show enrichment results
print(f"\n{Fore.CYAN}ML Enrichment Results:{Style.RESET_ALL}")
print(f"Keywords (DE) filled: {Fore.GREEN}{ml_stats['keywords_filled']}{Style.RESET_ALL} out of {ml_stats['original_empty_keywords']}")
print(f"Keywords Plus (ID) filled: {Fore.GREEN}{ml_stats['id_filled']}{Style.RESET_ALL} out of {ml_stats['original_empty_id']}")
print(f"Subject Categories (SC) filled: {Fore.GREEN}{ml_stats['subjects_filled']}{Style.RESET_ALL} out of {ml_stats['original_empty_subjects']}")
# After ML enrichment is complete, ask about API enrichment
while True:
api_confirm = input(f"\n{Fore.YELLOW}Would you like to perform API enrichment after ML enrichment? (Y/N): {Style.RESET_ALL}").strip().upper()
if api_confirm in ['Y', 'N']:
break
print("Invalid input. Please enter Y or N.")
if api_confirm == 'Y':
# Proceed with API enrichment using original merged file
print(f"\nPerforming API enrichment using original merged data...")
merged_df = pd.read_excel(merged_bib) # Her zaman orijinal dosyayı kullan
# API enrichment code
from Main.modules.api_utils import extract_metadata
from Main.modules.merge_utils import enrich_with_api
# Perform API enrichment
api_success, api_stats, enriched_df = enrich_with_api(
merged_df,
api_log=api_log,
api_updates=api_updates,
result_dir=analysis_dir
)
if api_success:
print("\nAPI enrichment completed successfully.")
else:
print("\nAPI enrichment process encountered some issues.")
# Save comprehensive statistics
all_stats = {
'WoS Statistics': wos_stats,
'Scopus Statistics': scopus_stats,
'Merge Statistics': enhanced_stats,
'ML Statistics': ml_stats,
'API Statistics': api_stats if api_success else {}
}
save_comprehensive_statistics(all_stats, wos_df, scopus_df, enriched_df, stats_excel)
print("Statistics completed.")
except Exception as e:
print(f"\nError during ML enrichment: {str(e)}")
print("Please try API enrichment instead.")
else:
print("\nML enrichment cancelled.")
break
else:
print("\nAPI enrichment cancelled.")
break
elif choice == "2":
# ML Enrichment
print("\nPerforming ML enrichment (Experimental)...")
from Main.modules.ml_utils import enrich_metadata_ml
# Read the merged file
merged_df = pd.read_excel(merged_bib)
# Show ML enrichment capabilities
print(f"\n{Fore.CYAN}ML Enrichment Capabilities:{Style.RESET_ALL}")
print("-" * 50)
print(f"{Fore.WHITE}Field | Training Data | Empty Records | Model Type{Style.RESET_ALL}")
print("-" * 50)
# Check training data availability
de_train = len(merged_df[merged_df['DE'].notna()])
id_train = len(merged_df[merged_df['ID'].notna()]) # Add ID training data count
sc_train = len(merged_df[merged_df['SC'].notna()])
de_empty = merged_df['DE'].isna().sum()
id_empty = merged_df['ID'].isna().sum() # Add ID empty count
sc_empty = merged_df['SC'].isna().sum()
# Show DE stats with color coding
if de_train > 100:
train_color = Fore.GREEN
elif de_train > 50:
train_color = Fore.YELLOW
else:
train_color = Fore.RED
if de_empty == 0:
empty_color = Fore.GREEN
elif de_empty < len(merged_df) * 0.3:
empty_color = Fore.YELLOW
else:
empty_color = Fore.RED
print(f"{Fore.WHITE}DE | {train_color}{de_train:>5} records{Style.RESET_ALL} | {empty_color}{de_empty:>5} records{Style.RESET_ALL} | TF-IDF + RandomForest")
# Show ID stats with color coding
if id_train > 100:
train_color = Fore.GREEN
elif id_train > 50:
train_color = Fore.YELLOW
else:
train_color = Fore.RED
if id_empty == 0:
empty_color = Fore.GREEN
elif id_empty < len(merged_df) * 0.3:
empty_color = Fore.YELLOW
else:
empty_color = Fore.RED
print(f"{Fore.WHITE}ID | {train_color}{id_train:>5} records{Style.RESET_ALL} | {empty_color}{id_empty:>5} records{Style.RESET_ALL} | TF-IDF + RandomForest")
# Show SC stats with color coding
if sc_train > 100:
train_color = Fore.GREEN
elif sc_train > 50:
train_color = Fore.YELLOW
else:
train_color = Fore.RED
if sc_empty == 0:
empty_color = Fore.GREEN
elif sc_empty < len(merged_df) * 0.3:
empty_color = Fore.YELLOW
else:
empty_color = Fore.RED
print(f"{Fore.WHITE}SC | {train_color}{sc_train:>5} records{Style.RESET_ALL} | {empty_color}{sc_empty:>5} records{Style.RESET_ALL} | TF-IDF + RandomForest")
print("-" * 50)
# Show warning if training data is insufficient
if de_train < 10 and id_train < 10 and sc_train < 10: # Updated condition to include ID
print(f"\n{Fore.RED}Warning: Insufficient training data for all fields.{Style.RESET_ALL}")
print("ML enrichment requires at least 10 records with known values.")
print("Consider using API enrichment instead.")
continue
# Ask for confirmation
while True:
confirm = input(f"\n{Fore.YELLOW}Do you want to proceed with ML enrichment? (Y/N): {Style.RESET_ALL}").strip().upper()
if confirm in ['Y', 'N']:
break
print("Invalid input. Please enter Y or N.")
if confirm == 'Y':
try:
# Perform ML enrichment with progress indicator
with ProcessIndicator("Training and applying ML models") as indicator:
enriched_df, ml_stats = enrich_metadata_ml(merged_df)
# Save ML enriched results
enriched_df.to_excel(ml_enriched_bib, index=False)
convert_excel_to_wos(ml_enriched_bib, ml_enriched_vos)
# Create ML statistics DataFrame
ml_stats_df = pd.DataFrame([{
'Metric': 'Total Records',
'Value': ml_stats['total_records']
}, {
'Metric': 'Original Empty Keywords (DE)',
'Value': ml_stats['original_empty_keywords']
}, {
'Metric': 'Original Empty Keywords Plus (ID)',
'Value': ml_stats['original_empty_id']
}, {
'Metric': 'Original Empty Subjects (SC)',
'Value': ml_stats['original_empty_subjects']
}, {
'Metric': 'Remaining Empty Keywords (DE)',
'Value': ml_stats['enriched_empty_keywords']
}, {
'Metric': 'Remaining Empty Keywords Plus (ID)',
'Value': ml_stats['enriched_empty_id']
}, {
'Metric': 'Remaining Empty Subjects (SC)',
'Value': ml_stats['enriched_empty_subjects']
}, {
'Metric': 'Keywords (DE) Filled',
'Value': ml_stats['keywords_filled']
}, {
'Metric': 'Keywords Plus (ID) Filled',
'Value': ml_stats['id_filled']
}, {
'Metric': 'Subjects (SC) Filled',
'Value': ml_stats['subjects_filled']
}, {
'Metric': 'Keywords Fill Rate (%)',
'Value': round((ml_stats['keywords_filled'] / ml_stats['original_empty_keywords'] * 100), 2) if ml_stats['original_empty_keywords'] > 0 else 0
}, {
'Metric': 'Keywords Plus Fill Rate (%)',
'Value': round((ml_stats['id_filled'] / ml_stats['original_empty_id'] * 100), 2) if ml_stats['original_empty_id'] > 0 else 0
}, {
'Metric': 'Subjects Fill Rate (%)',
'Value': round((ml_stats['subjects_filled'] / ml_stats['original_empty_subjects'] * 100), 2) if ml_stats['original_empty_subjects'] > 0 else 0
}])
# Save ML statistics to Excel
ml_stats_df.to_excel(ml_updates, index=False)
# Show enrichment results
print(f"\n{Fore.CYAN}ML Enrichment Results:{Style.RESET_ALL}")
print(f"Keywords (DE) filled: {Fore.GREEN}{ml_stats['keywords_filled']}{Style.RESET_ALL} out of {ml_stats['original_empty_keywords']}")
print(f"Keywords Plus (ID) filled: {Fore.GREEN}{ml_stats['id_filled']}{Style.RESET_ALL} out of {ml_stats['original_empty_id']}")
print(f"Subject Categories (SC) filled: {Fore.GREEN}{ml_stats['subjects_filled']}{Style.RESET_ALL} out of {ml_stats['original_empty_subjects']}")
# After ML enrichment is complete, ask about API enrichment
while True:
api_confirm = input(f"\n{Fore.YELLOW}Would you like to perform API enrichment after ML enrichment? (Y/N): {Style.RESET_ALL}").strip().upper()
if api_confirm in ['Y', 'N']:
break
print("Invalid input. Please enter Y or N.")
if api_confirm == 'Y':
# Proceed with API enrichment using original merged file
print(f"\nPerforming API enrichment using original merged data...")
merged_df = pd.read_excel(merged_bib) # Her zaman orijinal dosyayı kullan
# API enrichment code
from Main.modules.api_utils import extract_metadata
from Main.modules.merge_utils import enrich_with_api
# Perform API enrichment
api_success, api_stats, enriched_df = enrich_with_api(
merged_df,
api_log=api_log,
api_updates=api_updates,
result_dir=analysis_dir
)
if api_success:
print("\nAPI enrichment completed successfully.")
else:
print("\nAPI enrichment process encountered some issues.")
# Save comprehensive statistics
all_stats = {
'WoS Statistics': wos_stats,
'Scopus Statistics': scopus_stats,
'Merge Statistics': enhanced_stats,
'ML Statistics': ml_stats,
'API Statistics': api_stats if api_success else {}
}
save_comprehensive_statistics(all_stats, wos_df, scopus_df, enriched_df, stats_excel)
print("Statistics completed.")
except Exception as e:
print(f"\nError during ML enrichment: {str(e)}")
print("Please try API enrichment instead.")
else:
print("\nML enrichment cancelled.")
break
elif choice == "3":
# Combined API + ML Enrichment
print("\nPerforming API enrichment followed by ML enrichment...")
# First perform API enrichment using existing logic
from Main.modules.api_utils import extract_metadata
from Main.modules.merge_utils import enrich_with_api
# Read the merged file
merged_df = pd.read_excel(merged_bib)
# API supported fields and which APIs support which fields
api_supported_fields = {
'DT': {'name': 'Document Type', 'apis': ['CrossRef', 'OpenAlex', 'Scopus', 'DataCite', 'EuropePMC']},
'TI': {'name': 'Title', 'apis': ['CrossRef', 'OpenAlex', 'Scopus', 'DataCite', 'EuropePMC', 'SemanticScholar']},
'AU': {'name': 'Author', 'apis': ['CrossRef', 'OpenAlex', 'Scopus', 'DataCite', 'EuropePMC', 'SemanticScholar']},