def check_binary_nan(self, name, xp, dtype):
a = xp.array([-3, numpy.NAN, -1, numpy.NAN, 0, numpy.NAN, 2],
dtype=dtype)
b = xp.array([numpy.NAN, numpy.NAN, 1, 0, numpy.NAN, -1, -2],
dtype=dtype)
return getattr(xp, name)(a, b)
python类NAN的实例源码
def check_unary_nan(self, name, xp, dtype):
a = xp.array(
[-3, numpy.NAN, -1, numpy.NAN, 0, numpy.NAN, dtype('inf')],
dtype=dtype)
return getattr(xp, name)(a)
def _create_empty_variables(self):
total_gap_size = self._get_total_length_of_all_gaps()
self.new_df = pd.DataFrame(np.empty([self.orig_df.shape[0]+total_gap_size,self.orig_df.shape[1]]))
self.new_df.iloc[:]=np.NAN
self.new_time_stamps = []
self.new_i = 0
def empty_np_array_with_size(self,rows,columns):
np_array = np.zeros((rows,columns))
np_array[:] = np.NAN
return np_array
def set_fit(self, fit_functions, fit_initial_parameters, fit_optimized_parameters, **kwargs):
# if no pixel was provided the current pixel is updated
if 'pixel' not in kwargs.keys() or kwargs['pixel'] == -1:
px = self._focus[0]
else:
px = kwargs['pixel'][0]
# clear the old fit data
self._fit_functions[px, :] = numpy.zeros(6)
self._fit_initial_parameters[px, :, :] = numpy.NAN
self._fit_optimized_parameters[px, :, :] = numpy.NAN
# set new fit data
i_parameter = 0
for i_peak in range(len(fit_functions)):
if fit_functions[i_peak] == 1:
self._fit_functions[px, i_peak] = 1
self._fit_initial_parameters[px, i_peak, :3] = fit_initial_parameters[i_parameter:i_parameter+3]
self._fit_initial_parameters[px, i_peak, 3] = 0
self._fit_optimized_parameters[px, i_peak, :3] = fit_optimized_parameters[i_parameter:i_parameter+3]
self._fit_optimized_parameters[px, i_peak, 3] = 0
i_parameter += 3
elif fit_functions[i_peak] == 2:
self._fit_functions[px, i_peak] = 2
self._fit_initial_parameters[px, i_peak, :3] = fit_initial_parameters[i_parameter:i_parameter+3]
self._fit_initial_parameters[px, i_peak, 3] = 0
self._fit_optimized_parameters[px, i_peak, :3] = fit_optimized_parameters[i_parameter:i_parameter+3]
self._fit_optimized_parameters[px, i_peak, 3] = 0
i_parameter += 3
elif fit_functions[i_peak] == 3:
self._fit_functions[px, i_peak] = 3
self._fit_initial_parameters[px, i_peak, :] = fit_initial_parameters[i_parameter:i_parameter+4]
self._fit_optimized_parameters[px, i_peak, :] = fit_optimized_parameters[i_parameter:i_parameter+4]
i_parameter += 4
# emit signal
if 'emit' not in kwargs or kwargs['emit']:
self._app.fit_changed.emit(self._id)
def set_fit(self, fit_functions, fit_initial_parameters, fit_optimized_parameters, **kwargs):
# if no pixel was provided the current pixel is updated
if 'pixel' not in kwargs.keys() or kwargs['pixel'] == -1:
px = self._focus[0]
py = self._focus[1]
else:
px = kwargs['pixel'][0]
py = kwargs['pixel'][1]
# clear the old fit data
self._fit_functions[px, py, :] = numpy.zeros(6)
self._fit_initial_parameters[px, py, :, :] = numpy.NAN
self._fit_optimized_parameters[px, py, :, :] = numpy.NAN
# set new fit data
i_parameter = 0
for i_peak in range(len(fit_functions)):
if fit_functions[i_peak] == 1:
self._fit_functions[px, py, i_peak] = 1
self._fit_initial_parameters[px, py, i_peak, :3] = fit_initial_parameters[i_parameter:i_parameter+3]
self._fit_initial_parameters[px, py, i_peak, 3] = 0
self._fit_optimized_parameters[px, py, i_peak, :3] = fit_optimized_parameters[i_parameter:i_parameter+3]
self._fit_optimized_parameters[px, py, i_peak, 3] = 0
i_parameter += 3
elif fit_functions[i_peak] == 2:
self._fit_functions[px, py, i_peak] = 2
self._fit_initial_parameters[px, py, i_peak, :3] = fit_initial_parameters[i_parameter:i_parameter+3]
self._fit_initial_parameters[px, py, i_peak, 3] = 0
self._fit_optimized_parameters[px, py, i_peak, :3] = fit_optimized_parameters[i_parameter:i_parameter+3]
self._fit_optimized_parameters[px, py, i_peak, 3] = 0
i_parameter += 3
elif fit_functions[i_peak] == 3:
self._fit_functions[px, py, i_peak] = 3
self._fit_initial_parameters[px, py, i_peak, :] = fit_initial_parameters[i_parameter:i_parameter+4]
self._fit_optimized_parameters[px, py, i_peak, :] = fit_optimized_parameters[i_parameter:i_parameter+4]
i_parameter += 4
# emit signal
if 'emit' not in kwargs or kwargs['emit']:
self._app.fit_changed.emit(self._id)
def _compose_alpha(img_in, img_layer, opacity):
"""
Calculate alpha composition ratio between two images.
"""
comp_alpha = np.minimum(img_in[:, :, 3], img_layer[:, :, 3])*opacity
new_alpha = img_in[:, :, 3] + (1.0 - img_in[:, :, 3])*comp_alpha
np.seterr(divide='ignore', invalid='ignore')
ratio = comp_alpha/new_alpha
ratio[ratio == np.NAN] = 0.0
return ratio
def process(self, obj_data):
'''
NaN's data from DataWrapper
@param obj_data: Input DataWrapper, which will be modified in place
'''
labels = self.labels
column_names = self.column_names
for label, data, err in obj_data.getIterator():
if (labels is None or label in labels) and \
(column_names is None or data.name in column_names):
index = data.index
if self.start is None:
start = index[0]
else:
start = self.start
if self.end is None:
end = index[-1]
else:
end = self.end
new_nans = np.empty(len(data[start:end]))
new_nans[:] = np.NAN
new_nans = pd.Series(new_nans, index=data[start:end].index)
data.loc[start:end] = new_nans
def calc_fishers_method(pos_pvals, offset):
pvals_np = np.empty(pos_pvals[-1][1] + 1)
pvals_np[:] = np.NAN
pvals_np[[list(zip(*pos_pvals)[1])]] = np.maximum(
zip(*pos_pvals)[0], nh.SMALLEST_PVAL)
fishers_pvals = [
_calc_fm_pval(pvals_np[pos - offset:pos + offset + 1])
if pos - offset >= 0 and
pos + offset + 1 <= pvals_np.shape[0] and
not np.any(np.isnan(pvals_np[pos - offset:pos + offset + 1])
) else 1.0
for _, pos, _, _ in pos_pvals]
return fishers_pvals
def test_single_point_nan(self):
"""Test behaviour for a single NaN grid cell."""
self.cube.data[0][0][6][7] = np.NAN
msg = "NaN detected in input cube data"
with self.assertRaisesRegexp(ValueError, msg):
neighbourhood_method = CircularNeighbourhood
NBHood(neighbourhood_method, self.RADIUS).process(self.cube)
def test_threshold_point_nan(self):
"""Test behaviour for a single NaN grid cell."""
# Need to copy the cube as we're adjusting the data.
self.cube.data[0][2][2] = np.NAN
msg = "NaN detected in input cube data"
plugin = Threshold(
2.0, fuzzy_factor=self.fuzzy_factor, below_thresh_ok=True)
with self.assertRaisesRegexp(ValueError, msg):
plugin.process(self.cube)
def make_corners(self):
x = self.x_t
y = self.y_t
dx_half = np.empty_like(x)
dy_half = np.empty_like(x)
dx_half[:, 1:] = (x[:, 1:] - x[:, 0:-1]) / 2.0
dy_half[1:, :] = (y[1:, :] - y[0:-1, :]) / 2.0
# Need to extend South
dy_half[0, 1:] = dy_half[1, 1:]
dx_half[0, 1:] = dx_half[1, 1:]
# and West
dy_half[:, 0] = dy_half[:, 1]
dx_half[:, 0] = dx_half[:, 1]
clon = np.empty((self.num_lat_points, self.num_lon_points, 4))
clon[:] = np.NAN
clon[:, :, 0] = x - dx_half
clon[:, :, 1] = x + dx_half
clon[:, :, 2] = x + dx_half
clon[:, :, 3] = x - dx_half
assert(not np.isnan(np.sum(clon)))
clat = np.empty((self.num_lat_points, self.num_lon_points, 4))
clat[:] = np.NAN
clat[:, :, 0] = y - dy_half
clat[:, :, 1] = y - dy_half
clat[:, :, 2] = y + dy_half
clat[:, :, 3] = y + dy_half
assert(not np.isnan(np.sum(clat)))
self.clon_t = clon
self.clat_t = clat
def calc_area_of_polygons(clons, clats):
"""
Calculate the area of lat-lon polygons.
We project sphere onto a flat surface using an equal area projection
and then calculate the area of flat polygon.
This is slow we should do some caching to avoid recomputing.
"""
areas = np.zeros(clons.shape[1:])
areas[:] = np.NAN
for j in range(areas.shape[0]):
for i in range(areas.shape[1]):
lats = clats[:, j, i]
lons = clons[:, j, i]
lat_centre = lats[0] + abs(lats[2] - lats[1]) / 2
lon_centre = lons[0] + abs(lons[1] - lons[0]) / 2
pa = pyproj.Proj(proj_str.format(lat_centre, lon_centre))
x, y = pa(lons, lats)
cop = {"type": "Polygon", "coordinates": [zip(x, y)]}
areas[j, i] = shape(cop).area
assert(np.sum(areas) is not np.NAN)
assert(np.min(areas) > 0)
return areas
def cell2val(matrix_list, index):
pos_index = 0
matpos = {}
for i in range(0,index):
for j in range(0,index):
if i <= j:
matpos[(i,j)] = pos_index
pos_index += 1
valmatrix = np.zeros(pos_index)
for matrix in matrix_list:
newvec = np.zeros(pos_index)
matrix_open = open(matrix)
for line in matrix_open:
bin1, bin2, raw, norm, chrom1, chrom2 = line.split()
# if chrom1 == chrom2: #Ignore intrachromosomal contacts for now; could also implement as a check to see how close bin1 and bin2 are
# norm = np.NAN
coord = (int(bin1), int(bin2))
pos = matpos[coord]
if bin1 == bin2 or raw == 0:
newvec[pos] = 0 #set to which value you want to compute cors for (root normalized coverage, or raw coverage)
else:
newvec[pos] = log10(float(raw))
valmatrix = np.vstack((valmatrix, newvec))
matrix_open.close()
valmatrix = np.delete(valmatrix, 0, 0)
return valmatrix
def _init_pane(self):
nan = np.array([np.NAN, np.NAN])
X = np.column_stack([nan] * len(self.legend))
Y = np.column_stack([nan] * len(self.legend))
return self.viz.line(
X=X, Y=Y, env=self.env, opts=self.opts)
def make_corners(self):
# Uses double density grid to figure out corners.
x = self.x_vt
y = self.y_vt
# Corners of t points. Index 0 is bottom left and then
# anti-clockwise.
clon = np.empty((self.x_t.shape[0], self.x_t.shape[1], 4))
clon[:] = np.NAN
clon[:,:,0] = x[0:-1:2,0:-1:2]
clon[:,:,1] = x[0:-1:2,2::2]
clon[:,:,2] = x[2::2,2::2]
clon[:,:,3] = x[2::2,0:-1:2]
assert(not np.isnan(np.sum(clon)))
clat = np.empty((self.x_t.shape[0], self.x_t.shape[1], 4))
clat[:] = np.NAN
clat[:,:,0] = y[0:-1:2,0:-1:2]
clat[:,:,1] = y[0:-1:2,2::2]
clat[:,:,2] = y[2::2,2::2]
clat[:,:,3] = y[2::2,0:-1:2]
assert(not np.isnan(np.sum(clat)))
self.clon_t = clon
self.clat_t = clat
def make_corners(self):
x = self.x_t
y = self.y_t
dx_half = self.dx / 2.0
dy_half = self.dy / 2.0
# Set grid corners, we do these one corner at a time. Start at the
# bottom left and go anti-clockwise. This is the SCRIP convention.
clon = np.empty((self.num_lat_points, self.num_lon_points, 4))
clon[:] = np.NAN
clon[:,:,0] = x - dx_half
clon[:,:,1] = x + dx_half
clon[:,:,2] = x + dx_half
clon[:,:,3] = x - dx_half
assert(not np.isnan(np.sum(clon)))
clat = np.empty((self.num_lat_points, self.num_lon_points, 4))
clat[:] = np.NAN
clat[:,:,0] = y - dy_half
clat[:,:,1] = y - dy_half
clat[:,:,2] = y + dy_half
clat[:,:,3] = y + dy_half
assert(not np.isnan(np.sum(clat)))
# The bottom latitude band should always be Southern extent.
assert(np.all(clat[0, :, 0] == np.min(y) - dy_half))
assert(np.all(clat[0, :, 1] == np.min(y) - dy_half))
# The top latitude band should always be Northern extent.
assert(np.all(clat[-1, :, 2] == np.max(y) + dy_half))
assert(np.all(clat[-1, :, 3] == np.max(y) + dy_half))
self.clon_t = clon
self.clat_t = clat
def __mask_plot_data__(self, arr):
#tmp = self.Data.variables[key][:].ravel()FIGURE_FILENAME_TEMPLATE = 'qa-cab_pres_temp_%s_r%.2i_%s.png
#arr.ravel()[self.Index] = np.NAN
arr[self.Index] = np.NAN
return arr
def cosegregation(counts_table):
"""
Return the co-segregation frequency of n loci given their
contingency table.
"""
probs_table = frequency_to_probability(counts_table)
if either_locus_not_detected(probs_table):
return np.NAN
return probs_table.flat[-1]
def expected(counts_table):
"""
Return the expected co-segregation probability of an arbitrary number
of loci given their contingency table.
"""
probs_table = frequency_to_probability(counts_table)
marginal_probs = get_marginal_probabilities(probs_table)
if either_locus_not_detected(marginal_probs):
return np.NAN
exp_freqs = marginal_probs.prod(axis=0)[0]
return exp_freqs