~ubuntu-branches/ubuntu/saucy/mricron/saucy

« back to all changes in this revision

Viewing changes to batch.pas

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2010-08-22 08:32:25 UTC
  • mfrom: (1.1.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20100822083225-lt6kc7smjq5hexmp
Tags: 0.20100820.1~dfsg.1-1
* New upstream code.
* Add patch to identify the Debian package version as such in the 'About'
  dialog.
* Bumped Standards-version to 3.9.1, no changes necessary.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
unit batch;
2
 
 
3
 
interface
4
 
{$H+}
5
 
uses define_types;
6
 
procedure BatchVOI;
7
 
 
8
 
implementation
9
 
 
10
 
uses nifti_img, nifti_img_view, dialogs, nifti_hdr_view, text,sysutils,classes, fdr;
11
 
 
12
 
function VOIVol (lOverlayNum: integer): integer;
13
 
var
14
 
   lInc,lVox: integer;
15
 
begin
16
 
   result := 0;
17
 
   if gMRIcroOverlay[lOverlayNum].ScrnBufferItems < 1 then
18
 
    exit;
19
 
   if gMRIcroOverlay[lOverlayNum].ScrnBufferItems <> gMRIcroOverlay[kBGOverlayNum].ScrnBufferItems then
20
 
      exit;
21
 
   lVox := 0;
22
 
   for lInc := 1 to gMRIcroOverlay[lOverlayNum].ScrnBufferItems do begin
23
 
       if gMRIcroOverlay[lOverlayNum].ScrnBuffer^[lInc] > 0 then
24
 
          inc(lVox);
25
 
   end; //for each voxel
26
 
   result := lVox;
27
 
end; //VOIVol
28
 
 
29
 
function VOIMean (lOverlayNum: integer): double;
30
 
var
31
 
   lSum,lInten,lVol: double;
32
 
   lInc: integer;
33
 
begin
34
 
   result := 0;
35
 
   lVol := VOIVol(lOverlayNum);
36
 
   if lVol < 1 then
37
 
    exit;
38
 
   lSum := 0;
39
 
   for lInc := 1 to gMRIcroOverlay[lOverlayNum].ScrnBufferItems do begin
40
 
       //Next line - only voxels that are part of VOI
41
 
       if gMRIcroOverlay[lOverlayNum].ScrnBuffer^[lInc] > 0 then begin
42
 
          lInten := RawBGIntensity(lInc);
43
 
          lSum := lSum + lInten;
44
 
       end;
45
 
   end; //for each voxel
46
 
   result := lSum/lVol;
47
 
end; //VOIMean
48
 
 
49
 
 
50
 
(*
51
 
function LesionVol (lOverlayNum: integer): integer;
52
 
var
53
 
   lInc,lVox: integer;
54
 
begin //proc ShowDescript
55
 
   result := 0;
56
 
   if gMRIcroOverlay[lOverlayNum].ScrnBufferItems <> gMRIcroOverlay[kBGOverlayNum].ScrnBufferItems then
57
 
      exit;
58
 
   lVox := 0;
59
 
   for lInc := 1 to gMRIcroOverlay[lOverlayNum].ScrnBufferItems do begin
60
 
       if gMRIcroOverlay[lOverlayNum].ScrnBuffer^[lInc] > 0 then
61
 
          inc(lVox);
62
 
   end; //for each voxel
63
 
   result := lVox;
64
 
end;
65
 
 
66
 
function LesionFrac (lOverlayNum: integer): double;
67
 
var
68
 
   lMaxSum,lLesionSum,lInten: double;
69
 
   lInc: integer;
70
 
begin //proc ShowDescript
71
 
   result := 0;
72
 
   if gMRIcroOverlay[lOverlayNum].ScrnBufferItems <> gMRIcroOverlay[kBGOverlayNum].ScrnBufferItems then
73
 
      exit;
74
 
   lMaxSum := 0;
75
 
   lLesionSum := 0;
76
 
   for lInc := 1 to gMRIcroOverlay[lOverlayNum].ScrnBufferItems do begin
77
 
       lInten := RawBGIntensity(lInc);
78
 
       lMaxSum := lInten + lMaxSum;
79
 
       if gMRIcroOverlay[lOverlayNum].ScrnBuffer^[lInc] > 0 then
80
 
          lLesionSum := lLesionSum + lInten;
81
 
   end; //for each voxel
82
 
   if lMaxSum = 0 then
83
 
      exit;
84
 
   //result := lLesionSum / lMaxSum;
85
 
   result := lLesionSum;
86
 
end;*)
87
 
 
88
 
function VOIMeanFrac10pct (lOverlayNum: integer; lMax: boolean): double;
89
 
//if lMax is true, return top 10pct, if false return bottom
90
 
var
91
 
   lSum: double;
92
 
   lVox,lInc,l10pct: integer;
93
 
   lRA: singlep;
94
 
begin //proc ShowDescript
95
 
   result := 0;
96
 
   if gMRIcroOverlay[lOverlayNum].ScrnBufferItems <> gMRIcroOverlay[kBGOverlayNum].ScrnBufferItems then
97
 
      exit;
98
 
   //first - count number of voxels in ROI
99
 
   lVox := 0;
100
 
   for lInc := 1 to gMRIcroOverlay[lOverlayNum].ScrnBufferItems do
101
 
       if gMRIcroOverlay[lOverlayNum].ScrnBuffer^[lInc] > 0 then
102
 
          inc(lVox);
103
 
   //next - get memory
104
 
   if lVox < 1 then
105
 
    exit;
106
 
   getmem(lRA,lVox * sizeof(single));
107
 
   lVox := 0;
108
 
   for lInc := 1 to gMRIcroOverlay[lOverlayNum].ScrnBufferItems do
109
 
       if gMRIcroOverlay[lOverlayNum].ScrnBuffer^[lInc] > 0 then begin
110
 
          inc(lVox);
111
 
          lRA^[lVox] := RawBGIntensity(lInc);
112
 
       end;
113
 
   qsort(1, lVox,lRA);
114
 
   l10pct := round(lVox / 10);
115
 
   if l10pct < 1 then
116
 
    l10pct := 1;
117
 
 
118
 
   lSum := 0;
119
 
   if not lMax then begin //lower 10pct
120
 
      for lInc := 1 to l10pct do
121
 
        lSum := lSum + lRA^[lInc]
122
 
   end else begin //top 10pct
123
 
      for lInc :=  (lVox-l10pct+1) to lVox do
124
 
        lSum := lSum + lRA^[lInc];
125
 
   end;
126
 
   result := lSum / l10pct;
127
 
   freemem(lRA);
128
 
end;
129
 
 
130
 
 
131
 
(*procedure BatchVOI;
132
 
var
133
 
        lInc,lNumberofFiles: integer;
134
 
        lFilename:string;
135
 
        lStrings : TStringList;
136
 
begin
137
 
        if gMRIcroOverlay[kBGOverlayNum].ScrnBufferItems < 1 then begin
138
 
                showmessage('Please load a background image for rescaling.');
139
 
                exit;
140
 
        end;
141
 
        for lInc := 1 to (knMaxOverlay-1) do
142
 
            FreeImgMemory(gMRIcroOverlay[lInc]);
143
 
        ImgForm.UpdateLayerMenu;
144
 
 
145
 
        if not OpenDialogExecute(kImgFilter,'Select PMaps',true) then exit;
146
 
        lNumberofFiles:= HdrForm.OpenHdrDlg.Files.Count;
147
 
        if  lNumberofFiles < 1 then
148
 
                exit;
149
 
        lStrings := TStringList.Create;
150
 
        lStrings.AddStrings(HdrForm.OpenHdrDlg.Files);
151
 
        if not OpenDialogExecute(kImgFilter,'Select images you wish to analyze',true) then exit;
152
 
        lNumberofFiles:= HdrForm.OpenHdrDlg.Files.Count;
153
 
        if  lNumberofFiles < 1 then
154
 
                exit;
155
 
        TextForm.Memo1.Lines.Clear;
156
 
        TextForm.Memo1.lines.add('Background =,'+gMRIcroOverlay[kBGOverlayNum].hdrfilename);
157
 
        for lInc:= 1 to lNumberofFiles do begin
158
 
                lFilename := HdrForm.OpenHdrDlg.Files[lInc-1];
159
 
                ImgForm.OverlayOpenCore ( lFilename, 2);
160
 
 
161
 
                TextForm.Memo1.lines.add(lfilename+','{+ inttostr(LesionVol(2))+','}+ floattostr(LesionFrac(2))  );
162
 
                //ImgForm.ShowDescriptive(2,true);
163
 
        end;
164
 
        FreeImgMemory(gMRIcroOverlay[2]);
165
 
        ImgForm.UpdateLayerMenu;
166
 
        //SaveDialog1.Filename := ExtractFileDirWithPathDelim(HdrForm.OpenHdrDlg.Files[0])+'desc.csv';
167
 
        lStrings.Free;
168
 
        //ImgForm.SaveDialog1.Filename := ExtractFileDirWithPathDelim(gMRIcroOverlay[lOverlayNum].HdrFileName)+'desc.csv';
169
 
        TextForm.Show;
170
 
end;*)
171
 
procedure BatchVOI;
172
 
var
173
 
        lNumberofP,lP,lInc,lNumberofFiles,lLoop: integer;
174
 
        lFilename,lStr:string;
175
 
        lStrings : TStringList;
176
 
begin
177
 
 
178
 
        for lInc := 1 to (knMaxOverlay-1) do
179
 
            FreeImgMemory(gMRIcroOverlay[lInc]);
180
 
        ImgForm.UpdateLayerMenu;
181
 
 
182
 
        if not OpenDialogExecute(kImgFilter,'Select background images (stat maps)',true) then exit;
183
 
        lNumberofP:= HdrForm.OpenHdrDlg.Files.Count;
184
 
        if  lNumberofP < 1 then
185
 
                exit;
186
 
        lStrings := TStringList.Create;
187
 
        lStrings.AddStrings(HdrForm.OpenHdrDlg.Files);
188
 
 
189
 
        if not OpenDialogExecute(kImgFilter,'Select overlay images (ROIs)',true) then exit;
190
 
        lNumberofFiles:= HdrForm.OpenHdrDlg.Files.Count;
191
 
        if  lNumberofFiles < 1 then
192
 
                exit;
193
 
    TextForm.Memo1.Lines.Clear;
194
 
    for lLoop := 1 to 3 do begin
195
 
        if lLoop=3 then
196
 
          lStr := 'min10pct_Filename,Vol'
197
 
        else if lLoop=2 then
198
 
          lStr := 'max10pct_Filename,Vol'
199
 
        else
200
 
          lStr := 'mean,Vol';
201
 
        for lP := 1 to lNumberofP do
202
 
            lStr := lStr + ','+Parsefilename(extractfilename(lStrings.Strings[lP-1]));
203
 
        TextForm.Memo1.lines.add(lStr);
204
 
        for lInc:= 1 to lNumberofFiles do begin
205
 
            ImgForm.StatusLabel.Caption := inttostr(lInc)+'/'+inttostr(lNumberofFiles);
206
 
            IMgForm.refresh;
207
 
            lStr := Parsefilename(extractfilename(HdrForm.OpenHdrDlg.Files[lInc-1]));
208
 
            for lP := 1 to lNumberofP do begin
209
 
                lFilename := lStrings.Strings[lP-1];
210
 
                ImgForm.OpenAndDisplayImg(lFilename,True);
211
 
                lFilename := HdrForm.OpenHdrDlg.Files[lInc-1];
212
 
                      ImgForm.OverlayOpenCore ( lFilename, 2);
213
 
                if lP = 1 then
214
 
                   lStr := lStr + ','+ inttostr(VOIVol(2) );
215
 
                if lLoop = 3 then
216
 
                  lStr := lStr + ','+ floattostr(VOIMeanFrac10Pct(2,false))
217
 
                else if lLoop = 2 then
218
 
                  lStr := lStr + ','+ floattostr(VOIMeanFrac10Pct(2,true))
219
 
                else
220
 
                  lStr := lStr + ','+ floattostr(VOIMean(2));
221
 
            end;
222
 
                TextForm.Memo1.lines.add(lStr );
223
 
        end;
224
 
    end;//lLoop
225
 
        FreeImgMemory(gMRIcroOverlay[2]);
226
 
        ImgForm.UpdateLayerMenu;
227
 
        //SaveDialog1.Filename := ExtractFileDirWithPathDelim(HdrForm.OpenHdrDlg.Files[0])+'desc.csv';
228
 
        lStrings.Free;
229
 
        //ImgForm.SaveDialog1.Filename := ExtractFileDirWithPathDelim(gMRIcroOverlay[lOverlayNum].HdrFileName)+'desc.csv';
230
 
        TextForm.Show;
231
 
end;
232
 
 
233
 
end.
234
 
 
 
1
unit batch;
 
2
{$H+}
 
3
interface
 
4
uses
 
5
{$IFNDEF UNIX} Windows,
 
6
{$ELSE}
 
7
 lclintf,LCLType,LResources,BaseUnix,
 
8
{$ENDIF}
 
9
define_types;
 
10
procedure BatchVOI;
 
11
 
 
12
implementation
 
13
 
 
14
uses
 
15
  Forms,
 
16
  //lclintf,LResources,{$IFNDEF Unix}  Controls, {$ELSE}BaseUnix, LCLType,{$ENDIF}
 
17
  nifti_img, nifti_img_view, dialogs, nifti_hdr_view, text,sysutils,classes, fdr,batchstatselect;
 
18
 
 
19
(*function LesionFrac (lOverlayNum: integer): double;
 
20
var
 
21
   lLesionSum,lInten: double;
 
22
   lInc: integer;
 
23
begin
 
24
   result := 0;
 
25
   if gMRIcroOverlay[lOverlayNum].ScrnBufferItems < 1 then
 
26
    exit;
 
27
   if gMRIcroOverlay[lOverlayNum].ScrnBufferItems <> gMRIcroOverlay[kBGOverlayNum].ScrnBufferItems then
 
28
      exit;
 
29
   lLesionSum := 0;
 
30
   for lInc := 1 to gMRIcroOverlay[lOverlayNum].ScrnBufferItems do begin
 
31
       lInten := RawBGIntensity(lInc);
 
32
       if gMRIcroOverlay[lOverlayNum].ScrnBuffer[lInc] > 0 then
 
33
          lLesionSum := lLesionSum + lInten;
 
34
   end; //for each voxel
 
35
   result := lLesionSum;
 
36
end;*)
 
37
 
 
38
function VOIVol (lOverlayNum: integer): integer;
 
39
var
 
40
   lInc,lVox: integer;
 
41
begin
 
42
   result := 0;
 
43
   if gMRIcroOverlay[lOverlayNum].ScrnBufferItems < 1 then
 
44
    exit;
 
45
   if gMRIcroOverlay[lOverlayNum].ScrnBufferItems <> gMRIcroOverlay[kBGOverlayNum].ScrnBufferItems then
 
46
      exit;
 
47
   lVox := 0;
 
48
   for lInc := 1 to gMRIcroOverlay[lOverlayNum].ScrnBufferItems do begin
 
49
       if gMRIcroOverlay[lOverlayNum].ScrnBuffer^[lInc] > 0 then
 
50
          inc(lVox);
 
51
   end; //for each voxel
 
52
   result := lVox;
 
53
end; //VOIVol
 
54
 
 
55
function VOIMean (lOverlayNum: integer): double;
 
56
var
 
57
   lSum,lInten,lVol: double;
 
58
   lInc: integer;
 
59
begin
 
60
   result := 0;
 
61
   lVol := VOIVol(lOverlayNum);
 
62
   if lVol < 1 then
 
63
    exit;
 
64
   lSum := 0;
 
65
   for lInc := 1 to gMRIcroOverlay[lOverlayNum].ScrnBufferItems do begin
 
66
       lInten := RawBGIntensity(lInc);
 
67
       //Next line - only voxels that are part of VOI
 
68
       if gMRIcroOverlay[lOverlayNum].ScrnBuffer^[lInc] > 0 then
 
69
        lSum := lSum + lInten;
 
70
   end; //for each voxel
 
71
   result := lSum/lVol;
 
72
end; //VOIMean
 
73
 
 
74
function VOIMeanFrac10pct (lOverlayNum: integer; lMax: boolean): double;
 
75
//if lMax is true, return top 10pct, if false return bottom
 
76
var
 
77
   lSum: double;
 
78
   lVox,lInc,l10pct: integer;
 
79
   lRA: singlep;
 
80
begin //proc ShowDescript
 
81
   result := 0;
 
82
   if gMRIcroOverlay[lOverlayNum].ScrnBufferItems < 1 then
 
83
    exit;
 
84
   if gMRIcroOverlay[lOverlayNum].ScrnBufferItems <> gMRIcroOverlay[kBGOverlayNum].ScrnBufferItems then
 
85
      exit;
 
86
   //first - count number of voxels in ROI
 
87
   lVox := 0;
 
88
   for lInc := 1 to gMRIcroOverlay[lOverlayNum].ScrnBufferItems do
 
89
       if gMRIcroOverlay[lOverlayNum].ScrnBuffer^[lInc] > 0 then
 
90
          inc(lVox);
 
91
   //next - get memory
 
92
   if lVox < 1 then
 
93
    exit;
 
94
   getmem(lRA,lVox * sizeof(single));
 
95
   lVox := 0;
 
96
   for lInc := 1 to gMRIcroOverlay[lOverlayNum].ScrnBufferItems do
 
97
       if gMRIcroOverlay[lOverlayNum].ScrnBuffer^[lInc] > 0 then begin
 
98
          inc(lVox);
 
99
          lRA^[lVox] := RawBGIntensity(lInc);
 
100
       end;
 
101
   qsort(1, lVox,lRA);
 
102
   l10pct := round(lVox / 10);
 
103
   if l10pct < 1 then
 
104
    l10pct := 1;
 
105
   lSum := 0;
 
106
   if not lMax then begin //lower 10pct
 
107
      for lInc := 1 to l10pct do
 
108
        lSum := lSum + lRA^[lInc]
 
109
   end else begin //top 10pct
 
110
      for lInc :=  (lVox-l10pct+1) to lVox do
 
111
        lSum := lSum + lRA^[lInc];
 
112
   end;
 
113
   result := lSum / l10pct;
 
114
   freemem(lRA);
 
115
end;
 
116
 
 
117
procedure BatchVOI;
 
118
var
 
119
        lNumberofP,lP,lInc,lNumberofFiles,lLoop: integer;
 
120
        lFilename,lStr:string;
 
121
        lBGStrings : TStrings;
 
122
begin
 
123
 
 
124
        for lInc := 1 to (knMaxOverlay-1) do
 
125
            FreeImgMemory(gMRIcroOverlay[lInc]);
 
126
        ImgForm.UpdateLayerMenu;
 
127
        lBGStrings := TStringList.Create;
 
128
        if (ssShift in KeyDataToShiftState(vk_Shift)) then begin
 
129
                GetFilesInDir(ExtractFileDir(HdrForm.OpenHdrDlg.Filename),lBGStrings)
 
130
        end else begin
 
131
            if not OpenDialogExecute(kImgFilter,'Select background images (stat maps)',true) then
 
132
               exit;
 
133
            lBGStrings.AddStrings(HdrForm.OpenHdrDlg.Files);
 
134
        end;
 
135
        lNumberofP:= lBGStrings.Count;
 
136
        if  lNumberofP < 1 then begin
 
137
            lBGStrings.free;
 
138
            exit;
 
139
 
 
140
        end;
 
141
 
 
142
        if not OpenDialogExecute(kImgFilter,'Select overlay images (ROIs)',true) then exit;
 
143
        lNumberofFiles:= HdrForm.OpenHdrDlg.Files.Count;
 
144
        if  lNumberofFiles < 1 then
 
145
                exit;
 
146
    TextForm.MemoT.Lines.Clear;
 
147
    lStr := 'Function'+kTextSep+'VOIname'+kTextSep+'VOIvol';
 
148
    for lP := 1 to lNumberofP do
 
149
        lStr := lStr + kTextSep+(lBGStrings.Strings[lP-1]);
 
150
    TextForm.MemoT.lines.add(lStr);
 
151
    for lLoop := 1 to 3 do begin
 
152
        {if lLoop=3 then
 
153
          lStr := 'min10pct'+kSep+'Filename+'kSep+'Vol'
 
154
        else if lLoop=2 then
 
155
          lStr := 'max10pct'+Filename+'kSep+'Vol'
 
156
        else
 
157
          lStr := 'mean'+Filename+kSep'Vol';}
 
158
 
 
159
        for lInc:= 1 to lNumberofFiles do begin
 
160
            ImgForm.StatusLabel.Caption := inttostr(lInc)+'/'+inttostr(lNumberofFiles);
 
161
            IMgForm.refresh;
 
162
            if lLoop=3 then
 
163
               lStr := 'min10pct'
 
164
            else if lLoop=2 then
 
165
                 lStr := 'max10pct'
 
166
            else
 
167
                lStr := 'mean';
 
168
            lStr := lStr +kTextSep+ (HdrForm.OpenHdrDlg.Files[lInc-1]);
 
169
            for lP := 1 to lNumberofP do begin
 
170
                lFilename := lBGStrings.Strings[lP-1];
 
171
                ImgForm.OpenAndDisplayImg(lFilename,True);
 
172
                lFilename := HdrForm.OpenHdrDlg.Files[lInc-1];
 
173
                      ImgForm.OverlayOpenCore ( lFilename, 2);
 
174
                if lP = 1 then
 
175
                   lStr := lStr + kTextSep+ inttostr(VOIVol(2) );
 
176
                if lLoop = 3 then
 
177
                  lStr := lStr + kTextSep+ floattostr(VOIMeanFrac10Pct(2,false))
 
178
                else if lLoop = 2 then
 
179
                  lStr := lStr + kTextSep+ floattostr(VOIMeanFrac10Pct(2,true))
 
180
                else
 
181
                  lStr := lStr + kTextSep+ floattostr(VOIMean(2));
 
182
            end;
 
183
                TextForm.MemoT.lines.add(lStr );
 
184
        end;
 
185
    end;//lLoop
 
186
        FreeImgMemory(gMRIcroOverlay[2]);
 
187
        ImgForm.UpdateLayerMenu;
 
188
        //SaveDialog1.Filename := ExtractFileDirWithPathDelim(HdrForm.OpenHdrDlg.Files[0])+'desc.csv';
 
189
        lBGStrings.Free;
 
190
        //ImgForm.SaveDialog1.Filename := ExtractFileDirWithPathDelim(gMRIcroOverlay[lOverlayNum].HdrFileName)+'desc.csv';
 
191
        TextForm.Show;
 
192
end;
 
193
 
 
194
end.
 
195