Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def read_txt(input_file_name):
"""Creates a Key object from information stored in a text file.
:param input_file_name: name of the file to read from
"""
key = Key()
models, testsegs, trial = numpy.loadtxt(input_file_name,
delimiter=' ',
dtype={'names': ('mod', 'seg', 'key'),
'formats': ('S1000', 'S1000', 'S10')},
unpack=True)
models = models.astype('|O', copy=False).astype('S', copy=False)
testsegs = testsegs.astype('|O', copy=False).astype('S', copy=False)
trial = trial.astype('|O', copy=False).astype('S', copy=False)
if sys.version_info[0] == 3:
models = models.astype('U', copy=False)
testsegs = testsegs.astype('U', copy=False)
trial = trial.astype('U', copy=False)
def read(input_file_fame):
"""Reads a Key object from an hdf5 file.
:param input_file_fame: name of the file to read from
"""
with h5py.File(input_file_fame, "r") as f:
key = Key()
key.modelset = f.get("modelset").value
key.segset = f.get("segset").value
# if running python 3, need a conversion to unicode
if sys.version_info[0] == 3:
key.modelset = key.modelset.astype('U100', copy=False)
key.segset = key.segset.astype('U100', copy=False)
trialmask = f.get("trial_mask").value
key.tar = (trialmask == 1)
key.non = (trialmask == -1)
assert key.validate(), "Error: wrong Key format"
return key
:param keep: a boolean indicating whether modlist and seglist are the
models to keep or discard.
:return: a filtered version of 'inkey'.
"""
if keep:
keepmods = modlist
keepsegs = seglist
else:
keepmods = diff(self.modelset, modlist)
keepsegs = diff(self.segset, seglist)
keepmodidx = numpy.array(ismember(self.modelset, keepmods))
keepsegidx = numpy.array(ismember(self.segset, keepsegs))
outkey = Key()
outkey.modelset = self.modelset[keepmodidx]
outkey.segset = self.segset[keepsegidx]
tmp = self.tar[numpy.array(keepmodidx), :]
outkey.tar = tmp[:, numpy.array(keepsegidx)]
tmp = self.non[numpy.array(keepmodidx), :]
outkey.non = tmp[:, numpy.array(keepsegidx)]
assert(outkey.validate())
if self.modelset.shape[0] > outkey.modelset.shape[0]:
logging.info('Number of models reduced from %d to %d', self.modelset.shape[0], outkey.modelset.shape[0])
if self.segset.shape[0] > outkey.segset.shape[0]:
logging.info('Number of test segments reduced from %d to %d', self.segset.shape[0], outkey.segset.shape[0])
return outkey
Key objects to merge in the curent one.
:param key_list: the list of Keys to merge
"""
# the output key must have all models and segment in the input
# keys (only once) and the same target and non-target trials.
# It is an error if a trial is a target in one key and a
# non-target in another, but a target or non-target marker will
# override a 'non-trial' marker.
assert isinstance(key_list, list), "Input is not a list"
for key in key_list:
assert isinstance(key_list, list), \
'{} {} {}'.format("Element ", key, " is not a list")
for key2 in key_list:
key_new = Key()
key1 = self
# create new ndx with empty masks
key_new.modelset = numpy.union1d(key1.modelset, key2.modelset)
key_new.segset = numpy.union1d(key1.segset, key2.segset)
# expand ndx1 mask
tar_1 = numpy.zeros((key_new.modelset.shape[0],
key_new.segset.shape[0]),
dtype="bool")
non_1 = numpy.zeros((key_new.modelset.shape[0],
key_new.segset.shape[0]), dtype="bool")
model_index_a = numpy.argwhere(numpy.in1d(key_new.modelset, key1.modelset))
model_index_b = numpy.argwhere(numpy.in1d(key1.modelset, key_new.modelset))
seg_index_a = numpy.argwhere(numpy.in1d(key_new.segset, key1.segset))
seg_index_b = numpy.argwhere(numpy.in1d(key1.segset, key_new.segset))