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

« back to all changes in this revision

Viewing changes to npm/upower.pas

  • Committer: Bazaar Package Importer
  • Author(s): Michael Hanke
  • Date: 2010-07-29 22:07:43 UTC
  • Revision ID: james.westby@ubuntu.com-20100729220743-q621ts2zj806gu0n
Tags: upstream-0.20100725.1~dfsg.1
ImportĀ upstreamĀ versionĀ 0.20100725.1~dfsg.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
unit upower;
 
2
interface
 
3
 
 
4
uses define_types, statcr, distr, dialogs;
 
5
function Sum2Power(lOutImgSum: SingleP; lVolVox,lnTotal,lnDeficit: integer; lBinomial: boolean): boolean;
 
6
function Sum2PowerCont(lOutImgSum: SingleP; lVolVox,lnTotal: integer): boolean;
 
7
function Sum2PowerBinom(lOutImgSum: SingleP; lVolVox,lnTotal,lnDeficit: integer): boolean;
 
8
function k_out_n (k,n: integer): double; //total possible permutations
 
9
 
 
10
implementation
 
11
 
 
12
function k_out_n (k,n: integer): double; //total possible permutations
 
13
//k= smaller group, n=sum of both groups
 
14
begin
 
15
        if not gFactRAready then InitFact;
 
16
        result := round(gFactRA[n] / (gFactRA[k]*gFactRA[n-k]  )  );
 
17
// k out n = n!/(k!*(n-k)! which is equal to the PROD(i=k; 1){(n-i+1)/i}
 
18
end; //k_out_n
 
19
 
 
20
function Sum2Power(lOutImgSum: SingleP; lVolVox,lnTotal,lnDeficit: integer; lBinomial: boolean): boolean;
 
21
begin
 
22
     if lBinomial then
 
23
        result :=  Sum2PowerBinom(lOutImgSum, lVolVox,lnTotal,lnDeficit)
 
24
     else
 
25
        result :=  Sum2PowerCont(lOutImgSum, lVolVox,lnTotal)
 
26
end;
 
27
 
 
28
function Sum2PowerCont(lOutImgSum: SingleP; lVolVox,lnTotal: integer): boolean;
 
29
//convert Sum image to power map showing maximum possible effect size
 
30
//'Cont' version is for continuous data
 
31
var
 
32
   lDensity,lN,lRank: integer;
 
33
   lDensityPowerRA: singleP;
 
34
begin
 
35
    result := false;
 
36
    if (lnTotal < 2)  or (lVolVox < 1) then
 
37
       exit;
 
38
    getmem(lDensityPowerRA,lnTotal* sizeof(single));
 
39
    //no need to compute power for [lnTotal] and [0] - no variability when everyone or no one has a lesion
 
40
    //lDensityPowerRA[lnTotal] := 0; //everyone has a lesion = no variability
 
41
    lRank := 0;
 
42
    for lN := 1 to (lnTotal -1) do begin
 
43
        //most power when all participants with a lesion have most extreme behavioural data
 
44
        //therefore, they will have the lowest ranks: rank 1,2,3,4
 
45
        lRank := lRank + lN;
 
46
        if (lnTotal > 360) then //cannot calculate values this large...
 
47
            lDensityPowerRA^[lN] := 0
 
48
        else if (lN > 10) and (lnTotal > 64) then  //avoid overflow...
 
49
            lDensityPowerRA^[lN] := pNormalInv ( 1/(k_out_n(10,lnTotal)) )
 
50
        else begin
 
51
            lDensityPowerRA^[lN] := 1/(k_out_n(lN,lnTotal)); //compute Wilcoxon probability
 
52
            lDensityPowerRA^[lN] := pNormalInv (lDensityPowerRA^[lN]);//convert p to z-score
 
53
        end;
 
54
        //max power when every possible person with a lesion has a defict, and everyone w/o lesion does not...
 
55
        //lDensityPowerRA[lN] := Liebermeister (lLD,lnoLD,lLnoD,lnoLnoD); //probability of this observation
 
56
        //lDensityPowerRA[lN] := pNormalInv (lDensityPowerRA[lN]);//convert p to z-score
 
57
        //fx(lDensityPowerRA[lN]);
 
58
    end;
 
59
    //now use lookup table to convert overlay density to effective power
 
60
    for lN := 1 to lVolVox do begin
 
61
        lDensity := round( lOutImgSum^[lN]);
 
62
        if (lDensity > 0) and (lDensity < lnTotal) then
 
63
           lOutImgSum^[lN] := lDensityPowerRA^[lDensity]
 
64
        else
 
65
            lOutImgSum^[lN] := 0;
 
66
    end; //for each voxel
 
67
    freemem(lDensityPowerRA);
 
68
    result := true;
 
69
end;
 
70
 
 
71
function Sum2PowerBinom(lOutImgSum: SingleP; lVolVox,lnTotal,lnDeficit: integer): boolean;
 
72
//convert Sum image to power map showing maximum possible effect size
 
73
var
 
74
   lDensity,lN,lLD,lLnoD,lnoLD,lnoLnoD: integer;
 
75
   lDensityPowerRA: singleP;
 
76
begin
 
77
    result := false;
 
78
    if (lnTotal < 2) or (lnDeficit < 1) or (lVolVox < 1) then
 
79
       exit;
 
80
    if(lnDeficit >= lnTotal) then begin
 
81
       showmessage('Sum2Power error: people with deficit must be less than sample size');
 
82
       exit;
 
83
    end;
 
84
    getmem(lDensityPowerRA,lnTotal* sizeof(single));
 
85
    //no need to compute power for lnTotal and 0 - no variability when everyone or no one has a lesion
 
86
    //lDensityPowerRA[lnTotal] := 0; //everyone has a lesion = no variability
 
87
    for lN := 1 to (lnTotal -1) do begin
 
88
        //max power when every possible person with a lesion has a defict, and everyone w/o lesion does not...
 
89
        if lN > lnDeficit then begin
 
90
            lLD := lnDeficit;
 
91
            lLnoD := lN - lnDeficit;
 
92
        end else begin
 
93
            lLD := lN;
 
94
            lLnoD := 0;
 
95
        end;
 
96
        lnoLD := lnDeficit-lLD; //number of people with deficit who do not have a lesion - as close to zero as possible
 
97
        lnoLnoD := lnTotal-lnoLD-lLnoD-lLD;
 
98
        lDensityPowerRA^[lN] := Liebermeister (lLD,lnoLD,lLnoD,lnoLnoD); //probability of this observation
 
99
        lDensityPowerRA^[lN] := pNormalInv (lDensityPowerRA^[lN]);//convert p to z-score
 
100
        //fx(lLD,lnoLD,lLnoD,lnoLnoD,lDensityPowerRA[lN]);
 
101
    end;
 
102
    //now use lookup table to convert overlay density to effective power
 
103
    for lN := 1 to lVolVox do begin
 
104
        lDensity := round( lOutImgSum^[lN]);
 
105
        if (lDensity > 0) and (lDensity < lnTotal) then
 
106
           lOutImgSum^[lN] := lDensityPowerRA^[lDensity]
 
107
        else
 
108
            lOutImgSum^[lN] := 0;
 
109
 
 
110
    end; //for each voxel
 
111
    freemem(lDensityPowerRA);
 
112
    result := true;
 
113
end;
 
114
 
 
115
 
 
116
end.