Skip to content
Snippets Groups Projects
Commit 3fb4e2f4 authored by Mark Jenkinson's avatar Mark Jenkinson
Browse files

Added label option and checked basic functionality is giving zero diffs

parent ce4ffaed
No related branches found
No related tags found
No related merge requests found
...@@ -51,6 +51,9 @@ Option<string> inname(string("-i"), string(""), ...@@ -51,6 +51,9 @@ Option<string> inname(string("-i"), string(""),
Option<string> maskname(string("-m"), string(""), Option<string> maskname(string("-m"), string(""),
string("~<filename>\tinput 3D mask"), string("~<filename>\tinput 3D mask"),
false, requires_argument); false, requires_argument);
Option<string> labelname(string("--label"), string(""),
string("input 3D label image (generate separate mean for each integer label value - cannot be used with showall)"),
false, requires_argument);
Option<string> outmat(string("-o"), string(""), Option<string> outmat(string("-o"), string(""),
string("~<filename>\toutput text matrix"), string("~<filename>\toutput text matrix"),
false, requires_argument); false, requires_argument);
...@@ -89,6 +92,7 @@ int main(int argc,char *argv[]) ...@@ -89,6 +92,7 @@ int main(int argc,char *argv[])
options.add(eig); options.add(eig);
options.add(order); options.add(order);
options.add(bin_mask); options.add(bin_mask);
options.add(labelname);
options.add(transpose); options.add(transpose);
options.add(verbose); options.add(verbose);
options.add(help); options.add(help);
...@@ -109,7 +113,7 @@ int main(int argc,char *argv[]) ...@@ -109,7 +113,7 @@ int main(int argc,char *argv[])
volume4D<float> vin; volume4D<float> vin;
read_volume4D(vin,inname.value()); read_volume4D(vin,inname.value());
volume<float> mask; volume<float> mask, label;
volume4D<float> mask_nonbin; volume4D<float> mask_nonbin;
if (maskname.set()) { if (maskname.set()) {
read_volume(mask,maskname.value()); read_volume(mask,maskname.value());
...@@ -118,8 +122,19 @@ int main(int argc,char *argv[]) ...@@ -118,8 +122,19 @@ int main(int argc,char *argv[])
mask = 1.0; mask = 1.0;
} }
int nlabs=1;
if (labelname.set() && showall.unset()) {
read_volume(label,labelname.value());
if (verbose.value()) { print_volume_info(label,"label"); }
float labmax=label.max();
if (labmax>=0.99) {
mask *= label;
}
nlabs=MISCMATHS::round(labmax);
if (nlabs<1) nlabs=1;
}
// override the mask if a coordinate is specified // override the mask (and label) if a coordinate is specified
if (coordval.set()) { if (coordval.set()) {
mask = vin[0]; mask = vin[0];
mask = 0.0; mask = 0.0;
...@@ -139,97 +154,103 @@ int main(int argc,char *argv[]) ...@@ -139,97 +154,103 @@ int main(int argc,char *argv[])
x = v(1); y = v(2); z = v(3); x = v(1); y = v(2); z = v(3);
mask(MISCMATHS::round(x),MISCMATHS::round(y),MISCMATHS::round(z)) = 1.0; mask(MISCMATHS::round(x),MISCMATHS::round(y),MISCMATHS::round(z)) = 1.0;
} }
if (!samesize(vin[0],mask)) { if (!samesize(vin[0],mask)) {
cerr << "ERROR: Mask and Input volumes have different (x,y,z) size." cerr << "ERROR: Mask and Input volumes have different (x,y,z) size."
<< endl; << endl;
return -1; return -1;
} }
mask_nonbin.addvolume(mask); mask_nonbin.addvolume(mask);
mask.binarise(1e-8); // arbitrary "0" threshold if (labelname.unset()) { mask.binarise(1e-8); } // arbitrary "0" threshold
if(eig.value()){ Matrix meants;
Matrix dat, evecs, scales; int nt = vin.tsize();
scales = mask_nonbin.matrix(mask); int nvox = nlabs;
dat = vin.matrix(mask); if (showall.value()) {
if(!bin_mask.value()) nvox = no_mask_voxels(mask);
dat = SP (dat, ones(dat.Nrows(),1) * scales); nt += 3;
dat = remmean(dat,1); }
meants.ReSize(nt,nvox);
meants = 0;
for (int iter=1; iter<=nlabs; iter++) {
if (eig.value()) {
Matrix dat, evecs, scales;
scales = mask_nonbin.matrix(mask);
dat = vin.matrix(mask);
if(!bin_mask.value())
dat = SP (dat, ones(dat.Nrows(),1) * scales);
dat = remmean(dat,1);
if (verbose.value()) { if (verbose.value()) {
cout << "Number of voxels used = " << dat.Ncols() << endl; cout << "Number of voxels used = " << dat.Ncols() << endl;
} }
SymmetricMatrix Corr; SymmetricMatrix Corr;
Corr << dat * dat.t() / dat.Ncols(); Corr << dat * dat.t() / dat.Ncols();
DiagonalMatrix tmpD; DiagonalMatrix tmpD;
EigenValues(Corr,tmpD,evecs); EigenValues(Corr,tmpD,evecs);
evecs = fliplr(evecs.Columns(evecs.Ncols()-order.value()+1 , evecs.Ncols())) * std::sqrt(dat.Nrows()); evecs = fliplr(evecs.Columns(evecs.Ncols()-order.value()+1 , evecs.Ncols())) * std::sqrt(dat.Nrows());
Matrix res2 = mean(dat,2); Matrix res2 = mean(dat,2);
res2 = res2.Column(1).t() * evecs.Column(1); res2 = res2.Column(1).t() * evecs.Column(1);
if((float)res2.AsScalar()<0) if((float)res2.AsScalar()<0)
evecs = -1.0 * evecs; evecs = -1.0 * evecs;
if (transpose.value()) { evecs=evecs.t(); } if (transpose.value()) { evecs=evecs.t(); }
if (outmat.set()) { if (outmat.set()) {
write_ascii_matrix(evecs,outmat.value()); write_ascii_matrix(evecs,outmat.value());
} else { } else {
cout << evecs << endl; cout << evecs << endl;
} }
if (transpose.value()) { evecs=evecs.t(); } if (transpose.value()) { evecs=evecs.t(); }
}
else{ } else {
int nt = vin.tsize(); // NOT EIG
int nvox = 1; long int num=0;
if (showall.value()) {
nvox = MISCMATHS::round(mask.sum());
nt += 3;
}
Matrix meants(nt,nvox);
meants = 0;
long int num=0;
for (int z=mask.minz(); z<=mask.maxz(); z++) { for (int z=mask.minz(); z<=mask.maxz(); z++) {
for (int y=mask.miny(); y<=mask.maxy(); y++) { for (int y=mask.miny(); y<=mask.maxy(); y++) {
for (int x=mask.minx(); x<=mask.maxx(); x++) { for (int x=mask.minx(); x<=mask.maxx(); x++) {
if (mask(x,y,z)>0.5) { if (MISCMATHS::round(mask(x,y,z))==iter) {
num++; num++;
if (showall.value()) { if (showall.value()) {
ColumnVector v(4); ColumnVector v(4);
v << x << y << z << 1.0; v << x << y << z << 1.0;
v = (vin[0].niftivox2newimagevox_mat()).i() * v; v = (vin[0].niftivox2newimagevox_mat()).i() * v;
meants(1,num) = v(1); meants(1,num) = v(1);
meants(2,num) = v(2); meants(2,num) = v(2);
meants(3,num) = v(3); meants(3,num) = v(3);
meants.SubMatrix(4,nt,num,num) = vin.voxelts(x,y,z); meants.SubMatrix(4,nt,num,num) = vin.voxelts(x,y,z);
} else { } else {
meants += vin.voxelts(x,y,z); meants.SubMatrix(1,nt,iter,iter) = meants.SubMatrix(1,nt,iter,iter) + vin.voxelts(x,y,z);
}
} }
} }
} }
} }
}
if (verbose.value()) {
cout << "Number of voxels used = " << num << endl;
}
// normalise for number of valid entries if averaging if (verbose.value()) {
if (!showall.value()) { cout << "Number of voxels used = " << num << endl;
if (num>0) meants /= (float) num; }
}
// save the result // normalise for number of valid entries if averaging
if (transpose.value()) { meants=meants.t(); } if (!showall.value()) {
if (outmat.set()) { if (num>0) meants.SubMatrix(1,nt,iter,iter) = meants.SubMatrix(1,nt,iter,iter) / ((float) num);
write_ascii_matrix(meants,outmat.value()); }
} else {
cout << meants << endl; // save the result
if (transpose.value()) { meants=meants.t(); }
if (outmat.set()) {
write_ascii_matrix(meants,outmat.value());
} else {
cout << meants << endl;
}
if (transpose.value()) { meants=meants.t(); }
} }
if (transpose.value()) { meants=meants.t(); }
} }
return 0; return 0;
} }
......
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