def populate_churn_plots(q): shap_plot = churn_predictor.get_shap_explanation( q.client.selected_customer_index) q.page["shap_plot"] = ui.image_card( box=config.boxes["shap_plot"], title="", type="png", image=get_image_from_matplotlib(shap_plot), ) top_negative_pd_plot = churn_predictor.get_top_negative_pd_explanation( q.client.selected_customer_index) q.page["top_negative_pd_plot"] = ui.image_card( box=config.boxes["top_negative_pd_plot"], title="Feature Most Contributing to Retention", type="png", image=get_image_from_matplotlib(top_negative_pd_plot), ) top_positive_pd_plot = churn_predictor.get_top_positive_pd_explanation( q.client.selected_customer_index) q.page["top_positive_pd_plot"] = ui.image_card( box=config.boxes["top_positive_pd_plot"], title="Feature Most Contributing to Churn", type="png", image=get_image_from_matplotlib(top_positive_pd_plot), )
def render_text_word_cloud_image(q: Q, image): q.page['all'] = ui.image_card( box=config.boxes['middle_panel'], title=f'Word Cloud of the {config.column_mapping[q.client.review]}', type='png', image=image, )
def render_all_text_word_cloud(q: Q): image = plot_word_cloud(merge_to_single_text(config.dataset[q.client.review])) q.page['all'] = ui.image_card( box=config.boxes['middle_panel'], title=f'Word Cloud of the {config.column_mapping[q.client.review]}', type='png', image=image, )
def init(q: Q): q.page['meta'] = ui.meta_card( box='', title='Explain Ratings', layouts=[ ui.layout(breakpoint='xs', zones=[ ui.zone('header'), ui.zone('body', direction=ui.ZoneDirection.ROW, size='calc(100vh - 70px)', zones=[ ui.zone('sidebar', size='350px'), ui.zone('content', direction=ui.ZoneDirection.ROW) ]) ]), ]) q.page['header'] = ui.header_card(box='header', title='Hotel Reviews', subtitle='Explains the hotel reviews', icon='ReviewSolid', icon_color='#00A8E0') q.client.review = config.review_column_list[0] form_filters = [] for column in config.filterable_columns: choices = [ui.choice(name='empty', label='All')] + [ ui.choice(name=str(column), label=str(column)) for column in config.dataset[column].drop_duplicates() ] form_filters.append( ui.dropdown(name=f'filter_{column}', label=config.column_mapping[column], trigger=True, value='empty', choices=choices)) sidebar_items = [ ui.dropdown(name='review', label='Review type', value=q.client.review, trigger=True, choices=[ ui.choice(name=column, label=config.column_mapping[column]) for column in config.dataset[config.review_column_list] ]), ui.separator('Filters') ] + form_filters q.page['sidebar'] = ui.form_card(box='sidebar', items=sidebar_items) q.page['original'] = ui.image_card(box='content', title='Original', type='png', image=plot_word_cloud( config.dataset[q.client.review], q))
def image_card_from_image(path_image: Path, box: str, title: str, image_type: str = 'png') -> ui.image_card: """ Putting an image into Q ui.image_card. """ with open(Path(path_image), 'rb') as file_image: base64_encoded_string = base64.b64encode( file_image.read()).decode('utf8') card = ui.image_card( box=box, title=title, type=image_type, image=base64_encoded_string, ) return card
def render_compare_word_cloud(q: Q): df = filter_data_frame(config.dataset, q.client.filters) if len(df): image = plot_word_cloud(merge_to_single_text(df[q.client.review])) q.page['compare'] = ui.image_card( box=config.boxes['right_panel'], title='Word Cloud based on the selected filters', type='png', image=image, ) else: q.page['compare'] = ui.form_card( box=config.boxes['right_panel'], items=[ ui.message_bar(type='warning', text='No reviews matching filter criteria!') ])
def render_customer_page(q: Q): init(q) selected_row = int(q.args.risk_table[0]) training_df = predictor.get_testing_data_as_pd_frame() predictions_df = predictor.predicted_df.as_data_frame() contributions_df = predictor.contributions_df.as_data_frame() del contributions_df['BiasTerm'] q.client.selected_customer_id = training_df.loc[selected_row]["ID"] print("selected id : ", q.client.selected_customer_id) score = predictions_df.loc[selected_row]["predict"] approve = bool(score < config.approval_threshold) drop_column_from_df(training_df, 'default.payment.next.month') add_column_to_df(training_df, predictions_df, 'Default Prediction Rate', 'predict') training_df = round_df_column(training_df, 'Default Prediction Rate', 4) render_customer_details_table(q, training_df, selected_row) shap_plot = predictor.get_shap_explanation(selected_row) q.page["shap_plot"] = ui.image_card( box='shap_plot', title="Effectiveness of each attribute on defaulting next payment", type="png", image=get_image_from_matplotlib(shap_plot, dpi=85), ) render_customer_summary(q, training_df, contributions_df, selected_row, approve) q.page["buttons"] = ui.form_card(box='button_group', items=[ ui.buttons([ ui.button(name='reject_btn', label='Reject', primary=not approve), ui.button(name='approve_btn', label='Approve', primary=approve), ]) ])
def render_diff_word_cloud(q: Q): df = config.dataset for key, value in q.client.filters.items(): df = df[df[key] == value] if len(df): q.page['diff'] = ui.image_card(box='content', title='Diff', type='png', image=plot_word_cloud( df[q.client.review], q)) else: # TODO: Move into sidebar when https://github.com/h2oai/wave/pull/507 merged. q.page['diff'] = ui.form_card( box='content', items=[ ui.message_bar(type='warning', text='No reviews matching filter criteria!') ])
from h2o_wave import site, ui import io import base64 import numpy as np import matplotlib.pyplot as plt np.random.seed(19680801) n = 25 plt.figure(figsize=(3, 3)) plt.scatter( np.random.rand(n), np.random.rand(n), s=(30 * np.random.rand(n)) ** 2, c=np.random.rand(n), alpha=0.5, ) buf = io.BytesIO() plt.savefig(buf, format='png') buf.seek(0) image = base64.b64encode(buf.read()).decode('utf-8') page = site['/demo'] page['example'] = ui.image_card( box='1 1 3 5', title='An image', type='png', image=image, ) page.save()
plt.scatter( np.random.rand(n), np.random.rand(n), s=(30 * np.random.rand(n)) ** 2, c=np.random.rand(n), alpha=0.5, ) buf = io.BytesIO() plt.savefig(buf, format='png') buf.seek(0) image = base64.b64encode(buf.read()).decode('utf-8') page = site['/demo'] page['example1'] = ui.image_card( box='1 1 3 5', title='An image', type='png', image=image, ) # Another way to achieve the same result is to use a data URL for the path: # The example below constructs the data URL from the base64-encoded # used in the previous example. page['example2'] = ui.image_card( box='1 6 3 5', title='An image', path=f"data:image/png;base64,{image}", ) page.save()