Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/gmierz/pupil-lib-python i…
Browse files Browse the repository at this point in the history
…nto setuptools_dev
  • Loading branch information
gmierz committed Feb 3, 2018
2 parents 567cca7 + 96b45dc commit 5a9d746
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 30 deletions.
72 changes: 42 additions & 30 deletions pupillib/core/data_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ def time_or_data(self, val):
def load(self, all_data=None):
self.all_data = all_data

def destroy_all_data(self):
self.all_data = None


class PupilDatasets(CommonPupilData):
def __init__(self, config, all_data):
Expand All @@ -39,52 +42,60 @@ def data_type(self, data_type):
self._data_type = data_type

for _, ds in self.datasets.items():
ds.data_type = self._data_type
if ds:
ds.data_type = self._data_type

@CommonPupilData.time_or_data.setter
def time_or_data(self, val):
self._time_or_data = val

for _, ds in self.datasets.items():
ds.time_or_data = self._time_or_data
if ds:
ds.time_or_data = self._time_or_data

def save_csv(self, output_dir, name=''):
print('Saving data into a csv file.')
for _, dataset in self.datasets.items():
dataset.save_csv(output_dir, name=name)
if dataset:
dataset.save_csv(output_dir, name=name)

def get_csv(self):
print('Getting csv file.')
csv_files = {}
for dname, dataset in self.datasets.items():
csv_files[dname] = dataset.get_csv()
if dataset:
csv_files[dname] = dataset.get_csv()
return csv_files

def get_matrix(self):
print('Getting matrix.')
dataset_mat = {}
for dname, dataset in self.datasets.items():
dataset_mat[dname] = dataset.get_matrix()
if dataset:
dataset_mat[dname] = dataset.get_matrix()
return dataset_mat

# If source_dset_name is defined, then we will take
# a dataset from within this object and merge it into
# target_dset_name. If dsets_object is defined then we
# will take all streams, from all datasets, from that
# object and merge them into target_dset_name.
def merge(self, target_dset_name, source_dset_name=None, dsets_object=None):
def merge(self, target_dset_name, source_dset_name=None, dsets_object=None, keep_raw=False):
if target_dset_name not in self.datasets:
raise Exception('Error merging: target_dset_name=' + target_dset_name + ' does not exist in this datastore object.')
if not self.datasets[target_dset_name]:
raise Exception(
'Error merging: target_dset_name=' + target_dset_name + ' is None in this datastore object.')
merge_into = self.datasets[target_dset_name]

if source_dset_name:
merge_src = self.datasets[source_dset_name]
merge_into.merge(merge_src)
merge_into.merge(merge_src, keep_raw=keep_raw)
elif dsets_object:
for dset in dsets_object.datasets.items():
merge_into.merge(dset)
merge_into.merge(dset, keep_raw=keep_raw)
else:
raise Exception("Error: Merging requires either a name to be defined or a datasets object.")
for dset in self.datasets:
if dset != target_dset_name:
merge_into.merge(self.datasets[dset], keep_raw=keep_raw)
self.datasets[dset] = None

self.datasets[target_dset_name] = merge_into

Expand All @@ -103,6 +114,8 @@ def load(self, all_data=None):
pd.load()
self.datasets[dataset_name] = pd

self.destroy_all_data()


class PupilDataset(CommonPupilData):
def __init__(self, dataset, dataset_name):
Expand All @@ -125,29 +138,26 @@ def time_or_data(self, val):
ds.time_or_data = self._time_or_data

def save_csv(self, output_dir, name=''):
print('Saving data into a csv file.')
for _, data in self.data_streams.items():
data.save_csv(output_dir, name + '_' + self.dataset_name)

def get_csv(self):
print('Getting csv file.')
csv_files = {}
for dname, data in self.data_streams.items():
csv_files[dname] = data.get_csv()
return csv_files

def get_matrix(self):
print('Getting matrix.')
datastream_mats = {}
for dname, data in self.data_streams.items():
datastream_mats[dname] = data.get_matrix()
return datastream_mats

# Merges all data from dset_src into itself.
def merge(self, dset_src):
for data_name, datastream in dset_src.items():
def merge(self, dset_src, keep_raw=False):
for data_name, datastream in dset_src.data_streams.items():
if data_name in self.data_streams:
self.data_streams[data_name].merge(datastream)
self.data_streams[data_name].merge(datastream, keep_raw=keep_raw)

def load(self, all_data=None):
if all_data is not None:
Expand All @@ -159,6 +169,8 @@ def load(self, all_data=None):
pd_stream.load()
self.data_streams[data_name] = pd_stream

self.destroy_all_data()


class PupilDatastream(CommonPupilData):
def __init__(self, stream_data, data_name):
Expand Down Expand Up @@ -190,26 +202,26 @@ def time_or_data(self, val):
trigger.time_or_data = self._time_or_data

def save_csv(self, output_dir, name=''):
print('Saving data into a csv file.')
for _, trigger in self.triggers.items():
trigger.save_csv(output_dir, name + '_' + self.data_name)

def get_csv(self):
print('Getting csv file.')
csv_files = {}
for _, trigger in self.triggers.items():
csv_files[trigger.name] = trigger.get_csv()

def get_matrix(self):
print('Getting matrix.')
stream_mats = {}
for _, trigger in self.triggers.items():
stream_mats[trigger.name] = trigger.get_matrix()
return stream_mats

def merge(self, dstream_src):
for trigger_name, trig_data in dstream_src.items():
def merge(self, dstream_src, keep_raw=False):
for trigger_name, trig_data in dstream_src.triggers.items():
if trigger_name in self.triggers:
if not keep_raw:
trig_data.data = None
trig_data.timestamps = None
self.triggers[trigger_name].merge(trig_data)

def load(self, all_data=None):
Expand All @@ -222,6 +234,8 @@ def load(self, all_data=None):
pds_trigger.load()
self.triggers[trigger_name] = pds_trigger

self.destroy_all_data()


class PupilTrigger(CommonPupilData):
def __init__(self, trigger_data, trigger_name):
Expand All @@ -244,14 +258,12 @@ def time_or_data(self, val):
trial.time_or_data = self._time_or_data

def save_csv(self, output_dir, name=''):
print('Saving data into a csv file.')
fname = name + '_' + self.time_or_data + '_' + self.data_type + '_trigger_' + self.trigger_name + '.csv'
with open(os.path.join(output_dir, fname), 'w+') as csv_output:
csv_output.write(self.get_csv())

def get_csv(self):
# Depends on get matrix
print('Getting csv file.')
csv_file = ''
mat = self.get_matrix()
count = 0
Expand All @@ -265,16 +277,14 @@ def get_csv(self):
return csv_file

def get_matrix(self):
print('Getting matrix.')
pupil_matrix = []
for trial in self.trials:
mat = trial.get_matrix()
print(len(mat))
pupil_matrix.append(trial.get_matrix())
return pupil_matrix

def merge(self, trigger_src):
for trial in trigger_src.trials.items():
for trial in trigger_src.trials:
self.trials.append(trial)

def load(self, all_data=None):
Expand All @@ -288,6 +298,8 @@ def load(self, all_data=None):
pdst_trial.load()
self.trials.append(pdst_trial)

self.destroy_all_data()


class PupilTrial(CommonPupilData):
def __init__(self, trial_data, trial_num):
Expand Down Expand Up @@ -323,14 +335,12 @@ def proc_data(self, data):
self.__proc_data[self.time_or_data] = data

def save_csv(self, output_dir, name=''):
print('Saving data into a csv file.')
fname = name + '_' + self.time_or_data + '_' + self.data_type + '_' + 'trial_' + str(self.trial_num) + '.csv'
with open(os.path.join(output_dir, fname), 'w+') as csv_output:
csv_output.write(self.get_csv())

def get_csv(self):
# Depends on get matrix
print('Getting csv file.')
return ",".join(map(str, self.get_matrix()))

def get_matrix(self, data_type=None):
Expand Down Expand Up @@ -358,3 +368,5 @@ def load(self, all_data=None):
self.__baserem_data = self.all_data['trial_rmbaseline']
self.__pc_data = self.all_data['trial_pc']
self.proc_data = copy.deepcopy(self.__original_data)

self.destroy_all_data()
25 changes: 25 additions & 0 deletions pupillib/pupil_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,31 @@ def script_run(yaml_path=''):
plibrunner.run()
return plibrunner

def save_csv(matrix, output_dir, name='temp'):
fname = name + '.csv'
with open(os.path.join(output_dir, fname), 'w+') as csv_output:
csv_output.write(get_csv(matrix))

def save_csv_line(line, output_dir, name='temp'):
fname = name + '.csv'
with open(os.path.join(output_dir, fname), 'w+') as csv_output:
csv_output.write(",".join(map(str, line)))

def get_csv(mat):
# Depends on get matrix
csv_file = ''
count = 0
max_count = len(mat)
mat = np.asmatrix(mat)

for trial in mat:
if count < max_count - 1:
csv_file += ",".join(map(str, trial)) + '\n'
else:
csv_file += ",".join(map(str, trial))

return csv_file

def main():
# Used to run Pupil-Lib from CLI. Alternatively,
# this function can be used in another script that would
Expand Down

0 comments on commit 5a9d746

Please sign in to comment.