numpy.ravel

Here are the examples of the python api numpy.ravel taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

146 Examples 7

Example 1

Project: orange Source File: correspondence.py
    def plot_biplot(self, axes = (0, 1)):
        import pylab
        if len(axes) != 2:
           raise ValueError("Dim tuple must be of length two")
        rows = self.row_factors()[:, axes]
        columns = self.column_factors()[:, axes]
        pylab.plot(numpy.ravel(rows[:, 0]), numpy.ravel(rows[:, 1]), "ro")
        pylab.plot(numpy.ravel(columns[:, 0]), numpy.ravel(columns[:, 1]), "bs")
        
        if self.row_labels:
             for i, coord in enumerate(rows):
                x, y = coord.T
                pylab.text(x, y, self.row_labels[i], horizontalalignment='center')
        if self.col_labels:
             for i, coord in enumerate(columns):
                x, y = coord.T 
                pylab.text(x, y, self.col_labels[i], horizontalalignment='center')                
        pylab.grid()
        pylab.show()

Example 2

Project: tensorly Source File: base.py
def tensor_to_vec(tensor):
    """Vectorises a tensor

    Parameters
    ----------
    tensor : ndarray
             tensor of shape ``(i_1, ..., i_n)``

    Returns
    -------
    1D-array
        vectorised tensor of shape ``(i_1 * i_2 * ... * i_n)``
    """
    return np.ravel(tensor)

Example 3

Project: snorkel Source File: utils.py
def matrix_coverage(L):
    """
    Given an N x M matrix where L_{i,j} is the label given by the jth LF to the ith candidate:
    Return the **fraction of candidates that each LF labels.**
    """
    return np.ravel(sparse_abs(L).sum(axis=0) / float(L.shape[0]))

Example 4

Project: kobocat Source File: tools.py
Function: mode
def mode(a, axis=0):
    """
    Adapted from
    https://github.com/scipy/scipy/blob/master/scipy/stats/stats.py#L568
    """
    a, axis = _chk_asarray(a, axis)
    scores = np.unique(np.ravel(a))       # get ALL unique values
    testshape = list(a.shape)
    testshape[axis] = 1
    oldmostfreq = np.zeros(testshape)
    oldcounts = np.zeros(testshape)
    for score in scores:
        template = (a == score)
        counts = np.expand_dims(np.sum(template, axis), axis)
        mostfrequent = np.where(counts > oldcounts, score, oldmostfreq)
        oldcounts = np.maximum(counts, oldcounts)
        oldmostfreq = mostfrequent
    return mostfrequent, oldcounts

Example 5

Project: mlab Source File: mlabwrap.py
    def _format_struct(self, varname):
        res = []
        fieldnames = self._do("fieldnames(%s)" % varname)
        size       = numpy.ravel(self._do("size(%s)" % varname))
        return "%dx%d struct array with fields:\n%s" % (
            size[0], size[1], "\n   ".join([""] + fieldnames))

Example 6

Project: pyNastran Source File: cart3d.py
Function: write_points
    def _write_points(self, outfile, points, is_binary, float_fmt='%6.6f'):
        if is_binary:
            four = pack(self._endian + b('i'), 4)
            outfile.write(four)

            npoints = points.shape[0]
            fmt = self._endian + b('%if' % (npoints * 3))
            floats = pack(fmt, *np.ravel(points))

            outfile.write(floats)
            outfile.write(four)
        else:
            if isinstance(float_fmt, bytes_type):
                fmt_ascii = float_fmt
            else:
                fmt_ascii = float_fmt.encode('latin1')
            np.savetxt(outfile, points, fmt_ascii)

Example 7

Project: lhcb_trigger_ml Source File: gradient_boosting.py
def get_higgs_data(train_file = '/Users/axelr/ipython/datasets/higgs/training.csv'):
    data = pandas.read_csv(train_file, index_col='EventId')
    answers_bs = numpy.ravel(data.Label)
    weights = numpy.ravel(data.Weight)
    data = data.drop(['Label', 'Weight'], axis=1)
    answers = numpy.zeros(len(answers_bs), dtype=numpy.int)
    answers[answers_bs == 's'] = 1
    return data, answers, weights

Example 8

Project: pybrain Source File: classification.py
Function: calculate_statistics
    def calculateStatistics(self):
        """Return a class histogram."""
        self.assignClasses()
        self.classHist = {}
        flat_labels = list(ravel(self['class']))
        for class_ in range(self.nClasses):
            self.classHist[class_] = flat_labels.count(class_)
        return self.classHist

Example 9

Project: pybrain Source File: cmaes.py
def sorti(vect):
    """ sort, but also return the indices-changes """
    tmp = sorted([(x_y[1], x_y[0]) for x_y in enumerate(ravel(vect))])
    res1 = array([x[0] for x in tmp])
    res2 = array([int(x[1]) for x in tmp])
    return res1, res2

Example 10

Project: numdifftools Source File: extrapolation.py
Function: convolve
def convolve(sequence, rule, **kwds):
    """Wrapper around scipy.ndimage.convolve1d that allows complex input."""
    dtype = np.result_type(float, np.ravel(sequence)[0])
    seq = np.asarray(sequence, dtype=dtype)
    if np.iscomplexobj(seq):
        return (convolve1d(seq.real, rule, **kwds) + 1j *
                convolve1d(seq.imag, rule, **kwds))
    return convolve1d(seq, rule, **kwds)

Example 11

Project: drmad Source File: test_numpy.py
def test_ravel_call():
    A = npr.randn(5, 6, 4)
    def fun(x): return to_scalar(np.ravel(x))
    d_fun = lambda x : to_scalar(grad(fun)(x))
    check_grads(fun, A)
    check_grads(d_fun, A)

Example 12

Project: chaco Source File: datarange_2d_test_case.py
def assert_close_(desired,actual):
    diff_allowed = 1e-5
    diff = abs(ravel(actual) - ravel(desired))
    for d in diff:
        if not isinf(d):
            assert_(alltrue(d <= diff_allowed))
            return

Example 13

Project: statsmodels Source File: test_quantile_regression.py
    def test_pvalues(self):
        pvals_stata = scipy.stats.t.sf(self.res2.table[:, 2], self.res2.df_r)
        assert_allclose(np.ravel(self.res1.pvalues.ix[idx]),
                        pvals_stata, rtol=1.1)

        # test that we use the t distribution for the p-values
        pvals_t = scipy.stats.t.sf(self.res1.tvalues, self.res2.df_r) * 2
        assert_allclose(np.ravel(self.res1.pvalues),
                        pvals_t, rtol=1e-9, atol=1e-10)

Example 14

Project: snorkel Source File: learning_utils.py
def LF_coverage(L):
    """
    Given an N x M matrix where L_{i,j} is the label given by the jth LF to the ith candidate:
    Return the **fraction of candidates that each LF labels.**
    """
    return np.ravel(sparse_abs(L).sum(axis=0) / float(L.shape[0]))

Example 15

Project: pybrain Source File: classification.py
    def assignClasses(self):
        """Ensure that the class field is properly defined and nClasses is set.
        """
        if len(self['class']) < len(self['target']):
            if self.outdim > 1:
                raise IndexError('Classes and 1-of-k representation out of sync!')
            else:
                self.setField('class', self.getField('target').astype(int))

        if self.nClasses <= 0:
            flat_labels = list(ravel(self['class']))
            classes = list(set(flat_labels))
            self.nClasses = len(classes)

Example 16

Project: imagen Source File: image.py
def edge_average(a):
    "Return the mean value around the edge of an array."

    if len(np.ravel(a)) < 2:
        return float(a[0])
    else:
        top_edge = a[0]
        bottom_edge = a[-1]
        left_edge = a[1:-1,0]
        right_edge = a[1:-1,-1]

        edge_sum = np.sum(top_edge) + np.sum(bottom_edge) + np.sum(left_edge) + np.sum(right_edge)
        num_values = len(top_edge)+len(bottom_edge)+len(left_edge)+len(right_edge)

        return float(edge_sum)/num_values

Example 17

Project: pyNastran Source File: oes_bars.py
Function: eid_to_element_node_index
    def eid_to_element_node_index(self, eids):
        ind = ravel([searchsorted(self.element == eid) for eid in eids])
        #ind = searchsorted(eids, self.element)
        #ind = ind.reshape(ind.size)
        #ind.sort()
        return ind

Example 18

Project: franklin Source File: statistics.py
Function: calculate_percentiles
def _calculate_percentiles(numbers, percents):
    'It calculates the percentiles for some numbers'
    #we need a numpy array
    if 'any' not in dir(numbers):
        numbers = numpy.ravel(numbers)
    if not numbers.any():
        raise ValueError('No data to calculate percentiles')

    mlab = sys.modules['matplotlib.mlab']

    percentiles = mlab.prctile(numbers, percents)
    return list(percentiles)

Example 19

Project: micropsi2 Source File: native_modules.py
def compute_analytic_gradient(netapi, node, a_i, a_h, a_o, b_h, b_o, w_hi, w_oh, weight_decay,
                              sparsity_value, sparsity_penalty, ada_rho, ada_eps):

    # make sure borrow is False here because otherwise the buffers are overwritten and
    # compute_numerical_gradient(..) still needs these same input values for proper comparison
    node.a_i.set_value(a_i, borrow=False)
    node.a_h.set_value(a_h, borrow=False)
    node.a_o.set_value(a_o, borrow=False)
    node.b_h.set_value(b_h, borrow=False)
    node.b_o.set_value(b_o, borrow=False)
    node.w_hi.set_value(w_hi, borrow=False)
    node.w_oh.set_value(w_oh, borrow=False)

    delta_o, delta_w_oh, delta_h, delta_w_hi = \
        node.get_gradients(weight_decay, sparsity_value, sparsity_penalty, ada_rho, ada_eps)

    gradient = np.concatenate((delta_o, np.ravel(delta_w_oh), delta_h, np.ravel(delta_w_hi)))

    return gradient

Example 20

Project: mne-python Source File: test_dipole.py
def _compute_depth(dip, fname_bem, fname_trans, subject, subjects_dir):
    """Compute dipole depth."""
    trans = _get_trans(fname_trans)[0]
    bem = read_bem_solution(fname_bem)
    surf = _bem_find_surface(bem, 'inner_skull')
    points = surf['rr']
    points = apply_trans(trans['trans'], points)
    depth = _compute_nearest(points, dip.pos, return_dists=True)[1][0]
    return np.ravel(depth)

Example 21

Project: automl_gpu Source File: libscores.py
Function: sanitize_array
def sanitize_array(array):
    ''' Replace NaN and Inf (there should not be any!)'''
    a=np.ravel(array)
    maxi = np.nanmax((filter(lambda x: x != float('inf'), a))) # Max except NaN and Inf
    mini = np.nanmin((filter(lambda x: x != float('-inf'), a))) # Mini except NaN and Inf
    array[array==float('inf')]=maxi
    array[array==float('-inf')]=mini
    mid = (maxi + mini)/2
    array[np.isnan(array)]=mid
    return array

Example 22

Project: megaman Source File: adjacency.py
Function: knn_adjacency
    def knn_adjacency(self, X):
        n_samples = X.shape[0]
        flindex = self._get_built_index(X)
        A_ind, A_data = flindex.nn_index(X, self.n_neighbors)
        A_ind = np.ravel(A_ind)
        A_data = np.sqrt(np.ravel(A_data))  # FLANN returns square distances
        A_indptr = self.n_neighbors * np.arange(n_samples + 1)
        return sparse.csr_matrix((A_data, A_ind, A_indptr),
                                 shape=(n_samples, n_samples))

Example 23

Project: scikit-learn Source File: multiclass.py
Function: predict_binary
def _predict_binary(estimator, X):
    """Make predictions using a single binary estimator."""
    if is_regressor(estimator):
        return estimator.predict(X)
    try:
        score = np.ravel(estimator.decision_function(X))
    except (AttributeError, NotImplementedError):
        # probabilities of the positive class
        score = estimator.predict_proba(X)[:, 1]
    return score

Example 24

Project: QSTK Source File: report.py
def print_benchmark_coer(fund_ts, benchmark_close, sym,  ostream):
    """
    @summary prints standard deviation of returns for a fund
    @param fund_ts: pandas fund time series
    @param years: list of years to print out
    @param ostream: stream to print to
    """
    fund_ts=fund_ts.fillna(method='pad')
    fund_ts=fund_ts.fillna(method='bfill')
    benchmark_close=benchmark_close.fillna(method='pad')
    benchmark_close=benchmark_close.fillna(method='bfill')
    faCorr=np.corrcoef(np.ravel(tsu.daily(fund_ts.values)),np.ravel(tsu.daily(benchmark_close)));
    b=np.ravel(tsu.daily(benchmark_close))
    f=np.ravel(tsu.daily(fund_ts))
    fBeta, unused = np.polyfit(b,f, 1);
    print_line(sym+"Correlattion","%+6.2f" % faCorr[0,1],i_spacing=3,ostream=ostream)
    print_line(sym+"Beta","%+6.2f" % fBeta,i_spacing=3,ostream=ostream)

Example 25

Project: auto-sklearn Source File: util.py
Function: sanitize_array
def sanitize_array(array):
    """
    Replace NaN and Inf (there should not be any!)
    :param array:
    :return:
    """
    a = np.ravel(array)
    maxi = np.nanmax(a[np.isfinite(a)])
    mini = np.nanmin(a[np.isfinite(a)])
    array[array == float('inf')] = maxi
    array[array == float('-inf')] = mini
    mid = (maxi + mini) / 2
    array[np.isnan(array)] = mid
    return array

Example 26

Project: chaco Source File: colormapper_test_case.py
    def test_array_factory(self):
        """ Test that the array factory creates valid colormap. """

        colors = array([[0.0,0.0,0.0], [1.0,1.0,1.0]])
        cm = ColorMapper.from_palette_array(colors)
        cm.range = DataRange1D()

        ar = ArrayDataSource(array([0.0, 0.5, 1.0]))
        cm.range.add(ar)
        b = cm.map_screen(ar.get_data())
        cm.range.remove(ar)

        expected = array([0.0, 0.5, 1.0])

        self.assertTrue(allclose(ravel(b[:,:1]), expected, atol=0.02),
            "Array factory failed.  Expected %s.  Got %s" % (expected, b[:,:1]))

        return

Example 27

Project: StockPredictionRNN Source File: nn.py
Function: test
    def test(self, data):
        y_predicted = np_utils.probas_to_classes(self.model.predict(data.x))
        y_actual = np_utils.probas_to_classes(data.y)
        error = (np.ravel(y_predicted) != np.ravel(y_actual)).sum().astype(float)/y_actual.shape[0]

        print("PREDICTED: class 0: {0}, class 1: {1}, class 2: {2}".format(
              np.sum(np.ravel(y_predicted) == 0),
              np.sum(np.ravel(y_predicted) == 1),
              np.sum(np.ravel(y_predicted) == 2)))
        print("ACTUAL: class 0: {0}, class 1: {1}, class 2: {2}".format(
              np.sum(np.ravel(y_actual) == 0),
              np.sum(np.ravel(y_actual) == 1),
              np.sum(np.ravel(y_actual) == 2)))
        print("ERROR RATE: ", error)

        return error

Example 28

Project: bayespy Source File: vmp.py
Function: dot
    def dot(self, x1, x2):
        """
        Computes dot products of given vectors (in parameter format)
        """
        v = 0
        # Loop over nodes
        for (y1, y2) in zip(x1, x2):
            # Loop over parameters
            for (z1, z2) in zip(y1, y2):
                v += np.dot(np.ravel(z1), np.ravel(z2))
        return v

Example 29

Project: chaco Source File: speedups_test_case.py
Function: assert_close
def assert_close(desired,actual):
    diff_allowed = 1e-5
    diff = abs(ravel(actual) - ravel(desired))
    for d in diff:
        if not isinf(d):
            assert alltrue(d <= diff_allowed)
            return

Example 30

Project: franklin Source File: statistics.py
    def get_sample_percentiles(self, percents):
        'It returns the percentiles given a percent list'
        if not self._sample:
            raise ValueError('No data to calculate percentiles')

        vect = numpy.ravel(self.sample)
        percentiles = mlab.prctile(vect, percents)
        return list(percentiles)

Example 31

Project: EDeN Source File: __init__.py
    def _extract_targets(self, graphs):
        y = []
        for graph in graphs:
            if graph.graph.get('target', None) is not None:
                y.append(graph.graph['target'])
            else:
                raise Exception('Missing the attribute "target" \
                    in graph dictionary!')
        y = np.ravel(y)
        return y

Example 32

Project: statsmodels Source File: grouputils.py
Function: transform_dataframe
    def transform_dataframe(self, dataframe, function, level=0, **kwargs):
        """Apply function to each column, by group
        Assumes that the dataframe already has a proper index"""
        if dataframe.shape[0] != self.nobs:
            raise Exception('dataframe does not have the same shape as index')
        out = dataframe.groupby(level=level).apply(function, **kwargs)
        if 1 in out.shape:
            return np.ravel(out)
        else:
            return np.array(out)

Example 33

Project: snorkel Source File: learning_utils.py
def LF_accuracies(L, labels):
    """
    Given an N x M matrix where L_{i,j} is the label given by the jth LF to the ith candidate, and labels {-1,1}
    Return the accuracy of each LF w.r.t. these labels
    """
    return np.ravel(0.5*(L.T.dot(labels) / sparse_abs(L).sum(axis=0) + 1))

Example 34

Project: pyNastran Source File: oes_composite_plates.py
    def eid_to_element_node_index(self, eids):
        ind = ravel([searchsorted(self.element_layer[:, 0] == eid) for eid in eids])
        #ind = searchsorted(eids, self.element)
        #ind = ind.reshape(ind.size)
        #ind.sort()
        return ind

Example 35

Project: robothon Source File: test_old_ma.py
Function: test_testminmax
    def test_testMinMax (self):
        "Test minimum and maximum."
        (x, y, a10, m1, m2, xm, ym, z, zm, xf, s) = self.d
        xr = numpy.ravel(x) #max doesn't work if shaped
        xmr = ravel(xm)

        #true because of careful selection of data
        self.failUnless(eq(max(xr), maximum(xmr)))

        #true because of careful selection of data
        self.failUnless(eq(min(xr), minimum(xmr)))

Example 36

Project: pyNastran Source File: cart3d.py
Function: write_elements
    def _write_elements(self, outfile, elements, is_binary, int_fmt='%6i'):
        min_e = elements.min()
        assert min_e == 1, 'min(elements)=%s' % min_e
        if is_binary:
            fmt = self._endian + b('i')
            four = pack(fmt, 4)
            outfile.write(four)
            nelements = elements.shape[0]
            fmt = self._endian + b('%ii' % (nelements * 3))
            ints = pack(fmt, *np.ravel(elements))

            outfile.write(ints)
            outfile.write(four)
        else:
            if isinstance(int_fmt, bytes_type):
                fmt_ascii = int_fmt
            else:
                fmt_ascii = int_fmt.encode('latin1')
            np.savetxt(outfile, elements, fmt_ascii)

Example 37

Project: automl-phase-2 Source File: sandpit.py
Function: f
    @staticmethod
    def _f(x):
        # iris = load_iris()
        X, y = X, y = make_hastie_10_2(random_state=0)
        x = np.ravel(x)
        f = np.zeros(x.shape)
        for i in range(f.size):
            clf = RandomForestClassifier(n_estimators=1, min_samples_leaf=int(np.round(x[i])), random_state=0)
            # scores = cross_val_score(clf, iris.data, iris.target)
            scores = cross_val_score(clf, X, y, cv=5)
            f[i] = -scores.mean()
        return f.ravel()

Example 38

Project: pymc3 Source File: sqlite.py
Function: record
    def record(self, point):
        """Record results of a sampling iteration.

        Parameters
        ----------
        point : dict
            Values mapped to variable names
        """
        for varname, value in zip(self.varnames, self.fn(point)):
            values = (self.draw_idx, self.chain) + tuple(np.ravel(value))
            self._queue[varname].append(values)

        if len(self._queue[varname]) > self._queue_limit:
            self._execute_queue()
        self.draw_idx += 1

Example 39

Project: postpic Source File: datahandling.py
Function: extent
    @property
    def extent(self):
        '''
        returns the extents in a linearized form,
        as required by "matplotlib.pyplot.imshow".
        '''
        return np.ravel([a.extent for a in self.axes])

Example 40

Project: chaco Source File: colormapper_test_case.py
    def test_simple_map(self):

        a = ArrayDataSource(array([0.0, 0.5, 1.0]))
        self.colormap.range.add(a)
        b = self.colormap.map_screen(a.get_data())
        self.colormap.range.remove(a)

        expected = array([0.0, 0.5, 1.0])

        close = allclose(ravel(b[:,:1]), expected, atol=0.02)
        self.assert_(close,
            "Simple map failed.  Expected %s.  Got %s" % (expected, b[:,:1]))

        return

Example 41

Project: cgat Source File: Genomics.py
def CalculatePairIndices(seq1, seq2, gap_char="-", with_codons=False):
    """returns number of idential and transitions/transversions substitutions
    in the alignment.

    If with-codons = True, synonymous and nonsynonymous changes will
    be recorded as well. The routine assumes no frame-shifts and will
    count more than one change as non-synonymous.

    """
    alphabet = "ACGT" + gap_char

    map_char2pos = {}
    for x in alphabet:
        map_char2pos[x] = len(map_char2pos)

    # build coordinates for various substitution subsets
    transitions, transversions = [], []
    for x in ("AG", "GA", "CT", "TC"):
        transitions.append((map_char2pos[x[0]], map_char2pos[x[1]]))

    for x in ("AT", "TA", "GT", "TG", "GC", "CG", "AC", "CA"):
        transversions.append((map_char2pos[x[0]], map_char2pos[x[1]]))

    matrix = AlignedPair2SubstitutionMatrix(seq1, seq2, alphabet)
    matrix_acgt = matrix[0:4, 0:4]

    if with_codons:
        result = SequencePairInfoCodons()
    else:
        result = SequencePairInfo()

    result.mMatrix = matrix
    result.mMapChar2Pos = map_char2pos
    result.mNAligned = numpy.sum(numpy.ravel(matrix_acgt))
    result.mNIdentical = numpy.sum(numpy.trace(matrix_acgt))
    result.mNTransitions = numpy.sum([matrix[x] for x in transitions])
    result.mNTransversions = numpy.sum([matrix[x] for x in transversions])
    result.mNDifferent = result.mNAligned - result.mNIdentical
    result.mNUnaligned1 = numpy.sum(numpy.ravel(matrix[0:4, 4]))
    result.mNUnaligned2 = numpy.sum(numpy.ravel(matrix[4, 0:4]))

    if with_codons:
        nsyn, nnon = 0, 0
        pairs = list(zip(seq1, seq2))
        for x in range(len(pairs)):
            a, b = pairs[x]
            if a != b:

                l = (x // 3) * 3
                c1 = MapCodon2AA(seq1[l:l + 3])
                c2 = MapCodon2AA(seq2[l:l + 3])

                if c1 == GAP_CHAR or c2 == GAP_CHAR:
                    continue

                # print x, a, b, l, c1, c2, seq1[l:l+3], seq2[l:l+3], c1 == c2
                if c1 == c2:
                    nsyn += 1
                else:
                    nnon += 1

        result.mNSynonymous = nsyn
        result.mNNonSynonymous = nnon

    return result

Example 42

Project: cgat Source File: SequencePairProperties.py
Function: loadpair
    def loadPair(self, seq1, seq2):
        """load sequence properties from a pair.
        """
        SequencePairProperties.loadPair(self, seq1, seq2)

        alphabet = self.mAlphabet + self.mGapChar

        map_char2pos = {}
        for x in alphabet:
            map_char2pos[x] = len(map_char2pos)

        # build coordinates for various substitution subsets
        transitions, transversions = [], []
        for x in ("AG", "GA", "CT", "TC"):
            transitions.append((map_char2pos[x[0]], map_char2pos[x[1]]))

        for x in ("AT", "TA", "GT", "TG", "GC", "CG", "AC", "CA"):
            transversions.append((map_char2pos[x[0]], map_char2pos[x[1]]))

        matrix = self.buildSubstitutionMatrix(seq1, seq2, alphabet)
        matrix_acgt = matrix[0:4, 0:4]

        self.mMatrix = matrix
        self.mMapChar2Pos = map_char2pos
        self.mNAligned = numpy.sum(numpy.ravel(matrix_acgt))
        self.mNIdentical = numpy.sum(numpy.trace(matrix_acgt))
        self.mNTransitions = numpy.sum([matrix[x] for x in transitions])
        self.mNTransversions = numpy.sum([matrix[x] for x in transversions])
        self.mNDifferent = self.mNAligned - self.mNIdentical
        self.mNUnaligned1 = numpy.sum(numpy.ravel(matrix[0:4, 4]))
        self.mNUnaligned2 = numpy.sum(numpy.ravel(matrix[4, 0:4]))

        cp = self.mMapChar2Pos['C']
        gp = self.mMapChar2Pos['G']

        # sum all rows and columns that have a least one G or C
        # and remove those that have two in order to not double count
        gc = numpy.sum(
            self.mMatrix[0:4, cp] +
            self.mMatrix[0:4, gp] +
            self.mMatrix[cp, 0:4] +
            self.mMatrix[gp, 0:4]) \
            - self.mMatrix[cp, cp] \
            - self.mMatrix[gp, gp] \
            - self.mMatrix[cp, gp] \
            - self.mMatrix[gp, cp]

        try:
            self.mPercentGC = "%5.2f" % (100.0 * float(gc) / self.mNAligned)
        except ZeroDivisionError:
            self.mPercentGC = "na"

Example 43

Project: xarray Source File: plot.py
Function: hist
def hist(darray, ax=None, **kwargs):
    """
    Histogram of DataArray

    Wraps matplotlib.pyplot.hist

    Plots N dimensional arrays by first flattening the array.

    Parameters
    ----------
    darray : DataArray
        Can be any dimension
    ax : matplotlib axes, optional
        If not passed, uses the current axis
    **kwargs : optional
        Additional keyword arguments to matplotlib.pyplot.hist

    """
    import matplotlib.pyplot as plt

    if ax is None:
        ax = plt.gca()

    no_nan = np.ravel(darray.values)
    no_nan = no_nan[pd.notnull(no_nan)]

    primitive = ax.hist(no_nan, **kwargs)

    ax.set_ylabel('Count')

    if darray.name is not None:
        ax.set_title('Histogram of {0}'.format(darray.name))

    return primitive

Example 44

Project: xarray Source File: utils.py
def _determine_cmap_params(plot_data, vmin=None, vmax=None, cmap=None,
                           center=None, robust=False, extend=None,
                           levels=None, filled=True, cnorm=None):
    """
    Use some heuristics to set good defaults for colorbar and range.

    Adapted from Seaborn:
    https://github.com/mwaskom/seaborn/blob/v0.6/seaborn/matrix.py#L158

    Parameters
    ==========
    plot_data: Numpy array
        Doesn't handle xarray objects

    Returns
    =======
    cmap_params : dict
        Use depends on the type of the plotting function
    """
    ROBUST_PERCENTILE = 2.0
    import matplotlib as mpl

    calc_data = np.ravel(plot_data[~pd.isnull(plot_data)])

    # Setting center=False prevents a divergent cmap
    possibly_divergent = center is not False

    # Set center to 0 so math below makes sense but remember its state
    center_is_none = False
    if center is None:
        center = 0
        center_is_none = True

    # Setting both vmin and vmax prevents a divergent cmap
    if (vmin is not None) and (vmax is not None):
        possibly_divergent = False

    # vlim might be computed below
    vlim = None

    if vmin is None:
        if robust:
            vmin = np.percentile(calc_data, ROBUST_PERCENTILE)
        else:
            vmin = calc_data.min()
    elif possibly_divergent:
        vlim = abs(vmin - center)

    if vmax is None:
        if robust:
            vmax = np.percentile(calc_data, 100 - ROBUST_PERCENTILE)
        else:
            vmax = calc_data.max()
    elif possibly_divergent:
        vlim = abs(vmax - center)

    if possibly_divergent:
        # kwargs not specific about divergent or not: infer defaults from data
        divergent = ((vmin < 0) and (vmax > 0)) or not center_is_none
    else:
        divergent = False

    # A divergent map should be symmetric around the center value
    if divergent:
        if vlim is None:
            vlim = max(abs(vmin - center), abs(vmax - center))
        vmin, vmax = -vlim, vlim

    # Now add in the centering value and set the limits
    vmin += center
    vmax += center

    # Choose default colormaps if not provided
    if cmap is None:
        if divergent:
            cmap = "RdBu_r"
        else:
            cmap = "viridis"

    # Allow viridis before matplotlib 1.5
    if cmap == "viridis":
        cmap = _load_default_cmap()

    # Handle discrete levels
    if levels is not None:
        if isinstance(levels, int):
            ticker = mpl.ticker.MaxNLocator(levels)
            levels = ticker.tick_values(vmin, vmax)
        vmin, vmax = levels[0], levels[-1]

    if extend is None:
        extend = _determine_extend(calc_data, vmin, vmax)

    if levels is not None:
        cmap, cnorm = _build_discrete_cmap(cmap, levels, extend, filled)

    return dict(vmin=vmin, vmax=vmax, cmap=cmap, extend=extend,
                levels=levels, norm=cnorm)

Example 45

Project: pystruct Source File: linear_programming.py
def lp_general_graph(unaries, edges, edge_weights):
    if unaries.shape[1] != edge_weights.shape[1]:
        raise ValueError("incompatible shapes of unaries"
                         " and edge_weights.")
    if edge_weights.shape[1] != edge_weights.shape[2]:
        raise ValueError("Edge weights not square!")
    if edge_weights.shape[0] != edges.shape[0]:
        raise ValueError("Number of edge weights different from number of"
                         "edges")

    n_nodes, n_states = map(int, unaries.shape)
    n_edges = len(edges)

    # variables: n_nodes * n_states for nodes,
    # n_edges * n_states ** 2 for edges
    n_variables = n_nodes * n_states + n_edges * n_states ** 2

    # constraints: one per node,
    # and n_nodes * n_states for pairwise minus one redundant per edge
    n_constraints = n_nodes + n_edges * (2 * n_states - 1)

    # offset to get to the edge variables in columns
    edges_offset = n_nodes * n_states
    # store constraints as triple (data, I, J)
    data, I, J = [], [], []

    # summation constraints
    for i in range(n_nodes):
        for j in range(n_states):
            data.append(1)
            I.append(i)
            J.append(i * n_states + j)
            #constraints[i, i * n_states + j] = 1
    # we row_idx tracks constraints = rows in constraint matrix
    row_idx = n_nodes
    # edge marginalization constraint
    for i in range(2 * n_edges * n_states):
        edge = i // (2 * n_states)
        state = (i % n_states)
        vertex_in_edge = i % (2 * n_states) // n_states
        vertex = edges[edge][vertex_in_edge]
        if vertex_in_edge == 1 and state == n_states - 1:
            # the last summation constraint is redundant.
            continue
        # for one vertex iterate over all states of the other vertex
        #[row_idx, int(vertex) * n_states + state] = -1
        data.append(-1)
        I.append(row_idx)
        J.append(int(vertex) * n_states + state)
        edge_var_index = edges_offset + edge * n_states ** 2
        if vertex_in_edge == 0:
            # first vertex in edge
            for j in range(n_states):
                data.append(1)
                I.append(row_idx)
                J.append(edge_var_index + state * n_states + j)
                #[row_idx, edge_var_index + state * n_states + j] = 1
        else:
            # second vertex in edge
            for j in range(n_states):
                data.append(1)
                I.append(row_idx)
                J.append(edge_var_index + j * n_states + state)
                #[row_idx, edge_var_index + j * n_states + state] = 1
        row_idx += 1

    coef = np.ravel(unaries)
    # pairwise:
    repeated_pairwise = edge_weights.ravel()
    coef = np.hstack([coef, repeated_pairwise])
    c = cvxopt.matrix(coef, tc='d')
    # for positivity inequalities
    G = cvxopt.spdiag(cvxopt.matrix(-np.ones(n_variables)))
    #G = cvxopt.matrix(-np.eye(n_variables))
    h = cvxopt.matrix(np.zeros(n_variables))  # for positivity inequalities
    # unary and pairwise summation constratints
    A = cvxopt.spmatrix(data, I, J)
    assert(n_constraints == A.size[0])
    b_ = np.zeros(A.size[0])  # zeros for pairwise summation constraints
    b_[:n_nodes] = 1    # ones for unary summation constraints
    b = cvxopt.matrix(b_)

    # don't be verbose.
    show_progress_backup = cvxopt.solvers.options.get('show_progress', False)
    cvxopt.solvers.options['show_progress'] = False
    result = cvxopt.solvers.lp(c, G, h, A, b)
    cvxopt.solvers.options['show_progress'] = show_progress_backup

    x = np.array(result['x'])
    unary_variables = x[:n_nodes * n_states].reshape(n_nodes, n_states)
    pairwise_variables = x[n_nodes * n_states:].reshape(n_edges, n_states ** 2)
    assert((np.abs(unary_variables.sum(axis=1) - 1) < 1e-4).all())
    assert((np.abs(pairwise_variables.sum(axis=1) - 1) < 1e-4).all())
    return unary_variables, pairwise_variables, result['primal objective']

Example 46

Project: pyresample Source File: geometry.py
    def get_cartesian_coords(self, nprocs=None, data_slice=None, cache=False):
        """Retrieve cartesian coordinates of geometry definition

        Parameters
        ----------
        nprocs : int, optional
            Number of processor cores to be used.
            Defaults to the nprocs set when instantiating object
        data_slice : slice object, optional
            Calculate only cartesian coordnates for the defined slice
        cache : bool, optional
            Store result the result. Requires data_slice to be None

        Returns
        -------
        cartesian_coords : numpy array
        """

        if self.cartesian_coords is None:
            # Coordinates are not cached
            if nprocs is None:
                nprocs = self.nprocs

            if data_slice is None:
                # Use full slice
                data_slice = slice(None)

            lons, lats = self.get_lonlats(nprocs=nprocs, data_slice=data_slice)

            if nprocs > 1:
                cartesian = _spatial_mp.Cartesian_MP(nprocs)
            else:
                cartesian = _spatial_mp.Cartesian()

            cartesian_coords = cartesian.transform_lonlats(np.ravel(lons),
                                                           np.ravel(lats))

            if isinstance(lons, np.ndarray) and lons.ndim > 1:
                # Reshape to correct shape
                cartesian_coords = cartesian_coords.reshape(lons.shape[0],
                                                            lons.shape[1], 3)

            if cache and data_slice is None:
                self.cartesian_coords = cartesian_coords
        else:
            # Coordinates are cached
            if data_slice is None:
                cartesian_coords = self.cartesian_coords
            else:
                cartesian_coords = self.cartesian_coords[data_slice]

        return cartesian_coords

Example 47

Project: pycwt Source File: helpers.py
Function: find
def find(condition):
    "Return the indices where ravel(condition) is true"
    res, = np.nonzero(np.ravel(condition))
    return res

Example 48

Project: picrust Source File: evaluate_test_datasets.py
def evaluate_test_dataset(observed_table,expected_table):
    """ evaluate the correlation between an observed and expected
    biom table.

    Returns data points for a scatter plot of observed v. expected values,
    and a dict of correlations keyed by method (each containing the r value,
    then the probability)
    """
    # identify the overlapping otus that can be used to predict metagenomes
    overlapping_ids = list(set(observed_table.ids(axis='observation')) &
                            set(expected_table.ids(axis='observation')))

    if len(overlapping_ids) < 1:
        print "obs ids:",observed_table.ids(axis='observation')[0:10]
        print "exp ids:",expected_table.ids(axis='observation')[0:10]

        raise ValueError,\
         "No ids are in common  between the observed and expected tables, so no evaluations can be performed."

    # create lists to contain filtered data - we're going to need the data in
    # numpy arrays, so it makes sense to compute this way rather than filtering
    # the tables
    obs_data = []
    exp_data = []

    # build lists of filtered data
    for obs_id in overlapping_ids:
        obs_data.append(observed_table.data(obs_id, axis='observation'))
        exp_data.append(expected_table.data(obs_id, axis='observation'))

    flat_obs_data = ravel(array(obs_data))
    flat_exp_data = ravel(array(exp_data))

    #GET THE SCATTER PLOT POINTS
    scatter_data_points =\
      zip(flat_obs_data,flat_exp_data)

    correlations = {}
    if len(scatter_data_points) <= 2:
        #can't validly calc correlation
        correlations["pearson"] = (None,None)
        correlations["spearman"] = (None,None)
        return scatter_data_points,correlations
    # CALCULATE CORRELATIONS


    pearson_r,pearson_t_prob =\
      correlation(flat_obs_data,flat_exp_data)

    correlations["pearson"] = (pearson_r,pearson_t_prob)
    pearson_r2 = pearson_r**2
    correlations["pearson_r2"] = [pearson_r2]
    spearman_r,spearman_t_prob =\
      spearman_correlation(flat_obs_data,flat_exp_data)

    correlations["spearman"] = (spearman_r,spearman_t_prob)
    spearman_r2 = spearman_r**2
    correlations["spearman_r2"] = [spearman_r2]

    return scatter_data_points,correlations

Example 49

Project: auto-sklearn Source File: util.py
Function: normalize_array
def normalize_array(solution, prediction):
    """
    Use min and max of solution as scaling factors to normalize prediction,
    then threshold it to [0, 1].

    Binarize solution to {0, 1}. This allows applying classification
    scores to all cases. In principle, this should not do anything to
    properly formatted classification inputs and outputs.

    :param solution:
    :param prediction:
    :return:
    """
    # Binarize solution
    sol = np.ravel(solution)  # convert to 1-d array
    maxi = np.nanmax(sol[np.isfinite(sol)])
    mini = np.nanmin(sol[np.isfinite(sol)])
    if maxi == mini:
        print('Warning, cannot normalize')
        return [solution, prediction]
    diff = maxi - mini
    mid = (maxi + mini) / 2.

    solution[solution >= mid] = 1
    solution[solution < mid] = 0
    # Normalize and threshold predictions (takes effect only if solution not
    # in {0, 1})

    prediction -= float(mini)
    prediction /= float(diff)

    # and if predictions exceed the bounds [0, 1]
    prediction[prediction > 1] = 1
    prediction[prediction < 0] = 0
    # Make probabilities smoother
    # new_prediction = np.power(new_prediction, (1./10))
    return [solution, prediction]

Example 50

Project: numexpr Source File: test_numexpr.py
def equal(a, b, exact):
    if array_equal(a, b):
        return True

    if hasattr(a, 'dtype') and a.dtype in ['f4', 'f8']:
        nnans = isnan(a).sum()
        if nnans > 0:
            # For results containing NaNs, just check that the number
            # of NaNs is the same in both arrays.  This check could be
            # made more exhaustive, but checking element by element in
            # python space is very expensive in general.
            return nnans == isnan(b).sum()
        ninfs = isinf(a).sum()
        if ninfs > 0:
            # Ditto for Inf's
            return ninfs == isinf(b).sum()
    if exact:
        return (shape(a) == shape(b)) and alltrue(ravel(a) == ravel(b), axis=0)
    else:
        if hasattr(a, 'dtype') and a.dtype == 'f4':
            atol = 1e-5  # Relax precission for special opcodes, like fmod
        else:
            atol = 1e-8
        return (shape(a) == shape(b) and
                allclose(ravel(a), ravel(b), atol=atol))
See More Examples - Go to Next Page
Page 1 Selected Page 2 Page 3