Example #1
0
def test(image_list, model_images):
    """Performs testing by performing K-nearest neighbors classification
       Stores the predicted orientation in the object itself
    :param image_list: List of test images
    :param model_images: List of training images
    """
    def calculate_distance(image1, image2):
        """Calculates the Euclidean distance between 2 images
        :param image1: First image object
        :param image2: Second image object
        :return: Euclidean distance between the 2 images
        """
        dist = 0
        for f_image1, f_image2 in zip(image1.features, image2.features):
            dist += (f_image1 - f_image2)**2
        return 0.0 + dist

    # test() starts from here
    for test_image in image_list:
        # Traversing all the model images and calculating the distance
        least_dist = SortedList(K)
        for model_image in model_images:
            curr_dist = calculate_distance(test_image, model_image)
            least_dist.insert(curr_dist, model_image)

        # Get a voting from all the K nearest neighbors
        model_dict = defaultdict(lambda: 0)
        for index in range(K):
            curr_img = least_dist.get(index)
            model_dict[curr_img.orientation] += 1

        # print 'Printing model_dict:', model_dict
        max_orientation = max(model_dict, key=model_dict.get)
        test_image.pred_orientation = max_orientation
Example #2
0
 def test_binary_insertion_worst_case_scenario(self):
     l = SortedList()
     l.insert_item_fast(1)
     l.insert_item_fast(2)
     l.insert_item_fast(3)
     l.insert_item_fast(4)
     l.insert_item_fast(5)
     self.assertEqual(l.print_sorted_list(), [1, 2, 3, 4, 5])
Example #3
0
 def test_linear_insertion_best_case_scenario(self):
     l = SortedList()
     l.insert_item_slow(5)
     l.insert_item_slow(4)
     l.insert_item_slow(3)
     l.insert_item_slow(2)
     l.insert_item_slow(1)
     self.assertEqual(l.print_sorted_list(), [1, 2, 3, 4, 5])
Example #4
0
 def test_get_item_for_nonexistent_index(self):
     l = SortedList()
     l.insert_item_fast(1)
     l.insert_item_fast(2)
     l.insert_item_fast(3)
     with self.assertRaises(IndexError):
         l.get_item(3)
Example #5
0
def perform_top_n_filtering(similarity_df, n):
    """Finds the n most similar user/item, and returns it in the form of a list of tuples
    :param similarity_df: Pandas dataframe representing pairwise similarities between m users/items
    :param n: The number of neighbors
    :return: A dictionary that maps a user/item to at most n nearest users/items
    """
    neighbor_dict = dict()
    for row_idx, row in similarity_df.iterrows():
        nearest = SortedList(n)
        for col_idx, cell in row.iteritems():
            if not (math.isnan(cell) or row_idx == col_idx):
                nearest.insert(cell, col_idx)
        neighbor_dict[row_idx] = nearest.get_all()
    # print neighbor_list
    return neighbor_dict
Example #6
0
def perform_threshold_filtering(similarity_df, threshold):
    """Finds the user/item whose similarity >= threshold, and returns it in the form of a list of tuples
    :param similarity_df: Pandas dataframe representing pairwise similarities between m users/items
    :param threshold: The similarity threshold
    :return: A list of tuples that maps a user/item to at most n nearest users/items
    """
    neighbor_dict = dict()
    cols = len(similarity_df.columns)
    for row_idx, row in similarity_df.iterrows():
        nearest = SortedList(cols)
        for col_idx, cell in row.iteritems():
            if not (math.isnan(cell) or row_idx == col_idx
                    or cell < threshold):
                nearest.insert(cell, col_idx)
        neighbor_dict[row_idx] = nearest.get_all()
    # print neighbor_list
    return neighbor_dict
Example #7
0
class Stream():

    def __init__(self, length, save=True):
        self.length = length
        self.N = 0
        self.n = 0
        self.save = save
        self.elements = SortedList()


    def __iter__(self):
        return self


    def __next__(self):
        self.N += 1
        if self.N > self.length:
            raise StopIteration
        element = self.next_element()
        if self.save:  # To speed-up tests in which it is not necessary to check accuracy
            self.elements.process_element(element)
            self.n = self.elements.size()
        return element


    @abstractmethod
    def next_element(self):
        pass


    def top_k_query(self, k):
        return [(str(id), count/self.N) for id, count in itertools.islice(iter(self.elements), k)]


    def frequent_query(self, freq):
        return [(str(id), count/self.N) for id, count in itertools.takewhile(lambda element: element[1] >= math.ceil(freq * self.N), iter(self.elements))]
Example #8
0
# 09_02-A Realistic Example SortedList

from sorted_list import SortedList

sl = SortedList([4, 3, 78, 11])
sl
len(sl)
sl.add(-42)
sl
sl.add(7)
sl

from sorted_list import IntList

il = IntList([1, 2, 3, 4])
il
il.add(19)
il
# TypeError: IntList only supports integer values.
il.add('5')
Example #9
0
 def test_isEmpty_on_non_empty_list(self):
     l = SortedList()
     l.insert_item_fast('77')
     self.assertFalse(l.isEmpty())
Example #10
0
 def test_get_item(self):
     l = SortedList()
     for i in range(10):
         l.insert_item_slow(i)
     self.assertEqual(l.get_item(5), 5)
Example #11
0
 def test_remove_item(self):
     l = SortedList()
     for i in range(10):
         l.insert_item_slow(i)
     l.remove_item(5)
     self.assertEqual(l.print_sorted_list(), [0, 1, 2, 3, 4, 6, 7, 8, 9])
Example #12
0
 def test_isEmpty_on_empty_list(self):
     l = SortedList()
     self.assertTrue(l.isEmpty())
# 09_02-A Realistic Example SortedList

from sorted_list import SortedList
sl = SortedList([4, 3, 78, 11])
sl
len(sl)
sl.add(-42)
sl
sl.add(7)
sl

from sorted_list import IntList
il = IntList([1,2,3,4])
il
il.add(19)
il
# TypeError: IntList only supports integer values.
il.add('5')
s = Sub()
# Base initializer if __init__ is not available for Sub else
# Sub initializer

# The following output is when we call super class's __init__ with super().__init__()
# Base initializer
# Sub initializer

s.f()
# Sub.f()
"""
Other languages automatically call base class initializers. But Python treats __init__() like any other method. Base class __init__() is not called if overridden. To call base class's __init__() along with subclass's __init__() use super()
"""
from sorted_list import SortedList
sl = SortedList([4, 3, 2, 1])
print(sl)  # SortedList([1, 2, 3, 4])
print(len(sl))  # 4
sl.add(-42)
print(sl)  # SortedList([-42, 1, 2, 3, 4])
"""
isinstance() --> determinses if an object is of a specified type
            --> used for run time type checking
"""
# Examples
isinstance(3, int)
# => True
x = []
isinstance(x, (float, dict, list))
# => True
"""
Example #15
0
 def __init__(self, length, save=True):
     self.length = length
     self.N = 0
     self.n = 0
     self.save = save
     self.elements = SortedList()
Example #16
0
 def test_binary_insertion_stress_case(self):
     l = SortedList()
     for i in range(1000):
         l.insert_item_fast(i)
     self.assertEqual(l.print_sorted_list(), range(1000))
Example #17
0
class Expression(object):
    '''
	The base class of all generic expressions. This is also the class
	representing an expression.
	
	There are two ways to construct an expression. You could rewrite the
	expression in prefix form, and use the initializer::
	
		# 2 * (x + y/w + z/w)
		Expression('*', Constant(2),
		                Expression('+', Symbol('x'),
		                                Expression('/', Symbol('y'), Symbol('w')),
		                                Expression('/', Symbol('z'), Symbol('w'))
		                )
		)
	
	However, this class also have overloaded several operators, that you could
	write it naturally::
	
		Constant(2) * (Symbol('x') + Symbol('y')/Symbol('w') + Symbol('x')/Symbol('w'))
	
	.. method:: __neg__(self)
		__invert__(self)
		__add__(self, other)
		__radd__(self, other)
		__sub__(self, other)
		__rsub__(self, other)
		__mul__(self, other)
		__rmul__(self, other)
		__floordiv__(self, other)
		__truediv__(self, other)
		__rfloordiv__(self, other)
		__rtruediv__(self, other)
		__mod__(self, other)
		__rmod__(self, other)
		__pow__(self, other)
		__rpow__(self, other)
		__lshift__(self, other)
		__rlshift__(self, other)
		__rshift__(self, other)
		__rrshift__(self, other)
		__and__(self, other)
		__rand__(self, other)
		__or__(self, other)
		__ror__(self, other)
		__xor__(self, other)
		__rxor__(self, other)
	
		These operators are overloaded for expression construction.

	.. staticmethod:: and_(x, y)
		or_(x, y)
		not_(x)
		rol(x, y)
		ror(x, y)
		fn(fptr, *args)
		if_(cond, true, false)
		eq(x, y)
		ne(x, y)
		gt(x, y)
		lt(x, y)
		ge(x, y)
		le(x, y)
		
		Convenient static methods to construct an expression.

	.. attribute:: children
	
		Children of this expression. It is a :class:`collections.Counter` if the
		type is a commutative semigroup operator, and a list if not.
	
	.. attribute:: type
	
		The operator type (e.g. ``'+'``, ``'*'``, etc.) of the expression.
	
	'''
    @staticmethod
    def isType(typ):
        '''
		Returns a functor that checks if the input has the specified type.
		Usage:
		
		>>> isPlus = Expression.isType('+')
		>>> isPlus(Expression('+', Constant(1), Constant(2)))
		True
		>>> isPlus(Expression('*', Constant(1), Constant(2)))
		False
		
		'''
        def checker(x):
            return x.type == typ

        return checker

    @staticmethod
    def isConstant(x):
        """Checks whether the input is a :class:`Constant`."""
        return x.type == '<const>'

    @staticmethod
    def isAtomic(x):
        """Checks whether the expression is atomic (i.e. cannot be further simplified)."""
        return x.type == '<const>' or x.type == '<symbol>'

    __associativeOpers = set(['+', '*', '&', '|', '^', '&&', '||'])

    def __init__(self, typ, *args):
        self.type = typ
        concretizer = Counter if typ in self.__associativeOpers else list
        self.children = concretizer(list(map(deepcopy, args)))
        self._isConstant = False

        assert all(isinstance(k, type(self)) for k in self.children)

    def addChild(self, x):
        """Append the expression *x* as the children of this expression."""
        self.children.append(x)

    def replaceChildren(self, newChildren):
        """
		Returns a copy of itself, but replace the children with *newChildren*.
		Usage::
		
			>>> j = Expression('/', Constant(4), Constant(6))
			>>> j.replaceChildren([Constant(8), Constant(2)])
			Expression('/', Constant(8), Constant(2))
			>>> k = Expression('+', Constant(23), Constant(11))
			>>> k.replaceChildren(Counter([Constant(6), Constant(22), Symbol('x')]))
			Expression('+', Symbol('x'), Constant(22), Constant(6))
		
		"""
        cp = Expression(self.type)
        cp.children = newChildren
        return cp

    def __eq__(self, other):
        """Compare equality of two expressions."""
        if not isinstance(other, type(self)):
            return False
        else:
            return self.type == other.type and self.children == other.children

    def __hash__(self):
        # Warning: only use when you guarantee the expression is immutable.
        iterToHash = list(self.children.items()) if isinstance(
            self.children, Counter) else self.children
        return hash((self.type, tuple(iterToHash)))

    def __str__(self):
        if isinstance(self.children, Counter):
            return '(' + self.type.join(
                ('[{}]{}'.format(count, v) if count != 1 else str(v))
                for v, count in list(self.children.items())) + ')'
        elif len(self.children) == 2:
            return '(' + self.type.join(map(str, self.children)) + ')'
        else:
            return '{}({})'.format(self.type,
                                   ', '.join(map(str, self.children)))

    def __repr__(self):
        elems = self.children.elements() if isinstance(
            self.children, Counter) else self.children
        return 'Expression({!r}, {})'.format(self.type,
                                             ', '.join(map(repr, elems)))

    __simplicationRules = SortedList()

    @classmethod
    def addSimplificationRule(cls, rule, name):
        """Adds a new simplification rule.
		
		The simplification rule takes exactly 1 argument: the expression. It
		should return the simplified expression, or ``None`` if no simplication
		was done. If ``None`` is returned, the expression should not be modified.
		
		Usage::
		
			def powEval(expr):
			    if expr.type == '**':
			        if all(Expression.isConstant, expr.children):
			            return Constant(expr.children[0].value ** expr.children[1].value)
			
			Expression.addSimplificationRule(powEval, 'evaluate x ** y')
		
		"""
        cls.__simplicationRules.append((rule, name))

    __debugSimplify = False

    @classmethod
    def setDebugSimplify(cls, shouldDebug):
        """Set whether to debug simplification."""
        cls.__debugSimplify = shouldDebug


#	@classmethod
#	def simplificationRules(cls):
#		"""Get an iterator of simplification rules and their use count"""
#		useCounts = cls.__simplicationRules.useCount
#		rules = cls.__simplicationRules.items
#		return ((r[1], -u) for u, r in zip(useCounts, rules))

    def simplify(self, getSimplifyState=False):
        """Simplify the expression.
		
		.. note::
		
			By default there were no simplification routines available. Import
			modules from the subpackage :mod:`symbolic.simplify` to define them.
		
		If the *getSimplifyState* parameter is set to ``True``, the return value
		will be an (Expression, bool) tuple. The last element indicates whether
		simplification has taken place.
		
		"""

        retval = self
        hasSimplified = False

        # For simulating goto.
        def _retval():
            if getSimplifyState:
                return (retval, hasSimplified)
            else:
                return retval

        while True:
            for i, (f, name) in enumerate(self.__simplicationRules):
                r = f(retval)
                if r is not None:
                    self.__simplicationRules.use((f, name), hint=i)

                    hasSimplified = True
                    if self.__debugSimplify:
                        print((name, ":", r, "<-", retval))
                    retval = r
                    # we stop when we have simplified to something no longer an
                    # expression (i.e. an atomic value.)
                    if not isinstance(retval, Expression):
                        return _retval()
                    break
            else:
                return _retval()

    def __neg__(self):
        return Expression('*', Constant(-1), self)

    def __invert__(self):
        return Expression('~', self)

    def __add__(self, other):
        return Expression('+', self, other)

    def __radd__(self, other):
        return Expression('+', other, self)

    def __sub__(self, other):
        return self + (-other)

    def __rsub__(self, other):
        return (-self) + other

    def __mul__(self, other):
        return Expression('*', self, other)

    def __rmul__(self, other):
        return Expression('*', other, self)

    def __floordiv__(self, other):
        return Expression('//', self, other)

    def __truediv__(self, other):
        return Expression('/', self, other)

    def __rfloordiv__(self, other):
        return Expression('//', other, self)

    def __rtruediv__(self, other):
        return Expression('/', other, self)

    def __mod__(self, other):
        return Expression('%', self, other)

    def __rmod__(self, other):
        return Expression('%', other, self)

    def __pow__(self, other):
        return Expression('**', self, other)

    def __rpow__(self, other):
        return Expression('**', other, self)

    def __lshift__(self, other):
        return Expression('*', self, Expression('**', Constant(2), other))

    def __rlshift__(self, other):
        return Expression('*', other, Expression('**', Constant(2), self))

    def __rshift__(self, other):
        return Expression('>>', self, other)

    def __rrshift__(self, other):
        return Expression('>>', other, self)

    def __and__(self, other):
        return Expression('&', self, other)

    def __rand__(self, other):
        return Expression('&', other, self)

    def __or__(self, other):
        return Expression('|', self, other)

    def __ror__(self, other):
        return Expression('|', other, self)

    def __xor__(self, other):
        return Expression('^', self, other)

    def __rxor__(self, other):
        return Expression('^', other, self)

    @staticmethod
    def and_(x, y):
        return Expression('&&', x, y)

    @staticmethod
    def or_(x, y):
        return Expression('||', x, y)

    @staticmethod
    def not_(x):
        return Expression('!', x)

    @staticmethod
    def rol(x, y):
        return Expression('rol', x, y)

    @staticmethod
    def ror(x, y):
        return Expression('ror', x, y)

    @staticmethod
    def fn(fptr, *args):
        return Expression('fn', fptr, *args)

    @staticmethod
    def if_(cond, true, false):
        return Expression('?:', cond, true, false)

    @staticmethod
    def eq(x, y):
        return Expression('==', x, y)

    @staticmethod
    def ne(x, y):
        return Expression('!=', x, y)

    @staticmethod
    def gt(x, y):
        return Expression('<', y, x)

    @staticmethod
    def lt(x, y):
        return Expression('<', x, y)

    @staticmethod
    def ge(x, y):
        return Expression('<=', y, x)

    @staticmethod
    def le(x, y):
        return Expression('<=', x, y)