# -*- coding: utf-8 -*-

import time
import cPickle
import logging
import numpy as np
import scipy.sparse as sparse

log = logging.getLogger(__name__)

__all__ = ["load_checkins", "tomatrix", "load_locations"]

def load_checkins(infile, index=None):
    """Load checkins data from file.
       infile: an open file object for read, should support `read` method.
       index: specify the (uid, iid) or (uid, iid, freq) index,
                ie. index=(0,1) or index=(0,1,-1)
       usage:
        >>> import StringIO
        >>> s = StringIO.StringIO("0 1 2\\n0 2 3\\n2 2 4\\n")
        >>> print load_checkins(s, index=[0,1,2])
        {0: [(1, 2), (2, 3)], 2: [(2, 4)]}
    """
    if index is None:
        index = [0, 1]
    t0 = time.time()

    count = 0
    users = set()
    items = set()
    pairs  = set()
    
    checkins = {}
    for line in infile:
        params = line.strip().split()
        user = int(params[index[0]])
        item = int(params[index[1]])
        if len(index) >= 3:
            freq = int(params[index[2]])
        else:
            freq = 1

        if (user, item) in pairs:
            continue
        pairs.add((user, item))

        count += 1
        users.add(user)
        items.add(item)
        if user not in checkins:
            checkins[user] = []
        checkins[user].append((item, freq))

    t1 = time.time()
    log.debug("load %i checkins, %i users, %i pois." % (count, len(users), len(items)))
    log.debug('time %.4f seconds' % (t1 - t0))
    return checkins 


def tomatrix(checkins):
    """Make checkins to a `sparse matrix` object.
       checkins: {uid: [(iid, freq), ...], ...} or {uid: [uid, ...], ...},
                see `load_checkins` for detail.
       usage:
        >>> import StringIO
        >>> s = StringIO.StringIO("0 1 2\\n0 2 3\\n2 2 4\\n")
        >>> cks = load_checkins(s, index=[0,1,2])
        >>> m = tomatrix(cks)
        >>> print ("%s" % m).replace("\\t", " ")
          (0, 1) 2
          (0, 2) 3
          (2, 2) 4
    """
    row = []
    col = []
    data = []

    users = set()
    items = set()
    
    for user in checkins:
        feedbacks = checkins[user] 
        for feed in feedbacks:
            if type(feed) == int:
                item = feed
                freq = 1
            else:
                item = feed[0]
                freq = feed[1]
            row.append(user)
            col.append(item)
            data.append(freq)

            items.add(item)
        
        users.add(user)

    matrix = sparse.csr_matrix((data, (row, col)), shape=(max(users) + 1, max(items) + 1))
    return matrix


def load_locations(infile, index=None):
    """Load locations from input file.
        infile: file pointer, an file kind object.
        index: (loc, lantitude, longititude) index in line,
            default index=(0,1,2)
        usage:
        >>> import StringIO
        >>> s = StringIO.StringIO("0 1.0 2.0\\n1 1.0 3.0\\n")
        >>> print load_locations(s)
        {0: (1.0, 2.0), 1: (1.0, 3.0)}
    """
    if index is None:
        index = (0, 1, 2)
    locations = {}
    for line in infile:
        params = line.strip().split()
        item = int(params[index[0]])
        lat = float(params[index[1]])
        lon = float(params[index[2]])
        locations[item] = (lat, lon)
    return locations
 

@@ -666,16 +666,142 @@
 2, 4)%5D%7D%0A
+        %3E%3E%3E s = StringIO.StringIO(%220 1 2%5C%5Cn0 1 3%5C%5Cn%22)%0A        %3E%3E%3E print load_checkins(s, index=%5B0,1,2%5D)%0A        %7B0: %5B(1, 5)%5D%7D%0A
     %22%22%22%0A
@@ -925,39 +925,18 @@
     
-pairs  = set()%0A    %0A    checkin
+%0A    count
 s = 
@@ -1191,54 +1191,42 @@
     
-if (user, item) in pairs:%0A            continue
+count += 1%0A        users.add(user)
 %0A   
@@ -1234,165 +1234,271 @@
     
-pair
+item
 s.add(
-(user, 
 item)
-)%0A%0A
+%0A 
        
- count += 1%0A        users.add(user)%0A        items.add(item)%0A        if user not in checkins:%0A            checkins%5Buser%5D = %5B%5D%0A
+if user not in counts:%0A            counts%5Buser%5D = %7B%7D %0A        counts%5Buser%5D%5Bitem%5D = counts%5Buser%5D.get(item, 0) + freq%0A%0A    checkins = %7B%7D%0A    for user in counts:%0A        checkins%5Buser%5D = %5B%5D%0A        for item, freq in counts%5Buser%5D.items():%0A    
     
@@ -1537,17 +1537,16 @@
  freq))%0A
-%0A
     t1 =

add with sum the frequence feature.