diff --git a/biosppy/biometrics.py b/biosppy/biometrics.py index 24c68e0a..a6c5210e 100644 --- a/biosppy/biometrics.py +++ b/biosppy/biometrics.py @@ -247,7 +247,7 @@ def list_subjects(self): """ - subjects = self._subject2label.keys() + subjects = list(self._subject2label.keys()) return subjects @@ -356,7 +356,7 @@ def batch_train(self, data=None): if data is None: raise TypeError("Please specify the data to train.") - for sub, val in data.iteritems(): + for sub, val in data.items(): if val is None: try: self.dismiss(sub, deferred=True) @@ -399,10 +399,10 @@ def update_thresholds(self, fraction=1.): # gather data to test data = {} - for subject, label in self._subject2label.iteritems(): + for subject, label in self._subject2label.items(): # select a random fraction of the training data aux = self.io_load(label) - indx = range(len(aux)) + indx = list(range(len(aux))) use, _ = utils.random_fraction(indx, fraction, sort=True) data[subject] = aux[use] @@ -411,7 +411,7 @@ def update_thresholds(self, fraction=1.): _, res = self.evaluate(data, ths) # choose thresholds at EER - for subject, label in self._subject2label.iteritems(): + for subject, label in self._subject2label.items(): EER_auth = res['subject'][subject]['authentication']['rates']['EER'] self.set_auth_thr(label, EER_auth[self.EER_IDX, 0], ready=True) @@ -639,7 +639,7 @@ def evaluate(self, data, thresholds=None, show=False): thresholds = self.get_thresholds() # get subjects - subjects = filter(lambda item: self.check_subject(item), data.keys()) + subjects = [item for item in list(data.keys()) if self.check_subject(item)] if len(subjects) == 0: raise ValueError("No enrolled subjects in test set.") @@ -722,7 +722,7 @@ def cross_validation(cls, data, labels, cv, thresholds=None, **kwargs): lbl = labels[item] train_idx[lbl].append(item) - train_data = {sub: data[idx] for sub, idx in train_idx.iteritems()} + train_data = {sub: data[idx] for sub, idx in train_idx.items()} # test data set test_idx = collections.defaultdict(list) @@ -730,7 +730,7 @@ def cross_validation(cls, data, labels, cv, thresholds=None, **kwargs): lbl = labels[item] test_idx[lbl].append(item) - test_data = {sub: data[idx] for sub, idx in test_idx.iteritems()} + test_data = {sub: data[idx] for sub, idx in test_idx.items()} # instantiate classifier clf = cls(**kwargs) @@ -832,8 +832,8 @@ def _prepare(self, data, targets=None): # target class labels if targets is None: - targets = self._subject2label.values() - elif isinstance(targets, basestring): + targets = list(self._subject2label.values()) + elif isinstance(targets, str): targets = [targets] return data @@ -975,7 +975,7 @@ def _authenticate(self, data, label, threshold): # select based on subject label aux = [] ns = len(dists) - for i in xrange(ns): + for i in range(ns): aux.append(dists[i, train_labels[i, :] == label]) dists = np.array(aux) @@ -984,7 +984,7 @@ def _authenticate(self, data, label, threshold): dists = dists[:, :self.k] decision = np.zeros(ns, dtype='bool') - for i in xrange(ns): + for i in range(ns): # compare distances to threshold count = np.sum(dists[i, :] <= threshold) @@ -1014,8 +1014,8 @@ def _get_thresholds(self): return np.linspace(self.min_thr, 1., 100) maxD = [] - for _ in xrange(3): - for label in self._subject2label.values(): + for _ in range(3): + for label in list(self._subject2label.values()): # randomly select samples aux = self.io_load(label) ind = np.random.randint(0, aux.shape[0], 3) @@ -1064,7 +1064,7 @@ def _identify(self, data, threshold=None): ns = len(dists) labels = [] - for i in xrange(ns): + for i in range(ns): lbl, _ = majority_rule(train_labels[i, :], random=True) # compare distances to threshold @@ -1102,8 +1102,8 @@ def _prepare(self, data, targets=None): # target class labels if targets is None: - targets = self._subject2label.values() - elif isinstance(targets, basestring): + targets = list(self._subject2label.values()) + elif isinstance(targets, str): targets = [targets] dists = [] @@ -1415,7 +1415,7 @@ def _authenticate(self, data, label, threshold): aux = aux[sel, :] decision = [] - for i in xrange(ns): + for i in range(ns): # determine majority predMax, count = majority_rule(aux[:, i], random=True) rate = float(count) / norm @@ -1483,7 +1483,7 @@ def _identify(self, data, threshold=None): norm = 1.0 labels = [] - for i in xrange(ns): + for i in range(ns): # determine majority predMax, count = majority_rule(aux[:, i], random=True) rate = float(count) / norm @@ -1524,11 +1524,11 @@ def _prepare(self, data, targets=None): # target class labels if self._nbSubjects == 1: - pairs = self._models.keys() + pairs = list(self._models.keys()) else: if targets is None: - pairs = self._models.keys() - elif isinstance(targets, basestring): + pairs = list(self._models.keys()) + elif isinstance(targets, str): labels = list( set(self._subject2label.values()) - set([targets])) pairs = [[targets, lbl] for lbl in labels] @@ -1563,9 +1563,9 @@ def _train(self, enroll=None, dismiss=None): dismiss = [] # process dismiss - pairs = self._models.keys() + pairs = list(self._models.keys()) for t in dismiss: - pairs = filter(lambda p: t in p, pairs) + pairs = [p for p in pairs if t in p] for p in pairs: self._del_clf(p) @@ -1590,11 +1590,11 @@ def _train(self, enroll=None, dismiss=None): # check singles if self._nbSubjects == 1: - label = self._subject2label.values()[0] + label = list(self._subject2label.values())[0] X = self.io_load(label) self._get_single_clf(X, label) elif self._nbSubjects > 1: - aux = filter(lambda p: '' in p, self._models.keys()) + aux = [p for p in list(self._models.keys()) if '' in p] if len(aux) != 0: for p in aux: self._del_clf(p) @@ -1850,7 +1850,7 @@ def get_subject_results(results=None, R = np.zeros(nth, dtype='float') CM = [] - for i in xrange(nth): # for each threshold + for i in range(nth): # for each threshold # authentication for k, lbl in enumerate(subject_idx): # for each subject subject_tst = subjects[k] @@ -2163,7 +2163,7 @@ def combination(results=None, weights=None): weights = {} # compile results to find all classes - vec = results.values() + vec = list(results.values()) if len(vec) == 0: raise CombinationError("No keys found.") @@ -2182,13 +2182,13 @@ def combination(results=None, weights=None): # multi-class counts = np.zeros(nb, dtype='float') - for n in results.iterkeys(): + for n in results.keys(): # ensure array res = np.array(results[n]) ns = float(len(res)) # get count for each unique class - for i in xrange(nb): + for i in range(nb): aux = float(np.sum(res == unq[i])) w = weights.get(n, 1.) counts[i] += ((aux / ns) * w) diff --git a/biosppy/clustering.py b/biosppy/clustering.py index 5071cc28..2d15aa34 100644 --- a/biosppy/clustering.py +++ b/biosppy/clustering.py @@ -134,7 +134,7 @@ def hierarchical(data=None, 'ward', 'weighted']: raise ValueError("Unknown linkage criterion '%r'." % linkage) - if not isinstance(metric, basestring): + if not isinstance(metric, str): raise TypeError("Please specify the distance metric as a string.") N = len(data) @@ -405,7 +405,7 @@ def create_coassoc(ensemble=None, N=None): nparts = len(ensemble) assoc = 0 for part in ensemble: - nsamples = np.array([len(part[key]) for key in part.iterkeys()]) + nsamples = np.array([len(part[key]) for key in part.keys()]) dim = np.sum(nsamples * (nsamples - 1)) / 2 I = np.zeros(dim) @@ -413,11 +413,11 @@ def create_coassoc(ensemble=None, N=None): X = np.ones(dim) ntriplets = 0 - for v in part.itervalues(): + for v in part.values(): nb = len(v) if nb > 0: - for h in xrange(nb): - for f in xrange(h + 1, nb): + for h in range(nb): + for f in range(h + 1, nb): I[ntriplets] = v[h] J[ntriplets] = v[f] ntriplets += 1 @@ -539,7 +539,7 @@ def mdist_templates(data=None, clusters = {0: np.arange(len(data), dtype='int')} # cluster labels - ks = clusters.keys() + ks = list(clusters.keys()) # remove the outliers' cluster, if present if '-1' in ks: @@ -631,7 +631,7 @@ def centroid_templates(data=None, clusters=None, ntemplates=1): raise TypeError("Please specify a data clustering.") # cluster labels - ks = clusters.keys() + ks = list(clusters.keys()) # remove the outliers' cluster, if present if '-1' in ks: @@ -839,7 +839,7 @@ def outliers_dmean(data=None, outliers.append(i) outliers = np.unique(outliers) - normal = np.setdiff1d(range(len(data)), outliers, assume_unique=True) + normal = np.setdiff1d(list(range(len(data))), outliers, assume_unique=True) # output clusters = {-1: outliers, 0: normal} @@ -987,7 +987,7 @@ def _merge_clusters(clusters): """ - keys = clusters.keys() + keys = list(clusters.keys()) # outliers if -1 in keys: diff --git a/biosppy/metrics.py b/biosppy/metrics.py index b06debb8..876442ac 100644 --- a/biosppy/metrics.py +++ b/biosppy/metrics.py @@ -83,7 +83,7 @@ def pdist(X, metric='euclidean', p=2, w=None, V=None, VI=None): """ - if isinstance(metric, basestring): + if isinstance(metric, str): if metric == 'pcosine': metric = pcosine @@ -127,7 +127,7 @@ def cdist(XA, XB, metric='euclidean', p=2, V=None, VI=None, w=None): """ - if isinstance(metric, basestring): + if isinstance(metric, str): if metric == 'pcosine': metric = pcosine diff --git a/biosppy/plotting.py b/biosppy/plotting.py index 0d07b2a9..47bf578b 100644 --- a/biosppy/plotting.py +++ b/biosppy/plotting.py @@ -770,7 +770,7 @@ def _plot_multichannel(ts=None, # check labels if labels is None: - labels = ['Ch. %d' % i for i in xrange(nch)] + labels = ['Ch. %d' % i for i in range(nch)] if nch < nrows: nrows = nch @@ -794,7 +794,7 @@ def _plot_multichannel(ts=None, ax0.grid() axs = {(0, 0): ax0} - for i in xrange(1, nch - 1): + for i in range(1, nch - 1): a = i % nrows b = int(np.floor(i / float(nrows))) ax = fig.add_subplot(gs[a, b], sharex=ax0) @@ -822,7 +822,7 @@ def _plot_multichannel(ts=None, if xlabel is not None: ax.set_xlabel(xlabel) - for b in xrange(0, ncols - 1): + for b in range(0, ncols - 1): a = nrows - 1 ax = axs[(a, b)] ax.set_xlabel(xlabel) @@ -1042,7 +1042,7 @@ def plot_biometrics(assessment=None, eer_idx=None, path=None, show=False): id_ax = fig.add_subplot(122) # subject results - for sub in assessment['subject'].iterkeys(): + for sub in assessment['subject'].keys(): auth_rates = assessment['subject'][sub]['authentication']['rates'] _ = _plot_rates(ths, auth_rates, ['FAR', 'FRR'], lw=MINOR_LW, @@ -1134,7 +1134,7 @@ def plot_clustering(data=None, clusters=None, path=None, show=False): ymin, ymax = _yscaling(data, alpha=1.2) # determine number of clusters - keys = clusters.keys() + keys = list(clusters.keys()) nc = len(keys) if nc <= 4: diff --git a/biosppy/signals/bvp.py b/biosppy/signals/bvp.py index 82bce919..95f4aa6f 100644 --- a/biosppy/signals/bvp.py +++ b/biosppy/signals/bvp.py @@ -181,7 +181,7 @@ def find_onsets(signal=None, sampling_rate=1000., sm_size=None, size=None, # analyze between maxima of 2nd derivative of ss detected = False - for i in xrange(1, len(dpidx) + 1): + for i in range(1, len(dpidx) + 1): try: v, u = dpidx[i - 1], dpidx[i] except IndexError: diff --git a/biosppy/signals/ecg.py b/biosppy/signals/ecg.py index 58d27d9d..00ef0310 100644 --- a/biosppy/signals/ecg.py +++ b/biosppy/signals/ecg.py @@ -664,11 +664,11 @@ def engzee_segmenter(signal=None, sampling_rate=1000., threshold=0.48): mmp = 0.2 # Differentiator (1) - y1 = map(lambda i: signal[i] - signal[i - 4], xrange(4, len(signal))) + y1 = [signal[i] - signal[i - 4] for i in range(4, len(signal))] # Low pass filter (2) c = [1, 4, 6, 4, 1, -1, -4, -6, -4, -1] - y2 = np.array(map(lambda n: np.dot(c, y1[n - 9:n + 1]), xrange(9, len(y1)))) + y2 = np.array([np.dot(c, y1[n - 9:n + 1]) for n in range(9, len(y1))]) y2_len = len(y2) # vars @@ -1087,7 +1087,7 @@ def hamilton_segmenter(signal=None, sampling_rate=1000.): pass # getting positive peaks - for i in xrange(len(pospeaks) - 1): + for i in range(len(pospeaks) - 1): if abs(pospeaks[0][1] - pospeaks[i + 1][1]) > adjacency: twopeaks.append(pospeaks[i + 1]) break @@ -1097,7 +1097,7 @@ def hamilton_segmenter(signal=None, sampling_rate=1000.): error[0] = True # getting negative peaks - for i in xrange(len(negpeaks) - 1): + for i in range(len(negpeaks) - 1): if abs(negpeaks[0][1] - negpeaks[i + 1][1]) > adjacency: twonegpeaks.append(negpeaks[i + 1]) break diff --git a/biosppy/signals/eda.py b/biosppy/signals/eda.py index 82d4c827..1514cdc7 100644 --- a/biosppy/signals/eda.py +++ b/biosppy/signals/eda.py @@ -156,7 +156,7 @@ def basic_scr(signal=None, sampling_rate=1000.): i0[0] = 0 # amplitude - a = np.array(map(lambda i: np.max(signal[i1[i]:i3[i]]), range(li))) + a = np.array([np.max(signal[i1[i]:i3[i]]) for i in range(li)]) # output args = (i3, i1, a) @@ -217,7 +217,7 @@ def kbk_scr(signal=None, sampling_rate=1000.): thr = 0.1 * np.max(df) scrs, amps, ZC, pks = [], [], [], [] - for i in xrange(0, len(zeros) - 1, 2): + for i in range(0, len(zeros) - 1, 2): scrs += [df[zeros[i]:zeros[i + 1]]] aux = scrs[-1].max() if aux > thr: diff --git a/biosppy/signals/eeg.py b/biosppy/signals/eeg.py index dcaa6e0b..4b9c2927 100644 --- a/biosppy/signals/eeg.py +++ b/biosppy/signals/eeg.py @@ -75,7 +75,7 @@ def eeg(signal=None, sampling_rate=1000., labels=None, show=True): nch = signal.shape[1] if labels is None: - labels = ['Ch. %d' % i for i in xrange(nch)] + labels = ['Ch. %d' % i for i in range(nch)] else: if len(labels) != nch: raise ValueError( @@ -261,8 +261,8 @@ def get_power_features(signal=None, # must be odd md_size += 1 - for i in xrange(nb): - for j in xrange(nch): + for i in range(nb): + for j in range(nch): values[:, i, j], _ = st.smoother(signal=values[:, i, j], kernel='median', size=md_size) @@ -331,7 +331,7 @@ def get_plf_features(signal=None, sampling_rate=1000., size=0.25, overlap=0.5): N = min_pad # PLF pairs - pairs = [(i, j) for i in xrange(nch) for j in xrange(i + 1, nch)] + pairs = [(i, j) for i in range(nch) for j in range(i + 1, nch)] nb = len(pairs) # windower @@ -349,7 +349,7 @@ def get_plf_features(signal=None, sampling_rate=1000., size=0.25, overlap=0.5): # must be odd md_size += 1 - for i in xrange(nb): + for i in range(nb): values[:, i], _ = st.smoother(signal=values[:, i], kernel='median', size=md_size) @@ -389,7 +389,7 @@ def _power_features(signal=None, sampling_rate=1000., bands=None, pad=0): nch = signal.shape[1] out = np.zeros((len(bands), nch), dtype='float') - for i in xrange(nch): + for i in range(nch): # compute power spectrum freqs, power = st.power_spectrum(signal=signal[:, i], sampling_rate=sampling_rate, diff --git a/biosppy/signals/tools.py b/biosppy/signals/tools.py index 31011bed..75a964ce 100644 --- a/biosppy/signals/tools.py +++ b/biosppy/signals/tools.py @@ -479,7 +479,7 @@ def smoother(signal=None, kernel='boxzen', size=10, mirror=True, **kwargs): length = len(signal) - if isinstance(kernel, basestring): + if isinstance(kernel, str): # check length if size > length: size = length - 1 @@ -998,7 +998,7 @@ def windower(signal=None, length = len(signal) - if isinstance(kernel, basestring): + if isinstance(kernel, str): # check size if size > length: raise ValueError("Window size must be smaller than signal length.") @@ -1029,7 +1029,7 @@ def windower(signal=None, index = [] values = [] - for i in xrange(nb): + for i in range(nb): start = i * step stop = start + size index.append(start) diff --git a/biosppy/storage.py b/biosppy/storage.py index 4fbff053..a89f0015 100644 --- a/biosppy/storage.py +++ b/biosppy/storage.py @@ -320,7 +320,7 @@ def store_txt(path, data, sampling_rate=1000., resolution=None, date=None, if resolution is not None: header += "Resolution:= %d\n" % resolution if date is not None: - if isinstance(date, basestring): + if isinstance(date, str): header += "Date:= %s\n" % date elif isinstance(date, datetime.datetime): header += "Date:= %s\n" % date.isoformat() @@ -338,7 +338,7 @@ def store_txt(path, data, sampling_rate=1000., resolution=None, date=None, ncols = data.shape[1] if labels is None: - labels = ['%d' % i for i in xrange(ncols)] + labels = ['%d' % i for i in range(ncols)] elif len(labels) != ncols: raise ValueError("Inconsistent number of labels.") @@ -720,7 +720,7 @@ def del_signal_group(self, group=''): if node.name == '/signals': # delete all elements - for _, item in node.iteritems(): + for _, item in node.items(): try: del self._file[item.name] except IOError: @@ -759,7 +759,7 @@ def list_signals(self, group='', recursive=False): raise KeyError("Inexistent signal group.") out = [] - for name, item in node.iteritems(): + for name, item in node.items(): if isinstance(item, h5py.Dataset): out.append((group, name)) elif recursive and isinstance(item, h5py.Group): @@ -972,7 +972,7 @@ def del_event_group(self, group=''): if node.name == '/events': # delete all elements - for _, item in node.iteritems(): + for _, item in node.items(): try: del self._file[item.name] except IOError: @@ -1011,7 +1011,7 @@ def list_events(self, group='', recursive=False): raise KeyError("Inexistent event group.") out = [] - for name, item in node.iteritems(): + for name, item in node.items(): if isinstance(item, h5py.Group): try: _ = item.attrs['json'] diff --git a/biosppy/utils.py b/biosppy/utils.py index be9dfeed..f3ca6f20 100644 --- a/biosppy/utils.py +++ b/biosppy/utils.py @@ -87,7 +87,7 @@ def remainderAllocator(votes, k, reverse=True, check=False): else: ind = np.argsort(aux - seats) - for i in xrange(nb): + for i in range(nb): seats[ind[i % length]] += 1 return seats.tolist() @@ -137,9 +137,9 @@ def highestAveragesAllocator(votes, k, divisor='dHondt', check=False): # compute coefficients tab = [] length = len(votes) - D = [fcn(i) for i in xrange(1, k + 1)] - for i in xrange(length): - for j in xrange(k): + D = [fcn(i) for i in range(1, k + 1)] + for i in range(length): + for j in range(k): tab.append((i, votes[i] / D[j])) # sort @@ -148,7 +148,7 @@ def highestAveragesAllocator(votes, k, divisor='dHondt', check=False): tab = np.array([item[0] for item in tab], dtype='int') seats = np.zeros(length, dtype='int') - for i in xrange(length): + for i in range(length): seats[i] = np.sum(tab == i) return seats.tolist() @@ -230,14 +230,14 @@ def __init__(self, values, names=None): if names is None: # create names - names = ['_%d' % i for i in xrange(nargs)] + names = ['_%d' % i for i in range(nargs)] else: # check length if len(names) != nargs: raise ValueError("Number of names and values mismatch.") # convert to str - names = map(str, names) + names = list(map(str, names)) # check for keywords, alphanumeric, digits, repeats seen = set() @@ -270,7 +270,7 @@ def as_dict(self): """ - return collections.OrderedDict(zip(self._names, self)) + return collections.OrderedDict(list(zip(self._names, self))) __dict__ = property(as_dict) @@ -291,7 +291,7 @@ def __getitem__(self, key): """ - if isinstance(key, basestring): + if isinstance(key, str): if key not in self._names: raise KeyError("Unknown key: %r." % key) diff --git a/docs/conf.py b/docs/conf.py index 28b48300..00ce1be9 100644 --- a/docs/conf.py +++ b/docs/conf.py @@ -76,9 +76,9 @@ def __getattr__(cls, name): master_doc = 'index' # General information about the project. -project = u'BioSPPy' -copyright = u'2015, Instituto de Telecomunicacoes' -author = u'Instituto de Telecomunicacoes' +project = 'BioSPPy' +copyright = '2015, Instituto de Telecomunicacoes' +author = 'Instituto de Telecomunicacoes' # The version info for the project you're documenting, acts as replacement for # |version| and |release|, also used in various other places throughout the @@ -252,8 +252,8 @@ def __getattr__(cls, name): # (source start file, target name, title, # author, documentclass [howto, manual, or own class]). latex_documents = [ - (master_doc, 'BioSPPy.tex', u'BioSPPy Documentation', - u'Instituto de Telecomunicacoes', 'manual'), + (master_doc, 'BioSPPy.tex', 'BioSPPy Documentation', + 'Instituto de Telecomunicacoes', 'manual'), ] # The name of an image file (relative to this directory) to place at the top of @@ -282,7 +282,7 @@ def __getattr__(cls, name): # One entry per manual page. List of tuples # (source start file, name, description, authors, manual section). man_pages = [ - (master_doc, 'biosppy', u'BioSPPy Documentation', + (master_doc, 'biosppy', 'BioSPPy Documentation', [author], 1) ] @@ -296,7 +296,7 @@ def __getattr__(cls, name): # (source start file, target name, title, author, # dir menu entry, description, category) texinfo_documents = [ - (master_doc, 'BioSPPy', u'BioSPPy Documentation', + (master_doc, 'BioSPPy', 'BioSPPy Documentation', author, 'BioSPPy', 'One line description of project.', 'Miscellaneous'), ]