/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Copyright 2012 The MITRE Corporation * * * * Licensed under the Apache License, Version 2.0 (the "License"); * * you may not use this file except in compliance with the License. * * You may obtain a copy of the License at * * * * http://www.apache.org/licenses/LICENSE-2.0 * * * * Unless required by applicable law or agreed to in writing, software * * distributed under the License is distributed on an "AS IS" BASIS, * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * * See the License for the specific language governing permissions and * * limitations under the License. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ #include #include #include #include #include #include #include #include "core/bee.h" #include "core/common.h" #include "core/fuse.h" using namespace cv; static void normalizeMatrix(Mat &matrix, const Mat &mask, const QString &method) { if (method == "None") return; QList vals; vals.reserve(matrix.rows*matrix.cols); for (int i=0; i(i,j); if ((mask.at(i,j) == BEE::DontCare) || (val == -std::numeric_limits::infinity()) || (val == std::numeric_limits::infinity())) continue; vals.append(val); } } float min, max; double mean, stddev; Common::MinMax(vals, &min, &max); Common::MeanStdDev(vals, &mean, &stddev); if (method == "MinMax") { for (int i=0; i(i,j) == BEE::DontCare) continue; float &val = matrix.at(i,j); if (val == -std::numeric_limits::infinity()) val = 0; else if (val == std::numeric_limits::infinity()) val = 1; else val = (val - min) / (max - min); } } } else if (method == "ZScore") { if (stddev == 0) qFatal("fuse.cpp normalizeMatrix stddev is 0."); for (int i=0; i(i,j) == BEE::DontCare) continue; float &val = matrix.at(i,j); if (val == -std::numeric_limits::infinity()) val = (min - mean) / stddev; else if (val == std::numeric_limits::infinity()) val = (max - mean) / stddev; else val = (val - mean) / stddev; } } } else { qFatal("fuse.cpp normalizeMatrix invalid normalization method %s.", qPrintable(method)); } } void br::Fuse(const QStringList &inputSimmats, const QString &mask, const QString &normalization, const QString &fusion, const QString &outputSimmat) { qDebug("Fusing %d to %s", inputSimmats.size(), qPrintable(outputSimmat)); QList matrices; foreach (const QString &simmat, inputSimmats) matrices.append(BEE::readSimmat(simmat)); if ((matrices.size() < 2) && (fusion != "None")) qFatal("br::Fuse expected at least two similarity matrices."); if ((matrices.size() > 1) && (fusion == "None")) qFatal("mm:Fuse expected exactly one similarity matrix."); Mat matrix_mask = BEE::readMask(mask); for (int i=0; i weights; QStringList words = fusion.right(fusion.size()-3).split(":", QString::SkipEmptyParts); if (words.size() == 0) { for (int k=0; k