Skip to content

Commit

Permalink
ENH: Remove dependency on EbsdLib for the Parameter. (BlueQuartzSoftw…
Browse files Browse the repository at this point in the history
…are#702)

Checks should be done down in the specific filter using this parameter

Signed-off-by: Michael Jackson <[email protected]>
  • Loading branch information
imikejackson committed Oct 3, 2023
1 parent 27a4753 commit 32d735f
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,6 @@ IFilter::PreflightResult ImportHDF5Dataset::preflightImpl(const DataStructure& d

fs::path inputFilePath(inputFile);
std::string ext = inputFilePath.extension().string();
if(ext != ".h5" && ext != ".hdf5" && ext != ".dream3d")
{
return {nonstd::make_unexpected(std::vector<Error>{Error{-20002, fmt::format("The selected file '{}' is not an HDF5 file.", inputFilePath.filename().string())}})};
}

if(!fs::exists(inputFilePath))
{
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ Parameters ImportH5EspritDataFilter::parameters() const
params.insertSeparator(Parameters::Separator{"Input Parameters"});
params.insert(std::make_unique<OEMEbsdScanSelectionParameter>(k_SelectedScanNames_Key, "Scan Names", "The name of the scan in the .h5 file. EDAX can store multiple scans in a single file",
OEMEbsdScanSelectionParameter::ValueType{},
OEMEbsdScanSelectionParameter::AllowedManufacturers{EbsdLib::OEM::Bruker, EbsdLib::OEM::DREAM3D},
/* OEMEbsdScanSelectionParameter::AllowedManufacturers{EbsdLib::OEM::Bruker, EbsdLib::OEM::DREAM3D},*/
OEMEbsdScanSelectionParameter::EbsdReaderType::Esprit, OEMEbsdScanSelectionParameter::ExtensionsType{".h5"}));
params.insert(std::make_unique<Float32Parameter>(k_ZSpacing_Key, "Z Spacing (Microns)", "The spacing in microns between each layer.", 1.0f));
params.insert(std::make_unique<VectorFloat32Parameter>(k_Origin_Key, "Origin", "The origin of the volume", std::vector<float32>{0.0F, 0.0F, 0.0F}, std::vector<std::string>{"x", "y", "z"}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ Parameters ImportH5OimDataFilter::parameters() const
// Create the parameter descriptors that are needed for this filter
params.insertSeparator(Parameters::Separator{"Input Parameters"});
params.insert(std::make_unique<OEMEbsdScanSelectionParameter>(k_SelectedScanNames_Key, "Scan Names", "The name of the scan in the .h5 file. EDAX can store multiple scans in a single file",
OEMEbsdScanSelectionParameter::ValueType{}, OEMEbsdScanSelectionParameter::AllowedManufacturers{EbsdLib::OEM::EDAX},
OEMEbsdScanSelectionParameter::ValueType{},
/* OEMEbsdScanSelectionParameter::AllowedManufacturers{EbsdLib::OEM::EDAX},*/
OEMEbsdScanSelectionParameter::EbsdReaderType::Oim, OEMEbsdScanSelectionParameter::ExtensionsType{".h5"}));
params.insert(std::make_unique<Float32Parameter>(k_ZSpacing_Key, "Z Spacing (Microns)", "The spacing in microns between each layer.", 1.0f));
params.insert(std::make_unique<VectorFloat32Parameter>(k_Origin_Key, "Origin", "The origin of the volume", std::vector<float32>{0.0F, 0.0F, 0.0F}, std::vector<std::string>{"x", "y", "z"}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,6 @@
#include "complex/Common/StringLiteral.hpp"
#include "complex/Utilities/StringUtilities.hpp"

#include "H5Support/H5ScopedSentinel.h"
#include "H5Support/H5Utilities.h"

#include "EbsdLib/IO/BrukerNano/EspritConstants.h"
#include "EbsdLib/IO/BrukerNano/H5EspritReader.h"
#include "EbsdLib/IO/HKL/CtfFields.h"
#include "EbsdLib/IO/TSL/H5OIMReader.h"

#include <fmt/core.h>
#include <fmt/ranges.h>

Expand All @@ -28,10 +20,9 @@ constexpr StringLiteral k_ScanNames = "scan_names";

//-----------------------------------------------------------------------------
OEMEbsdScanSelectionParameter::OEMEbsdScanSelectionParameter(const std::string& name, const std::string& humanName, const std::string& helpText, const ValueType& defaultValue,
const AllowedManufacturers& allowedManufacturers, const EbsdReaderType& readerType, const ExtensionsType& extensionsType)
const EbsdReaderType& readerType, const ExtensionsType& extensionsType)
: ValueParameter(name, humanName, helpText)
, m_DefaultValue(defaultValue)
, m_AllowedManufacturers(allowedManufacturers)
, m_AvailableExtensions(extensionsType)
, m_ReaderType(readerType)
{
Expand Down Expand Up @@ -112,10 +103,10 @@ Result<std::any> OEMEbsdScanSelectionParameter::fromJson(const nlohmann::json& j
}

const auto orderCheck = json[k_StackingOrder].get<int32>();
if(orderCheck != EbsdLib::RefFrameZDir::LowtoHigh && orderCheck != EbsdLib::RefFrameZDir::HightoLow)
if(orderCheck != RefFrameZDir::k_LowtoHigh && orderCheck != RefFrameZDir::k_HightoLow)
{
return MakeErrorResult<std::any>(FilterParameter::Constants::k_Json_Value_Not_Enumeration, fmt::format("{}JSON value for key '{}' was not a valid ordering Value. [{}|{}] allowed.", prefix.view(),
k_StackingOrder.view(), EbsdLib::RefFrameZDir::LowtoHigh, EbsdLib::RefFrameZDir::HightoLow));
k_StackingOrder.view(), RefFrameZDir::k_LowtoHigh, RefFrameZDir::k_HightoLow));
}

ValueType value;
Expand Down Expand Up @@ -161,7 +152,7 @@ Result<std::any> OEMEbsdScanSelectionParameter::fromJson(const nlohmann::json& j
//-----------------------------------------------------------------------------
IParameter::UniquePointer OEMEbsdScanSelectionParameter::clone() const
{
return std::make_unique<OEMEbsdScanSelectionParameter>(name(), humanName(), helpText(), m_DefaultValue, m_AllowedManufacturers, m_ReaderType, m_AvailableExtensions);
return std::make_unique<OEMEbsdScanSelectionParameter>(name(), humanName(), helpText(), m_DefaultValue, m_ReaderType, m_AvailableExtensions);
}

//-----------------------------------------------------------------------------
Expand Down Expand Up @@ -193,98 +184,9 @@ Result<> OEMEbsdScanSelectionParameter::validate(const std::any& valueRef) const
{
return {nonstd::make_unexpected(std::vector<Error>{{-20032, fmt::format("File extension '{}' is not a valid file extension.", value.inputFilePath.extension().string())}})};
}

if(value.stackingOrder != EbsdLib::RefFrameZDir::LowtoHigh && value.stackingOrder != EbsdLib::RefFrameZDir::HightoLow)
{
errors.push_back({-20033, fmt::format("{} is not a valid value for the stacking order. Please choose either {} or {}", value.stackingOrder, EbsdLib::RefFrameZDir::LowtoHigh,
EbsdLib::RefFrameZDir::HightoLow)});
return {nonstd::make_unexpected(std::move(errors))};
}

std::list<std::string> scanNames;
int32 err = 0;
if(m_ReaderType == EbsdReaderType::Oim)
{
const H5OIMReader::Pointer reader = H5OIMReader::New();
reader->setFileName(value.inputFilePath.string());
err = reader->readScanNames(scanNames);
}
else
{
const H5EspritReader::Pointer reader = H5EspritReader::New();
reader->setFileName(value.inputFilePath.string());
err = reader->readScanNames(scanNames);
}

if(err < 0)
{
errors.push_back({-20034, fmt::format("H5 file '{}' could not be opened. Reported error code from the H5OIMReader class is '{}'", value.inputFilePath.string(), err)});
return {nonstd::make_unexpected(std::move(errors))};
}

const ManufacturerType manufacturer = ReadManufacturer(value.inputFilePath.string());
if(m_AllowedManufacturers.find(manufacturer) == m_AllowedManufacturers.end())
{
errors.push_back({-20035, fmt::format("Original data source type {} is not a valid manufacturer", fmt::underlying(manufacturer))});
return {nonstd::make_unexpected(std::move(errors))};
}

if(value.scanNames.empty())
{
errors.push_back({-20036, fmt::format("At least one scan must be chosen. Please select a scan from the list.")});
return {nonstd::make_unexpected(std::move(errors))};
}

return {};
}

//-----------------------------------------------------------------------------
OEMEbsdScanSelectionParameter::ManufacturerType OEMEbsdScanSelectionParameter::ReadManufacturer(const std::string& inputFile)
{
EbsdLib::OEM manuf = EbsdLib::OEM::Unknown;

const hid_t fid = H5Utilities::openFile(inputFile, true);
if(fid < 0)
{
return manuf;
}
H5ScopedFileSentinel sentinel(fid, false);
std::string dsetName;
std::list<std::string> names;
herr_t err = H5Utilities::getGroupObjects(fid, H5Utilities::CustomHDFDataTypes::Any, names);
auto findIter = std::find(names.begin(), names.end(), EbsdLib::H5OIM::Manufacturer);
if(findIter != names.end())
{
dsetName = EbsdLib::H5OIM::Manufacturer;
}

findIter = std::find(names.begin(), names.end(), EbsdLib::H5Esprit::Manufacturer);
if(findIter != names.end())
{
dsetName = EbsdLib::H5Esprit::Manufacturer;
}

std::string manufacturer("Unknown");
err = H5Lite::readStringDataset(fid, dsetName, manufacturer);
if(err < 0)
{
return manuf;
}
if(manufacturer == EbsdLib::H5OIM::EDAX)
{
manuf = EbsdLib::OEM::EDAX;
}
if(manufacturer == EbsdLib::H5Esprit::BrukerNano)
{
manuf = EbsdLib::OEM::Bruker;
}
if(manufacturer == "DREAM.3D")
{
manuf = EbsdLib::OEM::DREAM3D;
}
return manuf;
}

//-----------------------------------------------------------------------------
OEMEbsdScanSelectionParameter::ExtensionsType OEMEbsdScanSelectionParameter::getAvailableExtensions() const
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include "complex/Utilities/FilePathGenerator.hpp"
#include "complex/complex_export.hpp"

#include "EbsdLib/Core/EbsdLibConstants.h"

#include <filesystem>
#include <list>
#include <string>
Expand All @@ -16,6 +14,16 @@ namespace fs = std::filesystem;

namespace complex
{
/** @brief RefFrameZDir defined for the Stacking order of images into a 3D Volume. This is taken from
* EbsdLib. If EbsdLib changes, this should follow suit.
*/
namespace RefFrameZDir
{
inline constexpr uint32_t k_LowtoHigh = 0;
inline constexpr uint32_t k_HightoLow = 1;
inline constexpr uint32_t UnknownRefFrameZDirection = 2;
} // namespace RefFrameZDir

class COMPLEX_EXPORT OEMEbsdScanSelectionParameter : public ValueParameter
{

Expand All @@ -26,16 +34,16 @@ class COMPLEX_EXPORT OEMEbsdScanSelectionParameter : public ValueParameter
struct ValueType
{
fs::path inputFilePath;
int32 stackingOrder = EbsdLib::RefFrameZDir::LowtoHigh;
uint32_t stackingOrder = RefFrameZDir::k_LowtoHigh;
std::list<std::string> scanNames = {};
};
using ManufacturerType = EbsdLib::OEM;
using AllowedManufacturers = std::unordered_set<ManufacturerType>;

using ExtensionsType = std::unordered_set<std::string>;
enum EbsdReaderType : uint8
{
Oim = 0,
Esprit = 1
Esprit = 1,
H5Oina = 2
};

OEMEbsdScanSelectionParameter() = delete;
Expand All @@ -47,7 +55,8 @@ class COMPLEX_EXPORT OEMEbsdScanSelectionParameter : public ValueParameter
* @param helpText The help text that should be displayed to a user
* @param defaultValue The default value for the parameter
*/
OEMEbsdScanSelectionParameter(const std::string& name, const std::string& humanName, const std::string& helpText, const ValueType& defaultValue, const AllowedManufacturers& allowedManufacturers,
OEMEbsdScanSelectionParameter(const std::string& name, const std::string& humanName, const std::string& helpText, const ValueType& defaultValue,
/* const AllowedManufacturers& allowedManufacturers, */
const EbsdReaderType& readerType, const ExtensionsType& extensionsType);

~OEMEbsdScanSelectionParameter() override = default;
Expand Down Expand Up @@ -112,16 +121,8 @@ class COMPLEX_EXPORT OEMEbsdScanSelectionParameter : public ValueParameter
*/
EbsdReaderType getReaderType() const;

/**
* @brief
* @param inputFile
* @return
*/
static ManufacturerType ReadManufacturer(const std::string& inputFile);

private:
ValueType m_DefaultValue = {};
AllowedManufacturers m_AllowedManufacturers = {};
ExtensionsType m_AvailableExtensions = {};
EbsdReaderType m_ReaderType = {};
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ struct ORIENTATIONANALYSIS_EXPORT ImportH5DataInputValues
DataPath ImageGeometryPath;
DataPath CellAttributeMatrixPath;
DataPath CellEnsembleAttributeMatrixPath;
bool EdaxHexagonalAlignment;
bool ConvertPhaseToInt32;
};

/**
Expand Down Expand Up @@ -55,6 +57,8 @@ class ORIENTATIONANALYSIS_EXPORT ImportH5Data
int index = 0;
for(const auto& currentScanName : m_InputValues->SelectedScanNames.scanNames)
{
m_MessageHandler({IFilter::Message::Type::Info, fmt::format("Importing Index {}", currentScanName)});

Result<> readResults = readData(currentScanName);
if(readResults.invalid())
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,11 @@ PYBIND11_MODULE(orientationanalysis, mod)
oemEbsdScanSelectionEbsdReaderType.value("Oim", OEMEbsdScanSelectionParameter::EbsdReaderType::Oim);
oemEbsdScanSelectionEbsdReaderType.value("Esprit", OEMEbsdScanSelectionParameter::EbsdReaderType::Esprit);

py::enum_<OEMEbsdScanSelectionParameter::ManufacturerType> oemEbsdScanSelectionManufacturerType(oemEbsdScanSelectionParameter, "ManufacturerType");
oemEbsdScanSelectionManufacturerType.value("EDAX", OEMEbsdScanSelectionParameter::ManufacturerType::EDAX);
oemEbsdScanSelectionManufacturerType.value("Oxford", OEMEbsdScanSelectionParameter::ManufacturerType::Oxford);
oemEbsdScanSelectionManufacturerType.value("Bruker", OEMEbsdScanSelectionParameter::ManufacturerType::Bruker);
oemEbsdScanSelectionManufacturerType.value("HEDM", OEMEbsdScanSelectionParameter::ManufacturerType::HEDM);
oemEbsdScanSelectionManufacturerType.value("Zeiss", OEMEbsdScanSelectionParameter::ManufacturerType::Zeiss);
oemEbsdScanSelectionManufacturerType.value("Phillips", OEMEbsdScanSelectionParameter::ManufacturerType::Phillips);
oemEbsdScanSelectionManufacturerType.value("ThermoFisher", OEMEbsdScanSelectionParameter::ManufacturerType::ThermoFisher);
oemEbsdScanSelectionManufacturerType.value("DREAM3D", OEMEbsdScanSelectionParameter::ManufacturerType::DREAM3D);
oemEbsdScanSelectionManufacturerType.value("Unknown", OEMEbsdScanSelectionParameter::ManufacturerType::Unknown);

BindParameterConstructor(h5EbsdReaderParameter);

oemEbsdScanSelectionParameter.def(
py::init<const std::string&, const std::string&, const std::string&, const OEMEbsdScanSelectionParameter::ValueType&, const OEMEbsdScanSelectionParameter::AllowedManufacturers&,
OEMEbsdScanSelectionParameter::EbsdReaderType, OEMEbsdScanSelectionParameter::ExtensionsType>(),
"name"_a, "human_name"_a, "help_text"_a, "default_value"_a, "allowed_manufacturers"_a, "reader_type"_a, "extensions_type"_a);
oemEbsdScanSelectionParameter.def(py::init<const std::string&, const std::string&, const std::string&, const OEMEbsdScanSelectionParameter::ValueType&, OEMEbsdScanSelectionParameter::EbsdReaderType,
OEMEbsdScanSelectionParameter::ExtensionsType>(),
"name"_a, "human_name"_a, "help_text"_a, "default_value"_a, "reader_type"_a, "extensions_type"_a);

internals.addConversion<H5EbsdReaderParameter>();
internals.addConversion<OEMEbsdScanSelectionParameter>();
Expand Down

0 comments on commit 32d735f

Please sign in to comment.