我们从Python开源项目中,提取了以下50个代码示例,用于说明如何使用numpy.logical_or()。
def calc_shared(self, label): """ Get the subset of scores that are shared across all Selections in each Condition. """ if self.check_store("/main/{}/scores_shared".format(label)): return idx = pd.IndexSlice logging.info("Identifying subset shared across all Selections ({})" "".format(label), extra={'oname': self.name}) data = self.store.select("/main/{}/scores_shared_full".format(label)) # identify variants found in all selections in at least one condition complete = np.full(len(data.index), False, dtype=bool) for cnd in data.columns.levels[0]: complete = np.logical_or(complete, data.loc[:, idx[cnd, :, :]].notnull().all( axis='columns')) data = data.loc[complete] self.store.put("/main/{}/scores_shared".format(label), data, format="table")
def iany(arrays, axis = -1): """ Test whether any array elements along a given axis evaluate to True. Parameters ---------- arrays : iterable Arrays to be reduced. axis : int or None, optional Axis along which a logical OR reduction is performed. The default is to perform a logical AND along the 'stream axis', as if all arrays in ``array`` were stacked along a new dimension. If ``axis = None``, arrays in ``arrays`` are flattened before reduction. Yields ------ any : ndarray, dtype bool """ yield from ireduce_ufunc(arrays, ufunc = np.logical_or, axis = axis)
def compute_test_accuracy(X_test, Y_test, model, prediction_type, cellgroup_map_array): prediction = model.predict(X_test) auc = [] if prediction_type=="cellgroup": prediction = np.dot(prediction, cellgroup_map_array) Y_test = np.dot(Y_test, cellgroup_map_array) mask = ~np.logical_or(Y_test.sum(1)==0, Y_test.sum(1)==Y_test.shape[1]) for y,pred in zip(Y_test.T,prediction.T): pos = np.logical_and(mask, y==1) neg = np.logical_and(mask, y==0) try: U = stats.mannwhitneyu(pred[pos], pred[neg])[0] auc.append(1.-U/(np.count_nonzero(pos)*np.count_nonzero(neg))) except ValueError: auc.append(0.5) return auc
def test_truth_table_logical(self): # 2, 3 and 4 serves as true values input1 = [0, 0, 3, 2] input2 = [0, 4, 0, 2] typecodes = (np.typecodes['AllFloat'] + np.typecodes['AllInteger'] + '?') # boolean for dtype in map(np.dtype, typecodes): arg1 = np.asarray(input1, dtype=dtype) arg2 = np.asarray(input2, dtype=dtype) # OR out = [False, True, True, True] for func in (np.logical_or, np.maximum): assert_equal(func(arg1, arg2).astype(bool), out) # AND out = [False, False, False, True] for func in (np.logical_and, np.minimum): assert_equal(func(arg1, arg2).astype(bool), out) # XOR out = [False, True, True, False] for func in (np.logical_xor, np.not_equal): assert_equal(func(arg1, arg2).astype(bool), out)
def test_object_logical(self): a = np.array([3, None, True, False, "test", ""], dtype=object) assert_equal(np.logical_or(a, None), np.array([x or None for x in a], dtype=object)) assert_equal(np.logical_or(a, True), np.array([x or True for x in a], dtype=object)) assert_equal(np.logical_or(a, 12), np.array([x or 12 for x in a], dtype=object)) assert_equal(np.logical_or(a, "blah"), np.array([x or "blah" for x in a], dtype=object)) assert_equal(np.logical_and(a, None), np.array([x and None for x in a], dtype=object)) assert_equal(np.logical_and(a, True), np.array([x and True for x in a], dtype=object)) assert_equal(np.logical_and(a, 12), np.array([x and 12 for x in a], dtype=object)) assert_equal(np.logical_and(a, "blah"), np.array([x and "blah" for x in a], dtype=object)) assert_equal(np.logical_not(a), np.array([not x for x in a], dtype=object)) assert_equal(np.logical_or.reduce(a), 3) assert_equal(np.logical_and.reduce(a), None)
def test_NotImplemented_not_returned(self): # See gh-5964 and gh-2091. Some of these functions are not operator # related and were fixed for other reasons in the past. binary_funcs = [ np.power, np.add, np.subtract, np.multiply, np.divide, np.true_divide, np.floor_divide, np.bitwise_and, np.bitwise_or, np.bitwise_xor, np.left_shift, np.right_shift, np.fmax, np.fmin, np.fmod, np.hypot, np.logaddexp, np.logaddexp2, np.logical_and, np.logical_or, np.logical_xor, np.maximum, np.minimum, np.mod ] # These functions still return NotImplemented. Will be fixed in # future. # bad = [np.greater, np.greater_equal, np.less, np.less_equal, np.not_equal] a = np.array('1') b = 1 for f in binary_funcs: assert_raises(TypeError, f, a, b)
def outer(self, a, b): """ Return the function applied to the outer product of a and b. """ (da, db) = (getdata(a), getdata(b)) d = self.f.outer(da, db) ma = getmask(a) mb = getmask(b) if ma is nomask and mb is nomask: m = nomask else: ma = getmaskarray(a) mb = getmaskarray(b) m = umath.logical_or.outer(ma, mb) if (not m.ndim) and m: return masked if m is not nomask: np.copyto(d, da, where=m) if not d.shape: return d masked_d = d.view(get_masked_subclass(a, b)) masked_d._mask = m return masked_d
def _test_valid_x_range(x, x_range, outname): """ Test if any of the x values are outside of the valid range Parameters ---------- x : float array wavenumbers in inverse microns x_range: 2 floats allowed min/max of x outname: str name of curve for error message """ if np.logical_or(np.any(x < x_range[0]), np.any(x > x_range[1])): raise ValueError('Input x outside of range defined for ' + outname \ + ' [' + str(x_range[0]) + ' <= x <= ' + str(x_range[1]) + ', x has units 1/micron]')
def get_metrics(predictions, targets): assert(np.logical_or(predictions == 1, predictions == 0).all()) assert(np.logical_or(targets == 1, targets == 0).all()) TP = np.logical_and(predictions == 1, targets == 1).sum() FP = np.logical_and(predictions == 1, targets == 0).sum() FN = np.logical_and(predictions == 0, targets == 1).sum() TN = np.logical_and(predictions == 0, targets == 0).sum() N = TP + FP + FN + TN PPV = float(TP) / float(TP + FP) if TP != 0.0 else 0.0 FPV = float(TN) / float(TN + FN) if TN != 0.0 else 0.0 ACC = float(TP + TN) / float(N) TPR = float(TP) / float(TP + FN) if TP != 0.0 else 0.0 FPR = float(FP) / float(FP + TN) if FP != 0.0 else 0.0 tp, tn, fp, fn = float(TP) / N, float(TN) / N, float(FP) / N, float(FN) / N MCC = float(tp*tn - fp*fn) / (np.sqrt(tp+fp)*np.sqrt(tp+fn)*np.sqrt(tn+fp)*np.sqrt(tn+fn)) F1 = 2 * TP / float(2 * TP + FP + FN) metrics = { "TP": TP, "FP": FP, "FN": FN, "TN": TN, "N": N, "PPV": PPV, "FPV": FPV, "MCC": MCC, "ACC": ACC, "F1": F1 } return metrics
def get_metrics_per_contact_type(predicted_cmap, target_cmap): L = predicted_cmap.shape[0] short_range = np.zeros((L, L), dtype = np.bool) medium_range = np.zeros((L, L), dtype = np.bool) long_range = np.zeros((L, L), dtype = np.bool) more_than_6 = np.tril_indices(L, -6) more_than_12 = np.tril_indices(L, -12) more_than_24 = np.tril_indices(L, -24) short_range[more_than_6] = True short_range[more_than_12] = False medium_range[more_than_12] = True medium_range[more_than_24] = False long_range[more_than_24] = True short_range = np.logical_or(short_range, short_range.T) medium_range = np.logical_or(medium_range, medium_range.T) long_range = np.logical_or(long_range, long_range.T) short_range_metrics = get_metrics(predicted_cmap[short_range], target_cmap[short_range]) medium_range_metrics = get_metrics(predicted_cmap[medium_range], target_cmap[medium_range]) long_range_metrics = get_metrics(predicted_cmap[long_range], target_cmap[long_range]) return short_range_metrics, medium_range_metrics, long_range_metrics
def simDeath(self): ''' Determines which agents die this period and must be replaced. Uses the sequence in LivPrb to determine survival probabilities for each agent. Parameters ---------- None Returns ------- which_agents : np.array(bool) Boolean array of size AgentCount indicating which agents die. ''' # Determine who dies DiePrb_by_t_cycle = 1.0 - np.asarray(self.LivPrb) DiePrb = DiePrb_by_t_cycle[self.t_cycle-1] # Time has already advanced, so look back one DeathShks = drawUniform(N=self.AgentCount,seed=self.RNG.randint(0,2**31-1)) which_agents = DeathShks < DiePrb if self.T_age is not None: # Kill agents that have lived for too many periods too_old = self.t_age >= self.T_age which_agents = np.logical_or(which_agents,too_old) return which_agents
def simDeath(self): ''' Determines which agents die this period and must be replaced. Uses the sequence in LivPrb to determine survival probabilities for each agent. Parameters ---------- None Returns ------- which_agents : np.array(bool) Boolean array of size AgentCount indicating which agents die. ''' # Determine who dies LivPrb = np.array(self.LivPrb)[self.t_cycle-1,self.MrkvNow] # Time has already advanced, so look back one DiePrb = 1.0 - LivPrb DeathShks = drawUniform(N=self.AgentCount,seed=self.RNG.randint(0,2**31-1)) which_agents = DeathShks < DiePrb if self.T_age is not None: # Kill agents that have lived for too many periods too_old = self.t_age >= self.T_age which_agents = np.logical_or(which_agents,too_old) return which_agents
def save_total_power(data,times,SCADA_faults,filename): total_power = np.array([]) new_times = np.array([]) percent_active = np.array([]) for time in np.unique(times): state_fault = SCADA_faults[times == time] fault_mask = [state_fault == 2,state_fault == 1] fault_mask = reduce(np.logical_or,fault_mask) total_power = np.append(total_power,np.sum(data[times == time])) new_times = np.append(new_times,time) percent_active = np.append(percent_active,float(np.sum(fault_mask))/float(len(fault_mask))) total_dictionary = {} total_dictionary['total_power'] = total_power total_dictionary['time'] = new_times total_dictionary['percent_active'] = percent_active file_path = os.path.normpath('%s/FormattedData/%s' % (os.getcwd(),filename)) np.savez(file_path,**total_dictionary)
def contains(self, other): if isinstance(other, Point): x = other._x elif isinstance(other, np.ndarray): x = other elif isinstance(other, Polygon): x = _points_to_array(other.vertices) return np.all(self.contains(x)) else: raise TypeError("P must be point or ndarray") # keep track of whether each point is contained in a face bools = np.full(x.shape[0], False, dtype=bool) for f in self.faces: bools = np.logical_or(bools, f.contains(x)) return bools
def zdivide(x, y): """ Return x/y, with 0 instead of NaN where y is 0. Parameters ---------- x : array_like Numerator y : array_like Denominator Returns ------- z : ndarray Quotient `x`/`y` """ with np.errstate(divide='ignore', invalid='ignore'): div = x / y div[np.logical_or(np.isnan(div), np.isinf(div))] = 0 return div
def zero_safe_divide(a, b, default_error_value=0.): """Element-wise division that accounts for floating point errors. Both invalid floating-point (e.g. 0. / 0.) and divide be zero errors are suppressed. Resulting values (NaN and Inf respectively) are replaced with `default_error_value`. """ import numpy as np with np.errstate(invalid='ignore', divide='ignore'): quotient = np.true_divide(a, b) bad_value_indices = np.logical_or( np.isnan(quotient), np.isinf(quotient)) quotient[bad_value_indices] = default_error_value return quotient
def log_mat(x, n, g_coeff, c_1, const): with np.errstate(divide='ignore', invalid='ignore'): K = g_coeff.shape[0] - 1 thres = 2 * c_1 * math.log(n) / n [T, X] = np.meshgrid(thres, x) ratio = np.clip(2*X/T - 1, 0, 1) # force MATLAB-esque behavior with NaN, inf ratio[T == 0] = 1.0 ratio[X == 0] = 0.0 q = np.reshape(np.arange(K), [1, 1, K]) g = np.tile(np.reshape(g_coeff, [1, 1, K + 1]), [c_1.shape[1], 1]) g[:, :, 0] = g[:, :, 0] + np.log(thres) MLE = np.log(X) + (1-X) / (2*X*n) MLE[X == 0] = -np.log(n) - const tmp = (n*X[:,:,np.newaxis] - q)/(T[:,:,np.newaxis]*(n - q)) polyApp = np.sum(np.cumprod(np.dstack([np.ones(T.shape + (1,)), tmp]), axis=2) * g, axis=2) polyFail = np.logical_or(np.isnan(polyApp), np.isinf(polyApp)) polyApp[polyFail] = MLE[polyFail] return ratio*MLE + (1-ratio)*polyApp
def classifyLevel1Assign(classLevel1Img): # Create Output Array level1 = numpy.empty_like(classLevel1Img, dtype = numpy.dtype('a255')) level1[...] = "NA" # Non Vegetated level1 = numpy.where(numpy.logical_or(classLevel1Img == "NA", numpy.logical_or(classLevel1Img == "Water", classLevel1Img == "Urban")), "Non Vegetated", level1) # Vegetated level1 = numpy.where(numpy.logical_or(classLevel1Img == "Photosynthetic Vegetated", classLevel1Img == "Non Photosynthetic Vegetated", classLevel1Img == "Non Submerged Aquatic Vegetated"), "Vegetated", level1) return level1 # A function for classifying level 2
def _verify_species(self, elements): # Format the elements and then check that all are real. if isinstance(elements, string_types): elements = [elements] elements = [str(element).title() for element in elements] species = [] transitions = self.transitions for element in elements: # Get the species associated with this element ii = np.logical_or( transitions["elem1"] == element, transitions["elem2"] == element) # Note plurality/singularity of specie/species. # APJ modified to remove isotopes in species specie = transitions[ii]["species"] specie = (specie*10).astype(int)/10.0 specie = list(np.unique(specie)) species.append(specie) return species
def find_spikes(data, spike_thresh): data -= np.median(np.median(np.median(data, axis=0), axis=0), axis=0) slice_mean = np.median(np.median(data, axis=0), axis=0) t_z = _robust_zscore(slice_mean) spikes = np.abs(t_z) > spike_thresh spike_inds = np.transpose(spikes.nonzero()) # mask out the spikes and recompute z-scores using variance uncontaminated with spikes. # This will catch smaller spikes that may have been swamped by big # ones. data.mask[:, :, spike_inds[:, 0], spike_inds[:, 1]] = True slice_mean2 = np.median(np.median(data, axis=0), axis=0) t_z = _robust_zscore(slice_mean2) spikes = np.logical_or(spikes, np.abs(t_z) > spike_thresh) spike_inds = [tuple(i) for i in np.transpose(spikes.nonzero())] return spike_inds, t_z
def isnull(self): """ Detect missing values Both missing values (-1 in .codes) and NA as a category are detected. Returns ------- a boolean array of whether my values are null See also -------- pandas.isnull : pandas version Categorical.notnull : boolean inverse of Categorical.isnull """ ret = self._codes == -1 # String/object and float categories can hold np.nan if self.categories.dtype.kind in ['S', 'O', 'f']: if np.nan in self.categories: nan_pos = np.where(isnull(self.categories))[0] # we only have one NA in categories ret = np.logical_or(ret, self._codes == nan_pos) return ret
def make_sessions(data, session_th=30 * 60, is_ordered=False, user_key='user_id', item_key='item_id', time_key='ts'): """Assigns session ids to the events in data without grouping keys""" if not is_ordered: # sort data by user and time data.sort_values(by=[user_key, time_key], ascending=True, inplace=True) # compute the time difference between queries tdiff = np.diff(data[time_key].values) # check which of them are bigger then session_th split_session = tdiff > session_th split_session = np.r_[True, split_session] # check when the user chenges is data new_user = data['user_id'].values[1:] != data['user_id'].values[:-1] new_user = np.r_[True, new_user] # a new sessions stars when at least one of the two conditions is verified new_session = np.logical_or(new_user, split_session) # compute the session ids session_ids = np.cumsum(new_session) data['session_id'] = session_ids return data
def outer(self, a, b): """ Return the function applied to the outer product of a and b. """ (da, db) = (getdata(a), getdata(b)) d = self.f.outer(da, db) ma = getmask(a) mb = getmask(b) if ma is nomask and mb is nomask: m = nomask else: ma = getmaskarray(a) mb = getmaskarray(b) m = umath.logical_or.outer(ma, mb) if (not m.ndim) and m: return masked if m is not nomask: np.copyto(d, da, where=m) if not d.shape: return d masked_d = d.view(get_masked_subclass(a, b)) masked_d._mask = m masked_d._update_from(d) return masked_d
def construct_embedding_matrix(self): min_delay = numpy.min(self.delays) max_delay = numpy.max(self.delays) t_list = [] embedding_list = [] for i in range(self.x.shape[0]): if (i - max_delay < 0) or (i - min_delay) >= self.x.shape[0]: continue delay_vector = numpy.array([self.x[i - delay] for delay in self.delays]) if numpy.any(numpy.logical_or(numpy.isnan(delay_vector), numpy.isinf(delay_vector))): continue t_list.append(i) embedding_list.append(delay_vector) if len(embedding_list) == 0: self.t = numpy.array(t_list, dtype=float) self.embedding_mat = numpy.zeros((0, len(self.delays)), dtype=float) else: self.t = numpy.array(t_list) self.embedding_mat = numpy.array(embedding_list) assert self.embedding_mat.shape[1] == len(self.delays)
def correlation_valid(x, y): invalid = numpy.logical_or(numpy.isnan(x), numpy.isnan(y)) valid = numpy.logical_not(invalid) valid_count = valid.sum() if valid_count == 0: corr = float('nan') sd_x = float('nan') sd_y = float('nan') else: sd_x = numpy.std(x[valid]) sd_y = numpy.std(y[valid]) if sd_x == 0 and sd_y == 0: corr = 1.0 elif sd_x == 0 or sd_y == 0: corr = 0.0 else: corr = numpy.corrcoef(x[valid], y[valid])[0,1] return corr, valid_count, sd_x, sd_y
def sutton_chen(x): x = x.reshape((int(x.size/3), 3)) idx = np.array(list(np.arange(0, x.shape[0])) * x.shape[0]) jdx = np.concatenate([[a] * x.shape[0] for a in range( 0, x.shape[0])]) index = np.column_stack((idx, jdx)) index = index[index[:, 0] < index[:, 1], :] rij = np.zeros(index.shape[0]) for i in range(index.shape[0]): rij[i] = np.sqrt(np.sum((x[index[i, 0], :] - x[ index[i, 1], :]) ** 2)) f1s = np.zeros(index.shape[0]) rhos = np.zeros(index.shape[0]) for i in range(0, x.shape[0]): idx = np.logical_or(index[:, 0] == i, index[:, 1] == i) f1s[i] = 0.5 * (A**K) * np.sum(1/rij[idx] ** K) rhos[i] = (A**M) * sum(1/(rij[idx]) ** M) return np.sum(f1s - C * np.sqrt(rhos))
def gradient(self, x): g = np.zeros(x.size, np.float64) for i in range(x.size): x1 = np.array(x) x2 = np.array(x) respl = self.reps respr = self.reps x1[i] = x[i] + respr if x1[i] > self.upper[i]: x1[i] = self.upper[i] respr = x1[i] - x[i] x2[i] = x[i] - respl if x2[i] < self.lower[i]: x2[i] = self.lower[i] respl = x[i] - x2[i] f1 = self.func_wrapper(x1) f2 = self.func_wrapper(x2) g[i] = ((f1 - f2)) / (respl + respr) idx = np.logical_or(np.isnan(g), np.isinf(g)) g[idx] = 101.0 return g
def _idvalid(data, isinvalid=None, minval=None, maxval=None): """Identifies valid entries in an array and returns the corresponding indices Invalid values are NaN and Inf. Other invalid values can be passed using the isinvalid keyword argument. Parameters ---------- data : :class:`numpy:numpy.ndarray` of floats isinvalid : list of what is considered an invalid value """ if isinvalid is None: isinvalid = [-99., 99, -9999., -9999] ix = np.ma.masked_invalid(data).mask for el in isinvalid: ix = np.logical_or(ix, np.ma.masked_where(data == el, data).mask) if minval is not None: ix = np.logical_or(ix, np.ma.masked_less(data, minval).mask) if maxval is not None: ix = np.logical_or(ix, np.ma.masked_greater(data, maxval).mask) return np.where(np.logical_not(ix))[0]
def m_menu(self): """Runs the Mathmatically defined sculpture menu item.""" sin, cos = np.sin, np.cos res = raw_input("Enter a functional definition of a volume (x**2+y**2+z**2 < 1) \n") self.user_text = res self.volume_data = self.bool_ops() self.create_iso_surface(.7) while True: res = raw_input("Enter another functional definition of a volume (x**2+y**2+z**2 < 1) \n") self.user_text = res self.sec_volume_data = self.bool_ops() self.create_iso_surface(.7, second=True) res = raw_input("Enter a boolean operation to do with the previous solid (a = and, o = or, n = not, x = xor):\n") if res == "a": self.sec_volume_data = 0+ np.logical_and(my_sculpture.volume_data, my_sculpture.bool_ops()) elif res == "o": self.sec_volume_data = 0+ np.logical_or(my_sculpture.volume_data, my_sculpture.bool_ops()) elif res == "n": self.sec_volume_data = 0+ np.logical_not(my_sculpture.volume_data, my_sculpture.bool_ops()) elif res == "x": self.sec_volume_data = 0+ np.logical_xor(my_sculpture.volume_data, my_sculpture.bool_ops()) self.create_iso_surface(.7, second=True)
def indexSearch(self, indexes): """Filters the data by a list of indexes. Args: indexes (list of int): List of index numbers to return. Returns: list: A list containing all indexes with filtered data. Matches will be `True`, the remaining items will be `False`. If the dataFrame is empty, an empty list will be returned. """ if not self._dataFrame.empty: filter0 = self._dataFrame.index == -9999 for index in indexes: filter1 = self._dataFrame.index == index filter0 = np.logical_or(filter0, filter1) return filter0 else: return []
def simpleMask(config): #params = ugali.utils.(config, kwargs) roi = ugali.observation.roi.ROI(config) # De-project the bin centers to get magnitude depths mesh_x, mesh_y = numpy.meshgrid(roi.centers_x, roi.centers_y) r = numpy.sqrt(mesh_x**2 + mesh_y**2) # Think about x, y conventions here #z = (0. * (r > 1.)) + (21. * (r < 1.)) #z = 21. - r #z = (21. - r) * (mesh_x > 0.) * (mesh_y < 0.) z = (21. - r) * numpy.logical_or(mesh_x > 0., mesh_y > 0.) return MaskBand(z, roi) ############################################################
def loaddata(filename): if filename.find(ext)==-1: filename += ext data = None hdulist = pf.open(filename) # extract data hdu per hdu for item in hdulist: if item.header.get(card)=='DATA': if data is None: # first set data = Oidata(src=item.header.get('SRC'), flatten=item.header.get('FLATTEN'), degrees=item.header.get('DEGREES'), significant_figures=item.header.get('SIG_FIG'), **{item.header.get('DATATYPE'):item.data}) else: data.addData(src=item.header.get('SRC'), flatten=item.header.get('FLATTEN'), degrees=item.header.get('DEGREES'), significant_figures=item.header.get('SIG_FIG'), **{item.header.get('DATATYPE'):item.data}) # apply data mode per mode for item in hdulist: if item.header.get(card)=='DATAMASK': theoldmask = getattr(data, item.header.get('DATATYPE')).mask if theoldmask.shape != item.data.shape: print("Error while applying data mask on "+item.header.get('DATATYPE')) else: getattr(data, item.header.get('DATATYPE')).mask = np.logical_or(theoldmask, item.data.astype(bool)) return data