sigset.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646
  1. #include <string.h>
  2. #include <stdlib.h>
  3. #include <grass/gis.h>
  4. #include <grass/glocale.h>
  5. #include <grass/imagery.h>
  6. static int gettag(FILE *, char *);
  7. static int get_semantic_labels(FILE *, struct SigSet *);
  8. static int get_title(FILE *, struct SigSet *);
  9. static int get_class(FILE *, struct SigSet *);
  10. static int get_classnum(FILE *, struct ClassSig *);
  11. static int get_classtype(FILE *, struct ClassSig *);
  12. static int get_classtitle(FILE *, struct ClassSig *);
  13. static int get_subclass(FILE *, struct SigSet *, struct ClassSig *);
  14. static int get_subclass_pi(FILE *, struct SubSig *);
  15. static int get_subclass_means(FILE *, struct SubSig *, int);
  16. static int get_subclass_covar(FILE *, struct SubSig *, int);
  17. static double **alloc_matrix(int rows, int cols)
  18. {
  19. double **m;
  20. int i;
  21. m = (double **)G_calloc(rows, sizeof(double *));
  22. m[0] = (double *)G_calloc(rows * cols, sizeof(double));
  23. for (i = 1; i < rows; i++)
  24. m[i] = m[i - 1] + cols;
  25. return m;
  26. }
  27. int I_SigSetNClasses(struct SigSet *S)
  28. {
  29. int i, count;
  30. for (i = 0, count = 0; i < S->nclasses; i++)
  31. if (S->ClassSig[i].used)
  32. count++;
  33. return count;
  34. }
  35. struct ClassData *I_AllocClassData(struct SigSet *S,
  36. struct ClassSig *C, int npixels)
  37. {
  38. struct ClassData *Data;
  39. Data = &(C->ClassData);
  40. Data->npixels = npixels;
  41. Data->count = 0;
  42. Data->x = alloc_matrix(npixels, S->nbands);
  43. Data->p = alloc_matrix(npixels, C->nsubclasses);
  44. return Data;
  45. }
  46. /*!
  47. * \brief Initialize struct SigSet before use
  48. *
  49. * No need to call before calling I_ReadSigSet.
  50. *
  51. * \param *Signature to initialize
  52. * \param nbands band (imagery group member) count
  53. */
  54. int I_InitSigSet(struct SigSet *S, int nbands)
  55. {
  56. S->nbands = nbands;
  57. S->semantic_labels = (char **)G_malloc(nbands * sizeof(char **));
  58. for (int i = 0; i < nbands; i++)
  59. S->semantic_labels[i] = NULL;
  60. S->nclasses = 0;
  61. S->ClassSig = NULL;
  62. S->title = NULL;
  63. return 0;
  64. }
  65. struct ClassSig *I_NewClassSig(struct SigSet *S)
  66. {
  67. struct ClassSig *Sp;
  68. if (S->nclasses == 0)
  69. S->ClassSig = (struct ClassSig *)G_malloc(sizeof(struct ClassSig));
  70. else
  71. S->ClassSig = (struct ClassSig *)G_realloc((char *)S->ClassSig,
  72. sizeof(struct ClassSig) *
  73. (S->nclasses + 1));
  74. Sp = &S->ClassSig[S->nclasses++];
  75. Sp->classnum = 0;
  76. Sp->nsubclasses = 0;
  77. Sp->used = 1;
  78. Sp->type = SIGNATURE_TYPE_MIXED;
  79. Sp->title = NULL;
  80. return Sp;
  81. }
  82. struct SubSig *I_NewSubSig(struct SigSet *S, struct ClassSig *C)
  83. {
  84. struct SubSig *Sp;
  85. int i;
  86. if (C->nsubclasses == 0)
  87. C->SubSig = (struct SubSig *)G_malloc(sizeof(struct SubSig));
  88. else
  89. C->SubSig = (struct SubSig *)G_realloc((char *)C->SubSig,
  90. sizeof(struct SubSig) *
  91. (C->nsubclasses + 1));
  92. Sp = &C->SubSig[C->nsubclasses++];
  93. Sp->used = 1;
  94. Sp->R = (double **)G_calloc(S->nbands, sizeof(double *));
  95. Sp->R[0] = (double *)G_calloc(S->nbands * S->nbands, sizeof(double));
  96. for (i = 1; i < S->nbands; i++)
  97. Sp->R[i] = Sp->R[i - 1] + S->nbands;
  98. Sp->Rinv = (double **)G_calloc(S->nbands, sizeof(double *));
  99. Sp->Rinv[0] = (double *)G_calloc(S->nbands * S->nbands, sizeof(double));
  100. for (i = 1; i < S->nbands; i++)
  101. Sp->Rinv[i] = Sp->Rinv[i - 1] + S->nbands;
  102. Sp->means = (double *)G_calloc(S->nbands, sizeof(double));
  103. Sp->N = 0;
  104. Sp->pi = 0;
  105. Sp->cnst = 0;
  106. return Sp;
  107. }
  108. #define eq(a,b) strcmp(a,b)==0
  109. /*!
  110. * \brief Read sigset signatures from file
  111. *
  112. * File stream should be opened in advance by call to
  113. * I_fopen_sigset_file_old()
  114. * It is up to caller to fclose the file stream afterwards.
  115. *
  116. * There is no need to initialise struct SigSet in advance, as this
  117. * function internally calls I_InitSigSet.
  118. *
  119. * \param pointer to FILE*
  120. * \param pointer to struct SigSet *S
  121. *
  122. * \return 1 on success, -1 on failure
  123. */
  124. int I_ReadSigSet(FILE * fd, struct SigSet *S)
  125. {
  126. char tag[256];
  127. unsigned int version;
  128. if (fscanf(fd, "%u", &version) != 1) {
  129. G_warning(_("Invalid signature file"));
  130. return -1;
  131. }
  132. if (version != 1) {
  133. G_warning(_("Invalid signature file version"));
  134. return -1;
  135. }
  136. I_InitSigSet(S, 0);
  137. while (gettag(fd, tag)) {
  138. if (eq(tag, "title:"))
  139. if (get_title(fd, S) != 0)
  140. return -1;
  141. if (eq(tag, "semantic_labels:"))
  142. if (get_semantic_labels(fd, S) != 0)
  143. return -1;
  144. if (eq(tag, "class:"))
  145. if (get_class(fd, S) != 0)
  146. return -1;
  147. }
  148. return 1; /* for now assume success */
  149. }
  150. static int gettag(FILE * fd, char *tag)
  151. {
  152. if (fscanf(fd, "%255s", tag) != 1)
  153. return 0;
  154. G_strip(tag);
  155. return 1;
  156. }
  157. static int get_semantic_labels(FILE * fd, struct SigSet *S)
  158. {
  159. int n, pos;
  160. char c, prev;
  161. char semantic_label[GNAME_MAX];
  162. /* Read semantic labels and count them to set nbands */
  163. n = 0;
  164. pos = 0;
  165. S->semantic_labels = (char **)G_realloc(S->semantic_labels, (n + 1) * sizeof(char **));
  166. while ((c = (char)fgetc(fd)) != EOF) {
  167. if (c == '\n') {
  168. if (prev != ' ') {
  169. semantic_label[pos] = '\0';
  170. if (strlen(semantic_label) > 0) {
  171. S->semantic_labels[n] = G_store(semantic_label);
  172. n++;
  173. }
  174. }
  175. S->nbands = n;
  176. break;
  177. }
  178. if (c == ' ') {
  179. semantic_label[pos] = '\0';
  180. if (strlen(semantic_label) > 0) {
  181. S->semantic_labels[n] = G_store(semantic_label);
  182. n++;
  183. /* [n] is 0 based thus: (n + 1) */
  184. S->semantic_labels =
  185. (char **)G_realloc(S->semantic_labels,
  186. (n + 1) * sizeof(char **));
  187. }
  188. pos = 0;
  189. prev = c;
  190. continue;
  191. }
  192. /* Semantic labels are limited to GNAME_MAX - 1 + \0 in length;
  193. * n is 0-based */
  194. if (pos == (GNAME_MAX - 2)) {
  195. G_warning(_("Invalid signature file: semantic label length limit exceeded"));
  196. return -1;
  197. }
  198. semantic_label[pos] = c;
  199. pos++;
  200. prev = c;
  201. }
  202. if (!(S->nbands > 0)) {
  203. G_warning(_("Signature file does not contain bands"));
  204. return -1;
  205. }
  206. return 0;
  207. }
  208. static int get_title(FILE * fd, struct SigSet *S)
  209. {
  210. char title[1024];
  211. *title = 0;
  212. if (fscanf(fd, "%1024[^\n]", title) != 1)
  213. return -1;
  214. G_strip(title);
  215. I_SetSigTitle(S, title);
  216. return 0;
  217. }
  218. static int get_class(FILE * fd, struct SigSet *S)
  219. {
  220. char tag[1024];
  221. struct ClassSig *C;
  222. C = I_NewClassSig(S);
  223. while (gettag(fd, tag)) {
  224. if (eq(tag, "endclass:"))
  225. break;
  226. if (eq(tag, "classnum:"))
  227. if (get_classnum(fd, C) != 0)
  228. return -1;
  229. if (eq(tag, "classtype:"))
  230. if (get_classtype(fd, C) != 0)
  231. return -1;
  232. if (eq(tag, "classtitle:"))
  233. if (get_classtitle(fd, C) != 0)
  234. return -1;
  235. if (eq(tag, "subclass:"))
  236. if (get_subclass(fd, S, C) != 0)
  237. return -1;
  238. }
  239. return 0;
  240. }
  241. static int get_classnum(FILE * fd, struct ClassSig *C)
  242. {
  243. if (fscanf(fd, "%ld", &C->classnum) != 1)
  244. return -1;
  245. return 0;
  246. }
  247. static int get_classtype(FILE * fd, struct ClassSig *C)
  248. {
  249. if (fscanf(fd, "%d", &C->type) != 1)
  250. return -1;
  251. return 0;
  252. }
  253. static int get_classtitle(FILE * fd, struct ClassSig *C)
  254. {
  255. char title[1024];
  256. *title = 0;
  257. if (fscanf(fd, "%1024[^\n]", title) != 1)
  258. return -1;
  259. G_strip(title);
  260. I_SetClassTitle(C, title);
  261. return 0;
  262. }
  263. static int get_subclass(FILE * fd, struct SigSet *S, struct ClassSig *C)
  264. {
  265. struct SubSig *Sp;
  266. char tag[1024];
  267. Sp = I_NewSubSig(S, C);
  268. while (gettag(fd, tag)) {
  269. if (eq(tag, "endsubclass:"))
  270. break;
  271. if (eq(tag, "pi:"))
  272. if (get_subclass_pi(fd, Sp) != 0)
  273. return -1;
  274. if (eq(tag, "means:"))
  275. if (get_subclass_means(fd, Sp, S->nbands) != 0)
  276. return -1;
  277. if (eq(tag, "covar:"))
  278. if (get_subclass_covar(fd, Sp, S->nbands) != 0)
  279. return -1;
  280. }
  281. return 0;
  282. }
  283. static int get_subclass_pi(FILE * fd, struct SubSig *Sp)
  284. {
  285. if (fscanf(fd, "%lf", &Sp->pi) != 1)
  286. return -1;
  287. return 0;
  288. }
  289. static int get_subclass_means(FILE * fd, struct SubSig *Sp, int nbands)
  290. {
  291. int i;
  292. for (i = 0; i < nbands; i++)
  293. if (fscanf(fd, "%lf", &Sp->means[i]) != 1)
  294. return -1;
  295. return 0;
  296. }
  297. static int get_subclass_covar(FILE * fd, struct SubSig *Sp, int nbands)
  298. {
  299. int i, j;
  300. for (i = 0; i < nbands; i++)
  301. for (j = 0; j < nbands; j++)
  302. if (fscanf(fd, "%lf", &Sp->R[i][j]) != 1)
  303. return -1;
  304. return 0;
  305. }
  306. int I_SetSigTitle(struct SigSet *S, const char *title)
  307. {
  308. if (title == NULL)
  309. title = "";
  310. if (S->title)
  311. free(S->title);
  312. S->title = G_store(title);
  313. return 0;
  314. }
  315. const char *I_GetSigTitle(const struct SigSet *S)
  316. {
  317. if (S->title)
  318. return S->title;
  319. else
  320. return "";
  321. }
  322. int I_SetClassTitle(struct ClassSig *C, const char *title)
  323. {
  324. if (title == NULL)
  325. title = "";
  326. if (C->title)
  327. free(C->title);
  328. C->title = G_store(title);
  329. return 0;
  330. }
  331. const char *I_GetClassTitle(const struct ClassSig *C)
  332. {
  333. if (C->title)
  334. return C->title;
  335. else
  336. return "";
  337. }
  338. int I_WriteSigSet(FILE * fd, const struct SigSet *S)
  339. {
  340. const struct ClassSig *Cp;
  341. const struct SubSig *Sp;
  342. int i, j, b1, b2;
  343. /* This is version 1 sigset file format */
  344. fprintf(fd, "1\n");
  345. fprintf(fd, "title: %s\n", I_GetSigTitle(S));
  346. fprintf(fd, "semantic_labels: ");
  347. for (i = 0; i < S->nbands; i++) {
  348. fprintf(fd, "%s ", S->semantic_labels[i]);
  349. }
  350. fprintf(fd, "\n");
  351. for (i = 0; i < S->nclasses; i++) {
  352. Cp = &S->ClassSig[i];
  353. if (!Cp->used)
  354. continue;
  355. if (Cp->nsubclasses <= 0)
  356. continue;
  357. fprintf(fd, "class:\n");
  358. fprintf(fd, " classnum: %ld\n", Cp->classnum);
  359. fprintf(fd, " classtitle: %s\n", I_GetClassTitle(Cp));
  360. fprintf(fd, " classtype: %d\n", Cp->type);
  361. for (j = 0; j < Cp->nsubclasses; j++) {
  362. Sp = &Cp->SubSig[j];
  363. fprintf(fd, " subclass:\n");
  364. fprintf(fd, " pi: %g\n", Sp->pi);
  365. fprintf(fd, " means:");
  366. for (b1 = 0; b1 < S->nbands; b1++)
  367. fprintf(fd, " %g", Sp->means[b1]);
  368. fprintf(fd, "\n");
  369. fprintf(fd, " covar:\n");
  370. for (b1 = 0; b1 < S->nbands; b1++) {
  371. fprintf(fd, " ");
  372. for (b2 = 0; b2 < S->nbands; b2++)
  373. fprintf(fd, " %g", Sp->R[b1][b2]);
  374. fprintf(fd, "\n");
  375. }
  376. fprintf(fd, " endsubclass:\n");
  377. }
  378. fprintf(fd, "endclass:\n");
  379. }
  380. return 0;
  381. }
  382. /*!
  383. * \brief Reorder struct SigSet to match imagery group member order
  384. *
  385. * The function will check for semantic label match between sigset struct
  386. * and imagery group.
  387. *
  388. * In the case of a complete semantic label match, values of passed in
  389. * struct SigSet are reordered to match the order of imagery group items.
  390. * This reordering is done only for items present in the sigset file.
  391. * Thus reordering should be done only after calling I_ReadSigSet.
  392. *
  393. * If all semantic labels are not identical (in
  394. * arbitrary order), function will return two dimensional array with
  395. * comma separated list of:
  396. * - [0] semantic labels present in the signature struct but
  397. * absent in the imagery group
  398. * - [1] semantic labels present in the imagery group but
  399. * absent in the signature struct
  400. *
  401. * If no mismatch of semantic labels for signatures or imagery group are
  402. * detected (== all are present in the other list), a NULL value will be
  403. * returned in the particular list of mismatches (not an empty string).
  404. * For example:
  405. * \code if (ret && ret[1]) printf("List of imagery group bands without signatures: %s\n, ret[1]); \endcode
  406. *
  407. * \param *SigSet existing signatures to check & sort
  408. * \param *Ref group reference
  409. *
  410. * \return NULL successfully sorted
  411. * \return err_array two comma separated lists of mismatches
  412. */
  413. char **I_SortSigSetBySemanticLabel(struct SigSet *S, const struct Ref *R)
  414. {
  415. unsigned int total, complete;
  416. unsigned int *match1, *match2, mc1, mc2, *new_order;
  417. double ***new_means, ****new_vars;
  418. char **group_semantic_labels, **mismatches, **new_semantic_labels;
  419. /* Safety measure. Untranslated as this should not happen in production! */
  420. if (S->nbands < 1 || R->nfiles < 1)
  421. G_fatal_error("Programming error. Invalid length structs passed to "
  422. "I_sort_signatures_by_semantic_label(%d, %d);", S->nbands,
  423. R->nfiles);
  424. /* Obtain group semantic labels */
  425. group_semantic_labels = (char **)G_malloc(R->nfiles * sizeof(char *));
  426. for (unsigned int j = R->nfiles; j--;) {
  427. group_semantic_labels[j] = Rast_get_semantic_label_or_name(R->file[j].name, R->file[j].mapset);
  428. }
  429. /* If lengths are not equal, there will be a mismatch */
  430. complete = S->nbands == R->nfiles;
  431. /* Initialize match tracker */
  432. new_order = (unsigned int *)G_malloc(S->nbands * sizeof(unsigned int));
  433. match1 = (unsigned int *)G_calloc(S->nbands, sizeof(unsigned int));
  434. match2 = (unsigned int *)G_calloc(R->nfiles, sizeof(unsigned int));
  435. /* Allocate memory for temporary storage of sorted values */
  436. new_semantic_labels = (char **)G_malloc(S->nbands * sizeof(char *));
  437. new_means = (double ***)G_malloc(S->nclasses * sizeof(double **));
  438. // new_vars[S.ClassSig[x]][.SubSig[y]][R[band1]][R[band1]]
  439. new_vars = (double ****)G_malloc(S->nclasses * sizeof(double ***));
  440. for (unsigned int c = S->nclasses; c--;) {
  441. new_means[c] =
  442. (double **)G_malloc(S->ClassSig[c].nsubclasses *
  443. sizeof(double *));
  444. new_vars[c] =
  445. (double ***)G_malloc(S->ClassSig[c].nsubclasses *
  446. sizeof(double **));
  447. for (unsigned int s = S->ClassSig[c].nsubclasses; s--;) {
  448. new_means[c][s] = (double *)G_malloc(S->nbands * sizeof(double));
  449. new_vars[c][s] =
  450. (double **)G_malloc(S->nbands * sizeof(double *));
  451. for (unsigned int i = S->nbands; i--;)
  452. new_vars[c][s][i] =
  453. (double *)G_malloc(S->nbands * sizeof(double));
  454. }
  455. }
  456. /* Obtain order of matching items */
  457. for (unsigned int j = R->nfiles; j--;) {
  458. for (unsigned int i = S->nbands; i--;) {
  459. if (S->semantic_labels[i] && group_semantic_labels[j] &&
  460. !strcmp(S->semantic_labels[i], group_semantic_labels[j])) {
  461. if (complete) {
  462. /* Reorder pointers to existing strings only */
  463. new_semantic_labels[j] = S->semantic_labels[i];
  464. new_order[i] = j;
  465. }
  466. /* Keep a track of matching items for error reporting */
  467. match1[i] = 1;
  468. match2[j] = 1;
  469. break;
  470. }
  471. }
  472. }
  473. /* Check for semantic label mismatch */
  474. mc1 = mc2 = 0;
  475. mismatches = (char **)G_malloc(2 * sizeof(char **));
  476. mismatches[0] = NULL;
  477. mismatches[1] = NULL;
  478. total = 1;
  479. for (unsigned int i = 0; i < S->nbands; i++) {
  480. if (!match1[i]) {
  481. if (S->semantic_labels[i])
  482. total = total + strlen(S->semantic_labels[i]);
  483. else
  484. total = total + 24;
  485. mismatches[0] =
  486. (char *)G_realloc(mismatches[0], total * sizeof(char *));
  487. if (mc1)
  488. strcat(mismatches[0], ",");
  489. else
  490. mismatches[0][0] = '\0';
  491. if (S->semantic_labels[i])
  492. strcat(mismatches[0], S->semantic_labels[i]);
  493. else
  494. strcat(mismatches[0], "<semantic label missing>");
  495. mc1++;
  496. total = total + 1;
  497. }
  498. }
  499. total = 1;
  500. for (unsigned int j = 0; j < R->nfiles; j++) {
  501. if (!match2[j]) {
  502. if (group_semantic_labels[j])
  503. total = total + strlen(group_semantic_labels[j]);
  504. else
  505. total = total + 24;
  506. mismatches[1] =
  507. (char *)G_realloc(mismatches[1], total * sizeof(char *));
  508. if (mc2)
  509. strcat(mismatches[1], ",");
  510. else
  511. mismatches[1][0] = '\0';
  512. if (group_semantic_labels[j])
  513. strcat(mismatches[1], group_semantic_labels[j]);
  514. else
  515. strcat(mismatches[1], "<semantic label missing>");
  516. mc2++;
  517. total = total + 1;
  518. }
  519. }
  520. /* Swap mean and var matrix values in each of classes */
  521. if (!mc1 && !mc2) {
  522. for (unsigned int c = S->nclasses; c--;) {
  523. for (unsigned int s = S->ClassSig[c].nsubclasses; s--;) {
  524. for (unsigned int b1 = 0; b1 < S->nbands; b1++) {
  525. new_means[c][s][new_order[b1]] =
  526. S->ClassSig[c].SubSig[s].means[b1];
  527. for (unsigned int b2 = 0; b2 < S->nbands; b2++) {
  528. new_vars[c][s][new_order[b1]][new_order[b2]] =
  529. S->ClassSig[c].SubSig[s].R[b1][b2];
  530. }
  531. }
  532. }
  533. }
  534. /* Replace values in struct with ordered ones */
  535. memcpy(S->semantic_labels, new_semantic_labels, S->nbands * sizeof(char **));
  536. for (unsigned int c = S->nclasses; c--;) {
  537. for (unsigned int s = S->ClassSig[c].nsubclasses; s--;) {
  538. memcpy(S->ClassSig[c].SubSig[s].means, new_means[c][s],
  539. S->nbands * sizeof(double));
  540. for (unsigned int i = S->nbands; i--;)
  541. memcpy(S->ClassSig[c].SubSig[s].R[i], new_vars[c][s][i],
  542. S->nbands * sizeof(double));
  543. }
  544. }
  545. }
  546. /* Clean up */
  547. for (unsigned int j = R->nfiles; j--;)
  548. free(group_semantic_labels[j]);
  549. free(group_semantic_labels);
  550. free(new_order);
  551. free(match1);
  552. free(match2);
  553. free(new_semantic_labels);
  554. for (unsigned int c = S->nclasses; c--;) {
  555. for (unsigned int s = S->ClassSig[c].nsubclasses; s--;) {
  556. free(new_means[c][s]);
  557. for (unsigned int i = S->nbands; i--;)
  558. free(new_vars[c][s][i]);
  559. free(new_vars[c][s]);
  560. }
  561. free(new_means[c]);
  562. free(new_vars[c]);
  563. }
  564. free(new_means);
  565. free(new_vars);
  566. if (mc1 || mc2) {
  567. return mismatches;
  568. }
  569. free(mismatches);
  570. return NULL;
  571. }