Skip to content

Instantly share code, notes, and snippets.

import scipy
import scipy.stats
from scipy.cluster.hierarchy import dendrogram, linkage
from scipy.cluster import hierarchy
def sort_df_by_hclust_olo(df, how='both', method='ward', metric='euclidean'):
'''
how={'index', 'columns', 'both'}
'''
df = df.fillna(0)
from scipy.stats import gmean
def geometric_mean(df):
'''https://www.reddit.com/r/learnpython/comments/mq5ea7/pandas_calculate_geometric_mean_while_ignoring/'''
return df.replace(0, np.nan).apply(lambda row: gmean(row[~row.isna()]), axis=1).fillna(0)
import matplotlib.pyplot as plt
from matplotlib_venn import venn2, venn3
import upsetplot
import matplotlib_inline.backend_inline
matplotlib_inline.backend_inline.set_matplotlib_formats('svg')
%config InlineBackend.figure_format = 'retina'
%matplotlib inline
plt.rcParams['figure.figsize'] = [6, 3]
import sys
from types import ModuleType, FunctionType
from gc import get_referents
# Custom objects know their class.
# Function objects seem to know way too much, including modules.
# Exclude modules as well.
BLACKLIST = type, ModuleType, FunctionType
# Long running cell
import multiprocessing, time
def network_call():
for i in range(20):
print(i)
time.sleep(1)
multiprocessing.Process(target=network_call).start()
import mygene
def namespace_mapping(names, map_from=['symbol', 'alias'], map_to='symbol', species='human'):
names = pd.Series(names)
print(f"passed {len(names)} symbols")
names_stripped = names.str.strip()
if any(names_stripped != names):
import PIL.Image
from matplotlib.pyplot import imshow
def show(*Ms):
fig, axs = plt.subplots(ncols=len(Ms))
for M, ax in zip(Ms, (axs if type(axs) == list else [axs])):
M = np.atleast_2d(M)
ax.imshow(PIL.Image.fromarray((M - M.min()) * 255 / M.max()))
ax.set_xticks([0, M.shape[1]])
ax.set_yticks([0, M.shape[0]])
for side in ["right","top","bottom","left"]:
{
"cells": [
{
"cell_type": "code",
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
"import pandas as pd\n",
"import numpy as np\n",
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.