本文整理汇总了Python中numpy.nanargmin函数的典型用法代码示例。如果您正苦于以下问题:Python nanargmin函数的具体用法?Python nanargmin怎么用?Python nanargmin使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了nanargmin函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: divide_spans
def divide_spans(complete_df):
'''
Divide individual variables into useful spans and return an array of transition times.
'''
span_variables = list(complete_df.key_measures)
span_cutoffs = []
for a_variable in span_variables:
data_values = complete_df.mloc(measures = [a_variable])[:, 0, :]
# Middle overall value.
if a_variable in ['intensity_95', 'intensity_90', 'intensity_80', 'life_texture']:
cutoff_value = np.ndarray.flatten(data_values)
cutoff_value = cutoff_value[~np.isnan(cutoff_value)]
cutoff_value = np.mean(cutoff_value)
span_side = np.abs(data_values - cutoff_value)
span_side = np.nanargmin(span_side, axis = 1)
span_cutoffs.append(span_side)
# Overall young adult value is 'healthy'.
elif a_variable in ['bulk_movement']:
young_adult = np.abs(complete_df.mloc(measures = ['egg_age'])[:, 0, :])
young_adult = np.nanargmin(young_adult, axis = 1)
cutoff_value = np.array([data_values[i, young_adult[i]] for i in range(0, young_adult.shape[0])])
cutoff_value = np.mean(cutoff_value)/2
span_side = np.abs(data_values - cutoff_value)
span_side = np.nanargmin(span_side, axis = 1)
span_cutoffs.append(span_side)
# Point of own first maximum value.
elif a_variable in ['total_size', 'cumulative_eggs', 'cumulative_area', 'adjusted_size']:
span_side = np.nanargmax(data_values, axis = 1)
span_cutoffs.append(span_side)
# Raise an exception if I can't find the variable.
else:
raise BaseException('Can\'t find ' + a_variable + '.')
return (span_variables, np.array(span_cutoffs))
示例2: getPerpContourInd
def getPerpContourInd(skeleton, skel_ind, contour_side1, contour_side2, contour_width):
#get the closest point in the contour from a line perpedicular to the skeleton.
#get the slop of a line perpendicular to the keleton
dR = skeleton[skel_ind+1] - skeleton[skel_ind-1]
#m = dR[1]/dR[0]; M = -1/m
a = -dR[0]
b = +dR[1]
c = b*skeleton[skel_ind,1]-a*skeleton[skel_ind,0]
max_width_squared = np.max(contour_width)**2
#modified from https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line
#a = M, b = -1
#make sure we are not selecting a point that get traversed by a coiled worm
dist2cnt1 = np.sum((contour_side1-skeleton[skel_ind])**2,axis=1)
d1 = np.abs(a*contour_side1[:,0] - b*contour_side1[:,1]+ c)
d1[dist2cnt1>max_width_squared] = np.nan
cnt1_ind = np.nanargmin(d1)
dist2cnt2 = np.sum((contour_side2-skeleton[skel_ind])**2,axis=1)
d2 = np.abs(a*contour_side2[:,0] - b*contour_side2[:,1]+ c)
d2[dist2cnt2>max_width_squared] = np.nan
cnt2_ind = np.nanargmin(d2)
return cnt1_ind, cnt2_ind
示例3: test_reductions_2D_int
def test_reductions_2D_int():
x = np.arange(1, 122).reshape((11, 11)).astype('i4')
a = da.from_array(x, chunks=(4, 4))
reduction_2d_test(da.sum, a, np.sum, x)
reduction_2d_test(da.prod, a, np.prod, x)
reduction_2d_test(da.mean, a, np.mean, x)
reduction_2d_test(da.var, a, np.var, x, False) # Difference in dtype algo
reduction_2d_test(da.std, a, np.std, x, False) # Difference in dtype algo
reduction_2d_test(da.min, a, np.min, x, False)
reduction_2d_test(da.max, a, np.max, x, False)
reduction_2d_test(da.any, a, np.any, x, False)
reduction_2d_test(da.all, a, np.all, x, False)
reduction_2d_test(da.nansum, a, np.nansum, x)
with ignoring(AttributeError):
reduction_2d_test(da.nanprod, a, np.nanprod, x)
reduction_2d_test(da.nanmean, a, np.mean, x)
reduction_2d_test(da.nanvar, a, np.nanvar, x, False) # Difference in dtype algo
reduction_2d_test(da.nanstd, a, np.nanstd, x, False) # Difference in dtype algo
reduction_2d_test(da.nanmin, a, np.nanmin, x, False)
reduction_2d_test(da.nanmax, a, np.nanmax, x, False)
assert eq(da.argmax(a, axis=0), np.argmax(x, axis=0))
assert eq(da.argmin(a, axis=0), np.argmin(x, axis=0))
assert eq(da.nanargmax(a, axis=0), np.nanargmax(x, axis=0))
assert eq(da.nanargmin(a, axis=0), np.nanargmin(x, axis=0))
assert eq(da.argmax(a, axis=1), np.argmax(x, axis=1))
assert eq(da.argmin(a, axis=1), np.argmin(x, axis=1))
assert eq(da.nanargmax(a, axis=1), np.nanargmax(x, axis=1))
assert eq(da.nanargmin(a, axis=1), np.nanargmin(x, axis=1))
示例4: get_spans
def get_spans(adult_df, a_variable, method = 'young', fraction = 0.5, reverse_direction = False):
'''
Get healthspans as defined by young adult function for a_variable.
'''
data_values = adult_df.mloc(measures = [a_variable])[:, 0, :]
if reverse_direction:
data_values = data_values*-1
if method == 'young':
young_adult = np.abs(adult_df.mloc(measures = ['egg_age'])[:, 0, :])
young_adult = np.nanargmin(young_adult, axis = 1)
cutoff_value = np.array([data_values[i, young_adult[i]] for i in range(0, young_adult.shape[0])])
cutoff_value = np.mean(cutoff_value)*fraction
if method == 'overall_time':
all_data = np.ndarray.flatten(data_values)
all_data = all_data[~np.isnan(all_data)]
cutoff_value = np.percentile(all_data, (1-fraction)*100)
# Compute the time of crossing the health-gero threshold.
span_side = data_values - cutoff_value
health_span_length = np.nanargmin(np.abs(span_side), axis = 1)
health_span_length = np.array([adult_df.ages[a_time]*24 for a_time in health_span_length])
# Deal with the special cases of individuals that spend their entire lives in health or gerospan.
span_list = [a_span[~np.isnan(a_span)] for a_span in span_side]
only_health = np.array([(a_span > 0).all() for a_span in span_list])
only_gero = np.array([(a_span < 0).all() for a_span in span_list])
adultspans = selectData.get_adultspans(adult_df)
health_span_length[only_health] = adultspans[only_health]
health_span_length[only_gero] = 0
return health_span_length
示例5: test_reductions_1D
def test_reductions_1D(dtype):
x = np.arange(5).astype(dtype)
a = da.from_array(x, chunks=(2,))
reduction_1d_test(da.sum, a, np.sum, x)
reduction_1d_test(da.prod, a, np.prod, x)
reduction_1d_test(da.mean, a, np.mean, x)
reduction_1d_test(da.var, a, np.var, x)
reduction_1d_test(da.std, a, np.std, x)
reduction_1d_test(da.min, a, np.min, x, False)
reduction_1d_test(da.max, a, np.max, x, False)
reduction_1d_test(da.any, a, np.any, x, False)
reduction_1d_test(da.all, a, np.all, x, False)
reduction_1d_test(da.nansum, a, np.nansum, x)
with ignoring(AttributeError):
reduction_1d_test(da.nanprod, a, np.nanprod, x)
reduction_1d_test(da.nanmean, a, np.mean, x)
reduction_1d_test(da.nanvar, a, np.var, x)
reduction_1d_test(da.nanstd, a, np.std, x)
reduction_1d_test(da.nanmin, a, np.nanmin, x, False)
reduction_1d_test(da.nanmax, a, np.nanmax, x, False)
assert eq(da.argmax(a, axis=0), np.argmax(x, axis=0))
assert eq(da.argmin(a, axis=0), np.argmin(x, axis=0))
assert eq(da.nanargmax(a, axis=0), np.nanargmax(x, axis=0))
assert eq(da.nanargmin(a, axis=0), np.nanargmin(x, axis=0))
assert eq(da.argmax(a, axis=0, split_every=2), np.argmax(x, axis=0))
assert eq(da.argmin(a, axis=0, split_every=2), np.argmin(x, axis=0))
assert eq(da.nanargmax(a, axis=0, split_every=2), np.nanargmax(x, axis=0))
assert eq(da.nanargmin(a, axis=0, split_every=2), np.nanargmin(x, axis=0))
示例6: mouse_drag
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()
示例7: __store_estimate
def __store_estimate(self):
for i_method in range(0, self.n_methods):
self.estimate[i_method, :]\
= np.unravel_index(
np.nanargmin(self.criterion_result[i_method, :, :]),
self.criterion_result[i_method, :, :].shape)
self.best_actvt[i_method]\
= self.actvt_result\
[self.estimate[i_method, 0]][self.estimate[i_method, 1]]
self.best_base[i_method]\
= self.base_result\
[self.estimate[i_method, 0]][self.estimate[i_method, 1]]
self.best_completion[i_method]\
= self.completion_result\
[self.estimate[i_method, 0]][self.estimate[i_method, 1]]
if not (self.true_width == None):
for i_method in range(0, self.n_methods):
self.estimate_given_width[i_method]\
= np.nanargmin(self.criterion_result[i_method, self.true_width, :])
self.best_actvt_given_width[i_method]\
= self.actvt_result\
[self.true_width][self.estimate_given_width[i_method]]
self.best_base_given_width[i_method]\
= self.base_result\
[self.true_width][self.estimate_given_width[i_method]]
self.best_completion_given_width[i_method]\
= self.completion_result\
[self.true_width][self.estimate_given_width[i_method]]
示例8: _initialize
def _initialize(self):
""" Enforce bounds """
system = self._system
u = system.vec['u'].array
du = system.vec['du'].array
lower = system.vec['lb'].array
upper = system.vec['ub'].array
self.alpha = 1.0
if not numpy.isnan(lower).all():
lower_const = u + self.alpha*du - lower
ind = numpy.nanargmin(lower_const)
if lower_const[ind] < 0:
self.alpha = (lower[ind] - u[ind]) / du[ind]
if not numpy.isnan(upper).all():
upper_const = upper - u - self.alpha*du
ind = numpy.nanargmin(upper_const)
if upper_const[ind] < 0:
self.alpha = (upper[ind] - u[ind]) / du[ind]
self.info = self.alpha
norm0 = self._norm()
if norm0 == 0.0:
norm0 = 1.0
system.vec['u'].array[:] += self.alpha * system.vec['du'].array[:]
norm = self._norm()
return norm0, norm
示例9: dataindex
def dataindex(x_loc,y_loc,datain_loc):
x_loc = float(x_loc)
y_loc = float(y_loc)
radius = np.sqrt(x_loc**(2.)+y_loc**(2.))
angle = np.arctan(x_loc/y_loc)
line_radius = datain_loc[np.nanargmin((np.abs(datain_loc['r']-(radius))), axis=0)]
radius = line_radius[3]
line_angle = datain_loc[datain_loc['r']==radius][np.nanargmin((np.abs((datain_loc[datain_loc['r']==radius]['theta'])-(angle))), axis=0)]
angle = line_angle[4]
return np.nanargmin((np.abs(datain_loc['r']-(radius)) + np.abs(datain_loc ['theta']-(angle))), axis=0)
示例10: mapmean
def mapmean(tempDF, meta, name = '', option = 0):
import cartopy.crs as ccrs
from cartopy.io.img_tiles import MapQuestOSM
from mpl_toolkits.axes_grid1 import make_axes_locatable
#fig = plt.figure(figsize=(30, 30))
x = meta['location:Longitude'].values
y = meta['location:Latitude'].values
c = tempDF[meta.index].mean()
marker_size = 350
imagery = MapQuestOSM()
fig = plt.figure(figsize=[15,15])
ax = plt.axes(projection=imagery.crs)
ax.set_extent(( meta['location:Longitude'].min()-.005,
meta['location:Longitude'].max()+.005 ,
meta['location:Latitude'].min()-.005,
meta['location:Latitude'].max()+.005))
ax.add_image(imagery, 14)
cmap = matplotlib.cm.OrRd
bounds = np.linspace(round((c.mean()-3)),round((c.mean()+3)),13)
norm = matplotlib.colors.BoundaryNorm(bounds, cmap.N)
plotHandle = ax.scatter(x,y,c = c, s = marker_size, transform=ccrs.Geodetic(),
cmap = cmap,
norm = norm)
if option ==0 :
cbar1 = plt.colorbar(plotHandle, label = 'Temperature in $^\circ $C')
else :
cbar1 = plt.colorbar(plotHandle, label = option)
lon = x[np.nanargmax(c)]
lat = y[np.nanargmax(c)]
at_x, at_y = ax.projection.transform_point(lon, lat,
src_crs=ccrs.Geodetic())
plt.annotate(
'%2.1f'%np.nanmax(c.values), xy=(at_x, at_y), #xytext=(30, 20), textcoords='offset points',
color='black', backgroundcolor='none', size=22,
)
lon = x[np.nanargmin(c)]
lat = y[np.nanargmin(c)]
at_x, at_y = ax.projection.transform_point(lon, lat,
src_crs=ccrs.Geodetic())
plt.annotate(
'%2.1f'%np.nanmin(c.values), xy=(at_x, at_y), #xytext=(30, 20), textcoords='offset points',
color='black', size = 22, backgroundcolor='none')
plt.annotate(
'$\mu = $ %2.1f, $\sigma = $ %2.1f'%(np.nanmean(c.values), np.nanstd(c.values)), (0.01,0.01), xycoords ='axes fraction', #xytext=(30, 20), textcoords='offset points',
color='black', size = 22, backgroundcolor='none')
plt.title('Mean Temperature %s'%name)
filename = './plots/meantempmap%s.eps'%name
plt.savefig(filename, format = 'eps', dpi = 600)
示例11: closest_real_time
def closest_real_time(complete_df, a_worm, a_time, egg_mode = True):
'''
For a_worm at a_time, find the closest real time point.
'''
time_split = a_time.split('.')
hours_time = int(time_split[0])*24 + int(time_split[1])*3
if not egg_mode:
time_index = np.nanargmin(np.abs(complete_df.raw[a_worm].loc[:, 'age'] - hours_time))
else:
time_index = np.nanargmin(np.abs(complete_df.raw[a_worm].loc[:, 'egg_age'] - hours_time))
real_time = complete_df.raw[a_worm].index[time_index]
return real_time
示例12: test_reductions_2D_nans
def test_reductions_2D_nans():
# chunks are a mix of some/all/no NaNs
x = np.full((4, 4), np.nan)
x[:2, :2] = np.array([[1, 2], [3, 4]])
x[2, 2] = 5
x[3, 3] = 6
a = da.from_array(x, chunks=(2, 2))
reduction_2d_test(da.sum, a, np.sum, x, False, False)
reduction_2d_test(da.prod, a, np.prod, x, False, False)
reduction_2d_test(da.mean, a, np.mean, x, False, False)
reduction_2d_test(da.var, a, np.var, x, False, False)
reduction_2d_test(da.std, a, np.std, x, False, False)
reduction_2d_test(da.min, a, np.min, x, False, False)
reduction_2d_test(da.max, a, np.max, x, False, False)
reduction_2d_test(da.any, a, np.any, x, False, False)
reduction_2d_test(da.all, a, np.all, x, False, False)
reduction_2d_test(da.nansum, a, np.nansum, x, False, False)
reduction_2d_test(da.nanprod, a, nanprod, x, False, False)
reduction_2d_test(da.nanmean, a, np.nanmean, x, False, False)
with pytest.warns(None): # division by 0 warning
reduction_2d_test(da.nanvar, a, np.nanvar, x, False, False)
with pytest.warns(None): # division by 0 warning
reduction_2d_test(da.nanstd, a, np.nanstd, x, False, False)
with pytest.warns(None): # all NaN axis warning
reduction_2d_test(da.nanmin, a, np.nanmin, x, False, False)
with pytest.warns(None): # all NaN axis warning
reduction_2d_test(da.nanmax, a, np.nanmax, x, False, False)
assert_eq(da.argmax(a), np.argmax(x))
assert_eq(da.argmin(a), np.argmin(x))
with pytest.warns(None): # all NaN axis warning
assert_eq(da.nanargmax(a), np.nanargmax(x))
with pytest.warns(None): # all NaN axis warning
assert_eq(da.nanargmin(a), np.nanargmin(x))
assert_eq(da.argmax(a, axis=0), np.argmax(x, axis=0))
assert_eq(da.argmin(a, axis=0), np.argmin(x, axis=0))
with pytest.warns(None): # all NaN axis warning
assert_eq(da.nanargmax(a, axis=0), np.nanargmax(x, axis=0))
with pytest.warns(None): # all NaN axis warning
assert_eq(da.nanargmin(a, axis=0), np.nanargmin(x, axis=0))
assert_eq(da.argmax(a, axis=1), np.argmax(x, axis=1))
assert_eq(da.argmin(a, axis=1), np.argmin(x, axis=1))
with pytest.warns(None): # all NaN axis warning
assert_eq(da.nanargmax(a, axis=1), np.nanargmax(x, axis=1))
with pytest.warns(None): # all NaN axis warning
assert_eq(da.nanargmin(a, axis=1), np.nanargmin(x, axis=1))
示例13: maximum_posterior
def maximum_posterior(self):
"""Returns the maximum log posterior (minimum negative log posterior)
from the set of chains, along with the position giving the maximum
posterior.
"""
if not self.chains:
raise Exception("There are no chains in the MCMCSet.")
max_posterior = np.inf
max_posterior_position = None
for chain in self.chains:
# Make sure the chain is not empty!
if len(chain.posteriors) > 0:
chain_max_posterior_index = np.nanargmin(chain.posteriors)
chain_max_posterior = \
chain.posteriors[chain_max_posterior_index]
if chain_max_posterior < max_posterior:
max_posterior = chain_max_posterior
max_posterior_position = \
chain.positions[chain_max_posterior_index]
# Check if there are no positions
if max_posterior_position is None:
raise NoPositionsException('The maximum posterior could not be determined '
'because there are no accepted positions.')
return (max_posterior, max_posterior_position)
示例14: apply_roi
def apply_roi(self, roi):
if not isinstance(roi, PointROI):
raise NotImplementedError("Only PointROI supported")
if self._layout is None or self.display_data is None:
return
x, y = roi.x, roi.y
if not roi.defined():
return
xs, ys = self._layout[:, ::3]
parent_ys = self._layout[1, 1::3]
delt = np.abs(x - xs)
delt[y > ys] = np.nan
delt[y < parent_ys] = np.nan
if np.isfinite(delt).any():
select = np.nanargmin(delt)
if self.select_substruct:
select = self._substructures(select)
select = np.asarray(select, dtype=np.int)
else:
select = np.array([], dtype=np.int)
state = CategorySubsetState(self.display_data.pixel_component_ids[0],
select)
EditSubsetMode().update(self.collect, state,
focus_data=self.display_data)
示例15: maximum_likelihood
def maximum_likelihood(self):
"""Returns the maximum log likelihood (minimum negative log likelihood)
from the set of chains, along with the position giving the maximum
likelihood.
"""
if not self.chains:
raise Exception("There are no chains in the MCMCSet.")
max_likelihood = np.inf
max_likelihood_position = None
for chain in self.chains:
# Make sure the chain is not empty!
if len(chain.likelihoods) > 0:
chain_max_likelihood_index = np.nanargmin(chain.likelihoods)
chain_max_likelihood = \
chain.likelihoods[chain_max_likelihood_index]
if chain_max_likelihood < max_likelihood:
max_likelihood = chain_max_likelihood
max_likelihood_position = \
chain.positions[chain_max_likelihood_index]
# Check if there are no positions
if max_likelihood_position is None:
raise NoPositionsException('The maximum likelihood could not be '
'determined because there are no accepted positions.')
return (max_likelihood, max_likelihood_position)