{"id":26383339,"url":"https://github.com/thesofakillers/brainformer","last_synced_at":"2025-03-17T06:33:56.341Z","repository":{"id":281518020,"uuid":"944944571","full_name":"thesofakillers/brainformer","owner":"thesofakillers","description":"manual fork from @fracapuano","archived":false,"fork":false,"pushed_at":"2025-03-09T16:11:04.000Z","size":8911,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-09T17:20:28.979Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/thesofakillers.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-08T09:42:21.000Z","updated_at":"2025-03-09T16:11:07.000Z","dependencies_parsed_at":"2025-03-09T17:31:10.769Z","dependency_job_id":null,"html_url":"https://github.com/thesofakillers/brainformer","commit_stats":null,"previous_names":["thesofakillers/brainformer"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thesofakillers%2Fbrainformer","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thesofakillers%2Fbrainformer/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thesofakillers%2Fbrainformer/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/thesofakillers%2Fbrainformer/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/thesofakillers","download_url":"https://codeload.github.com/thesofakillers/brainformer/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243987778,"owners_count":20379603,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-17T06:33:55.851Z","updated_at":"2025-03-17T06:33:56.331Z","avatar_url":"https://github.com/thesofakillers.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"THIS README IS OUT OF DATE/STALE. IGNORE FOR NOW.\n\n\n# brainformer\nA transformer-based approach to predicting MEG (Magnetoencephalography) readings from EEG (Electroencephalography) sensory inputs. The project leverages multi-dimensional attention to predict over multi-channel timeseries data.\n\n## Overview\nBrainformer is a novel architecture that aims to bridge the gap between EEG and MEG neuroimaging techniques by learning to predict MEG signals from EEG inputs. The model employs a specialized transformer architecture with two-stage attention mechanisms to handle both temporal and spatial dependencies in brain activity data.\n\n## Key Features\nTwo-Stage Attention: Implements a novel attention mechanism that processes both temporal and spatial dimensions of brain signals independently\nCross-Channel Communication: Enables information flow between different sensor channels through a router-based attention mechanism\nScalable Architecture: Handles variable-length sequences and different numbers of input/output channels\nHuggingFace Integration: Full integration with the HuggingFace ecosystem for easy model sharing and deployment\n\n## Architecture\nThe core architecture used for the task of translating EEG to MEG signals is a derivation of `CrossFormer`, a specialized transformer that implements:\n  - Time-wise self-attention across sequence length\n  - Channel-wise attention through a router mechanism\n  - Cross-attention between EEG and MEG modalities\n\nWe extend the original crossformer architecture implementing optimizations for the task at hand, such as Pre-LN normalization. Further, we also extend the architecture to be encoder-decoder based and entirely sequence to sequence, to fully exploit the wealth of data available regarding coupled EEG and MEG recordings in the OpenNeuro.\n\n![brainformer-v1](media/brainformer-v1.png)*Brainformer architecture overview: Two-stage attention mechanism for processing temporal and spatial dimensions of brain signals, with cross-attention between EEG and MEG modalities.*\n\nWhile novel in the encoder-decoder formulation, Crossformer is partially derived from the Two-Stage Attention Layer introduced in [Crossformer: Transformer Utilizing Cross-Dimension Dependency for Multivariate Time Series Forecasting](https://openreview.net/forum?id=vSVLM2j9eie).\nIn short, the Two-Stage Attention Layer processes data in two stages:\n- Temporal stage: Captures dependencies across time\n- Spatial stage: Models relationships between different sensor channels\n\n## Installation\n\n```bash\ngit clone https://github.com/fracapuano/brainformer.git \u0026\u0026 cd brainformer\npip install -e .\n```\n\n## Usage\n\n### Data\nThe dataset used for training is derived from the OpenNeuro repository ([here](https://openneuro.org/datasets/ds000117)), but processed according to a custom defined processing pipeline. Multi-subject, cleaned and ready to train data are openly available on 🤗 Hugging Face at [`fracapuano/eeg2meg-medium-tokenized`](https://huggingface.co/datasets/fracapuano/eeg2meg-medium-tokenized). Evaluation is performed on a test set, derived from a held-out portion of the training dataset.\n\nFor context, processing the data is a fairly lenghty process due to its dimensionality. Each data point indeed represents a ~8min recording, sampled at 1100Hz, with 70 channels for the EEG modality and 306 channels for MEG.\n\n### Training\nEffort has been spent to make the custom transformer architecture defined in `model` compatible with Hugging Face's `Trainer` API, so as to allow for optimized training. This requires defining a `hf_transformer.py` wrapper around the custom `crossformer.py` module. Further, we define custom `hf_registermodel.py` and `hf_configs.py` files to be able to share the model weights and configurations with the open-source community. \n\nLastly, we completely open-sourced our training runs, available on 🐝 Weights\u0026Biases at [fracapuano/eeg2meg-sub1](https://wandb.ai/francescocapuano/eeg2meg-sub1?nw=nwuserfrancescocapuano).\n\n```bash\npython train.py --d_model 64 --n_heads 4 --n_layers 1 --sequence_length 256\n```\n\nKey parameters:\n- `d_model`: Embedding dimension\n- `n_heads`: Number of attention heads\n- `n_layers`: Number of transformer layers\n- `sequence_length`: Maximum sequence length to process\n\n### Evaluation\n```bash\npython eval.py\n```\n\n## License\nThis project is licensed under the MIT License - see the LICENSE file for details.\n\n## Citation\nIf you use this code in your research, please cite:\n\n\n\u003c!-- @misc{capuano2024brainformer,\n  title={Brainformer: A Transformer-based Approach to EEG-to-MEG Signal Translation},\n  author={Capuano, Francesco and Ludington, William and Cinà, Gabriele and Zhang (Mingfang) Lucy},\n  year={2024},\n  journal={arXiv},\n  howpublished={\\url{https://arxiv.org/abs/XXXXXX}}\n} --\u003e\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthesofakillers%2Fbrainformer","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fthesofakillers%2Fbrainformer","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fthesofakillers%2Fbrainformer/lists"}