本文整理汇总了Python中numpy.sort方法的典型用法代码示例。如果您正苦于以下问题:Python numpy.sort方法的具体用法?Python numpy.sort怎么用?Python numpy.sort使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类numpy
的用法示例。
在下文中一共展示了numpy.sort方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: read_cpg_profiles
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import sort [as 别名]
def read_cpg_profiles(filenames, log=None, *args, **kwargs):
"""Read methylation profiles.
Input files can be gzip compressed.
Returns
-------
dict
`dict (key, value)`, where `key` is the output name and `value` the CpG
table.
"""
cpg_profiles = OrderedDict()
for filename in filenames:
if log:
log(filename)
#cpg_file = dat.GzipFile(filename, 'r')
cpg_file = get_fh(filename, 'r')
output_name = split_ext(filename)
cpg_profile = dat.read_cpg_profile(cpg_file, sort=True, *args, **kwargs)
cpg_profiles[output_name] = cpg_profile
cpg_file.close()
return cpg_profiles
示例2: map_values
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import sort [as 别名]
def map_values(values, pos, target_pos, dtype=None, nan=dat.CPG_NAN):
"""Maps `values` array at positions `pos` to `target_pos`.
Inserts `nan` for uncovered positions.
"""
assert len(values) == len(pos)
assert np.all(pos == np.sort(pos))
assert np.all(target_pos == np.sort(target_pos))
values = values.ravel()
pos = pos.ravel()
target_pos = target_pos.ravel()
idx = np.in1d(pos, target_pos)
pos = pos[idx]
values = values[idx]
if not dtype:
dtype = values.dtype
target_values = np.empty(len(target_pos), dtype=dtype)
target_values.fill(nan)
idx = np.in1d(target_pos, pos).nonzero()[0]
assert len(idx) == len(values)
assert np.all(target_pos[idx] == pos)
target_values[idx] = values
return target_values
示例3: map_cpg_tables
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import sort [as 别名]
def map_cpg_tables(cpg_tables, chromo, chromo_pos):
"""Maps values from cpg_tables to `chromo_pos`.
Positions in `cpg_tables` for `chromo` must be a subset of `chromo_pos`.
Inserts `dat.CPG_NAN` for uncovered positions.
"""
chromo_pos.sort()
mapped_tables = OrderedDict()
for name, cpg_table in six.iteritems(cpg_tables):
cpg_table = cpg_table.loc[cpg_table.chromo == chromo]
cpg_table = cpg_table.sort_values('pos')
mapped_table = map_values(cpg_table.value.values,
cpg_table.pos.values,
chromo_pos)
assert len(mapped_table) == len(chromo_pos)
mapped_tables[name] = mapped_table
return mapped_tables
示例4: add_exon
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import sort [as 别名]
def add_exon(self, chrom, strand, start, stop):
if strand != self.strand or chrom != self.chrom:
print("The exon has different chrom or strand to the transcript.")
return
_exon = np.array([start, stop], "int").reshape(1, 2)
self.exons = np.append(self.exons, _exon, axis=0)
self.exons = np.sort(self.exons, axis=0)
self.tranL += abs(int(stop) - int(start) + 1)
self.exonNum += 1
self.seglen = np.zeros(self.exons.shape[0] * 2 - 1, "int")
self.seglen[0] = self.exons[0, 1] - self.exons[0, 0] + 1
for i in range(1, self.exons.shape[0]):
self.seglen[i * 2 - 1] = self.exons[i, 0] - self.exons[i - 1, 1] - 1
self.seglen[i * 2] = self.exons[i, 1] - self.exons[i, 0] + 1
if ["-", "-1", "0", 0, -1].count(self.strand) > 0:
self.seglen = self.seglen[::-1]
示例5: add_exon
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import sort [as 别名]
def add_exon(self, chrom, strand, start, stop):
if strand != self.strand or chrom != self.chrom:
print("The exon has different chrom or strand to the transcript.")
return
_exon = np.array([start, stop], "int").reshape(1,2)
self.exons = np.append(self.exons, _exon, axis=0)
self.exons = np.sort(self.exons, axis=0)
self.tranL += abs(int(stop) - int(start) + 1)
self.exonNum += 1
self.seglen = np.zeros(self.exons.shape[0] * 2 - 1, "int")
self.seglen[0] = self.exons[0,1]-self.exons[0,0] + 1
for i in range(1, self.exons.shape[0]):
self.seglen[i*2-1] = self.exons[i,0]-self.exons[i-1,1] - 1
self.seglen[i*2] = self.exons[i,1]-self.exons[i,0] + 1
if ["-","-1","0",0,-1].count(self.strand) > 0:
self.seglen = self.seglen[::-1]
示例6: test_convert_to_normalized_and_back
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import sort [as 别名]
def test_convert_to_normalized_and_back(self):
coordinates = np.random.uniform(size=(100, 4))
coordinates = np.round(np.sort(coordinates) * 200)
coordinates[:, 2:4] += 1
coordinates[99, :] = [0, 0, 201, 201]
img = tf.ones((128, 202, 202, 3))
boxlist = box_list.BoxList(tf.constant(coordinates, tf.float32))
boxlist = box_list_ops.to_normalized_coordinates(boxlist,
tf.shape(img)[1],
tf.shape(img)[2])
boxlist = box_list_ops.to_absolute_coordinates(boxlist,
tf.shape(img)[1],
tf.shape(img)[2])
with self.test_session() as sess:
out = sess.run(boxlist.get())
self.assertAllClose(out, coordinates)
示例7: test_convert_to_absolute_and_back
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import sort [as 别名]
def test_convert_to_absolute_and_back(self):
coordinates = np.random.uniform(size=(100, 4))
coordinates = np.sort(coordinates)
coordinates[99, :] = [0, 0, 1, 1]
img = tf.ones((128, 202, 202, 3))
boxlist = box_list.BoxList(tf.constant(coordinates, tf.float32))
boxlist = box_list_ops.to_absolute_coordinates(boxlist,
tf.shape(img)[1],
tf.shape(img)[2])
boxlist = box_list_ops.to_normalized_coordinates(boxlist,
tf.shape(img)[1],
tf.shape(img)[2])
with self.test_session() as sess:
out = sess.run(boxlist.get())
self.assertAllClose(out, coordinates)
示例8: test_all_columns_accounted_for
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import sort [as 别名]
def test_all_columns_accounted_for(self):
# Note: deliberately setting to small number so not always
# all possibilities appear (matched, unmatched, ignored)
num_matches = 10
match_results = tf.random_uniform(
[num_matches], minval=-2, maxval=5, dtype=tf.int32)
match = matcher.Match(match_results)
matched_column_indices = match.matched_column_indices()
unmatched_column_indices = match.unmatched_column_indices()
ignored_column_indices = match.ignored_column_indices()
with self.test_session() as sess:
matched, unmatched, ignored = sess.run([
matched_column_indices, unmatched_column_indices,
ignored_column_indices
])
all_indices = np.hstack((matched, unmatched, ignored))
all_indices_sorted = np.sort(all_indices)
self.assertAllEqual(all_indices_sorted,
np.arange(num_matches, dtype=np.int32))
示例9: test_honor_arange
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import sort [as 别名]
def test_honor_arange(self):
"""
Tests that the input range for semi-major axis is properly set.
"""
exclude_setrange = ['EarthTwinHabZone1', 'EarthTwinHabZone2', 'JupiterTwin',
'AlbedoByRadiusDulzPlavchan', 'DulzPlavchan', 'EarthTwinHabZone1SDET',
'EarthTwinHabZone3', 'EarthTwinHabZoneSDET']
arangein = np.sort(np.random.rand(2)*10.0)
for mod in self.allmods:
if mod.__name__ not in exclude_setrange:
with RedirectStreams(stdout=self.dev_null):
obj = mod(arange = arangein,**self.spec)
#test that the correct input arange is used
self.assertTrue(obj.arange[0].value == arangein[0],'sma low bound set failed for %s'%mod.__name__)
self.assertTrue(obj.arange[1].value == arangein[1],'sma high bound set failed for %s'%mod.__name__)
# test that an incorrect input arange is corrected
with RedirectStreams(stdout=self.dev_null):
obj = mod(arange=arangein[::-1], **self.spec)
self.assertTrue(obj.arange[0].value == arangein.min(), 'sma low bound set failed for %s' % mod.__name__)
self.assertTrue(obj.arange[1].value == arangein.max(), 'sma high bound set failed for %s' % mod.__name__)
示例10: test_revise_update
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import sort [as 别名]
def test_revise_update(self):
"""
Ensure that target completeness update revisions are appropriately sized
"""
for mod in self.allmods:
if 'revise_updates' not in mod.__dict__:
continue
with RedirectStreams(stdout=self.dev_null):
obj = mod(**copy.deepcopy(self.spec))
obj.gen_update(self.TL)
ind = np.sort(np.random.choice(self.TL.nStars,size=int(self.TL.nStars/2.0),replace=False))
obj.revise_updates(ind)
self.assertTrue(obj.updates.shape == (len(ind),5),"Updates array improperly resized for %s"%mod.__name__)
示例11: train_model
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import sort [as 别名]
def train_model(self):
minibatch = random.sample(self.memory, self.batch_size)
state_stack = [mini[0] for mini in minibatch]
next_state_stack = [mini[1] for mini in minibatch]
action_stack = [mini[2] for mini in minibatch]
reward_stack = [mini[3] for mini in minibatch]
done_stack = [mini[4] for mini in minibatch]
done_stack = [int(i) for i in done_stack]
onehotaction = np.zeros([self.batch_size, self.output_size])
for i, j in zip(onehotaction, action_stack):
i[j] = 1
action_stack = np.stack(onehotaction)
Q_next_state = self.sess.run(self.target_network, feed_dict={self.targetNet.input: next_state_stack})
next_action = np.argmax(np.mean(Q_next_state, axis=2), axis=1)
Q_next_state_next_action = [Q_next_state[i, action, :] for i, action in enumerate(next_action)]
Q_next_state_next_action = np.sort(Q_next_state_next_action)
T_theta = [np.ones(self.num_support) * reward if done else reward + self.gamma * Q for reward, Q, done in
zip(reward_stack, Q_next_state_next_action, done_stack)]
_, l = self.sess.run([self.train_op, self.loss],
feed_dict={self.mainNet.input: state_stack, self.action: action_stack, self.Y: T_theta})
return l
示例12: map_onto_unit_simplex
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import sort [as 别名]
def map_onto_unit_simplex(rnd, method):
n_points, n_dim = rnd.shape
if method == "sum":
ret = rnd / rnd.sum(axis=1)[:, None]
elif method == "kraemer":
M = sys.maxsize
rnd *= M
rnd = rnd[:, :n_dim - 1]
rnd = np.column_stack([np.zeros(n_points), rnd, np.full(n_points, M)])
rnd = np.sort(rnd, axis=1)
ret = np.full((n_points, n_dim), np.nan)
for i in range(1, n_dim + 1):
ret[:, i - 1] = rnd[:, i] - rnd[:, i - 1]
ret /= M
else:
raise Exception("Invalid unit simplex mapping!")
return ret
示例13: order_crossover_contributed_no_shift
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import sort [as 别名]
def order_crossover_contributed_no_shift(x1, x2, seq=None):
assert len(x1) == len(x2)
if seq is not None:
start, end = seq
else:
start, end = np.sort(np.random.choice(len(x1), 2, replace=False))
y1 = x1.copy()
y2 = x2.copy()
# build y1 and y2
segment1 = set(y1[start:end])
segment2 = set(y2[start:end])
I = np.concatenate((np.arange(0, start), np.arange(end, len(x1))))
# find elements in x2 that are not in segment1
y1[I] = [y for y in x2 if y not in segment1]
# find elements in x1 that are not in segment2
y2[I] = [y for y in x1 if y not in segment2]
return y1, y2
示例14: diagonalize
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import sort [as 别名]
def diagonalize(a, b, nroots=4):
a_aa, a_ab, a_bb = a
b_aa, b_ab, b_bb = b
nocc_a, nvir_a, nocc_b, nvir_b = a_ab.shape
a_aa = a_aa.reshape((nocc_a*nvir_a,nocc_a*nvir_a))
a_ab = a_ab.reshape((nocc_a*nvir_a,nocc_b*nvir_b))
a_bb = a_bb.reshape((nocc_b*nvir_b,nocc_b*nvir_b))
b_aa = b_aa.reshape((nocc_a*nvir_a,nocc_a*nvir_a))
b_ab = b_ab.reshape((nocc_a*nvir_a,nocc_b*nvir_b))
b_bb = b_bb.reshape((nocc_b*nvir_b,nocc_b*nvir_b))
a = numpy.bmat([[ a_aa , a_ab],
[ a_ab.T, a_bb]])
b = numpy.bmat([[ b_aa , b_ab],
[ b_ab.T, b_bb]])
e = numpy.linalg.eig(numpy.bmat([[a , b ],
[-b.conj(),-a.conj()]]))[0]
lowest_e = numpy.sort(e[e.real > 0].real)[:nroots]
return lowest_e
示例15: dynamic_occ_
# 需要导入模块: import numpy [as 别名]
# 或者: from numpy import sort [as 别名]
def dynamic_occ_(mf, tol=1e-3):
assert(isinstance(mf, hf.RHF))
old_get_occ = mf.get_occ
def get_occ(mo_energy, mo_coeff=None):
mol = mf.mol
nocc = mol.nelectron // 2
sort_mo_energy = numpy.sort(mo_energy)
lumo = sort_mo_energy[nocc]
if abs(sort_mo_energy[nocc-1] - lumo) < tol:
mo_occ = numpy.zeros_like(mo_energy)
mo_occ[mo_energy<lumo] = 2
lst = abs(mo_energy - lumo) < tol
mo_occ[lst] = 0
logger.warn(mf, 'set charge = %d', mol.charge+int(lst.sum())*2)
logger.info(mf, 'HOMO = %.12g LUMO = %.12g',
sort_mo_energy[nocc-1], sort_mo_energy[nocc])
logger.debug(mf, ' mo_energy = %s', sort_mo_energy)
else:
mo_occ = old_get_occ(mo_energy, mo_coeff)
return mo_occ
mf.get_occ = get_occ
return mf