| 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" |
| 30 | #include "Distributions_and_Strings.h" |
| 31 | #include "Strings_extensions.h" |
| 32 | #include "NUM2.h" |
| 33 | |
| 34 | Thing_implement (ClassificationTable, TableOfReal, 0); |
| 35 | |
| 36 | autoClassificationTable ClassificationTable_create (integer numberOfRows, integer numberOfClasses) { |
| 37 | try { |
| 38 | autoClassificationTable me = Thing_new (ClassificationTable); |
| 39 | TableOfReal_init (me.get(), numberOfRows, numberOfClasses); |
| 40 | return me; |
| 41 | } catch (MelderError) { |
| 42 | Melder_throw (U"ClassificationTable not created."); |
| 43 | } |
| 44 | } |
| 45 | |
| 46 | autoConfusion ClassificationTable_to_Confusion (ClassificationTable me, bool onlyClassLabels) { |
| 47 | try { |
| 48 | autoStrings responses = TableOfReal_extractColumnLabelsAsStrings (me); |
| 49 | autoStrings s2 = TableOfReal_extractRowLabelsAsStrings (me); |
| 50 | autoDistributions d2 = Strings_to_Distributions (s2.get()); |
| 51 | autoStrings stimuli = TableOfReal_extractRowLabelsAsStrings (d2.get()); |
| 52 | autoConfusion thee = Confusion_createFromStringses (( onlyClassLabels ? responses.get() : stimuli.get() ), responses.get()); |
| 53 | Confusion_ClassificationTable_increase (thee.get(), me); |
| 54 | return thee; |
| 55 | } catch (MelderError) { |
| 56 | Melder_throw (me, U": confusions cannot be calculated."); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | void Confusion_ClassificationTable_increase (Confusion me, ClassificationTable thee) { |
| 61 | Melder_require (my numberOfColumns == thy numberOfColumns, |
| 62 | U"The number of columns should be equal."); |
| 63 | for (integer irow = 1; irow <= thy numberOfRows; irow ++) { |
| 64 | const integer index = TableOfReal_getColumnIndexAtMaximumInRow (thee, irow); |
| 65 | Confusion_increase (me, thy rowLabels [irow].get(), my columnLabels [index].get()); |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | autoStrings ClassificationTable_to_Strings_maximumProbability (ClassificationTable me) { |
| 70 | try { |
| 71 | autoStrings thee = Strings_createFixedLength (my numberOfRows); |
| 72 | Melder_assert (my numberOfColumns > 0); |
| 73 | for (integer irow = 1; irow <= my numberOfRows; irow ++) { |
| 74 | const integer col = NUMmaxPos (my data.row (irow)); |
| 75 | if (my columnLabels [col]) |
| 76 | Strings_replace (thee.get(), irow, my columnLabels [col].get()); |
| 77 | } |
| 78 | return thee; |
| 79 | } catch (MelderError) { |
| 80 | Melder_throw (me, U": strings cannot be created."); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | autoCategories ClassificationTable_to_Categories_maximumProbability (ClassificationTable me) { |
| 85 | try { |
| 86 | autoCategories thee = Categories_create (); |
| 87 | Melder_assert (my numberOfColumns > 0); |
| 88 | for (integer irow = 1; irow <= my numberOfRows; irow ++) { |
| 89 | const integer col = NUMmaxPos (my data.row (irow)); |
| 90 | OrderedOfString_append (thee.get(), my columnLabels [col].get()); |
| 91 | } |
| 92 | return thee; |
| 93 | } catch (MelderError) { |
| 94 | Melder_throw (me, U": no Categories created."); |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | autoCorrelation ClassificationTable_to_Correlation_columns (ClassificationTable me) { |
| 99 | try { |
| 100 | autoCorrelation thee = Correlation_create (my numberOfColumns); |
| 101 | for (integer icol = 1; icol <= thy numberOfColumns; icol ++) { |
| 102 | const conststring32 label = my columnLabels [icol].get(); |
| 103 | TableOfReal_setRowLabel (thee.get(), icol, label); |
| 104 | TableOfReal_setColumnLabel (thee.get(), icol, label); |
| 105 | } |
| 106 | for (integer irow = 1; irow <= thy numberOfColumns; irow ++) { |
| 107 | thy data [irow] [irow] = 1.0; |
| 108 | for (integer icol = irow + 1; icol <= thy numberOfColumns; icol ++) { |
| 109 | const double n12 = NUMinner (my data.column (irow), my data.column (icol)); |
| 110 | const double n11 = NUMinner (my data.column (irow), my data.column (irow)); |
| 111 | const double n22 = NUMinner (my data.column (icol), my data.column (icol)); |
| 112 | // probabilities might be very low! |
| 113 | if (n12 > 0.0 && n22 > 0.0) { |
| 114 | thy data [irow] [icol] = thy data [icol] [irow] = n12 / sqrt (n11 * n22); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | thy numberOfObservations = my numberOfRows; |
| 119 | return thee; |
| 120 | } catch (MelderError) { |
| 121 | Melder_throw (me, U": no correlation created."); |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | /* End of file ClassificationTable.cpp */ |