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)
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)
def testPrimitive(self): test1 = self.__jp.Test1() intexpectation = 'int' if not sys.version_info[ 0] > 2 and sys.maxint == 2**31 - 1 else 'long' self.assertEquals(intexpectation, test1.testPrimitive(5)) self.assertEquals('long', test1.testPrimitive(2**31)) self.assertEquals('byte', test1.testPrimitive(JByte(5))) self.assertEquals('Byte', test1.testPrimitive(java.lang.Byte(5))) self.assertEquals('short', test1.testPrimitive(JShort(5))) self.assertEquals('Short', test1.testPrimitive(java.lang.Short(5))) self.assertEquals('int', test1.testPrimitive(JInt(5))) self.assertEquals('Integer', test1.testPrimitive(java.lang.Integer(5))) self.assertEquals('long', test1.testPrimitive(JLong(5))) self.assertEquals('Long', test1.testPrimitive(java.lang.Long(5))) self.assertEquals('float', test1.testPrimitive(JFloat(5))) self.assertEquals('Float', test1.testPrimitive(java.lang.Float(5.0))) self.assertEquals('double', test1.testPrimitive(JDouble(5))) self.assertEquals('Double', test1.testPrimitive(java.lang.Double(5.0))) self.assertEquals('boolean', test1.testPrimitive(JBoolean(5))) self.assertEquals('Boolean', test1.testPrimitive(java.lang.Boolean(5))) self.assertEquals('char', test1.testPrimitive(JChar('5'))) self.assertEquals('Character', test1.testPrimitive(java.lang.Character('5')))
def testPrimitive(self): test1 = self.__jp.Test1() intexpectation = 'long' # FIXME it is not possible to determine if this is bool/char/byte currently #self.assertEqual(intexpectation, test1.testPrimitive(5)) #self.assertEqual('long', test1.testPrimitive(2**31)) self.assertEqual('byte', test1.testPrimitive(JByte(5))) self.assertEqual('Byte', test1.testPrimitive(java.lang.Byte(5))) self.assertEqual('short', test1.testPrimitive(JShort(5))) self.assertEqual('Short', test1.testPrimitive(java.lang.Short(5))) self.assertEqual('int', test1.testPrimitive(JInt(5))) self.assertEqual('Integer', test1.testPrimitive(java.lang.Integer(5))) self.assertEqual('long', test1.testPrimitive(JLong(5))) self.assertEqual('Long', test1.testPrimitive(java.lang.Long(5))) self.assertEqual('float', test1.testPrimitive(JFloat(5))) self.assertEqual('Float', test1.testPrimitive(java.lang.Float(5.0))) self.assertEqual('double', test1.testPrimitive(JDouble(5))) self.assertEqual('Double', test1.testPrimitive(java.lang.Double(5.0))) self.assertEqual('boolean', test1.testPrimitive(JBoolean(5))) self.assertEqual('Boolean', test1.testPrimitive(java.lang.Boolean(5))) self.assertEqual('char', test1.testPrimitive(JChar('5'))) self.assertEqual('Character', test1.testPrimitive(java.lang.Character('5')))
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
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