Here are the examples of the python api numpy.npmin taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
2 Examples
0
Example 1
Project: TADbit Source File: tad_cmo.py
def optimal_cmo(hic1, hic2, num_v=None, max_num_v=None, verbose=False,
method='frobenius', long_nw=True, long_dist=True):
"""
Calculates the optimal contact map overlap between 2 matrices
TODO: make the selection of number of eigen vectors automatic or relying on
the summed contribution (e.g. select the EVs that sum 80% of the info)
.. note::
penalty is defined as the minimum value of the pre-scoring matrix.
:param hic1: first matrix to align
:param hic2: second matrix to align
:param None num_v: number of eigen vectors to consider, max is:
max(min(len(hic1), len(hic2)))
:param None max_num_v: maximum number of eigen vectors to consider.
:param score method: distance function to use as alignment score. if 'score'
distance will be the result of the last value of the Needleman-Wunsch
algorithm. If 'frobenius' a modification of the Frobenius distance will
be used
:returns: two lists, one per aligned matrix, plus a dict summarizing the
goodness of the alignment with the distance between matrices, their
Spearman correlation Rho value and pvalue.
"""
l_p1 = len(hic1)
l_p2 = len(hic2)
num_v = num_v or min(l_p1, l_p2)
if max_num_v:
num_v = min(max_num_v, num_v)
if num_v > l_p1 or num_v > l_p2:
raise Exception('\nnum_v should be at most %s\n' % (min(l_p1, l_p2)))
val1, vec1 = eigh(hic1)
if npsum(vec1).imag:
raise Exception("ERROR: Hi-C data is not symmetric.\n" +
'%s\n\n%s' % (hic1, vec1))
val2, vec2 = eigh(hic2)
if npsum(vec2).imag:
raise Exception("ERROR: Hi-C data is not symmetric.\n" +
'%s\n\n%s' % (hic2, vec2))
#
val1 = array([sqrt(abs(v)) for v in val1])
val2 = array([sqrt(abs(v)) for v in val2])
idx = val1.argsort()[::-1]
val1 = val1[idx]
vec1 = vec1[idx]
idx = val2.argsort()[::-1]
val2 = val2[idx]
vec2 = vec2[idx]
#
vec1 = array([val1[i] * vec1[:, i] for i in xrange(num_v)]).transpose()
vec2 = array([val2[i] * vec2[:, i] for i in xrange(num_v)]).transpose()
nearest = float('inf')
nw = core_nw_long if long_nw else core_nw
dister = _get_dist_long if long_dist else _get_dist
best_alis = []
for num in xrange(1, num_v + 1):
for factors in product([1, -1], repeat=num):
vec1p = factors * vec1[:, :num]
vec2p = vec2[:, :num]
p_scores = _prescoring(vec1p, vec2p, l_p1, l_p2)
penalty = min([npmin(p_scores)] + [-npmax(p_scores)])
align1, align2, dist = nw(p_scores, penalty, l_p1, l_p2)
try:
if method == 'frobenius':
dist = dister(align1, align2, hic1, hic2)
else:
dist *= -1
if dist < nearest:
if not penalty:
for scr in p_scores:
print ' '.join(['%7s' % (round(y, 2)) for y in scr])
nearest = dist
best_alis = [align1, align2]
best_pen = penalty
except IndexError as e:
print e
try:
align1, align2 = best_alis
except ValueError:
pass
if verbose:
print '\n Alignment (score = %s):' % (nearest)
print 'TADS 1: '+'|'.join(['%4s' % (str(int(x)) \
if x!='-' else '-'*3) for x in align1])
print 'TADS 2: '+'|'.join(['%4s' % (str(int(x)) \
if x!='-' else '-'*3) for x in align2])
rho, pval = _get_score(align1, align2, hic1, hic2)
# print best_pen
if not best_pen:
print 'WARNING: penalty NULL!!!\n\n'
return align1, align2, {'dist': nearest, 'rho': rho, 'pval': pval}
0
Example 2
Project: gwpy Source File: timeseries.py
def gen_plot(self, args):
"""Generate the plot from time series and arguments"""
self.max_size = 16384. * 6400. # that works on my mac
self.yscale_factor = 1.0
from gwpy.plotter.tex import label_to_latex
from numpy import min as npmin
from numpy import max as npmax
if self.timeseries[0].size <= self.max_size:
self.plot = self.timeseries[0].plot()
else:
self.plot = self.timeseries[0].plot(linestyle='None', marker='.')
self.ymin = self.timeseries[0].min().value
self.ymax = self.timeseries[0].max().value
self.xmin = self.timeseries[0].times.value.min()
self.xmax = self.timeseries[0].times.value.max()
if len(self.timeseries) > 1:
for idx in range(1, len(self.timeseries)):
chname = self.timeseries[idx].channel.name
lbl = label_to_latex(chname)
if self.timeseries[idx].size <= self.max_size:
self.plot.add_timeseries(self.timeseries[idx], label=lbl)
else:
self.plot.add_timeseries(self.timeseries[idx], label=lbl,
linestyle='None', marker='.')
self.ymin = min(self.ymin, self.timeseries[idx].min().value)
self.ymax = max(self.ymax, self.timeseries[idx].max().value)
self.xmin = min(self.xmin,
self.timeseries[idx].times.value.min())
self.xmax = max(self.xmax,
self.timeseries[idx].times.value.max())
# if they chose to set the range of the x-axis find the range of y
strt = self.xmin
stop = self.xmax
# a bit weird but global ymax will be >= any value in
# the range same for ymin
new_ymin = self.ymax
new_ymax = self.ymin
if args.xmin:
strt = float(args.xmin)
if args.xmax:
stop = float(args.xmax)
if strt != self.xmin or stop != self.xmax:
for idx in range(0, len(self.timeseries)):
x0 = self.timeseries[idx].x0.value
dt = self.timeseries[idx].dt.value
if strt < 1e8:
strt += x0
if stop < 1e8:
stop += x0
b = int(max(0, (strt - x0) / dt))
e = int(min(self.xmax, (stop - x0) / dt))
if e >= self.timeseries[idx].size:
e = self.timeseries[idx].size - 1
new_ymin = min(new_ymin, npmin(self.timeseries[idx].value[b:e]))
new_ymax = max(new_ymax, npmax(self.timeseries[idx].value[b:e]))
self.ymin = new_ymin
self.ymax = new_ymax
if self.yscale_factor > 1:
self.log(2, ('Scaling y-limits, original: %f, %f)' %
(self.ymin, self.ymax)))
yrange = self.ymax - self.ymin
mid = (self.ymax + self.ymin) / 2.
self.ymax = mid + yrange / (2 * self.yscale_factor)
self.ymin = mid - yrange / (2 * self.yscale_factor)
self.log(2, ('Scaling y-limits, new: %f, %f)' %
(self.ymin, self.ymax)))
return