|
| 1 | +#include "ComputeArrayNormFilter.hpp" |
| 2 | + |
| 3 | +#include "SimplnxReview/Filters/Algorithms/ComputeArrayNorm.hpp" |
| 4 | + |
| 5 | +#include "simplnx/DataStructure/DataArray.hpp" |
| 6 | +#include "simplnx/DataStructure/DataPath.hpp" |
| 7 | +#include "simplnx/Filter/Actions/CreateArrayAction.hpp" |
| 8 | +#include "simplnx/Parameters/ArraySelectionParameter.hpp" |
| 9 | +#include "simplnx/Parameters/DataObjectNameParameter.hpp" |
| 10 | +#include "simplnx/Parameters/NumberParameter.hpp" |
| 11 | +#include "simplnx/Utilities/SIMPLConversion.hpp" |
| 12 | + |
| 13 | +using namespace nx::core; |
| 14 | + |
| 15 | +namespace nx::core |
| 16 | +{ |
| 17 | +//------------------------------------------------------------------------------ |
| 18 | +std::string ComputeArrayNormFilter::name() const |
| 19 | +{ |
| 20 | + return FilterTraits<ComputeArrayNormFilter>::name.str(); |
| 21 | +} |
| 22 | + |
| 23 | +//------------------------------------------------------------------------------ |
| 24 | +std::string ComputeArrayNormFilter::className() const |
| 25 | +{ |
| 26 | + return FilterTraits<ComputeArrayNormFilter>::className; |
| 27 | +} |
| 28 | + |
| 29 | +//------------------------------------------------------------------------------ |
| 30 | +Uuid ComputeArrayNormFilter::uuid() const |
| 31 | +{ |
| 32 | + return FilterTraits<ComputeArrayNormFilter>::uuid; |
| 33 | +} |
| 34 | + |
| 35 | +//------------------------------------------------------------------------------ |
| 36 | +std::string ComputeArrayNormFilter::humanName() const |
| 37 | +{ |
| 38 | + return "Compute Array Norm"; |
| 39 | +} |
| 40 | + |
| 41 | +//------------------------------------------------------------------------------ |
| 42 | +std::vector<std::string> ComputeArrayNormFilter::defaultTags() const |
| 43 | +{ |
| 44 | + return {className(), "Statistics", "DREAM3DReview"}; |
| 45 | +} |
| 46 | + |
| 47 | +//------------------------------------------------------------------------------ |
| 48 | +Parameters ComputeArrayNormFilter::parameters() const |
| 49 | +{ |
| 50 | + Parameters params; |
| 51 | + |
| 52 | + params.insertSeparator(Parameters::Separator{"Input Parameter"}); |
| 53 | + params.insert(std::make_unique<Float32Parameter>(k_PSpace_Key, "p-Space Value", "p-Value used for computing the norm (2 = Euclidean, 1 = Manhattan)", 2.0f)); |
| 54 | + |
| 55 | + params.insertSeparator(Parameters::Separator{"Input Data"}); |
| 56 | + params.insert(std::make_unique<ArraySelectionParameter>(k_SelectedArrayPath_Key, "Input Attribute Array", "The input array for computing the norm", DataPath{}, |
| 57 | + ArraySelectionParameter::AllowedTypes{DataType::int8, DataType::uint8, DataType::int16, DataType::uint16, DataType::int32, DataType::uint32, |
| 58 | + DataType::int64, DataType::uint64, DataType::float32, DataType::float64}, |
| 59 | + ArraySelectionParameter::AllowedComponentShapes{})); |
| 60 | + |
| 61 | + params.insertSeparator(Parameters::Separator{"Output Data"}); |
| 62 | + params.insert(std::make_unique<DataObjectNameParameter>(k_NormArrayName_Key, "Norm Array Name", "The name of the output norm array", "Norm")); |
| 63 | + |
| 64 | + return params; |
| 65 | +} |
| 66 | + |
| 67 | +//------------------------------------------------------------------------------ |
| 68 | +IFilter::UniquePointer ComputeArrayNormFilter::clone() const |
| 69 | +{ |
| 70 | + return std::make_unique<ComputeArrayNormFilter>(); |
| 71 | +} |
| 72 | + |
| 73 | +//------------------------------------------------------------------------------ |
| 74 | +IFilter::VersionType ComputeArrayNormFilter::parametersVersion() const |
| 75 | +{ |
| 76 | + return 1; |
| 77 | +} |
| 78 | + |
| 79 | +//------------------------------------------------------------------------------ |
| 80 | +IFilter::PreflightResult ComputeArrayNormFilter::preflightImpl(const DataStructure& dataStructure, const Arguments& filterArgs, const MessageHandler& messageHandler, |
| 81 | + const std::atomic_bool& shouldCancel, const ExecutionContext& executionContext) const |
| 82 | +{ |
| 83 | + auto pSpaceValue = filterArgs.value<float32>(k_PSpace_Key); |
| 84 | + auto pSelectedArrayPathValue = filterArgs.value<DataPath>(k_SelectedArrayPath_Key); |
| 85 | + auto pNormArrayNameValue = filterArgs.value<std::string>(k_NormArrayName_Key); |
| 86 | + |
| 87 | + PreflightResult preflightResult; |
| 88 | + nx::core::Result<OutputActions> resultOutputActions; |
| 89 | + std::vector<PreflightValue> preflightUpdatedValues; |
| 90 | + |
| 91 | + if(pSpaceValue < 0.0f) |
| 92 | + { |
| 93 | + return {MakeErrorResult<OutputActions>(-11002, "p-space value must be greater than or equal to 0")}; |
| 94 | + } |
| 95 | + |
| 96 | + const auto* inputArray = dataStructure.getDataAs<IDataArray>(pSelectedArrayPathValue); |
| 97 | + if(inputArray == nullptr) |
| 98 | + { |
| 99 | + return {MakeErrorResult<OutputActions>(-11003, fmt::format("Cannot find the selected input array at path '{}'", pSelectedArrayPathValue.toString()))}; |
| 100 | + } |
| 101 | + |
| 102 | + DataPath normArrayPath = pSelectedArrayPathValue.replaceName(pNormArrayNameValue); |
| 103 | + |
| 104 | + { |
| 105 | + auto createArrayAction = std::make_unique<CreateArrayAction>(DataType::float32, inputArray->getTupleShape(), std::vector<usize>{1}, normArrayPath); |
| 106 | + resultOutputActions.value().appendAction(std::move(createArrayAction)); |
| 107 | + } |
| 108 | + |
| 109 | + return {std::move(resultOutputActions), std::move(preflightUpdatedValues)}; |
| 110 | +} |
| 111 | + |
| 112 | +//------------------------------------------------------------------------------ |
| 113 | +Result<> ComputeArrayNormFilter::executeImpl(DataStructure& dataStructure, const Arguments& filterArgs, const PipelineFilter* pipelineNode, const MessageHandler& messageHandler, |
| 114 | + const std::atomic_bool& shouldCancel, const ExecutionContext& executionContext) const |
| 115 | +{ |
| 116 | + ComputeArrayNormInputValues inputValues; |
| 117 | + |
| 118 | + inputValues.PSpace = filterArgs.value<float32>(k_PSpace_Key); |
| 119 | + inputValues.SelectedArrayPath = filterArgs.value<DataPath>(k_SelectedArrayPath_Key); |
| 120 | + inputValues.NormArrayPath = inputValues.SelectedArrayPath.replaceName(filterArgs.value<std::string>(k_NormArrayName_Key)); |
| 121 | + |
| 122 | + return ComputeArrayNorm(dataStructure, messageHandler, shouldCancel, &inputValues)(); |
| 123 | +} |
| 124 | + |
| 125 | +namespace |
| 126 | +{ |
| 127 | +namespace SIMPL |
| 128 | +{ |
| 129 | +constexpr StringLiteral k_SelectedArrayPathKey = "SelectedArrayPath"; |
| 130 | +constexpr StringLiteral k_NormArrayPathKey = "NormArrayPath"; |
| 131 | +constexpr StringLiteral k_PSpaceKey = "PSpace"; |
| 132 | +} // namespace SIMPL |
| 133 | +} // namespace |
| 134 | + |
| 135 | +Result<Arguments> ComputeArrayNormFilter::FromSIMPLJson(const nlohmann::json& json) |
| 136 | +{ |
| 137 | + Arguments args = ComputeArrayNormFilter().getDefaultArguments(); |
| 138 | + |
| 139 | + std::vector<Result<>> results; |
| 140 | + |
| 141 | + results.push_back(SIMPLConversion::ConvertParameter<SIMPLConversion::FloatFilterParameterConverter<float32>>(args, json, SIMPL::k_PSpaceKey, k_PSpace_Key)); |
| 142 | + results.push_back(SIMPLConversion::ConvertParameter<SIMPLConversion::DataArraySelectionFilterParameterConverter>(args, json, SIMPL::k_SelectedArrayPathKey, k_SelectedArrayPath_Key)); |
| 143 | + results.push_back(SIMPLConversion::ConvertParameter<SIMPLConversion::DataArrayCreationToDataObjectNameFilterParameterConverter>(args, json, SIMPL::k_NormArrayPathKey, k_NormArrayName_Key)); |
| 144 | + |
| 145 | + Result<> conversionResult = MergeResults(std::move(results)); |
| 146 | + |
| 147 | + return ConvertResultTo<Arguments>(std::move(conversionResult), std::move(args)); |
| 148 | +} |
| 149 | +} // namespace nx::core |
0 commit comments