1using Microsoft.VisualBasic.Devices;
7using System.Collections.Generic;
12using System.Threading.Tasks;
22 Exception m_loadException =
null;
28 ManualResetEvent m_evtCancel =
new ManualResetEvent(
false);
29 AutoResetEvent m_evtDone =
new AutoResetEvent(
false);
30 List<ItemSet> m_rgItems =
new List<ItemSet>();
32 double m_dfReplacementPct;
33 int m_nRefreshUpdateMs;
39 Thread m_loadThread =
null;
42 bool m_bNormalizeData =
false;
43 double m_dfLoadPct = 0;
44 object m_objSync =
new object();
60 public TemporalSet(
Log log,
DatabaseTemporal db,
SourceDescriptor src,
DB_LOAD_METHOD loadMethod,
int nLoadLimit,
double dfReplacementPct,
int nRefreshUpdateMs,
CryptoRandom random,
int nHistoricSteps,
int nFutureSteps,
int nChunks)
66 m_loadMethod = loadMethod;
67 m_nLoadLimit = nLoadLimit;
68 m_nHistoricSteps = nHistoricSteps;
69 m_nFutureSteps = nFutureSteps;
70 m_nTotalSteps = m_nHistoricSteps + m_nFutureSteps;
72 m_dfReplacementPct = dfReplacementPct;
73 m_nRefreshUpdateMs = nRefreshUpdateMs;
78 if (m_nLoadLimit > 0 && m_nLoadLimit < 1000)
81 if (m_dfReplacementPct > 0.9)
82 m_dfReplacementPct = 0.9;
84 if (m_nRefreshUpdateMs < 250)
85 m_nRefreshUpdateMs = 250;
101 if (m_loadThread !=
null)
117 foreach (
ItemSet item
in m_rgItems)
129 public bool Initialize(
bool bNormalizedData, EventWaitHandle evtCancel)
131 m_bNormalizeData = bNormalizedData;
133 if (m_loadThread ==
null)
135 m_loadThread =
new Thread(
new ThreadStart(loadThread));
136 m_loadThread.Start();
145 private void removeItem(List<ItemSet> rgItems)
149 if (m_rgItems ==
null)
152 if (m_rgItems.Count == 0)
161 private void loadThread()
163 DatabaseLoader loader =
new DatabaseLoader();
165 double dfMemMin = 1000.0;
177 if (m_dtEnd.HasValue && m_dtStart.HasValue && m_dtEnd <= m_dtStart)
178 throw new Exception(
"The end date must be greater than the start date.");
181 DateTime? dt = m_dtStart;
184 Stopwatch sw =
new Stopwatch();
189 ComputerInfo info =
new ComputerInfo();
190 Stopwatch swReplacement =
new Stopwatch();
192 swReplacement.Start();
194 while (!m_evtCancel.WaitOne(0))
199 while (m_rgItems.Count < rgItems.Count && (m_nLoadLimit <= 0 || m_rgItems.Count < m_nLoadLimit))
201 ItemSet item =
new ItemSet(m_random, m_db, rgItems[nIdx], rgStrm);
202 item.Load(out bEOD1);
209 m_dfLoadPct = (double)m_rgItems.Count / rgItems.Count;
213 if (sw.Elapsed.TotalMilliseconds > 1000)
217 m_log.
WriteLine(
"Loading '" + m_src.
Name +
"' data for item " + item.Item.Name +
" (" + m_dfLoadPct.ToString(
"P") +
")...",
true);
220 if (m_evtCancel.WaitOne(0))
225 if (nIdx >= rgItems.Count)
228 if (nIdx % m_nChunks == 0)
232 if (m_nLoadLimit > 0)
234 int nLoadLimit = m_nLoadLimit;
236 if ((
int)swReplacement.Elapsed.TotalMilliseconds > m_nRefreshUpdateMs)
237 nLoadLimit = (
int)(m_nLoadLimit * (1 - m_dfReplacementPct));
239 if (nLoadLimit > 1000)
241 while (m_rgItems.Count > nLoadLimit)
243 removeItem(m_rgItems);
246 GC.Collect(2, GCCollectionMode.Forced);
250 ulong nMem = info.AvailablePhysicalMemory;
251 double dfMb = (double)nMem / (1024.0 * 1024.0);
254 while (dfMb < dfMemMin && nRetryCount < 10 && m_rgItems.Count > 1000)
256 m_log.
WriteLine(
"Waiting for memory to free up...",
true);
258 for (
int i=0; i<1000 && m_rgItems.Count > 1000; i++)
260 removeItem(m_rgItems);
263 GC.Collect(2, GCCollectionMode.Forced);
264 nMem = info.AvailablePhysicalMemory;
265 dfMb = (double)nMem / (1024.0 * 1024.0);
274 if (m_rgItems.Count == rgItems.Count && m_nLoadLimit <= 0)
278 catch (Exception excpt)
280 m_loadException = excpt;
297 WaitHandle[] rgWait =
new WaitHandle[] { m_evtCancel, evtCancel, m_evtDone };
298 int nWait = WaitHandle.WaitAny(rgWait, nWaitMs);
311 get {
return m_dfLoadPct; }
319 get {
return m_loadMethod; }
346 m_nItemIdx = m_random.
Next(m_rgItems.Count);
350 if (m_nItemIdx >= m_rgItems.Count)
354 if (nItemIdx.HasValue)
355 m_nItemIdx = nItemIdx.Value;
356 nItemIdx = m_nItemIdx;
358 if (m_nItemIdx >= m_rgItems.Count)
361 data = m_rgItems[m_nItemIdx].GetData(nQueryIdx, ref nValueIdx, valueSelectionMethod, m_nHistoricSteps, m_nFutureSteps, nValueStepOffset, bEnableDebug, strDebugPath);
364 while (data ==
null && nRetryCount < 40)
368 m_nItemIdx = m_random.
Next(m_rgItems.Count);
374 if (m_nItemIdx >= m_rgItems.Count)
378 nItemIdx = m_nItemIdx;
379 data = m_rgItems[m_nItemIdx].GetData(nQueryIdx, ref nValueIdx, valueSelectionMethod, m_nHistoricSteps, m_nFutureSteps, nValueStepOffset, bEnableDebug, strDebugPath);
395 return m_rgItems.Sum(p => p.GetCount(m_nHistoricSteps + m_nFutureSteps));
408 return m_rgItems.Count;
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 Log class provides general output in text form.
void WriteLine(string str, bool bOverrideEnabled=false, bool bHeader=false, bool bError=false, bool bDisable=false)
Write a line of output.
double Progress
Get/set the progress associated with the Log.
The SimpleTemporalDatumCollection manages a collection of SimpleTemporalDatum objects.
int ID
Get/set the database ID of the item.
string Name
Get/set the name of the item.
The ordered value stream descriptor set is used to order the value stream descriptors by class and va...
The SourceDescriptor class contains all information describing a data source.
The TemporalDescriptor is used to describe a temporal aspects of the data source.
DateTime? EndDate
Return the end date.
OrderedValueStreamDescriptorSet OrderedValueStreamDescriptors
Retunrs the ordered set of stream descriptors.
DateTime? StartDate
Return the start date.
The DatabaseTemporal is used to manage all temporal specific database objects.
List< ValueItem > GetAllValueItems(int nSrcID)
Returns a list of all value items associated with a SourceID.
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.
The TemporalSet manages a set of temporal data for a given data source.
SimpleTemporalDatumCollection GetData(int nQueryIdx, ref int? nItemIdx, ref int? nValueIdx, DB_LABEL_SELECTION_METHOD itemSelectionMethod, DB_ITEM_SELECTION_METHOD valueSelectionMethod, int nValueStepOffset=1, bool bEnableDebug=false, string strDebugPath=null)
Get a data set consisting of the static, historical, and future data for a selected item where the st...
bool WaitForLoadingToComplete(EventWaitHandle evtCancel, int nWaitMs=int.MaxValue)
Wait for the image set to complete loading.
void Dispose()
Release all resources used.
double LoadPercent
Returns the percentage of the load completed.
void Reset()
Reset all indexes to their starting locations.
DB_LOAD_METHOD LoadMethod
Get the image load method used on initialization.
void CleanUp()
Release all resources used by the temporal set and shut down all internal threads.
TemporalSet(Log log, DatabaseTemporal db, SourceDescriptor src, DB_LOAD_METHOD loadMethod, int nLoadLimit, double dfReplacementPct, int nRefreshUpdateMs, CryptoRandom random, int nHistoricSteps, int nFutureSteps, int nChunks)
The constructor.
int Count
Return the total number of queries available in the temporal set.
bool Initialize(bool bNormalizedData, EventWaitHandle evtCancel)
Load the data based on the data load method.
int GetCount()
Return the total number of queries available in the temporal set.
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.
DB_LOAD_METHOD
Defines how to laod the items into the in-memory database.
DB_LABEL_SELECTION_METHOD
Defines the label selection method.
The MyCaffe.db.image namespace contains all image database related classes.
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-...