Skip to content
Snippets Groups Projects
Commit e8bec9d1 authored by Constantin Pape's avatar Constantin Pape
Browse files

Update apply_registration

parent 975cc6b3
Branches registration-wrapper
No related tags found
1 merge request!6Add python scripts to run registration on the cluster
...@@ -15,6 +15,8 @@ from cluster_tools.cluster_tasks import SlurmTask, LocalTask, LSFTask ...@@ -15,6 +15,8 @@ from cluster_tools.cluster_tasks import SlurmTask, LocalTask, LSFTask
class ApplyRegistrationBase(luigi.Task): class ApplyRegistrationBase(luigi.Task):
""" ApplyRegistration base class """ ApplyRegistration base class
""" """
default_fiji = '/g/arendt/EM_6dpf_segmentation/platy-browser-data/software/Fiji.app/ImageJ-linux64'
default_elastix = '/g/arendt/EM_6dpf_segmentation/platy-browser-data/software/elastix_v4.8'
task_name = 'apply_registration' task_name = 'apply_registration'
src_file = os.path.abspath(__file__) src_file = os.path.abspath(__file__)
...@@ -23,8 +25,8 @@ class ApplyRegistrationBase(luigi.Task): ...@@ -23,8 +25,8 @@ class ApplyRegistrationBase(luigi.Task):
input_path_file = luigi.Parameter() input_path_file = luigi.Parameter()
output_path_file = luigi.Parameter() output_path_file = luigi.Parameter()
transformation_file = luigi.Parameter() transformation_file = luigi.Parameter()
fiji_executable = luigi.Parameter(default='/g/almf/software/Fiji.app/ImageJ-linux64') fiji_executable = luigi.Parameter(default=default_fiji)
elastix_directory = luigi.Parameter(default='/g/almf/software/elastix_v4.8') elastix_directory = luigi.Parameter(default=default_elastix)
dependency = luigi.TaskParameter(default=DummyTask()) dependency = luigi.TaskParameter(default=DummyTask())
def requires(self): def requires(self):
...@@ -108,12 +110,12 @@ def apply_for_file(input_path, output_path, ...@@ -108,12 +110,12 @@ def apply_for_file(input_path, output_path,
# with individual arguments comma separated # with individual arguments comma separated
# the argument to transformaix needs to be one large comma separated string # the argument to transformaix needs to be one large comma separated string
transformix_argument = ["elastixDirectory=\'%s\'" % elastix_directory, transformix_argument = ["elastixDirectory=\'%s\'" % elastix_directory,
"workingDirectory=\'%s\'" % tmp_folder, "workingDirectory=\'%s\'" % os.path.abspath(tmp_folder),
"inputImageFile=\'%s\'" % input_path, "inputImageFile=\'%s\'" % input_path,
"transformationFile=\'%s\'" % transformation_file, "transformationFile=\'%s\'" % transformation_file,
"outputFile=\'%s\'" % output_path, "outputFile=\'%s\'" % output_path,
"outputModality=\'Save as BigDataViewer .xml/.h5\'", "outputModality=\'Save as BigDataViewer .xml/.h5\'",
"numThreads=\'1\'"] # TODO why do we use numThreads=1 and not the same as in -c? "numThreads=\'%i\'" % n_threads]
transformix_argument = ",".join(transformix_argument) transformix_argument = ",".join(transformix_argument)
transformix_argument = "\"%s\"" % transformix_argument transformix_argument = "\"%s\"" % transformix_argument
...@@ -129,14 +131,16 @@ def apply_for_file(input_path, output_path, ...@@ -129,14 +131,16 @@ def apply_for_file(input_path, output_path,
fu.log("Calling the following command:") fu.log("Calling the following command:")
fu.log(cmd_str) fu.log(cmd_str)
# the elastix wrapper only works properly if we set these as environment variables as well, see cwd = os.getcwd()
# TODO make issue about this
# os.environ['TMPDIR'] = tmp_folder
os.environ['TRAFO'] = transformation_file
try: try:
# we need to change the working dir to the transformation directroy, so that relative paths in
# the transformations are correct
trafo_dir = os.path.split(transformation_file)[0]
fu.log("Change directory to %s" % trafo_dir)
os.chdir(trafo_dir)
# check_output(cmd) # check_output(cmd)
# the CLI parser is very awkward (to put it nicely). # the CLI parser is very awkward.
# I could only get it to work by passing the whole command string # I could only get it to work by passing the whole command string
# and setting shell to True. # and setting shell to True.
# otherwise, it would parse something wrong, and do nothing but # otherwise, it would parse something wrong, and do nothing but
...@@ -144,7 +148,10 @@ def apply_for_file(input_path, output_path, ...@@ -144,7 +148,10 @@ def apply_for_file(input_path, output_path,
# [WARNING] Ignoring invalid argument: --run # [WARNING] Ignoring invalid argument: --run
check_output([cmd_str], shell=True) check_output([cmd_str], shell=True)
except CalledProcessError as e: except CalledProcessError as e:
raise RuntimeError(e.output) raise RuntimeError(e.output.decode('utf-8'))
finally:
fu.log("Go back to cwd: %s" % cwd)
os.chdir(cwd)
def apply_registration(job_id, config_path): def apply_registration(job_id, config_path):
......
...@@ -2,17 +2,14 @@ import os ...@@ -2,17 +2,14 @@ import os
import json import json
import luigi import luigi
# TODO remove path hack once we merge this into master
import sys
sys.path.insert(0, '../..')
from scripts.extension.registration import ApplyRegistrationLocal from scripts.extension.registration import ApplyRegistrationLocal
def check_wrapper_simple(): def check_wrapper():
in_path = '/g/almf/software/elastix-test/muscles.tif' in_path = '/g/kreshuk/pape/Work/my_projects/platy-browser-data/registration/9.9.9/ProSPr/stomach.tif'
tmp_folder = '/g/kreshuk/pape/Work/my_projects/dev-platy/test/registration/tmp_registration_simple' tmp_folder = os.path.abspath('tmp_registration')
out_path = os.path.join(tmp_folder, 'out') out_path = os.path.join(tmp_folder, 'stomach_prospr_registered')
in_list = [in_path] in_list = [in_path]
out_list = [out_path] out_list = [out_path]
...@@ -33,51 +30,23 @@ def check_wrapper_simple(): ...@@ -33,51 +30,23 @@ def check_wrapper_simple():
with open(os.path.join(conf_dir, 'global.config'), 'w') as f: with open(os.path.join(conf_dir, 'global.config'), 'w') as f:
json.dump(global_conf, f) json.dump(global_conf, f)
trafo = '/g/almf/software/elastix-test/TransformParameters.RotationPreAlign.0.txt' trafo_dir = '/g/kreshuk/pape/Work/my_projects/platy-browser-data/registration/0.0.0/transformations'
t = task(tmp_folder=tmp_folder, config_dir=conf_dir, max_jobs=1,
input_path_file=in_file, output_path_file=out_file, transformation_file=trafo)
ret = luigi.build([t], local_scheduler=True)
assert ret
expected_out_xml = out_path + '.xml'
assert os.path.exists(expected_out_xml), expected_out_xml
expected_out_h5 = out_path + '.h5'
assert os.path.exists(expected_out_h5), expected_out_h5
def check_wrapper():
in_path = os.path.join('/g/kreshuk/pape/Work/my_projects/platy-browser-data/registration/9.9.9/images/ProSPr',
'Stomach_forRegistration.tif')
out_path = '/g/kreshuk/pape/Work/my_projects/dev-platy/test/registration/somach_prospr_registered'
in_list = [in_path]
out_list = [out_path]
in_file = './in_list.json'
with open(in_file, 'w') as f:
json.dump(in_list, f)
out_file = './out_list.json'
with open(out_file, 'w') as f:
json.dump(out_list, f)
task = ApplyRegistrationLocal # This is the full transformation, but it takes a lot of time!
conf_dir = './configs' trafo = os.path.join(trafo_dir, 'TransformParameters.BSpline10-3Channels.0.txt')
os.makedirs(conf_dir, exist_ok=True)
global_conf = task.default_global_config() # For now, we use the similarity trafo to save time
shebang = '/g/kreshuk/pape/Work/software/conda/miniconda3/envs/cluster_env37/bin/python' trafo = os.path.join(trafo_dir, 'TransformParameters.Similarity-3Channels.0.txt')
global_conf.update({'shebang': shebang})
with open(os.path.join(conf_dir, 'global.config'), 'w') as f:
json.dump(global_conf, f)
# TODO which one is the correct trafo ? t = task(tmp_folder=tmp_folder, config_dir=conf_dir, max_jobs=1,
trafo = os.path.join('/g/kreshuk/pape/Work/my_projects/platy-browser-data/registration/0.0.0/transformations/0.0.0',
'TransformParameters.BSpline10-3Channels.0.txt')
t = task(tmp_folder='tmp_registration', config_dir=conf_dir, max_jobs=1,
input_path_file=in_file, output_path_file=out_file, transformation_file=trafo) input_path_file=in_file, output_path_file=out_file, transformation_file=trafo)
ret = luigi.build([t], local_scheduler=True) ret = luigi.build([t], local_scheduler=True)
assert ret assert ret
expected_out = out_path + '.xml' expected_xml = out_path + '.xml'
assert os.path.exists(expected_out), expected_out assert os.path.exists(expected_xml), expected_xml
expected_h5 = out_path + '.h5'
assert os.path.exists(expected_h5), expected_h5
# check_wrapper() if __name__ == '__main__':
check_wrapper_simple() check_wrapper()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment