-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathData.php
More file actions
328 lines (244 loc) · 9.32 KB
/
Copy pathData.php
File metadata and controls
328 lines (244 loc) · 9.32 KB
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
<?php
require_once('Utility/LinkedList.php');
require_once('Utility/PHPExcel/Classes/PHPExcel.php');
require_once('Utility/Utils.php');
class Data {
public $name; //TODO private
public $antimicrobics;
private $number_tested = 0;
private $data_range_from = "";
private $data_range_to = "";
public function __construct($name){
$this->name = $name;
$this->antimicrobics = new LinkedList();
}
// Original data contains cumulative percentage.
public function fix_percentage() {
// Iterate each Antimicrobic
for ($i = 0; $i < $this->antimicrobics->size; $i++) {
$value = $this->antimicrobics->get($i)->get_value();
// Iterate each value and subtract his previous;
$ticks = array_keys($value);
end($ticks);
while ($tick = current($ticks)) {
$prev_tick = prev($ticks);
if ( (false !== $prev_tick) && ($value[$tick] !== 0) )
$this->antimicrobics->get($i)->value[$tick] -= $value[$prev_tick];
}
}
}
// Some Antimicrobic can be splitted (e.g. 'Cefotaxime' and 'Cefotaxime (meningitis)')
// merge_duplicates() merge them and recalc percentage.
function merge_duplicates() {
$parent_antimicrobic_idx = 0;
$parent_antimicrobic_name = "dummydummy";
// Iterate each Antimicrobic
for ($i = 0; $i < $this->antimicrobics->size; $i++) {
$max_child_nonzero = 0;
$max_child_idx = 0;
// Se il nome dell'antimicrobico corrente ha il carattere '(' allora dovrebbe essere un "figlio"
while ( ($i < $this->antimicrobics->size) && (false !== strpos($this->antimicrobics->get($i)->get_name(), '(')) ) {
// Checking coherence
if ( false === strpos($this->antimicrobics->get($i)->get_name(), $parent_antimicrobic_name) ) {
Utils::log("Errore. Impossibile aggregare l'Antimicrobic: " . $this->antimicrobics->get($i)->get_name());
$this->antimicrobics->removeObjectAtIndex($i);
} else {
// Scegli solo il "figlio" con maggiore dispersione, ovvero con count_nonzero() maggiore
if ( $max_child_nonzero < ($child_nonzero = $this->antimicrobics->get($i)->count_nonzero()) ) {
$max_child_idx = $i;
$max_child_nonzero = $child_nonzero;
}
$i++;
}
}
if ( $max_child_idx !== 0 ) {
// Child found!
$child_full_name = $this->antimicrobics->get($max_child_idx)->get_name();
// Estrae il nome del "padre" e del "figlio"; e.g. 'Cefotaxime (meningitis)'
$parent_antimicrobic_name = extract_parent_name($child_full_name); // e.g. Cefotaxime
$child_antimicrobic_name = extract_child_name($child_full_name); // e.g. meningitis
$child_number_tested = $this->antimicrobics->get($max_child_idx)->get_number_tested();
$parent_number_tested = $this->antimicrobics->get($parent_antimicrobic_idx)->get_number_tested();
// Recalc percentage of child (total amount is now $child_number_tested + $parent_number_tested)
foreach ($this->antimicrobics->get($max_child_idx)->get_value() as $tick => $tick_val)
$this->antimicrobics->get($max_child_idx)->value[$tick] = $tick_val * $child_number_tested / ($child_number_tested+$parent_number_tested);
// Recalc percentage of parent AND sum child values
foreach ($this->antimicrobics->get($parent_antimicrobic_idx)->get_value() as $tick => $tick_val)
$this->antimicrobics->get($parent_antimicrobic_idx)->value[$tick] = round( $tick_val * $parent_number_tested / ($child_number_tested+$parent_number_tested) + $this->antimicrobics->get($max_child_idx)->value[$tick] );
// Pruning childs. Now max child is merged with parent so can be safely removed
for (; $i>$parent_antimicrobic_idx+1; $i--)
$this->antimicrobics->removeObjectAtIndex($parent_antimicrobic_idx+1);
}
$parent_antimicrobic_idx = $i;
$parent_antimicrobic_name = $this->antimicrobics->get($i)->get_name();
}
}
// Funzione di servizio per merge_duplicates()
function extract_parent_name($full_antimicrobic_name) {
$start = strpos($full_antimicrobic_name, '(');
if ($start === false)
return $full_antimicrobic_name;
// Estrae il nome del "padre" (prima delle parentesi)
return trim( substr($full_antimicrobic_name, 0, $start) );
}
// Funzione di servizio per merge_duplicates()
function extract_child_name($full_antimicrobic_name) {
$start = strpos($full_antimicrobic_name, '(');
$end = strpos($full_antimicrobic_name, ')', $start);
if ($start === false || $end === false)
return "";
// Estrae il nome del "padre" (prima delle parentesi)
return trim( substr($full_antimicrobic_name, $start+1, $end-$start-1) );
}
public function add_antimicrobic($antimicrobic) {
// Look up bp/blue markings (based on the pair germ-antimicrobic)
if (false !== ($markings = $this->lookup_markings($this->get_name(), $antimicrobic->get_name())) ) {
$antimicrobic->set_bp($markings['bp']);
$antimicrobic->set_blue($markings['blue']);
}
// Update $this->number_tested as the MAX of every $antimicrobic->get_number_tested()
if ( ($number_tested = $antimicrobic->get_number_tested()) > $this->number_tested )
$this->number_tested = $number_tested;
$this->antimicrobics->add($antimicrobic);
}
public function __toString(){
$tmp = "$this->name\n";
$tmp .= "=====================\n";
$tmp .= $this->antimicrobics;
return $tmp;
}
// lookup_markings() can return no values
function lookup_markings($germ, $antimicrobic) {
// $t_germ is the germ to look for (w/o any spaces)
$t_germ = str_replace(' ', '', $germ);
$t_antimicrobic = str_replace(' ', '', $antimicrobic);
$objPHPExcel = PHPExcel_IOFactory::load($GLOBALS['config']['markings']);
$highestRow = $objPHPExcel->getActiveSheet()->getHighestRow();
$r=2;
while ( $r <= $highestRow ) {
// $c_germ is the germ read from current line (w/o any spaces)
$c_germ = $objPHPExcel->getActiveSheet()->getCell('C' . $r)->getValue();
$c_germ = str_replace(' ', '', $c_germ);
if (strcasecmp($c_germ, $t_germ) == 0) {
$c_antimicrobic = $objPHPExcel->getActiveSheet()->getCell('B' . $r)->getValue();
$c_antimicrobic = str_replace(' ', '', $c_antimicrobic);
if (strcasecmp($c_antimicrobic, $t_antimicrobic) == 0) {
$out['blue'] = $objPHPExcel->getActiveSheet()->getCell('D' . $r)->getValue();
$out['blue'] = str_replace('.', ',', $out['blue']);
$out['bp'] = $objPHPExcel->getActiveSheet()->getCell('E' . $r)->getValue();
$out['bp'] = str_replace('.', ',', $out['bp']);
if ($out['blue'] == '')
Utils::log('Valore WT non trovato per ' . $antimicrobic . ' - ' . $germ);
if ($out['bp'] == '')
Utils::log('Valore BP non trovato per ' . $antimicrobic . ' - ' . $germ);
return $out;
}
}
$r++;
}
}
public function get_number_tested() {
return $this->number_tested;
}
public function set_number_tested($number_tested) {
$this->number_tested = $number_tested;
}
public function get_name() {
return $this->name;
}
public function get_data_range_from() {
return $this->data_range_from;
}
public function set_data_range_from($data_range_from) {
$this->data_range_from = trim($data_range_from);
}
public function get_data_range_to() {
return $this->data_range_to;
}
public function set_data_range_to($data_range_to) {
$this->data_range_to = trim($data_range_to);
}
}
class Antimicrobic {
private $name;
// inizializziamo tutto a 0, piu' comodo.
public $value = array('0,002' => 0, '0,004'=> 0, '0,008'=> 0, '0,015'=> 0,
'0,03'=> 0, '0,06'=> 0, '0,12'=> 0, '0,25'=> 0, '0,5'=> 0, '1'=> 0, '2'=> 0,
'4'=> 0, '8'=> 0, '16'=> 0, '32'=> 0, '64'=> 0, '128'=> 0, '256'=> 0, '512'=> 0);
private $number_tested = 0;
private $bp; // break point
private $blue; // ultima casella blu
public function __construct($name) {
$this->name = $name;
}
public function count_nonzero() {
$count = 0;
foreach ($this->value as $val)
if ($val !== 0)
$count++;
return $count;
}
public function getBpPosition(){
$array = array_keys($this->value);
while ($i = current($array)) {
if ($i == $this->bp) {
return key($array);
}
next($array);
}
}
public function getLastBluePosition(){
$array = array_keys($this->value);
while ($i = current($array)) {
if ($i == $this->blue) {
return key($array);
}
next($array);
}
}
public function get_name(){
return $this->name;
}
public function get_value() {
return $this->value;
}
public function set_value($tick, $value) {
// Replace asterisk (used as a marker in the source document)
$value = str_replace('*', '', $value);
if (!isset($this->value[$tick]) || !is_numeric($value))
return false;
$this->value[$tick] = $value;
return true;
}
public function get_bp() {
return $this->bp; // can be not set
}
public function set_bp($bp) {
if ($bp != '')
$this->bp = $bp;
}
public function get_blue() {
return $this->blue; // can be not set
}
public function set_blue($blue) {
if ($blue != '')
$this->blue = $blue;
}
public function get_number_tested() {
return $this->number_tested;
}
public function set_number_tested($number_tested) {
$this->number_tested = $number_tested;
}
public function __toString(){
$tmp = "$this->name\n";
$tmp .= "Ultima casella blu: $this->blue\n";
$tmp .= "Break Point: $this->bp\n";
$tmp .= "-----------------------\n";
foreach ($this->value as $level => $entry){
$tmp .= "$level = $entry\n";
}
return $tmp;
}
}
?>