Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
chiarasch committed Jul 29, 2024
1 parent 9997992 commit 910b76a
Show file tree
Hide file tree
Showing 2 changed files with 307 additions and 6,475 deletions.
203 changes: 187 additions & 16 deletions notebooks/SignalStar_concatenate_tiles.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,46 +2,46 @@
"cells": [
{
"cell_type": "code",
"execution_count": 49,
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"# load libraries\n",
"\n",
"import os\n",
"import os\n",
"import numpy as np\n",
"import tifffile as tiff"
"import tifffile as tiff\n",
"from skimage import util"
]
},
{
"cell_type": "code",
"execution_count": 62,
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"# Paths to input and output directories\n",
"input_dir = './../../../../data/Human_squamous_cell_carcinoma_stained_with_SignalStar_mIHC_technology/test9/'\n",
"output_file = './../../../../data/Human_squamous_cell_carcinoma_stained_with_SignalStar_mIHC_technology/numpy_stacked_output9.tif'"
"input_dir = './../../../../data/Human_squamous_cell_carcinoma_stained_with_SignalStar_mIHC_technology/test/'\n",
"output_file = './../../../../data/Human_squamous_cell_carcinoma_stained_with_SignalStar_mIHC_technology/stacked_image_4tiles_float32.tif'"
]
},
{
"cell_type": "code",
"execution_count": 63,
"execution_count": 28,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Stacked image saved to ./../../../../data/Human_squamous_cell_carcinoma_stained_with_SignalStar_mIHC_technology/numpy_stacked_output9.tif\n"
"Stacked image saved to ./../../../../data/Human_squamous_cell_carcinoma_stained_with_SignalStar_mIHC_technology/stacked_image_4tiles_float32.tif\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"/var/folders/79/cjwcycvd2_9dyfffbym60x200000gn/T/ipykernel_44301/1808586505.py:27: DeprecationWarning: <tifffile.imsave> is deprecated. Use tifffile.imwrite\n",
"/var/folders/79/cjwcycvd2_9dyfffbym60x200000gn/T/ipykernel_9976/1808586505.py:27: DeprecationWarning: <tifffile.imsave> is deprecated. Use tifffile.imwrite\n",
" tiff.imsave(output_file, stacked_image)\n"
]
}
Expand Down Expand Up @@ -80,15 +80,15 @@
},
{
"cell_type": "code",
"execution_count": 64,
"execution_count": 24,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"float32\n",
"0.0 100.97945\n"
"0.0 145.08916\n"
]
}
],
Expand All @@ -100,19 +100,20 @@
},
{
"cell_type": "code",
"execution_count": 66,
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"#rescale float32 to uint16 with scikit image:\n",
"numpy_stacked_tif = './../../../../data/Human_squamous_cell_carcinoma_stained_with_SignalStar_mIHC_technology/numpy_stacked_output9.tif'\n",
"#rescale float32 to uint16 with scikit image with max value per image\n",
"numpy_stacked_tif = output_file\n",
"from skimage import util\n",
"\n",
"# Read the float TIFF\n",
"img = tiff.imread(numpy_stacked_tif)\n",
"# Assuming img is your image data\n",
"\n",
"# Normalize to [0, 1] !!!per channel!!! TODO\n",
"# Normalize to [0, 1] per channel\n",
"\n",
"img_normalized = img / img.max()\n",
"\n",
"# Convert to uint16\n",
Expand All @@ -121,9 +122,179 @@
"# Save the converted image\n",
"#io.imsave('./output_uint16.tif', img_uint16)\n",
"# Save the converted image\n",
"tiff.imwrite('./../../../../data/Human_squamous_cell_carcinoma_stained_with_SignalStar_mIHC_technology/numpy_stacked_output9_uint16.tif', img_uint16)"
"tiff.imwrite('./../../../../data/Human_squamous_cell_carcinoma_stained_with_SignalStar_mIHC_technology/numpy_stacked_output_day2_uint16.tif', img_uint16)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"20.023974266052278\n",
"3.3319761753082275\n",
"3.9181082248687744\n",
"1.088370442390442\n",
"66.18979965209974\n",
"11.126164436340332\n"
]
}
],
"source": [
"# CORRECT normalize with channel 99th percentile\n",
"import numpy as np\n",
"\n",
"numpy_stacked_tif = output_file\n",
"\n",
"# Read the float TIFF\n",
"img = tiff.imread(numpy_stacked_tif)\n",
"\n",
"# Initialize an empty array for the normalized image\n",
"img_normalized = np.empty_like(img, dtype=np.float32)\n",
"\n",
"# Normalize each channel separately\n",
"for ch in range(img.shape[1]): # Now channels are the second dimension\n",
" percentile_99 = np.percentile(img[:, ch, :], 99)\n",
" img_normalized[:, ch, :] = img[:, ch, :] / percentile_99\n",
" img_normalized[:, ch, :][img_normalized[:, ch, :] > 1] = 1 # Cap values at 1\n",
" print(percentile_99)\n",
"\n",
"# Save the converted image\n",
"tiff.imwrite('./../../../../data/Human_squamous_cell_carcinoma_stained_with_SignalStar_mIHC_technology/numpy_stacked_output_day2_min99th_uint16.tif', img_normalized)\n"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"6"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"import numpy as np\n",
"from skimage import util\n",
"\n",
"numpy_stacked_tif = output_file\n",
"# Read the float TIFF\n",
"img = tiff.imread(numpy_stacked_tif)\n",
"\n",
"# Initialize an empty array for the normalized image\n",
"img_normalized = np.empty_like(img, dtype=np.float32)\n",
"img.shape[1]"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"array([[[0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,\n",
" 2.14847937e-01, 0.00000000e+00, 0.00000000e+00],\n",
" [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,\n",
" 0.00000000e+00, 1.91504240e-01, 0.00000000e+00],\n",
" [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,\n",
" 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],\n",
" ...,\n",
" [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,\n",
" 9.18459058e-01, 5.46028256e-01, 5.46562254e-01],\n",
" [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,\n",
" 5.60921371e-01, 5.48131049e-01, 5.30799508e-01],\n",
" [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,\n",
" 9.31420028e-01, 5.48307240e-01, 5.64069986e-01]],\n",
"\n",
" [[0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,\n",
" 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],\n",
" [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,\n",
" 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],\n",
" [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,\n",
" 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],\n",
" ...,\n",
" [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,\n",
" 1.10834405e-01, 7.11129513e-03, 1.19373500e-01],\n",
" [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,\n",
" 1.88111383e-02, 1.57222394e-02, 1.17059074e-01],\n",
" [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,\n",
" 1.19447317e-02, 1.82931498e-02, 2.06075739e-02]],\n",
"\n",
" [[0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,\n",
" 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],\n",
" [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,\n",
" 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],\n",
" [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,\n",
" 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],\n",
" ...,\n",
" [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,\n",
" 1.66345999e-01, 3.14501405e-01, 1.76972955e-01],\n",
" [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,\n",
" 1.83697209e-01, 1.78726181e-01, 1.25935599e-01],\n",
" [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,\n",
" 1.27350181e-01, 1.33352369e-01, 1.84389725e-01]],\n",
"\n",
" [[0.00000000e+00, 0.00000000e+00, 3.62526695e-03, ...,\n",
" 3.80290183e-03, 0.00000000e+00, 5.48279728e-04],\n",
" [0.00000000e+00, 0.00000000e+00, 3.36017460e-03, ...,\n",
" 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],\n",
" [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,\n",
" 1.97691545e-02, 0.00000000e+00, 3.80645297e-03],\n",
" ...,\n",
" [3.36017460e-03, 0.00000000e+00, 5.48279728e-04, ...,\n",
" 5.87619916e-02, 6.42875731e-02, 6.46389276e-02],\n",
" [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,\n",
" 6.75737858e-02, 4.54218239e-02, 8.10298920e-02],\n",
" [0.00000000e+00, 9.12924483e-03, 3.67093482e-04, ...,\n",
" 8.10262784e-02, 4.55377400e-02, 2.91467719e-02]],\n",
"\n",
" [[0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,\n",
" 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],\n",
" [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,\n",
" 0.00000000e+00, 0.00000000e+00, 1.90975890e-01],\n",
" [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,\n",
" 0.00000000e+00, 0.00000000e+00, 0.00000000e+00],\n",
" ...,\n",
" [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,\n",
" 4.55362701e+00, 4.28997660e+00, 3.70709682e+00],\n",
" [0.00000000e+00, 0.00000000e+00, 0.00000000e+00, ...,\n",
" 5.04805708e+00, 4.89953804e+00, 4.13413906e+00],\n",
" [0.00000000e+00, 1.92572594e-01, 0.00000000e+00, ...,\n",
" 4.70491791e+00, 3.12774277e+00, 3.29225278e+00]],\n",
"\n",
" [[2.27079415e+00, 2.22328615e+00, 2.26587939e+00, ...,\n",
" 2.24671698e+00, 2.79649043e+00, 2.47201729e+00],\n",
" [2.46686459e+00, 2.22328615e+00, 2.29281616e+00, ...,\n",
" 2.24424291e+00, 2.64450240e+00, 1.92618632e+00],\n",
" [2.39839983e+00, 2.62137651e+00, 2.57351351e+00, ...,\n",
" 2.47098351e+00, 2.46686459e+00, 2.24465942e+00],\n",
" ...,\n",
" [2.29281616e+00, 2.41935658e+00, 2.47201729e+00, ...,\n",
" 3.70872140e+00, 3.31832981e+00, 3.31832981e+00],\n",
" [2.22328615e+00, 2.27079415e+00, 2.27079415e+00, ...,\n",
" 3.12313414e+00, 3.31832981e+00, 3.51352572e+00],\n",
" [2.27079415e+00, 1.91873717e+00, 2.49323750e+00, ...,\n",
" 3.51352572e+00, 3.31832981e+00, 3.12313414e+00]]], dtype=float32)"
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": []
},
{
"cell_type": "code",
"execution_count": 54,
Expand Down
Loading

0 comments on commit 910b76a

Please sign in to comment.