Commit 17f40a8c authored by inhuszar's avatar inhuszar
Browse files

Updated script launcher.

parent bc4c0cbf
......@@ -80,10 +80,5 @@ setup(name="tirl",
"tirl/transformations/linear",
"tirl/transformations/nonlinear",
"tirl/usermodules"],
scripts=["tirl/scripts/mnd/histology_to_block",
"tirl/scripts/mnd/find_sites",
"tirl/scripts/mnd/block_to_slice",
"tirl/scripts/mnd/slice_to_volume",
"tirl/tirlinfo",
"tirl/tirlconfig",
"tirl/tirl"])
scripts=["tirl/tirl"]
)
......@@ -785,7 +785,7 @@ def verbosity(level):
raise AttributeError(f"Invalid verbosity level: {str(level)}.")
def main(args):
def block_to_slice(args):
"""
Main program code. Controls program flow, handles command-line arguments.
......@@ -839,15 +839,21 @@ def create_cli(parser):
return parser
# Program execution starts here
if __name__ == "__main__":
def main(*args):
""" Main program code. """
parser = argparse.ArgumentParser(
prog="block_to_slice",
description="Registers a tissue block photograph to "
"a corresponding brain slice photograph.")
parser = create_cli(parser)
if len(sys.argv) > 1:
main(parser.parse_args(sys.argv[1:]))
if args:
block_to_slice(parser.parse_args(args))
else:
parser.print_help()
# Program execution starts here
if __name__ == "__main__":
main(*sys.argv[1:])
#!/usr/bin/env fslpython
# -*- coding: utf-8 -*-
# _______ _____ _____ _
# |__ __| |_ _| | __ \ | |
# | | | | | |__) | | |
# | | | | | _ / | |
# | | _| |_ | | \ \ | |____
# |_| |_____| |_| \_\ |______|
#
# Copyright (C) 2018-2020 University of Oxford
# Part of the FMRIB Software Library (FSL)
# Author: Istvan N. Huszar
# Date: 10 June 2020
# SHBASECOPYRIGHT
__version__ = "2020.6.0"
# DEPENDENCIES
import tirl
histo = tirl.load("/mnt/rio/mnd_k2cluster/example/stage1/fixed4_nonlinear.timg")
block = tirl.load("/mnt/rio/mnd_k2cluster/example/stage2_14A/fixed4_nonlinear.timg")
slice_ = tirl.load("/mnt/rio/mnd_k2cluster/example/stage3_14A_025mm_new1/4_stage4.timg")
mri = tirl.load("/mnt/rio/mnd_k2cluster/example/stage3_14A_05mm_halton_succesive/volume.timg")
block_native = block.copy()
block_native.domain.chain = block_native.domain.chain[:2]
block_native.evaluate(histo.domain).preview()
block.preview()
slice_native = slice_.copy()
slice_native.domain.chain = slice_native.domain.chain[:2]
slice_native.evaluate(block.domain).preview()
mri.evaluate(slice_.domain).preview()
block.domain.chain.pop(0)
block.domain.chain.pop(0)
slice_.domain.chain.pop(0)
slice_.domain.chain.pop(0)
print("Histo")
print("\n".join(str(tx) for tx in histo.domain.chain))
print("Block")
print("\n".join(str(tx) for tx in block.domain.chain))
print("Slice")
print("\n".join(str(tx) for tx in slice_.domain.chain))
fullchain = histo.domain.chain + block.domain.chain + slice_.domain.chain
for tx in fullchain:
tx.parameters.unlock()
histo.domain.chain = fullchain
mri.evaluate(histo.domain).preview()
#!/usr/bin/env fslpython
# -*- coding: utf-8 -*-
# _______ _____ _____ _
# |__ __| |_ _| | __ \ | |
# | | | | | |__) | | |
# | | | | | _ / | |
# | | _| |_ | | \ \ | |____
# |_| |_____| |_| \_\ |______|
#
# Copyright (C) 2018-2020 University of Oxford
# Part of the FMRIB Software Library (FSL)
# Author: Istvan N. Huszar
# Date: 10 June 2020
# SHBASECOPYRIGHT
__version__ = "2020.6.0"
# DEPENDENCIES
import sys
import tirl
def main(*args):
histo = tirl.load("/mnt/rio/mnd_k2cluster/example/stage1/fixed4_nonlinear.timg")
block = tirl.load("/mnt/rio/mnd_k2cluster/example/stage2_14A/fixed4_nonlinear.timg")
slice_ = tirl.load("/mnt/rio/mnd_k2cluster/example/stage3_14A_025mm_new1/4_stage4.timg")
mri = tirl.load("/mnt/rio/mnd_k2cluster/example/stage3_14A_05mm_halton_succesive/volume.timg")
block_native = block.copy()
block_native.domain.chain = block_native.domain.chain[:2]
block_native.evaluate(histo.domain).preview()
block.preview()
slice_native = slice_.copy()
slice_native.domain.chain = slice_native.domain.chain[:2]
slice_native.evaluate(block.domain).preview()
mri.evaluate(slice_.domain).preview()
block.domain.chain.pop(0)
block.domain.chain.pop(0)
slice_.domain.chain.pop(0)
slice_.domain.chain.pop(0)
print("Histo")
print("\n".join(str(tx) for tx in histo.domain.chain))
print("Block")
print("\n".join(str(tx) for tx in block.domain.chain))
print("Slice")
print("\n".join(str(tx) for tx in slice_.domain.chain))
fullchain = histo.domain.chain + block.domain.chain + slice_.domain.chain
for tx in fullchain:
tx.parameters.unlock()
histo.domain.chain = fullchain
mri.evaluate(histo.domain).preview()
if __name__ == "__main__":
main(*sys.argv[1:])
#!/usr/bin/env python
#!/usr/bin/env fslpython
# -*- coding: utf-8 -*-
# _______ _____ _____ _
# |__ __| |_ _| | __ \ | |
# | | | | | |__) | | |
# | | | | | _ / | |
# | | _| |_ | | \ \ | |____
# |_| |_____| |_| \_\ |______|
#
# Copyright (C) 2018-2020 University of Oxford
# Part of the FMRIB Software Library (FSL)
# Author: Istvan N. Huszar
# Date: 9 June 2020
# SHBASECOPYRIGHT
__version__ = "2020.6.0"
# DEPENDENCIES
import os
import sys
......@@ -10,13 +32,17 @@ from skimage.filters import threshold_otsu
from skimage.measure import label, regionprops
# DEFINITIONS
OPTIONS = {
"distance": 0.25,
"channel": 0
}
def main(args):
# IMPLEMENTATION
def crop_centre(args):
""" Main program code. """
img = TImage(args.image).tensors[:3]
......@@ -90,7 +116,11 @@ def main(args):
img.snapshot(fname, overwrite=True)
if __name__ == "__main__":
def create_cli():
"""
Creates command-line interface.
"""
descr = "Segments brain slice from dissection photograph."
parser = argparse.ArgumentParser(prog="preproc", description=descr)
......@@ -104,8 +134,18 @@ if __name__ == "__main__":
parser.add_argument("--flip", action="store_true", default=False,
required=False, help="Flip input horizontally.")
if len(sys.argv[1:]) > 0:
args = parser.parse_args(sys.argv[1:])
main(args)
return parser
def main(*args):
""" Main program code. """
parser = create_cli()
if args:
crop_centre(parser.parse_args(args))
else:
parser.print_help()
if __name__ == "__main__":
main(*sys.argv[1:])
#!/usr/bin/env python
#!/usr/bin/env fslpython
# -*- coding: utf-8 -*-
# _______ _____ _____ _
# |__ __| |_ _| | __ \ | |
# | | | | | |__) | | |
# | | | | | _ / | |
# | | _| |_ | | \ \ | |____
# |_| |_____| |_| \_\ |______|
#
# Copyright (C) 2018-2020 University of Oxford
# Part of the FMRIB Software Library (FSL)
# Author: Istvan N. Huszar
# Date: 9 June 2020
# SHBASECOPYRIGHT
__version__ = "2020.6.0"
# DEPENDENCIES
import os
import sys
......@@ -10,13 +32,17 @@ from sklearn.cluster import KMeans
from skimage.measure import label, regionprops
# DEFINITIONS
OPTIONS = {
"distance": 0.25,
"channel": 2
}
def main(args):
# IMPLEMENTATION
def extract_blocks(args):
""" Main program code. """
# Resample image to 1/16 resolution
......@@ -94,7 +120,11 @@ def main(args):
blockimg.snapshot(fname, overwrite=True)
if __name__ == "__main__":
def create_cli():
"""
Creates command-line interface.
"""
descr = "Segments brain slice from dissection photograph."
parser = argparse.ArgumentParser(prog="preproc", description=descr)
......@@ -112,8 +142,18 @@ if __name__ == "__main__":
parser.add_argument("--flip", action="store_true", default=False,
required=False, help="Flip input horizontally.")
if len(sys.argv[1:]) > 0:
args = parser.parse_args(sys.argv[1:])
main(args)
return parser
def main(*args):
""" Main program code. """
parser = create_cli()
if args:
extract_blocks(parser.parse_args(args))
else:
parser.print_help()
if __name__ == "__main__":
main(*sys.argv[1:])
......@@ -91,6 +91,9 @@ OPTIONS_NONLINEAR = {
"visualise": False
}
p = AttrMap({})
logger = logging.getLogger("tirl.scripts.mnd.find_sites")
# IMPLEMENTATION
......@@ -115,7 +118,7 @@ def sort_images(*images):
return tuple(images[ix] for ix in order)
def pairwise_registrations(n_cpu, *images):
def pairwise_registrations(args, *images):
"""
Assuming that the input images are ordered from the most intact to the most
sampled, determine the insertion sites. Rigid-body registration takes place
......@@ -145,7 +148,7 @@ def pairwise_registrations(n_cpu, *images):
outputdir = os.path.dirname(args.sites)
jobs = enumerate(zip(images[:-1], images[1:]))
worker = partial(_worker_func, outputdir=outputdir)
n_cpu = max(min(n_cpu % mp.cpu_count(), len(images) - 1), 1)
n_cpu = max(min(int(args.cpu) % mp.cpu_count(), len(images) - 1), 1)
n_cpu = 1 if p.debug else n_cpu
with mp.Pool(n_cpu) as pool:
sites = pool.map(worker, jobs)
......@@ -384,7 +387,7 @@ def identify_sites(intact, sampled, width, area, threshold):
return None
def main(args):
def find_sites(args):
"""
Main program code.
......@@ -404,10 +407,6 @@ def main(args):
# Create logger
global logger
logfile = os.path.join(outputdir, "find_sites.log")
# counter = 1
# while os.path.isfile(logfile):
# counter += 1
# logfile = os.path.join(outputdir, f"find_sites{counter}.log")
logging.basicConfig(format='[%(asctime)s] %(message)s',
datefmt='%d-%m-%Y %H:%M:%S',
filename=logfile,
......@@ -423,9 +422,8 @@ def main(args):
# Perform parallel pairwise rigid registrations to find sites
# and conclude run
try:
sites = pairwise_registrations(args.cpu, *sorted_images)
sites = pairwise_registrations(args, *sorted_images)
except Exception as exc:
sites = None
logger.error(exc.args)
raise
......@@ -437,8 +435,11 @@ def main(args):
logger.fatal(f"Insertion site search complete. No site was found.")
# Program execution starts here
if __name__ == "__main__":
def create_cli():
"""
Creates command-line interface.
"""
usage = "find_sites [--cpu=-1] <image1> <image2> [images ...] "
descr = "Finds tissue block insertion sites in a brain section photograph."
parser = argparse.ArgumentParser(
......@@ -478,9 +479,19 @@ if __name__ == "__main__":
required=False,
help="Parallelise across this number of CPU cores. "
"Default: -1 (all cores).")
return parser
if len(sys.argv[1:]) > 0:
args = parser.parse_args(sys.argv[1:])
main(args)
def main(*args):
""" Main program code. """
parser = create_cli()
if args:
find_sites(parser.parse_args(args))
else:
parser.print_help()
# Program execution starts here
if __name__ == "__main__":
main(*sys.argv[1:])
......@@ -17,6 +17,9 @@
# SHBASECOPYRIGHT
__version__ = "2020.6.0"
# DEPENDENCIES
import logging
......
......@@ -925,7 +925,7 @@ def verbosity(level):
raise AttributeError(f"Invalid verbosity level: {str(level)}.")
def main(args):
def histology_to_block(args):
"""
Main program code. Controls program flow, handles command-line arguments.
......@@ -971,14 +971,20 @@ def create_cli(parser):
return parser
if __name__ == "__main__":
def main(*args):
""" Main program code. """
parser = argparse.ArgumentParser(
prog="histology_to_block",
description="Registers a histology slide to "
"a corresponding tissue block photograph.")
parser = create_cli(parser)
if len(sys.argv) > 1:
main(parser.parse_args(sys.argv[1:]))
if args:
histology_to_block(parser.parse_args(args))
else:
parser.print_help()
if __name__ == "__main__":
main(*sys.argv[1:])
......@@ -17,6 +17,9 @@
# SHBASECOPYRIGHT
__version__ = "2020.6.0"
# DEPENDENCIES
import os
......@@ -1057,7 +1060,7 @@ def _halton(n_points, seed, **box):
# PROGRAM EXECUTION CONTROL
def main(args):
def histology_to_volume(args):
"""
Main program code. Controls program flow, handles command-line arguments.
......@@ -1108,14 +1111,20 @@ def create_cli():
required=False, help="3D TImage file")
parser.add_argument("-o", "--out", metavar="dir", type=str, default=None,
required=False, help="Output directory")
return parser
# Program execution starts here
if __name__ == "__main__":
def main(*args):
""" Main program code. """
parser = create_cli()
if len(sys.argv) > 1:
main(parser.parse_args(sys.argv[1:]))
if args:
histology_to_volume(parser.parse_args(args))
else:
parser.print_help()
# Program execution starts here
if __name__ == "__main__":
main(*sys.argv[1:])
......@@ -17,6 +17,9 @@
# SHBASECOPYRIGHT
__version__ = "2020.6.0"
# DEPENDENCIES
import logging
......
......@@ -17,6 +17,9 @@
# SHBASECOPYRIGHT
__version__ = "2020.6.0"
# DEPENDENCIES
import logging
......
#!/usr/bin/env python
#!/usr/bin/env fslpython
# -*- coding: utf-8 -*-
# _______ _____ _____ _
# |__ __| |_ _| | __ \ | |
# | | | | | |__) | | |
# | | | | | _ / | |
# | | _| |_ | | \ \ | |____
# |_| |_____| |_| \_\ |______|
#
# Copyright (C) 2018-2020 University of Oxford
# Part of the FMRIB Software Library (FSL)
# Author: Istvan N. Huszar
# Date: 21 June 2020
# SHBASECOPYRIGHT
__version__ = "2020.6.0"
# DEPENDENCIES
import os
import sys
......@@ -12,10 +34,14 @@ import tirl
from tirl import settings as ts
# DEFINITIONS
config = namedtuple("config", ["cnf", "mri", "direction", "ref", "slices"])
def main(confile, yestoall):
# IMPLEMENTATION
def prepare_multi_stage3(confile, yestoall):
""" Main program code. """
opts = parse_configuration_file(confile)
......@@ -196,12 +222,16 @@ def parse_configuration_file(cf):
return c
if __name__ == "__main__":
def main(*args):
""" Main program code. """
usage = "register_slices.py stage3.con [-y]"
args = sys.argv[1:]
if not args:
print(usage)
else:
if args:
y = "-y" in args
main(confile=args[0], yestoall=y)
prepare_multi_stage3(confile=args[0], yestoall=y)
else:
print(usage)
if __name__ == "__main__":
main(*sys.argv[1:])
......@@ -17,6 +17,9 @@
# SHBASECOPYRIGHT
__version__ = "2020.6.0"
# DEPENDENCIES
import os
......@@ -1596,7 +1599,7 @@ def stage5(img, vol, stparams):
# PROGRAM EXECUTION CONTROL
def main(args):
def slice_to_volume(args):
"""