|
|
|
"""dataprocessing.ipynb |
|
|
|
Automatically generated by Colaboratory. |
|
|
|
Original file is located at |
|
https://colab.research.google.com/drive/10At7vh21OGTlE-Myv1NhAHi7l7NwBocQ |
|
""" |
|
|
|
import pandas as pd |
|
import numpy as np |
|
import os |
|
from zipfile import ZipFile |
|
import re |
|
import json |
|
import base64 |
|
|
|
from google.colab import drive |
|
drive.mount('/content/drive') |
|
|
|
path = "/content/drive/MyDrive/Duke/huggingface_project/data" |
|
|
|
df = pd.read_excel(path+"/questionnaire-data.xlsx", header=2) |
|
|
|
df["vviq_score"] = np.sum(df.filter(like = "vviq"), axis = 1) |
|
df["osiq_score"] = np.sum(df.filter(like = "osiq"), axis = 1) |
|
df["treatment"] = np.where(df.vviq_score > 40, "control", "aphantasia") |
|
|
|
df = df.rename(columns={ |
|
"Sub ID": "sub_id", |
|
df.columns[5]: "art_ability", |
|
df.columns[6]: "art_experience", |
|
df.columns[9]: "difficult", |
|
df.columns[10]: "diff_explanation" |
|
}) |
|
|
|
df.columns = df.columns.str.lower() |
|
|
|
df = df.drop(df.filter(like="unnamed").columns, axis = 1) |
|
|
|
df = df.drop(df.filter(regex="(vviq|osiq)\d+").columns, axis = 1) |
|
|
|
df[df.columns[df.dtypes == "object"]] = df[df.columns[df.dtypes == "object"]].astype("string") |
|
|
|
df[df.columns] = df[df.columns].replace([np.nan,pd.NA, "nan","na","NA","n/a","N/A","N/a"], None) |
|
|
|
data = {} |
|
for ind, row in df.iterrows(): |
|
data[row["sub_id"]] = { |
|
"subject_id": int(row["sub_id"]), |
|
"treatment": row["treatment"], |
|
"demographics": dict(df.iloc[ind][1:-1]) |
|
} |
|
data[row["sub_id"]]["demographics"]["art_ability"] = int(data[row["sub_id"]]["demographics"]["art_ability"]) |
|
data[row["sub_id"]]["demographics"]["vviq_score"] = int(data[row["sub_id"]]["demographics"]["vviq_score"]) |
|
data[row["sub_id"]]["demographics"]["osiq_score"] = int(data[row["sub_id"]]["demographics"]["osiq_score"]) |
|
|
|
stored_images = {} |
|
with ZipFile(path + "/Images.zip", "r") as zip: |
|
for image_file in zip.namelist(): |
|
with zip.open(image_file, 'r') as fil: |
|
im = fil.read() |
|
im_encoded = base64.b64encode(im).decode("utf-8") |
|
stored_images[image_file.removesuffix(".jpg")] = im_encoded |
|
|
|
def get_sub_files(subject, file_list): |
|
pattern = re.compile("^.*" + subject + "-[a-z]{3}\d-(kitchen|livingroom|bedroom).*") |
|
sub_files = [f for f in file_list if pattern.match(f)] |
|
sub = { |
|
"kitchen": { |
|
"perception": "", |
|
"memory": "" |
|
}, |
|
"livingroom": { |
|
"perception": "", |
|
"memory": "" |
|
}, |
|
"bedroom": { |
|
"perception": "", |
|
"memory": "" |
|
}, |
|
} |
|
|
|
for fil in sub_files: |
|
if "kitchen" in fil: |
|
if "pic" in fil: |
|
sub["kitchen"]["perception"] = fil |
|
else: |
|
sub["kitchen"]["memory"] = fil |
|
elif "livingroom" in fil: |
|
if "pic" in fil: |
|
sub["livingroom"]["perception"] = fil |
|
else: |
|
sub["livingroom"]["memory"] = fil |
|
else: |
|
if "pic" in fil: |
|
sub["bedroom"]["perception"] = fil |
|
else: |
|
sub["bedroom"]["memory"] = fil |
|
return sub |
|
|
|
with ZipFile(path + "/Aphantasia-Drawings.zip", "r") as zip: |
|
files = zip.namelist() |
|
aphan_subs = list({f.split("/")[0] for f in files}) |
|
aphantasia_drawing_dataset = {} |
|
for s in aphan_subs: |
|
if int(s[3:]) in data.keys(): |
|
data[int(s[3:])]["drawings"] = get_sub_files(s, files) |
|
else: |
|
data[int(s[3:])] = {"drawings": get_sub_files(s,files)} |
|
|
|
with ZipFile(path + "/Control-Drawings.zip", "r") as zip: |
|
files = zip.namelist() |
|
cntrl_subs = list({f.split("/")[0] for f in files}) |
|
full_control = {} |
|
for s in cntrl_subs: |
|
if int(s[3:]) in data.keys(): |
|
data[int(s[3:])]["drawings"] = get_sub_files(s, files) |
|
else: |
|
data[int(s[3:])] = {"drawings": get_sub_files(s,files)} |
|
|
|
stored_images["kitchen"] = stored_images.pop('high_sun_ajwbpqrwvknlvpeh') |
|
stored_images["bedroom"] = stored_images.pop('low_sun_acqsqjhtcbxeomux') |
|
stored_images["livingroom"] = stored_images.pop('low_sun_byqgoskwpvsbllvy') |
|
|
|
def extract_images(subject, treatment): |
|
images_bytes = { |
|
"kitchen": { |
|
"perception": "", |
|
"memory": "" |
|
}, |
|
"livingroom": { |
|
"perception": "", |
|
"memory": "" |
|
}, |
|
"bedroom": { |
|
"perception": "", |
|
"memory": "" |
|
} |
|
} |
|
for room in ["kitchen", "livingroom", "bedroom"]: |
|
paths = data[subject]["drawings"].get(room).values() |
|
paths = [p for p in paths if p != ""] |
|
if treatment == "aphantasia": |
|
with ZipFile(path + "/Aphantasia-Drawings.zip", "r") as zip: |
|
for filename in paths: |
|
with zip.open(filename, 'r') as fil: |
|
im = fil.read() |
|
im_encoded = base64.b64encode(im).decode("utf-8") |
|
if "mem" in filename: |
|
images_bytes[room]["memory"] = im_encoded |
|
else: |
|
images_bytes[room]["perception"] = im_encoded |
|
else: |
|
with ZipFile(path + "/Control-Drawings.zip", "r") as zip: |
|
for filename in paths: |
|
with zip.open(filename, 'r') as fil: |
|
im = fil.read() |
|
im_encoded = base64.b64encode(im).decode("utf-8") |
|
if "mem" in filename: |
|
images_bytes[room]["memory"] = im_encoded |
|
else: |
|
images_bytes[room]["perception"] = im_encoded |
|
|
|
return images_bytes |
|
|
|
missing = [] |
|
for i in data.keys(): |
|
if "drawings" in data[i] and "treatment" in data[i]: |
|
data[i]["drawings"] = extract_images(i,data[i]["treatment"]) |
|
else: |
|
missing.append(i) |
|
|
|
for num in missing: |
|
data.pop(num, None) |
|
|
|
for sub in data.keys(): |
|
data[sub]["image"] = stored_images |
|
|
|
subject_data_path = path + "/clean_data.json" |
|
|
|
|
|
|
|
|
|
type(data) |
|
|
|
da = pd.DataFrame(data) |
|
|
|
|
|
|
|
|
|
|
|
flattened_data = [] |
|
|
|
for key, value in data.items(): |
|
flattened_subject = pd.json_normalize(value, sep='_') |
|
flattened_data.append(flattened_subject) |
|
|
|
|
|
da = pd.concat(flattened_data, ignore_index=True) |
|
|
|
|
|
da.to_parquet(path + 'data.parquet') |