Skip to content

Commit

Permalink
modify import
Browse files Browse the repository at this point in the history
  • Loading branch information
sky1ove committed May 29, 2024
1 parent 2012882 commit 118ff70
Show file tree
Hide file tree
Showing 4 changed files with 582 additions and 622 deletions.
1 change: 0 additions & 1 deletion katlas/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
# %% ../nbs/00_core.ipynb 4
import math, pandas as pd, numpy as np, seaborn as sns
from tqdm import tqdm
from fastcore.basics import partial
from scipy.stats import chi2
from typing import Callable
from functools import partial
Expand Down
39 changes: 19 additions & 20 deletions katlas/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,17 @@
'draw_corr', 'get_AUCDF', 'plot_confusion_matrix']

# %% ../nbs/02_plot.ipynb 4
import joblib,logomaker,seaborn as sns
import fastcore.all as fc
import joblib,logomaker
import fastcore.all as fc, pandas as pd, numpy as np, seaborn as sns
from adjustText import adjust_text
from pathlib import Path
from fastbook import *

from scipy.stats import spearmanr, pearsonr
from sklearn.metrics import confusion_matrix
from matplotlib import pyplot as plt
from matplotlib.ticker import MultipleLocator
from numpy import trapz
from sklearn.metrics import confusion_matrix


# Katlas
from .feature import *
Expand Down Expand Up @@ -233,7 +235,7 @@ def plot_2d(X: pd.DataFrame, # a dataframe that has first column to be x, and se
plt.figure(figsize=(7,7))
sns.scatterplot(data = X,x=X.columns[0],y=X.columns[1],alpha=0.7,**kwargs)

# %% ../nbs/02_plot.ipynb 32
# %% ../nbs/02_plot.ipynb 33
def plot_cluster(df: pd.DataFrame, # a dataframe of values that is waited for dimensionality reduction
method: str='pca', # dimensionality reduction method, choose from pca, umap, and tsne
hue: str=None, # colname of color
Expand All @@ -258,7 +260,7 @@ def plot_cluster(df: pd.DataFrame, # a dataframe of values that is waited for di
texts = [plt.text(embedding_df[x_col][i], embedding_df[y_col][i], name_list[i],fontsize=8) for i in range(len(embedding_df))]
adjust_text(texts, arrowprops=dict(arrowstyle='-', color='black'))

# %% ../nbs/02_plot.ipynb 36
# %% ../nbs/02_plot.ipynb 37
def plot_bokeh(X:pd.DataFrame, # a dataframe of two columns from dimensionality reduction
idx, # pd.Series or list that indicates identities for searching box
hue:None, # pd.Series or list that indicates category for each sample
Expand Down Expand Up @@ -356,7 +358,7 @@ def assign_colors(categories, palette):
layout = column(autocomplete, p)
show(layout)

# %% ../nbs/02_plot.ipynb 39
# %% ../nbs/02_plot.ipynb 40
def plot_count(cnt, # from df['x'].value_counts()
tick_spacing: float= None, # tick spacing for x axis
palette: str='tab20'):
Expand All @@ -372,7 +374,7 @@ def plot_count(cnt, # from df['x'].value_counts()
if tick_spacing is not None:
ax.xaxis.set_major_locator(MultipleLocator(tick_spacing))

# %% ../nbs/02_plot.ipynb 41
# %% ../nbs/02_plot.ipynb 42
@fc.delegates(sns.barplot)
def plot_bar(df,
value, # colname of value
Expand Down Expand Up @@ -427,16 +429,16 @@ def plot_bar(df,

plt.gca().spines[['right', 'top']].set_visible(False)

# %% ../nbs/02_plot.ipynb 44
# %% ../nbs/02_plot.ipynb 45
@fc.delegates(sns.barplot)
def plot_group_bar(df,
value_cols, # list of column names for values, the order depends on the first item
group, # column name of group (e.g., 'kinase')
title=None,
figsize=(12, 5),
order=None,
title=None,
fontsize=14,
rotation=90,
ascending=False,
**kwargs):

" Plot grouped bar graph from dataframe. "
Expand All @@ -447,9 +449,6 @@ def plot_group_bar(df,

plt.figure(figsize=figsize)

# Sort the groups based on the average of the first ranking column
order = df.groupby(group)[value_cols[0]].mean().sort_values(ascending=ascending).index

# Create the bar plot
sns.barplot(data=df_melted, x=group, y='Value', hue='Ranking', order=order,
capsize=0.1,errwidth=1.5,errcolor='gray', # adjust the error bar settings
Expand All @@ -471,9 +470,9 @@ def plot_group_bar(df,
plt.title(title, fontsize=fontsize)

plt.gca().spines[['right', 'top']].set_visible(False)
plt.legend(title='Ranking')
plt.legend(fontsize=fontsize) # if change legend location, use loc='upper right'

# %% ../nbs/02_plot.ipynb 47
# %% ../nbs/02_plot.ipynb 48
@fc.delegates(sns.boxplot)
def plot_box(df,
value, # colname of value
Expand Down Expand Up @@ -515,7 +514,7 @@ def plot_box(df,
# plt.gca().spines[['right', 'top']].set_visible(False)


# %% ../nbs/02_plot.ipynb 50
# %% ../nbs/02_plot.ipynb 51
@fc.delegates(sns.regplot)
def plot_corr(x, # x axis values, or colname of x axis
y, # y axis values, or colname of y axis
Expand Down Expand Up @@ -552,7 +551,7 @@ def plot_corr(x, # x axis values, or colname of x axis
transform=plt.gca().transAxes,
ha='center', va='center')

# %% ../nbs/02_plot.ipynb 54
# %% ../nbs/02_plot.ipynb 55
def draw_corr(corr):

"plot heatmap from df.corr()"
Expand All @@ -564,7 +563,7 @@ def draw_corr(corr):
plt.figure(figsize=(20, 16)) # Set the figure size
sns.heatmap(corr, annot=True, cmap='coolwarm', vmin=-1, vmax=1, mask=mask, fmt='.2f')

# %% ../nbs/02_plot.ipynb 58
# %% ../nbs/02_plot.ipynb 59
def get_AUCDF(df,col, reverse=False,plot=True,xlabel='Rank of reported kinase'):

"Plot CDF curve and get relative area under the curve"
Expand Down Expand Up @@ -629,7 +628,7 @@ def get_AUCDF(df,col, reverse=False,plot=True,xlabel='Rank of reported kinase'):

return AUCDF

# %% ../nbs/02_plot.ipynb 61
# %% ../nbs/02_plot.ipynb 62
def plot_confusion_matrix(target, # pd.Series
pred, # pd.Series
class_names:list=['0','1'],
Expand Down
Loading

0 comments on commit 118ff70

Please sign in to comment.