File: normalize.cc

package info (click to toggle)
rumba-utils 1.0.1-2
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,764 kB
  • ctags: 414
  • sloc: sh: 7,541; cpp: 6,037; makefile: 1,127; xml: 605; perl: 70
file content (122 lines) | stat: -rw-r--r-- 2,351 bytes parent folder | download
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
#include <rumba/baseManifold.h>
#include <rumba/exception.h>
#include "normalize.h"
#include "stats.h"

namespace 
{
void get_shift_and_scale 
( const RUMBA::StatFunctor& f, int type, double& shift, double& scale);
}


namespace RUMBA
{
	const int MEAN_NORMALIZE = 1;
	const int Z_NORMALIZE = 2;
	const int RANGE_NORMALIZE = 4;
}


namespace 
{
void get_shift_and_scale 
( const RUMBA::StatFunctor& f, int type, double& shift, double& scale)
{
	if ( type == RUMBA::RANGE_NORMALIZE )
	{
		scale = f.max() - f.min();
		shift = f.min();
	}
	else if ( type == RUMBA::Z_NORMALIZE )
	{
		shift = f.mean();
		scale = f.sd();
	}
	else if ( type == RUMBA::MEAN_NORMALIZE )
	{
		shift = f.mean();
		scale = 1;
	}
	else
	{
		throw RUMBA::Exception ("Invalid normalize type in get_shift_and_scale()"); 
	}
}
}

void RUMBA::global_normalize(RUMBA::BaseManifold* pM, int type)
{
	RUMBA::BaseManifold& M = *pM;
	RUMBA::StatFunctor f(pM);
	
	double shift=0,scale=1;

	get_shift_and_scale(f,type,shift,scale);

	if (scale != 0 )
	{
		for ( int i = 0; i < M.size(); ++i )
			M[i] = ((double)M[i] - shift) / scale;
	}
	else
	{
		for ( int i = 0; i < M.size(); ++i )
			M[i] = 0;
	}

}

void RUMBA::volume_normalize(RUMBA::BaseManifold* pM, int type)
{
	RUMBA::BaseManifold& M = *pM;

	double shift =0, scale=1;
	intPoint ext = M.extent();
	ext.t()=1;
	for ( int i = 0; i < M.timepoints(); ++i )
	{
		RUMBA::StatFunctor f(pM, intPoint(0,0,0,i), ext);
		get_shift_and_scale(f,type,shift,scale);
	
		if (scale != 0 )
		{
			for ( int j = 0; j < M.pixels(); ++j )
				M[i*M.pixels()+j] = ( (double)M[i*M.pixels()+j] - shift)/scale;
		}
		else
		{
			for ( int j = 0; j < M.pixels(); ++j )
				M[i*M.pixels()+j] = 0;
		}
	}
}

void RUMBA::pixel_normalize(RUMBA::BaseManifold* pM, int type)
{
	RUMBA::BaseManifold& M = *pM;
	double shift =0, scale=1;
	int x,y,z;
	for ( int i = 0; i < M.pixels(); ++i )
	{
		x = i % M.width();
		y = (i / M.width()) % M.height();
		z = (i / (M.width()*M.height()) ) % M.depth();

		RUMBA::StatFunctor 
			f(pM, intPoint(x,y,z,0), intPoint(1,1,1,M.timepoints()));
		get_shift_and_scale(f,type,shift,scale);
	
		if (scale != 0 )
		{
			for ( int j = 0; j < M.timepoints(); ++j )
				M[i+j*M.pixels()] = ((double)M[i+j*M.pixels()] - shift )/scale;
		}
		else
		{
			for ( int j = 0; j < M.timepoints(); ++j )
				M[i+j*M.pixels()] = 0;
		}
	}
}