예제 #1
0
def test_Quartic():
    w = testmod.Quartic()
    assert (w(0, 0) == approx(1))
    assert (w(50, 0) == approx((15 / 16)**2))
    assert (w(0, 50) == approx((15 / 16)**2))
    assert (w(0, 200) == 0)
    assert (w(100, 100) == approx(0.25))
    assert (w(150, 150) == 0)
예제 #2
0
 def predict(self, time):
     grid_pred = retro.RetroHotSpotGrid(grid=self.masked_grid)
     grid_pred.data = self.points
     grid_pred.weight = retro.Quartic(bandwidth=1000)
     grid_risk = grid_pred.predict(start_time=time -
                                   self.time_window_length,
                                   end_time=time)
     grid_risk.mask_with(self.masked_grid)
     return grid_risk
예제 #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)
예제 #4
0
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)
예제 #5
0
def test_Quartic_can_pass_array():
    w = testmod.Quartic()
    xcoords = np.array([0, 50, 0, 0, 100, 150])
    ycoords = np.array([0, 0, 50, 200, 100, 150])
    expected = np.array([1, (15 / 16)**2, (15 / 16)**2, 0, 0.25, 0])
    np.testing.assert_allclose(w(xcoords, ycoords), expected)