File size: 9,699 Bytes
478fb4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
986dc0b
478fb4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6404fdb
4fbf53e
478fb4a
 
986dc0b
478fb4a
 
 
 
 
 
 
30d8bff
478fb4a
 
 
 
 
ae658d3
a2eb370
478fb4a
 
a2eb370
ae658d3
478fb4a
 
a2eb370
 
478fb4a
 
 
a2eb370
 
ae658d3
478fb4a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0511667
478fb4a
 
 
 
 
 
0511667
478fb4a
 
18c25e2
478fb4a
27b68d2
478fb4a
986dc0b
478fb4a
c5edc51
 
48e6885
9b7c19e
 
 
 
 
 
 
 
 
30d8bff
9b7c19e
 
c5edc51
 
 
 
27b68d2
c5edc51
 
 
 
 
 
 
 
 
 
 
 
 
 
48e6885
478fb4a
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
# -*- coding: utf-8 -*-
"""aphantasia_drawing_dataset.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1FHMQJWfjSzSrtEpARqh7IKVrfF7LwyRA
"""







#!pip install datasets -q
#from google.colab import drive
#drive.mount('/content/drive')
#path = os.getcwd() + "/drive/MyDrive/Duke/huggingface_project/aphantasia_drawing"


# 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.
# TODO: Address all TODOs and remove all explanatory comments
"""TODO: Add a description here."""


#import csv
import base64
from PIL import Image
import numpy as np
import io
import json
import os
from typing import List
import datasets
import logging

# TODO: Add BibTeX citation
# Find for instance the citation on arxiv or on the dataset repo/website
_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}
}
"""

# TODO: Add description of the dataset here
# You can copy an official description
_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.
"""

# TODO: Add a link to an official homepage for the dataset here
_HOMEPAGE = "https://osf.io/cahyd/"

# TODO: Add the licence for the dataset here if you can find it
_LICENSE = ""

# TODO: Add link to the official dataset URLs here
# The HuggingFace Datasets library doesn't host the datasets but only points to the original files.
# This can be an arbitrary nested dict/list of URLs (see below in `_split_generators` method)

url = "https://drive.google.com/file/d/1aRhQlKPDk29yYPkx2kPhqaMwec5QZ4JE/view?usp=sharing"

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)}

# TODO: Name of the dataset usually matches the script name with CamelCase instead of snake_case
class AphantasiaDrawingDataset(datasets.GeneratorBasedBuilder):
    """TODO: Short description of my dataset."""

    _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"),#datasets.ClassLabel(names=["aphantasia", "control"]),
                  "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)
                  }
          }),
          # No default supervised_keys (as we have to pass both question
          # and context as input).
          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)

      with open(filepath, "r") as subjects_file:
        subjects_data = json.load(subjects_file)
        idx = 0
        for sub in subjects_data:
          for room in subjects_data[sub]["drawings"].keys():
            if subjects_data[sub]["drawings"][room]["perception"] != "":
              img_byt = base64.b64decode(subjects_data[sub]["drawings"][room]["perception"])
              img = Image.open(io.BytesIO(img_byt))
              subjects_data[sub]["drawings"][room]["perception"] = img
            else:
              subjects_data[sub]["drawings"][room]["perception"] = None

            if subjects_data[sub]["drawings"][room]["memory"] != "":
              img_byt = base64.b64decode(subjects_data[sub]["drawings"][room]["memory"])
              img = Image.open(io.BytesIO(img_byt))
              subjects_data[sub]["drawings"][room]["memory"] = img
            else:
              subjects_data[sub]["drawings"][room]["memory"] = None

          for room in subjects_data[sub]["image"].keys():
            img_byt = base64.b64decode(subjects_data[sub]["image"][room])
            img = Image.open(io.BytesIO(img_byt))
            subjects_data[sub]["image"][room] = img.resize((500,500))
          idx += 1
          age = int(subjects_data[sub]["demographics"]["age"]) if subjects_data[sub]["demographics"]["age"] else np.nan
          yield idx-1, {
              "subject_id": subjects_data[sub]["subject_id"],
              "treatment": subjects_data[sub]["treatment"],
              "demographics": {
                  "country": subjects_data[sub]["demographics"]["country"],
                  "age": subjects_data[sub]["demographics"]["age"],
                  "gender": subjects_data[sub]["demographics"]["gender"],
                  "occupation": subjects_data[sub]["demographics"]["occupation"],
                  "art_ability": subjects_data[sub]["demographics"]["art_ability"],
                  "art_experience": subjects_data[sub]["demographics"]["art_experience"],
                  "device": subjects_data[sub]["demographics"]["device"],
                  "input": subjects_data[sub]["demographics"]["input"],
                  "difficult": subjects_data[sub]["demographics"]["difficult"],
                  "diff_explanation": subjects_data[sub]["demographics"]["diff_explanation"],
                  "vviq_score": subjects_data[sub]["demographics"]["vviq_score"],
                  "osiq_score": subjects_data[sub]["demographics"]["osiq_score"]
              },
              "drawings": {
                  "kitchen": {
                      "perception": subjects_data[sub]["drawings"]["kitchen"]["perception"],
                      "memory": subjects_data[sub]["drawings"]["kitchen"]["memory"]
                      },
                  "livingroom": {
                      "perception": subjects_data[sub]["drawings"]["livingroom"]["perception"],
                      "memory": subjects_data[sub]["drawings"]["livingroom"]["memory"]
                      },
                  "bedroom": {
                      "perception": subjects_data[sub]["drawings"]["bedroom"]["perception"],
                      "memory": subjects_data[sub]["drawings"]["bedroom"]["memory"]
                      }
              },
              "image": {
                  "kitchen": subjects_data[sub]["image"]["kitchen"],
                  "livingroom": subjects_data[sub]["image"]["livingroom"],
                  "bedroom": subjects_data[sub]["image"]["bedroom"]
              }
            }