Delete transform_schema.py
Browse files- transform_schema.py +0 -133
transform_schema.py
DELETED
|
@@ -1,133 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
"""
|
| 3 |
-
Transform existing arxiv_citations parquet to new grouped schema.
|
| 4 |
-
|
| 5 |
-
Old schema:
|
| 6 |
-
cited_by: [{doi, raw_match, reference}, ...]
|
| 7 |
-
|
| 8 |
-
New schema:
|
| 9 |
-
reference_count: total reference instances
|
| 10 |
-
citation_count: unique citing works
|
| 11 |
-
cited_by: [{doi, matches: [{raw_match, reference}, ...]}, ...]
|
| 12 |
-
"""
|
| 13 |
-
|
| 14 |
-
import json
|
| 15 |
-
from collections import defaultdict
|
| 16 |
-
from pathlib import Path
|
| 17 |
-
|
| 18 |
-
import pyarrow as pa
|
| 19 |
-
import pyarrow.parquet as pq
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
def transform_record(record: dict) -> dict:
|
| 23 |
-
"""Transform a single record to the new grouped schema."""
|
| 24 |
-
# Group citations by citing DOI
|
| 25 |
-
citing_works = defaultdict(list)
|
| 26 |
-
|
| 27 |
-
for citation in record.get('cited_by', []):
|
| 28 |
-
doi = citation.get('doi', '')
|
| 29 |
-
match_entry = {
|
| 30 |
-
'raw_match': citation.get('raw_match', ''),
|
| 31 |
-
'reference': citation.get('reference', ''), # Already JSON string
|
| 32 |
-
}
|
| 33 |
-
citing_works[doi].append(match_entry)
|
| 34 |
-
|
| 35 |
-
# Convert to new format
|
| 36 |
-
cited_by = [
|
| 37 |
-
{'doi': doi, 'matches': matches}
|
| 38 |
-
for doi, matches in citing_works.items()
|
| 39 |
-
]
|
| 40 |
-
|
| 41 |
-
return {
|
| 42 |
-
'arxiv_doi': record.get('arxiv_doi', ''),
|
| 43 |
-
'arxiv_id': record.get('arxiv_id', ''),
|
| 44 |
-
'reference_count': sum(len(cw['matches']) for cw in cited_by),
|
| 45 |
-
'citation_count': len(cited_by),
|
| 46 |
-
'cited_by': cited_by,
|
| 47 |
-
}
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
def main():
|
| 51 |
-
input_path = Path('data/arxiv_citations_valid.parquet')
|
| 52 |
-
output_path = Path('data/arxiv_citations_valid_v2.parquet')
|
| 53 |
-
|
| 54 |
-
print(f"Reading {input_path}...")
|
| 55 |
-
table = pq.read_table(input_path)
|
| 56 |
-
records = table.to_pylist()
|
| 57 |
-
print(f"Loaded {len(records)} records")
|
| 58 |
-
|
| 59 |
-
print("Transforming to new schema...")
|
| 60 |
-
transformed = []
|
| 61 |
-
total_refs = 0
|
| 62 |
-
total_citations = 0
|
| 63 |
-
|
| 64 |
-
for i, record in enumerate(records):
|
| 65 |
-
new_record = transform_record(record)
|
| 66 |
-
transformed.append(new_record)
|
| 67 |
-
total_refs += new_record['reference_count']
|
| 68 |
-
total_citations += new_record['citation_count']
|
| 69 |
-
|
| 70 |
-
if (i + 1) % 100000 == 0:
|
| 71 |
-
print(f" Processed {i + 1} records...")
|
| 72 |
-
|
| 73 |
-
print(f"Transformation complete:")
|
| 74 |
-
print(f" Total arXiv works: {len(transformed)}")
|
| 75 |
-
print(f" Total references: {total_refs}")
|
| 76 |
-
print(f" Unique citing works: {total_citations}")
|
| 77 |
-
|
| 78 |
-
# Define new schema
|
| 79 |
-
schema = pa.schema([
|
| 80 |
-
('arxiv_doi', pa.string()),
|
| 81 |
-
('arxiv_id', pa.string()),
|
| 82 |
-
('reference_count', pa.int64()),
|
| 83 |
-
('citation_count', pa.int64()),
|
| 84 |
-
('cited_by', pa.list_(pa.struct([
|
| 85 |
-
('doi', pa.string()),
|
| 86 |
-
('matches', pa.list_(pa.struct([
|
| 87 |
-
('raw_match', pa.string()),
|
| 88 |
-
('reference', pa.string()),
|
| 89 |
-
]))),
|
| 90 |
-
]))),
|
| 91 |
-
])
|
| 92 |
-
|
| 93 |
-
print(f"Writing {output_path}...")
|
| 94 |
-
|
| 95 |
-
# Convert to Arrow format
|
| 96 |
-
arxiv_dois = [r['arxiv_doi'] for r in transformed]
|
| 97 |
-
arxiv_ids = [r['arxiv_id'] for r in transformed]
|
| 98 |
-
reference_counts = [r['reference_count'] for r in transformed]
|
| 99 |
-
citation_counts = [r['citation_count'] for r in transformed]
|
| 100 |
-
cited_by_lists = [r['cited_by'] for r in transformed]
|
| 101 |
-
|
| 102 |
-
table = pa.table({
|
| 103 |
-
'arxiv_doi': arxiv_dois,
|
| 104 |
-
'arxiv_id': arxiv_ids,
|
| 105 |
-
'reference_count': reference_counts,
|
| 106 |
-
'citation_count': citation_counts,
|
| 107 |
-
'cited_by': cited_by_lists,
|
| 108 |
-
}, schema=schema)
|
| 109 |
-
|
| 110 |
-
pq.write_table(table, output_path, compression='snappy')
|
| 111 |
-
|
| 112 |
-
output_size = output_path.stat().st_size / (1024 * 1024)
|
| 113 |
-
print(f"Done! Output size: {output_size:.1f} MB")
|
| 114 |
-
|
| 115 |
-
# Verify by reading back
|
| 116 |
-
print("\nVerifying output...")
|
| 117 |
-
verify_table = pq.read_table(output_path)
|
| 118 |
-
print(f" Rows: {verify_table.num_rows}")
|
| 119 |
-
print(f" Schema: {verify_table.schema}")
|
| 120 |
-
|
| 121 |
-
# Show sample record
|
| 122 |
-
sample = verify_table.to_pylist()[0]
|
| 123 |
-
print(f"\nSample record:")
|
| 124 |
-
print(f" arxiv_doi: {sample['arxiv_doi']}")
|
| 125 |
-
print(f" arxiv_id: {sample['arxiv_id']}")
|
| 126 |
-
print(f" reference_count: {sample['reference_count']}")
|
| 127 |
-
print(f" citation_count: {sample['citation_count']}")
|
| 128 |
-
print(f" First citing work: {sample['cited_by'][0]['doi']}")
|
| 129 |
-
print(f" Matches in first citing work: {len(sample['cited_by'][0]['matches'])}")
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
if __name__ == '__main__':
|
| 133 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|