Skip to content

Add Brain Network Transformer for connectome classification - #1766

Merged
solarsys merged 2 commits into
sunlabuiuc:masterfrom
AxelNoun:brain-network-transformer
Nov 30, 2026
Merged

solarsys merged 2 commits into
sunlabuiuc:masterfrom
AxelNoun:brain-network-transformer

Conversation

@AxelNoun

Copy link
Copy Markdown
Contributor

Contributor: Axel Coiffard-Donaty (@AxelNoun)

Contribution Type: New model

Description:

Adds Brain Network Transformer (Kan et al., NeurIPS 2022), ported from Wayfear/BrainNetworkTransformer (MIT) without omegaconf. It reads the (nodes, nodes) connectivity of ABIDEAutismClassification and pools the nodes with the orthonormal clustering readout; attention weights and cluster assignments stay available.

The encoder layer is written out instead of subclassing nn.TransformerEncoderLayer: on PyTorch 2 the official override rejects is_causal, and its positional constructor call builds the layer without biases from PyTorch 2.1 on.

Files to Review:

  • pyhealth/models/brain_network_transformer.py
  • tests/core/test_brain_network_transformer.py
  • examples/fmri/bnt_parity_check.py: comparison with the official code.
  • examples/fmri/abide_autism_bnt.py: BNT, the GCN example and logistic regression on the same patient splits.
  • Docs page and export.

Testing:

  • 21 new tests; with the LaBraM, ABIDE, GNN and processor compatibility tests: 86 passed (Windows, Python 3.13, PyTorch 2.15).
  • Same weights in the official model and the port: identical logits and cluster assignments, float32 and float64.
  • tools/check_pr_rules.py: passed.

Results (%, ABIDE, 1,035 scans, 70/10/20 patient split, 5 seeds, test at the best validation AUROC epoch; baselines keep their examples' settings):

  • BNT: AUROC 73.6 ± 3.6, accuracy 65.2 ± 5.7
  • Logistic regression: AUROC 71.3 ± 2.6, accuracy 62.6 ± 2.8
  • GCN example: AUROC 55.7 ± 5.2, accuracy 51.5 ± 2.9
  • Paper, BNT: AUROC 80.2 ± 1.0, accuracy 71.0 ± 1.2

Limits: the gap to the paper is not explained yet. Known differences: constant learning rate (no cosine decay), split not stratified by site, PyHealth's cohort (1,035 scans, the paper 1,009 subjects). Over 5 seeds, the lead of BNT over logistic regression (2.3 points on average, from -0.7 to 5.0) is not established.

Port BrainNetworkTransformer (Kan et al., NeurIPS 2022) from
Wayfear/BrainNetworkTransformer (MIT License) without the omegaconf
dependency. The model reads a square connectivity matrix, runs a
transformer over the nodes and pools them with the orthonormal
clustering readout. The number of nodes comes from the dataset, the
classifier is sized by the label processor, and the attention weights
and cluster assignments stay available.

The encoder layer is written out instead of subclassing
nn.TransformerEncoderLayer: the official override does not accept the
is_causal argument of PyTorch 2, and its positional constructor call
drops the layer biases on PyTorch 2.1 and later.

bnt_parity_check.py loads the official weights into the port and finds
identical logits and cluster assignments. abide_autism_bnt.py trains
BNT, the GCN baseline and logistic regression on the same patient splits
over several seeds.
@github-actions

github-actions Bot commented Nov 30, 2026 •

Copy link
Copy Markdown

Test Coverage

Total: 88.62% (60323 / 68071 lines)

Modified file Coverage Lines
pyhealth/models/__init__.py 100.00% 129 / 129
pyhealth/models/brain_network_transformer.py 98.20% 164 / 167

Workflow run · 816b5bd

@solarsys solarsys added the status: automatic The PR will go through automatic review process. label Nov 30, 2026
@solarsys

Copy link
Copy Markdown
Collaborator

/review

@github-actions github-actions Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous review

Reviewed commit 6ea9136

The defaults are the authors' configuration: two layers, the first
keeping the nodes and the second pooling them into 100 clusters, 4 heads,
a feed-forward size of 1024, no positional encoding. The number of nodes
is read from the dataset, so any atlas works. With

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 816b5bd: the class docstring and the docs page now say that the default 100 orthogonal clusters need at least 100 features per node (cc200 and larger atlases), and that smaller graphs need a smaller layer_sizes. The model also raises a ValueError that names layer_sizes in that case, with a test.

Comment thread examples/fmri/abide_autism_bnt.py Outdated
mixup of the inputs and labels during training. Known differences with
the official run: the learning rate stays constant (the official run
decays it from 1e-4 to 1e-5 with a cosine schedule), PyHealth's
``Trainer`` exempts biases and layer norms from weight decay (the

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, thanks: the Trainer exempts names containing bias, LayerNorm.bias or LayerNorm.weight, so norm1.weight and norm2.weight are decayed. Fixed in 816b5bd: the docstring now says only the biases are exempt, and that the layer norm weights are decayed in both runs.

@github-actions github-actions Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Previous review

AI Review

suggestion: need change
decision: need change

Thank you for this contribution. It adds the Brain Network Transformer (Kan et al., NeurIPS 2022) as a PyHealth model. It comes with a docs page, 21 unit tests, a parity check against the official code, and a same-split comparison with the GCN and logistic regression baselines. The model is self-contained and well tested. Coverage is 98% for brain_network_transformer.py and 88.6% for the repository, both above the bar. The code and tests look sound. Two documentation statements don't match the code, so I'm asking you to fix them before merge.

To get this merged:

  1. Atlas claim. The class docstring and the docs page say the model reads the node count from the data, so "any atlas works". With the default settings, the second layer builds 100 orthogonal centres in a node-feature space whose size equals the node count. ClusterAssignment raises ValueError when cluster_number > embedding_dimension. So any graph under 100 nodes fails unless the user passes layer_sizes with a smaller cluster count. Please either reword the claim, for example "any atlas with at least 100 nodes, or set layer_sizes for smaller ones", or make the default cluster count adapt.
  2. Weight-decay claim. The abide_autism_bnt.py docstring says PyHealth's Trainer exempts biases and layer norms from weight decay. The first reviewer reports that the Trainer matches only parameter names containing LayerNorm.. This model's norm1 and norm2 layers would then still be decayed. Please check this against the Trainer and correct the sentence. The "known differences" list matters because you compare against the paper's numbers.

Optional suggestions:

  • Add a small test for MixupBrainNetworkTransformer. For example, check that eval mode leaves the batch unchanged and that training mode mixes both inputs and labels.
  • The parity check needs an external clone and omegaconf. You could mention in the docs that it isn't run in CI.

Reviewed commit 6ea9136

The default 100 orthogonal clusters need at least 100 features per
node, so the docstring and docs page no longer say that any atlas
works, and the model now raises a ValueError naming layer_sizes when a
pooling layer asks for more clusters than that. The example states that
the Trainer exempts only the biases from weight decay, and the docs page
says the parity check is not run in CI.
The defaults are the authors' configuration: two layers, the first
keeping the nodes and the second pooling them into 100 clusters, 4 heads,
a feed-forward size of 1024, no positional encoding. The number of nodes
is read from the dataset, so any atlas works. With

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 816b5bd: the class docstring and the docs page now say that the default 100 orthogonal clusters need at least 100 features per node (cc200 and larger atlases), and that smaller graphs need a smaller layer_sizes. The model also raises a ValueError that names layer_sizes in that case, with a test.

Comment thread examples/fmri/abide_autism_bnt.py Outdated
mixup of the inputs and labels during training. Known differences with
the official run: the learning rate stays constant (the official run
decays it from 1e-4 to 1e-5 with a cosine schedule), PyHealth's
``Trainer`` exempts biases and layer norms from weight decay (the

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, thanks: the Trainer exempts names containing bias, LayerNorm.bias or LayerNorm.weight, so norm1.weight and norm2.weight are decayed. Fixed in 816b5bd: the docstring now says only the biases are exempt, and that the layer norm weights are decayed in both runs.

@AxelNoun

Copy link
Copy Markdown
Contributor Author

On the optional points: the docs page now says the parity check needs a clone of the official code and omegaconf, so it is not run in CI. I left MixupBrainNetworkTransformer without a test because it lives in the example script, not in the package; I can move it into pyhealth with a test if you prefer.

@AxelNoun

Copy link
Copy Markdown
Contributor Author

/review

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI Review

suggestion: approve
decision: approve

Thank you for contributing the Brain Network Transformer. It adds a faithful, well-documented port of the NeurIPS 2022 model for ABIDE connectome classification. It also adds a parity check against the official code and a comparison script that runs BNT, the GCN example and logistic regression on the same splits.

It looks ready to merge.

  • Scope: The change is self-contained. The only edits to existing files are the export line in models/__init__.py and the docs entry. No base class, Trainer or shared utility is touched.
  • Sharing: The example imports BrainGCN from the GCN example and uses LogisticRegression as a baseline. The description names both as comparison baselines, which explains the links.
  • Coverage: The new model file has 98.2% coverage, above the 80% bar. The whole repository is at 88.62%, above the 60% bar. __init__.py is at 100%.
  • Docs: The docs page matches the code. The paper and the official repository are cited, along with their licences.
  • Limits: The description states the gap to the paper's results, and that BNT's lead over logistic regression is not established. That is an honest limitation and not a blocker.

Optional suggestions:

  • The description says 21 new tests, but the test file appears to contain 22. You could correct the count.
  • The parity-check and training scripts have no CI test, which is understandable given their external dependencies. A short smoke test of the mixup wrapper would be a nice extra.

Reviewed commit 816b5bd

@solarsys
solarsys merged commit 037e300 into sunlabuiuc:master Nov 30, 2026
2 checks passed
@AxelNoun
AxelNoun deleted the brain-network-transformer branch November 30, 2026 14:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status: automatic The PR will go through automatic review process.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants