Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Overhaul of C++ part for 64bit compilation and double precision #1

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/examples/FilesAndFilters/FilesAndFilters.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,16 @@ int main()
{
//Declare class and input variables
Persistence1D p;
vector<float> data;
float currdata;
vector<double> data;
double currdata;
ifstream datafile;
char * filename = "data.txt";

//Declare variables for results
vector<TPairedExtrema> pairs;
vector<int> min, max;
int globalMinIndex;
float globalMinValue;
vector<size_t> min, max;
size_t globalMinIndex;
double globalMinValue;


//Open the data file for reading
Expand Down Expand Up @@ -77,7 +77,7 @@ int main()

//Now set a threshold for features filtering
//In this case, we choose a threshold between largest and smallest persistence, to select from of the data.
float filterThreshold = (pairs.front().Persistence + pairs.back().Persistence)/2;
double filterThreshold = (pairs.front().Persistence + pairs.back().Persistence)/2;

//The vector which hold the data for the Get functions are being reset before use.
//Use them to retrieve the filtered results:
Expand Down
12 changes: 6 additions & 6 deletions src/examples/MatlabVisualization/MatlabVisualization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ int main()
{
//Input and class variables declaration
Persistence1D p;
vector<float> data;
float currdata;
vector<double> data;
double currdata;
ifstream datafile;
ofstream outfile;
char * filename = "data.txt";
char * outfilename = "res.txt";
bool enableMatlabIndexing = true;

//Output variables declaration
vector<int> min, max;
int globalMinIndex;
vector<size_t> min, max;
size_t globalMinIndex;

//Open and read the data file
datafile.open(filename);
Expand Down Expand Up @@ -78,7 +78,7 @@ int main()
}

//Now, set a threshold for features filtering
float filterThreshold = 1.0;
double filterThreshold = 1.0;

//Retrieve the filtered results from the class.
//Since results are intended to be used within Matlab, set MatlabIndexing in all Get function calls
Expand All @@ -92,7 +92,7 @@ int main()
if (!outfile)
return -2;

for (unsigned int i = 0; i < min.size() && i < max.size(); i++)
for (size_t i = 0; i < min.size() && i < max.size(); i++)
{
outfile << min[i] << endl;
outfile << max[i] << endl;
Expand Down
2 changes: 1 addition & 1 deletion src/examples/SimpleDataVector/SimpleDataVector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ using namespace p1d;
int main()
{
//Create some data
vector< float > data;
vector<double> data;
data.push_back(2.0); data.push_back(5.0); data.push_back(7.0);
data.push_back(-12.0); data.push_back(-13.0); data.push_back(-7.0);
data.push_back(10.0); data.push_back(18.0); data.push_back(6.0);
Expand Down
90 changes: 45 additions & 45 deletions src/persistence1d/persistence1d.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ struct TIdxAndData
}

///The index of the vertex within the Data vector.
int Idx;
size_t Idx;

///Vertex data value from the original Data vector sent as an argument to RunPersistence.
float Data;
double Data;
};


Expand All @@ -51,14 +51,14 @@ struct TComponent
///A component is defined by the indices of its edges.
///Both variables hold the respective indices of the vertices in Data vector.
///All vertices between them are considered to belong to this component.
int LeftEdgeIndex;
int RightEdgeIndex;
size_t LeftEdgeIndex;
size_t RightEdgeIndex;

///The index of the local minimum within the component as longs as its alive.
int MinIndex;
size_t MinIndex;

///The value of the Data[MinIndex].
float MinValue; //redundant, but makes life easier
double MinValue; //redundant, but makes life easier

///Set to true when a component is created. Once components are merged,
///the destroyed component Alive value is set to false.
Expand All @@ -74,15 +74,15 @@ struct TComponent
struct TPairedExtrema
{
///Index of local minimum, as per Data vector.
int MinIndex;
size_t MinIndex;

///Index of local maximum, as per Data vector.
int MaxIndex;
size_t MaxIndex;

///The persistence of the two extrema.
///Data[MaxIndex] - Data[MinIndex]
///Guaranteed to be >= 0.
float Persistence;
double Persistence;

bool operator<(const TPairedExtrema& other) const
{
Expand Down Expand Up @@ -125,7 +125,7 @@ class Persistence1D

@param[in] InputData Vector of data to find features on, ordered according to its axis.
*/
bool RunPersistence(const std::vector<float>& InputData)
bool RunPersistence(const std::vector<double>& InputData)
{
Data = InputData;
Init();
Expand Down Expand Up @@ -153,7 +153,7 @@ class Persistence1D
void PrintPairs(const std::vector<TPairedExtrema>& pairs) const
{
for (std::vector<TPairedExtrema>::const_iterator it = pairs.begin();
it != pairs.end(); it++)
it != pairs.end(); ++it)
{
std::cout << "Persistence: " << (*it).Persistence
<< " minimum index: " << (*it).MinIndex
Expand All @@ -169,7 +169,7 @@ class Persistence1D
@param[in] threshold Threshold value for pair persistence.
@param[in] matlabIndexing Use Matlab indexing for printing.
*/
void PrintResults(const float threshold = 0.0, const bool matlabIndexing = false) const
void PrintResults(const double threshold = 0.0, const bool matlabIndexing = false) const
{
if (threshold < 0)
{
Expand Down Expand Up @@ -203,7 +203,7 @@ class Persistence1D

@param[in] matlabIndexing Set this to true to change all indices of features to Matlab's 1-indexing.
*/
bool GetPairedExtrema(std::vector<TPairedExtrema> & pairs, const float threshold = 0, const bool matlabIndexing = false) const
bool GetPairedExtrema(std::vector<TPairedExtrema> & pairs, const double threshold = 0, const bool matlabIndexing = false) const
{
//make sure the user does not use previous results that do not match the data
pairs.clear();
Expand All @@ -218,7 +218,7 @@ class Persistence1D

if (matlabIndexing) //match matlab indices by adding one
{
for (std::vector<TPairedExtrema>::iterator p = pairs.begin(); p != pairs.end(); p++)
for (std::vector<TPairedExtrema>::iterator p = pairs.begin(); p != pairs.end(); ++p)
{
(*p).MinIndex += MATLAB_INDEX_FACTOR;
(*p).MaxIndex += MATLAB_INDEX_FACTOR;
Expand All @@ -238,7 +238,7 @@ class Persistence1D
@param[in] threshold Return only indices for pairs whose persistence is greater than or equal to threshold.
@param[in] matlabIndexing Set this to true to change all indices to match Matlab's 1-indexing.
*/
bool GetExtremaIndices(std::vector<int> & min, std::vector<int> & max, const float threshold = 0, const bool matlabIndexing = false) const
bool GetExtremaIndices(std::vector<size_t> & min, std::vector<size_t> & max, const double threshold = 0, const bool matlabIndexing = false) const
{
//before doing anything, make sure the user does not use old results
min.clear();
Expand All @@ -249,12 +249,12 @@ class Persistence1D
min.reserve(PairedExtrema.size());
max.reserve(PairedExtrema.size());

int matlabIndexFactor = 0;
size_t matlabIndexFactor = 0;
if (matlabIndexing) matlabIndexFactor = MATLAB_INDEX_FACTOR;

std::vector<TPairedExtrema>::const_iterator lower_bound = FilterByPersistence(threshold);

for (std::vector<TPairedExtrema>::const_iterator p = lower_bound; p != PairedExtrema.end(); p++)
for (std::vector<TPairedExtrema>::const_iterator p = lower_bound; p != PairedExtrema.end(); ++p)
{
min.push_back((*p).MinIndex + matlabIndexFactor);
max.push_back((*p).MaxIndex + matlabIndexFactor);
Expand All @@ -266,9 +266,9 @@ class Persistence1D
The global minimum does not get paired and is not returned
via GetPairedExtrema and GetExtremaIndices.
*/
int GetGlobalMinimumIndex(const bool matlabIndexing = false) const
size_t GetGlobalMinimumIndex(const bool matlabIndexing = false) const
{
if (Components.empty()) return -1;
if (Components.empty()) return 0;

assert(Components.front().Alive);
if (matlabIndexing)
Expand All @@ -284,9 +284,9 @@ class Persistence1D
The global minimum does not get paired and is not returned
via GetPairedExtrema and GetExtremaIndices.
*/
float GetGlobalMinimumValue() const
double GetGlobalMinimumValue() const
{
if (Components.empty()) return 0;
if (Components.empty()) return 0.0;

assert(Components.front().Alive);
return Components.front().MinValue;
Expand All @@ -304,12 +304,12 @@ class Persistence1D
bool VerifyResults()
{
bool flag = true;
std::vector<int> min, max;
std::vector<int> combinedIndices;
std::vector<size_t> min, max;
std::vector<size_t> combinedIndices;

GetExtremaIndices(min, max);

int globalMinIdx = GetGlobalMinimumIndex();
size_t globalMinIdx = GetGlobalMinimumIndex();

std::sort(min.begin(), min.end());
std::sort(max.begin(), max.end());
Expand All @@ -323,11 +323,11 @@ class Persistence1D
flag = false;
}

if ((globalMinIdx > (int)Data.size()-1) || (globalMinIdx < -1)) flag = false;
if (globalMinIdx == -1 && min.size() != 0) flag = false;
if ((globalMinIdx > Data.size()-1) || Components.empty()) flag = false;
if (Components.empty() && min.size() != 0) flag = false;

std::vector<int>::iterator minUniqueEnd = std::unique(min.begin(), min.end());
std::vector<int>::iterator maxUniqueEnd = std::unique(max.begin(), max.end());
std::vector<size_t>::iterator minUniqueEnd = std::unique(min.begin(), min.end());
std::vector<size_t>::iterator maxUniqueEnd = std::unique(max.begin(), max.end());

if (minUniqueEnd != min.end() ||
maxUniqueEnd != max.end() ||
Expand All @@ -343,7 +343,7 @@ class Persistence1D
/*!
Contain a copy of the original input data.
*/
std::vector<float> Data;
std::vector<double> Data;


/*!
Expand Down Expand Up @@ -373,7 +373,7 @@ class Persistence1D
std::vector<TPairedExtrema> PairedExtrema;


unsigned int TotalComponents; //keeps track of component vector size and newest component "color"
size_t TotalComponents; //keeps track of component vector size and newest component "color"
bool AliveComponentsVerified; //Index of global minimum in Data vector. This minimum is never paired.


Expand All @@ -386,9 +386,9 @@ class Persistence1D

@param[in] firstIdx,secondIdx Indices of components to be merged. Their order does not matter.
*/
void MergeComponents(const int firstIdx, const int secondIdx)
void MergeComponents(const size_t firstIdx, const size_t secondIdx)
{
int survivorIdx, destroyedIdx;
size_t survivorIdx, destroyedIdx;
//survivor - component whose hub is bigger
if (Components[firstIdx].MinValue < Components[secondIdx].MinValue)
{
Expand Down Expand Up @@ -435,7 +435,7 @@ class Persistence1D

@param[in] firstIdx, secondIdx Indices of vertices to be paired. Order does not matter.
*/
void CreatePairedExtrema(const int firstIdx, const int secondIdx)
void CreatePairedExtrema(const size_t firstIdx, const size_t secondIdx)
{
TPairedExtrema pair;

Expand Down Expand Up @@ -488,7 +488,7 @@ class Persistence1D

@param[in] minIdx Index of a local minimum.
*/
void CreateComponent(const int minIdx)
void CreateComponent(const size_t minIdx)
{
TComponent comp;
comp.Alive = true;
Expand Down Expand Up @@ -518,7 +518,7 @@ class Persistence1D
@param[in] componentIdx Index of component (the value of a neighboring vertex in Colors[]).
@param[in] dataIdx Index of vertex which the component is extended to.
*/
void ExtendComponent(const int componentIdx, const int dataIdx)
void ExtendComponent(const size_t componentIdx, const size_t dataIdx)
{
#ifdef _DEUBG
assert(Components[componentIdx].Alive == true)
Expand All @@ -538,7 +538,7 @@ class Persistence1D
{
#ifdef _DEUBG
std::string errorMessage = "ExtendComponent: index mismatch. Data index: ";
errorMessage += std::to_string((long long)dataIdx);
errorMessage += std::to_string(dataIdx);
throw (errorMessage);
#endif
}
Expand All @@ -563,7 +563,7 @@ class Persistence1D
Colors.resize(Data.size());
std::fill(Colors.begin(), Colors.end(), NO_COLOR);

int vectorSize = (int)(Data.size()/RESIZE_FACTOR) + 1; //starting reserved size >= 1 at least
size_t vectorSize = (size_t)(Data.size()/RESIZE_FACTOR) + 1; //starting reserved size >= 1 at least

Components.clear();
Components.reserve(vectorSize);
Expand All @@ -584,13 +584,13 @@ class Persistence1D
{
if (Data.size()==0) return;

for (std::vector<float>::size_type i = 0; i != Data.size(); i++)
for (std::vector<double>::size_type i = 0; i != Data.size(); ++i)
{
TIdxAndData dataidxpair;

//this is going to make problems
dataidxpair.Data = Data[i];
dataidxpair.Idx = (int)i;
dataidxpair.Idx = i;

SortedData.push_back(dataidxpair);
}
Expand All @@ -617,9 +617,9 @@ class Persistence1D
return;
}

for (std::vector<TIdxAndData>::iterator p = SortedData.begin(); p != SortedData.end(); p++)
for (std::vector<TIdxAndData>::iterator p = SortedData.begin(); p != SortedData.end(); ++p)
{
int i = (*p).Idx;
size_t i = (*p).Idx;

//left most vertex - no left neighbor
//two options - either local minimum, or extend component
Expand Down Expand Up @@ -664,7 +664,7 @@ class Persistence1D
}
else if (Colors[i-1] != NO_COLOR && Colors[i+1] != NO_COLOR) //local maximum - merge components
{
int leftComp, rightComp;
size_t leftComp, rightComp;

leftComp = Colors[i-1];
rightComp = Colors[i+1];
Expand Down Expand Up @@ -702,7 +702,7 @@ class Persistence1D

@param[in] threshold Minimum persistence of features to be returned.
*/
std::vector<TPairedExtrema>::const_iterator FilterByPersistence(const float threshold = 0) const
std::vector<TPairedExtrema>::const_iterator FilterByPersistence(const double threshold = 0) const
{
if (threshold == 0 || threshold < 0) return PairedExtrema.begin();

Expand Down Expand Up @@ -733,7 +733,7 @@ class Persistence1D
#endif
}

for (std::vector<TComponent>::const_iterator it = Components.begin()+1; it != Components.end(); it++)
for (std::vector<TComponent>::const_iterator it = Components.begin()+1; it != Components.end(); ++it)
{
if ((*it).Alive == true)
{
Expand All @@ -751,4 +751,4 @@ class Persistence1D
}
};
}
#endif
#endif
Loading