tree.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <stdlib.h>
00003 #include "tree.h"
00004 #include "utils.h"
00005 #include "data.h"
00006 
00007 void change_leaves(tree *t, char *leaf_list)
00008 {
00009     list *llist = get_paths(leaf_list);
00010     char **leaves = (char **)list_to_array(llist);
00011     int n = llist->size;
00012     int i,j;
00013     int found = 0;
00014     for(i = 0; i < t->n; ++i){
00015         t->leaf[i] = 0;
00016         for(j = 0; j < n; ++j){
00017             if (0==strcmp(t->name[i], leaves[j])){
00018                 t->leaf[i] = 1;
00019                 ++found;
00020                 break;
00021             }
00022         }
00023     }
00024     fprintf(stderr, "Found %d leaves.\n", found);
00025 }
00026 
00027 float get_hierarchy_probability(float *x, tree *hier, int c)
00028 {
00029     float p = 1;
00030     while(c >= 0){
00031         p = p * x[c];
00032         c = hier->parent[c];
00033     }
00034     return p;
00035 }
00036 
00037 void hierarchy_predictions(float *predictions, int n, tree *hier, int only_leaves)
00038 {
00039     int j;
00040     for(j = 0; j < n; ++j){
00041         int parent = hier->parent[j];
00042         if(parent >= 0){
00043             predictions[j] *= predictions[parent]; 
00044         }
00045     }
00046     if(only_leaves){
00047         for(j = 0; j < n; ++j){
00048             if(!hier->leaf[j]) predictions[j] = 0;
00049         }
00050     }
00051 }
00052 
00053 tree *read_tree(char *filename)
00054 {
00055     tree t = {0};
00056     FILE *fp = fopen(filename, "r");
00057     
00058     char *line;
00059     int last_parent = -1;
00060     int group_size = 0;
00061     int groups = 0;
00062     int n = 0;
00063     while((line=fgetl(fp)) != 0){
00064         char *id = calloc(256, sizeof(char));
00065         int parent = -1;
00066         sscanf(line, "%s %d", id, &parent);
00067         t.parent = realloc(t.parent, (n+1)*sizeof(int));
00068         t.parent[n] = parent;
00069 
00070         t.name = realloc(t.name, (n+1)*sizeof(char *));
00071         t.name[n] = id;
00072         if(parent != last_parent){
00073             ++groups;
00074             t.group_offset = realloc(t.group_offset, groups * sizeof(int));
00075             t.group_offset[groups - 1] = n - group_size;
00076             t.group_size = realloc(t.group_size, groups * sizeof(int));
00077             t.group_size[groups - 1] = group_size;
00078             group_size = 0;
00079             last_parent = parent;
00080         }
00081         t.group = realloc(t.group, (n+1)*sizeof(int));
00082         t.group[n] = groups;
00083         ++n;
00084         ++group_size;
00085     }
00086     ++groups;
00087     t.group_offset = realloc(t.group_offset, groups * sizeof(int));
00088     t.group_offset[groups - 1] = n - group_size;
00089     t.group_size = realloc(t.group_size, groups * sizeof(int));
00090     t.group_size[groups - 1] = group_size;
00091     t.n = n;
00092     t.groups = groups;
00093     t.leaf = calloc(n, sizeof(int));
00094     int i;
00095     for(i = 0; i < n; ++i) t.leaf[i] = 1;
00096     for(i = 0; i < n; ++i) if(t.parent[i] >= 0) t.leaf[t.parent[i]] = 0;
00097 
00098     fclose(fp);
00099     tree *tree_ptr = calloc(1, sizeof(tree));
00100     *tree_ptr = t;
00101     //error(0);
00102     return tree_ptr;
00103 }


rail_object_detector
Author(s):
autogenerated on Sat Jun 8 2019 20:26:31