8 #include "StatCumProbDistDynCalc.h"
10 #include <QDataStream>
14 #include <QXmlStreamWriter>
20 #include "IException.h"
23 #include "XmlStackedHandlerReader.h"
55 : m_numberCells(other.m_numberCells),
56 m_numberQuantiles(other.m_numberQuantiles),
57 m_numberObservations(other.m_numberObservations),
58 m_quantiles(other.m_quantiles),
59 m_observationValues(other.m_observationValues),
60 m_idealNumObsBelowQuantile(other.m_idealNumObsBelowQuantile),
61 m_numObsBelowQuantile(other.m_numObsBelowQuantile) {
107 void StatCumProbDistDynCalc::setQuantiles(
unsigned int nodes) {
187 if (cumProb < 0.0 || cumProb > 1.0) {
189 "StatCumProbDistDynCalc::value(double cumProb). Must be on the domain [0, 1].";
206 unsigned int index = 0;
212 if ( dist < minDistance ) {
253 if ( x[0] == x[1] || x[0] == x[2] || x[1] == x[2]) {
259 double interp = (
cumProb-x[1]) * (
cumProb-x[2]) / (x[0]-x[1]) / (x[0]-x[2]) * y[0]
260 + (
cumProb-x[0]) * (
cumProb-x[2]) / (x[1]-x[0]) / (x[1]-x[2]) * y[1]
261 + (
cumProb-x[0]) * (
cumProb-x[1]) / (x[2]-x[0]) / (x[2]-x[1]) * y[2];
267 for (i = 0; i < 3; i++) {
274 if (y[i] <= interp && y[i+1] >= interp) {
279 return ( (x[i] -
cumProb) * y[i+1] + (x[i+1] -
cumProb) * y[i] ) / (x[i] - x[i+1]);
314 unsigned int index = 0;
317 if ( dist < minDistance) {
358 if ( x[0] == x[1] || x[0] == x[2] || x[1] == x[2]) {
364 double interp = (
value-x[1]) * (
value-x[2]) / (x[0]-x[1]) / (x[0]-x[2]) * y[0]
365 + (
value-x[0]) * (
value-x[2]) / (x[1]-x[0]) / (x[1]-x[2]) * y[1]
366 + (
value-x[0]) * (
value-x[1]) / (x[2]-x[0]) / (x[2]-x[1]) * y[2];
371 for (i = 0; i < 3; i++) {
376 if (y[i] <= interp && y[i+1] >= interp)
380 return ((x[i] -
value) * y[i+1] + (x[i+1] -
value) * y[i]) / (x[i] - x[i+1]);
441 if (fabs(temp)>1 && temp > 0.0) {
444 else if (fabs(temp)>1 && temp < 0.0) {
491 void StatCumProbDistDynCalc::save(QXmlStreamWriter &stream,
const Project *project)
const {
493 stream.writeStartElement(
"statCumProbDistDynCalc");
498 stream.writeStartElement(
"distributionData");
500 stream.writeStartElement(
"quantileInfo");
504 stream.writeAttribute(
"idealNumObsBelowQuantile",
507 stream.writeEndElement();
509 stream.writeEndElement();
510 stream.writeEndElement();
517 StatCumProbDistDynCalc::XmlHandler::XmlHandler(StatCumProbDistDynCalc *probabilityCalc,
519 m_xmlHandlerCumProbCalc = probabilityCalc;
520 m_xmlHandlerProject = project;
521 m_xmlHandlerCharacters =
"";
526 StatCumProbDistDynCalc::XmlHandler::~XmlHandler() {
529 m_xmlHandlerProject = NULL;
534 bool StatCumProbDistDynCalc::XmlHandler::startElement(
const QString &namespaceURI,
535 const QString &localName,
536 const QString &qName,
537 const QXmlAttributes &atts) {
539 m_xmlHandlerCharacters =
"";
540 if (XmlStackedHandler::startElement(namespaceURI, localName, qName, atts)) {
541 if (qName ==
"quantileInfo") {
543 QString quantile = atts.value(
"quantile");
544 QString obsValue = atts.value(
"dataValue");
545 QString idealObs = atts.value(
"idealNumObsBelowQuantile");
546 QString actualObs = atts.value(
"actualNumObsBelowQuantile");
548 if (!quantile.isEmpty() && !obsValue.isEmpty()
549 && !idealObs.isEmpty() && !actualObs.isEmpty()) {
550 m_xmlHandlerCumProbCalc->m_quantiles.append(
toDouble(quantile));
551 m_xmlHandlerCumProbCalc->m_observationValues.append(
toDouble(obsValue));
552 m_xmlHandlerCumProbCalc->m_idealNumObsBelowQuantile.append(
toDouble(idealObs));
553 m_xmlHandlerCumProbCalc->m_numObsBelowQuantile.append(
toDouble(actualObs));
563 bool StatCumProbDistDynCalc::XmlHandler::characters(
const QString &ch) {
564 m_xmlHandlerCharacters += ch;
565 return XmlStackedHandler::characters(ch);
570 bool StatCumProbDistDynCalc::XmlHandler::endElement(
const QString &namespaceURI,
571 const QString &localName,
572 const QString &qName) {
573 if (!m_xmlHandlerCharacters.isEmpty()) {
574 if (qName ==
"numberCells") {
575 m_xmlHandlerCumProbCalc->m_numberCells =
toInt(m_xmlHandlerCharacters);
577 else if (qName ==
"numberQuantiles") {
578 m_xmlHandlerCumProbCalc->m_numberQuantiles =
toInt(m_xmlHandlerCharacters);
580 else if (qName ==
"numberObservations") {
581 m_xmlHandlerCumProbCalc->m_numberObservations =
toInt(m_xmlHandlerCharacters);
584 m_xmlHandlerCharacters =
"";
586 return XmlStackedHandler::endElement(namespaceURI, localName, qName);
591 QDataStream &StatCumProbDistDynCalc::write(QDataStream &stream)
const {
604 QDataStream &StatCumProbDistDynCalc::read(QDataStream &stream) {
606 qint32 numCells, numQuantiles, numObservations;
624 return scpddc.write(stream);
630 return scpddc.read(stream);
633 void StatCumProbDistDynCalc::validate() {
636 QString msg =
"StatCumProbDistDynCalc will return no data until the quantiles have been set. "
643 QString msg =
"StatCumProbDistDynCalc will return no data until the number of observations "
646 +
"] (i.e. number of nodes) selected.";