{ "cells": [ { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] }, { "cell_type": "code", "execution_count": 1, "metadata": {}, "outputs": [], "source": [ "class ImageProcessor:\n", " def __init__(self, flatmasks):\n", " self.flatmasks = flatmasks\n", " \n", " def update_adjacency_value(self, adjacency_matrix, original, neighbor):\n", " border = False\n", "\n", " if original != 0 and original != neighbor:\n", " border = True\n", " if neighbor != 0:\n", " adjacency_matrix[int(original - 1), int(neighbor - 1)] += 1\n", " return border\n", "\n", " def update_adjacency_matrix(self, plane_mask_flattened, width, height, adjacency_matrix, index):\n", " mod_value_width = index % width\n", " origin_mask = plane_mask_flattened[index]\n", " left, right, up, down = False, False, False, False\n", "\n", " if (mod_value_width != 0):\n", " left = self.update_adjacency_value(adjacency_matrix, origin_mask, plane_mask_flattened[index-1])\n", " if (mod_value_width != width - 1):\n", " right = self.update_adjacency_value(adjacency_matrix, origin_mask, plane_mask_flattened[index+1])\n", " if (index >= width):\n", " up = self.update_adjacency_value(adjacency_matrix, origin_mask, plane_mask_flattened[index-width])\n", " if (index <= len(plane_mask_flattened) - 1 - width):\n", " down = self.update_adjacency_value(adjacency_matrix, origin_mask, plane_mask_flattened[index+width])\n", " \n", " if (left or right or up or down):\n", " adjacency_matrix[int(origin_mask - 1), int(origin_mask-1)] += 1\n", "\n", " def compute_channel_means_sums_compensated(self, image):\n", " height, width, n_channels = image.shape\n", " mask_height, mask_width = self.flatmasks.shape\n", " n_masks = len(np.unique(self.flatmasks)) - 1\n", " channel_sums = np.zeros((n_masks, n_channels))\n", " channel_counts = np.zeros((n_masks, n_channels))\n", " if n_masks == 0:\n", " return channel_sums, channel_sums, channel_counts\n", "\n", " squashed_image = np.reshape(image, (height*width, n_channels))\n", " \n", " #masklocs = np.nonzero(self.flatmasks)\n", " #plane_mask = np.zeros((mask_height, mask_width), dtype = np.uint32)\n", " #plane_mask[masklocs[0], masklocs[1]] = masklocs[2] + 1\n", " #plane_mask = plane_mask.flatten()\n", " plane_mask = self.flatmasks.flatten()\n", " \n", " adjacency_matrix = np.zeros((n_masks, n_masks))\n", " for i in range(len(plane_mask)):\n", " self.update_adjacency_matrix(plane_mask, mask_width, mask_height, adjacency_matrix, i)\n", " \n", " mask_val = plane_mask[i] - 1\n", " if mask_val != -1:\n", " channel_sums[mask_val.astype(np.int32)] += squashed_image[i]\n", " channel_counts[mask_val.astype(np.int32)] += 1\n", " \n", " \n", " # Normalize adjacency matrix\n", " for i in range(n_masks):\n", " adjacency_matrix[i] = adjacency_matrix[i] / (max(adjacency_matrix[i, i], 1) * 2)\n", " adjacency_matrix[i, i] = 1\n", " \n", " means = np.true_divide(channel_sums, channel_counts, out=np.zeros_like(channel_sums, dtype='float'), where=channel_counts!=0)\n", " results = np.linalg.lstsq(adjacency_matrix, means, rcond=None)\n", " compensated_means = np.maximum(results[0], np.zeros((1,1))) \n", "\n", " return compensated_means, means, channel_counts[:,0]" ] }, { "cell_type": "code", "execution_count": 2, "metadata": {}, "outputs": [], "source": [ "import torch" ] }, { "cell_type": "code", "execution_count": 41, "metadata": {}, "outputs": [], "source": [ "class ImageProcessor:\n", " def __init__(self, flatmasks):\n", " self.flatmasks = flatmasks\n", " \n", " def update_adjacency_value(self, adjacency_matrix, original, neighbor):\n", " border = False\n", "\n", " if original != 0 and original != neighbor:\n", " border = True\n", " if neighbor != 0:\n", " adjacency_matrix[int(original - 1), int(neighbor - 1)] += 1\n", " return border\n", "\n", " def update_adjacency_matrix(self, plane_mask_flattened, width, height, adjacency_matrix, index):\n", " mod_value_width = index % width\n", " origin_mask = plane_mask_flattened[index]\n", " left, right, up, down = False, False, False, False\n", "\n", " if (mod_value_width != 0):\n", " left = self.update_adjacency_value(adjacency_matrix, origin_mask, plane_mask_flattened[index-1])\n", " if (mod_value_width != width - 1):\n", " right = self.update_adjacency_value(adjacency_matrix, origin_mask, plane_mask_flattened[index+1])\n", " if (index >= width):\n", " up = self.update_adjacency_value(adjacency_matrix, origin_mask, plane_mask_flattened[index-width])\n", " if (index <= len(plane_mask_flattened) - 1 - width):\n", " down = self.update_adjacency_value(adjacency_matrix, origin_mask, plane_mask_flattened[index+width])\n", " \n", " if (left or right or up or down):\n", " adjacency_matrix[int(origin_mask - 1), int(origin_mask-1)] += 1\n", "\n", " def compute_channel_means_sums_compensated(self, image):\n", " height, width, n_channels = image.shape\n", " mask_height, mask_width = self.flatmasks.shape\n", " n_masks = len(np.unique(self.flatmasks)) - 1\n", " channel_sums = np.zeros((n_masks, n_channels))\n", " channel_counts = np.zeros((n_masks, n_channels))\n", " if n_masks == 0:\n", " return channel_sums, channel_sums, channel_counts\n", "\n", " squashed_image = np.reshape(image, (height*width, n_channels))\n", " \n", " #masklocs = np.nonzero(self.flatmasks)\n", " #plane_mask = np.zeros((mask_height, mask_width), dtype = np.uint32)\n", " #plane_mask[masklocs[0], masklocs[1]] = masklocs[2] + 1\n", " #plane_mask = plane_mask.flatten()\n", " plane_mask = self.flatmasks.flatten()\n", " \n", " adjacency_matrix = np.zeros((n_masks, n_masks))\n", " for i in range(len(plane_mask)):\n", " self.update_adjacency_matrix(plane_mask, mask_width, mask_height, adjacency_matrix, i)\n", " \n", " mask_val = plane_mask[i] - 1\n", " if mask_val != -1:\n", " channel_sums[mask_val.astype(np.int32)] += squashed_image[i]\n", " channel_counts[mask_val.astype(np.int32)] += 1\n", " \n", " \n", " # Normalize adjacency matrix\n", " for i in range(n_masks):\n", " adjacency_matrix[i] = adjacency_matrix[i] / (max(adjacency_matrix[i, i], 1) * 2)\n", " adjacency_matrix[i, i] = 1\n", " \n", " means = np.true_divide(channel_sums, channel_counts, out=np.zeros_like(channel_sums, dtype='float'), where=channel_counts!=0)\n", " # Convert your numpy arrays to PyTorch tensors\n", " adjacency_matrix_torch = torch.from_numpy(adjacency_matrix)\n", " means_torch = torch.from_numpy(means)\n", "\n", " # Solve the least squares problem\n", " results_torch = torch.linalg.lstsq(adjacency_matrix_torch, means_torch).solution\n", "\n", " # Convert the result back to a numpy array if needed\n", " # Convert the result back to a numpy array if needed\n", " results = results_torch.numpy()\n", " compensated_means = np.maximum(results, np.zeros(results.shape)) \n", "\n", " return compensated_means, means, channel_counts[:,0]" ] }, { "cell_type": "code", "execution_count": 76, "metadata": {}, "outputs": [], "source": [ "# load pickle file\n", "import pickle \n", "\n", "with open(\"/home/timkempchen/Downloads/seg_output_tonsil2.pickle\", 'rb') as f:\n", " seg_output = pickle.load(f)" ] }, { "cell_type": "code", "execution_count": 77, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "dict_keys(['img', 'masks', 'image_dict'])" ] }, "execution_count": 77, "metadata": {}, "output_type": "execute_result" } ], "source": [ "seg_output.keys()" ] }, { "cell_type": "code", "execution_count": 78, "metadata": {}, "outputs": [], "source": [ "# get keys of image_dict\n", "channelnames = list(seg_output['image_dict'].keys())\n", "\n", "images = seg_output['image_dict']\n", "masks = seg_output['masks']\n", "masks = masks.squeeze()" ] }, { "cell_type": "code", "execution_count": 79, "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "\n", "# Assuming `images` is your dictionary of 2D numpy arrays\n", "# and `masks` is your 2D numpy array of masks\n", "\n", "# Create a list of the 2D numpy arrays in the dictionary\n", "image_list = [images[channel_name] for channel_name in images.keys()]" ] }, { "cell_type": "code", "execution_count": 80, "metadata": {}, "outputs": [], "source": [ "\n", "\n", "# Stack the 2D numpy arrays along the third dimension to create a 3D numpy array\n", "image = np.stack(image_list, axis=-1)\n", "\n", "# Now you can use `image` as the input for the function\n", "processor = ImageProcessor(masks)\n", "compensated_means, means, channel_counts = processor.compute_channel_means_sums_compensated(image)" ] }, { "cell_type": "code", "execution_count": 81, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Unnamed: 0DAPIFoxP3HLA-DRCD103CHGAEGFRCD206GFAPPD-1...GATA3xyeccentricityperimeterconvex_areaareaaxis_major_lengthaxis_minor_lengthlabel
01105.9931971.3401360.55782316.4421778.2789126.1836733.3061221.06802713.020408...11.0748304.9863951472.2380950.60348544.142136154.0147.015.43963312.3111691
12123.6776860.6198350.83057917.22314017.1942155.9752073.6239670.94214915.983471...11.9090915.3595041322.8512400.85389363.248737267.0242.024.68874112.8492012
23107.2031251.2812500.67187517.9257819.6992196.5898443.5664061.02343813.890625...11.8164065.7109381506.2265620.76601761.798990268.0256.022.85532214.6918703
3449.6609590.1369860.00684939.62328825.1027402.7979450.9897260.8013708.965753...6.7226038.544521641.9383560.64576463.112698306.0292.022.07756316.8570444
45148.7025321.3101271.56329118.18354433.2278487.9810135.0822780.94303821.196203...16.8291149.0063291303.7025320.76680048.420310172.0158.017.88862111.4824485
..................................................................
233262332750.5123460.9567901.40123523.74691418.39506224.4814812.9320990.90123512.617284...6.6543212523.8888891384.0864200.76153948.627417176.0162.017.97348611.64897023327
2332723328160.0156861.1764713.13725519.73725534.21960811.3098044.7215690.97647119.717647...21.0352942522.0470591438.5686270.72257862.077164274.0255.021.86642215.11601523328
233282332956.7341770.7594940.86075916.83544317.89873416.4050632.0632910.92405110.569620...6.5063292523.9240511349.7594940.70759932.97056386.079.012.1341678.57416923329
233292333086.3265310.8061221.31632718.61224519.30612216.5714292.5612241.01020413.540816...9.8061222525.2346941420.3877550.76093335.556349103.098.013.9299859.03819523330
233302333111.5227270.1477270.0000001.1704551.2386361.3977270.1363640.8863644.488636...1.6590912535.886364231.3295450.47236531.55634989.088.011.2565389.92154423331
\n", "

23331 rows × 69 columns

\n", "
" ], "text/plain": [ " Unnamed: 0 DAPI FoxP3 HLA-DR CD103 CHGA \\\n", "0 1 105.993197 1.340136 0.557823 16.442177 8.278912 \n", "1 2 123.677686 0.619835 0.830579 17.223140 17.194215 \n", "2 3 107.203125 1.281250 0.671875 17.925781 9.699219 \n", "3 4 49.660959 0.136986 0.006849 39.623288 25.102740 \n", "4 5 148.702532 1.310127 1.563291 18.183544 33.227848 \n", "... ... ... ... ... ... ... \n", "23326 23327 50.512346 0.956790 1.401235 23.746914 18.395062 \n", "23327 23328 160.015686 1.176471 3.137255 19.737255 34.219608 \n", "23328 23329 56.734177 0.759494 0.860759 16.835443 17.898734 \n", "23329 23330 86.326531 0.806122 1.316327 18.612245 19.306122 \n", "23330 23331 11.522727 0.147727 0.000000 1.170455 1.238636 \n", "\n", " EGFR CD206 GFAP PD-1 ... GATA3 x \\\n", "0 6.183673 3.306122 1.068027 13.020408 ... 11.074830 4.986395 \n", "1 5.975207 3.623967 0.942149 15.983471 ... 11.909091 5.359504 \n", "2 6.589844 3.566406 1.023438 13.890625 ... 11.816406 5.710938 \n", "3 2.797945 0.989726 0.801370 8.965753 ... 6.722603 8.544521 \n", "4 7.981013 5.082278 0.943038 21.196203 ... 16.829114 9.006329 \n", "... ... ... ... ... ... ... ... \n", "23326 24.481481 2.932099 0.901235 12.617284 ... 6.654321 2523.888889 \n", "23327 11.309804 4.721569 0.976471 19.717647 ... 21.035294 2522.047059 \n", "23328 16.405063 2.063291 0.924051 10.569620 ... 6.506329 2523.924051 \n", "23329 16.571429 2.561224 1.010204 13.540816 ... 9.806122 2525.234694 \n", "23330 1.397727 0.136364 0.886364 4.488636 ... 1.659091 2535.886364 \n", "\n", " y eccentricity perimeter convex_area area \\\n", "0 1472.238095 0.603485 44.142136 154.0 147.0 \n", "1 1322.851240 0.853893 63.248737 267.0 242.0 \n", "2 1506.226562 0.766017 61.798990 268.0 256.0 \n", "3 641.938356 0.645764 63.112698 306.0 292.0 \n", "4 1303.702532 0.766800 48.420310 172.0 158.0 \n", "... ... ... ... ... ... \n", "23326 1384.086420 0.761539 48.627417 176.0 162.0 \n", "23327 1438.568627 0.722578 62.077164 274.0 255.0 \n", "23328 1349.759494 0.707599 32.970563 86.0 79.0 \n", "23329 1420.387755 0.760933 35.556349 103.0 98.0 \n", "23330 231.329545 0.472365 31.556349 89.0 88.0 \n", "\n", " axis_major_length axis_minor_length label \n", "0 15.439633 12.311169 1 \n", "1 24.688741 12.849201 2 \n", "2 22.855322 14.691870 3 \n", "3 22.077563 16.857044 4 \n", "4 17.888621 11.482448 5 \n", "... ... ... ... \n", "23326 17.973486 11.648970 23327 \n", "23327 21.866422 15.116015 23328 \n", "23328 12.134167 8.574169 23329 \n", "23329 13.929985 9.038195 23330 \n", "23330 11.256538 9.921544 23331 \n", "\n", "[23331 rows x 69 columns]" ] }, "execution_count": 81, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# open df\n", "import pandas as pd\n", "\n", "df = pd.read_csv(\"/home/timkempchen/Downloads/tonsil2_mesmer_result.csv\")\n", "df" ] }, { "cell_type": "code", "execution_count": 82, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "array([[73.4003194 , 0.96314494, 0. , ..., 17.3796773 ,\n", " 6.1359789 , 4.10462704],\n", " [62.18477628, 0. , 0. , ..., 1.98561595,\n", " 3.43656937, 2.04505872],\n", " [95.65718851, 1.13134308, 0. , ..., 22.91852613,\n", " 10.15428826, 9.49503184],\n", " ...,\n", " [23.74777761, 0.40885537, 1.12656697, ..., 2.91814228,\n", " 2.94357561, 30.74130252],\n", " [41.32538188, 0.15023363, 0.5936194 , ..., 2.48362393,\n", " 2.98513237, 47.74424314],\n", " [11.52272727, 0.14772727, 0. , ..., 2.59090909,\n", " 1.65909091, 1.88636364]])" ] }, "execution_count": 82, "metadata": {}, "output_type": "execute_result" } ], "source": [ "compensated_means" ] }, { "cell_type": "code", "execution_count": 83, "metadata": {}, "outputs": [], "source": [ "# Get the keys\n", "keys = list(images.keys())" ] }, { "cell_type": "code", "execution_count": 84, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "60" ] }, "execution_count": 84, "metadata": {}, "output_type": "execute_result" } ], "source": [ "len(keys)" ] }, { "cell_type": "code", "execution_count": 85, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Unnamed: 0DAPIFoxP3HLA-DRCD103CHGAEGFRCD206GFAPPD-1...GATA3xyeccentricityperimeterconvex_areaareaaxis_major_lengthaxis_minor_lengthlabel
01105.9931971.3401360.55782316.4421778.2789126.1836733.3061221.06802713.020408...11.0748304.9863951472.2380950.60348544.142136154.0147.015.43963312.3111691
12123.6776860.6198350.83057917.22314017.1942155.9752073.6239670.94214915.983471...11.9090915.3595041322.8512400.85389363.248737267.0242.024.68874112.8492012
23107.2031251.2812500.67187517.9257819.6992196.5898443.5664061.02343813.890625...11.8164065.7109381506.2265620.76601761.798990268.0256.022.85532214.6918703
3449.6609590.1369860.00684939.62328825.1027402.7979450.9897260.8013708.965753...6.7226038.544521641.9383560.64576463.112698306.0292.022.07756316.8570444
45148.7025321.3101271.56329118.18354433.2278487.9810135.0822780.94303821.196203...16.8291149.0063291303.7025320.76680048.420310172.0158.017.88862111.4824485
..................................................................
233262332750.5123460.9567901.40123523.74691418.39506224.4814812.9320990.90123512.617284...6.6543212523.8888891384.0864200.76153948.627417176.0162.017.97348611.64897023327
2332723328160.0156861.1764713.13725519.73725534.21960811.3098044.7215690.97647119.717647...21.0352942522.0470591438.5686270.72257862.077164274.0255.021.86642215.11601523328
233282332956.7341770.7594940.86075916.83544317.89873416.4050632.0632910.92405110.569620...6.5063292523.9240511349.7594940.70759932.97056386.079.012.1341678.57416923329
233292333086.3265310.8061221.31632718.61224519.30612216.5714292.5612241.01020413.540816...9.8061222525.2346941420.3877550.76093335.556349103.098.013.9299859.03819523330
233302333111.5227270.1477270.0000001.1704551.2386361.3977270.1363640.8863644.488636...1.6590912535.886364231.3295450.47236531.55634989.088.011.2565389.92154423331
\n", "

23331 rows × 69 columns

\n", "
" ], "text/plain": [ " Unnamed: 0 DAPI FoxP3 HLA-DR CD103 CHGA \\\n", "0 1 105.993197 1.340136 0.557823 16.442177 8.278912 \n", "1 2 123.677686 0.619835 0.830579 17.223140 17.194215 \n", "2 3 107.203125 1.281250 0.671875 17.925781 9.699219 \n", "3 4 49.660959 0.136986 0.006849 39.623288 25.102740 \n", "4 5 148.702532 1.310127 1.563291 18.183544 33.227848 \n", "... ... ... ... ... ... ... \n", "23326 23327 50.512346 0.956790 1.401235 23.746914 18.395062 \n", "23327 23328 160.015686 1.176471 3.137255 19.737255 34.219608 \n", "23328 23329 56.734177 0.759494 0.860759 16.835443 17.898734 \n", "23329 23330 86.326531 0.806122 1.316327 18.612245 19.306122 \n", "23330 23331 11.522727 0.147727 0.000000 1.170455 1.238636 \n", "\n", " EGFR CD206 GFAP PD-1 ... GATA3 x \\\n", "0 6.183673 3.306122 1.068027 13.020408 ... 11.074830 4.986395 \n", "1 5.975207 3.623967 0.942149 15.983471 ... 11.909091 5.359504 \n", "2 6.589844 3.566406 1.023438 13.890625 ... 11.816406 5.710938 \n", "3 2.797945 0.989726 0.801370 8.965753 ... 6.722603 8.544521 \n", "4 7.981013 5.082278 0.943038 21.196203 ... 16.829114 9.006329 \n", "... ... ... ... ... ... ... ... \n", "23326 24.481481 2.932099 0.901235 12.617284 ... 6.654321 2523.888889 \n", "23327 11.309804 4.721569 0.976471 19.717647 ... 21.035294 2522.047059 \n", "23328 16.405063 2.063291 0.924051 10.569620 ... 6.506329 2523.924051 \n", "23329 16.571429 2.561224 1.010204 13.540816 ... 9.806122 2525.234694 \n", "23330 1.397727 0.136364 0.886364 4.488636 ... 1.659091 2535.886364 \n", "\n", " y eccentricity perimeter convex_area area \\\n", "0 1472.238095 0.603485 44.142136 154.0 147.0 \n", "1 1322.851240 0.853893 63.248737 267.0 242.0 \n", "2 1506.226562 0.766017 61.798990 268.0 256.0 \n", "3 641.938356 0.645764 63.112698 306.0 292.0 \n", "4 1303.702532 0.766800 48.420310 172.0 158.0 \n", "... ... ... ... ... ... \n", "23326 1384.086420 0.761539 48.627417 176.0 162.0 \n", "23327 1438.568627 0.722578 62.077164 274.0 255.0 \n", "23328 1349.759494 0.707599 32.970563 86.0 79.0 \n", "23329 1420.387755 0.760933 35.556349 103.0 98.0 \n", "23330 231.329545 0.472365 31.556349 89.0 88.0 \n", "\n", " axis_major_length axis_minor_length label \n", "0 15.439633 12.311169 1 \n", "1 24.688741 12.849201 2 \n", "2 22.855322 14.691870 3 \n", "3 22.077563 16.857044 4 \n", "4 17.888621 11.482448 5 \n", "... ... ... ... \n", "23326 17.973486 11.648970 23327 \n", "23327 21.866422 15.116015 23328 \n", "23328 12.134167 8.574169 23329 \n", "23329 13.929985 9.038195 23330 \n", "23330 11.256538 9.921544 23331 \n", "\n", "[23331 rows x 69 columns]" ] }, "execution_count": 85, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df" ] }, { "cell_type": "code", "execution_count": 86, "metadata": {}, "outputs": [], "source": [ "# Get the keys\n", "keys = list(images.keys())\n", "\n", "# Cycle over the keys\n", "for i in range(len(keys)):\n", " # Add the compensated_means to the DataFrame with column names from keys\n", " df[keys[i]] = compensated_means[:, i]" ] }, { "cell_type": "code", "execution_count": 87, "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
Unnamed: 0DAPIFoxP3HLA-DRCD103CHGAEGFRCD206GFAPPD-1...xyeccentricityperimeterconvex_areaareaaxis_major_lengthaxis_minor_lengthlabelsegmentation_channel
0173.4003190.9631450.00000010.7010060.0000004.1744741.7921120.8419418.013893...4.9863951472.2380950.60348544.142136154.0147.015.43963312.31116914.104627
1262.1847760.0000000.0000008.1095331.6521472.6260561.1733890.6135477.322614...5.3595041322.8512400.85389363.248737267.0242.024.68874112.84920122.045059
2395.6571891.1313430.00000015.1475175.4911885.5093423.1373700.87505511.599501...5.7109381506.2265620.76601761.798990268.0256.022.85532214.69187039.495032
3449.6609590.1369860.00684939.62328825.1027402.7979450.9897260.8013708.965753...8.544521641.9383560.64576463.112698306.0292.022.07756316.857044417.931507
45114.6108991.1527590.88852013.45490526.3007205.7848913.9613480.70360016.377783...9.0063291303.7025320.76680048.420310172.0158.017.88862111.482448525.419915
..................................................................
233262332742.1964260.8328061.08636021.06772215.17184922.8107522.5189380.78261010.996428...2523.8888891384.0864200.76153948.627417176.0162.017.97348611.6489702332739.302940
2332723328131.5779190.6817512.11409814.00940427.1143387.1969723.6721310.73118214.524425...2522.0470591438.5686270.72257862.077164274.0255.021.86642215.1160152332821.348579
233282332923.7477780.4088551.12656710.59343212.50965511.7273300.9976540.6614026.655545...2523.9240511349.7594940.70759932.97056386.079.012.1341678.5741692332930.741303
233292333041.3253820.1502340.59361912.79347610.45975514.3492091.0553910.7417303.383721...2525.2346941420.3877550.76093335.556349103.098.013.9299859.0381952333047.744243
233302333111.5227270.1477270.0000001.1704551.2386361.3977270.1363640.8863644.488636...2535.886364231.3295450.47236531.55634989.088.011.2565389.921544233311.886364
\n", "

23331 rows × 70 columns

\n", "
" ], "text/plain": [ " Unnamed: 0 DAPI FoxP3 HLA-DR CD103 CHGA \\\n", "0 1 73.400319 0.963145 0.000000 10.701006 0.000000 \n", "1 2 62.184776 0.000000 0.000000 8.109533 1.652147 \n", "2 3 95.657189 1.131343 0.000000 15.147517 5.491188 \n", "3 4 49.660959 0.136986 0.006849 39.623288 25.102740 \n", "4 5 114.610899 1.152759 0.888520 13.454905 26.300720 \n", "... ... ... ... ... ... ... \n", "23326 23327 42.196426 0.832806 1.086360 21.067722 15.171849 \n", "23327 23328 131.577919 0.681751 2.114098 14.009404 27.114338 \n", "23328 23329 23.747778 0.408855 1.126567 10.593432 12.509655 \n", "23329 23330 41.325382 0.150234 0.593619 12.793476 10.459755 \n", "23330 23331 11.522727 0.147727 0.000000 1.170455 1.238636 \n", "\n", " EGFR CD206 GFAP PD-1 ... x \\\n", "0 4.174474 1.792112 0.841941 8.013893 ... 4.986395 \n", "1 2.626056 1.173389 0.613547 7.322614 ... 5.359504 \n", "2 5.509342 3.137370 0.875055 11.599501 ... 5.710938 \n", "3 2.797945 0.989726 0.801370 8.965753 ... 8.544521 \n", "4 5.784891 3.961348 0.703600 16.377783 ... 9.006329 \n", "... ... ... ... ... ... ... \n", "23326 22.810752 2.518938 0.782610 10.996428 ... 2523.888889 \n", "23327 7.196972 3.672131 0.731182 14.524425 ... 2522.047059 \n", "23328 11.727330 0.997654 0.661402 6.655545 ... 2523.924051 \n", "23329 14.349209 1.055391 0.741730 3.383721 ... 2525.234694 \n", "23330 1.397727 0.136364 0.886364 4.488636 ... 2535.886364 \n", "\n", " y eccentricity perimeter convex_area area \\\n", "0 1472.238095 0.603485 44.142136 154.0 147.0 \n", "1 1322.851240 0.853893 63.248737 267.0 242.0 \n", "2 1506.226562 0.766017 61.798990 268.0 256.0 \n", "3 641.938356 0.645764 63.112698 306.0 292.0 \n", "4 1303.702532 0.766800 48.420310 172.0 158.0 \n", "... ... ... ... ... ... \n", "23326 1384.086420 0.761539 48.627417 176.0 162.0 \n", "23327 1438.568627 0.722578 62.077164 274.0 255.0 \n", "23328 1349.759494 0.707599 32.970563 86.0 79.0 \n", "23329 1420.387755 0.760933 35.556349 103.0 98.0 \n", "23330 231.329545 0.472365 31.556349 89.0 88.0 \n", "\n", " axis_major_length axis_minor_length label segmentation_channel \n", "0 15.439633 12.311169 1 4.104627 \n", "1 24.688741 12.849201 2 2.045059 \n", "2 22.855322 14.691870 3 9.495032 \n", "3 22.077563 16.857044 4 17.931507 \n", "4 17.888621 11.482448 5 25.419915 \n", "... ... ... ... ... \n", "23326 17.973486 11.648970 23327 39.302940 \n", "23327 21.866422 15.116015 23328 21.348579 \n", "23328 12.134167 8.574169 23329 30.741303 \n", "23329 13.929985 9.038195 23330 47.744243 \n", "23330 11.256538 9.921544 23331 1.886364 \n", "\n", "[23331 rows x 70 columns]" ] }, "execution_count": 87, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df" ] }, { "cell_type": "code", "execution_count": 88, "metadata": {}, "outputs": [], "source": [ "# save df to csv\n", "df.to_csv(\"/home/timkempchen/Downloads/tonsil2_mesmer_result_compensated.csv\", index=False)" ] } ], "metadata": { "kernelspec": { "display_name": "spacec_nomod", "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.10.0" } }, "nbformat": 4, "nbformat_minor": 2 }