-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added creation of bvec and bval files when creating NIfTI files #335
Added creation of bvec and bval files when creating NIfTI files #335
Conversation
…WI files (when bvalue and direction are found in the MINC header)
@MounaSafiHarab As I created the PR, I just thought I should probably create a tool script to back populate the |
@MounaSafiHarab @nicolasbrossard this should be ready for review :) |
tools/create_nifti_bval_bvec.pl
Outdated
my $sth = $dbh->prepare($query); | ||
$sth->execute('acquisition:bvalues'); | ||
|
||
my @file_ids; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
my @file_ids = map { $_->{'FileID'} } @{ $sth->fetchall_arrayref( {} ) };
...but if you think it is too cryptic, leave the current code as is.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done. Thanks for the suggestion!
tools/create_nifti_bval_bvec.pl
Outdated
|
||
# determine paths for bval/bvec files | ||
my $minc = $file->getFileDatum('File'); | ||
my ($bval_file, $bvec_file) = map { $minc }(0 .. 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you are creating an array of identical elements, use the repetiton (x
) operator:
my ($bval_file, $bvec_file) = ($minc) x 2;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A lot simpler. Thanks!
tools/create_nifti_bval_bvec.pl
Outdated
|
||
# determine paths for bval/bvec files | ||
my $minc = $file->getFileDatum('File'); | ||
my ($bval_file, $bvec_file) = map { $minc }(0 .. 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since you are matching the end of the file name, the g
pattern match modifier is not needed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right. Removed the g
pattern match modifier
tools/create_nifti_bval_bvec.pl
Outdated
$bvec_file =~ s/mnc$/bvec/g; | ||
|
||
# create complementary nifti files for DWI acquisitions | ||
my $bval_success = NeuroDB::MRI::create_dwi_nifti_bval_file( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like "$data_dir/$bval_file"
better than $data_dir . '/' . $bval_file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done!
tools/create_nifti_bval_bvec.pl
Outdated
my $bval_success = NeuroDB::MRI::create_dwi_nifti_bval_file( | ||
\$file, $data_dir . '/' . $bval_file | ||
); | ||
my $bvec_success = NeuroDB::MRI::create_dwi_nifti_bvec_file( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like "$data_dir/$bvec_file"
better than $data_dir . '/' . $bvec_file
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
uploadNeuroDB/NeuroDB/MRI.pm
Outdated
|
||
# clean up the bvals string | ||
$bvals =~ s/\.\,//g; # remove all '.,' from the string | ||
$bvals =~ s/\.$//g; # remove the last trailing '.' from the string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unnecessary g
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
uploadNeuroDB/NeuroDB/MRI.pm
Outdated
# print bvals into bval_file | ||
write_to_file($bval_file, $bvals, '>'); | ||
|
||
return 1 if (-e $bval_file); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could also do
return -e $bval_file;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
did not think of that. Nice!
uploadNeuroDB/NeuroDB/MRI.pm
Outdated
$mode = '>>'; # set to 0 to append to the file the other rows | ||
} | ||
|
||
return 1 if (-e $bvec_file); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
return -e $bvec_file;
uploadNeuroDB/NeuroDB/MRI.pm
Outdated
|
||
return undef unless @bvecs; | ||
|
||
# loop through all bvecs, clean them up and print them into the bvec file |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think opening the file, writing to it and then closing it on each iteration is not a good idea. I wouldn't even define a function to print to a file
s/^\"+|\"$//g for @bvec;
open(OUT, '>', $bvec_file) or die "Cannot write to file $bvec_file: $!\n";
print OUT map { "$_\n" } @bvec;
close(OUT);
$file->getParameter('acquisition:direction_y'), | ||
$file->getParameter('acquisition:direction_z') | ||
); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think @bvecs
will always contain 3 elements and so will always evaluate to true. return undef unless @bvecs
is unnecessary IMHO. Have you ever come across such a case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, for T1W acquisitions for example, those 3 elements are not there and it will return undef
, skipping the bval/bvec creation since there are no fields to create them. This avoids having to add another config setting to specify the scan type for which to create bval/bvec files (which would be a config setting a bit obscure). So if those headers are present, continue in this function to create the bvec file, if they are not present, then return from the function and don't create bvec file. Does that make more sense?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nicolasbrossard Thank you for your feedback!! They should all have been taken care of. Please re-review.
tools/create_nifti_bval_bvec.pl
Outdated
my $sth = $dbh->prepare($query); | ||
$sth->execute('acquisition:bvalues'); | ||
|
||
my @file_ids; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done. Thanks for the suggestion!
tools/create_nifti_bval_bvec.pl
Outdated
|
||
# determine paths for bval/bvec files | ||
my $minc = $file->getFileDatum('File'); | ||
my ($bval_file, $bvec_file) = map { $minc }(0 .. 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A lot simpler. Thanks!
tools/create_nifti_bval_bvec.pl
Outdated
|
||
# determine paths for bval/bvec files | ||
my $minc = $file->getFileDatum('File'); | ||
my ($bval_file, $bvec_file) = map { $minc }(0 .. 1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
right. Removed the g
pattern match modifier
tools/create_nifti_bval_bvec.pl
Outdated
$bvec_file =~ s/mnc$/bvec/g; | ||
|
||
# create complementary nifti files for DWI acquisitions | ||
my $bval_success = NeuroDB::MRI::create_dwi_nifti_bval_file( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done!
tools/create_nifti_bval_bvec.pl
Outdated
my $bval_success = NeuroDB::MRI::create_dwi_nifti_bval_file( | ||
\$file, $data_dir . '/' . $bval_file | ||
); | ||
my $bvec_success = NeuroDB::MRI::create_dwi_nifti_bvec_file( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
uploadNeuroDB/NeuroDB/MRI.pm
Outdated
my $nifti = $minc; | ||
$nifti =~ s/mnc$/nii/g; | ||
my ($nifti, $bval_file, $bvec_file) = map { $minc }(0 .. 2); | ||
$nifti =~ s/mnc$/nii/g; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done. As well as the ($minc) x 2
instead of map { $minc }(0 .. 2)
uploadNeuroDB/NeuroDB/MRI.pm
Outdated
|
||
# mnc2nii command | ||
my $m2n_cmd = "mnc2nii -nii -quiet " . | ||
$data_dir . "/" . $minc . " " . | ||
$data_dir . "/" . $nifti; | ||
system($m2n_cmd); | ||
|
||
# create complementary nifti files for DWI acquisitions |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
indeed. A lot clearer. I also did that for the command four lines before and it all fits in one line :)
uploadNeuroDB/NeuroDB/MRI.pm
Outdated
|
||
# clean up the bvals string | ||
$bvals =~ s/\.\,//g; # remove all '.,' from the string | ||
$bvals =~ s/\.$//g; # remove the last trailing '.' from the string |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
uploadNeuroDB/NeuroDB/MRI.pm
Outdated
# print bvals into bval_file | ||
write_to_file($bval_file, $bvals, '>'); | ||
|
||
return 1 if (-e $bval_file); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
did not think of that. Nice!
$file->getParameter('acquisition:direction_y'), | ||
$file->getParameter('acquisition:direction_z') | ||
); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, for T1W acquisitions for example, those 3 elements are not there and it will return undef
, skipping the bval/bvec creation since there are no fields to create them. This avoids having to add another config setting to specify the scan type for which to create bval/bvec files (which would be a config setting a bit obscure). So if those headers are present, continue in this function to create the bvec file, if they are not present, then return from the function and don't create bvec file. Does that make more sense?
uploadNeuroDB/NeuroDB/MRI.pm
Outdated
$bvals =~ s/\.$//; # remove the last trailing '.' from the string | ||
|
||
# print bvals into bval_file | ||
open(FILE, '>', $bval_file) or die "Could not open file $bval_file $!"; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing semi-colon in error message (see corresponding message in create_dwi_nifti_bvec_file
).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done in the next commit!
@nicolasbrossard Fixed the missing colon. Thank you! Ready for review again :) |
For DWI files, the binary
mnc2nii
is not creating the.bvec
and.bval
files necessary to obtain the direction informations. Added support in the imaging pipeline so that those files can be created automatically within the functionmake_nii
of theMRI.pm
file.Behaviour of the script:
acquisition:bvalues
then create the.bval
file using these valuesacquisition:direction_x
,acquisition:direction_y
andacquisition:direction_z
then create the.bvec
file using these valuesNotes for existing projects
A script has been created in the
tools
directory to create the missing bval/bvec files for projects that have the 'create_nii' configuration set to 'Yes'. This script is calledcreate_nifti_bval_bvec.pl
.Notes for people that want to create those files from a different script
Simply run
mnc2nii
, then call theMRI.pm
functionscreate_dwi_nifti_bval_file
andcreate_dwi_nifti_bvec_file
to create the.bval
and.bvec
files. Args to the function are:File.pm
library.bval
or.bvec
file