This project investigates how different regions within the human language network respond to various measures of linguistic complexity during naturalistic story comprehension. We extend beyond previous network-level analyses by Shain et al. (2020) to examine regional specialization patterns and non-linear complexity processing mechanisms.
This repository contains code and analysis for a computational psycholinguistics study that challenges assumptions about uniform complexity processing in the language network. Our research reveals functional specialization across brain regions and uncovers a surprising semantic processing paradox.
The provided code is focused on the analysis itself. If you would like to see how the GPT-2 surprisal values or the GloVe embeddings were derived, please refer to the accompanying Jupyter notebooks, where the data generation process is fully documented.
-
Regional Specialization: Do different areas within the language network exhibit specialized response patterns to different types of prediction difficulty (syntactic vs. lexical vs. contextual)?
-
Non-Linear Processing: Does linguistic complexity processing exhibit capacity constraints and non-linear patterns rather than uniform linear scaling?
-
Semantic Complexity Paradox: How does story-specific semantic complexity influence language network activation during naturalistic comprehension?
-
Functional Specialization: Frontal regions (LangMFG, LangIFG) showed strongest sensitivity to syntactic complexity, temporal regions (LangAntTemp, LangPostTemp) to lexical predictability, while angular gyrus (LangAngG) responded most to GPT-2 contextual complexity.
-
Non-Linear Complexity Processing: Systematic deviations from linearity across all regions and measures, contradicting linear assumptions of previous approaches.
-
Semantic Processing Paradox: Stories with highest semantic diversity produced language network deactivation, while coherent narratives elicited maximal activation, suggesting adaptive processing mechanisms.
- Python 3.11+
- Conda or Miniconda
- Git
-
Clone the repository:
git clone https://github.com/LizMelamed/lang-network-specialization.git cd functional-specialization-language-network -
Create the conda environment:
conda env create -f environment.yml
-
Activate the environment:
conda activate cogsci-proj
-
Download the Data Folder:
python code/data_download.py
Open-ended analysis (primary contributions):
# Regional specialization analysis
python code/open_task_surprisal.py
# Semantic complexity analysis
python code/open_task_semantic_analysis.pyStructured replication tasks:
# Open Jupyter notebook for structured tasks
jupyter notebook notebooks/Closed_Task1.ipynb
jupyter notebook notebooks/Closed_Task2.ipynb
jupyter notebook notebooks/Closed_Task3.ipynbInteractive exploration:
jupyter notebook notebooks/Open_Task_Semantic_Analysis.ipynb
jupyter notebook notebooks/Open_Task_Surprisal.ipynb- Complexity Binning Analysis: Creates percentile-based surprisal bins to identify non-linear response patterns
- Regional Specialization: Analyzes differential responses across six language network regions
- Multi-Model Comparison: Compares PCFG, 5-gram, and GPT-2 surprisal measures
- Semantic Characterization: Uses GloVe embeddings to quantify story semantic properties
- HRF Convolution: Properly accounts for hemodynamic response delays
├── code/ # Analysis scripts
│ ├── open_task_surprisal.py # Regional specialization analysis
│ ├── open_task_semantic_analysis.py # Semantic complexity analysis
│ └── utils/ # Helper functions
├── data/ # Input datasets
│ ├── LANG_MD_ts_X.csv # Combined predictors (PCFG, 5-gram surprisal)
│ ├── LANG_MD_y_train.csv # Combined BOLD responses (LANG + MD networks)
│ ├── pred_use.csv # Preprocessed predictor variables
│ ├── resp_use.csv # Preprocessed brain response data
│ ├── merged.csv # Combined analysis-ready dataset
│ ├── gpt2_surprisal_results.csv # GPT-2 surprisal computations
│ └── merged_df_with_vectors.parquet # Generated dataset (appears after running data_download.py)
├── notebooks/ # Jupyter notebooks
│ ├── Closed_Task1.ipynb # Structured Task 1: FastText vs GloVe decoding
│ ├── Closed_Task2.ipynb # Structured Task 2: Static vs contextual embeddings
│ ├── Closed_Task3.ipynb # Structured Task 3: Neural encoding models
│ ├── Open_Task_Semantic_Analysis.ipynb # Semantic complexity analysis
│ └── Open_Task_Surprisal.ipynb # Regional specialization analysis
├── results/ # Auto-generated outputs
│ ├── all_fROIs_GPT_2.png # GPT-2 regional responses
│ ├── all_fROIs_PCFG.png # PCFG regional responses
│ ├── all_fROIs_5_gram.png # 5-gram regional responses
│ ├── bold_by_bins_*.png # Complexity binning visualizations
│ ├── semantic_*.png # Semantic analysis plots
│ ├── story_progression_*.png # Story-specific response patterns
│ └── surprisal_overview.png # Summary visualizations
├── environment.yml # Conda environment specification
└── README.md # This file
Natural Stories fMRI Corpus (Shain et al., 2020):
- Participants: 78 native English speakers
- Stories: 8 naturalistic narratives (990-1099 words each)
- Brain Networks: Language (LANG) and Multiple Demand (MD) networks
- Regions: 6 language network regions (LangAngG, LangAntTemp, LangIFG, LangIFGorb, LangMFG, LangPostTemp)
- Predictors: PCFG surprisal, 5-gram surprisal, word timing information
- Responses: BOLD signals temporally aligned to word-level predictors
Pereira et al. (2018) Structured Tasks:
- Participants: 16 fluent English speakers
- Stimuli: Individual words (180), sentence passages (384), naturalistic sentences (243)
- Purpose: Baseline validation for neural decoding approaches
Predictor Variables:
totsurp: PCFG surprisal values (syntactic complexity)fwprob5surp: 5-gram surprisal values (lexical predictability)gpt2_surprisal: GPT-2 surprisal values (contextual complexity)word: Word tokens with precise timingdocid,subject,time,fROI: Experimental identifiers
Response Variables:
BOLD: Brain activation values (HRF-convolved)network: Brain network labels (LANG, MD)fROI: Functional region of interest within networks
Semantic Variables:
semantic_coherence: Story-level semantic consistency (GloVe-based)semantic_diversity: Story-level semantic complexity (1 - coherence)vocabulary_size: Unique words per story
-
Data Preprocessing:
- Temporal alignment of word-level predictors with BOLD responses
- Hemodynamic Response Function (HRF) convolution
- Z-score normalization across all surprisal measures
-
Complexity Binning:
- Divide each surprisal measure into 10 equal-count percentile bins
- Ensure balanced sample sizes (~9,500 observations per bin)
- Capture full range from predictable to highly complex constructions
-
Regional Specialization Analysis:
- Linear correlations between surprisal measures and BOLD responses
- Region-specific response profiles across complexity bins
- Cross-measure comparisons (PCFG vs 5-gram vs GPT-2)
-
Non-linearity Testing:
- Systematic deviations from linear fits
- Identification of peaked responses at intermediate complexity
- Statistical validation of capacity-limited processing
-
Semantic Analysis:
- GloVe embedding-based story characterization
- Semantic coherence and diversity metrics
- Story-specific brain response patterns
- Multi-scale surprisal analysis: First comparison of classical (PCFG, n-gram) and modern (GPT-2) measures in naturalistic fMRI
- Regional complexity binning: Beyond network-level analysis to understand within-network specialization
- Semantic-neural mapping: Story-level semantic properties linked to regional brain responses
- Non-linear complexity modeling: Systematic testing of capacity constraints in language processing
- LangMFG: Primary executive control hub with strongest correlations across all surprisal measures (PCFG: r = 0.903)
- LangIFG: Core syntactic processor with preferential sensitivity to structural complexity
- LangPostTemp: Integrates semantic complexity across all measures
- LangAntTemp: Specializes in lexical processing (5-gram: r = 0.703)
- LangAngG: Strongest response to GPT-2 contextual complexity (r = 0.736)
- LangIFGorb: Narrative complexity gate with extreme story-specific variability
Stories with highest semantic diversity ("Tulips": 0.390 diversity) produced language network deactivation, while coherent narratives ("Elvis": 0.768 coherence) elicited maximal activation. This suggests:
- Inverted-U relationship between semantic complexity and network engagement
- Adaptive processing mechanisms preventing resource depletion
- Potential shifting to MD network under excessive semantic load
- Systematic deviations from linearity across all regions and measures
- Peaked responses at intermediate complexity levels rather than monotonic increases
- Most consistent patterns observed for GPT-2 surprisal, suggesting modern neural language models better capture human comprehension mechanisms
- Memory: GPT-2 analysis requires substantial RAM (8GB+ recommended)
- Reproducibility: All random operations use fixed seeds
- Platform: Tested on Python 3.11 with provided conda environment
- Data Size: Large fMRI files not included in repository due to size constraints
- No synthetic data generation: All analyses use real fMRI and linguistic data
- HRF convolution: Proper temporal alignment accounts for 4-6 second hemodynamic delays
- Balanced binning: Percentile-based approach ensures equal sample sizes across complexity levels
- Multiple comparison correction: Statistical results account for multiple regions and measures
- Code optimized for clarity and reproducibility rather than maximum speed
- For large-scale analysis, consider parallelization options in utils modules
- GPT-2 processing can be memory-intensive; reduce batch size if needed
Melamed, E., & Bamberger, N. (2025). Functional Specialization and Non-Linear Processing in the Human Language Network. Course Project Report, Language, Computation and Cognition (00960222), Technion.
Shain, C., Blank, I. A., van Schijndel, M., Schuler, W., & Fedorenko, E. (2020). fMRI reveals language-specific predictive coding during naturalistic sentence comprehension. Neuropsychologia, 138, 107307. OSF Repository
Pereira, F., Lou, B., Pritchett, B., Ritter, S., Gershman, S. J., Kanwisher, N., & Fedorenko, E. (2018). Toward a universal decoder of linguistic meaning from brain activation. Nature Communications, 9(1), 963.
- Neural Language Models: Transformers library (Hugging Face) for GPT-2 analysis
- Brain Analysis: Nilearn, SciPy, PyGAM for fMRI data processing
- Embeddings: GloVe for semantic analysis
- Standard Stack: NumPy, Pandas, Matplotlib, Seaborn, Statsmodels
- Predictive Processing: Bastos et al. (2012), Kuperberg & Jaeger (2016)
- Language Network Organization: Fedorenko et al. (2013)
- Neural Language Models: Caucheteux & King (2022), Goldstein et al. (2022)
Contact: For questions about this analysis, to report issues, or to request access to processed data outputs, please open a GitHub issue or contact the project maintainers at elizavetam@campus.technion.ac.il or noabamberger@campus.technion.ac.il.
GitHub Repository: [[(https://github.com/LizMelamed/lang-network-specialization.git)]]
Academic Affiliation: Data and Decision Sciences, Technion - Israel Institute of Technology