grass icon indicating copy to clipboard operation
grass copied to clipboard

grass.experimental: Add NumPy arrays IO to Tools

Open wenzeslaus opened this issue 4 months ago • 0 comments

This is adding NumPy array as input and output to tools when called through the Tools object.

This is building on top of Tools class addition in #2923 (which is not merged yet), so for now it includes changes also from that PR. The big picture is also discussed in #5830.

While this can't be merged now, we can discuss the specific aspects of NumPy array handling here. My focus with this PR is to create a good API which can be used in various contexts and is useful as is. However, the specifics of the implementation, especially performance are secondary issues for me in this PR.

Main functionality

In an existing session (e.g., in a GRASS tool):

tools = Tools()
tools.g_region(rows=2, cols=3)
slope = tools.r_slope_aspect(elevation=np.ones((2, 3)), slope=np.ndarray)

In a Python script:

gs.create_project("project1")
with gs.setup.init("project1") as session:
    tools = Tools(session=session)
    tools.g_region(rows=2, cols=3)
    slope = tools.r_slope_aspect(elevation=np.ones((2, 3)), slope=np.ndarray)

For tests

Writing pytests and testing the outputs is greatly simplified without introducing additional functionality (not saying that we should not evaluate what more we need for pytest, but we can do it in light of this):

def test_slope(xy_session):
    tools = Tools(session=xy_session)
    tools.g_region(rows=2, cols=3)
    slope = tools.r_slope_aspect(elevation=np.ones((2, 3)), slope=np.ndarray)
    assert slope.shape == (2, 3)
    assert np.all(slope == np.full((2, 3), 0))

wenzeslaus avatar Jun 11 '25 18:06 wenzeslaus