Esempio n. 1
0
def test_rect_nofill():
    p = (ggplot(df)
         + aes(xmin='xmin', xmax='xmax', ymin='ymin', ymax='ymax')
         + geom_rect(color='red', fill=None, size=2)
         + geom_rect(aes(ymin='ymin+2', ymax='ymax+2'),
                     color='blue', fill='None', size=2)
         + geom_rect(aes(ymin='ymin+4', ymax='ymax+4'),
                     color='green', fill='', size=2)
         + geom_rect(aes(ymin='ymin+6', ymax='ymax+6'),
                     color='yellow', fill='gray', size=2))

    assert p == 'rect-nofill'
Esempio n. 2
0
def test_rect_nofill():
    p = (ggplot(df)
         + aes(xmin='xmin', xmax='xmax', ymin='ymin', ymax='ymax')
         + geom_rect(color='red', fill=None, size=2)
         + geom_rect(aes(ymin='ymin+2', ymax='ymax+2'),
                     color='blue', fill='None', size=2)
         + geom_rect(aes(ymin='ymin+4', ymax='ymax+4'),
                     color='green', fill='', size=2)
         + geom_rect(aes(ymin='ymin+6', ymax='ymax+6'),
                     color='yellow', fill='gray', size=2))

    assert p == 'rect-nofill'
Esempio n. 3
0
def test_dodge_empty_data():
    empty_df = pd.DataFrame({'x': [], 'y': []})
    p = (ggplot(df1, aes('x', 'y')) + geom_point() +
         geom_rect(empty_df,
                   aes(xmin='x', xmax='x+1', ymin='y', ymax='y+1'),
                   position='dodge'))
    p.draw_test()
Esempio n. 4
0
def test_rect_aesthetics():
    p = (
        ggplot(df, aes(xmin='xmin', xmax='xmax', ymin='ymin', ymax='ymax')) +
        geom_rect() + geom_rect(aes(ymin='ymin+2', ymax='ymax+2', alpha='z'),
                                show_legend=False) +
        geom_rect(aes(ymin='ymin+4', ymax='ymax+4', fill='factor(z)')) +
        geom_rect(aes(ymin='ymin+6', ymax='ymax+6', color='factor(z+1)'),
                  size=2) +
        geom_rect(aes(ymin='ymin+8', ymax='ymax+8', linetype='factor(z+2)'),
                  color='yellow',
                  size=2) + _theme +
        # for comparison with geom_tile which
        # has labels by default
        labs(x='x', y='y'))

    assert p == 'rect-aesthetics'
Esempio n. 5
0
def plot_calibrations():
    params = data.sample_calibrations()
    return (
        pn.ggplot(
            params,
            pn.aes(xmin='boardsize-.25',
                   xmax='boardsize+.25',
                   group='boardsize',
                   fill='factor(boardsize)')) +
        pn.geom_hline(yintercept=.5, alpha=.2) + pn.geom_rect(
            pn.aes(ymin='lower', ymax='upper'), show_legend=False, color='k') +
        pn.geom_rect(pn.aes(ymin='mid', ymax='mid'),
                     show_legend=False,
                     color='k',
                     size=2) + pn.scale_y_continuous(labels=percent_format()) +
        pn.scale_fill_hue(l=.4) + pn.coord_cartesian(ylim=(.4, .6)) +
        pn.labs(y='Win rate v. perfect play', x='Board size') + plot.IEEE())
Esempio n. 6
0
def ranges_graphics(target, signatures_long, ranges, hydro_state, year,
                    algorithm):
    if ranges is None:
        return

    alg = ''
    if algorithm == "boruta":
        alg = "Boruta"
    elif algorithm == "lasso":
        alg = "LASSO"
    elif algorithm == "kbestcorr":
        alg = "SelectKBest (correlation)"
    elif algorithm == "kbestmi":
        alg = "SelectKBest (mutual information)"
    elif algorithm == "ga":
        alg = "Genetic Algorithm"

    signatures_long["wavelength"] = pandas.to_numeric(
        signatures_long["wavelength"])
    # signatures_long["value"] = pandas.to_numeric(signatures_long["value"])

    y_max = signatures_long["value"].max()

    graph_signatures = ggplot(signatures_long) \
        + theme(legend_position = "none") \
        + aes(x = "wavelength", y = "value", color = "variable") \
        + labs(
            x = "Wavelength (nm)",
            y = "Reflectance (%)",
            title = f"Ranges in signature: {target} {hydro_state} {alg}, {year}.",
            subtitle = f"{alg}, {hydro_state} set."
            )

    if ranges is not None:
        for i in range(len(ranges)):
            i_range = []
            for j in range(len(ranges[i])):
                # graph_signatures = graph_signatures + geom_vline(xintercept = ranges[i][j], color="black", alpha = 0.2)
                i_range.append(ranges[i][j])
            graph_signatures = graph_signatures + geom_rect(aes(
                xmin=i_range[0], xmax=i_range[1], ymin=0.0, ymax=y_max),
                                                            fill="steelblue",
                                                            alpha=0.1,
                                                            color=None)
        graph_signatures = graph_signatures + geom_line()

    else:
        graph_signatures = graph_signatures + geom_line()

    print(graph_signatures)
    graph_signatures.save(filename=os.path.join(
        PLOT_DIR, f"{hydro_state}-{year}-{algorithm}-ranges-{target}"))

    return
Esempio n. 7
0
def test_rect_aesthetics():
    p = (ggplot(df, aes(xmin='xmin', xmax='xmax',
                        ymin='ymin', ymax='ymax')) +
         geom_rect() +
         geom_rect(aes(ymin='ymin+2', ymax='ymax+2',
                       alpha='z'),
                   show_legend=False) +
         geom_rect(aes(ymin='ymin+4', ymax='ymax+4',
                       fill='factor(z)')) +
         geom_rect(aes(ymin='ymin+6', ymax='ymax+6',
                       color='factor(z+1)'), size=2) +
         geom_rect(aes(ymin='ymin+8', ymax='ymax+8',
                       linetype='factor(z+2)'),
                   color='yellow', size=2) +
         _theme +
         # for comparison with geom_tile which
         # has labels by default
         labs(x='x', y='y'))

    assert p == 'rect-aesthetics'
Esempio n. 8
0
def test_coord_trans_groups():
    df = pd.DataFrame({
        'xmin': [0, 2, 4],
        'xmax': [1, 3, 5],
        'ymin': [0, 2, 4],
        'ymax': [1, 3, 5],
        'c': list('abc')
    })

    p = (ggplot(df)
         + geom_rect(
             aes(xmin='xmin', xmax='xmax', ymin='ymin', ymax='ymax', fill='c'))
         + coord_trans()
         )
    assert p == 'coord-trans-groups'
Esempio n. 9
0
def test_infinite_rects():
    df = pd.DataFrame({'x': range(10), 'y': range(10)})
    rdf = pd.DataFrame({
        'xmin': [3],
        'xmax': 7,
        'ymin': -np.inf,
        'ymax': np.inf,
    })

    p = (ggplot(df, aes('x', 'y')) + geom_point() + geom_rect(
        data=rdf,
        mapping=aes(xmin='xmin', xmax='xmax', ymin='ymin', ymax='ymax'),
        alpha=0.2,
        inherit_aes=False))

    assert p == 'infinite-rects'
Esempio n. 10
0
    def plot_windows(self, query=""):
        """Plot selection windows with 'plotnine'.

        Install plotnine separately.

        Args:
           query (str): a query used for subselection in "self.windows"
        Returns:
            ggplot: a plot with selection windows
        """
        from plotnine import ggplot, aes, geom_rect, theme_minimal, xlab, ylab, labs
        D = self.windows.reset_index().query(query) if query else self.windows[1:].reset_index()
        plot = (ggplot(aes(), data=D) + 
                geom_rect(aes(xmin='mz_left', xmax='mz_right',
                              ymin='IM_min',  ymax='IM_max',
                              fill='pd.Categorical(window_gr)'), 
                          alpha=.5, color='black')+
                theme_minimal() +
                xlab('mass/charge') +
                ylab('1/K0') +
                labs(fill='Window Group'))
        return plot
Esempio n. 11
0
def plot_cor(df):
    # drop missing correlations
    out = df[~df['corr'].isnull()]
    # add pair column
    out = out.assign(pair=out.col_1 + '&' + out.col_2)
    # add a sign column
    sign = ((out['corr'] > 0).astype('int')).to_list()
    sign = [['Negative', 'Positive'][i] for i in sign]
    out['sign'] = sign
    #out  = out.sort_values('pair', ascending = False).reset_index(drop = True)
    # add ind column
    out['ind'] = [out.shape[0] - i for i in range(out.shape[0])]
    # plot using bands
    ggplt = p9.ggplot(data = out, mapping = p9.aes(x = 'pair', y = 'corr')) \
        + p9.geom_hline(
            yintercept = 0,
            linetype = "dashed",
            color = "#c2c6cc"
            ) \
        + p9.geom_rect(
            alpha = 0.4,
            xmin = out.ind.values - 0.4,
            xmax = out.ind.values + 0.4,
            ymin = out.lower.values,
            ymax = out.upper.values,
            fill = [['b', '#abaeb3'][int(x > 0.05)] for x in out.p_value]
          ) \
        + p9.geom_segment(
            x = out.ind.values - 0.4,
            y = out['corr'].values,
            xend = out.ind.values + 0.4,
            yend = out['corr'].values
          ) \
        + p9.coord_flip() \
        + p9.ylim(np.min(out.lower.values), np.max(out.upper.values)) \
        + p9.labs(x = "", y = "Correlation")
    return ggplt
Esempio n. 12
0
abortion.loc[(abortion.wht==0) & (abortion.male==1), 'bm'] = 1

abortion['bf'] = 0
abortion.loc[(abortion.wht==0) & (abortion.male==0), 'bf'] = 1


abortion_filt = abortion[(abortion.bf==1) & (abortion.age.isin([15,25]))]

reg = (
    smf
    .wls("""lnr ~ C(repeal)*C(year) + C(younger)*C(repeal) + C(younger)*C(year) + 
C(yr)*C(year) + C(fip)*t + acc + ir + pi + alcohol + crack + poverty + income + ur""", 
        data=abortion_filt, weights=abortion_filt.totpop.values)
    .fit(
        cov_type='cluster', 
        cov_kwds={'groups': abortion_filt.fip.values}, 
        method='pinv')
)

abortion_plot = pd.DataFrame({'sd': reg.bse['C(yr)[T.1]:C(year)[T.1986.0]':'C(yr)[T.1]:C(year)[T.2000.0]'],
             'mean': reg.params['C(yr)[T.1]:C(year)[T.1986.0]':'C(yr)[T.1]:C(year)[T.2000.0]'],
             'year':np.arange(1986, 2001)})

abortion_plot['lb'] = abortion_plot['mean'] - abortion_plot['sd']*1.96
abortion_plot['ub'] = abortion_plot['mean'] + abortion_plot['sd']*1.96


p.ggplot(abortion_plot, p.aes(x = 'year', y = 'mean')) +     p.geom_rect(p.aes(xmin=1986, xmax=1991, ymin=-np.inf, ymax=np.inf), fill = "cyan", alpha = 0.01)+    p.geom_point()+    p.geom_text(p.aes(label = 'year'), ha='right')+    p.geom_hline(yintercept = 0) +    p.geom_errorbar(p.aes(ymin = 'lb', ymax = 'ub'), width = 0.2,
                position = p.position_dodge(0.05)) +\
    p.labs(title= "Estimated effect of abortion legalization on gonorrhea")
Esempio n. 13
0
    .wls(formula, data=abortion_bf15, weights=abortion_bf15.totpop.values)
    .fit(
        cov_type='cluster', 
        cov_kwds={'groups': abortion_bf15.fip.values}, 
        method='pinv')
)

reg.summary()


abortion_plot = pd.DataFrame(
    {
        'sd': reg.bse['C(repeal)[T.1.0]:C(year)[T.1986.0]':'C(repeal)[T.1.0]:C(year)[T.2000.0]'],
        'mean': reg.params['C(repeal)[T.1.0]:C(year)[T.1986.0]':'C(repeal)[T.1.0]:C(year)[T.2000.0]'],
        'year': np.arange(1986, 2001)
    })
abortion_plot['lb'] = abortion_plot['mean'] - abortion_plot['sd']*1.96
abortion_plot['ub'] = abortion_plot['mean'] + abortion_plot['sd']*1.96

(
    p.ggplot(abortion_plot, p.aes(x = 'year', y = 'mean')) + 
    p.geom_rect(p.aes(xmin=1985, xmax=1992, ymin=-np.inf, ymax=np.inf), fill="cyan", alpha = 0.01) +
    p.geom_point() +
    p.geom_text(p.aes(label = 'year'), ha='right') +
    p.geom_hline(yintercept = 0) +
    p.geom_errorbar(p.aes(ymin = 'lb', ymax = 'ub'), width = 0.2,
                    position = p.position_dodge(0.05)) +
    p.labs(title= "Estimated effect of abortion legalization on gonorrhea")
)