def mouse_drag(self, event):
'''
'''
if event.inaxes == self.ax and event.button == 1:
# Index of nearest point
i = np.nanargmin(((event.xdata - self.x) / self.nx) ** 2)
j = np.nanargmin(((event.ydata - self.y) / self.ny) ** 2)
if (i == self.last_i) and (j == self.last_j):
return
else:
self.last_i = i
self.last_j = j
# Toggle pixel
if self.aperture[j,i]:
self.aperture[j,i] = 0
else:
self.aperture[j,i] = 1
# Update the contour
self.update()
python类nanargmin()的实例源码
def mouse_click(self, event):
'''
'''
if event.mouseevent.inaxes == self.ax:
# Index of nearest point
i = np.nanargmin(((event.mouseevent.xdata - self.x) / self.nx) ** 2)
j = np.nanargmin(((event.mouseevent.ydata - self.y) / self.ny) ** 2)
self.last_i = i
self.last_j = j
# Toggle pixel
if self.aperture[j,i]:
self.aperture[j,i] = 0
else:
self.aperture[j,i] = 1
# Update the contour
self.update()
def update(self, arx, xarchive=None, arf=None, evals=None):
"""checks for better solutions in list ``arx``.
Based on the smallest corresponding value in ``arf``,
alternatively, `update` may be called with a `BestSolution`
instance like ``update(another_best_solution)`` in which case
the better solution becomes the current best.
``xarchive`` is used to retrieve the genotype of a solution.
"""
if isinstance(arx, BestSolution):
if self.evalsall is None:
self.evalsall = arx.evalsall
elif arx.evalsall is not None:
self.evalsall = max((self.evalsall, arx.evalsall))
if arx.f is not None and arx.f < np.inf:
self.update([arx.x], xarchive, [arx.f], arx.evals)
return self
assert arf is not None
# find failsave minimum
try:
minidx = np.nanargmin(arf)
except ValueError:
return
if minidx is np.nan:
return
minarf = arf[minidx]
# minarf = reduce(lambda x, y: y if y and y is not np.nan
# and y < x else x, arf, np.inf)
if minarf < np.inf and (minarf < self.f or self.f is None):
self.x, self.f = arx[minidx], arf[minidx]
if xarchive is not None and xarchive.get(self.x) is not None:
self.x_geno = xarchive[self.x].get('geno')
else:
self.x_geno = None
self.evals = None if not evals else evals - len(arf) + minidx + 1
self.evalsall = evals
elif evals:
self.evalsall = evals
self.last.x = arx[minidx]
self.last.f = minarf
def test_nanargmin(self):
tgt = np.argmin(self.mat)
for mat in self.integer_arrays():
assert_equal(np.nanargmin(mat), tgt)
def update(self, arx, xarchive=None, arf=None, evals=None):
"""checks for better solutions in list `arx`.
Based on the smallest corresponding value in `arf`,
alternatively, `update` may be called with a `BestSolution`
instance like ``update(another_best_solution)`` in which case
the better solution becomes the current best.
`xarchive` is used to retrieve the genotype of a solution.
"""
if isinstance(arx, BestSolution):
if self.evalsall is None:
self.evalsall = arx.evalsall
elif arx.evalsall is not None:
self.evalsall = max((self.evalsall, arx.evalsall))
if arx.f is not None and arx.f < np.inf:
self.update([arx.x], xarchive, [arx.f], arx.evals)
return self
assert arf is not None
# find failsave minimum
minidx = np.nanargmin(arf)
if minidx is np.nan:
return
minarf = arf[minidx]
# minarf = reduce(lambda x, y: y if y and y is not np.nan
# and y < x else x, arf, np.inf)
if minarf < np.inf and (minarf < self.f or self.f is None):
self.x, self.f = arx[minidx], arf[minidx]
if xarchive is not None and xarchive.get(self.x) is not None:
self.x_geno = xarchive[self.x].get('geno')
else:
self.x_geno = None
self.evals = None if not evals else evals - len(arf) + minidx + 1
self.evalsall = evals
elif evals:
self.evalsall = evals
self.last.x = arx[minidx]
self.last.f = minarf
def update(self, arx, xarchive=None, arf=None, evals=None):
"""checks for better solutions in list `arx`.
Based on the smallest corresponding value in `arf`,
alternatively, `update` may be called with a `BestSolution`
instance like ``update(another_best_solution)`` in which case
the better solution becomes the current best.
`xarchive` is used to retrieve the genotype of a solution.
"""
if isinstance(arx, BestSolution):
if self.evalsall is None:
self.evalsall = arx.evalsall
elif arx.evalsall is not None:
self.evalsall = max((self.evalsall, arx.evalsall))
if arx.f is not None and arx.f < np.inf:
self.update([arx.x], xarchive, [arx.f], arx.evals)
return self
assert arf is not None
# find failsave minimum
minidx = np.nanargmin(arf)
if minidx is np.nan:
return
minarf = arf[minidx]
# minarf = reduce(lambda x, y: y if y and y is not np.nan
# and y < x else x, arf, np.inf)
if minarf < np.inf and (minarf < self.f or self.f is None):
self.x, self.f = arx[minidx], arf[minidx]
if xarchive is not None and xarchive.get(self.x) is not None:
self.x_geno = xarchive[self.x].get('geno')
else:
self.x_geno = None
self.evals = None if not evals else evals - len(arf) + minidx + 1
self.evalsall = evals
elif evals:
self.evalsall = evals
self.last.x = arx[minidx]
self.last.f = minarf
def update(self, arx, xarchive=None, arf=None, evals=None):
"""checks for better solutions in list `arx`.
Based on the smallest corresponding value in `arf`,
alternatively, `update` may be called with a `BestSolution`
instance like ``update(another_best_solution)`` in which case
the better solution becomes the current best.
`xarchive` is used to retrieve the genotype of a solution.
"""
if isinstance(arx, BestSolution):
if self.evalsall is None:
self.evalsall = arx.evalsall
elif arx.evalsall is not None:
self.evalsall = max((self.evalsall, arx.evalsall))
if arx.f is not None and arx.f < np.inf:
self.update([arx.x], xarchive, [arx.f], arx.evals)
return self
assert arf is not None
# find failsave minimum
minidx = np.nanargmin(arf)
if minidx is np.nan:
return
minarf = arf[minidx]
# minarf = reduce(lambda x, y: y if y and y is not np.nan
# and y < x else x, arf, np.inf)
if minarf < np.inf and (minarf < self.f or self.f is None):
self.x, self.f = arx[minidx], arf[minidx]
if xarchive is not None and xarchive.get(self.x) is not None:
self.x_geno = xarchive[self.x].get('geno')
else:
self.x_geno = None
self.evals = None if not evals else evals - len(arf) + minidx + 1
self.evalsall = evals
elif evals:
self.evalsall = evals
self.last.x = arx[minidx]
self.last.f = minarf
def test_nanargmin(self):
tgt = np.argmin(self.mat)
for mat in self.integer_arrays():
assert_equal(np.nanargmin(mat), tgt)
def figure_mouse_pick(self, event):
"""
Trigger for when the mouse is used to select an item in the figure.
:param event:
The matplotlib event.
"""
ycol = "abundance"
xcol = {
self.ax_excitation_twin: "expot",
self.ax_line_strength_twin: "reduced_equivalent_width"
}[event.inaxes]
xscale = np.ptp(event.inaxes.get_xlim())
yscale = np.ptp(event.inaxes.get_ylim())
try:
distance = np.sqrt(
((self._state_transitions[ycol] - event.ydata)/yscale)**2 \
+ ((self._state_transitions[xcol] - event.xdata)/xscale)**2)
except AttributeError:
# Stellar parameters have not been measured yet
return None
index = np.nanargmin(distance)
# Because the state transitions are linked to the parent source model of
# the table view, we will have to get the proxy index.
proxy_index = self.table_view.model().mapFromSource(
self.proxy_spectral_models.sourceModel().createIndex(index, 0)).row()
self.table_view.selectRow(proxy_index)
return None
def find_min(x, bin_width = 10):
xm = binned_average(x, bin_width=bin_width);
imin = np.nanargmin(xm);
return int((imin + 0.5) * bin_width);
def find_min(x, bin_width = 10):
xm = binned_average(x, bin_width=bin_width);
imin = np.nanargmin(xm);
return int((imin + 0.5) * bin_width);
def find_min(x, bin_width = 10):
xm = binned_average(x, bin_width=bin_width);
imin = np.nanargmin(xm);
return int((imin + 0.5) * bin_width);
def find_min(x, bin_width = 10):
xm = binned_average(x, bin_width=bin_width);
imin = np.nanargmin(xm);
return int((imin + 0.5) * bin_width);
def find_min(x, bin_width = 10):
xm = binned_average(x, bin_width=bin_width);
imin = np.nanargmin(xm);
return int((imin + 0.5) * bin_width);
def Variogram(self):
"""
:return:
"""
self.run()
# find the best Variogram
idx = np.nanargmin(self.e)
return self.V[idx]
def _select_best_measure_index(curr_measures, args):
idx = None
try:
if args.measure == 'aicc':
# The best score for AICc is the minimum.
idx = np.nanargmin(curr_measures)
elif args.measure in ['hmm-distance', 'wasserstein', 'mahalanobis']:
# The best score for the l-d measure is the maximum.
idx = np.nanargmax(curr_measures)
except:
idx = random.choice(range(len(curr_measures)))
assert idx is not None
return idx
def initial_count(classify, test_set_x, data, valid):
(valid_st2,valid_st5,valid_st8) = valid
(ns_test_set_x_st2,ns_test_set_x_st5,ns_test_set_x_st8) = data
# classify st_2 it is always valid
(st2_count, st2_res, st2_entropy) = count_in_interval(classify, test_set_x, ns_test_set_x_st2, 0, 0, 81) #100 - 19 etc.
# check if st5 is valid. if not return st2 count
if (valid_st5 == 1):
(st5_count, st5_res, st5_entropy) = count_in_interval(classify, test_set_x, ns_test_set_x_st5, 0, 0, 21)
else:
st8_entropy = numpy.inf
if (valid_st8 == 1):
(st8_count, st8_res, st8_entropy) = count_in_interval(classify, test_set_x, ns_test_set_x_st8, 0, 0, 6)
else:
st8_entropy = numpy.inf
winner = numpy.nanargmin(numpy.array([st2_entropy, st5_entropy, st8_entropy]))
if (winner == 0):
# winner is stride 2
return (st2_count, (st2_res*2/2,st2_res*2/5, st2_res*2/8))
if (winner == 1):
# winner is stride 5
return (st5_count, (st5_res*5/2,st5_res*5/5, st5_res*5/8))
if (winner == 2):
# winner is stride 8
return (st8_count, (st8_res*8/2,st8_res*8/5, st8_res*8/8))
def get_next_count(classify, test_set_x, data, valid, global_count, curr_residue, start_frame):
(valid_st2,valid_st5,valid_st8) = valid
(ns_test_set_x_st2,ns_test_set_x_st5,ns_test_set_x_st8) = data
(curr_residue_st2, curr_residue_st5, curr_residue_st8) = curr_residue
# classify st_2 it is always valid
(st2_count, st2_res, st2_entropy) = count_in_interval(classify, test_set_x, ns_test_set_x_st2, curr_residue_st2, (start_frame/2-19), (start_frame/2-19)+20)
# check if st5 is valid. if not return st2 count
if (valid_st5 == 1):
(st5_count, st5_res, st5_entropy) = count_in_interval(classify, test_set_x, ns_test_set_x_st5, curr_residue_st5, (start_frame/5-19), (start_frame/5-19)+8)
else:
st5_entropy = numpy.inf
if (valid_st8 == 1):
(st8_count, st8_res, st8_entropy) = count_in_interval(classify, test_set_x, ns_test_set_x_st8, curr_residue_st8, (start_frame/8-19), (start_frame/8-19)+5)
else:
st8_entropy = numpy.inf
winner = numpy.nanargmin(numpy.array([st2_entropy, st5_entropy, st8_entropy]))
if (winner == 0):
# winner is stride 2
return (global_count + st2_count, (st2_res*2/2,st2_res*2/5, st2_res*2/8))
if (winner == 1):
# winner is stride 5
return (global_count + st5_count, (st5_res*5/2,st5_res*5/5, st5_res*5/8))
if (winner == 2):
# winner is stride 8
return (global_count + st8_count, (st8_res*8/2,st8_res*8/5, st8_res*8/8))
def get_remain_count(classify, test_set_x, data, valid, global_count, curr_residue, start_frame):
(valid_st2,valid_st5,valid_st8) = valid
(ns_test_set_x_st2,ns_test_set_x_st5,ns_test_set_x_st8) = data
(curr_residue_st2, curr_residue_st5, curr_residue_st8) = curr_residue
# classify st_2 it is always valid
(st2_count, st2_res, st2_entropy) = count_in_interval(classify, test_set_x, ns_test_set_x_st2, curr_residue_st2, (start_frame/2-19), ns_test_set_x_st2.shape[0])
# check if st5 is valid. if not return st2 count
if (valid_st5 == 1):
(st5_count, st5_res, st5_entropy) = count_in_interval(classify, test_set_x, ns_test_set_x_st5, curr_residue_st5, (start_frame/5-19), ns_test_set_x_st5.shape[0])
else:
st5_entropy = numpy.inf
if (valid_st8 == 1):
(st8_count, st8_res, st8_entropy) = count_in_interval(classify, test_set_x, ns_test_set_x_st8, curr_residue_st8, (start_frame/8-19), ns_test_set_x_st8.shape[0])
else:
st8_entropy = numpy.inf
winner = numpy.nanargmin(numpy.array([st2_entropy, st5_entropy, st8_entropy]))
if (winner == 0):
# winner is stride 2
return (global_count + st2_count)
if (winner == 1):
# winner is stride 5
return (global_count + st5_count)
if (winner == 2):
# winner is stride 8
return (global_count + st8_count)
def count_entire_movie(classify, test_set_x, data, valid, global_count, curr_residue, start_frame):
(valid_st2,valid_st5,valid_st8) = valid
(ns_test_set_x_st2,ns_test_set_x_st5,ns_test_set_x_st8) = data
(curr_residue_st2, curr_residue_st5, curr_residue_st8) = curr_residue
# classify st_2 it is always valid
(st2_count, st2_res, st2_entropy) = count_in_interval(classify, test_set_x, ns_test_set_x_st2, curr_residue_st2, 0, ns_test_set_x_st2.shape[0])
# check if st5 is valid. if not return st2 count
if (valid_st5 == 1):
(st5_count, st5_res, st5_entropy) = count_in_interval(classify, test_set_x, ns_test_set_x_st5, curr_residue_st5, 0, ns_test_set_x_st5.shape[0])
else:
st5_entropy = numpy.inf
if (valid_st8 == 1):
(st8_count, st8_res, st8_entropy) = count_in_interval(classify, test_set_x, ns_test_set_x_st8, curr_residue_st8, 0, ns_test_set_x_st8.shape[0])
else:
st8_entropy = numpy.inf
winner = numpy.nanargmin(numpy.array([st2_entropy, st5_entropy, st8_entropy]))
if (winner == 0):
# winner is stride 2
return (global_count + st2_count)
if (winner == 1):
# winner is stride 5
return (global_count + st5_count)
if (winner == 2):
# winner is stride 8
return (global_count + st8_count)
def test_nanargmin(self):
tgt = np.argmin(self.mat)
for mat in self.integer_arrays():
assert_equal(np.nanargmin(mat), tgt)
def __find_nearest_nodes(self, num, signal, mahar=True):
#if mahar: return self.__find_nearest_nodes_by_mahar(num, signal)
n = self.nodes.shape[0]
indexes = [0.0] * num
sq_dists = [0.0] * num
D = util.calc_distance(self.nodes, np.asarray([signal] * n))
for i in range(num):
indexes[i] = np.nanargmin(D)
sq_dists[i] = D[indexes[i]]
D[indexes[i]] = float('nan')
return indexes, sq_dists
def update(self, arx, xarchive=None, arf=None, evals=None):
"""checks for better solutions in list `arx`.
Based on the smallest corresponding value in `arf`,
alternatively, `update` may be called with a `BestSolution`
instance like ``update(another_best_solution)`` in which case
the better solution becomes the current best.
`xarchive` is used to retrieve the genotype of a solution.
"""
if isinstance(arx, BestSolution):
if self.evalsall is None:
self.evalsall = arx.evalsall
elif arx.evalsall is not None:
self.evalsall = max((self.evalsall, arx.evalsall))
if arx.f is not None and arx.f < np.inf:
self.update([arx.x], xarchive, [arx.f], arx.evals)
return self
assert arf is not None
# find failsave minimum
minidx = np.nanargmin(arf)
if minidx is np.nan:
return
minarf = arf[minidx]
# minarf = reduce(lambda x, y: y if y and y is not np.nan
# and y < x else x, arf, np.inf)
if minarf < np.inf and (minarf < self.f or self.f is None):
self.x, self.f = arx[minidx], arf[minidx]
if xarchive is not None and xarchive.get(self.x) is not None:
self.x_geno = xarchive[self.x].get('geno')
else:
self.x_geno = None
self.evals = None if not evals else evals - len(arf) + minidx + 1
self.evalsall = evals
elif evals:
self.evalsall = evals
self.last.x = arx[minidx]
self.last.f = minarf
def test_nanargmin(self):
tgt = np.argmin(self.mat)
for mat in self.integer_arrays():
assert_equal(np.nanargmin(mat), tgt)
def test_nanargmin(self):
tgt = np.argmin(self.mat)
for mat in self.integer_arrays():
assert_equal(np.nanargmin(mat), tgt)
def plot(self, ax=None, write_tau=True):
"""
Returns
-------
fig : matplotlib.figure.Figure
Figure instance containing the plot.
"""
check_is_fitted(self, 'neg_log_likelihood_')
if ax is None:
fig = plt.figure()
ax = fig.gca()
else:
fig = ax.figure
blue, green, red, purple, yellow, cyan = SEABORN_PALETTES['deep']
i_best = np.nanargmin(self.neg_log_likelihood_)
ax.plot(self.delays_ms_, self.neg_log_likelihood_, color=purple)
ax.plot(self.delays_ms_[i_best], self.neg_log_likelihood_[i_best], 'D',
color=red)
ax.set_xlabel('Delay (ms)')
ax.set_ylabel('Neg. log likelihood / T')
ax.grid('on')
if write_tau:
ax.text(0.5, 0.80, r'$\mathrm{Estimated}$',
horizontalalignment='center', transform=ax.transAxes)
ax.text(0.5, 0.66, r'$\tau_0 = %.0f \;\mathrm{ms}$' %
(self.delays_ms_[i_best], ), horizontalalignment='center',
transform=ax.transAxes)
return fig
def test_delay_shape():
est = fast_delay()
assert_equal(est.neg_log_likelihood_.shape, est.delays_ms_.shape)
assert_greater(est.neg_log_likelihood_.shape[0], 1)
i_best = np.nanargmin(est.neg_log_likelihood_)
assert_equal(est.best_delay_ms_, est.delays_ms_[i_best])
def update(self, arx, xarchive=None, arf=None, evals=None):
"""checks for better solutions in list `arx`.
Based on the smallest corresponding value in `arf`,
alternatively, `update` may be called with a `BestSolution`
instance like ``update(another_best_solution)`` in which case
the better solution becomes the current best.
`xarchive` is used to retrieve the genotype of a solution.
"""
if isinstance(arx, BestSolution):
if self.evalsall is None:
self.evalsall = arx.evalsall
elif arx.evalsall is not None:
self.evalsall = max((self.evalsall, arx.evalsall))
if arx.f is not None and arx.f < np.inf:
self.update([arx.x], xarchive, [arx.f], arx.evals)
return self
assert arf is not None
# find failsave minimum
minidx = np.nanargmin(arf)
if minidx is np.nan:
return
minarf = arf[minidx]
# minarf = reduce(lambda x, y: y if y and y is not np.nan
# and y < x else x, arf, np.inf)
if minarf < np.inf and (minarf < self.f or self.f is None):
self.x, self.f = arx[minidx], arf[minidx]
if xarchive is not None and xarchive.get(self.x) is not None:
self.x_geno = xarchive[self.x].get('geno')
else:
self.x_geno = None
self.evals = None if not evals else evals - len(arf) + minidx + 1
self.evalsall = evals
elif evals:
self.evalsall = evals
self.last.x = arx[minidx]
self.last.f = minarf
def optimize_threshold_with_f1(f1c, thresholds, criterion='max'):
#f1c[np.isnan(f1c)] = 0
if criterion == 'max':
ti = np.nanargmax(f1c)
else:
ti = np.nanargmin(np.abs(thresholds-0.5*f1c))
#assert(np.all(thresholds>=0))
#idx = (thresholds>=f1c*0.5-mp) & (thresholds<=f1c*0.5+mp)
#assert(np.any(idx))
#ti = np.where(idx)[0][f1c[idx].argmax()]
return thresholds[ti], ti
def _(artist, event):
offsets = artist.get_offsets()
ds = np.hypot(
*(artist.axes.transData.transform(offsets) - [event.x, event.y]).T)
argmin = np.nanargmin(ds)
if ds[argmin] < artist.get_pickradius():
target = with_attrs(offsets[argmin], index=argmin)
return Selection(artist, target, ds[argmin], None, None)
else:
return None