def a_valid_RetroHotSpot():
    r = testmod.RetroHotSpot()
    r.weight = TestWeight()
    r.data = open_cp.TimedPoints.from_coords([
        datetime.datetime(2017, 3, 1),
        datetime.datetime(2017, 3, 2),
        datetime.datetime(2017, 3, 3)
    ], [50, 100, 125], [50, 100, 25])
    return r
def test_RetroHotSpot_single_event():
    r = testmod.RetroHotSpot()
    r.weight = TestWeight()
    r.data = open_cp.TimedPoints.from_coords([datetime.datetime(2017, 3, 1)],
                                             [50], [60])
    prediction = r.predict()
    assert (prediction.risk(50, 60) == 1)
    assert (prediction.risk(0, 10) == 1)
    assert (prediction.risk(0, 9) == 0)
Beispiel #3
0
def runRhsModel(training_data, grid, bandwidth=250, rand_seed=None):
    # Set RNG seed if given
    if rand_seed != None:
        np.random.seed(rand_seed)

    grid_cells = getRegionCells(grid)

    # Obtain model and prediction on grid cells
    rhs_pred = retro.RetroHotSpot()
    rhs_pred.data = training_data
    rhs_pred.weight = retro.Quartic(bandwidth=bandwidth)
    rhs_risk = rhs_pred.predict()
    rhs_grid_risk = open_cp.predictors.grid_prediction(rhs_risk, grid)
    rhs_grid_risk_matrix = rhs_grid_risk.intensity_matrix

    # Sort cellcoords by risk in intensity matrix, highest risk first
    return sortCellsByRiskMatrix(grid_cells, rhs_grid_risk_matrix)
def averageRhsModels(training_data, grid, bandwidth=250, num_runs=1, \
                     rand_seed_list = None):

    #

    if rand_seed_list == None:
        rand_seed_list = range(num_runs)
    else:
        num_runs = len(rand_seed_list)

    grid_cells = getRegionCells(grid)

    # Obtain model and prediction on grid cells
    rhs_pred = retro.RetroHotSpot()
    rhs_pred.data = training_data
    rhs_pred.weight = retro.Quartic(bandwidth=bandwidth)

    rhs_risk = rhs_pred.predict()

    matrix_collection = []
    for rand_seed in rand_seed_list:
        np.random.seed(rand_seed)
        rhs_grid_risk = open_cp.predictors.grid_prediction(rhs_risk, grid)
        rhs_grid_risk_matrix = rhs_grid_risk.intensity_matrix
        matrix_collection.append(rhs_grid_risk_matrix)

    master_matrix = sum(matrix_collection)

    for m in matrix_collection:
        print(m[12:20, 12:20])
    print(master_matrix[12:20, 12:20])

    for m in matrix_collection:
        print(sortCellsByRiskMatrix(grid_cells, m)[:8])
    print(sortCellsByRiskMatrix(grid_cells, master_matrix)[:8])
    sys.exit(0)

    # Sort cellcoords by risk in intensity matrix, highest risk first
    return sortCellsByRiskMatrix(grid_cells, master_matrix)