From a8aafcb70ad93da9a28cd990de3b336225ee0b12 Mon Sep 17 00:00:00 2001 From: Benoit Chevallier-Mames Date: Fri, 15 Oct 2021 15:40:02 +0200 Subject: [PATCH] docs: autogenerate the list of supported functions closes #410 --- Makefile | 15 ++- .../tutorial/WORKING_WITH_FLOATING_POINTS.md | 114 +++++++++++++----- script/doc_utils/gen_supported_ufuncs.py | 85 +++++++++++++ 3 files changed, 180 insertions(+), 34 deletions(-) create mode 100644 script/doc_utils/gen_supported_ufuncs.py diff --git a/Makefile b/Makefile index 7d76b4cab..d1af79bc2 100644 --- a/Makefile +++ b/Makefile @@ -63,7 +63,7 @@ flake8: python_linting: pylint flake8 .PHONY: python_linting -conformance: finalize_nb python_format +conformance: finalize_nb python_format supported_functions .PHONY: conformance pcc: @@ -72,7 +72,7 @@ pcc: .PHONY: pcc PCC_DEPS := check_python_format check_finalize_nb python_linting mypy_ci pydocstyle shell_lint -PCC_DEPS += check_version_coherence +PCC_DEPS += check_version_coherence check_supported_functions pcc_internal: $(PCC_DEPS) .PHONY: pcc_internal @@ -158,7 +158,7 @@ docker_publish_measurements: docker_build /bin/bash ./script/progress_tracker_utils/benchmark_and_publish_findings_in_docker.sh .PHONY: docker_publish_measurements -docs: clean_docs +docs: clean_docs supported_functions @# Generate the auto summary of documentations poetry run sphinx-apidoc -o docs/_apidoc $(SRC_DIR) @@ -257,3 +257,12 @@ todo: --pylintrc-path pylintrc);\ grep -rInH --exclude-dir='.[^.]*' --exclude=pylintrc --exclude='*.ipynb' "$${NOTES_ARGS}" . .PHONY: todo + +# Update docs with supported functions +supported_functions: + poetry run python script/doc_utils/gen_supported_ufuncs.py docs/user/tutorial/WORKING_WITH_FLOATING_POINTS.md +.PHONY: supported_functions + +check_supported_functions: + poetry run python script/doc_utils/gen_supported_ufuncs.py docs/user/tutorial/WORKING_WITH_FLOATING_POINTS.md --check +.PHONY: check_supported_functions diff --git a/docs/user/tutorial/WORKING_WITH_FLOATING_POINTS.md b/docs/user/tutorial/WORKING_WITH_FLOATING_POINTS.md index 62d088baf..afaaeb4d4 100644 --- a/docs/user/tutorial/WORKING_WITH_FLOATING_POINTS.md +++ b/docs/user/tutorial/WORKING_WITH_FLOATING_POINTS.md @@ -27,37 +27,89 @@ circuit.run(60) == 58 The following operations are supported in the latest release, and we'll add more operations in the upcoming releases. -- np.arccos -- np.arccosh -- np.arcsin -- np.arcsinh -- np.arctan -- np.arctanh -- np.cbrt -- np.ceil -- np.cos -- np.cosh -- np.deg2rad -- np.degrees -- np.exp -- np.exp2 -- np.expm1 -- np.fabs -- np.floor -- np.log -- np.log10 -- np.log1p -- np.log2 -- np.rad2deg -- np.radians -- np.rint -- np.sin -- np.sinh -- np.spacing -- np.sqrt -- np.tan -- np.tanh -- np.trunc + + +List of supported unary functions: +- absolute +- arccos +- arccosh +- arcsin +- arcsinh +- arctan +- arctanh +- cbrt +- ceil +- cos +- cosh +- deg2rad +- degrees +- exp +- exp2 +- expm1 +- fabs +- floor +- invert +- isfinite +- isinf +- isnan +- log +- log10 +- log1p +- log2 +- logical_not +- negative +- positive +- rad2deg +- radians +- reciprocal +- rint +- sign +- signbit +- sin +- sinh +- spacing +- sqrt +- square +- tan +- tanh +- trunc + +List of supported binary functions if one of the two operators is a constant scalar: +- arctan2 +- bitwise_and +- bitwise_or +- bitwise_xor +- copysign +- equal +- float_power +- floor_divide +- fmax +- fmin +- fmod +- gcd +- greater +- greater_equal +- heaviside +- hypot +- lcm +- ldexp +- left_shift +- less +- less_equal +- logaddexp +- logaddexp2 +- logical_and +- logical_or +- logical_xor +- maximum +- minimum +- nextafter +- not_equal +- power +- remainder +- right_shift +- true_divide + ## Limitations diff --git a/script/doc_utils/gen_supported_ufuncs.py b/script/doc_utils/gen_supported_ufuncs.py new file mode 100644 index 000000000..6de3baeaa --- /dev/null +++ b/script/doc_utils/gen_supported_ufuncs.py @@ -0,0 +1,85 @@ +"""Update list of supported functions in the doc.""" +import argparse + +from concrete.numpy import tracing + + +def main(file_to_update): + """Update list of supported functions in file_to_update""" + supported_unary_ufunc = sorted( + f.__name__ for f in tracing.NPTracer.LIST_OF_SUPPORTED_UFUNC if f.nin == 1 + ) + supported_binary_ufunc = sorted( + f.__name__ for f in tracing.NPTracer.LIST_OF_SUPPORTED_UFUNC if f.nin == 2 + ) + + with open(file_to_update, "r", encoding="utf-8") as file: + lines = file.readlines() + + newlines = [] + keep_line = True + + for line in lines: + if line.startswith( + "" + ): + keep_line = False + newlines.append(line) + newlines.append( + "\n" + ) + elif line.startswith( + "" + ): + pass + elif line.startswith( + "" + ): + keep_line = True + + # Inject the supported functions + newlines.append("List of supported unary functions:\n") + + newlines.extend(f"- {f}\n" for f in supported_unary_ufunc) + + newlines.append("\n") + newlines.append( + "List of supported binary functions if one of the " + "two operators is a constant scalar:\n" + ) + + newlines.extend(f"- {f}\n" for f in supported_binary_ufunc) + + newlines.append(line) + else: + assert "gen_supported_ufuncs.py" not in line, ( + f"Error: not expected to have 'gen_supported_ufuncs.py' at line {line} " + f"of {file_to_update}" + ) + + if keep_line: + newlines.append(line) + + if args.check: + + with open(file_to_update, "r", encoding="utf-8") as file: + oldlines = file.readlines() + + assert ( + oldlines == newlines + ), "List of supported functions is not up to date. Please run `make supported_functions`." + + else: + with open(file_to_update, "w", encoding="utf-8") as file: + file.writelines(newlines) + + +if __name__ == "__main__": + parser = argparse.ArgumentParser(description="Update list of supported functions in the doc") + parser.add_argument("--check", action="store_true", help="flag to enable just checking mode") + + parser.add_argument("file_to_update", type=str, help=".md file to update") + args = parser.parse_args() + main(args.file_to_update)