def update_sensor(sensor_id):
    '''Updates sensor information

    Updates existing virtual sensor information and returns updated information.

    Args as data:
        {
            "_id": Object ID of an existing virtual sensor
            "name": A name of a virtual sensor
            "user_id": ID of a user who creates this sensor 
            "labels": An array of strings denoting labels
            "inputs": An array of UUIDs of real sensors used as inputs for a classifier
            "sensor_uuid": A sensor UUID of this virtual sensor in BD (can pass blank) 
            "description": A description of this virtual sensor 
        }

    Returns:
        {
            "url": A URL of the HTTP call
            "method": "PUT"
            "result": ok or error
            "ret":{
                "_id": A virtual sensor's object ID
                "name": A sensor name
                "sensor_uuid": A UUID of a sensor if it's registered in BD
                "user_id": An ID of a user who created this sensor
                "labels": An array of labels
                "inputs": An array of sensor UUIDs in BD used as inputs for a classifier
                "description": A description of a sensor
            }
        } 
    '''
    user_id = 'default'
    dic = {
        'uri':request.url,
        'method':request.method
    }

    sensor = MLSensor(request.get_json())
    result = database_manager.update_sensor(sensor)

    if result is not None:
        dic['result'] = 'ok'
        dic['ret'] = database_manager.sensor(sensor._id, user_id).to_dictionary()
    else:
        dic['result'] = 'error'

    return jsonString(dic)
예제 #2
0
def predict(sensor_id, user_id, end_time=None):
    '''Makes a prediction with a virtual sensor

    Makes a prediciton using a pre-trained classifer with timeseries data in a range
    [end time - sampling period, end time]. sampling period is an average of sampling
    durations in a traing set, which is stored as a part of a classifier.
    When end_time is not specifiec, current time is used as end_time.
    This function generated a sample on timestamps passed to this function. Then,
    makes a prediction using a pre-trained classifier.
    Actual feature extraction and prediction are implemented in a classifier class.
    Check random_forest.py for current the implementation of the current classifier. 

    Args:
        sensor_id: An object ID of a virtual sensor.
        user_id: A user ID of a user who own the virtual sensor. 

    Returns:
        cls_result: An instance of a container class MLClassifierResult
    '''
    clf_result = MLClassifierResult()
    classifier = db_manager.classifier(sensor_id, user_id)

    if classifier is None:  # classifier not found in the database
        clf_result.result = 'error'
        clf_result.message = 'A classifier for the sensor not found.'
        return clf_result

    sensor = db_manager.sensor(sensor_id, user_id)

    if end_time is not None:
        end_time = float(end_time)
        timeseries = db_manager.timeseries_for_inputs(
            sensor.inputs, end_time - classifier.sampling_period, end_time)
    else:
        timeseries = db_manager.latest_timeseries_for_inputs(
            sensor.inputs, classifier.sampling_period)

    prediction = classifier.predict(timeseries)  # make prediction

    if prediction is None:  # cannot make a prediction
        clf_result.result = 'error'
        clf_result.message = 'A classification error occurred.'
        return clf_result

    clf_result.prediction = prediction
    print prediction

    return clf_result
def predict(sensor_id, user_id, end_time=None):    
    '''Makes a prediction with a virtual sensor

    Makes a prediciton using a pre-trained classifer with timeseries data in a range
    [end time - sampling period, end time]. sampling period is an average of sampling
    durations in a traing set, which is stored as a part of a classifier.
    When end_time is not specifiec, current time is used as end_time.
    This function generated a sample on timestamps passed to this function. Then,
    makes a prediction using a pre-trained classifier.
    Actual feature extraction and prediction are implemented in a classifier class.
    Check random_forest.py for current the implementation of the current classifier. 

    Args:
        sensor_id: An object ID of a virtual sensor.
        user_id: A user ID of a user who own the virtual sensor. 

    Returns:
        cls_result: An instance of a container class MLClassifierResult
    '''    
    clf_result = MLClassifierResult()
    classifier = db_manager.classifier(sensor_id, user_id)

    if classifier is None:  # classifier not found in the database
        clf_result.result = 'error'
        clf_result.message = 'A classifier for the sensor not found.'
        return clf_result

    sensor = db_manager.sensor(sensor_id, user_id)

    if end_time is not None:
        end_time = float(end_time)
        timeseries = db_manager.timeseries_for_inputs(sensor.inputs, end_time-classifier.sampling_period, end_time)
    else:
        timeseries = db_manager.latest_timeseries_for_inputs(sensor.inputs, classifier.sampling_period)

    prediction = classifier.predict(timeseries)   # make prediction

    if prediction is None:  # cannot make a prediction
        clf_result.result = 'error'
        clf_result.message = 'A classification error occurred.'
        return clf_result

    clf_result.prediction = prediction
    print prediction

    return clf_result
예제 #4
0
def update_sensor(sensor_id):
    '''Updates sensor information

    Updates existing virtual sensor information and returns updated information.

    Args as data:
        {
            "_id": Object ID of an existing virtual sensor
            "name": A name of a virtual sensor
            "user_id": ID of a user who creates this sensor 
            "labels": An array of strings denoting labels
            "inputs": An array of UUIDs of real sensors used as inputs for a classifier
            "sensor_uuid": A sensor UUID of this virtual sensor in BD (can pass blank) 
            "description": A description of this virtual sensor 
        }

    Returns:
        {
            "url": A URL of the HTTP call
            "method": "PUT"
            "result": ok or error
            "ret":{
                "_id": A virtual sensor's object ID
                "name": A sensor name
                "sensor_uuid": A UUID of a sensor if it's registered in BD
                "user_id": An ID of a user who created this sensor
                "labels": An array of labels
                "inputs": An array of sensor UUIDs in BD used as inputs for a classifier
                "description": A description of a sensor
            }
        } 
    '''
    user_id = 'default'
    dic = {'uri': request.url, 'method': request.method}

    sensor = MLSensor(request.get_json())
    result = database_manager.update_sensor(sensor)

    if result is not None:
        dic['result'] = 'ok'
        dic['ret'] = database_manager.sensor(sensor._id,
                                             user_id).to_dictionary()
    else:
        dic['result'] = 'error'

    return jsonString(dic)
def get_sensor(sensor_id):
    '''Returns informatoin about a virtual sensor

    Returns an object containing information about a virtual sensor.

    Args as a part of URL:
        <sensor_id>: A sensor's object ID.

    Returns:
        {
            "url": A URL of the HTTP call
            "method": "GET"
            "result": ok or error
            "ret":{
                "_id": A virtual sensor's object ID
                "name": A sensor name
                "sensor_uuid": A UUID of a sensor if it's registerd in BD
                "user_id": An ID of a user who created this sensor
                "labels": An array of labels
                "inputs": An array of sensor UUIDs in BD used as inputs for a classifier
                "description": A description of a sensor
            }
        }    
    '''
    user_id = 'default'
    
    sensor = database_manager.sensor(sensor_id, user_id)
    dic = {
        'url':request.url,
        'method':request.method,
    }
    if sensor is not None:
        dic['ret'] = sensor.to_dictionary()
        dic['result'] = 'ok'
    else:
        dic['result'] = 'error'

    return jsonString(dic)
예제 #6
0
def get_sensor(sensor_id):
    '''Returns informatoin about a virtual sensor

    Returns an object containing information about a virtual sensor.

    Args as a part of URL:
        <sensor_id>: A sensor's object ID.

    Returns:
        {
            "url": A URL of the HTTP call
            "method": "GET"
            "result": ok or error
            "ret":{
                "_id": A virtual sensor's object ID
                "name": A sensor name
                "sensor_uuid": A UUID of a sensor if it's registerd in BD
                "user_id": An ID of a user who created this sensor
                "labels": An array of labels
                "inputs": An array of sensor UUIDs in BD used as inputs for a classifier
                "description": A description of a sensor
            }
        }    
    '''
    user_id = 'default'

    sensor = database_manager.sensor(sensor_id, user_id)
    dic = {
        'url': request.url,
        'method': request.method,
    }
    if sensor is not None:
        dic['ret'] = sensor.to_dictionary()
        dic['result'] = 'ok'
    else:
        dic['result'] = 'error'

    return jsonString(dic)