-
Notifications
You must be signed in to change notification settings - Fork 0
Add Python examples for KNI usage #1
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
Open
bruno-at-orange
wants to merge
8
commits into
main
Choose a base branch
from
add-python-src
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
b23b906
Add Python examples for KNI usage
bruno-at-orange 47b6876
After review
bruno-at-orange e352da5
Refactor Python API to use exceptions instead of return codes
bruno-at-orange fc91638
Update README
bruno-at-orange 83f9580
Add new khiops-kni Python package
bruno-at-orange 70e8735
Improve documentation and API
bruno-at-orange 0849cd0
After review
bruno-at-orange 2502f7a
Python API in markdown
bruno-at-orange File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,3 @@ | ||
| .vscode/ | ||
| python/__pycache__/ | ||
| .DS_Store |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| #!/usr/bin/env python3 | ||
| # Copyright (c) 2023-2026 Orange. All rights reserved. | ||
| # This software is distributed under the BSD 3-Clause-clear License, the text of which is available | ||
| # at https://spdx.org/licenses/BSD-3-Clause-Clear.html or see the "LICENSE" file for more details. | ||
|
|
||
| """ | ||
| KNIRecodeFile: Recode an input file to an output file using a Khiops dictionary. | ||
|
|
||
| This script demonstrates the use of the Khiops Native Interface (KNI) from Python | ||
| to deploy a Khiops model for real-time scoring without temporary files. | ||
| """ | ||
|
|
||
| import sys | ||
| import argparse | ||
| from kni import KNI, KNIError | ||
|
|
||
|
|
||
| def recode_file( | ||
| dictionary_file_name, | ||
| dictionary_name, | ||
| input_file_name, | ||
| output_file_name, | ||
| error_file_name="", | ||
| ): | ||
| """ | ||
| Recode an input file to an output file using a Khiops dictionary. | ||
|
|
||
| Args: | ||
| dictionary_file_name: Path to the dictionary file | ||
| dictionary_name: Name of the dictionary to use | ||
| input_file_name: Path to input file (must have header line) | ||
| output_file_name: Path to output file | ||
| error_file_name: Optional path to error log file (empty for no logging) | ||
|
|
||
| Raises: | ||
| KNIError: If any KNI operation fails | ||
| FileNotFoundError: If input file is not found | ||
| ValueError: If input file is empty or invalid | ||
| """ | ||
| # Initialize KNI | ||
| kni = KNI() | ||
|
|
||
| # Set error log file | ||
| if error_file_name: | ||
| kni.set_log_file_name(error_file_name) | ||
|
|
||
| print(f"\nRecode records of {input_file_name} to {output_file_name}") | ||
|
|
||
| # Open input and output files | ||
| with open(input_file_name, "r", encoding="utf-8") as input_file, open( | ||
| output_file_name, "w", encoding="utf-8" | ||
| ) as output_file: | ||
|
|
||
| # Read header line | ||
| header_line = input_file.readline().rstrip() | ||
| if not header_line: | ||
| raise ValueError("Empty input file") | ||
|
|
||
| # Open KNI stream | ||
| stream_handle = kni.open_stream( | ||
| dictionary_file_name, dictionary_name, header_line, "\t" | ||
| ) | ||
|
|
||
| try: | ||
| # Process all records | ||
| record_number = 0 | ||
| for line_number, line in enumerate(input_file, start=2): | ||
| # Remove trailing whitespace | ||
| input_record = line.rstrip() | ||
|
|
||
| # Skip empty lines | ||
| if not input_record: | ||
| continue | ||
|
|
||
| # Recode the record | ||
| output_record = kni.recode_stream_record(stream_handle, input_record) | ||
|
|
||
| # Write output record | ||
| output_file.write(f"{output_record}\n") | ||
| record_number += 1 | ||
| finally: | ||
| # Close stream | ||
| kni.close_stream(stream_handle) | ||
|
|
||
| print(f"{record_number} records recoded") | ||
|
|
||
|
|
||
| def main(): | ||
| """Main entry point for command-line execution.""" | ||
| parser = argparse.ArgumentParser( | ||
| description="Recode an input file to an output file using a Khiops dictionary.", | ||
| epilog="The input file must have a header line, describing the structure of all its instances. " | ||
| "The input and output files have a tabular format. " | ||
| "The error file may be useful for debugging purposes.", | ||
| ) | ||
|
|
||
| parser.add_argument( | ||
| "dictionary_file", | ||
| help="Path to the dictionary file", | ||
| ) | ||
| parser.add_argument( | ||
| "dictionary_name", | ||
| help="Name of the dictionary to use", | ||
| ) | ||
| parser.add_argument( | ||
| "input_file", | ||
| help="Path to input file (must have header line)", | ||
| ) | ||
| parser.add_argument( | ||
| "output_file", | ||
| help="Path to output file", | ||
| ) | ||
| parser.add_argument( | ||
| "error_file", | ||
| nargs="?", | ||
| default="", | ||
| help="Optional path to error log file (empty for no logging)", | ||
| ) | ||
|
|
||
| args = parser.parse_args() | ||
|
|
||
| # Execute recoding | ||
| try: | ||
| recode_file( | ||
| args.dictionary_file, | ||
| args.dictionary_name, | ||
| args.input_file, | ||
| args.output_file, | ||
| args.error_file, | ||
| ) | ||
| return 0 | ||
| except FileNotFoundError as e: | ||
| print(f"Error: File not found: {e.filename}", file=sys.stderr) | ||
| return 1 | ||
| except Exception as e: | ||
| print(f"Error: {e}", file=sys.stderr) | ||
| return 1 | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| sys.exit(main()) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.