forked from CenterForOpenScience/osf.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmigrate_funder_ids_to_ror.py
More file actions
348 lines (288 loc) · 13.5 KB
/
migrate_funder_ids_to_ror.py
File metadata and controls
348 lines (288 loc) · 13.5 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
#!/usr/bin/env python3
"""
Management command to migrate Crossref Funder IDs to ROR IDs.
This script reads a CSV mapping file and updates all GuidMetadataRecord entries
that have funding_info with Crossref Funder IDs, converting them to ROR IDs.
Usage:
# Dry run (recommended first)
python manage.py migrate_funder_ids_to_ror --csv-file /path/to/mapping.csv --dry-run
# Actual migration
python manage.py migrate_funder_ids_to_ror --csv-file /path/to/mapping.csv
CSV Format Expected (tab or comma separated):
Funder Name, ror ID, ROR name, Crossref DOI, Funder ID
Example:
National Science Foundation, https://ror.org/021nxhr62, National Science Foundation, http://dx.doi.org/10.13039/100000001, 100000001
"""
import csv
import logging
import re
from django.core.management.base import BaseCommand
from django.db import transaction
from osf.models import GuidMetadataRecord
logger = logging.getLogger(__name__)
class Command(BaseCommand):
help = 'Migrate Crossref Funder IDs to ROR IDs in GuidMetadataRecord.funding_info'
def add_arguments(self, parser):
parser.add_argument(
'--csv-file',
type=str,
required=True,
help='Path to the CSV file containing the Crossref to ROR mapping.',
)
parser.add_argument(
'--dry-run',
action='store_true',
dest='dry_run',
help='Run without making any changes to the database.',
)
parser.add_argument(
'--batch-size',
type=int,
default=1000,
help='Number of records to process in each batch (default: 1000).',
)
parser.add_argument(
'--update-funder-name',
action='store_true',
dest='update_funder_name',
help='Also update funder_name to the ROR name from the mapping.',
)
parser.add_argument(
'--skip-reindex',
action='store_true',
dest='skip_reindex',
help='Skip triggering SHARE/DataCite re-indexing after migration. '
'Use this if you plan to run recatalog_metadata separately.',
)
def handle(self, *args, **options):
csv_file = options['csv_file']
dry_run = options['dry_run']
batch_size = options['batch_size']
update_funder_name = options['update_funder_name']
reindex = not options['skip_reindex']
if dry_run:
self.stdout.write(self.style.WARNING('[DRY RUN] No changes will be made to the database.'))
if not reindex:
self.stdout.write(self.style.WARNING('Re-indexing is disabled. Run recatalog_metadata after migration.'))
# Load the mapping
mapping = self.load_mapping(csv_file)
if not mapping:
self.stdout.write(self.style.ERROR('No valid mappings found in CSV file.'))
return
self.stdout.write(f'Loaded {len(mapping)} Crossref to ROR mappings.')
# Find and update records
stats = self.migrate_records(mapping, dry_run, batch_size, update_funder_name, reindex)
# Print summary
self.stdout.write('\n' + '=' * 60)
self.stdout.write(self.style.SUCCESS('Migration Summary:'))
self.stdout.write(f" Records scanned: {stats['scanned']}")
self.stdout.write(f" Records updated: {stats['updated']}")
self.stdout.write(f" Records re-indexed: {stats['reindexed']}")
self.stdout.write(f" Funders migrated: {stats['funders_migrated']}")
self.stdout.write(f" Unmapped funders removed: {stats['not_in_mapping']}")
if stats['errors']:
self.stdout.write(self.style.ERROR(f" Errors: {stats['errors']}"))
if stats['unmapped_ids']:
self.stdout.write('\nUnmapped Crossref Funder IDs (not in CSV):')
for funder_id in sorted(stats['unmapped_ids'])[:50]: # Show first 50
self.stdout.write(f' - {funder_id}')
if len(stats['unmapped_ids']) > 50:
self.stdout.write(f' ... and {len(stats["unmapped_ids"]) - 50} more')
def load_mapping(self, csv_file):
"""Load the Crossref to ROR mapping from CSV file.
Returns a dict mapping various forms of Crossref ID to ROR info:
{
'100000001': {'ror_id': 'https://ror.org/021nxhr62', 'ror_name': 'National Science Foundation'},
'http://dx.doi.org/10.13039/100000001': {...},
'https://doi.org/10.13039/100000001': {...},
...
}
"""
mapping = {}
try:
with open(csv_file, 'r', encoding='utf-8-sig') as f:
# Try to detect delimiter
sample = f.read(2048)
f.seek(0)
if '\t' in sample:
delimiter = '\t'
else:
delimiter = ','
reader = csv.DictReader(f, delimiter=delimiter)
# Normalize column names (handle various formats)
for row in reader:
# Try to find the relevant columns
ror_id = None
ror_name = None
crossref_doi = None
funder_id = None
for key, value in row.items():
if not key:
continue
key_lower = key.lower().strip()
if 'ror' in key_lower and 'id' in key_lower and 'ror_name' not in key_lower:
ror_id = value.strip() if value else None
elif 'ror' in key_lower and 'name' in key_lower:
ror_name = value.strip() if value else None
elif 'crossref' in key_lower and 'doi' in key_lower:
crossref_doi = value.strip() if value else None
elif key_lower == 'funder id' or key_lower == 'funder_id':
funder_id = value.strip() if value else None
if not ror_id:
continue
ror_info = {
'ror_id': ror_id,
'ror_name': ror_name,
}
# Add mappings for various ID formats
if funder_id:
mapping[funder_id] = ror_info
# Also add with various DOI prefixes
mapping[f'http://dx.doi.org/10.13039/{funder_id}'] = ror_info
mapping[f'https://doi.org/10.13039/{funder_id}'] = ror_info
mapping[f'10.13039/{funder_id}'] = ror_info
if crossref_doi:
mapping[crossref_doi] = ror_info
# Normalize the DOI URL
if crossref_doi.startswith('http://'):
mapping[crossref_doi.replace('http://', 'https://')] = ror_info
elif crossref_doi.startswith('https://'):
mapping[crossref_doi.replace('https://', 'http://')] = ror_info
except FileNotFoundError:
self.stdout.write(self.style.ERROR(f'CSV file not found: {csv_file}'))
return None
except Exception as e:
self.stdout.write(self.style.ERROR(f'Error reading CSV file: {e}'))
return None
return mapping
def extract_funder_id(self, identifier):
"""Extract the numeric funder ID from various identifier formats."""
if not identifier:
return None
# Already just a number
if re.match(r'^\d+$', identifier):
return identifier
# Extract from DOI URL (e.g., http://dx.doi.org/10.13039/100000001)
match = re.search(r'10\.13039/(\d+)', identifier)
if match:
return match.group(1)
return identifier
def migrate_records(self, mapping, dry_run, batch_size, update_funder_name, reindex):
"""Find and migrate all GuidMetadataRecord entries with Crossref Funder IDs."""
stats = {
'scanned': 0,
'updated': 0,
'reindexed': 0,
'funders_migrated': 0,
'not_in_mapping': 0,
'errors': 0,
'unmapped_ids': set(),
}
# Query records that have non-empty funding_info
# We need to check if any funder has 'Crossref Funder ID' type
queryset = GuidMetadataRecord.objects.exclude(funding_info=[]).exclude(funding_info__isnull=True)
total_count = queryset.count()
self.stdout.write(f'Found {total_count} records with funding_info to scan.')
processed = 0
for record in queryset.iterator(chunk_size=batch_size):
stats['scanned'] += 1
processed += 1
if processed % 500 == 0:
self.stdout.write(f' Processed {processed}/{total_count} records...')
try:
updated, funder_stats = self.migrate_record(record, mapping, dry_run, update_funder_name)
if updated:
stats['updated'] += 1
if reindex and not dry_run:
try:
self.reindex_record(record)
stats['reindexed'] += 1
except Exception as e:
logger.error(f'Error re-indexing record {record.guid._id}: {e}')
stats['funders_migrated'] += funder_stats['migrated']
stats['not_in_mapping'] += funder_stats['not_found']
stats['unmapped_ids'].update(funder_stats['unmapped_ids'])
except Exception as e:
stats['errors'] += 1
logger.error(f'Error migrating record {record.guid._id}: {e}')
return stats
def migrate_record(self, record, mapping, dry_run, update_funder_name):
"""Migrate a single GuidMetadataRecord's funding_info.
Returns (was_updated, funder_stats)
"""
funder_stats = {
'migrated': 0,
'not_found': 0,
'unmapped_ids': set(),
}
if not record.funding_info:
return False, funder_stats
updated_funding_info = []
record_modified = False
for funder in record.funding_info:
funder_type = funder.get('funder_identifier_type', '')
funder_identifier = funder.get('funder_identifier', '')
# Only migrate Crossref Funder IDs (includes legacy 'Crossref Funder URI' type)
if funder_type not in ('Crossref Funder ID', 'Crossref Funder URI'):
updated_funding_info.append(funder)
continue
# Try to find in mapping
ror_info = None
# Try exact match first
if funder_identifier in mapping:
ror_info = mapping[funder_identifier]
else:
# Try to extract numeric ID and look up
numeric_id = self.extract_funder_id(funder_identifier)
if numeric_id and numeric_id in mapping:
ror_info = mapping[numeric_id]
if ror_info:
# Create updated funder entry
updated_funder = funder.copy()
updated_funder['funder_identifier'] = ror_info['ror_id']
updated_funder['funder_identifier_type'] = 'ROR'
if update_funder_name and ror_info.get('ror_name'):
updated_funder['funder_name'] = ror_info['ror_name']
updated_funding_info.append(updated_funder)
record_modified = True
funder_stats['migrated'] += 1
logger.info(
f'{"[DRY RUN] " if dry_run else ""}'
f'Migrating funder in {record.guid._id}: '
f'{funder_identifier} -> {ror_info["ror_id"]}'
)
else:
# No mapping found, remove unmapped Crossref funder
record_modified = True
funder_stats['not_found'] += 1
funder_stats['unmapped_ids'].add(funder_identifier)
logger.warning(
f'{"[DRY RUN] " if dry_run else ""}'
f'Removing unmapped Crossref Funder ID: {funder_identifier} '
f'from record {record.guid._id}'
)
# Warn about duplicate ROR IDs that would result from migration
if record_modified:
ror_identifiers = [
f['funder_identifier']
for f in updated_funding_info
if f.get('funder_identifier_type') == 'ROR'
]
seen = set()
duplicates = {rid for rid in ror_identifiers if rid in seen or seen.add(rid)}
if duplicates:
logger.warning(
f'Record {record.guid._id} has duplicate ROR IDs after migration: {duplicates}'
)
if record_modified and not dry_run:
with transaction.atomic():
record.funding_info = updated_funding_info
record.save(update_fields=['funding_info'])
return record_modified, funder_stats
def reindex_record(self, record):
"""Trigger SHARE/ElasticSearch and DataCite re-indexing for the record's referent."""
referent = record.guid.referent
if hasattr(referent, 'update_search'):
referent.update_search()
if hasattr(referent, 'request_identifier_update'):
referent.request_identifier_update('doi')