You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
2.5 KiB
95 lines
2.5 KiB
#include "basicTree.h"
|
|
#include "basicData.h"
|
|
#include "basicUtils.h"
|
|
|
|
void change_leaves(cortexTREE *t, char *leaf_list){
|
|
cortexList *llist = get_paths(leaf_list);
|
|
//char **leaves = (char **)list_to_array(llist);
|
|
uint32_t n = llist->size();
|
|
uint32_t found = 0;
|
|
for (uint32_t i = 0; i < t->n; ++i){
|
|
t->leaf[i] = 0;
|
|
for (uint32_t j = 0; j < n; ++j){
|
|
if (0 == strcmp(t->name[i], llist->at(j) )){// leaves[j])){
|
|
t->leaf[i] = 1;
|
|
++found;
|
|
break;
|
|
};
|
|
};
|
|
};
|
|
fprintf(stderr, "Found %d leaves.\n", found);
|
|
};
|
|
|
|
float get_hierarchy_probability(float *x, cortexTREE *hier, uint32_t c){
|
|
float p = 1;
|
|
while (c >= 0){
|
|
p = p * x[c];
|
|
c = hier->parent[c];
|
|
};
|
|
return p;
|
|
};
|
|
|
|
void hierarchy_predictions(float *predictions, uint32_t n, cortexTREE *hier, bool only_leaves){
|
|
for (uint32_t j = 0; j < n; ++j){
|
|
int parent = hier->parent[j];
|
|
if (parent >= 0){
|
|
predictions[j] *= predictions[parent];
|
|
};
|
|
};
|
|
if (only_leaves){
|
|
for (uint32_t j = 0; j < n; ++j){
|
|
if (!hier->leaf[j]) predictions[j] = 0;
|
|
};
|
|
};
|
|
};
|
|
|
|
cortexTREE *read_tree(char *filename){
|
|
cortexTREE t = { 0 };
|
|
FILE *fp = fopen(filename, "r");
|
|
|
|
char *line;
|
|
int last_parent = -1;
|
|
int group_size = 0;
|
|
int groups = 0;
|
|
int n = 0;
|
|
while ((line = fgetl(fp)) != 0){
|
|
char *id = (char *)calloc(256, sizeof(char));
|
|
int parent = -1;
|
|
sscanf(line, "%s %d", id, &parent);
|
|
t.parent = (uint32_t*)realloc(t.parent, (n + 1)*sizeof(uint32_t));
|
|
t.parent[n] = parent;
|
|
|
|
t.name = (char**)realloc(t.name, (n + 1)*sizeof(char *));
|
|
t.name[n] = id;
|
|
if (parent != last_parent){
|
|
++groups;
|
|
t.group_offset = (uint32_t*)realloc(t.group_offset, groups * sizeof(uint32_t));
|
|
t.group_offset[groups - 1] = n - group_size;
|
|
t.group_size = (uint32_t*)realloc(t.group_size, groups * sizeof(uint32_t));
|
|
t.group_size[groups - 1] = group_size;
|
|
group_size = 0;
|
|
last_parent = parent;
|
|
}
|
|
t.group = (uint32_t*)realloc(t.group, (n + 1)*sizeof(uint32_t));
|
|
t.group[n] = groups;
|
|
++n;
|
|
++group_size;
|
|
}
|
|
++groups;
|
|
t.group_offset = (uint32_t*)realloc(t.group_offset, groups * sizeof(uint32_t));
|
|
t.group_offset[groups - 1] = n - group_size;
|
|
t.group_size = (uint32_t*)realloc(t.group_size, groups * sizeof(uint32_t));
|
|
t.group_size[groups - 1] = group_size;
|
|
t.n = n;
|
|
t.groups = groups;
|
|
t.leaf = (uint32_t*)calloc(n, sizeof(uint32_t));
|
|
int i;
|
|
for (i = 0; i < n; ++i) t.leaf[i] = 1;
|
|
for (i = 0; i < n; ++i) if (t.parent[i] >= 0) t.leaf[t.parent[i]] = 0;
|
|
|
|
fclose(fp);
|
|
cortexTREE *tree_ptr = (cortexTREE*)calloc(1, sizeof(cortexTREE));
|
|
*tree_ptr = t;
|
|
//error(0);
|
|
return tree_ptr;
|
|
}
|