Esempio n. 1
0
def make_prediction(webcam, params, timestamp):
    """
    Make a prediction using NearestNeighbors algorithm.

    Parameters
    ----------
    webcam : Webcam instance
        The source of pictures
    params : PredictionParams instance
        The parameters to use to compute the prediction
    timestamp : int
        The Unix-Epoch-timestamp (UTC) of when the prediction is made

    Return
    ------
    Prediction instance
    """
    cam_id = webcam.webcam_id
    result = None
    with webcam_fs.get_dataset(cam_id) as dataset:
        ex_set = dataset.get_set(params.name)
        new_input = dataset.make_input(ex_set, timestamp)
        if new_input is not None and len(ex_set.input) > 0:
            neighbors = NearestNeighbors()
            neighbors.fit(ex_set.input)
            output_ref = neighbors.predict(new_input)
            output = dataset.output_img(ex_set, output_ref)
            # FIXME reference existing image (data and file)
            imgpath = webcam_fs.prediction_path(cam_id, params.name, timestamp)
            result = Prediction(params=params, path=imgpath)
            result.comp_date = datetime.fromtimestamp(timestamp, utc)
            result.sci_bytes = output
            result.create()
    return result
Esempio n. 2
0
def test_predict_wminkowski(fixed_array):
    "Weighing the metric should be taken into account"
    nn = NearestNeighbors(metric='wminkowski', w=np.array([10, 1, 1]))
    nn.fit(*fixed_array)
    y4 = nn.predict(np.array([4.4, 4.4, 4.6]))
    y5 = nn.predict(np.array([4.6, 4.4, 4.4]))
    assert(y4 == 4)
    assert(y5 == 5)
Esempio n. 3
0
def test_predict_extendedarray(variable_array, fixed_array):
    "Extending the array after fitting it should not be a problem"
    X, y = variable_array
    nn = NearestNeighbors()
    nn.fit(X, y)
    newX, newy = fixed_array
    X.append(newX)
    y.append(newy)
    y = nn.predict(np.array([4.2, 4.2, 4.2]))
    assert(y == 4)
Esempio n. 4
0
def test_predict_notexisting(fixed_array):
    "Find the nearest neighbor of an unexisting row"
    nn = NearestNeighbors()
    nn.fit(*fixed_array)
    y = nn.predict(np.array([4.6, 4.4, 4.4]))
    assert(y == 4)
Esempio n. 5
0
def test_predict_multibatch(small_batch, fixed_array):
    "An existing row should be found, only one batch"
    nn = NearestNeighbors()
    nn.fit(*fixed_array)
    y = nn.predict(np.array([17, 17, 17]))
    assert(y == 17)