00001
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 #include <string.h>
00019 #include <stdlib.h>
00020 #include <stdio.h>
00021
00022
00023 #define _LIBRASCH_BUILD
00024 #include <ra_eval.h>
00025
00026
00033 int
00034 read_evaluation(meas_handle mh)
00035 {
00036 int ret = -1;
00037 eval_handle eh;
00038 struct ra_meas *meas = (struct ra_meas *)mh;
00039 struct plugin_struct *pl;
00040
00041 if (!mh)
00042 return -1;
00043 pl = meas->p;
00044
00045
00046 if (ra_eval_get_original(mh) != NULL)
00047 return 0;
00048
00049 ra_eval_edit_start(mh);
00050
00051 if ((eh = add_eval_orig(mh, pl)) == NULL)
00052 goto error_eval;
00053 if (add_class_orig(mh, eh, pl) != 0)
00054 goto error_eval;
00055
00056 if (do_post_processing(mh, eh) != 0)
00057 goto error_eval;
00058
00059 ra_eval_edit_complete(mh);
00060
00061 return 0;
00062
00063 error_eval:
00064 ra_eval_edit_cancel(mh);
00065
00066 _ra_set_error(mh, RA_ERR_READ_EVAL, "libRASCH");
00067
00068 return ret;
00069 }
00070
00071
00079 eval_handle
00080 add_eval_orig(meas_handle mh, struct plugin_struct *pl)
00081 {
00082 eval_handle eh = NULL;
00083 value_handle vh, vh_name, vh_desc;
00084
00085 if (pl->access.get_eval_info == NULL)
00086 return NULL;
00087
00088 vh = ra_value_malloc();
00089 vh_name = ra_value_malloc();
00090 vh_desc = ra_value_malloc();
00091
00092 if ((*pl->access.get_eval_info)(mh, vh_name, vh_desc) != 0)
00093 goto clean;
00094
00095 eh = ra_eval_add(mh, ra_value_get_string(vh_name), ra_value_get_string(vh_desc), 1);
00096 if (!eh)
00097 goto clean;
00098
00099 clean:
00100 ra_value_free(vh);
00101 ra_value_free(vh_name);
00102 ra_value_free(vh_desc);
00103
00104 return eh;
00105 }
00106
00107
00116 int
00117 add_class_orig(meas_handle mh, eval_handle eh, struct plugin_struct *pl)
00118 {
00119 int ret = -1;
00120 long n_class, l;
00121 value_handle vh_id, vh_name, vh_desc;
00122 class_handle clh;
00123 long *ev_ids, num_ev;
00124
00125 if ((pl->access.get_class_num == NULL) || (pl->access.get_class_info == NULL))
00126 return -1;
00127
00128 vh_id = ra_value_malloc();
00129 vh_name = ra_value_malloc();
00130 vh_desc = ra_value_malloc();
00131
00132 n_class = (*pl->access.get_class_num)(mh);
00133 for (l = 0; l < n_class; l++)
00134 {
00135 if ((*pl->access.get_class_info)(mh, l, vh_id, vh_name, vh_desc) != 0)
00136 goto error;
00137
00138 if (!ra_value_is_ok(vh_id))
00139 goto error;
00140
00141 clh = ra_class_add(eh, ra_value_get_string(vh_id), ra_value_get_string(vh_name), ra_value_get_string(vh_desc));
00142 if (!clh)
00143 goto error;
00144
00145 if (add_events_orig(clh, pl, &ev_ids, &num_ev) != 0)
00146 goto error;
00147
00148 if (add_prop_orig(clh, pl, ev_ids, num_ev) != 0)
00149 goto error;
00150
00151 if (add_summaries_orig(clh, pl) != 0)
00152 goto error;
00153 }
00154
00155 ret = 0;
00156
00157 error:
00158 ra_value_free(vh_id);
00159 ra_value_free(vh_name);
00160 ra_value_free(vh_desc);
00161
00162 if (ev_ids)
00163 free(ev_ids);
00164
00165 return ret;
00166 }
00167
00168
00177 int
00178 add_events_orig(class_handle clh, struct plugin_struct *pl, long **ev_ids, long *num)
00179 {
00180 int ret = -1;
00181 value_handle vh_start, vh_end;
00182 long l;
00183 const long *start, *end;
00184
00185 if ((ev_ids == NULL) || (num == NULL))
00186 return -1;
00187 *ev_ids = NULL;
00188 *num = 0;
00189
00190 if ((pl->access.get_ev_info == NULL))
00191 return 0;
00192
00193 vh_start = ra_value_malloc();
00194 vh_end = ra_value_malloc();
00195
00196 if ((*pl->access.get_ev_info)(clh, vh_start, vh_end) != 0)
00197 goto error;
00198 if (!ra_value_is_ok(vh_start) || !ra_value_is_ok(vh_end))
00199 goto error;
00200
00201 start = ra_value_get_long_array(vh_start);
00202 end = ra_value_get_long_array(vh_end);
00203
00204 *num = ra_value_get_num_elem(vh_start);
00205 *ev_ids = (long *)malloc(sizeof(long) * (*num));
00206
00207 for (l = 0; l < *num; l++)
00208 {
00209 (*ev_ids)[l] = ra_class_add_event(clh, start[l], end[l]);
00210 if ((*ev_ids)[l] < 0)
00211 goto error;
00212 }
00213
00214 ret = 0;
00215
00216 error:
00217 ra_value_free(vh_start);
00218 ra_value_free(vh_end);
00219
00220 if (ret != 0)
00221 {
00222 free(*ev_ids);
00223 *ev_ids = NULL;
00224 *num = 0;
00225 }
00226
00227 return ret;
00228 }
00229
00230
00239 int
00240 add_prop_orig(class_handle clh, struct plugin_struct *pl, long *ev_ids, long num_ev)
00241 {
00242 int ret = -1;
00243 long n_prop, l;
00244 value_handle vh, vh_id, vh_type, vh_len, vh_name, vh_desc, vh_unit, vh_use_minmax, vh_min, vh_max;
00245 prop_handle ph;
00246
00247 if (pl->access.get_prop_info == NULL)
00248 return -1;
00249
00250 vh = ra_value_malloc();
00251 vh_id = ra_value_malloc();
00252 vh_name = ra_value_malloc();
00253 vh_desc = ra_value_malloc();
00254 vh_unit = ra_value_malloc();
00255 vh_type = ra_value_malloc();
00256 vh_len = ra_value_malloc();
00257 vh_use_minmax = ra_value_malloc();
00258 vh_min = ra_value_malloc();
00259 vh_max = ra_value_malloc();
00260
00261 n_prop = (*pl->access.get_prop_num)(clh);
00262 for (l = 0; l < n_prop; l++)
00263 {
00264 if ((*pl->access.get_prop_info)(clh, l, vh_id, vh_type, vh_len, vh_name,
00265 vh_desc, vh_unit, vh_use_minmax, vh_min, vh_max) != 0)
00266 {
00267 goto error;
00268 }
00269
00270 if (!ra_value_is_ok(vh_id) || !ra_value_is_ok(vh_type) || !ra_value_is_ok(vh_len))
00271 goto error;
00272
00273 ph = ra_prop_add(clh, ra_value_get_string(vh_id), ra_value_get_long(vh_type),
00274 ra_value_get_long(vh_len), ra_value_get_string(vh_name),
00275 ra_value_get_string(vh_desc), ra_value_get_string(vh_unit),
00276 ra_value_get_long(vh_use_minmax),
00277 ra_value_get_double(vh_min), ra_value_get_double(vh_max));
00278 if (!ph)
00279 goto error;
00280
00281 add_values_orig(ph, pl, ev_ids, num_ev);
00282 }
00283
00284 ret = 0;
00285
00286 error:
00287 ra_value_free(vh);
00288 ra_value_free(vh_id);
00289 ra_value_free(vh_name);
00290 ra_value_free(vh_desc);
00291 ra_value_free(vh_unit);
00292 ra_value_free(vh_type);
00293 ra_value_free(vh_len);
00294 ra_value_free(vh_use_minmax);
00295 ra_value_free(vh_min);
00296 ra_value_free(vh_max);
00297
00298 return ret;
00299 }
00300
00301
00310 int
00311 add_values_orig(prop_handle ph, struct plugin_struct *pl, long *ev_ids, long num_ev)
00312 {
00313 int ret = -1;
00314 value_handle vh, vh_ch, vh_single, vh_ids;
00315 long l, m, n_ch;
00316 const long *ch;
00317
00318 if (pl->access.get_ev_value == NULL)
00319 return 0;
00320
00321 vh = ra_value_malloc();
00322 vh_ch = ra_value_malloc();
00323 vh_single = ra_value_malloc();
00324 vh_ids = ra_value_malloc();
00325
00326 for (l = 0; l < num_ev; l++)
00327 {
00328 if ((ret = (*pl->access.get_ev_value)(ph, l, vh, vh_ch)) != 0)
00329 goto error;
00330
00331 n_ch = ra_value_get_num_elem(vh_ch);
00332 ch = ra_value_get_long_array(vh_ch);
00333
00334 for (m = 0; m < n_ch; m++)
00335 {
00336 ra_value_get_single_elem(vh_single, vh, m);
00337 if ((ret = ra_prop_set_value(ph, ev_ids[l], ch[m], vh_single)) != 0)
00338 goto error;
00339 }
00340 }
00341 ret = 0;
00342
00343 error:
00344 ra_value_free(vh);
00345 ra_value_free(vh_ch);
00346 ra_value_free(vh_single);
00347 ra_value_free(vh_ids);
00348
00349 return ret;
00350 }
00351
00352
00361 int
00362 add_summaries_orig(class_handle clh, struct plugin_struct *pl)
00363 {
00364 int ret = -1;
00365 value_handle vh_id, vh_ch, vh_name, vh_desc;
00366 long l, n_sum;
00367 sum_handle sh;
00368
00369 if ((pl->access.get_sum_num == NULL) || (pl->access.get_sum_info == NULL)
00370 || (pl->access.get_sum_ch_info == NULL))
00371 {
00372 return 0;
00373 }
00374
00375 n_sum = (*pl->access.get_sum_num)(clh);
00376 if (n_sum <= 0)
00377 return 0;
00378
00379 vh_id = ra_value_malloc();
00380 vh_ch = ra_value_malloc();
00381 vh_name = ra_value_malloc();
00382 vh_desc = ra_value_malloc();
00383
00384 for (l = 0; l < n_sum; l++)
00385 {
00386 ret = (*pl->access.get_sum_info)(clh, l, vh_id, vh_name, vh_desc, vh_ch);
00387 if (ret != 0)
00388 goto error;
00389
00390 if (!ra_value_is_ok(vh_id))
00391 goto error;
00392
00393 sh = ra_sum_add(clh, ra_value_get_string(vh_id), ra_value_get_string(vh_name), ra_value_get_string(vh_desc));
00394 if (!sh)
00395 goto error;
00396
00397 if ((ret = add_sum_ch(clh, sh, l, ra_value_get_num_elem(vh_ch), ra_value_get_long_array(vh_ch), pl)) != 0)
00398 goto error;
00399 }
00400
00401 ret = 0;
00402
00403 error:
00404 ra_value_free(vh_id);
00405 ra_value_free(vh_ch);
00406 ra_value_free(vh_name);
00407 ra_value_free(vh_desc);
00408
00409 return ret;
00410 }
00411
00412
00413 int
00414 add_sum_ch(class_handle clh, sum_handle sh, long sum_num, long num_ch, const long *ch, struct plugin_struct *pl)
00415 {
00416 int ret;
00417 long l;
00418 value_handle vh_fid_offset, vh_dim_unit, vh_dim_name;
00419
00420 if (pl->access.get_sum_ch_info == NULL)
00421 return -1;
00422
00423 vh_fid_offset = ra_value_malloc();
00424 vh_dim_unit = ra_value_malloc();
00425 vh_dim_name = ra_value_malloc();
00426
00427 for (l = 0; l < num_ch; l++)
00428 {
00429 ret = (*pl->access.get_sum_ch_info)(clh, sum_num, ch[l], vh_fid_offset, vh_dim_unit, vh_dim_name);
00430 if (ret != 0)
00431 goto error;
00432
00433 ret = ra_sum_add_ch(sh, ch[l], ra_value_get_long(vh_fid_offset), ra_value_get_num_elem(vh_dim_unit),
00434 ra_value_get_string_array(vh_dim_unit), ra_value_get_string_array(vh_dim_name));
00435 if (ret != 0)
00436 goto error;
00437
00438 if ((ret = add_sum_part_orig(sh, num_ch, ra_value_get_num_elem(vh_dim_unit), pl)) != 0)
00439 goto error;
00440 }
00441
00442 ret = 0;
00443
00444 error:
00445 ra_value_free(vh_fid_offset);
00446 ra_value_free(vh_dim_unit);
00447 ra_value_free(vh_dim_name);
00448
00449 return ret;
00450 }
00451
00452
00453 int
00454 add_sum_part_orig(sum_handle sh, long n_ch, long n_dim, struct plugin_struct *pl)
00455 {
00456 int ret = -1;
00457 value_handle vh_events, vh;
00458 long part_id;
00459 long l, m;
00460
00461 if ((pl->access.get_sum_events == NULL) || (pl->access.get_sum_part_data == NULL))
00462 return -1;
00463
00464 vh_events = ra_value_malloc();
00465 vh = ra_value_malloc();
00466
00467 if ((ret = (*pl->access.get_sum_events)(sh, vh_events)) != 0)
00468 return -1;
00469 if (ra_value_get_num_elem(vh_events) <= 0)
00470 {
00471 ret = 0;
00472 goto clean;
00473 }
00474
00475 part_id = ra_sum_add_part(sh, ra_value_get_num_elem(vh_events), ra_value_get_long_array(vh_events));
00476 if (part_id < 0)
00477 goto clean;
00478
00479 for (l = 0; l < n_ch; l++)
00480 {
00481 for (m = 0; m < n_dim; m++)
00482 {
00483 if ((*pl->access.get_sum_part_data)(sh, l, m, vh) != 0)
00484 goto clean;
00485
00486 if ((ret = ra_sum_set_part_data(sh, part_id, l, m, vh)) != 0)
00487 goto clean;
00488 }
00489 }
00490
00491 ret = 0;
00492
00493 clean:
00494 ra_value_free(vh);
00495 ra_value_free(vh_events);
00496
00497 return ret;
00498 }
00499
00500
00510 int
00511 do_post_processing(meas_handle mh, eval_handle eh)
00512 {
00513 long l;
00514 value_handle vh;
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542 vh = ra_value_malloc();
00543 ra_class_get(eh, "heartbeat", vh);
00544 for (l = 0; l < ra_value_get_num_elem(vh); l++)
00545 {
00546 class_handle clh = (class_handle)(ra_value_get_voidp_array(vh)[l]);
00547 if (ra_prop_get(clh, "qrs-pos") != NULL)
00548 post_process_ecg(mh, eh, clh);
00549 }
00550 ra_value_free(vh);
00551
00552 return 0;
00553 }
00554
00555
00563 void
00564 post_process_ecg(meas_handle mh, eval_handle eh, class_handle clh)
00565 {
00566 ra_handle ra;
00567 plugin_handle p;
00568 value_handle vh;
00569 struct proc_info *pi;
00570
00571 ra = ra_lib_handle_from_any_handle(mh);
00572 p = ra_plugin_get_by_name(ra, "ecg", 1);
00573 if (!p)
00574 return;
00575
00576 pi = (struct proc_info *)ra_proc_get(mh, p, NULL);
00577 vh = ra_value_malloc();
00578 ra_value_set_voidp(vh, clh);
00579 ra_lib_set_option(pi, "clh", vh);
00580 ra_value_free(vh);
00581 ra_proc_do(pi);
00582 ra_proc_free(pi);
00583 }
00584
00585