Example #1
0
    def reduction_table(self, labels=None, vertical=False):
        """Table with steps of model reduction

        Parameters
        ----------
        labels : dict {str: str}
            Substitute new labels for predictors.
        vertical : bool
            Orient table vertically.
        """
        if not self._reduction_results:
            self.execute()
        if labels is None:
            labels = {}
        n_steps = len(self._reduction_results)
        # find terms
        terms = []
        for ress in self._reduction_results:
            terms.extend(term for term in ress.keys() if term not in terms)
        n_terms = len(terms)
        # cell content
        cells = {}
        for x in terms:
            for i, ress in enumerate(self._reduction_results):
                if x in ress:
                    res = ress[x]
                    pmin = res.p.min()
                    t_cell = fmtxt.stat(res.t.max(), stars=pmin)
                    p_cell = fmtxt.p(pmin)
                else:
                    t_cell = p_cell = ''
                cells[i, x] = t_cell, p_cell

        if vertical:
            t = fmtxt.Table('ll' + 'l' * n_terms)
            t.cells('Step', '')
            for x in terms:
                t.cell(labels.get(x, x))
            t.midrule()
            for i in range(n_steps):
                t_row = t.add_row()
                p_row = t.add_row()
                t_row.cells(i + 1, fmtxt.symbol('t', 'max'))
                p_row.cells('', fmtxt.symbol('p'))
                for x in terms:
                    t_cell, p_cell = cells[i, x]
                    t_row.cell(t_cell)
                    p_row.cell(p_cell)
        else:
            t = fmtxt.Table('l' + 'rr' * n_steps)
            t.cell()
            for _ in range(n_steps):
                t.cell(fmtxt.symbol('t', 'max'))
                t.cell(fmtxt.symbol('p'))
            t.midrule()
            for x in terms:
                t.cell(labels.get(x, x))
                for i in range(n_steps):
                    t.cells(*cells[i, x])
        return t
Example #2
0
    def table(self):
        table = fmtxt.Table('lrrrr')
        table.title(self.title)
        table.caption("Results based on %i samples" % self._n_samples)
        table.cell('Comparison')
        table.cell(fmtxt.symbol('t', df=self._df))
        table.cell(fmtxt.symbol('p', df='param'))
        table.cell(fmtxt.symbol('p', df='corr'))
        table.cell(fmtxt.symbol('p', df='boot'))
        table.midrule()

        p_corr = mcp_adjust(self._p_parametric)
        stars_parametric = star(self._p_parametric)
        stars_boot = star(self._p_boot, corr=None)

        for name, t, p1, pc, s1, p2, s2 in zip(self._comp_names, self.t,
                                           self._p_parametric, p_corr,
                                           stars_parametric,
                                           self._p_boot, stars_boot):
            table.cell(name)
            table.cell(t, fmt='%.2f')
            table.cell(fmtxt.p(p1))
            table.cell(fmtxt.p(pc, stars=s1))
            table.cell(fmtxt.p(p2, stars=s2))
        return table
Example #3
0
 def clusters(self, p=0.05):
     """Table with significant clusters"""
     if self.test_type is LMGroup:
         raise NotImplementedError
     else:
         table = fmtxt.Table('lrrll')
         table.cells('Effect',
                     't-start',
                     't-stop',
                     fmtxt.symbol('p'),
                     'sig',
                     just='l')
         table.midrule()
         for key, res in self.items():
             table.cell(key)
             table.endline()
             clusters = res.find_clusters(p)
             clusters.sort('tstart')
             if self.test_type != anova:
                 clusters[:, 'effect'] = ''
             for effect, tstart, tstop, p_, sig in clusters.zip(
                     'effect', 'tstart', 'tstop', 'p', 'sig'):
                 table.cells(f'  {effect}', ms(tstart), ms(tstop),
                             fmtxt.p(p_), sig)
     return table
Example #4
0
 def table(self, title=None, caption=None):
     """Table with effects and smallest p-value"""
     if self.test_type is LMGroup:
         cols = sorted(
             {col
              for res in self.values() for col in res.column_names})
         table = fmtxt.Table('l' * (1 + len(cols)),
                             title=title,
                             caption=caption)
         table.cell('')
         table.cells(*cols)
         table.midrule()
         for key, lmg in self.items():
             table.cell(key)
             for res in (lmg.tests[c] for c in cols):
                 pmin = res.p.min()
                 table.cell(fmtxt.FMText([fmtxt.p(pmin), star(pmin)]))
     elif self.test_type is anova:
         table = fmtxt.Table('lllll', title=title, caption=caption)
         table.cells('Test', 'Effect',
                     fmtxt.symbol(self.test_type._statistic, 'max'),
                     fmtxt.symbol('p'), 'sig')
         table.midrule()
         for key, res in self.items():
             for i, effect in enumerate(res.effects):
                 table.cells(key, effect)
                 pmin = res.p[i].min()
                 table.cell(fmtxt.stat(res._max_statistic(i)))
                 table.cell(fmtxt.p(pmin))
                 table.cell(star(pmin))
                 key = ''
     else:
         table = fmtxt.Table('llll', title=title, caption=caption)
         table.cells('Effect',
                     fmtxt.symbol(self.test_type._statistic, 'max'),
                     fmtxt.symbol('p'), 'sig')
         table.midrule()
         for key, res in self.items():
             table.cell(key)
             pmin = res.p.min()
             table.cell(fmtxt.stat(res._max_statistic()))
             table.cell(fmtxt.p(pmin))
             table.cell(star(pmin))
     return table
Example #5
0
 def clusters(self, p=0.05):
     """Table with significant clusters"""
     if self.test_type is TestType.TWO_STAGE:
         raise NotImplementedError
     else:
         table = fmtxt.Table('lrrrrll')
         table.cells('Effect', 't-start', 't-stop', fmtxt.symbol(self._statistic, 'max'), fmtxt.symbol('t', 'peak'), fmtxt.symbol('p'), 'sig', just='l')
         table.midrule()
         for key, res in self.items():
             table.cell(key)
             table.endline()
             clusters = res.find_clusters(p, maps=True)
             clusters.sort('tstart')
             if self.test_type is not TestType.MULTI_EFFECT:
                 clusters[:, 'effect'] = ''
             for effect, tstart, tstop, p_, sig, cmap in clusters.zip('effect', 'tstart', 'tstop', 'p', 'sig', 'cluster'):
                 max_stat, max_time = res._max_statistic(mask=cmap != 0, return_time=True)
                 table.cells(f'  {effect}', ms(tstart), ms(tstop), fmtxt.stat(max_stat), ms(max_time), fmtxt.p(p_), sig)
     return table
Example #6
0
def test(Y, X=None, against=0, match=None, sub=None,
         par=True, corr='Hochberg',
         title='{desc}'):
    """
    One-sample tests.

    kwargs
    ------
    X: perform tests separately for all categories in X.
    Against: can be
             - value
             - string (category in X)

    """
    ct = celltable(Y, X, match, sub)

    if par:
        title_desc = "t-tests against %s" % against
        statistic_name = 't'
    else:
        raise NotImplementedError

    names = []; ts = []; dfs = []; ps = []

    if isinstance(against, str):
        k = len(ct.indexes) - 1
        assert against in ct.cells
        for id in ct.indexes:
            label = ct.cells[id]
            if against == label:
                baseline_id = id
                baseline = ct.data[id]

        for id in ct.indexes:
            if id == baseline_id:
                continue
            names.append(ct.cells[id])
            if (ct.within is not False) and ct.within[id, baseline_id]:
                t, p = scipy.stats.ttest_rel(baseline, ct.data[id])
                df = len(baseline) - 1
            else:
                data = ct.data[id]
                t, p = scipy.stats.ttest_ind(baseline, data)
                df = len(baseline) + len(data) - 2
            ts.append(t)
            dfs.append(df)
            ps.append(p)

    elif np.isscalar(against):
        k = len(ct.cells)

        for id in ct.indexes:
            label = ct.cells[id]
            data = ct.data[id]
            t, p = scipy.stats.ttest_1samp(data, against)
            df = len(data) - 1
            names.append(label); ts.append(t); dfs.append(df); ps.append(p)

    if corr:
        ps_adjusted = mcp_adjust(ps, corr)
    else:
        ps_adjusted = np.zeros(len(ps))
    stars = star(ps, out=str)  # , levels=levels, trend=trend, corr=corr
    if len(np.unique(dfs)) == 1:
        df_in_header = True
    else:
        df_in_header = False

    table = fmtxt.Table('l' + 'r' * (3 - df_in_header + bool(corr)))
    table.title(title.format(desc=title_desc))
    if corr:
        table.caption(_get_correction_caption(corr, k))

    # header
    table.cell("Effect")
    if df_in_header:
        table.cell([statistic_name,
                    fmtxt.texstr(dfs[0], property='_'),
                    ], mat=True)
    else:
        table.cell(statistic_name, mat=True)
        table.cell('df', mat=True)
    table.cell('p', mat=True)
    if corr:
        table.cell(fmtxt.symbol('p', df=corr))
    table.midrule()

    # body
    for name, t, mark, df, p, p_adj in zip(names, ts, stars, dfs, ps, ps_adjusted):
        table.cell(name)
        tex_stars = fmtxt.Stars(mark, of=3)
        tex_t = fmtxt.texstr(t, fmt='%.2f')
        table.cell([tex_t, tex_stars])
        if not df_in_header:
            table.cell(df)

        table.cell(fmtxt.p(p))
        if corr:
            table.cell(fmtxt.p(p_adj))
    return table
Example #7
0
def test_symbol():
    "Test fmtxt.symbol()"
    s = fmtxt.symbol('t', 21)
    eq_(str(s), 't(21)')
    eq_(html(s), 't<sub>21</sub>')
    eq_(tex(s), '$t_{21}$')
Example #8
0
def test_symbol():
    "Test fmtxt.symbol()"
    s = fmtxt.symbol('t', 21)
    assert_equal(str(s), 't(21)')
    assert_equal(html(s), 't<sub>21</sub>')
Example #9
0
def test_symbol():
    "Test fmtxt.symbol()"
    s = fmtxt.symbol('t', 21)
    assert str(s) == 't(21)'
    assert html(s) == 't<sub>21</sub>'
    assert tex(s) == '$t_{21}$'
Example #10
0
def test_symbol():
    "Test fmtxt.symbol()"
    s = fmtxt.symbol('t', 21)
    assert_equal(str(s), 't(21)')
    assert_equal(html(s), 't<sub>21</sub>')
Example #11
0
def test_symbol():
    "Test fmtxt.symbol()"
    s = fmtxt.symbol('t', 21)
    eq_(str(s), 't(21)')
    eq_(html(s), 't<sub>21</sub>')
    eq_(tex(s), '$t_{21}$')