Python numpy.bitwise_not() 使用实例

The following are code examples for showing how to use . They are extracted from open source Python projects. You can vote up the examples you like or vote down the exmaples you don’t like. You can also save this page to your account.

Example 1

def test_values(self):
        for dt in self.bitwise_types:
            zeros = np.array([0], dtype=dt)
            ones = np.array([-1], dtype=dt)
            msg = "dt = '%s'" % dt.char

            assert_equal(np.bitwise_not(zeros), ones, err_msg=msg)
            assert_equal(np.bitwise_not(ones), zeros, err_msg=msg)

            assert_equal(np.bitwise_or(zeros, zeros), zeros, err_msg=msg)
            assert_equal(np.bitwise_or(zeros, ones), ones, err_msg=msg)
            assert_equal(np.bitwise_or(ones, zeros), ones, err_msg=msg)
            assert_equal(np.bitwise_or(ones, ones), ones, err_msg=msg)

            assert_equal(np.bitwise_xor(zeros, zeros), zeros, err_msg=msg)
            assert_equal(np.bitwise_xor(zeros, ones), ones, err_msg=msg)
            assert_equal(np.bitwise_xor(ones, zeros), ones, err_msg=msg)
            assert_equal(np.bitwise_xor(ones, ones), zeros, err_msg=msg)

            assert_equal(np.bitwise_and(zeros, zeros), zeros, err_msg=msg)
            assert_equal(np.bitwise_and(zeros, ones), zeros, err_msg=msg)
            assert_equal(np.bitwise_and(ones, zeros), zeros, err_msg=msg)
            assert_equal(np.bitwise_and(ones, ones), ones, err_msg=msg) 

Example 2

def test_types(self):
        for dt in self.bitwise_types:
            zeros = np.array([0], dtype=dt)
            ones = np.array([-1], dtype=dt)
            msg = "dt = '%s'" % dt.char

            assert_(np.bitwise_not(zeros).dtype == dt, msg)
            assert_(np.bitwise_or(zeros, zeros).dtype == dt, msg)
            assert_(np.bitwise_xor(zeros, zeros).dtype == dt, msg)
            assert_(np.bitwise_and(zeros, zeros).dtype == dt, msg) 

Example 3

def _mask_trigs(events, mask):
    """Helper function for masking digital trigger values"""
    if not isinstance(mask, int):
        raise TypeError('You provided a(n) %s. Mask must be an int.'
                        % type(mask))
    n_events = len(events)
    if n_events == 0:
        return events.copy()

    mask = np.bitwise_not(mask)
    events[:, 1:] = np.bitwise_and(events[:, 1:], mask)
    events = events[events[:, 1] != events[:, 2]]

    return events 

Example 4

def _numpy(self, data, weights, shape):
        q = self.quantity(data)
        self._checkNPQuantity(q, shape)
        self._checkNPWeights(weights, shape)
        weights = self._makeNPWeights(weights, shape)
        newentries = weights.sum()

        import numpy

        selection = numpy.isnan(q)
        numpy.bitwise_not(selection, selection)
        subweights = weights.copy()
        subweights[selection] = 0.0
        self.nanflow._numpy(data, subweights, shape)

        # switch to float here like in bin.py else numpy throws
        # TypeError on trivial integer cases such as:
        # >>> q = numpy.array([1,2,3,4])
        # >>> np.divide(q,1,q)
        # >>> np.floor(q,q)
        q = numpy.array(q, dtype=numpy.float64)
        neginfs = numpy.isneginf(q)
        posinfs = numpy.isposinf(q)

        numpy.subtract(q, self.origin, q)
        numpy.divide(q, self.binWidth, q)
        numpy.floor(q, q)
        q = numpy.array(q, dtype=numpy.int64)
        q[neginfs] = LONG_MINUSINF
        q[posinfs] = LONG_PLUSINF

        selected = q[weights > 0.0]

        selection = numpy.empty(q.shape, dtype=numpy.bool)
        for index in numpy.unique(selected):
            if index != LONG_NAN:
                bin = self.bins.get(index)
                if bin is None:
                    bin = self.value.zero()
                    self.bins[index] = bin

                numpy.not_equal(q, index, selection)
                subweights[:] = weights
                subweights[selection] = 0.0
                bin._numpy(data, subweights, shape)

        # no possibility of exception from here on out (for rollback)
        self.entries += float(newentries) 

Example 5

def __init__(self, M, row_sparsity=None, column_sparsity=None, sparsity_threshold=0.05):
        # pylint: disable=len-as-condition
        self.M = M
        self.num_rows, self.num_columns = M.shape
        self.sparsity_threshold = sparsity_threshold*np.max(M.shape)

        self.M_csr = sp.sparse.csr_matrix(M)
        if row_sparsity is None:
            self.elements_per_row = np.array([self.M_csr.indptr[i + 1] - self.M_csr.indptr[i] for i in range(0, len(self.M_csr.indptr) - 1)])
            row_sparsity = self.elements_per_row < self.sparsity_threshold


        if column_sparsity is None:
            self.M_csc = sp.sparse.csc_matrix(M)
            self.elements_per_column = np.array([self.M_csc.indptr[i + 1] - self.M_csc.indptr[i] for i in range(0, len(self.M_csc.indptr) - 1)])
            column_sparsity = self.elements_per_column < self.sparsity_threshold

        self.r_s = row_sparsity if len(row_sparsity) else np.array([True]*self.M.shape[0])
        self.r_d = np.bitwise_not(self.r_s)
        self.ri_s = self.r_s.nonzero()[0]
        self.ri_d = self.r_d.nonzero()[0]

        self.c_s = column_sparsity if len(column_sparsity) else np.array([True]*self.M.shape[1])
        self.c_d = np.bitwise_not(self.c_s)
        self.ci_s = self.c_s.nonzero()[0]
        self.ci_d = self.c_d.nonzero()[0]

        M_coo = sp.sparse.coo_matrix(M)
        # sparse blocks s, and ss are created to be the size of the entire matrix, M. Dense blocks, however, are just the size of the subblocks.

        self.block_s = mask_sparse_matrix_by_rows(M_coo, self.row_sparsity)
        self.block_ss = mask_sparse_matrix_by_columns(self.block_s, self.column_sparsity).tocsr()
        self.block_ss_csc = self.block_ss.tocsc()
        self.block_sd = mask_sparse_matrix_by_columns(self.block_s, self.column_density).tocsr()[:, self.dense_column_indices].todense() if self.num_dense_columns else np.zeros((self.num_sparse_rows, self.num_dense_columns))
        self.block_s = self.block_s.tocsr()
        self.block_s_csc = self.block_s.tocsc()

        self.block_d_sparse = mask_sparse_matrix_by_rows(M_coo, self.row_density).tocsr()
        self.block_d = self.block_d_sparse[self.dense_row_indices, :].todense()
        self.block_ds = self.block_d[:, self.sparse_column_indices]
        self.block_dd = self.block_d[:, self.dense_column_indices]

        self.sparse_block = self.block_ss_csc[:, self.sparse_column_indices].tocsr()[self.sparse_row_indices, :] 

Example 6

def get_demand_or_head_residual(self, head, demand):

        if self.mode == 'PDD':
            minP = self.minimum_pressures
            nomP = self.nominal_pressures
            j_d = self.junction_demand
            m = self._slope_of_pdd_curve
            delta = self._pdd_smoothing_delta
            n_j = self.num_junctions
            P = head[:n_j] - self.node_elevations[:n_j]
            H = head[:n_j]
            Dact = demand[:n_j]

            self.demand_or_head_residual[:n_j] = (
                self.isolated_junction_array * H + (1.0 - self.isolated_junction_array)*(
                    (P <= minP) * (Dact - j_d*m*(P-minP)) +
                    (P > minP) * (P <= (minP + delta)) * (
                        Dact - j_d*(
                            self.pdd_poly1_coeffs_a*P**3 +
                            self.pdd_poly1_coeffs_b*P**2 +
                            self.pdd_poly1_coeffs_c*P +
                            self.pdd_poly1_coeffs_d
                        )
                    ) +
                    (P > (nomP - delta)) * (P <= nomP) * (
                        Dact - j_d*(
                            self.pdd_poly2_coeffs_a*P**3 +
                            self.pdd_poly2_coeffs_b*P**2 +
                            self.pdd_poly2_coeffs_c*P +
                            self.pdd_poly2_coeffs_d
                        )
                    ) +
                    (P > nomP) * (Dact - j_d * (m*(P-nomP) + 1.0))
                )
            )
            # for the last segment, assignment is required because 0*np.nan does not equal 0 (same with np.inf)
            last_segment = (Dact - j_d*((P-minP)/(nomP-minP))**0.5)
            last_segment[np.bitwise_not((P > (minP + delta))*(P <= (nomP - delta)))] = 0.0
            self.demand_or_head_residual[:n_j] = (self.demand_or_head_residual[:n_j] +
                                                  last_segment*(1.0-self.isolated_junction_array))
        else:
            self.demand_or_head_residual[:self.num_junctions] = (
                self.isolated_junction_array * head[:self.num_junctions] +
                (1.0 - self.isolated_junction_array) * (demand[:self.num_junctions] - self.junction_demand)
            )
        for node_id in self._tank_ids:
            self.demand_or_head_residual[node_id] = head[node_id] - self.tank_head[node_id]
        for node_id in self._reservoir_ids:
            self.demand_or_head_residual[node_id] = head[node_id] - self.reservoir_head[node_id] 
点赞