-
Notifications
You must be signed in to change notification settings - Fork 0
/
glim.c
2780 lines (2422 loc) · 92.5 KB
/
glim.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
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ----------------------------- MNI Header -----------------------------------
@NAME : glim.c
@INPUT :
@OUTPUT : (nothing)
@RETURNS :
@DESCRIPTION: Source file for generalized linear models
@CREATED : Sept 11, 1997 (J. Taylor)
@MODIFIED :
---------------------------------------------------------------------------- */
#include "glim.h"
#include "volume_io.h"
extern FILE *tmpfile_list;
extern char *tmpfile_name;
extern int num_tmpfiles;
extern int verbose;
/* ----------------------------- MNI Header -----------------------------------
@NAME : delete_tmpfiles
@INPUT :
@OUTPUT :
@RETURNS : TRUE since nextarg is used.
@DESCRIPTION: deletes tmpfiles created by glim_image
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : May 18, 1998 (J. Taylor)
@MODIFIED :
---------------------------------------------------------------------------- */
void delete_tmpfiles(FILE **tmpfile_list)
{
char tmpstring[2056];
FILE *tmp;
int is_null;
fclose(*tmpfile_list);
if (num_tmpfiles > 0) {
*tmpfile_list = fopen(tmpfile_name, "r");
while(fscanf(*tmpfile_list, "%s", &tmpstring) == 1) {
is_null = TRUE;
tmp = fopen(tmpstring,"r");
if (tmp != NULL) {
is_null = FALSE;
fclose(tmp);
}
if (is_null == FALSE)
remove(tmpstring);
}
fclose(*tmpfile_list);
}
remove(tmpfile_name);
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : get_contrast_from_raw
@INPUT : Array of raw contrasts, pointer to final contrast matrices
@OUTPUT : (none)
@RETURNS : TRUE since nextarg is used.
@DESCRIPTION: Parses contrasts entered on command line to contrast matrices
@METHOD :
@GLOBALS :
@CALLS : get_contrast_from_matrix
@CREATED : June 21, 1997 (J. Taylor)
@MODIFIED :
---------------------------------------------------------------------------- */
int get_contrast_from_raw( Contrast_Raw_Array *contrast_list,
Contrast_Mat_Array **contrast_matrices)
{
Contrast_Raw *cur_raw;
Contrast_Matrix *cur_matrix;
Avg_Info *cur_avg;
int ifile, icont, iavg, tmp_len;
int itest;
char *tmp_name;
char *prefix = "glim";
char *tmp_dir = NULL;
iavg = 0;
icont = 0;
(*contrast_matrices)->num_avg = contrast_list->num_avg;
(*contrast_matrices)->num_contrasts = (contrast_list->num_output -
contrast_list->num_avg);
(*contrast_matrices)->num_test = 0;
(*contrast_matrices)->avg_array->num_test = 0;
if(contrast_list->num_avg > 0) {
(*contrast_matrices)->avg_array->avg_info = GI_MALLOC(sizeof(Avg_Info*) *
contrast_list->num_avg);
}
else
(*contrast_matrices)->avg_array = NULL;
for(ifile=0 ; ifile<contrast_list->num_output ; ifile++) {
/* Get pointer to contrast array */
cur_raw = contrast_list->contrast_array[ifile];
if(cur_raw->is_avg == FALSE) {
cur_matrix = GI_MALLOC(sizeof(Contrast_Matrix));
cur_matrix->outfile = cur_raw->outfile_name;
cur_matrix->out_id = ifile;
if(cur_raw->out_type == NULL) {
cur_matrix->out_mode = F_STAT;
(*contrast_matrices)->num_test++;
}
else if (strcmp(cur_raw->out_type,"stdev") == 0)
cur_matrix->out_mode = STDEV_BETA;
else if(strcmp(cur_raw->out_type,"beta") == 0)
cur_matrix->out_mode = BETA_HAT;
else if(strcmp(cur_raw->out_type,"t_stat") == 0) {
cur_matrix->out_mode = T_STAT;
(*contrast_matrices)->num_test++;
}
else if(strcmp(cur_raw->out_type,"corr") == 0) {
cur_matrix->out_mode = P_CORR;
(*contrast_matrices)->num_test++;
}
else {
fprintf(stderr,"\nError in get_contrast_from_raw.\n");
fprintf(stderr,"\n%s is not an output type.\n",cur_raw->out_type);
delete_tmpfiles(&tmpfile_list);
exit(EXIT_FAILURE);
}
GI_FREE(cur_raw->out_type);
if(strcmp(cur_raw->stdev_type,"voxel") == 0)
cur_matrix->stdev_mode = VOXEL_SD;
else if(strcmp(cur_raw->stdev_type,"pool") == 0)
cur_matrix->stdev_mode = POOLED_SD;
else {
fprintf(stderr,"\nError in get_contrast_from_raw.\n");
fprintf(stderr,"\n%s is not an stdev type.\n",
cur_raw->stdev_type);
delete_tmpfiles(&tmpfile_list);
exit(EXIT_FAILURE);
}
if ((cur_matrix->stdev_mode == POOLED_SD) &&
(cur_matrix->out_mode == BETA_HAT)) {
fprintf(stderr,"\nWarning: using pooled stdev for output of beta_hat for file %s.\nBeta_hat output option can't use pooled stdev. Pool option ignored.\n", cur_matrix->outfile);
cur_matrix->stdev_mode = VOXEL_SD;
}
GI_FREE(cur_raw->stdev_type);
if ((cur_matrix->stdev_mode == POOLED_SD) &&
(cur_matrix->out_mode == STDEV_BETA)) {
fprintf(stderr,"\nWarning: using pooled stdev for output of stdev for file %s.\nStdev output option can't use pooled stdev. Pool option ignored.\n", cur_matrix->outfile);
cur_matrix->stdev_mode = VOXEL_SD;
}
if (cur_matrix->stdev_mode == VOXEL_SD)
cur_matrix->tmpfile = NULL;
else if (cur_matrix->stdev_mode == POOLED_SD) {
tmp_name = tempnam(tmp_dir, prefix);
fprintf(tmpfile_list, "%s\n", tmp_name);
num_tmpfiles++;
tmp_len = strlen(tmp_name);
cur_matrix->tmpfile = GI_MALLOC(sizeof(char) * (tmp_len + 1));
if (strcpy(cur_matrix->tmpfile, tmp_name) == NULL) {
fprintf(stderr,"\nError in get_contrast_from_raw.\n");
fprintf(stderr,"Can't copy tmp_name to outfile.\n");
delete_tmpfiles(&tmpfile_list);
exit(EXIT_FAILURE);
}
}
if(strcmp(cur_raw->in_type,"matrix") == 0) {
if(get_contrast_from_matrix(cur_raw->raw_contrast ,
(*contrast_matrices)->num_columns,
&cur_matrix) != TRUE) {
fprintf(stderr,"\nError getting contrast matrix for outfile %s, type %s.\n", cur_matrix->outfile, cur_raw->in_type);
delete_tmpfiles(&tmpfile_list);
exit(EXIT_FAILURE);
}
}
else if(strcmp(cur_raw->in_type,"file") == 0) {
if(get_contrast_from_file(cur_raw->raw_contrast,
(*contrast_matrices)->num_columns,
&cur_matrix) != TRUE) {
fprintf(stderr,"\nError getting contrast matrix for outfile %s, type %s.\n", cur_matrix->outfile, cur_raw->in_type);
delete_tmpfiles(&tmpfile_list);
exit(EXIT_FAILURE);
}
}
else if(strcmp(cur_raw->in_type,"column") == 0) {
if(get_contrast_from_column(cur_raw->raw_contrast,
(*contrast_matrices)->num_columns,
&cur_matrix) != TRUE) {
fprintf(stderr,"\nError getting contrast matrix for outfile %s, type %s.\n", cur_matrix->outfile, cur_raw->in_type);
delete_tmpfiles(&tmpfile_list);
exit(EXIT_FAILURE);
}
}
else {
fprintf(stderr,"\nError in get_contrast_from_raw.");
fprintf(stderr,"\n%s isn't a proper input method.\n",
cur_raw->in_type);
delete_tmpfiles(&tmpfile_list);
exit(EXIT_FAILURE);
}
(*contrast_matrices)->contrast_matrix_array[icont] = cur_matrix;
icont++;
cur_matrix++;
GI_FREE(cur_raw->in_type);
}
else {
cur_avg = GI_MALLOC(sizeof(Avg_Info));
cur_avg->outfile = cur_raw->outfile_name;
cur_avg->out_id = ifile;
if(cur_raw->out_type == NULL) {
cur_avg->out_mode = F_STAT;
(*contrast_matrices)->avg_array->num_test++;
}
else if (strcmp(cur_raw->out_type,"stdev") == 0)
cur_avg->out_mode = STDEV_BETA;
else if(strcmp(cur_raw->out_type,"beta") == 0)
cur_avg->out_mode = BETA_HAT;
else if(strcmp(cur_raw->out_type,"t_stat") == 0) {
cur_avg->out_mode = T_STAT;
(*contrast_matrices)->avg_array->num_test++;
}
else if(strcmp(cur_raw->out_type,"corr") == 0) {
cur_avg->out_mode = P_CORR;
(*contrast_matrices)->avg_array->num_test++;
}
else {
fprintf(stderr,"\nError in get_contrast_from_raw.\n");
fprintf(stderr,"\n%s is not an output type.\n",cur_raw->out_type);
delete_tmpfiles(&tmpfile_list);
exit(EXIT_FAILURE);
}
GI_FREE(cur_raw->out_type);
if(strcmp(cur_raw->stdev_type,"voxel") == 0)
cur_avg->stdev_mode = VOXEL_SD;
else if(strcmp(cur_raw->stdev_type,"pool") == 0)
cur_avg->stdev_mode = POOLED_SD;
else {
fprintf(stderr,"\nError in get_contrast_from_raw.\n");
fprintf(stderr,"\n%s is not an stdev type.\n",
cur_raw->stdev_type);
delete_tmpfiles(&tmpfile_list);
exit(EXIT_FAILURE);
}
if ((cur_avg->stdev_mode == POOLED_SD) &&
(cur_avg->out_mode == BETA_HAT)) {
fprintf(stderr,"\nWarning: using pooled stdev for output of beta_hat for file %s.\nBeta_hat output option can't use pooled stdev. Pool option ignored.\n", cur_avg->outfile);
cur_avg->stdev_mode = VOXEL_SD;
}
GI_FREE(cur_raw->stdev_type);
if ((cur_avg->stdev_mode == POOLED_SD) &&
(cur_avg->out_mode == STDEV_BETA)) {
fprintf(stderr,"\nWarning: using pooled stdev for output of stdev for file %s.\nStdev output option can't use pooled stdev. Pool option ignored.\n", cur_avg->outfile);
cur_avg->stdev_mode = VOXEL_SD;
}
if (cur_avg->stdev_mode == VOXEL_SD)
cur_avg->tmpfile = NULL;
else if (cur_avg->stdev_mode == POOLED_SD) {
tmp_name = tempnam(tmp_dir, prefix);
fprintf(tmpfile_list, "%s\n", tmp_name);
num_tmpfiles++;
tmp_len = strlen(tmp_name);
cur_avg->tmpfile = GI_MALLOC(sizeof(char) * (tmp_len + 1));
if (strcpy(cur_avg->tmpfile, tmp_name) == NULL) {
fprintf(stderr,"\nError in get_contrast_from_raw.\n");
fprintf(stderr,"Can't copy tmp_name to outfile.\n");
delete_tmpfiles(&tmpfile_list);
exit(EXIT_FAILURE);
}
}
(*contrast_matrices)->avg_array->avg_info[iavg] = cur_avg;
iavg++;
cur_avg++;
}
}
/* Get the mapping of all the contrasts that are test-statistics */
(*contrast_matrices)->test_map = GI_MALLOC((*contrast_matrices)->num_test
* sizeof(int));
itest = 0;
for(icont=0 ; icont<(*contrast_matrices)->num_contrasts ; icont++) {
cur_matrix = (*contrast_matrices)->contrast_matrix_array[icont];
if ((cur_matrix->out_mode == T_STAT) ||
(cur_matrix->out_mode == F_STAT)){
(*contrast_matrices)->test_map[itest] = icont;
itest++;
}
}
return TRUE;
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : get_contrast_from_matrix
@INPUT : raw contrast string,
num_columns
pointer to current contrast matrix in get_contrast_from_raw
@OUTPUT : (none)
@RETURNS :
@DESCRIPTION: gets a contrast matrix entered in the form "matrix"
(i.e. '0 0 0 1 0 ; 0 1 0 0 0' etc.)
@METHOD :
@GLOBALS :
@CALLS : get_double_list, create_matrix
@CREATED : June 22, 1997 (J. Taylor)
@MODIFIED :
---------------------------------------------------------------------------- */
int get_contrast_from_matrix(char *contrast_string, int num_columns,
Contrast_Matrix **cur_matrix)
{
int num_rows;
Double_Array *converted_values = NULL;
double value;
int i,j;
char *cur, *end;
num_rows = 1;
/* Get number of rows of current contrast */
cur = contrast_string;
end = contrast_string + strlen(contrast_string);
while(cur != end) {
if(*cur == ROW_SEPARATOR)
num_rows++;
cur++;
}
/* Initialize current matrix */
(*cur_matrix)->contrast_matrix = create_matrix(num_rows, num_columns);
get_double_list(contrast_string, &converted_values);
if (converted_values->num_values != (num_columns * num_rows)) {
fprintf(stderr,"\nMismatched number of elements for contrast %s \n", (*cur_matrix)->outfile);
return FALSE;
}
for(i=0; i<num_rows; i++) {
for(j=0 ; j<num_columns; j++) {
value = converted_values->values[i*num_columns+j];
(*cur_matrix)->contrast_matrix->values[i][j] = value;
}
}
return TRUE;
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : get_contrast_from_column
@INPUT : raw contrast string,
num_columns
pointer to current contrast matrix in get_contrast_from_raw
@OUTPUT : (none)
@RETURNS :
@DESCRIPTION: gets a contrast matrix entered in the form "column"
where the contrast outputs a t-statistic testing the hypothesis
glm_obj->beta_hat->values[column-1][0] == 0
@METHOD :
@GLOBALS :
@CALLS : create_matrix
@CREATED : June 22, 1997 (J. Taylor)
@MODIFIED :
---------------------------------------------------------------------------- */
int get_contrast_from_column(char *contrast_column, int num_columns,
Contrast_Matrix **cur_matrix)
{
int column;
char **ptr = NULL;
column = strtol(contrast_column, ptr, 10);
if(column > num_columns) {
fprintf(stderr,"\nError in get_contrast_from_column: column number too large.\n");
printf( "column = %d num_columns = %d\n", column, num_columns );
return FALSE;
}
(*cur_matrix)->contrast_matrix = NULL;
(*cur_matrix)->column_num = column - 1;
return TRUE;
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : get_contrast_from_file
@INPUT : raw contrast string,
num_columns
pointer to current contrast matrix in get_contrast_from_raw
@OUTPUT : (none)
@RETURNS :
@DESCRIPTION: gets a contrast matrix entered in the form "file"
a file containing a contrast matrix with the appropriate number
of rows
@METHOD :
@GLOBALS :
@CALLS : create_matrix
@CREATED : June 22, 1997 (J. Taylor)
@MODIFIED :
---------------------------------------------------------------------------- */
int get_contrast_from_file(char *contrast_filename, int num_columns,
Contrast_Matrix **cur_matrix)
{
FILE *contrast_file;
int num_rows;
int i,j,k;
int initial_rows = 10;
double **values;
double value;
contrast_file = fopen(contrast_filename, "r");
if (contrast_file == NULL) {
fprintf(stderr,"\nError: can't find file %s.\n", contrast_filename);
return FALSE;
}
values = GI_MALLOC(sizeof(double *) * initial_rows);
for(i=0; i<initial_rows; i++)
values[i] = GI_MALLOC(sizeof(double) * num_columns);
i = 0;
num_rows = 0;
while(fscanf(contrast_file, "%lf", &(values[i][0])) == 1) {
j=1;
while(fscanf(contrast_file, "%lf", &(values[i][j])) == 1) {
j++;
}
if(j != num_columns) {
fprintf(stderr,"\nError: mismatched number of entries in line %d of %s.\n", i, contrast_filename);
return FALSE;
}
i++;
num_rows++;
if(num_rows == (initial_rows - 1)) {
initial_rows = initial_rows + 50;
GI_REALLOC(values, sizeof(double *) * initial_rows);
for(k=num_rows; k<initial_rows; k++) {
values[k] = GI_MALLOC(sizeof(double) * num_columns);
}
}
}
(*cur_matrix)->contrast_matrix = create_matrix(num_rows,num_columns);
for(i=0; i<num_rows; i++) {
for(j=0; j<num_columns; j++) {
value = values[i][j];
(*cur_matrix)->contrast_matrix->values[i][j] = value;
}
}
return TRUE;
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : get_double_list
@INPUT : double_string - string to be converted
double_array - destination of string
@OUTPUT : (none)
@RETURNS : TRUE since nextarg is used.
@DESCRIPTION: Gets a list (array) of double values.
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : March 8, 1995 (Peter Neelin)
@MODIFIED : June 22, 1997 (J. Taylor) changed function so it could take
input other than from ParseArgv
---------------------------------------------------------------------------- */
int get_double_list(char *double_string, Double_Array **double_array)
{
int num_elements;
int num_alloc;
double *double_list = NULL;
double dvalue;
char *cur, *end, *prev;
/* Set up pointers to end of string and first non-space character */
end = double_string + strlen(double_string);
cur = double_string;
while (isspace(*cur)) cur++;
num_elements = 0;
num_alloc = 0;
double_list = NULL;
/* Loop through string looking for doubles */
while (cur!=end) {
/* Get double */
prev = cur;
dvalue = strtod(prev, &cur);
if(prev==cur) {
(void) fprintf(stderr,
"Error: expected vector of doubles for but got \"%s\"\n",
double_string);
return FALSE;
}
/* Add the value to the list */
num_elements++;
if (num_elements > num_alloc) {
num_alloc += 20;
if (double_list == NULL) {
double_list =
GI_MALLOC(num_alloc * sizeof(*double_list));
}
else {
double_list =
GI_REALLOC(double_list, num_alloc * sizeof(*double_list));
}
}
double_list[num_elements-1] = dvalue;
/* Skip any spaces */
while (isspace(*cur)) cur++;
/* Skip row separator and an optional comma */
if (*cur == ENTRY_SEPARATOR) cur++;
if (*cur == ROW_SEPARATOR) cur++;
}
if((*double_array) == NULL) {
(*double_array) = GI_MALLOC(sizeof(Double_Array *));
(*double_array)->values = GI_MALLOC(sizeof(double) * num_elements);
}
/* Update the global variables */
(*double_array)->num_values = num_elements;
if ((*double_array)->values != NULL) {
GI_FREE((*double_array)->values);
}
(*double_array)->values = double_list;
return TRUE;
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : delete_glm_matrices
@INPUT : pointer to a Glm_Object
@OUTPUT : (none)
@RETURNS :
@DESCRIPTION: deletes matrices of glm_obj
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : June 22, 1997 (J. Taylor)
@MODIFIED :
---------------------------------------------------------------------------- */
void delete_glm_matrices(Glm_Object *glm_obj, int is_last)
{
int i,j;
Contrast_Matrix *cur_contrast;
if(glm_obj->control->do_fit == TRUE) {
if(glm_obj->family == GAUSSIAN) {
delete_matrix(glm_obj->info_inv);
delete_matrix(glm_obj->lm_matrix);
delete_matrix(glm_obj->response);
delete_matrix(glm_obj->mu);
delete_matrix(glm_obj->beta_hat);
delete_matrix(glm_obj->output);
delete_matrix(glm_obj->missing);
}
else if (glm_obj->family != GAUSSIAN) {
delete_matrix(glm_obj->info_inv);
delete_matrix(glm_obj->beta_hat);
delete_matrix(glm_obj->lm_matrix);
delete_matrix(glm_obj->mu);
delete_matrix(glm_obj->response);
delete_matrix(glm_obj->z_i);
delete_matrix(glm_obj->x_beta);
delete_matrix(glm_obj->output);
delete_matrix(glm_obj->g_prime);
delete_matrix(glm_obj->info_mat);
delete_matrix(glm_obj->sqrt_weight);
delete_matrix(glm_obj->missing);
if (glm_obj->corr_struct == DIAGONAL_CORR) {
delete_matrix(glm_obj->weight);
}
else {
delete_matrix(glm_obj->sigma);
}
}
if (is_last == TRUE) {
for(j=0; j<glm_obj->contrast->num_contrasts; j++) {
cur_contrast = glm_obj->contrast->contrast_matrix_array[j];
if(cur_contrast->contrast_matrix != NULL) {
for(i=0; i<6; i++) {
delete_matrix(glm_obj->tmp_con[j][i]);
}
delete_matrix(cur_contrast->contrast_matrix);
}
}
}
}
return;
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : initialize_glm
@INPUT : pointer to Glm_Object
@OUTPUT : (none)
@RETURNS :
@DESCRIPTION: allocates memory for all matrices needed for fitting of glm and
contrasts
@METHOD :
@GLOBALS : uses family_input, link_input, variance_function_input
@CALLS : create_contrast
@CREATED : June 22, 1997 (J. Taylor)
@MODIFIED :
---------------------------------------------------------------------------- */
void initialize_glm(Glm_Object *glm_obj, Initial_Data *initial_data)
{
int num_rows, num_columns;
glm_obj->pooled_dev = 0.0;
glm_obj->avg_pooled_dev = 0.0;
glm_obj->num_pool = 0;
glm_obj->avg_num_pool = 0;
glm_obj->design_matrix = initial_data->design_matrix;
glm_obj->control = initial_data->control;
glm_obj->scale_est = initial_data->scale_est;
num_rows = glm_obj->design_matrix->num_rows;
num_columns = glm_obj->design_matrix->num_columns;
glm_obj->deg_free = 1.0 * (num_rows - num_columns);
glm_obj->num_var = num_columns;
glm_obj->response = create_matrix(num_rows, 1);
glm_obj->resid = create_matrix(num_rows, 1);
glm_obj->missing = create_matrix(num_rows, 1);
if(glm_obj->deg_free < 1) {
fprintf(stderr,"Error: overspecified model, insufficient degrees of freedom\n");
delete_tmpfiles(&tmpfile_list);
exit(EXIT_FAILURE);
}
glm_obj->contrast = initial_data->contrast;
if(glm_obj->contrast->avg_array != NULL) {
if(glm_obj->contrast->avg_array->num_test > 0)
glm_obj->avg_resid = create_matrix(num_rows,
glm_obj->response->num_columns);
}
/* Get family information */
glm_obj->family = initial_data->family;
if ((glm_obj->family != QUASI)
&& (initial_data->variance_function != VARIANCE_DEFAULT)) {
fprintf(stderr, "\nError: If family type is not QUASI, variance_function is implied\n");
glm_obj->family = QUASI;
}
/* Set up links and variances */
/* Check to see if the link is the canonical link */
if (initial_data->link == LINK_DEFAULT)
glm_obj->control->is_canonical = TRUE;
else
glm_obj->control->is_canonical = FALSE;
switch(glm_obj->family) {
case BINOMIAL:
if(glm_obj->control->est_scale == FALSE) {
glm_obj->control->is_scale = TRUE;
glm_obj->scale_est = 1.0 / glm_obj->control->binomial_n;
glm_obj->pearson = glm_obj->scale_est / glm_obj->deg_free;
}
else
glm_obj->control->is_scale = FALSE;
glm_obj->variance_function = MU_ONE_MINUS_MU;
switch(initial_data->link) {
case LINK_DEFAULT:
glm_obj->link = LOGIT;
break;
case LOGIT:
glm_obj->link = LOGIT;
glm_obj->control->is_canonical = TRUE;
break;
case PROBIT:
glm_obj->link = PROBIT;
break;
case CLOGLOG:
glm_obj->link = CLOGLOG;
break;
default:
fprintf(stderr, "\nError: Improper choice of link function \n");
fprintf(stderr, "\n For BINOMIAL, choices are: CLOGLOG, LOGIT, PROBIT\n");
delete_tmpfiles(&tmpfile_list);
exit(EXIT_FAILURE);
break;
}
if(initial_data->corr_struct == CORR_DEFAULT)
glm_obj->corr_struct = DIAGONAL_CORR;
break;
case GAMMA:
glm_obj->variance_function = MU_SQUARED;
switch(initial_data->link) {
case LINK_DEFAULT:
glm_obj->link = INVERSE;
break;
case INVERSE:
glm_obj->link = INVERSE;
glm_obj->control->is_canonical = TRUE;
break;
case IDENTITY:
glm_obj->link = IDENTITY;
break;
case LOG:
glm_obj->link = LOG;
break;
default:
fprintf(stderr, "\nError: Improper choice of link function \n");
fprintf(stderr, "\n For GAMMA, choices are: IDENTITY, INVERSE, LOG\n");
delete_tmpfiles(&tmpfile_list);
exit(EXIT_FAILURE);
break;
}
if(initial_data->corr_struct == CORR_DEFAULT)
glm_obj->corr_struct = DIAGONAL_CORR;
break;
case EXPONENTIAL:
glm_obj->variance_function = MU_SQUARED;
switch(initial_data->link) {
case LINK_DEFAULT:
glm_obj->link = LOG;
break;
case LOG:
glm_obj->link = LOG;
break;
default:
fprintf(stderr, "\nError: Improper choice of link function \n");
fprintf(stderr, "\n For EXPONENTIAL, link function is LOG.\n");
delete_tmpfiles(&tmpfile_list);
exit(EXIT_FAILURE);
break;
}
glm_obj->scale_est = 1.0;
glm_obj->pearson = glm_obj->scale_est / glm_obj->deg_free;
if(initial_data->corr_struct == CORR_DEFAULT)
glm_obj->corr_struct = DIAGONAL_CORR;
break;
case CHI_SQ:
glm_obj->variance_function = MU_SQUARED;
switch(initial_data->link) {
case LINK_DEFAULT:
glm_obj->link = INVERSE;
break;
case INVERSE:
glm_obj->link = INVERSE;
glm_obj->control->is_canonical = TRUE;
break;
default:
fprintf(stderr, "\nError: Improper choice of link function \n");
fprintf(stderr, "\n For CHI_SQ, link function is INVERSE.\n");
delete_tmpfiles(&tmpfile_list);
exit(EXIT_FAILURE);
break;
}
if(initial_data->corr_struct == CORR_DEFAULT)
glm_obj->corr_struct = DIAGONAL_CORR;
break;
case GAUSSIAN:
glm_obj->variance_function = CONST;
switch(initial_data->link) {
case LINK_DEFAULT:
glm_obj->link = IDENTITY;
break;
case IDENTITY:
glm_obj->link = IDENTITY;
glm_obj->control->is_canonical = TRUE;
break;
default:
fprintf(stderr, "\nError: Improper choice of link function \n");
fprintf(stderr, "\nError: For GAUSSIAN, choices are: IDENTITY\n");
delete_tmpfiles(&tmpfile_list);
exit(EXIT_FAILURE);
break;
}
if(initial_data->corr_struct == CORR_DEFAULT)
glm_obj->corr_struct = DIAGONAL_CORR;
break;
case INV_GAUSS:
glm_obj->variance_function = MU_CUBED;
switch(initial_data->link) {
case LINK_DEFAULT:
glm_obj->link = INVERSE_SQ;
break;
case INVERSE_SQ:
glm_obj->link = INVERSE_SQ;
glm_obj->control->is_canonical = TRUE;
break;
default:
fprintf(stderr, "\nError: Improper choice of link function \n");
fprintf(stderr, "\nError: For INV_GAUSS, choices are: INVERSE_SQ\n");
delete_tmpfiles(&tmpfile_list);
exit(EXIT_FAILURE);
break;
}
if(initial_data->corr_struct == CORR_DEFAULT)
glm_obj->corr_struct = DIAGONAL_CORR;
break;
case POISSON:
if(glm_obj->control->est_scale == FALSE) {
glm_obj->control->is_scale = TRUE;
glm_obj->scale_est = 1.0;
glm_obj->pearson = glm_obj->scale_est / glm_obj->deg_free;
}
else
glm_obj->control->is_scale = FALSE;
glm_obj->variance_function = MU;
switch(initial_data->link) {
case LINK_DEFAULT:
glm_obj->link = LOG;
break;
case LOG:
glm_obj->link = LOG;
glm_obj->control->is_canonical = TRUE;
break;
case IDENTITY:
glm_obj->link = IDENTITY;
break;
case SQRT:
glm_obj->link = SQRT;
break;
default:
fprintf(stderr, "\nError: Improper choice of link function \n");
fprintf(stderr, "\nError: For POISSON, choices are: IDENTITY, LOG, SQRT\n");
delete_tmpfiles(&tmpfile_list);
exit(EXIT_FAILURE);
break;
}
if(initial_data->corr_struct == CORR_DEFAULT)
glm_obj->corr_struct = DIAGONAL_CORR;
break;
case QUASI:
glm_obj->variance_function = initial_data->variance_function;
glm_obj->link = initial_data->link;
if(initial_data->corr_struct == CORR_DEFAULT)
glm_obj->corr_struct = DIAGONAL_CORR;
break;
}
if(glm_obj->corr_struct != DIAGONAL_CORR)
glm_obj->family = QUASI;
/* Create necessary matrices */
if(glm_obj->control->do_fit == TRUE) {
if (glm_obj->family == GAUSSIAN) {
glm_obj->beta_hat = create_matrix(glm_obj->design_matrix->num_columns,
glm_obj->response->num_columns);
glm_obj->lm_matrix = create_matrix(num_columns, num_rows);
glm_obj->mu = create_matrix(num_rows, glm_obj->response->num_columns);
glm_obj->info_inv = create_matrix(num_columns, num_columns);
glm_obj->output = create_matrix(initial_data->contrast->num_contrasts,
1);
glm_obj->x_beta = NULL;
glm_obj->z_i = NULL;
glm_obj->g_prime = NULL;
glm_obj->info_mat = NULL;
glm_obj->sqrt_weight = NULL;
}
else {
glm_obj->lm_matrix = create_matrix(num_columns, num_rows);
glm_obj->beta_hat = create_matrix(glm_obj->design_matrix->num_columns,
glm_obj->response->num_columns);
glm_obj->mu = create_matrix(num_rows, glm_obj->response->num_columns);
glm_obj->info_inv = create_matrix(num_columns, num_columns);
glm_obj->x_beta = create_matrix(num_rows,
glm_obj->response->num_columns);
glm_obj->z_i = create_matrix(num_rows, 1);
glm_obj->output = create_matrix(initial_data->contrast->num_contrasts,
1);
glm_obj->g_prime = create_matrix(num_rows,
glm_obj->response->num_columns);
glm_obj->info_mat = create_matrix(num_columns + 1, num_columns + 1);
if(initial_data->corr_struct == CORR_DEFAULT)
glm_obj->corr_struct = DIAGONAL_CORR;
else
glm_obj->corr_struct = initial_data->corr_struct;
if(glm_obj->corr_struct != DIAGONAL_CORR) {
glm_obj->sigma = create_matrix(num_rows, num_rows);
glm_obj->weight= create_matrix(num_rows, glm_obj->response->num_columns);
}
else if(glm_obj->corr_struct == DIAGONAL_CORR) {
glm_obj->sigma = create_matrix(num_rows, glm_obj->response->num_columns);
glm_obj->weight= create_matrix(num_rows, glm_obj->response->num_columns);
}
glm_obj->sqrt_weight = create_matrix(num_rows,
glm_obj->response->num_columns);
}
/* create tmp_matrices */
create_contrast(glm_obj);
}
return;
}
/* ----------------------------- MNI Header -----------------------------------
@NAME : create_contrast
@INPUT : pointer to Glm_Object
@OUTPUT : (none)
@RETURNS : void
@DESCRIPTION: creates matrices needed for calculating contrasts
@METHOD :
@GLOBALS :
@CALLS :
@CREATED : June 22, 1997 (J. Taylor)
@MODIFIED :
---------------------------------------------------------------------------- */
void create_contrast(Glm_Object *glm_obj)
{
int icont, num_rows, need_tmp_con;
Contrast_Matrix *cur_contrast;
need_tmp_con = 0;
glm_obj->tmp_con = GI_MALLOC(sizeof(Matrix **) *
glm_obj->contrast->num_contrasts);
for(icont=0; icont < glm_obj->contrast->num_contrasts; icont++) {
cur_contrast = glm_obj->contrast->contrast_matrix_array[icont];
if (glm_obj->family != GAUSSIAN)
cur_contrast->deriv = create_matrix(glm_obj->response->num_rows, 1);
if(cur_contrast->contrast_matrix != NULL) {
need_tmp_con++;
glm_obj->tmp_con[icont] = GI_MALLOC(sizeof(Matrix *) * 6);
num_rows = cur_contrast->contrast_matrix->num_rows;
glm_obj->tmp_con[icont][0]=create_matrix(num_rows,
glm_obj->response->num_columns);
glm_obj->tmp_con[icont][1]=create_matrix(glm_obj->design_matrix->num_columns, num_rows);