| 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" |
| 20 |
#include "Graphics.h" |
| 21 |
|
| 22 |
#include "oo_DESTROY.h" |
| 23 |
#include "NoulliGrid_def.h" |
| 24 |
#include "oo_COPY.h" |
| 25 |
#include "NoulliGrid_def.h" |
| 26 |
#include "oo_EQUAL.h" |
| 27 |
#include "NoulliGrid_def.h" |
| 28 |
#include "oo_CAN_WRITE_AS_ENCODING.h" |
| 29 |
#include "NoulliGrid_def.h" |
| 30 |
#include "oo_WRITE_TEXT.h" |
| 31 |
#include "NoulliGrid_def.h" |
| 32 |
#include "oo_READ_TEXT.h" |
| 33 |
#include "NoulliGrid_def.h" |
| 34 |
#include "oo_WRITE_BINARY.h" |
| 35 |
#include "NoulliGrid_def.h" |
| 36 |
#include "oo_READ_BINARY.h" |
| 37 |
#include "NoulliGrid_def.h" |
| 38 |
#include "oo_DESCRIPTION.h" |
| 39 |
#include "NoulliGrid_def.h" |
| 40 |
|
| 41 |
Thing_implement (NoulliPoint, Function, 0); |
| 42 |
|
| 43 |
Thing_implement (NoulliTier, Function, 0); |
| 44 |
|
| 45 |
Thing_implement (NoulliGrid, Function, 0); |
| 46 |
|
| 47 |
integer NoulliPoint_getWinningCategory (NoulliPoint me) { |
| 48 |
integer winningCategory = 0; |
| 49 |
double maximumProbability = 0.0; |
| 50 |
for (integer icat = 1; icat <= my numberOfCategories; icat ++) { |
| 51 |
if (my probabilities [icat] > maximumProbability) { // NaN-safe comparison |
| 52 |
maximumProbability = my probabilities [icat]; |
| 53 |
winningCategory = icat; |
| 54 |
} |
| 55 |
} |
| 56 |
return winningCategory; |
| 57 |
} |
| 58 |
|
| 59 |
autoNoulliPoint NoulliGrid_average (NoulliGrid me, integer tierNumber, double tmin, double tmax) { |
| 60 |
try { |
| 61 |
Melder_require (tierNumber >= 1, U"Tier number should be positive"); |
| 62 |
Melder_require (tierNumber <= my tiers.size, |
| 63 |
U"Tier number (", tierNumber, U") should not be higher than the number of tiers (", my tiers.size, U")."); |
| 64 |
NoulliTier tier = my tiers.at [tierNumber]; |
| 65 |
autoNoulliPoint you = Thing_new (NoulliPoint); |
| 66 |
your numberOfCategories = my numberOfCategories; |
| 67 |
your probabilities = zero_VEC (my numberOfCategories); |
| 68 |
double numberOfSeconds = 0.0; |
| 69 |
for (integer ipoint = 1; ipoint <= tier -> points.size; ipoint ++) { |
| 70 |
NoulliPoint inpoint = tier -> points.at [ipoint]; |
| 71 |
if (inpoint -> xmax > tmin && inpoint -> xmin < tmax) { |
| 72 |
const double duration = inpoint -> xmax - inpoint -> xmin; |
| 73 |
for (integer icat = 1; icat <= my numberOfCategories; icat ++) |
| 74 |
your probabilities [icat] += inpoint -> probabilities [icat] * duration; |
| 75 |
numberOfSeconds += duration; |
| 76 |
} |
| 77 |
} |
| 78 |
if (numberOfSeconds == 0.0) { |
| 79 |
for (integer icat = 1; icat <= my numberOfCategories; icat ++) |
| 80 |
your probabilities [icat] = undefined; |
| 81 |
} else { |
| 82 |
for (integer icat = 1; icat <= my numberOfCategories; icat ++) |
| 83 |
your probabilities [icat] /= numberOfSeconds; |
| 84 |
} |
| 85 |
return you; |
| 86 |
} catch (MelderError) { |
| 87 |
Melder_throw (me, U": average not computed."); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
autoVEC NoulliGrid_getAverageProbabilities (NoulliGrid me, integer tierNumber, double tmin, double tmax) { |
| 92 |
autoNoulliPoint point = NoulliGrid_average (me, tierNumber, tmin, tmax); |
| 93 |
return copy_VEC (point -> probabilities.get()); |
| 94 |
} |
| 95 |
|
| 96 |
void NoulliGrid_paintInside (NoulliGrid me, Graphics graphics, double tmin, double tmax) { |
| 97 |
Function_unidirectionalAutowindow (me, & tmin, & tmax); |
| 98 |
Graphics_setColour (graphics, Melder_WHITE); |
| 99 |
Graphics_setWindow (graphics, 0.0, 1.0, 0.0, 1.0); |
| 100 |
Graphics_fillRectangle (graphics, 0.0, 1.0, 0.0, 1.0); |
| 101 |
if (my numberOfCategories == 2) { |
| 102 |
Graphics_setWindow (graphics, tmin, tmax, 0.0, 1.0); |
| 103 |
Graphics_setLineWidth (graphics, 3.0); |
| 104 |
for (integer itier = 1; itier <= my tiers.size; itier ++) { |
| 105 |
NoulliTier tier = my tiers.at [itier]; |
| 106 |
for (integer ipoint = 1; ipoint < tier -> points.size; ipoint ++) { |
| 107 |
NoulliPoint point = tier -> points.at [ipoint], nextPoint = tier -> points.at [ipoint + 1]; |
| 108 |
const double time = 0.5 * (point -> xmin + point -> xmax); |
| 109 |
const double nextTime = 0.5 * (nextPoint -> xmin + nextPoint -> xmax); |
| 110 |
if (time > tmin && nextTime < tmax) { |
| 111 |
const double prob = point -> probabilities [1], nextProb = nextPoint -> probabilities [1]; |
| 112 |
Graphics_setColour (graphics, Melder_cyclingBackgroundColour (itier)); |
| 113 |
Graphics_line (graphics, time, prob, nextTime, nextProb); |
| 114 |
} |
| 115 |
} |
| 116 |
Graphics_setColour (graphics, Melder_BLACK); |
| 117 |
} |
| 118 |
} else { |
| 119 |
Graphics_setWindow (graphics, tmin, tmax, 0.0, my tiers.size); |
| 120 |
for (integer itier = 1; itier <= my tiers.size; itier ++) { |
| 121 |
NoulliTier tier = my tiers.at [itier]; |
| 122 |
const double ymin = my tiers.size - itier, ymax = ymin + 1.0; |
| 123 |
for (integer ipoint = 1; ipoint <= tier -> points.size; ipoint ++) { |
| 124 |
NoulliPoint point = tier -> points.at [ipoint]; |
| 125 |
if (point -> xmax > tmin && point -> xmin < tmax) { |
| 126 |
const double xmin = Melder_clippedLeft (tmin, point -> xmin); |
| 127 |
const double xmax = Melder_clippedRight (point -> xmax, tmax); |
| 128 |
double prob1 = 1.0; |
| 129 |
for (integer icategory = 1; icategory <= point -> numberOfCategories; icategory ++) { |
| 130 |
const double prob2 = prob1; |
| 131 |
prob1 -= point -> probabilities [icategory]; |
| 132 |
Graphics_setColour (graphics, Melder_cyclingBackgroundColour (icategory)); |
| 133 |
Graphics_fillRectangle (graphics, xmin, xmax, |
| 134 |
ymin + prob1 * (ymax - ymin), ymin + prob2 * (ymax - ymin)); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
Graphics_setColour (graphics, Melder_BLACK); |
| 139 |
if (itier > 1) { |
| 140 |
Graphics_setLineWidth (graphics, 1.0); |
| 141 |
Graphics_line (graphics, tmin, ymax, tmax, ymax); |
| 142 |
} |
| 143 |
} |
| 144 |
} |
| 145 |
Graphics_setLineWidth (graphics, 1.0); |
| 146 |
Graphics_setColour (graphics, Melder_BLACK); |
| 147 |
} |
| 148 |
|
| 149 |
void NoulliGrid_paint (NoulliGrid me, Graphics g, double tmin, double tmax, bool garnish) { |
| 150 |
Graphics_setInner (g); |
| 151 |
NoulliGrid_paintInside (me, g, tmin, tmax); |
| 152 |
Graphics_unsetInner (g); |
| 153 |
if (garnish) { |
| 154 |
Graphics_drawInnerBox (g); |
| 155 |
Graphics_textBottom (g, true, U"Time (s)"); |
| 156 |
Graphics_marksBottom (g, 2, true, true, false); |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
/* End of file NoulliGrid.cpp */ |
| 161 |
|