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

Linux Cross Reference
Tina6/tina-libs/tina/math/mathQPR_io.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 <math.h>
 44 #include <stdlib.h>
 45 #include <stdio.h>
 46 #include <strings.h>
 47 
 48 #include <tina/math/mathMatv_mat.h>
 49 #include <tina/math/mathDef.h>
 50 #include <tina/math/mathPro.h>
 51 #include <tina/math/math_QPRDef.h>
 52 #include <tina/math/mathQPR_alloc.h>
 53 #include <tina/math/mathQPR_sample.h>
 54 #include <tina/math/mathQPR_analysis.h>
 55 
 56 #include "mathQPR_io.h"
 57 
 58 void qpr_record_results(QPRData *data, char *filename)
 59 {
 60    FILE *fp;
 61    int k, b;
 62    QPRDataBlock *block;
 63 
 64    if ((fp = fopen(filename, "at")) == NULL)
 65    {
 66       error("qpr_record_results: Unable to open file\n", non_fatal);
 67       return;
 68    }
 69 
 70    /* File format: block number, class number, quantity, sigma, fit */
 71 
 72    for (b=0; b<data->block_count; b++)
 73    {
 74       block = data->blocks[b];
 75       for (k=0; k<data->class_count; k++)
 76       {
 77          fprintf(fp, "%i, %i, %f, %f, %f\n", b, k, block->class_quantities[k],
 78                  sqrt(block->class_cov->el[k][k]), block->model_fit);
 79       }
 80    }
 81 
 82    fflush(fp);
 83    fclose(fp);
 84 }
 85 
 86 void qpr_save_class(QPRModel *model, int clss, char *filename)
 87 {
 88    FILE *fp;
 89    int *subclasses, components, k, X;
 90    double pXk, var_pXk;
 91 
 92    if ((fp = fopen(filename, "wb")) == NULL)
 93    {
 94       error("qpr_save_class: unable to open file\n", non_fatal);
 95       return;
 96    }
 97 
 98    subclasses = ralloc(sizeof(int)*model->component_count);
 99 
100    components = 0;
101    for (k=0; k<model->component_count; k++)
102    {
103       if (model->class_map->el[clss][k] != 0)
104       {
105          subclasses[components] = k;
106          components++;
107       }
108    }
109 
110    for (k=0; k<components; k++)
111    {
112       for (X=0; X<model->bin_count; X++)
113       {
114          if (model->components[X] != NULL)
115          {
116             pXk = model->components[X][subclasses[k]].pXk;
117             var_pXk = model->components[X][subclasses[k]].var_pXk;
118             fwrite(&k, sizeof(int), 1, fp);
119             fwrite(&X, sizeof(int), 1, fp);
120             fwrite(&pXk, sizeof(double), 1, fp);
121             fwrite(&var_pXk, sizeof(double), 1, fp);
122          }
123       }
124    }
125 
126    fclose(fp);
127    rfree(subclasses);
128 }
129 
130 void qpr_save_simple_class(char *filename)
131 {
132    FILE *fp;
133    int k, X;
134    double pXk, var_pXk;
135 
136    if ((fp = fopen(filename, "wb")) == NULL)
137    {
138       error("qpr_save_class: unable to open file\n", non_fatal);
139       return;
140    }
141 
142    k = 0; X = 0;
143    pXk = 0.999; var_pXk = 0;
144    fwrite(&k, sizeof(int), 1, fp);
145    fwrite(&X, sizeof(int), 1, fp);
146    fwrite(&pXk, sizeof(double), 1, fp);
147    fwrite(&var_pXk, sizeof(double), 1, fp);
148 
149    k = 0; X = 1;
150    pXk = 0.001; var_pXk = 0;
151    fwrite(&k, sizeof(int), 1, fp);
152    fwrite(&X, sizeof(int), 1, fp);
153    fwrite(&pXk, sizeof(double), 1, fp);
154    fwrite(&var_pXk, sizeof(double), 1, fp);
155 
156    k = 1; X = 0;
157    pXk = 0.001; var_pXk = 0;
158    fwrite(&k, sizeof(int), 1, fp);
159    fwrite(&X, sizeof(int), 1, fp);
160    fwrite(&pXk, sizeof(double), 1, fp);
161    fwrite(&var_pXk, sizeof(double), 1, fp);
162 
163    k = 1; X = 1;
164    pXk = 0.999; var_pXk = 0;
165    fwrite(&k, sizeof(int), 1, fp);
166    fwrite(&X, sizeof(int), 1, fp);
167    fwrite(&pXk, sizeof(double), 1, fp);
168    fwrite(&var_pXk, sizeof(double), 1, fp);
169    
170    fclose(fp);
171 }
172 
173 void qpr_load_class(QPRModel *model, int clss, int first_subclass, char *filename)
174 {
175    FILE *fp;
176    int k, X;
177    double pXk, var_pXk;
178 
179    if ((fp = fopen(filename, "rb")) == NULL)
180    {
181       error("qpr_load_class: unable to open file\n", non_fatal);
182       return;
183    }
184 
185    while (true)
186    {   
187       fread(&k, sizeof(int), 1, fp);
188       if (feof(fp)) break;
189       fread(&X, sizeof(int), 1, fp);
190       fread(&pXk, sizeof(double), 1, fp);
191       fread(&var_pXk, sizeof(double), 1, fp);
192       
193       if (first_subclass+k >= model->component_count)
194       {
195          error("qpr_load_class: Model in file is wrong shape (component overflow) \n", non_fatal);
196          fclose(fp);
197          return;
198       }
199       if (X < 0)
200       {
201          error("qpr_load_class: Model in file is wrong shape (X underflow) \n", non_fatal);
202          fclose(fp);
203          return;
204       }
205       if (X >= model->bin_count)
206       {
207          error("qpr_load_class: Model in file is wrong shape (X overflow) \n", non_fatal);
208          fclose(fp);
209          return;
210       }
211 
212       qpr_model_fill(model, X, first_subclass+k, clss, pXk);
213       if (model->components[X] != NULL)
214       {
215          model->components[X][first_subclass+k].var_pXk = var_pXk;
216       }
217    }
218    
219    /* normalisation should not really be necessary if the
220       model from the file was already normalised. */
221    qpr_normalise_model(model, false);
222    
223    fclose(fp);
224 }
225 
226 void qpr_debug_model(QPRModel *model, int index, char *filename)
227 {
228    int X, k;
229    double pXk;
230    FILE *fp;
231    
232    if ((fp = fopen(filename, "at")) == NULL)
233    {
234       error("qpr_debug_model: Unable to open file\n", non_fatal);
235       return;
236    }
237 
238    for (k=0; k<model->component_count; k++)
239    {
240       fprintf(fp, "%i, %i, ", index, k);
241       for (X=0; X<model->bin_count; X++)
242       {
243          pXk = 0.0;
244          if (model->components[X] != NULL)
245          {
246             pXk = model->components[X][k].pXk;
247          }
248          fprintf(fp, "%f, ", pXk);
249       }
250       fprintf(fp, "\n");
251    }
252 
253    fflush(fp);
254    fclose(fp);
255 }
256 
257 void qpr_load_class_map(QPRModel *model, char *filename)
258 {
259    int clss, subclass, element;
260    int fsubclass_count, fclass_count;
261    FILE *fp;
262 
263    if ((fp = fopen(filename, "rt")) == NULL)
264    {
265       error("qpr_load_class_map: Unable to open file\n", non_fatal);
266       return;
267    }
268 
269    fscanf(fp, "%i, %i\n", &fclass_count, &fsubclass_count);
270 
271    if (fsubclass_count != model->component_count ||
272        fclass_count != model->class_count)
273    {
274       error("qpr_load_class_map: Class map file is wrong shape for model\n", non_fatal);
275       fclose(fp);
276       return;
277    }
278 
279    for (clss=0; clss<model->class_count; clss++)
280    {
281       for (subclass=0; subclass<model->component_count; subclass++)
282       {
283          fscanf(fp, "%i, ", &element);
284          model->class_map->el[clss][subclass] = (double)element;
285       }
286       fscanf(fp, "\n");
287    }
288 
289    fclose(fp);
290 }
291 
292 static int hamming_distance(unsigned int X, unsigned int Y)
293 {
294    unsigned int difference;
295    int distance;
296 
297    difference = X^Y;
298    distance = 0;
299 
300    while (difference > 0)
301    {
302       distance += difference & 0x01;
303       difference = difference>>1;
304    }
305 
306    return distance;
307 }
308 
309 void qpr_save_cov(Mat *cov, char *filename)
310 {
311    int i, j;
312    
313    FILE *fp;
314    shistogram **hists;
315 
316    hists = hist_vec();
317    if (hists[198] != NULL) hfree(hists[198]);
318    hists[198] = hbook1(198, "Diagonal", -2, 2, 10);
319    if (hists[199] != NULL) hfree(hists[199]);
320    hists[199] = hbook1(199, "Off-diagonal", -2, 2, 10);
321 
322    if ((fp = fopen(filename, "wt")) == NULL)
323    {
324       error("qpr_save_cov: Unable to open file\n", non_fatal);
325       return;
326    }
327 
328    /* full matrix */
329    for (i=0; i<cov->m; i++)
330    {
331       for (j=0; j<cov->n; j++)
332       {
333          fprintf(fp, "%f, ", cov->el[i][j]);
334       }
335       fprintf(fp, "\n");
336    }
337 
338    fprintf(fp, "\n");
339 
340    /* diagonal terms only */
341    for (i=0; i<cov->m; i++)
342    {
343       fprintf(fp, "%i, %f\n", hamming_distance(0, i), cov->el[i][i]);
344       hfill1(hists[198], cov->el[i][i], 1);
345    }
346 
347    fprintf(fp, "\n");
348    fprintf(fp, "\n");
349 
350    /* off-diagonal terms only */
351    for (i=0; i<cov->m; i++)
352    {
353       for (j=0; j<cov->n; j++)
354       {
355          if (i != j)
356          {
357             fprintf(fp, "%i, %f\n", (int)(hamming_distance(0, i)+hamming_distance(0, j))/2, cov->el[i][j]);
358             hfill1(hists[199], cov->el[i][j], 1);
359          }
360       }
361    }
362 
363    fprintf(fp, "\n");
364 
365    fflush(fp);
366    fclose(fp);
367 }
368 

~ [ 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.