Skip to content
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

WIP: use std::filesystem for handling of file paths #3026

Draft
wants to merge 5 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 16 additions & 11 deletions cmd/5tt2gmwmi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "dwi/tractography/ACT/act.h"
#include "dwi/tractography/ACT/tissues.h"

#include <filesystem>

using namespace MR;
using namespace App;

Expand All @@ -36,19 +38,19 @@ void usage() {
SYNOPSIS = "Generate a mask image appropriate for seeding streamlines on the grey matter-white matter interface";

REFERENCES
+ "Smith, R. E.; Tournier, J.-D.; Calamante, F. & Connelly, A. " // Internal
"Anatomically-constrained tractography:"
" Improved diffusion MRI streamlines tractography through effective use of anatomical information. "
"NeuroImage, 2012, 62, 1924-1938";
+ "Smith, R. E.; Tournier, J.-D.; Calamante, F. & Connelly, A. " // Internal
"Anatomically-constrained tractography:"
" Improved diffusion MRI streamlines tractography through effective use of anatomical information. "
"NeuroImage, 2012, 62, 1924-1938";

ARGUMENTS
+ Argument ("5tt_in", "the input 5TT segmented anatomical image").type_image_in()
+ Argument ("mask_out", "the output mask image").type_image_out();
+ Argument ("5tt_in", "the input 5TT segmented anatomical image").type_image_in()
+ Argument ("mask_out", "the output mask image").type_image_out();

OPTIONS
+ Option("mask_in", "Filter an input mask image according to those voxels that lie upon the grey matter - white matter boundary. "
"If no input mask is provided, "
"the output will be a whole-brain mask image calculated using the anatomical image only.")
+ Option("mask_in", "Filter an input mask image according to those voxels that lie upon the grey matter - white matter boundary. "
"If no input mask is provided, "
"the output will be a whole-brain mask image calculated using the anatomical image only.")
+ Argument ("image", "the input mask image").type_image_in();

}
Expand Down Expand Up @@ -107,8 +109,11 @@ class Processor {
};

void run() {
const std::filesystem::path input_path{argument.front()};
const std::filesystem::path output_path{argument.begin()[1]};

auto input = Image<float>::open(input_path);

auto input = Image<float>::open(argument[0]);
DWI::Tractography::ACT::verify_5TT_image(input);
check_3D_nonunity(input);

Expand All @@ -132,7 +137,7 @@ void run() {
H = input;
H.ndim() = 3;
}
auto output = Image<float>::create(argument[1], H);
auto output = Image<float>::create(output_path, H);

ThreadedLoop("Generating GMWMI seed mask", input, 0, 3).run(Processor(mask), input, output);
}
8 changes: 6 additions & 2 deletions cmd/5tt2vis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
#include "dwi/tractography/ACT/act.h"
#include "dwi/tractography/ACT/tissues.h"

#include <filesystem>

using namespace MR;
using namespace App;

Expand Down Expand Up @@ -74,8 +76,10 @@ void usage() {
// clang-format on

void run() {
const std::filesystem::path input_path{argument.front()};
const std::filesystem::path output_path{argument.begin()[1]};

auto input = Image<float>::open(argument[0]);
auto input = Image<float>::open(input_path);
DWI::Tractography::ACT::verify_5TT_image(input);

Header H(input);
Expand All @@ -88,7 +92,7 @@ void run() {
const float csf_multiplier = get_option_value("csf", VALUE_DEFAULT_CSF);
const float path_multiplier = get_option_value("path", VALUE_DEFAULT_PATH);

auto output = Image<float>::create(argument[1], H);
auto output = Image<float>::create(output_path, H);

auto f = [&](decltype(input) &in, decltype(output) &out) {
const DWI::Tractography::ACT::Tissues t(in);
Expand Down
16 changes: 9 additions & 7 deletions cmd/5ttcheck.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@

#include "dwi/tractography/ACT/act.h"

#include <filesystem>

using namespace MR;
using namespace App;

Expand Down Expand Up @@ -54,15 +56,15 @@ void run() {

size_t major_error_count = 0, minor_error_count = 0;
for (size_t i = 0; i != argument.size(); ++i) {

auto in = Image<float>::open(argument[i]);
const std::filesystem::path inPath{argument[i]};
auto in = Image<float>::open(inPath);

Image<bool> voxels;
Header H_out(in);
H_out.ndim() = 3;
H_out.datatype() = DataType::Bit;
if (!voxels_prefix.empty())
voxels = Image<bool>::scratch(H_out, "Scratch image for " + argument[i]);
voxels = Image<bool>::scratch(H_out, "Scratch image for " + inPath.string());

try {

Expand Down Expand Up @@ -104,21 +106,21 @@ void run() {
}

if (voxel_error_sum == 1) {
INFO("Image \"" + argument[i] +
INFO("Image \"" + inPath.string() +
"\" contains just one isolated voxel with non-unity sum of partial volume fractions");
} else if (voxel_error_sum) {
WARN("Image \"" + argument[i] + "\" contains " + str(voxel_error_sum) +
WARN("Image \"" + inPath.string() + "\" contains " + str(voxel_error_sum) +
" brain voxels with non-unity sum of partial volume fractions");
if (!voxel_error_abs)
++minor_error_count;
} else if (!voxel_error_abs) {
INFO("Image \"" + argument[i] + "\" conforms to 5TT format");
INFO("Image \"" + inPath.string() + "\" conforms to 5TT format");
}

if ((voxel_error_sum || voxel_error_abs) && voxels.valid()) {
std::string path = voxels_prefix;
if (argument.size() > 1) {
path += Path::basename(argument[i]);
path += inPath.filename();
} else {
bool has_extension = false;
for (auto p = MR::Formats::known_extensions; *p; ++p) {
Expand Down
8 changes: 6 additions & 2 deletions cmd/5ttedit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@

#include "dwi/tractography/ACT/act.h"

#include <filesystem>

using namespace MR;
using namespace App;

Expand Down Expand Up @@ -128,10 +130,12 @@ class Modifier {
};

void run() {
const std::filesystem::path input_path{argument[0]};
const std::filesystem::path output_path{argument[1]};

auto in = Image<float>::open(argument[0]);
auto in = Image<float>::open(input_path);
DWI::Tractography::ACT::verify_5TT_image(in);
auto out = Image<float>::create(argument[1], in);
auto out = Image<float>::create(output_path, in);

Modifier modifier(in, out);

Expand Down
12 changes: 8 additions & 4 deletions cmd/afdconnectivity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include "memory.h"
#include "version.h"

#include <filesystem>

using namespace MR;
using namespace MR::DWI;
using namespace App;
Expand Down Expand Up @@ -287,18 +289,20 @@ void AFDConnectivity::save(const std::string &path) {
}

void run() {
const std::string wbft_path = get_option_value<std::string>("wbft", "");
const std::filesystem::path input_image_path{argument[0]};
const std::filesystem::path input_tracks_path{argument[1]};
const std::filesystem::path wbft_path{get_option_value<std::string>("wbft", "")};

DWI::Directions::FastLookupSet dirs(1281);
auto fod = Image<value_type>::open(argument[0]);
auto fod = Image<value_type>::open(input_image_path);
Math::SH::check(fod);
check_3D_nonunity(fod);
AFDConnectivity model(fod, dirs, argument[1], wbft_path);
AFDConnectivity model(fod, dirs, input_tracks_path, wbft_path);

auto opt = get_options("all_fixels");
model.set_all_fixels(!opt.empty());

const value_type connectivity_value = model.get(argument[1]);
const value_type connectivity_value = model.get(input_tracks_path);

// output the AFD sum using std::cout. This enables output to be redirected to a file without the console output.
std::cout << connectivity_value << std::endl;
Expand Down
18 changes: 13 additions & 5 deletions cmd/amp2response.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
#include "math/sphere.h"
#include "types.h"

#include <filesystem>

using namespace MR;
using namespace App;

Expand Down Expand Up @@ -198,7 +200,12 @@ class Accumulator {
void run() {

// Get directions from either selecting a b-value shell, or the header, or external file
auto header = Header::open(argument[0]);
const std::filesystem::path amps_input_path{argument[0]};
const std::filesystem::path mask_input_path{argument[1]};
const std::filesystem::path directions_input_path{argument[2]};
const std::filesystem::path response_output_path{argument[3]};

auto header = Header::open(amps_input_path);

// May be dealing with multiple shells
std::vector<Eigen::MatrixXd> dirs_azel;
Expand Down Expand Up @@ -275,13 +282,14 @@ void run() {
}

auto image = header.get_image<float>();
auto mask = Image<bool>::open(argument[1]);
auto mask = Image<bool>::open(mask_input_path);
check_dimensions(image, mask, 0, 3);
if (!(mask.ndim() == 3 || (mask.ndim() == 4 && mask.size(3) == 1)))
throw Exception("input mask must be a 3D image");
auto dir_image = Image<float>::open(argument[2]);
auto dir_image = Image<float>::open(directions_input_path);
if (dir_image.ndim() < 4 || dir_image.size(3) < 3)
throw Exception("input direction image \"" + std::string(argument[2]) + "\" does not have expected dimensions");
throw Exception("input direction image \"" + directions_input_path.string() +
"\" does not have expected dimensions");
check_dimensions(image, dir_image, 0, 3);

size_t num_voxels = 0;
Expand Down Expand Up @@ -365,5 +373,5 @@ void run() {
line += "," + str<int>((*shells)[i].get_mean());
keyvals["Shells"] = line;
}
File::Matrix::save_matrix(responses, argument[3], keyvals);
File::Matrix::save_matrix(responses, response_output_path, keyvals);
}
9 changes: 7 additions & 2 deletions cmd/amp2sh.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include "phase_encoding.h"
#include "progressbar.h"

#include <filesystem>

using namespace MR;
using namespace App;

Expand Down Expand Up @@ -183,7 +185,10 @@ class Amp2SH {
};

void run() {
auto amp = Image<value_type>::open(argument[0]).with_direct_io(3);
const std::filesystem::path input_path{argument[0]};
const std::filesystem::path output_path{argument[1]};

auto amp = Image<value_type>::open(input_path).with_direct_io(3);
Header header(amp);

std::vector<size_t> bzeros, dwis;
Expand Down Expand Up @@ -229,7 +234,7 @@ void run() {

header.size(3) = sh2amp.cols();
Stride::set_from_command_line(header);
auto SH = Image<value_type>::create(argument[1], header);
auto SH = Image<value_type>::create(output_path, header);

Amp2SHCommon common(sh2amp, bzeros, dwis, normalise);

Expand Down
Loading