Ejemplo n.º 1
0
def add(db, feature_type, sample):
    print "function: add"
    collection = db[feature_type]
    if collection.find({'_id':sample._id}).count()>0:
        return my_classifier.error_json("sample " + sample._id + " already exists.")
    
    print collection.find({}).count()
    print sample.__dict__
    try:
        collection.insert(sample.__dict__)
    except:
        return my_classifier.error_json(str(sys.exc_info()))
    return my_classifier.success_json()
Ejemplo n.º 2
0
def clear_samples(db,feature_type,data):
    print "in clear_samples"
    #query = {}
    query = data['selector']
    collection = db[feature_type]
    data_count = collection.find(query).count()
    if data_count==0:
        return my_classifier.error_json("No samples are hit.")
    try:
        collection.remove(query)
    except:
        return my_classifier.error_json(sys.exc_info()[1])
    result = my_classifier.success_json()
    
    result['event'] = {'_id': generate_event_id('clear_samples',feature_type,json.dumps(query))}
    return result
Ejemplo n.º 3
0
def disband(db,feature_type,data):
    print "function: disband"
    if not data.has_key('group'):
        return my_classifier.error_json("'group' must be set.")
    group_name = data['group']
    collections = db[feature_type]

    samples = collections.find({'group':{'$all':[group_name]}})
    if samples.count() == 0:
        return my_classifier.error_json("ERROR: no samples are hit.")
    for s in samples:
        groups = s['group']
        while group_name in groups:
            groups.remove(group_name)
        _id = s['_id']
        collections.update({"_id":_id},{"$set":{'group':groups}})
    result = my_classifier.success_json()
    result['event'] = {'_id':generate_event_id('disband',feature_type,group_name)}
    return result   
Ejemplo n.º 4
0
def get_sample_detail(db, feature_type,data):
    collection = db[feature_type]
    query = {'_id':data['id']}
    sample = collection.find_one(query)
    if not sample:
        return my_classifier.error_json("No samples are hit")
    result = my_classifier.success_json()
    result['sample'] =sample
    result['event'] = {'_id': generate_event_id('get_sample_detail',feature_type, query)}
    return result
Ejemplo n.º 5
0
def clear_classifier(db, feature_type, data, algorithm):
    print "function: " + __name__
    if algorithm==None:
        return my_classifier.error_json('algorithm must be designated')


    clf_id = my_classifier.generate_clf_id(algorithm,feature_type,data)
    query = {'_id':clf_id}
    
    collection = db['classifiers']
    data_count = collection.find(query).count()
    if data_count==0:
        return my_classifier.error_json("No classifiers are hit.")
    
    try:
        db['classifiers'].remove(query)
    except:
        return my_classifier.error_json(sys.exc_info()[1])

    result = my_classifier.success_json()
    result['event'] = {'_id': generate_event_id('clear_classifier', feature_type, clf_id )}   
    return result
Ejemplo n.º 6
0
def get_samples(db, feature_type, data):
    collection = db[feature_type]
    query = data['selector']
    samples = collection.find(query)
    if samples.count() == 0:
        return my_classifier.error_json("No samples are hit")
    result = my_classifier.success_json()
    result['samples'] = []
    for s in samples:
        result['samples'].append(s['_id'])

    result['event'] = {'_id': generate_event_id('get_samples',feature_type, query)}
    return result
Ejemplo n.º 7
0
 def wrapper(db, feature_type, *args,**kwargs):
     result_json = func(db,feature_type, *args,**kwargs)
     if my_classifier.is_success(result_json):
         access_history = db['timestamps']
         event = result_json['event']
         time = datetime.now()
         #    print time
         event['time'] = time
         try:
             #print event
             access_history.replace_one({'_id':event['_id']},event,True)
         except:
             return my_classifier.error_json(sys.exc_info()[1])
     return result_json
Ejemplo n.º 8
0
def band(db,feature_type,data):
    print "function: band"
    if not data.has_key('group'):
        return my_classifier.error_json("'group' must be set.")
    group_name = data['group']
    
    selector = data['selector']
    
    collections = db[feature_type]

    samples = collections.find(selector)
    #print samples
    if samples.count() == 0:
        return my_classifier.error_json("ERROR: no samples are hit.")
    for s in samples:
        groups = s['group']
        if not group_name in groups:
            groups = ensure_list(groups)
            groups.append(group_name)
            _id = s['_id']
            collections.update_one({"_id":_id},{"$set":{'group':groups}})
    result = my_classifier.success_json()
    result['event'] = {'_id':generate_event_id('band',feature_type,[group_name,json.dumps(selector)])}
    return result
Ejemplo n.º 9
0
def evaluate(db,feature_type, data,algorithm):
    #print "function: evaluate"
        
    # class_name2idのために識別器のデータを呼ぶ
    clf_id = my_classifier.generate_clf_id(algorithm,feature_type,data)
    #print "clf_id: " + clf_id
    try:
        record = db["classifiers"].find_one({'_id':clf_id})
        if record == None:
            return my_classifier.error_json("No classifier was found.")
    except:
        return my_classifier.error_json(sys.exc_info()[1])
    #print record

    name2id = record['class_name2id']
    y = []
    y_pred = []
    weights = []
    
    selector = data['selector']
    selector['likelihood.'+clf_id] = {"$exists":True}
    print selector
    samples = get_training_samples(db,feature_type,False,selector)
    for s in samples:
        if not s['likelihood'].has_key(clf_id):
            continue
        y.append(name2id[s['ground_truth']])
        likelihood = dict(s['likelihood'][clf_id])
        pred_name = max([(v,k) for k,v in likelihood.items()])[1]
        y_pred.append(name2id[pred_name])
        if s.has_key('weight'):
            weights.append(float(s['weight']))
        else:
            weights.append(1.0)
        
    
    if not y:
        return my_classifier.error_json("ERROR: samples are not found.")

    result = my_classifier.success_json()

    result['event'] = {'_id':generate_event_id('evaluate', feature_type, clf_id)}

    id2name = record['class_id2name']
    result['class_list'] = [id2name[k] for k in sorted(id2name.keys())]

    # confution_matrix
    cm = confusion_matrix(y, y_pred)
    cm_json_searizable = []
    for line in cm:
        cm_json_searizable.append(line.tolist())
#    id2name = record['class_id2name']

    result['confusion_matrix'] = json.dumps(cm_json_searizable)

    #print precision_score(y,y_pred,sample_weight=weights)
    #print precision_score(y,y_pred)
    #print weights
    result['precision_score'] = precision_score(y,y_pred,average=None).tolist()#,sample_weight=weights)
    result['recall_score'] = recall_score(y,y_pred,average=None).tolist()#,sample_weight=weights)
    result['f1_score'] = f1_score(y,y_pred,average=None).tolist()#,sample_weight=weights)
    return result