-
Notifications
You must be signed in to change notification settings - Fork 0
/
TimmeParameterInferenceEngine.cpp
352 lines (352 loc) · 10.8 KB
/
TimmeParameterInferenceEngine.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
///*
// * TimmeParameterInferenceEngine.cpp
// *
// * Created on: 12 Jan 2014
// * Author: rusty
// */
//
//#include "TimmeParameterInferenceEngine.h"
//
//
//TimmeParameterInferenceEngine::TimmeParameterInferenceEngine(int ID, int unitCount, int nTimesteps, int unitDimension, int parametersPerUnit, IDynamicalUnit** units, double timestepSize){
// this->ID = ID;
// this->unitCount = unitCount;
// this->nTimesteps = nTimesteps;
// this->unitDimension = unitDimension;
// this->parametersPerUnit = parametersPerUnit;
// this->timeseriesFileName = "null";
//
// X = zeros<mat>(1,1);
// G = zeros<mat>(1,1);
//
// this->jHat = new double*[unitCount];
// for (int i=0; i < this->unitCount; i++){jHat[i] = new double[unitCount+(parametersPerUnit*unitCount)];}
//
// this->fullTimeseries = new double*[nTimesteps];
// int columnCount = unitCount * unitDimension + 1;
// for (int i = 0; i < nTimesteps; i++) {
// fullTimeseries[i] = new double[columnCount];
// for (int j=0; j<columnCount; j++){
// fullTimeseries[i][j] = 0;
// }
// }
//
// this->timestepSize = timestepSize;
// this->units =units;
//
//
//}
//
//TimmeParameterInferenceEngine::TimmeParameterInferenceEngine(int unitCount, int nTimesteps, int unitDimension, int parametersPerUnit, const char* timeseriesFileName, IDynamicalUnit** dummyUnits){
//
// this->unitCount = unitCount;
// this->nTimesteps = nTimesteps;
// this->unitDimension = unitDimension;
// this->parametersPerUnit = parametersPerUnit;
// this->timeseriesFileName = timeseriesFileName;
//// this->parameters = parameters;
//
// X = zeros<mat>(1,1);
// G = zeros<mat>(1,1);
//
// this->jHat = new double*[unitCount];
// this->fullTimeseries = new double*[nTimesteps];
//
// for (int i=0; i < this->unitCount; i++){jHat[i] = new double[unitCount+(parametersPerUnit*unitCount)];}
//
// readTimseriesToArray();
//
// this->timestepSize = 0;
// this->units = dummyUnits;
//
// }
//
//TimmeParameterInferenceEngine::TimmeParameterInferenceEngine(int unitCount, int nTimesteps, int unitDimension, int parametersPerUnit, vector<vector <double> >& populationTimeseries, IDynamicalUnit** dummyUnits){
//
// this->ID = 0;
// this->unitCount = unitCount;
//// this->nTimesteps = nTimesteps;
// this->nTimesteps = (int)populationTimeseries.size();
// this->unitDimension = unitDimension;
// this->parametersPerUnit = parametersPerUnit;
// this->timeseriesFileName = "not needed";
//
// X = zeros<mat>(1,1);
// G = zeros<mat>(1,1);
//
// this->jHat = new double*[unitCount];
// for (int i=0; i < this->unitCount; i++){jHat[i] = new double[unitCount+(parametersPerUnit*unitCount)];}
//
// this->fullTimeseries = new double*[nTimesteps];
// int columnCount = unitCount + 1;
// for (int i = 0; i <= nTimesteps; i++) {
// fullTimeseries[i] = new double[columnCount];
// for (int j=0; j<columnCount; j++){
// fullTimeseries[i][j] = populationTimeseries.at(i).at(j);
// }
// }
//
//
// this->timestepSize = 0;
// this->units = dummyUnits;
//
//}
//
//bool TimmeParameterInferenceEngine::runInference(int sampleStepLength){
// // fill timeseries:
//// readUnitHistoriesToArray();
//
// // REPLACE THIS!! DATA BODGE..
//// double replaceZero = 100;
//// int columnCount = unitCount * unitDimension + 1;
//// for (int i = 0; i<nTimesteps; i++){
//// for (int j=0; j<columnCount; j++){
//// if (fullTimeseries[i][j]==0){
//// fullTimeseries[i][j] = replaceZero;
//// }
//// }
//// }
////
//
// if(!fillJhat(sampleStepLength)){
// return false;
// }
////!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!switch!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//// saveResult();
//
// return true;
// }
//
//bool TimmeParameterInferenceEngine::saveResult(){
//
// string fname = "_GM_result.log";
// stringstream str_ID;
// str_ID << ID;
// fname.insert(0, str_ID.str());
//
// ofstream ofile;
// ofile.open(fname.c_str());
// if(!ofile){
// cerr << "GM result log file not open" << endl;
// return false;
// }
// ofile << "recontructed adjacency (Jhat) = " << endl << endl;
// for (int i=0;i<unitCount;i++){
//
// for (int j=0;j<unitCount;j++){
// ofile << jHat[i][j] << " ";
// }
// ofile << endl;
// }
// ofile << endl;
//
// // this should be a separate function:
// ofile << "recontructed parameters = " << endl << endl;
// for (int i=0; i<unitCount; i++){ofile << jHat[i][unitCount] << endl;}
//
// ofile << "*********************************************************" << endl;
// ofile.close();
//
//
// return true;
//}
//
//bool TimmeParameterInferenceEngine::fillJhat(int sampleStepLength){
//
// for (int i=0;i<unitCount;i++){
// mat temp = zeros(1,unitCount+(parametersPerUnit));
//
// if(!timmeJhatI(i, sampleStepLength,temp)){
// return false;
// }
//
// for (int j=0;j<unitCount+(parametersPerUnit);j++){
// jHat[i][j] = temp(0,j);
// }
// }
//
// return true;
//
// }
//
//bool TimmeParameterInferenceEngine::timmeJhatI(int unitID, int sampleStepLength, mat &JhatI){
//
// int t = 0;
// int t2;
// int M = (nTimesteps/sampleStepLength) - 1;
// mat X = zeros<mat>(1,M);
// mat G = zeros<mat>(unitCount+(parametersPerUnit),M);
// double xtau[unitCount];
//
// double xdotTau;
//
// for (int m=0;m<M;m++){
//
// t2 = t + sampleStepLength;
//
// xtau[unitID] = (fullTimeseries[t2][1+unitID] + fullTimeseries[t][1+unitID])/2;
// xdotTau = (fullTimeseries[t2][1+unitID] - fullTimeseries[t][1+unitID])/((fullTimeseries[t2][0] - fullTimeseries[t][0]));
// //xtau[unitID] = (histories[unitID]->getHistoryDiTi(0,t2)+histories[unitID]->getHistoryDiTi(0,t))/2;
// //xdotTau = (histories[unitID]->getHistoryDiTi(0,t2)-histories[unitID]->getHistoryDiTi(0,t))/(thistories[t2]-thistories[t]);
//
// X(0,m) = xdotTau;// - (xtau[unitID]*parameters[unitID]); // !! naughty workaround now
// //X(0,m) = xdotTau - (xtau[unitID]*Units[unitID]->getParamI(0)); // !! need unit parameters...
//
//
// for(int i=0;i<unitCount;i++){
// xtau[i] = (fullTimeseries[t2][1+i] + fullTimeseries[t][1+i])/2;
//
// //G(i,m) = sin(xtau[i]);
// G(i,m) = xtau[i] * xtau[unitID];
// }
//
// G(unitCount,m) = xtau[unitID];
// t = t2;
//
// }
//
// //mat Jhat = X*trans(G)*inv(G*trans(G));
// // INSERT CHECK FOR IF THIS OPERATION IS SUCCESSFUL..
// mat Iv_temp;
// if(!inv(Iv_temp, G*trans(G))){
// cerr << "inference failure: sampled matrix singular" << endl;
// return false;
// }
//
// JhatI = X*trans(G)*Iv_temp;
// return true;
// }
//
//double** TimmeParameterInferenceEngine::getJhat(){return jHat;}
//
//void TimmeParameterInferenceEngine::printJhat(){
// cout << "recontructed adjacency (Jhat) = " << endl << endl;
// for (int i=0;i<unitCount;i++){
//
// for (int j=0;j<unitCount;j++){
// cout << jHat[i][j] << " ";
// }
// cout << endl;
// }
// cout << endl;
//
// // this should be a separate function:
// cout << "recontructed parameters = " << endl << endl;
// for (int i=0; i<unitCount; i++){cout << jHat[i][unitCount] << endl;}
//
// cout << "*********************************************************" << endl;
// }
//
//
//// void printQ(double accuracy, INetwork *net){
//// cout << "quality of reconstruction = " << qualityOfReconstruction(accuracy, net);
//// cout << " at accuracy: " << accuracy << endl << endl;
//// }
//
//
//bool TimmeParameterInferenceEngine::originalQualityOfReconstruction(double accuracy, double& quality, vector<double > IMelements){
//// this only considers inter-specific interactions, and implements the oringal TImme metric.
// quality = 0;
// double deltaJij = 0;
// for (int i = 0;i<unitCount;i++){
// for (int j = 0;j<unitCount;j++){
// if (i!=j){
// deltaJij = abs(jHat[i][j] - IMelements.at((i*2)+j))/(2*max(abs(jHat[i][j]), abs(IMelements.at((2*i)+j))));
// quality = quality + heaviside((1-accuracy)-deltaJij);
// }
// }
// }
// quality = quality / (unitCount*unitCount - unitCount);
//
//
// return true;
// }
//
//
//bool TimmeParameterInferenceEngine::qualityOfReconstruction(double accuracy, double& quality, vector<double > IMelements){
//
// quality = 0;
// double deltaJij = 0;
// for (int i = 0;i<unitCount;i++){
// for (int j = 0;j<unitCount;j++){
//
// deltaJij = abs(jHat[i][j] - IMelements.at((i*2)+j))/(2*max(abs(jHat[i][j]), abs(IMelements.at((2*i)+j))));
// //quality = quality + heaviside((1-accuracy)-deltaJij);
// // just using normalised elemnt wise difference:
// quality = quality + deltaJij;
// }
// }
// quality = quality / (unitCount*unitCount);
//
// // metric that only considers the inter-specific interactions...
//// quality = 0;
//// double deltaJij = 0;
//// for (int i = 0;i<unitCount;i++){
//// for (int j = 0;j<unitCount;j++){
//// if (i!=j){
//// deltaJij = abs(jHat[i][j] - IMelements.at((i*2)+j))/(2*max(abs(jHat[i][j]), abs(IMelements.at((2*i)+j))));
//// //quality = quality + heaviside((1-accuracy)-deltaJij);
//// // just using normalised elemnt wise difference:
//// quality = quality + deltaJij;
//// }
//// }
//// }
//// quality = quality / (unitCount);
//
//
// return true;
// }
//
//bool TimmeParameterInferenceEngine::percentageQualityOfReconstruction(double accuracy, double& quality, vector<double > IMelements){
//// for two species, only considering inter-specific interactions:
// quality = 0;
// double deltaJij = 0;
// for (int i = 0;i<unitCount;i++){
// for (int j = 0;j<unitCount;j++){
// if (i!=j){
// deltaJij = abs(jHat[i][j] - IMelements.at((i*2)+j))/(abs(IMelements.at((2*i)+j)));
// //quality = quality + heaviside((1-accuracy)-deltaJij);
// // just using normalised elemnt wise difference:
// quality = quality + deltaJij;
// }
// }
// }
// quality = quality / (unitCount);
// return true;
// }
//
//
//bool TimmeParameterInferenceEngine::unweightedQualityOfReconstruction(double threshold, double& quality, vector< vector<double > > adjacency){
//
//
// if (unitCount!=(signed int)adjacency.size()){
// cerr << "cannot calculate quality: adjacency given is of wrong dimension" << endl;
// return false;
// }
// // do thresholding here or in inference method??
// quality = 0;
// double estimate = 0;
// for (unsigned int i =0; i< adjacency.size(); i++){
// for (unsigned int j=0; j<adjacency.size(); j++){
// if (i!=j){
// if (abs(jHat[i][j])<threshold){estimate=0;}
// else {estimate=jHat[i][j];}
// if (estimate*adjacency.at(i).at(j)>0){quality++;}
//// else if (adjacency.at(i).at(j)==0 && estimate==0){quality++;}
// else if (abs(adjacency.at(i).at(j))<=threshold && estimate==0){quality++;} //!!!
// }
// }
// }
// quality = quality / (unitCount*unitCount-unitCount);
// return true;
//}
//
//double TimmeParameterInferenceEngine::heaviside(double x){
// if (x >= 0){return 1.0;}
// else {return 0.0;}
// }
//
//double TimmeParameterInferenceEngine::getSampleRate(int sampleStepLength){return 1/(fullTimeseries[sampleStepLength][0] - fullTimeseries[0][0]);}
//
//
//