-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpercentile.c
115 lines (93 loc) · 2.36 KB
/
percentile.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
/** @file percentile.c
*/
#include "CommandLineInterface/CLIcore.h"
#include "COREMOD_memory/COREMOD_memory.h"
#include "COREMOD_tools/COREMOD_tools.h"
float img_percentile_float(const char *ID_name, float p)
{
imageID ID;
uint32_t naxes[2];
float value = 0;
float *array;
uint64_t nelements;
uint64_t n;
ID = image_ID(ID_name);
naxes[0] = data.image[ID].md[0].size[0];
naxes[1] = data.image[ID].md[0].size[1];
nelements = naxes[0] * naxes[1];
array = (float *) malloc(nelements * sizeof(float));
if(array == NULL)
{
PRINT_ERROR("malloc returns NULL pointer");
abort();
}
for(unsigned long ii = 0; ii < nelements; ii++)
{
array[ii] = data.image[ID].array.F[ii];
}
quick_sort_float(array, nelements);
n = (uint64_t)(p * naxes[1] * naxes[0]);
if(n > 0)
{
if(n > (nelements - 1))
{
n = (nelements - 1);
}
}
value = array[n];
free(array);
printf("percentile %f = %f (%ld)\n", p, value, n);
return (value);
}
double img_percentile_double(const char *ID_name, double p)
{
imageID ID;
uint32_t naxes[2];
double value = 0;
double *array;
uint64_t nelements;
uint64_t n;
ID = image_ID(ID_name);
naxes[0] = data.image[ID].md[0].size[0];
naxes[1] = data.image[ID].md[0].size[1];
nelements = naxes[0] * naxes[1];
array = (double *) malloc(nelements * sizeof(double));
if(array == NULL)
{
PRINT_ERROR("malloc returns NULL pointer");
abort();
}
for(unsigned long ii = 0; ii < nelements; ii++)
{
array[ii] = data.image[ID].array.F[ii];
}
quick_sort_double(array, nelements);
n = (uint64_t)(p * naxes[1] * naxes[0]);
if(n > 0)
{
if(n > (nelements - 1))
{
n = (nelements - 1);
}
}
value = array[n];
free(array);
return (value);
}
double img_percentile(const char *ID_name, double p)
{
imageID ID;
uint8_t datatype;
double value = 0.0;
ID = image_ID(ID_name);
datatype = data.image[ID].md[0].datatype;
if(datatype == _DATATYPE_FLOAT)
{
value = (double) img_percentile_float(ID_name, (float) p);
}
if(datatype == _DATATYPE_DOUBLE)
{
value = img_percentile_double(ID_name, p);
}
return value;
}