-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBioFVM_matlab.cpp
More file actions
623 lines (480 loc) · 16.1 KB
/
Copy pathBioFVM_matlab.cpp
File metadata and controls
623 lines (480 loc) · 16.1 KB
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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
/*
#############################################################################
# If you use BioFVM in your project, please cite BioFVM and the version #
# number, such as below: #
# #
# We solved the diffusion equations using BioFVM (Version 1.1.6) [1] #
# #
# [1] A. Ghaffarizadeh, S.H. Friedman, and P. Macklin, BioFVM: an efficient #
# parallelized diffusive transport solver for 3-D biological simulations,#
# Bioinformatics 32(8): 1256-8, 2016. DOI: 10.1093/bioinformatics/btv730 #
# #
#############################################################################
# #
# BSD 3-Clause License (see https://opensource.org/licenses/BSD-3-Clause) #
# #
# Copyright (c) 2015-2017, Paul Macklin and the BioFVM Project #
# All rights reserved. #
# #
# Redistribution and use in source and binary forms, with or without #
# modification, are permitted provided that the following conditions are #
# met: #
# #
# 1. Redistributions of source code must retain the above copyright notice, #
# this list of conditions and the following disclaimer. #
# #
# 2. Redistributions in binary form must reproduce the above copyright #
# notice, this list of conditions and the following disclaimer in the #
# documentation and/or other materials provided with the distribution. #
# #
# 3. Neither the name of the copyright holder nor the names of its #
# contributors may be used to endorse or promote products derived from this #
# software without specific prior written permission. #
# #
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS #
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED #
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A #
# PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER #
# OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, #
# EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, #
# PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR #
# PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF #
# LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING #
# NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS #
# SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #
# #
#############################################################################
*/
#include "BioFVM_matlab.h"
#include <ctime>
#include <string>
#include <cstring>
#include <cstdio>
namespace BioFVM{
unsigned int thousands( unsigned int& input )
{ return input / 1000;}
unsigned int hundreds( unsigned int& input )
{ return ( input % 1000 ) / 100; }
unsigned int tens( unsigned int& input )
{ return ( input % 100 ) / 10; }
unsigned int ones( unsigned int& input )
{ return (input % 10); }
// vector< vector<double> > read_matlab4( string filename )
std::vector< std::vector<double> > read_matlab( std::string filename )
{
// Matrix output;
std::vector< std::vector<double> > output;
FILE* fp;
fp = fopen( filename.c_str() , "rb" );
if( fp == NULL )
{
std::cout << "Error: could not open file " << filename << "!" << std::endl;
return output;
}
typedef unsigned int UINT;
UINT UINTs = sizeof(UINT);
// read the basic header information
UINT temp;
fread( (char*) &temp , UINTs , 1 , fp );
UINT type_numeric_format = thousands(temp);
UINT type_reserved = hundreds(temp);
UINT type_data_format = tens(temp);
UINT type_matrix_type = ones(temp);
/*
cout << type_numeric_format << " "
<< type_reserved << " "
<< type_data_format << " "
<< type_matrix_type << endl;
*/
// make sure it's a matlab L4 file
if( type_numeric_format != 0 || // little-endian
type_reserved != 0 || // should always be 0
type_data_format > 5 || // unknown format
type_matrix_type != 0 ) // want full matrices, not sparse
{
std::cout << "Error reading file " << filename << ": I can't read this format yet!" << std::endl;
return output;
}
// get the size of the data
UINT rows;
fread( (char*) &rows , UINTs , 1, fp );
UINT cols;
fread( (char*) &cols, UINTs , 1 , fp );
// resize the output accordingly
// output.SetSize( rows, cols );
std::vector<double> TemplateRow(cols,0.0);
output.resize( rows , TemplateRow );
// make sure we're not dealing with complex numbers
UINT imag;
fread( (char*) &imag, UINTs, 1 , fp );
if( imag != 0 )
{
std::cout << "Error: I can't read imaginary matrices yet!" << std::endl;
return output;
}
// Get the name of the variable. We don't tend to use this on reading (for now).
// But if we were to output a more complex data structure with
// vector< vector<double> > and vector<string>, we could!
// if we actually use the names, then I'd suggest that we do a little parsing:
// is it a MultiCellDS field array?
// Make a format for that. Something like this:
// MultiCellDS_Fields:name1,name2,...,nameN, where N = rows - 3;
UINT name_length;
fread( (char*) &name_length, UINTs, 1 , fp );
char* name;
name = new char [name_length];
// this is the end of the 20-byte header
// read the name
fread( name , name_length , 1 , fp );
// read the real part of the matrix
int i = 0;
int j = 0;
switch( type_data_format )
{
case 0:
// all fields are doubles
for( int n=0; n < rows*cols ; n++ )
{
double temp;
fread( (char*) &temp, sizeof(double), 1 , fp );
(output[i])[j] = temp;
i++;
if( i == rows )
{ i=0; j++; }
}
break;
case 1:
// all fields are floats
for( int n=0; n < rows*cols ; n++ )
{
float temp;
fread( (char*) &temp, sizeof(float), 1 , fp );
(output[i])[j] = (double) temp;
i++;
if( i == rows )
{ i=0; j++; }
}
break;
case 2:
// all fields are signed ints of size 4 bytes
for( int n=0; n < rows*cols ; n++ )
{
int temp;
fread( (char*) &temp, sizeof(int), 1 , fp );
(output[i])[j] = (double) temp;
i++;
if( i == rows )
{ i=0; j++; }
}
break;
case 3:
// all fields are signed ints of size 2 bytes
for( int n=0; n < rows*cols ; n++ )
{
short temp;
fread( (char*) &temp, sizeof(short), 1 , fp );
(output[i])[j] = (double) temp;
i++;
if( i == rows )
{ i=0; j++; }
}
break;
case 4:
// all fields are unsigned ints of size 2 bytes
for( int n=0; n < rows*cols ; n++ )
{
unsigned short temp;
fread( (char*) &temp, sizeof(unsigned short), 1 , fp );
(output[i])[j] = (double) temp;
i++;
if( i == rows )
{ i=0; j++; }
}
break;
case 5:
// all fields are unsigned ints of size 1 bytes
for( int n=0; n < rows*cols ; n++ )
{
unsigned char temp;
fread( (char*) &temp, sizeof(unsigned char), 1 , fp );
(output[i])[j] = (double) temp;
i++;
if( i == rows )
{ i=0; j++; }
}
break;
default:
std::cout << "Error: Unknown format!" << std::endl;
break;
}
// read the imaginary part of the matrix (not supported!)
fclose( fp );
delete name;
return output;
}
named_vector_data read_matlab_with_names( std::string filename )
{
named_vector_data output;
FILE* fp;
fp = fopen( filename.c_str() , "rb" );
if( fp == NULL )
{
std::cout << "Error: could not open file " << filename << "!" << std::endl;
return output;
}
typedef unsigned int UINT;
UINT UINTs = sizeof(UINT);
// read the basic header information
UINT temp;
fread( (char*) &temp , UINTs , 1 , fp );
UINT type_numeric_format = thousands(temp);
UINT type_reserved = hundreds(temp);
UINT type_data_format = tens(temp);
UINT type_matrix_type = ones(temp);
// make sure it's a matlab L4 file
if( type_numeric_format != 0 || // little-endian
type_reserved != 0 || // should always be 0
type_data_format > 5 || // unknown format
type_matrix_type != 0 ) // want full matrices, not sparse
{
std::cout << "Error reading file " << filename << ": I can't read this format yet!" << std::endl;
return output;
}
// get the size of the data
UINT rows;
fread( (char*) &rows , UINTs , 1, fp );
UINT cols;
fread( (char*) &cols, UINTs , 1 , fp );
// resize the output accordingly
std::vector<double> TemplateRow(cols,0.0);
output.data.resize( rows , TemplateRow );
// make sure we're not dealing with complex numbers
UINT imag;
fread( (char*) &imag, UINTs, 1 , fp );
if( imag != 0 )
{
std::cout << "Error: I can't read imaginary matrices yet!" << std::endl;
return output;
}
// Get the name of the variable. We don't tend to use this on reading (for now).
// But if we were to output a more complex data structure with
// vector< vector<double> > and vector<string>, we could!
// if we actually use the names, then I'd suggest that we do a little parsing:
// is it a MultiCellDS field array?
// Make a format for that. Something like this:
// MultiCellDS_Fields:name1,name2,...,nameN, where N = rows - 3;
UINT name_length;
fread( (char*) &name_length, UINTs, 1 , fp );
char* name;
name = new char [name_length];
// this is the end of the 20-byte header
// read the name
fread( name , name_length , 1 , fp );
output.names[0] = name;
// read the real part of the matrix
int i = 0;
int j = 0;
switch( type_data_format )
{
case 0:
// all fields are doubles
for( int n=0; n < rows*cols ; n++ )
{
double temp;
fread( (char*) &temp, sizeof(double), 1 , fp );
(output.data[i])[j] = temp;
i++;
if( i == rows )
{ i=0; j++; }
}
break;
case 1:
// all fields are floats
for( int n=0; n < rows*cols ; n++ )
{
float temp;
fread( (char*) &temp, sizeof(float), 1 , fp );
(output.data[i])[j] = (double) temp;
i++;
if( i == rows )
{ i=0; j++; }
}
break;
case 2:
// all fields are signed ints of size 4 bytes
for( int n=0; n < rows*cols ; n++ )
{
int temp;
fread( (char*) &temp, sizeof(int), 1 , fp );
(output.data[i])[j] = (double) temp;
i++;
if( i == rows )
{ i=0; j++; }
}
break;
case 3:
// all fields are signed ints of size 2 bytes
for( int n=0; n < rows*cols ; n++ )
{
short temp;
fread( (char*) &temp, sizeof(short), 1 , fp );
(output.data[i])[j] = (double) temp;
i++;
if( i == rows )
{ i=0; j++; }
}
break;
case 4:
// all fields are unsigned ints of size 2 bytes
for( int n=0; n < rows*cols ; n++ )
{
unsigned short temp;
fread( (char*) &temp, sizeof(unsigned short), 1 , fp );
(output.data[i])[j] = (double) temp;
i++;
if( i == rows )
{ i=0; j++; }
}
break;
case 5:
// all fields are unsigned ints of size 1 bytes
for( int n=0; n < rows*cols ; n++ )
{
unsigned char temp;
fread( (char*) &temp, sizeof(unsigned char), 1 , fp );
(output.data[i])[j] = (double) temp;
i++;
if( i == rows )
{ i=0; j++; }
}
break;
default:
std::cout << "Error: Unknown format!" << std::endl;
break;
}
// read the imaginary part of the matrix (not supported!)
fclose( fp );
delete name;
return output;
}
FILE* read_matlab_header( int* rows, int* cols , std::string filename )
{
FILE* fp;
fp = fopen( filename.c_str() , "rb" );
if( fp == NULL )
{
std::cout << "Error: could not open file " << filename << "!" << std::endl;
return NULL;
}
typedef unsigned int UINT;
UINT UINTs = sizeof(UINT);
// read the basic header information
UINT temp;
fread( (char*) &temp , UINTs , 1 , fp );
UINT type_numeric_format = thousands(temp);
UINT type_reserved = hundreds(temp);
UINT type_data_format = tens(temp);
UINT type_matrix_type = ones(temp);
// make sure it's a matlab L4 file
if( type_numeric_format != 0 || // little-endian
type_reserved != 0 || // should always be 0
type_data_format > 5 || // unknown format
type_matrix_type != 0 ) // want full matrices, not sparse
{
std::cout << "Error reading file " << filename << ": I can't read this format yet!" << std::endl;
fclose( fp );
return NULL;
}
// get the size of the data
// UINT rows;
fread( (char*) rows , UINTs , 1, fp );
// UINT cols;
fread( (char*) cols, UINTs , 1 , fp );
// resize the output accordingly
// make sure we're not dealing with complex numbers
UINT imag;
fread( (char*) &imag, UINTs, 1 , fp );
if( imag != 0 )
{
std::cout << "Error: I can't read imaginary matrices yet!" << std::endl;
fclose( fp );
return NULL;
}
UINT name_length;
fread( (char*) &name_length, UINTs, 1 , fp );
char* name;
name = new char [name_length];
// this is the end of the 20-byte header
// read the name
fread( name , name_length , 1 , fp );
delete name;
return fp;
}
FILE* write_matlab4_header( int nrows, int ncols, std::string filename, std::string variable_name )
{
FILE* fp;
fp = fopen( filename.c_str() , "wb" );
if( fp == NULL )
{
std::cout << "Error: could not open file " << filename << "!" << std::endl;
return NULL;
}
typedef unsigned int UINT;
UINT UINTs = sizeof(UINT);
UINT temp;
UINT type_numeric_format = 0; // little-endian assumed for now!
UINT type_reserved = 0;
UINT type_data_format = 0; // doubles for all entries
UINT type_matrix_type = 0; // full matrix, not sparse
temp = 1000*type_numeric_format + 100*type_reserved + 10*type_data_format + type_matrix_type;
fwrite( (char*) &temp , UINTs , 1 , fp );
// UINT rows = (UINT) number_of_data_entries; // storing data as rows
UINT rows = (UINT) nrows; // size_of_each_datum; // storing data as cols
fwrite( (char*) &rows , UINTs , 1, fp );
// UINT cols = (UINT) size_of_each_datum; // storing data as rows
UINT cols = (UINT) ncols; // number_of_data_entries; // storing data as cols
fwrite( (char*) &cols, UINTs , 1 , fp );
UINT imag = 0; // no complex matrices!
fwrite( (char*) &imag, UINTs, 1 , fp );
UINT name_length = variable_name.size(); // strlen( variable_name );
fwrite( (char*) &name_length, UINTs, 1 , fp );
// this is the end of the 20-byte header
// write the name
fwrite( variable_name.c_str() , name_length , 1 , fp );
return fp;
}
FILE* write_matlab_header( int rows, int cols, std::string filename, std::string variable_name )
{
return write_matlab4_header( rows, cols, filename, variable_name );
}
bool write_matlab4( std::vector< std::vector<double> > input, std::string filename , std::string variable_name )
{
int number_of_data_entries = input.size();
int size_of_each_datum = input[0].size();
int rows = size_of_each_datum; // storing data as cols
int cols = number_of_data_entries; // storing data as cols
FILE* fp = write_matlab4_header( rows, cols , filename, variable_name );
// // storing data as rows
// for( int j=0; j < size_of_each_datum ; j++ )
// {
// for( int i=0; i < number_of_data_entries ; i++ )
// {
// fwrite( (char*) &(input[i])[j] , sizeof(double), 1 , fp );
// }
// }
// storing data as cols
for( int i=0; i < number_of_data_entries ; i++ )
{
for( int j=0; j < size_of_each_datum ; j++ )
{
fwrite( (char*) &(input[i])[j] , sizeof(double), 1 , fp );
}
}
fclose( fp );
return true;
}
bool write_matlab( std::vector< std::vector<double> >& input , std::string filename )
{
return write_matlab4( input, filename , "none" );
}
};