Exemplo n.º 1
0
 def testStringArray(self):
     test1 = self.__jp.Test1()
     self.assertEqual('Object', test1.testStringArray(self._i4impl))
     self.assertEqual('Object', test1.testStringArray(1))
     self.assertRaisesRegex(
         TypeError, 'Ambiguous overloads found', test1.testStringArray, None)
     self.assertEqual('String', test1.testStringArray('somestring'))
     self.assertEqual('String[]', test1.testStringArray([]))
     self.assertEqual('String[]', test1.testStringArray(['a', 'b']))
     self.assertEqual('String[]', test1.testStringArray(
         JArray(java.lang.String, 1)(['a', 'b'])))
     self.assertEqual('Object', test1.testStringArray(
         JArray(JInt, 1)([1, 2])))
Exemplo n.º 2
0
def list1d_to_java_array(a):
    """
    Converts python list of primitive int or double to java array
    """
    if type(a) is list:
        if type(a[0]) is int:
            return JArray(JInt)(a)
        elif type(a[0]) is float:
            return JArray(JDouble)(a)
        elif type(a[0]) is str:
            return JArray(JString)(a)
        else:
            return JArray(JObject)(a)
    else:
        raise TypeError("Not a list: " + str(a))
Exemplo n.º 3
0
def _to_imglib_argb(source):
    address = _get_address(source)
    long_address = JLong(address)
    long_arr_source = JArray(JLong)(source.shape[::-1])

    if not (source.dtype == np.dtype('int32')
            or source.dtype == np.dtype('uint32')):
        raise NotImplementedError("source.dtype must be int32 or uint32")
    if source.flags['CARRAY']:
        return NumpyToImgLibConversions.toARGB(long_address, *long_arr_source)
    else:
        stride = np.array(source.strides[::-1]) / source.itemsize
        long_arr_stride = JArray(JLong)(stride)
        return NumpyToImgLibConversionsWithStride.toARGB(
            long_address, long_arr_stride, long_arr_source)
Exemplo n.º 4
0
 def testCallWithArray(self):
     h2 = JClass('jpype.attr.Test1')()
     StringArray = JArray(JString)
     v = StringArray(["Foo", "bar"])
     t = JClass('jpype.attr.Test1')()
     result = t.testStringArray(v)
     self.assertSequenceEqual(["Foo", "bar"], result)
Exemplo n.º 5
0
 def testCallWithArray(self):
     h2 = self.__jp.Test1()
     StringArray = JArray(JString)
     v = StringArray(["Foo", "bar"])
     t = self.__jp.Test1()
     result = t.testStringArray(v)
     self.assertSequenceEqual(["Foo", "bar"], result)
Exemplo n.º 6
0
def _to_imglib(source):
    address = _get_address(source)
    long_address = JLong(address)
    long_arr_source = JArray(JLong)(source.shape[::-1])

    if not source.dtype in numpy_dtype_to_conversion_method:
        raise NotImplementedError(
            "Cannot convert dtype to ImgLib2 type yet: {}".format(
                source.dtype))
    elif source.flags['CARRAY']:
        return numpy_dtype_to_conversion_method[source.dtype](long_address,
                                                              *long_arr_source)
    else:
        stride = np.array(source.strides[::-1]) / source.itemsize
        long_arr_stride = JArray(JLong)(stride)
        return numpy_dtype_to_conversion_with_stride_method[source.dtype](
            long_address, long_arr_stride, long_arr_source)
Exemplo n.º 7
0
def assert_ndarray_equal_to_img(img, nparr):
    cursor = img.cursor()
    arr = JArray(JInt)(5)
    while cursor.hasNext():
        y = cursor.next().get()
        cursor.localize(arr)
        # TODO: Imglib has inverted dimensions - extract this behavior into a helper function
        x = nparr[tuple(arr[::-1])]
        assert x == y
Exemplo n.º 8
0
 def testCallWithArrayMismatch(self):
     h2 = JClass('jpype.attr.Test1')()
     StringArray = JArray(JString)
     v = StringArray(["Foo", "bar"])
     t = JClass('jpype.attr.Test1')()
     result = t.testStringArray(v)
     self.assertFalse([1, 2] == result)
     self.assertFalse(result == [1, 2])
     self.assertTrue([1, 2] != result)
     self.assertTrue(result != [1, 2])
Exemplo n.º 9
0
 def test_step_not_enough_dims(self, ij_fixture, img):
     # Create a stepped img via Views
     Views = sj.jimport("net.imglib2.view.Views")
     steps = JArray(JLong)([2, 1, 1])
     expected = Views.subsample(img, steps)
     expected = Views.dropSingletonDimensions(expected)
     # Create a stepped img via slicing notation
     actual = img[::2]
     for i in range(3):
         for j in range(4):
             assert expected[i, j] == actual[i, j]
Exemplo n.º 10
0
 def test_slice_and_step(self, ij_fixture, img):
     # Create a stepped img via Views
     Views = sj.jimport("net.imglib2.view.Views")
     intervaled = Views.hyperSlice(img, 0, 0)
     steps = JArray(JLong)([1, 2])
     expected = Views.subsample(intervaled, steps)
     # Create a stepped img via slicing notation
     actual = img[:1, :, ::2]
     for i in range(3):
         for j in range(2):
             assert expected[i, j] == actual[i, j]
Exemplo n.º 11
0
    def _get_img():
        # Create img
        CreateNamespace = sj.jimport("net.imagej.ops.create.CreateNamespace")
        dims = JArray(JLong)([1, 2, 3, 4, 5])
        ns = ij_fixture.op().namespace(CreateNamespace)
        img = ns.img(dims)

        # Populate img with random data
        cursor = img.cursor()
        while cursor.hasNext():
            val = random.random()
            cursor.next().set(val)

        return img
Exemplo n.º 12
0
 def testVarArgsCall(self):
     test1 = self.__jp.Test1()
     self.assertEqual('A,B...', test1.testVarArgs(self._a, []))
     self.assertEqual('A,B...', test1.testVarArgs(self._a, None))
     self.assertEqual('A,B...', test1.testVarArgs(self._a, [self._b]))
     self.assertEqual('A,A...', test1.testVarArgs(self._a, [self._a]))
     self.assertEqual('A,B...', test1.testVarArgs(
         self._a, [self._b, self._b]))
     self.assertEqual('B,B...', test1.testVarArgs(
         self._b, [self._b, self._b]))
     self.assertEqual('B,B...', test1.testVarArgs(
         self._c, [self._c, self._b]))
     self.assertEqual('B,B...', test1.testVarArgs(
         self._c, JArray(self._cclass, 1)([self._c, self._c])))
     self.assertEqual('B,B...', test1.testVarArgs(self._c, None))
     self.assertEqual('B,B...', test1.testVarArgs(None, None))
Exemplo n.º 13
0
    def load_jar_class(jar_path, class_name):
        """
        Load a class at runtime directly from a jar file.
        Note that with some libraries this can cause problems because the library
        will not be *visible* to the default class loader.

        :param jar_path: Path to the `.jar` file.
        :param class_name: The fully qualified Class Name within the jar to load.
        :return:
        """
        URL = JClass('java.net.URL')
        URLClassLoader = JClass('java.net.URLClassLoader')
        Class = JClass('java.lang.Class')
        UrlArray = JArray(URL)
        urls = UrlArray(1)
        urls[0] = get_url(jar_path)
        java_class = JClass(Class.forName(class_name, True, URLClassLoader(urls)))
        return java_class
Exemplo n.º 14
0
def getOptionalInstance(x):
    if type(x) is list:
        internalObjects = list(getOptionalInstance(thing) for thing in x)
        className = JClass(internalObjects[0].getClass().getName())
        retVal = JArray(className, 1)(internalObjects)
        return retVal

    if type(x) is dict:
        # local import necessary to avoid a circular dependency - import it when you need it
        from blnx.java.util.HashMap import JHashMap
        hashMap = JHashMap()
        for key,value in x.items():
            hashMap.put(key, getOptionalInstance(value))
        return hashMap



    if hasattr(x, 'instance'):
        return x.instance
    else:
        return x
Exemplo n.º 15
0
def get_faacets_moment_matrix(A_configuration, B_configuration, coefficients):
    from jpype import startJVM, shutdownJVM, JPackage, getDefaultJVMPath, \
        JArray, JDouble
    # find the JAR file and start the JVM
    jarFiles = glob.glob('faacets-*.jar')
    assert len(jarFiles) == 1
    jarFile = os.path.join(os.getcwd(), jarFiles[0])
    startJVM(getDefaultJVMPath(), "-Djava.class.path=" + jarFile)
    com = JPackage('com')

    # main code
    sc = com.faacets.Core.Scenario(
        configuration_to_faacets(A_configuration, B_configuration))
    representation = com.faacets.Core.Representation('NCRepresentation')
    ope = com.faacets.SDP.OperatorElements(['', 'A', 'AB'])
    pts = com.faacets.SDP.PartialTransposes([])
    vec = com.faacets.Core.QVector(JArray(JDouble)(coefficients))
    expr = com.faacets.Core.Expr(sc, representation, vec)
    sdp = com.faacets.SDP.CorrSDP(sc, ope, pts, expr.symmetryGroup())
    M = np.array(sdp.indexArray())
    ncIndices = np.array(sdp.ncIndices())
    shutdownJVM()
    return M, ncIndices
Exemplo n.º 16
0
 def testObjectArraySimple(self) :
     a = JArray(java.lang.String, 1)(2)
     a[1] = "Foo"
     self.assertEqual("Foo", a[1])
Exemplo n.º 17
0
 def testByteArraySimple(self) :
     a = JArray(JByte)(2)
     a[1] = 2
     self.assertEqual(a[1], 2)
Exemplo n.º 18
0
def convert_map_to_vector(map, label_index):
    v = np.empty(len(map), dtype=float)
    for e in map:
        v[label_index[e]] = map[e]
    return JArray(JFloat, v.ndim)(v.tolist())  # 将numpy数组转为Java数组
Exemplo n.º 19
0
def convert_map_to_matrix(map, label_index1, label_index2):
    m = np.empty((len(label_index1), len(label_index2)), dtype=float)
    for line in map:
        for col in map[line]:
            m[label_index1[line]][label_index2[col]] = map[line][col]
    return JArray(JFloat, m.ndim)(m.tolist())
Exemplo n.º 20
0
def rai_slice(rai, imin: Tuple, imax: Tuple, istep: Tuple):
    """Slice ImgLib2 images.

    Slice ImgLib2 images using Python's slice notation to define the
    desired slice range. Returned interval includes both imin and imax

    :param rai: An ImgLib2 RandomAccessibleInterval
    :param imin: Tuple of minimum interval range values.
    :param imax: Tuple of maximum interval range values.
    :return: Sliced ImgLib2 RandomAccisbleInterval.
    """

    # HACK: Avoid importing JLong at global scope.
    # Otherwise, building the sphinx docs in doc/rtd fails with:
    #
    #   Warning, treated as error:
    #   autodoc: failed to determine imagej.stack.JLong (<java class 'JLong'>) to be documented, the following exception was raised:
    #   Java Virtual Machine is not running
    #
    # Which can be reproduced in a REPL like this:
    #
    #   >>> from jpype import JLong
    #   >>> help(JLong)
    #
    # So while the import here is unfortunate, it avoids the issue.
    from jpype import JArray, JLong

    Views = sj.jimport("net.imglib2.view.Views")
    shape = rai.shape
    imin_fix = JArray(JLong)(len(shape))
    imax_fix = JArray(JLong)(len(shape))
    dim_itr = range(len(shape))

    for py_dim, j_dim in zip(dim_itr, dim_itr):

        # Set minimum
        if imin[py_dim] == None:
            index = 0
        else:
            index = imin[py_dim]
            if index < 0:
                index += shape[j_dim]
        imin_fix[j_dim] = JLong(index)
        # Set maximum
        if imax[py_dim] == None:
            index = shape[j_dim] - 1
        else:
            index = imax[py_dim]
            if index < 0:
                index += shape[j_dim]
        imax_fix[j_dim] = JLong(index)

    istep_fix = JArray(JLong)(istep)

    if _index_within_range(imin_fix, shape) and _index_within_range(
            imax_fix, shape):
        intervaled = Views.interval(rai, imin_fix, imax_fix)
        stepped = Views.subsample(intervaled, istep_fix)

    # TODO: better mach NumPy squeeze behavior. See pyimagej/#1231
    dimension_reduced = Views.dropSingletonDimensions(stepped)
    return dimension_reduced
Exemplo n.º 21
0
B = convert_map_to_matrix(emission_probability, states_label_index,
                          observations_label_index)
observations_index = convert_observations_to_index(observations,
                                                   observations_label_index)
pi = convert_map_to_vector(start_probability, states_label_index)

FirstOrderHiddenMarkovModel = JClass(
    'com.hankcs.hanlp.model.hmm.FirstOrderHiddenMarkovModel')
given_model = FirstOrderHiddenMarkovModel(pi, A, B)

for O, S in given_model.generate(3, 5, 2):
    print(" ".join((observations_index_label[o] + '/' + states_index_label[s])
                   for o, s in zip(O, S)))

trained_model = FirstOrderHiddenMarkovModel()
trained_model.train(given_model.generate(3, 10, 100000))
assert trained_model.similar(given_model)
trained_model.unLog()  # 将对数形式的概率还原回来

print(trained_model.start_probability)
for vec in trained_model.transition_probability:
    print(vec)
for vec in trained_model.emission_probability:
    print(vec)

pred = JArray(JInt, 1)([0, 0, 0])
prob = given_model.predict(observations_index, pred)
print(" ".join((observations_index_label[o] + '/' + states_index_label[s])
               for o, s in zip(observations_index, pred)) +
      " {:.3f}".format(np.math.exp(prob)))
Exemplo n.º 22
0
from jpype import (JClass, JArray, getDefaultJVMPath, java, shutdownJVM,
                   startJVM)

startJVM(getDefaultJVMPath(),
         '-ea',
         f'-Djava.class.path=dnmso.jar',
         convertStrings=False)

DnmsoFactory: JClass = JClass('domain.DnmsoFactory')
LutefiskXPService: JClass = JClass('service.LutefiskXPService')
DNMSO: JClass = JClass('domain.DNMSO')

dnmsoFactory: DnmsoFactory = DnmsoFactory()
targetDNMSO: DNMSO = dnmsoFactory.createDnmso()

lutefiskXPService: LutefiskXPService = LutefiskXPService()

lutefiskXPArgs: JArray(
    java.lang.String) = ["read", "-p", "Qtof_ELVISLIVESK.lut", "-n", "2"]

targetDNMSO: DNMSO = lutefiskXPService.run(targetDNMSO, lutefiskXPArgs)

print(targetDNMSO.getPredictions().get(0).getPrediction().size())
Exemplo n.º 23
0
 def testCallWithArray(self):
     h2 = self.__jp.Test1()
     StringArray = JArray(JString)
     v = StringArray(["Foo", "bar"])
     t = self.__jp.Test1()
     t.testStringArray(v)
Exemplo n.º 24
0
    def execute(self, operation, params=None):
        """
        Execute a sql statement with an optional set of parameters

        :param operation: Sql text
        :param params: a sequence or dictionary of parameters
                       Parameters can be positional templates ``%s`` or named templates ``:name``

        :param operation:
        :param params:
        :return:
        """
        if not self._connection_valid():
            raise Error('the connection has been closed')

        self._reset()

        conn = self._connection.jdbc_connection()

        # handle parameters
        if params is not None:
            operation, params = self._parse_params(operation, params)

        # prepare the statement
        self._statement = stmt = conn.prepareStatement(operation)
        stmt.clearParameters()
        for column, value in enumerate(params or [], start=1):
            # note that in JDBC the first column index is 1
            if isinstance(value, str):
                jvalue = JString(value)
                stmt.setString(column, jvalue)
            elif isinstance(value, float):
                jvalue = JDouble(value)
                stmt.setDouble(column, jvalue)
            elif isinstance(value, int):
                try:
                    jvalue = JInt(value)
                    stmt.setInt(column, jvalue)
                except Exception:
                    jvalue = JLong(value)
                    stmt.setLong(column, jvalue)
            elif isinstance(value, bool):
                jvalue = JBoolean(value)
                stmt.setBoolean(column, jvalue)
            elif isinstance(value, bytes):
                ByteArray = JArray(JByte)
                jvalue = ByteArray(value.decode('utf-8'))
                stmt.setBytes(column, jvalue)
            else:
                stmt.setObject(column, value)

        try:
            has_resultset = stmt.execute()
        except JClass('org.apache.hive.service.cli.HiveSQLException') as e:
            raise ProgrammingError('Error executing statement:\n{}\n{}'.format(
                operation, e)) from None

        self._rowcount = -1
        if has_resultset:
            self._resultset = resultset = stmt.getResultSet()
            self._metadata = resultset.getMetaData()

            # get rowcount
            if self._get_rowcounts:
                try:
                    if self._resultset.last(
                    ):  # if the cursor can be moved to the last row.
                        self._rowcount = resultset.getRow()
                    resultset.beforeFirst()
                except JClass('java.sql.SQLException'):
                    # ResultSet.last() is not supported
                    pass

        else:
            try:
                self._rowcount = stmt.getUpdateCount()
            except JClass('java.sql.SQLException'):
                # not supported
                pass
Exemplo n.º 25
0
def ndarray_to_stallone_array(pyarray, copy=True):
    """
    Convert numpy ndarrays to the corresponding wrapped type in Stallone. 
    Currently only int32 and double are supported in stallone.
    
    Parameters
    ----------
    pyarray : numpy.ndarray or scipy.sparse type one or two dimensional
    
    copy : boolean
      if false, a Java side ByteBuffer wrapping the given array buffer will
      be used to avoid a copy. This is useful for very huge data sets.
    
    Returns
    -------
    IDoubleArray or IIntArray depending on input type
    
    Note:
    -----
    scipy.sparse types will be currently converted to dense, before passing
    them to the java side!
    """
    if not isinstance(pyarray, _np.ndarray):
        raise TypeError('Only numpy arrays supported. Given type was "%s"' %
                        type(pyarray))

    if pyarray.dtype not in _supported_types:
        raise TypeError('Given type %s not mapped in stallone library' %
                        pyarray.dtype)
    """
    from scipy.sparse.base import issparse
    if issparse(pyarray):
        _log.warning("converting sparse object to dense for stallone.")
        pyarray = pyarray.todense()
    """

    shape = pyarray.shape
    dtype = pyarray.dtype
    factory = None
    cast_func = None

    # stallone does currently support only wrappers for int32 and float64
    if dtype == _np.float32:
        _warnings.warn("Upcasting floats to doubles!")
        pyarray = pyarray.astype(_np.float64)
    if dtype == _np.int64:
        _warnings.warn("Downcasting long to 32 bit integer."
                       " You will loose precision by doing so!")
        pyarray = pyarray.astype(_np.int32)

    # Pass memory to jpype and create a java array.
    # Also set corresponding factory method in stallone to wrap the array.
    if dtype == _np.float32 or dtype == _np.float64:
        factory = API.doublesNew
        cast_func = JDouble

    elif dtype == _np.int32 or dtype == _np.int64:
        factory = API.intsNew
        cast_func = JInt

    if not copy:
        if not pyarray.flags.c_contiguous:
            raise RuntimeError('Can only pass contiguous memory to Java!')
        jbuff = _nio.convertToDirectBuffer(pyarray)
        rows = shape[0]
        cols = 1 if len(shape) == 1 else shape[1]
        return factory.arrayFrom(jbuff, rows, cols)

    if len(shape) == 1:
        # create a JArray wrapper
        jarr = JArray(cast_func)(pyarray)
        if cast_func is JDouble:
            return factory.array(jarr)
        if cast_func is JInt:
            return factory.arrayFrom(jarr)
        raise TypeError('type not mapped to a stallone factory')

    elif len(shape) == 2:
        # TODO: use linear memory layout here, when supported in stallone
        jarr = JArray(cast_func, 2)(pyarray)
        if cast_func is JDouble:
            # for double arrays
            A = factory.array(jarr)
        elif cast_func is JInt:
            # for int 2d arrays
            A = factory.table(jarr)
        else:
            raise TypeError('type not mapped to a stallone factory')
        return A
    else:
        raise ValueError('unsupported shape:', shape)
Exemplo n.º 26
0
 def testObjectArraySimple(self):
     a = JArray(java.lang.String, 1)(2)
     a[1] = "Foo"
     assert "Foo" == a[1]
Exemplo n.º 27
0
 def testByteArraySimple(self):
     a = JArray(JByte)(2)
     a[1] = 2
     assert a[1] == 2
Exemplo n.º 28
0
 def testBytes(self):
     UTF_8 = java.nio.charset.StandardCharsets.UTF_8
     smiley = java.lang.String(
         JArray(JByte)([0xF0, 0x9F, 0x98, 0x8A]), UTF_8)
     self.assertEqual(list(smiley.getBytes(UTF_8)),
                      [0xF0, 0x9F, 0x98, 0x8A])