Skip to main content

A scalable framework for fMRI dataset aggregation and modeling of human vision

Project description

mosaic-dataset banner

Open In Colab test mosaic

Load the mosaic dataset (Lahner et al.) and the associated pre-trained models

pip install mosaic-dataset
import mosaic

dataset = mosaic.load(
    names_and_subjects={
        "NaturalScenesDataset": [1], ## set this to "all" if you want to download data for all subjects
        # "THINGS": "all"
    },
    folder="./MOSAIC",
    # if set to False, dataset[i]["betas"] will return a tensor containing the betas for all ROIs concatenated together
    parse_betas=True
)

print(dataset[0].keys())

Visualization

import mosaic
from mosaic.utils import visualize

visualize(
    betas=dataset[0]["betas"],
    ## set rois to None if you want to visualize all of the rois
    rois=[
        "L_FFC",
        "R_FFC",
        "L_PHA2",
        "R_PHA2",
        "L_V1",
        "R_V1",

    ],
    ## other modes are: 'white', 'midthickness', 'pial', 'inflated', 'very_inflated', 'flat', 'sphere'
    mode = "inflated",
    save_as = "plot.html",
)

Loading pre-trained models

import mosaic

model = mosaic.from_pretrained(
    backbone_name='ResNet18', 
    framework='multihead', 
    subjects='all', 
    vertices='visual', 
    folder='./mosaic_models/'
)

## or load a single subject model
single_subject_model = mosaic.from_pretrained(
    backbone_name="CNN8",
    framework="singlehead",
    subjects="sub-05_NSD",
    vertices="visual",
)

Running inference with pre-trained models:

from mosaic.utils.inference import MosaicInference

inference = MosaicInference(
    model=model,
    batch_size=32,
    device="cpu"
)

results = inference.run(
    images = [
        Image.open("face.jpg").convert("RGB"),
    ],
    names_and_subjects={"NaturalScenesDataset": "all"}
)

Visualizing model predictions

#note responses to the face are highest in the ventral stream
inference.plot(
    image=Image.open("face.jpg").convert("RGB"),
    save_as="predicted_voxel_responses.html",
    dataset_name="NaturalScenesDataset",
    subject_id=1,
    ## other modes are: 'white', 'midthickness', 'pial', 'inflated', 'very_inflated', 'flat', 'sphere'
    mode="inflated"
)

Loading up stimulus info:

stim_info = mosaic.get_stiminfo(
    dataset_name="deeprecon",
    folder="./MOSAIC"
)

print(stim_info.head())

Merging files for easier loading

from mosaic.utils.merging import merge_hdf5_files
from mosaic.datasets import MergedDataset

merge_hdf5_files(
    files=[
        './MOSAIC/NSD/sub-01_NSD.hdf5',
        './MOSAIC/deep_recon/sub-01_deeprecon.hdf5'
    ],
    save_as="./merged-test.hdf5"
)

dataset = MergedDataset(
    filename="./merged-test.hdf5"
)

print(len(dataset))

Downloading resting state data

from mosaic.datasets.resting_state import download_resting_state_data

download_resting_state_data(
    dataset="BMD", ## or "NSD" or "THINGS"
    subject=1,
    session=1,
    run=1,
    folder="./MOSAIC"
)

Downloading time series data

from mosaic.datasets.timeseries import download_timeseries_data

download_timeseries_data(
    folder = "./mosaic-timeseries",
    dataset_name = "deeprecon"
)

Dev Setup

git clone git+https://github.com/Mayukhdeb/mosaic-dataset.git
cd mosaic-dataset
python setup.py develop

Running tests

Note: Before you start running tests, open tests/tesing_config.json and make the following changes

  1. Rename the dataset_folder and models_folder to wherever you already have the files or where you'd like to download them
  2. set download_pretrained_models to true if you want to test the models with their actual trained weights. It's set to false by default to avoid spamming the AWS bucket with requests.
pytest -vvx tests/

And to make the tests more verbose (with the prints displayed)

pytest -vvx --capture=no tests/

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

mosaic_dataset-0.0.5.tar.gz (43.9 kB view details)

Uploaded Source

Built Distribution

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

mosaic_dataset-0.0.5-py3-none-any.whl (47.1 kB view details)

Uploaded Python 3

File details

Details for the file mosaic_dataset-0.0.5.tar.gz.

File metadata

  • Download URL: mosaic_dataset-0.0.5.tar.gz
  • Upload date:
  • Size: 43.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for mosaic_dataset-0.0.5.tar.gz
Algorithm Hash digest
SHA256 528eb0eb163a4f00722c69fa420211514be8622139fb8161b3f6600c7bf4d49c
MD5 f3beea87eced1dc16417f78fbdafada8
BLAKE2b-256 a6cd3fb91180e19c1010137213b35d47509a3d61349e8a4c18f9d1af25c0d620

See more details on using hashes here.

File details

Details for the file mosaic_dataset-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: mosaic_dataset-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 47.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.2

File hashes

Hashes for mosaic_dataset-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 3d4632985741b9600f90883b845e6d011b3220eb27147dd6ad04c6d74dd3671a
MD5 c15e36298fef601698be787d8d01b532
BLAKE2b-256 723208f85ee0b1082c1c8cf7791894b51104b831f956139cc57da37f11084b45

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