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

ENH : Add support for getting the pipeline json object from a dream3d file #890

Merged
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
1 change: 0 additions & 1 deletion src/simplnx/Pipeline/Pipeline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ constexpr StringLiteral k_PipelineItemsKey = "pipeline";
constexpr StringLiteral k_PipelineVersionKey = "version";
constexpr uint64 k_PipelineVersion = 1;

constexpr StringLiteral k_SIMPLPipelineBuilderKey = "PipelineBuilder";
constexpr StringLiteral k_SIMPLPipelineNameKey = "Name";
constexpr StringLiteral k_SIMPLNumFilterseKey = "Number_Filters";

Expand Down
1 change: 1 addition & 0 deletions src/simplnx/Pipeline/Pipeline.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class SIMPLNX_EXPORT Pipeline : public AbstractPipelineNode, protected PipelineN
using const_iterator = collection_type::const_iterator;

static constexpr StringLiteral k_Extension = ".d3dpipeline";
static constexpr StringLiteral k_SIMPLPipelineBuilderKey = "PipelineBuilder";

/**
* @brief Constructs a Pipeline from json.
Expand Down
41 changes: 37 additions & 4 deletions src/simplnx/Utilities/Parsing/DREAM3D/Dream3dIO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1331,21 +1331,39 @@ Result<DataStructure> DREAM3D::ImportDataStructureFromFile(const std::filesystem

Result<Pipeline> DREAM3D::ImportPipelineFromFile(const nx::core::HDF5::FileReader& fileReader)
{
if(GetPipelineVersion(fileReader) != k_CurrentPipelineVersion)
Result<nlohmann::json> pipelineJson = ImportPipelineJsonFromFile(fileReader);
if(pipelineJson.invalid())
{
return MakeErrorResult<Pipeline>(k_InvalidPipelineVersion, fmt::format("Could not parse Pipeline version '{}'. Expected version: '{}'", GetPipelineVersion(fileReader), k_CurrentFileVersion));
return ConvertInvalidResult<Pipeline, nlohmann::json>(std::move(pipelineJson));
}
const auto fileVersion = GetFileVersion(fileReader);
if(fileVersion == k_CurrentFileVersion)
{
if(GetPipelineVersion(fileReader) != k_CurrentPipelineVersion)
{
return MakeErrorResult<Pipeline>(k_InvalidPipelineVersion, fmt::format("Could not parse Pipeline version '{}'. Expected version: '{}'", GetPipelineVersion(fileReader), k_CurrentFileVersion));
}
return Pipeline::FromJson(pipelineJson.value());
}
if(fileVersion == Legacy::FileVersion)
{
return Pipeline::FromSIMPLJson(pipelineJson.value());
}
return MakeErrorResult<Pipeline>(k_InvalidPipelineVersion, fmt::format("Could not parse file version '{}'", k_CurrentFileVersion));
}

Result<nlohmann::json> DREAM3D::ImportPipelineJsonFromFile(const nx::core::HDF5::FileReader& fileReader)
{
auto pipelineGroupReader = fileReader.openGroup(k_PipelineJsonTag);
auto pipelineDatasetReader = pipelineGroupReader.openDataset(k_PipelineJsonTag);
if(!pipelineDatasetReader.isValid())
{
return MakeErrorResult<Pipeline>(k_PipelineGroupUnavailable, "Could not open '/Pipeline' HDF5 Group.");
return MakeErrorResult<nlohmann::json>(k_PipelineGroupUnavailable, "Could not open '/Pipeline' HDF5 Group.");
}

auto pipelineJsonString = pipelineDatasetReader.readAsString();
auto pipelineJson = nlohmann::json::parse(pipelineJsonString);
return Pipeline::FromJson(pipelineJson);
return {pipelineJson};
}

Result<Pipeline> DREAM3D::ImportPipelineFromFile(const std::filesystem::path& filePath)
Expand All @@ -1363,6 +1381,21 @@ Result<Pipeline> DREAM3D::ImportPipelineFromFile(const std::filesystem::path& fi
return ImportPipelineFromFile(fileReader);
}

Result<nlohmann::json> DREAM3D::ImportPipelineJsonFromFile(const std::filesystem::path& filePath)
{
if(!std::filesystem::exists(filePath))
{
return MakeErrorResult<nlohmann::json>(-1, fmt::format("DREAM3D::ImportPipelineFromFile: File does not exist. '{}'", filePath.string()));
}
nx::core::HDF5::FileReader fileReader(filePath);
if(!fileReader.isValid())
{
return MakeErrorResult<nlohmann::json>(-1, fmt::format("DREAM3D::ImportPipelineFromFile: Unable to open '{}' for reading", filePath.string()));
}

return ImportPipelineJsonFromFile(fileReader);
}

Result<DREAM3D::FileData> DREAM3D::ReadFile(const nx::core::HDF5::FileReader& fileReader, bool preflight)
{
// Pipeline pipeline;
Expand Down
17 changes: 17 additions & 0 deletions src/simplnx/Utilities/Parsing/DREAM3D/Dream3dIO.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,23 @@ SIMPLNX_EXPORT Result<DataStructure> ImportDataStructureFromFile(const std::file
*/
SIMPLNX_EXPORT Result<Pipeline> ImportPipelineFromFile(const nx::core::HDF5::FileReader& fileReader);

/**
* @brief Imports and returns a Pipeline from the target .dream3d file.
*
* This method does not import legacy Pipelines.
* @param fileReader
* @return Pipeline
*/
SIMPLNX_EXPORT Result<nlohmann::json> ImportPipelineJsonFromFile(const nx::core::HDF5::FileReader& fileReader);

/**
* @brief Imports and returns a Pipeline from the target .dream3d file.
* This method does not import legacy Pipelines.
* @param fileReader
* @return Pipeline
*/
SIMPLNX_EXPORT Result<nlohmann::json> ImportPipelineJsonFromFile(const std::filesystem::path& filePath);

/**
* @brief Imports and returns a Pipeline from the target .dream3d file.
* This method does not import legacy Pipelines.
Expand Down
Loading