Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": 36, | |
"metadata": {}, | |
"outputs": [], | |
"source": [ | |
"import pandas as pd\n", | |
"import numpy as np\n", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"]: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import h5py | |
import numpy as np | |
def read_h5_to_dict(h5_path): | |
out_dict = {} | |
def add_h5_node_to_dict(name, node, out_dict=out_dict): | |
fullname = node.name |