Skip to main content

Library to run Reverse Predictivity

Project description

Reverse Predictivity

A lightweight, modular Python library for computing bidirectional alignment between artificial neural network (ANN) representations and primate inferior temporal (IT) cortex responses.

This package accompanies the preprint:

Muzellec & Kar (2025). Reverse Predictivity: Going Beyond One-Way Mapping to Compare Artificial Neural Network Models and Brains. bioRxiv. https://www.biorxiv.org/content/10.1101/2025.08.08.669382v1

Reverse predictivity complements traditional forward neural predictivity by asking the reciprocal question:

How well do neural responses predict ANN activations?

Together, the forward and reverse metrics provide a more complete picture of representational similarity between brains and models.


🧠 Library Overview

This library contains four core mapping modules:

Module Mapping Direction Question Answered
model_to_monkey.py Model → Monkey How well do ANN features predict neural responses? (forward predictivity)
monkey_to_model.py Monkey → Model How well do IT neurons predict ANN unit activations? (reverse predictivity)
monkey_to_monkey.py Monkey A → Monkey B How consistent are neural populations across animals? (biological upper bound)
model_to_model.py Model A → Model B How aligned are representations across models or layers?

All functions compute explained variance (EV) using repeated linear mappings and save EV arrays to disk.


🔧 Installation

We recommend using a clean environment:

conda create -n reverse_pred python=3.10 -y
conda activate reverse_pred

Install required Python packages:

pip install numpy scipy scikit-learn matplotlib

Install this library:

pip install reverse_pred

🚀 Usage

Each mapping function takes:

  • model_features: (n_images × n_units) array
  • rates: (n_images × n_neurons × n_repeats) array
  • out_dir: output directory for saving EV results
  • reps: number of cross-validated fits (default: 20)
  • model_type: Choice of regressor models among ridge, linear, lasso, elasticnet, pls. Default=ridge

1. Forward Predictivity

Module: model_to_monkey.py
Function: compute_model_to_monkey

from reverse_predictivity.model_to_monkey import compute_model_to_monkey
import numpy as np

model_features = np.load("features/resnet50_itlayer.npy")
rates = np.load("data/it_rates.npy")

compute_model_to_monkey(
    model_features=model_features,
    rates=rates,
    out_dir="results/model_to_monkey/resnet50",
    reps=20,
    out_name='forward_ev'
)

Output:

results/model_to_monkey/resnet50/forward_ev.npy

2. Reverse Predictivity

Module: monkey_to_model.py
Function: compute_monkey_to_model

from reverse_predictivity.monkey_to_model import compute_monkey_to_model
import numpy as np

model_features = np.load("features/resnet50_itlayer.npy")
rates = np.load("data/it_rates.npy")

compute_monkey_to_model(
    model_features=model_features,
    rates=rates,
    out_dir="results/monkey_to_model/resnet50",
    max_n=None,
    reps=20,
    out_name='reverse_ev'
)

Parameters:

max_n: can be set to subsample n number of neurons within the neural population. Each repetition will be done using a different sampling.

Output:

results/monkey_to_model/resnet50/reverse_ev.npy

3. Neural–Neural Consistency

Module: monkey_to_monkey.py
Function: compute_monkey_to_monkey

from reverse_predictivity.monkey_to_monkey import compute_monkey_to_monkey
import numpy as np

ratesA = np.load("data/monkeyA_rates.npy")
ratesB = np.load("data/monkeyB_rates.npy")

compute_monkey_to_monkey(
    rates_predictor=ratesA,
    rates_predicted=ratesB,
    out_dir="results/monkey_to_monkey/",
    reps=20,
    max_n=None,
    name_predicted="monkeyB",
    name_predictor="monkeyA",
)

Parameters:

max_n: can be set to subsample n number of predictor neurons. Each repetition will be done using a different sampling.

Output:

results/monkey_to_monkey/monkeyA_to_monkeyB_ev.npy

4. Model–Model Alignment

Module: model_to_model.py
Function: compute_model_to_model

from reverse_predictivity.model_to_model import compute_model_to_model
import numpy as np

modelA = np.load("features/resnet50_itlayer.npy")
modelB = np.load("features/convnext_itlayer.npy")

compute_model_to_model(
    model_features_predictor=modelA,
    model_features_predicted=modelB,
    out_dir="results/model_to_model/resnet_to_convnext",
    reps=20,
    name_predicted="convnext",
    name_predictor="resnet50",
)

Output:

results/model_to_model/resnet_to_convnext/resnet50_to_convnext_ev.npy

📊 Downstream Analysis

After generating EV results:

  1. Load the saved .npy files.
  2. Compare forward vs reverse predictivity.
  3. Compare model–monkey EV to monkey–monkey EV.
  4. Compare model–model EV across architectures.
import numpy as np
import matplotlib.pyplot as plt

fwd = np.load("results/model_to_monkey/resnet50/forward_ev.npy")
rev = np.load("results/monkey_to_model/resnet50/reverse_ev.npy")

plt.hist(fwd, bins=30, alpha=0.6, label="Forward")
plt.hist(rev, bins=30, alpha=0.6, label="Reverse")
plt.legend()
plt.xlabel("Explained Variance")
plt.ylabel("Count")
plt.show()

📌 Citation

If you use this library, please cite:

@article{muzellec_kar_2025_reversepredictivity,
  title   = {Reverse Predictivity: Going Beyond One-Way Mapping to Compare Artificial Neural Network Models and Brains},
  author  = {Muzellec, Sabine and Kar, Kohitij},
  journal = {bioRxiv},
  year    = {2025}
}

📜 License

MIT License — see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

reverse_pred-0.1.8.tar.gz (9.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

reverse_pred-0.1.8-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file reverse_pred-0.1.8.tar.gz.

File metadata

  • Download URL: reverse_pred-0.1.8.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for reverse_pred-0.1.8.tar.gz
Algorithm Hash digest
SHA256 ad1c5b3719e02c814375b1ec3639a3f2450f52b7bf6f5a461621443b41e483fa
MD5 4e9d3cdaa93954082b24d24b83d34d1a
BLAKE2b-256 6a7ae481bc14151fb437f46eb280aca79f96f8fd46c0737b03bb58dd56469f2b

See more details on using hashes here.

File details

Details for the file reverse_pred-0.1.8-py3-none-any.whl.

File metadata

  • Download URL: reverse_pred-0.1.8-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for reverse_pred-0.1.8-py3-none-any.whl
Algorithm Hash digest
SHA256 02bace10143e9330ee2d9e5301967449a69e03bd4b47b276159e82ec61788a6c
MD5 baa094b10bfc2a312f9028b0407376d2
BLAKE2b-256 046718bb64c3e1513edadb9a50b4563f600594bf810dd51875fa062c32b9872f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page