コード例 #1
0
def recognize_last_activities(uuid, algorithm, feature_set, start_ts, end_ts):

    start_datetime = datetime.fromtimestamp(start_ts / 1e3)
    end_datetime = datetime.fromtimestamp(end_ts / 1e3)

    records = DataRecord.objects.filter(user_id=uuid)\
        .filter(date_time__gt=start_datetime)\
        .filter(date_time__lt=end_datetime)

    if records:
        avg_prob = [0, 0, 0, 0, 0, 0, 0, 0]

        current_activity = "none"

        for j in xrange(len(records)):
            prob = get_probability_for_data_record(records[j], feature_set, algorithm)

            if j == 0:
                current_activity = activity_table_json.get(np.argmax(prob) + 1)

            for i in xrange(len(prob)):
                avg_prob[i] += prob[i]/len(records)

        return {"vector": avg_prob, "time": records[0].date_time, "current_activity": current_activity}
    else:
        raise Exception('No record found')
コード例 #2
0
def recognize_last_activities(uuid, algorithm, feature_set, delta_time):
    """
    Recognises the last activities within a time last delta_time seconds
    :param uuid: user id of the user
    :param algorithm: the requested algorithm
    :param feature_set: the requested feature set
    :param delta_time: the time in seconds
    :return: a dict consisting of a probability vector the time and the current activity :raise Exception:
    """
    datetime_point = datetime.now() - timedelta(seconds=delta_time)
    records = DataRecord.objects.filter(user_id=uuid).filter(
        record_date__gt=datetime_point)

    if records:
        avg_prob = [0, 0, 0, 0, 0, 0, 0, 0]

        current_activity = "none"

        for j in xrange(len(records)):
            prob = get_probability_for_data_record(records[j], feature_set,
                                                   algorithm)

            if j == 0:
                current_activity = activity_table_json.get(np.argmax(prob) + 1)

            for i in xrange(len(prob)):
                avg_prob[i] += prob[i] / len(records)

        return {
            "vector": avg_prob,
            "time": records[0].record_date,
            "current_activity": current_activity
        }
    else:
        raise Exception('No record found')
コード例 #3
0
    def get(self, request):

        try:
            if 'uuid' not in request.GET:
                raise Exception("Bad request")

            algorithm = request.GET['alg'] if 'alg' in request.GET else 'svm'
            feature_set = request.GET['fs'] if 'fs' in request.GET else 'standard'
            current_activity = request.GET["curr_act"] if 'curr_act' in request.GET else 'false'
            ac = request.GET['ac'] if 'ac' in request.GET else '1'

            if ac != '1':
                raise Exception("Not yet implemented")

            if 'tp' in request.GET:
                record = recognize_last_activities(request.GET['uuid'],
                                                   algorithm,
                                                   feature_set,
                                                   int(request.GET['tp']))
            else:
                record = recognize_last_activity(request.GET['uuid'],
                                                 algorithm,
                                                 feature_set)

            response_text = '{ "date_time":"%s", ' % record.get("time")
            response_text += '"uuid" : "%s",' % request.GET['uuid']

            if current_activity == "true":
                response_text += '"curr_act" :  "%s",' % record.get("current_activity")
            elif current_activity != "false":
                raise Exception("Wrong value for curr_act")

            if algorithm == 'svm':
                response_text += '"svm_vector" : {'
                reduced_vec = reduce_activity_vector(record['vector'])
                #reduced_vec = record['vector']

                for i in xrange(len(reduced_vec)):
                    if i != len(reduced_vec) - 1:
                        response_text += '"%s":"%s",' % (reduced_activity_table_json.get(i+1), reduced_vec[i])
                        #response_text += '"%s":"%s",' % (activity_table.get(i+1), reduced_vec[i])
                    else:
                        response_text += '"%s":"%s"}' % (reduced_activity_table_json.get(i+1), reduced_vec[i])
                        #response_text += '"%s":"%s"}' % (activity_table.get(i+1), reduced_vec[i])
            else:
                index = np.argmax(record['vector']) + 1
                response_text += '"dt_category" : "%s"' % activity_table_json.get(index)

            response_text += "}"

            response = HttpResponse(response_text)
            response.status_code = 200
            return response
        except Exception as e:
            response = HttpResponse('{"error":"%s"}' % e.message)
            request.status_code = 404
            return response
コード例 #4
0
ファイル: hidden_views.py プロジェクト: MrHakala/LCI-FIC2-SE
    def get(self, request):

        try:
            if 'uuid' not in request.GET:
                raise Exception("Bad request")

            algorithm = request.GET['alg'] if 'alg' in request.GET else 'svm'
            feature_set = request.GET['fs'] if 'fs' in request.GET else 'standard'
            current_activity = request.GET["curr_act"] if 'curr_act' in request.GET else 'false'
            ac = request.GET['ac'] if 'ac' in request.GET else '1'

            if ac != '1':
                raise Exception("Not yet implemented")

            if 'start_ts' in request.GET and 'end_ts':
                record = recognize_last_activities(request.GET['uuid'],
                                                   algorithm,
                                                   feature_set,
                                                   int(request.GET['start_ts']),
                                                   int(request.GET['end_ts']))
            else:
                record = recognize_last_activity(request.GET['uuid'],
                                                 algorithm,
                                                 feature_set)

            response_text = '{ "date_time":"%s", ' % record.get("time")
            response_text += '"uuid" : "%s",' % request.GET['uuid']

            if current_activity == "true":
                response_text += '"curr_act" :  "%s",' % record.get("current_activity")
            elif current_activity != "false":
                raise Exception("Wrong value for curr_act")

            if algorithm == 'svm':
                response_text += '"svm_vector" : {'
                reduced_vec = reduce_activity_vector(record['vector'])

                for i in xrange(len(reduced_vec)):
                    if i != len(reduced_vec) - 1:
                        response_text += '"%s":"%s",' % (reduced_activity_table_json.get(i+1), reduced_vec[i])
                    else:
                        response_text += '"%s":"%s"}' % (reduced_activity_table_json.get(i+1), reduced_vec[i])

            else:
                index = np.argmax(record['vector']) + 1
                response_text += '"dt_category" : "%s"' % activity_table_json.get(index)

            response_text += "}"

            response = HttpResponse(response_text)
            response.status_code = 200
            return response
        except Exception as e:
            response = HttpResponse('{"error":"%s"}' % e.message)
            request.status_code = 404
            return response
コード例 #5
0
def recognize_last_activity(uuid, algorithm, feature_set):
    """
    Recognises the last activity for the given user.
    :param uuid: user id of the user
    :param algorithm: the requested algorithm
    :param feature_set: the requested feature set
    :return: a dict consisting of a probability vector the time and the current activity
    """
    record = DataRecord.objects.filter(user_id=uuid).latest('record_date')

    prob = get_probability_for_data_record(record, feature_set, algorithm)
    current_activity = activity_table_json.get(np.argmax(prob) + 1)

    return {
        "vector": prob,
        "time": record.record_date,
        "current_activity": current_activity
    }
コード例 #6
0
def recognize_last_activity(uuid, algorithm, feature_set):

    record = DataRecord.objects.raw_query({'user_id': {'$eq': uuid}}).reverse()[:1]

    if algorithm == 'svm' and feature_set == 'standard':
        prob = record[0].activity.svm
    elif algorithm == 'svm' and feature_set == 'enhanced':
        prob = record[0].activity.svm_ech
    elif algorithm == 'dt' and feature_set == 'standard':
        prob = record[0].activity.dt
    elif algorithm == 'dt' and feature_set == 'enhanced':
        prob = record[0].activity.dt_ech
    else:
        raise Exception("Bad request")

    current_activity = activity_table_json.get(np.argmax(prob) + 1)

    return {"vector": prob, "time": record[0].date_time, "current_activity": current_activity}
コード例 #7
0
def recognize_last_activities(uuid, algorithm, feature_set, delta_time):

    datetime_point = datetime.now() - timedelta(seconds=delta_time)
    records = DataRecord.objects.filter(user_id=uuid).filter(date_time__gt=datetime_point)

    if records:
        avg_prob = [0, 0, 0, 0, 0, 0, 0, 0]

        current_activity = "none"

        for j in xrange(len(records)):
            prob = get_probability_for_data_record(records[j], feature_set, algorithm)

            if j == 0:
                current_activity = activity_table_json.get(np.argmax(prob) + 1)

            for i in xrange(len(prob)):
                avg_prob[i] += prob[i]/len(records)

        return {"vector": avg_prob, "time": records[0].date_time, "current_activity": current_activity}
    else:
        raise Exception('No record found')