Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def create_readme_image():
kwargs = dict(width=6, height=4)
df = pd.DataFrame({'x': np.linspace(0, 2*np.pi, 500)})
p = (df
>> define(y='np.sin(x)')
>> define_where('y>=0', sign=('"positive"', '"negative"'))
>> (ggplot(aes('x', 'y'))
+ geom_line(aes(color='sign'), size=1.5))
)
p.save('readme-image.png', **kwargs)
ticks = [x / bin_nb_total * 100 for x in ticks]
labels = [str(int(round(x, 0))) + "%" for x in np.linspace(0, 100, 6)]
p += scale_x_continuous(expand=[0, 0], breaks=ticks, labels=labels)
else:
p += scale_x_continuous(expand=[0, 0])
# -------------------------------------------------------------------------
#
# Adding geom_line
#
# -------------------------------------------------------------------------
p += geom_line(size=line_width)
# --------------------------------------------------------------------------
#
# Add facets
#
# --------------------------------------------------------------------------
if facet_var is not None:
# update facet_col
facet_col = min(facet_col, len(dm[facet_var].unique()))
p += facet_wrap("~ " + facet_var, ncol=facet_col)
else:
facet_col = 1
message("facet_col " + str(facet_col))
def plot_regret(df_in: pd.DataFrame,
sweep_vars: Sequence[Text] = None,
num_episodes: int = NUM_EPISODES) -> gg.ggplot:
"""Plot average regret of deep_sea through time by size."""
df = df_in.copy()
df = df[df['size'].isin([10, 20, 30, 40, 50])]
df['avg_bad'] = df.total_bad_episodes / df.episode
df['size'] = df['size'].astype('category')
p = (gg.ggplot(df[df.episode <= num_episodes])
+ gg.aes('episode', 'avg_bad', group='size', colour='size')
+ gg.geom_line(size=2, alpha=0.75)
+ gg.geom_hline(
gg.aes(yintercept=0.99), linetype='dashed', alpha=0.4, size=1.75)
+ gg.geom_hline(gg.aes(yintercept=0.0), alpha=0) # axis hack
+ gg.ylab('average bad episodes')
+ gg.scale_colour_manual(values=plotting.FIVE_COLOURS)
)
return plotting.facet_sweep_plot(p, sweep_vars)
df_ranks_lsq = pd.merge(df_teams[['team_id', 'firstName']], df_ranks, on='team_id')
# Space out labels on x-axis according to final rankings
df_ranks_lsq['label_x_pos'] = df_ranks_lsq.get(99).rank() * 100 / df_ranks_lsq.get(99).size
# Convert to long format for plotting ease
df_ranks_lsq_long = (
df_ranks_lsq
.rename({'ranks': '0'}, axis='columns')
.melt(id_vars=['team_id', 'firstName', 'label_x_pos'])
)
# Convert iteration variable to int
df_ranks_lsq_long.variable = df_ranks_lsq_long.variable.astype(int)
# Make the plot
p = (
ggplot(aes(x='variable', y='value', color='factor(team_id)', group='team_id'),
data=df_ranks_lsq_long) +
geom_line() +
geom_label(aes(label='firstName', x='label_x_pos', y='value', color='factor(team_id)'),
data=df_ranks_lsq_long[df_ranks_lsq_long.variable == 99],
size=10) +
labs(x='Iteration', y='LSQ rank') +
theme_bw() +
guides(color=False)
)
# Save plot
if show:
p.draw()
# make dir if it doesn't exist already
out_dir = Path(f'output/{year}/week{week}')
out_dir.mkdir(parents=True, exist_ok=True)
out_name = out_dir / 'lsq_iter_rankings.png'
# plotnine is throwing too many warnings
warnings.filterwarnings('ignore')
import plotnine as gg
df2 = self.jobs_df.loc[:, set(['qid', 'id', color_by, facet_by, 'submitted_offset', 'started_offset', 'ended_offset', 'processed_offset'])].melt(id_vars=set(['qid', 'id', color_by, facet_by]))
df2 = df2[df2['value'].notnull()]
df_submit_start = df2[(df2['variable'] == 'submitted_offset') | (df2['variable'] == 'started_offset')]
df_start_end = df2[(df2['variable'] == 'started_offset') | (df2['variable'] == 'ended_offset')]
df_end_processed = df2[(df2['variable'] == 'ended_offset') | (df2['variable'] == 'processed_offset')]
labs = { 'x': 'duration', 'y': 'job id' }
if title is not None:
labs['title'] = title
gg_obj = gg.ggplot(gg.aes(x='value', y='id', group='factor(id)')) + \
gg.geom_line(df_submit_start, color='gray', size=bar_width, alpha=0.2) + \
gg.geom_line(df_start_end,
gg.aes(color='factor({})'.format(color_by)),
size=bar_width, show_legend=bool(show_legend)) + \
gg.geom_line(df_end_processed, color='gray', size=bar_width, alpha=0.2) + \
gg.labs(**labs) + \
gg.labs(color=color_by) + \
Plot._plot_theme(grid_axis='x', theme=theme) + \
gg.facet_grid(facets='{}~'.format(facet_by), labeller='label_both', scales=facet_scale, as_table=True)
if color_pal is None or not isinstance(color_pal, list):
gg_obj += gg.scale_color_hue(h=.65)
else:
n_colors = self.jobs_df[color_by].unique().size
if len(color_pal) < n_colors:
log.warning('Insufficient number of colors; need at least ' + str(n_colors))
Returns:
p: ggplot plot
"""
df = load_data(experiment_name, data_path)
plt_df = (df.groupby(['t', 'agent'])
.agg({'instant_regret': np.mean})
.reset_index())
plt_df['agent_new_name'] = plt_df.agent.apply(rename_agent)
custom_labels = ['Laplace TS','Langevin TS','TS','bootstrap TS']
custom_colors = ["#E41A1C","#377EB8","#4DAF4A","#984EA3"]
p = (gg.ggplot(plt_df)
+ gg.aes('t', 'instant_regret', colour='agent_new_name')
+ gg.geom_line(size=1.25, alpha=0.75)
+ gg.xlab('time period (t)')
+ gg.ylab('per-period regret')
+ gg.scale_color_manual(name='agent', labels = custom_labels,values=custom_colors))
return p
yii[np.where(yii > 0)], bins, range=(0, 1))
# yii_bins is a bins + 1 length list of bin endpoints, so we need to calculate bin midpoints so that
# the we have a one-to-one list of x (yii) and y (frequency) values.
# To do this we add half the bin width to each lower bin edge x-value
midpoints = yii_bins[:-1] + 0.5 * np.diff(yii_bins)
# Calculate which non-zero bin has the maximum Fv/Fm value
max_bin = midpoints[np.argmax(yii_hist)]
# Create Histogram Plot, if you change the bin number you might need to change binx so that it prints
# an appropriate number of labels
# Create a dataframe
dataset = pd.DataFrame({'Plant Pixels': yii_hist, parameter: midpoints})
# Make the histogram figure using plotnine
yii_hist_fig = (ggplot(data=dataset, mapping=aes(x=parameter, y='Plant Pixels'))
+ geom_line(color='green', show_legend=True)
+ geom_label(label='Peak Bin Value: ' + str(max_bin),
x=.15, y=205, size=8, color='green'))
analysis_images.append(yii_hist_fig)
if params.debug == 'print':
print_image(fmin_mask, os.path.join(params.debug_outdir,
str(params.device) + '_fmin_mask.png'))
print_image(fmax_mask, os.path.join(params.debug_outdir,
str(params.device) + '_fmax_mask.png'))
print_image(yii, os.path.join(params.debug_outdir,
str(params.device) + '_yii.png'))
yii_hist_fig.save(os.path.join(params.debug_outdir,
str(params.device) + '_yii_hist.png'))
elif params.debug == 'plot':
plot_image(fmin_mask, cmap='gray')
plot_image(fmax_mask, cmap='gray')
def plot_action_proportion(df_agent):
"""Plot the action proportion for the sub-dataframe for a single agent."""
n_action = np.max(df_agent.action) + 1
plt_data = []
for i in range(n_action):
probs = (df_agent.groupby('t')
.agg({'action': lambda x: np.mean(x == i)})
.rename(columns={'action': 'action_' + str(i)}))
plt_data.append(probs)
plt_df = pd.concat(plt_data, axis=1).reset_index()
p = (gg.ggplot(pd.melt(plt_df, id_vars='t'))
+ gg.aes('t', 'value', colour='variable', group='variable')
+ gg.geom_line(size=1.25, alpha=0.75)
+ gg.xlab('time period (t)')
+ gg.ylab('Action probability')
+ gg.ylim(0, 1)
+ gg.scale_colour_brewer(name='Variable', type='qual', palette='Set1'))
return p
p = ggplot(human_df) + geom_point(shape='.')
else:
df = self.char_plot_df
if 1 not in self.rounds:
df = df[df['Dataset'] != 'Round 1 - IR Adversarial']
if 2 not in self.rounds:
df = df[df['Dataset'] != 'Round 2 - IR Adversarial']
df = df[df['Dataset'] != 'Round 2 - RNN Adversarial']
p = ggplot(df)
if self.save_df is not None:
eprint(f'Saving df to: {self.save_df}')
df.to_json(self.save_df)
if os.path.exists('data/external/all_human_gameplay.json') and not self.no_humans:
eprint('Loading human data')
p = p + geom_line(data=human_df)
if columns:
facet_conf = facet_wrap('Guessing_Model', ncol=1)
else:
facet_conf = facet_wrap('Guessing_Model', nrow=1)
if not no_models:
if self.mvg_avg_char:
chart = stat_smooth(method='mavg', se=False, method_args={'window': 400})
else:
chart = stat_summary_bin(fun_data=mean_no_se, bins=20, shape='.', linetype='None', size=0.5)
else:
chart = None
p = (
p + facet_conf