Skip to content

Commit

Permalink
ENH: ReadCSVFilter and TriangleCentroidFilter improvements (#854)
Browse files Browse the repository at this point in the history
Signed-off-by: Michael Jackson <[email protected]>
Signed-off-by: Joey Kleingers <[email protected]>
Co-authored-by: Michael Jackson <[email protected]>
  • Loading branch information
joeykleingers and imikejackson authored Feb 12, 2024
1 parent b568c7b commit dcdb63e
Show file tree
Hide file tree
Showing 10 changed files with 198 additions and 137 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -688,6 +688,7 @@ set(SIMPLNX_SRCS
${SIMPLNX_SOURCE_DIR}/Utilities/ArrayThreshold.cpp
${SIMPLNX_SOURCE_DIR}/Utilities/FilePathGenerator.cpp
${SIMPLNX_SOURCE_DIR}/Utilities/FilterUtilities.cpp
${SIMPLNX_SOURCE_DIR}/Utilities/FileUtilities.cpp
${SIMPLNX_SOURCE_DIR}/Utilities/TooltipGenerator.cpp
${SIMPLNX_SOURCE_DIR}/Utilities/TooltipRowItem.cpp
${SIMPLNX_SOURCE_DIR}/Utilities/DataArrayUtilities.cpp
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ Result<> FeatureFaceCurvature::operator()()
g->wait(); // Wait for all the threads to complete before moving on.
#endif

// Remove elements containing vertices, because Element neighbors created it quietly under the covers
triangleGeomPtr->deleteElementsContainingVert();
return {};
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace
class CalculateCentroidsImpl
{
public:
CalculateCentroidsImpl(const TriangleGeom* triangleGeom, Float32Array* centroids, const std::atomic_bool& shouldCancel)
CalculateCentroidsImpl(const TriangleGeom* triangleGeom, Float64Array* centroids, const std::atomic_bool& shouldCancel)
: m_TriangleGeom(triangleGeom)
, m_Centroids(centroids)
, m_ShouldCancel(shouldCancel)
Expand Down Expand Up @@ -52,7 +52,7 @@ class CalculateCentroidsImpl

private:
const TriangleGeom* m_TriangleGeom = nullptr;
Float32Array* m_Centroids = nullptr;
Float64Array* m_Centroids = nullptr;
const std::atomic_bool& m_ShouldCancel;
};
} // namespace
Expand Down Expand Up @@ -83,7 +83,7 @@ Result<> TriangleCentroid::operator()()
const AttributeMatrix& faceAttributeMatrix = triangleGeom->getFaceAttributeMatrixRef();

const DataPath pCentroidsPath = m_InputValues->TriangleGeometryDataPath.createChildPath(faceAttributeMatrix.getName()).createChildPath(m_InputValues->CentroidsArrayName);
auto* centroidsArray = m_DataStructure.getDataAs<Float32Array>(pCentroidsPath);
auto* centroidsArray = m_DataStructure.getDataAs<Float64Array>(pCentroidsPath);

// Parallel algorithm to calculate the centroids
ParallelDataAlgorithm dataAlg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,10 @@
#include "simplnx/Parameters/DynamicTableParameter.hpp"
#include "simplnx/Parameters/ReadCSVFileParameter.hpp"
#include "simplnx/Utilities/FileUtilities.hpp"
#include "simplnx/Utilities/FilterUtilities.hpp"
#include "simplnx/Utilities/StringUtilities.hpp"

#include "simplnx/Utilities/SIMPLConversion.hpp"
#include "simplnx/Utilities/StringUtilities.hpp"

#include <cstdio>
#include <fstream>

using namespace nx::core;
Expand Down Expand Up @@ -309,6 +308,7 @@ IFilter::PreflightResult readHeaders(const std::string& inputFilePath, usize hea
headerCache.HeadersLine = headersLineNum;
return {};
}

} // namespace

namespace nx::core
Expand Down Expand Up @@ -421,6 +421,11 @@ IFilter::PreflightResult ReadCSVFileFilter::preflightImpl(const DataStructure& d
StringVector headers;
if(readCSVData.inputFilePath != s_HeaderCache[s_InstanceId].FilePath)
{
int64 lineCount = nx::core::FileUtilities::LinesInFile(inputFilePath);
if(lineCount < 0)
{
return {MakeErrorResult<OutputActions>(to_underlying(IssueCodes::FILE_NOT_OPEN), fmt::format("Could not open file for reading: {}", inputFilePath)), {}};
}
std::fstream in(inputFilePath.c_str(), std::ios_base::in);
if(!in.is_open())
{
Expand All @@ -429,17 +434,18 @@ IFilter::PreflightResult ReadCSVFileFilter::preflightImpl(const DataStructure& d

s_HeaderCache[s_InstanceId].FilePath = readCSVData.inputFilePath;

usize lineCount = 0;
usize currentLine = 0;
while(!in.eof())
{
std::string line;
std::getline(in, line);
lineCount++;
currentLine++;

if(headerMode == ReadCSVData::HeaderMode::LINE && lineCount == readCSVData.headersLine)
if(headerMode == ReadCSVData::HeaderMode::LINE && currentLine == readCSVData.headersLine)
{
s_HeaderCache[s_InstanceId].Headers = line;
s_HeaderCache[s_InstanceId].HeadersLine = readCSVData.headersLine;
break;
}
}

Expand Down Expand Up @@ -704,7 +710,6 @@ namespace
namespace SIMPL
{
constexpr StringLiteral k_SelectedPathKey = "Wizard_SelectedPath";
constexpr StringLiteral k_TupleDimsKey = "Wizard_TupleDims";
} // namespace SIMPL
} // namespace

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ IFilter::PreflightResult TriangleCentroidFilter::preflightImpl(const DataStructu
{
DataPath createArrayDataPath = pTriangleGeometryDataPath.createChildPath(faceAttributeMatrix->getName()).createChildPath(pCentroidsArrayName);
// Create the face areas DataArray Action and store it into the resultOutputActions
auto createArrayAction = std::make_unique<CreateArrayAction>(nx::core::DataType::float32, std::vector<usize>{triangleGeom->getNumberOfFaces()}, std::vector<usize>{3}, createArrayDataPath);
auto createArrayAction = std::make_unique<CreateArrayAction>(nx::core::DataType::float64, std::vector<usize>{triangleGeom->getNumberOfFaces()}, std::vector<usize>{3}, createArrayDataPath);
resultOutputActions.value().appendAction(std::move(createArrayAction));
}

Expand Down
59 changes: 21 additions & 38 deletions src/Plugins/SimplnxCore/test/ReadCSVFileTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ using namespace nx::core;
namespace
{
const fs::path k_TestInput = fs::path(unit_test::k_BinaryDir.view()) / "ReadCSVFileTest" / "Input.txt";
constexpr int32 k_InvalidArgumentErrorCode = -100;
constexpr int32 k_OverflowErrorCode = -101;
constexpr int32 k_InvalidArgumentErrorCode = -10351;
constexpr int32 k_OverflowErrorCode = -10353;
constexpr int32 k_BlankLineErrorCode = -119;
constexpr int32 k_EmptyFile = -100;
constexpr int32 k_InconsistentCols = -104;
Expand Down Expand Up @@ -79,7 +79,21 @@ void CreateTestDataFile(const fs::path& inputFilePath, nonstd::span<std::string>
}
}

// -----------------------------------------------------------------------------
/**
*
* @param inputFilePath
* @param startImportRow
* @param headerMode
* @param headersLine
* @param delimiters
* @param customHeaders
* @param dataTypes
* @param skippedArrayMask
* @param tupleDims
* @param values
* @param newGroupName
* @return
*/
Arguments createArguments(const std::string& inputFilePath, usize startImportRow, ReadCSVData::HeaderMode headerMode, usize headersLine, const std::vector<char>& delimiters,
const std::vector<std::string>& customHeaders, const std::vector<DataType>& dataTypes, const std::vector<bool>& skippedArrayMask, const std::vector<usize>& tupleDims,
nonstd::span<std::string> values, const std::string& newGroupName)
Expand Down Expand Up @@ -159,10 +173,11 @@ void TestCase_TestPrimitives_Error(nonstd::span<std::string> values, int32 expec
std::string arrayName = "Array";
DataPath arrayPath = DataPath({newGroupName, arrayName});

usize tupleCount = std::count_if(values.begin(), values.end(), [](const std::string& s) { return !s.empty(); });

ReadCSVFileFilter filter;
DataStructure dataStructure;
Arguments args =
createArguments(k_TestInput.string(), 2, ReadCSVData::HeaderMode::LINE, 1, {','}, {arrayName}, {GetDataType<T>()}, {false}, {static_cast<usize>(values.size())}, values, newGroupName);
Arguments args = createArguments(k_TestInput.string(), 2, ReadCSVData::HeaderMode::LINE, 1, {','}, {arrayName}, {GetDataType<T>()}, {false}, {tupleCount}, values, newGroupName);

// Create the test input data file
fs::create_directories(k_TestInput.parent_path());
Expand Down Expand Up @@ -542,37 +557,5 @@ TEST_CASE("SimplnxCore::ReadCSVFileFilter (Case 6): Invalid filter execution - B
v = {std::to_string(std::numeric_limits<bool>::min()), "", std::to_string(std::numeric_limits<bool>::max())};
TestCase_TestPrimitives_Error<bool>(v, k_BlankLineErrorCode);

// End line blank tests
v = {std::to_string(std::numeric_limits<int8>::min()), std::to_string(std::numeric_limits<int8>::max()), ""};
TestCase_TestPrimitives_Error<int8>(v, k_BlankLineErrorCode);

v = {std::to_string(std::numeric_limits<int16>::min()), std::to_string(std::numeric_limits<int16>::max()), ""};
TestCase_TestPrimitives_Error<int16>(v, k_BlankLineErrorCode);

v = {std::to_string(std::numeric_limits<int32>::min()), std::to_string(std::numeric_limits<int32>::max()), ""};
TestCase_TestPrimitives_Error<int32>(v, k_BlankLineErrorCode);

v = {std::to_string(std::numeric_limits<int64>::min()), std::to_string(std::numeric_limits<int64>::max()), ""};
TestCase_TestPrimitives_Error<int64>(v, k_BlankLineErrorCode);

v = {std::to_string(std::numeric_limits<uint8>::min()), std::to_string(std::numeric_limits<uint8>::max()), ""};
TestCase_TestPrimitives_Error<uint8>(v, k_BlankLineErrorCode);

v = {std::to_string(std::numeric_limits<uint16>::min()), std::to_string(std::numeric_limits<uint16>::max()), ""};
TestCase_TestPrimitives_Error<uint16>(v, k_BlankLineErrorCode);

v = {std::to_string(std::numeric_limits<uint32>::min()), std::to_string(std::numeric_limits<uint32>::max()), ""};
TestCase_TestPrimitives_Error<uint32>(v, k_BlankLineErrorCode);

v = {std::to_string(std::numeric_limits<uint64>::min()), std::to_string(std::numeric_limits<uint64>::max()), ""};
TestCase_TestPrimitives_Error<uint64>(v, k_BlankLineErrorCode);

v = {std::to_string(std::numeric_limits<float32>::min()), std::to_string(std::numeric_limits<float32>::max()), ""};
TestCase_TestPrimitives_Error<float32>(v, k_BlankLineErrorCode);

v = {std::to_string(std::numeric_limits<float64>::min()), std::to_string(std::numeric_limits<float64>::max()), ""};
TestCase_TestPrimitives_Error<float64>(v, k_BlankLineErrorCode);

v = {std::to_string(std::numeric_limits<bool>::min()), std::to_string(std::numeric_limits<bool>::max()), ""};
TestCase_TestPrimitives_Error<bool>(v, k_BlankLineErrorCode);
// Blank lines at the end of the file are not counted in the line count
}
2 changes: 1 addition & 1 deletion src/Plugins/SimplnxCore/test/TriangleCentroidTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ TEST_CASE("SimplnxCore::TriangleCentroidFilter", "[SimplnxCore][TriangleCentroid

std::vector<Point3Df> centroids = {{0.5F, 0.333333F, 0.0F}, {3.0F, 0.333333, 0.0F}, {6.66667, 0.0F, 0.0F}, {9.0F, 0.0F, 0.0F}};

const auto& calculatedCentroids = dataStructure.getDataRefAs<Float32Array>(triangleCentroidsDataPath);
const auto& calculatedCentroids = dataStructure.getDataRefAs<Float64Array>(triangleCentroidsDataPath);

for(size_t t = 0; t < 4; t++)
{
Expand Down
12 changes: 6 additions & 6 deletions src/simplnx/Utilities/DataArrayUtilities.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,15 @@ namespace
value = FUNCTION(input); \
} catch(const std::invalid_argument& e) \
{ \
return nx::core::MakeErrorResult<TYPE>(-100, fmt::format("Error trying to convert '{}' to type '{}' using function '{}'", input, #TYPE, #FUNCTION)); \
return nx::core::MakeErrorResult<TYPE>(-10351, fmt::format("Error trying to convert '{}' to type '{}' using function '{}'", input, #TYPE, #FUNCTION)); \
} catch(const std::out_of_range& e) \
{ \
return nx::core::MakeErrorResult<TYPE>(-101, fmt::format("Overflow error trying to convert '{}' to type '{}' using function '{}'", input, #TYPE, #FUNCTION)); \
return nx::core::MakeErrorResult<TYPE>(-10353, fmt::format("Overflow error trying to convert '{}' to type '{}' using function '{}'", input, #TYPE, #FUNCTION)); \
} \
\
if(value > std::numeric_limits<TYPE>::max() || value < std::numeric_limits<TYPE>::min()) \
{ \
return nx::core::MakeErrorResult<TYPE>(-101, fmt::format("Overflow error trying to convert '{}' to type '{}' using function '{}'", input, #TYPE, #FUNCTION)); \
return nx::core::MakeErrorResult<TYPE>(-10353, fmt::format("Overflow error trying to convert '{}' to type '{}' using function '{}'", input, #TYPE, #FUNCTION)); \
} \
\
return {static_cast<TYPE>(value)};
Expand All @@ -73,7 +73,7 @@ namespace
{ \
if(!input.empty() && input.at(0) == '-') \
{ \
return nx::core::MakeErrorResult<TYPE>(-101, fmt::format("Overflow error trying to convert '{}' to type '{}' using function '{}'", input, #TYPE, #FUNCTION)); \
return nx::core::MakeErrorResult<TYPE>(-10353, fmt::format("Overflow error trying to convert '{}' to type '{}' using function '{}'", input, #TYPE, #FUNCTION)); \
} \
\
SIMPLNX_DEF_STRING_CONVERTOR_INT(CONTAINER_TYPE, TYPE, FUNCTION) \
Expand All @@ -92,10 +92,10 @@ namespace
value = static_cast<TYPE>(FUNCTION(input)); \
} catch(const std::invalid_argument& e) \
{ \
return nx::core::MakeErrorResult<TYPE>(-100, fmt::format("Error trying to convert '{}' to type '{}' using function '{}'", input, #TYPE, #FUNCTION)); \
return nx::core::MakeErrorResult<TYPE>(-10351, fmt::format("Error trying to convert '{}' to type '{}' using function '{}'", input, #TYPE, #FUNCTION)); \
} catch(const std::out_of_range& e) \
{ \
return nx::core::MakeErrorResult<TYPE>(-101, fmt::format("Overflow error trying to convert '{}' to type '{}' using function '{}'", input, #TYPE, #FUNCTION)); \
return nx::core::MakeErrorResult<TYPE>(-10353, fmt::format("Overflow error trying to convert '{}' to type '{}' using function '{}'", input, #TYPE, #FUNCTION)); \
} \
return {value}; \
} \
Expand Down
Loading

0 comments on commit dcdb63e

Please sign in to comment.