예제 #1
0
def calc_means(h5, gk, grp, prop_obj, prop_dd, A, C):
    grp_tb = fetch_grp_tb(h5, grp, prop_obj.name)
    _l = []
    for tb in grp_tb:
        _ = tb.read(field=prop_obj.ifield).mean()
        _l.append(_)

    if 'denorminators' in prop_dd:
        denorm = float(U.get_param(prop_dd['denorminators'], gk))
        logger.info('denormator: {0}'.format(denorm))
        return np.array([np.mean(_l) / denorm, U.sem(_l) / denorm])

    return np.array([np.mean(_l), U.sem(_l)])
예제 #2
0
def calc_alx(h5, gk, grp, prop_obj, prop_dd, A, C):
    grp_tb = fetch_grp_tb(h5, grp, prop_obj.name)
    # x assumed to be FIELD_0_NAME
    tb0 = grp_tb[0]
    xf = tb0._f_getAttr('FIELD_0_NAME') # xf: xfield, and it's assumed to be
                                        # the same in all tabls in the grp_tb
    min_len = min(tb.read(field=xf).shape[0] for tb in grp_tb)
    _l = []
    ref_col = grp_tb[0].read(field=xf)[:min_len]

    for tb in grp_tb:
        col1 = tb.read(field=xf)[:min_len]
        assert (col1 == ref_col).all() == True
        col2 = tb.read(field=prop_obj.ifield)[:min_len]
        _l.append(col2)
    _a = np.array(_l)
    y = _a.mean(axis=0)
    ye = np.array([U.sem(_a[:,i]) for i in xrange(len(_a[0]))])
    # ye = stats.sem(_a, axis=0)

    if 'xdenorm' in prop_dd:
        ref_col = ref_col / float(prop_dd['xdenorm'])
    if 'denorminators' in prop_dd:
        denorm = float(U.get_param(prop_dd['denorminators'], gk))
        y, ye = y / denorm, ye / denorm

    _aa = np.array([ref_col, y, ye])
    prop_dd = U.get_prop_dd(C, prop_obj.name)

    # nb_blave: number of blocks for block averaging
    n = int(prop_dd.get('nb_blave', 100))
    res = block_average(_aa, n)
    return res
예제 #3
0
def calc_distr(h5, gk, grp, prop_obj, prop_dd, A, C):
    grp_tb = fetch_grp_tb(h5, grp, prop_obj.name)
    min_len = min(tb.read(field='time').shape[0] for tb in grp_tb)
    _l = []
    for tb in grp_tb:
        _l.append(tb.read(field=prop_obj.ifield)[:min_len])
    _la = np.array(_l)

    # grped_distr_ave is a variant of grped_distr
    special_cases = {'grped_distr_ave': 'grped_distr'}
    pt_dd = U.get_pt_dd(
        C, A.property, 
        special_cases.get(A.plot_type, A.plot_type))

    if 'bins' in pt_dd:
        i, j, s = [float(_) for _ in pt_dd['bins']]
        bins = np.arange(i, j, s)
    else:
        # assume usually 36 bins would be enough
        bins = np.linspace(_la.min(), _la.max(), 36)

    ps = []                             # probability distributions for each tb
    for _ in _la:
        fnormed = pt_dd.get('normed', False)
        p, __ = np.histogram(_, bins, normed=fnormed) 
        if not fnormed:
            p = p / float(sum(p)) # unnormed probability
        ps.append(p)
    ps = np.array(ps)

    bn = (bins[:-1] + bins[1:]) / 2.             # to gain the same length as psm, pse
    psm = ps.mean(axis=0)
    pse = [U.sem(ps[:,i]) for i in xrange(len(ps[0]))]
    pse = np.array(pse)
    return np.array([bn, psm, pse])
예제 #4
0
def grped_pmf(data, A, C, **kw):
    pt_dd = U.get_pt_dd(C, A.property, A.plot_type)
    logger.info('pt_dd: {0}'.format(pt_dd))

    dsets = grp_datasets(data, pt_dd)
    ncol, nrow = U.gen_rc(len(dsets.keys()), pt_dd)
    logger.info('col: {0}, row; {1}'.format(ncol, nrow))

    fig = plt.figure(figsize=pt_dd.get('figsize', [16, 9]))
    if 'subplots_adjust' in pt_dd:
        fig.subplots_adjust(**pt_dd['subplots_adjust'])

    for dsetc, dsetk in enumerate(dsets.keys()):  # dsetc: dset counter
        ax = fig.add_subplot(nrow, ncol, dsetc + 1)
        # da.shape: (x, 3, y), where x: # of replicas; 3: the shape of [bn, pmf, pmf_e],
        # y: # of bn, pmf, or pmf_e of each subgroup
        for k, gk in enumerate(dsets[dsetk].keys()):
            da = data[gk]
            pre_pmfm = da.mean(axis=0)  # means over x, DIM: (3, y)
            pre_pmfe = U.sem3(da)  # sems  over x, DIM: (3, y)

            if 'pmf_cutoff' in pt_dd:
                cf = float(pt_dd['pmf_cutoff'])
                bs, es = filter_pmf_data(pre_pmfm, cf)  # get slicing indices
            else:
                bs, es = filter_pmf_data(pre_pmfm)

            bn, pmfm, _ = sliceit(pre_pmfm, bs, es)  # bn: bin; pmfm: pmf mean
            bne, pmfe, _ = sliceit(pre_pmfe, bs,
                                   es)  # bne: bin err; pmfe: pmf err

            # pmf sem, equivalent to stats.sem(da, axis=0)
            pmfe = sliceit(pre_pmfe, bs,
                           es)[1]  # tricky: 1 corresponds err of pmf mean

            # now, prepare the errobars for the fit
            _pfits, _ks, _l0s = [], [], []
            for subda in da:
                sliced = sliceit(subda, bs, es)
                bn, pm, pe = sliced
                a, b, c = np.polyfit(bn, pm, deg=2)
                _pfv = parabola(bn, a, b, c)  # pfv: pmf fit values
                _pfits.append(_pfv)
                _ks.append(convert_k(a))
                _l0s.append(-b / (2 * a))
            logger.info('modulus values for {0}'.format(
                pt_dd['grp_REs'][dsetc]))
            for _ in _ks:
                logger.info(' {0}'.format(_))

            _pfit = np.mean(_pfits, axis=0)
            _k = np.mean(_ks)  # prefix it with _ to avoid confusion
            _ke = U.sem(_ks)
            _l0 = np.mean(_l0s)
            _l0e = U.sem(_l0s)
            _r2 = U.calc_r2(pmfm, _pfit)
            _ky, _kye = ky(_k, _l0, _ke, _l0e)

            ax.annotate(
                '\n'.join([
                    'k   = {0:.1f} $\pm$ {1:.1f} pN/nm'.format(_k, _ke),
                    'd$_0$  = {0:.1f} $\pm$ {1:.1f} nm'.format(_l0, _l0e),
                    'r$^2$ = {0:.2f}'.format(_r2)
                ]), **pt_dd['annotate'])

            # ax.text(s = '\n'.join(['k   = {0:.1f} $\pm$ {1:.1f} pN/nm'.format(_k, _ke),
            #                        'd$_0$  = {0:.1f} $\pm$ {1:.1f} nm'.format(_l0, _l0e),
            #                        'r$^2$ = {0:.2f}'.format(_r2)]),
            #         # 'ky  = {0:.1f} +/- {1:.1f} MPa'.format(_ky, _kye)]),
            #         **pt_dd['text'])

            params = get_params(gk, pt_dd)
            line = ax.plot(bn, pmfm, **params)
            ax.fill_between(bn,
                            pmfm - pmfe,
                            pmfm + pmfe,
                            where=None,
                            facecolor=line[0].get_color(),
                            alpha=.3)

            ax.plot(bn, _pfit, '--', color=line[0].get_color())
        decorate_ax(ax, gk, pt_dd, ncol, nrow, dsetc)

    plt.savefig(U.gen_output_filename(A, C), **pt_dd.get('savefig', {}))
예제 #5
0
파일: grped_pmf.py 프로젝트: zyxue/xit
def grped_pmf(data, A, C, **kw):
    pt_dd = U.get_pt_dd(C, A.property, A.plot_type)
    logger.info('pt_dd: {0}'.format(pt_dd))

    dsets = grp_datasets(data,  pt_dd)
    ncol, nrow = U.gen_rc(len(dsets.keys()), pt_dd)
    logger.info('col: {0}, row; {1}'.format(ncol, nrow))

    fig = plt.figure(figsize=pt_dd.get('figsize', [16,9]))
    if 'subplots_adjust' in pt_dd:
        fig.subplots_adjust(**pt_dd['subplots_adjust'])

    for dsetc, dsetk in enumerate(dsets.keys()): # dsetc: dset counter
        ax = fig.add_subplot(nrow, ncol, dsetc+1)
        # da.shape: (x, 3, y), where x: # of replicas; 3: the shape of [bn, pmf, pmf_e],
        # y: # of bn, pmf, or pmf_e of each subgroup
        for k, gk in enumerate(dsets[dsetk].keys()):
            da = data[gk]
            pre_pmfm = da.mean(axis=0) # means over x, DIM: (3, y)
            pre_pmfe = U.sem3(da)      # sems  over x, DIM: (3, y)

            if 'pmf_cutoff' in pt_dd:
                cf = float(pt_dd['pmf_cutoff'])
                bs, es = filter_pmf_data(pre_pmfm, cf) # get slicing indices
            else:
                bs, es = filter_pmf_data(pre_pmfm)
                
            bn, pmfm, _ = sliceit(pre_pmfm, bs, es)  # bn: bin; pmfm: pmf mean
            bne, pmfe, _ = sliceit(pre_pmfe, bs, es) # bne: bin err; pmfe: pmf err

            # pmf sem, equivalent to stats.sem(da, axis=0)
            pmfe = sliceit(pre_pmfe, bs, es)[1] # tricky: 1 corresponds err of pmf mean

            # now, prepare the errobars for the fit
            _pfits, _ks, _l0s = [], [], []
            for subda in da:
                sliced = sliceit(subda, bs, es)
                bn, pm, pe = sliced
                a, b, c = np.polyfit(bn, pm, deg=2)
                _pfv = parabola(bn, a, b, c)                    # pfv: pmf fit values
                _pfits.append(_pfv)
                _ks.append(convert_k(a))
                _l0s.append(-b/(2*a))
            logger.info('modulus values for {0}'.format(pt_dd['grp_REs'][dsetc]))
            for _ in _ks:
                logger.info(' {0}'.format(_))

            _pfit = np.mean(_pfits, axis=0)
            _k    = np.mean(_ks)                   # prefix it with _ to avoid confusion
            _ke   = U.sem(_ks)
            _l0   = np.mean(_l0s)
            _l0e  = U.sem(_l0s)
            _r2   = U.calc_r2(pmfm, _pfit)
            _ky, _kye  = ky(_k, _l0, _ke, _l0e)

            ax.annotate('\n'.join(['k   = {0:.1f} $\pm$ {1:.1f} pN/nm'.format(_k, _ke),
                                   'd$_0$  = {0:.1f} $\pm$ {1:.1f} nm'.format(_l0, _l0e),
                                   'r$^2$ = {0:.2f}'.format(_r2)]),
                        **pt_dd['annotate'])

            # ax.text(s = '\n'.join(['k   = {0:.1f} $\pm$ {1:.1f} pN/nm'.format(_k, _ke),
            #                        'd$_0$  = {0:.1f} $\pm$ {1:.1f} nm'.format(_l0, _l0e),
            #                        'r$^2$ = {0:.2f}'.format(_r2)]),
            #         # 'ky  = {0:.1f} +/- {1:.1f} MPa'.format(_ky, _kye)]),
            #         **pt_dd['text'])

            params = get_params(gk, pt_dd)
            line = ax.plot(bn, pmfm, **params)
            ax.fill_between(bn, pmfm-pmfe, pmfm+pmfe, 
                            where=None, facecolor=line[0].get_color(), alpha=.3)

            ax.plot(bn, _pfit, '--', color=line[0].get_color())
        decorate_ax(ax, gk, pt_dd, ncol, nrow, dsetc)

    plt.savefig(U.gen_output_filename(A, C), **pt_dd.get('savefig', {}))
예제 #6
0
 def __init__(self, vals, norm=1.):
     self.vals = vals
     self.mean = np.mean(vals)
     self.sem = utils.sem(vals)
     self.nmean = np.mean(vals) / norm
     self.nsem = utils.sem(vals) / norm
예제 #7
0
파일: objs.py 프로젝트: zyxue/xit
 def __init__(self, vals, norm=1.):
     self.vals = vals
     self.mean = np.mean(vals)
     self.sem = utils.sem(vals)
     self.nmean = np.mean(vals) / norm
     self.nsem = utils.sem(vals) / norm