Exemple #1
0
def kmeans(context):
    rawData = getDataFromSvr(context['rsrc'])
    if(context['distinct']):
        rawData = dm.distinct(rawData)
    idList, data = processData(rawData, context['start'], \
        context['end'], context['cols'])
    dataList = convertDataToArr(data)
    dataList = preprocess(dataList, context)

    from sklearn.cluster import KMeans
    clf = KMeans(**context['args'])
    clf.fit(dataList)

    conn = config.getConn()
    cursor = conn.cursor()
    hid = dbAddHistory(cursor, context, 'cluster')
    result = []
    for i in xrange(len(clf.labels_)):
        result.append((hid, idList[i], clf.labels_[i], 
            json.stringify(data[i])))

    dbWriteBack(cursor, result)
    dbAddMessage(cursor, context, hid)
    try: sendMessageByEmail(cursor, context, hid)
    except Exception: pass

    conn.commit()
    cursor.close()
    conn.close()
    return json.stringify({'succ': True, 'msg': 'Done...'})
Exemple #2
0
def kmedoids(context):
    rawData = getDataFromSvr(context['rsrc'])
    if(context['distinct']):
        rawData = dm.distinct(rawData)
    idList, data = processData(rawData, context['start'], \
        context['end'], context['cols'])
    dataList = convertDataToArr(data)
    dataList = preprocess(dataList, context)

    _, _, rawRes = dm.kmedoids(dataList, **context['args'])

    conn = config.getConn()
    cursor = conn.cursor()
    hid = dbAddHistory(cursor, context, 'cluster')
    result = []
    clusterId = 0
    for medoid in rawRes.keys():
        for i in rawRes[medoid]:
            result.append((hid, idList[i], clusterId,
                json.stringify(dataList[i])))
        clusterId += 1

    dbWriteBack(cursor, result)
    dbAddMessage(cursor, context, hid)
    try: sendMessageByEmail(cursor, context, hid)
    except Exception: pass

    conn.commit()
    cursor.close()
    conn.close()
    return json.stringify({'succ': True, 'msg': 'Done...'})
Exemple #3
0
def apriori(context):
    rawData = getDataFromSvr(context['rsrc'])
    _, data = processData(rawData, context['start'], \
        context['end'], context['cols'])
    dataList = convertDataToArr(data)

    rawRes = dm.apriori(dataList, **context['args'])

    conn = config.getConn()
    cursor = conn.cursor()
    hid = dbAddHistory(cursor, context, 'assoc')
    result = []
    count = 0
    for row in rawRes:
        v = "{0} -> {1}".format(
            ', '.join(row[0]),
            ', '.join(row[1])
        )
        result.append((hid, count, row[2], v, row[3], row[4]))
        count += 1
    dbWriteBackAssoc(cursor, result)
    dbAddMessage(cursor, context, hid)
    try: sendMessageByEmail(cursor, context, hid)
    except Exception: pass

    conn.commit()
    cursor.close()
    conn.close()
    return json.stringify({'succ': True, 'msg': 'Done...'})
Exemple #4
0
def getEmail(uid):
    conn = config.getConn()
    cur = conn.cursor()
    email = dbGetEmail(cur, uid)
    conn.commit()
    cur.close()
    conn.close()
    return make_response(json.stringify({"succ": True, "data": email}))
Exemple #5
0
def getResultById(id):
    conn = config.getConn()
    cur = conn.cursor()
    result = dbGetResult(cur, id)
    cur.close()
    conn.close()
    res = make_response(json.stringify({"succ": True, "data": result}))
    return res
Exemple #6
0
def notify(uid):
    conn = config.getConn()
    cur = conn.cursor()
    sql = "select count(*) from message where userid=%s and isread=0"
    cur.execute(sql, (uid,))
    num = cur.fetchall()[0][0]
    cur.close()
    conn.close()
    return make_response(json.stringify({"succ": True, "unread": num}))
Exemple #7
0
def getHistoryById(uid, id):
    conn = config.getConn()
    cur = conn.cursor()
    result = dbGetHistory(cur, uid, id)
    cur.close()
    conn.close()
    res = make_response(json.stringify({"succ": True, "data": result}))
    res.headers["Content-Type"] = "application/json"
    return res
Exemple #8
0
def markMessage(uid, id):
    conn = config.getConn()
    cur = conn.cursor()
    sql = "update message set isread=1 where userid=%s"
    if id != 0:
        sql += " and id=" + str(id)
    cur.execute(sql, (uid,))
    conn.commit()
    cur.close()
    conn.close()
    return make_response(json.stringify({"succ": True}))
Exemple #9
0
def classify(context):

    from sklearn.neighbors import KNeighborsClassifier
    from sklearn.naive_bayes import MultinomialNB
    from sklearn.svm import SVC

    label = context['label']
    algo = context['algo']
    if algo == 'svm':
        classifier = SVC
    elif algo == 'knn':
        classifier = KNeighborsClassifier
    elif algo == 'naive_bayes':
        classifier = MultinomialNB
    else:
        assert False
    args = context['args']
    for k in args:
        if isinstance(args[k], unicode):
            args[k] = str(args[k])

    rawData = getDataFromSvr(context['rsrc'])
    if(context['distinct']):
        rawData = dm.distinct(rawData)
    labelList, train = getTrainingSet(rawData, label, context['start'], \
        context['end'], context['cols'])
    trainList = convertDataToArr(train)
    trainList = preprocess(trainList, context)
    idList, predict = getPredictSet(rawData, context['predictStart'], \
        context['predictEnd'], context['cols'])
    predictList = convertDataToArr(predict)
    predictList = preprocess(predictList, context)

    clf = classifier(**args)
    clf.fit(trainList, labelList)
    rawRes = clf.predict(predictList)

    conn = config.getConn()
    cursor = conn.cursor()
    id = dbAddHistory(cursor, context, 'classify')

    result = []
    for i in xrange(len(rawRes)):
        result.append((id, idList[i], rawRes[i], json.stringify(predict[i])))

    dbWriteBack(cursor, result)
    dbAddMessage(cursor, context, id)
    try: sendMessageByEmail(cursor, context, hid)
    except Exception: pass

    conn.commit()
    cursor.close()
    conn.close()
    return json.stringify({'succ': True, 'msg': 'Done...'})
Exemple #10
0
def setEmail(uid):
    email = request.form.get('email')
    if email is None: email = ""
    if not re.match(r'^[\w\-\.]+?@(\w+?\.)+?\w{2,4}$', email):
        return make_response(json.stringify({"succ": False, "msg": "Email format error!"}))
    conn = config.getConn()
    cur = conn.cursor()
    sql = "replace into email (userid, email) values (%s,%s)"
    cur.execute(sql, (uid, email))
    conn.commit()
    cur.close()
    conn.close()
    return make_response(json.stringify({"succ": True}))
Exemple #11
0
def getResultCsv(id):
    conn = config.getConn()
    cur = conn.cursor()
    history = dbGetHistory(cur, 0, id)
    data = dbGetResult(cur, id)
    cur.close()
    conn.close()
    
    if len(history) == 0:
        assert False
    history = history[0]
    
    if history['type'] == 'assoc':
        result = generateCsvAssoc(data)
    else:
        result = generateCsvOther(data)
    res = make_response(result)
    res.headers["Content-Type"] = "application/csv;charset=utf-8"
    res.headers["Content-Disposition"] = "attachment; filename=" + str(id) + ".csv"
    return res
Exemple #12
0
def getMessage(uid, isread):
    conn = config.getConn()
    cur = conn.cursor()
    sql = "select id,content,tm,isread from message where userid=%s"
    if isread == 1:
        sql += " and isread=1"
    elif isread == 0:
        sql += " and isread=0"
    sql += " order by id desc"
    cur.execute(sql, (uid,))
    result = cur.fetchall()
    cur.close()
    conn.close()
    newResult = []
    for row in result:
        obj = {
            "id": row[0],
            "content": row[1],
            "tm": str(row[2]),
            "isread": 1 if row[3] else 0
        }
        newResult.append(obj)
    return make_response(json.stringify({"succ": True, "data": newResult}))