python类ndenumerate()的实例源码

HBLRWrapper.py 文件源码 项目:PersonalizedMultitaskLearning 作者: mitmedialab 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def saveHintonPlot(self, matrix, num_tests, max_weight=None, ax=None):
        """Draw Hinton diagram for visualizing a weight matrix."""
        fig,ax = plt.subplots(1,1)

        if not max_weight:
            max_weight = 2**np.ceil(np.log(np.abs(matrix).max())/np.log(2))

        ax.patch.set_facecolor('gray')
        ax.set_aspect('equal', 'box')
        ax.xaxis.set_major_locator(plt.NullLocator())
        ax.yaxis.set_major_locator(plt.NullLocator())

        for (x, y), w in np.ndenumerate(matrix):
            color = 'white' if w > 0 else 'black'
            size = np.sqrt(np.abs(0.5*w/num_tests)) # Need to scale so that it is between 0 and 0.5
            rect = plt.Rectangle([x - size / 2, y - size / 2], size, size,
                                 facecolor=color, edgecolor=color)
            ax.add_patch(rect)

        ax.autoscale_view()
        ax.invert_yaxis()
        plt.savefig(self.figures_path + self.save_prefix + '-Hinton.eps')
        plt.close()
point_generator.py 文件源码 项目:MatchZoo 作者: faneshion 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def cal_hist(self, t1, t2, data1_maxlen, hist_size):
        mhist = np.zeros((data1_maxlen, hist_size), dtype=np.float32)
        d1len = len(self.data1[t1])
        if self.use_hist_feats:
            assert (t1, t2) in self.hist_feats
            caled_hist = np.reshape(self.hist_feats[(t1, t2)], (d1len, hist_size))
            if d1len < data1_maxlen:
                mhist[:d1len, :] = caled_hist[:, :]
            else:
                mhist[:, :] = caled_hist[:data1_maxlen, :]
        else:
            t1_rep = self.embed[self.data1[t1]]
            t2_rep = self.embed[self.data2[t2]]
            mm = t1_rep.dot(np.transpose(t2_rep))
            for (i,j), v in np.ndenumerate(mm):
                if i >= data1_maxlen:
                    break
                vid = int((v + 1.) / 2. * ( hist_size - 1.))
                mhist[i][vid] += 1.
            mhist += 1.
            mhist = np.log10(mhist)
        return mhist
list_generator.py 文件源码 项目:MatchZoo 作者: faneshion 项目源码 文件源码 阅读 47 收藏 0 点赞 0 评论 0
def cal_hist(self, t1, t2, data1_maxlen, hist_size):
        mhist = np.zeros((data1_maxlen, hist_size), dtype=np.float32)
        t1_cont = list(self.data1[t1])
        t2_cont = list(self.data2[t2])
        d1len = len(t1_cont)
        if self.use_hist_feats:
            assert (t1, t2) in self.hist_feats
            caled_hist = np.reshape(self.hist_feats[(t1, t2)], (d1len, hist_size))
            if d1len < data1_maxlen:
                mhist[:d1len, :] = caled_hist[:, :]
            else:
                mhist[:, :] = caled_hist[:data1_maxlen, :]
        else:
            t1_rep = self.embed[t1_cont]
            t2_rep = self.embed[t2_cont]
            mm = t1_rep.dot(np.transpose(t2_rep))
            for (i,j), v in np.ndenumerate(mm):
                if i >= data1_maxlen:
                    break
                vid = int((v + 1.) / 2. * ( hist_size - 1.))
                mhist[i][vid] += 1.
            mhist += 1.
            mhist = np.log10(mhist)
        return mhist
pair_generator.py 文件源码 项目:MatchZoo 作者: faneshion 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def cal_hist(self, t1, t2, data1_maxlen, hist_size):
        mhist = np.zeros((data1_maxlen, hist_size), dtype=np.float32)
        t1_cont = list(self.data1[t1])
        t2_cont = list(self.data2[t2])
        d1len = len(t1_cont)
        if self.use_hist_feats:
            assert (t1, t2) in self.hist_feats
            curr_pair_feats = list(self.hist_feats[(t1, t2)])
            caled_hist = np.reshape(curr_pair_feats, (d1len, hist_size))
            if d1len < data1_maxlen:
                mhist[:d1len, :] = caled_hist[:, :]
            else:
                mhist[:, :] = caled_hist[:data1_maxlen, :]
        else:
            t1_rep = self.embed[t1_cont]
            t2_rep = self.embed[t2_cont]
            mm = t1_rep.dot(np.transpose(t2_rep))
            for (i,j), v in np.ndenumerate(mm):
                if i >= data1_maxlen:
                    break
                vid = int((v + 1.) / 2. * ( hist_size - 1.))
                mhist[i][vid] += 1.
            mhist += 1.
            mhist = np.log10(mhist)
        return mhist
list_generator.py 文件源码 项目:MatchZoo 作者: faneshion 项目源码 文件源码 阅读 69 收藏 0 点赞 0 评论 0
def cal_hist(self, t1, t2, data1_maxlen, hist_size):
        mhist = np.zeros((data1_maxlen, hist_size), dtype=np.float32)
        t1_cont = list(self.data1[t1])
        t2_cont = list(self.data2[t2])
        d1len = len(t1_cont)
        if self.use_hist_feats:
            assert (t1, t2) in self.hist_feats
            caled_hist = np.reshape(self.hist_feats[(t1, t2)], (d1len, hist_size))
            if d1len < data1_maxlen:
                mhist[:d1len, :] = caled_hist[:, :]
            else:
                mhist[:, :] = caled_hist[:data1_maxlen, :]
        else:
            t1_rep = self.embed[t1_cont]
            t2_rep = self.embed[t2_cont]
            mm = t1_rep.dot(np.transpose(t2_rep))
            for (i,j), v in np.ndenumerate(mm):
                if i >= data1_maxlen:
                    break
                vid = int((v + 1.) / 2. * ( hist_size - 1.))
                mhist[i][vid] += 1.
            mhist += 1.
            mhist = np.log10(mhist)
        return mhist
pair_generator.py 文件源码 项目:MatchZoo 作者: faneshion 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def cal_hist(self, t1, t2, data1_maxlen, hist_size):
        mhist = np.zeros((data1_maxlen, hist_size), dtype=np.float32)
        t1_cont = list(self.data1[t1])
        t2_cont = list(self.data2[t2])
        d1len = len(t1_cont)
        if self.use_hist_feats:
            assert (t1, t2) in self.hist_feats
            curr_pair_feats = list(self.hist_feats[(t1, t2)])
            caled_hist = np.reshape(curr_pair_feats, (d1len, hist_size))
            if d1len < data1_maxlen:
                mhist[:d1len, :] = caled_hist[:, :]
            else:
                mhist[:, :] = caled_hist[:data1_maxlen, :]
        else:
            t1_rep = self.embed[t1_cont]
            t2_rep = self.embed[t2_cont]
            mm = t1_rep.dot(np.transpose(t2_rep))
            for (i,j), v in np.ndenumerate(mm):
                if i >= data1_maxlen:
                    break
                vid = int((v + 1.) / 2. * ( hist_size - 1.))
                mhist[i][vid] += 1.
            mhist += 1.
            mhist = np.log10(mhist)
        return mhist
bc.py 文件源码 项目:fem 作者: mlp6 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def write_pml_elems(sorted_pml_elems, pmlfile="elems_pml.dyn"):
    """Create a new elements file that the PML elements.

    :param sorted_pml_elems:
    :param pmlfile: default = elems_pml.dyn
    :returns:
    """
    from numpy import ndenumerate

    pml = open(pmlfile, 'w')
    pml.write('$ PML elements generated by bc.py\n')
    pml.write('*ELEMENT_SOLID\n')
    for i, e in ndenumerate(sorted_pml_elems):
        pml.write('%i,%i,%i,%i,%i,%i,%i,%i,%i,%i\n' % (e['id'], e['pid'],
                                                       e['n1'], e['n2'],
                                                       e['n3'], e['n4'],
                                                       e['n5'], e['n6'],
                                                       e['n7'], e['n8']))
    pml.write('*END\n')
    pml.close()

    return 0
checkerboard.py 文件源码 项目:dataset-shift-osdc16 作者: pprett 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def generate_data(sample_size=200, pd=[[0.4, 0.4], [0.1, 0.1]]):
    pd = np.array(pd)
    pd /= pd.sum()
    offset = 50
    bins = np.r_[np.zeros((1,)), np.cumsum(pd)]
    bin_counts = np.histogram(np.random.rand(sample_size), bins)[0]
    data = np.empty((0, 2))
    targets = []
    for ((i, j), p), count in zip(np.ndenumerate(pd), bin_counts):
        xs = np.random.uniform(low=0.0, high=50.0, size=count) + j * offset
        ys = np.random.uniform(low=0.0, high=50.0, size=count) + -i * offset
        data = np.vstack((data, np.c_[xs, ys]))
        if i == j:
            targets.extend([1] * count)
        else:
            targets.extend([-1] * count)
    return np.c_[data, targets]
Model.py 文件源码 项目:NADE 作者: MarcCote 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def finite_diff_gradients(self, f, delta=1e-6):
        """
        f is called without parameters, the changes in the parameters happen as a side effect
        """
        gradients = dict()
        fx = f()
        for p in self.parameters_to_optimise:
            original = self.parameters[p].get_value()
            grad = np.zeros_like(original)
            if np.prod(original.shape) > 1:
                for index, _ in np.ndenumerate(original):
                    xh = original.copy()
                    xh[index] += delta
                    self.parameters[p].set_value(xh)
                    grad[index] = (f() - fx) / delta
                    self.parameters[p].set_value(original)
            else:
                xh = original.copy()
                xh += delta
                self.parameters[p].set_value(xh)
                grad = (f() - fx) / delta
                self.parameters[p].set_value(original)
            gradients[p] = grad
        return gradients
test_gpu_operations.py 文件源码 项目:Aurora 作者: upul 项目源码 文件源码 阅读 45 收藏 0 点赞 0 评论 0
def test_reduce_sum_axis_zero():
    ctx = ndarray.gpu(0)
    shape = (500, 200, 100)
    to_shape = (200, 100)
    x = np.random.uniform(0, 20, shape).astype(np.float32)
    arr_x = ndarray.array(x, ctx=ctx)
    arr_y = ndarray.empty(to_shape, ctx=ctx)
    gpu_op.reduce_sum_axis_zero(arr_x, arr_y)
    y = arr_y.asnumpy()
    y_ = np.sum(x, axis=0)
    for index, _ in np.ndenumerate(y):
        v = y[index]
        v_ = y_[index]
        if abs((v - v_) / v_) > 1e-4:
            print(index, v, v_)
    np.testing.assert_allclose(np.sum(x, axis=0), y, rtol=1e-5)
datamodel.py 文件源码 项目:heliopy 作者: heliopython 项目源码 文件源码 阅读 48 收藏 0 点赞 0 评论 0
def _dateToISO(indict):
    """
    covert datetimes to iso strings inside of datamodel attributes
    """
    retdict = dmcopy(indict)
    if isinstance(indict, dict):
        for key in indict:
            if isinstance(indict[key], datetime.datetime):
                retdict[key] = retdict[key].isoformat()
            elif hasattr(indict[key], '__iter__'):
                for idx, el in enumerate(indict[key]):
                    if isinstance(el, datetime.datetime):
                        retdict[key][idx] = el.isoformat()
    else:
        if isinstance(indict, datetime.datetime):
            retdict = retdict.isoformat()
        elif hasattr(indict, '__iter__'):
            retdict = numpy.asanyarray(indict)
            for idx, el in numpy.ndenumerate(indict):
                if isinstance(el, datetime.datetime):
                    retdict[idx] = el.isoformat()
    return retdict
core.py 文件源码 项目:PyBloqs 作者: manahl 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def _flatten_data(data, chart_cfg, switch_zy=False):
        plot_axes_def = [(0, XAxis), (1, YAxis)]

        # Inject categories into the axis definitions of the plot
        if isinstance(data, NDFrame):
            for i, plot_axis in plot_axes_def[:data.ndim]:
                categories = data.axes[i]
                # Skip numeric indices
                if not categories.is_numeric():
                    chart_cfg = chart_cfg.inherit_many(plot_axis(categories=list(categories)))

        data = [list(index) + [value] for index, value in list(np.ndenumerate(data))]

        if switch_zy:
            for i in xrange(len(data)):
                tmp = data[i][-1]
                data[i][-1] = data[i][-2]
                data[i][-2] = tmp

        return data, chart_cfg
Spacetime.py 文件源码 项目:spectroscopy 作者: jgoodknight 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def returnAmplitudeFromListOfFunctionValues(self, listOfFunctionValues, additive=False):
        """Helper function for the amplitude setting function
        [f(x), g(y), ...]
        additive=True  => F(x, y, ...) = f(x) + g(y) + ...
        additive=False => F(x, y, ...) = f(x) * g(y) * ..."""
        output = self.functionSpaceZero()
        if additive:
            initialValue = 0.0
        else:
            initialValue = 1.0
        for indexTuple, value in np.ndenumerate(output):
            newValue = initialValue
            for tupleIndex, tupleValue in enumerate(indexTuple):
                if additive:
                    newValue += listOfFunctionValues[tupleIndex][tupleValue]
                else:
                    newValue *= listOfFunctionValues[tupleIndex][tupleValue]
            output[indexTuple] = newValue
        return output
mask.py 文件源码 项目:CNN-Glasses-Remover 作者: JubilantJerry 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def generate(width, height, s, output):
    canvas = np.zeros((height, width), dtype='float64')
    max_d = math.sqrt(width**2 + height**2) / 2
    offset_angular = 0
    if (width >= height):
        offset_angular = math.pi / 4
    for (i, j), _ in np.ndenumerate(canvas):
        y = height // 2 - i
        x = j - width // 2
        d = math.sqrt(x**2 + y**2)
        t = math.atan2(y, x)
        canvas[i,j] = (255 / 4) * \
            (2 + radial_sin(d, s, t) + angular_sin (
                d, t, max_d, s, offset_angular))
    f = open(output, 'wb')
    w = png.Writer(width, height, greyscale=True)
    w.write(f, canvas)
    f.close()
modelbase.py 文件源码 项目:pymake 作者: dtrckd 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def sample(self):
        self._update_m()

        indices = np.ndenumerate(self.count_k_by_j)

        lgg.debug('Sample m...')
        for ind in indices:
            j, k = ind[0]
            count = ind[1]

            if count > 0:
                # Sample number of tables in j serving dishe k
                params = self.prob_jk(j, k)
                sample = categorical(params) + 1
            else:
                sample = 0

            self.m[j, k] = sample

        self.m_dotk = self.m.sum(0)
        self.purge_empty_tables()

        return self.m
projection.py 文件源码 项目:wrf-python 作者: NCAR 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def cartopy_xlim(self, geobounds):
        """Return the x extents in projected coordinates for cartopy.

        Returns:

            :obj:`list`: A pair of [xmin, xmax].

        See Also:

            :mod:`cartopy`, :mod:`matplotlib`

        """
        try:
            _ = len(geobounds)
        except TypeError:
            x_extents= self._cart_extents(geobounds)[0]
        else:
            extents = self._cart_extents(geobounds)
            x_extents = np.empty(extents.shape, np.object)

            for idxs, extent in np.ndenumerate(extents):
                x_extents[idxs] = extent[0]

        return x_extents
projection.py 文件源码 项目:wrf-python 作者: NCAR 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def cartopy_ylim(self, geobounds):
        """Return the y extents in projected coordinates for cartopy.

        Returns:

            :obj:`list`: A pair of [ymin, ymax].

        See Also:

            :mod:`cartopy`, :mod:`matplotlib`

        """
        try:
            _ = len(geobounds)
        except TypeError:
            y_extents= self._cart_extents(geobounds)[1]
        else:
            extents = self._cart_extents(geobounds)
            y_extents = np.empty(extents.shape, np.object)

            for idxs, extent in np.ndenumerate(extents):
                y_extents[idxs] = extent[1]

        return y_extents
tools.py 文件源码 项目:mtcnn-caffe 作者: CongWeilin 项目源码 文件源码 阅读 35 收藏 0 点赞 0 评论 0
def detect_face_12net(cls_prob,roi,out_side,scale,width,height,threshold):
    in_side = 2*out_side+11
    stride = 0
    if out_side != 1:
        stride = float(in_side-12)/(out_side-1)
    boundingBox = []

    for (x,y), prob in np.ndenumerate(cls_prob):
        if(prob >= threshold):
            original_x1 = int((stride*x + 1)*scale)
            original_y1 = int((stride*y + 1)*scale)
            original_w  = int((12.0 -1)*scale)
            original_h  = int((12.0 -1)*scale)
            original_x2 = original_x1 + original_w
            original_y2 = original_y1 + original_h
            rect = []
            x1 = int(round(max(0     , original_x1 + original_w * roi[0][x][y])))
            y1 = int(round(max(0     , original_y1 + original_h * roi[1][x][y])))
            x2 = int(round(min(width , original_x2 + original_w * roi[2][x][y])))
            y2 = int(round(min(height, original_y2 + original_h * roi[3][x][y])))
        if x2>x1 and y2>y1:
                rect = [x1,y1,x2,y2,prob]
                boundingBox.append(rect)
    return NMS(boundingBox,0.5,'iou')
plotting.py 文件源码 项目:kvae 作者: simonkamronn 项目源码 文件源码 阅读 42 收藏 0 点赞 0 评论 0
def hinton(matrix, max_weight=None, ax=None):
    """Draw Hinton diagram for visualizing a weight matrix."""
    ax = ax if ax is not None else plt.gca()

    if not max_weight:
        max_weight = 2 ** np.ceil(np.log(np.abs(matrix).max()) / np.log(2))

    ax.patch.set_facecolor('gray')
    ax.set_aspect('equal', 'box')
    ax.xaxis.set_major_locator(plt.NullLocator())
    ax.yaxis.set_major_locator(plt.NullLocator())

    for (x, y), w in np.ndenumerate(matrix):
        color = 'white' if w > 0 else 'black'
        size = np.sqrt(np.abs(w) / max_weight)
        rect = plt.Rectangle([x - size / 2, y - size / 2], size, size,
                             facecolor=color, edgecolor=color)
        ax.add_patch(rect)

    ax.autoscale_view()
    ax.invert_yaxis()
stats.py 文件源码 项目:siHMM 作者: Ardavans 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def sample_crp_tablecounts(concentration,customers,colweights):
    m = np.zeros_like(customers)
    tot = customers.sum()
    randseq = np.random.random(tot)

    starts = np.empty_like(customers)
    starts[0,0] = 0
    starts.flat[1:] = np.cumsum(np.ravel(customers)[:customers.size-1])

    for (i,j), n in np.ndenumerate(customers):
        w = colweights[j]
        for k in xrange(n):
            m[i,j] += randseq[starts[i,j]+k] \
                    < (concentration * w) / (k + concentration * w)

    return m

### Entropy
stats.py 文件源码 项目:siHMM 作者: Ardavans 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def sample_crp_tablecounts(concentration,customers,colweights):
    m = np.zeros_like(customers)
    tot = customers.sum()
    randseq = np.random.random(tot)

    starts = np.empty_like(customers)
    starts[0,0] = 0
    starts.flat[1:] = np.cumsum(np.ravel(customers)[:customers.size-1])

    for (i,j), n in np.ndenumerate(customers):
        w = colweights[j]
        for k in xrange(n):
            m[i,j] += randseq[starts[i,j]+k] \
                    < (concentration * w) / (k + concentration * w)

    return m

### Entropy
hsmm_states.py 文件源码 项目:siHMM 作者: Ardavans 项目源码 文件源码 阅读 31 收藏 0 点赞 0 评论 0
def hmm_trans_matrix(self):
        # NOTE: more general version, allows different delays, o/w we could
        # construct with np.kron
        if self._hmm_trans_matrix is None:
            ps, delays = map(np.array,zip(*[(d.p,d.delay) for d in self.dur_distns]))
            starts, ends = cumsum(delays,strict=True), cumsum(delays,strict=False)
            trans_matrix = self._hmm_trans_matrix = np.zeros((ends[-1],ends[-1]))

            for (i,j), Aij in np.ndenumerate(self.trans_matrix):
                block = trans_matrix[starts[i]:ends[i],starts[j]:ends[j]]
                if i == j:
                    block[:-1,1:] = np.eye(block.shape[0]-1)
                    block[-1,-1] = 1-ps[i]
                else:
                    block[-1,0] = ps[j]*Aij

        return self._hmm_trans_matrix
hsmm_inb_states.py 文件源码 项目:siHMM 作者: Ardavans 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def mf_bwd_trans_matrix(self):
        rs = self.rs
        starts, ends = cumsum(rs,strict=True), cumsum(rs,strict=False)
        trans_matrix = np.zeros((ends[-1],ends[-1]))

        Elnps, Eln1mps = zip(*[d._fixedr_distns[d.ridx]._mf_expected_statistics() for d in self.dur_distns])
        Eps, E1mps = np.exp(Elnps), np.exp(Eln1mps) # NOTE: actually exp(E[ln(p)]) etc

        enters = self.mf_bwd_enter_rows(rs,Eps,E1mps)
        for (i,j), Aij in np.ndenumerate(self.mf_trans_matrix):
            block = trans_matrix[starts[i]:ends[i],starts[j]:ends[j]]
            block[-1,:] = Aij * eE1mps[i] * enters[j]
            if i == j:
                block[...] += np.diag(np.repeat(eEps[i],rs[i])) \
                        + np.diag(np.repeat(eE1mps[i],rs[i]-1),k=1)

        assert np.all(trans_matrix >= 0)
        return trans_matrix
hsmm_inb_states.py 文件源码 项目:siHMM 作者: Ardavans 项目源码 文件源码 阅读 29 收藏 0 点赞 0 评论 0
def hmm_trans_matrix_orig(self):
        rs, ps, delays = self.rs, self.ps, self.delays
        starts, ends = cumsum(rs+delays,strict=True), cumsum(rs+delays,strict=False)
        trans_matrix = np.zeros((ends[-1],ends[-1]))

        enters = self.bwd_enter_rows
        for (i,j), Aij in np.ndenumerate(self.trans_matrix):
            block = trans_matrix[starts[i]:ends[i],starts[j]:ends[j]]

            if delays[i] == 0:
                block[-1,:rs[j]] = Aij * enters[j] * (1-ps[i])
            else:
                block[-1,:rs[j]] = Aij * enters[j]

            if i == j:
                block[:rs[i],:rs[i]] += \
                    np.diag(np.repeat(ps[i],rs[i])) + np.diag(np.repeat(1-ps[i],rs[i]-1),k=1)
                if delays[i] > 0:
                    block[rs[i]-1,rs[i]] = (1-ps[i])
                    block[rs[i]:,rs[i]:] = np.eye(delays[i],k=1)

        assert np.allclose(trans_matrix.sum(1),1.)
        return trans_matrix
hsmm_inb_states.py 文件源码 项目:siHMM 作者: Ardavans 项目源码 文件源码 阅读 34 收藏 0 点赞 0 评论 0
def hmm_trans_matrix_1(self):
        rs, ps, delays = self.rs, self.ps, self.delays
        starts, ends = cumsum(rs+delays,strict=True), cumsum(rs+delays,strict=False)
        trans_matrix = np.zeros((ends[-1],ends[-1]))

        enters = self.bwd_enter_rows
        for (i,j), Aij in np.ndenumerate(self.trans_matrix):
            block = trans_matrix[starts[i]:ends[i],starts[j]:ends[j]]

            block[-1,:rs[j]] = Aij * enters[j] * (1-ps[i])

            if i == j:
                block[-rs[i]:,-rs[i]:] += \
                    np.diag(np.repeat(ps[i],rs[i])) + np.diag(np.repeat(1-ps[i],rs[i]-1),k=1)
                if delays[i] > 0:
                    block[:delays[i]:,:delays[i]] = np.eye(delays[i],k=1)
                    block[delays[i]-1,delays[i]] = 1

        assert np.allclose(trans_matrix.sum(1),1.)
        return trans_matrix
hsmm_inb_states.py 文件源码 项目:siHMM 作者: Ardavans 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def hmm_trans_matrix_2(self):
        rs, ps, delays = self.rs, self.ps, self.delays
        starts, ends = cumsum(rs+delays,strict=True), cumsum(rs+delays,strict=False)
        trans_matrix = np.zeros((ends[-1],ends[-1]))

        enters = self.bwd_enter_rows
        for (i,j), Aij in np.ndenumerate(self.trans_matrix):
            block = trans_matrix[starts[i]:ends[i],starts[j]:ends[j]]

            block[-1,0] = Aij * (1-ps[i])

            if i == j:
                block[-rs[i]:,-rs[i]:] += \
                    np.diag(np.repeat(ps[i],rs[i])) + np.diag(np.repeat(1-ps[i],rs[i]-1),k=1)
                if delays[i] > 0:
                    block[:delays[i]:,:delays[i]] = np.eye(delays[i],k=1)
                    block[delays[i]-1,-rs[i]:] = enters[i]

        assert np.allclose(trans_matrix.sum(1),1.)
        return trans_matrix
common.py 文件源码 项目:cellranger 作者: 10XGenomics 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def convert_numpy_array_to_line_chart(array, ntype):
    array = np.sort(array)[::-1]

    rows = []
    previous_count = None
    for (index,), count in np.ndenumerate(array):
        if index == 0 or index == len(array)-1:
            rows.append([index, ntype(count)])
        elif previous_count != count:
            previous_index = rows[-1][0]
            if previous_index != index - 1:
                rows.append([index - 1, ntype(previous_count)])
            rows.append([index, ntype(count)])
        previous_count = count
    return rows
preprocess.py 文件源码 项目:MatchZoo 作者: faneshion 项目源码 文件源码 阅读 38 收藏 0 点赞 0 评论 0
def cal_hist(t1_rep, t2_rep, qnum, hist_size):
    #qnum = len(t1_rep)
    mhist = np.zeros((qnum, hist_size), dtype=np.float32)
    mm = t1_rep.dot(np.transpose(t2_rep))
    for (i,j), v in np.ndenumerate(mm):
        if i >= qnum:
            break
        vid = int((v + 1.) / 2. * (hist_size - 1.))
        mhist[i][vid] += 1.
    mhist += 1.
    mhist = np.log10(mhist)
    return mhist.flatten()
preprocess.py 文件源码 项目:MatchZoo 作者: faneshion 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def cal_binsum(t1_rep, t2_rep, qnum, bin_num):
    mbinsum = np.zeros((qnum, bin_num), dtype=np.float32)
    mm = t1_rep.dot(np.transpose(t2_rep))
    for (i, j), v in np.ndenumerate(mm):
        if i >= qnum:
            break
        vid = int((v + 1.) / 2. * (bin_num - 1.))
        mbinsum[i][vid] += v
    #mhist += 1. # smooth is not needed for computing bin sum
    #mhist = np.log10(mhist) # not needed for computing  bin sum
    return mbinsum.flatten()
preprocess.py 文件源码 项目:MatchZoo 作者: faneshion 项目源码 文件源码 阅读 33 收藏 0 点赞 0 评论 0
def cal_binsum(t1_rep, t2_rep, qnum, bin_num):
    mbinsum = np.zeros((qnum, bin_num), dtype=np.float32)
    mm = t1_rep.dot(np.transpose(t2_rep))
    for (i, j), v in np.ndenumerate(mm):
        if i >= qnum:
            break
        vid = int((v + 1.) / 2. * (bin_num - 1.))
        mbinsum[i][vid] += v
    #mhist += 1. # smooth is not needed for computing bin sum
    #mhist = np.log10(mhist) # not needed for computing  bin sum
    return mbinsum.flatten()


问题


面经


文章

微信
公众号

扫码关注公众号