MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
DatabaseTemporal.cs
1using MyCaffe.basecode;
3using MyCaffe.db.image;
4using SimpleGraphing;
5using System;
6using System.Collections;
7using System.Collections.Generic;
8using System.Data;
9using System.Data.SqlClient;
10using System.Diagnostics;
11using System.IO;
12using System.Linq;
13using System.Runtime.Remoting.Messaging;
14using System.Text;
15using System.Threading.Tasks;
16using System.Windows.Forms.VisualStyles;
18
19namespace MyCaffe.db.temporal
20{
25 {
26 ConnectInfo m_ci;
27 DNNEntitiesTemporal m_entitiesTemporal = null;
28 Dictionary<int, string> m_rgstrValueItems = new Dictionary<int, string>();
29 Dictionary<int, string> m_rgstrValueItemsByIndex = new Dictionary<int, string>();
30 DataTable m_rgRawValueCache = new DataTable();
31 SqlBulkCopy m_sqlBulkCopy = null;
32
37 {
38 m_rgRawValueCache.Columns.Add("SourceID", typeof(int));
39 m_rgRawValueCache.Columns.Add("ItemID", typeof(int));
40 m_rgRawValueCache.Columns.Add("TimeStamp", typeof(DateTime));
41 m_rgRawValueCache.Columns.Add("RawData", typeof(byte[]));
42 m_rgRawValueCache.Columns.Add("Active", typeof(bool));
43 }
44
51 public override void Open(int nSrcId, FORCE_LOAD nForceLoad = FORCE_LOAD.NONE, ConnectInfo ci = null)
52 {
53 base.Open(nSrcId, nForceLoad, ci);
54 m_ci = ci;
55 m_entitiesTemporal = EntitiesConnectionTemporal.CreateEntities(ci);
56 }
57
62 public void EnableBulk(bool bEnable)
63 {
64 if (m_entitiesTemporal == null)
65 return;
66
67 m_sqlBulkCopy = new SqlBulkCopy(m_entitiesTemporal.Database.Connection.ConnectionString, SqlBulkCopyOptions.TableLock);
68 m_sqlBulkCopy.DestinationTableName = "dbo.RawValues";
69 m_sqlBulkCopy.BulkCopyTimeout = 600;
70 m_sqlBulkCopy.ColumnMappings.Add("SourceID", "SourceID");
71 m_sqlBulkCopy.ColumnMappings.Add("ItemID", "ItemID");
72 m_sqlBulkCopy.ColumnMappings.Add("TimeStamp", "TimeStamp");
73 m_sqlBulkCopy.ColumnMappings.Add("RawData", "RawData");
74 m_sqlBulkCopy.ColumnMappings.Add("Active", "Active");
75
76 m_entitiesTemporal.Configuration.AutoDetectChangesEnabled = false;
77 m_entitiesTemporal.Configuration.ValidateOnSaveEnabled = false;
78 }
79
83 public override void Close()
84 {
85 if (m_sqlBulkCopy != null)
86 {
87 m_sqlBulkCopy.Close();
88 m_sqlBulkCopy = null;
89 }
90
91 if (m_entitiesTemporal != null)
92 {
93 m_entitiesTemporal.Dispose();
94 m_entitiesTemporal = null;
95 }
96
97 m_ci = null;
98
99 base.Close();
100 }
101
107 public override bool DeleteSource(int nSrcId = 0)
108 {
109 string strCmd;
110
111 if (!base.DeleteSource(nSrcId))
112 return false;
113
115 {
116 strCmd = "IF OBJECT_ID (N'dbo.ValueItems', N'U') IS NOT NULL DELETE FROM ValueItems WHERE (SourceID = " + nSrcId.ToString() + ")";
117 entities.Database.ExecuteSqlCommand(strCmd);
118
119 strCmd = "IF OBJECT_ID (N'dbo.RawValues', N'U') IS NOT NULL DELETE FROM RawValues WHERE (SourceID = " + nSrcId.ToString() + ")";
120 entities.Database.ExecuteSqlCommand(strCmd);
121 }
122
123 DeleteSourceData(nSrcId);
124
125 return true;
126 }
127
133 public override bool DeleteSourceData(int nSrcId = 0)
134 {
135 if (!base.DeleteSourceData(nSrcId))
136 return false;
137
138 DeleteValueItems(nSrcId);
139 DeleteRawValues(nSrcId);
140
141 return true;
142 }
143
151 public override void DeleteDataset(string strDsName, bool bDeleteRelatedProjects, Log log, CancelEvent evtCancel)
152 {
153 Dataset ds = GetDataset(strDsName);
154 if (ds == null)
155 return;
156
157 Source srcTraining = GetSource(ds.TrainingSourceID.GetValueOrDefault());
158 Source srcTesting = GetSource(ds.TestingSourceID.GetValueOrDefault());
159
160 DeleteDatasetTemporalTables(srcTraining.ID, srcTesting.ID, log, evtCancel);
161
162 base.DeleteDataset(strDsName, bDeleteRelatedProjects, log, evtCancel);
163 }
164
172 public static void DeleteDatasetTemporalTables(int nSrcIDTrain, int nSrcIDTest, Log log, CancelEvent evtCancel)
173 {
174 string strCmd;
175
176 if (!DeleteRawValuesEx(nSrcIDTest, log, evtCancel))
177 return;
178
179 if (!DeleteRawValuesEx(nSrcIDTrain, log, evtCancel))
180 return;
181
183 {
184 entities.Database.CommandTimeout = 180;
185
186 strCmd = "DELETE ValueItems WHERE (SourceID = " + nSrcIDTrain.ToString() + ") OR (SourceID = " + nSrcIDTest.ToString() + ")";
187 entities.Database.ExecuteSqlCommand(strCmd);
188 }
189 }
190
195 public void DeleteRawValues(int nSrcId = 0)
196 {
197 if (nSrcId == 0)
198 nSrcId = m_src.ID;
199
200 string strCmd = "IF OBJECT_ID (N'dbo.RawValues', N'U') IS NOT NULL DELETE FROM RawValues WHERE (SourceID = " + nSrcId.ToString() + ")";
201
203 {
204 entities.Database.ExecuteSqlCommand(strCmd);
205 }
206 }
207
214 public static bool DeleteRawValuesEx(int nSrcId, Log log, CancelEvent evtCancel)
215 {
216 string strCmd = "IF OBJECT_ID (N'dbo.RawValues', N'U') IS NOT NULL SELECT COUNT(ID) FROM RawValues WHERE (SourceID = " + nSrcId.ToString() + ") ELSE SELECT 0";
217 Stopwatch sw = new Stopwatch();
218
219 sw.Start();
220
222 {
223 entities.Database.CommandTimeout = 180;
224 int lCount = entities.Database.SqlQuery<int>(strCmd).FirstOrDefault();
225 long lBlock = 10000;
226 long lStep = lCount / lBlock;
227 long lIdx = 0;
228
229 while (lIdx < lCount)
230 {
231 strCmd = "DELETE TOP (" + lBlock.ToString() + ") RawValues WHERE (SourceID = " + nSrcId.ToString() + ")";
232 entities.Database.ExecuteSqlCommand(strCmd);
233
234 lIdx += lBlock;
235
236 if (sw.Elapsed.TotalMilliseconds > 1000)
237 {
238 if (evtCancel.WaitOne(0))
239 return false;
240
241 double dfPct = (double)lIdx / (double)lCount;
242 log.WriteLine("Deleting RawValues at " + dfPct.ToString("P4"));
243 }
244 }
245
246 strCmd = "DELETE RawValues WHERE (SourceID = " + nSrcId.ToString() + ")";
247 entities.Database.ExecuteSqlCommand(strCmd);
248 }
249
250 return true;
251 }
252
257 public void DeleteValueItems(int nSrcId = 0)
258 {
259 if (nSrcId == 0)
260 nSrcId = m_src.ID;
261
262 string strCmd = "IF OBJECT_ID (N'dbo.ValueItems', N'U') IS NOT NULL DELETE FROM ValueItems WHERE (SourceID = " + nSrcId.ToString() + ")";
263
265 {
266 entities.Database.ExecuteSqlCommand(strCmd);
267 }
268 }
269
270
271 //---------------------------------------------------------------------
272 // Raw Values
273 //---------------------------------------------------------------------
274 #region Raw Values
275
283 public void PutRawValue(int nSrcID, int nItemID, RawValueDataCollection data)
284 {
285 DataRow dr = m_rgRawValueCache.NewRow();
286
287 dr["SourceID"] = nSrcID;
288 dr["ItemID"] = nItemID;
289 if (data.TimeStamp.HasValue)
290 dr["TimeStamp"] = data.TimeStamp.Value;
291 dr["RawData"] = data.ToBytes();
292 dr["Active"] = true;
293
294 m_rgRawValueCache.Rows.Add(dr);
295 }
296
306 public void PutRawValue(int nSrcID, int nItemID, DateTime dt, RawValueDataCollection data, bool bActive)
307 {
308 DataRow dr = m_rgRawValueCache.NewRow();
309
310 dr["SourceID"] = nSrcID;
311 dr["ItemID"] = nItemID;
312 dr["TimeStamp"] = dt;
313 dr["RawData"] = data.ToBytes();
314 dr["Active"] = bActive;
315
316 m_rgRawValueCache.Rows.Add(dr);
317 }
318
322 public void SaveRawValues()
323 {
324 if (m_rgRawValueCache.Rows.Count > 0)
325 {
326 try
327 {
328 m_sqlBulkCopy.WriteToServer(m_rgRawValueCache);
329 }
330 catch (Exception excpt)
331 {
332 throw excpt;
333 }
334 finally
335 {
337 }
338 }
339 }
340
344 public void ClearRawValues()
345 {
346 m_rgRawValueCache.Clear();
347 }
348
355 public RawValueSet GetValues(int nSrcID, int nItemID)
356 {
358 {
359 List<RawValue> rgData = entities.RawValues.AsNoTracking().Where(p => p.SourceID == nSrcID && p.ItemID == nItemID).OrderBy(p => p.TimeStamp).ToList();
360 return RawValueSet.FromData(nSrcID, nItemID, rgData);
361 }
362 }
363
364 #endregion
365
366
367 //---------------------------------------------------------------------
368 // Value Items
369 //---------------------------------------------------------------------
370 #region Value Items
371
382 public int AddValueItem(int nSrcId, int nItemIdx, string strName, DateTime? dtStart = null, DateTime? dtEnd = null, int? nSteps = null)
383 {
385 {
386 List<ValueItem> rgItems = entities.ValueItems.Where(p=>p.SourceID == nSrcId && p.Name == strName).ToList();
387
388 if (rgItems.Count > 0)
389 return rgItems[0].ID;
390
391 ValueItem item = new ValueItem();
392 item.Name = strName;
393 item.Idx = nItemIdx;
394 item.SourceID = nSrcId;
395 item.StartTime = dtStart;
396 item.EndTime = dtEnd;
397 item.Steps = nSteps;
398 entities.ValueItems.Add(item);
399 entities.SaveChanges();
400
401 return item.ID;
402 }
403 }
404
410 public int GetValueItemID(string strName)
411 {
413 {
414 List<ValueItem> rgItems = entities.ValueItems.Where(p => p.Name == strName).ToList();
415
416 if (rgItems.Count > 0)
417 return rgItems[0].ID;
418
419 return 0;
420 }
421 }
422
428 public string GetValueItemName(int nID)
429 {
430 if (m_rgstrValueItems.ContainsKey(nID))
431 return m_rgstrValueItems[nID];
432
434 {
435 List<ValueItem> rgItems = entities.ValueItems.Where(p => p.ID == nID).ToList();
436
437 if (rgItems.Count > 0)
438 {
439 m_rgstrValueItems.Add(nID, rgItems[0].Name);
440 m_rgstrValueItemsByIndex.Add(rgItems[0].Idx.GetValueOrDefault(0), rgItems[0].Name);
441 return rgItems[0].Name;
442 }
443
444 return null;
445 }
446 }
447
453 public List<int> GetAllItemIDs(int nSrcID)
454 {
456 {
457 return entities.ValueItems.Where(p => p.SourceID == nSrcID).Select(p => p.ID).ToList();
458 }
459 }
460
466 public int GetValueItemIndex(string strName)
467 {
469 {
470 List<ValueItem> rgItems = entities.ValueItems.Where(p => p.Name == strName).ToList();
471
472 if (rgItems.Count > 0)
473 return rgItems[0].Idx.GetValueOrDefault(0);
474
475 return 0;
476 }
477 }
478
484 public string GetValueItemNamByIndex(int nIdx)
485 {
486 if (m_rgstrValueItemsByIndex.ContainsKey(nIdx))
487 return m_rgstrValueItemsByIndex[nIdx];
488
490 {
491 List<ValueItem> rgItems = entities.ValueItems.Where(p => p.Idx == nIdx).ToList();
492
493 if (rgItems.Count > 0)
494 {
495 m_rgstrValueItemsByIndex.Add(nIdx, rgItems[0].Name);
496 m_rgstrValueItems.Add(rgItems[0].ID, rgItems[0].Name);
497 return rgItems[0].Name;
498 }
499
500 return null;
501 }
502 }
503
509 public List<int> GetAllItemIndices(int nSrcID)
510 {
512 {
513 return entities.ValueItems.Where(p => p.SourceID == nSrcID).Select(p => p.Idx.GetValueOrDefault(0)).ToList();
514 }
515 }
516
522 public List<ValueItem> GetAllValueItems(int nSrcID)
523 {
525 {
526 return entities.ValueItems.Where(p => p.SourceID == nSrcID).ToList();
527 }
528 }
529
530 #endregion
531
532 #region Value Streams
533
547 public int AddValueStream(int nSrcID, string strName, int nOrdering, STREAM_CLASS_TYPE classType, STREAM_VALUE_TYPE valType, DateTime? dtStart = null, DateTime? dtEnd = null, int? nSecPerStep = null, int nTotalSteps = 1)
548 {
550 {
551 ValueStream vs = null;
552
553 List<ValueStream> rg = entities.ValueStreams.Where(p => p.SourceID == nSrcID && p.Name == strName).ToList();
554 if (rg.Count == 0)
555 vs = new ValueStream();
556 else
557 vs = rg[0];
558
559 vs.SourceID = nSrcID;
560 vs.Name = strName;
561 vs.Ordering = (short)nOrdering;
562 vs.ClassTypeID = (byte)classType;
563 vs.ValueTypeID = (byte)valType;
564 vs.StartTime = dtStart;
565 vs.EndTime = dtEnd;
566 vs.SecondsPerStep = nSecPerStep;
567 vs.TotalSteps = nTotalSteps;
568
569 if (rg.Count == 0)
570 entities.ValueStreams.Add(vs);
571
572 entities.SaveChanges();
573
574 return vs.ID;
575 }
576 }
577
583 public List<ValueStream> GetAllValueStreams(int nSrcID)
584 {
586 {
587 return entities.ValueStreams.Where(p => p.SourceID == nSrcID).ToList();
588 }
589 }
590
591 #endregion
592 }
593
597 public class RawValueSet
598 {
599 int m_nSourceID;
600 int m_nItemID;
601 RawValueDataCollection m_staticValues = null;
602 List<RawValueDataCollection> m_rgValues = new List<RawValueDataCollection>();
603 bool m_bSorted = false;
604
610 public RawValueSet(int nSrcID, int nItemID)
611 {
612 m_nSourceID = nSrcID;
613 m_nItemID = nItemID;
614 }
615
623 public static RawValueSet FromData(int nSrcID, int nItemID, List<RawValue> rg)
624 {
625 RawValueSet set = new RawValueSet(nSrcID, nItemID);
626
627 foreach (RawValue val in rg)
628 {
629 set.Add(val);
630 }
631
632 return set;
633 }
634
640 public void Add(RawValue val)
641 {
642 if (!val.TimeStamp.HasValue)
643 {
644 if (m_staticValues != null)
645 throw new Exception("There should only be one static value set for an item!");
646
647 m_staticValues = RawValueDataCollection.LoadFromBytes(null, val.RawData);
648 }
649 else
650 {
651 m_rgValues.Add(RawValueDataCollection.LoadFromBytes(val.TimeStamp, val.RawData));
652 }
653 }
654
658 public int ColCount
659 {
660 get
661 {
662 if (m_rgValues.Count == 0)
663 return 0;
664
665 return m_rgValues.Count;
666 }
667 }
668
672 public DateTime StartTime
673 {
674 get
675 {
676 if (m_rgValues.Count == 0)
677 return DateTime.MinValue;
678
679 return m_rgValues[0].TimeStamp.Value;
680 }
681 }
682
686 public DateTime EndTime
687 {
688 get
689 {
690 if (m_rgValues.Count == 0)
691 return DateTime.MinValue;
692
693 return m_rgValues[m_rgValues.Count - 1].TimeStamp.Value;
694 }
695 }
696
700 public int SourceID
701 {
702 get { return m_nSourceID; }
703 }
704
708 public int ItemID
709 {
710 get { return m_nItemID; }
711 }
712
717 public Tuple<float[], float[]> GetStaticValues()
718 {
719 List<float> rgNum = new List<float>();
720 List<float> rgCat = new List<float>();
721
722 foreach (RawValueData data in m_staticValues)
723 {
724 if (data.ValueType == STREAM_VALUE_TYPE.NUMERIC)
725 rgNum.Add(data.Value);
726 else
727 rgCat.Add(data.Value);
728 }
729
730 return new Tuple<float[], float[]>(rgNum.ToArray(), rgCat.ToArray());
731 }
732
739 public Tuple<float[], float[], DateTime[]> GetObservedValues(int nIdx, int nCount)
740 {
741 if (nIdx + nCount >= m_rgValues.Count)
742 return null;
743
744 List<float> rgNum = new List<float>();
745 List<float> rgCat = new List<float>();
746 List<DateTime> rgTime = new List<DateTime>();
747
748 if (!m_bSorted)
749 {
750 m_rgValues = m_rgValues.OrderBy(p => p.TimeStamp).ToList();
751 m_bSorted = true;
752 }
753
754 for (int i=nIdx; i<nIdx + nCount; i++)
755 {
756 foreach (RawValueData data in m_rgValues[i])
757 {
758 if (data.ClassType == STREAM_CLASS_TYPE.OBSERVED)
759 {
760 if (data.ValueType == STREAM_VALUE_TYPE.NUMERIC)
761 rgNum.Add(data.ValueNormalized.GetValueOrDefault(data.Value));
762 else
763 rgCat.Add(data.Value);
764
765 rgTime.Add(data.TimeStamp.Value);
766 }
767 }
768 }
769
770 return new Tuple<float[], float[], DateTime[]>(rgNum.ToArray(), rgCat.ToArray(), rgTime.ToArray());
771 }
772
780 public Tuple<float[],DateTime[]> GetObservedNumValues(int nIdx, int nCount, int nValIdx)
781 {
782 if (nIdx + nCount >= m_rgValues.Count)
783 return null;
784
785 List<float> rgNum = new List<float>();
786 List<DateTime> rgTime = new List<DateTime>();
787
788 if (!m_bSorted)
789 {
790 m_rgValues = m_rgValues.OrderBy(p => p.TimeStamp).ToList();
791 m_bSorted = true;
792 }
793
794 for (int i = nIdx; i < nIdx + nCount; i++)
795 {
796 RawValueDataCollection col = m_rgValues[i];
797 RawValueData val = col[nValIdx];
798
799 if (val.ClassType != STREAM_CLASS_TYPE.OBSERVED && val.ValueType != STREAM_VALUE_TYPE.NUMERIC)
800 throw new Exception("The value at index " + nValIdx.ToString() + " is not a numeric observed value!");
801
802 rgNum.Add(val.ValueNormalized.GetValueOrDefault(val.Value));
803 rgTime.Add(val.TimeStamp.Value);
804 }
805
806 return new Tuple<float[], DateTime[]>(rgNum.ToArray(), rgTime.ToArray());
807 }
808
815 public Tuple<float[], float[], DateTime[]> GetKnownValues(int nIdx, int nCount)
816 {
817 if (nIdx + nCount >= m_rgValues.Count)
818 return null;
819
820 List<float> rgNum = new List<float>();
821 List<float> rgCat = new List<float>();
822 List<DateTime> rgTime = new List<DateTime>();
823
824 if (!m_bSorted)
825 {
826 m_rgValues = m_rgValues.OrderBy(p => p.TimeStamp).ToList();
827 m_bSorted = true;
828 }
829
830 if (nIdx + nCount >= m_rgValues.Count)
831 return null;
832
833 for (int i = nIdx; i < nIdx + nCount; i++)
834 {
835 foreach (RawValueData data in m_rgValues[i])
836 {
837 if (data.ClassType == STREAM_CLASS_TYPE.KNOWN)
838 {
839 if (data.ValueType == STREAM_VALUE_TYPE.NUMERIC)
840 rgNum.Add(data.ValueNormalized.GetValueOrDefault(data.Value));
841 else
842 rgCat.Add(data.Value);
843
844 rgTime.Add(data.TimeStamp.Value);
845 }
846 }
847 }
848
849 return new Tuple<float[], float[], DateTime[]>(rgNum.ToArray(), rgCat.ToArray(), rgTime.ToArray());
850 }
851
858 public DateTime[] GetTimeSyncValues(int nIdx, int nCount)
859 {
860 List<DateTime> rgTime = new List<DateTime>();
861
862 for (int i = nIdx; i < nIdx + nCount; i++)
863 {
864 RawValueDataCollection col = m_rgValues[i];
865 if (col.TimeStamp.HasValue)
866 rgTime.Add(col.TimeStamp.Value);
867 }
868
869 return rgTime.ToArray();
870 }
871 }
872
876 public class RawValueDataCollection : IEnumerable<RawValueData>
877 {
878 DateTime? m_dt;
879 List<RawValueData> m_rgData = new List<RawValueData>();
880 Dictionary<int, RawValueData> m_rgDataById = new Dictionary<int, RawValueData>();
881
885 public RawValueDataCollection(DateTime? dt)
886 {
887 m_dt = dt;
888 }
889
893 public DateTime? TimeStamp
894 {
895 get { return m_dt; }
896 }
897
901 public int Count
902 {
903 get { return m_rgData.Count; }
904 }
905
911 public RawValueData this[int nIdx]
912 {
913 get { return m_rgData[nIdx]; }
914 }
915
921 public bool Add(RawValueData data)
922 {
923 if (m_rgDataById.ContainsKey(data.StreamID))
924 return false;
925
926 m_rgDataById.Add(data.StreamID, data);
927 m_rgData.Add(data);
928
929 return true;
930 }
931
937 public void SetData(float[] rgData)
938 {
939 if (m_rgData.Count != rgData.Length)
940 throw new Exception("The number of data items must match the number of raw value data items!");
941
942 for (int i = 0; i < rgData.Length; i++)
943 {
944 m_rgData[i].Value = rgData[i];
945 }
946 }
947
954 public void SetData(DateTime dt, float[] rgData)
955 {
956 m_dt = dt;
957 SetData(rgData);
958 }
959
964 public byte[] ToBytes()
965 {
966 using (MemoryStream ms = new MemoryStream())
967 using (BinaryWriter bw = new BinaryWriter(ms))
968 {
969 bw.Write(m_rgData.Count);
970
971 for (int i = 0; i < m_rgData.Count; i++)
972 {
973 m_rgData[i].Save(bw);
974 }
975
976 ms.Flush();
977 return ms.ToArray();
978 }
979 }
980
987 public static RawValueDataCollection LoadFromBytes(DateTime? dt, byte[] rg)
988 {
990
991 using (MemoryStream ms = new MemoryStream(rg))
992 using (BinaryReader br = new BinaryReader(ms))
993 {
994 int nCount = br.ReadInt32();
995
996 for (int i = 0; i < nCount; i++)
997 {
998 col.m_rgData.Add(RawValueData.Load(dt, br));
999 }
1000 }
1001
1002 return col;
1003 }
1004
1009 public IEnumerator<RawValueData> GetEnumerator()
1010 {
1011 return ((IEnumerable<RawValueData>)m_rgData).GetEnumerator();
1012 }
1013
1018 IEnumerator IEnumerable.GetEnumerator()
1019 {
1020 return ((IEnumerable)m_rgData).GetEnumerator();
1021 }
1022 }
1023
1027 public class RawValueData
1028 {
1029 DateTime? m_dt;
1030 int m_nStrmID = 0;
1031 STREAM_CLASS_TYPE m_classType = STREAM_CLASS_TYPE.STATIC;
1032 STREAM_VALUE_TYPE m_valueType = STREAM_VALUE_TYPE.NUMERIC;
1033 float m_fVal;
1034 float? m_fValNorm;
1035
1044 public RawValueData(STREAM_CLASS_TYPE classType, STREAM_VALUE_TYPE valueType, DateTime? dt, float fVal, float? fValNorm = null)
1045 {
1046 m_dt = dt;
1047 m_classType = classType;
1048 m_valueType = valueType;
1049 m_fVal = fVal;
1050 m_fValNorm = fValNorm;
1051 }
1052
1059 public RawValueData(STREAM_CLASS_TYPE classType, STREAM_VALUE_TYPE valueType, int nStrmId)
1060 {
1061 m_nStrmID = nStrmId;
1062 m_classType = classType;
1063 m_valueType = valueType;
1064 }
1065
1066
1071 public void Save(BinaryWriter bw)
1072 {
1073 bw.Write((byte)m_classType);
1074 bw.Write((byte)m_valueType);
1075 bw.Write(m_fVal);
1076
1077 bw.Write(m_fValNorm.HasValue);
1078 if (m_fValNorm.HasValue)
1079 bw.Write(m_fValNorm.Value);
1080 }
1081
1088 public static RawValueData Load(DateTime? dt, BinaryReader br)
1089 {
1090 STREAM_CLASS_TYPE classType = (STREAM_CLASS_TYPE)br.ReadByte();
1091 STREAM_VALUE_TYPE valueType = (STREAM_VALUE_TYPE)br.ReadByte();
1092 float fVal = br.ReadSingle();
1093
1094 float? fValNorm = null;
1095 if (br.ReadBoolean())
1096 fValNorm = br.ReadSingle();
1097
1098 return new RawValueData(classType, valueType, dt, fVal, fValNorm);
1099 }
1100
1104 public STREAM_CLASS_TYPE ClassType
1105 {
1106 get { return m_classType; }
1107 }
1108
1112 public STREAM_VALUE_TYPE ValueType
1113 {
1114 get { return m_valueType; }
1115 }
1116
1120 public int StreamID
1121 {
1122 get { return m_nStrmID; }
1123 }
1124
1128 public DateTime? TimeStamp
1129 {
1130 get { return m_dt; }
1131 }
1132
1136 public float Value
1137 {
1138 get { return m_fVal; }
1139 set { m_fVal = value; }
1140 }
1141
1145 public float? ValueNormalized
1146 {
1147 get { return m_fValNorm; }
1148 set { m_fValNorm = value; }
1149 }
1150 }
1151}
The CancelEvent provides an extension to the manual cancel event that allows for overriding the manua...
Definition: CancelEvent.cs:17
bool WaitOne(int nMs=int.MaxValue)
Waits for the signal state to occur.
Definition: CancelEvent.cs:290
The ConnectInfo class specifies the server, database and username/password used to connect to a datab...
Definition: ConnectInfo.cs:14
The Log class provides general output in text form.
Definition: Log.cs:13
void WriteLine(string str, bool bOverrideEnabled=false, bool bHeader=false, bool bError=false, bool bDisable=false)
Write a line of output.
Definition: Log.cs:80
The value stream descriptor describes a single value stream within a value item.
The DNNEntities class defines the entities used to connecto the database via Entity Frameworks.
The Database class manages the actual connection to the physical database using Entity Framworks from...
Definition: Database.cs:23
Dataset GetDataset(int nID, ConnectInfo ci=null)
Returns the Dataset entity for a dataset ID.
Definition: Database.cs:4648
FORCE_LOAD
Defines the force load type.
Definition: Database.cs:57
Source m_src
Specifies the default data source.
Definition: Database.cs:27
void DeleteSourceData()
Deletes the data source data for the open data source.
Definition: Database.cs:3896
Source GetSource(string strName, ConnectInfo ci=null)
Returns the Source entity given a data source name.
Definition: Database.cs:4051
The EntitiesConnection class defines how to connect to the database via Entity Frameworks.
static DNNEntities CreateEntities(ConnectInfo ci=null)
Returns the DNNEntities to use.
The DNNEntities class defines the entities used to connecto the database via Entity Frameworks.
The DatabaseTemporal is used to manage all temporal specific database objects.
override void Open(int nSrcId, FORCE_LOAD nForceLoad=FORCE_LOAD.NONE, ConnectInfo ci=null)
Opens a data source.
override bool DeleteSourceData(int nSrcId=0)
Delete the data source data (images, means, results and parameters) from the database.
string GetValueItemNamByIndex(int nIdx)
Returns the value item name given the value item index if found, or 0.
static bool DeleteRawValuesEx(int nSrcId, Log log, CancelEvent evtCancel)
Delete all RawValues in a data source.
RawValueSet GetValues(int nSrcID, int nItemID)
Load the static value stream categorical values for a given source and item.
void ClearRawValues()
Clear the raw values.
static void DeleteDatasetTemporalTables(int nSrcIDTrain, int nSrcIDTest, Log log, CancelEvent evtCancel)
Delete a dataset temporal tables..
override void DeleteDataset(string strDsName, bool bDeleteRelatedProjects, Log log, CancelEvent evtCancel)
Delete a dataset.
void SaveRawValues()
Save the raw values.
int AddValueStream(int nSrcID, string strName, int nOrdering, STREAM_CLASS_TYPE classType, STREAM_VALUE_TYPE valType, DateTime? dtStart=null, DateTime? dtEnd=null, int? nSecPerStep=null, int nTotalSteps=1)
Add a new value stream to the source ID.
int AddValueItem(int nSrcId, int nItemIdx, string strName, DateTime? dtStart=null, DateTime? dtEnd=null, int? nSteps=null)
Add a new value item to the database.
List< ValueStream > GetAllValueStreams(int nSrcID)
Returns a list of all value streams associated with a SourceID.
void PutRawValue(int nSrcID, int nItemID, DateTime dt, RawValueDataCollection data, bool bActive)
Add raw values for a data stream.
override bool DeleteSource(int nSrcId=0)
Delete a data source from the database.
List< ValueItem > GetAllValueItems(int nSrcID)
Returns a list of all value items associated with a SourceID.
List< int > GetAllItemIndices(int nSrcID)
Returns a list of all value item Indices associated with a SourceID.
List< int > GetAllItemIDs(int nSrcID)
Returns a list of all value item IDs associated with a SourceID.
int GetValueItemIndex(string strName)
Returns the value item ID given the value item name if found, or 0.
void DeleteRawValues(int nSrcId=0)
Delete all RawValues in a data source.
string GetValueItemName(int nID)
Returns the value item ID given the value item name if found, or 0.
override void Close()
Close the current data source.
void EnableBulk(bool bEnable)
Enable bulk inserts.
void DeleteValueItems(int nSrcId=0)
Delete all ValueItems in a data source.
int GetValueItemID(string strName)
Returns the value item ID given the value item name if found, or 0.
void PutRawValue(int nSrcID, int nItemID, RawValueDataCollection data)
Add static raw values for a data stream.
The EntitiesConnection class defines how to connect to the database via Entity Frameworks.
static new DNNEntitiesTemporal CreateEntities(ConnectInfo ci=null)
Returns the DNNEntitiesTemporal to use.
The RawValueDataCollection class is used to hold a collection of RawValueData items.
IEnumerator< RawValueData > GetEnumerator()
Returns an enumerator for the raw value data items.
void SetData(float[] rgData)
Set the data values in the collection with the data values provided.
void SetData(DateTime dt, float[] rgData)
Set the data values in the collection with the data values provided.
DateTime? TimeStamp
Returns the time stamp of the raw value data items.
static RawValueDataCollection LoadFromBytes(DateTime? dt, byte[] rg)
Loads a raw value data collection from a byte array.
int Count
Returns the number of raw value items.
bool Add(RawValueData data)
Manually add a new raw value data item.
byte[] ToBytes()
Converts the raw value data items to a byte aray.
RawValueDataCollection(DateTime? dt)
The constructor.
The RawValueData class contains a single raw value item.
DateTime? TimeStamp
Returns the time of the data point or null if static data.
RawValueData(STREAM_CLASS_TYPE classType, STREAM_VALUE_TYPE valueType, DateTime? dt, float fVal, float? fValNorm=null)
The constructor.
int StreamID
Returns the stream ID.
float Value
Returns the raw value item data.
static RawValueData Load(DateTime? dt, BinaryReader br)
Load a raw value item from a binary reader.
STREAM_VALUE_TYPE ValueType
Returns the raw value item value type.
RawValueData(STREAM_CLASS_TYPE classType, STREAM_VALUE_TYPE valueType, int nStrmId)
The constructor.
float? ValueNormalized
Returns the raw value item normalized data if it exists.
void Save(BinaryWriter bw)
Save the raw value item to a binary writer.
STREAM_CLASS_TYPE ClassType
Returns the raw value item class.
The RawValueData class is used to hold the data values for an ItemID.
RawValueSet(int nSrcID, int nItemID)
The constructor.
static RawValueSet FromData(int nSrcID, int nItemID, List< RawValue > rg)
Creates a RawValueSet from a list of RawValues.
void Add(RawValue val)
Add a raw value to the set.
Tuple< float[], float[]> GetStaticValues()
Return the static values.
Tuple< float[], float[], DateTime[]> GetObservedValues(int nIdx, int nCount)
Returns the observed values.
DateTime StartTime
Returns the start time of the data values.
Tuple< float[], DateTime[]> GetObservedNumValues(int nIdx, int nCount, int nValIdx)
Returns the observed values.
Tuple< float[], float[], DateTime[]> GetKnownValues(int nIdx, int nCount)
Returns the observed values.
int ColCount
Returns the row count of observed and known items.
DateTime[] GetTimeSyncValues(int nIdx, int nCount)
Returns the time sync values.
DateTime EndTime
Returns the end time of the data values.
int SourceID
Returns the source ID.
int ItemID
Returns the item ID.
The descriptors namespace contains all descriptor used to describe various items stored within the da...
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
The MyCaffe.db.image namespace contains all image database related classes.
Definition: Database.cs:18
The MyCaffe.db.temporal namespace contains all classes used to create the MyCaffeTemporalDatabase in-...
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...
Definition: Annotation.cs:12