forked from tcama/olfactoryRSN
-
Notifications
You must be signed in to change notification settings - Fork 1
/
csv_read.c
176 lines (149 loc) · 5.08 KB
/
csv_read.c
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
/*******************************************************************************
*
* This routine imports roi data from csv file format and calculates the
* Pearsons Correlation coefficient for all roi combinations. These values are
* written to a NxN csv file (where N is the number of rois).
*
* The roi matrix is also transposed as the csv file from nifti_simu.c outputs
* the data in a timepoint x roi orientation as opposed to roi x timepoint.
*
* Compile with: cc -o csv_read csv_read.c -lm
* Execute with: ./csv_read filename.csv
*
******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <time.h>
main(argc,argv)
int argc;
char *argv[];
{
FILE *f,*fp;
char *csv;
char delim;
csv = argv[1]; //set filename equal to csv
int i, j, val;
float *data[627];
for (i=0; i<627; i++){
data[i] = (float *)malloc(2400 * sizeof(float));
}
float temp;
// start timer
/*** set a time parameter ***/
time_t tstart, tend;
tstart = time(0);
// open csv file
f = fopen(csv,"r");
// used to index matrix, start at zero
i=0;
j=0;
int n=0;
/***************************************************************************
*
* The data must be read in from a .csv file and then transposed. The
* output from nifti_simu.c is in 1200x601 (the last column is zeros). This
* needs to be transposed into a 605(rois) x 1200(timepoints) matrix. The
* result of this will be called "data"
*
***************************************************************************/
//fscanf(f,"%i",&temp);
while(val = fscanf(f,"%f%c",&temp,&delim)){
if(i <= 626){
// is int value, put in array
data[i][j] = temp;
//if(i<=1){
//data[i][j] = temp;}
//fprintf(stderr,"\ni=%i j=%i\n",i,j); // used to check proper i,j output
if(i>=626){
i=0;
j++;
if(j>=2400){break;}
}else{
i++;
}
}else{
break;
}
}
/***************************************************************************
*
* This section can be used to verify output file is properly transposed
*
*
// open csv file to write
fp = fopen("transpose.csv","w");
for(i=0; i<605; i++){
for(j=0; j<1200; j++){
fprintf(fp,"%i,",data[i][j]);
}
fprintf(fp,"\n",data[i][j]);
}
***************************************************************************/
// close csv file to write
fclose(f);
/***************************************************************************
*
* This section beneath is for calculating the correlation matrix, also
* known as the functional connectivity map (fcMap). Each roi is correlated
* with each other roi producing an NxN matrix where N is the number of
* rois.
*
***************************************************************************/
int k;
float x[2400], y[2400], xy[2400], xsquare[2400], ysquare[2400];
float xsum, ysum, xysum, xsqr_sum, ysqr_sum;
long double num, deno;
long double coeff;
// open file for writing fcMap to
fp = fopen("fcMap.csv","w");
// loop through each combination of i,j from 1 to N
for(k=0; k<627; k++){
for(j=0; j<627; j++){
xsum = ysum = xysum = xsqr_sum = ysqr_sum = 0;
// set X and Y
for(n=0; n<2400; n++){
x[n]=data[k][n];
}
for(n=0; n<2400; n++){
y[n]=data[j][n];
}
/* find the needed data to manipulate correlation coeff */
for (i = 0; i < 2400; i++) {
xy[i] = x[i] * y[i];
xsquare[i] = x[i] * x[i];
ysquare[i] = y[i] * y[i];
xsum += x[i];
ysum = ysum + y[i];
xysum = xysum + xy[i];
xsqr_sum = xsqr_sum + xsquare[i];
ysqr_sum = ysqr_sum + ysquare[i];
}
// calculate numerator and denominator
/** 1 formerly 1.0 for float type **/
num = 1 * ((2400 * xysum) - (xsum * ysum));
deno = 1 * ((2400 * xsqr_sum - xsum * xsum) * (2400 * ysqr_sum - ysum * ysum));
// calcualte correlation coefficient [ num/sqrt(deno) ]
coeff = num / sqrt(deno);
if (k==1 && j==0){
fprintf(stderr, "\nxsum = %f\n",xsum);
fprintf(stderr, "\nysum = %f\n",ysum);
fprintf(stderr, "\nxysum = %f\n",xysum);
fprintf(stderr, "\nxsqr_sum = %f\n",xsqr_sum);
fprintf(stderr, "\nysqr_sum = %f\n",ysqr_sum);
fprintf(stderr, "\nnum = %Lf\n",num);
fprintf(stderr, "\ndeno = %Lf\n",deno);
fprintf(stderr, "\ncoeff = %Lf\n",coeff);
}
// put corr coef in fcMap
fprintf(fp,"%Lf,",coeff);
}
fprintf(fp,"\n");
}
fclose(fp);
/*** call back time parameter ***/
tend = time(0);
fprintf(stderr, "\nIt took %.3f second(s) to run this code.\n",difftime(tend, tstart));
return 0;
}