Commit bc50fbfb authored by inhuszar's avatar inhuszar
Browse files

Added similarity metric option to stage-2 script.

parent 27d49cda
...@@ -14,7 +14,8 @@ ...@@ -14,7 +14,8 @@
"verbose": false, "verbose": false,
"outputdir": "/Users/inhuszar/temp/stage2", "outputdir": "/Users/inhuszar/temp/stage2",
"stages": ["rigid", "affine", "nonlinear"], "stages": ["rigid", "affine", "nonlinear"],
"warnings": false "warnings": false,
"cost": "MIND"
}, },
"block": { "block": {
"file": "/Users/inhuszar/temp/2_tissue_block/tissue_block.tif", "file": "/Users/inhuszar/temp/2_tissue_block/tissue_block.tif",
......
...@@ -56,6 +56,7 @@ from tirl.chain import Chain ...@@ -56,6 +56,7 @@ from tirl.chain import Chain
from tirl.timage import TField from tirl.timage import TField
from tirl.timage import TImage from tirl.timage import TImage
from tirl.costs.mi import CostMI from tirl.costs.mi import CostMI
from tirl.costs.msd import CostMSD
from tirl.costs.mind import CostMIND from tirl.costs.mind import CostMIND
from tirl.regularisers.diffusion import DiffusionRegulariser from tirl.regularisers.diffusion import DiffusionRegulariser
from tirl.transformations.linear.scale import TxScale from tirl.transformations.linear.scale import TxScale
...@@ -338,6 +339,7 @@ def find_best_site(fixed, moving, cnf): ...@@ -338,6 +339,7 @@ def find_best_site(fixed, moving, cnf):
# Find best site # Find best site
costvals = np.stack(costvals, axis=0) costvals = np.stack(costvals, axis=0)
best_site = int(costvals[np.argmin(costvals[:, -1]), 0]) best_site = int(costvals[np.argmin(costvals[:, -1]), 0])
logger.debug(f"Cost values at each site:\n {costvals}")
logger.info(f"Best site: ({best_site}): {sites[best_site]}") logger.info(f"Best site: ({best_site}): {sites[best_site]}")
# Update the site offset and rotation parameters to the optima # Update the site offset and rotation parameters to the optima
...@@ -376,10 +378,16 @@ def jiggle(fixed, moving, cnf): ...@@ -376,10 +378,16 @@ def jiggle(fixed, moving, cnf):
endpoint=False, retstep=True) endpoint=False, retstep=True)
# Measure grid cost: find N best location + rotation parameters # Measure grid cost: find N best location + rotation parameters
# cost = CostMSD(moving, fixed, normalise=True) if p.general.cost == "MSD":
cost = CostMIND(moving, fixed, normalise=True, kernel=MK_FULL, sigma=1, cost = CostMSD(moving, fixed, normalise=True)
truncate=1.5) elif p.general.cost == "MI":
# cost = CostMI(moving, fixed, normalise=False) cost = CostMI(moving, fixed, normalise=True)
elif p.general.cost == "MIND":
cost = CostMIND(moving, fixed, normalise=True, kernel=MK_FULL,
sigma=1, truncate=1.5)
else:
raise ValueError("Unsupported cost function.")
gridcost = [] gridcost = []
for y in yy: for y in yy:
for x in xx: for x in xx:
...@@ -407,8 +415,7 @@ def jiggle(fixed, moving, cnf): ...@@ -407,8 +415,7 @@ def jiggle(fixed, moving, cnf):
1: (x - xstep[1] / 2, x + xstep[1] / 2)}) 1: (x - xstep[1] / 2, x + xstep[1] / 2)})
tx_rotation.parameters.unlock() tx_rotation.parameters.unlock()
tx_site.parameters.unlock() tx_site.parameters.unlock()
# # Set cost function
# cost = CostMI(moving, fixed, maskmode="and", normalise=False)
# Start optimisation # Start optimisation
OptNL(og, cost, method="LN_BOBYQA", visualise=q.visualise, OptNL(og, cost, method="LN_BOBYQA", visualise=q.visualise,
xtol_abs=q.xtol_abs, xtol_rel=q.xtol_rel, step=q.opt_step, xtol_abs=q.xtol_abs, xtol_rel=q.xtol_rel, step=q.opt_step,
...@@ -458,9 +465,18 @@ def rigid2d(fixed, moving, cnf): ...@@ -458,9 +465,18 @@ def rigid2d(fixed, moving, cnf):
tx_scale = fixed_smooth.domain.chain["scale"] tx_scale = fixed_smooth.domain.chain["scale"]
tx_translation = fixed_smooth.domain.chain["translation"] tx_translation = fixed_smooth.domain.chain["translation"]
og = OptimisationGroup(tx_rotation, tx_scale, tx_translation) og = OptimisationGroup(tx_rotation, tx_scale, tx_translation)
# Set cost function # Set cost function
# cost = CostMI(moving_smooth, fixed_smooth, normalise=True) if p.general.cost == "MSD":
cost = CostMIND(moving_smooth, fixed_smooth, normalise=False) cost = CostMSD(moving_smooth, fixed_smooth, normalise=True)
elif p.general.cost == "MI":
cost = CostMI(moving_smooth, fixed_smooth, normalise=True)
elif p.general.cost == "MIND":
cost = CostMIND(moving_smooth, fixed_smooth, normalise=True,
kernel=MK_FULL, sigma=1, truncate=1.5)
else:
raise ValueError("Unsupported cost function.")
# Start optimisation # Start optimisation
OptNL(og, cost, method="LN_BOBYQA", visualise=q.visualise, OptNL(og, cost, method="LN_BOBYQA", visualise=q.visualise,
xtol_abs=q.xtol_abs, xtol_rel=q.xtol_rel, step=q.opt_step, xtol_abs=q.xtol_abs, xtol_rel=q.xtol_rel, step=q.opt_step,
...@@ -494,9 +510,18 @@ def affine2d(fixed, moving, cnf): ...@@ -494,9 +510,18 @@ def affine2d(fixed, moving, cnf):
moving_smooth = moving.smooth(sm, copy=True) moving_smooth = moving.smooth(sm, copy=True)
# Prepare transformation to optimise # Prepare transformation to optimise
tx_affine = fixed_smooth.domain.chain["affine"] tx_affine = fixed_smooth.domain.chain["affine"]
# Set cost function # Set cost function
# cost = CostMI(moving_smooth, fixed_smooth, normalise=True) if p.general.cost == "MSD":
cost = CostMIND(moving_smooth, fixed_smooth, normalise=False) cost = CostMSD(moving_smooth, fixed_smooth, normalise=True)
elif p.general.cost == "MI":
cost = CostMI(moving_smooth, fixed_smooth, normalise=True)
elif p.general.cost == "MIND":
cost = CostMIND(moving_smooth, fixed_smooth, normalise=True,
kernel=MK_FULL, sigma=1, truncate=1.5)
else:
raise ValueError("Unsupported cost function.")
# Start optimisation # Start optimisation
OptNL(tx_affine, cost, method="LN_BOBYQA", OptNL(tx_affine, cost, method="LN_BOBYQA",
xtol_rel=q.xtol_rel, xtol_abs=q.xtol_abs, xtol_rel=q.xtol_rel, xtol_abs=q.xtol_abs,
......
...@@ -14,7 +14,8 @@ ...@@ -14,7 +14,8 @@
"verbose": false, "verbose": false,
"outputdir": "/home/inhuszar/Desktop/example/stage2", "outputdir": "/home/inhuszar/Desktop/example/stage2",
"stages": ["rigid", "affine", "nonlinear"], "stages": ["rigid", "affine", "nonlinear"],
"warnings": false "warnings": false,
"cost": "MIND"
}, },
"block": { "block": {
"file": "/home/inhuszar/Desktop/example/2_tissue_block/tissue_block.tif", "file": "/home/inhuszar/Desktop/example/2_tissue_block/tissue_block.tif",
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment