コード例 #1
0
def test_evaluate_gradient():
    grid = StructuredGrid()
    # test by setting the scalar field to the y coordinate
    vector = grid.evaluate_gradient(grid.barycentre, grid.nodes[:, 1])
    assert np.sum(vector - np.array([0, 1, 0])) == 0

    # same test but for a bigger grid, making sure scaling for cell is ok
    grid = StructuredGrid(step_vector=np.array([100, 100, 100]))
    vector = grid.evaluate_gradient(grid.barycentre, grid.nodes[:, 1])
    assert np.sum(vector - np.array([0, 1, 0])) == 0
コード例 #2
0
def test_FDI():
    xy = np.array(np.meshgrid(np.linspace(0, 1, 50),
                              np.linspace(0, 1, 50))).T.reshape(-1, 2)
    xyz = np.hstack([xy, np.zeros((xy.shape[0], 1))])
    data = pd.DataFrame(xyz, columns=['X', 'Y', 'Z'])
    data['val'] = np.sin(data['X'])
    data['w'] = 1
    data['feature_name'] = 'strati'
    randind = np.arange(0, len(data))
    # np.random.shuffle(randind)
    # # data.loc[randind[:int(50*50*.5)],'val'] = np.nan
    # np.random.shuffle(randind)
    # data.loc[randind[:int(50*50*.1)],'val'] = 0
    origin = np.array([-0.1, -0.1, -0.1])
    maximum = np.array([1.1, 1.1, 1.1])
    nsteps = np.array([20, 20, 20])
    step_vector = (maximum - origin) / nsteps
    grid = StructuredGrid(origin=origin,
                          nsteps=nsteps,
                          step_vector=step_vector)
    interpolator = FDI(grid)
    interpolator.set_value_constraints(data[['X', 'Y', 'Z', 'val',
                                             'w']].to_numpy())
    interpolator._setup_interpolator()
    interpolator.solve_system()
    assert np.sum(
        interpolator.evaluate_value(data[['X', 'Y', 'Z']].to_numpy()) -
        data[['val']].to_numpy()) / len(data) < 0.5
コード例 #3
0
def test_inequality_FDI_nodes():
    try:
        import osqp
    except ImportError:
        print('osqp not installed')
        return
    xy = np.array(np.meshgrid(np.linspace(0, 1, 50),
                              np.linspace(0, 1, 50))).T.reshape(-1, 2)
    xyz = np.hstack([xy, np.zeros((xy.shape[0], 1))])
    data = pd.DataFrame(xyz, columns=['X', 'Y', 'Z'])
    data['val'] = np.sin(data['X'])
    data['w'] = 1
    data['feature_name'] = 'strati'
    data['l'] = -3
    data['u'] = 10
    randind = np.arange(0, len(data))

    origin = np.array([-0.1, -0.1, -0.1])
    maximum = np.array([1.1, 1.1, 1.1])
    nsteps = np.array([20, 20, 20])
    step_vector = (maximum - origin) / nsteps
    grid = StructuredGrid(origin=origin,
                          nsteps=nsteps,
                          step_vector=step_vector)
    interpolator = FDI(grid)
    interpolator.set_value_constraints(data[['X', 'Y', 'Z', 'val',
                                             'w']].to_numpy())
    interpolator.set_inequality_constraints(data[['X', 'Y', 'Z', 'l',
                                                  'u']].to_numpy())
    interpolator._setup_interpolator()
    interpolator.solve_system(solver='osqp')
コード例 #4
0
def test_global_to_local_coordinates():
    grid = StructuredGrid()
    point = np.array([[1.2, 1.5, 1.7]])
    lx, ly, lz = grid.position_to_local_coordinates(point)
    assert (np.isclose(lx[0], .2))
    assert (np.isclose(ly[0], .5))
    assert (np.isclose(lz[0], .7))
コード例 #5
0
def test_equality_FDI_nodes():
    xy = np.array(np.meshgrid(np.linspace(0, 1, 50),
                              np.linspace(0, 1, 50))).T.reshape(-1, 2)
    xyz = np.hstack([xy, np.zeros((xy.shape[0], 1))])
    data = pd.DataFrame(xyz, columns=['X', 'Y', 'Z'])
    data['val'] = np.sin(data['X'])
    data['w'] = 1
    data['feature_name'] = 'strati'
    origin = np.array([-0.1, -0.1, -0.1])
    maximum = np.array([1.1, 1.1, 1.1])
    nsteps = np.array([20, 20, 20])
    step_vector = (maximum - origin) / nsteps
    grid = StructuredGrid(origin=origin,
                          nsteps=nsteps,
                          step_vector=step_vector)
    interpolator = FDI(grid)
    interpolator.set_value_constraints(data[['X', 'Y', 'Z', 'val',
                                             'w']].to_numpy())

    node_idx = np.arange(
        0, interpolator.nx)[interpolator.support.nodes[:, 2] > .9]
    interpolator.add_equality_constraints(node_idx,
                                          np.ones(node_idx.shape[0]),
                                          name='top')
    interpolator._setup_interpolator()
    interpolator.solve_system(solver='cg')
コード例 #6
0
def test_outside_box():
    grid = StructuredGrid()
    # test by setting the scalar field to the y coordinate
    inside = grid.inside(grid.barycentre + 5)
    assert np.all(
        ~inside == np.any((grid.barycentre + 5) > grid.maximum, axis=1))
    inside = grid.inside(grid.barycentre - 5)
    assert np.all(
        ~inside == np.any((grid.barycentre - 5) < grid.origin, axis=1))

    cix, ciy, ciz = grid.position_to_cell_index(grid.barycentre - 5)
    assert np.all(cix[inside] < grid.nsteps_cells[0])
    assert np.all(ciy[inside] < grid.nsteps_cells[1])
    assert np.all(ciz[inside] < grid.nsteps_cells[2])
    cornersx, cornersy, cornersz = grid.cell_corner_indexes(cix, ciy, ciz)
    assert np.all(cornersx[inside] < grid.nsteps[0])
    assert np.all(cornersy[inside] < grid.nsteps[1])
    assert np.all(cornersz[inside] < grid.nsteps[2])
    globalidx = grid.global_indicies(
        np.dstack([cornersx, cornersy, cornersz]).T)
    # print(globalidx[inside],grid.n_nodes,inside)
    assert np.all(globalidx[inside] < grid.n_nodes)
    inside = grid.inside(grid.barycentre - 5)
    inside, grid.position_to_cell_corners(grid.barycentre - 5)
    vector = grid.evaluate_gradient(grid.barycentre - 5, grid.nodes[:, 1])
    assert np.sum(np.mean(vector[inside, :], axis=0) -
                  np.array([0, 1, 0])) == 0
    vector = grid.evaluate_gradient(grid.nodes, grid.nodes[:, 1])
コード例 #7
0
def test_create_structured_grid_origin_nsteps():
    grid = StructuredGrid(
        origin=np.zeros(3),
        nsteps=np.array([10, 10, 10]),
        step_vector=np.array([0.1, 0.1, 0.1]),
    )
    assert np.sum(grid.step_vector - np.array([0.1, 0.1, 0.1])) == 0
    assert np.sum(grid.maximum - np.ones(3)) == 0
コード例 #8
0
def test_change_maximum_and_origin():
    grid = StructuredGrid(origin=np.zeros(3), nsteps=np.array([5, 5, 5]))
    grid.origin = np.array([-1.0, -1.0, -1.0])
    assert np.all(np.isclose(grid.origin, np.array([-1, -1, -1])))
    assert np.all(np.isclose(grid.nsteps, np.array([6, 6, 6])))
    assert np.all(np.isclose(grid.step_vector, np.ones(3)))
    grid.maximum = np.array([7.0, 7.0, 7.0])
    assert np.all(np.isclose(grid.nsteps, np.array([9, 9, 9])))
    assert np.all(np.isclose(grid.maximum, np.array([7.0, 7.0, 7.0])))
    assert np.all(np.isclose(grid.step_vector, np.ones(3)))
コード例 #9
0
def test_evaluate_gradient2():
    # this test is the same as above but we will use a random vector
    np.random.seed(0)
    for i in range(10):
        step = np.random.uniform(0, 100)
        grid = StructuredGrid(step_vector=np.array([step, step, step]))

        # define random vector
        n = np.random.random(3)
        n /= np.linalg.norm(n)
        distance = (n[0] * grid.nodes[:, 0] + n[1] * grid.nodes[:, 1] +
                    n[2] * grid.nodes[:, 2])
        vector = grid.evaluate_gradient(np.random.uniform(1, 8, size=(100, 3)),
                                        distance)
        assert np.all(np.isclose(np.sum(vector - n[None, :], axis=1),
                                 0)) == True
    assert i == 9
コード例 #10
0
def test_inequality_FDI():
    try:
        import osqp
    except ImportError:
        print('osqp not installed')
        return
    xy = np.array(np.meshgrid(np.linspace(0, 1, 50),
                              np.linspace(0, 1, 50))).T.reshape(-1, 2)
    xyz = np.hstack([xy, np.zeros((xy.shape[0], 1))])
    data = pd.DataFrame(xyz, columns=['X', 'Y', 'Z'])
    data['val'] = np.sin(data['X'])
    data['w'] = 1
    data['feature_name'] = 'strati'
    data['l'] = -3
    data['u'] = 10
    randind = np.arange(0, len(data))
    # np.random.shuffle(randind)
    # # data.loc[randind[:int(50*50*.5)],'val'] = np.nan
    # np.random.shuffle(randind)
    # data.loc[randind[:int(50*50*.1)],'val'] = 0
    origin = np.array([-0.1, -0.1, -0.1])
    maximum = np.array([1.1, 1.1, 1.1])
    nsteps = np.array([20, 20, 20])
    step_vector = (maximum - origin) / nsteps
    grid = StructuredGrid(origin=origin,
                          nsteps=nsteps,
                          step_vector=step_vector)
    interpolator = FDI(grid)
    interpolator.set_value_constraints(data[['X', 'Y', 'Z', 'val',
                                             'w']].to_numpy())
    interpolator.set_inequality_constraints(data[['X', 'Y', 'Z', 'l',
                                                  'u']].to_numpy())
    interpolator._setup_interpolator()
    # col = np.arange(0,interpolator.nx,dtype=int)
    # col = np.tile(col, (interpolator.nx, 1)).T
    # interpolator.add_inequality_constraints_to_matrix(np.eye(interpolator.nx),
    #                                                 np.zeros(interpolator.nx)-4,
    #                                                 np.zeros(interpolator.nx)+np.inf,
    #                                                 col
    #                                                 )
    interpolator.solve_system(solver='osqp')
コード例 #11
0
    def __init__(self,
                 values,
                 step_vector,
                 nsteps,
                 origin=np.zeros(3),
                 name="scalar field"):
        """[summary]

        Parameters
        ----------
        values : numpy array
            nodes values of regular grid
        step_vector : numpy array
            step vector for x,y,z step
        nsteps : numpy array
            number of steps
        name : string, optional
            name of the feature for the visualisation
        """
        self.values = values
        self.grid = StructuredGrid(origin, nsteps, step_vector)
        self.name = name
コード例 #12
0
def test_create_structured_grid():
    grid = StructuredGrid()
コード例 #13
0
def test_get_element_outside():
    grid = StructuredGrid()
    point = np.array([grid.origin - np.ones(3)])
    idc, inside = grid.position_to_cell_corners(point)
    assert inside[0] == False
コード例 #14
0
def test_change_origin():
    grid = StructuredGrid(origin=np.zeros(3), nsteps=np.array([5, 5, 5]))
    grid.origin = np.array([-1, -1, -1])
    assert np.all(grid.origin == np.array([-1, -1, -1]))
    assert np.all(grid.nsteps == np.array([6, 6, 6]))
    assert np.all(grid.step_vector == np.ones(3))
コード例 #15
0
def test_get_element():
    grid = StructuredGrid()
    point = grid.barycentre()[[0], :]
    idc, inside = grid.position_to_cell_corners(point)
    bary = np.mean(grid.nodes[idc, :], axis=0)
    assert np.sum(point - bary) == 0
コード例 #16
0
def test_evaluate_gradient():
    grid = StructuredGrid()
    grid.update_property('Y', grid.nodes[:, 1])
    vector = np.mean(grid.evaluate_gradient(grid.barycentre(), 'Y'), axis=0)
    # vector/=np.linalg.norm(vector)
    assert np.sum(vector - np.array([0, grid.step_vector[1], 0])) == 0
コード例 #17
0
def test_evaluate_value():
    grid = StructuredGrid()
    grid.update_property('X', grid.nodes[:, 0])
    assert np.sum(grid.barycentre()[:, 0] -
                  grid.evaluate_value(grid.barycentre(), 'X')) == 0
コード例 #18
0
def test_create_structured_grid_origin_nsteps():
    grid = StructuredGrid(origin=np.zeros(3), nsteps=np.array([5, 5, 5]))
    assert grid.n_nodes == 5 * 5 * 5
    assert np.sum(grid.maximum - np.ones(3) * 5) == 0
コード例 #19
0
def test_change_maximum():
    grid = StructuredGrid(origin=np.zeros(3), nsteps=np.array([5, 5, 5]))
    grid.maximum = np.array([7, 7, 7])
    assert np.all(np.isclose(grid.nsteps, np.array([8, 8, 8])))
    assert np.all(np.isclose(grid.maximum, np.array([7, 7, 7])))
    assert np.all(np.isclose(grid.step_vector, np.ones(3)))
コード例 #20
0
def test_evaluate_value():
    grid = StructuredGrid()
    assert (
        np.sum(grid.barycentre[:, 0] -
               grid.evaluate_value(grid.barycentre, grid.nodes[:, 0])) == 0)