Skip to content

Commit

Permalink
Add getGraphStatistics emethod and GraphStatistics class and it's exp…
Browse files Browse the repository at this point in the history
…orted C api

improve graph statistics detail string
add in/out degree Histogram and indegree count
fix
Restore two outputs: cerr for procecced and cout when mode==e

Signed-off-by: kpango <[email protected]>
  • Loading branch information
kpango committed Jul 12, 2024
1 parent eb857d0 commit 85fb164
Show file tree
Hide file tree
Showing 5 changed files with 636 additions and 2 deletions.
84 changes: 83 additions & 1 deletion lib/NGT/Capi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// limitations under the License.
//

#include <algorithm>
#include <string>
#include <iostream>
#include <sstream>
Expand Down Expand Up @@ -105,7 +106,88 @@ NGTIndex ngt_create_graph_and_tree_in_memory(NGTProperty prop, NGTError error) {
#endif
}

NGTProperty ngt_create_property(NGTError error) {
NGTGraphStatistics ngt_get_graph_statistics(const NGTIndex index, char mode, size_t edgeSize, NGTError error) {
NGTGraphStatistics cStats;
if(index == NULL){
std::stringstream ss;
ss << "Capi : " << __FUNCTION__ << "() : parametor error: index = " << index;
operate_error_string_(ss, error);
return cStats;
}

NGT::Index* pindex = static_cast<NGT::Index*>(index);
NGT::GraphIndex &graph = static_cast<NGT::GraphIndex&>(pindex->getIndex());
NGT::GraphIndex::GraphStatistics stats = NGT::GraphIndex::getGraphStatistics(graph, mode, edgeSize);
try {
cStats.numberOfObjects = stats.getNumberOfObjects();
cStats.numberOfIndexedObjects = stats.getNumberOfIndexedObjects();
cStats.sizeOfObjectRepository = stats.getSizeOfObjectRepository();
cStats.sizeOfRefinementObjectRepository = stats.getSizeOfRefinementObjectRepository();
cStats.numberOfRemovedObjects = stats.getNumberOfRemovedObjects();
cStats.numberOfNodes = stats.getNumberOfNodes();
cStats.numberOfEdges = stats.getNumberOfEdges();
cStats.meanEdgeLength = static_cast<double>(stats.getMeanEdgeLength());
cStats.meanNumberOfEdgesPerNode = stats.getMeanNumberOfEdgesPerNode();
cStats.numberOfNodesWithoutEdges = stats.getNumberOfNodesWithoutEdges();
cStats.maxNumberOfOutdegree = stats.getMaxNumberOfOutdegree();
cStats.minNumberOfOutdegree = stats.getMinNumberOfOutdegree();
cStats.numberOfNodesWithoutIndegree = stats.getNumberOfNodesWithoutIndegree();
cStats.maxNumberOfIndegree = stats.getMaxNumberOfIndegree();
cStats.minNumberOfIndegree = stats.getMinNumberOfIndegree();
cStats.meanEdgeLengthFor10Edges = static_cast<double>(stats.getMeanEdgeLengthFor10Edges());
cStats.nodesSkippedFor10Edges = stats.getNodesSkippedFor10Edges();
cStats.meanIndegreeDistanceFor10Edges = static_cast<double>(stats.getMeanIndegreeDistanceFor10Edges());
cStats.nodesSkippedForIndegreeDistance = stats.getNodesSkippedForIndegreeDistance();
cStats.varianceOfOutdegree = stats.getVarianceOfOutdegree();
cStats.varianceOfIndegree = stats.getVarianceOfIndegree();
cStats.medianOutdegree = stats.getMedianOutdegree();
cStats.modeOutdegree = stats.getModeOutdegree();
cStats.c95Outdegree = stats.getC95Outdegree();
cStats.c99Outdegree = stats.getC99Outdegree();
cStats.medianIndegree = stats.getMedianIndegree();
cStats.modeIndegree = stats.getModeIndegree();
cStats.c5Indegree = stats.getC5Indegree();
cStats.c1Indegree = stats.getC1Indegree();

cStats.indegreeCountSize = stats.getIndegreeCount().size();
cStats.indegreeCount = new int64_t[cStats.indegreeCountSize];
std::copy(stats.getIndegreeCount().begin(), stats.getIndegreeCount().end(), cStats.indegreeCount);

cStats.outdegreeHistogramSize = stats.getOutdegreeHistogram().size();
cStats.outdegreeHistogram = new size_t[cStats.outdegreeHistogramSize];
std::copy(stats.getOutdegreeHistogram().begin(), stats.getOutdegreeHistogram().end(), cStats.outdegreeHistogram);

cStats.indegreeHistogramSize = stats.getIndegreeHistogram().size();
cStats.indegreeHistogram = new size_t[cStats.indegreeHistogramSize];
std::copy(stats.getIndegreeHistogram().begin(), stats.getIndegreeHistogram().end(), cStats.indegreeHistogram);

cStats.valid = stats.isValid();
}catch(std::exception &err){
std::stringstream ss;
ss << "Capi : " << __FUNCTION__ << "() : Error: " << err.what();
operate_error_string_(ss, error);
return cStats;
}

return cStats;
}

void ngt_free_graph_statistics(NGTGraphStatistics *cStats) {
delete[] cStats->indegreeCount;
cStats->indegreeCount = nullptr;
cStats->indegreeCountSize = 0;

delete[] cStats->outdegreeHistogram;
cStats->outdegreeHistogram = nullptr;
cStats->outdegreeHistogramSize = 0;

delete[] cStats->indegreeHistogram;
cStats->indegreeHistogram = nullptr;
cStats->indegreeHistogramSize = 0;
}

NGTProperty ngt_create_property(NGTError error)
{
try{
return static_cast<NGTProperty>(new NGT::Property());
}catch(std::exception &err){
Expand Down
43 changes: 43 additions & 0 deletions lib/NGT/Capi.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,45 @@ typedef struct {
bool log;
} NGTAnngEdgeOptimizationParameter;

typedef struct {
size_t numberOfObjects;
size_t numberOfIndexedObjects;
size_t sizeOfObjectRepository;
size_t sizeOfRefinementObjectRepository;
size_t numberOfRemovedObjects;
size_t numberOfNodes;
size_t numberOfEdges;
double meanEdgeLength;
double meanNumberOfEdgesPerNode;
size_t numberOfNodesWithoutEdges;
size_t maxNumberOfOutdegree;
size_t minNumberOfOutdegree;
size_t numberOfNodesWithoutIndegree;
size_t maxNumberOfIndegree;
size_t minNumberOfIndegree;
double meanEdgeLengthFor10Edges;
size_t nodesSkippedFor10Edges;
double meanIndegreeDistanceFor10Edges;
size_t nodesSkippedForIndegreeDistance;
double varianceOfOutdegree;
double varianceOfIndegree;
int medianOutdegree;
size_t modeOutdegree;
double c95Outdegree;
double c99Outdegree;
int medianIndegree;
size_t modeIndegree;
double c5Indegree;
double c1Indegree;
bool valid;
int64_t *indegreeCount;
size_t indegreeCountSize;
size_t *outdegreeHistogram;
size_t outdegreeHistogramSize;
size_t *indegreeHistogram;
size_t indegreeHistogramSize;
} NGTGraphStatistics;

NGTIndex ngt_open_index(const char *, NGTError);

NGTIndex ngt_open_index_as_read_only(const char *, NGTError);
Expand All @@ -88,6 +127,10 @@ NGTIndex ngt_create_graph_and_tree(const char *, NGTProperty, NGTError);

NGTIndex ngt_create_graph_and_tree_in_memory(NGTProperty, NGTError);

NGTGraphStatistics ngt_get_graph_statistics(const NGTIndex index, char mode, size_t edgeSize, NGTError error);

void ngt_free_graph_statistics(NGTGraphStatistics *cStats);

NGTProperty ngt_create_property(NGTError);

bool ngt_save_index(const NGTIndex, const char *, NGTError);
Expand Down
2 changes: 1 addition & 1 deletion lib/NGT/Command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1285,7 +1285,7 @@ using namespace std;
void
NGT::Command::info(Args &args)
{
const string usage = "Usage: ngt info [-E #-of-edges] [-m h|e] index";
const string usage = "Usage: ngt info [-E #-of-edges] [-m a|e|h|p] index";

std::cout << "NGT version: " << NGT::Index::getVersion() << std::endl;
std::cout << "CPU SIMD types: ";
Expand Down
Loading

0 comments on commit 85fb164

Please sign in to comment.