Skip to content
This repository has been archived by the owner on Mar 15, 2021. It is now read-only.

package visual search object array #16

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Data files
mkgu_packaging/klab/search_datasets*

brainio_contrib.egg-info*
build*
dist*
.ipynb_checkpoints*
Empty file added mkgu_packaging/klab/__init__.py
Empty file.
118 changes: 118 additions & 0 deletions mkgu_packaging/klab/search_naturaldesign.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
import os

import h5py
import numpy as np
import pandas as pd
import scipy.misc
import xarray as xr
from pathlib import Path
from result_caching import store
from sklearn.model_selection import StratifiedShuffleSplit
from tqdm import tqdm
from scipy.io import loadmat

from brainio_collection.knownfile import KnownFile as kf
from brainio_base.assemblies import BehavioralAssembly
from brainio_base.stimuli import StimulusSet
from brainio_contrib.packaging import package_stimulus_set, package_data_assembly

def collect_stimuli(data_path):
stimuli = []

# search images
for i in range(1, 241):
target_path = os.path.join(data_path / 'stimuli', 's_' + str(i) + '.jpg')
filename = 's_' + str(i) + '.jpg'
image_id = 'klab_vs_naturaldesign_stimuli_' + str(i)
image_label = 'stimuli'
sample_number = i

stimuli.append({
'image_current_local_file_path': target_path,
'image_path_within_store': filename,
'image_label': image_label,
'image_id': image_id,
'sample_number': sample_number,
})

# target images
for i in range(1, 241):
target_path = os.path.join(data_path / 'target', 't_' + str(i) + '.jpg')
filename = 't_' + str(i) + '.jpg'
image_id = 'klab_vs_naturaldesign_target_' + str(i)
image_label = 'target'
sample_number = i

stimuli.append({
'image_current_local_file_path': target_path,
'image_path_within_store': filename,
'image_label': image_label,
'image_id': image_id,
'sample_number': sample_number,
})

# target mask
for i in range(1, 241):
target_path = os.path.join(data_path / 'gt', 'gt_' + str(i) + '.jpg')
filename = 'gt_' + str(i) + '.jpg'
image_id = 'klab_vs_naturaldesign_gt_' + str(i)
image_label = 'gt'
sample_number = i

stimuli.append({
'image_current_local_file_path': target_path,
'image_path_within_store': filename,
'image_label': image_label,
'image_id': image_id,
'sample_number': sample_number,
})

stimuli = StimulusSet(stimuli)

stimuli.image_paths = {row.image_id: row.image_current_local_file_path for row in stimuli.itertuples()}
stimuli['image_file_name']= stimuli['image_path_within_store']

return stimuli

def collect_data(data_path, sub_id):
image_id = ['stimuli_' + str(i) for i in range(1, 241)]
subjects = []
for i in sub_id:
subjects += [i]*len(image_id)

S_data = np.load(os.path.join(data_path / 'human_data', 'human_all.npy'))
I_data = np.load(os.path.join(data_path / 'human_data', 'I_human_all.npy'))
data = np.zeros((240*len(sub_id), 66, 2), dtype=int)
data[:,:65,:] = S_data
data[:,65,:] = I_data

assembly = BehavioralAssembly(data,
coords={'image_id': ('presentation', image_id*len(sub_id)),
'subjects': ('presentation', subjects),
'fixation': [*range(66)],
'position': ['x', 'y']},
dims=['presentation', 'fixation', 'position'])
return assembly

def main():
data_dir = Path(__file__).parent / 'search_datasets'
data_path = data_dir / 'naturaldesign'

# create stimuli
stimuli = collect_stimuli(data_path)
stimuli.name = 'klab.Zhang2018.search_naturaldesign'

# create assembly for different subjects
assembly = collect_data(data_path, [*range(1, 16)])
assembly.name = 'klab.Zhang2018search_naturaldesign'

# package
print("\nPackaging Stimuli ----------")
package_stimulus_set(stimuli, stimulus_set_name=stimuli.name)

print("\nPackaging Assembly ----------")
package_data_assembly(assembly, data_assembly_name=assembly.name, stimulus_set_name=stimuli.name)


if __name__ == '__main__':
main()
121 changes: 121 additions & 0 deletions mkgu_packaging/klab/search_obj_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import os

import h5py
import numpy as np
import pandas as pd
import scipy.misc
import xarray as xr
from pathlib import Path
from result_caching import store
from sklearn.model_selection import StratifiedShuffleSplit
from tqdm import tqdm
from scipy.io import loadmat

from brainio_base.assemblies import BehavioralAssembly
from brainio_base.stimuli import StimulusSet
from brainio_contrib.packaging import package_stimulus_set, package_data_assembly

def collect_stimuli(data_path):
stimuli = []

arr_loc = loadmat(os.path.join(data_path / 'gt', 'array.mat'))

for i in range(1, 301):
j = (np.argmax(arr_loc['MyData'][i-1]['arraycate'][0] == arr_loc['MyData'][i-1]['targetcate'][0]))
target_path = os.path.join(data_path / 'stimuli', 'array_' + str(i) + '.jpg')
filename = 'array_' + str(i) + '.jpg'
image_id = 'stimuli_' + str(i)
image_label = 'stimuli'
sample_number = i

stimuli.append({
'image_current_local_file_path': target_path,
'image_path_within_store': filename,
'image_label': image_label,
'image_id': image_id,
'tar_obj_pos': j,
'sample_number': sample_number,
})

for i in range(1, 301):
j = (np.argmax(arr_loc['MyData'][i-1]['arraycate'][0] == arr_loc['MyData'][i-1]['targetcate'][0]))
target_path = os.path.join(data_path / 'target', 'target_' + str(i) + '.jpg')
filename = 'target_' + str(i) + '.jpg'
image_id = 'target_' + str(i)
image_label = 'target'
sample_number = i

stimuli.append({
'image_current_local_file_path': target_path,
'image_path_within_store': filename,
'image_label': image_label,
'image_id': image_id,
'tar_obj_pos': j,
'sample_number': sample_number,
})

for i in range(1,7):
target_path = os.path.join(data_path / 'gt', 'mask' + str(i) + '.jpg')
filename = 'mask' + str(i) + '.jpg'
image_id = 'mask_' + str(i)
image_label = 'mask'
sample_number = i

stimuli.append({
'image_current_local_file_path': target_path,
'image_path_within_store': filename,
'image_label': image_label,
'image_id': image_id,
'tar_obj_pos': i - 1,
'sample_number': sample_number,
})

stimuli = StimulusSet(stimuli)

stimuli.image_paths = {row.image_id: row.image_current_local_file_path for row in stimuli.itertuples()}
stimuli['image_file_name']= stimuli['image_path_within_store']

return stimuli

def collect_data(data_path, sub_id):
image_id = ['stimuli_' + str(i) for i in range(1, 301)]
subjects = []
for i in sub_id:
subjects += [i]*len(image_id)

S_data = np.load(os.path.join(data_path / 'human_data', 'human_all.npy'))
I_data = np.load(os.path.join(data_path / 'human_data', 'I_human_all.npy'))
data = np.zeros((300*len(sub_id), 8, 2), dtype=int)
data[:,:7,:] = S_data
data[:,7,:] = I_data

assembly = BehavioralAssembly(data,
coords={'image_id': ('presentation', image_id*len(sub_id)),
'subjects': ('presentation', subjects),
'fixation': [*range(8)],
'position': ['x', 'y']},
dims=['presentation', 'fixation', 'position'])
return assembly

def main():
data_dir = Path(__file__).parent / 'search_datasets'
data_path = data_dir / 'array'

# create stimuli
stimuli = collect_stimuli(data_path)
stimuli.name = 'klab.Zhang2018.search_obj_array'

# create assembly for different subjects
assembly = collect_data(data_path, [*range(1, 16)])
assembly.name = 'klab.Zhang2018search_obj_array'

# package
print("\nPackaging Stimuli ----------")
package_stimulus_set(stimuli, stimulus_set_name=stimuli.name)

print("\nPackaging Assembly ----------")
package_data_assembly(assembly, data_assembly_name=assembly.name, stimulus_set_name=stimuli.name)


if __name__ == '__main__':
main()
117 changes: 117 additions & 0 deletions mkgu_packaging/klab/search_waldo.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
import os

import h5py
import numpy as np
import pandas as pd
import scipy.misc
import xarray as xr
from pathlib import Path
from result_caching import store
from sklearn.model_selection import StratifiedShuffleSplit
from tqdm import tqdm
from scipy.io import loadmat

from brainio_base.assemblies import BehavioralAssembly
from brainio_base.stimuli import StimulusSet
from brainio_contrib.packaging import package_stimulus_set, package_data_assembly

def collect_stimuli(data_path):
stimuli = []

# search images
for i in range(1, 68):
target_path = os.path.join(data_path / 'stimuli', 's_' + str(i) + '.jpg')
filename = 's_' + str(i) + '.jpg'
image_id = 'klab_vs_waldo_stimuli_' + str(i)
image_label = 'stimuli'
sample_number = i

stimuli.append({
'image_current_local_file_path': target_path,
'image_path_within_store': filename,
'image_label': image_label,
'image_id': image_id,
'sample_number': sample_number,
})

# target images
for i in range(1, 68):
target_path = os.path.join(data_path / 'target', 't_' + str(i) + '.jpg')
filename = 't_' + str(i) + '.jpg'
image_id = 'klab_vs_waldo_target_' + str(i)
image_label = 'target'
sample_number = i

stimuli.append({
'image_current_local_file_path': target_path,
'image_path_within_store': filename,
'image_label': image_label,
'image_id': image_id,
'sample_number': sample_number,
})

# target mask
for i in range(1, 68):
target_path = os.path.join(data_path / 'gt', 'gt_' + str(i) + '.jpg')
filename = 'gt_' + str(i) + '.jpg'
image_id = 'klab_vs_waldo_gt_' + str(i)
image_label = 'gt'
sample_number = i

stimuli.append({
'image_current_local_file_path': target_path,
'image_path_within_store': filename,
'image_label': image_label,
'image_id': image_id,
'sample_number': sample_number,
})

stimuli = StimulusSet(stimuli)

stimuli.image_paths = {row.image_id: row.image_current_local_file_path for row in stimuli.itertuples()}
stimuli['image_file_name']= stimuli['image_path_within_store']

return stimuli

def collect_data(data_path, sub_id):
image_id = ['stimuli_' + str(i) for i in range(1, 68)]
subjects = []
for i in sub_id:
subjects += [i]*len(image_id)

S_data = np.load(os.path.join(data_path / 'human_data', 'human_all.npy'))
I_data = np.load(os.path.join(data_path / 'human_data', 'I_human_all.npy'))
data = np.zeros((67*len(sub_id), 81, 2), dtype=int)
data[:,:80,:] = S_data
data[:,80,:] = I_data

assembly = BehavioralAssembly(data,
coords={'image_id': ('presentation', image_id*len(sub_id)),
'subjects': ('presentation', subjects),
'fixation': [*range(81)],
'position': ['x', 'y']},
dims=['presentation', 'fixation', 'position'])
return assembly

def main():
data_dir = Path(__file__).parent / 'search_datasets'
data_path = data_dir / 'waldo'

# create stimuli
stimuli = collect_stimuli(data_path)
stimuli.name = 'klab.Zhang2018.search_waldo'

# create assembly for different subjects
assembly = collect_data(data_path, [*range(1, 16)])
assembly.name = 'klab.Zhang2018search_waldo'

# package
print("\nPackaging Stimuli ----------")
package_stimulus_set(stimuli, stimulus_set_name=stimuli.name)

print("\nPackaging Assembly ----------")
package_data_assembly(assembly, data_assembly_name=assembly.name, stimulus_set_name=stimuli.name)


if __name__ == '__main__':
main()