From fad91685270e4e3dca0b82b653d7567aa6dc2115 Mon Sep 17 00:00:00 2001 From: sunwen Date: Wed, 28 Jun 2023 16:48:28 +0800 Subject: [PATCH] Add usage of command line. --- src/common/fileHelper.cpp | 41 +++++++++++++++++++++++ src/common/fileHelper.h | 18 ++++++++++ src/config/config.cpp | 19 +++++------ src/config/config.h | 2 +- src/main.cxx | 64 +++++++++++++++++++++++++++++------- src/startReconstructions.cpp | 15 +++------ src/startReconstructions.h | 3 +- test/Reconstruction_Test.cpp | 4 +-- 8 files changed, 130 insertions(+), 36 deletions(-) create mode 100644 src/common/fileHelper.cpp create mode 100644 src/common/fileHelper.h diff --git a/src/common/fileHelper.cpp b/src/common/fileHelper.cpp new file mode 100644 index 0000000..a95c4c2 --- /dev/null +++ b/src/common/fileHelper.cpp @@ -0,0 +1,41 @@ +#include "fileHelper.h" + +#include + +std::string Recon::getPath(const std::string &aFullPath) +{ + size_t pos = aFullPath.find_last_of('/'); + if (pos != std::string::npos) + { + return aFullPath.substr(0, pos); + } + return ""; +} + +bool Recon::endsWithMat(const std::string &aStr) + { + if (aStr.length() >= 4) + { + return (aStr.compare(aStr.length() - 4, 4, ".mat") == 0); + } + return false; +} + +bool Recon::isDirectory(const std::string &aPath) +{ + struct stat info; + if (stat(aPath.c_str(), &info) != 0) + { + return false; + } + return (info.st_mode & S_IFDIR) != 0; +} + +std::string Recon::fixPathSlash(const std::string& aPath) +{ + if(aPath.back() == '/') + { + return aPath; + } + return aPath + '/'; +} \ No newline at end of file diff --git a/src/common/fileHelper.h b/src/common/fileHelper.h new file mode 100644 index 0000000..dc08f16 --- /dev/null +++ b/src/common/fileHelper.h @@ -0,0 +1,18 @@ +#ifndef FILE_HELPER_H +#define FILE_HELPER_H + +#include + +namespace Recon +{ + const std::string DEFAULT_CONFIG_PATH = "/home/sun/UR/build/"; + const std::string DEFAULT_OUTPUT_PATH = "/home/sun/USCT_Result.mat"; + const std::string DEFAULT_OUTPUT_FILENAME = "USCT_Result.mat"; + + std::string getPath(const std::string &aFullPath); + bool endsWithMat(const std::string &aStr); + bool isDirectory(const std::string &aPath); + std::string fixPathSlash(const std::string& aPath); +} + +#endif \ No newline at end of file diff --git a/src/config/config.cpp b/src/config/config.cpp index 9ee7770..db8bcd7 100644 --- a/src/config/config.cpp +++ b/src/config/config.cpp @@ -12,7 +12,6 @@ namespace Recon const std::string ReconParamsFileName = "configReconstruction_USCT3Dv3.json"; const std::string ReflectParamsFileName = "configReflectionReconstruction_USCT3Dv3.json"; const std::string TransParramsFileName = "configTransmissionReconstruction_USCT3Dv3.json"; - const std::string ConfigPath= "./"; Aurora::Matrix readToMatrix(const nlohmann::json& aJsonObj, bool aIsHorizontal = true) { @@ -37,14 +36,14 @@ namespace Recon return Aurora::Matrix(); } - void initalizeConfigFromFiles() + void initalizeConfigFromFiles(const std::string aConfigPath) { nlohmann::json reconParamsJson; nlohmann::json reflectParamsJson; nlohmann::json transParamsJson; - std::ifstream reconParamsFile(ReconParamsFileName); - std::ifstream reflectParamsFile(ReflectParamsFileName); - std::ifstream transParamsFile(TransParramsFileName); + std::ifstream reconParamsFile(aConfigPath + ReconParamsFileName); + std::ifstream reflectParamsFile(aConfigPath + ReflectParamsFileName); + std::ifstream transParamsFile(aConfigPath + TransParramsFileName); if(reconParamsFile.good()) { reconParamsFile >> reconParamsJson; @@ -111,7 +110,7 @@ namespace Recon } else { - RECON_INFO(ReconParamsFileName + " not found."); + RECON_INFO(aConfigPath + ReconParamsFileName + " not found."); } if(reflectParamsFile.good()) @@ -360,7 +359,7 @@ namespace Recon } else { - RECON_INFO(ReflectParamsFileName + " not found."); + RECON_INFO(aConfigPath + ReflectParamsFileName + " not found."); } if(transParamsFile.good()) @@ -611,14 +610,14 @@ namespace Recon } else { - RECON_INFO(TransParramsFileName + " not found."); + RECON_INFO(aConfigPath + TransParramsFileName + " not found."); } reflectParams::expectedAScanDataLength = reconParams::expectedAScanDataLength; reflectParams::gpuSelectionList = reconParams::gpuSelectionList; transParams::gpuSelectionList = reconParams::gpuSelectionList; } - void initalizeConfig() + void initalizeConfig(const std::string aConfigPath) { //reconParams.measurementInfo.ce reconParams::useCEMeasured = true; @@ -761,6 +760,6 @@ namespace Recon transParams::runTransmissionReco = true; transParams::nonNeg = false; - initalizeConfigFromFiles(); + initalizeConfigFromFiles(aConfigPath); } } diff --git a/src/config/config.h b/src/config/config.h index 50943b9..304448c 100644 --- a/src/config/config.h +++ b/src/config/config.h @@ -161,7 +161,7 @@ namespace Recon EXTERN_C bool nonNeg; } - void initalizeConfig(); + void initalizeConfig(const std::string aConfigPath); } #endif //RECON_CONFIG_H \ No newline at end of file diff --git a/src/main.cxx b/src/main.cxx index 6d38b66..6975aab 100644 --- a/src/main.cxx +++ b/src/main.cxx @@ -1,24 +1,66 @@ #include "config/config.h" +#include #define EIGEN_USE_MKL_ALL -#include -#include - - -#include - -#include "common/ceMatchedFilterHandling.h" -#include "MatlabReader.h" +#include "src/common/fileHelper.h" #include "startReconstructions.h" #include "log/log.h" -int main() +/* 0 is data path. + 1 is dataRef path. + 2 is output path. + 3 is config file path. +*/ + +int main(int argc, char *argv[]) { auto defaultLogger = getLogger("Main"); spdlog::set_default_logger(defaultLogger); + int argNum = 4; + std::vector args(argNum); + args[0] = ""; + args[1] = ""; + args[2] = Recon::DEFAULT_OUTPUT_PATH; + args[3] = Recon::DEFAULT_CONFIG_PATH; + argc = argc <= argNum? argc-1 : argNum; + for (int i = 0; i < argc; i++) + { + args[i] = argv[i+1]; + } + + if(args[0].empty()) + { + RECON_INFO("No reconstruction data."); + return 0; + } + std::string configPath = Recon::fixPathSlash(args[3]); + Recon::initalizeConfig(configPath); + if( args[1].empty() && Recon::transParams::runTransmissionReco) + { + RECON_INFO("Running transmission reconstruction, but no refrence data."); + return 0; + } + + std::string outPutPath = args[2]; + std::string directoryPath = outPutPath; + if(Recon::endsWithMat(outPutPath)) + { + directoryPath = Recon::getPath(outPutPath); + } + else + { + outPutPath = Recon::fixPathSlash(outPutPath) + Recon::DEFAULT_OUTPUT_FILENAME; + } + if(!Recon::isDirectory(directoryPath)) + { + RECON_INFO("Output directory is not valid."); + return 0; + } + + std::string dataPath = Recon::fixPathSlash(args[0]); + std::string dataRefPath = Recon::fixPathSlash(args[1]); RECON_INFO("start"); - Recon::initalizeConfig(); - Recon::startReconstructions(); + Recon::startReconstructions(dataPath, dataRefPath, outPutPath); SPDLOG_INFO("finish"); return 0; } diff --git a/src/startReconstructions.cpp b/src/startReconstructions.cpp index a9b16c3..d5fc740 100644 --- a/src/startReconstructions.cpp +++ b/src/startReconstructions.cpp @@ -29,18 +29,11 @@ using namespace Recon; using namespace Aurora; -void Recon::startReconstructions() +void Recon::startReconstructions(const std::string& aDataPath, const std::string& aDataRefPath, const std::string& aOutputPath) { - std::string dataPath = "/home/AScans_Data/ADW_TAS_Issue/20230418T145123/"; - std::string refPath = "/home/AScans_Data/ADW_TAS_Issue/20230418T141000/"; - //std::string dataPath = "/home/AScans_Data/volunteer_20230620/20230620T120410/"; - //std::string refPath = "/home/AScans_Data/volunteer_20230620/20230620T122424/"; - //std::string dataPath = "/home/AScans_Data/10VolunteerStudyData/20221107/20221107T142539"; - //std::string refPath = "/home/AScans_Data/10VolunteerStudyData/20221107/20221107T152522"; - std::string outputPath = "/home/sun/20230627_20230418T145123_UR.mat"; - MatlabWriter writer(outputPath); - Parser dataParser(dataPath); - Parser refParser(refPath); + MatlabWriter writer(aOutputPath); + Parser dataParser(aDataPath); + Parser refParser(aDataRefPath); //init total used receiver/emitter/motorPos Matrix motorPosTotal, slList, snList, rlList, rnList; if(transParams::runTransmissionReco && reflectParams::runReflectionReco) diff --git a/src/startReconstructions.h b/src/startReconstructions.h index 4febe4a..017958c 100644 --- a/src/startReconstructions.h +++ b/src/startReconstructions.h @@ -1,9 +1,10 @@ #ifndef START_RECONSTRUCTIONS_H #define START_RECONSTRUCTIONS_H +#include namespace Recon { - void startReconstructions(); + void startReconstructions(const std::string& aDataPath, const std::string& aDataRefPath, const std::string& aOutputPath); } #endif \ No newline at end of file diff --git a/test/Reconstruction_Test.cpp b/test/Reconstruction_Test.cpp index eb4ba67..c851b9f 100644 --- a/test/Reconstruction_Test.cpp +++ b/test/Reconstruction_Test.cpp @@ -67,7 +67,7 @@ TEST_F(Reconstruction_Test, determineOptimalPulse) { TEST_F(Reconstruction_Test, reconstructionSAFT) { - Recon::initalizeConfig(); + Recon::initalizeConfig(""); MatlabReader m("/home/sun/testData/recontructSAFT.mat"); auto Ascans = m.read("AScans"); auto AttenuationMap3D = m.read("AttenuationMap3D"); @@ -107,7 +107,7 @@ TEST_F(Reconstruction_Test, reconstructionSAFT) { } TEST_F(Reconstruction_Test, preprocessAScanBlockForReflection) { - Recon::initalizeConfig(); + Recon::initalizeConfig(""); MatlabReader m2("/home/krad/TestData/preprocessRefC.mat"); auto blockedAScans = m2.read("blockedAScans"); auto blockedMP = m2.read("blockedMP");