Skip to content

Commit

Permalink
Add pipe functionality '... --pipe | ...'
Browse files Browse the repository at this point in the history
  • Loading branch information
nok committed Sep 15, 2017
1 parent dda6daf commit 8a57746
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 9 deletions.
18 changes: 15 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ joblib.dump(clf, 'estimator.pkl')
After that the model can be transpiled by using the following command:

```
python -m sklearn_porter --input <PICKLE_FILE> [--output <DEST_DIR>] [--c] [--java] [--js] [--go] [--php] [--ruby]
python -m sklearn_porter -i <PICKLE_FILE> [-o <DEST_DIR>] [--c] [--java] [--js] [--go] [--php] [--ruby]
python -m sklearn_porter --input <PICKLE_FILE> [--output <DEST_DIR>] [--pipe] [--c] [--java] [--js] [--go] [--php] [--ruby]
python -m sklearn_porter -i <PICKLE_FILE> [-o <DEST_DIR>] [-p] [--c] [--java] [--js] [--go] [--php] [--ruby]
```

For instance the following command transpiles the estimator to the target programming language Java:
Expand All @@ -249,7 +249,7 @@ For instance the following command transpiles the estimator to the target progra
python -m sklearn_porter -i estimator.pkl --java
```

You can change the target programming language on the fly:
The target programming language is changeable on the fly:

```
python -m sklearn_porter -i estimator.pkl --c
Expand All @@ -259,6 +259,18 @@ python -m sklearn_porter -i estimator.pkl --php
python -m sklearn_porter -i estimator.pkl --ruby
```

The transpiled estimator is useable for further processing by using the `--pipe` parameter:

```
python -m sklearn_porter -i estimator.pkl --js --pipe > estimator.js
```

For instance the generated JavaScript code can be minified by using [UglifyJS](https://github.com/mishoo/UglifyJS2):

```
python -m sklearn_porter -i estimator.pkl --js --pipe | uglifyjs --compress -o estimator.min.js
```

Further information will be shown by using the `--help` parameter:

```
Expand Down
23 changes: 17 additions & 6 deletions sklearn_porter/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ def parse_args(args):
'Set the destination directory, '
'where the transpiled estimator will be '
'stored.'))
parser.add_argument(
'--pipe', '-p',
required=False,
default=False,
action='store_true',
help='Print the transpiled estimator to the console.')
languages = {
'c': 'C',
'java': 'Java',
Expand Down Expand Up @@ -55,7 +61,7 @@ def main():
args = parse_args(sys.argv[1:])

# Check input data:
input_path = str(args['input'])
input_path = str(args.get('input'))
if not input_path.endswith('.pkl') or not os.path.isfile(input_path):
error = 'No valid estimator in pickle format was found.'
sys.exit('Error: {}'.format(error))
Expand All @@ -65,7 +71,7 @@ def main():
estimator = joblib.load(input_path)

# Determine the target programming language:
language = str(args['language']) # with default language
language = str(args.get('language')) # with default language
languages = ['c', 'java', 'js', 'go', 'php', 'ruby']
for key in languages:
if args.get(key): # found explicit assignment
Expand All @@ -75,13 +81,18 @@ def main():
# Port estimator:
try:
porter = Porter(estimator, language=language)
details = porter.export(details=True)
output = porter.export(details=True)
except Exception as e:
sys.exit('Error: {}'.format(str(e)))
else:
# Print transpiled estimator to the console:
if bool(args.get('pipe', False)):
print(output.get('model'))
sys.exit(0)

# Define destination path:
dest_dir = str(args['output'])
filename = details.get('filename')
dest_dir = str(args.get('output'))
filename = output.get('filename')
if dest_dir != '' and os.path.isdir(dest_dir):
dest_path = os.path.join(dest_dir, filename)
else:
Expand All @@ -92,7 +103,7 @@ def main():

# Save transpiled estimator:
with open(dest_path, 'w') as file_:
file_.write(details.get('model'))
file_.write(output.get('model'))


if __name__ == "__main__":
Expand Down

0 comments on commit 8a57746

Please sign in to comment.