| 1 | /* ClassificationTable.cpp |
| 2 | * |
| 3 | * Copyright (C) 1993-2021 David Weenink |
| 4 | * |
| 5 | * This code is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or (at |
| 8 | * your option) any later version. |
| 9 | * |
| 10 | * This code is distributed in the hope that it will be useful, but |
| 11 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | * General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this work. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | djmw 1998 |
| 21 | djmw 20020315 GPL header |
| 22 | djmw 20040422 Added ClassificationTable_to_Categories_maximumProbability |
| 23 | djmw 20040623 Added ClassificationTable_to_Strings_maximumProbability |
| 24 | djmw 20040824 Added Strings_extensions.h header |
| 25 | djmw 20101122 ClassificationTable_to_Correlation_columns |
| 26 | djmw 20110304 Thing_new |
| 27 | */ |
| 28 | |
| 29 | #include "ClassificationTable.h"cxx . . $ `<file>/dwtools/ClassificationTable.h`/ |
| 30 | #include "Distributions_and_Strings.h"cxx . . $ `<file>/stat/Distributions_and_Strings.h`/ |
| 31 | #include "Strings_extensions.h"cxx . . $ `<file>/dwtools/Strings_extensions.h`/ |
| 32 | #include "NUM2.h"cxx . . $ `<file>/dwsys/NUM2.h`/ |
| 33 | |
| 34 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (ClassificationTable, TableOfReal, 0); |
| 35 | |
| 36 | autoClassificationTablecxx . . $ autoClassificationTable# ClassificationTable_createcxx . . $ ClassificationTable_create(b9c68632944d67c8). (integercxx . . $ integer# numberOfRows, integercxx . . $ integer# numberOfClasses) { |
| 37 | try { |
| 38 | autoClassificationTablecxx . . $ autoClassificationTable# me = Thing_newcxx . . $ `sys/Thing.h:514:9`! (ClassificationTable); |
| 39 | TableOfReal_initcxx . . $ TableOfReal_init(cfadf70b559ed315). (me.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), numberOfRows, numberOfClasses); |
| 40 | return mecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 41 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 42 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (U"ClassificationTable not created."); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | autoConfusioncxx . . $ autoConfusion# ClassificationTable_to_Confusioncxx . . $ ClassificationTable_to_Confusion(a1b1b4cfadc79996). (ClassificationTablecxx . . $ ClassificationTable# me, bool onlyClassLabels) { |
| 47 | try { |
| 48 | autoStringscxx . . $ autoStrings# responses = TableOfReal_extractColumnLabelsAsStringscxx . . $ TableOfReal_extractColumnLabelsAsStrings(397082e846780902). (me); |
| 49 | autoStringscxx . . $ autoStrings# s2 = TableOfReal_extractRowLabelsAsStringscxx . . $ TableOfReal_extractRowLabelsAsStrings(397082e846780902). (me); |
| 50 | autoDistributionscxx . . $ autoDistributions# d2 = Strings_to_Distributionscxx . . $ Strings_to_Distributions(51ca0faf08ce563). (s2.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 51 | autoStringscxx . . $ autoStrings# stimuli = TableOfReal_extractRowLabelsAsStringscxx . . $ TableOfReal_extractRowLabelsAsStrings(397082e846780902). (d2.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 52 | autoConfusioncxx . . $ autoConfusion# thee = Confusion_createFromStringsescxx . . $ Confusion_createFromStringses(5db7a5f4207d480c). (( onlyClassLabels ? responses.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).() : stimuli.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).() ), responses.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).()); |
| 53 | Confusion_ClassificationTable_increasecxx . . $ Confusion_ClassificationTable_increase(e46a30857ff6c218). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), me); |
| 54 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 55 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 56 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": confusions cannot be calculated."); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void Confusion_ClassificationTable_increasecxx . . $ Confusion_ClassificationTable_increase(e46a30857ff6c218). (Confusioncxx . . $ Confusion# me, ClassificationTablecxx . . $ ClassificationTable# thee) { |
| 61 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (mycxx . . $ `melder/melder.h:44:9`! numberOfColumns == thycxx . . $ `melder/melder.h:45:9`! numberOfColumns, |
| 62 | U"The number of columns should be equal."); |
| 63 | for (integercxx . . $ integer# irow = 1; irow <= thycxx . . $ `melder/melder.h:45:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; irow ++) { |
| 64 | const integercxx . . $ integer# index = TableOfReal_getColumnIndexAtMaximumInRowcxx . . $ TableOfReal_getColumnIndexAtMaximumInRow(a5db92b16a8de5dc). (thee, irow); |
| 65 | Confusion_increasecxx . . $ Confusion_increase(60c4476b94ec172e). (me, thycxx . . $ `melder/melder.h:45:9`! rowLabelscxx . . $ structTableOfReal#rowLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).irow].getcxx . . $ _autostring#get(5a2a78a048fb49a8).(), mycxx . . $ `melder/melder.h:44:9`! columnLabelscxx . . $ structTableOfReal#columnLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).index].getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | autoStringscxx . . $ autoStrings# ClassificationTable_to_Strings_maximumProbabilitycxx . . $ ClassificationTable_to_Strings_maximumProbability(ae45cd644b16526e). (ClassificationTablecxx . . $ ClassificationTable# me) { |
| 70 | try { |
| 71 | autoStringscxx . . $ autoStrings# thee = Strings_createFixedLengthcxx . . $ Strings_createFixedLength(4779411fb981e1c). (mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.); |
| 72 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (mycxx . . $ `melder/melder.h:44:9`! numberOfColumns > 0); |
| 73 | for (integercxx . . $ integer# irow = 1; irow <= mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; irow ++) { |
| 74 | const integercxx . . $ integer# col = NUMmaxPoscxx . . $ NUMmaxPos(99bbb1d062110345). (mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (irow)); |
| 75 | if (mycxx . . $ `melder/melder.h:44:9`! columnLabelscxx . . $ structTableOfReal#columnLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).col]) |
| 76 | Strings_replacecxx . . $ Strings_replace(48d18350c34fd7ae). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), irow, mycxx . . $ `melder/melder.h:44:9`! columnLabelscxx . . $ structTableOfReal#columnLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).col].getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 77 | } |
| 78 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 79 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 80 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": strings cannot be created."); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | autoCategoriescxx . . $ autoCategories# ClassificationTable_to_Categories_maximumProbabilitycxx . . $ ClassificationTable_to_Categories_maximumProbability(dd87c7efa0d39070). (ClassificationTablecxx . . $ ClassificationTable# me) { |
| 85 | try { |
| 86 | autoCategoriescxx . . $ autoCategories# thee = Categories_createcxx . . $ Categories_create(8007a1fe88fa0475). (); |
| 87 | Melder_assertcxx . . $ `melder/melder_assert.h:39:10`! (mycxx . . $ `melder/melder.h:44:9`! numberOfColumns > 0); |
| 88 | for (integercxx . . $ integer# irow = 1; irow <= mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; irow ++) { |
| 89 | const integercxx . . $ integer# col = NUMmaxPoscxx . . $ NUMmaxPos(99bbb1d062110345). (mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data..rowcxx . . $ automatrix#row(2d545b3cb3f8e666). (irow)); |
| 90 | OrderedOfString_appendcxx . . $ OrderedOfString_append(ade3ccabe4a97830). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), mycxx . . $ `melder/melder.h:44:9`! columnLabelscxx . . $ structTableOfReal#columnLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).col].getcxx . . $ _autostring#get(5a2a78a048fb49a8).()); |
| 91 | } |
| 92 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 93 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 94 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no Categories created."); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | autoCorrelationcxx . . $ autoCorrelation# ClassificationTable_to_Correlation_columnscxx . . $ ClassificationTable_to_Correlation_columns(acb4bc943cce890a). (ClassificationTablecxx . . $ ClassificationTable# me) { |
| 99 | try { |
| 100 | autoCorrelationcxx . . $ autoCorrelation# thee = Correlation_createcxx . . $ Correlation_create(daa5c53f023e950f). (mycxx . . $ `melder/melder.h:44:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.); |
| 101 | for (integercxx . . $ integer# icol = 1; icol <= thycxx . . $ `melder/melder.h:45:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.; icol ++) { |
| 102 | const conststring32cxx . . $ conststring32# label = mycxx . . $ `melder/melder.h:44:9`! columnLabelscxx . . $ structTableOfReal#columnLabels. [cxx . . $ _autostringautovector#`operator[]`(f191be2b65019068).icol].getcxx . . $ _autostring#get(5a2a78a048fb49a8).(); |
| 103 | TableOfReal_setRowLabelcxx . . $ TableOfReal_setRowLabel(7bba53c4f5b398fa). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), icol, label); |
| 104 | TableOfReal_setColumnLabelcxx . . $ TableOfReal_setColumnLabel(7bba53c4f5b398fa). (thee.getcxx . . $ autoSomeThing#get(34d6acfd341ba7ab).(), icol, label); |
| 105 | } |
| 106 | for (integercxx . . $ integer# irow = 1; irow <= thycxx . . $ `melder/melder.h:45:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.; irow ++) { |
| 107 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).irow] = 1.0; |
| 108 | for (integercxx . . $ integer# icol = irow + 1; icol <= thycxx . . $ `melder/melder.h:45:9`! numberOfColumnscxx . . $ structTableOfReal#numberOfColumns.; icol ++) { |
| 109 | const double n12 = NUMinnercxx . . $ NUMinner(5e5adb47f563a3e3). (mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data..columncxx . . $ automatrix#column(a8b9553e85c2b1fc). (irow), mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data..columncxx . . $ automatrix#column(a8b9553e85c2b1fc). (icol)); |
| 110 | const double n11 = NUMinnercxx . . $ NUMinner(5e5adb47f563a3e3). (mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data..columncxx . . $ automatrix#column(a8b9553e85c2b1fc). (irow), mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data..columncxx . . $ automatrix#column(a8b9553e85c2b1fc). (irow)); |
| 111 | const double n22 = NUMinnercxx . . $ NUMinner(5e5adb47f563a3e3). (mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data..columncxx . . $ automatrix#column(a8b9553e85c2b1fc). (icol), mycxx . . $ `melder/melder.h:44:9`! datacxx . . $ structTableOfReal#data..columncxx . . $ automatrix#column(a8b9553e85c2b1fc). (icol)); |
| 112 | // probabilities might be very low! |
| 113 | if (n12 > 0.0 && n22 > 0.0) { |
| 114 | thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).irow] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).icol] = thycxx . . $ `melder/melder.h:45:9`! datacxx . . $ structTableOfReal#data. [cxx . . $ automatrix#`operator[]`(2d545b3cb3f8e666).icol] [cxx . . $ vector#`operator[]`(8a4599b8835d1ce1).irow] = n12 / sqrtcxx . . $ sqrt(fee4f1d22bc8e617). (n11 * n22); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | thycxx . . $ `melder/melder.h:45:9`! numberOfObservationscxx . . $ structSSCP#numberOfObservations. = mycxx . . $ `melder/melder.h:44:9`! numberOfRowscxx . . $ structTableOfReal#numberOfRows.; |
| 119 | return theecxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 120 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 121 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": no correlation created."); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /* End of file ClassificationTable.cpp */ |