Skip to content

Instantly share code, notes, and snippets.

@gokceneraslan
Last active November 24, 2019 20:35
Show Gist options
  • Save gokceneraslan/2744cfeda702fb9b9e48d0216427372c to your computer and use it in GitHub Desktop.
Save gokceneraslan/2744cfeda702fb9b9e48d0216427372c to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## log1p with different bases in Scanpy"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import scanpy as sc\n",
"import numpy as np\n",
"from numba import vectorize, float64, float32, int32, int64"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"@vectorize([float64(int32),\n",
" float64(int64),\n",
" float32(float32),\n",
" float64(float64)])\n",
"def log1p(x):\n",
" # see: https://github.com/numpy/numpy/blob/master/numpy/core/src/npymath/npy_math_internal.h.src#L86\n",
" if np.isinf(x) and x > 0: \n",
" return x\n",
" \n",
" u = 1. + x\n",
" d = u - 1.\n",
" \n",
" if d == 0.:\n",
" return x\n",
" \n",
" return np.log(u) * x / d\n",
"\n",
"@vectorize([float64(int32),\n",
" float64(int64),\n",
" float32(float32),\n",
" float64(float64)])\n",
"def log2_1p(x):\n",
" if np.isinf(x) and x > 0: \n",
" return x\n",
" \n",
" u = 1. + x\n",
" d = u - 1.\n",
" \n",
" if d == 0:\n",
" return x\n",
"\n",
" return np.log2(u) * x / d\n",
"\n",
"\n",
"@vectorize([float64(int32),\n",
" float64(int64),\n",
" float32(float32),\n",
" float64(float64)])\n",
"def log10_1p(x):\n",
" if np.isinf(x) and x > 0: \n",
" return x\n",
" \n",
" u = 1. + x\n",
" d = u - 1.\n",
" \n",
" if d == 0.:\n",
" return x\n",
"\n",
" return np.log10(u) * x / d"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"WARNING: In Scanpy 0.*, this returned logarithmized data. Now it returns non-logarithmized data.\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"... storing 'paul15_clusters' as categorical\n",
"Trying to set attribute `.uns` of view, making a copy.\n"
]
},
{
"data": {
"text/plain": [
"AnnData object with n_obs × n_vars = 2730 × 3451 \n",
" obs: 'paul15_clusters'\n",
" uns: 'iroot'"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"adata = sc.datasets.paul15()\n",
"adata"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"sc.pp.normalize_total(adata, target_sum=10000)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"adata.X[0, 0] = 1e-30"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1.0000000e-30, 0.0000000e+00, 0.0000000e+00, ..., 0.0000000e+00,\n",
" 5.6657223e+01, 0.0000000e+00],\n",
" [0.0000000e+00, 0.0000000e+00, 3.9123631e+00, ..., 0.0000000e+00,\n",
" 3.9123631e+00, 0.0000000e+00],\n",
" [2.1510003e+00, 0.0000000e+00, 6.4530010e+00, ..., 4.3020005e+00,\n",
" 6.4530010e+00, 0.0000000e+00],\n",
" ...,\n",
" [0.0000000e+00, 0.0000000e+00, 2.2573362e+01, ..., 0.0000000e+00,\n",
" 0.0000000e+00, 0.0000000e+00],\n",
" [9.2250929e+00, 0.0000000e+00, 9.2250929e+00, ..., 0.0000000e+00,\n",
" 3.0750308e+00, 0.0000000e+00],\n",
" [0.0000000e+00, 0.0000000e+00, 6.2490234e+00, ..., 1.5622559e+00,\n",
" 1.5622559e+00, 1.5622559e+00]], dtype=float32)"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"adata.X"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/local/lib/python3.7/site-packages/ipykernel_launcher.py:1: RuntimeWarning: invalid value encountered in log1p\n",
" \"\"\"Entry point for launching an IPython kernel.\n"
]
},
{
"data": {
"text/plain": [
"(array([[1.0000000e-30, 0.0000000e+00, 0.0000000e+00, ..., 0.0000000e+00,\n",
" 4.0545154e+00, 0.0000000e+00],\n",
" [0.0000000e+00, 0.0000000e+00, 1.5917552e+00, ..., 0.0000000e+00,\n",
" 1.5917552e+00, 0.0000000e+00],\n",
" [1.1477200e+00, 0.0000000e+00, 2.0086167e+00, ..., 1.6680843e+00,\n",
" 2.0086167e+00, 0.0000000e+00],\n",
" ...,\n",
" [0.0000000e+00, 0.0000000e+00, 3.1601174e+00, ..., 0.0000000e+00,\n",
" 0.0000000e+00, 0.0000000e+00],\n",
" [2.3248448e+00, 0.0000000e+00, 2.3248448e+00, ..., 0.0000000e+00,\n",
" 1.4048783e+00, 0.0000000e+00],\n",
" [0.0000000e+00, 0.0000000e+00, 1.9808668e+00, ..., 9.4088805e-01,\n",
" 9.4088805e-01, 9.4088805e-01]], dtype=float32),\n",
" array([[1.0000000e-30, 0.0000000e+00, 0.0000000e+00, ..., 0.0000000e+00,\n",
" 4.0545154e+00, 0.0000000e+00],\n",
" [0.0000000e+00, 0.0000000e+00, 1.5917552e+00, ..., 0.0000000e+00,\n",
" 1.5917552e+00, 0.0000000e+00],\n",
" [1.1477200e+00, 0.0000000e+00, 2.0086167e+00, ..., 1.6680843e+00,\n",
" 2.0086167e+00, 0.0000000e+00],\n",
" ...,\n",
" [0.0000000e+00, 0.0000000e+00, 3.1601174e+00, ..., 0.0000000e+00,\n",
" 0.0000000e+00, 0.0000000e+00],\n",
" [2.3248448e+00, 0.0000000e+00, 2.3248448e+00, ..., 0.0000000e+00,\n",
" 1.4048783e+00, 0.0000000e+00],\n",
" [0.0000000e+00, 0.0000000e+00, 1.9808668e+00, ..., 9.4088805e-01,\n",
" 9.4088805e-01, 9.4088805e-01]], dtype=float32),\n",
" array([[0. , 0. , 0. , ..., 0. , 4.0545154 ,\n",
" 0. ],\n",
" [0. , 0. , 1.5917552 , ..., 0. , 1.5917552 ,\n",
" 0. ],\n",
" [1.14772 , 0. , 2.0086167 , ..., 1.6680843 , 2.0086167 ,\n",
" 0. ],\n",
" ...,\n",
" [0. , 0. , 3.1601174 , ..., 0. , 0. ,\n",
" 0. ],\n",
" [2.3248448 , 0. , 2.3248448 , ..., 0. , 1.4048783 ,\n",
" 0. ],\n",
" [0. , 0. , 1.9808668 , ..., 0.94088805, 0.94088805,\n",
" 0.94088805]], dtype=float32))"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"log1p(adata.X), np.log1p(adata.X), np.log(adata.X + 1.)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/local/lib/python3.7/site-packages/ipykernel_launcher.py:1: RuntimeWarning: invalid value encountered in log1p\n",
" \"\"\"Entry point for launching an IPython kernel.\n"
]
},
{
"data": {
"text/plain": [
"True"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"np.allclose(log1p(adata.X), np.log1p(adata.X), rtol=1e-50, atol=1e-50)"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"AnnData object with n_obs × n_vars = 2730 × 3451 \n",
" obs: 'paul15_clusters'\n",
" uns: 'iroot'"
]
},
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"adata"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/local/lib/python3.7/site-packages/ipykernel_launcher.py:2: RuntimeWarning: invalid value encountered in log1p\n",
" \n"
]
},
{
"data": {
"text/plain": [
"(array([[0. , 0. , 0. , 0. ],\n",
" [0. , 3.912363, 3.912363, 0. ]], dtype=float32),\n",
" array([[0. , 0. , 0. , 0. ],\n",
" [0. , 1.5917552, 1.5917552, 0. ]], dtype=float32))"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"x = adata.X[:2, 1:5]\n",
"x, log1p(x)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[1.0000000e-30, 0.0000000e+00, 0.0000000e+00, ..., 0.0000000e+00,\n",
" 5.8494296e+00, 0.0000000e+00],\n",
" [0.0000000e+00, 0.0000000e+00, 2.2964172e+00, ..., 0.0000000e+00,\n",
" 2.2964172e+00, 0.0000000e+00],\n",
" [1.6558099e+00, 0.0000000e+00, 2.8978214e+00, ..., 2.4065368e+00,\n",
" 2.8978214e+00, 0.0000000e+00],\n",
" ...,\n",
" [0.0000000e+00, 0.0000000e+00, 4.5590858e+00, ..., 0.0000000e+00,\n",
" 0.0000000e+00, 0.0000000e+00],\n",
" [3.3540421e+00, 0.0000000e+00, 3.3540421e+00, ..., 0.0000000e+00,\n",
" 2.0268109e+00, 0.0000000e+00],\n",
" [0.0000000e+00, 0.0000000e+00, 2.8577867e+00, ..., 1.3574146e+00,\n",
" 1.3574146e+00, 1.3574146e+00]], dtype=float32)"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"log2_1p(adata.X)"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/usr/local/lib/python3.7/site-packages/ipykernel_launcher.py:1: RuntimeWarning: invalid value encountered in log10_1p\n",
" \"\"\"Entry point for launching an IPython kernel.\n"
]
},
{
"data": {
"text/plain": [
"array([[1.0000000e-30, 0.0000000e+00, 0.0000000e+00, ..., 0.0000000e+00,\n",
" 1.7608538e+00, 0.0000000e+00],\n",
" [0.0000000e+00, 0.0000000e+00, 6.9129044e-01, ..., 0.0000000e+00,\n",
" 6.9129044e-01, 0.0000000e+00],\n",
" [4.9844843e-01, 0.0000000e+00, 8.7233120e-01, ..., 7.2443974e-01,\n",
" 8.7233120e-01, 0.0000000e+00],\n",
" ...,\n",
" [0.0000000e+00, 0.0000000e+00, 1.3724215e+00, ..., 0.0000000e+00,\n",
" 0.0000000e+00, 0.0000000e+00],\n",
" [1.0096673e+00, 0.0000000e+00, 1.0096673e+00, ..., 0.0000000e+00,\n",
" 6.1013091e-01, 0.0000000e+00],\n",
" [0.0000000e+00, 0.0000000e+00, 8.6027950e-01, ..., 4.0862250e-01,\n",
" 4.0862250e-01, 4.0862250e-01]], dtype=float32)"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"log10_1p(adata.X)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
}
},
"nbformat": 4,
"nbformat_minor": 4
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment