-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtutorial05.py
65 lines (47 loc) · 1.96 KB
/
tutorial05.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
from PySide2.QtWidgets import QApplication
from nlScript.parser import Parser
from nlScript.ui.ui import ACEditor
from preprocessing import Preprocessing
"""
The previous tutorial introduced custom types.
In Tutorial04, they will be used to change how they are autocompleted in the editor
For details, see
https://nlScript.github.io/nlScript-java/#fine-tuning-autocompletion-parameterized-autocompletion
"""
if __name__ == '__main__':
# Needed for running a PySide application
app = QApplication([])
preprocessing = Preprocessing(None)
preprocessing.open('http://imagej.net/images/clown.jpg')
preprocessing.show()
parser = Parser()
# 1. Define the type "units" as the string literal "pixel(s)"
# Here we use a lambda expression to define the evaluator
parser.defineType("units", "pixel(s)", evaluator=lambda pn: False)
# 2. Define the type "units" as the string literal "calibrated units"
parser.defineType("units", "calibration units", evaluator=lambda pn: True)
def evaluateFilterSize(pn):
stddev = pn.evaluate("stddev")
# Note that the "units" type evaluates to a Boolean, which is true if "calibrated units" was
# parsed, and false if "pixel(s)" was parsed
units = pn.evaluate("units")
# Convert stddev to pixel units in case it was specified in calibrated units
if units:
stddev /= preprocessing.getPixelWidth()
return stddev
parser.defineType(
"filter-size",
"{stddev:float} {units:units}",
evaluator=evaluateFilterSize,
autocompleter=True
)
def evaluateSentence(pn):
stddev = pn.evaluate("stddev")
preprocessing.gaussianBlur(stddev)
parser.defineSentence(
"Apply Gaussian blurring with a standard deviation of {stddev:filter-size}.",
evaluator=evaluateSentence)
editor = ACEditor(parser)
editor.show()
# Needed for running a PySide application
exit(app.exec_())