~ubuntu-branches/ubuntu/utopic/mricron/utopic

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
unit roc;
interface
//demonstrates the ROC tests that are in the Brunner.pas file
uses
  define_types,SysUtils,
part,StatThds,statcr,StatThdsUtil,Brunner,//Brunner,nifti_img, DISTR
   Messages,  Classes, Graphics, Controls, Forms, Dialogs,
StdCtrls,  ComCtrls,ExtCtrls,Menus, //overlap,ReadInt,stats,LesionStatThds,nifti_hdr,

{$IFDEF FPC} LResources,gzio2,
{$ELSE} gziod,associate,{$ENDIF}   //must be in search path, e.g. C:\pas\mricron\npm\math
{$IFNDEF UNIX} Windows, {$ENDIF}
upower,IniFiles,cpucount,userdir,math,
regmult,utypes;
procedure testROC;
procedure testROC2;
function AUCbinomcont (lBinomdataRA,lContdataRA: singlep; lnSubj :integer): double;
function AUCcontcont (ldatara1,ldatara2: singlep; lnSubj :integer): double;
implementation

uses npmform;

function readCSV2 (lFilename: string; lCol1,lCol2: integer;  var lnObservations : integer; var ldataRA1,ldataRA2: singlep): boolean;
const
     kHdrRow = 0;//1;
     kHdrCol = 0;//1;
var
   lNumStr: string;
   F: TextFile;
   lTempFloat: double;
   lCh: char;
   lnFactors,MaxC,R,C:integer;
   lError: boolean;

begin
     lError := false;
     result := false;
	 if not fileexists(lFilename) then begin
            showmessage('Can not find '+lFilename);
            exit;
         end;
	 AssignFile(F, lFilename);
	 FileMode := 0;  //Set file access to read only
	 //First pass: determine column height/width
	 Reset(F);
	 C := 0;
	 MaxC := 0;
	 R := 0;
	 while not Eof(F) do begin
		//read next line
		//read next line
		Read(F, lCh);
		if lCh = '#' then
		   while not (lCh in [#10,#13]) do
			   Read(F, lCh)
		else if not (lCh in [#10,#13,#9,',']) then begin
		   lNumStr := lNumStr + lCh;
		end else if lNumStr <> '' then begin
			lNumStr := '';
			inc(C);
			if C > MaxC then
				MaxC := C;
			if (lCh in [#10,#13]) then begin
				C := 0;
			   inc(R);

			end; //eoln
		end; //if lNumStr <> '' and not tab
	 end;
	 if lNumStr <> '' then  //july06- read data immediately prior to EOF
        inc(R);

     if (R <= (kHdrRow+1)) or (MaxC < (kHdrCol+lCol1)) or (MaxC < (kHdrCol+lCol2)) then begin
         showmessage('problems reading CSV - not enough columns/rows '+inttostr(lCol1)+'  '+inttostr(lCol2));
         exit;
     end;

     lnObservations := R -kHdrRow ; //-1: first row is header....
     lnFactors := MaxC-1;// -1: first column is Y values
     //fx(lnObservations,lnFactors);

     //exit;
     getmem(ldataRA1,lnObservations*sizeof(single));
     getmem(ldataRA2,lnObservations*sizeof(single));

     //second pass
	 Reset(F);
	 C := 1;
	 MaxC := 0;
	 R := 1;
	 lNumStr := '';
     lTempfloat := 0;
	 while not Eof(F) do begin
		//read next line
		Read(F, lCh);
		if lCh = '#' then
		   while not (lCh in [#10,#13]) do
			   Read(F, lCh)
		else if not (lCh in [#10,#13,#9,',']) then begin
		   lNumStr := lNumStr + lCh;
		end else if lNumStr <> '' then begin
            if (R > kHdrRow) and (C > kHdrCol) then begin
             if ((C-kHdrCol) = lCol1) or ((C-kHdrCol) = lCol2) then begin
               if lNumStr = '-' then begin
                  lTempFloat := 0;
               end else begin //number
                try
                   lTempFloat := strtofloat(lNumStr);
                except
                                    on EConvertError do begin
                                       if not lError then
                                          showmessage('Empty cells? Error reading CSV file row:'+inttostr(R)+' col:'+inttostr(C)+' - Unable to convert the string '+lNumStr+' to a number');
                                       lError := true;
                                       lTempFloat := nan;
                                    end;
                end;//except
                //showmessage(lNumStr);
                if (C-kHdrCol) = lCol1 then
                   ldataRA1^[R-kHdrRow] := lTempFloat
                else if (C-kHdrCol) = lCol2 then
                    ldataRA2^[R-kHdrRow] := lTempFloat;
               end; //number
             end; //col1 or col2
            end;// else //R > 1

			lNumStr := '';
			inc(C);
			if C > MaxC then
				MaxC := C;
			if (lCh in [#10,#13]) then begin
				C := 1;
			   inc(R);
			end; //eoln
		end; //if lNumStr <> '' and not tab
	 end;
     if (lNumStr <> '') and (C = lnFactors) then begin  //unterminated string
        try
           lTempFloat := strtofloat(lNumStr);
        except
              on EConvertError do begin
                 if not lError then
                    showmessage('Empty cells? Error reading CSV file row:'+inttostr(R)+' col:'+inttostr(C)+' - Unable to convert the string '+lNumStr+' to a number');
                 lError := true;
                 lTempFloat := nan;
              end;
        end;//except
        ldataRA2^[R-1] := lTempFloat;
     end;//unterminated string
     //read finel item
	 CloseFile(F);
	 FileMode := 2;  //Set file access to read/write
     result := true;
end;

function AUCcontcont (ldatara1,ldatara2: singlep; lnSubj :integer): double;
var
   lIn,lInDX : DoubleP0;
   lnGroup0,lnGroup1,lI: integer;
begin
   result := 0.5;
   if lnSubj < 1 then
      exit;
   Getmem(lIn,lnSubj*sizeof(double));
   Getmem(lInDX,lnSubj*sizeof(double));
   for lI := 1 to lnSubj do begin
       lIn^[lI-1] := ldatara2^[lI];
       lInDX^[lI-1] := ldatara1^[lI];
   end;
   result := continROC2 (lnSubj, lIn, lInDX);
   freemem(lIn);
   freemem(lInDX);
end;

function AUCbinomcont (lBinomdataRA,lContdataRA: singlep; lnSubj :integer): double;
var
   lIn : DoubleP0;
   lnGroup0,lnGroup1,lI: integer;
begin
   result := 0.5;
   if lnSubj < 1 then
      exit;
   Getmem(lIn,lnSubj*sizeof(double));
   lnGroup0 := 0;
   lnGroup1 := 0;
   for lI := 1 to lnSubj do begin
       if lBinomdataRA^[lI] = 0 then begin
          lIn^[lnGroup0] := lContdataRA^[lI];
          inc (lnGroup0);
       end else begin
          inc (lnGroup1);
          lIn^[lnSubj-lnGroup1] := lContdataRA^[lI];

       end;
   end;
   result := continROC (lnSubj, lnGroup0, lIn);
   freemem(lIn);
end;

procedure testROC;
var
   lROC : single;
   lI,lnSubj,lnGroup0: integer;
   //lIn : DoubleP0;
   //csv
   lnGroup1,lCol1,lCol2: integer;  var lnObservations : integer; var ldataRA1,ldataRA2: singlep ;
begin
         npmform.MainForm.memo1.lines.clear;
     npmform.MainForm.memo1.lines.add('ROC analysis requires CSV format text file.');
     npmform.MainForm.memo1.lines.add('First column is the filename (ignored).');
     npmform.MainForm.memo1.lines.add('Second column is 0 [deficit present] or 1 [no deficit].');
     npmform.MainForm.memo1.lines.add('Third column is number of voxels injured in ROI [0 or greater]:');
        npmform.MainForm.memo1.lines.add('Example file:');
   //npmform.MainForm.memo1.lines.add('deficit, voxels');
   npmform.MainForm.memo1.lines.add('c:\c01.voi,0, 121');
   npmform.MainForm.memo1.lines.add('c:\c02.voi,1, 33');
   npmform.MainForm.memo1.lines.add('c:\c03.voi,0, 222');
   npmform.MainForm.memo1.lines.add('c:\c04.voi,1, 56');
   npmform.MainForm.memo1.lines.add('c:\c05.voi,1, 96');
   npmform.MainForm.memo1.lines.add('c:\c06.voi,0, 100');
   //get csv
   npmform.MainForm.memo1.lines.add('  ...requesting CSV file');

   if not MainForm.OpenDialogExecute('Select comma separated filenames ',false,false,kTxtFilter) then
         exit;
   npmform.MainForm.memo1.lines.add('  ...reading CSV file');
   if not readCSV2 (MainForm.OpenHdrDlg.Filename, 2,3, lnObservations, ldataRA1,ldataRA2) then
        exit;
   npmform.MainForm.memo1.lines.add('  ...observations: '+inttostr(lnObservations));
   if lnObservations < 3 then begin
       showmessage('At least 3 subjects required.');
       exit;
   end;
   lnSubj := lnObservations;
   lnGroup0 := 0;
   for lI := 1 to lnSubj do
       if ldatara1^[lI] = 0 then
          inc (lnGroup0);
   npmform.MainForm.memo1.lines.add('  ...observations with deficit [0]: '+inttostr(lnGroup0));
   if (lnGroup0 = lnSubj) or (lnGroup0 = 0) then begin
       showmessage('Some values in the first column must be zero, some must be non-zero.');
       exit;
   end;
   lROC := AUCbinomcont (ldatara1,ldatara2, lnSubj);
   (*Getmem(lIn,lnSubj*sizeof(double));
   lnGroup0 := 0;
   lnGroup1 := 0;
   for lI := 1 to lnSubj do begin
       if ldatara1[lI] = 0 then begin
          lIn[lnGroup0] := ldatara2[lI];
          inc (lnGroup0);
       end else begin
          inc (lnGroup1);
          lIn[lnSubj-lnGroup1] := ldatara2[lI];

       end;
   end;
   lROC := continROC (lnSubj, lnGroup0, lIn);
   freemem(lIn);
     *)

   freemem(ldataRA1);
   freemem(ldataRA2);
   //now analyze
   npmform.MainForm.memo1.lines.add('ROC = '+floattostr(lROC));
   //fx(lROC);
end;


procedure testROC2;
var
   //lDouble: double;
   lVariable: boolean;
   lF,lROC : single;
   lI,lnSubj: integer;
   lIn,lInDX : DoubleP0;
   //csv
   lnGroup1,lCol1,lCol2: integer;  var lnObservations : integer; var ldataRA1,ldataRA2: singlep ;
begin
     npmform.MainForm.memo1.lines.clear;
     npmform.MainForm.memo1.lines.add('ROC analysis requires CSV format text file.');
     npmform.MainForm.memo1.lines.add('First column is the filename (ignored).');
     npmform.MainForm.memo1.lines.add('Second column is degree of deficit [lower value = more impaired].');
     npmform.MainForm.memo1.lines.add('Third column is number of voxels injured in ROI [0 or greater]:');
        npmform.MainForm.memo1.lines.add('Example file:');
   //npmform.MainForm.memo1.lines.add('deficit, voxels');
   npmform.MainForm.memo1.lines.add('c:\c01.voi,0.3, 121');
   npmform.MainForm.memo1.lines.add('c:\c02.voi,0.1, 33');
   npmform.MainForm.memo1.lines.add('c:\c03.voi,0.2, 222');
   npmform.MainForm.memo1.lines.add('c:\c04.voi,1.3, 56');
   npmform.MainForm.memo1.lines.add('c:\c05.voi,1.7, 96');
   npmform.MainForm.memo1.lines.add('c:\c06.voi,1.5, 100');
   //get csv
   npmform.MainForm.memo1.lines.add('  ...requesting CSV file');

   if not MainForm.OpenDialogExecute('Select comma separated filenames ',false,false,kTxtFilter) then
         exit;
   npmform.MainForm.memo1.lines.add('  ...reading CSV file');
   if not readCSV2 (MainForm.OpenHdrDlg.Filename, 2,3, lnObservations, ldataRA1,ldataRA2) then
        exit;
   npmform.MainForm.memo1.lines.add('  ...observations: '+inttostr(lnObservations));
   if lnObservations < 3 then begin
       showmessage('At least 3 subjects required.');
       exit;
   end;
   lnSubj := lnObservations;
   lF := ldatara1^[1];
   lVariable := false;
   for lI := 1 to lnSubj do
       if ldatara1^[lI] <> lF then
          lVariable := true;
   if (not lVariable) then begin
       showmessage('The columns must have some variability.');
       exit;
   end;
   Getmem(lIn,lnSubj*sizeof(double));
   Getmem(lInDX,lnSubj*sizeof(double));
   for lI := 1 to lnSubj do begin
       lIn^[lI-1] := ldatara2^[lI];
       lInDX^[lI-1] := ldatara1^[lI];
   end;
   freemem(ldataRA1);
   freemem(ldataRA2);
   //now analyze
   (*lnSubj := 10;
   lnGroup0 := 5;
   Getmem(lIn,lnSubj*sizeof(double));
   for lI := 0 to (lnSubj-1) do
       lIn[lI] := -lI;//random(99);  *)
   lROC := continROC2 (lnSubj, lIn, lInDX);
   npmform.MainForm.memo1.lines.add('ROC = '+floattostr(lROC));
   freemem(lIn);
   freemem(lInDX);

end;

(*procedure testROC;
var
   lROC : single;
   lI,lnSubj,lnGroup0: integer;
   lIn : DoubleP0;
begin
   lnSubj := 10;
   lnGroup0 := 5;
   Getmem(lIn,lnSubj*sizeof(double));
   for lI := 0 to (lnSubj-1) do
       lIn[lI] := -lI;//random(99);
   lROC := continROC (lnSubj, lnGroup0, lIn);
   npmform.MainForm.memo1.lines.add('ROC = '+floattostr(lROC));
   //fx(lROC);
   freemem(lIn);

end;      *)


end.