MyCaffe  1.12.2.41
Deep learning software for Windows C# programmers.
MgrQueryGeneral.cs
1using MyCaffe.basecode;
2using System;
3using System.Collections.Generic;
4using System.Diagnostics;
5using System.IO;
6using System.Linq;
7using System.Reflection;
8using System.Text;
9using System.Threading;
10using System.Threading.Tasks;
11
12namespace MyCaffe.db.stream
13{
18 public class MgrQueryGeneral : IXQuery
19 {
20 CustomQueryCollection m_colCustomQuery = new CustomQueryCollection();
21 PropertySet m_schema;
22 CancelEvent m_evtCancel = new CancelEvent();
23 IXCustomQuery m_iquery;
24
41 public MgrQueryGeneral(string strSchema, List<IXCustomQuery> rgCustomQueries)
42 {
43 m_colCustomQuery.Load();
44 m_schema = new PropertySet(strSchema);
45
46 m_colCustomQuery.Add(new StandardQueryTextFile());
47 m_colCustomQuery.Add(new StandardQueryWAVFile());
48
49 foreach (IXCustomQuery icustomquery in rgCustomQueries)
50 {
51 m_colCustomQuery.Add(icustomquery);
52 }
53
54 int nConnections = m_schema.GetPropertyAsInt("ConnectionCount");
55 if (nConnections != 1)
56 throw new Exception("Currently, the general query type only supports 1 connection.");
57
58 string strConTag = "Connection0";
59 string strCustomQuery = m_schema.GetProperty(strConTag + "_CustomQueryName");
60 string strCustomQueryParam = m_schema.GetProperty(strConTag + "_CustomQueryParam");
61
62 IXCustomQuery iqry = m_colCustomQuery.Find(strCustomQuery);
63 if (iqry == null)
64 throw new Exception("Could not find the custom query '" + strCustomQuery + "'!");
65
66 if (iqry.QueryType != CUSTOM_QUERY_TYPE.BYTE && iqry.QueryType != CUSTOM_QUERY_TYPE.REAL_FLOAT && iqry.QueryType != CUSTOM_QUERY_TYPE.REAL_DOUBLE)
67 throw new Exception("The custom query '" + iqry.Name + "' must support the 'CUSTOM_QUERY_TYPE.BYTE' or 'CUSTOM_QUERY_TYPE.REAL_FLOAT' or 'CUSTOM_QUERY_TYPE.REAL_DOUBLE' query type!");
68
69 string strParam = ParamPacker.UnPack(strCustomQueryParam);
70 m_iquery = iqry.Clone(strParam);
71 m_iquery.Open();
72 }
73
85 public void AddDirectQuery(IXCustomQuery iqry)
86 {
87 m_colCustomQuery.Add(iqry);
88 }
89
94 public void Reset(int nStartOffset)
95 {
96 m_iquery.Reset();
97 }
98
102 public void Shutdown()
103 {
104 m_evtCancel.Set();
105 m_iquery.Close();
106 }
107
115 public List<int> GetQuerySize()
116 {
117 return m_iquery.GetQuerySize();
118 }
119
125 public SimpleDatum Query(int nWait)
126 {
127 if (m_iquery.QueryType == CUSTOM_QUERY_TYPE.REAL_DOUBLE)
128 {
129 List<double[]> rgData = m_iquery.QueryRealD();
130
131 if (rgData == null || rgData.Count == 0 || rgData[0].Length == 0)
132 return null;
133
134 double[] rgFullData = new double[rgData[0].Length * rgData.Count];
135 int nOffset = 0;
136
137 for (int i = 0; i < rgData.Count; i++)
138 {
139 Array.Copy(rgData[i], 0, rgFullData, nOffset, rgData[i].Length);
140 nOffset += rgData[i].Length;
141 }
142
143 return new SimpleDatum(true, rgData.Count, 1, rgData[0].Length, -1, DateTime.MinValue, rgFullData.ToList(), 0, false, -1);
144 }
145 else if (m_iquery.QueryType == CUSTOM_QUERY_TYPE.REAL_FLOAT)
146 {
147 List<float[]> rgData = m_iquery.QueryRealF();
148
149 if (rgData == null || rgData.Count == 0 || rgData[0].Length == 0)
150 return null;
151
152 float[] rgFullData = new float[rgData[0].Length * rgData.Count];
153 int nOffset = 0;
154
155 for (int i = 0; i < rgData.Count; i++)
156 {
157 Array.Copy(rgData[i], 0, rgFullData, nOffset, rgData[i].Length);
158 nOffset += rgData[i].Length;
159 }
160
161 return new SimpleDatum(true, rgData.Count, 1, rgData[0].Length, -1, DateTime.MinValue, rgFullData.ToList(), 0, false, -1);
162 }
163 else
164 {
165 byte[] rgData = m_iquery.QueryBytes();
166
167 if (rgData == null || rgData.Length == 0)
168 return null;
169
170 return new SimpleDatum(false, rgData.Length, 1, 1, -1, DateTime.MinValue, rgData.ToList(), 0, false, -1);
171 }
172 }
173
180 public byte[] ConvertOutput(float[] rg, out string type)
181 {
182 return m_iquery.ConvertOutput(rg, out type);
183 }
184
189 public Dictionary<string, float> QueryInfo()
190 {
191 return m_iquery.QueryInfo();
192 }
193 }
194}
The CancelEvent provides an extension to the manual cancel event that allows for overriding the manua...
Definition: CancelEvent.cs:17
void Set()
Sets the event to the signaled state.
Definition: CancelEvent.cs:270
Specifies a key-value pair of properties.
Definition: PropertySet.cs:16
string GetProperty(string strName, bool bThrowExceptions=true)
Returns a property as a string value.
Definition: PropertySet.cs:146
int GetPropertyAsInt(string strName, int nDefault=0)
Returns a property as an integer value.
Definition: PropertySet.cs:287
The SimpleDatum class holds a data input within host memory.
Definition: SimpleDatum.cs:161
The CustomQueryCollection manages the external Custom Queries placed in the
void Load()
Loads all custom query DLL's (if found).
IXCustomQuery Find(string strName)
Locates a custom query by name and returns it.
void Add(IXCustomQuery iqry)
Directly adds a custom query to the list.
The MgrQueryTime class manages the collection of data queries, and the internal data queue that conta...
byte[] ConvertOutput(float[] rg, out string type)
Converts the output values into the native type used by the CustomQuery.
SimpleDatum Query(int nWait)
Query the next data in the streaming database.
void AddDirectQuery(IXCustomQuery iqry)
Add a custom query directly to the streaming database.
Dictionary< string, float > QueryInfo()
The Query information returns information about the data queried such as header information.
void Shutdown()
Shutdown the data queries and consolidation thread.
void Reset(int nStartOffset)
Reset the query to the start date used in Initialize, optionally with an offset from the start.
MgrQueryGeneral(string strSchema, List< IXCustomQuery > rgCustomQueries)
The constructor.
List< int > GetQuerySize()
Returns the query size of the data in the form: [0] = channels [1] = height [2] = width.
The ParamPacker is use to pack and unpack parameters sent to each custom query.
Definition: Interfaces.cs:246
static string UnPack(string str)
Unpack the custom query parameters.
Definition: Interfaces.cs:263
The StandardQueryTextFile provides queries that read text (*.txt) files residing in a given directory...
The StandardQueryWAVFile provides queries that read sound frequencies from (*.WAV) files residing in ...
The custom query interface defines the functions implemented by each Custom Query object used to spec...
Definition: Interfaces.cs:168
CUSTOM_QUERY_TYPE QueryType
Returns the custom query type supported by the custom query.
Definition: Interfaces.cs:172
void Open()
Open a connection to the underlying database using the connection string specified.
IXCustomQuery Clone(string strParam)
Return a new instance of the custom query.
string Name
Returns the name of the Custom Query.
Definition: Interfaces.cs:176
List< float[]> QueryRealF()
Query the data as a set one or more float arrays.
byte[] QueryBytes()
Query the raw bytes.
List< double[]> QueryRealD()
Query the data as a set one or more double arrays.
Dictionary< string, float > QueryInfo()
The Query information returns information about the data queried such as header information.
byte[] ConvertOutput(float[] rg, out string strType)
Converts the output values into the native type used by the CustomQuery.
void Reset()
Reset the custom query.
List< int > GetQuerySize()
Returns the query count for the current query.
void Close()
Close a currently open connection.
The MyCaffe.basecode contains all generic types used throughout MyCaffe.
Definition: Annotation.cs:12
The MyCaffe.db.stream namespace contains all data streaming related classes.
CUSTOM_QUERY_TYPE
Defines the custom query type to use.
Definition: Interfaces.cs:134
The MyCaffe namespace contains the main body of MyCaffe code that closesly tracks the C++ Caffe open-...
Definition: Annotation.cs:12