MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
LabelMapping.cs
1using System;
2using System.Collections.Generic;
4using System.Linq;
5using System.Text;
6using System.Threading.Tasks;
7
8namespace MyCaffe.basecode
9{
13 [Serializable]
14 public class LabelMappingCollection : IEnumerable<LabelMapping>
15 {
16 List<LabelMapping> m_rgMappings = new List<LabelMapping>();
17
22 {
23 }
24
28 public int Count
29 {
30 get { return m_rgMappings.Count; }
31 }
32
36 public List<LabelMapping> Mappings
37 {
38 get { return m_rgMappings; }
39 set { m_rgMappings = value.OrderBy(p => p.OriginalLabel).ToList(); }
40 }
41
47 public LabelMapping this[int nIdx]
48 {
49 get { return m_rgMappings[nIdx]; }
50 set { m_rgMappings[nIdx] = value; }
51 }
52
57 public void Add(LabelMapping map)
58 {
59 foreach (LabelMapping m in m_rgMappings)
60 {
61 if (m.OriginalLabel == map.OriginalLabel && m.ConditionBoostEquals == map.ConditionBoostEquals)
62 throw new Exception("You already have a mapping for the original label '" + map.OriginalLabel.ToString() + "' with the conditional boost = '" + map.ConditionBoostEquals.ToString() + "'.");
63 }
64
65 m_rgMappings.Add(map);
66 m_rgMappings = m_rgMappings.OrderBy(p => p.OriginalLabel).ToList();
67 }
68
74 public bool Remove(LabelMapping map)
75 {
76 return m_rgMappings.Remove(map);
77 }
78
84 {
86
87 foreach (LabelMapping m in m_rgMappings)
88 {
89 col.Add(m.Clone());
90 }
91
92 return col;
93 }
94
108 public int MapLabel(int nLabel, int nBoost)
109 {
110 foreach (LabelMapping m in m_rgMappings)
111 {
112 if (m.OriginalLabel == nLabel)
113 {
114 if (!m.ConditionBoostEquals.HasValue)
115 {
116 return m.NewLabel;
117 }
118 else
119 {
120 if (m.ConditionBoostEquals == nBoost)
121 return m.NewLabel;
122 else if (m.NewLabelConditionFalse.HasValue)
123 return m.NewLabelConditionFalse.Value;
124 }
125 }
126 }
127
128 return nLabel;
129 }
130
136 public int MapLabelWithoutBoost(int nLabel)
137 {
138 foreach (LabelMapping m in m_rgMappings)
139 {
140 if (m.OriginalLabel == nLabel )
141 return m.NewLabel;
142 }
143
144 return nLabel;
145 }
146
151 public IEnumerator<LabelMapping> GetEnumerator()
152 {
153 return m_rgMappings.GetEnumerator();
154 }
155
160 System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
161 {
162 return m_rgMappings.GetEnumerator();
163 }
164
168 public void Sort()
169 {
170 m_rgMappings.Sort(new Comparison<LabelMapping>(compare));
171 }
172
173 private int compare(LabelMapping a, LabelMapping b)
174 {
176 return -1;
177
179 return 1;
180
181 if (a.NewLabel < b.NewLabel)
182 return -1;
183
184 if (a.NewLabel > b.NewLabel)
185 return 1;
186
187 return 0;
188 }
189
196 {
197 Sort();
198 col.Sort();
199
200 string strA = ToString();
201 string strB = col.ToString();
202
203 if (strA == strB)
204 return true;
205
206 return false;
207 }
208
213 public override string ToString()
214 {
215 List<string> rgstr = ToStringList();
216 string str = "";
217
218 foreach (string strMapping in rgstr)
219 {
220 str += strMapping;
221 str += ";";
222 }
223
224 return str.TrimEnd(';');
225 }
226
231 public List<string> ToStringList()
232 {
233 List<string> rgstrMappings = new List<string>();
234
235 foreach (LabelMapping map in m_rgMappings)
236 {
237 rgstrMappings.Add(map.ToString());
238 }
239
240 return rgstrMappings;
241 }
242
248 public static LabelMappingCollection Parse(string strMappings)
249 {
250 string[] rgstrMappings = strMappings.Split(';');
251 return Parse(new List<string>(rgstrMappings));
252 }
253
259 public static LabelMappingCollection Parse(List<string> rgstrMappings)
260 {
262 List<string> rgstrSeparators = new List<string>() { "->" };
263
264 foreach (string strMapping in rgstrMappings)
265 {
266 string[] rgstr = strMapping.Split(rgstrSeparators.ToArray(), StringSplitOptions.RemoveEmptyEntries);
267
268 if (rgstr.Length != 2)
269 throw new Exception("Each label mapping should have the format 'original->new_label'.");
270
271 if (rgstr[0].Length == 0 || rgstr[1].Length == 0)
272 throw new Exception("Each label mapping should have the format 'original->new_label'.");
273
274 string str1 = rgstr[0].Trim('\"');
275 string str2 = rgstr[1].Trim('\"');
276 int? nConditionBoostEquals = null;
277 int? nConditionFalse = null;
278
279 rgstr = str2.Split('?');
280 if (rgstr.Length > 1)
281 {
282 str2 = rgstr[0].Trim();
283
284 string strCondition = "";
285 string strRightSide = rgstr[1].Trim();
286 rgstr = strRightSide.Split(',');
287
288 if (rgstr.Length == 2)
289 {
290 string str3 = rgstr[0].Trim();
291 if (str3.Length > 0)
292 nConditionFalse = int.Parse(str3);
293
294 strCondition = rgstr[1].Trim();
295 }
296 else if (rgstr.Length == 1)
297 {
298 strCondition = rgstr[0].Trim();
299 }
300 else
301 {
302 throw new Exception("Invalid mapping format! Expected format <true_int>?<false_int>,boost=<int>");
303 }
304
305 rgstr = strCondition.Split('=');
306
307 if (rgstr.Length != 2 || rgstr[0].Trim().ToLower() != "boost")
308 throw new Exception("Invalid boost condition! Expected format = ?boost=<int>");
309
310 nConditionBoostEquals = int.Parse(rgstr[1].Trim());
311 }
312
313 col.Add(new LabelMapping(int.Parse(str1), int.Parse(str2), nConditionBoostEquals, nConditionFalse));
314 }
315
316 return col;
317 }
318 }
319
323 [Serializable]
324 [TypeConverter(typeof(ExpandableObjectConverter))]
325 public class LabelMapping
326 {
327 int m_nOrignalLabel = 0;
328 int m_nNewLabelConditionTrue = 0;
329 int? m_nNewLabelConditionFalse = null;
330 int? m_nConditionBoostEquals = null;
331
332
340 public LabelMapping(int nOriginalLabel, int nNewLabel, int? nConditionBoostEquals, int? nNewLabelConditionFalse)
341 {
342 m_nOrignalLabel = nOriginalLabel;
343 m_nNewLabelConditionTrue = nNewLabel;
344 m_nNewLabelConditionFalse = nNewLabelConditionFalse;
345 m_nConditionBoostEquals = nConditionBoostEquals;
346 }
347
352 {
353 }
354
358 [Description("Specifies the original label.")]
359 public int OriginalLabel
360 {
361 get { return m_nOrignalLabel; }
362 set { m_nOrignalLabel = value; }
363 }
364
368 [Description("Specifies the new label replacement.")]
369 public int NewLabel
370 {
371 get { return m_nNewLabelConditionTrue; }
372 set { m_nNewLabelConditionTrue = value; }
373 }
374
378 [Description("Specifies the label to use if the boost condition fails.")]
380 {
381 get { return m_nNewLabelConditionFalse; }
382 set { m_nNewLabelConditionFalse = value; }
383 }
384
388 [Description("Specifies the boost condition to test.")]
390 {
391 get { return m_nConditionBoostEquals; }
392 set { m_nConditionBoostEquals = value; }
393 }
394
400 {
401 return new LabelMapping(m_nOrignalLabel, m_nNewLabelConditionTrue, m_nConditionBoostEquals, m_nNewLabelConditionFalse);
402 }
403
425 public static LabelMapping Parse(string str)
426 {
427 string strTarget = "->";
428 int nPos = str.IndexOf(strTarget);
429 if (nPos < 0)
430 throw new Exception("Invalid label mapping format, missing '" + strTarget + "'!");
431
432 string strLabelOriginal = str.Substring(0, nPos);
433 str = str.Substring(nPos + strTarget.Length);
434
435 string strCondition = null;
436 string strNewLabelFalse = null;
437 string strNewLabel = str;
438 nPos = str.IndexOf('?');
439 if (nPos > 0)
440 {
441 strNewLabel = str.Substring(0, nPos);
442 str = str.Substring(nPos + 1);
443
444 nPos = str.IndexOf(',');
445 if (nPos > 0)
446 {
447 strNewLabelFalse = str.Substring(0, nPos);
448 str = str.Substring(nPos + 1);
449 }
450
451 strTarget = "boost=";
452 nPos = str.IndexOf(strTarget);
453 if (nPos < 0)
454 throw new Exception("Invalid label mapping format, missing '" + strTarget + "'!");
455
456 str = str.Substring(nPos + strTarget.Length);
457 strCondition = str;
458 }
459
460 int nOriginalLabel = int.Parse(strLabelOriginal);
461 int nNewLabel = int.Parse(strNewLabel);
462 int? nNewLabelFalse = null;
463 int? nCondition = null;
464
465 if (strNewLabelFalse != null)
466 nNewLabelFalse = int.Parse(strNewLabelFalse);
467
468 if (strCondition != null)
469 nCondition = int.Parse(strCondition);
470
471 return new LabelMapping(nOriginalLabel, nNewLabel, nCondition, nNewLabelFalse);
472 }
473
478 public override string ToString()
479 {
480 string strMapping = m_nOrignalLabel.ToString() + "->" + m_nNewLabelConditionTrue.ToString();
481
482 if (m_nConditionBoostEquals.HasValue)
483 {
484 strMapping += "?";
485
486 if (m_nNewLabelConditionFalse.HasValue)
487 {
488 strMapping += m_nNewLabelConditionFalse.Value.ToString();
489 strMapping += ",";
490 }
491
492 strMapping += "boost=";
493 strMapping += m_nConditionBoostEquals.Value.ToString();
494 }
495
496 return strMapping;
497 }
498 }
499}
The LabelMappingCollection manages a collection of LabelMapping's.
Definition: LabelMapping.cs:15
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.
Definition: LabelMapping.cs:29
List< LabelMapping > Mappings
Returns the label mapping list.
Definition: LabelMapping.cs:37
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.
Definition: LabelMapping.cs:57
LabelMappingCollection Clone()
Returns a copy of the label mapping collection.
Definition: LabelMapping.cs:83
LabelMappingCollection()
The LabelMappingCollection constructor.
Definition: LabelMapping.cs:21
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.
Definition: LabelMapping.cs:74
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.
Definition: Annotation.cs:12