예제 #1
0
def _custom_newTable(*args):
    # should only be called via newTable method below
    if (len(args) in [2, 3]) and (isinstance(args[0], int) or isinstance(args[0], float)):
        rows = int(args[0])

        if isinstance(args[1], dict):
            # desired args = (long, map<String, ColumnSource>)
            pydict = args[1]
            java_map = jpy.get_type("java.util.HashMap")()
            for key in pydict:
                java_map.put(key, pydict[key])
            return _java_type_.newTable(rows, java_map)
        elif (len(args) == 3) and isinstance(args[1], list) and isinstance(args[2], list):
            # desired args = (long, List<String>, List<ColumnSource>)
            if _isJavaArray(args[1]):
                names = convertToJavaList(args[1])
            else:
                names = convertToJavaList(jpy.array('java.lang.String', args[1]))
            if _isJavaArray(args[2]):
                sources = convertToJavaList(args[2])
            else:
                sources = convertToJavaList(jpy.array('io.deephaven.engine.table.ColumnSource', args[2]))
            return _java_type_.newTable(rows, names, sources)

    return _java_type_.newTable(*args)
예제 #2
0
파일: face.py 프로젝트: nerddd/Useful_tools
    def extract(self, image, height, width):
        """
        Extract facial features in given image
        return faceFeatures: float[][] to java
        [
          x1, y1, x2, y2,   # bounding box
          score,            # confidence
          feature           # center feature
        ]
        """
        image = self._listToNp(image, height, width)
        face_infos = self._detect(image)
        feat_infos = []
        fpoints = []

        for face_info in face_infos:
            feat_info = face_info['bbox']
            feat_info.append(face_info['score'])
            feat_infos.append(feat_info)
            fpoints.append(face_info['fpoints'])

        features = self.center.extract(image, fpoints)
        for i in xrange(features.shape[0]):
            feat_infos[i].extend(list(features[i]))
            # print feat_infos[i][:6]
            feat_infos[i] = jpy.array('float', feat_infos[i]) # float[]
        return jpy.array(FloatArray, feat_infos)    # float[][]
예제 #3
0
def _custom_charCol(name, *data):
    def makeElementChar(el):
        if el is None:
            return NULL_CHAR
        if isinstance(el, int):
            return el
        if _isStr(el):
            if len(el) < 1:
                return NULL_CHAR
            return ord(el[0])
        try:
            return int(el)
        except ValueError:
            return NULL_CHAR

    # should only be called from charCol() below
    if len(data) < 1:
        raise ValueError("No data provided")
    if len(data) == 1:
        if _isJavaType(data[0]):
            return _java_type_.charCol(name, data[0])
        elif _isStr(data[0]):
            return _java_type_.charCol(name, [ord(char) for char in data[0]])  # NB: map returns an iterable in py3
        elif isinstance(data[0], numpy.ndarray):
            if data[0].dtype == numpy.uint16:
                return _java_type_.charCol(name, jpy.array('char', data[0]))
            elif data[0].dtype in [numpy.int8, numpy.uint8, numpy.int16, numpy.int32, numpy.uint32, numpy.int64, numpy.uint64]:
                # not entirely sure that this is necessary
                return _java_type_.charCol(name, jpy.array('char', data[0].astype(numpy.uint16)))
            elif data[0].dtype == numpy.dtype('U1') and data[0].dtype.name in ['unicode32', 'str32', 'string32', 'bytes32']:
                junk = numpy.copy(data[0])
                junk.dtype = numpy.uint32
                return _java_type_.charCol(name, jpy.array('char', junk.astype(numpy.uint16)))
            elif data[0].dtype == numpy.dtype('S1') and data[0].dtype.name in ['str8', 'string8', 'bytes8']:
                junk = numpy.copy(data[0])
                junk.dtype = numpy.uint8
                return _java_type_.charCol(name, jpy.array('char', junk.astype(numpy.uint16)))
            elif data[0].dtype == numpy.object:
                # do our best
                return _java_type_.charCol(name, jpy.array('char', numpy.array([makeElementChar(el) for el in data[0]], dtype=numpy.uint16)))
            else:
                raise ValueError("Input was an ndarray, expected integer dtype or "
                                 "one character string dtype, and got {}".format(data[0].dtype))
        elif isinstance(data[0], pandas.Series):
            return _custom_charCol(name, data[0].values)
        elif hasattr(data[0], '__iter__'):
            # naively turn it into a numpy array, and see what happens
            try:
                return _custom_charCol(name, numpy.asarray(data[0]))
            except Exception as e:
                logging.error("Attempted converting charCol() input to numpy array and failed.")
                raise e
    else:
        # naively turn it into a numpy array, and see what happens
        try:
            narr = numpy.asarray(data)
            return _custom_charCol(name, narr)
        except Exception as e:
            logging.error("Attempted converting charCol() input to numpy array and failed.")
            raise e
예제 #4
0
def _custom_avroSchemaToColumnDefinitions(schema, mapping:dict = None):
    if mapping is None:
        return _java_type_.avroSchemaToColumnDefinitions(schema)

    field_names = jpy.array('java.lang.String', mapping.keys())
    column_names = jpy.array('java.lang.String', mapping.values())
    mapping = _java_type_.fieldNameMappingFromParallelArrays(field_names, column_names)
    return _java_type_.avroSchemaToColumnDefinitions(schema, mapping)
예제 #5
0
    def do_test_basic_buffer_protocol(self, type, itemsize, values):

        a = jpy.array(type, 4)
        self.assertEqual(len(a), 4)

        a[0] = values[0]
        a[1] = values[1]
        a[2] = values[2]
        a[3] = values[3]

        m = memoryview(a)
        self.assertEqual(len(m), 4)
        self.assertEqual(m.ndim, 1)
        self.assertEqual(m.itemsize, itemsize)
        self.assertEqual(m.shape, (4, ))
        self.assertEqual(m.strides, (itemsize, ))
        self.assertEqual(m.readonly, True)
        if sys.version_info >= (3, 0, 0):
            # Python 2.7: AttributeError: 'memoryview' object has no attribute 'nbytes'
            self.assertEqual(m.nbytes, 4 * itemsize)
            # Python 2.7: AttributeError: 'memoryview' object has no attribute 'contiguous'
            self.assertEqual(m.contiguous, True)
            # Python 2.7: AttributeError: 'memoryview' object has no attribute 'c_contiguous'
            self.assertEqual(m.c_contiguous, True)
        return m
예제 #6
0
    def test_returnIntArray(self):
        fixture = self.Fixture()

        # See https://docs.python.org/2/c-api/buffer.html
        # "An array can only expose its contents via the old-style buffer interface.
        #    This limitation does not apply to Python 3, where memoryview objects can be
        #    constructed from arrays, too."
        # >>> import array
        # >>> a = array.array('i', [1,2,3])
        # >>> m = memoryview(a)
        # TypeError: cannot make memory view because object does not have the buffer interface
        #
        if sys.version_info >= (3, 0, 0):
            a1 = array.array('i', [0, 0, 0])
            a2 = fixture.returnIntArray(a1)
            # AssertionError: array('i', [0, 0, 0]) is not [I(objectRef=0x0778C364)
            self.assertIs(a1, a2)

        if np:
            a1 = np.array([0, 0, 0], dtype='int32')
            a2 = fixture.returnIntArray(a1)
            self.assertIs(a1, a2)

        a1 = jpy.array('int', 3)
        a2 = fixture.returnIntArray(a1)
        self.assertIs(a1, a2)

        a1 = None
        a2 = fixture.returnIntArray(a1)
        self.assertEqual(type(a2), jpy.get_type('[I'))

        a1 = [0, 0, 0]
        a2 = fixture.returnIntArray(a1)
        self.assertEqual(type(a2), jpy.get_type('[I'))
def _arrayColumnSource(array, javaTypeString):
    # Assumed to be called strictly by _convertNdarrayToImmutableSource...no error checking at all performed

    # create a placeholder for our array elements
    data = []

    arrayType = None

    if javaTypeString in _javaTypeToDbarrayType:
        arrayType = _javaTypeToDbarrayType[javaTypeString]
    elif javaTypeString in jpy.dtypes:
        arrayType = 'io.deephaven.db.tables.dbarrays.DbArrayDirect'

    arrayCls = jpy.get_type(arrayType)
    if javaTypeString == 'java.lang.Boolean':
        clsInstance = arrayCls(True).getClass()  # constructor must take an argument...
    else:
        clsInstance = arrayCls().getClass()

    for el in array:
        if el is None:
            data.append(None)
        else:
            javaArray = _makeJavaArray(el, javaTypeString)
            if javaArray is None:
                data.append(None)
            else:
                data.append(arrayCls(javaArray))
    return __ObjectColumnSource__, jpy.array(arrayType,  data), clsInstance
    def test_returnIntArray(self):
        fixture = self.Fixture()

        # See https://docs.python.org/2/c-api/buffer.html
        #   "An array can only expose its contents via the old-style buffer interface.
        #    This limitation does not apply to Python 3, where memoryview objects can be
        #    constructed from arrays, too."
        # >>> import array
        # >>> a = array.array('i', [1,2,3])
        # >>> m = memoryview(a)
        # TypeError: cannot make memory view because object does not have the buffer interface
        #
        if sys.version_info >= (3, 0, 0):
            a1 = array.array('i', [0, 0, 0])
            a2 = fixture.returnIntArray(a1)
            # AssertionError: array('i', [0, 0, 0]) is not [I(objectRef=0x0778C364)
            self.assertIs(a1, a2)

        if np:
            a1 = np.array([0, 0, 0], dtype='int32')
            a2 = fixture.returnIntArray(a1)
            self.assertIs(a1, a2)

        a1 = jpy.array('int', 3)
        a2 = fixture.returnIntArray(a1)
        self.assertIs(a1, a2)

        a1 = None
        a2 = fixture.returnIntArray(a1)
        self.assertEqual(type(a2), jpy.get_type('[I'))

        a1 = [0, 0, 0]
        a2 = fixture.returnIntArray(a1)
        self.assertEqual(type(a2), jpy.get_type('[I'))
예제 #9
0
파일: jpy_rt_test.py 프로젝트: bcdev/jpy
    def test_HashMap(self):
        f = self.File('/usr/local/bibo')
        fa = jpy.array('java.io.File', 2)
        fa[0] = f
        fa[1] = f

        hash_map = self.HashMap()
        hash_map.put(0, 'A')
        hash_map.put(1, 12)
        hash_map.put(2, 3.4)
        hash_map.put(3, f)
        hash_map.put(4, fa)

        self.assertEqual(hash_map.size(), 5)
        self.assertEqual(hash_map.get(0), 'A')
        self.assertEqual(hash_map.get(1), 12)
        self.assertEqual(hash_map.get(2), 3.4)
        self.assertEqual(hash_map.get(3), f)
        self.assertEqual(type(hash_map.get(3)), type(f))
        self.assertEqual(hash_map.get(4), fa)

        hash_map = self.HashMap(hash_map)
        self.assertEqual(hash_map.size(), 5)
        self.assertEqual(hash_map.get(0), 'A')
        self.assertEqual(hash_map.get(1), 12)
        self.assertEqual(hash_map.get(2), 3.4)
        self.assertEqual(hash_map.get(3), f)
        self.assertEqual(type(hash_map.get(3)), type(f))
        self.assertEqual(hash_map.get(4), fa)
예제 #10
0
파일: jpy_array_test.py 프로젝트: bcdev/jpy
    def do_test_basic_buffer_protocol(self, type, itemsize, values):

        a = jpy.array(type, 4)
        self.assertEqual(len(a), 4)

        a[0] = values[0]
        a[1] = values[1]
        a[2] = values[2]
        a[3] = values[3]

        m = memoryview(a)
        self.assertEqual(len(m), 4)
        self.assertEqual(m.ndim, 1)
        self.assertEqual(m.itemsize, itemsize)
        self.assertEqual(m.shape, (4,))
        self.assertEqual(m.strides, (itemsize,))
        self.assertEqual(m.readonly, True)
        if sys.version_info >= (3, 0, 0):
            # Python 2.7: AttributeError: 'memoryview' object has no attribute 'nbytes'
            self.assertEqual(m.nbytes, 4 * itemsize)
            # Python 2.7: AttributeError: 'memoryview' object has no attribute 'contiguous'
            self.assertEqual(m.contiguous, True)
            # Python 2.7: AttributeError: 'memoryview' object has no attribute 'c_contiguous'
            self.assertEqual(m.c_contiguous, True)
        return m
예제 #11
0
 def test_array_item_del(self):
     Integer = jpy.get_type('java.lang.Integer')
     a = jpy.array(Integer, 3)
     try:
         del a[1]
     except RuntimeError as err:
         self.assertEqual(err.args[0], 'cannot delete items of Java arrays')
예제 #12
0
파일: jpy_array_test.py 프로젝트: bcdev/jpy
 def test_array_item_del(self):
     Integer = jpy.get_type('java.lang.Integer')
     a = jpy.array(Integer, 3)
     try:
         del a[1]
     except RuntimeError as err:
         self.assertEqual(err.args[0], 'cannot delete items of Java arrays')
예제 #13
0
def array(dtype: DType,
          seq: Sequence,
          remap: Callable[[Any], Any] = None) -> jpy.JType:
    """ Creates a Java array of the specified data type populated with values from a sequence.

    Note:
        this method does unsafe casting, meaning precision and values might be lost with down cast

    Args:
        dtype (DType): the component type of the array
        seq (Sequence): a sequence of compatible data, e.g. list, tuple, numpy array, Pandas series, etc.
        remap (optional): a callable that takes one value and maps it to another, for handling the translation of
            special DH values such as NULL_INT, NAN_INT between Python and the DH engine

    Returns:
        a Java array

    Raises:
        DHError
    """
    try:
        if remap:
            if not callable(remap):
                raise ValueError("Not a callable")
            seq = [remap(v) for v in seq]
        else:
            if isinstance(seq, str) and dtype == char:
                return array(char, seq, remap=ord)

        return jpy.array(dtype.j_type, seq)
    except Exception as e:
        raise DHError(e,
                      f"failed to create a Java {dtype.j_name} array.") from e
예제 #14
0
  def test_pyobject_unwrap_via_array(self):
    # Very similar to test_pyobject_unwrap, but we are doing the unwrapping via array
    class CustomClass:
      def __init__(self):
        pass

    obj = CustomClass()
    obj_id = id(obj)
    self.assertEqual(get_refcount(obj_id), 1)

    obj_in_array = jpy.array('org.jpy.PyObject', [obj])
    self.assertEqual(get_refcount(obj_id), 2)

    extracted_obj = obj_in_array[0]
    self.assertTrue(obj is extracted_obj)
    self.assertEqual(get_refcount(obj_id), 3)

    del extracted_obj
    self.assertEqual(get_refcount(obj_id), 2)

    del obj
    self.assertEqual(get_refcount(obj_id), 1)

    # Note: the ref count will not decrease until Java GCs and PyObject does the decRef.
    # While this del + check is racy, it is probably very rare to fail here.
    del obj_in_array
    self.assertEqual(get_refcount(obj_id), 1)
예제 #15
0
 def do_test_basic_array_protocol_with_length(self, type, initial, expected):
     #*** jtypes extension ***#
     with self.assertRaises(ValueError, msg="ValueError expected") as e:
         jpy.array(type, -3)
     self.assertEqual(str(e.exception),
          "array: argument 2 (init) must be either an integer array length or any sequence")
     #*** jtypes extension ***#
     a = jpy.array(type, 3)
     self.assertEqual(len(a), 3)
     self.assertEqual(a[0], initial[0])
     self.assertEqual(a[1], initial[1])
     self.assertEqual(a[2], initial[2])
     a[0] = expected[0]
     a[1] = expected[1]
     a[2] = expected[2]
     return a
예제 #16
0
    def test_HashMap(self):
        f = self.File('/usr/local/bibo')
        fa = jpy.array('java.io.File', 2)
        fa[0] = f
        fa[1] = f

        hash_map = self.HashMap()
        hash_map.put(0, 'A')
        hash_map.put(1, 12)
        hash_map.put(2, 3.4)
        hash_map.put(3, f)
        hash_map.put(4, fa)

        self.assertEqual(hash_map.size(), 5)
        self.assertEqual(hash_map.get(0), 'A')
        self.assertEqual(hash_map.get(1), 12)
        self.assertEqual(hash_map.get(2), 3.4)
        self.assertEqual(hash_map.get(3), f)
        self.assertEqual(type(hash_map.get(3)), type(f))
        self.assertEqual(hash_map.get(4), fa)

        hash_map = self.HashMap(hash_map)
        self.assertEqual(hash_map.size(), 5)
        self.assertEqual(hash_map.get(0), 'A')
        self.assertEqual(hash_map.get(1), 12)
        self.assertEqual(hash_map.get(2), 3.4)
        self.assertEqual(hash_map.get(3), f)
        self.assertEqual(type(hash_map.get(3)), type(f))
        self.assertEqual(hash_map.get(4), fa)
예제 #17
0
 def __init__(self):
     self.jpy_boolean_array_type = type(jpy.array('boolean', []))
     self.jpy_char_array_type = type(jpy.array('char', []))
     self.jpy_byte_array_type = type(jpy.array('byte', []))
     self.jpy_short_array_type = type(jpy.array('short', []))
     self.jpy_int_array_type = type(jpy.array('int', []))
     self.jpy_long_array_type = type(jpy.array('long', []))
     self.jpy_float_array_type = type(jpy.array('float', []))
     self.jpy_double_array_type = type(jpy.array('double', []))
예제 #18
0
 def test_toReproduceAndFixIssue54(self):
     String = jpy.get_type('java.lang.String')
     Arrays = jpy.get_type('java.util.Arrays')
     a = jpy.array(String, ['A', 'B', 'C'])
     # jpy.diag.flags = jpy.diag.F_METH
     s = Arrays.toString(a)
     # jpy.diag.flags = 0
     # without the fix, we get str(s) = "java.lang.String@xxxxxx"
     self.assertEqual(str(s), '[A, B, C]')
예제 #19
0
 def test_toReproduceAndFixIssue54(self):
     String = jpy.get_type('java.lang.String')
     Arrays = jpy.get_type('java.util.Arrays')
     a = jpy.array(String, ['A', 'B', 'C'])
     # jpy.diag.flags = jpy.diag.F_METH
     s = Arrays.toString(a)
     # jpy.diag.flags = 0
     # without the fix, we get str(s) = "java.lang.String@xxxxxx"
     self.assertEqual(str(s), '[A, B, C]')
예제 #20
0
 def do_test_basic_array_protocol_with_length(self, type, initial, expected):
     a = jpy.array(type, 3)
     self.assertEqual(len(a), 3)
     self.assertEqual(a[0], initial[0])
     self.assertEqual(a[1], initial[1])
     self.assertEqual(a[2], initial[2])
     a[0] = expected[0]
     a[1] = expected[1]
     a[2] = expected[2]
     return a
예제 #21
0
def _handleIntTypeColumn(typ, func, name, *data):
    if len(data) < 1:
        raise ValueError("No data provided")
    if len(data) == 1 and isinstance(data[0], numpy.ndarray):
        if data[0].dtype in _intTypeMapping[typ]:
            return func(name, jpy.array(typ, data[0]))
        elif data[0].dtype in _intTypes:
            return func(name, jpy.array(typ, data[0].astype(_intTypeMapping[typ][0])))
        elif data[0].dtype in _floatTypes:
            boolc = numpy.isnan(data[0])
            junk = data[0].astype(_intTypeMapping[typ][0])
            junk[boolc] = _nullValues[typ]
            return func(name, jpy.array(typ, junk))
        else:
            raise ValueError("Incompatible numpy dtype ({}) for {} array".format(data[0].dtype, typ))
    elif len(data) == 1 and isinstance(data[0], pandas.Series):
        return _handleIntTypeColumn(typ, func, name, data[0].values)
    else:
        return func(name, *data)
예제 #22
0
def _make_input_column(col: str, np_array: np.ndarray) -> InputColumn:
    """ Creates a InputColumn with the given column name and the numpy array. """
    dtype = dtypes.from_np_dtype(np_array.dtype)
    if dtype == dtypes.bool_:
        bytes_ = np_array.astype(dtype=np.int8)
        j_bytes = dtypes.array(dtypes.byte, bytes_)
        np_array = _JPrimitiveArrayConversionUtility.translateArrayByteToBoolean(j_bytes)

    if dtype == dtypes.DateTime:
        longs = jpy.array('long', np_array.astype('datetime64[ns]').astype('int64'))
        np_array = _JPrimitiveArrayConversionUtility.translateArrayLongToDateTime(longs)

    return InputColumn(name=_to_column_name(col), data_type=dtype, input_data=np_array)
예제 #23
0
def _custom_col(name, *data):
    # should only be called from col() below
    if len(data) < 1:
        raise ValueError("No data provided")
    if len(data) == 1:
        if isinstance(data[0], int) or isinstance(data[0], float) or isinstance(data[0], bool) or \
                isinstance(data[0], datetime) or isinstance(data[0], date):
            return _custom_col(name, numpy.asarray(data))
        elif _isJavaType(data[0]):
            return _java_type_.col(name, data[0])
        elif _isStr(data[0]):
            return _java_type_.col(name, jpy.array('java.lang.String', data))
        elif isinstance(data[0], numpy.ndarray) or isinstance(data[0], pandas.Series) \
                or isinstance(data[0], pandas.Categorical):
            arr = makeJavaArray(data[0], name)
            dimension, basic_type = _parseJavaArrayType(arr)
            if dimension == 0:  # this is an empty array
                return _java_type_.col(name, [])
            if dimension == 1 and basic_type in _boxedArrayTypes:
                # it's supposed to be boxed. This is silly.
                return _java_type_.col(name, jpy.array(_boxedArrayTypes[basic_type], arr))
            return _java_type_.col(name, arr)
        elif isinstance(data[0], list) or isinstance(data[0], tuple):
            if len(data[0]) < 1:
                return _java_type_.col(name, [])
            # naively try to turn it into a numpy array and send it through
            return _custom_col(name, numpy.asarray(data[0]))
        else:
            raise ValueError("Encountered unexpected type {}".format(type(data[0])))
    else:
        if isinstance(data[0], int) or isinstance(data[0], float):
            # naively try to turn it into a numpy array and send it through
            return _custom_col(name, numpy.asarray(data))
        elif _isJavaType(data[0]):
            # push it straight through
            return _java_type_.col(name, *data)
        else:
            # naively try to turn it into a numpy array and send it through
            return _custom_col(name, numpy.asarray(data))
예제 #24
0
 def test_leak(self):
     '''
     This isn't a very good "unit"-test - the failure of this test depends
     on the amount of RAM and specifics of the OS. On my machine I've been
     able to demonstrate failure with the following constants.
     '''
     # skip the test unless you need to stress test array release logic
     if True:
         return
     j_int_array = jpy.array('int', range(100000))
     # ensure that the bufferExportCount doesn't go to 0
     keep_around = memoryview(j_int_array)
     for i in range(1000000):
         memory_view = memoryview(j_int_array)
예제 #25
0
    def test_modifyAndReturnIntArray(self):
        fixture = self.Fixture()

        # See https://docs.python.org/2/c-api/buffer.html
        # "An array can only expose its contents via the old-style buffer interface.
        #    This limitation does not apply to Python 3, where memoryview objects can be
        #    constructed from arrays, too."
        # >>> import array
        # >>> a = array.array('i', [1,2,3])
        # >>> m = memoryview(a)
        # TypeError: cannot make memory view because object does not have the buffer interface
        #
        if sys.version_info >= (3, 0, 0):
            a1 = array.array('i', [0, 0, 0])
            # Python 2.7: TypeError: must be impossible<bad format char>, not bool
            a2 = fixture.modifyAndReturnIntArray(a1, 16, 17, 18)
            self.assertIs(a1, a2)
            self.assertEqual(a2[0], 16)
            self.assertEqual(a2[1], 17)
            self.assertEqual(a2[2], 18)

        if np:
            a1 = np.array([0, 0, 0], dtype='int32')
            a2 = fixture.modifyAndReturnIntArray(a1, 10, 11, 12)
            self.assertIs(a1, a2)
            self.assertEqual(a2[0], 10)
            self.assertEqual(a2[1], 11)
            self.assertEqual(a2[2], 12)

        a1 = jpy.array('int', 3)
        a2 = fixture.modifyAndReturnIntArray(a1, 16, 17, 18)
        self.assertIs(a1, a2)
        self.assertEqual(a2[0], 16)
        self.assertEqual(a2[1], 17)
        self.assertEqual(a2[2], 18)

        a1 = None
        a2 = fixture.modifyAndReturnIntArray(a1, 16, 17, 18)
        self.assertEqual(type(a2), jpy.get_type('[I'))
        self.assertEqual(a2[0], 16)
        self.assertEqual(a2[1], 17)
        self.assertEqual(a2[2], 18)

        a1 = [0, 0, 0]
        a2 = fixture.modifyAndReturnIntArray(a1, 16, 17, 18)
        self.assertEqual(type(a2), jpy.get_type('[I'))
        self.assertEqual(a2[0], 16)
        self.assertEqual(a2[1], 17)
        self.assertEqual(a2[2], 18)
예제 #26
0
    def test_modifyAndReturnIntArray(self):
        fixture = self.Fixture()

        # See https://docs.python.org/2/c-api/buffer.html
        #   "An array can only expose its contents via the old-style buffer interface.
        #    This limitation does not apply to Python 3, where memoryview objects can be
        #    constructed from arrays, too."
        # >>> import array
        # >>> a = array.array('i', [1,2,3])
        # >>> m = memoryview(a)
        # TypeError: cannot make memory view because object does not have the buffer interface
        #
        if sys.version_info >= (3, 0, 0):
            a1 = array.array('i', [0, 0, 0])
            # Python 2.7: TypeError: must be impossible<bad format char>, not bool
            a2 = fixture.modifyAndReturnIntArray(a1, 16, 17, 18)
            self.assertIs(a1, a2)
            self.assertEqual(a2[0], 16)
            self.assertEqual(a2[1], 17)
            self.assertEqual(a2[2], 18)

        if np:
            a1 = np.array([0, 0, 0], dtype='int32')
            a2 = fixture.modifyAndReturnIntArray(a1, 10, 11, 12)
            self.assertIs(a1, a2)
            self.assertEqual(a2[0], 10)
            self.assertEqual(a2[1], 11)
            self.assertEqual(a2[2], 12)

        a1 = jpy.array('int', 3)
        a2 = fixture.modifyAndReturnIntArray(a1, 16, 17, 18)
        self.assertIs(a1, a2)
        self.assertEqual(a2[0], 16)
        self.assertEqual(a2[1], 17)
        self.assertEqual(a2[2], 18)

        a1 = None
        a2 = fixture.modifyAndReturnIntArray(a1, 16, 17, 18)
        self.assertEqual(type(a2), jpy.get_type('[I'))
        self.assertEqual(a2[0], 16)
        self.assertEqual(a2[1], 17)
        self.assertEqual(a2[2], 18)

        a1 = [0, 0, 0]
        a2 = fixture.modifyAndReturnIntArray(a1, 16, 17, 18)
        self.assertEqual(type(a2), jpy.get_type('[I'))
        self.assertEqual(a2[0], 16)
        self.assertEqual(a2[1], 17)
        self.assertEqual(a2[2], 18)
예제 #27
0
파일: face.py 프로젝트: nerddd/Useful_tools
 def detect(self, image, height, width):
     """
     Detect face informations of given numpy OpenCV like: BGR uint8
     return faceInfos : float[][18] to java
     [
       x1, y1, x2, y2,   # bounding box 
       xy,xy,xy,xy,xy,   # facial points 
       score,            # confidence
       - roll,           # Z axis 翻转角:歪着脸  * NotImplemented
       - pitch,          # X axis 俯仰角:上下看  * NotImplemented
       - yaw             # Y axis 偏航角:左右看  * NotImplemented
     ]
     """
     image = self._listToNp(image, height, width)
     raw_infos = self.mtcnn.detect(image)
     infos = []
     num = len(raw_infos) / 18
     # print num,
     for i in xrange(num):
         info = []
         for f in raw_infos[18*i:18*(i+1)]:
             info.append(round(f, 3))
         infos.append(jpy.array('float', info)) # float[]
     return jpy.array(FloatArray, infos) # flaot[][]
예제 #28
0
def _getQstCol(col_name: str, col_type: DataType, col_data=None):
    if col_data is None or len(col_data) < 1:
        col_header = _qst_col_header_.of(col_name, col_type)
        return _qst_column_.empty(col_header)
    jtype = _jpyTypeFromType(col_type)
    if jtype is None:
        raise Exception("value for argument 'col_type' " + str(col_type) +
                        " is not a known data type.")
    if col_type is char and len(col_data) > 0 and isinstance(col_data[0], str):
        col_data = [ord(x[0]) for x in col_data]
    jvalues = jpy.array(jtype, col_data)
    if _isPrimitive(col_type):
        return _qst_column_.ofUnsafe(col_name, jvalues)
    else:
        return _qst_column_.of(col_name, col_type, jvalues)
예제 #29
0
def EmaArray(type, mode, timeScales):
    """
    Constructor for object managing an array of Exponential Moving Average (EMA) objects.

    :param type: enum value 'LEVEL', 'DIFFERENCE'.
    :param mode: enum value 'TICK', 'TIME' specifying whether to calculate the ema with respect to record count
      or elapsed time.
    :param timeScales: the ema decay constants (wrt nanosecond timestamp) - list, tuple, numpy.ndarray, or
      java double array.
    :return: io.deephaven.numerics.movingaverages.EmaArray instance.
    """

    type = _convertEnumBehavior(type, _Type_)
    mode = _convertEnumBehavior(mode, _Mode_)
    if isinstance(timeScales, list) or isinstance(
            timeScales, tuple) or isinstance(timeScales, numpy.ndarray):
        timeScales = jpy.array('double', timeScales)
    return _EmaArray_(type, mode, timeScales)
예제 #30
0
  def test_numpy_array(self):
    import numpy
    jpy_array = jpy.array('int', range(100))
    jpy_array_id = id(jpy_array)
    jpy_array_refcount = get_refcount(jpy_array_id)
    np_array = numpy.frombuffer(jpy_array, numpy.int32)
    self.assertEqual(list(jpy_array), list(np_array))
    self.assertEqual(get_refcount(jpy_array_id), jpy_array_refcount + 1)
    np_array = None
    self.assertEqual(get_refcount(jpy_array_id), jpy_array_refcount)

    mv = memoryview(b'123412341234')
    mv_id = id(mv)
    mv_refcount = get_refcount(mv_id)
    np_array = numpy.frombuffer(mv, numpy.int32)
    self.assertEqual(get_refcount(mv_id), mv_refcount + 1)
    np_array = None
    self.assertEqual(get_refcount(mv_id), mv_refcount)
예제 #31
0
def getFileObject(input):
    """
    Helper function for easily creating a java file object from a path string
    :param input: path string, or list of path strings
    :return: java File object, or java array of File objects
    """

    if _isJavaType(input):
        return input
    elif _isStr(input):
        return _java_file_type_(input)
    elif isinstance(input, list):
        # NB: map() returns an iterator in python 3, so list comprehension is appropriate here
        return jpy.array("java.io.File",
                         [_java_file_type_(el) for el in input])
    else:
        raise ValueError(
            "Method accepts only a java type, string, or list of strings as input. "
            "Got {}".format(type(input)))
예제 #32
0
def _jpy_partitions(partitions):
    if partitions is None:
        return ALL_PARTITIONS
    if isinstance(partitions, collections.Sequence):
        try:
            jarr = jpy.array('int', partitions)
        except Exception as e:
            raise ValueError(
                "when not one of the predefined constants, keyword argument 'partitions' has to " +
                "represent a sequence of integer partition with values >= 0, instead got " +
                str(partitions) + " of type " + type(partitions).__name__
            ) from e
        return _java_type_.partitionFilterFromArray(jarr)
    if not isinstance(partitions, jpy.JType):
        raise TypeError(
            "argument 'partitions' has to be of str or sequence type, " +
            "or a predefined compatible constant, instead got partitions " +
            str(partitions) + " of type " + type(partitions).__name__)
    return partitions
예제 #33
0
def generate_pdf(report: str, query: str, data: str, parameters: Dict[str, str]) -> bytearray:
    """
    Generate PDF from a report file using JSON.

    :param report:          The name of the report .jrxml.
    :param query:           A JSON query, e.g. "contacts.person".
    :param data:            The data in the form of a JSONified dict.
    :param parameters:      Settings for the report in the form of a dictionary
                            where the values are the string representations (in
                            Java format, so Python's True is 'true').
    :return: a bytearray.
    """
    #
    # Create the JasperStarter configuration. See Config.java for details.
    #
    config = Config()
    config.setInput(report)
    config.setOutput('-')
    config.setDbType(DsType.json)
    config.setJsonQuery(query)
    config.setDataFile(File('-'))
    config.setOutputFormats(Arrays.asList([]))
    config.setParams(Arrays.asList([k + '=' + v for k, v in parameters.items()]))
    #
    # Run the report. See Report.java for details.
    #
    report = Report(config, File(config.getInput()))
    savedStdin = getattr(System, 'in')
    savedStdout = System.out
    tmpStdout = ByteArrayOutputStream()
    try:
        System.setIn(ByteArrayInputStream(jpy.array('byte', bytearray(data, 'utf-8'))))
        System.setOut(PrintStream(tmpStdout))
        report.fill()
        report.exportPdf()
    finally:
        System.out.flush()
        System.setIn(savedStdin)
        System.setOut(savedStdout)
    #
    # Emit PDF.
    #
    return bytearray(tmpStdout.toByteArray())
예제 #34
0
    def test_modifyIntArray(self):
        fixture = self.Fixture()

        # See https://docs.python.org/2/c-api/buffer.html
        # "An array can only expose its contents via the old-style buffer interface.
        #    This limitation does not apply to Python 3, where memoryview objects can be
        #    constructed from arrays, too."
        # >>> import array
        # >>> a = array.array('i', [1,2,3])
        # >>> m = memoryview(a)
        # TypeError: cannot make memory view because object does not have the buffer interface
        #
        if sys.version_info >= (3, 0, 0):
            a = array.array('i', [0, 0, 0])
            fixture.modifyIntArray(a, 12, 13, 14)
            self.assertEqual(a[0], 12)
            self.assertEqual(a[1], 13)
            self.assertEqual(a[2], 14)

        if np:
            a = np.array([0, 0, 0], dtype='int32')
            fixture.modifyIntArray(a, 10, 11, 12)
            self.assertEqual(a[0], 10)
            self.assertEqual(a[1], 11)
            self.assertEqual(a[2], 12)

        a = jpy.array('int', 3)
        fixture.modifyIntArray(a, 12, 13, 14)
        self.assertEqual(a[0], 12)
        self.assertEqual(a[1], 13)
        self.assertEqual(a[2], 14)

        a = [0, 0, 0]
        fixture.modifyIntArray(a, 12, 13, 14)
        self.assertEqual(a[0], 0)
        self.assertEqual(a[1], 0)
        self.assertEqual(a[2], 0)

        with self.assertRaises(RuntimeError, msg='RuntimeError expected') as e:
            a = None
            fixture.modifyIntArray(a, 14, 15, 16)
        self.assertEqual(str(e.exception), 'java.lang.NullPointerException')
예제 #35
0
    def test_modifyIntArray(self):
        fixture = self.Fixture()

        # See https://docs.python.org/2/c-api/buffer.html
        #   "An array can only expose its contents via the old-style buffer interface.
        #    This limitation does not apply to Python 3, where memoryview objects can be
        #    constructed from arrays, too."
        # >>> import array
        # >>> a = array.array('i', [1,2,3])
        # >>> m = memoryview(a)
        # TypeError: cannot make memory view because object does not have the buffer interface
        #
        if sys.version_info >= (3, 0, 0):
            a = array.array('i', [0, 0, 0])
            fixture.modifyIntArray(a, 12, 13, 14)
            self.assertEqual(a[0], 12)
            self.assertEqual(a[1], 13)
            self.assertEqual(a[2], 14)

        if np:
            a = np.array([0, 0, 0], dtype='int32')
            fixture.modifyIntArray(a, 10, 11, 12)
            self.assertEqual(a[0], 10)
            self.assertEqual(a[1], 11)
            self.assertEqual(a[2], 12)

        a = jpy.array('int', 3)
        fixture.modifyIntArray(a, 12, 13, 14)
        self.assertEqual(a[0], 12)
        self.assertEqual(a[1], 13)
        self.assertEqual(a[2], 14)

        a = [0, 0, 0]
        fixture.modifyIntArray(a, 12, 13, 14)
        self.assertEqual(a[0], 0)
        self.assertEqual(a[1], 0)
        self.assertEqual(a[2], 0)

        with self.assertRaises(RuntimeError, msg='RuntimeError expected') as e:
            a = None
            fixture.modifyIntArray(a, 14, 15, 16)
        self.assertEqual(str(e.exception), 'java.lang.NullPointerException')
def _ensureBoxedArray(javaArray):
    """
    Ensure a one-dimensional primitive array gets boxed. Needed for signature matching efforts.

    :param javaArray: object
    :return: boxed array, if applicable
    """

    basicTypeInfo = _parseJavaArrayType(javaArray)
    if basicTypeInfo is None:
        # It's not an array at all...leave it alone?
        return javaArray

    dimension, basicType = basicTypeInfo
    if dimension != 1:
        # nothing to be done
        return javaArray
    elif basicType in _boxedArrayTypes:
        # let's box it
        return jpy.array(_boxedArrayTypes[basicType], javaArray)
    return javaArray
예제 #37
0
    def test_modifyAndOutputIntArray(self):
        fixture = self.Fixture()

        # See https://docs.python.org/2/c-api/buffer.html
        # "An array can only expose its contents via the old-style buffer interface.
        #    This limitation does not apply to Python 3, where memoryview objects can be
        #    constructed from arrays, too."
        # >>> import array
        # >>> a = array.array('i', [1,2,3])
        # >>> m = memoryview(a)
        # TypeError: cannot make memory view because object does not have the buffer interface
        #
        if sys.version_info >= (3, 0, 0):
            a = array.array('i', [0, 0, 0])
            fixture.modifyAndOutputIntArray(a, 16, 17, 18)
            self.assertEqual(a[0], 16)
            self.assertEqual(a[1], 17)
            self.assertEqual(a[2], 18)

        if np:
            a = np.array([0, 0, 0], dtype='int32')
            fixture.modifyIntArray(a, 10, 11, 12)
            self.assertEqual(a[0], 10)
            self.assertEqual(a[1], 11)
            self.assertEqual(a[2], 12)

        a = jpy.array('int', 3)
        fixture.modifyAndOutputIntArray(a, 15, 16, 17)
        self.assertEqual(a[0], 15)
        self.assertEqual(a[1], 16)
        self.assertEqual(a[2], 17)

        a = None
        fixture.modifyAndOutputIntArray(a, 16, 17, 18)

        a = [0, 0, 0]
        fixture.modifyAndOutputIntArray(a, 16, 17, 18)
        self.assertEqual(a[0], 0)
        self.assertEqual(a[1], 0)
        self.assertEqual(a[2], 0)
예제 #38
0
    def test_modifyAndOutputIntArray(self):
        fixture = self.Fixture()

        # See https://docs.python.org/2/c-api/buffer.html
        #   "An array can only expose its contents via the old-style buffer interface.
        #    This limitation does not apply to Python 3, where memoryview objects can be
        #    constructed from arrays, too."
        # >>> import array
        # >>> a = array.array('i', [1,2,3])
        # >>> m = memoryview(a)
        # TypeError: cannot make memory view because object does not have the buffer interface
        #
        if sys.version_info >= (3, 0, 0):
            a = array.array('i', [0, 0, 0])
            fixture.modifyAndOutputIntArray(a, 16, 17, 18)
            self.assertEqual(a[0], 16)
            self.assertEqual(a[1], 17)
            self.assertEqual(a[2], 18)

        if np:
            a = np.array([0, 0, 0], dtype='int32')
            fixture.modifyIntArray(a, 10, 11, 12)
            self.assertEqual(a[0], 10)
            self.assertEqual(a[1], 11)
            self.assertEqual(a[2], 12)

        a = jpy.array('int', 3)
        fixture.modifyAndOutputIntArray(a, 15, 16, 17)
        self.assertEqual(a[0], 15)
        self.assertEqual(a[1], 16)
        self.assertEqual(a[2], 17)

        a = None
        fixture.modifyAndOutputIntArray(a, 16, 17, 18)

        a = [0, 0, 0]
        fixture.modifyAndOutputIntArray(a, 16, 17, 18)
        self.assertEqual(a[0], 0)
        self.assertEqual(a[1], 0)
        self.assertEqual(a[2], 0)
예제 #39
0
파일: try_jpy.py 프로젝트: kikkomep/pydoop
# Copyright 2009-2015 CRS4.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may not
# use this file except in compliance with the License. You may obtain a copy
# of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
# END_COPYRIGHT

from timer import Timer

import jpyutil
jvm = jpyutil.init_jvm(jvm_maxmem='48M')

import jpy

N = 256
z = '\xff' * N * 1024
with Timer() as t:
    bz = bytearray(z)  # view string as bytearray
    a = jpy.array('byte', bz)  # project this to a java array
    ba = bytearray(a)  # bring it back to a python bytearray
print "=> round trip for size %sKB: %ss [%s KB/sec]" % (N, t.secs, N/t.secs)
예제 #40
0
파일: jpy_array_test.py 프로젝트: bcdev/jpy
 def do_test_array_with_initializer(self, type, expected):
     a = jpy.array(type, expected)
     self.assertEqual(len(a), 3)
     self.assertEqual(a[0], expected[0])
     self.assertEqual(a[1], expected[1])
     self.assertEqual(a[2], expected[2])