TrainingSetFile::TSFResult TrainingSetFile::fromFile(QFile &file) { QString version, text; QStringRef name; QXmlStreamReader tsReadXML; QXmlStreamReader::TokenType tt; QStringList textElements; QXmlStreamAttributes attributes; TrainingSetFile *retTSF = new TrainingSetFile(); TSFResult res = {retTSF, true, NoError, "", 0}; TrainingSet *ts = retTSF->getTrainingSet(); int lastPatternIndex = 0, sTextElements, pSize = 0, iSize = 0, tSize = 0; Normalization *inor = new Normalization(), *tnor = new Normalization(); vector<vector<double> > inputs, targets; DataRepresentation *idr = ts->getInputsDataRepresentation(), *tdr = ts->getTargetsDataRepresentation(); if(file.open(QIODevice::ReadOnly)){ tsReadXML.setDevice(&file); while (!tsReadXML.atEnd()) { tt = tsReadXML.readNext(); if(tsReadXML.hasError()){ file.close(); return {retTSF, false, toTSFError(tsReadXML.error()), tsReadXML.errorString(), tsReadXML.lineNumber()}; } if(tt == QXmlStreamReader::StartDocument){ continue; }else if(tt == QXmlStreamReader::StartElement){ name = tsReadXML.name(); if(name == STR_TRAININGSET){ attributes = tsReadXML.attributes(); if(attributes.hasAttribute(STR_PATTERNSIZE) && attributes.hasAttribute(STR_INPUTSSIZE) && attributes.hasAttribute(STR_TARGETSSIZE)) { pSize = attributes.value(STR_PATTERNSIZE).toInt(); iSize = attributes.value(STR_INPUTSSIZE).toInt(); tSize = attributes.value(STR_TARGETSSIZE).toInt(); inputs = vector<vector<double> >(pSize, vector<double>(iSize, 0)); targets = vector<vector<double> >(pSize, vector<double>(tSize, 0)); }else{ file.close(); return { retTSF, false, NotWellFormedError, "NotWellFormedError: Missing attributes (" + STR_PATTERNSIZE + ", " + STR_INPUTSSIZE + ", " + STR_TARGETSSIZE + ") on tag " + STR_TRAININGSET, tsReadXML.lineNumber() }; } }else if(name == STR_PROPERTIES){ attributes = tsReadXML.attributes(); if(attributes.hasAttribute(STR_VERSION)){ version = attributes.value(STR_VERSION).toString(); }else{ file.close(); return { retTSF, false, NotWellFormedError, "NotWellFormedError: Missing attributes (" + STR_VERSION + ") on tag " + STR_PROPERTIES, tsReadXML.lineNumber() }; } }else if(name == STR_INPUTSDATAREPRESENTATION){ attributes = tsReadXML.attributes(); if(attributes.hasAttribute(STR_NAME) && attributes.hasAttribute(STR_WIDTH) && attributes.hasAttribute(STR_HEIGHT) && attributes.hasAttribute(STR_FORMAT)) { idr->setType(drFromStrToInt(attributes.value(STR_NAME).toString())); idr->setWidth(attributes.value(STR_WIDTH).toInt()); idr->setHeight(attributes.value(STR_HEIGHT).toInt()); idr->setImageFormat(fromStrToImgFormat(attributes.value(STR_FORMAT).toString())); }else{ file.close(); return { retTSF, false, NotWellFormedError, "NotWellFormedError: Missing attributes (" + STR_NAME + ", " + STR_WIDTH + ", " + STR_HEIGHT + ", " + STR_FORMAT + ") on tag " + STR_INPUTSDATAREPRESENTATION, tsReadXML.lineNumber() }; } }else if(name == STR_TARGETSDATAREPRESENTATION){ attributes = tsReadXML.attributes(); if(attributes.hasAttribute(STR_NAME) && attributes.hasAttribute(STR_WIDTH) && attributes.hasAttribute(STR_HEIGHT) && attributes.hasAttribute(STR_FORMAT)) { tdr->setType(drFromStrToInt(attributes.value(STR_NAME).toString())); tdr->setWidth(attributes.value(STR_WIDTH).toInt()); tdr->setHeight(attributes.value(STR_HEIGHT).toInt()); tdr->setImageFormat(fromStrToImgFormat(attributes.value(STR_FORMAT).toString())); }else{ file.close(); return { retTSF, false, NotWellFormedError, "NotWellFormedError: Missing attributes (" + STR_NAME + ", " + STR_WIDTH + ", " + STR_HEIGHT + ", " + STR_FORMAT + ") on tag " + STR_TARGETSDATAREPRESENTATION, tsReadXML.lineNumber() }; } }else if(name == STR_INPUTSNORMALIZATION){ attributes = tsReadXML.attributes(); if(attributes.hasAttribute(STR_TYPE) && attributes.hasAttribute(STR_MAXVALUE) && attributes.hasAttribute(STR_MINVALUE) && attributes.hasAttribute(STR_THRESHOLD) && attributes.hasAttribute(STR_AMPLITUDE) && attributes.hasAttribute(STR_ELONGATION)) { inor->setType(normFromStrToInt(attributes.value(STR_TYPE).toString())); inor->setMaxValue(attributes.value(STR_MAXVALUE).toDouble()); inor->setMinValue(attributes.value(STR_MINVALUE).toDouble()); inor->setThreshold(attributes.value(STR_THRESHOLD).toDouble()); inor->setAmplitude(attributes.value(STR_AMPLITUDE).toDouble()); inor->setElongation(attributes.value(STR_ELONGATION).toDouble()); }else{ file.close(); return { retTSF, false, NotWellFormedError, "NotWellFormedError: Missing attributes (" + STR_TYPE + ", " + STR_MAXVALUE + ", " + STR_MINVALUE + ", " + STR_THRESHOLD + ", " + STR_ELONGATION + ") on tag " + STR_INPUTSNORMALIZATION, tsReadXML.lineNumber() }; } }else if(name == STR_TARGETSNORMALIZATION){ attributes = tsReadXML.attributes(); if(attributes.hasAttribute(STR_TYPE) && attributes.hasAttribute(STR_MAXVALUE) && attributes.hasAttribute(STR_MINVALUE) && attributes.hasAttribute(STR_THRESHOLD) && attributes.hasAttribute(STR_AMPLITUDE) && attributes.hasAttribute(STR_ELONGATION)) { tnor->setType(normFromStrToInt(attributes.value(STR_TYPE).toString())); tnor->setMaxValue(attributes.value(STR_MAXVALUE).toDouble()); tnor->setMinValue(attributes.value(STR_MINVALUE).toDouble()); tnor->setThreshold(attributes.value(STR_THRESHOLD).toDouble()); tnor->setAmplitude(attributes.value(STR_AMPLITUDE).toDouble()); tnor->setElongation(attributes.value(STR_ELONGATION).toDouble()); }else{ file.close(); return { retTSF, false, NotWellFormedError, "NotWellFormedError: Missing attributes (" + STR_TYPE + ", " + STR_MAXVALUE + ", " + STR_MINVALUE + ", " + STR_THRESHOLD + ", " + STR_ELONGATION + ") on tag " + STR_TARGETSNORMALIZATION, tsReadXML.lineNumber() }; } }else if(name == STR_PATTERN){ attributes = tsReadXML.attributes(); if(attributes.hasAttribute(STR_INDEX)) { lastPatternIndex = attributes.value(STR_INDEX).toInt(); }else{ file.close(); return { retTSF, false, NotWellFormedError, "NotWellFormedError: Missing attributes (" + STR_INDEX + ") on tag " + STR_PATTERN, tsReadXML.lineNumber() }; } }else if(name == STR_INPUTS){ text = tsReadXML.readElementText(QXmlStreamReader::SkipChildElements); textElements = text.split(STR_SEPARATOR, QString::KeepEmptyParts, Qt::CaseInsensitive); sTextElements = textElements.size(); if(sTextElements == iSize){ for(int i = 0; i < sTextElements; i++){ inputs[lastPatternIndex][i] = textElements[i].toDouble(); } }else{ file.close(); return { retTSF, false, NotWellFormedError, "NotWellFormedError: Incongruence between reported input size with found inputs elements", tsReadXML.lineNumber() }; } }else if(name == STR_TARGETS){ text = tsReadXML.readElementText(QXmlStreamReader::SkipChildElements); textElements = text.split(STR_SEPARATOR, QString::KeepEmptyParts, Qt::CaseInsensitive); sTextElements = textElements.size(); if(sTextElements == tSize){ for(int t = 0; t < sTextElements; t++){ targets[lastPatternIndex][t] = textElements[t].toDouble(); } }else{ file.close(); return { retTSF, false, NotWellFormedError, "NotWellFormedError: Incongruence between reported target size with found target elements", tsReadXML.lineNumber() }; } } } } retTSF->setFileName(file.fileName()); res.file = retTSF; ts->setPatternCount(pSize); ts->setInputs(inputs, iSize); ts->setTargets(targets, tSize); ts->setInputsNormalization(inor); ts->setTargetsNormalization(tnor); ts->setInputsDataRepresentation(idr); ts->setTargetsDataRepresentation(tdr); res.sucess = true; res.errnum = toTSFError(QXmlStreamReader::NoError); res.errormsg = ""; res.line = -1; file.close(); return res; }else{ file.close(); return { retTSF, false, toTSFError(file.error()), file.errorString(), -1 }; } }