| 1 | /* ExperimentMFC.cpp |
| 2 | * |
| 3 | * Copyright (C) 2001-2009,2011-2013,2015-2024 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 | /* |
| 20 | * pb 2002/07/16 GPL |
| 21 | * pb 2002/10/31 NUMlog2 |
| 22 | * pb 2003/03/08 inter-stimulus interval; version 2 |
| 23 | * pb 2003/09/14 MelderFolder_relativePathToFile |
| 24 | * pb 2004/06/22 added response keys; version 3 |
| 25 | * pb 2004/08/12 removed a bug (something said carrierBefore instead of carrierAfter) |
| 26 | * that caused Praat to crash if the carrier before was longer than the carrier after |
| 27 | * pb 2005/11/21 added replay button; version 4 |
| 28 | * pb 2005/12/02 response sounds are read |
| 29 | * pb 2006/10/28 erased MacOS 9 stuff |
| 30 | * pb 2006/12/20 stereo |
| 31 | * pb 2007/08/12 wchar |
| 32 | * pb 2007/09/26 added font size; version 5 |
| 33 | * pb 2007/10/01 can write as encoding |
| 34 | * pb 2008/04/08 in ExtractResults, check that a resonse was given |
| 35 | * pb 2008/10/20 except nonstandard sound files |
| 36 | * pb 2009/03/18 removed a bug introduced by the previous change (an error check got hidden) |
| 37 | * pb 2011/03/03 added reaction times; version 2 of ResultsMFC |
| 38 | * pb 2011/03/15 allowed result extraction from incomplete experiments |
| 39 | * pb 2011/03/23 C++ |
| 40 | * pb 2011/07/06 C++ |
| 41 | * pb 2013/01/01 added blank while playing; version 6 |
| 42 | * pb 2016/09/25 added font size and response key to goodness ratings; version 7 |
| 43 | */ |
| 44 | |
| 45 | #include "ExperimentMFC.h" |
| 46 | |
| 47 | #include "oo_DESTROY.h" |
| 48 | #include "ExperimentMFC_def.h" |
| 49 | #include "oo_COPY.h" |
| 50 | #include "ExperimentMFC_def.h" |
| 51 | #include "oo_EQUAL.h" |
| 52 | #include "ExperimentMFC_def.h" |
| 53 | #include "oo_CAN_WRITE_AS_ENCODING.h" |
| 54 | #include "ExperimentMFC_def.h" |
| 55 | #include "oo_READ_TEXT.h" |
| 56 | #include "ExperimentMFC_def.h" |
| 57 | #include "oo_WRITE_TEXT.h" |
| 58 | #include "ExperimentMFC_def.h" |
| 59 | #include "oo_READ_BINARY.h" |
| 60 | #include "ExperimentMFC_def.h" |
| 61 | #include "oo_WRITE_BINARY.h" |
| 62 | #include "ExperimentMFC_def.h" |
| 63 | #include "oo_DESCRIPTION.h" |
| 64 | #include "ExperimentMFC_def.h" |
| 65 | |
| 66 | #include "enums_getText.h" |
| 67 | #include "Experiment_enums.h" |
| 68 | #include "enums_getValue.h" |
| 69 | #include "Experiment_enums.h" |
| 70 | |
| 71 | |
| 72 | #pragma mark - class ExperimentMFC |
| 73 | |
| 74 | Thing_implement (ExperimentMFC, Daata, 7); |
| 75 | |
| 76 | static void readSound (ExperimentMFC me, conststring32 fileNameHead, conststring32 fileNameTail, |
| 77 | double medialSilenceDuration, conststring32 name, autoSound *sound) |
| 78 | { |
| 79 | char32 fileNameBuffer [256], *fileNames = & fileNameBuffer [0]; |
| 80 | Melder_sprint (fileNameBuffer,256, name); |
| 81 | structMelderFile file { }; |
| 82 | /* |
| 83 | The following conversion is needed when fileNameHead is an absolute path, |
| 84 | and the stimulus names contain slashes for relative paths. |
| 85 | An ugly case, but allowed. |
| 86 | */ |
| 87 | #if defined (_WIN32) |
| 88 | for (;;) { char32 *slash = str32chr (fileNames, U'/'); if (! slash) break; *slash = U'\\'; } |
| 89 | #endif |
| 90 | sound -> reset(); |
| 91 | char32 pathName [kMelder_MAXPATH+1]; |
| 92 | /* |
| 93 | `fileNames` can contain commas, which separate partial file names. |
| 94 | The separate files should be concatenated. |
| 95 | */ |
| 96 | for (;;) { |
| 97 | /* |
| 98 | Determine partial file name. |
| 99 | */ |
| 100 | char32 *comma = str32chr (fileNames, U','); |
| 101 | if (comma) |
| 102 | *comma = U'\0'; |
| 103 | /* |
| 104 | Determine complete (relative) file name. |
| 105 | */ |
| 106 | Melder_sprint (pathName,kMelder_MAXPATH+1, fileNameHead, fileNames, fileNameTail); |
| 107 | /* |
| 108 | Make sure we are in the correct folder. |
| 109 | */ |
| 110 | if (MelderFolder_isNull (& my rootDirectory)) { |
| 111 | /* |
| 112 | Absolute file name. |
| 113 | */ |
| 114 | Melder_pathToFile (pathName, & file); |
| 115 | } else { |
| 116 | /* |
| 117 | Relative or absolute file name. |
| 118 | */ |
| 119 | MelderFolder_relativePathToFile (& my rootDirectory, pathName, & file); |
| 120 | if (Melder_debug == 32) { |
| 121 | MelderInfo_open (); |
| 122 | MelderInfo_writeLine (U"Path name <", pathName, U">"); |
| 123 | MelderInfo_writeLine (U"Root folder <", MelderFolder_peekPath (& my rootDirectory), U">"); |
| 124 | MelderInfo_writeLine (U"Full path name <", MelderFile_peekPath (& file), U">"); |
| 125 | MelderInfo_close (); |
| 126 | } |
| 127 | } |
| 128 | /* |
| 129 | Read the substimulus. |
| 130 | */ |
| 131 | autoSound substimulus = Data_readFromFile (& file).static_cast_move <structSound>(); |
| 132 | Melder_require (substimulus -> classInfo == classSound, |
| 133 | U"File ", & file, U" contains a ", Thing_className (substimulus.get()), U" instead of a sound."); |
| 134 | /* |
| 135 | Check whether all sounds have the same number of channels. |
| 136 | */ |
| 137 | if (my numberOfChannels == 0) { // first time? |
| 138 | my numberOfChannels = substimulus -> ny; |
| 139 | } else if (substimulus -> ny != my numberOfChannels) { |
| 140 | Melder_throw (U"The sound in file ", & file, U" has a different number of channels than some other sound."); |
| 141 | } |
| 142 | /* |
| 143 | Check whether all sounds have the same sampling frequency. |
| 144 | */ |
| 145 | if (my samplePeriod == 0.0) { |
| 146 | my samplePeriod = substimulus -> dx; // this must be the first sound read |
| 147 | } else if (substimulus -> dx != my samplePeriod) { |
| 148 | Melder_throw (U"The sound in file ", & file, U" has a different sampling frequency than some other sound."); |
| 149 | } |
| 150 | /* |
| 151 | Append the substimuli, perhaps with silent intervals. |
| 152 | */ |
| 153 | if (*sound) { |
| 154 | *sound = Sounds_append (sound->get(), medialSilenceDuration, substimulus.get()); |
| 155 | } else { |
| 156 | *sound = substimulus.move(); |
| 157 | } |
| 158 | /* |
| 159 | Cycle. |
| 160 | */ |
| 161 | if (! comma) |
| 162 | break; |
| 163 | fileNames = & comma [1]; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | static void permuteRandomly (ExperimentMFC me, integer first, integer last) { |
| 168 | for (integer itrial = first; itrial < last; itrial ++) { |
| 169 | integer jtrial = NUMrandomInteger (itrial, last); |
| 170 | std::swap (my stimuli [itrial], my stimuli [jtrial]); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | void ExperimentMFC_start (ExperimentMFC me) { |
| 175 | try { |
| 176 | integer maximumStimulusPlaySamples, maximumResponsePlaySamples, maximumPlaySamples; |
| 177 | integer stimulusCarrierBeforeSamples = 0, stimulusCarrierAfterSamples = 0, maximumStimulusSamples = 0; |
| 178 | integer responseCarrierBeforeSamples = 0, responseCarrierAfterSamples = 0, maximumResponseSamples = 0; |
| 179 | Melder_warningOff (); |
| 180 | my trial = 0; |
| 181 | my playBuffer. reset(); // is this needed? |
| 182 | my pausing = false; |
| 183 | my numberOfTrials = my numberOfDifferentStimuli * my numberOfReplicationsPerStimulus; |
| 184 | my stimuli = zero_INTVEC (my numberOfTrials); |
| 185 | my responses = zero_INTVEC (my numberOfTrials); |
| 186 | my goodnesses = zero_VEC (my numberOfTrials); |
| 187 | my startingTime = undefined; // not zero, in order that reaction times are never the duration that the computer has been running |
| 188 | my reactionTimes = zero_VEC (my numberOfTrials); |
| 189 | /* |
| 190 | Read all the sounds. They must all have the same sampling frequency and number of channels. |
| 191 | */ |
| 192 | my samplePeriod = 0.0; |
| 193 | my numberOfChannels = 0; |
| 194 | if (my stimuliAreSounds) { |
| 195 | if (my stimulusCarrierBefore. name && my stimulusCarrierBefore. name [0]) { |
| 196 | readSound (me, my stimulusFileNameHead.get(), my stimulusFileNameTail.get(), my stimulusMedialSilenceDuration, |
| 197 | my stimulusCarrierBefore. name.get(), & my stimulusCarrierBefore. sound); |
| 198 | stimulusCarrierBeforeSamples = my stimulusCarrierBefore. sound -> nx; |
| 199 | } |
| 200 | if (my stimulusCarrierAfter. name && my stimulusCarrierAfter. name [0]) { |
| 201 | readSound (me, my stimulusFileNameHead.get(), my stimulusFileNameTail.get(), my stimulusMedialSilenceDuration, |
| 202 | my stimulusCarrierAfter. name.get(), & my stimulusCarrierAfter. sound); |
| 203 | stimulusCarrierAfterSamples = my stimulusCarrierAfter. sound -> nx; |
| 204 | } |
| 205 | for (integer istim = 1; istim <= my numberOfDifferentStimuli; istim ++) { |
| 206 | readSound (me, my stimulusFileNameHead.get(), my stimulusFileNameTail.get(), my stimulusMedialSilenceDuration, |
| 207 | my stimulus [istim]. name.get(), & my stimulus [istim]. sound); |
| 208 | if (my stimulus [istim]. sound -> nx > maximumStimulusSamples) |
| 209 | maximumStimulusSamples = my stimulus [istim]. sound -> nx; |
| 210 | } |
| 211 | } |
| 212 | if (my responsesAreSounds) { |
| 213 | if (my responseCarrierBefore. name && my responseCarrierBefore. name [0]) { |
| 214 | readSound (me, my responseFileNameHead.get(), my responseFileNameTail.get(), my responseMedialSilenceDuration, |
| 215 | my responseCarrierBefore. name.get(), & my responseCarrierBefore. sound); |
| 216 | responseCarrierBeforeSamples = my responseCarrierBefore. sound -> nx; |
| 217 | } |
| 218 | if (my responseCarrierAfter. name && my responseCarrierAfter. name [0]) { |
| 219 | readSound (me, my responseFileNameHead.get(), my responseFileNameTail.get(), my responseMedialSilenceDuration, |
| 220 | my responseCarrierAfter. name.get(), & my responseCarrierAfter. sound); |
| 221 | responseCarrierAfterSamples = my responseCarrierAfter. sound -> nx; |
| 222 | } |
| 223 | for (integer iresp = 1; iresp <= my numberOfDifferentResponses; iresp ++) { |
| 224 | readSound (me, my responseFileNameHead.get(), my responseFileNameTail.get(), my responseMedialSilenceDuration, |
| 225 | my response [iresp]. name.get(), & my response [iresp]. sound); |
| 226 | if (my response [iresp]. sound -> nx > maximumResponseSamples) |
| 227 | maximumResponseSamples = my response [iresp]. sound -> nx; |
| 228 | } |
| 229 | } |
| 230 | /* |
| 231 | Create the play buffer. |
| 232 | */ |
| 233 | maximumStimulusPlaySamples = |
| 234 | Melder_iround (my stimulusInitialSilenceDuration / my samplePeriod) |
| 235 | + Melder_iround (my stimulusFinalSilenceDuration / my samplePeriod) |
| 236 | + stimulusCarrierBeforeSamples + maximumStimulusSamples + stimulusCarrierAfterSamples + 2; |
| 237 | maximumResponsePlaySamples = |
| 238 | Melder_iround (my responseInitialSilenceDuration / my samplePeriod) |
| 239 | + Melder_iround (my responseFinalSilenceDuration / my samplePeriod) |
| 240 | + responseCarrierBeforeSamples + maximumResponseSamples + responseCarrierAfterSamples + 2; |
| 241 | maximumPlaySamples = maximumStimulusPlaySamples > maximumResponsePlaySamples ? maximumStimulusPlaySamples : maximumResponsePlaySamples; |
| 242 | my playBuffer = Sound_create (my numberOfChannels, 0.0, maximumPlaySamples * my samplePeriod, |
| 243 | maximumPlaySamples, my samplePeriod, 0.5 * my samplePeriod); |
| 244 | /* |
| 245 | Determine the order in which the stimuli will be presented to the subject. |
| 246 | */ |
| 247 | if (my randomize == kExperiment_randomize::CYCLIC_NON_RANDOM) { |
| 248 | for (integer itrial = 1; itrial <= my numberOfTrials; itrial ++) |
| 249 | my stimuli [itrial] = (itrial - 1) % my numberOfDifferentStimuli + 1; |
| 250 | } else if (my randomize == kExperiment_randomize::PERMUTE_ALL) { |
| 251 | for (integer itrial = 1; itrial <= my numberOfTrials; itrial ++) |
| 252 | my stimuli [itrial] = (itrial - 1) % my numberOfDifferentStimuli + 1; |
| 253 | permuteRandomly (me, 1, my numberOfTrials); |
| 254 | } else if (my randomize == kExperiment_randomize::PERMUTE_BALANCED) { |
| 255 | for (integer ireplica = 1; ireplica <= my numberOfReplicationsPerStimulus; ireplica ++) { |
| 256 | integer offset = (ireplica - 1) * my numberOfDifferentStimuli; |
| 257 | for (integer istim = 1; istim <= my numberOfDifferentStimuli; istim ++) |
| 258 | my stimuli [offset + istim] = istim; |
| 259 | permuteRandomly (me, offset + 1, offset + my numberOfDifferentStimuli); |
| 260 | } |
| 261 | } else if (my randomize == kExperiment_randomize::PERMUTE_BALANCED_NO_DOUBLETS) { |
| 262 | for (integer ireplica = 1; ireplica <= my numberOfReplicationsPerStimulus; ireplica ++) { |
| 263 | integer offset = (ireplica - 1) * my numberOfDifferentStimuli; |
| 264 | for (integer istim = 1; istim <= my numberOfDifferentStimuli; istim ++) |
| 265 | my stimuli [offset + istim] = istim; |
| 266 | do { |
| 267 | permuteRandomly (me, offset + 1, offset + my numberOfDifferentStimuli); |
| 268 | } while (ireplica != 1 && my stimuli [offset + 1] == my stimuli [offset] && my numberOfDifferentStimuli > 1); |
| 269 | } |
| 270 | } else if (my randomize == kExperiment_randomize::WITH_REPLACEMENT) { |
| 271 | for (integer itrial = 1; itrial <= my numberOfTrials; itrial ++) |
| 272 | my stimuli [itrial] = NUMrandomInteger (1, my numberOfDifferentStimuli); |
| 273 | } |
| 274 | Melder_warningOn (); |
| 275 | } catch (MelderError) { |
| 276 | Melder_warningOn (); |
| 277 | my numberOfTrials = 0; |
| 278 | my stimuli. reset(); |
| 279 | Melder_throw (me, U": not started."); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | static void playSound (ExperimentMFC me, Sound sound, Sound carrierBefore, Sound carrierAfter, |
| 284 | double initialSilenceDuration, double finalSilenceDuration) |
| 285 | { |
| 286 | integer numberOfSamplesWritten = 0; |
| 287 | |
| 288 | const integer initialSilenceSamples = Melder_iround (initialSilenceDuration / my samplePeriod); |
| 289 | my playBuffer -> z.verticalBand (1, initialSilenceSamples) <<= 0.0; |
| 290 | numberOfSamplesWritten += initialSilenceSamples; |
| 291 | |
| 292 | if (carrierBefore) { |
| 293 | my playBuffer -> z.verticalBand (numberOfSamplesWritten + 1, numberOfSamplesWritten + carrierBefore -> nx) |
| 294 | <<= carrierBefore -> z.all(); |
| 295 | numberOfSamplesWritten += carrierBefore -> nx; |
| 296 | } |
| 297 | |
| 298 | if (sound) { |
| 299 | my playBuffer -> z.verticalBand (numberOfSamplesWritten + 1, numberOfSamplesWritten + sound -> nx) |
| 300 | <<= sound -> z.all(); |
| 301 | numberOfSamplesWritten += sound -> nx; |
| 302 | } |
| 303 | |
| 304 | if (carrierAfter) { |
| 305 | my playBuffer -> z.verticalBand (numberOfSamplesWritten + 1, numberOfSamplesWritten + carrierAfter -> nx) |
| 306 | <<= carrierAfter -> z.all(); |
| 307 | numberOfSamplesWritten += carrierAfter -> nx; |
| 308 | } |
| 309 | |
| 310 | const integer finalSilenceSamples = Melder_iround (finalSilenceDuration / my samplePeriod); |
| 311 | my playBuffer -> z.verticalBand (numberOfSamplesWritten + 1, numberOfSamplesWritten + finalSilenceSamples) <<= 0.0; |
| 312 | numberOfSamplesWritten += finalSilenceSamples; |
| 313 | |
| 314 | if (my stimuliAreSounds && ! my blankWhilePlaying) |
| 315 | my startingTime = Melder_clock (); |
| 316 | Sound_playPart (my playBuffer.get(), 0.0, numberOfSamplesWritten * my samplePeriod, nullptr, nullptr); |
| 317 | if (my stimuliAreSounds && my blankWhilePlaying) |
| 318 | my startingTime = Melder_clock (); |
| 319 | } |
| 320 | |
| 321 | void ExperimentMFC_playStimulus (ExperimentMFC me, integer istim) { |
| 322 | playSound (me, my stimulus [istim]. sound.get(), |
| 323 | my stimulusCarrierBefore. sound.get(), my stimulusCarrierAfter. sound.get(), |
| 324 | my stimulusInitialSilenceDuration, my stimulusFinalSilenceDuration); |
| 325 | } |
| 326 | |
| 327 | void ExperimentMFC_playResponse (ExperimentMFC me, integer iresp) { |
| 328 | playSound (me, my response [iresp]. sound.get(), |
| 329 | my responseCarrierBefore. sound.get(), my responseCarrierAfter. sound.get(), |
| 330 | my responseInitialSilenceDuration, my responseFinalSilenceDuration); |
| 331 | } |
| 332 | |
| 333 | |
| 334 | #pragma mark - class ExperimentMFCList |
| 335 | |
| 336 | Thing_implement (ExperimentMFCList, Ordered, 0); |
| 337 | |
| 338 | |
| 339 | #pragma mark - class ResultsMFC |
| 340 | |
| 341 | Thing_implement (ResultsMFC, Daata, 2); |
| 342 | |
| 343 | autoResultsMFC ResultsMFC_create (integer numberOfTrials) { |
| 344 | try { |
| 345 | autoResultsMFC me = Thing_new (ResultsMFC); |
| 346 | my numberOfTrials = numberOfTrials; |
| 347 | my result = newvectorzero <structTrialMFC> (my numberOfTrials); |
| 348 | return me; |
| 349 | } catch (MelderError) { |
| 350 | Melder_throw (U"ResultsMFC not created."); |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | autoResultsMFC ExperimentMFC_extractResults (ExperimentMFC me) { |
| 355 | try { |
| 356 | if (my trial == 0 || my trial <= my numberOfTrials) |
| 357 | Melder_warning (U"The experiment was not finished. Only the first ", my trial - 1 + my pausing, U" responses are valid."); |
| 358 | autoResultsMFC thee = ResultsMFC_create (my numberOfTrials); |
| 359 | for (integer trial = 1; trial <= my numberOfTrials; trial ++) { |
| 360 | const char32 *pipe = ( my stimulus [my stimuli [trial]]. visibleText ? |
| 361 | str32chr (my stimulus [my stimuli [trial]]. visibleText.get(), U'|') : nullptr ); |
| 362 | thy result [trial]. stimulus = Melder_dup (Melder_cat (my stimulus [my stimuli [trial]]. name.get(), pipe)); |
| 363 | //if (my responses [trial] < 1) Melder_throw (U"No response for trial ", trial, U".") |
| 364 | thy result [trial]. response = Melder_dup (my responses [trial] ? my response [my responses [trial]]. name.get() : U""); |
| 365 | thy result [trial]. goodness = my goodnesses [trial]; |
| 366 | thy result [trial]. reactionTime = my reactionTimes [trial]; |
| 367 | } |
| 368 | return thee; |
| 369 | } catch (MelderError) { |
| 370 | Melder_throw (me, U": results not extracted."); |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | autoResultsMFC ResultsMFC_removeUnsharedStimuli (ResultsMFC me, ResultsMFC thee) { |
| 375 | try { |
| 376 | autoResultsMFC him = ResultsMFC_create (thy numberOfTrials); |
| 377 | his numberOfTrials = 0; |
| 378 | for (integer i = 1; i <= thy numberOfTrials; i ++) { |
| 379 | bool present = false; |
| 380 | for (integer j = 1; j <= my numberOfTrials; j ++) { |
| 381 | if (str32equ (thy result [i]. stimulus.get(), my result [j]. stimulus.get())) { |
| 382 | present = true; |
| 383 | break; |
| 384 | } |
| 385 | } |
| 386 | if (present) { |
| 387 | his numberOfTrials ++; |
| 388 | his result [his numberOfTrials]. stimulus = Melder_dup (thy result [i]. stimulus.get()); |
| 389 | his result [his numberOfTrials]. response = Melder_dup (thy result [i]. response.get()); |
| 390 | } |
| 391 | } |
| 392 | if (his numberOfTrials == 0) |
| 393 | Melder_throw (U"No shared stimuli."); |
| 394 | return him; |
| 395 | } catch (MelderError) { |
| 396 | Melder_throw (me, U" & ", thee, U": unshared stimuli not removed."); |
| 397 | } |
| 398 | } |
| 399 | |
| 400 | autoTable ResultsMFCs_to_Table (OrderedOf<structResultsMFC>* me) { |
| 401 | try { |
| 402 | integer irow = 0; |
| 403 | bool hasGoodnesses = false, hasReactionTimes = false; |
| 404 | for (integer iresults = 1; iresults <= my size; iresults ++) { |
| 405 | ResultsMFC results = my at [iresults]; |
| 406 | for (integer itrial = 1; itrial <= results -> numberOfTrials; itrial ++) { |
| 407 | irow ++; |
| 408 | if (results -> result [itrial]. goodness != 0) |
| 409 | hasGoodnesses = true; |
| 410 | if (results -> result [itrial]. reactionTime != 0.0) |
| 411 | hasReactionTimes = true; |
| 412 | } |
| 413 | } |
| 414 | autoTable thee = Table_create (irow, 3 + hasGoodnesses + hasReactionTimes); |
| 415 | Table_renameColumn_e (thee.get(), 1, U"subject"); |
| 416 | Table_renameColumn_e (thee.get(), 2, U"stimulus"); |
| 417 | Table_renameColumn_e (thee.get(), 3, U"response"); |
| 418 | if (hasGoodnesses) |
| 419 | Table_renameColumn_e (thee.get(), 4, U"goodness"); |
| 420 | if (hasReactionTimes) |
| 421 | Table_renameColumn_e (thee.get(), 4 + hasGoodnesses, U"reactionTime"); |
| 422 | irow = 0; |
| 423 | for (integer iresults = 1; iresults <= my size; iresults ++) { |
| 424 | ResultsMFC results = my at [iresults]; |
| 425 | for (integer itrial = 1; itrial <= results -> numberOfTrials; itrial ++) { |
| 426 | irow ++; |
| 427 | Table_setStringValue (thee.get(), irow, 1, results -> name.get()); |
| 428 | Table_setStringValue (thee.get(), irow, 2, results -> result [itrial]. stimulus.get()); |
| 429 | Table_setStringValue (thee.get(), irow, 3, results -> result [itrial]. response.get()); |
| 430 | if (hasGoodnesses) |
| 431 | Table_setNumericValue (thee.get(), irow, 4, results -> result [itrial]. goodness); |
| 432 | if (hasReactionTimes) |
| 433 | Table_setNumericValue (thee.get(), irow, 4 + hasGoodnesses, results -> result [itrial]. reactionTime); |
| 434 | } |
| 435 | } |
| 436 | return thee; |
| 437 | } catch (MelderError) { |
| 438 | Melder_throw (U"ResultsMFC objects not collected to Table."); |
| 439 | } |
| 440 | } |
| 441 | |
| 442 | autoCategories ResultsMFC_to_Categories_stimuli (ResultsMFC me) { |
| 443 | try { |
| 444 | autoCategories thee = Categories_create (); |
| 445 | for (integer trial = 1; trial <= my numberOfTrials; trial ++) { |
| 446 | autoSimpleString category = SimpleString_create (my result [trial]. stimulus.get()); |
| 447 | thy addItem_move (category.move()); |
| 448 | } |
| 449 | return thee; |
| 450 | } catch (MelderError) { |
| 451 | Melder_throw (me, U": stimuli not converted to Categories."); |
| 452 | } |
| 453 | } |
| 454 | |
| 455 | autoCategories ResultsMFC_to_Categories_responses (ResultsMFC me) { |
| 456 | try { |
| 457 | autoCategories thee = Categories_create (); |
| 458 | for (integer trial = 1; trial <= my numberOfTrials; trial ++) { |
| 459 | autoSimpleString category = SimpleString_create (my result [trial]. response.get()); |
| 460 | thy addItem_move (category.move()); |
| 461 | } |
| 462 | return thee; |
| 463 | } catch (MelderError) { |
| 464 | Melder_throw (me, U": responses not converted to Categories."); |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | |
| 469 | #pragma mark - class Categories extensions |
| 470 | |
| 471 | void Categories_sort (Categories me) { |
| 472 | my sort (SimpleString_compare); |
| 473 | } |
| 474 | |
| 475 | double Categories_getEntropy (Categories me) { |
| 476 | integer numberOfTokens = 0; |
| 477 | conststring32 previousString = nullptr; |
| 478 | double entropy = 0.0; |
| 479 | autoCategories thee = Data_copy (me); |
| 480 | Categories_sort (thee.get()); |
| 481 | for (integer i = 1; i <= thy size; i ++) { |
| 482 | SimpleString s = thy at [i]; |
| 483 | conststring32 string = s -> string.get(); |
| 484 | if (previousString && ! str32equ (string, previousString)) { |
| 485 | double p = (double) numberOfTokens / thy size; |
| 486 | entropy -= p * NUMlog2 (p); |
| 487 | numberOfTokens = 1; |
| 488 | } else { |
| 489 | numberOfTokens ++; |
| 490 | } |
| 491 | previousString = string; |
| 492 | } |
| 493 | if (numberOfTokens) { |
| 494 | double p = (double) numberOfTokens / thy size; |
| 495 | entropy -= p * NUMlog2 (p); |
| 496 | } |
| 497 | return entropy; |
| 498 | } |
| 499 | |
| 500 | /* End of file ExperimentMFC.cpp */ |