コード例 #1
0
 def test_einsum_float32(self):
     np_ein = lambda *x, equation=None: numpy.einsum(equation, *x)
     x = numpy.array([[0.5, 0.1], [-0.5, -0.1]], dtype=numpy.float32)
     self.common_testn(
         (x, x),
         np_ein,
         nxnpy.einsum,  # pylint: disable=E1101
         FctVersion((numpy.float32, numpy.float32), ('ab,bc->ac', )),
         equation='ab,bc->ac')
     self.common_testn(
         (x, x, x),
         np_ein,
         nxnpy.einsum,  # pylint: disable=E1101
         FctVersion((numpy.float32, numpy.float32, numpy.float32),
                    ('ab,bc,cd->acd', )),
         equation='ab,bc,cd->acd')
     self.common_test1(
         x,
         np_ein,
         nxnpy.einsum,  # pylint: disable=E1101
         FctVersion((numpy.float32, ), ('ii', )),
         equation='ii')
     self.common_test1(
         x,
         np_ein,
         nxnpy.einsum,  # pylint: disable=E1101
         FctVersion((numpy.float32, ), ('ij->ji', )),
         equation='ij->ji')
コード例 #2
0
ファイル: test_wrappers.py プロジェクト: sdpython/mlprodict
 def test_signature_optional2(self):
     # sig, args, kwargs, version
     f32 = numpy.float32
     sigs = [
         # 2: optional
         (NDArrayType(("all", "all", "all"), n_optional=2), ['X', 'Y',
                                                             'Z'], {},
          FctVersion((f32, f32, f32), None)),
         (NDArrayType(("all", "all", "all"), n_optional=2), ['X', 'Y'], {},
          FctVersion((f32, f32), None)),
         (NDArrayType(("all", "all", "all"),
                      n_optional=2), ['X'], {}, FctVersion((f32, ), None)),
     ]
     expected = [
         ("[('X', FloatTensorType(shape=[])), ('Y', FloatTensorType(shape=[])), "
          "('Z', FloatTensorType(shape=[]))]",
          "[('y', FloatTensorType(shape=[]))]", 2),
         ("[('X', FloatTensorType(shape=[])), ('Y', FloatTensorType(shape=[]))]",
          "[('y', FloatTensorType(shape=[]))]", 1),
         ("[('X', FloatTensorType(shape=[]))]",
          "[('y', FloatTensorType(shape=[]))]", 0),
     ]
     self.assertEqual(len(expected), len(sigs))
     for i, (sigt, expe) in enumerate(zip(sigs, expected)):  # pylint: disable=W0612
         sig, args, kwargs, version = sigt
         inputs, kwargs, outputs, optional, n_vars = sig.get_inputs_outputs(
             args, kwargs, version)
         self.assertIsInstance(kwargs, (OrderedDict, dict))
         si, so, opt = expe
         self.assertEqual(optional, opt)
         self.assertEqual(si, str(inputs))
         self.assertEqual(so, str(outputs))
         self.assertEqual(n_vars, 0)
コード例 #3
0
 def test_version(self):
     version = FctVersion(None, None)
     r = repr(version)
     self.assertEqual(r, "FctVersion(None, None)")
     self.assertEqual(version.args, None)
     self.assertEqual(version.kwargs, None)
     self.assertEqual(len(version), 0)
     self.assertEqual(version.as_tuple(), tuple())
     self.assertEqual(version.as_string(), '_')
コード例 #4
0
    def test_version_dictionary(self):
        def fct():
            pass

        keys = [
            FctVersion(None, None),
            FctVersion((numpy.float32, ), ('constant', )),
            FctVersion((numpy.float32, ), (fct, ))
        ]
        dc = {}
        for k in keys:
            dc[k] = None
        self.assertEqual(len(dc), len(keys))
コード例 #5
0
    def test_version_exc(self):
        self.assertRaise(
            lambda: FctVersion([], None)._check_(),  # pylint: disable=W0212
            TypeError)
        self.assertRaise(
            lambda: FctVersion(None, [])._check_(),  # pylint: disable=W0212
            TypeError)
        version = FctVersion(None, None)

        def do(v):
            v.args = []

        self.assertRaise(lambda: do(version), AttributeError)
コード例 #6
0
 def test_version_string(self):
     version = FctVersion((numpy.float32, ), ('constant', ))
     self.assertEqual(version.as_string(), 'float32___constant')
     version = FctVersion((numpy.float32, ), None)
     self.assertEqual(version.as_string(), 'float32__')
     version = FctVersion((numpy.float32, ), None)
     self.assertEqual(repr(version), "FctVersion((numpy.float32,), None)")
コード例 #7
0
 def test_expand_dims_float32(self):
     x = numpy.array([[0.5, 0.1], [-0.5, -0.1]], dtype=numpy.float32)
     self.common_test1(x,
                       numpy.expand_dims,
                       nxnpy.expand_dims,
                       FctVersion((numpy.float32, ), (0, )),
                       axis=0)
コード例 #8
0
 def common_test_clas(self,
                      x,
                      model_class,
                      nxfct,
                      key,
                      dtype_out=None,
                      ort=True,
                      **kwargs):
     X, y = make_classification(100,
                                n_informative=2,
                                n_features=2,
                                n_redundant=0)
     if not isinstance(key, tuple):
         key = (key, )
     model = model_class().fit(X, y)
     key = FctVersion(key, (model, ))
     expected = model.predict(x), model.predict_proba(x)
     got = nxfct(x, model=model)
     self.assertIn(key, nxfct.signed_compiled)
     got = nxfct[key](x)
     compiled = nxfct[key].compiled
     self.assertEqualArray(expected[0], got[0])
     self.assertEqualArray(expected[1], got[1])
     if ort:
         onx = compiled.onnx_
         rt2 = OnnxInference(onx, runtime="onnxruntime1")
         inputs = rt2.input_names
         outputs = rt2.output_names
         data = {inputs[0]: x}
         got2 = rt2.run(data)
         self.assertEqualArray(expected[0], got2[outputs[0]], decimal=6)
         self.assertEqualArray(expected[1], got2[outputs[1]], decimal=6)
コード例 #9
0
ファイル: test_wrappers.py プロジェクト: sdpython/mlprodict
 def test_signature_optional3_kwargs_more(self):
     # sig, args, kwargs, version
     f32 = numpy.float32
     i64 = numpy.int64
     sigs = [
         (NDArrayType(("T:all", numpy.int64, 'T'),
                      n_optional=1), ['X', 'I', 'Y'], {
                          'mode': 'constant'
                      }, FctVersion((f32, i64), ('constant', ))),
     ]
     expected = [
         ("[('X', FloatTensorType(shape=[])), ('I', Int64TensorType(shape=[]))]",
          "[('y', FloatTensorType(shape=[]))]", 0),
     ]
     self.assertEqual(len(expected), len(sigs))
     for i, (sigt, expe) in enumerate(zip(sigs, expected)):  # pylint: disable=W0612
         sig, args, kwargs, version = sigt
         inputs, kwargs, outputs, optional, n_vars = sig.get_inputs_outputs(
             args, kwargs, version)
         self.assertIsInstance(kwargs, (OrderedDict, dict))
         si, so, opt = expe
         self.assertEqual(optional, opt)
         self.assertEqual(si, str(inputs))
         self.assertEqual(so, str(outputs))
         self.assertEqual(n_vars, 0)
コード例 #10
0
    def test_pad_float32(self):
        def custom_pad(x, pads, constant_value=None, mode='constant'):
            return onnx_pad(x, pads, constant_value=constant_value, mode=mode)

        kwargs = [{
            'mode': 'constant'
        }, {
            'mode': 'edge'
        }, {
            'mode': 'reflect'
        }, {}]
        for kw in kwargs:
            with self.subTest(kw=kw):
                x = numpy.array([[1.0, 1.2], [2.3, 3.4], [4.5, 5.7]],
                                dtype=numpy.float32)
                pads = numpy.array([0, 2, 0, 0], dtype=numpy.int64)
                value = numpy.array([1.77], dtype=numpy.float32)
                self.common_testn(
                    (x, pads, value),
                    custom_pad,
                    nxnpy.pad,
                    FctVersion((numpy.float32, numpy.int64, numpy.float32),
                               (kw.get('mode', 'constant'), )),
                    **kw,
                    ort=kw.get('mode', 'constant') != 'reflect')
コード例 #11
0
 def common_test1(self,
                  x,
                  npfct,
                  nxfct,
                  key,
                  dtype_out=None,
                  ort=True,
                  **kwargs):
     if not isinstance(key, tuple):
         key = (key, )
     if not isinstance(key, FctVersion):
         key = FctVersion(key, None)
     expected = npfct(x, **kwargs)
     got = nxfct(x, **kwargs)
     self.assertIn(key, nxfct.signed_compiled)
     got = nxfct[key](x)
     compiled = nxfct[key].compiled
     self.assertEqualArray(expected, got)
     if dtype_out is not None:
         self.assertEqual(got.dtype, dtype_out)
     if ort:
         onx = compiled.onnx_
         rt2 = OnnxInference(onx, runtime="onnxruntime1")
         inputs = rt2.input_names
         outputs = rt2.output_names
         data = {inputs[0]: x}
         got2 = rt2.run(data)[outputs[0]]
         self.assertEqualArray(expected, got2, decimal=6)
コード例 #12
0
 def test_amin_float32(self):
     kwargs = [{'axis': 0}, {}, {'axis': 1}]
     for kw in kwargs:
         with self.subTest(kw=kw):
             x = numpy.array([[-6.1, 5], [-3.5, 7.8]], dtype=numpy.float32)
             sig = FctVersion((numpy.float32, ), (kw.get('axis', None), 0))
             self.common_test1(x, numpy.amin, nxnpy.amin, sig, **kw)
コード例 #13
0
ファイル: test_wrappers.py プロジェクト: sdpython/mlprodict
 def test_signature(self):
     # sig, args, kwargs, version
     f32 = numpy.float32
     i64 = numpy.int64
     bbb = numpy.bool_
     sigs = [
         # 0
         (NDArraySameTypeSameShape("all"), ['X'], {},
          FctVersion((f32, ), None)),
         (NDArraySameTypeSameShape("floats"), ['X'], {},
          FctVersion((f32, ), None)),
         (NDArrayType("all_int"), ['X'], {}, FctVersion((i64, ), None)),
         (NDArrayType(("bool", "T:all"), dtypes_out=('T', )), ['X',
                                                               'C'], {},
          FctVersion((bbb, f32), None)),
         (NDArrayType(
             ("all", "ints")), ['X', 'I'], {}, FctVersion((f32, i64),
                                                          None)),
         (NDArrayType("T:all", "T"), ['X'], {}, FctVersion((f32, ), None)),
         (NDArrayTypeSameShape("all_bool"), ['B'], {},
          FctVersion((bbb, ), None)),
         (NDArrayType(("T:all", "ints"),
                      ("T", (numpy.int64, ))), ['X', 'I'], {},
          FctVersion((f32, i64), None)),
     ]
     expected = [
         ("[('X', FloatTensorType(shape=[]))]",
          "[('y', FloatTensorType(shape=[]))]"),
         ("[('X', FloatTensorType(shape=[]))]",
          "[('y', FloatTensorType(shape=[]))]"),
         ("[('X', Int64TensorType(shape=[]))]",
          "[('y', Int64TensorType(shape=[]))]"),
         ("[('X', BooleanTensorType(shape=[])), ('C', FloatTensorType(shape=[]))]",
          "[('y', FloatTensorType(shape=[]))]"),
         ("[('X', FloatTensorType(shape=[])), ('I', Int64TensorType(shape=[]))]",
          "[('y', FloatTensorType(shape=[]))]"),
         ("[('X', FloatTensorType(shape=[]))]",
          "[('y', FloatTensorType(shape=[]))]"),
         ("[('B', BooleanTensorType(shape=[]))]",
          "[('y', BooleanTensorType(shape=[]))]"),
         ("[('X', FloatTensorType(shape=[])), ('I', Int64TensorType(shape=[]))]",
          "[('y', FloatTensorType(shape=[])), ('z', Int64TensorType(shape=[]))]"
          ),
     ]
     self.assertEqual(len(expected), len(sigs))
     for i, (sigt, expe) in enumerate(zip(sigs, expected)):  # pylint: disable=W0612
         sig, args, kwargs, version = sigt
         inputs, kwargs, outputs, optional, n_vars = sig.get_inputs_outputs(
             args, kwargs, version)
         self.assertEqual(optional, 0)
         self.assertIsInstance(kwargs, (OrderedDict, dict))
         si, so = expe
         self.assertEqual(si, str(inputs))
         self.assertEqual(so, str(outputs))
         self.assertEqual(n_vars, False)
コード例 #14
0
 def test_concat_float32(self):
     npc = lambda *x, axis=None: numpy.vstack(x)
     x = numpy.array([[0.5, 0.1], [-0.5, -0.1]], dtype=numpy.float32)
     self.common_testn(
         (x, x),
         npc,
         nxnpy.concat,  # pylint: disable=E1101
         FctVersion((numpy.float32, numpy.float32), (0, )),
         axis=0)
コード例 #15
0
 def test_arange_float32(self):
     kwargs = [{}, {'step': 1}, {'step': 3}]
     for kw in kwargs:
         with self.subTest(kw=kw):
             begin = numpy.array([5], dtype=numpy.int64)
             stop = numpy.array([20], dtype=numpy.int64)
             sig = FctVersion((numpy.int64, numpy.int64),
                              (kw.get('step', 1), ))
             self.common_testn((begin, stop), numpy.arange, nxnpy.arange,
                               sig, **kw)
コード例 #16
0
 def test_transpose_float32(self):
     np_tr = lambda x, perm=None: numpy.transpose(x, perm)
     x = numpy.array([[0.5, 0.1], [-0.5, -0.1], [1, 1]],
                     dtype=numpy.float32)
     self.common_test1(
         x,
         np_tr,
         nxnpy.transpose,  # pylint: disable=E1101
         FctVersion((numpy.float32, ), ((1, 0), )),
         perm=(1, 0))
コード例 #17
0
ファイル: test_wrappers.py プロジェクト: sdpython/mlprodict
 def test_signature_optional_errors_runtime(self):
     # sig, args, kwargs, version
     f32 = numpy.float32
     i64 = numpy.int64
     sigs = [
         (NDArrayType(("T:all", numpy.int64, 'T'),
                      n_optional=1), ['X', 'I', 'R', 'T'], {
                          'mode': 'constant'
                      }, FctVersion((f32, i64), ('constant', ))),
         (NDArrayType(("T:all", numpy.int64), 'T',
                      n_optional=1), ['X', 'I', 'Y'], {},
          FctVersion((f32, i64, f32), None)),
         (NDArrayType(("T:all", numpy.int64), 'T',
                      n_optional=1), ['X', 'I', 'Y'], {},
          FctVersion((f32, i64, f32, f32), None)),
     ]
     for i, sigt in enumerate(sigs):  # pylint: disable=W0612
         sig, args, kwargs, version = sigt
         self.assertRaise(
             lambda: sig.get_inputs_outputs(  # pylint: disable=W0640
                 args, kwargs, version),  # pylint: disable=W0640
             RuntimeError)
コード例 #18
0
 def common_testn(self, xs, npfct, nxfct, key, ort=True, **kwargs):
     if not isinstance(key, FctVersion):
         key = FctVersion(key, None)
     if isinstance(xs, numpy.ndarray):
         xts = [xs]
     else:
         xts = xs
     expected = npfct(*xts, **kwargs)
     got = nxfct(*xts, **kwargs)
     self.assertIn(key, nxfct.signed_compiled)
     got = nxfct[key](*xts)
     compiled = nxfct[key].compiled
     self.assertEqualArray(expected, got)
     if ort:
         onx = compiled.onnx_
         rt2 = OnnxInference(onx, runtime="onnxruntime1")
         inputs = rt2.input_names
         outputs = rt2.output_names
         data = {n: x for n, x in zip(inputs, xts)}
         got2 = rt2.run(data)[outputs[0]]
         self.assertEqualArray(expected, got2, decimal=6)
コード例 #19
0
    def test_compress_float32(self):
        x = numpy.array([[-6.1, 5, 6], [-3.5, 7.8, 5]], dtype=numpy.float32)
        cond = numpy.array([False, True])
        exp = numpy.compress(cond, x, axis=0)
        got = nxnpy.compress(cond, x, axis=0)
        self.assertEqualArray(got, exp)
        cond = numpy.array([False, True, False])
        exp = numpy.compress(cond, x, axis=1)
        got = nxnpy.compress(cond, x, axis=1)
        self.assertEqualArray(got, exp)

        axes = [0, 1, None]
        for a in axes:
            with self.subTest(axis=a):
                x = numpy.array([[-6.1, 5], [-3.5, 7.8]], dtype=numpy.float32)
                cond = numpy.array([False, True])
                self.common_testn((cond, x),
                                  numpy.compress,
                                  nxnpy.compress,
                                  FctVersion((numpy_bool, numpy.float32),
                                             (a, )),
                                  axis=a)