aphantasia_drawing_dataset / aphantasia_drawing_dataset.py
jmc255's picture
Update aphantasia_drawing_dataset.py
be6ec69 verified
# -*- coding: utf-8 -*-
"""aphantasia_drawing_dataset.ipynb
Automatically generated by Colab.
Original file is located at
https://colab.research.google.com/drive/1DYVroeFqoNK7DDiw_3OIczPeqEME-rbh
"""
# Copyright 2020 The HuggingFace Datasets Authors and the current dataset script contributor.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import pandas as pd
import numpy as np
import io
from typing import List
import datasets
import logging
from PIL import Image
_CITATION = """\
@misc{Bainbridge_Pounder_Eardley_Baker_2023,
title={Quantifying Aphantasia through drawing: Those without visual imagery show deficits in object but not spatial memory},
url={osf.io/cahyd},
publisher={OSF},
author={Bainbridge, Wilma A and Pounder, Zoë and Eardley, Alison and Baker, Chris I},
year={2023},
month={Sep}
}
"""
_DESCRIPTION = """\
This dataset comes from the Brain Bridge Lab from the University of Chicago.
It is from an online memory drawing experiment with 61 individuals with aphantasia
and 52 individuals with normal imagery. In the experiment participants 1) studied 3 separate
scene photographs presented one after the other, 2) then drew them from memory,
3) completed a recognition task, 4) copied the images while viewing them, 5) filled out
a VVIQ and OSIQ questionnaire and also demographics questions. The data from the experiment
was made available on the OSF website linked above. It was created July 31, 2020 and last
updated September 27, 2023.
"""
_HOMEPAGE = "https://osf.io/cahyd/"
url = "https://drive.google.com/file/d/1v1oaZog5j5dD_vIElOEWLCZUrXvJ3jzx/view?usp=drive_link"
def _get_drive_url(url):
base_url = 'https://drive.google.com/uc?id='
split_url = url.split('/')
return base_url + split_url[5]
_URL = {"train": _get_drive_url(url)}
class AphantasiaDrawingDataset(datasets.GeneratorBasedBuilder):
_URL = _URL
VERSION = datasets.Version("1.1.0")
def _info(self):
return datasets.DatasetInfo(
description=_DESCRIPTION,
features=datasets.Features({
"subject_id": datasets.Value("int32"),
"treatment": datasets.Value("string"),
"demographics": {
"country": datasets.Value("string"),
"age": datasets.Value("int32"),
"gender": datasets.Value("string"),
"occupation": datasets.Value("string"),
"art_ability": datasets.Value("int32"),
"art_experience": datasets.Value("string"),
"device": datasets.Value("string"),
"input": datasets.Value("string"),
"difficult": datasets.Value("string"),
"diff_explanation": datasets.Value("string"),
"vviq_score": datasets.Value("int32"),
"osiq_score": datasets.Value("int32")
},
"drawings": {
"kitchen": {
"perception": datasets.Image(decode=True),
"memory": datasets.Image(decode=True)
},
"livingroom": {
"perception": datasets.Image(decode=True),
"memory": datasets.Image(decode=True)
},
"bedroom": {
"perception": datasets.Image(decode=True),
"memory": datasets.Image(decode=True)
}
},
"image": {
"kitchen": datasets.Image(decode=True),
"livingroom": datasets.Image(decode=True),
"bedroom": datasets.Image(decode = True)
}
}),
supervised_keys=None,
homepage=_HOMEPAGE,
citation=_CITATION
)
def _split_generators(self, dl_manager: datasets.DownloadManager) -> List[datasets.SplitGenerator]:
url_to_download = self._URL
downloaded_file = dl_manager.download_and_extract(url_to_download)
return [
datasets.SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs={
"filepath": downloaded_file["train"]
})
]
def _generate_examples(self, filepath):
"""This function returns the examples in the raw (text) form."""
logging.info("generating examples from = %s", filepath)
def byt_to_image(image_bytes):
if image_bytes is not None:
image_buffer = io.BytesIO(image_bytes)
image = Image.open(image_buffer)
return image
return None
with open(filepath, "rb") as subjects_file:
subjects_data = pd.read_parquet(subjects_file)
for idx, sub_row in subjects_data.iterrows():
yield idx, {
"subject_id": sub_row["subject_id"],
"treatment": sub_row["treatment"],
"demographics": {
"country": sub_row["demographics.country"],
"age": sub_row["demographics.age"],
"gender": sub_row["demographics.gender"],
"occupation": sub_row["demographics.occupation"],
"art_ability": sub_row["demographics.art_ability"],
"art_experience": sub_row["demographics.art_experience"],
"device": sub_row["demographics.device"],
"input": sub_row["demographics.input"],
"difficult": sub_row["demographics.difficult"],
"diff_explanation": sub_row["demographics.diff_explanation"],
"vviq_score": sub_row["demographics.vviq_score"],
"osiq_score": sub_row["demographics.osiq_score"]
},
"drawings": {
"kitchen": {
"perception": byt_to_image(sub_row["drawings.kitchen.perception"]),
"memory": byt_to_image(sub_row["drawings.kitchen.memory"])
},
"livingroom": {
"perception": byt_to_image(sub_row["drawings.livingroom.perception"]),
"memory": byt_to_image(sub_row["drawings.livingroom.memory"])
},
"bedroom": {
"perception": byt_to_image(sub_row["drawings.bedroom.perception"]),
"memory": byt_to_image(sub_row["drawings.bedroom.memory"])
}
},
"image": {
"kitchen": byt_to_image(sub_row["image.kitchen"]),
"livingroom": byt_to_image(sub_row["image.livingroom"]),
"bedroom": byt_to_image(sub_row["image.bedroom"])
}
}