5using System.Collections.Generic;
11using System.Threading.Tasks;
25 ValueItem m_item =
null;
28 List<int> m_rgRowCount =
new List<int>(8);
29 int m_nTargetStreamNumIdx = 0;
70 get {
return m_item; }
78 get {
return m_rgStrm; }
87 public Tuple<DateTime, DateTime>
Load(out
bool bEOD)
89 int nSrcID = m_item.SourceID.Value;
90 int nItemID = m_item.ID;
106#warning("TBD: LoadLimit")
126 int nTotalSteps = nHistSteps + nFutSteps;
127 int nColCount = m_nColCount;
129 if (m_item.Steps.HasValue)
130 nColCount = m_item.Steps.Value;
132 if (nColCount < nTotalSteps)
137 m_nValIdx = m_random.
Next(nColCount - nTotalSteps);
141 if (m_nValIdx >= nColCount - nTotalSteps)
144 nValueIdx = m_nValIdx;
152 if (nValueIdx.HasValue && nValueIdx >= 0)
153 m_nValIdx = nValueIdx.Value;
155 nValueIdx = m_nValIdx;
159 getStaticData(ref sdStatNum, ref sdStatCat);
163 if (!getHistoricalData(m_nValIdx, nHistSteps, out sdHistNum, out sdHistCat))
168 if (!getFutureData(m_nValIdx + nHistSteps, nFutSteps, out sdFutNum, out sdFutCat))
173 if (!getTargetData(m_nValIdx, nHistSteps, nFutSteps, m_nTargetStreamNumIdx, out sdTarget, out sdTargetHist))
178 debug(
"trg", nQueryIdx, strDebugPath, m_nValIdx, nHistSteps, nFutSteps, sdTargetHist, sdTarget);
179 debugStatic(
"stat_cat", nQueryIdx, strDebugPath, m_nValIdx, sdStatCat);
180 debugStatic(
"stat_num", nQueryIdx, strDebugPath, m_nValIdx, sdStatNum);
181 debug(
"hist_cat", nQueryIdx, strDebugPath, m_nValIdx, sdHistCat, STREAM_CLASS_TYPE.OBSERVED);
182 debug(
"hist_num", nQueryIdx, strDebugPath, m_nValIdx, sdHistNum, STREAM_CLASS_TYPE.OBSERVED);
183 debug(
"fut_cat", nQueryIdx, strDebugPath, m_nValIdx, sdFutCat, STREAM_CLASS_TYPE.KNOWN);
184 debug(
"fut_num", nQueryIdx, strDebugPath, m_nValIdx, sdFutNum, STREAM_CLASS_TYPE.KNOWN);
188 rgData.
Add(m_sdStaticNum);
189 rgData.
Add(m_sdStaticCat);
190 rgData.
Add(sdHistNum);
191 rgData.
Add(sdHistCat);
192 rgData.
Add(sdFutNum);
193 rgData.
Add(sdFutCat);
194 rgData.
Add(sdTarget);
195 rgData.
Add(sdTargetHist);
201 private void debugStatic(
string strTag,
int nQueryIdx,
string strDebugPath,
int nIdx,
SimpleDatum sd)
203 if (
string.IsNullOrEmpty(strDebugPath))
204 throw new Exception(
"You must specify a debug path, when 'EnableDebug' = true.");
209 string strName = strTag +
": QueryIdx = " + nQueryIdx.ToString() +
", Idx = " + nIdx.ToString() +
", Length = " + sd.
ItemCount.ToString() +
" Time: None - Static";
210 PlotCollection plots =
new PlotCollection(strName);
212 float[] rgf = sd.GetData<
float>();
213 for (
int i = 0; i < rgf.Length; i++)
217 Plot plot =
new Plot(i, fVal);
221 if (!Directory.Exists(strDebugPath))
222 Directory.CreateDirectory(strDebugPath);
224 strDebugPath = strDebugPath.TrimEnd(
'\\') +
"\\";
225 string strFile = strDebugPath + nQueryIdx.ToString() +
"." + nIdx.ToString() +
"." + strTag +
".png";
226 Image img = SimpleGraphingControl.QuickRender(plots, 1000, 600,
true, ConfigurationAxis.VALUE_RESOLUTION.MINUTE,
null,
true,
null,
true);
233 private void debug(
string strTag,
int nQueryIdx,
string strDebugPath,
int nIdx,
SimpleDatum sd, STREAM_CLASS_TYPE classType)
235 if (
string.IsNullOrEmpty(strDebugPath))
236 throw new Exception(
"You must specify a debug path, when 'EnableDebug' = true.");
241 DateTime[] rgSync = getTimeSync(nIdx, sd.
Height);
242 if (rgSync.Length != sd.
Height)
243 throw new Exception(
"The sync and data lengths do not match!");
245 string strName = strTag +
": QueryIdx = " + nQueryIdx.ToString() +
", Idx = " + nIdx.ToString() +
", Length = " + sd.
ItemCount.ToString() +
" Time: " + rgSync[0].ToString() +
" - " + rgSync[rgSync.Length - 1].ToString();
246 PlotCollectionSet
set =
new PlotCollectionSet();
247 float[] rgf = sd.GetData<
float>();
249 for (
int i = 0; i < sd.
Width; i++)
251 PlotCollection plots =
new PlotCollection(strName +
" strm #" + i.ToString());
253 for (
int j = 0; j < sd.
Height; j++)
255 int nDataIdx = j * sd.
Width + i;
256 float fVal = rgf[nDataIdx];
258 Plot plot =
new Plot(rgSync[j].ToFileTime(), fVal);
259 plot.Tag = rgSync[i];
266 if (!Directory.Exists(strDebugPath))
267 Directory.CreateDirectory(strDebugPath);
269 DateTime dt = rgSync[0];
271 if (classType == STREAM_CLASS_TYPE.OBSERVED)
272 dt = rgSync[rgSync.Length - 1];
274 strDebugPath = strDebugPath.TrimEnd(
'\\') +
"\\";
275 string strFile = strDebugPath + nQueryIdx.ToString() +
"." + dt.Year.ToString() +
"." + dt.Month.ToString() +
"." + dt.Day.ToString() +
"_" + dt.Hour.ToString() +
"." + dt.Minute.ToString() +
"." + dt.Second.ToString() +
"." + nIdx.ToString() +
"." + strTag +
".png";
276 Image img = SimpleGraphingControl.QuickRender(
set, 1000, 600,
true, ConfigurationAxis.VALUE_RESOLUTION.MINUTE,
null,
true,
null,
true);
282 private void debug(
string strTag,
int nQueryIdx,
string strDebugPath,
int nIdx,
int nHistSteps,
int nFutSteps,
SimpleDatum sd1,
SimpleDatum sd2)
284 if (
string.IsNullOrEmpty(strDebugPath))
285 throw new Exception(
"You must specify a debug path, when 'EnableDebug' = true.");
287 DateTime[] rgSync = getTimeSync(nIdx, nHistSteps + nFutSteps);
288 SimpleDatum sd = getTargetData(nIdx, nHistSteps + nFutSteps, m_nTargetStreamNumIdx);
291 throw new Exception(
"The sync and data lengths do not match!");
294 throw new Exception(
"The target data length does not match the sum of the historical and future data lengths!");
296 string strName =
"TargetData: QueryIdx = " + nQueryIdx.ToString() +
", Idx = " + nIdx.ToString() +
", Hist = " + nHistSteps.ToString() +
", Fut = " + nFutSteps.ToString() +
"\nTime: " + rgSync[0].ToString() +
" - " + rgSync[rgSync.Length-1].ToString();
297 PlotCollection plots =
new PlotCollection(strName);
299 float[] rgf1 = sd1.GetData<
float>();
300 float[] rgf2 = sd2.GetData<
float>();
301 float[] rgfE = sd.GetData<
float>();
303 for (
int i=0; i<rgf1.Length; i++)
305 float fVal = rgf1[i];
308 throw new Exception(
"The data values do not match!");
310 Plot plot =
new Plot(rgSync[i].ToFileTime(), fVal);
311 plot.Tag = rgSync[i];
315 for (
int i = 0; i < rgf2.Length; i++)
317 float fVal = rgf2[i];
318 int nDataIdx = i + rgf1.Length;
320 if (fVal != rgfE[nDataIdx])
321 throw new Exception(
"The data values do not match!");
323 Plot plot =
new Plot(rgSync[nDataIdx].ToFileTime(), fVal);
324 plot.Tag = rgSync[nDataIdx];
328 if (!Directory.Exists(strDebugPath))
329 Directory.CreateDirectory(strDebugPath);
331 DateTime dt = rgSync[rgf1.Length];
333 strDebugPath = strDebugPath.TrimEnd(
'\\') +
"\\";
334 string strFile = strDebugPath + nQueryIdx.ToString() +
"." + dt.Year.ToString() +
"." + dt.Month.ToString() +
"." + dt.Day.ToString() +
"_" + dt.Hour.ToString() +
"." + dt.Minute.ToString() +
"." + dt.Second.ToString() +
"." + nIdx.ToString() +
"." + strTag +
".png";
336 int nActualWid = plots.Count * 5 + 55;
338 if (nImgWid > nActualWid)
339 nImgWid = nActualWid;
341 Image img = SimpleGraphingControl.QuickRender(plots, nImgWid, 600,
true, ConfigurationAxis.VALUE_RESOLUTION.MINUTE,
null,
true,
null,
true);
343 using (Graphics g = Graphics.FromImage(img))
345 Brush br =
new SolidBrush(Color.FromArgb(64, Color.Blue));
346 int nWid = 5 * nFutSteps;
347 int nLeft = img.Width - (55 + nWid);
348 Rectangle rc =
new Rectangle(nLeft, 0, nWid, img.Height);
349 g.FillRectangle(br, rc);
350 g.DrawLine(Pens.Lime, nLeft, 0, nLeft, img.Height);
360 if (m_sdStaticNum !=
null && m_sdStaticCat !=
null)
362 sdNum = m_sdStaticNum;
363 sdCat = m_sdStaticCat;
369 List<ValueStreamDescriptor> rgDesc = m_rgStrm.
GetStreamDescriptors(STREAM_CLASS_TYPE.STATIC, STREAM_VALUE_TYPE.NUMERIC);
372 if (rgDesc !=
null && rgDesc.Count > 0)
374 int nH = rgDesc.Count;
375 int nW = rgDesc.Max(p => p.Steps);
376 sdNum =
new SimpleDatum(nC, nW, nH, data.Item1, 0, data.Item1.Length);
377 sdNum.TagName =
"StaticNumeric";
384 rgDesc = m_rgStrm.
GetStreamDescriptors(STREAM_CLASS_TYPE.STATIC, STREAM_VALUE_TYPE.CATEGORICAL);
387 if (rgDesc !=
null && rgDesc.Count > 0)
389 int nH = rgDesc.Count;
390 int nW = rgDesc.Max(p => p.Steps);
391 sdCat =
new SimpleDatum(nC, nW, nH, data.Item2, 0, data.Item2.Length);
392 sdCat.TagName =
"StaticCategorical";
399 m_sdStaticNum = sdNum;
400 m_sdStaticCat = sdCat;
408 Tuple<float[], float[], DateTime[]> dataObs = m_data.
GetObservedValues(nIdx, nCount);
412 Tuple<float[], float[], DateTime[]> dataKnown = m_data.
GetKnownValues(nIdx, nCount);
413 if (dataKnown ==
null)
418 List<ValueStreamDescriptor> rgDescO = m_rgStrm.
GetStreamDescriptors(STREAM_CLASS_TYPE.OBSERVED, STREAM_VALUE_TYPE.NUMERIC);
419 int nObsCount = rgDescO ==
null ? 0 : rgDescO.Count;
420 List<ValueStreamDescriptor> rgDescK = m_rgStrm.
GetStreamDescriptors(STREAM_CLASS_TYPE.KNOWN, STREAM_VALUE_TYPE.NUMERIC);
421 int nKnownCount = rgDescK ==
null ? 0 : rgDescK.Count;
422 int nItemCount = nObsCount + nKnownCount;
424 float[] rgfNum =
null;
425 if (dataObs.Item1.Length > 0 && dataKnown.Item1.Length > 0)
427 rgfNum =
new float[dataObs.Item1.Length + dataKnown.Item1.Length];
429 for (
int i=0; i<nCount; i++)
431 for (
int j = 0; j < nObsCount; j++)
433 int nDataIdx = i * nItemCount + j;
434 rgfNum[nDataIdx] = dataObs.Item1[i * nObsCount + j];
437 for (
int j = 0; j < nKnownCount; j++)
439 int nDataIdx = i * nItemCount + nObsCount + j;
440 rgfNum[nDataIdx] = dataKnown.Item1[i * nKnownCount + j];
444 else if (dataObs.Item1.Length > 0)
446 rgfNum = dataObs.Item1;
448 else if (dataKnown.Item1.Length > 0)
450 rgfNum = dataKnown.Item1;
459 sdNum =
new SimpleDatum(nC, nW, nH, rgfNum, 0, rgfNum.Length);
460 sdNum.TagName =
"HistoricalNumeric";
461 sdNum.Tag = dataObs.Item3;
471 List<ValueStreamDescriptor> rgDescO = m_rgStrm.
GetStreamDescriptors(STREAM_CLASS_TYPE.OBSERVED, STREAM_VALUE_TYPE.CATEGORICAL);
472 int nObsCount = rgDescO ==
null ? 0 : rgDescO.Count;
473 List<ValueStreamDescriptor> rgDescK = m_rgStrm.
GetStreamDescriptors(STREAM_CLASS_TYPE.KNOWN, STREAM_VALUE_TYPE.CATEGORICAL);
474 int nKnownCount = rgDescK ==
null ? 0 : rgDescK.Count;
475 int nItemCount = nObsCount + nKnownCount;
477 float[] rgfCat =
null;
478 if (dataObs.Item2.Length > 0 && dataKnown.Item2.Length > 0)
480 rgfCat =
new float[dataObs.Item2.Length + dataKnown.Item2.Length];
482 for (
int i = 0; i < nCount; i++)
484 for (
int j = 0; j < nObsCount; j++)
486 int nDataIdx = i * nItemCount + j;
487 rgfCat[nDataIdx] = dataObs.Item2[i * nObsCount + j];
490 for (
int j = 0; j < nKnownCount; j++)
492 int nDataIdx = i * nItemCount + nObsCount + j;
493 rgfCat[nDataIdx] = dataKnown.Item2[i * nKnownCount + j];
497 else if (dataObs.Item2.Length > 0)
499 rgfCat = dataObs.Item2;
501 else if (dataKnown.Item2.Length > 0)
503 rgfCat = dataKnown.Item2;
512 sdCat =
new SimpleDatum(nC, nW, nH, rgfCat, 0, rgfCat.Length);
513 sdCat.TagName =
"HistoricalCategorical";
514 sdCat.Tag = dataObs.Item3;
530 Tuple<float[], float[], DateTime[]> dataKnown = m_data.
GetKnownValues(nIdx, nCount);
531 if (dataKnown ==
null)
534 float[] rgfNum =
null;
535 if (dataKnown.Item1.Length > 0)
536 rgfNum = dataKnown.Item1;
538 float[] rgfCat =
null;
539 if (dataKnown.Item2.Length > 0)
540 rgfCat = dataKnown.Item2;
544 List<ValueStreamDescriptor> rgDescK = m_rgStrm.
GetStreamDescriptors(STREAM_CLASS_TYPE.KNOWN, STREAM_VALUE_TYPE.NUMERIC);
547 int nW = rgDescK.Count;
549 sdNum =
new SimpleDatum(nC, nW, nH, rgfNum, 0, rgfNum.Length);
550 sdNum.TagName =
"FutureNumeric";
551 sdNum.Tag = dataKnown.Item3;
560 List<ValueStreamDescriptor> rgDescK = m_rgStrm.
GetStreamDescriptors(STREAM_CLASS_TYPE.KNOWN, STREAM_VALUE_TYPE.CATEGORICAL);
563 int nW = rgDescK.Count;
565 sdCat =
new SimpleDatum(nC, nW, nH, rgfCat, 0, rgfCat.Length);
566 sdCat.TagName =
"FutureCategorical";
576 private bool getTargetData(
int nIdx,
int nHistSteps,
int nFutSteps,
int nTargetIdx, out
SimpleDatum sdTarget, out
SimpleDatum sdTargetHist)
581 Tuple<float[], DateTime[]> rgfTgt = m_data.
GetObservedNumValues(nIdx + nHistSteps, nFutSteps, nTargetIdx);
590 int nH = rgfTgt.Item1.Length;
592 sdTarget =
new SimpleDatum(nC, nW, nH, rgfTgt.Item1, 0, rgfTgt.Item1.Length);
593 sdTarget.TagName =
"Target";
594 sdTarget.Tag = rgfTgt.Item2;
596 nH = rgfTgtH.Item1.Length;
597 sdTargetHist =
new SimpleDatum(nC, nW, nH, rgfTgtH.Item1, 0, rgfTgtH.Item1.Length);
598 sdTargetHist.TagName =
"TargetHist";
599 sdTargetHist.Tag = rgfTgtH.Item2;
604 private SimpleDatum getTargetData(
int nIdx,
int nCount,
int nTargetIdx)
610 int nW = rgfTgt.Item1.Length;
617 private DateTime[] getTimeSync(
int nIdx,
int nCount)
629 int nCount = m_nColCount - nSteps;
638 public partial class RawValue
643 public partial class ValueItem
648 public partial class ValueStream
The CryptoRandom is a random number generator that can use either the standard .Net Random objec or t...
int Next(int nMinVal, int nMaxVal, bool bMaxInclusive=true)
Returns a random int within the range
The SimpleDatum class holds a data input within host memory.
string TagName
Returns a user-defined name of the tag associated with the data.
int ItemCount
Returns the number of data items.
int Width
Return the width of the data.
int Height
Return the height of the data.
The SimpleTemporalDatumCollection manages a collection of SimpleTemporalDatum objects.
void Add(SimpleDatum sd)
Add a new datum to the collection (must support the RealDataF data);
The ordered value stream descriptor set is used to order the value stream descriptors by class and va...
List< ValueStreamDescriptor > GetStreamDescriptors(STREAM_CLASS_TYPE classType, STREAM_VALUE_TYPE valueType)
Retrieves the set of stream descriptors with the given class and value type.
The value stream descriptor describes a single value stream within a value item.
The DatabaseTemporal is used to manage all temporal specific database objects.
RawValueSet GetValues(int nSrcID, int nItemID)
Load the static value stream categorical values for a given source and item.
The ItemSet manages the data for a single item (e.g., customer, station, stock symbol,...
void CleanUp()
Release all resources used.
void Reset()
Reset the value index.
SimpleTemporalDatumCollection GetData(int nQueryIdx, ref int? nValueIdx, DB_ITEM_SELECTION_METHOD valueSelectionMethod, int nHistSteps, int nFutSteps, int nValueStepOffset=1, bool bEnableDebug=false, string strDebugPath=null)
Retreives the static, historical and future data at a selected step.
OrderedValueStreamDescriptorSet Streams
Returns the value streams.
ValueItem Item
Returns the value item.
ItemSet(CryptoRandom random, DatabaseTemporal db, ValueItem item, OrderedValueStreamDescriptorSet rgStrm)
The constructor.
void LoadLimit(int nMax)
Clips the data to the load limit by removing older data items.
Tuple< DateTime, DateTime > Load(out bool bEOD)
Loads the data for the item starting at the specified date/time and loading the specified number of s...
int GetCount(int nSteps)
Return the total number of queries available in the item set.
The RawValueData class is used to hold the data values for an ItemID.
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.
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.
DB_ITEM_SELECTION_METHOD
Defines the item (e.g., image or temporal item) selection method.
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-...