Exemplo 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
Exemplo n.º 2
0
def test_init_batch(small_batch, fixed_array):
    "Check computation of batch"
    nn = NearestNeighbors()
    nn.fit(*fixed_array)
    assert(nn.batch_len == 5)  # (15 / 3)
    assert(nn.nb_batch == 5)  # 22 / 5 + 1
    assert(nn.batch.shape == (5+1, 3))
Exemplo n.º 3
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)
Exemplo n.º 4
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)
Exemplo n.º 5
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)
Exemplo n.º 6
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)