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

Linux Cross Reference
Tina6/tina-tools/tinatool/tlbase/tlbaseHist_tool.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: tlbaseHist_tool.c $
 27  * Date    :  $Date: 2012/06/22 11:30 $
 28  * Version :  $Revision: 1.2 $
 29  *
 30  * Author  : Legacy TINA modified NAT
 31  *
 32  * Notes : written by Simon Crossley 2002 and put in libraries by NAT 6 years later
 33  *
 34  *
 35  *
 36  *********
 37 */
 38 #include "tlbaseHist_tool.h"
 39 
 40 #if HAVE_CONFIG_H
 41 #include <config.h>
 42 #endif
 43 
 44 #include <math.h>
 45 #include <stdio.h>
 46 #include <limits.h>
 47 #include <string.h>
 48 #include <tina/sys/sysDef.h>
 49 #include <tina/sys/sysPro.h>
 50 #include <tina/math/mathDef.h>
 51 #include <tina/math/mathPro.h>
 52 #include <tina/file/fileDef.h>
 53 #include <tina/file/filePro.h>
 54 #include <tina/image/imgDef.h>
 55 #include <tina/image/imgPro.h>
 56 #include <tinatool/wdgts/wdgtsDef.h>
 57 #include <tinatool/wdgts/wdgtsPro.h>
 58 #include <tinatool/draw/drawDef.h>
 59 #include <tinatool/draw/drawPro.h>
 60 #include <tinatool/tlbase/tlbaseDef.h>
 61 #include <tinatool/tlbase/tlbasePro.h>
 62 
 63 
 64 static void *pframe2 = NULL;
 65 static int hnum = 0;
 66 static void *pframe3 = NULL;
 67 static char name_str[256] = "";
 68 static double cut1 = 5.0,  cut2 = 3.0;
 69 static char file_str[256] = "";
 70 
 71 static Tv *graph_tv = NULL;
 72 static void *graph_tool = NULL;
 73 static shistogram **hist;
 74 
 75 static void update_name_str(void)
 76 {
 77   if (hist[hnum]== NULL)
 78     {
 79       strcpy(name_str, "");
 80       tw_sglobal_reset(pframe3);
 81       return;
 82     }
 83 
 84   strcpy((char *)&name_str[0], hist[hnum]->title);
 85   tw_sglobal_reset(pframe3);
 86 }
 87 
 88 static void graph_tv_proc(void)
 89 {
 90   extern void *display_tool();
 91 
 92 
 93   graph_tool = (void *)display_tool(100, 200, 400, 256);
 94   graph_tv = tv_create("histogram");
 95   tv_install(graph_tv, graph_tool);
 96   tv_set_next(graph_tv);
 97 }
 98 
 99 static void plot_hist_proc(void)
100 {
101   char temp[256];
102   Imrect *histim;
103   int i, j, n;
104   shistogram *ph;
105   float *xvals, *yvals;
106 
107   if ((hnum < 0) || (hnum >= NHIST))
108     return;
109 
110   ph = hist[hnum];
111 
112   if (ph == NULL)
113   {
114       error("plot_hist_proc: histogram not in use", warning);
115       return;
116   }
117 
118   update_name_str();
119 
120   if (ph->type == 2)
121   {
122       histim = im_alloc(ph->ybins,ph->xbins,NULL,float_v);
123       for (j = 0; j < ph->ybins; j++)
124       { 
125           for (i = 0; i < ph->xbins; i++)
126                   IM_PIX_SET(histim, j, i, ph->array[j][i]);
127       }
128       stack_push(histim,IMRECT,im_free);
129       format("2D histogram pushed to Imcalc Tool\n");
130 
131       return;
132   }
133 
134   if (graph_tool == NULL)
135     {
136       error("plot_hist_proc: no histogram TV", warning);
137       return;
138     }
139   tv_erase(graph_tv);
140 
141   strcpy(temp, hist[hnum]->title);
142 
143   if (hist[hnum]->shFuncSuper == NULL)
144   {
145      plot(PL_INIT,
146           PL_TV, graph_tv,
147           PL_AXIS_COLOR, black,
148           PL_TITLE, temp,
149           PL_COLOR, red,
150           PL_HIST, hist[hnum],
151           PL_PLOT,
152           NULL);
153    }
154    else
155    {
156 
157       n = hist[hnum]->xbins;
158       xvals = ralloc(sizeof(float)*n);
159       yvals = ralloc(sizeof(float)*n);
160 
161       for (i=0; i<n; i++)
162       {
163          xvals[i] = hist[hnum]->xmin + ((i+0.5)*hist[hnum]->xincr);
164          yvals[i] = hist[hnum]->shFuncSuper(hist[hnum]->npar, hist[hnum]->par, xvals[i]);
165       }
166 
167       plot(PL_INIT,
168           PL_TV, graph_tv,
169           PL_AXIS_COLOR, black,
170           PL_TITLE, temp,
171           PL_COLOR, red,
172           PL_HIST, hist[hnum],
173           PL_COLOR, blue,
174           PL_GRAPH_DATA, n, xvals, yvals,
175           PL_PLOT,
176           NULL);
177 
178        rfree(xvals);
179        rfree(yvals);
180    }
181 }
182 
183 static void hist_stats_proc(void)
184 {
185   float std_dev;
186 
187   if ((hnum < 0) || (hnum >= NHIST))
188     return;
189 
190   if (hist[hnum] == NULL)
191     {
192       error("hist_stats_proc: current histogram not in use", warning);
193       return;
194     }
195 
196   std_dev = (float)sqrt(fabs(hist[hnum]->mean2 - hist[hnum]->mean *
197                              hist[hnum]->mean));
198   format("histogram: %s\n", hist[hnum]->title);
199   format("mean = %e = %f\nstd_dev = %e = %f\n", hist[hnum]->mean,
200          hist[hnum]->mean, std_dev, std_dev);
201   format("entries = %d\noverflows = %f\nunderflows = %f\n",
202          hist[hnum]->entries, hist[hnum]->over, hist[hnum]->under);
203   format("xmin = %f\nxmax = %f\nno. of bins = %d\n",
204          hist[hnum]->xmin, hist[hnum]->xmax, hist[hnum]->xbins);
205 }
206 
207 static void inc_hist_num_proc(void)
208 {
209   plot_hist_proc();
210 
211   if (hnum <0 ) hnum =0;
212   if (hnum >NHIST) hnum = NHIST-1;
213   while(hnum < NHIST-1 && hist[++hnum] == NULL);
214   tw_iglobal_reset(pframe2);
215 
216   update_name_str();
217   plot_hist_proc();
218 }
219 
220 static void integf_hist_proc(void)
221 {
222     if (hnum >= 0 && hnum < NHIST && hist[hnum]!=NULL)
223     {
224        hintegf(hist[hnum]);
225        update_name_str();
226     }
227 }
228 
229 static void dec_hist_num_proc(void)
230 {
231   plot_hist_proc();
232 
233   if (hnum <0 ) hnum =0;
234   if (hnum >NHIST) hnum = NHIST-1;
235   while (hnum > 0 && hist[--hnum] ==NULL);
236 
237   tw_iglobal_reset(pframe2);
238 
239   update_name_str();
240 
241   plot_hist_proc();
242 }
243 
244 void ecd_proc( void )
245 {   
246    int i;
247    float x, *memVeca, *memVecb, *memVec2, *inc; 
248    double maxdiff = 0.0, prob, max_sep = 0.0;
249 
250    integf_hist_proc();
251 
252 
253    memVeca = (float *)ralloc(50*sizeof(float));
254    memVecb = (float *)ralloc(50*sizeof(float));
255    memVec2 = (float *)ralloc(50*sizeof(float));
256    inc = (float *)ralloc(50*sizeof(float));
257    if( graph_tv!=NULL )
258    {
259     memVeca[0] = 0.0;
260     memVecb[0] = 0.0;
261     memVec2[0] = 0.0;
262     inc[0] = 0; 
263     x = cut1/100;
264 
265     for ( i = 1; i < 50; i++ )
266     {
267 
268         prob =  (-exp( -x ) + 1.0) ;
269         memVeca[i] = prob;
270 
271 /*
272         memVeca[i] = prob + 3.0*sqrt(prob-prob*prob)/sqrt(hist[hnum]->entries);
273         memVecb[i] = prob - 3.0*sqrt(prob-prob*prob)/sqrt(hist[hnum]->entries);
274 */
275         memVec2[i] =  hfill1(hist[hnum],x,0.0)/hist[hnum]->entries;
276         
277 
278 if ( fabs( memVeca[i] - memVec2[i] ) > max_sep ) max_sep = fabs( memVeca[i] - memVec2[i] );
279 
280         inc[i] = x;
281         x += cut1/50.0; 
282         if (fabs(prob-memVec2[i])>maxdiff ) maxdiff = fabs(prob-memVec2[i]);
283     }
284 //printf("hisstnuments = %d \n", hist[hnum]->entries );
285 
286 printf("max vert sep = %f \n", max_sep );
287 
288     tv_erase(graph_tv);
289     plot(PL_INIT,
290          PL_TV, graph_tv,
291          PL_AXIS_COLOR, black,
292          PL_TITLE, "cumDistExpFunc",
293          PL_X_RANGE, 0.0, cut1, 1.0,
294          PL_Y_RANGE, 0.0, 1.1, 1.0,
295          PL_STYLE, PL_LINE,            
296          PL_COLOR, black,
297          PL_GRAPH_DATA, i, inc, memVeca,  
298          PL_COLOR, black,
299          PL_GRAPH_DATA, i, inc, memVecb,  
300          PL_COLOR, red,
301          PL_GRAPH_DATA, i, inc, memVec2,
302          PL_PLOT,
303          NULL);
304    }
305    format("maximum difference = %f \n", maxdiff);
306    rfree((void *)memVeca);
307    rfree((void *)memVecb);
308    rfree((void *)memVec2);
309    rfree((void *)inc);
310 
311    hdiff(hist[hnum]);
312 }
313 
314 
315 void histmax_proc( void )
316 { 
317     float maxx, yhist;
318 
319     yhist = hmax1(hist[hnum], &maxx);
320 
321     format("maximum value = %f at %f \n", yhist, maxx);
322 }
323 
324 
325 void ncd_proc( void )
326 {   
327    int i;
328    float x, *memVeca, *memVecb, *memVec2, *inc;
329    double maxdiff = 0.0, prob, max_sep = 0.0;
330 
331 
332   integf_hist_proc();
333 
334 
335    memVeca = (float *)ralloc(50*sizeof(float));
336    memVecb = (float *)ralloc(50*sizeof(float));
337    memVec2 = (float *)ralloc(50*sizeof(float));
338    inc = (float *)ralloc(50*sizeof(float));
339    if( graph_tv!=NULL )
340    {
341     memVeca[0] = 0.0;
342     memVecb[0] = 0.0;
343     memVec2[0] = 0.0;
344     inc[0] = 0;
345     x = cut2/100;
346 
347     for ( i = 1; i < 50; i++ )
348     {
349         prob = erf( x/sqrt(2.0) );
350 
351         memVeca[i] = prob; /*+ 3.0*sqrt(prob-prob*prob)/sqrt (hist[hnum]->entries);*/
352 //        memVecb[i] = prob - 3.0*sqrt(prob-prob*prob)/sqrt(hist[hnum]->entries);
353         memVec2[i] = hfill1(hist[hnum],x,0.0)/hist[hnum]->entries;
354 
355 if ( fabs( memVeca[i] - memVec2[i] ) > max_sep ) max_sep = fabs( memVeca[i] - memVec2[i] );
356 
357 
358         inc[i] = x; 
359         x += cut2/50.0;
360         if (fabs(prob-memVec2[i])>maxdiff ) maxdiff = fabs(prob-memVec2[i]);
361     }
362 //printf("hisstnuments = %d \n", hist[hnum]->entries );
363 
364 printf("max vert sep = %f \n", max_sep );
365 
366 
367 
368     tv_erase(graph_tv);
369     plot(PL_INIT,
370          PL_TV, graph_tv,
371          PL_AXIS_COLOR, black,
372          PL_TITLE, "cumDistNormFunc",
373          PL_X_RANGE, 0.0, cut2, 1.0,
374          PL_Y_RANGE, 0.0, 1.1, 1.0,
375          PL_STYLE, PL_LINE,
376          PL_COLOR, black,
377          PL_GRAPH_DATA, i, inc, memVeca,
378          PL_COLOR, black,
379          PL_GRAPH_DATA, i, inc, memVecb,
380          PL_COLOR, red,
381          PL_GRAPH_DATA, i, inc, memVec2,
382          PL_PLOT,
383          NULL);
384    }
385    format("maximum difference = %f \n", maxdiff);
386    rfree((void *)memVeca);
387    rfree((void *)memVecb);
388    rfree((void *)memVec2);
389    rfree((void *)inc);
390 
391    hdiff(hist[hnum]);
392 }
393 
394 static void store_hists_proc()
395 {
396    FILE *fp;
397 
398    if ((fp = fopen(file_str, "wt")) == NULL)
399    {
400       error("Unable to open file\n", non_fatal);
401       return;
402    }
403  
404    hstore_all(fp);
405    fclose(fp);
406 
407    format("Histograms saved to file\n");
408 }
409 
410 static void fetch_hists_proc()
411 {
412    FILE *fp;
413 
414    if ((fp = fopen(file_str, "rt")) == NULL)
415    {
416       error("Unable to open file\n", non_fatal);
417       return;
418    }
419  
420    hfetch_all(fp);
421    fclose(fp);
422 
423    format("Histograms loaded from file\n");
424 }
425 
426 static void load_csv_proc()
427 {
428    FILE *fp;
429 
430    if ((fp = fopen(file_str, "rt")) == NULL)
431    {
432       error("Unable to open file\n", non_fatal);
433       return;
434    }
435  
436    hfetch_csv(fp);
437    fclose(fp);
438 
439    format("CSV loaded from file\n");
440 }
441 
442 void qpr_tool_proc()
443 {
444    void qpr_tool(int x , int y);
445    qpr_tool(50, 50);
446 }
447 
448 void hist_tool(int x, int y)
449 {
450   static void *tool = NULL;
451 
452   if (tool)
453     {
454       tw_show_tool(tool);
455       return;
456     }
457   hist = hist_vec();
458 
459   tool = (void *)tw_tool("Histogram Tool", x, y);
460 
461   tw_button("graph TV", graph_tv_proc, NULL);
462   tw_button("QPR Tool", qpr_tool_proc, NULL);
463   tw_newrow();
464   pframe2 = (void *)tw_iglobal("Current Histogram:", &hnum, 5);
465   tw_button("<", dec_hist_num_proc, NULL);
466   tw_button(">", inc_hist_num_proc, NULL);
467   tw_newrow();
468   pframe3 = (void *)tw_sglobal("Name: ", name_str, 30);
469   tw_newrow();
470   tw_button("plot hist", plot_hist_proc, NULL);
471   tw_button("hist stats", hist_stats_proc, NULL);
472   tw_button("integrate hist", integf_hist_proc, NULL);
473   tw_newrow();
474   tw_button("expCDF",  ecd_proc, NULL );
475   tw_button("normCDF",  ncd_proc, NULL );
476   tw_button("hist max",  histmax_proc, NULL );
477   tw_newrow();
478   tw_sglobal("File: ", file_str, 30);
479   tw_newrow();
480   tw_button("Store all", store_hists_proc, NULL);
481   tw_button("Fetch all", fetch_hists_proc, NULL);
482   tw_button("Load CSV", load_csv_proc, NULL);
483   
484   tw_end_tool();
485 
486   update_name_str();
487 }
488 

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