Skip to content

Commit

Permalink
Finish cleaning up "Compute" filters in the SimplnxCore module
Browse files Browse the repository at this point in the history
  • Loading branch information
nyoungbq committed Jul 25, 2024
1 parent d288fe6 commit fe12389
Show file tree
Hide file tree
Showing 14 changed files with 370 additions and 375 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,22 @@ const std::atomic_bool& ComputeFeatureRect::getCancel()
// -----------------------------------------------------------------------------
Result<> ComputeFeatureRect::operator()()
{
const auto& featureIds = m_DataStructure.getDataRefAs<Int32Array>(m_InputValues->FeatureIdsArrayPath);
auto& corners = m_DataStructure.getDataRefAs<UInt32Array>(m_InputValues->FeatureRectArrayPath);
auto& cornersDataStore = corners.getDataStoreRef();
const auto* featureIds = m_DataStructure.getDataAs<Int32Array>(m_InputValues->FeatureIdsArrayPath);
const auto& featureIdsStore = featureIds->getDataStoreRef();
auto* corners = m_DataStructure.getDataAs<UInt32Array>(m_InputValues->FeatureRectArrayPath);
auto& cornersStore = corners->getDataStoreRef();

// Create corners array, which stores pixel coordinates for the top-left and bottom-right coordinates of each feature object
for(usize i = 0; i < corners.getNumberOfTuples(); i++)
for(usize i = 0; i < cornersStore.getNumberOfTuples(); i++)
{
cornersDataStore.setComponent(i, 0, std::numeric_limits<uint32>::max());
cornersDataStore.setComponent(i, 1, std::numeric_limits<uint32>::max());
cornersDataStore.setComponent(i, 2, std::numeric_limits<uint32>::max());
cornersDataStore.setComponent(i, 3, std::numeric_limits<uint32>::min());
cornersDataStore.setComponent(i, 4, std::numeric_limits<uint32>::min());
cornersDataStore.setComponent(i, 5, std::numeric_limits<uint32>::min());
cornersStore.setComponent(i, 0, std::numeric_limits<uint32>::max());
cornersStore.setComponent(i, 1, std::numeric_limits<uint32>::max());
cornersStore.setComponent(i, 2, std::numeric_limits<uint32>::max());
cornersStore.setComponent(i, 3, std::numeric_limits<uint32>::min());
cornersStore.setComponent(i, 4, std::numeric_limits<uint32>::min());
cornersStore.setComponent(i, 5, std::numeric_limits<uint32>::min());
}
std::vector<usize> imageDims = featureIds.getTupleShape();
std::vector<usize> imageDims = featureIdsStore.getTupleShape();

/*
* Array dimension ordering is flipped compared to geometry dimension ordering.
Expand All @@ -75,17 +76,17 @@ Result<> ComputeFeatureRect::operator()()
{
index = IndexFromCoord(imageDims, x, y, z); // Index into featureIds array

const int32 featureId = featureIds[index];
const int32 featureId = featureIdsStore[index];
if(featureId == 0)
{
continue;
}

if(featureId >= corners.getNumberOfTuples())
if(featureId >= cornersStore.getNumberOfTuples())
{
const DataPath parentPath = m_InputValues->FeatureRectArrayPath.getParent();
return MakeErrorResult(-31000, fmt::format("The parent data object '{}' of output array '{}' has a smaller tuple count than the maximum feature id in '{}'", parentPath.getTargetName(),
corners.getName(), featureIds.getName()));
corners->getName(), featureIds->getName()));
}

const uint32 indices[3] = {x, y, z}; // Sequence dependent DO NOT REORDER
Expand All @@ -94,11 +95,11 @@ Result<> ComputeFeatureRect::operator()()
{
if(l > 2)
{
corners[featureShift + l] = std::max(corners[featureShift + l], indices[l - 3]);
cornersStore[featureShift + l] = std::max(cornersStore[featureShift + l], indices[l - 3]);
}
else
{
corners[featureShift + l] = std::min(corners[featureShift + l], indices[l]);
cornersStore[featureShift + l] = std::min(cornersStore[featureShift + l], indices[l]);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ template <typename T>
class ComputeKMeansTemplate
{
public:
ComputeKMeansTemplate(ComputeKMeans* filter, const IDataArray& inputIDataArray, IDataArray& meansIDataArray, const std::unique_ptr<MaskCompare>& maskDataArray, usize numClusters, Int32Array& fIds,
ClusterUtilities::DistanceMetric distMetric, std::mt19937_64::result_type seed)
ComputeKMeansTemplate(ComputeKMeans* filter, const IDataArray* inputIDataArray, IDataArray* meansIDataArray, const std::unique_ptr<MaskCompare>& maskDataArray, usize numClusters,
Int32AbstractDataStore& fIds, ClusterUtilities::DistanceMetric distMetric, std::mt19937_64::result_type seed)
: m_Filter(filter)
, m_InputArray(dynamic_cast<const DataArrayT&>(inputIDataArray))
, m_Means(dynamic_cast<DataArrayT&>(meansIDataArray))
, m_InputArray(dynamic_cast<const DataArrayT*>(inputIDataArray)->getDataStoreRef())
, m_Means(dynamic_cast<DataArrayT*>(meansIDataArray)->getDataStoreRef())
, m_Mask(maskDataArray)
, m_NumClusters(numClusters)
, m_FeatureIds(fIds)
Expand Down Expand Up @@ -101,12 +101,13 @@ class ComputeKMeansTemplate

private:
using DataArrayT = DataArray<T>;
using AbstractDataStoreT = AbstractDataStore<T>;
ComputeKMeans* m_Filter;
const DataArrayT& m_InputArray;
DataArrayT& m_Means;
const AbstractDataStoreT& m_InputArray;
AbstractDataStoreT& m_Means;
const std::unique_ptr<MaskCompare>& m_Mask;
usize m_NumClusters;
Int32Array& m_FeatureIds;
Int32AbstractDataStore& m_FeatureIds;
ClusterUtilities::DistanceMetric m_DistMetric;
std::mt19937_64::result_type m_Seed;

Expand Down Expand Up @@ -207,7 +208,7 @@ const std::atomic_bool& ComputeKMeans::getCancel()
// -----------------------------------------------------------------------------
Result<> ComputeKMeans::operator()()
{
auto& clusteringArray = m_DataStructure.getDataRefAs<IDataArray>(m_InputValues->ClusteringArrayPath);
auto* clusteringArray = m_DataStructure.getDataAs<IDataArray>(m_InputValues->ClusteringArrayPath);

std::unique_ptr<MaskCompare> maskCompare;
try
Expand All @@ -221,8 +222,8 @@ Result<> ComputeKMeans::operator()()
return MakeErrorResult(-54060, message);
}

RunTemplateClass<ComputeKMeansTemplate, types::NoBooleanType>(clusteringArray.getDataType(), this, clusteringArray, m_DataStructure.getDataRefAs<IDataArray>(m_InputValues->MeansArrayPath),
maskCompare, m_InputValues->InitClusters, m_DataStructure.getDataRefAs<Int32Array>(m_InputValues->FeatureIdsArrayPath),
RunTemplateClass<ComputeKMeansTemplate, types::NoBooleanType>(clusteringArray->getDataType(), this, clusteringArray, m_DataStructure.getDataAs<IDataArray>(m_InputValues->MeansArrayPath),
maskCompare, m_InputValues->InitClusters, m_DataStructure.getDataAs<Int32Array>(m_InputValues->FeatureIdsArrayPath)->getDataStoreRef(),
m_InputValues->DistanceMetric, m_InputValues->Seed);

return {};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ template <typename T>
class KMedoidsTemplate
{
public:
KMedoidsTemplate(ComputeKMedoids* filter, const IDataArray& inputIDataArray, IDataArray& medoidsIDataArray, const std::unique_ptr<MaskCompare>& maskDataArray, usize numClusters, Int32Array& fIds,
ClusterUtilities::DistanceMetric distMetric, std::mt19937_64::result_type seed)
KMedoidsTemplate(ComputeKMedoids* filter, const IDataArray* inputIDataArray, IDataArray* medoidsIDataArray, const std::unique_ptr<MaskCompare>& maskDataArray, usize numClusters,
Int32AbstractDataStore& fIds, ClusterUtilities::DistanceMetric distMetric, std::mt19937_64::result_type seed)
: m_Filter(filter)
, m_InputArray(dynamic_cast<const DataArrayT&>(inputIDataArray))
, m_Medoids(dynamic_cast<DataArrayT&>(medoidsIDataArray))
, m_InputArray(dynamic_cast<const DataArrayT*>(inputIDataArray)->getDataStoreRef())
, m_Medoids(dynamic_cast<DataArrayT*>(medoidsIDataArray)->getDataStoreRef())
, m_Mask(maskDataArray)
, m_NumClusters(numClusters)
, m_FeatureIds(fIds)
Expand Down Expand Up @@ -89,12 +89,13 @@ class KMedoidsTemplate

private:
using DataArrayT = DataArray<T>;
using AbstractDataStoreT = AbstractDataStore<T>;
ComputeKMedoids* m_Filter;
const DataArrayT& m_InputArray;
DataArrayT& m_Medoids;
const AbstractDataStoreT& m_InputArray;
AbstractDataStoreT& m_Medoids;
const std::unique_ptr<MaskCompare>& m_Mask;
usize m_NumClusters;
Int32Array& m_FeatureIds;
Int32AbstractDataStore& m_FeatureIds;
ClusterUtilities::DistanceMetric m_DistMetric;
std::mt19937_64::result_type m_Seed;

Expand Down Expand Up @@ -207,7 +208,7 @@ const std::atomic_bool& ComputeKMedoids::getCancel()
// -----------------------------------------------------------------------------
Result<> ComputeKMedoids::operator()()
{
auto& clusteringArray = m_DataStructure.getDataRefAs<IDataArray>(m_InputValues->ClusteringArrayPath);
auto* clusteringArray = m_DataStructure.getDataAs<IDataArray>(m_InputValues->ClusteringArrayPath);
std::unique_ptr<MaskCompare> maskCompare;
try
{
Expand All @@ -219,9 +220,9 @@ Result<> ComputeKMedoids::operator()()
std::string message = fmt::format("Mask Array DataPath does not exist or is not of the correct type (Bool | UInt8) {}", m_InputValues->MaskArrayPath.toString());
return MakeErrorResult(-54070, message);
}
RunTemplateClass<KMedoidsTemplate, types::NoBooleanType>(clusteringArray.getDataType(), this, clusteringArray, m_DataStructure.getDataRefAs<IDataArray>(m_InputValues->MedoidsArrayPath), maskCompare,
m_InputValues->InitClusters, m_DataStructure.getDataRefAs<Int32Array>(m_InputValues->FeatureIdsArrayPath), m_InputValues->DistanceMetric,
m_InputValues->Seed);
RunTemplateClass<KMedoidsTemplate, types::NoBooleanType>(clusteringArray->getDataType(), this, clusteringArray, m_DataStructure.getDataAs<IDataArray>(m_InputValues->MedoidsArrayPath), maskCompare,
m_InputValues->InitClusters, m_DataStructure.getDataAs<Int32Array>(m_InputValues->FeatureIdsArrayPath)->getDataStoreRef(),
m_InputValues->DistanceMetric, m_InputValues->Seed);

return {};
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ Result<> ComputeLargestCrossSections::operator()()
{
const auto& imageGeom = m_DataStructure.getDataRefAs<ImageGeom>(m_InputValues->ImageGeometryPath);
auto& featureIds = m_DataStructure.getDataRefAs<Int32Array>(m_InputValues->FeatureIdsArrayPath);
auto& largestCrossSections = m_DataStructure.getDataRefAs<Float32Array>(m_InputValues->LargestCrossSectionsArrayPath);
const usize numFeatures = largestCrossSections.getNumberOfTuples();
auto& featureIdsStore = featureIds.getDataStoreRef();
auto& largestCrossSectStore = m_DataStructure.getDataAs<Float32Array>(m_InputValues->LargestCrossSectionsArrayPath)->getDataStoreRef();
const usize numFeatures = largestCrossSectStore.getNumberOfTuples();

// Validate the largestCrossSections array is the proper size
// Validate the largestCrossSectStore array is the proper size
auto validateResults = ValidateNumFeaturesInArray(m_DataStructure, m_InputValues->LargestCrossSectionsArrayPath, featureIds);
if(validateResults.invalid())
{
Expand Down Expand Up @@ -93,16 +94,16 @@ Result<> ComputeLargestCrossSections::operator()()
{
kStride = k * stride3;
point = iStride + jStride + kStride;
gNum = featureIds[point];
gNum = featureIdsStore[point];
featureCounts[gNum]++;
}
}
for(size_t g = 1; g < numFeatures; g++)
{
area = featureCounts[g] * resScalar;
if(area > largestCrossSections[g])
if(area > largestCrossSectStore[g])
{
largestCrossSections[g] = area;
largestCrossSectStore[g] = area;
}
}
}
Expand Down
Loading

0 comments on commit fe12389

Please sign in to comment.