~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

Linux Cross Reference
Tina6/tina-libs/tina/math/mathQPR_alloc.c

Version: ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /**********
  2  *
  3  * This file is part of the TINA Open Source Image Analysis Environment
  4  * henceforth known as TINA
  5  *
  6  * TINA is free software; you can redistribute it and/or modify
  7  * it under the terms of the GNU General Public License as
  8  * published by the Free Software Foundation.
  9  *
 10  * TINA is distributed in the hope that it will be useful,
 11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
 12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 13  * GNU General Public License for more details.
 14  *
 15  * You should have received a copy of the GNU General Public License
 16  * along with TINA; if not, write to the Free Software Foundation, Inc.,
 17  * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 18  *
 19  * ANY users of TINA who require exemption from the existing licence must
 20  * negotiate a new licence with Dr. Neil.A.Thacker, the sole agent for
 21  * the University of Manchester.
 22  *
 23  **********
 24  *
 25  * Program :    TINA
 26  * File    :  $Source:  $
 27  * Date    :  $Date:  $
 28  * Version :  $Revision: $
 29  *
 30  * Author  : Paul D Tar
 31  *
 32  * Notes : 
 33  *
 34  *
 35  *
 36  *********
 37 */
 38 
 39 #if HAVE_CONFIG_H
 40 #include <config.h>
 41 #endif
 42 
 43 #include <stdio.h>
 44 #include <stdlib.h>
 45 #include <math.h>
 46 
 47 #include <tina/math/mathUtil_rand.h>
 48 #include <tina/math/mathMatv_mat.h>
 49 #include <tina/sys/sys_MemDef.h>
 50 #include <tina/sys/sysDef.h>
 51 #include <tina/sys/sysPro.h>
 52 
 53 #include "math_QPRDef.h"
 54 #include "mathQPR_alloc.h"
 55 #include "mathQPR_sample.h"
 56 #include "mathQPR_utils.h"
 57 
 58 QPRModelBin *qpr_alloc_model_bin()
 59 {
 60    QPRModelBin *bin;
 61 
 62    bin = ralloc(sizeof(QPRModelBin));
 63    if (bin != NULL)
 64    {
 65       bin->pXk = 0.0;
 66       bin->var_pXk = 0.0;
 67    }
 68 
 69    return bin;
 70 }
 71 
 72 QPRModelBin *qpr_copy_model_bin(QPRModelBin *bin)
 73 {
 74    QPRModelBin *copy;
 75 
 76    copy = ralloc(sizeof(QPRModelBin));
 77    if (copy != NULL)
 78    {
 79       copy->pXk = bin->pXk;
 80       copy->var_pXk = bin->var_pXk;
 81    }
 82 
 83    return copy;
 84 }
 85 
 86 void qpr_free_model_bin(QPRModelBin *bin)
 87 {
 88    rfree(bin);
 89 }
 90 
 91 QPRDataBin *qpr_alloc_data_bin()
 92 {
 93    QPRDataBin *bin;
 94 
 95    bin = ralloc(sizeof(QPRDataBin));
 96    if (bin != NULL)
 97    {
 98       bin->pkX = 0.0;
 99       bin->pknX = 0.0;
100    }
101 
102    return bin;
103 }
104 
105 QPRDataBin *qpr_copy_data_bin(QPRDataBin *bin)
106 {
107    QPRDataBin *copy;
108 
109    copy = ralloc(sizeof(QPRDataBin));
110    if (copy != NULL)
111    {
112       copy->pkX = bin->pkX;
113       copy->pknX = bin->pknX;
114    }
115 
116    return copy;
117 }
118 
119 void qpr_free_data_bin(QPRDataBin *bin)
120 {
121    rfree(bin);
122 }
123 
124 QPRModel *qpr_alloc_model(int bin_count, int component_count, int class_count)
125 {
126    QPRModel *model;
127    int X, k;
128 
129    if (bin_count < 1)
130    {
131       error("qpr_alloc_model: Need at least 1 bin\n", non_fatal);
132       return NULL;
133    }
134    if (component_count < 1)
135    {
136       error("qpr_alloc_model: Need at least 1 component\n", non_fatal);
137       return NULL;
138    }
139    if (class_count < 1)
140    {
141       error("qpr_alloc_model: Need at least 1 class\n", non_fatal);
142       return NULL;
143    }
144      
145    model = ralloc(sizeof(QPRModel));
146    if (model == NULL)
147    {
148       return NULL;
149    }
150 
151    model->bin_count = bin_count;
152    model->component_count = component_count;
153    model->class_count = class_count;
154    model->class_map = mat_make(class_count, component_count);
155    if (model->class_map == NULL)
156    {
157       qpr_free_model(model);
158       return NULL;
159    }
160    
161    model->components = (QPRModelBin**)pvector_alloc(0, model->bin_count);
162    if (model->components == NULL)
163    {
164       qpr_free_model(model);
165       return NULL;
166    }
167 
168    for (X=0; X<model->bin_count; X++)
169    {
170       model->components[X] = NULL;
171    }
172 
173    model->component_types = ivector_alloc(0, model->component_count);
174    if (model->component_types == NULL)
175    {
176       qpr_free_model(model);
177       return NULL;
178    }
179 
180    for (k=0; k<model->component_count; k++)
181    {
182       model->component_types[k] = QPR_NORMAL_COMPONENT;
183    }
184 
185    return model;
186 }
187 
188 QPRModel *qpr_alloc_monte_carlo_model(QPRModel *source, double *quantities)
189 {
190    QPRModel *model;
191    int k, X;
192    double mX, hX;
193 
194    model = qpr_alloc_model(source->bin_count,
195                            source->component_count,
196                            source->class_count);
197 
198    if (model != NULL)
199    {
200       tarray_copy_inplace(model->class_map->el, source->class_map->el, 0, 0,
201                           model->class_map->m, model->class_map->n, double);
202 
203       for (X=0; X<source->bin_count; X++)
204       {
205          if (source->components[X] != NULL)
206          {
207             for (k=0; k<source->component_count; k++)
208             {
209                mX = source->components[X][k].pXk * quantities[k];
210                hX = rand_poisson(mX);
211                qpr_model_fill(model, X, k, QPR_CLASS_UNKNOWN, hX);
212             }
213          }
214       }
215 
216       qpr_normalise_model(model, true);
217    }
218 
219    return model;
220 }
221 
222 QPRModel *qpr_alloc_2_class_model(QPRModel *model_a, QPRModel *model_b)
223 {
224    QPRModel *model;
225    int bin_count, component_count;
226    int X, k;
227 
228    bin_count = model_a->bin_count;
229    if (model_b->bin_count != bin_count)
230    {
231       error("qpr_alloc_2_class_model: Model A and B must contain same number of bins\n", non_fatal);
232       return NULL;
233    }
234 
235    component_count = model_a->component_count + model_b->component_count;
236 
237    model = qpr_alloc_model(bin_count, component_count, 2);
238 
239    for (X=0; X<bin_count; X++)
240    {
241       model->components[X] = ralloc(sizeof(QPRModelBin)*component_count);
242       for (k=0; k<component_count; k++)
243       {
244          model->components[X][k].pXk = 0;
245          model->components[X][k].var_pXk = 0;
246       }
247    }
248 
249    for (k=0; k<model_a->component_count; k++)
250    {
251       model->class_map->el[0][k] = 1.0;
252       for (X=0; X<bin_count; X++) if (model_a->components[X] != NULL)
253       {
254          model->components[X][k].pXk = model_a->components[X][k].pXk;
255          model->components[X][k].var_pXk = model_a->components[X][k].var_pXk;
256       }
257    }
258 
259    for (k=model_a->component_count; k<model->component_count; k++)
260    {
261       model->class_map->el[1][k] = 1.0;
262       for (X=0; X<bin_count; X++) if (model_b->components[X] != NULL)
263       {
264          model->components[X][k].pXk = model_b->components[X][k-model_a->component_count].pXk;
265          model->components[X][k].var_pXk = model_b->components[X][k-model_a->component_count].var_pXk;
266       }
267    }
268 
269    return model;
270 }
271 
272 QPRModel *qpr_resize_model(QPRModel *model, int component_count)
273 {
274    QPRModel *resized;
275    int k, X, lower_component_count;
276 
277    resized = ralloc(sizeof(QPRModel));
278    if (resized == NULL)
279    {
280       return NULL;
281    }
282 
283    lower_component_count = component_count < model->component_count ?
284                            component_count : model->component_count;
285 
286    resized->bin_count = model->bin_count;
287    resized->component_count = component_count; /* Resize field */
288    resized->class_count = model->class_count;
289 
290    resized->class_map = mat_make(resized->class_count, resized->component_count);
291    if (resized->class_map == NULL)
292    {
293       qpr_free_model(resized);
294       return NULL;
295    }
296    tarray_copy_inplace(resized->class_map->el, model->class_map->el, 0, 0,
297                        resized->class_map->m, lower_component_count, double);
298 
299    resized->components = (QPRModelBin**)pvector_alloc(0, resized->bin_count);
300    if (resized->components == NULL)
301    {
302       qpr_free_model(resized);
303       return NULL;
304    }
305 
306    for (X=0; X<resized->bin_count; X++)
307    {
308       if (model->components[X] != NULL)
309       {
310          resized->components[X] = ralloc(sizeof(QPRModelBin)*resized->component_count);
311          if (resized->components[X] == NULL)
312          {
313             qpr_free_model(resized);
314             return NULL;
315          }
316          for (k=0; k<resized->component_count; k++)
317          {
318             if (k < model->component_count)
319             {
320                resized->components[X][k].pXk = model->components[X][k].pXk;
321                resized->components[X][k].var_pXk = model->components[X][k].var_pXk;
322             }
323             else
324             {
325                resized->components[X][k].pXk = 0;
326                resized->components[X][k].var_pXk = 0;
327             }
328          }
329       }
330       else
331       {
332          resized->components[X] = NULL;
333       }
334    }
335 
336    resized->component_types = ivector_alloc(0, resized->component_count);
337    if (resized->component_types == NULL)
338    {
339       qpr_free_model(resized);
340       return NULL;
341    }
342 
343    for (k=0; k<model->component_count; k++)
344    {
345       if (k < model->component_count)
346       {
347          resized->component_types[k] = model->component_types[k];
348       }
349       else
350       {
351          resized->component_types[k] = QPR_NORMAL_COMPONENT;
352       }
353    }
354 
355    return resized;
356 }
357 
358 QPRModel *qpr_copy_model(QPRModel *model)
359 {
360    return qpr_resize_model(model, model->component_count);
361 }
362 
363 void qpr_free_model(QPRModel *model)
364 {
365    int X;
366 
367    if (model->class_map != NULL)
368    {
369       mat_free(model->class_map);
370    }
371    else
372    {
373       error("qpr_free_model: found NULL class_map\n", non_fatal);
374    }
375    
376    if (model->components != NULL)
377    {
378       for (X=0; X<model->bin_count; X++)
379       {
380          if (model->components[X] != NULL)
381          {
382             rfree(model->components[X]);
383          }
384          else
385          {
386             /* This is fine, as not all X bins are expected to be allocated */
387          }
388       }
389       pvector_free(model->components, 0);
390    }
391    else
392    {
393       error("qpr_free_model: found NULL components\n", non_fatal);
394    }
395 
396    if (model->component_types != NULL)
397    {
398       ivector_free(model->component_types, 0);
399    }
400    else
401    {
402       error("qpr_free_model: found NULL component_types\n", non_fatal);
403    }
404    
405    rfree(model);
406 }
407 
408 QPRDataBlock *qpr_alloc_data_block(int bin_count, int component_count, int class_count,
409                                    void *source_specific, void (*free_source_specific)(void*))
410 {
411    QPRDataBlock *block;
412    int X, k;
413 
414    block = ralloc(sizeof(QPRDataBlock));
415    if (block == NULL)
416    {
417       return NULL;
418    }
419 
420    block->bin_count = bin_count;
421 
422    block->hist = dvector_alloc(0, block->bin_count);
423    if (block->hist == NULL)
424    {
425       qpr_free_data_block(block);
426       return NULL;
427    }
428 
429    block->components = (QPRDataBin**)pvector_alloc(0, block->bin_count);
430    if (block->components == NULL)
431    {
432       qpr_free_data_block(block);
433       return NULL;
434    }
435 
436    for (X=0; X<bin_count; X++)
437    {
438       block->hist[X] = 0;
439       block->components[X] = NULL;
440    }
441 
442    block->component_quantities = dvector_alloc(0, component_count);
443    if (block->component_quantities == NULL)
444    {
445       qpr_free_data_block(block);
446       return NULL;
447    }
448 
449    for (k=0; k<component_count; k++) block->component_quantities[k] = 0;
450 
451    block->component_cov_data = mat_make(component_count, component_count);
452    if (block->component_cov_data == NULL)
453    {
454       qpr_free_data_block(block);
455       return NULL;
456    }
457 
458    block->component_cov_model = mat_make(component_count, component_count);
459    if (block->component_cov_model == NULL)
460    {
461       qpr_free_data_block(block);
462       return NULL;
463    }
464 
465    block->component_cov_total = mat_make(component_count, component_count);
466    if (block->component_cov_total == NULL)
467    {
468       qpr_free_data_block(block);
469       return NULL;
470    }
471 
472    block->class_quantities = dvector_alloc(0, class_count);
473    if (block->class_quantities == NULL)
474    {
475       qpr_free_data_block(block);
476       return NULL;
477    }
478 
479    for (k=0; k<class_count; k++) block->class_quantities[k] = 0;
480 
481    block->class_cov = mat_make(class_count, class_count);
482    if (block->class_cov == NULL)
483    {
484       qpr_free_data_block(block);
485       return NULL;
486    }
487 
488    block->model_fit = 0;
489    block->unknown = 0;
490    block->poorly_modelled = 0;
491    block->total_quantity = 0;
492 
493    block->source_specific = source_specific;
494    block->free_source_specific = free_source_specific;
495 
496    return block;
497 }
498 
499 void qpr_free_data_block(QPRDataBlock *block)
500 {
501    int X;
502 
503    if (block->hist != NULL)
504    {
505       dvector_free(block->hist, 0);
506    }
507    else
508    {
509       error("qpr_free_data_block: found NULL hist\n", non_fatal);
510    }
511 
512    if (block->components != NULL)
513    {
514       for (X=0; X<block->bin_count; X++)
515       {
516          if (block->components[X] != NULL)
517          {
518             rfree(block->components[X]);
519          }
520          else
521          {
522             /* This is fine, as not all X bins are expected to be allocated */
523          }
524       }
525       pvector_free(block->components, 0);
526    }
527    else
528    {
529       error("qpr_free_data_block: found NULL components\n", non_fatal);
530    }
531 
532    if (block->component_quantities != NULL)
533    {
534       dvector_free(block->component_quantities, 0);
535    }
536    else
537    {
538       error("qpr_free_data_block: found NULL component_quantities\n", non_fatal);
539    }
540 
541    if (block->component_cov_data != NULL)
542    {
543       mat_free(block->component_cov_data);
544    }
545    else
546    {
547       error("qpr_free_data_block: found NULL component_cov_data\n", non_fatal);
548    }
549 
550    if (block->component_cov_model != NULL)
551    {
552       mat_free(block->component_cov_model);
553    }
554    else
555    {
556       error("qpr_free_data_block: found NULL component_cov_model\n", non_fatal);
557    }
558 
559    if (block->component_cov_total != NULL)
560    {
561       mat_free(block->component_cov_total);
562    }
563    else
564    {
565       error("qpr_free_data_block: found NULL component_cov_total\n", non_fatal);
566    }
567 
568    if (block->class_quantities != NULL)
569    {
570       dvector_free(block->class_quantities, 0);
571    }
572    else
573    {
574       error("qpr_free_data_block: found NULL class_quantities\n", non_fatal);
575    }
576 
577    if (block->class_cov != NULL)
578    {
579       mat_free(block->class_cov);
580    }
581    else
582    {
583       error("qpr_free_data_block: found NULL class_cov\n", non_fatal);
584    }
585 
586    if (block->source_specific != NULL)
587    {
588       if (block->free_source_specific != NULL)
589       {
590          (block->free_source_specific)(block->source_specific);
591       }
592       else
593       {
594          error("qpr_free_data_block: found source_specific with no free function\n", non_fatal);
595       }
596    }
597 
598    rfree(block);
599 }
600 
601 QPRData *qpr_alloc_data(int bin_count, int component_count, int class_count, int block_count,
602                         void *source_specific, void (*free_source_specific)(void*))
603 {
604    QPRData *data;
605    int r, k;
606 
607    if (bin_count < 1)
608    {
609       error("qpr_alloc_data: Must have at least 1 bin\n", non_fatal);
610       return NULL;
611    }
612 
613    if (component_count < 1)
614    {
615       error("qpr_alloc_data: Must have at least 1 component\n", non_fatal);
616       return NULL;
617    }
618 
619    if (class_count < 1)
620    {
621       error("qpr_alloc_data: Must have at least 1 class\n", non_fatal);
622       return NULL;
623    }
624 
625    if (block_count < 1)
626    {
627       error("qpr_alloc_data: Must have at least 1 block\n", non_fatal);
628       return NULL;
629    }
630 
631    data = ralloc(sizeof(QPRData));
632    if (data == NULL)
633    {
634       return NULL;
635    }
636 
637    data->bin_count = bin_count;
638    data->component_count = component_count;
639    data->class_count = class_count;
640    data->block_count = block_count;
641    
642    data->blocks = (QPRDataBlock**)pvector_alloc(0, data->block_count);
643    if (data->blocks == NULL)
644    {
645       qpr_free_data(data);
646       return NULL;
647    }
648 
649    for (r=0; r<data->block_count; r++)
650    {
651       data->blocks[r] = qpr_alloc_data_block(data->bin_count, data->component_count,
652                                              data->class_count, NULL, NULL);
653       if (data->blocks[r] == NULL)
654       {
655          qpr_free_data(data);
656          return NULL;
657       }
658    }
659 
660    data->class_quantities = dvector_alloc(0, data->class_count);
661    if (data->class_quantities == NULL)
662    {
663       qpr_free_data(data);
664       return NULL;
665    }
666 
667    for (k=0; k<class_count; k++) data->class_quantities[k] = 0;
668 
669    data->class_cov = mat_make(class_count, class_count);
670    if (data->class_cov == NULL)
671    {
672       qpr_free_data(data);
673       return NULL;
674    }
675    
676    data->model_fit = 0;
677    data->unknown = 0;
678    data->poorly_modelled = 0;
679    data->total_quantity = 0;
680 
681    data->source_specific = source_specific;
682    data->free_source_specific = free_source_specific;
683    
684    return data;
685 }
686 
687 void qpr_free_data(QPRData *data)
688 {
689    int r;
690 
691    if (data->blocks != NULL)
692    {
693       for (r=0; r<data->block_count; r++)
694       {
695          if (data->blocks[r] != NULL)
696          {
697             qpr_free_data_block(data->blocks[r]);
698          }
699          else
700          {
701             error("qpr_free_data: found NULL blocks element\n", non_fatal);
702          }
703       }
704       pvector_free(data->blocks, 0);
705    }
706    else
707    {
708       error("qpr_free_data: found NULL blocks\n", non_fatal);
709    }
710 
711    if (data->class_quantities != NULL)
712    {
713       dvector_free(data->class_quantities, 0);
714    }
715    else
716    {
717       error("qpr_free_data: found NULL class_quantities\n", non_fatal);
718    }
719 
720    if (data->class_cov != NULL)
721    {
722       mat_free(data->class_cov);
723    }
724    else
725    {
726       error("qpr_free_data: found NULL class_cov\n", non_fatal);
727    }
728 
729    if (data->source_specific != NULL)
730    {
731       if (data->free_source_specific != NULL)
732       {
733          (data->free_source_specific)(data->source_specific);
734       }
735       else
736       {
737          error("qpr_free_data: found source_specific with no free function\n", non_fatal);
738       }
739    }
740 
741    rfree(data);
742 }
743 
744 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~ [ freetext search ] ~ [ file search ] ~

This page was automatically generated by the LXR engine.
Visit the LXR main site for more information.