Chris@439
|
1 /* -*- c-basic-offset: 4 indent-tabs-mode: nil -*- vi:set ts=8 sts=4 sw=4: */
|
Chris@439
|
2
|
Chris@439
|
3 /*
|
Chris@439
|
4 Sonic Visualiser
|
Chris@439
|
5 An audio file viewer and annotation editor.
|
Chris@439
|
6 Centre for Digital Music, Queen Mary, University of London.
|
Chris@439
|
7 This file copyright 2008 QMUL.
|
Chris@439
|
8
|
Chris@439
|
9 This program is free software; you can redistribute it and/or
|
Chris@439
|
10 modify it under the terms of the GNU General Public License as
|
Chris@439
|
11 published by the Free Software Foundation; either version 2 of the
|
Chris@439
|
12 License, or (at your option) any later version. See the file
|
Chris@439
|
13 COPYING included with this distribution for more information.
|
Chris@439
|
14 */
|
Chris@439
|
15
|
Chris@439
|
16 #include "RDFImporter.h"
|
Chris@439
|
17
|
Chris@439
|
18 #include <map>
|
Chris@439
|
19 #include <vector>
|
Chris@439
|
20
|
Chris@439
|
21 #include <iostream>
|
Chris@439
|
22 #include <cmath>
|
Chris@439
|
23
|
Chris@439
|
24 #include "SimpleSPARQLQuery.h"
|
Chris@439
|
25
|
Chris@439
|
26 #include "base/ProgressReporter.h"
|
Chris@439
|
27 #include "base/RealTime.h"
|
Chris@439
|
28
|
Chris@439
|
29 #include "data/model/SparseOneDimensionalModel.h"
|
Chris@439
|
30 #include "data/model/SparseTimeValueModel.h"
|
Chris@439
|
31 #include "data/model/EditableDenseThreeDimensionalModel.h"
|
Chris@439
|
32
|
Chris@439
|
33 using std::cerr;
|
Chris@439
|
34 using std::endl;
|
Chris@439
|
35
|
Chris@439
|
36 class RDFImporterImpl
|
Chris@439
|
37 {
|
Chris@439
|
38 public:
|
Chris@439
|
39 RDFImporterImpl(QString url, int sampleRate);
|
Chris@439
|
40 virtual ~RDFImporterImpl();
|
Chris@439
|
41
|
Chris@439
|
42 bool isOK();
|
Chris@439
|
43 QString getErrorString() const;
|
Chris@439
|
44
|
Chris@439
|
45 std::vector<Model *> getDataModels(ProgressReporter *);
|
Chris@439
|
46
|
Chris@439
|
47 protected:
|
Chris@439
|
48 QString m_uristring;
|
Chris@439
|
49 QString m_errorString;
|
Chris@439
|
50 int m_sampleRate;
|
Chris@439
|
51
|
Chris@439
|
52 typedef std::vector<float> ValueList;
|
Chris@439
|
53 typedef std::map<RealTime, ValueList> TimeValueMap;
|
Chris@439
|
54 typedef std::map<QString, TimeValueMap> TypeTimeValueMap;
|
Chris@439
|
55 typedef std::map<QString, TypeTimeValueMap> SourceTypeTimeValueMap;
|
Chris@439
|
56
|
Chris@440
|
57 void getDataModelsSparse(std::vector<Model *> &, ProgressReporter *);
|
Chris@440
|
58 void getDataModelsDense(std::vector<Model *> &, ProgressReporter *);
|
Chris@440
|
59
|
Chris@440
|
60 void getDenseFeatureProperties(QString featureUri,
|
Chris@440
|
61 int &sampleRate, int &windowLength,
|
Chris@440
|
62 int &hopSize, int &width, int &height);
|
Chris@440
|
63
|
Chris@439
|
64 void extractStructure(const TimeValueMap &map, bool &sparse,
|
Chris@439
|
65 int &minValueCount, int &maxValueCount);
|
Chris@439
|
66
|
Chris@439
|
67 void fillModel(SparseOneDimensionalModel *, const TimeValueMap &);
|
Chris@439
|
68 void fillModel(SparseTimeValueModel *, const TimeValueMap &);
|
Chris@439
|
69 void fillModel(EditableDenseThreeDimensionalModel *, const TimeValueMap &);
|
Chris@439
|
70 };
|
Chris@439
|
71
|
Chris@439
|
72
|
Chris@439
|
73 QString
|
Chris@439
|
74 RDFImporter::getKnownExtensions()
|
Chris@439
|
75 {
|
Chris@439
|
76 return "*.rdf *.n3 *.ttl";
|
Chris@439
|
77 }
|
Chris@439
|
78
|
Chris@439
|
79 RDFImporter::RDFImporter(QString url, int sampleRate) :
|
Chris@439
|
80 m_d(new RDFImporterImpl(url, sampleRate))
|
Chris@439
|
81 {
|
Chris@439
|
82 }
|
Chris@439
|
83
|
Chris@439
|
84 RDFImporter::~RDFImporter()
|
Chris@439
|
85 {
|
Chris@439
|
86 delete m_d;
|
Chris@439
|
87 }
|
Chris@439
|
88
|
Chris@439
|
89 bool
|
Chris@439
|
90 RDFImporter::isOK()
|
Chris@439
|
91 {
|
Chris@439
|
92 return m_d->isOK();
|
Chris@439
|
93 }
|
Chris@439
|
94
|
Chris@439
|
95 QString
|
Chris@439
|
96 RDFImporter::getErrorString() const
|
Chris@439
|
97 {
|
Chris@439
|
98 return m_d->getErrorString();
|
Chris@439
|
99 }
|
Chris@439
|
100
|
Chris@439
|
101 std::vector<Model *>
|
Chris@439
|
102 RDFImporter::getDataModels(ProgressReporter *r)
|
Chris@439
|
103 {
|
Chris@439
|
104 return m_d->getDataModels(r);
|
Chris@439
|
105 }
|
Chris@439
|
106
|
Chris@439
|
107 RDFImporterImpl::RDFImporterImpl(QString uri, int sampleRate) :
|
Chris@439
|
108 m_uristring(uri),
|
Chris@439
|
109 m_sampleRate(sampleRate)
|
Chris@439
|
110 {
|
Chris@439
|
111 }
|
Chris@439
|
112
|
Chris@439
|
113 RDFImporterImpl::~RDFImporterImpl()
|
Chris@439
|
114 {
|
Chris@439
|
115 }
|
Chris@439
|
116
|
Chris@439
|
117 bool
|
Chris@439
|
118 RDFImporterImpl::isOK()
|
Chris@439
|
119 {
|
Chris@439
|
120 return (m_errorString == "");
|
Chris@439
|
121 }
|
Chris@439
|
122
|
Chris@439
|
123 QString
|
Chris@439
|
124 RDFImporterImpl::getErrorString() const
|
Chris@439
|
125 {
|
Chris@439
|
126 return m_errorString;
|
Chris@439
|
127 }
|
Chris@439
|
128
|
Chris@439
|
129 std::vector<Model *>
|
Chris@439
|
130 RDFImporterImpl::getDataModels(ProgressReporter *reporter)
|
Chris@439
|
131 {
|
Chris@439
|
132 std::vector<Model *> models;
|
Chris@439
|
133
|
Chris@440
|
134 getDataModelsDense(models, reporter);
|
Chris@440
|
135
|
Chris@440
|
136 QString error;
|
Chris@440
|
137 if (!isOK()) error = m_errorString;
|
Chris@440
|
138 m_errorString = "";
|
Chris@440
|
139
|
Chris@440
|
140 getDataModelsSparse(models, reporter);
|
Chris@440
|
141
|
Chris@440
|
142 if (isOK()) m_errorString = error;
|
Chris@440
|
143
|
Chris@440
|
144 return models;
|
Chris@440
|
145 }
|
Chris@440
|
146
|
Chris@440
|
147 void
|
Chris@440
|
148 RDFImporterImpl::getDataModelsDense(std::vector<Model *> &models,
|
Chris@440
|
149 ProgressReporter *reporter)
|
Chris@440
|
150 {
|
Chris@440
|
151 SimpleSPARQLQuery query = SimpleSPARQLQuery
|
Chris@440
|
152 (QString
|
Chris@440
|
153 (
|
Chris@440
|
154 " PREFIX mo: <http://purl.org/ontology/mo/>"
|
Chris@440
|
155 " PREFIX af: <http://purl.org/ontology/af/>"
|
Chris@440
|
156
|
Chris@440
|
157 " SELECT ?feature ?signal_source ?feature_signal_type ?value "
|
Chris@440
|
158 " FROM <%1> "
|
Chris@440
|
159
|
Chris@440
|
160 " WHERE { "
|
Chris@440
|
161
|
Chris@440
|
162 " ?signal a mo:Signal ; "
|
Chris@440
|
163 " mo:available_as ?signal_source ; "
|
Chris@440
|
164 " af:signal_feature ?feature . "
|
Chris@440
|
165
|
Chris@440
|
166 " ?feature a ?feature_signal_type ; "
|
Chris@440
|
167 " af:value ?value . "
|
Chris@440
|
168
|
Chris@440
|
169 " } "
|
Chris@440
|
170 )
|
Chris@440
|
171 .arg(m_uristring));
|
Chris@440
|
172
|
Chris@440
|
173 SimpleSPARQLQuery::ResultList results = query.execute();
|
Chris@440
|
174
|
Chris@440
|
175 if (!query.isOK()) {
|
Chris@440
|
176 m_errorString = query.getErrorString();
|
Chris@440
|
177 return;
|
Chris@440
|
178 }
|
Chris@440
|
179
|
Chris@440
|
180 if (query.wasCancelled()) {
|
Chris@440
|
181 m_errorString = "Query cancelled";
|
Chris@440
|
182 return;
|
Chris@440
|
183 }
|
Chris@440
|
184
|
Chris@440
|
185 for (int i = 0; i < results.size(); ++i) {
|
Chris@440
|
186
|
Chris@440
|
187 QString feature = results[i]["feature"].value;
|
Chris@440
|
188 QString source = results[i]["signal_source"].value;
|
Chris@440
|
189 QString type = results[i]["feature_signal_type"].value;
|
Chris@440
|
190 QString value = results[i]["value"].value;
|
Chris@440
|
191
|
Chris@440
|
192 int sampleRate = 0;
|
Chris@440
|
193 int windowLength = 0;
|
Chris@440
|
194 int hopSize = 0;
|
Chris@440
|
195 int width = 0;
|
Chris@440
|
196 int height = 0;
|
Chris@440
|
197 getDenseFeatureProperties
|
Chris@440
|
198 (feature, sampleRate, windowLength, hopSize, width, height);
|
Chris@440
|
199
|
Chris@440
|
200 if (sampleRate != 0 && sampleRate != m_sampleRate) {
|
Chris@440
|
201 cerr << "WARNING: Sample rate in dense feature description does not match our underlying rate -- using rate from feature description" << endl;
|
Chris@440
|
202 }
|
Chris@440
|
203 if (sampleRate == 0) sampleRate = m_sampleRate;
|
Chris@440
|
204
|
Chris@440
|
205 if (hopSize == 0) {
|
Chris@440
|
206 cerr << "WARNING: Dense feature description does not specify a hop size -- assuming 1" << endl;
|
Chris@440
|
207 hopSize = 1;
|
Chris@440
|
208 }
|
Chris@440
|
209
|
Chris@440
|
210 if (height == 0) {
|
Chris@440
|
211 cerr << "WARNING: Dense feature description does not specify feature signal dimensions -- assuming one-dimensional (height = 1)" << endl;
|
Chris@440
|
212 height = 1;
|
Chris@440
|
213 }
|
Chris@440
|
214
|
Chris@440
|
215 QStringList values = value.split(' ', QString::SkipEmptyParts);
|
Chris@440
|
216
|
Chris@440
|
217 if (values.empty()) {
|
Chris@440
|
218 cerr << "WARNING: Dense feature description does not specify any values!" << endl;
|
Chris@440
|
219 continue;
|
Chris@440
|
220 }
|
Chris@440
|
221
|
Chris@440
|
222 if (height == 1) {
|
Chris@440
|
223
|
Chris@440
|
224 SparseTimeValueModel *m = new SparseTimeValueModel
|
Chris@440
|
225 (sampleRate, hopSize, false);
|
Chris@440
|
226
|
Chris@440
|
227 for (int j = 0; j < values.size(); ++j) {
|
Chris@440
|
228 float f = values[j].toFloat();
|
Chris@440
|
229 SparseTimeValueModel::Point point(j * hopSize, f, "");
|
Chris@440
|
230 m->addPoint(point);
|
Chris@440
|
231 }
|
Chris@440
|
232
|
Chris@440
|
233 models.push_back(m);
|
Chris@440
|
234
|
Chris@440
|
235 } else {
|
Chris@440
|
236
|
Chris@440
|
237 EditableDenseThreeDimensionalModel *m =
|
Chris@440
|
238 new EditableDenseThreeDimensionalModel(sampleRate, hopSize,
|
Chris@440
|
239 height, false);
|
Chris@440
|
240
|
Chris@440
|
241 EditableDenseThreeDimensionalModel::Column column;
|
Chris@440
|
242
|
Chris@440
|
243 int x = 0;
|
Chris@440
|
244
|
Chris@440
|
245 for (int j = 0; j < values.size(); ++j) {
|
Chris@440
|
246 if (j % height == 0 && !column.empty()) {
|
Chris@440
|
247 m->setColumn(x++, column);
|
Chris@440
|
248 column.clear();
|
Chris@440
|
249 }
|
Chris@440
|
250 column.push_back(values[j].toFloat());
|
Chris@440
|
251 }
|
Chris@440
|
252
|
Chris@440
|
253 if (!column.empty()) {
|
Chris@440
|
254 m->setColumn(x++, column);
|
Chris@440
|
255 }
|
Chris@440
|
256
|
Chris@440
|
257 models.push_back(m);
|
Chris@440
|
258 }
|
Chris@440
|
259 }
|
Chris@440
|
260 }
|
Chris@440
|
261
|
Chris@440
|
262 void
|
Chris@440
|
263 RDFImporterImpl::getDenseFeatureProperties(QString featureUri,
|
Chris@440
|
264 int &sampleRate, int &windowLength,
|
Chris@440
|
265 int &hopSize, int &width, int &height)
|
Chris@440
|
266 {
|
Chris@440
|
267 QString dimensionsQuery
|
Chris@440
|
268 (
|
Chris@440
|
269 " PREFIX mo: <http://purl.org/ontology/mo/>"
|
Chris@440
|
270 " PREFIX af: <http://purl.org/ontology/af/>"
|
Chris@440
|
271
|
Chris@440
|
272 " SELECT ?dimensions "
|
Chris@440
|
273 " FROM <%1> "
|
Chris@440
|
274
|
Chris@440
|
275 " WHERE { "
|
Chris@440
|
276
|
Chris@440
|
277 " <%2> af:dimensions ?dimensions . "
|
Chris@440
|
278
|
Chris@440
|
279 " } "
|
Chris@440
|
280 );
|
Chris@440
|
281
|
Chris@440
|
282 SimpleSPARQLQuery::Value dimensionsValue =
|
Chris@440
|
283 SimpleSPARQLQuery::singleResultQuery(dimensionsQuery
|
Chris@440
|
284 .arg(m_uristring).arg(featureUri),
|
Chris@440
|
285 "dimensions");
|
Chris@440
|
286
|
Chris@440
|
287 cerr << "Dimensions = \"" << dimensionsValue.value.toStdString() << "\""
|
Chris@440
|
288 << endl;
|
Chris@440
|
289
|
Chris@440
|
290 if (dimensionsValue.value != "") {
|
Chris@440
|
291 QStringList dl = dimensionsValue.value.split(" ");
|
Chris@440
|
292 if (dl.empty()) dl.push_back(dimensionsValue.value);
|
Chris@440
|
293 if (dl.size() > 0) height = dl[0].toInt();
|
Chris@440
|
294 if (dl.size() > 1) width = dl[1].toInt();
|
Chris@440
|
295 }
|
Chris@440
|
296
|
Chris@440
|
297 QString queryTemplate
|
Chris@440
|
298 (
|
Chris@440
|
299 " PREFIX mo: <http://purl.org/ontology/mo/>"
|
Chris@440
|
300 " PREFIX af: <http://purl.org/ontology/af/>"
|
Chris@440
|
301 " PREFIX tl: <http://purl.org/NET/c4dm/timeline.owl#>"
|
Chris@440
|
302
|
Chris@440
|
303 " SELECT ?%3 "
|
Chris@440
|
304 " FROM <%1> "
|
Chris@440
|
305
|
Chris@440
|
306 " WHERE { "
|
Chris@440
|
307
|
Chris@440
|
308 " <%2> mo:time ?time . "
|
Chris@440
|
309
|
Chris@440
|
310 " ?time a tl:Interval ; "
|
Chris@440
|
311 " tl:onTimeLine ?timeline . "
|
Chris@440
|
312
|
Chris@440
|
313 " ?map tl:rangeTimeLine ?timeline . "
|
Chris@440
|
314
|
Chris@440
|
315 " ?map tl:%3 ?%3 . "
|
Chris@440
|
316
|
Chris@440
|
317 " } "
|
Chris@440
|
318 );
|
Chris@440
|
319
|
Chris@440
|
320 // Another laborious workaround for rasqal's failure to handle
|
Chris@440
|
321 // multiple optionals properly
|
Chris@440
|
322
|
Chris@440
|
323 SimpleSPARQLQuery::Value srValue =
|
Chris@440
|
324 SimpleSPARQLQuery::singleResultQuery(queryTemplate
|
Chris@440
|
325 .arg(m_uristring).arg(featureUri)
|
Chris@440
|
326 .arg("sampleRate"),
|
Chris@440
|
327 "sampleRate");
|
Chris@440
|
328 if (srValue.value != "") {
|
Chris@440
|
329 sampleRate = srValue.value.toInt();
|
Chris@440
|
330 }
|
Chris@440
|
331
|
Chris@440
|
332 SimpleSPARQLQuery::Value hopValue =
|
Chris@440
|
333 SimpleSPARQLQuery::singleResultQuery(queryTemplate
|
Chris@440
|
334 .arg(m_uristring).arg(featureUri)
|
Chris@440
|
335 .arg("hopSize"),
|
Chris@440
|
336 "hopSize");
|
Chris@440
|
337 if (srValue.value != "") {
|
Chris@440
|
338 hopSize = hopValue.value.toInt();
|
Chris@440
|
339 }
|
Chris@440
|
340
|
Chris@440
|
341 SimpleSPARQLQuery::Value winValue =
|
Chris@440
|
342 SimpleSPARQLQuery::singleResultQuery(queryTemplate
|
Chris@440
|
343 .arg(m_uristring).arg(featureUri)
|
Chris@440
|
344 .arg("windowLength"),
|
Chris@440
|
345 "windowLength");
|
Chris@440
|
346 if (winValue.value != "") {
|
Chris@440
|
347 windowLength = winValue.value.toInt();
|
Chris@440
|
348 }
|
Chris@440
|
349
|
Chris@440
|
350 cerr << "sr = " << sampleRate << ", hop = " << hopSize << ", win = " << windowLength << endl;
|
Chris@440
|
351 }
|
Chris@440
|
352
|
Chris@440
|
353 void
|
Chris@440
|
354 RDFImporterImpl::getDataModelsSparse(std::vector<Model *> &models,
|
Chris@440
|
355 ProgressReporter *reporter)
|
Chris@440
|
356 {
|
Chris@439
|
357 // Our query is intended to retrieve every thing that has a time,
|
Chris@439
|
358 // and every feature type and value associated with a thing that
|
Chris@439
|
359 // has a time.
|
Chris@439
|
360
|
Chris@439
|
361 // We will then need to refine this big bag of results into a set
|
Chris@439
|
362 // of data models.
|
Chris@439
|
363
|
Chris@439
|
364 // Results that have different source signals should go into
|
Chris@439
|
365 // different models.
|
Chris@439
|
366
|
Chris@439
|
367 // Results that have different feature types should go into
|
Chris@439
|
368 // different models.
|
Chris@439
|
369
|
Chris@439
|
370 // Results that are sparse should go into different models from
|
Chris@439
|
371 // those that are dense (we need to examine the timestamps to
|
Chris@439
|
372 // establish this -- if the timestamps are regular, the results
|
Chris@439
|
373 // are dense -- so we can't do it as we go along, only after
|
Chris@439
|
374 // collecting all results).
|
Chris@439
|
375
|
Chris@439
|
376 // Timed things that have features associated with them should not
|
Chris@439
|
377 // appear directly in any model -- their features should appear
|
Chris@439
|
378 // instead -- and these should be different models from those used
|
Chris@439
|
379 // for timed things that do not have features.
|
Chris@439
|
380
|
Chris@439
|
381 // As we load the results, we'll push them into a partially
|
Chris@439
|
382 // structured container that maps from source signal (URI as
|
Chris@439
|
383 // string) -> feature type (likewise) -> time -> list of values.
|
Chris@439
|
384 // If the source signal or feature type is unavailable, the empty
|
Chris@439
|
385 // string will do.
|
Chris@439
|
386
|
Chris@439
|
387 SourceTypeTimeValueMap m;
|
Chris@439
|
388
|
Chris@439
|
389 QString queryString = QString(
|
Chris@439
|
390
|
Chris@439
|
391 " PREFIX event: <http://purl.org/NET/c4dm/event.owl#>"
|
Chris@439
|
392 " PREFIX time: <http://purl.org/NET/c4dm/timeline.owl#>"
|
Chris@439
|
393 " PREFIX mo: <http://purl.org/ontology/mo/>"
|
Chris@439
|
394 " PREFIX af: <http://purl.org/ontology/af/>"
|
Chris@439
|
395
|
Chris@440
|
396 " SELECT ?signal_source ?time ?event_type ?value"
|
Chris@439
|
397 " FROM <%1>"
|
Chris@439
|
398
|
Chris@439
|
399 " WHERE {"
|
Chris@440
|
400
|
Chris@440
|
401 " ?signal mo:available_as ?signal_source ."
|
Chris@440
|
402 " ?signal a mo:Signal ."
|
Chris@440
|
403
|
Chris@439
|
404 " ?signal mo:time ?interval ."
|
Chris@439
|
405 " ?interval time:onTimeLine ?tl ."
|
Chris@439
|
406 " ?t time:onTimeLine ?tl ."
|
Chris@439
|
407 " ?t time:at ?time ."
|
Chris@440
|
408 " ?timed_thing event:time ?t ."
|
Chris@440
|
409 " ?timed_thing a ?event_type ."
|
Chris@440
|
410
|
Chris@439
|
411 " OPTIONAL {"
|
Chris@440
|
412 " ?timed_thing af:feature ?value"
|
Chris@439
|
413 " }"
|
Chris@439
|
414 " }"
|
Chris@439
|
415
|
Chris@439
|
416 ).arg(m_uristring);
|
Chris@439
|
417
|
Chris@439
|
418 SimpleSPARQLQuery query(queryString);
|
Chris@439
|
419 query.setProgressReporter(reporter);
|
Chris@439
|
420
|
Chris@439
|
421 cerr << "Query will be: " << queryString.toStdString() << endl;
|
Chris@439
|
422
|
Chris@439
|
423 SimpleSPARQLQuery::ResultList results = query.execute();
|
Chris@439
|
424
|
Chris@439
|
425 if (!query.isOK()) {
|
Chris@439
|
426 m_errorString = query.getErrorString();
|
Chris@440
|
427 return;
|
Chris@439
|
428 }
|
Chris@439
|
429
|
Chris@439
|
430 if (query.wasCancelled()) {
|
Chris@439
|
431 m_errorString = "Query cancelled";
|
Chris@440
|
432 return;
|
Chris@439
|
433 }
|
Chris@439
|
434
|
Chris@439
|
435 for (int i = 0; i < results.size(); ++i) {
|
Chris@439
|
436
|
Chris@440
|
437 QString source = results[i]["signal_source"].value;
|
Chris@439
|
438
|
Chris@439
|
439 QString timestring = results[i]["time"].value;
|
Chris@439
|
440 RealTime time;
|
Chris@439
|
441 time = RealTime::fromXsdDuration(timestring.toStdString());
|
Chris@439
|
442 cerr << "time = " << time.toString() << " (from xsd:duration \""
|
Chris@439
|
443 << timestring.toStdString() << "\")" << endl;
|
Chris@439
|
444
|
Chris@440
|
445 QString type = results[i]["event_type"].value;
|
Chris@439
|
446
|
Chris@439
|
447 QString valuestring = results[i]["value"].value;
|
Chris@439
|
448 float value = 0.f;
|
Chris@439
|
449 bool haveValue = false;
|
Chris@439
|
450 if (valuestring != "") {
|
Chris@440
|
451 //!!! no -- runner actually writes a "CSV literal"
|
Chris@439
|
452 value = valuestring.toFloat(&haveValue);
|
Chris@439
|
453 cerr << "value = " << value << endl;
|
Chris@439
|
454 }
|
Chris@439
|
455
|
Chris@439
|
456 if (haveValue) {
|
Chris@439
|
457 m[source][type][time].push_back(value);
|
Chris@439
|
458 } else if (m[source][type].find(time) == m[source][type].end()) {
|
Chris@439
|
459 m[source][type][time] = ValueList();
|
Chris@439
|
460 }
|
Chris@439
|
461 }
|
Chris@439
|
462
|
Chris@439
|
463 for (SourceTypeTimeValueMap::const_iterator mi = m.begin();
|
Chris@439
|
464 mi != m.end(); ++mi) {
|
Chris@439
|
465
|
Chris@439
|
466 QString source = mi->first;
|
Chris@439
|
467
|
Chris@439
|
468 for (TypeTimeValueMap::const_iterator ttvi = mi->second.begin();
|
Chris@439
|
469 ttvi != mi->second.end(); ++ttvi) {
|
Chris@439
|
470
|
Chris@439
|
471 QString type = ttvi->first;
|
Chris@439
|
472
|
Chris@439
|
473 // Now we need to work out what sort of model to use for
|
Chris@439
|
474 // this source/type combination. Ultimately we'll
|
Chris@439
|
475 // hopefully be able to map directly from the type to the
|
Chris@439
|
476 // model on the basis of known structures for the types,
|
Chris@439
|
477 // but we also want to be able to handle untyped data
|
Chris@439
|
478 // according to its apparent structure so let's do that
|
Chris@439
|
479 // first.
|
Chris@439
|
480
|
Chris@439
|
481 bool sparse = false;
|
Chris@439
|
482 int minValueCount = 0, maxValueCount = 0;
|
Chris@439
|
483
|
Chris@439
|
484 extractStructure(ttvi->second, sparse, minValueCount, maxValueCount);
|
Chris@439
|
485
|
Chris@439
|
486 cerr << "For source \"" << source.toStdString() << "\", type \""
|
Chris@439
|
487 << type.toStdString() << "\" we have sparse = " << sparse
|
Chris@439
|
488 << ", min value count = " << minValueCount << ", max = "
|
Chris@439
|
489 << maxValueCount << endl;
|
Chris@439
|
490
|
Chris@439
|
491 // Model allocations:
|
Chris@439
|
492 //
|
Chris@439
|
493 // Sparse, no values: SparseOneDimensionalModel
|
Chris@439
|
494 //
|
Chris@439
|
495 // Sparse, always 1 value: SparseTimeValueModel
|
Chris@439
|
496 //
|
Chris@439
|
497 // Sparse, > 1 value: No standard model for this. If
|
Chris@439
|
498 // there are always 2 values, perhaps hack it into
|
Chris@439
|
499 // NoteModel for now? Or always use SparseTimeValueModel
|
Chris@439
|
500 // and discard all but the first value.
|
Chris@439
|
501 //
|
Chris@439
|
502 // Dense, no values: Meaningless; no suitable model
|
Chris@439
|
503 //
|
Chris@439
|
504 // Dense, > 0 values: EditableDenseThreeDimensionalModel
|
Chris@439
|
505 //
|
Chris@439
|
506 // These should just be our fallback positions; we want to
|
Chris@439
|
507 // be reading semantic data from the RDF in order to pick
|
Chris@439
|
508 // the right model directly
|
Chris@439
|
509
|
Chris@439
|
510 enum { SODM, STVM, EDTDM } modelType = SODM;
|
Chris@439
|
511
|
Chris@439
|
512 if (sparse) {
|
Chris@439
|
513 if (maxValueCount == 0) {
|
Chris@439
|
514 modelType = SODM;
|
Chris@439
|
515 } else if (minValueCount == 1 && maxValueCount == 1) {
|
Chris@439
|
516 modelType = STVM;
|
Chris@439
|
517 } else {
|
Chris@439
|
518 cerr << "WARNING: No suitable model available for sparse data with between " << minValueCount << " and " << maxValueCount << " values" << endl;
|
Chris@439
|
519 modelType = STVM;
|
Chris@439
|
520 }
|
Chris@439
|
521 } else {
|
Chris@439
|
522 if (maxValueCount == 0) {
|
Chris@439
|
523 cerr << "WARNING: Dense data set with no values is not meaningful, skipping" << endl;
|
Chris@439
|
524 continue;
|
Chris@439
|
525 } else {
|
Chris@439
|
526 modelType = EDTDM;
|
Chris@439
|
527 }
|
Chris@439
|
528 }
|
Chris@439
|
529
|
Chris@439
|
530 //!!! set model name &c
|
Chris@439
|
531
|
Chris@439
|
532 if (modelType == SODM) {
|
Chris@439
|
533
|
Chris@439
|
534 SparseOneDimensionalModel *model =
|
Chris@439
|
535 new SparseOneDimensionalModel(m_sampleRate, 1, false);
|
Chris@439
|
536
|
Chris@439
|
537 fillModel(model, ttvi->second);
|
Chris@439
|
538 models.push_back(model);
|
Chris@439
|
539
|
Chris@439
|
540 } else if (modelType == STVM) {
|
Chris@439
|
541
|
Chris@439
|
542 SparseTimeValueModel *model =
|
Chris@439
|
543 new SparseTimeValueModel(m_sampleRate, 1, false);
|
Chris@439
|
544
|
Chris@439
|
545 fillModel(model, ttvi->second);
|
Chris@439
|
546 models.push_back(model);
|
Chris@439
|
547
|
Chris@439
|
548 } else {
|
Chris@439
|
549
|
Chris@439
|
550 EditableDenseThreeDimensionalModel *model =
|
Chris@439
|
551 new EditableDenseThreeDimensionalModel(m_sampleRate, 1, 0,
|
Chris@439
|
552 false);
|
Chris@439
|
553
|
Chris@439
|
554 fillModel(model, ttvi->second);
|
Chris@439
|
555 models.push_back(model);
|
Chris@439
|
556 }
|
Chris@439
|
557 }
|
Chris@439
|
558 }
|
Chris@439
|
559 }
|
Chris@439
|
560
|
Chris@439
|
561 void
|
Chris@439
|
562 RDFImporterImpl::extractStructure(const TimeValueMap &tvm,
|
Chris@439
|
563 bool &sparse,
|
Chris@439
|
564 int &minValueCount,
|
Chris@439
|
565 int &maxValueCount)
|
Chris@439
|
566 {
|
Chris@439
|
567 // These are floats intentionally rather than RealTime --
|
Chris@439
|
568 // see logic for handling rounding error below
|
Chris@439
|
569 float firstTime = 0.f;
|
Chris@439
|
570 float timeStep = 0.f;
|
Chris@439
|
571 bool haveTimeStep = false;
|
Chris@439
|
572
|
Chris@439
|
573 for (TimeValueMap::const_iterator tvi = tvm.begin(); tvi != tvm.end(); ++tvi) {
|
Chris@439
|
574
|
Chris@439
|
575 RealTime time = tvi->first;
|
Chris@439
|
576 int valueCount = tvi->second.size();
|
Chris@439
|
577
|
Chris@439
|
578 if (tvi == tvm.begin()) {
|
Chris@439
|
579
|
Chris@439
|
580 minValueCount = valueCount;
|
Chris@439
|
581 maxValueCount = valueCount;
|
Chris@439
|
582
|
Chris@439
|
583 firstTime = time.toDouble();
|
Chris@439
|
584
|
Chris@439
|
585 } else {
|
Chris@439
|
586
|
Chris@439
|
587 if (valueCount < minValueCount) minValueCount = valueCount;
|
Chris@439
|
588 if (valueCount > maxValueCount) maxValueCount = valueCount;
|
Chris@439
|
589
|
Chris@439
|
590 if (!haveTimeStep) {
|
Chris@439
|
591 timeStep = time.toDouble() - firstTime;
|
Chris@439
|
592 if (timeStep == 0.f) sparse = true;
|
Chris@439
|
593 haveTimeStep = true;
|
Chris@439
|
594 } else if (!sparse) {
|
Chris@439
|
595 // test whether this time is within
|
Chris@439
|
596 // rounding-error range of being an integer
|
Chris@439
|
597 // multiple of some constant away from the
|
Chris@439
|
598 // first time
|
Chris@439
|
599 float timeAsFloat = time.toDouble();
|
Chris@439
|
600 int count = int((timeAsFloat - firstTime) / timeStep + 0.5);
|
Chris@439
|
601 float expected = firstTime + (timeStep * count);
|
Chris@439
|
602 if (fabsf(expected - timeAsFloat) > 1e-6) {
|
Chris@439
|
603 cerr << "Event at " << timeAsFloat << " is not evenly spaced -- would expect it to be " << expected << " for a spacing of " << count << " * " << timeStep << endl;
|
Chris@439
|
604 sparse = true;
|
Chris@439
|
605 }
|
Chris@439
|
606 }
|
Chris@439
|
607 }
|
Chris@439
|
608 }
|
Chris@439
|
609 }
|
Chris@439
|
610
|
Chris@439
|
611 void
|
Chris@439
|
612 RDFImporterImpl::fillModel(SparseOneDimensionalModel *model,
|
Chris@439
|
613 const TimeValueMap &tvm)
|
Chris@439
|
614 {
|
Chris@439
|
615 //!!! labels &c not yet handled
|
Chris@439
|
616
|
Chris@439
|
617 for (TimeValueMap::const_iterator tvi = tvm.begin();
|
Chris@439
|
618 tvi != tvm.end(); ++tvi) {
|
Chris@439
|
619
|
Chris@439
|
620 RealTime time = tvi->first;
|
Chris@439
|
621 long frame = RealTime::realTime2Frame(time, m_sampleRate);
|
Chris@439
|
622
|
Chris@439
|
623 SparseOneDimensionalModel::Point point(frame);
|
Chris@439
|
624
|
Chris@439
|
625 model->addPoint(point);
|
Chris@439
|
626 }
|
Chris@439
|
627 }
|
Chris@439
|
628
|
Chris@439
|
629 void
|
Chris@439
|
630 RDFImporterImpl::fillModel(SparseTimeValueModel *model,
|
Chris@439
|
631 const TimeValueMap &tvm)
|
Chris@439
|
632 {
|
Chris@439
|
633 //!!! labels &c not yet handled
|
Chris@439
|
634
|
Chris@439
|
635 for (TimeValueMap::const_iterator tvi = tvm.begin();
|
Chris@439
|
636 tvi != tvm.end(); ++tvi) {
|
Chris@439
|
637
|
Chris@439
|
638 RealTime time = tvi->first;
|
Chris@439
|
639 long frame = RealTime::realTime2Frame(time, m_sampleRate);
|
Chris@439
|
640
|
Chris@439
|
641 float value = 0.f;
|
Chris@439
|
642 if (!tvi->second.empty()) value = *tvi->second.begin();
|
Chris@439
|
643
|
Chris@439
|
644 SparseTimeValueModel::Point point(frame, value, "");
|
Chris@439
|
645
|
Chris@439
|
646 model->addPoint(point);
|
Chris@439
|
647 }
|
Chris@439
|
648 }
|
Chris@439
|
649
|
Chris@439
|
650 void
|
Chris@439
|
651 RDFImporterImpl::fillModel(EditableDenseThreeDimensionalModel *model,
|
Chris@439
|
652 const TimeValueMap &tvm)
|
Chris@439
|
653 {
|
Chris@439
|
654 //!!! labels &c not yet handled
|
Chris@439
|
655
|
Chris@439
|
656 //!!! start time offset not yet handled
|
Chris@439
|
657
|
Chris@439
|
658 size_t col = 0;
|
Chris@439
|
659
|
Chris@439
|
660 for (TimeValueMap::const_iterator tvi = tvm.begin();
|
Chris@439
|
661 tvi != tvm.end(); ++tvi) {
|
Chris@439
|
662
|
Chris@439
|
663 model->setColumn(col++, tvi->second);
|
Chris@439
|
664 }
|
Chris@439
|
665 }
|
Chris@439
|
666
|