src/proteingnn/util.py
def dual_reg_corr_plot(dual_data: Dict[str, Dict[str, torch.Tensor]], full_range: bool = False,
                       margin_ratio: float = 1.1, **kwargs) -> plt.Figure:
    """Utility function to overlay seaborn jointplots of train and validation set.

    Args:
        dual_data: Dictionary of train and val set data in the format of
            {'train': {'pred': predictions, 'true': target values}, 'val': ...}
        full_range: If false, restrict plotted data points to true value range.
        margin_ratio: Extra margin from min-max range.
    """
    # noinspection PyArgumentList
    y_cat = torch.cat((dual_data['train']['y'], dual_data['val']['y']), axis=0)
    # noinspection PyArgumentList
    pred_cat = torch.cat((dual_data['train']['pred'], dual_data['val']['pred']), axis=0)
    df = pd.DataFrame(zip(pred_cat.numpy(), y_cat.numpy()), columns=['predicted value', 'true value'])

    # define plot value range
    if full_range:
        # noinspection PyArgumentList
        lim = (df.values.min(), df.values.max())
    else:
        lim = (df['true value'].values.min(), df['true value'].values.max())

    lim_mid = (lim[0] + lim[1]) / 2
    lim_len = margin_ratio * (lim[1] - lim[0])
    lim = (lim_mid - lim_len / 2, lim_mid + lim_len / 2)
    if 'xlim' not in kwargs:
        kwargs['xlim'] = lim
    if 'ylim' not in kwargs:
        kwargs['ylim'] = lim

    colors = {
        'train': matplotlib.colors.to_rgba('orange', alpha=0.5),
        'val': matplotlib.colors.to_rgba('royalblue', alpha=1)
    }
    df = df.clip(lower=lim[0], upper=lim[1])
    df['hue'] = ['train'] * len(dual_data['train']['y']) + ['val'] * len(dual_data['val']['y'])

    sns.set_theme()
    jg = sns.JointGrid(data=df, x='predicted value', y='true value', hue='hue', **kwargs)
    jg.plot_joint(sns.scatterplot, palette=colors)
    # jg.ax_joint.invert_yaxis()
    legend = jg.ax_joint.get_legend()
    legend.set_title('')
    legend._set_loc(4)  # not a good practice

    common_kwargs = {'stat': 'density', 'alpha': 0.5, 'binwidth': 0.25, 'binrange': kwargs['ylim']}
    sns.histplot(y=dual_data['train']['y'], ax=jg.ax_marg_y, color=colors['train'], **common_kwargs)
    sns.histplot(y=dual_data['val']['y'], ax=jg.ax_marg_y, color=colors['val'], **common_kwargs)

    common_kwargs['binrange'] = kwargs['xlim']
    sns.histplot(x=dual_data['train']['pred'], ax=jg.ax_marg_x, color=colors['train'], **common_kwargs)
    sns.histplot(x=dual_data['val']['pred'], ax=jg.ax_marg_x, color=colors['val'], **common_kwargs)

    jg.set_axis_labels(xlabel='predicted value', ylabel='true value')
    jg.fig.tight_layout()
    return jg.fig
Utility function to overlay seaborn jointplots of train and validation set.

    Args:
        dual_data: Dictionary of train and val set data in the format of
            {'train': {'pred': predictions, 'true': target values}, 'val': ...}
        full_range: If false, restrict plotted data points to true value range.
        margin_ratio: Extra margin from min-max range.
    
Utility function to overlay seaborn jointplots of train and validation set.
Utility function to overlay seaborn jointplots of train and validation set .
def dual_reg_corr_plot(dual_data: Dict[str, Dict[str, torch.Tensor]], full_range: bool = False,
                       margin_ratio: float = 1.1, **kwargs) -> plt.Figure:
    y_cat = torch.cat((dual_data['train']['y'], dual_data['val']['y']), axis=0)
    pred_cat = torch.cat((dual_data['train']['pred'], dual_data['val']['pred']), axis=0)
    df = pd.DataFrame(zip(pred_cat.numpy(), y_cat.numpy()), columns=['predicted value', 'true value'])
    if full_range:
        lim = (df.values.min(), df.values.max())
    else:
        lim = (df['true value'].values.min(), df['true value'].values.max())
    lim_mid = (lim[0] + lim[1]) / 2
    lim_len = margin_ratio * (lim[1] - lim[0])
    lim = (lim_mid - lim_len / 2, lim_mid + lim_len / 2)
    if 'xlim' not in kwargs:
        kwargs['xlim'] = lim
    if 'ylim' not in kwargs:
        kwargs['ylim'] = lim
    colors = {
        'train': matplotlib.colors.to_rgba('orange', alpha=0.5),
        'val': matplotlib.colors.to_rgba('royalblue', alpha=1)
    }
    df = df.clip(lower=lim[0], upper=lim[1])
    df['hue'] = ['train'] * len(dual_data['train']['y']) + ['val'] * len(dual_data['val']['y'])
    sns.set_theme()
    jg = sns.JointGrid(data=df, x='predicted value', y='true value', hue='hue', **kwargs)
    jg.plot_joint(sns.scatterplot, palette=colors)
    legend = jg.ax_joint.get_legend()
    legend.set_title('')
    legend._set_loc(4)  
    common_kwargs = {'stat': 'density', 'alpha': 0.5, 'binwidth': 0.25, 'binrange': kwargs['ylim']}
    sns.histplot(y=dual_data['train']['y'], ax=jg.ax_marg_y, color=colors['train'], **common_kwargs)
    sns.histplot(y=dual_data['val']['y'], ax=jg.ax_marg_y, color=colors['val'], **common_kwargs)
    common_kwargs['binrange'] = kwargs['xlim']
    sns.histplot(x=dual_data['train']['pred'], ax=jg.ax_marg_x, color=colors['train'], **common_kwargs)
    sns.histplot(x=dual_data['val']['pred'], ax=jg.ax_marg_x, color=colors['val'], **common_kwargs)
    jg.set_axis_labels(xlabel='predicted value', ylabel='true value')
    jg.fig.tight_layout()
    return jg.fig
def dual_reg_corr_plot ( dual_data : Dict [ str , Dict [ str , torch . Tensor ] ] , full_range : bool = False , margin_ratio : float = 1.1 , ** kwargs ) -> plt . Figure : y_cat = torch . cat ( ( dual_data [ 'train' ] [ 'y' ] , dual_data [ 'val' ] [ 'y' ] ) , axis = 0 ) pred_cat = torch . cat ( ( dual_data [ 'train' ] [ 'pred' ] , dual_data [ 'val' ] [ 'pred' ] ) , axis = 0 ) df = pd . DataFrame ( zip ( pred_cat . numpy ( ) , y_cat . numpy ( ) ) , columns = [ 'predicted value' , 'true value' ] ) if full_range : lim = ( df . values . min ( ) , df . values . max ( ) ) else : lim = ( df [ 'true value' ] . values . min ( ) , df [ 'true value' ] . values . max ( ) ) lim_mid = ( lim [ 0 ] + lim [ 1 ] ) / 2 lim_len = margin_ratio * ( lim [ 1 ] - lim [ 0 ] ) lim = ( lim_mid - lim_len / 2 , lim_mid + lim_len / 2 ) if 'xlim' not in kwargs : kwargs [ 'xlim' ] = lim if 'ylim' not in kwargs : kwargs [ 'ylim' ] = lim colors = { 'train' : matplotlib . colors . to_rgba ( 'orange' , alpha = 0.5 ) , 'val' : matplotlib . colors . to_rgba ( 'royalblue' , alpha = 1 ) } df = df . clip ( lower = lim [ 0 ] , upper = lim [ 1 ] ) df [ 'hue' ] = [ 'train' ] * len ( dual_data [ 'train' ] [ 'y' ] ) + [ 'val' ] * len ( dual_data [ 'val' ] [ 'y' ] ) sns . set_theme ( ) jg = sns . JointGrid ( data = df , x = 'predicted value' , y = 'true value' , hue = 'hue' , ** kwargs ) jg . plot_joint ( sns . scatterplot , palette = colors ) legend = jg . ax_joint . get_legend ( ) legend . set_title ( '' ) legend . _set_loc ( 4 ) common_kwargs = { 'stat' : 'density' , 'alpha' : 0.5 , 'binwidth' : 0.25 , 'binrange' : kwargs [ 'ylim' ] } sns . histplot ( y = dual_data [ 'train' ] [ 'y' ] , ax = jg . ax_marg_y , color = colors [ 'train' ] , ** common_kwargs ) sns . histplot ( y = dual_data [ 'val' ] [ 'y' ] , ax = jg . ax_marg_y , color = colors [ 'val' ] , ** common_kwargs ) common_kwargs [ 'binrange' ] = kwargs [ 'xlim' ] sns . histplot ( x = dual_data [ 'train' ] [ 'pred' ] , ax = jg . ax_marg_x , color = colors [ 'train' ] , ** common_kwargs ) sns . histplot ( x = dual_data [ 'val' ] [ 'pred' ] , ax = jg . ax_marg_x , color = colors [ 'val' ] , ** common_kwargs ) jg . set_axis_labels ( xlabel = 'predicted value' , ylabel = 'true value' ) jg . fig . tight_layout ( ) return jg . fig
Utility function to overlay seaborn jointplots of train and validation set.
Utility function to overlay seaborn jointplots of train and validation set .
"""Utility function to overlay seaborn jointplots of train and validation set.

    Args:
        dual_data: Dictionary of train and val set data in the format of
            {'train': {'pred': predictions, 'true': target values}, 'val': ...}
        full_range: If false, restrict plotted data points to true value range.
        margin_ratio: Extra margin from min-max range.
    """ # noinspection PyArgumentList # noinspection PyArgumentList # define plot value range # noinspection PyArgumentList # jg.ax_joint.invert_yaxis() # not a good practice
dual_data
Dict[str, Dict[str, torch.Tensor]]
Dictionary of train and val set data in the format of
{'train': {'pred': predictions, 'true': target values}, 'val': ...}
Dictionary of train and val set data in the format of { ' train ' : { ' pred ' : predictions ' true ' : target values } ' val ' : ... }
full_range
bool
If false, restrict plotted data points to true value range.
If false restrict plotted data points to true value range .
margin_ratio
float
Extra margin from min-max range.
Extra margin from min - max range .