Skip to content

Commit

Permalink
release/0.7.0: Improve and replace 'porter' by native sdist
Browse files Browse the repository at this point in the history
  • Loading branch information
nok committed Nov 11, 2018
1 parent b69f115 commit 5f034ec
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 65 deletions.
15 changes: 5 additions & 10 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ install.requirements.development: install.requirements.examples
# Examples
#

link: install.function
install.function:
$(info Start [install.function (to .bash_profile)] ...)
$(BASH) recipes/install.function.sh

open.examples: install.requirements.examples examples.pid

examples.pid:
Expand All @@ -47,7 +42,11 @@ stop.examples: examples.pid
# Development
#

all: lint test clean
all: lint test jupytext clean

lint: install.requirements.development
$(info Start [lint] ...)
find ./sklearn_porter -name '*.py' -exec pylint {} \;

test: install.requirements.development
$(info Start [test] ...)
Expand All @@ -61,10 +60,6 @@ test.sample: install.requirements.development
TEST_N_EXISTING_FEATURE_SETS=3 \
$(BASH) recipes/run.tests.sh

lint: install.requirements.development
$(info Start [lint] ...)
find ./sklearn_porter -name '*.py' -exec pylint {} \;

jupytext: install.requirements.development
$(info Start [jupytext] ...)
$(BASH) recipes/run.jupytext.sh
Expand Down
13 changes: 5 additions & 8 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,17 +226,14 @@ $ make stop.examples

## Command-line interface

In general you can use the porter on the command line. Either you use `python -m sklearn_porter [-h]` or you install an executable to use `porter [-h]` directly:

```bash
$ make link
```
In general you can use the porter on the command line:

```
$ porter [-h] --input <PICKLE_FILE> [--output <DEST_DIR>] \
[--class_name <CLASS_NAME>] [--method_name <METHOD_NAME>] \
[--c] [--java] [--js] [--go] [--php] [--ruby] \
$ porter --input <PICKLE_FILE> [--output <DEST_DIR>]
[--class_name <CLASS_NAME>] [--method_name <METHOD_NAME>]
[--export] [--checksum] [--data] [--pipe]
[--c] [--java] [--js] [--go] [--php] [--ruby]
[--help] [--version]
```

The following example shows how you can save a trained estimator to the [pickle format](http://scikit-learn.org/stable/modules/model_persistence.html#persistence-example):
Expand Down
13 changes: 0 additions & 13 deletions recipes/function.sh

This file was deleted.

12 changes: 7 additions & 5 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@
import os
from setuptools import setup
from setuptools import find_packages
from typing import List


def read_version():
# type: () -> str
"""Read the module version from __version__.txt"""
src_dir = os.path.abspath(os.path.dirname(__file__))
ver_file = os.path.join(src_dir, 'sklearn_porter', '__version__.txt')
Expand All @@ -19,24 +17,28 @@ def read_version():


def parse_requirements():
# type: () -> List[str]
"""Parse the modules from requirements.txt"""
src_dir = os.path.abspath(os.path.dirname(__file__))
req_file = os.path.join(src_dir, 'requirements.txt')
reqs = open(req_file, 'r').read().strip().split('\n')
reqs = [req.strip() for req in reqs if 'git+' not in req]
return reqs


setup(
name='sklearn-porter',
packages=find_packages(exclude=["tests.*", "tests"]),
include_package_data=True,
version=read_version(),
description='Transpile trained scikit-learn models to C, Java, JavaScript and others.',
description='Transpile trained scikit-learn models '
'to C, Java, JavaScript and others.',
author='Darius Morawiec',
author_email='[email protected]',
url='https://github.com/nok/sklearn-porter/tree/stable',
entry_points={
'console_scripts': [
'porter = sklearn_porter.cli.__main__:main'
],
},
classifiers=[
'Programming Language :: Python :: 2',
'Programming Language :: Python :: 2.7',
Expand Down
Empty file added sklearn_porter/cli/__init__.py
Empty file.
80 changes: 52 additions & 28 deletions sklearn_porter/__main__.py → sklearn_porter/cli/__main__.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,41 @@
# -*- coding: utf-8 -*-

from __future__ import print_function

import sys
import os
import os.path
import argparse

from sklearn.externals import joblib

sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from sklearn_porter.Porter import Porter
from sklearn_porter.language import *


header = '''
#
### ### ### ### ### ###
# # # # # # ## #
### ### # ## ### # v{}
#
Transpile trained scikit-learn estimators
to C, Java, JavaScript and others.
Usage:
porter --input INPUT [--output OUTPUT]
[--class_name CLASS_NAME] [--method_name METHOD_NAME]
[--export] [--checksum] [--data] [--pipe]
[--c] [--java] [--js] [--go] [--php] [--ruby]
[--help] [--version]'''.format(Porter.__version__)


def parse_args(args):
parser = argparse.ArgumentParser(
description=('Transpile trained scikit-learn estimators '
'to C, Java, JavaScript and others.'),
epilog='More details on: https://github.com/nok/sklearn-porter')
epilog = 'More details on https://github.com/nok/sklearn-porter'
description = ''
parser = argparse.ArgumentParser(description=description,
epilog=epilog, usage=header)

# Remove the default arguments group:
parser._action_groups.pop()
Expand All @@ -27,7 +47,7 @@ def parse_args(args):
help=('Path to an exported estimator in pickle '
'(.pkl) format.'))

# Optional arguments:
# Optional arguments:
optional = parser.add_argument_group('Optional arguments')
optional.add_argument('--output', '-o',
required=False,
Expand All @@ -46,13 +66,13 @@ def parse_args(args):
default=False,
action='store_true',
help='Whether to export the model data or not.')
optional.add_argument('--checksum',
optional.add_argument('--checksum', '-s',
required=False,
default=False,
action='store_true',
help='Whether to append the checksum to the '
'filename or not.')
optional.add_argument('--data',
optional.add_argument('--data', '-d',
required=False,
default=False,
action='store_true',
Expand All @@ -62,22 +82,28 @@ def parse_args(args):
default=False,
action='store_true',
help='Print the transpiled estimator to the console.')
languages = {
'c': 'C',
'java': 'Java',
'js': 'JavaScript',
'go': 'Go',
'php': 'PHP',
'ruby': 'Ruby'
}
optional.add_argument('--language', '-l',
choices=languages.keys(),
default='java',
required=False,
help=argparse.SUPPRESS)
for key, lang in list(languages.items()):
help = 'Set \'{}\' as the target programming language.'.format(lang)
optional.add_argument('--{}'.format(key), action='store_true', help=help)

# Languages:
langs = parser.add_argument_group('Programming languages')
languages = {key: clazz.LABEL for key, clazz in list(LANGUAGES.items())}
langs.add_argument('--language', '-l',
choices=languages.keys(),
default='java',
required=False,
help=argparse.SUPPRESS)
for key, label in list(languages.items()):
help = 'Set \'{}\' as the target programming language.'.format(label)
langs.add_argument('--{}'.format(key), action='store_true', help=help)

# Extra arguments:
extras = parser.add_argument_group('Extra arguments')
extras.add_argument('--version', '-v', action='version',
version='sklearn-porter v{}'.format(Porter.__version__))

# Show help by default:
if len(sys.argv) == 1:
parser.print_help(sys.stderr)
sys.exit(1)

# Return dictionary:
args = vars(parser.parse_args(args))
Expand Down Expand Up @@ -119,10 +145,8 @@ def main():
with_export = bool(args.get('export'))
with_checksum = bool(args.get('checksum'))
porter = Porter(estimator, language=language)
output = porter.export(class_name=class_name,
method_name=method_name,
export_dir=dest_dir,
export_data=with_export,
output = porter.export(class_name=class_name, method_name=method_name,
export_dir=dest_dir, export_data=with_export,
export_append_checksum=with_checksum,
details=True)
except Exception as exception:
Expand Down
2 changes: 1 addition & 1 deletion sklearn_porter/language/PHP.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class PHP(object):

KEY = 'php'
LABEL = 'KEY'
LABEL = 'PHP'

DEPENDENCIES = ['php']
TEMP_DIR = 'php'
Expand Down

0 comments on commit 5f034ec

Please sign in to comment.