Skip to content

Commit

Permalink
remove six and __future__
Browse files Browse the repository at this point in the history
  • Loading branch information
dkaslovsky committed Aug 27, 2020
1 parent a6458c8 commit ff0fdfa
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 25 deletions.
14 changes: 3 additions & 11 deletions coupled_biased_random_walks/count.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,7 @@
from collections import Counter, defaultdict
from collections.abc import Mapping
from itertools import combinations, tee

from six import iteritems

try:
# python 2
from collections import Mapping
except ImportError:
# python 3
from collections.abc import Mapping


class IncrementingDict(Mapping):

Expand Down Expand Up @@ -91,9 +83,9 @@ def update(self, observation_iterable):
# feature name with value NaN represents a missing feature in the
# observation (e.g., a missing value is NaN-filled in a pandas DataFrame) so
# we remove any such features from the observation to avoid including in counts
obs = {key: value for key, value in iteritems(observation) if not isnan(value)}
obs = {key: value for key, value in observation.items() if not isnan(value)}
# create iterators of obs for updating counts
obs1, obs2 = tee(iteritems(obs), 2)
obs1, obs2 = tee(obs.items(), 2)
self._update_counts(obs1)
self._update_joint_counts(obs2)

Expand Down
15 changes: 6 additions & 9 deletions coupled_biased_random_walks/detect.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
from __future__ import division

from collections import defaultdict

import numpy as np
from six import iteritems, itervalues

from coupled_biased_random_walks.count import (
ObservationCounter,
Expand Down Expand Up @@ -71,7 +68,7 @@ def fit(self):
stationary_prob = {}
feature_relevance = defaultdict(int)

for feature, idx in iteritems(self._counter.index):
for feature, idx in self._counter.index.items():
prob = pi[idx]
stationary_prob[feature] = prob
feature_relevance[get_feature_name(feature)] += prob
Expand Down Expand Up @@ -99,7 +96,7 @@ def _score(self, observation):
Compute the weighted anomaly score (object_score in the paper) for an observation
:param observation: dict of the form {feature_name: feature_value, ...}
"""
return sum(itervalues(self._value_scores(observation)))
return sum(self._value_scores(observation).values())

def value_scores(self, observation_iterable):
"""
Expand All @@ -125,7 +122,7 @@ def _value_scores(self, observation):
get_feature_name(item):
self._get_feature_relevance(item) *
self._stationary_prob.get(item, self._unknown_feature_score)
for item in iteritems(observation)
for item in observation.items()
}

def _get_feature_relevance(self, feature_tuple):
Expand All @@ -144,7 +141,7 @@ def _compute_biased_transition_matrix(self):

bias_dict = self._compute_biases()

for (feature1, feature2), joint_count in iteritems(self._counter.joint_counts):
for (feature1, feature2), joint_count in self._counter.joint_counts.items():

# get index for features
feature1_idx = self._counter.index[feature1]
Expand Down Expand Up @@ -178,12 +175,12 @@ def _compute_biases(self):
Computes bias for random walk for each feature tuple
"""
bias_dict = {}
for feature_name, value_counts in iteritems(self._counter.counts):
for feature_name, value_counts in self._counter.counts.items():
mode = get_mode(value_counts)
base = 1 - (mode / self._counter.n_obs[feature_name])
bias = {
feature_val: (1 - (count / mode) + base) / 2
for feature_val, count in iteritems(value_counts)
for feature_val, count in value_counts.items()
}
bias_dict.update(bias)
return bias_dict
Expand Down
6 changes: 1 addition & 5 deletions coupled_biased_random_walks/matrix.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
from __future__ import division

import numpy as np
from scipy.sparse import csr_matrix
from six.moves import range


def random_walk(transition_matrix, alpha, err_tol, max_iter):
Expand Down Expand Up @@ -41,8 +38,7 @@ def dict_to_csr_matrix(data_dict, shape):

if isinstance(shape, int):
shape = (shape, shape)
# csr_matrix cannot accept iterators so cast to lists for python 3
data = list(data_dict.values())
data = list(data_dict.values()) # csr_matrix cannot accept iterator for data
idx = zip(*list(data_dict.keys()))
return csr_matrix((data, idx), shape=shape)

Expand Down

0 comments on commit ff0fdfa

Please sign in to comment.