added catalyst segmentation and hole detection

This commit is contained in:
2023-09-04 11:36:00 +02:00
parent d632979f50
commit f386d846d1
2 changed files with 3297 additions and 0 deletions
+935
View File
@@ -0,0 +1,935 @@
{
"cells": [
{
"cell_type": "markdown",
"id": "7db36e73-5182-45aa-865b-243485563add",
"metadata": {},
"source": [
"# Segmentation of CL at anode to study \"crumbling\""
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "646dd7c8-24e2-4f0b-b516-c4cae70e2682",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import xarray as xr\n",
"import os\n",
"import socket\n",
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"import subprocess\n",
"from skimage import filters\n",
"import dask\n",
"import dask.array\n",
"from dask.distributed import Client, LocalCluster\n",
"\n",
"host = socket.gethostname()\n",
"if host == 'mpc2959.psi.ch':\n",
" gitpath = '/mpc/homes/fische_r/lib/co2ely-tomcat'\n",
" toppath = '/mpc/homes/fische_r/NAS/DASCOELY'\n",
" toppathSSD = '/mnt/SSD/fische_r/COELY'\n",
" temppath = '/mnt/SSD/fische_r/tmp'\n",
" temppath_2 = '/mpc/homes/fische_r/NAS/tmp'\n",
" training_path = '/mpc/homes/fische_r/NAS/DASCOELY/processing/05_water_GDL_ML/'\n",
" memlim = '750GB'\n",
"elif host == 'mpc2053.psi.ch':\n",
" gitpath = '/mpc/homes/fische_r/lib/co2ely-tomcat'\n",
" toppath = '/mpc/homes/fische_r/NAS/DASCOELY'\n",
" toppathSSD = os.path.join(toppath, 'processing')\n",
" temppath = '/mnt/SSD_2TB_nvme0n1/Robert/tmp/'\n",
" temppath_2 = '/mpc/homes/fische_r/NAS/tmp'\n",
" training_path = '/mpc/homes/fische_r/NAS/DASCOELY/processing/05_membrane_ML/'\n",
" memlim = '350GB'\n",
"else:\n",
" print('host '+host+' currently not supported')\n",
" \n",
"path_02_4D = os.path.join(toppathSSD, '02_registered_3p1D') #h5 with registered data\n",
"if not host=='mpc2959.psi.ch':\n",
" path_02_4D = os.path.join(toppath, 'processing','02_registered_3p1D') #h5 with registered data\n",
"\n",
"# fetch githash\n",
"cwd = os.getcwd()\n",
"os.chdir(gitpath)\n",
"git_sha = subprocess.check_output(['git', 'rev-parse', '--short', 'HEAD']).decode().strip()\n",
"githash = subprocess.check_output(['git', 'rev-parse', 'HEAD']).decode().strip()\n",
"os.chdir(cwd)"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "287e9fe3-d2bb-4e24-ae20-6dd72bd8e18a",
"metadata": {},
"outputs": [],
"source": [
"tempfolder = temppath #a big SSD is a major adavantage to allow spill to disk and still be efficient. large dataset might crash with too small SSD or be slow with normal HDD\n",
"# tempfolder = temppath_2\n",
"dask.config.config['temporary-directory'] = tempfolder\n",
"# dask.config.config['distributed']['worker']['memory']['recent-to-old-time'] = '200000s'\n",
"\n",
"# here you have the option to use a virtual cluster or even slurm on ra (not attempted yet)\n",
"# cluster = LocalCluster(dashboard_address=':35000', memory_limit = memlim, n_workers=1) #settings optimised for mpc2959 and mpc2953, play around if needed, if you know nothing else is using RAM then you can almost go to the limit\n",
"# maybe less workers with more threads makes better use of shared memory \n",
"\n",
"# scheduler_port = 'tcp://129.129.188.222:8786' #<-- if scheduler on mpc2959; scheduler on mpc2053 -> 'tcp://129.129.188.248:8786'\n",
"# cluster = scheduler_port\n",
"\n",
"# client = Client(cluster)\n",
"# client.amm.start()\n",
"# print('Dashboard at '+client.dashboard_link)"
]
},
{
"cell_type": "markdown",
"id": "43329138-4421-4057-a1e3-821c8e1653c4",
"metadata": {},
"source": [
"## Data preparation\n",
"### Use ImageJ to define ROI in registered 4D-Data including Anode CL+GDL and save in dict"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "e0d6d5af-29d9-48a5-aa9d-4b488ebbe81c",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"352 419\n"
]
}
],
"source": [
"a = 352\n",
"b = a+67\n",
"print(a,b)"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "ef30708a-960c-401b-9ced-df6ee07f5883",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"anode_ROI = {\n",
" '5II': (332, 451),\n",
" '1': (313,380),\n",
" '3III': (344, 344+105),\n",
" '3II': (321, 321+106),\n",
" '4II': (323, 323+104),\n",
" '4': (342, 352+102),\n",
" '5': (328, 328+100),\n",
" '6': (344, 344+102),\n",
" '7x': (328, 328+110),\n",
" '8x': (338, 338+108)\n",
"\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "6eae57c6-8624-432f-a3ea-506584dcbb77",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"th = 15000\n",
"thmax = 3*th #to exlude registration artifacts"
]
},
{
"cell_type": "markdown",
"id": "9114f060-dad6-4442-b8d5-12b8ac1276e8",
"metadata": {},
"source": [
"## Sample processing"
]
},
{
"cell_type": "markdown",
"id": "51432875-7c65-41ab-ae0c-ae285caa1137",
"metadata": {},
"source": [
"### load ROI of selected sample"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "242cc850-473c-43ab-975f-359cfe9673b1",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n"
]
}
],
"source": [
"i = 1\n",
"# sample = '8x'\n",
"sample = list(anode_ROI.keys())[i]\n",
"print(sample)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "584c1acf-c369-4283-a2bc-b6b3b5a79572",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"sample = '8x'"
]
},
{
"cell_type": "code",
"execution_count": 8,
"id": "d0b8f31d-d726-4966-a252-920168557394",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"file = '02_'+sample+'_registered_3p1D.nc'\n",
"imagepath = os.path.join(path_02_4D, file)\n",
"data = xr.open_dataset(imagepath)\n",
"images = [im for im in data.keys() if im[3:7] == 'imag']\n",
"images.sort()"
]
},
{
"cell_type": "code",
"execution_count": 9,
"id": "4e3d7dda-9ab6-4df0-bea8-59803a8d5ff0",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"a,b = anode_ROI[sample]\n",
"shp = data[images[4]][ :,a:b, :].shape\n",
"shp = shp + (len(images),)"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "bb8d7578-6ce2-49de-bac9-fdb3cdf5bcc8",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(750, 108, 2016, 53)"
]
},
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"shp"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "702886e9-0c45-4ed0-a89d-53917146a230",
"metadata": {},
"outputs": [],
"source": [
"data.close()"
]
},
{
"cell_type": "code",
"execution_count": 10,
"id": "b3a87934-5c07-46ab-8ef7-64f578d642d4",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"im = np.zeros(shp, dtype = np.uint16)"
]
},
{
"cell_type": "code",
"execution_count": 11,
"id": "1193c93b-5e12-4de9-9a13-a74cde5d0493",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"10\n",
"20\n",
"30\n",
"40\n",
"50\n"
]
}
],
"source": [
"for i in range(shp[-1]):\n",
" if i%10==0:\n",
" print(i)\n",
" im[...,i] = data[images[i]][ :,a:b, :].data\n",
"\n",
"data.close()"
]
},
{
"cell_type": "code",
"execution_count": 12,
"id": "fe98d2ef-8646-4c77-89e2-66249e63b62b",
"metadata": {},
"outputs": [],
"source": [
"da = dask.array.from_array(im, chunks='auto')"
]
},
{
"cell_type": "code",
"execution_count": 13,
"id": "519f05c4-2013-43fa-b7ab-cdbf56a914c0",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<table>\n",
" <tr>\n",
" <td>\n",
" <table style=\"border-collapse: collapse;\">\n",
" <thead>\n",
" <tr>\n",
" <td> </td>\n",
" <th> Array </th>\n",
" <th> Chunk </th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" \n",
" <tr>\n",
" <th> Bytes </th>\n",
" <td> 16.12 GiB </td>\n",
" <td> 127.34 MiB </td>\n",
" </tr>\n",
" \n",
" <tr>\n",
" <th> Shape </th>\n",
" <td> (750, 108, 2016, 53) </td>\n",
" <td> (108, 108, 108, 53) </td>\n",
" </tr>\n",
" <tr>\n",
" <th> Dask graph </th>\n",
" <td colspan=\"2\"> 133 chunks in 1 graph layer </td>\n",
" </tr>\n",
" <tr>\n",
" <th> Data type </th>\n",
" <td colspan=\"2\"> uint16 numpy.ndarray </td>\n",
" </tr>\n",
" </tbody>\n",
" </table>\n",
" </td>\n",
" <td>\n",
" <svg width=\"329\" height=\"190\" style=\"stroke:rgb(0,0,0);stroke-width:1\" >\n",
"\n",
" <!-- Horizontal lines -->\n",
" <line x1=\"0\" y1=\"0\" x2=\"44\" y2=\"0\" style=\"stroke-width:2\" />\n",
" <line x1=\"0\" y1=\"25\" x2=\"44\" y2=\"25\" style=\"stroke-width:2\" />\n",
"\n",
" <!-- Vertical lines -->\n",
" <line x1=\"0\" y1=\"0\" x2=\"0\" y2=\"25\" style=\"stroke-width:2\" />\n",
" <line x1=\"6\" y1=\"0\" x2=\"6\" y2=\"25\" />\n",
" <line x1=\"12\" y1=\"0\" x2=\"12\" y2=\"25\" />\n",
" <line x1=\"19\" y1=\"0\" x2=\"19\" y2=\"25\" />\n",
" <line x1=\"25\" y1=\"0\" x2=\"25\" y2=\"25\" />\n",
" <line x1=\"32\" y1=\"0\" x2=\"32\" y2=\"25\" />\n",
" <line x1=\"38\" y1=\"0\" x2=\"38\" y2=\"25\" />\n",
" <line x1=\"44\" y1=\"0\" x2=\"44\" y2=\"25\" style=\"stroke-width:2\" />\n",
"\n",
" <!-- Colored Rectangle -->\n",
" <polygon points=\"0.0,0.0 44.64285714285714,0.0 44.64285714285714,25.412616514582485 0.0,25.412616514582485\" style=\"fill:#ECB172A0;stroke-width:0\"/>\n",
"\n",
" <!-- Text -->\n",
" <text x=\"22.321429\" y=\"45.412617\" font-size=\"1.0rem\" font-weight=\"100\" text-anchor=\"middle\" >750</text>\n",
" <text x=\"64.642857\" y=\"12.706308\" font-size=\"1.0rem\" font-weight=\"100\" text-anchor=\"middle\" transform=\"rotate(0,64.642857,12.706308)\">1</text>\n",
"\n",
"\n",
" <!-- Horizontal lines -->\n",
" <line x1=\"114\" y1=\"0\" x2=\"134\" y2=\"20\" style=\"stroke-width:2\" />\n",
" <line x1=\"114\" y1=\"6\" x2=\"134\" y2=\"26\" />\n",
" <line x1=\"114\" y1=\"12\" x2=\"134\" y2=\"33\" />\n",
" <line x1=\"114\" y1=\"19\" x2=\"134\" y2=\"39\" />\n",
" <line x1=\"114\" y1=\"25\" x2=\"134\" y2=\"46\" />\n",
" <line x1=\"114\" y1=\"32\" x2=\"134\" y2=\"52\" />\n",
" <line x1=\"114\" y1=\"38\" x2=\"134\" y2=\"59\" />\n",
" <line x1=\"114\" y1=\"45\" x2=\"134\" y2=\"65\" />\n",
" <line x1=\"114\" y1=\"51\" x2=\"134\" y2=\"71\" />\n",
" <line x1=\"114\" y1=\"57\" x2=\"134\" y2=\"78\" />\n",
" <line x1=\"114\" y1=\"64\" x2=\"134\" y2=\"84\" />\n",
" <line x1=\"114\" y1=\"70\" x2=\"134\" y2=\"91\" />\n",
" <line x1=\"114\" y1=\"77\" x2=\"134\" y2=\"97\" />\n",
" <line x1=\"114\" y1=\"83\" x2=\"134\" y2=\"104\" />\n",
" <line x1=\"114\" y1=\"90\" x2=\"134\" y2=\"110\" />\n",
" <line x1=\"114\" y1=\"96\" x2=\"134\" y2=\"116\" />\n",
" <line x1=\"114\" y1=\"102\" x2=\"134\" y2=\"123\" />\n",
" <line x1=\"114\" y1=\"109\" x2=\"134\" y2=\"129\" />\n",
" <line x1=\"114\" y1=\"115\" x2=\"134\" y2=\"136\" />\n",
" <line x1=\"114\" y1=\"120\" x2=\"134\" y2=\"140\" style=\"stroke-width:2\" />\n",
"\n",
" <!-- Vertical lines -->\n",
" <line x1=\"114\" y1=\"0\" x2=\"114\" y2=\"120\" style=\"stroke-width:2\" />\n",
" <line x1=\"134\" y1=\"20\" x2=\"134\" y2=\"140\" style=\"stroke-width:2\" />\n",
"\n",
" <!-- Colored Rectangle -->\n",
" <polygon points=\"114.0,0.0 134.54291417495367,20.542914174953655 134.54291417495367,140.54291417495367 114.0,120.0\" style=\"fill:#ECB172A0;stroke-width:0\"/>\n",
"\n",
" <!-- Horizontal lines -->\n",
" <line x1=\"114\" y1=\"0\" x2=\"144\" y2=\"0\" style=\"stroke-width:2\" />\n",
" <line x1=\"134\" y1=\"20\" x2=\"165\" y2=\"20\" style=\"stroke-width:2\" />\n",
"\n",
" <!-- Vertical lines -->\n",
" <line x1=\"114\" y1=\"0\" x2=\"134\" y2=\"20\" style=\"stroke-width:2\" />\n",
" <line x1=\"144\" y1=\"0\" x2=\"165\" y2=\"20\" style=\"stroke-width:2\" />\n",
"\n",
" <!-- Colored Rectangle -->\n",
" <polygon points=\"114.0,0.0 144.60647009662728,0.0 165.14938427158094,20.542914174953655 134.54291417495367,20.542914174953655\" style=\"fill:#ECB172A0;stroke-width:0\"/>\n",
"\n",
" <!-- Horizontal lines -->\n",
" <line x1=\"134\" y1=\"20\" x2=\"165\" y2=\"20\" style=\"stroke-width:2\" />\n",
" <line x1=\"134\" y1=\"26\" x2=\"165\" y2=\"26\" />\n",
" <line x1=\"134\" y1=\"33\" x2=\"165\" y2=\"33\" />\n",
" <line x1=\"134\" y1=\"39\" x2=\"165\" y2=\"39\" />\n",
" <line x1=\"134\" y1=\"46\" x2=\"165\" y2=\"46\" />\n",
" <line x1=\"134\" y1=\"52\" x2=\"165\" y2=\"52\" />\n",
" <line x1=\"134\" y1=\"59\" x2=\"165\" y2=\"59\" />\n",
" <line x1=\"134\" y1=\"65\" x2=\"165\" y2=\"65\" />\n",
" <line x1=\"134\" y1=\"71\" x2=\"165\" y2=\"71\" />\n",
" <line x1=\"134\" y1=\"78\" x2=\"165\" y2=\"78\" />\n",
" <line x1=\"134\" y1=\"84\" x2=\"165\" y2=\"84\" />\n",
" <line x1=\"134\" y1=\"91\" x2=\"165\" y2=\"91\" />\n",
" <line x1=\"134\" y1=\"97\" x2=\"165\" y2=\"97\" />\n",
" <line x1=\"134\" y1=\"104\" x2=\"165\" y2=\"104\" />\n",
" <line x1=\"134\" y1=\"110\" x2=\"165\" y2=\"110\" />\n",
" <line x1=\"134\" y1=\"116\" x2=\"165\" y2=\"116\" />\n",
" <line x1=\"134\" y1=\"123\" x2=\"165\" y2=\"123\" />\n",
" <line x1=\"134\" y1=\"129\" x2=\"165\" y2=\"129\" />\n",
" <line x1=\"134\" y1=\"136\" x2=\"165\" y2=\"136\" />\n",
" <line x1=\"134\" y1=\"140\" x2=\"165\" y2=\"140\" style=\"stroke-width:2\" />\n",
"\n",
" <!-- Vertical lines -->\n",
" <line x1=\"134\" y1=\"20\" x2=\"134\" y2=\"140\" style=\"stroke-width:2\" />\n",
" <line x1=\"165\" y1=\"20\" x2=\"165\" y2=\"140\" style=\"stroke-width:2\" />\n",
"\n",
" <!-- Colored Rectangle -->\n",
" <polygon points=\"134.54291417495364,20.542914174953655 165.1493842715809,20.542914174953655 165.1493842715809,140.54291417495367 134.54291417495364,140.54291417495367\" style=\"fill:#ECB172A0;stroke-width:0\"/>\n",
"\n",
" <!-- Text -->\n",
" <text x=\"149.846149\" y=\"160.542914\" font-size=\"1.0rem\" font-weight=\"100\" text-anchor=\"middle\" >53</text>\n",
" <text x=\"185.149384\" y=\"80.542914\" font-size=\"1.0rem\" font-weight=\"100\" text-anchor=\"middle\" transform=\"rotate(-90,185.149384,80.542914)\">2016</text>\n",
" <text x=\"114.271457\" y=\"150.271457\" font-size=\"1.0rem\" font-weight=\"100\" text-anchor=\"middle\" transform=\"rotate(45,114.271457,150.271457)\">108</text>\n",
"</svg>\n",
" </td>\n",
" </tr>\n",
"</table>"
],
"text/plain": [
"dask.array<array, shape=(750, 108, 2016, 53), dtype=uint16, chunksize=(108, 108, 108, 53), chunktype=numpy.ndarray>"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"da"
]
},
{
"cell_type": "markdown",
"id": "35f95ca1-bf3d-418b-91c8-a23a547cbc89",
"metadata": {},
"source": [
"### 3D Gaussian Blur sigma=1"
]
},
{
"cell_type": "code",
"execution_count": 14,
"id": "ee84d2b9-bb88-499c-89b4-271853233621",
"metadata": {
"scrolled": true,
"tags": []
},
"outputs": [],
"source": [
"def Gaussian_Blur_space(da, sigma): \n",
" deptharray = np.ones(da.ndim)+4*sigma\n",
" deptharray[-1] = 0\n",
" sigmas = np.ones(deptharray.shape)*sigma\n",
" deptharray = tuple(np.min([deptharray, da.shape], axis=0))\n",
"\n",
" sigmas[-1] = 0\n",
" G = da.map_overlap(filters.gaussian, depth=deptharray, boundary='nearest', sigma = sigmas, preserve_range=True)\n",
" return G"
]
},
{
"cell_type": "code",
"execution_count": 15,
"id": "74b1a23b-86e8-433e-b824-212d93162214",
"metadata": {},
"outputs": [],
"source": [
"da[da>thmax] = 0"
]
},
{
"cell_type": "code",
"execution_count": 16,
"id": "ca4dbf20-53b3-4763-b788-2bb8fb575b4f",
"metadata": {},
"outputs": [],
"source": [
"da = Gaussian_Blur_space(da, 1)"
]
},
{
"cell_type": "code",
"execution_count": 17,
"id": "33106fb6-30b8-41eb-9d6c-0d93ef90cd40",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"im = da.compute()"
]
},
{
"cell_type": "markdown",
"id": "09daf1cd-8504-4182-8abf-9f7172f6f6d1",
"metadata": {},
"source": [
"### segment data"
]
},
{
"cell_type": "code",
"execution_count": 19,
"id": "74d0b28e-e141-40a2-a925-35270eb51df8",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# segs = np.bitwise_and(im>th, im<thmax).astype(np.uint8)\n",
"segs = im>th"
]
},
{
"cell_type": "markdown",
"id": "afd970fb-76a6-48ef-96da-95e42e271d42",
"metadata": {},
"source": [
"### save to netcdf4 to be fed in Blender pipeline"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "e352cd15-dfd4-4fbd-b19b-ce6fc1a28e4b",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"outpath = os.path.join(toppath, 'processing','06_anode_CL', ''.join(['06_'+sample,'_anode_CL_segmented_filtered.nc']))"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "06ddabee-2c0a-4431-bb71-249e5a09a2d8",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"shp = segs.shape\n",
"segdata = xr.Dataset({'segmented': (['x','y','z','time'], segs)},\n",
" coords = {'x': np.arange(shp[0]),\n",
" 'y': np.arange(shp[1]),\n",
" 'z': np.arange(shp[2]),\n",
" 'time': np.arange(shp[3])},\n",
" attrs = {'name': sample,\n",
" 'cropping': [a,b]}\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "cea904b8-0b21-4f1c-9bd4-af2876c35058",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"segdata.to_netcdf(outpath)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "9fe1b37c-4cd9-4eeb-b072-e578777df8d1",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"'/mpc/homes/fische_r/NAS/DASCOELY/processing/06_anode_CL/06_8x_anode_CL_segmented_filtered.nc'"
]
},
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"outpath"
]
},
{
"cell_type": "markdown",
"id": "b53c01ac-4b13-4b0b-a02b-f3742ec8f96a",
"metadata": {},
"source": [
"## repeat for cathode as reference"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "503ceb11-13ac-4f83-9ea8-c35ecebd0331",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"284 339\n"
]
}
],
"source": [
"a = 284\n",
"b = a+55\n",
"print(a,b)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "72de8465-fa43-48e7-81d6-17da3eab2f9c",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"cathode_ROI = {\n",
" '5II': (262,338),\n",
" '1': (284,339),\n",
" '3III': (309, 309+39),\n",
" '3II': (277, 277+43),\n",
" '4II': (281, 281+44),\n",
" '4': (306, 306+46),\n",
" '5': (284, 284+46),\n",
" '6': (304, 304+46),\n",
" '7x': (284, 284+52),\n",
" '8x': (288, 288+52)\n",
" \n",
"}"
]
},
{
"cell_type": "code",
"execution_count": 22,
"id": "9a41ef95-ece5-4d5f-852b-dcce8fe53911",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"th = 15000\n",
"thmax = 3*th #to exlude registration artifacts"
]
},
{
"cell_type": "markdown",
"id": "cc2005e8-ee7a-4af2-a41f-46fb767226cf",
"metadata": {},
"source": [
"### load ROI of selected sample"
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "c0108fa9-9583-4a20-8bcb-8c2e79b0b01d",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"file = '02_'+sample+'_registered_3p1D.nc'\n",
"imagepath = os.path.join(path_02_4D, file)\n",
"data = xr.open_dataset(imagepath)\n",
"images = [im for im in data.keys() if im[3:7] == 'imag']\n",
"images.sort()"
]
},
{
"cell_type": "code",
"execution_count": 24,
"id": "8cc2cbc3-f709-43c1-9933-a8f6216e39a1",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"a,b = cathode_ROI[sample]\n",
"shp = data[images[4]][ :,a:b, :].shape\n",
"shp = shp + (len(images),)"
]
},
{
"cell_type": "code",
"execution_count": 25,
"id": "44554dce-d0ae-4316-b2fe-4a7fe479c204",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"im = np.zeros(shp, dtype = np.uint16)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"id": "c6ccddd2-ccd5-4490-a166-e4bd9f7159bb",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0\n",
"10\n",
"20\n",
"30\n",
"40\n",
"50\n",
"60\n",
"70\n",
"80\n"
]
}
],
"source": [
"for i in range(shp[-1]):\n",
" if i%10==0:\n",
" print(i)\n",
" im[...,i] = data[images[i]][ :,a:b, :].data"
]
},
{
"cell_type": "code",
"execution_count": 27,
"id": "3c1fe90a-54af-4b47-9b6a-42007c21aa50",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"data.close()"
]
},
{
"cell_type": "markdown",
"id": "ee68f981-9d63-4a54-ab04-7b1e0e05f6cd",
"metadata": {},
"source": [
"### segment data"
]
},
{
"cell_type": "code",
"execution_count": 28,
"id": "3f8b2568-8a71-49dc-aaf6-e7db4e43a21d",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"segs = np.bitwise_and(im>th, im<thmax).astype(np.uint8)"
]
},
{
"cell_type": "markdown",
"id": "82f615e6-d46b-44fa-a33c-74fbf90a4ffa",
"metadata": {},
"source": [
"### save to netcdf4 to be fed in Blender pipeline"
]
},
{
"cell_type": "code",
"execution_count": 29,
"id": "3416d6c0-5867-4258-9e86-b123715d5224",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"outpath = os.path.join(toppath, 'processing','06b_cathode_CL', ''.join(['06b_'+sample,'_cathode_CL_segmented.nc']))"
]
},
{
"cell_type": "code",
"execution_count": 30,
"id": "12112ec8-7d92-4f52-aae6-c7f09de00aa7",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"shp = segs.shape\n",
"segdata = xr.Dataset({'segmented': (['x','y','z','time'], segs)},\n",
" coords = {'x': np.arange(shp[0]),\n",
" 'y': np.arange(shp[1]),\n",
" 'z': np.arange(shp[2]),\n",
" 'time': np.arange(shp[3])},\n",
" attrs = {'name': sample,\n",
" 'cropping': [a,b]}\n",
" )"
]
},
{
"cell_type": "code",
"execution_count": 31,
"id": "6f263468-f366-48b0-9457-79bb95ba756a",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"segdata.to_netcdf(outpath)"
]
},
{
"cell_type": "code",
"execution_count": 150,
"id": "92af6892-253a-4fc8-9edf-bbef8c14b879",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"'/mpc/homes/fische_r/NAS/DASCOELY/processing/06b_cathode_CL/06b_8x_cathode_CL_segmented.nc'"
]
},
"execution_count": 150,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"outpath"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d73a6fc3-099a-4ee2-bd5c-cf41efd15299",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "cca837b1-1d6d-4c1f-aa24-95da9a6a36d4",
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"id": "d3034f7d-0fa1-42b5-a238-27af5a6cb238",
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
File diff suppressed because one or more lines are too long