Skip to content
Snippets Groups Projects
Commit 753adb16 authored by Fidel Alfaro Almagro's avatar Fidel Alfaro Almagro :speech_balloon:
Browse files

New versioning system and a bunch of other shit

parent cf048d09
No related branches found
No related tags found
No related merge requests found
1.8
\ No newline at end of file
import os.path as op
basedir = op.dirname(__file__)
__version__ = open(op.join(basedir, 'VERSION')).read()
...@@ -14,7 +14,7 @@ import os ...@@ -14,7 +14,7 @@ import os
import logging import logging
import nibabel as nib import nibabel as nib
from fsl import wrappers from fsl import wrappers
from pipe_tree import In, Out, Ref from pipe_tree import In, Out, Ref, Var
from bip.utils.log_utils import redirect_logging, tempdir from bip.utils.log_utils import redirect_logging, tempdir
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -24,7 +24,8 @@ def run(ctx, ...@@ -24,7 +24,8 @@ def run(ctx,
TBSS_prefix: Ref, TBSS_prefix: Ref,
aptx_txt_prefix: Ref, aptx_txt_prefix: Ref,
tmp_dir: Ref, tmp_dir: Ref,
aptx_tract_tmp: In(no_iter=True), aptx_tract_tmp: In,
autoptx_tract: Var(no_iter=True),
IDP_diff_autoPtx: Out): IDP_diff_autoPtx: Out):
with redirect_logging('IDP_diff_autoPtx', outdir=logs_dir),\ with redirect_logging('IDP_diff_autoPtx', outdir=logs_dir),\
...@@ -37,7 +38,7 @@ def run(ctx, ...@@ -37,7 +38,7 @@ def run(ctx,
result = "" result = ""
# TODO: See if it is possible to generate the aptx_tract_tmp files here # TODO: See if it is possible to generate the aptx_tract_tmp files here
wrappers.fslmerge(autoPtx_all, *list(aptx_tract_tmp.data)) wrappers.fslmerge("t", autoPtx_all, *list(aptx_tract_tmp.data))
tractnorm = wrappers.fslstats(autoPtx_all, t=True).m.run() tractnorm = wrappers.fslstats(autoPtx_all, t=True).m.run()
for suffix in ['FA','L1','L2','L3','MO','MD','ICVF','OD','ISOVF']: for suffix in ['FA','L1','L2','L3','MO','MD','ICVF','OD','ISOVF']:
...@@ -59,7 +60,7 @@ def run(ctx, ...@@ -59,7 +60,7 @@ def run(ctx,
with open(APTX_file, 'wt', encoding="utf-8") as f: with open(APTX_file, 'wt', encoding="utf-8") as f:
f.write(f'{cad}\n') f.write(f'{cad}\n')
result += cad result += " " + cad
else: else:
result += " " + result_NaN result += " " + result_NaN
......
...@@ -76,20 +76,20 @@ def run(ctx, ...@@ -76,20 +76,20 @@ def run(ctx,
N_tfMRI_featquery_5a_dir = tfMRI_featquery_5a_dir.replace(tfMRI_feat,"") N_tfMRI_featquery_5a_dir = tfMRI_featquery_5a_dir.replace(tfMRI_feat,"")
wrappers.featquery(N_featdirs="1", featdir1=tfMRI_feat, N_stats="2", wrappers.featquery(featdirs=[tfMRI_feat],
stats1=[N_tfMRI_cope1, N_tfMRI_zstat1], stats=[N_tfMRI_cope1, N_tfMRI_zstat1],
outputRootName=N_tfMRI_featquery_1_dir, outputRootName=N_tfMRI_featquery_1_dir,
mask=group_mask_1) mask=group_mask_1)
wrappers.featquery(N_featdirs="1", featdir1=tfMRI_feat, N_stats="2", wrappers.featquery(featdirs=[tfMRI_feat],
stats1=[N_tfMRI_cope2, N_tfMRI_zstat2], stats=[N_tfMRI_cope2, N_tfMRI_zstat2],
outputRootName=N_tfMRI_featquery_2_dir, outputRootName=N_tfMRI_featquery_2_dir,
mask=group_mask_2) mask=group_mask_2)
wrappers.featquery(N_featdirs="1", featdir1=tfMRI_feat, N_stats="2", wrappers.featquery(featdirs=[tfMRI_feat],
stats1=[N_tfMRI_cope5, N_tfMRI_zstat5], stats=[N_tfMRI_cope5, N_tfMRI_zstat5],
outputRootName=N_tfMRI_featquery_5_dir, outputRootName=N_tfMRI_featquery_5_dir,
mask=group_mask_5) mask=group_mask_5)
wrappers.featquery(N_featdirs="1", featdir1=tfMRI_feat, N_stats="2", wrappers.featquery(featdirs=[tfMRI_feat],
stats1=[N_tfMRI_cope5, N_tfMRI_zstat5], stats=[N_tfMRI_cope5, N_tfMRI_zstat5],
outputRootName=N_tfMRI_featquery_5a_dir, outputRootName=N_tfMRI_featquery_5a_dir,
mask=group_mask_5a) mask=group_mask_5a)
......
...@@ -27,7 +27,7 @@ from bip.pipelines.IDPs_gen import IDP_func_TSNR ...@@ -27,7 +27,7 @@ from bip.pipelines.IDPs_gen import IDP_func_TSNR
from bip.pipelines.IDPs_gen import IDP_func_task_activation from bip.pipelines.IDPs_gen import IDP_func_task_activation
from bip.pipelines.IDPs_gen import IDP_diff_eddy_outliers from bip.pipelines.IDPs_gen import IDP_diff_eddy_outliers
from bip.pipelines.IDPs_gen import IDP_diff_TBSS from bip.pipelines.IDPs_gen import IDP_diff_TBSS
#from bip.pipelines.IDPs_gen import IDP_diff_autoPtx from bip.pipelines.IDPs_gen import IDP_diff_autoPtx
from bip.pipelines.IDPs_gen import IDPs_generator from bip.pipelines.IDPs_gen import IDPs_generator
log = logging.getLogger(__name__) log = logging.getLogger(__name__)
...@@ -109,10 +109,10 @@ def add_to_pipeline(ctx, pipe, tree, targets): ...@@ -109,10 +109,10 @@ def add_to_pipeline(ctx, pipe, tree, targets):
kwargs={'ctx' : ctx}) kwargs={'ctx' : ctx})
targets.append('IDP_diff_TBSS') targets.append('IDP_diff_TBSS')
#pipe(IDP_diff_autoPtx.run, pipe(IDP_diff_autoPtx.run,
# submit=dict(jobtime=200, name="BIP_IDP_diff_autoPtx_"+ subj), submit=dict(jobtime=200, name="BIP_IDP_diff_autoPtx_"+ subj),
# kwargs={'ctx' : ctx}) kwargs={'ctx' : ctx})
#targets.append('IDP_diff_autoPtx') targets.append('IDP_diff_autoPtx')
pipe(IDP_subject_COG_table.run, pipe(IDP_subject_COG_table.run,
submit=dict(jobtime=200, name="BIP_IDP_subject_COG_table_" + subj), submit=dict(jobtime=200, name="BIP_IDP_subject_COG_table_" + subj),
......
...@@ -42,7 +42,8 @@ def run(ctx, ...@@ -42,7 +42,8 @@ def run(ctx,
eddy_data: Out, eddy_data: Out,
eddy_outlier_report: Out): eddy_outlier_report: Out):
with redirect_logging('diff_eddy', outdir=logs_dir): #TODO: Do the __name__ for everything
with redirect_logging(__name__, outdir=logs_dir):
# Creates links # Creates links
# TODO: These links are NOT relative. This may cause future problems. # TODO: These links are NOT relative. This may cause future problems.
...@@ -55,7 +56,6 @@ def run(ctx, ...@@ -55,7 +56,6 @@ def run(ctx,
if not os.path.exists(eddy_nodif_brain_mask_ud): if not os.path.exists(eddy_nodif_brain_mask_ud):
os.symlink(src="../../../" + fieldmap_mask_ud, dst=eddy_nodif_brain_mask_ud) os.symlink(src="../../../" + fieldmap_mask_ud, dst=eddy_nodif_brain_mask_ud)
# Generation of FSF file
copyfile(src=AP_bval, dst=eddy_bvals) copyfile(src=AP_bval, dst=eddy_bvals)
copyfile(src=AP_bvec, dst=eddy_bvecs) copyfile(src=AP_bvec, dst=eddy_bvecs)
......
...@@ -59,7 +59,7 @@ def run(ctx, ...@@ -59,7 +59,7 @@ def run(ctx,
TBSS_FA_to_MNI_warp_s2 = tmp_dir + '/FA_to_MNI_warp_s2.nii.gz' TBSS_FA_to_MNI_warp_s2 = tmp_dir + '/FA_to_MNI_warp_s2.nii.gz'
# Creates links # Creates links
# TODO: These links are NOT relative. This may cause future problems. # TODO: Do the cool OS.PATH.RELPATH
if not os.path.exists(TBSS_FA): if not os.path.exists(TBSS_FA):
os.symlink(src="../../../" + FA, dst=TBSS_FA) os.symlink(src="../../../" + FA, dst=TBSS_FA)
if not os.path.exists(TBSS_MNI): if not os.path.exists(TBSS_MNI):
...@@ -159,5 +159,4 @@ def run(ctx, ...@@ -159,5 +159,4 @@ def run(ctx,
wrappers.fslmaths(d_output).mas(TBSS_mean_FA_skeleton_mask).run(d_output_skel) wrappers.fslmaths(d_output).mas(TBSS_mean_FA_skeleton_mask).run(d_output_skel)
mean = wrappers.fslstats( d_output_skel, K=atlas).M.run() mean = wrappers.fslstats( d_output_skel, K=atlas).M.run()
with open(d_output_txt, 'wt', encoding="utf-8") as f: np.savetxt(d_output_txt, mean, fmt="%0.8f")
f.write(f'{mean}')
...@@ -75,7 +75,7 @@ def run(ctx, ...@@ -75,7 +75,7 @@ def run(ctx,
x1,y1,z1 = np.where(B0_AP_corr_tmp_imgf==0) x1,y1,z1 = np.where(B0_AP_corr_tmp_imgf==0)
x2,y2,z2 = np.where(B0_PA_corr_tmp_imgf==0) x2,y2,z2 = np.where(B0_PA_corr_tmp_imgf==0)
#For AP voxels with 0 value,, get the values in PA (And viceversa) #For AP voxels with 0 value, get the values in PA (And viceversa)
B0_AP_fixed_tmp_imgf = B0_AP_corr_tmp_imgf B0_AP_fixed_tmp_imgf = B0_AP_corr_tmp_imgf
B0_PA_fixed_tmp_imgf = B0_PA_corr_tmp_imgf B0_PA_fixed_tmp_imgf = B0_PA_corr_tmp_imgf
B0_AP_fixed_tmp_imgf[x1,y1,z1] = B0_PA_corr_tmp_imgf[x1,y1,z1] B0_AP_fixed_tmp_imgf[x1,y1,z1] = B0_PA_corr_tmp_imgf[x1,y1,z1]
......
...@@ -69,9 +69,11 @@ def choose_best_B0(ctx, img, bval, total_B0, indices, tmp, best_index, B0, ...@@ -69,9 +69,11 @@ def choose_best_B0(ctx, img, bval, total_B0, indices, tmp, best_index, B0,
scores[i,j] = corr scores[i,j] = corr
scores[j,i] = corr scores[j,i] = corr
final_scores = np.zeros(N) # TODO: Do in one line
for i in range(N): #final_scores = np.zeros(N)
final_scores[i]=np.sum(scores[:,i]) / (N - 1) #for i in range(N):
# final_scores[i]=np.sum(scores[:,i]) / (N - 1)
final_scores = np.sum(scores, axis=1) / (N - 1)
best_ind = final_scores.argmax() best_ind = final_scores.argmax()
best_score = final_scores[best_ind] best_score = final_scores[best_ind]
...@@ -201,3 +203,5 @@ def run(ctx, ...@@ -201,3 +203,5 @@ def run(ctx,
if (Zslices % 2) != 0: if (Zslices % 2) != 0:
wrappers.fslroi(B0_AP_PA, B0_AP_PA, 0, -1, 0, -1, 0, Zslices -1) wrappers.fslroi(B0_AP_PA, B0_AP_PA, 0, -1, 0, -1, 0, Zslices -1)
# TODO: Discuss this with Jesper.
...@@ -517,6 +517,7 @@ def bb_FS_get_IDPs(ctx, env): ...@@ -517,6 +517,7 @@ def bb_FS_get_IDPs(ctx, env):
dataDir = subjectDir + '/data/' dataDir = subjectDir + '/data/'
headersDir = subjectDir + '/headers/' headersDir = subjectDir + '/headers/'
#TODO: Raise an exception
if not os.path.isdir(subjectDir): if not os.path.isdir(subjectDir):
print("Error: FreeSurfer has not been run on this subject") print("Error: FreeSurfer has not been run on this subject")
sys.exit(-1) sys.exit(-1)
......
...@@ -32,6 +32,7 @@ def run(ctx, ...@@ -32,6 +32,7 @@ def run(ctx,
if os.path.exists(FreeSurfer_dir): if os.path.exists(FreeSurfer_dir):
shutil.rmtree(FreeSurfer_dir) shutil.rmtree(FreeSurfer_dir)
#TODO: SUBJECTS_DIR can be an argument of freesurfer instead of this craps
env = dict(os.environ, SUBJECTS_DIR=os.getcwd() + "/" + ctx.subject) env = dict(os.environ, SUBJECTS_DIR=os.getcwd() + "/" + ctx.subject)
T1_path = os.getcwd() + "/" + T1_unbiased T1_path = os.getcwd() + "/" + T1_unbiased
...@@ -39,16 +40,13 @@ def run(ctx, ...@@ -39,16 +40,13 @@ def run(ctx,
cmd = 'recon-all -all -s FreeSurfer -i ' + T1_path cmd = 'recon-all -all -s FreeSurfer -i ' + T1_path
if os.path.exists(T2_FLAIR_unbiased): #TODO: Do this for all cases of os.path.exits (all optional things)
if T2_FLAIR_unbiased is not None and os.path.exists(T2_FLAIR_unbiased):
cmd += " -FLAIR " + T2_path + " -FLAIRpial" cmd += " -FLAIR " + T2_path + " -FLAIRpial"
output = run_command(log, cmd, env) try:
output = run_command(log, cmd, env=env)
log.info(output) log.info(output)
finally:
if os.path.exists(fsaverage): if os.path.exists(fsaverage):
os.unlink(fsaverage) os.unlink(fsaverage)
#ext_wrappers.recon_all(subjects_dir=os.getcwd(),
# directive="all", subjid="FreeSurfer",
# infile=T1_unbiased, FLAIR=opt_T2_FLAIR)
...@@ -37,6 +37,8 @@ def run(ctx, ...@@ -37,6 +37,8 @@ def run(ctx,
output=T1_first_prefix, b=True) output=T1_first_prefix, b=True)
for f in glob.glob(T1_first_prefix + "-*_first*.nii.gz"): for f in glob.glob(T1_first_prefix + "-*_first*.nii.gz"):
os.remove(f) if os.path.exists(f):
os.remove(f)
for f in glob.glob(T1_first_prefix + "-*_corr*.nii.gz"): for f in glob.glob(T1_first_prefix + "-*_corr*.nii.gz"):
os.remove(f) if os.path.exists(f):
os.remove(f)
...@@ -22,13 +22,13 @@ from subprocess import check_output ...@@ -22,13 +22,13 @@ from subprocess import check_output
from fsl import wrappers from fsl import wrappers
#def run_command(logger, command, **kwargs): This fails def run_command(logger, command, **kwargs): This fails
def run_command(logger, command, env): #def run_command(logger, command, env):
try: try:
logger.info(command.strip()) logger.info(command.strip())
# This fails # This fails
#job_output = check_output(command,shell=True,**kwargs).decode('UTF-8') job_output = check_output(command,shell=True,**kwargs).decode('UTF-8')
job_output = check_output(command,shell=True,env=env).decode('UTF-8') #job_output = check_output(command,shell=True,env=env).decode('UTF-8')
logger.info('Output: \t' + job_output.strip()) logger.info('Output: \t' + job_output.strip())
except Exception as e: except Exception as e:
...@@ -36,7 +36,7 @@ def run_command(logger, command, env): ...@@ -36,7 +36,7 @@ def run_command(logger, command, env):
logger.error('Exception type: \t' + str(type(e))) logger.error('Exception type: \t' + str(type(e)))
logger.error('Exception args: \t' + str(e.args)) logger.error('Exception args: \t' + str(e.args))
logger.error('Exception message: \t' + str(e)) logger.error('Exception message: \t' + str(e))
job_output = "" raise
return job_output.strip() return job_output.strip()
...@@ -136,14 +136,11 @@ def redirect_logging(name, outdir='.', level=logging.INFO, verbose=True): ...@@ -136,14 +136,11 @@ def redirect_logging(name, outdir='.', level=logging.INFO, verbose=True):
if getattr(log, 'handler', None): if getattr(log, 'handler', None):
log.addHandler(log.handler) log.addHandler(log.handler)
@contextlib.contextmanager @contextlib.contextmanager
def tempdir(dir_name): def tempdir(dir_name):
''' This is meant to remove the temp directory only if the calling function
succeeds.
'''
os.makedirs(dir_name, exist_ok=True) os.makedirs(dir_name, exist_ok=True)
yield dir_name yield dir_name
for file_name in os.listdir(dir_name): shutil.rmtree(dir_name)
file_path = os.path.join(dir_name, file_name)
try:
shutil.rmtree(file_path)
except OSError:
os.remove(file_path)
import os.path as op
from setuptools import setup,find_packages from setuptools import setup,find_packages
with open('requirements.txt', 'rt') as f: with open('requirements.txt', 'rt') as f:
install_requires = [l.strip() for l in f.readlines()] install_requires = [l.strip() for l in f.readlines()]
basedir = op.dirname(__file__)
version = open(op.join(basedir, 'bip', 'VERSION')).read().strip()
setup(name='bip', setup(name='bip',
version='1.0.0', version=version,
description='Brain Imaging Pipeline', description='Brain Imaging Pipeline',
author='Fidel Alfaro Almagro', author='Fidel Alfaro Almagro',
install_requires=install_requires, install_requires=install_requires,
......
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