API Reference
Core prediction
Predicts the output of the model on the test data.
- braindec.predict.image_to_labels(image, model_path, vocabulary, vocabulary_emb, prior_probability, topk=10, logit_scale=None, return_posterior_probability=False, device=None, model=None, **kwargs)[source]
Predict the labels of an image using a pre-trained model.
Embeddings
Code to determine embeddings for text and images.
- class braindec.embedding.ImageEmbedding(standardize: bool = False, nilearn_dir: str = None, neuromaps_dir: str = None, atlas: str = 'difumo', dimension: int = 512, space: str = 'MNI152', density: str = None)[source]
Bases:
object
- class braindec.embedding.TextEmbedding(model_name: str = 'BrainGPT/BrainGPT-7B-v0.2', max_length: int = None, batch_size: int = 1, device: str = None)[source]
Bases:
object- chunk_text(texts: str) List[str][source]
Split text into chunks that respect the model’s token limit.
- Parameters:
text – Input text to be chunked
- Returns:
List of text chunks (tokenized dictionaries)
- generate_embedding(token_embeddings, attention_mask) ndarray[source]
Generate embedding from token embeddings.
- Parameters:
token_embeddings – Token embeddings
attention_mask – Attention mask
- Returns:
Numpy array containing the average embedding
Cognitive Atlas
- class braindec.cogatlas.CognitiveAtlas(data_dir=None, task_snapshot=None, concept_snapshot=None, concept_to_task=None, concept_to_process=None, reduced_tasks=None)[source]
Bases:
object- task_df
# Drops tasks with short definitions self.task_df = self.task_df.loc[self.task_df[“definition_text”].str.len() > 90] reduced_tasks = reduced_tasks.loc[
reduced_tasks[“task”].isin(self.task_df[“name”])
].reset_index(drop=True)
Data fetching
Download published braindec assets from OSF.
- braindec.fetcher.download_asset(name, destination_root='.', overwrite=False, node_id='dsj56', timeout=60)[source]
Download a named asset from the built-in manifest.
- braindec.fetcher.download_bundle(name, destination_root='.', overwrite=False, node_id='dsj56', timeout=60)[source]
Download a predefined bundle of assets.
- braindec.fetcher.download_osf_file(file_id, destination_root='.', overwrite=False, use_materialized_path=True, destination=None, timeout=60)[source]
Download a single OSF file by id.
- braindec.fetcher.download_osf_folder(remote_path, destination_root='.', node_id='dsj56', provider='osfstorage', overwrite=False, timeout=60)[source]
Download all files under a folder path from the published OSF project.
- braindec.fetcher.download_osf_path(remote_path, destination_root='.', node_id='dsj56', provider='osfstorage', overwrite=False, timeout=60)[source]
Download a published OSF file or folder by its remote path.
- braindec.fetcher.get_available_assets()[source]
Return the names of downloadable assets and bundles.
- braindec.fetcher.get_cogatlas_concepts()[source]
Fetch concept definitions from the Cognitive Atlas API.
Model
Model
- class braindec.model.Bottleneck(input_size, hidden_layers=(1024, 512, 256), dropout=0.2)[source]
Bases:
Module- forward(x)[source]
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class braindec.model.CLIP(embedding_dim, output_dim=512, dropout=0.1, logit_scale=np.float64(2.659260036932778), logit_bias=None)[source]
Bases:
Module- forward(image, text)[source]
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class braindec.model.Decoder(embedding_dim, output_dim, dropout)[source]
Bases:
Module- forward(x)[source]
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class braindec.model.Encoder3D(channels=(16, 32, 64), input_channels=1, kernel_size=3, stride=1, padding=1)[source]
Bases:
Module- forward(x)[source]
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class braindec.model.Flatten(*args: Any, **kwargs: Any)[source]
Bases:
Module- forward(input)[source]
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class braindec.model.ImageModel(output_dim, dropout)[source]
Bases:
Module- forward(x)[source]
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class braindec.model.MRI3dCNN(batch_size=8, input_channels=1, num_classes=3, input_shape=(91, 109, 91), channels=(16, 32, 64), hidden_layers=(1024, 512, 256), kernel_size=3, stride=1, dropout=0.2)[source]
Bases:
Module- bottleneck
- self.out = nn.Sequential(
nn.Linear(self.hidden_layers[-1], num_classes), nn.Sigmoid(),
)
- forward(x)[source]
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class braindec.model.ProjectionHead(embedding_dim, output_dim, dropout)[source]
Bases:
Module- forward(x)[source]
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class braindec.model.ResidualHead(dim, dropout)[source]
Bases:
Module- forward(x)[source]
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
- class braindec.model.TextModel(embedding_dim, output_dim, dropout)[source]
Bases:
Module- forward(x)[source]
Define the computation performed at every call.
Should be overridden by all subclasses.
Note
Although the recipe for forward pass needs to be defined within this function, one should call the
Moduleinstance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.
Utilities
Miscellaneous functions used for analyses.
- braindec.utils.get_data_dir(data_dir=None)[source]
Get path to gradec data directory.
- Parameters:
data_dir (str, optional) – Path to use as data directory. If not specified, will check for environmental variable ‘GRADEC_DATA’; if that is not set, will use ~/gradec-data instead. Default: None
- Returns:
data_dir – Path to use as data directory
- Return type:
Notes
Taken from Neuromaps. https://github.com/netneurolab/neuromaps/blob/abf5a5c3d3d011d644b56ea5c6a3953cedd80b37/ neuromaps/datasets/utils.py#LL91C1-L115C20