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

Fixing small bugs and generalizing a bit more

parent 03dd0b54
No related branches found
No related tags found
No related merge requests found
Pipeline #17732 passed
Source diff could not be displayed: it is stored in LFS. Options to address this: view the blob.
......@@ -37,9 +37,7 @@ def run(ctx,
list_vals = []
b_vals = np.loadtxt(eddy_bvals)
b_vecs = np.loadtxt(eddy_bvecs)
lim_file = ctx.get('dMRI_b0_threshold', 100)
lim = int(np.loadtxt(lim_file))
lim = ctx.get('dMRI_b0_threshold', 100)
b_value_shell_to_keep = ctx.get('dMRI_b_value_shell', 1000)
......
......@@ -124,7 +124,7 @@ def get_acqparams_row(phase_encode, im, im_json):
dtiDwell = get_dwell_time(im, im_json)
topupValue = (dtiDwell * (numlines - 1)) / 1000.0
return ' '.join(elements) + str(topupValue) + '\n'
return ' '.join(elements) + ' ' + str(topupValue) + '\n'
def generate_acqparams(AP, PA, AP_json, PA_json, numAP, numPA, acqparams):
......
......@@ -51,54 +51,59 @@ def run(ctx,
warp=rfMRI_example_func2standard_warp,
out=rfMRI_standard2example_func_warp)
# FIX: Extract features for subject
if fix_features is not None and \
not op.exists(fix_features):
d = extract.FixData.from_melodic_dir(rfMRI_ica, fixdir=fix_dir)
# TODO: Figure out how to clean the data if there's no trained FIX model
# Alternatively: Figure out what to produce in case there's no FIX
model = ctx.get('rfMRI_fix_model', '')
f = extract.extract_features(data=d,
features=legacy.LEGACY_FEATURE_EXTRACTORS)
io.save_features(op.join(fix_dir, 'features'), f)
else:
f = io.read_features(op.join(fix_dir, 'features'))
if op.exists(model):
# FIX: Extract features for subject
if fix_features is not None and \
not op.exists(fix_features):
d = extract.FixData.from_melodic_dir(rfMRI_ica, fixdir=fix_dir)
# FIX: Classify components for subject
clf = io.load_model(ctx.get_data('/fix/UKBB_model_2.pyfix_model'))
y_pred_pb = classify.classify(f, clf)[1]
y_pred = legacy.prob_threshold(y_pred_pb, threshold=0.2)
f = extract.extract_features(data=d,
features=legacy.LEGACY_FEATURE_EXTRACTORS)
io.save_features(op.join(fix_dir, 'features'), f)
else:
f = io.read_features(op.join(fix_dir, 'features'))
fin_list = []
with open(fix4melview, 'wt', encoding="utf-8") as f:
for ind, pred in enumerate(y_pred):
# Opposite of the convention in MelView
if not pred:
f.write(str(ind + 1) + ", Unclassified Noise, True\n")
fin_list.append(ind + 1)
else:
f.write(str(ind + 1) + ", Signal, False\n")
f.write(f'{fin_list}\n')
# FIX: Classify components for subject
clf = io.load_model(model)
y_pred_pb = classify.classify(f, clf)[1]
y_pred = legacy.prob_threshold(y_pred_pb, threshold=0.2)
# FIX: Clean
clean.apply(func_fname=filtered_func_data,
icamix=melodic_mix,
noise_idx=y_pred,
func_clean_fname=filtered_func_data_clean,
do_motion_regression=True,
motion_parameters=rfMRI_mc_parameters)
fin_list = []
with open(fix4melview, 'wt', encoding="utf-8") as f:
for ind, pred in enumerate(y_pred):
# Opposite of the convention in MelView
if not pred:
f.write(str(ind + 1) + ", Unclassified Noise, True\n")
fin_list.append(ind + 1)
else:
f.write(str(ind + 1) + ", Signal, False\n")
f.write(f'{fin_list}\n')
# Create this directory in case it does not exist. This may
# happen due to the overwrite function issue in Melodic
if rfMRI_reg_standard_dir is not None and \
not op.exists(rfMRI_reg_standard_dir):
os.mkdir(rfMRI_reg_standard_dir)
# FIX: Clean
clean.apply(func_fname=filtered_func_data,
icamix=melodic_mix,
noise_idx=y_pred,
func_clean_fname=filtered_func_data_clean,
do_motion_regression=True,
motion_parameters=rfMRI_mc_parameters)
# Generate the data in standard space
wrappers.applywarp(ref=rfMRI_standard, src=filtered_func_data_clean,
out=filtered_func_data_clean_std,
warp=rfMRI_example_func2standard_warp,
interp="spline")
mask = ctx.get_data('MNI/MNI152_T1_2mm_brain_mask_bin.nii.gz')
wrappers.fslmaths(filtered_func_data_clean_std).mas(mask).\
run(filtered_func_data_clean_std)
wrappers.fslmaths(filtered_func_data_clean_std).Tstd().bin().\
run(filtered_func_data_clean_stdmask)
# Create this directory in case it does not exist. This may
# happen due to the overwrite function issue in Melodic
if rfMRI_reg_standard_dir is not None and \
not op.exists(rfMRI_reg_standard_dir):
os.mkdir(rfMRI_reg_standard_dir)
# Generate the data in standard space
wrappers.applywarp(ref=rfMRI_standard, src=filtered_func_data_clean,
out=filtered_func_data_clean_std,
warp=rfMRI_example_func2standard_warp,
interp="spline")
mask = ctx.get_data('MNI/MNI152_T1_2mm_brain_mask_bin.nii.gz')
wrappers.fslmaths(filtered_func_data_clean_std).mas(mask).\
run(filtered_func_data_clean_std)
wrappers.fslmaths(filtered_func_data_clean_std).Tstd().bin().\
run(filtered_func_data_clean_stdmask)
......@@ -9,6 +9,7 @@
# pylint: disable=W0613,C0301
#
import os.path as op
import logging
from shutil import copyfile
from pipe_tree import In, Out, Ref
......@@ -46,43 +47,50 @@ def run(ctx,
with redirect_logging(job_name(run), outdir=logs_dir):
# Create bianca mask
wrappers.make_bianca_mask(T1_unbiased, T1_fast_pve_0,
T1_to_MNI_warp_coef_inv, keep_files=False)
# TODO: Figure out how to find WMHs if there's no trained BIANCA
# Alternatively: Think about what to produce (empty mask?)
# if there is no trained BIANCA Model and no alternative
bianca_class_data = ctx.get('bianca_class_data', '')
if op.exists(bianca_class_data):
# Create bianca mask
wrappers.make_bianca_mask(T1_unbiased, T1_fast_pve_0,
T1_to_MNI_warp_coef_inv, keep_files=False)
copyfile(src=T1_unbiased_brain_mask,
dst=bianca_T1_unbiased_brain_mask)
copyfile(src=T1_unbiased_bianca_mask,
dst=bianca_T1_unbiased_bianca_mask)
copyfile(src=T1_unbiased_ventmask,
dst=bianca_T1_unbiased_ventmask)
copyfile(src=T1_unbiased_brain_mask,
dst=bianca_T1_unbiased_brain_mask)
copyfile(src=T1_unbiased_bianca_mask,
dst=bianca_T1_unbiased_bianca_mask)
copyfile(src=T1_unbiased_ventmask,
dst=bianca_T1_unbiased_ventmask)
# Create masterfile for bianca
filenames = [T1_unbiased_brain, T2_FLAIR_unbiased, T1_to_MNI_linear_mat]
with open(T2_FLAIR_bianca_conf_file, 'w', encoding="utf-8") as f:
for j in filenames:
f.write(j+" ")
# Create masterfile for bianca
filenames = [T1_unbiased_brain, T2_FLAIR_unbiased,
T1_to_MNI_linear_mat]
with open(T2_FLAIR_bianca_conf_file, 'w', encoding="utf-8") as f:
for j in filenames:
f.write(j+" ")
# Run bianca
bianca_class_data = ctx.get_data("bianca/bianca_class_data")
wrappers.bianca(singlefile=T2_FLAIR_bianca_conf_file,
querysubjectnum=1, brainmaskfeaturenum=1,
loadclassifierdata=bianca_class_data,
matfeaturenum=3, featuresubset="1,2",
o=T2_FLAIR_bianca_mask)
# Run bianca
bianca_class_data = ctx.get_data("bianca/bianca_class_data")
wrappers.bianca(singlefile=T2_FLAIR_bianca_conf_file,
querysubjectnum=1, brainmaskfeaturenum=1,
loadclassifierdata=bianca_class_data,
matfeaturenum=3, featuresubset="1,2",
o=T2_FLAIR_bianca_mask)
# Multiply the lesions mask (bianca_mask) by the "unbiased_bianca_mask"
wrappers.fslmaths(T2_FLAIR_bianca_mask).mul(T1_brain_mask).thr(0.8).\
bin().run(T2_FLAIR_bianca_final_mask)
# Multiply the lesions mask (bianca_mask)
# by the "unbiased_bianca_mask"
wrappers.fslmaths(T2_FLAIR_bianca_mask).mul(T1_brain_mask).\
thr(0.8).bin().run(T2_FLAIR_bianca_final_mask)
# Calculate total WMH (volume of the final mask),
# periventricular WMH & deep WMH - 10mm Criteria
wrappers.bianca_perivent_deep(wmh_map=T2_FLAIR_bianca_final_mask,
vent_mask=bianca_T1_unbiased_ventmask,
minclustersize=0,
outputdir=T2_FLAIR_bianca_lesion_dir,
do_stats=2)
# Calculate total WMH (volume of the final mask),
# periventricular WMH & deep WMH - 10mm Criteria
wrappers.bianca_perivent_deep(wmh_map=T2_FLAIR_bianca_final_mask,
vent_mask=bianca_T1_unbiased_ventmask,
minclustersize=0,
outputdir=T2_FLAIR_bianca_lesion_dir,
do_stats=2)
vals = wrappers.fslstats(T2_FLAIR_bianca_final_mask).V.run()[0]
with open(T2_FLAIR_bianca_volume, 'wt', encoding="utf-8") as f:
f.write(str(vals) + "\n")
vals = wrappers.fslstats(T2_FLAIR_bianca_final_mask).V.run()[0]
with open(T2_FLAIR_bianca_volume, 'wt', encoding="utf-8") as f:
f.write(str(vals) + "\n")
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