Skip to content
This repository has been archived by the owner on Jan 13, 2024. It is now read-only.

Commit

Permalink
improve code coverage, fix unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
sdpython committed May 19, 2020
1 parent 37480df commit 8d49c91
Show file tree
Hide file tree
Showing 10 changed files with 134 additions and 109 deletions.
Expand Up @@ -33,7 +33,7 @@ def test_create_asv_benchmark_hist_gbc(self):
raise AssertionError(fullname)
with open(fullname, 'r', encoding='utf-8') as f:
content = f.read()
if (zoo.endswith("bench_HGBClas_default_b_cl_mxit10.py") and
if (zoo.endswith("bench_HGBClas_default_b_cl_mxit100.py") and
compare_module_version(sklearn.__version__, "0.21") >= 0):
if "random_state=42" not in content:
raise AssertionError(content)
Expand Down
Expand Up @@ -67,7 +67,7 @@ def test_create_asv_benchmark_pyspy_compiled(self):
fullname = os.path.join(path, zoo)
with open(fullname, 'r', encoding='utf-8') as f:
content = f.read()
if (zoo.endswith("bench_AdaBoostReg_default_b_reg_nest5_1_4_%d_float_.py" % ops) and
if (zoo.endswith("bench_AdaBoostReg_default_b_reg_nest10_1_4_%d_float_.py" % ops) and
compare_module_version(sklearn.__version__, "0.21") >= 0):
if "setup_profile" not in content:
raise AssertionError(content)
Expand Down
7 changes: 6 additions & 1 deletion mlprodict/asv_benchmark/_create_asv_helper.py
Expand Up @@ -512,4 +512,9 @@ def find_sklearn_module(piece):
import sklearn.cluster
glo[piece] = getattr(sklearn.cluster, piece)
return "sklearn.cluster"
raise ValueError("Unable to find module to import for '{}'.".format(piece))
if piece in {'OneVsRestClassifier', 'OneVsOneClassifier'}:
import sklearn.multiclass
glo[piece] = getattr(sklearn.multiclass, piece)
return "sklearn.multiclass"
raise ValueError(
"Unable to find module to import for '{}'.".format(piece))
40 changes: 21 additions & 19 deletions mlprodict/asv_benchmark/create_asv.py
Expand Up @@ -10,7 +10,7 @@
import re
try:
from pyquickhelper.pycode.code_helper import remove_extra_spaces_and_pep8 # pragma: no cover
except ImportError:
except ImportError: # pragma: no cover
remove_extra_spaces_and_pep8 = lambda code, *args, **kwargs: code
try:
from ._create_asv_helper import (
Expand Down Expand Up @@ -38,11 +38,11 @@
find_missing_sklearn_imports)

try:
from ...tools.asv_options_helper import get_opset_number_from_onnx
from ..tools.asv_options_helper import (
get_opset_number_from_onnx, shorten_onnx_options)
from ..onnxrt.validate.validate_helper import sklearn_operators
from ..onnxrt.validate.validate import (
_retrieve_problems_extra, _get_problem_data, _merge_options)
from ..tools.asv_options_helper import shorten_onnx_options
except (ValueError, ImportError): # pragma: no cover
from mlprodict.tools.asv_options_helper import get_opset_number_from_onnx
from mlprodict.onnxrt.validate.validate_helper import sklearn_operators
Expand Down Expand Up @@ -186,7 +186,8 @@ def create_asv_benchmark(
v in conf['matrix'].items() if k not in drop_keys}
conf['matrix'].update(matrix)
elif env is not None:
raise ValueError("Unable to handle env='{}'.".format(env))
raise ValueError("Unable to handle env='{}'.".format(
env)) # pragma: no cover
dest = os.path.join(location, "asv.conf.json")
created.append(dest)
with open(dest, "w", encoding='utf-8') as f:
Expand Down Expand Up @@ -312,10 +313,11 @@ def _enumerate_asv_benchmark_all_models( # pylint: disable=R0914

if models is not None:
if not all(map(lambda m: isinstance(m, str), models)):
raise ValueError("models must be a set of strings.")
raise ValueError(
"models must be a set of strings.") # pragma: no cover
ops_ = [_ for _ in ops if _['name'] in models]
if len(ops) == 0:
raise ValueError("Parameter models is wrong: {}\n{}".format(
raise ValueError("Parameter models is wrong: {}\n{}".format( # pragma: no cover
models, ops[0]))
ops = ops_
if skip_models is not None:
Expand All @@ -329,8 +331,8 @@ def iterate():
yield row

if verbose >= 11:
verbose -= 10
loop = iterate()
verbose -= 10 # pragma: no cover
loop = iterate() # pragma: no cover
else:
try:
from tqdm import trange
Expand All @@ -345,7 +347,7 @@ def iterate_tqdm():

loop = iterate_tqdm()

except ImportError:
except ImportError: # pragma: no cover
loop = iterate()
else:
loop = ops
Expand Down Expand Up @@ -401,7 +403,7 @@ def iterate_tqdm():
# Skips unrelated problem for a specific configuration.
continue
elif subset_problems is not None:
raise RuntimeError(
raise RuntimeError( # pragma: no cover
"subset_problems must be a set or a list not {}.".format(
subset_problems))

Expand Down Expand Up @@ -454,7 +456,7 @@ def _create_asv_benchmark_file( # pylint: disable=R0914
through the argument. It uses template @see cl TemplateBenchmark.
"""
if patterns is None:
raise ValueError("Patterns list is empty.")
raise ValueError("Patterns list is empty.") # pragma: no cover

def format_conv_options(d_options, class_name):
if d_options is None:
Expand All @@ -465,7 +467,7 @@ def format_conv_options(d_options, class_name):
if "." + class_name + "'" in str(k):
res[class_name] = v
continue
raise ValueError(
raise ValueError( # pragma: no cover
"Class '{}', unable to format options {}".format(
class_name, d_options))
res[k] = v
Expand Down Expand Up @@ -508,7 +510,7 @@ def _nick_name_options(model, opts):
model, scenario, optimisation, extra,
dofit, conv_options, problem,
shorten=True)
except ValueError as e:
except ValueError as e: # pragma: no cover
if exc:
raise e
warnings.warn(str(e))
Expand Down Expand Up @@ -540,7 +542,7 @@ def _nick_name_options(model, opts):
}
for k, v in rep.items():
if k not in class_content:
raise ValueError("Unable to find '{}'\n{}.".format(
raise ValueError("Unable to find '{}'\n{}.".format( # pragma: no cover
k, class_content))
class_content = class_content.replace(k, v + ',')
class_content = class_content.split(
Expand All @@ -549,7 +551,7 @@ def _nick_name_options(model, opts):
class_content = class_content.replace(
"'####", "").replace("####'", "")
if "####" in class_content:
raise RuntimeError(
raise RuntimeError( # pragma: no cover
"Substring '####' should not be part of the script for '{}'\n{}".format(
model.__name__, class_content))

Expand Down Expand Up @@ -583,15 +585,15 @@ def _nick_name_options(model, opts):
# Check compilation
try:
compile(class_content, filename, 'exec')
except SyntaxError as e:
except SyntaxError as e: # pragma: no cover
raise SyntaxError("Unable to compile model '{}'\n{}".format(
model.__name__, class_content)) from e

# Verifies missing imports.
to_import, _ = verify_code(class_content, exc=False)
try:
miss = find_missing_sklearn_imports(to_import)
except ValueError as e:
except ValueError as e: # pragma: no cover
raise ValueError(
"Unable to check import in script\n{}".format(
class_content)) from e
Expand All @@ -606,7 +608,7 @@ def _nick_name_options(model, opts):
# Check compilation again
try:
obj = compile(class_content, filename, 'exec')
except SyntaxError as e:
except SyntaxError as e: # pragma: no cover
raise SyntaxError("Unable to compile model '{}'\n{}".format(
model.__name__,
_display_code_lines(class_content))) from e
Expand All @@ -615,7 +617,7 @@ def _nick_name_options(model, opts):
if execute:
try:
exec(obj, globals(), locals()) # pylint: disable=W0122
except Exception as e:
except Exception as e: # pragma: no cover
raise RuntimeError(
"Unable to process class '{}' ('{}') a script due to '{}'\n{}".format(
model.__name__, filename, str(e),
Expand Down
67 changes: 38 additions & 29 deletions mlprodict/grammar/gactions.py
Expand Up @@ -20,10 +20,11 @@ def __init__(self, inputs, output, name, children=None):
@param children actions used to compute this one
"""
if not isinstance(inputs, list):
raise TypeError('inputs must be a list of MLType.')
raise TypeError(
'inputs must be a list of MLType.') # pragma: no cover
for t in inputs:
if not isinstance(t, MLType):
raise TypeError(
raise TypeError( # pragma: no cover
"Every input must be a MLType not '{0}'.".format(type(t)))
if not isinstance(output, MLType):
raise TypeError('output must be of MLType.')
Expand All @@ -32,7 +33,7 @@ def __init__(self, inputs, output, name, children=None):
self.name = name
self.children = children if children is not None else []
for child in self.children:
if not isinstance(child, MLAction):
if not isinstance(child, MLAction): # pragma: no cover
raise TypeError("All children must be of type MLAction")

def execute(self, **kwargs):
Expand Down Expand Up @@ -92,7 +93,8 @@ def _export_json(self, hook=None, result_name=None):
@AutoAction.cache
def _export_c(self, hook=None, result_name=None):
if result_name is None:
raise ValueError("result_name must not be None")
raise ValueError(
"result_name must not be None") # pragma: no cover
rows = []
rows.append("// {0}-{1} - children".format(id(self), self.name))
names = []
Expand Down Expand Up @@ -134,9 +136,9 @@ def guess_type(value):
"""
if isinstance(value, (float, numpy.float32)):
return MLNumTypeFloat32()
elif isinstance(value, (int, numpy.int32)):
if isinstance(value, (int, numpy.int32)):
return MLNumTypeInt32()
elif isinstance(value, numpy.ndarray):
if isinstance(value, numpy.ndarray):
a = numpy.zeros(1, value.dtype)
t = MLActionCst.guess_type(a[0])
return MLTensor(t, value.shape)
Expand All @@ -150,8 +152,7 @@ def execute(self, **kwargs):
def graph_execution(self):
if self.comment:
return "cst: {0} = {1}".format(self.comment, self.cst)
else:
return "cst: {0}".format(self.cst)
return "cst: {0}".format(self.cst)

@AutoAction.cache
def _export_json(self, hook=None, result_name=None):
Expand All @@ -164,7 +165,7 @@ def _export_json(self, hook=None, result_name=None):
@AutoAction.cache
def _export_c(self, hook=None, result_name=None):
if result_name is None:
raise ValueError("result_name cannot be None.")
raise ValueError("result_name cannot be None.") # pragma: no cover
dc = self.output._export_c(hook='declare', result_name=result_name)
res = "{0} = {1};".format(
dc['code'], self.output._format_value_c(self.cst))
Expand Down Expand Up @@ -192,7 +193,7 @@ def __init__(self, value, name, inout_type=None):
def execute(self, **kwargs):
MLAction.execute(self, **kwargs)
if self.name_var not in kwargs:
raise KeyError(
raise KeyError( # pragma: no cover
"Unable to find variable name '{0}'".format(self.name_var))
return self.output.validate(kwargs[self.name_var])

Expand All @@ -212,7 +213,8 @@ def _export_json(self, hook=None, result_name=None):
@AutoAction.cache
def _export_c(self, hook=None, result_name=None):
if result_name is None:
raise ValueError("result_name must not be None")
raise ValueError(
"result_name must not be None") # pragma: no cover
dc = self.output._export_c(hook='typeref', result_name=result_name)
res = "{0} = {1};".format(dc['code'], self.name_var)
return {'code': res, 'result_name': result_name}
Expand All @@ -231,7 +233,7 @@ def __init__(self, name, output, *acts):
"""
for act in acts:
if not isinstance(act, MLAction):
raise TypeError(
raise TypeError( # pragma: no cover
"All element of acts must be MLAction not '{0}'.".format(type(act)))
MLAction.__init__(self, [act.output for act in acts],
output, name, children=acts)
Expand All @@ -246,7 +248,8 @@ def _optional_parameters(self):
@AutoAction.cache
def _export_c(self, hook=None, result_name=None):
if result_name is None:
raise ValueError("result_name must not be None")
raise ValueError(
"result_name must not be None") # pragma: no cover
dcf = MLAction._export_c(self, hook=hook, result_name=result_name)
rows = [dcf['code']]
fcall = ", ".join(dcf['child_names'])
Expand Down Expand Up @@ -278,16 +281,17 @@ def __init__(self, act1, act2, name):
@param name operator name
"""
if not isinstance(act1, MLAction):
raise TypeError("act1 must be MLAction.")
raise TypeError("act1 must be MLAction.") # pragma: no cover
if not isinstance(act2, MLAction):
raise TypeError("act2 must be MLAction.")
raise TypeError("act2 must be MLAction.") # pragma: no cover
MLAction.__init__(self, [act1.output, act2.output], act2.output, name,
children=[act1, act2])

@AutoAction.cache
def _export_c(self, hook=None, result_name=None):
if result_name is None:
raise ValueError("result_name must not be None")
raise ValueError(
"result_name must not be None") # pragma: no cover
dc = MLAction._export_c(self, hook=hook, result_name=result_name)
rows = [dc['code']]
dc2 = self.output._export_c(hook='type')
Expand Down Expand Up @@ -336,9 +340,9 @@ def __init__(self, act1, act2):
@param act2 second element
"""
if not isinstance(act1, MLAction):
raise TypeError("act1 must be MLAction.")
raise TypeError("act1 must be MLAction.") # pragma: no cover
if not isinstance(act2, MLAction):
raise TypeError("act2 must be MLAction.")
raise TypeError("act2 must be MLAction.") # pragma: no cover
n1 = 1 if isinstance(
act1.output, MLNumTypeFloat32) else act1.output.dim[0]
n2 = 1 if isinstance(
Expand Down Expand Up @@ -392,16 +396,16 @@ def __init__(self, cond, act1, act2, check_type=True, comment=None):
@param comment comment
"""
if not isinstance(act1, MLAction):
raise TypeError("act1 must be MLAction.")
raise TypeError("act1 must be MLAction.") # pragma: no cover
if not isinstance(act2, MLAction):
raise TypeError("act2 must be MLAction.")
raise TypeError("act2 must be MLAction.") # pragma: no cover
if not isinstance(cond, MLAction):
raise TypeError("cond must be MLAction.")
raise TypeError("cond must be MLAction.") # pragma: no cover
if not isinstance(cond.output, MLNumTypeBool):
raise TypeError(
raise TypeError( # pragma: no cover
"No boolean condition {0}".format(type(cond.output)))
if check_type and type(act1.output) != type(act2.output):
raise TypeError("Not the same input type {0} != {1}".format(
raise TypeError("Not the same input type {0} != {1}".format( # pragma: no cover
type(act1.output), type(act2.output)))
MLAction.__init__(self, [cond.output, act1.output, act2.output], act2.output, "if",
children=[cond, act1, act2])
Expand All @@ -424,7 +428,8 @@ def execute(self, **kwargs):
@AutoAction.cache
def _export_c(self, hook=None, result_name=None):
if result_name is None:
raise ValueError("result_name must not be None")
raise ValueError(
"result_name must not be None") # pragma: no cover
dc = MLAction._export_c(self, hook=hook, result_name=result_name)
rows = [dc['code']]
dc2 = self.output._export_c(hook='type')
Expand Down Expand Up @@ -454,9 +459,11 @@ def execute(self, **kwargs):
@AutoAction.cache
def _export_c(self, hook=None, result_name=None):
if len(self.children) != 1:
raise ValueError("Only one result can be returned.")
raise ValueError(
"Only one result can be returned.") # pragma: no cover
if result_name is None:
raise ValueError("result_name must not be None")
raise ValueError(
"result_name must not be None") # pragma: no cover
dc = self.children[0]._export_c(hook=hook, result_name=result_name)
if not dc['cache']:
code = dc['code']
Expand Down Expand Up @@ -491,11 +498,13 @@ def execute(self, **kwargs):
@AutoAction.cache
def _export_c(self, hook=None, result_name=None):
if result_name is None:
raise ValueError("result_name must not be None")
raise ValueError(
"result_name must not be None") # pragma: no cover
if len(self.children) != 1:
raise ValueError("The function must return one result.")
if result_name[-1] == '0':
raise ValueError(
"The function must return one result.") # pragma: no cover
if result_name[-1] == '0':
raise ValueError( # pragma: no cover
"result_name '{0}' cannot end with 0.".format(result_name))

vars = {v.name: v for v in self.enumerate_variables()}
Expand Down

0 comments on commit 8d49c91

Please sign in to comment.