2using System.Collections.Generic;
6using System.Threading.Tasks;
16 List<LabelMapping> m_rgMappings =
new List<LabelMapping>();
30 get {
return m_rgMappings.Count; }
38 get {
return m_rgMappings; }
39 set { m_rgMappings = value.OrderBy(p => p.OriginalLabel).ToList(); }
49 get {
return m_rgMappings[nIdx]; }
50 set { m_rgMappings[nIdx] = value; }
62 throw new Exception(
"You already have a mapping for the original label '" + map.OriginalLabel.ToString() +
"' with the conditional boost = '" + map.ConditionBoostEquals.ToString() +
"'.");
65 m_rgMappings.Add(map);
66 m_rgMappings = m_rgMappings.OrderBy(p => p.OriginalLabel).ToList();
76 return m_rgMappings.Remove(map);
153 return m_rgMappings.GetEnumerator();
160 System.Collections.IEnumerator
System.Collections.IEnumerable.GetEnumerator()
162 return m_rgMappings.GetEnumerator();
170 m_rgMappings.Sort(
new Comparison<LabelMapping>(compare));
218 foreach (
string strMapping
in rgstr)
224 return str.TrimEnd(
';');
233 List<string> rgstrMappings =
new List<string>();
240 return rgstrMappings;
250 string[] rgstrMappings = strMappings.Split(
';');
251 return Parse(
new List<string>(rgstrMappings));
262 List<string> rgstrSeparators =
new List<string>() {
"->" };
264 foreach (
string strMapping
in rgstrMappings)
266 string[] rgstr = strMapping.Split(rgstrSeparators.ToArray(), StringSplitOptions.RemoveEmptyEntries);
268 if (rgstr.Length != 2)
269 throw new Exception(
"Each label mapping should have the format 'original->new_label'.");
271 if (rgstr[0].Length == 0 || rgstr[1].Length == 0)
272 throw new Exception(
"Each label mapping should have the format 'original->new_label'.");
274 string str1 = rgstr[0].Trim(
'\"');
275 string str2 = rgstr[1].Trim(
'\"');
276 int? nConditionBoostEquals =
null;
277 int? nConditionFalse =
null;
279 rgstr = str2.Split(
'?');
280 if (rgstr.Length > 1)
282 str2 = rgstr[0].Trim();
284 string strCondition =
"";
285 string strRightSide = rgstr[1].Trim();
286 rgstr = strRightSide.Split(
',');
288 if (rgstr.Length == 2)
290 string str3 = rgstr[0].Trim();
292 nConditionFalse =
int.Parse(str3);
294 strCondition = rgstr[1].Trim();
296 else if (rgstr.Length == 1)
298 strCondition = rgstr[0].Trim();
302 throw new Exception(
"Invalid mapping format! Expected format <true_int>?<false_int>,boost=<int>");
305 rgstr = strCondition.Split(
'=');
307 if (rgstr.Length != 2 || rgstr[0].Trim().ToLower() !=
"boost")
308 throw new Exception(
"Invalid boost condition! Expected format = ?boost=<int>");
310 nConditionBoostEquals =
int.Parse(rgstr[1].Trim());
324 [TypeConverter(typeof(ExpandableObjectConverter))]
327 int m_nOrignalLabel = 0;
328 int m_nNewLabelConditionTrue = 0;
329 int? m_nNewLabelConditionFalse =
null;
330 int? m_nConditionBoostEquals =
null;
340 public LabelMapping(
int nOriginalLabel,
int nNewLabel,
int? nConditionBoostEquals,
int? nNewLabelConditionFalse)
342 m_nOrignalLabel = nOriginalLabel;
343 m_nNewLabelConditionTrue = nNewLabel;
344 m_nNewLabelConditionFalse = nNewLabelConditionFalse;
345 m_nConditionBoostEquals = nConditionBoostEquals;
358 [Description(
"Specifies the original label.")]
361 get {
return m_nOrignalLabel; }
362 set { m_nOrignalLabel = value; }
368 [Description(
"Specifies the new label replacement.")]
371 get {
return m_nNewLabelConditionTrue; }
372 set { m_nNewLabelConditionTrue = value; }
378 [Description(
"Specifies the label to use if the boost condition fails.")]
381 get {
return m_nNewLabelConditionFalse; }
382 set { m_nNewLabelConditionFalse = value; }
388 [Description(
"Specifies the boost condition to test.")]
391 get {
return m_nConditionBoostEquals; }
392 set { m_nConditionBoostEquals = value; }
401 return new LabelMapping(m_nOrignalLabel, m_nNewLabelConditionTrue, m_nConditionBoostEquals, m_nNewLabelConditionFalse);
427 string strTarget =
"->";
428 int nPos = str.IndexOf(strTarget);
430 throw new Exception(
"Invalid label mapping format, missing '" + strTarget +
"'!");
432 string strLabelOriginal = str.Substring(0, nPos);
433 str = str.Substring(nPos + strTarget.Length);
435 string strCondition =
null;
436 string strNewLabelFalse =
null;
437 string strNewLabel = str;
438 nPos = str.IndexOf(
'?');
441 strNewLabel = str.Substring(0, nPos);
442 str = str.Substring(nPos + 1);
444 nPos = str.IndexOf(
',');
447 strNewLabelFalse = str.Substring(0, nPos);
448 str = str.Substring(nPos + 1);
451 strTarget =
"boost=";
452 nPos = str.IndexOf(strTarget);
454 throw new Exception(
"Invalid label mapping format, missing '" + strTarget +
"'!");
456 str = str.Substring(nPos + strTarget.Length);
460 int nOriginalLabel =
int.Parse(strLabelOriginal);
461 int nNewLabel =
int.Parse(strNewLabel);
462 int? nNewLabelFalse =
null;
463 int? nCondition =
null;
465 if (strNewLabelFalse !=
null)
466 nNewLabelFalse =
int.Parse(strNewLabelFalse);
468 if (strCondition !=
null)
469 nCondition =
int.Parse(strCondition);
471 return new LabelMapping(nOriginalLabel, nNewLabel, nCondition, nNewLabelFalse);
480 string strMapping = m_nOrignalLabel.ToString() +
"->" + m_nNewLabelConditionTrue.ToString();
482 if (m_nConditionBoostEquals.HasValue)
486 if (m_nNewLabelConditionFalse.HasValue)
488 strMapping += m_nNewLabelConditionFalse.Value.ToString();
492 strMapping +=
"boost=";
493 strMapping += m_nConditionBoostEquals.Value.ToString();
The LabelMappingCollection manages a collection of LabelMapping's.
List< string > ToStringList()
Returns a list of strings where each entry represents a mapping.
override string ToString()
Returns a string representation of the label mapping collection.
static LabelMappingCollection Parse(string strMappings)
Parses a label mapping string into a collection of label mappings.
bool Compare(LabelMappingCollection col)
Compares one label mapping collection to another.
int Count
Returns the number of items in the collection.
List< LabelMapping > Mappings
Returns the label mapping list.
int MapLabel(int nLabel, int nBoost)
Returns the mapped label associated with a given label and boost (if a boost condition is used).
void Add(LabelMapping map)
Adds a new label mapping.
LabelMappingCollection Clone()
Returns a copy of the label mapping collection.
LabelMappingCollection()
The LabelMappingCollection constructor.
int MapLabelWithoutBoost(int nLabel)
Returns the mapped label associated with a given label.
static LabelMappingCollection Parse(List< string > rgstrMappings)
Parses a list of strings where each string is a label mapping.
bool Remove(LabelMapping map)
Removes a label mapping.
void Sort()
Sorts the label mappings.
IEnumerator< LabelMapping > GetEnumerator()
Returns the enumerator of the collection.
The LabelMapping class represents a single label mapping.
LabelMapping Clone()
Return a copy of the LabelMapping.
int? ConditionBoostEquals
Get/set the boost condition to test which if met, the new label is set, otherwise it is not.
static LabelMapping Parse(string str)
Parse a string into a new LabelMapping.
int NewLabel
Get/set the new label.
LabelMapping()
The LabelMapping constructor.
override string ToString()
Return a string representation of the label mapping.
int? NewLabelConditionFalse
Get/set the label to use if the boost condition fails.
int OriginalLabel
Get/set the original label.
LabelMapping(int nOriginalLabel, int nNewLabel, int? nConditionBoostEquals, int? nNewLabelConditionFalse)
The LabelMapping constructor.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.