python中dtype的使用规范_Python numpy.dtype() 使用实例

The following are code examples for showing how to use . They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don’t like. You can also save this page to your account.

Example 1

def extract_images(filename):

"""Extract the images into a 4D uint8 numpy array [index, y, x, depth]."""

print('Extracting', filename)

with gzip.open(filename) as bytestream:

magic = _read32(bytestream)

if magic != 2051:

raise ValueError(

'Invalid magic number %d in MNIST image file: %s' %

(magic, filename))

num_images = _read32(bytestream)

rows = _read32(bytestream)

cols = _read32(bytestream)

buf = bytestream.read(rows * cols * num_images)

data = numpy.frombuffer(buf, dtype=numpy.uint8)

data = data.reshape(num_images, rows, cols, 1)

return data

Example 2

def gl_init(self,array_table):

self.gl_hide = False

self.gl_vertex_array = gl.VertexArray()

glBindVertexArray(self.gl_vertex_array)

self.gl_vertex_buffer = gl.Buffer()

glBindBuffer(GL_ARRAY_BUFFER,self.gl_vertex_buffer)

self.gl_element_count = 3*gl_count_triangles(self)

self.gl_element_buffer = gl.Buffer()

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,self.gl_element_buffer)

vertex_type = numpy.dtype([array_table[attribute].field() for attribute in self.attributes])

vertex_count = sum(len(primitive.vertices) for primitive in self.primitives)

vertex_array = numpy.empty(vertex_count,vertex_type)

for attribute in self.attributes:

array_table[attribute].load(self,vertex_array)

vertex_array,element_map = numpy.unique(vertex_array,return_inverse=True)

element_array = gl_create_element_array(self,element_map,self.gl_element_count)

glBufferData(GL_ARRAY_BUFFER,vertex_array.nbytes,vertex_array,GL_STATIC_DRAW)

glBufferData(GL_ELEMENT_ARRAY_BUFFER,element_array.nbytes,element_array,GL_STATIC_DRAW)

Example 3

def extract_images(filename):

"""Extract the images into a 4D uint8 numpy array [index, y, x, depth]."""

print('Extracting', filename)

with gzip.open(filename) as bytestream:

magic = _read32(bytestream)

if magic != 2051:

raise ValueError(

'Invalid magic number %d in MNIST image file: %s' %

(magic, filename))

num_images = _read32(bytestream)

rows = _read32(bytestream)

cols = _read32(bytestream)

buf = bytestream.read(rows * cols * num_images)

data = numpy.frombuffer(buf, dtype=numpy.uint8)

data = data.reshape(num_images, rows, cols, 1)

return data

Example 4

def __keytransform__(self, key):

if isinstance(key[0], np.ndarray):

shape = key[0].shape

dtype = key[0].dtype

i = key[1]

zero = True if len(key) == 2 else key[2]

elif isinstance(key[0], tuple):

if len(key) == 3:

shape, dtype, i = key

zero = True

elif len(key) == 4:

shape, dtype, i, zero = key

else:

raise TypeError("Wrong type of key for work array")

assert isinstance(zero, bool)

assert isinstance(i, int)

self.fillzero = zero

return (shape, np.dtype(dtype), i)

Example 5

def accumulate_strings(values, name="strings"):

"""Accumulates strings into a vector.

Args:

values: A 1-d string tensor that contains values to add to the accumulator.

Returns:

A tuple (value_tensor, update_op).

"""

tf.assert_type(values, tf.string)

strings = tf.Variable(

name=name,

initial_value=[],

dtype=tf.string,

trainable=False,

collections=[],

validate_shape=True)

value_tensor = tf.identity(strings)

update_op = tf.assign(

ref=strings, value=tf.concat([strings, values], 0), validate_shape=False)

return value_tensor, update_op

Example 6

def test_expect_dtypes_with_tuple(self):

allowed_dtypes = (dtype('datetime64[ns]'), dtype('float'))

@expect_dtypes(a=allowed_dtypes)

def foo(a, b):

return a, b

for d in allowed_dtypes:

good_a = arange(3).astype(d)

good_b = object()

ret_a, ret_b = foo(good_a, good_b)

self.assertIs(good_a, ret_a)

self.assertIs(good_b, ret_b)

with self.assertRaises(TypeError) as e:

foo(arange(3, dtype='uint32'), object())

expected_message = (

"{qualname}() expected a value with dtype 'datetime64[ns]' "

"or 'float64' for argument 'a', but got 'uint32' instead."

).format(qualname=qualname(foo))

self.assertEqual(e.exception.args[0], expected_message)

Example 7

def _classify_gems(counts0, counts1):

""" Infer number of distinct transcriptomes present in each GEM (1 or 2) and

report cr_constants.GEM_CLASS_GENOME0 for a single cell w/ transcriptome 0,

report cr_constants.GEM_CLASS_GENOME1 for a single cell w/ transcriptome 1,

report cr_constants.GEM_CLASS_MULTIPLET for multiple transcriptomes """

# Assumes that most of the GEMs are single-cell; model counts independently

thresh0, thresh1 = [cr_constants.DEFAULT_MULTIPLET_THRESHOLD] * 2

if sum(counts0 > counts1) >= 1 and sum(counts1 > counts0) >= 1:

thresh0 = np.percentile(counts0[counts0 > counts1], cr_constants.MULTIPLET_PROB_THRESHOLD)

thresh1 = np.percentile(counts1[counts1 > counts0], cr_constants.MULTIPLET_PROB_THRESHOLD)

doublet = np.logical_and(counts0 >= thresh0, counts1 >= thresh1)

dtype = np.dtype('|S%d' % max(len(cls) for cls in cr_constants.GEM_CLASSES))

result = np.where(doublet, cr_constants.GEM_CLASS_MULTIPLET, cr_constants.GEM_CLASS_GENOME0).astype(dtype)

result[np.logical_and(np.logical_not(result == cr_constants.GEM_CLASS_MULTIPLET), counts1 > counts0)] = cr_constants.GEM_CLASS_GENOME1

return result

Example 8

def widen_cat_column(old_ds, new_type):

name = old_ds.name

tmp_name = "__tmp_" + old_ds.name

grp = old_ds.parent

ds = grp.create_dataset(tmp_name,

data = old_ds[:],

shape = old_ds.shape,

maxshape = (None,),

dtype = new_type,

compression = COMPRESSION,

shuffle = True,

chunks = (CHUNK_SIZE,))

del grp[name]

grp.move(tmp_name, name)

return ds

Example 9

def create_levels(ds, levels):

# Create a dataset in the LEVEL_GROUP

# and store as native numpy / h5py types

level_grp = ds.file.get(LEVEL_GROUP)

if level_grp is None:

# Create a LEVEL_GROUP

level_grp = ds.file.create_group(LEVEL_GROUP)

ds_name = ds.name.split("/")[-1]

dt = h5py.special_dtype(vlen=str)

level_grp.create_dataset(ds_name,

shape = [len(levels)],

maxshape = (None,),

dtype = dt,

data = levels,

compression = COMPRESSION,

chunks = (CHUNK_SIZE,))

Example 10

def reg2bin_vector(begin, end):

'''Vectorized tabix reg2bin -- much faster than reg2bin'''

result = np.zeros(begin.shape)

# Entries filled

done = np.zeros(begin.shape, dtype=np.bool)

for (bits, bins) in rev_bit_bins:

begin_shift = begin >> bits

new_done = (begin >> bits) == (end >> bits)

mask = np.logical_and(new_done, np.logical_not(done))

offset = ((1 << (29 - bits)) - 1) / 7

result[mask] = offset + begin_shift[mask]

done = new_done

return result.astype(np.int32)

Example 11

def flip_code(code):

if isinstance(code, (numpy.dtype,type)):

# since several things map to complex64 we must carefully select

# the opposite that is an exact match (ticket 1518)

if code == numpy.int8:

return gdalconst.GDT_Byte

if code == numpy.complex64:

return gdalconst.GDT_CFloat32

for key, value in codes.items():

if value == code:

return key

return Non

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值