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

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "basicTree.h"
  2. #include "basicData.h"
  3. #include "basicUtils.h"
  4. void change_leaves(cortexTREE *t, char *leaf_list){
  5. cortexList *llist = get_paths(leaf_list);
  6. //char **leaves = (char **)list_to_array(llist);
  7. uint32_t n = llist->size();
  8. uint32_t found = 0;
  9. for (uint32_t i = 0; i < t->n; ++i){
  10. t->leaf[i] = 0;
  11. for (uint32_t j = 0; j < n; ++j){
  12. if (0 == strcmp(t->name[i], llist->at(j) )){// leaves[j])){
  13. t->leaf[i] = 1;
  14. ++found;
  15. break;
  16. };
  17. };
  18. };
  19. fprintf(stderr, "Found %d leaves.\n", found);
  20. };
  21. float get_hierarchy_probability(float *x, cortexTREE *hier, uint32_t c){
  22. float p = 1;
  23. while (c >= 0){
  24. p = p * x[c];
  25. c = hier->parent[c];
  26. };
  27. return p;
  28. };
  29. void hierarchy_predictions(float *predictions, uint32_t n, cortexTREE *hier, bool only_leaves){
  30. for (uint32_t j = 0; j < n; ++j){
  31. int parent = hier->parent[j];
  32. if (parent >= 0){
  33. predictions[j] *= predictions[parent];
  34. };
  35. };
  36. if (only_leaves){
  37. for (uint32_t j = 0; j < n; ++j){
  38. if (!hier->leaf[j]) predictions[j] = 0;
  39. };
  40. };
  41. };
  42. cortexTREE *read_tree(char *filename){
  43. cortexTREE t = { 0 };
  44. FILE *fp = fopen(filename, "r");
  45. char *line;
  46. int last_parent = -1;
  47. int group_size = 0;
  48. int groups = 0;
  49. int n = 0;
  50. while ((line = fgetl(fp)) != 0){
  51. char *id = (char *)calloc(256, sizeof(char));
  52. int parent = -1;
  53. sscanf(line, "%s %d", id, &parent);
  54. t.parent = (uint32_t*)realloc(t.parent, (n + 1)*sizeof(uint32_t));
  55. t.parent[n] = parent;
  56. t.name = (char**)realloc(t.name, (n + 1)*sizeof(char *));
  57. t.name[n] = id;
  58. if (parent != last_parent){
  59. ++groups;
  60. t.group_offset = (uint32_t*)realloc(t.group_offset, groups * sizeof(uint32_t));
  61. t.group_offset[groups - 1] = n - group_size;
  62. t.group_size = (uint32_t*)realloc(t.group_size, groups * sizeof(uint32_t));
  63. t.group_size[groups - 1] = group_size;
  64. group_size = 0;
  65. last_parent = parent;
  66. }
  67. t.group = (uint32_t*)realloc(t.group, (n + 1)*sizeof(uint32_t));
  68. t.group[n] = groups;
  69. ++n;
  70. ++group_size;
  71. }
  72. ++groups;
  73. t.group_offset = (uint32_t*)realloc(t.group_offset, groups * sizeof(uint32_t));
  74. t.group_offset[groups - 1] = n - group_size;
  75. t.group_size = (uint32_t*)realloc(t.group_size, groups * sizeof(uint32_t));
  76. t.group_size[groups - 1] = group_size;
  77. t.n = n;
  78. t.groups = groups;
  79. t.leaf = (uint32_t*)calloc(n, sizeof(uint32_t));
  80. int i;
  81. for (i = 0; i < n; ++i) t.leaf[i] = 1;
  82. for (i = 0; i < n; ++i) if (t.parent[i] >= 0) t.leaf[t.parent[i]] = 0;
  83. fclose(fp);
  84. cortexTREE *tree_ptr = (cortexTREE*)calloc(1, sizeof(cortexTREE));
  85. *tree_ptr = t;
  86. //error(0);
  87. return tree_ptr;
  88. }