20 using namespace KKMLL;
25 map<MLClassListPtr,MLClassListPtr>
MLClass::existingClassLists;
28 bool MLClass::needToRunFinalCleanUp =
false;
39 if (!needToRunFinalCleanUp)
41 needToRunFinalCleanUp =
true;
42 atexit (MLClass::FinalCleanUp);
52 if (!existingMLClasses)
56 if (!existingMLClasses)
59 existingMLClasses->Owner (
true);
63 return existingMLClasses;
73 existingClassLists.insert (pair<MLClassListPtr,MLClassListPtr> (list, list));
84 map<MLClassListPtr,MLClassListPtr>::iterator idx;
85 idx = existingClassLists.find (list);
86 if (idx != existingClassLists.end ())
88 existingClassLists.erase (idx);
92 cerr << endl << endl <<
"MLClass::DeleteImageClassList MLClassList instance was not found." << endl << endl;
157 for (
auto existingMLClass: *existingMLClasses)
159 if (!existingMLClass)
161 startingAncestor = existingMLClass;
162 MLClassPtr ancestor = startingAncestor->Parent ();
163 while (ancestor != NULL)
165 if (ancestor == parent)
167 results->PushOnBack (startingAncestor);
170 ancestor = ancestor->Parent ();
171 if (ancestor == startingAncestor)
182 const KKStr& newName,
193 existingMLClasses->ChangeNameOfClass (mlClass, oldName, newName, successful);
197 <<
"MLClass::ChangeNameOfClass 'existingMLClasses' failed to vahne name." << endl
198 <<
" NewName[" << newName <<
"]" << endl
199 <<
" OldName[" << mlClass->Name () <<
"]" << endl
208 mlClass->Name (newName);
210 map<MLClassListPtr,MLClassListPtr>::iterator idx;
211 for (idx = existingClassLists.begin (); idx != existingClassLists.end (); ++idx)
214 kkuint32 classInList = list->PtrToIdx (mlClass);
215 if (classInList >= 0)
217 bool nameChangedInList =
false;
218 list->ChangeNameOfClass (mlClass, oldName, newName, nameChangedInList);
238 MLClassList::iterator idx;
239 for (idx = globalClassList->begin (); idx != globalClassList->end (); ++idx)
242 if (ic == allClasses)
271 if (!needToRunFinalCleanUp)
275 if (needToRunFinalCleanUp)
283 if (existingMLClasses)
285 delete existingMLClasses;
286 existingMLClasses = NULL;
289 needToRunFinalCleanUp =
false;
307 storedOnDataBase (
false),
314 <<
"MLClass::MLClass *** ERROR *** Empty Name" << endl
319 KKStr topLevel = upperName;
325 (upperName
== "UNKNOWN") ||
326 (upperName
== "UNDEFINED") ||
327 (upperName
== "NONPLANKTON") ||
328 (topLevel
== "NOISE");
338 <<
"MLClass::MLClass (const MLClass& mlClass) *** ERROR ***" << endl
339 <<
"Should never ever call this method." << endl
385 bool allFollowingCharsAreNumeric =
true;
386 while ((y < className
.Len ()) && (allFollowingCharsAreNumeric))
388 char ch = className
[y];
389 allFollowingCharsAreNumeric = ((ch >=
'0') && (ch <=
'9'));
393 if (allFollowingCharsAreNumeric)
430 KKStr fullLevelName =
"";
433 while ((curLevel < level) && (curLevel < levelNames.size ()))
436 fullLevelName = levelNames[curLevel];
438 fullLevelName <<
"_" << levelNames[curLevel];
463 if (countFactor != 0.0f)
501 for (
kkint32 x = 0; x < c; ++x)
528 while (t && (!cancelFlag))
548 const KKStr& varName,
562 undefinedLoaded (
false)
564 MLClass::AddImageClassList (
this);
573 MLClass::AddImageClassList (
this);
575 kkuint32 numOfClasses = _mlClasses.QueueSize ();
578 for (x = 0; x < numOfClasses; x++)
580 AddMLClass (_mlClasses.IdxToPtr (x));
592 undefinedLoaded (
false)
594 MLClass::AddImageClassList (
this);
596 Load (_fileName
, _successfull
);
598 if (!undefinedLoaded)
602 undefinedLoaded =
true;
611 MLClass::DeleteImageClassList (
this);
618 return sizeof (MLClassList) +
sizeof (MLClassPtr) * size ();
633 const KKStr& oldName,
634 const KKStr& newName,
639 if ((existingClass != NULL) && (existingClass != mlClass))
642 <<
"MLClassList::ChangeNameOfClass NewName[" << newName <<
"] alreadty used." << endl
648 NameIndex::iterator idx = nameIndex.find (oldName.ToUpper ());
649 if (idx == nameIndex.end ())
652 <<
"MLClassList::ChangeNameOfClass NewName[" << oldName <<
"] not found." << endl
658 nameIndex.erase (idx);
659 nameIndex.insert (pair<KKStr,MLClassPtr> (newName.ToUpper (), mlClass));
671 MLClassList::const_iterator idx;
672 for (idx = begin (); idx != end (); idx++)
673 numHierarchialLevels = Max (numHierarchialLevels, (*idx)->NumHierarchialLevels ());
674 return numHierarchialLevels;
690 cerr <<
"MLClassList::ReadInData *** ERROR ***" << endl
691 <<
" Input File[" << _fileName <<
"] not Valid." << endl;
692 _successfull =
false;
707 undefinedLoaded =
true;
727 ofstream outFile (_fileName.Str ());
733 for (idx = 0; idx < qSize; idx++)
735 mlClass = IdxToPtr (idx);
736 outFile << mlClass->ToString ().Str () << endl;
748 NameIndex::iterator idx;
749 idx = nameIndex.find (_mlClass->UpperName ());
750 if (idx == nameIndex.end ())
751 nameIndex.insert (pair<KKStr,MLClassPtr>(_mlClass->UpperName (), _mlClass));
760 cerr <<
"MLClassList::AddMLClass Class Name Empty" << endl;
762 KKQueue<MLClass>::PushOnBack (_mlClass);
763 AddMLClassToNameIndex (_mlClass);
770 NameIndex::iterator idx;
771 MLClassPtr ic = KKQueue<MLClass>::PopFromBack ();
775 idx = nameIndex.find (ic->UpperName ());
776 if (idx != nameIndex.end ())
777 nameIndex.erase (idx);
785 NameIndex::iterator idx;
786 MLClassPtr ic = KKQueue<MLClass>::PopFromFront ();
790 idx = nameIndex.find (ic->UpperName ());
791 if (idx != nameIndex.end ())
792 nameIndex.erase (idx);
802 cerr <<
"MLClassList::PushOnBack Class Name Empty" << endl;
805 KKQueue<MLClass>::PushOnBack (_mlClass);
806 AddMLClassToNameIndex (_mlClass);
815 cerr <<
"MLClassList::PushOnFront Class Name Empty" << endl;
818 KKQueue<MLClass>::PushOnBack (_mlClass);
819 AddMLClassToNameIndex (_mlClass);
832 NameIndex::const_iterator idx;
833 idx = nameIndex.find (_name.ToUpper ());
834 if (idx == nameIndex.end ())
845 MLClassList::const_iterator idx;
847 for (idx = begin (); idx != end (); idx++)
878 for (
auto idx: *
this)
880 if (idx->UnDefined ())
929 sort (begin (), end (), *c);
939 KKStr s (10 * QueueSize ());
941 for (
kkint32 i = 0; i < QueueSize (); i++)
946 s << IdxToPtr (i)->Name ();
957 KKStr s (10 * QueueSize ());
958 for (
kkint32 i = 0; i < QueueSize (); i++)
960 if (i > 0) s <<
"\t";
961 s << IdxToPtr (i)->Name ();
972 KKStr s (10 * QueueSize ());
973 for (
kkint32 i = 0; i < QueueSize (); i++)
976 s << IdxToPtr (i)->Name ();
987 KKStr s (10 * QueueSize ());
988 for (
kkint32 i = 0; i < QueueSize (); i++)
991 s << IdxToPtr (i)->Name ().QuotedStr ();
1006 for (idx = begin(); idx != end (); ++idx)
1008 if ((*idx)->Summarize ())
1009 result->PushOnBack (*idx);
1023 for (idx = begin(); idx != end (); ++idx)
1025 if ((*idx)->Mandatory ())
1026 result->PushOnBack (*idx);
1043 for (x = 0; x < QueueSize (); x++)
1051 KKStr className = IdxToPtr (x)->Name ();
1055 titleLine2 << className;
1078 for (x = 0; x < QueueSize (); x++)
1087 KKStr part1, part2, part3;
1088 part1
= part2
= part3
= "";
1091 KKStr className = IdxToPtr (x)->Name ();
1112 case 1: titleLine3 << part1;
1115 case 2: titleLine2 << part1;
1116 titleLine3 << part2;
1119 case 3: titleLine1 << part1;
1120 titleLine2 << part2;
1121 titleLine3 << part3;
1144 for (x = 0; x < QueueSize (); x++)
1146 KKStr part1, part2, part3;
1147 part1
= part2
= part3
= "";
1150 KKStr className = IdxToPtr (x)->Name ();
1174 case 1: titleLine1 << blankField;
1175 titleLine2 << blankField;
1176 titleLine3 << part1;
1179 case 2: titleLine1 << blankField;
1180 titleLine2 << part1;
1181 titleLine3 << part2;
1184 case 3: titleLine1 << part1;
1185 titleLine2 << part2;
1186 titleLine3 << part3;
1197 KKStr header (QueueSize () * 50);
1201 MLClassList::const_iterator idx;
1202 for (idx = begin (); idx != end (); idx++)
1209 for (x = 0; x < parts.size (); x++)
1227 MLClassList::iterator idx;
1228 for (idx = begin (); idx != end (); idx++)
1233 if (newList->PtrToIdx (classForLevel) < 0)
1234 newList->AddMLClass (classForLevel);
1250 MLClassList::const_iterator idx;
1252 for (idx = list2.begin (); idx != list2.end (); idx++)
1255 if (result->PtrToIdx (ic) < 0)
1258 result->AddMLClass (ic);
1275 VectorKKStr::iterator idx;
1276 for (idx = names.begin (); idx != names.end (); idx++)
1277 MLClassPtr c = classList->GetMLClassPtr (*idx);
1293 startTag.AddAtribute (
"Count", (kkint32)size ());
1297 MLClassList::const_iterator idx;
1298 for (idx = begin (); idx != end (); idx++)
1315 if (QueueSize () != right.QueueSize ())
1318 for (
kkint32 i = 0; i < QueueSize (); i++)
1344 while (QueueSize () > 0)
1350 MLClassList::const_iterator idx;
1351 for (idx = right.begin (); idx != right.end (); idx++)
1367 MLClassList::const_iterator idx;
1368 for (idx = right.begin (); idx != right.end (); idx++)
1371 if (PtrToIdx (ic) < 0)
1388 MLClassList::const_iterator idx;
1389 for (idx = right.begin (); idx != right.end (); idx++)
1404 MLClassList::const_iterator idx;
1405 for (idx = begin (); idx != end (); idx++)
1408 if (right.PtrToIdx (ic) < 0)
1409 result.PushOnBack (ic);
1417 ostream&
KKMLL::operator<< ( ostream& os,
1448 while (t && (!cancelFlag))
1500 const KKStr& varName,
1535 bool dupEntry =
false;
1536 for (idx = _list.begin (); idx != _list.end (); idx++)
1538 AddClassIndexAssignment (idx->first, idx->second, dupEntry);
1547 MLClassList::const_iterator idx;
1548 for (idx = classes.begin (); idx != classes.end (); idx++)
1551 insert (pair<MLClassPtr, kkint16> (*idx, largestIndex));
1552 shortIdx.insert (pair<kkint16, MLClassPtr> (largestIndex, *idx));
1571 return sizeof (MLClassIndexList) + (shortIdx.size () * (
sizeof (kkint16) +
sizeof (MLClassPtr) + 10));
1581 map<MLClassPtr, kkint16>::iterator p;
1589 kkint32 index = largestIndex + 1;
1590 largestIndex = index;
1592 insert (pair<MLClassPtr, kkint16> (_ic, index));
1593 shortIdx.insert (pair<kkint16, MLClassPtr> (index, _ic));
1604 map<MLClassPtr, kkint16>::iterator p;
1612 insert (pair<MLClassPtr, kkint16> (_ic, _classIndex));
1613 shortIdx.insert (pair<kkint16, MLClassPtr> (_classIndex, _ic));
1615 if (_classIndex > largestIndex)
1616 largestIndex = _classIndex;
1624 map<MLClassPtr, kkint16>::iterator p;
1638 map<kkint16, MLClassPtr>::iterator p;
1639 p = shortIdx.find (classIndex);
1640 if (p == shortIdx.end ())
1665 log.Level (-1) << endl
1666 <<
"MLClassIndexList::ParseClassIndexList ***ERROR*** Missing index for Class: " << className << endl
1674 bool duplicate =
false;
1678 log.Level (-1) << endl
1679 <<
"MLClassIndexList::ParseClassIndexList ***ERROR*** Duplicate ClassIndex[" << classIndex <<
"] specified for class[" << className <<
"]" << endl
1692 map<kkint16, MLClassPtr>::const_iterator idx;
1693 for (idx = shortIdx.begin (); idx != shortIdx.end (); idx++)
1697 delStr << idx->second->Name ().QuotedStr () <<
":" << idx->first;
1713 while (t && (!cancelFlag))
static void WriteXML(const MLClass &mlClass, const KKStr &varName, std::ostream &o)
__int16 kkint16
16 bit signed integer.
KKStr(kkint32 size)
Creates a KKStr object that pre-allocates space for 'size' characters.
XmlTag(const KKStr &_name, TagTypes _tagType)
void StoredOnDataBase(bool _storedOnDataBase)
Provides a detailed description of the attributes of a dataset.
bool EqualIgnoreCase(const char *s2) const
void AddAtribute(const KKStr &attributeName, bool attributeValue)
MLClassPtr GetUnKnownClass()
Return a pointer to the MLClass object that represents the unknown Class in the list.
static MLClassListPtr MergeClassList(const MLClassList &list1, const MLClassList &list2)
Maintains a list of classes and their associated integer index.
virtual void ReadXML(XmlStream &s, XmlTagConstPtr tag, VolConstBool &cancelFlag, RunLog &log)
void TrimWhiteSpace(const char *_whiteSpace=" ")
After this call all leading and trailing whitespace will be trimmed from tokens.
KKStr ToCommaDelimitedStr() const
GoalKeeper * GoalKeeperPtr
KKStr & TrimRight(const char *whiteSpaceChars="\n\r\t ")
MLClassPtr GetNoiseClass() const
XmlElement(XmlTagPtr _nameTag, XmlStream &s, RunLog &log)
KKStrConstPtr AttributeValueByIndex(kkuint32 index) const
KKStr GetNextToken(const char *delStr="\n\t\r ")
Extract next Token from string, tokens will be separated by delimiter characters. ...
KKStr ToCommaDelString() const
Returns string consisting of all contained classes indicating ClassIndex assigned to each class...
void LeftPad(kkint32 width, uchar ch= ' ')
pads the string with enough 'ch' characters on the left side until the string is as long as 'width' c...
static bool FinalCleanUpRanAlready()
KKStr ExtractToken(const char *delStr="\n\t\r ")
kkint16 GetClassIndex(MLClassPtr c)
Returns the corresponding index to the class 'c'; if not in list will return -1.
kkint32 InstancesOfChar(char ch) const
KKStr ExtractHTMLTableHeader() const
Will generate a HTML formatted string that can be used in a HTML table.
MLClassIndexList(const MLClassIndexList &_list)
unsigned __int16 kkuint16
16 bit unsigned integer.
static void ChangeNameOfClass(MLClassPtr mlClass, const KKStr &newName, bool &changeSuccessful)
Changes the name of an existing class verifying that a duplicate does not get created.
KKStr & operator=(const char *src)
static void Destroy(volatile GoalKeeperPtr &_goalKeeperInstance)
Destroys an existing instance of GoalKeeper.
Represents a "Class" in the Machine Learning Sense.
void ExtractTwoTitleLines(KKStr &titleLine1, KKStr &titleLine2) const
Using the class names create two title lines where we split names by "_" characters between the two l...
static void WriteXML(const MLClassList &mlClassList, const KKStr &varName, std::ostream &o)
char LastDelimiter() const
XmlContent * XmlContentPtr
MLClassList operator-(const MLClassList &right) const
bool operator!=(const MLClassList &right) const
static KKStr GetClassNameFromDirName(const KKStr &subDir)
bool operator==(const char *rtStr) const
static MLClassPtr GetUnKnownClassStatic()
kkint32 MemoryConsumedEstimated() const
bool operator==(const MLClassList &right) const
XmlElementMLClassNameList(XmlTagPtr tag, XmlStream &s, VolConstBool &cancelFlag, RunLog &log)
virtual MLClassPtr LookUpByName(const KKStr &_name) const
Returns a pointer of MLClass object with name (_name); if none in list will then return NULL...
const KKStr & ParentName() const
void Parent(MLClassPtr _parent)
virtual void WriteXML(const KKStr &varName, std::ostream &o) const
static void Create(const KKStr &_name, volatile GoalKeeperPtr &_newGoalKeeper)
Create a GoalKeeper object and avoid a race condition doing it.
static void FinalCleanUp()
Call this as the last thing the application does, will delete all existing instances of 'MLClass'...
void CountFactor(float _countFactor)
unsigned __int32 kkuint32
void Mandatory(bool _mandatory)
KKStr ToCommaDelimitedQuotedStr() const
char operator[](kkuint32 i) const
MLClassPtr GetMLClass(kkint16 classIndex)
Locates the MLClass that was assigned classIndex.
bool operator()(MLClassPtr p1, MLClassPtr p2)
static void FinalCleanUp()
Clean up function, call just before exiting the application.
KKStr & operator=(KKStr &&src)
void Description(const KKStr &_description)
MLClassIndexList(const MLClassList &_classes)
void Save(KKStr _fileName, bool &_successfull)
XmlElementMLClass(XmlTagPtr tag, XmlStream &s, VolConstBool &cancelFlag, RunLog &log)
static MLClassListPtr BuildListFromDelimtedStr(const KKStr &s, char delimiter)
kkuint32 Len() const
Returns the number of characters in the string.
KKStrConstPtr AttributeValueByName(const KKStr &name) const
void AddAtribute(const KKStr &attributeName, kkint32 attributeValue)
MLClassList & operator+=(const MLClassList &right)
KKStrParser(const KKStr &_str)
kkint32 AttributeCount() const
virtual MLClassPtr PopFromFront()
MLClassList & operator-=(const MLClassList &right)
KKStr(const KKStr &str)
Copy Constructor.
void AddClass(MLClassPtr _ic, bool &_dupEntry)
MLClassListPtr ExtractMandatoryClasses() const
void StartBlock()
Initiates a Block as long as another thread has not already locked this object.
void AddAtribute(const KKStr &attributeName, const KKStr &attributeValue)
KKStr ToTabDelimitedStr() const
void TrimLeft(const char *whiteSpaceChars="\n\r\t ")
virtual void Clear()
Clears the contents of this list and updates nameIndex structure.
XmlTag const * XmlTagConstPtr
MLClassListPtr TakeOwnership()
Manages the reading and writing of objects in a simple XML format. For a class to be supported by Xml...
virtual ~XmlElementMLClass()
KKStr SubStrPart(kkint32 firstChar, kkint32 lastChar) const
returns a SubString consisting of all characters starting at index 'firstChar' and ending at 'lastInd...
kkuint16 NumHierarchialLevels() const
virtual void PushOnBack(MLClassPtr mlClass)
MLClassListPtr Value() const
void EndBlock()
Ends the block and allows other threads to pass through StatBlock.
static void ResetAllParentsToAllClasses()
static KKStr Concat(const std::vector< std::string > &values)
Concatenates the list of 'std::string' strings.
kkint32 LocateLastOccurrence(char ch) const
Returns index of last occurrence of 'ch' otherwise -1.
MLClassList(const KKStr &fileName, bool &successfull)
Construct a MLClassList object from the contents of a file.
kkint32 LocateCharacter(char ch) const
Returns index of 1st occurrence of 'ch' otherwise -1.
static const KKStr & EmptyStr()
Static method that returns an Empty String.
void ExtractThreeTitleLines(KKStr &titleLine1, KKStr &titleLine2, KKStr &titleLine3) const
Using the class names create three title lines where we split names by "_" characters between the thr...
KKB::kkuint16 NumHierarchialLevels() const
void AddClassIndexAssignment(MLClassPtr _ic, kkint16 _classIndex, bool &_dupEntry)
MLClassListPtr ExtractListOfClassesForAGivenHierarchialLevel(kkint32 level)
KKStr osGetRootNameOfDirectory(KKStr fullDirName)
const KKStr & UpperName() const
MLClassList & operator=(const MLClassList &right)
void ParseClassIndexList(const KKStr &s, RunLog &log)
const KKStr & Name() const
void ClassId(kkint32 _classId)
virtual MLClassPtr LookUpByClassId(kkint32 _classId) const
std::ostream &__cdecl operator<<(std::ostream &os, const KKStr &str)
const char * Str() const
Returns a pointer to a ascii string.
static MLClassPtr CreateNewMLClass(const KKStr &_name, kkint32 _classId=-1)
Static method used to create a new instance of a MLClass object.
void RightPad(kkint32 width, char ch= ' ')
Pads string on the right side with specified character so that the string will be of specified length...
void WriteXML(const KKStr &varName, std::ostream &o) const
MLClassList(const MLClassList &_mlClasses)
Copy constructor; will copy list but not own the contents.
void Summarize(bool _summarize)
virtual MLClassPtr PopFromBack()
void ExtractThreeTitleLines(KKStr &titleLine1, KKStr &titleLine2, KKStr &titleLine3, kkint32 fieldWidth) const
FILE * osFOPEN(const char *fileName, const char *mode)
static void WriteXml(const KKStr &s, std::ostream &o)
void WriteXML(std::ostream &o)
static MLClassListPtr BuildListOfDecendents(MLClassPtr parent)
KKStr & operator=(const KKStr &src)
KKStrPtr const Content() const
Used for logging messages.
void EncodeProblem(const struct svm_paramater ¶m, struct svm_problem &prob_in, struct svm_problem &prob_out)
bool operator<(const KKStr &right) const
void Load(const KKStr &_fileName, bool &_successfull)
virtual ~XmlElementMLClassNameList()
MLClassList * MLClassListPtr
void WriteXML(const KKStr &varName, std::ostream &o) const
virtual MLClassPtr GetMLClassPtr(const KKStr &_name)
return pointer to instance with '_name'; if none exists, create one and add to list.
static MLClassListPtr GlobalClassList()
kkint32 MemoryConsumedEstimated() const
Class that manages the extraction of tokens from a String without being destructive to the original s...
MLClassListPtr ExtractSummarizeClasses() const
virtual XmlTokenPtr GetNextToken(VolConstBool &cancelFlag, RunLog &log)
VectorKKStr Split(char del) const
Splits the string up into tokens using 'del' as the separator returning them in a vector...
bool ToBool() const
Returns the bool equivalent of the string, ex 'Yes' = true, 'No' = false, 'True' = true...
Maintains a list of MLClass instances.
virtual void PushOnFront(MLClassPtr mlClass)
virtual void AddMLClass(MLClassPtr _mlClass)
Will only write the ClassName rather than complete MLClass instances.
static MLClassPtr GetByClassId(kkint32 _classId)
MLClassPtr MLClassForGivenHierarchialLevel(KKB::kkuint16 level) const
KKStr SubStrPart(kkint32 firstChar) const
returns a SubString consisting of all characters starting at index 'firstChar' until the end of the s...
bool IsAnAncestor(MLClassPtr c) const
#define XmlFactoryMacro(NameOfClass)
KKStrConstPtr AttributeNameByIndex(kkuint32 index) const
KKB::KKStrPtr osReadRestOfLine(FILE *in, bool &eof)
void AddAtribute(const KKStr &attributeName, double attributeValue)
volatile const bool VolConstBool