In [ ]:
%run ./msgc_experiments_ct_init.ipynb
# the other imports are not necessary
In [1]:
%pylab inline
import sys
import os.path as op
from pathlib import Path
import shutil
# sys.path.insert(0, "/home/mjirik/projects/pyseg_base/")
sys.path.insert(0, op.abspath("../"))
import scipy
import time
import pandas as pd
from imcut import pycut
import sed3
import itertools
import data2tex as dtt
import io3d
latex_dir = Path("../../papers/cmbbeiv19/tmp/")
dtt.set_output(latex_dir)
# sh 155,160, r10, dpoff 3, seeds 3
# dp_ircad_id = [1, 11]
# dp_ircad_id = [1, 5, 6, 7]
dp_ircad_id = [1, 5, 6, 7, 11, 20]
# dp_ircad_id = [11, 20]
# dp_ircad_id = [1]
dp_keys = ["left_kidney"]
dp_ircad_id_key = [
# [1, "left_kidney"],
# [5, "left_kidney"],
# [6, "left_kidney"],
# [7, "left_kidney"],
# [11, "left_kidney"],
# [20, "left_kidney"],
[3, "right_kidney"],
]
working_voxelsize_mm = None
# working_voxelsize_mm = [1.5, 1.5, 1.5]
working_voxelsize_mm = [1.3, 1.3, 1.3]
# working_voxelsize_mm = [1.7, 1.7, 1.7]
# working_voxelsize_mm = "orig*2"
# working_voxelsize_mm=[2, 2, 2]
# working_voxelsize_mm=[2.2, 2.5, 2.5]
fname = "exp062-multiscale_delme.csv"
fnamenew = "msgc_experiment_ct.csv"
rnd_seed=1
dpi = 400
In [ ]:
In [2]:
# dry_run = True
dry_run = False
force_rewrite = False
# force_rewrite = True
In [3]:
%pwd
Out[3]:
In [4]:
# block size bylo 10
segparams0 = {
'method':'graphcut',
# 'method':'multiscale_graphcut',
'use_boundary_penalties': True,
'boundary_dilatation_distance': 2,
'boundary_penalties_weight': 1,
'block_size': 10,
'tile_zoom_constant': 1
}
segparams1 = {
# 'method':'graphcut',
'method':'multiscale_graphcut_hi2lo',
'use_boundary_penalties': True,
'boundary_dilatation_distance': 2,
'boundary_penalties_weight': 1,
'block_size': 10,
'tile_zoom_constant': 1
}
segparams2 = {
# 'method':'graphcut',
'method':'multiscale_graphcut_lo2hi',
'use_boundary_penalties': True,
'boundary_dilatation_distance': 2,
'boundary_penalties_weight': 1,
'block_size': 10,
'tile_zoom_constant': 1
}
labels = [
"ssgc ",
"msgc_hi2lo ",
"msgc_lo2hi ",
]
In [5]:
data_seeds_path = Path(io3d.datasets.join_path("medical", "orig", "ircad1b_seeds", get_root=True))
d01_pth = data_seeds_path / "ircadb1-01.pklz"
datap = io3d.read(d01_pth)
datap
str(d01_pth)
datap.keys()
Out[5]:
In [6]:
# io3d.write(datap, data_seeds_path / "ircad1b01.hdf5")
# io3d.read(data_seeds_path / "ircad1b01.hdf5")
In [7]:
# datap['saved_seeds']["left_kidney"]
In [8]:
# pth_data3d = Path(io3d.datasets.join_path("medical", "orig", "3Dircadb1.{}", "PATIENT_DICOM", get_root=True))
# pth_ground_true = Path(io3d.datasets.join_path("medical", "orig", "3Dircadb1.{}", "MASKS_DICOM", "{}" get_root=True))
# pth_seeds = Path(io3d.datasets.join_path("medical", "orig", "ircad1b_seeds", "ircad1b{:02d}.pklz", get_root=True))
# print(pth_data3d)
# print(pth_seeds)
In [9]:
import imma
help(imma.image_manipulation.resize_to_mm)
In [10]:
def prepare_data(i, seeds_key):
ground_true_key = seeds_key.replace("_", "")
pth_data3d = Path(io3d.datasets.join_path("medical", "orig", "3Dircadb1.{}", "PATIENT_DICOM", get_root=True))
pth_ground_true = Path(io3d.datasets.join_path("medical", "orig", "3Dircadb1.{}", "MASKS_DICOM", "{}", get_root=True))
pth_seeds = Path(io3d.datasets.join_path("medical", "orig", "ircad1b_seeds", "ircadb1-{:02d}.pklz", get_root=True))
datap_data3d = io3d.read(str(pth_data3d).format(i))
datap_seeds = io3d.read(str(pth_seeds).format(i))
datap_ground_true = io3d.read(str(pth_ground_true).format(i, ground_true_key))
seeds = datap_seeds["saved_seeds"][seeds_key]
data3d = datap_data3d["data3d"]
seg_true = datap_ground_true["data3d"]
vs = datap_data3d["voxelsize_mm"]
if working_voxelsize_mm is not None:
if working_voxelsize_mm == "orig*2":
wvs = np.asarray(vs) * 2
else:
wvs = working_voxelsize_mm
data3d = imma.image_manipulation.resize_to_mm(data3d, vs, wvs)
seg_true = imma.image_manipulation.resize_to_mm(seg_true, vs, wvs, order=0)
seeds = imma.image_manipulation.resize_to_mm(seeds, vs, wvs, order=0)
return data3d, seg_true, seeds, wvs, vs
In [11]:
# [0.3, 5.5] * 2.
In [12]:
matplotlib.__version__
Out[12]:
In [13]:
data3d, true_segmentation, seeds, voxelsize_mm, orig_vs_mm = prepare_data(1, "left_kidney")
plt.figure(figsize=(10,8))
# sed3.show_slices(data3d, seeds=seeds, contour=true_segmentation, shape=[3, 4])
plt.axis("off")
sed3.show_slices(
data3d,
# seeds=seeds,
contour=(true_segmentation > 0).astype(np.int) * 2,
shape=[4, 5]),
# show=False
Out[13]:
In [14]:
# help(sed3.show_slices)
In [15]:
datap["processing_information"]["organ_segmentation"].keys()
Out[15]:
In [16]:
img, seg, seeds, voxelsize_mm, orig_vs_mm = prepare_data(1, "left_kidney")
# plt.figure(figsize=(10,15))
plt.figure()
import copy
imgview = copy.copy(img)
imgview[:,:,-1] = 0
imgview[:,-1,:] = 0
imgview[-1,:,:] = 0
# sed3.show_slices(imgview, contour=seg, seeds=seeds, show=False, slice_step=16.9, first_slice_offset_to_see_seed_with_label=2)
sed3.show_slices(imgview,
contour=(true_segmentation > 0).astype(np.int) * 3,
show=False, shape=[3,3],
# first_slice_offset_to_see_seed_with_label=2
)
plt.axis("off")
# plt.savefig("../graphics/msgc-ct-imgsample_separated.png")
plt.savefig(
latex_dir / "ircadb01_slices_uncropped.png",
dpi=dpi, bbox_inches='tight')
In [17]:
np.unique(seeds)
Out[17]:
In [18]:
def to_latex_file(df, fn):
with open(fn, "w") as f:
f.write(df.to_latex())
def latex_float(f, precision=4):
float_str = "{0:." + str(int(precision)) + "g}"
float_str = float_str.format(f)
if "e" in float_str:
base, exponent = float_str.split("e")
return r"{0} \times 10^{{{1}}}".format(base, int(exponent))
else:
return float_str
def float_to_latex_file(fl, fn, precision=4):
string = latex_float(fl, precision=precision)
with open(fn, "w") as f:
f.write(string)
def num2latex(num, filename=None, precision=4):
if type(num) is str:
float_str = num
else:
float_str = "{0:." + str(int(precision)) + "g}"
float_str = float_str.format(num)
if float_str[:4] == r"\num":
pass
else:
float_str = "\\num{" + float_str + "}"
if filename is not None:
with open(filename, "w") as f:
f.write(float_str)
return float_str
def to_file(text, fn):
with open(fn, "w") as f:
f.write(text)
In [19]:
def process_gc_stats(stats1, prefix=None):
if prefix is None:
prefix = ""
outstats = {}
for key in stats1:
outstats[prefix + key] = stats1[key]
outstats[prefix + "nlinks number"] = np.sum(np.asarray(outstats[prefix + "nlinks shape"]), axis=0)[0]
outstats[prefix + "tlinks number"] = np.sum(np.asarray(outstats[prefix + "tlinks shape"]), axis=0)[0]
outstats.pop(prefix + "tlinks shape")
outstats.pop(prefix + "nlinks shape")
outstats[prefix + "edge number"] = outstats[prefix + "nlinks number"] + outstats[prefix + "tlinks number"]
return outstats
def merge_stats(stats0, stats1, stats2, labels=None):
if labels is None:
labels = [""] * 3
stats0 = process_gc_stats(stats0, labels[0])
stats1 = process_gc_stats(stats1, labels[1])
stats2 = process_gc_stats(stats2, labels[2])
stats = {}
stats.update(stats0)
stats.update(stats1)
stats.update(stats2)
return stats
def run_gc_with_defined_setup(img, segparams, seeds, true_seg, dry_run=False):
start = time.time()
gc = pycut.ImageGraphCut(img, segparams=segparams)
gc.set_seeds(seeds)
if dry_run:
thr = np.mean([np.min(img), np.max(img)])
sg1 = (img < thr).astype(np.uint8)
stats1 = {"nlinks shape": [[5, 5]], "tlinks shape": [[5, 5]]}
else:
gc.run()
sg1 = gc.segmentation
stats1 = gc.stats
elapsed1 = (time.time() - start)
err1 = np.sum(np.abs(true_seg - (1 - sg1)))
stats1["time"] = elapsed1
stats1["error"] = err1
stats1["data segmentation size px"] = np.sum(sg1 > 0)
return stats1
def add_data_and_algoritm_info(stats, data_params_dict, segparams, start, true_segmentation, voxelsize_mm, orig_vs_mm):
# stats['msgc time'] = elapsed1
# stats['normal time'] = elapsed2
# stats['data id'] = data_params[0]
# stats['data offset'] = data_params[1]
# stats['target organ'] = data_params[1]
# stats['data radius'] = data_params[2]
# stats['data size'] = data_params[0]
stats.update(data_params_dict)
stats["data size 0"] = true_segmentation.shape[0]
stats["data size 1"] = true_segmentation.shape[1]
stats["data size 2"] = true_segmentation.shape[2]
stats["data size px"] = np.prod(true_segmentation.shape)
stats["data target size px"] = np.sum(true_segmentation > 0)
stats["data voxelsize mm^3"] = np.prod(voxelsize_mm)
stats["data voxelsize mm 0"] = voxelsize_mm[0]
stats["data voxelsize mm 1"] = voxelsize_mm[1]
stats["data voxelsize mm 2"] = voxelsize_mm[2]
stats["data orig voxelsize mm 0"] = orig_vs_mm[0]
stats["data orig voxelsize mm 1"] = orig_vs_mm[1]
stats["data orig voxelsize mm 2"] = orig_vs_mm[2]
stats["block size"] = segparams["block_size"]
# stats["data seedsz"] = data_params[3]
# stats["GC error"] = err2
# stats["MSGC error"] = err1
stats['machine hostname'] = machine_hostname
stats['experiment iteration start time'] = start
return stats
def add_data_seaborn(stats, data_params_dict, segparams, start, i, label, true_segmentation, voxelsize_mm, orig_vs_mm):
stats = process_gc_stats(stats, "")
stats = add_data_and_algoritm_info(stats, data_params_dict, segparams, start, true_segmentation, voxelsize_mm, orig_vs_mm)
stats["method"] = label
dfinew = pd.DataFrame(stats, index=[i*3 + 0])
#dfnew = dfnew.append(dfinew, sort=True)
return dfinew
In [20]:
#for par in it:
# print par
i = 0
In [21]:
if op.exists(fname) and not force_rewrite:
df = pd.read_csv(fname)#, index_col=0)
else:
df = pd.DataFrame([])
if op.exists(fnamenew) and not force_rewrite:
dfnew = pd.read_csv(fnamenew)#, index_col=0)
else:
dfnew = pd.DataFrame([])
i = 0
np.random.seed(rnd_seed)
import platform
machine_hostname = platform.node()
# it = itertools.product(dp_ircad_id, dp_keys)
it = itertools.product(dp_ircad_id_key)
for data_params in it:
# dp_ircad_id = [1, 10, 11]
# dp_keys = ["left_kidney"]
start = time.time()
img, true_seg, seeds, voxelsize_mm, orig_vs_mm = prepare_data(data_params[0], data_params[1])
stats0 = run_gc_with_defined_setup(img, segparams0, seeds, true_seg, dry_run=dry_run)
stats1 = run_gc_with_defined_setup(img, segparams1, seeds, true_seg, dry_run=dry_run)
stats2 = run_gc_with_defined_setup(img, segparams2, seeds, true_seg, dry_run=dry_run)
data_params_dict = dict(zip(["data id", "data organ key"], data_params))
# stats = merge_stats(stats0, stats1, stats2, labels)
# stats = add_data_and_algoritm_info(stats, data_params_dict, segparams0, start, seg, voxelsize_mm)
# dfi = pd.DataFrame(stats, index=[i])
# # display(df)
# df = df.append(dfi, sort=True)
# df.to_csv(fname, index=False)
dfinew = add_data_seaborn(stats0, data_params_dict, segparams0, start, i, labels[0], true_seg, voxelsize_mm, orig_vs_mm)
dfnew = dfnew.append(dfinew, sort=True)
dfinew = add_data_seaborn(stats1, data_params_dict, segparams1, start, i, labels[1], true_seg, voxelsize_mm, orig_vs_mm)
dfnew = dfnew.append(dfinew, sort=True)
dfinew = add_data_seaborn(stats2, data_params_dict, segparams2, start, i, labels[2], true_seg, voxelsize_mm, orig_vs_mm)
dfnew = dfnew.append(dfinew, sort=True)
dfnew.to_csv(fnamenew, index=False)
i += 1
# plt.figure(figsize=[10,15])
# sed3.show_slices(img, contour=sg1, seeds=seeds, slice_step=10)
# plt.figure(figsize=[10,15])
# sed3.show_slices(img, contour=sg2, seeds=seeds, slice_step=10)
In [22]:
np.unique(seeds)
Out[22]:
In [23]:
i
Out[23]:
In [24]:
# export all to latex
dtt.save(dfnew, "raw_table")
In [25]:
stats0
Out[25]:
In [26]:
# gc
In [27]:
df = pd.read_csv(fnamenew)
df.rename(columns={"msgc time": "MSGC time"})
# dfs = df[(df["data seedsz"]==3) & (df["data offset"] == 3) & (df["data radius"] == 10)]
# dfs_plus = dfs[dfs['data size'] > 160]
dfs = df
import seaborn as sns
sns.set_context("paper")
sns.set_style("white")
In [28]:
dfs.keys()
Out[28]:
In [29]:
dfs
Out[29]:
In [30]:
sns.boxplot(data=df, y="time", x="method")
Out[30]:
In [31]:
uu = pd.melt(df.rename(columns={"gc time": "gc", "time": "total"}), value_vars=["gc", "total"], id_vars=["method"], var_name="time type", value_name="time")
sns.boxplot(data=uu, hue="time type",y="time", x="method")
plt.savefig(op.join(latex_dir, "msgc_time_boxplot_ct.pdf"), dpi=1000)
In [32]:
# uu["method"] == "msgc_lo2hi"
uu[uu["method"] == "msgc_lo2hi "]
Out[32]:
In [33]:
table = pd.pivot_table(
dfs, values=['gc time', 'time', "t3", "t4", "t5", "t6", "t7", "t8"], index=['method'],
aggfunc={'gc time': np.mean, 'time': [np.mean, min, max], "t3": np.mean,"t4": np.mean, "t5": np.mean, "t6": np.mean, "t7": np.mean, "t8": np.mean}
)
table
Out[33]:
In [34]:
table = pd.pivot_table(
dfs, values=['gc time', 'time', "t2", "t3", "t3.1", "t3.2", "t3.3", "t4", "t5", "t6", "t7", "t8", "low level image voxels", "low level object voxels"], index=['method'],
aggfunc=np.min
)
table
Out[34]:
In [35]:
table = pd.pivot_table(
dfs, values=['gc time', 'time', "t graph 01", "t graph 10", "t graph 11", "t graph 13", "t graph 14", "low level image voxels", "low level object voxels"], index=['method'],
aggfunc=np.mean
)
table
Out[35]:
In [36]:
table = pd.pivot_table(
dfs, values=[
'gc time', 'time',
"t graph low",
"t graph high",
"t split 01",
"t split 02",
"t split 03",
"t split 04",
"t split 05",
"t split 06",
"t split 07",
"t split 08",
"t split 081",
"t split 082",
"t split 09",
"t split 10",
"low level image voxels", "low level object voxels"], index=['method'],
aggfunc=np.mean
)
table
Out[36]:
In [37]:
dfs.keys()
Out[37]: