Skip to content

Commit

Permalink
Fix python API examples (#768)
Browse files Browse the repository at this point in the history
* fix index

* run python examples in CI

* test
  • Loading branch information
wu-haoze authored Feb 27, 2024
1 parent f9c12ca commit 8129640
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 9 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ jobs:
export PYTHONPATH="$PYTHONPATH:$(dirname $(find $GITHUB_WORKSPACE -name "maraboupy" -type d))"
python -c "import maraboupy"
- name: Run Python API examples
run: |
export PYTHONPATH="$PYTHONPATH:../../"
for file in *py; do python -u $file; done
working-directory: maraboupy/examples

- name: Generate Python Code Coverage
if: ${{ ( matrix.compiler == 'g++' ) && ( matrix.build_type == 'Debug' ) }}
run: python -m pytest --cov=maraboupy --cov-report=xml maraboupy/test
Expand Down
4 changes: 2 additions & 2 deletions maraboupy/examples/0_NNetExample.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
# %%
# Load the network from NNet file, and set a lower bound on first output variable
net1 = Marabou.read_nnet(nnetFile)
net1.setLowerBound(net1.outputVars[0][0], .5)
net1.setLowerBound(net1.outputVars[0][0][0], .5)

# %%
# Solve Marabou query
Expand Down Expand Up @@ -59,4 +59,4 @@

net2 = Marabou.read_nnet(nnetFile)
outputsMarabou = net2.evaluateWithMarabou([inputs])
assert max(abs(outputsMarabou.flatten() - outputsExpected)) < 1e-8
assert max(abs(outputsMarabou[0].flatten() - outputsExpected)) < 1e-8
8 changes: 4 additions & 4 deletions maraboupy/examples/1_TensorflowExample.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
# Or, you can specify the operation names of the input and output operations.
# The default chooses the placeholder operations as input and the last operation as output
inputNames = ['Placeholder']
outputName = 'y_out'
network = Marabou.read_tf(filename = filename, inputNames = inputNames, outputName = outputName)
outputNames = ['y_out']
network = Marabou.read_tf(filename = filename, inputNames = inputNames, outputNames = outputNames)

# %%
# Get the input and output variable numbers; [0] since first dimension is batch size
Expand All @@ -43,8 +43,8 @@

# %%
# Set output bounds on the second output variable
network.setLowerBound(outputVars[1], 194.0)
network.setUpperBound(outputVars[1], 210.0)
network.setLowerBound(outputVars[0][1], 194.0)
network.setUpperBound(outputVars[0][1], 210.0)

# %%
# Call to C++ Marabou solver
Expand Down
2 changes: 1 addition & 1 deletion maraboupy/examples/2_ONNXExample.py
Original file line number Diff line number Diff line change
Expand Up @@ -129,4 +129,4 @@
print(onnxEval)
print("\nDifference:")
print(onnxEval - marabouEval)
assert max(abs(onnxEval - marabouEval)) < 1e-6
assert max(abs(onnxEval - marabouEval).flatten()) < 1e-6
2 changes: 1 addition & 1 deletion maraboupy/examples/4_DncExample.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
# Load an example network and place an output constraint
nnet_file_name = "../../src/input_parsers/acas_example/ACASXU_run2a_1_1_tiny_2.nnet"
net = Marabou.read_nnet(nnet_file_name)
net.setLowerBound(net.outputVars[0][0], .5)
net.setLowerBound(net.outputVars[0][0][0], .5)

# %%
# Solve the query with DNC mode turned on, which should return satisfying variable values
Expand Down
2 changes: 1 addition & 1 deletion maraboupy/examples/5_DisjunctionConstraintExample.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

# %%
# Path to NNet file
nnetFile = "./resources/nnet/mnist/mnist10x10.nnet"
nnetFile = "../../resources/nnet/mnist/mnist10x10.nnet"

# %%
# Load the network from NNet file, and set a lower bound on first output variable
Expand Down

0 comments on commit 8129640

Please sign in to comment.