N-Neuron Network Coherence Code
Here's some code I wrote to take spiketimes and find the average coherence between each pair of neurons. Files must be named xvoltage_raster.out, and each line should start with the time of spike for the xth neuron. The directory containing the raster files should be in this format: <neurons>_<gsyn>_<iapp>_<tausyn>_<heterogeneity>. An example would be: /50_0.35_3.0_1.0_0.038/. The method is from White et al., 1998, and the epsilon/heterogeneity function comes from Skinner et al., 2005. This is a rough draft, but if I don't post it now I'll probably forget later. Click through for the code.
Warning: This code determines the window size by the fastest neuron in the network, not by the fastest of each respective pair. The literature isn't too clear, and I don't have experience with cross-correlation outside of this application. If you want the 'correct' code, contact me. Otherwise the concept is similar except for the computing of each pair separately.
-
#include <stdio.h>
-
#include <pthread.h>
-
#include <stdlib.h>
-
#include <math.h>
-
#include <unistd.h>
-
-
double eps2het(double iapp, double epsilon)
-
{
-
double het, iapp1, iapp2, intfreq1, intfreq2;
-
-
iapp1 = iapp - epsilon;
-
iapp2 = iapp + epsilon;
-
-
intfreq1 = (-0.112*pow(iapp1,4)) + (1.88*pow(iapp1,3)) - (13.1*pow(iapp1,2)) + (70.5*iapp1) + 0.258;
-
intfreq2 = (-0.112*pow(iapp2,4)) + (1.88*pow(iapp2,3)) - (13.1*pow(iapp2,2)) + (70.5*iapp2) + 0.258;
-
-
het = ((intfreq2 - intfreq1)/intfreq2);
-
-
return het;
-
}
-
-
double het2eps(double iapp, double hetero)
-
{
-
double testeps = 0.1;
-
double step = 0.000001;
-
while ( 1 )
-
{
-
double newhet = eps2het(iapp, testeps);
-
double hetdiff = newhet - hetero;
-
double hetdiffabs = fabs(hetdiff);
-
//printf("%lf\n",hetdiffabs);
-
-
if (hetdiffabs <step)
-
{
-
return testeps;
-
}
-
else if (hetdiff> 0)
-
testeps = testeps - step;
-
else if (hetdiff <0)
-
testeps = testeps + step;
-
}
-
}
-
-
double unitsquarewavearea(short *waveform, int length, double binsize)
-
{
-
double area = 0;
-
-
int i;
-
for (i = 0; i <length; i++)
-
{
-
if (waveform[i] == 1)
-
{
-
area = area + binsize;
-
}
-
}
-
-
return area;
-
}
-
-
void setrelativedir(char *reldir)
-
{
-
int i = 0;
-
int j = 0;
-
-
char cdir[200];
-
getcwd(cdir,sizeof(char) * 200);
-
-
for (i = 0; cdir[i]> 0; i++)
-
{
-
if (cdir[i] == '/')
-
{
-
reldir[j] = 0;
-
j = 0;
-
}
-
else
-
{
-
reldir[j] = cdir[i];
-
j++;
-
}
-
-
reldir[j] = 0;
-
}
-
}
-
-
void setvaluefromdir(char *dir, int *neurons, double *gsyn, double *iapp, double *tausyn, double *het)
-
{
-
int i = 0;
-
int j = 0;
-
int par = 0;
-
int done = 0;
-
-
double parval;
-
char *strparam = malloc(200);;
-
-
for (i = 0; done != 1 ; i++)
-
{
-
if (dir[i] == '\0' && i> 0 && par> 5)
-
{
-
done = 1;
-
}
-
-
if (dir[i] == '_' || (dir[i] == '\0' && i> 0))
-
{
-
char *endptr;
-
strparam[j] = 0;
-
-
parval = strtod(strparam, &endptr);
-
-
j = 0;
-
-
switch (par)
-
{
-
case 0:
-
*neurons = (int)parval;
-
break;
-
case 1:
-
*gsyn = parval;
-
break;
-
case 2:
-
*iapp = parval;
-
break;
-
case 3:
-
*tausyn = parval;
-
break;
-
case 4:
-
*het = parval;
-
break;
-
default:
-
break;
-
}
-
-
par++;
-
}
-
else
-
{
-
strparam[j] = dir[i];
-
j++;
-
}
-
}
-
-
free(strparam);
-
}
-
-
int main(int argc, char *argv[])
-
{
-
int neurons;
-
int *pneurons;
-
pneurons = &neurons;
-
-
int maxspikes, totalbins, leftshift;
-
int i, j, k;
-
-
double epsilon, stepsize, windowsize, starttime, endtime, timelength;
-
double itotal, fastfreq, periodms, window, halfwindow;
-
-
double gsyn, iapp, tausyn, het;
-
double *pgsyn = &gsyn, *piapp = &iapp, *ptausyn = &tausyn, *phet = &het;
-
-
char reldir[200];
-
setrelativedir(reldir);
-
-
//printf("reldir: %s\n", reldir);
-
-
setvaluefromdir(reldir, pneurons, pgsyn, piapp, ptausyn, phet);
-
-
epsilon = het2eps(iapp, het);
-
-
-
stepsize = 0.01;
-
windowsize = 0.2;
-
-
starttime = 7000.0;
-
endtime = 8000.0;
-
-
timelength = endtime - starttime;
-
-
totalbins = (int)ceil(timelength / stepsize);
-
//printf("bins: %d\n", totalbins);
-
-
itotal = iapp + epsilon;
-
-
fastfreq = -0.112*pow(itotal,4) + 1.88*pow(itotal,3) - 13.1*pow(itotal,2) + 70.5*itotal + 0.258;
-
-
periodms = 1000 / fastfreq;
-
//printf("periodms: %lf\n", periodms);
-
-
window = windowsize * periodms;
-
halfwindow = 0.5 * window;
-
leftshift = (int)ceil(halfwindow/stepsize);
-
//printf("leftshift: %d\n", leftshift);
-
-
FILE *vraster[neurons];
-
-
maxspikes = (int)ceil((timelength/periodms) * 2);
-
//printf("%d\n", maxspikes);
-
double spiketrain[neurons][maxspikes];
-
double area[neurons];
-
-
short *waveform = malloc(sizeof(short) * neurons * totalbins);
-
-
for (i = 0; i <neurons; i++)
-
{
-
char filename[42];
-
sprintf(filename, "%dvoltage_raster.out", i+1);
-
//printf("%d %s\n", i+1, filename);
-
vraster[i] = fopen(filename, "r");
-
-
j = 0;
-
double tempdouble;
-
while (fscanf(vraster[i], "%lf %d", &tempdouble, &k) == 2)
-
{
-
if (tempdouble> starttime && tempdouble <= endtime)
-
{
-
spiketrain[i][j] = tempdouble;
-
j++;
-
}
-
}
-
-
fclose(vraster[i]);
-
-
for (j = 0; j <totalbins; j++)
-
{
-
double spiketest = starttime + j*stepsize;
-
waveform[(i*totalbins)+j] = 0;
-
-
int m;
-
for (m = 0; m <maxspikes; m++)
-
{
-
if (spiketrain[i][m]> 0)
-
{
-
double timediff = fabs(spiketest - spiketrain[i][m]);
-
-
if (timediff <0.001)
-
{
-
int n;
-
for (n = 0; n <(leftshift*2); n++)
-
{
-
int postshift = (j - leftshift) + n;
-
if (postshift> 0 && postshift <totalbins)
-
{
-
waveform[(i*totalbins)+postshift] = 1;
-
}
-
}
-
j = j + leftshift - 1;
-
}
-
}
-
}
-
}
-
-
area[i] = unitsquarewavearea(waveform + i*totalbins, totalbins, stepsize);
-
}
-
-
short *product = malloc(sizeof(short) * totalbins);
-
double rhosum = 0;
-
int perms = 0;
-
int x, y, z;
-
for (x = (neurons - 2); x>= 0; x--)
-
{
-
for (y = (neurons - 1); y> x; y--)
-
{
-
for (z = 0; z <totalbins; z++)
-
{
-
product[z] = waveform[(x*totalbins)+z] * waveform[(y*totalbins)+z];
-
}
-
-
double productarea = unitsquarewavearea(product, totalbins, stepsize);
-
double rho = productarea / (sqrt(area[x]*area[y]));
-
-
rhosum = rhosum + rho;
-
-
perms++;
-
//printf("area_%d: %lf area_%d: %lf rho: %lf\n", x, area[x], y, area[y], rho);
-
}
-
}
-
-
double avgrho = rhosum / perms;
-
-
-
free(waveform);
-
free(product);
-
-
return 0;
-
}
http://www.brainbasedbusiness.com/2006/07/can_science_really_blueprint_t.html
I hate average rhos. I tend to go for the exceptional rhos, myself.
I JUST POSTED GARBAGE IN YOUR DISCUSSION FORUM!!
thats newbie code.
Yowtch! (O_o)~