| 1 | /* NoulliGrid.cpp |
| 2 | * |
| 3 | * Copyright (C) 2018,2020,2021,2023 Paul Boersma |
| 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. |
| 13 | * See the GNU 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 | #include "NoulliGrid.h"cxx . . $ `<file>/gram/NoulliGrid.h`/ |
| 20 | #include "Graphics.h"cxx . . $ `<file>/sys/Graphics.h`/ |
| 21 | |
| 22 | #include "oo_DESTROY.h"cxx . . $ `<file>/sys/oo_DESTROY.h`/ |
| 23 | #include "NoulliGrid_def.h"cxx . . $ `<file>/gram/NoulliGrid_def.h`/ |
| 24 | #include "oo_COPY.h"cxx . . $ `<file>/sys/oo_COPY.h`/ |
| 25 | #include "NoulliGrid_def.h"cxx . . $ `<file>/gram/NoulliGrid_def.h`/ |
| 26 | #include "oo_EQUAL.h"cxx . . $ `<file>/sys/oo_EQUAL.h`/ |
| 27 | #include "NoulliGrid_def.h"cxx . . $ `<file>/gram/NoulliGrid_def.h`/ |
| 28 | #include "oo_CAN_WRITE_AS_ENCODING.h"cxx . . $ `<file>/sys/oo_CAN_WRITE_AS_ENCODING.h`/ |
| 29 | #include "NoulliGrid_def.h"cxx . . $ `<file>/gram/NoulliGrid_def.h`/ |
| 30 | #include "oo_WRITE_TEXT.h"cxx . . $ `<file>/sys/oo_WRITE_TEXT.h`/ |
| 31 | #include "NoulliGrid_def.h"cxx . . $ `<file>/gram/NoulliGrid_def.h`/ |
| 32 | #include "oo_READ_TEXT.h"cxx . . $ `<file>/sys/oo_READ_TEXT.h`/ |
| 33 | #include "NoulliGrid_def.h"cxx . . $ `<file>/gram/NoulliGrid_def.h`/ |
| 34 | #include "oo_WRITE_BINARY.h"cxx . . $ `<file>/sys/oo_WRITE_BINARY.h`/ |
| 35 | #include "NoulliGrid_def.h"cxx . . $ `<file>/gram/NoulliGrid_def.h`/ |
| 36 | #include "oo_READ_BINARY.h"cxx . . $ `<file>/sys/oo_READ_BINARY.h`/ |
| 37 | #include "NoulliGrid_def.h"cxx . . $ `<file>/gram/NoulliGrid_def.h`/ |
| 38 | #include "oo_DESCRIPTION.h"cxx . . $ `<file>/sys/oo_DESCRIPTION.h`/ |
| 39 | #include "NoulliGrid_def.h"cxx . . $ `<file>/gram/NoulliGrid_def.h`/ |
| 40 | |
| 41 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (NoulliPoint, Function, 0); |
| 42 | |
| 43 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (NoulliTier, Function, 0); |
| 44 | |
| 45 | Thing_implementcxx . . $ `sys/Thing.h:65:9`! (NoulliGrid, Function, 0); |
| 46 | |
| 47 | integercxx . . $ integer# NoulliPoint_getWinningCategorycxx . . $ NoulliPoint_getWinningCategory(836ef92ee70b59b). (NoulliPoint me) { |
| 48 | integercxx . . $ integer# winningCategory = 0; |
| 49 | double maximumProbability = 0.0; |
| 50 | for (integercxx . . $ integer# icat = 1; icat <= mycxx . . $ `melder/melder.h:44:9`! numberOfCategories; icat ++) { |
| 51 | if (mycxx . . $ `melder/melder.h:44:9`! probabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).icat] > maximumProbability) { // NaN-safe comparison |
| 52 | maximumProbability = mycxx . . $ `melder/melder.h:44:9`! probabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).icat]; |
| 53 | winningCategory = icat; |
| 54 | } |
| 55 | } |
| 56 | return winningCategory; |
| 57 | } |
| 58 | |
| 59 | autoNoulliPoint NoulliGrid_averagecxx . . $ NoulliGrid_average(e93752e781671d06). (NoulliGrid me, integercxx . . $ integer# tierNumber, double tmin, double tmax) { |
| 60 | try { |
| 61 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (tierNumber >= 1, U"Tier number should be positive"); |
| 62 | Melder_requirecxx . . $ `melder/melder_require.h:21:9`! (tierNumber <= mycxx . . $ `melder/melder.h:44:9`! tiers.size, |
| 63 | U"Tier number (", tierNumber, U") should not be higher than the number of tiers (", mycxx . . $ `melder/melder.h:44:9`! tiers.size, U")."); |
| 64 | NoulliTier tier = mycxx . . $ `melder/melder.h:44:9`! tiers.atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).tierNumber]; |
| 65 | autoNoulliPoint you = Thing_newcxx . . $ `sys/Thing.h:514:9`! (NoulliPoint); |
| 66 | yourcxx . . $ `melder/melder.h:46:9`! numberOfCategories = mycxx . . $ `melder/melder.h:44:9`! numberOfCategories; |
| 67 | yourcxx . . $ `melder/melder.h:46:9`! probabilities =cxx . . $ autovector#`operator=`(f4f0443dbfdd1e40). zero_VECcxx . . $ zero_VEC(e4395f764b859b1e). (mycxx . . $ `melder/melder.h:44:9`! numberOfCategories); |
| 68 | double numberOfSeconds = 0.0; |
| 69 | for (integercxx . . $ integer# ipoint = 1; ipoint <= tier -> points.sizecxx . . $ CollectionOf#size.; ipoint ++) { |
| 70 | NoulliPoint inpoint = tier -> points.atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).ipoint]; |
| 71 | if (inpoint -> xmaxcxx . . $ structFunction#xmax. > tmin && inpoint -> xmincxx . . $ structFunction#xmin. < tmax) { |
| 72 | const double duration = inpoint -> xmaxcxx . . $ structFunction#xmax. - inpoint -> xmincxx . . $ structFunction#xmin.; |
| 73 | for (integercxx . . $ integer# icat = 1; icat <= mycxx . . $ `melder/melder.h:44:9`! numberOfCategories; icat ++) |
| 74 | yourcxx . . $ `melder/melder.h:46:9`! probabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).icat] += inpoint -> probabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).icat] * duration; |
| 75 | numberOfSeconds += duration; |
| 76 | } |
| 77 | } |
| 78 | if (numberOfSeconds == 0.0) { |
| 79 | for (integercxx . . $ integer# icat = 1; icat <= mycxx . . $ `melder/melder.h:44:9`! numberOfCategories; icat ++) |
| 80 | yourcxx . . $ `melder/melder.h:46:9`! probabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).icat] = undefinedcxx . . $ undefined.; |
| 81 | } else { |
| 82 | for (integercxx . . $ integer# icat = 1; icat <= mycxx . . $ `melder/melder.h:44:9`! numberOfCategories; icat ++) |
| 83 | yourcxx . . $ `melder/melder.h:46:9`! probabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).icat] /= numberOfSeconds; |
| 84 | } |
| 85 | return youcxx . . $ autoSomeThing#autoSomeThing(b79141a71f7bc86f).; |
| 86 | } catch (MelderErrorcxx . . $ MelderError#) { |
| 87 | Melder_throwcxx . . $ `melder/melder_error.h:77:9`! (me, U": average not computed."); |
| 88 | } |
| 89 | } |
| 90 | |
| 91 | autoVECcxx . . $ autoVEC# NoulliGrid_getAverageProbabilitiescxx . . $ NoulliGrid_getAverageProbabilities(80aa975bde4cea04). (NoulliGrid me, integercxx . . $ integer# tierNumber, double tmin, double tmax) { |
| 92 | autoNoulliPoint point = NoulliGrid_averagecxx . . $ NoulliGrid_average(e93752e781671d06). (me, tierNumber, tmin, tmax); |
| 93 | return copy_VECcxx . . $ copy_VEC(d4415e68b25283a8). (pointcxx . . $ constvectorview#constvectorview(eac9d6ec4c4bfecd). ->cxx . . $ autoSomeThing#`operator->`(34d6acfd341ba7ab). probabilities.getcxx . . $ autovector#get(e8d1da7fe68a6585).()); |
| 94 | } |
| 95 | |
| 96 | void NoulliGrid_paintInsidecxx . . $ NoulliGrid_paintInside(e9370d2b65d977e3). (NoulliGrid me, Graphicscxx . . $ Graphics# graphics, double tmin, double tmax) { |
| 97 | Function_unidirectionalAutowindowcxx . . $ Function_unidirectionalAutowindow(88b7840c59a57eaf). (me, & tmin, & tmax); |
| 98 | Graphics_setColourcxx . . $ Graphics_setColour(eb47b9513d3f4721). (graphics, Melder_WHITEcxx . . $ Melder_WHITE.); |
| 99 | Graphics_setWindowcxx . . $ Graphics_setWindow(72bdb7937dbb13da). (graphics, 0.0, 1.0, 0.0, 1.0); |
| 100 | Graphics_fillRectanglecxx . . $ Graphics_fillRectangle(72bdb7937dbb13da). (graphics, 0.0, 1.0, 0.0, 1.0); |
| 101 | if (mycxx . . $ `melder/melder.h:44:9`! numberOfCategories == 2) { |
| 102 | Graphics_setWindowcxx . . $ Graphics_setWindow(72bdb7937dbb13da). (graphics, tmin, tmax, 0.0, 1.0); |
| 103 | Graphics_setLineWidthcxx . . $ Graphics_setLineWidth(2514a1c07b6be5e2). (graphics, 3.0); |
| 104 | for (integercxx . . $ integer# itier = 1; itier <= mycxx . . $ `melder/melder.h:44:9`! tiers.sizecxx . . $ CollectionOf#size.; itier ++) { |
| 105 | NoulliTier tier = mycxx . . $ `melder/melder.h:44:9`! tiers.atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).itier]; |
| 106 | for (integercxx . . $ integer# ipoint = 1; ipoint < tier -> points.sizecxx . . $ CollectionOf#size.; ipoint ++) { |
| 107 | NoulliPoint point = tier -> points.atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).ipoint], nextPoint = tier -> points.atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).ipoint + 1]; |
| 108 | const double time = 0.5 * (point -> xmincxx . . $ structFunction#xmin. + point -> xmaxcxx . . $ structFunction#xmax.); |
| 109 | const double nextTime = 0.5 * (nextPoint -> xmincxx . . $ structFunction#xmin. + nextPoint -> xmaxcxx . . $ structFunction#xmax.); |
| 110 | if (time > tmin && nextTime < tmax) { |
| 111 | const double prob = point -> probabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1], nextProb = nextPoint -> probabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).1]; |
| 112 | Graphics_setColourcxx . . $ Graphics_setColour(eb47b9513d3f4721). (graphics, Melder_cyclingBackgroundColourcxx . . $ Melder_cyclingBackgroundColour(919a6c82b92b763e). (itier)); |
| 113 | Graphics_linecxx . . $ Graphics_line(72bdb7937dbb13da). (graphics, time, prob, nextTime, nextProb); |
| 114 | } |
| 115 | } |
| 116 | Graphics_setColourcxx . . $ Graphics_setColour(eb47b9513d3f4721). (graphics, Melder_BLACKcxx . . $ Melder_BLACK.); |
| 117 | } |
| 118 | } else { |
| 119 | Graphics_setWindowcxx . . $ Graphics_setWindow(72bdb7937dbb13da). (graphics, tmin, tmax, 0.0, mycxx . . $ `melder/melder.h:44:9`! tiers.sizecxx . . $ CollectionOf#size.); |
| 120 | for (integercxx . . $ integer# itier = 1; itier <= mycxx . . $ `melder/melder.h:44:9`! tiers.sizecxx . . $ CollectionOf#size.; itier ++) { |
| 121 | NoulliTier tier = mycxx . . $ `melder/melder.h:44:9`! tiers.atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).itier]; |
| 122 | const double ymin = mycxx . . $ `melder/melder.h:44:9`! tiers.sizecxx . . $ CollectionOf#size. - itier, ymax = ymin + 1.0; |
| 123 | for (integercxx . . $ integer# ipoint = 1; ipoint <= tier -> points.sizecxx . . $ CollectionOf#size.; ipoint ++) { |
| 124 | NoulliPoint point = tier -> points.atcxx . . $ CollectionOf#at. [cxx . . $ ArrayOf#`operator[]`(a7f36fee1ae21897).ipoint]; |
| 125 | if (point -> xmaxcxx . . $ structFunction#xmax. > tmin && point -> xmincxx . . $ structFunction#xmin. < tmax) { |
| 126 | const double xmin = Melder_clippedLeftcxx . . $ Melder_clippedLeft(f80f7d6260b3ee3a). (tmin, point -> xmincxx . . $ structFunction#xmin.); |
| 127 | const double xmax = Melder_clippedRightcxx . . $ Melder_clippedRight(f80f7d6260b3ee3a). (point -> xmaxcxx . . $ structFunction#xmax., tmax); |
| 128 | double prob1 = 1.0; |
| 129 | for (integercxx . . $ integer# icategory = 1; icategory <= point -> numberOfCategories; icategory ++) { |
| 130 | const double prob2 = prob1; |
| 131 | prob1 -= point -> probabilities [cxx . . $ autovector#`operator[]`(8a4599b8835d1ce1).icategory]; |
| 132 | Graphics_setColourcxx . . $ Graphics_setColour(eb47b9513d3f4721). (graphics, Melder_cyclingBackgroundColourcxx . . $ Melder_cyclingBackgroundColour(919a6c82b92b763e). (icategory)); |
| 133 | Graphics_fillRectanglecxx . . $ Graphics_fillRectangle(72bdb7937dbb13da). (graphics, xmin, xmax, |
| 134 | ymin + prob1 * (ymax - ymin), ymin + prob2 * (ymax - ymin)); |
| 135 | } |
| 136 | } |
| 137 | } |
| 138 | Graphics_setColourcxx . . $ Graphics_setColour(eb47b9513d3f4721). (graphics, Melder_BLACKcxx . . $ Melder_BLACK.); |
| 139 | if (itier > 1) { |
| 140 | Graphics_setLineWidthcxx . . $ Graphics_setLineWidth(2514a1c07b6be5e2). (graphics, 1.0); |
| 141 | Graphics_linecxx . . $ Graphics_line(72bdb7937dbb13da). (graphics, tmin, ymax, tmax, ymax); |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | Graphics_setLineWidthcxx . . $ Graphics_setLineWidth(2514a1c07b6be5e2). (graphics, 1.0); |
| 146 | Graphics_setColourcxx . . $ Graphics_setColour(eb47b9513d3f4721). (graphics, Melder_BLACKcxx . . $ Melder_BLACK.); |
| 147 | } |
| 148 | |
| 149 | void NoulliGrid_paintcxx . . $ NoulliGrid_paint(a524d22acd509ed9). (NoulliGrid me, Graphicscxx . . $ Graphics# g, double tmin, double tmax, bool garnish) { |
| 150 | Graphics_setInnercxx . . $ Graphics_setInner(2d838ed765120c5d). (g); |
| 151 | NoulliGrid_paintInsidecxx . . $ NoulliGrid_paintInside(e9370d2b65d977e3). (me, g, tmin, tmax); |
| 152 | Graphics_unsetInnercxx . . $ Graphics_unsetInner(2d838ed765120c5d). (g); |
| 153 | if (garnish) { |
| 154 | Graphics_drawInnerBoxcxx . . $ Graphics_drawInnerBox(2d838ed765120c5d). (g); |
| 155 | Graphics_textBottomcxx . . $ Graphics_textBottom(48cf1f690ec61203). (g, true, U"Time (s)"); |
| 156 | Graphics_marksBottomcxx . . $ Graphics_marksBottom(c3a11a8a55b243ff). (g, 2, true, true, false); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /* End of file NoulliGrid.cpp */ |
| 161 |