Add TIMSS processing script
Browse files
scripts/download_timss_locally.py
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""
|
| 3 |
+
TIMSS 2019 Processing Script
|
| 4 |
+
=============================
|
| 5 |
+
TIMSS data requires manual download from:
|
| 6 |
+
https://timssandpirls.bc.edu/timss2019/international-database/
|
| 7 |
+
|
| 8 |
+
Steps:
|
| 9 |
+
1. Go to URL above
|
| 10 |
+
2. Download SPSS data files for your countries of interest
|
| 11 |
+
3. Extract the zip
|
| 12 |
+
4. Run: python download_timss_locally.py <path_to_extracted_data>
|
| 13 |
+
|
| 14 |
+
Key files in download:
|
| 15 |
+
BSA*.sav = Student Achievement (item scores)
|
| 16 |
+
BSG*.sav = Student Background (demographics)
|
| 17 |
+
|
| 18 |
+
Merge on IDCNTRY + IDSTUD for DIF analysis.
|
| 19 |
+
ITSEX: 1=Girl, 2=Boy
|
| 20 |
+
Item columns: M071001, S071001, etc.
|
| 21 |
+
"""
|
| 22 |
+
import os, sys, json, glob
|
| 23 |
+
import pandas as pd
|
| 24 |
+
import pyreadstat
|
| 25 |
+
|
| 26 |
+
def process(data_dir):
|
| 27 |
+
OUT = "timss_2019_processed"
|
| 28 |
+
os.makedirs(OUT, exist_ok=True)
|
| 29 |
+
|
| 30 |
+
savs = glob.glob(os.path.join(data_dir, "**/*.sav"), recursive=True)
|
| 31 |
+
ach = [f for f in savs if os.path.basename(f).upper().startswith(('ASA','BSA'))]
|
| 32 |
+
bg = [f for f in savs if os.path.basename(f).upper().startswith(('ASG','BSG'))]
|
| 33 |
+
|
| 34 |
+
for f in ach:
|
| 35 |
+
df, _ = pyreadstat.read_sav(f)
|
| 36 |
+
items = [c for c in df.columns if c.startswith(('M0','S0','M1','S1'))]
|
| 37 |
+
print(f"{os.path.basename(f)}: {df.shape}, {len(items)} items")
|
| 38 |
+
df.to_parquet(os.path.join(OUT, os.path.basename(f).replace('.sav','.parquet')))
|
| 39 |
+
|
| 40 |
+
for f in bg:
|
| 41 |
+
df, _ = pyreadstat.read_sav(f)
|
| 42 |
+
print(f"{os.path.basename(f)}: {df.shape}")
|
| 43 |
+
if 'ITSEX' in df.columns:
|
| 44 |
+
print(f" Gender: {df['ITSEX'].value_counts().to_dict()}")
|
| 45 |
+
df.to_parquet(os.path.join(OUT, os.path.basename(f).replace('.sav','.parquet')))
|
| 46 |
+
|
| 47 |
+
if __name__ == "__main__":
|
| 48 |
+
if len(sys.argv) < 2:
|
| 49 |
+
print(__doc__)
|
| 50 |
+
else:
|
| 51 |
+
process(sys.argv[1])
|