Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graphical modeler: export variables to Python #3702

Merged
merged 12 commits into from
Jun 3, 2024

Conversation

landam
Copy link
Member

@landam landam commented May 11, 2024

Steps to reproduce

  1. Add variable(s) into model

image

  1. Export to Python

Sample model: 06_grass_chm_v3.zip

Current behaviour

No variables exported (as UI options). On the other hand variables are used in command statements (see last row in example below):

#!/usr/bin/env python3
#
##############################################################################
#
# MODULE:       model
#
# AUTHOR(S):    martin
#
# PURPOSE:      Script generated by wxGUI Graphical Modeler.
#
# DATE:         Sat May 11 22:38:15 2024
#
##############################################################################

# %module
# % description: Script generated by wxGUI Graphical Modeler.
# %end

import sys
import os
import atexit

from grass.script import parser
from grass.pygrass.modules import Module

def cleanup():
    pass

def main(options, flags):
    Module("r.in.pdal",
           flags='eo',
           overwrite=True,
           input="%path/dmr5g/%tile.laz",

New behaviour

Variables exported as UI options and used in the Python code correctly:

#!/usr/bin/env python3
#
##############################################################################
#
# MODULE:       model
#
# AUTHOR(S):    martin
#
# PURPOSE:      Script generated by wxGUI Graphical Modeler.
#
# DATE:         Sat May 11 22:43:48 2024
#
##############################################################################

# %module
# % description: Script generated by wxGUI Graphical Modeler.
# %end
# %option G_OPT_M_DIR
# % key: path
# % description: Path to input LAZ data
# % required: yes
# % answer: /home/martin/geodata/vyuka/155FGIS/06
# %end
# %option
# % key: tile
# % description: Tile name
# % required: yes
# % type: string
# % answer: KRAV59
# %end
# %option
# % key: resolution
# % description: Target resolution
# % required: yes
# % type: integer
# % answer: 5
# %end
# %option
# % key: fill
# % description: Fill distance (in pixels)
# % required: yes
# % type: integer
# % answer: 10
# %end

import sys
import os
import atexit

from grass.script import parser
from grass.pygrass.modules import Module

def cleanup():
    pass

def main(options, flags):
    Module("r.in.pdal",
           flags='eo',
           overwrite=True,
           input=f"{options['path']}/dmr5g/{options['tile']}.laz",

@landam landam added enhancement New feature or request GUI wxGUI related labels May 11, 2024
@landam landam added this to the 8.4.0 milestone May 11, 2024
@landam landam self-assigned this May 11, 2024
@landam landam marked this pull request as draft May 11, 2024 15:44
@github-actions github-actions bot added the Python Related code is in Python label May 11, 2024
@landam landam requested a review from pesekon2 May 11, 2024 20:47
@landam landam marked this pull request as ready for review May 11, 2024 20:48
Copy link
Contributor

@pesekon2 pesekon2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I define a variable without description, the script generation fails with the following error message:

Traceback (most recent call last):
  File "/home/ondrej/workspace/grass/dist.x86_64-pc-linux-gnu/gui/wxpython/gmodeler/panels.py", line 268, in OnPageChanged
    self.pythonPanel.RefreshScript()
  File "/home/ondrej/workspace/grass/dist.x86_64-pc-linux-gnu/gui/wxpython/gmodeler/panels.py", line 1724, in RefreshScript
    self.write_object(
  File "/home/ondrej/workspace/grass/dist.x86_64-pc-linux-gnu/gui/wxpython/gmodeler/model.py", line 3333, in __init__
    self._writePython()
  File "/home/ondrej/workspace/grass/dist.x86_64-pc-linux-gnu/gui/wxpython/gmodeler/model.py", line 3464, in _writePython
    description=vdesc["description"],
                ~~~~~^^^^^^^^^^^^^^^
KeyError: 'description'

@landam
Copy link
Member Author

landam commented May 31, 2024

When I define a variable without description, the script generation fails with the following error message:

Right, a variable description is optional in the current implementation (see the fill variable in screenshot below):

Screenshot from 2024-05-31 17-49-03

Now (834a8cc) it's exported as:

...
# % key: fill
# % description: 
# % required: yes
...

When running exported Python script, the parser complains about missing description:

WARNING: Bug in UI description. Description for option <fill> missing

@landam
Copy link
Member Author

landam commented May 31, 2024

In e2ac8e7, options are formatted only if necessary:

    run_command("r.in.pdal",
                flags="eo",
                overwrite=True,
                input=f"{options['path']}/dmr5g/{options['tile']}.laz",
                output=f"{options['tile']}_dmr",
                method="mean",
                type="FCELL",
                zscale=1.0,
                iscale=1.0,
                resolution=options["resolution"],
                dimension="z")

@landam landam requested a review from pesekon2 May 31, 2024 16:32
Copy link
Contributor

@pesekon2 pesekon2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have tried to create a simple model with only v.buffer, using variable o for output. It crashes with the following error:

Traceback (most recent call last):
  File "/home/pesek/workspace/grass/dist.x86_64-pc-linux-gnu/gui/wxpython/gmodeler/panels.py", line 1860, in OnRefresh
    if self.RefreshScript():
  File "/home/pesek/workspace/grass/dist.x86_64-pc-linux-gnu/gui/wxpython/gmodeler/panels.py", line 1724, in RefreshScript
    self.write_object(
  File "/home/pesek/workspace/grass/dist.x86_64-pc-linux-gnu/gui/wxpython/gmodeler/model.py", line 3333, in __init__
    self._writePython()
  File "/home/pesek/workspace/grass/dist.x86_64-pc-linux-gnu/gui/wxpython/gmodeler/model.py", line 3526, in _writePython
    self._writeItem(item, variables=modelParams)
  File "/home/pesek/workspace/grass/dist.x86_64-pc-linux-gnu/gui/wxpython/gmodeler/model.py", line 2657, in _writeItem
    self._writePythonAction(
  File "/home/pesek/workspace/grass/dist.x86_64-pc-linux-gnu/gui/wxpython/gmodeler/model.py", line 3566, in _writePythonAction
    strcmd + self._getPythonActionCmd(item, task, len(strcmd), variables) + "\n"
  File "/home/pesek/workspace/grass/dist.x86_64-pc-linux-gnu/gui/wxpython/gmodeler/model.py", line 3606, in _getPythonActionCmd
    found = pattern.search(value)
TypeError: expected string or bytes-like object

The model is simple, but still, attached here. Sorry for the txt extension, it's because github doesn't support attaching *.gxm files. It's just mimikry.
model.txt

@landam
Copy link
Member Author

landam commented Jun 1, 2024

I have tried to create a simple model with only v.buffer, using variable o for output. It crashes with the following error:

Thanks for testing! The error is caused by when pyGRASS API (option with multiple values provided a list) is used. Fixed by 8468739.

@landam landam requested a review from pesekon2 June 1, 2024 18:37
Copy link
Contributor

@pesekon2 pesekon2 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Everything seems to work now. Thanks!

@landam landam merged commit 50da15e into OSGeo:main Jun 3, 2024
26 checks passed
@landam landam deleted the gmodeler_export_variables_to_python branch June 3, 2024 09:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request GUI wxGUI related Python Related code is in Python
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants