Split the lib in smaller libs to ease unit tests

This commit is contained in:
emeric
2020-02-13 18:04:35 +01:00
parent 1e2c1caeed
commit 15e53caa2d
131 changed files with 382 additions and 138 deletions
+25
View File
@@ -0,0 +1,25 @@
add_library(lmsrecommendation SHARED
impl/Engine.cpp
impl/ProviderCreator.cpp
impl/features/som/DataNormalizer.cpp
impl/features/som/Network.cpp
)
target_include_directories(lmsrecommendation INTERFACE
include
)
target_include_directories(lmsrecommendation PRIVATE
include
)
target_link_libraries(lmsrecommendation PRIVATE
lmsdatabase
)
target_link_libraries(lmsrecommendation PUBLIC
)
install(TARGETS lmsrecommendation DESTINATION lib)
+138
View File
@@ -0,0 +1,138 @@
/*
* Copyright (C) 2019 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Engine.hpp"
//#include "features/SimilarityFeaturesScannerAddon.hpp"
//#include "cluster/SimilarityClusterSearcher.hpp"
#include "database/ScanSettings.hpp"
#include "database/TrackList.hpp"
namespace Recommendation {
std::unique_ptr<IEngine>
createEngine()
{
return std::make_unique<Engine>();
}
void
Engine::clearProviders()
{
_providers.clear();
}
void
Engine::addProvider(std::unique_ptr<Provider> provider, unsigned priority)
{
_providers.emplace(priority, std::move(provider));
}
std::vector<Database::IdType>
Engine::getSimilarTracksFromTrackList(Database::Session& /*session*/, Database::IdType /*trackListId*/, std::size_t /*maxCount*/)
{
#if 0
auto engineType {getEngineType(session)};
auto somSearcher {_somAddon.getSearcher()};
std::set<Database::IdType> trackIds;
{
auto transaction {session.createSharedTransaction()};
Database::TrackList::pointer trackList {Database::TrackList::getById(session, trackListId)};
if (trackList)
{
const std::vector<Database::IdType> orderedTrackIds {trackList->getTrackIds()};
trackIds = std::set<Database::IdType> {std::cbegin(orderedTrackIds), std::cend(orderedTrackIds)};
}
}
if (trackIds.empty())
return {};
if (engineType == Database::ScanSettings::SimilarityEngineType::Features
&& somSearcher
&& std::any_of(std::cbegin(trackIds), std::cend(trackIds), [&](Database::IdType trackId) { return somSearcher->isTrackClassified(trackId); } ))
{
return somSearcher->getSimilarTracks(trackIds, maxCount);
}
else
return ClusterEngine::getSimilarTracksFromTrackList(session, trackListId, maxCount);
#endif
return {};
}
std::vector<Database::IdType>
Engine::getSimilarTracks(Database::Session& /*dbSession*/, const std::unordered_set<Database::IdType>& /*trackIds*/, std::size_t /*maxCount*/)
{
#if 0
auto engineType {getEngineType(dbSession)};
auto somSearcher {_somAddon.getSearcher()};
if (engineType == Database::ScanSettings::SimilarityEngineType::Features
&& somSearcher
&& std::any_of(std::cbegin(trackIds), std::cend(trackIds), [&](Database::IdType trackId) { return somSearcher->isTrackClassified(trackId); } ))
{
return somSearcher->getSimilarTracks(trackIds, maxCount);
}
else
return ClusterEngine::getSimilarTracks(dbSession, trackIds, maxCount);
#endif
return {};
}
std::vector<Database::IdType>
Engine::getSimilarReleases(Database::Session& /*dbSession*/, Database::IdType /*releaseId*/, std::size_t /*maxCount*/)
{
#if 0
auto engineType {getEngineType(dbSession)};
auto somSearcher {_somAddon.getSearcher()};
if (engineType == Database::ScanSettings::SimilarityEngineType::Features
&& somSearcher
&& somSearcher->isReleaseClassified(releaseId))
{
return somSearcher->getSimilarReleases(releaseId, maxCount);
}
else
return ClusterEngine::getSimilarReleases(dbSession, releaseId, maxCount);
#endif
return {};
}
std::vector<Database::IdType>
Engine::getSimilarArtists(Database::Session& /*dbSession*/, Database::IdType /*artistId*/, std::size_t /*maxCount*/)
{
#if 0
auto engineType {getEngineType(dbSession)};
auto somSearcher {_somAddon.getSearcher()};
if (engineType == Database::ScanSettings::SimilarityEngineType::Features
&& somSearcher
&& somSearcher->isArtistClassified(artistId))
{
return somSearcher->getSimilarArtists(artistId, maxCount);
}
else
return ClusterEngine::getSimilarArtists(dbSession, artistId, maxCount);
#endif
return {};
}
} // ns Similarity
+52
View File
@@ -0,0 +1,52 @@
/*
* Copyright (C) 2019 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <map>
#include "recommendation/IEngine.hpp"
#include "recommendation/Provider.hpp"
namespace Database
{
class Session;
}
namespace Recommendation
{
class Engine : public IEngine
{
public:
void clearProviders() override;
void addProvider(std::unique_ptr<Provider> provider, unsigned priority) override;
// Closest results first
std::vector<Database::IdType> getSimilarTracksFromTrackList(Database::Session& session, Database::IdType tracklistId, std::size_t maxCount) override;
std::vector<Database::IdType> getSimilarTracks(Database::Session& session, const std::unordered_set<Database::IdType>& tracksId, std::size_t maxCount) override;
std::vector<Database::IdType> getSimilarReleases(Database::Session& session, Database::IdType releaseId, std::size_t maxCount) override;
std::vector<Database::IdType> getSimilarArtists(Database::Session& session, Database::IdType artistId, std::size_t maxCount) override;
private:
std::map<unsigned, std::unique_ptr<Provider>> _providers;
};
} // ns Recommendation
@@ -0,0 +1,38 @@
/*
* Copyright (C) 2020 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "recommendation/ClustersRecommendationProviderCreator.hpp"
#include "recommendation/FeaturesRecommendationProviderCreator.hpp"
#include "recommendation/Provider.hpp"
namespace Recommendation
{
std::unique_ptr<Provider> createClustersRecommendationProvider()
{
return {};
}
std::unique_ptr<Provider> createFeaturesRecommendationProvider(Scanner::IMediaScanner&)
{
return {};
}
}
@@ -0,0 +1,101 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "SimilarityClusterSearcher.hpp"
#include "database/Artist.hpp"
#include "database/Cluster.hpp"
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "database/TrackList.hpp"
namespace Similarity {
namespace ClusterSearcher {
std::vector<Database::IdType>
getSimilarTracks(Database::Session& dbSession, const std::set<Database::IdType>& trackIds, std::size_t maxCount)
{
auto transaction {dbSession.createSharedTransaction()};
auto tracks {Database::Track::getSimilarTracks(dbSession, trackIds, 0, maxCount)};
std::vector<Database::IdType> res;
res.reserve(tracks.size());
std::transform(std::cbegin(tracks), std::cend(tracks), std::back_inserter(res), [](const auto& track) { return track.id(); });
return res;
}
std::vector<Database::IdType>
getSimilarTracksFromTrackList(Database::Session& session, Database::IdType tracklistId, std::size_t maxCount)
{
std::vector<Database::IdType> res;
auto transaction {session.createSharedTransaction()};
const Database::TrackList::pointer trackList {Database::TrackList::getById(session, tracklistId)};
if (!trackList)
return res;
const std::vector<Database::Track::pointer> tracks {trackList->getSimilarTracks(0, maxCount)};
res.reserve(tracks.size());
std::transform(std::cbegin(tracks), std::cend(tracks), std::back_inserter(res),
[](const Database::Track::pointer& track) { return track.id(); });
return res;
}
std::vector<Database::IdType>
getSimilarReleases(Database::Session& dbSession, Database::IdType releaseId, std::size_t maxCount)
{
std::vector<Database::IdType> res;
auto transaction {dbSession.createSharedTransaction()};
auto release {Database::Release::getById(dbSession, releaseId)};
if (!release)
return res;
const auto releases {release->getSimilarReleases(0, maxCount)};
res.reserve(releases.size());
std::transform(std::cbegin(releases), std::cend(releases), std::back_inserter(res), [](const auto& release) { return release.id(); });
return res;
}
std::vector<Database::IdType>
getSimilarArtists(Database::Session& dbSession, Database::IdType artistId, std::size_t maxCount)
{
std::vector<Database::IdType> res;
auto transaction {dbSession.createSharedTransaction()};
auto artist {Database::Artist::getById(dbSession, artistId)};
if (!artist)
return res;
const auto artists {artist->getSimilarArtists(0, maxCount)};
res.reserve(artists.size());
std::transform(std::cbegin(artists), std::cend(artists), std::back_inserter(res), [](const auto& artist) { return artist.id(); });
return res;
}
} // namespace ClusterSearcher
} // namespace Similarity
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <set>
#include "database/Types.hpp"
namespace Database {
class Session;
}
namespace Similarity {
namespace ClusterSearcher
{
std::vector<Database::IdType> getSimilarTracksFromTrackList(Database::Session& session, Database::IdType tracklistId, std::size_t maxCount);
std::vector<Database::IdType> getSimilarTracks(Database::Session& session, const std::set<Database::IdType>& tracksId, std::size_t maxCount);
std::vector<Database::IdType> getSimilarReleases(Database::Session& session, Database::IdType releaseId, std::size_t maxCount);
std::vector<Database::IdType> getSimilarArtists(Database::Session& session, Database::IdType artistId, std::size_t maxCount);
};
} // namespace Similarity
@@ -0,0 +1,86 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "AcousticBrainzUtils.hpp"
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/json_parser.hpp>
#include <Wt/WIOService.h>
#include <Wt/Http/Client.h>
#include "utils/IConfig.hpp"
#include "utils/Logger.hpp"
#include "utils/Service.hpp"
namespace AcousticBrainz
{
static
std::string
getJsonData(const UUID& mbid)
{
static const std::string defaultAPIURL = "https://acousticbrainz.org/api/v1/";
const std::string url {ServiceProvider<IConfig>::get()->getString("acousticbrainz-api-url", defaultAPIURL) + std::string {mbid.getAsString()} + "/low-level"};
boost::asio::io_service ioService;
Wt::Http::Client client {ioService};
client.setFollowRedirect(true);
client.setSslCertificateVerificationEnabled(true);
client.setMaximumResponseSize(256*1024);
if (!client.get(url))
{
LMS_LOG(SIMILARITY, ERROR) << "Cannot perform a GET request to url '" << url << "'";
return {};
}
std::string response;
client.done().connect([&](Wt::AsioWrapper::error_code ec, const Wt::Http::Message &msg)
{
if (ec)
{
LMS_LOG(SIMILARITY, ERROR) << "GET request to url '" << url << "' failed: " << ec.message();
return;
}
if (msg.status() != 200)
{
LMS_LOG(SIMILARITY, ERROR) << "GET request to url '" << url << "' failed: status = " << msg.status() << ", body = " << msg.body();
return;
}
response = msg.body();
});
ioService.run();
return response;
}
std::string
extractLowLevelFeatures(const UUID& mbid)
{
return getJsonData(mbid);
}
} // namespace Scanner::AcousticBrainz
@@ -0,0 +1,30 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <string>
#include "utils/UUID.hpp"
namespace AcousticBrainz
{
std::string extractLowLevelFeatures(const UUID& MBID);
}
@@ -0,0 +1,261 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "SimilarityFeaturesCache.hpp"
#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>
#include "utils/IConfig.hpp"
#include "utils/Logger.hpp"
#include "utils/Service.hpp"
namespace Similarity {
static
std::filesystem::path getCacheDirectory()
{
return ServiceProvider<IConfig>::get()->getPath("working-dir") / "cache" / "features";
}
static std::filesystem::path getCacheNetworkFilePath()
{
return getCacheDirectory() / "network";
};
static std::filesystem::path getCacheTrackPositionsFilePath()
{
return getCacheDirectory() / "track_positions";
}
static
bool
networkToCacheFile(const SOM::Network& network, std::filesystem::path path)
{
try
{
boost::property_tree::ptree root;
root.put("width", network.getWidth());
root.put("height", network.getHeight());
root.put("dim_count", network.getInputDimCount());
for (SOM::InputVector::value_type weight : network.getDataWeights())
root.add("weights.weight", weight);
for (SOM::Coordinate x = 0; x < network.getWidth(); ++x)
{
for (SOM::Coordinate y = 0; y < network.getWidth(); ++y)
{
const auto& refVector = network.getRefVector({x, y});
boost::property_tree::ptree node;
for (auto value : refVector)
node.add("values.value", value);
node.put("coord_x", x);
node.put("coord_y", y);
root.add_child("ref_vectors.ref_vector", node);
}
}
boost::property_tree::write_xml(path.string(), root);
LMS_LOG(SIMILARITY, DEBUG) << "Created network cache";
return true;
}
catch (boost::property_tree::ptree_error& error)
{
LMS_LOG(SIMILARITY, ERROR) << "Cannot create network cache: " << error.what();
return false;
}
}
static
std::optional<SOM::Network>
createNetworkFromCacheFile(const std::filesystem::path& path)
{
if (!std::filesystem::exists(path))
return std::nullopt;
try
{
LMS_LOG(SIMILARITY, INFO) << "Reading network from cache...";
boost::property_tree::ptree root;
boost::property_tree::read_xml(path.string(), root);
SOM::Coordinate width {root.get<SOM::Coordinate>("width")};
SOM::Coordinate height {root.get<SOM::Coordinate>("height")};
std::size_t dimCount {root.get<std::size_t>("dim_count")};
SOM::Network res {width, height, dimCount};
{
SOM::InputVector weights {dimCount};
std::size_t i {};
for (const auto& val : root.get_child("weights"))
weights[i++] = val.second.get_value<double>();
res.setDataWeights(weights);
}
for (const auto& node : root.get_child("ref_vectors"))
{
SOM::Coordinate x {node.second.get<SOM::Coordinate>("coord_x")};
SOM::Coordinate y {node.second.get<SOM::Coordinate>("coord_y")};
SOM::InputVector refVector {dimCount};
std::size_t i {};
for (const auto& val : node.second.get_child("values"))
refVector[i++] = val.second.get_value<SOM::InputVector::value_type>();
res.setRefVector({x, y}, refVector);
}
LMS_LOG(SIMILARITY, INFO) << "Successfully read network from cache";
return res;
}
catch (boost::property_tree::ptree_error& error)
{
LMS_LOG(SIMILARITY, ERROR) << "Cannot read network cache: " << error.what();
return std::nullopt;
}
}
static
bool
objectPositionToCacheFile(const std::map<Database::IdType, std::set<SOM::Position>>& objectsPosition, std::filesystem::path path)
{
try
{
boost::property_tree::ptree root;
for (const auto& objectPosition : objectsPosition)
{
boost::property_tree::ptree node;
node.put("id", objectPosition.first);
for (const auto& position : objectPosition.second)
{
boost::property_tree::ptree positionNode;
positionNode.put("x", position.x);
positionNode.put("y", position.y);
node.add_child("position.position", positionNode);
}
root.add_child("objects.object", node);
}
boost::property_tree::write_xml(path.string(), root);
return true;
}
catch (boost::property_tree::ptree_error& error)
{
LMS_LOG(SIMILARITY, ERROR) << "Cannot cache object position: " << error.what();
return false;
}
}
static
std::optional<std::map<Database::IdType, std::set<SOM::Position>>>
createObjectPositionsFromCacheFile(std::filesystem::path path)
{
try
{
LMS_LOG(SIMILARITY, INFO) << "Reading object position from cache...";
boost::property_tree::ptree root;
boost::property_tree::read_xml(path.string(), root);
std::map<Database::IdType, std::set<SOM::Position>> res;
for (const auto& object : root.get_child("objects"))
{
auto id = object.second.get<Database::IdType>("id");
for (const auto& position : object.second.get_child("position"))
{
auto x = position.second.get<SOM::Coordinate>("x");
auto y = position.second.get<SOM::Coordinate>("y");
res[id].insert({x, y});
}
}
LMS_LOG(SIMILARITY, INFO) << "Successfully read object position from cache";
return res;
}
catch (boost::property_tree::ptree_error& error)
{
LMS_LOG(SIMILARITY, ERROR) << "Cannot create object position from cache file: " << error.what();
return std::nullopt;
}
}
void
FeaturesCache::invalidate()
{
std::filesystem::remove(getCacheNetworkFilePath());
std::filesystem::remove(getCacheTrackPositionsFilePath());
}
std::optional<FeaturesCache>
FeaturesCache::read()
{
std::optional<FeaturesCache> res;
auto network{createNetworkFromCacheFile(getCacheNetworkFilePath())};
if (!network)
return res;
auto trackPositions{createObjectPositionsFromCacheFile(getCacheTrackPositionsFilePath())};
if (!trackPositions)
return res;
return FeaturesCache{std::move(*network), std::move(*trackPositions)};
}
void
FeaturesCache::write()
{
std::filesystem::create_directories(ServiceProvider<IConfig>::get()->getPath("working-dir") / "cache" / "features");
if (!networkToCacheFile(_network, getCacheNetworkFilePath())
|| !objectPositionToCacheFile(_trackPositions, getCacheTrackPositionsFilePath()))
{
invalidate();
}
}
FeaturesCache::FeaturesCache(SOM::Network network, ObjectPositions trackPositions)
: _network {std::move(network)},
_trackPositions {std::move(trackPositions)}
{
}
} // namespace Similarity
@@ -0,0 +1,51 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <map>
#include <optional>
#include <set>
#include "database/Types.hpp"
#include "som/Network.hpp"
namespace Similarity {
class FeaturesCache
{
public:
static void invalidate();
static std::optional<FeaturesCache> read();
void write();
private:
using ObjectPositions = std::map<Database::IdType, std::set<SOM::Position>>;
FeaturesCache(SOM::Network network, ObjectPositions trackPositions);
friend class FeaturesSearcher;
SOM::Network _network;
ObjectPositions _trackPositions;
};
} // namespace Similarity
@@ -0,0 +1,402 @@
/*
* Copyright (C) 2019 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "SimilarityFeaturesDefs.hpp"
#include <algorithm>
#include <iterator>
#include "utils/Exception.hpp"
namespace Similarity {
static const std::unordered_map<FeatureName, FeatureDef> featureDefinitions
{
{ "lowlevel.average_loudness", {1}},
{ "lowlevel.barkbands.dmean", {27}},
{ "lowlevel.barkbands.dmean2", {27}},
{ "lowlevel.barkbands.dvar", {27}},
{ "lowlevel.barkbands.dvar2", {27}},
{ "lowlevel.barkbands.max", {27}},
{ "lowlevel.barkbands.mean", {27}},
{ "lowlevel.barkbands.median", {27}},
{ "lowlevel.barkbands.min", {27}},
{ "lowlevel.barkbands.var", {27}},
{ "lowlevel.barkbands_crest.dmean", {1}},
{ "lowlevel.barkbands_crest.dmean2", {1}},
{ "lowlevel.barkbands_crest.dvar", {1}},
{ "lowlevel.barkbands_crest.dvar2", {1}},
{ "lowlevel.barkbands_crest.max", {1}},
{ "lowlevel.barkbands_crest.mean", {1}},
{ "lowlevel.barkbands_crest.median", {1}},
{ "lowlevel.barkbands_crest.min", {1}},
{ "lowlevel.barkbands_crest.var", {1}},
{ "lowlevel.barkbands_flatness_db.dmean", {1}},
{ "lowlevel.barkbands_flatness_db.dmean2", {1}},
{ "lowlevel.barkbands_flatness_db.dvar", {1}},
{ "lowlevel.barkbands_flatness_db.dvar2", {1}},
{ "lowlevel.barkbands_flatness_db.max", {1}},
{ "lowlevel.barkbands_flatness_db.mean", {1}},
{ "lowlevel.barkbands_flatness_db.median", {1}},
{ "lowlevel.barkbands_flatness_db.min", {1}},
{ "lowlevel.barkbands_flatness_db.var", {1}},
{ "lowlevel.barkbands_kurtosis.dmean", {1}},
{ "lowlevel.barkbands_kurtosis.dmean2", {1}},
{ "lowlevel.barkbands_kurtosis.dvar", {1}},
{ "lowlevel.barkbands_kurtosis.dvar2", {1}},
{ "lowlevel.barkbands_kurtosis.max", {1}},
{ "lowlevel.barkbands_kurtosis.mean", {1}},
{ "lowlevel.barkbands_kurtosis.median", {1}},
{ "lowlevel.barkbands_kurtosis.min", {1}},
{ "lowlevel.barkbands_kurtosis.var", {1}},
{ "lowlevel.barkbands_skewness.dmean", {1}},
{ "lowlevel.barkbands_skewness.dmean2", {1}},
{ "lowlevel.barkbands_skewness.dvar", {1}},
{ "lowlevel.barkbands_skewness.dvar2", {1}},
{ "lowlevel.barkbands_skewness.max", {1}},
{ "lowlevel.barkbands_skewness.mean", {1}},
{ "lowlevel.barkbands_skewness.median", {1}},
{ "lowlevel.barkbands_skewness.min", {1}},
{ "lowlevel.barkbands_skewness.var", {1}},
{ "lowlevel.barkbands_spread.dmean", {1}},
{ "lowlevel.barkbands_spread.dmean2", {1}},
{ "lowlevel.barkbands_spread.dvar", {1}},
{ "lowlevel.barkbands_spread.dvar2", {1}},
{ "lowlevel.barkbands_spread.max", {1}},
{ "lowlevel.barkbands_spread.mean", {1}},
{ "lowlevel.barkbands_spread.median", {1}},
{ "lowlevel.barkbands_spread.min", {1}},
{ "lowlevel.barkbands_spread.var", {1}},
{ "lowlevel.dissonance.dmean", {1}},
{ "lowlevel.dissonance.dmean2", {1}},
{ "lowlevel.dissonance.dvar", {1}},
{ "lowlevel.dissonance.dvar2", {1}},
{ "lowlevel.dissonance.max", {1}},
{ "lowlevel.dissonance.mean", {1}},
{ "lowlevel.dissonance.median", {1}},
{ "lowlevel.dissonance.min", {1}},
{ "lowlevel.dissonance.var", {1}},
{ "lowlevel.dynamic_complexity", {1}},
{ "lowlevel.spectral_contrast_coeffs.dmean", {6}},
{ "lowlevel.spectral_contrast_coeffs.dmean2", {6}},
{ "lowlevel.spectral_contrast_coeffs.dvar", {6}},
{ "lowlevel.spectral_contrast_coeffs.dvar2", {6}},
{ "lowlevel.spectral_contrast_coeffs.max", {6}},
{ "lowlevel.spectral_contrast_coeffs.mean", {6}},
{ "lowlevel.spectral_contrast_coeffs.median", {6}},
{ "lowlevel.spectral_contrast_coeffs.min", {6}},
{ "lowlevel.spectral_contrast_coeffs.var", {6}},
{ "lowlevel.erbbands.dmean", {40}},
{ "lowlevel.erbbands.dmean2", {40}},
{ "lowlevel.erbbands.dvar", {40}},
{ "lowlevel.erbbands.dvar2", {40}},
{ "lowlevel.erbbands.max", {40}},
{ "lowlevel.erbbands.mean", {40}},
{ "lowlevel.erbbands.median", {40}},
{ "lowlevel.erbbands.min", {40}},
{ "lowlevel.erbbands.var", {40}},
{ "lowlevel.gfcc.mean", {13}},
{ "lowlevel.hfc.dmean", {1}},
{ "lowlevel.hfc.dmean2", {1}},
{ "lowlevel.hfc.dvar", {1}},
{ "lowlevel.hfc.dvar2", {1}},
{ "lowlevel.hfc.max", {1}},
{ "lowlevel.hfc.mean", {1}},
{ "lowlevel.hfc.median", {1}},
{ "lowlevel.hfc.min", {1}},
{ "lowlevel.hfc.var", {1}},
{ "tonal.hpcp.median", {36}},
{ "lowlevel.melbands.dmean", {40}},
{ "lowlevel.melbands.dmean2", {40}},
{ "lowlevel.melbands.dvar", {40}},
{ "lowlevel.melbands.dvar2", {40}},
{ "lowlevel.melbands.max", {40}},
{ "lowlevel.melbands.mean", {40}},
{ "lowlevel.melbands.median", {40}},
{ "lowlevel.melbands.min", {40}},
{ "lowlevel.melbands.var", {40}},
{ "lowlevel.melbands_crest.dmean", {1}},
{ "lowlevel.melbands_crest.dmean2", {1}},
{ "lowlevel.melbands_crest.dvar", {1}},
{ "lowlevel.melbands_crest.dvar2", {1}},
{ "lowlevel.melbands_crest.max", {1}},
{ "lowlevel.melbands_crest.mean", {1}},
{ "lowlevel.melbands_crest.median", {1}},
{ "lowlevel.melbands_crest.min", {1}},
{ "lowlevel.melbands_crest.var", {1}},
{ "lowlevel.melbands_flatness_db.dmean", {1}},
{ "lowlevel.melbands_flatness_db.dmean2", {1}},
{ "lowlevel.melbands_flatness_db.dvar", {1}},
{ "lowlevel.melbands_flatness_db.dvar2", {1}},
{ "lowlevel.melbands_flatness_db.max", {1}},
{ "lowlevel.melbands_flatness_db.mean", {1}},
{ "lowlevel.melbands_flatness_db.median", {1}},
{ "lowlevel.melbands_flatness_db.min", {1}},
{ "lowlevel.melbands_flatness_db.var", {1}},
{ "lowlevel.melbands_kurtosis.dmean", {1}},
{ "lowlevel.melbands_kurtosis.dmean2", {1}},
{ "lowlevel.melbands_kurtosis.dvar", {1}},
{ "lowlevel.melbands_kurtosis.dvar2", {1}},
{ "lowlevel.melbands_kurtosis.max", {1}},
{ "lowlevel.melbands_kurtosis.mean", {1}},
{ "lowlevel.melbands_kurtosis.median", {1}},
{ "lowlevel.melbands_kurtosis.min", {1}},
{ "lowlevel.melbands_kurtosis.var", {1}},
{ "lowlevel.melbands_skewness.dmean", {1}},
{ "lowlevel.melbands_skewness.dmean2", {1}},
{ "lowlevel.melbands_skewness.dvar", {1}},
{ "lowlevel.melbands_skewness.dvar2", {1}},
{ "lowlevel.melbands_skewness.max", {1}},
{ "lowlevel.melbands_skewness.mean", {1}},
{ "lowlevel.melbands_skewness.median", {1}},
{ "lowlevel.melbands_skewness.min", {1}},
{ "lowlevel.melbands_skewness.var", {1}},
{ "lowlevel.melbands_spread.dmean", {1}},
{ "lowlevel.melbands_spread.dmean2", {1}},
{ "lowlevel.melbands_spread.dvar", {1}},
{ "lowlevel.melbands_spread.dvar2", {1}},
{ "lowlevel.melbands_spread.max", {1}},
{ "lowlevel.melbands_spread.mean", {1}},
{ "lowlevel.melbands_spread.median", {1}},
{ "lowlevel.melbands_spread.min", {1}},
{ "lowlevel.melbands_spread.var", {1}},
{ "lowlevel.mfcc.mean", {13}},
{ "lowlevel.pitch_salience.dmean", {1}},
{ "lowlevel.pitch_salience.dmean2", {1}},
{ "lowlevel.pitch_salience.dvar", {1}},
{ "lowlevel.pitch_salience.dvar2", {1}},
{ "lowlevel.pitch_salience.max", {1}},
{ "lowlevel.pitch_salience.mean", {1}},
{ "lowlevel.pitch_salience.median", {1}},
{ "lowlevel.pitch_salience.min", {1}},
{ "lowlevel.pitch_salience.var", {1}},
{ "lowlevel.silence_rate_30dB.dmean", {1}},
{ "lowlevel.silence_rate_30dB.dmean2", {1}},
{ "lowlevel.silence_rate_30dB.dvar", {1}},
{ "lowlevel.silence_rate_30dB.dvar2", {1}},
{ "lowlevel.silence_rate_30dB.max", {1}},
{ "lowlevel.silence_rate_30dB.mean", {1}},
{ "lowlevel.silence_rate_30dB.median", {1}},
{ "lowlevel.silence_rate_30dB.min", {1}},
{ "lowlevel.silence_rate_30dB.var", {1}},
{ "lowlevel.silence_rate_60dB.dmean", {1}},
{ "lowlevel.silence_rate_60dB.dmean2", {1}},
{ "lowlevel.silence_rate_60dB.dvar", {1}},
{ "lowlevel.silence_rate_60dB.dvar2", {1}},
{ "lowlevel.silence_rate_60dB.max", {1}},
{ "lowlevel.silence_rate_60dB.mean", {1}},
{ "lowlevel.silence_rate_60dB.median", {1}},
{ "lowlevel.silence_rate_60dB.min", {1}},
{ "lowlevel.silence_rate_60dB.var", {1}},
{ "lowlevel.spectral_centroid.dmean", {1}},
{ "lowlevel.spectral_centroid.dmean2", {1}},
{ "lowlevel.spectral_centroid.dvar", {1}},
{ "lowlevel.spectral_centroid.dvar2", {1}},
{ "lowlevel.spectral_centroid.max", {1}},
{ "lowlevel.spectral_centroid.mean", {1}},
{ "lowlevel.spectral_centroid.median", {1}},
{ "lowlevel.spectral_centroid.min", {1}},
{ "lowlevel.spectral_centroid.var", {1}},
{ "lowlevel.spectral_complexity.dmean", {1}},
{ "lowlevel.spectral_complexity.dmean2", {1}},
{ "lowlevel.spectral_complexity.dvar", {1}},
{ "lowlevel.spectral_complexity.dvar2", {1}},
{ "lowlevel.spectral_complexity.max", {1}},
{ "lowlevel.spectral_complexity.mean", {1}},
{ "lowlevel.spectral_complexity.median", {1}},
{ "lowlevel.spectral_complexity.min", {1}},
{ "lowlevel.spectral_complexity.var", {1}},
{ "lowlevel.spectral_contrast_coeffs.dmean", {6}},
{ "lowlevel.spectral_contrast_coeffs.dmean2", {6}},
{ "lowlevel.spectral_contrast_coeffs.dvar", {6}},
{ "lowlevel.spectral_contrast_coeffs.dvar2", {6}},
{ "lowlevel.spectral_contrast_coeffs.max", {6}},
{ "lowlevel.spectral_contrast_coeffs.mean", {6}},
{ "lowlevel.spectral_contrast_coeffs.median", {6}},
{ "lowlevel.spectral_contrast_coeffs.min", {6}},
{ "lowlevel.spectral_contrast_coeffs.var", {6}},
{ "lowlevel.spectral_contrast_valleys.dmean", {6}},
{ "lowlevel.spectral_contrast_valleys.dmean2", {6}},
{ "lowlevel.spectral_contrast_valleys.dvar", {6}},
{ "lowlevel.spectral_contrast_valleys.dvar2", {6}},
{ "lowlevel.spectral_contrast_valleys.max", {6}},
{ "lowlevel.spectral_contrast_valleys.mean", {6}},
{ "lowlevel.spectral_contrast_valleys.median", {6}},
{ "lowlevel.spectral_contrast_valleys.min", {6}},
{ "lowlevel.spectral_contrast_valleys.var", {6}},
{ "lowlevel.spectral_decrease.dmean", {1}},
{ "lowlevel.spectral_decrease.dmean2", {1}},
{ "lowlevel.spectral_decrease.dvar", {1}},
{ "lowlevel.spectral_decrease.dvar2", {1}},
{ "lowlevel.spectral_decrease.max", {1}},
{ "lowlevel.spectral_decrease.mean", {1}},
{ "lowlevel.spectral_decrease.median", {1}},
{ "lowlevel.spectral_decrease.min", {1}},
{ "lowlevel.spectral_decrease.var", {1}},
{ "lowlevel.spectral_energy.dmean", {1}},
{ "lowlevel.spectral_energy.dmean2", {1}},
{ "lowlevel.spectral_energy.dvar", {1}},
{ "lowlevel.spectral_energy.dvar2", {1}},
{ "lowlevel.spectral_energy.max", {1}},
{ "lowlevel.spectral_energy.mean", {1}},
{ "lowlevel.spectral_energy.median", {1}},
{ "lowlevel.spectral_energy.min", {1}},
{ "lowlevel.spectral_energy.var", {1}},
{ "lowlevel.spectral_energyband_high.dmean", {1}},
{ "lowlevel.spectral_energyband_high.dmean2", {1}},
{ "lowlevel.spectral_energyband_high.dvar", {1}},
{ "lowlevel.spectral_energyband_high.dvar2", {1}},
{ "lowlevel.spectral_energyband_high.max", {1}},
{ "lowlevel.spectral_energyband_high.mean", {1}},
{ "lowlevel.spectral_energyband_high.median", {1}},
{ "lowlevel.spectral_energyband_high.min", {1}},
{ "lowlevel.spectral_energyband_high.var", {1}},
{ "lowlevel.spectral_energyband_low.dmean", {1}},
{ "lowlevel.spectral_energyband_low.dmean2", {1}},
{ "lowlevel.spectral_energyband_low.dvar", {1}},
{ "lowlevel.spectral_energyband_low.dvar2", {1}},
{ "lowlevel.spectral_energyband_low.max", {1}},
{ "lowlevel.spectral_energyband_low.mean", {1}},
{ "lowlevel.spectral_energyband_low.median", {1}},
{ "lowlevel.spectral_energyband_low.min", {1}},
{ "lowlevel.spectral_energyband_low.var", {1}},
{ "lowlevel.spectral_energyband_middle_high.dmean", {1}},
{ "lowlevel.spectral_energyband_middle_high.dmean2", {1}},
{ "lowlevel.spectral_energyband_middle_high.dvar", {1}},
{ "lowlevel.spectral_energyband_middle_high.dvar2", {1}},
{ "lowlevel.spectral_energyband_middle_high.max", {1}},
{ "lowlevel.spectral_energyband_middle_high.mean", {1}},
{ "lowlevel.spectral_energyband_middle_high.median", {1}},
{ "lowlevel.spectral_energyband_middle_high.min", {1}},
{ "lowlevel.spectral_energyband_middle_high.var", {1}},
{ "lowlevel.spectral_energyband_middle_low.dmean", {1}},
{ "lowlevel.spectral_energyband_middle_low.dmean2", {1}},
{ "lowlevel.spectral_energyband_middle_low.dvar", {1}},
{ "lowlevel.spectral_energyband_middle_low.dvar2", {1}},
{ "lowlevel.spectral_energyband_middle_low.max", {1}},
{ "lowlevel.spectral_energyband_middle_low.mean", {1}},
{ "lowlevel.spectral_energyband_middle_low.median", {1}},
{ "lowlevel.spectral_energyband_middle_low.min", {1}},
{ "lowlevel.spectral_energyband_middle_low.var", {1}},
{ "lowlevel.spectral_entropy.dmean", {1}},
{ "lowlevel.spectral_entropy.dmean2", {1}},
{ "lowlevel.spectral_entropy.dvar", {1}},
{ "lowlevel.spectral_entropy.dvar2", {1}},
{ "lowlevel.spectral_entropy.max", {1}},
{ "lowlevel.spectral_entropy.mean", {1}},
{ "lowlevel.spectral_entropy.median", {1}},
{ "lowlevel.spectral_entropy.min", {1}},
{ "lowlevel.spectral_entropy.var", {1}},
{ "lowlevel.spectral_flux.dmean", {1}},
{ "lowlevel.spectral_flux.dmean2", {1}},
{ "lowlevel.spectral_flux.dvar", {1}},
{ "lowlevel.spectral_flux.dvar2", {1}},
{ "lowlevel.spectral_flux.max", {1}},
{ "lowlevel.spectral_flux.mean", {1}},
{ "lowlevel.spectral_flux.median", {1}},
{ "lowlevel.spectral_flux.min", {1}},
{ "lowlevel.spectral_flux.var", {1}},
{ "lowlevel.spectral_kurtosis.dmean", {1}},
{ "lowlevel.spectral_kurtosis.dmean2", {1}},
{ "lowlevel.spectral_kurtosis.dvar", {1}},
{ "lowlevel.spectral_kurtosis.dvar2", {1}},
{ "lowlevel.spectral_kurtosis.max", {1}},
{ "lowlevel.spectral_kurtosis.mean", {1}},
{ "lowlevel.spectral_kurtosis.median", {1}},
{ "lowlevel.spectral_kurtosis.min", {1}},
{ "lowlevel.spectral_kurtosis.var", {1}},
{ "lowlevel.spectral_rms.dmean", {1}},
{ "lowlevel.spectral_rms.dmean2", {1}},
{ "lowlevel.spectral_rms.dvar", {1}},
{ "lowlevel.spectral_rms.dvar2", {1}},
{ "lowlevel.spectral_rms.max", {1}},
{ "lowlevel.spectral_rms.mean", {1}},
{ "lowlevel.spectral_rms.median", {1}},
{ "lowlevel.spectral_rms.min", {1}},
{ "lowlevel.spectral_rms.var", {1}},
{ "lowlevel.spectral_rolloff.dmean", {1}},
{ "lowlevel.spectral_rolloff.dmean2", {1}},
{ "lowlevel.spectral_rolloff.dvar", {1}},
{ "lowlevel.spectral_rolloff.dvar2", {1}},
{ "lowlevel.spectral_rolloff.max", {1}},
{ "lowlevel.spectral_rolloff.mean", {1}},
{ "lowlevel.spectral_rolloff.median", {1}},
{ "lowlevel.spectral_rolloff.min", {1}},
{ "lowlevel.spectral_rolloff.var", {1}},
{ "lowlevel.spectral_skewness.dmean", {1}},
{ "lowlevel.spectral_skewness.dmean2", {1}},
{ "lowlevel.spectral_skewness.dvar", {1}},
{ "lowlevel.spectral_skewness.dvar2", {1}},
{ "lowlevel.spectral_skewness.max", {1}},
{ "lowlevel.spectral_skewness.mean", {1}},
{ "lowlevel.spectral_skewness.median", {1}},
{ "lowlevel.spectral_skewness.min", {1}},
{ "lowlevel.spectral_skewness.var", {1}},
{ "lowlevel.spectral_spread.dmean", {1}},
{ "lowlevel.spectral_spread.dmean2", {1}},
{ "lowlevel.spectral_spread.dvar", {1}},
{ "lowlevel.spectral_spread.dvar2", {1}},
{ "lowlevel.spectral_spread.max", {1}},
{ "lowlevel.spectral_spread.mean", {1}},
{ "lowlevel.spectral_spread.median", {1}},
{ "lowlevel.spectral_spread.min", {1}},
{ "lowlevel.spectral_spread.var", {1}},
{ "lowlevel.spectral_strongpeak.dmean", {1}},
{ "lowlevel.spectral_strongpeak.dmean2", {1}},
{ "lowlevel.spectral_strongpeak.dvar", {1}},
{ "lowlevel.spectral_strongpeak.dvar2", {1}},
{ "lowlevel.spectral_strongpeak.max", {1}},
{ "lowlevel.spectral_strongpeak.mean", {1}},
{ "lowlevel.spectral_strongpeak.median", {1}},
{ "lowlevel.spectral_strongpeak.min", {1}},
{ "lowlevel.spectral_strongpeak.var", {1}},
{ "lowlevel.zerocrossingrate.dmean", {1}},
{ "lowlevel.zerocrossingrate.dmean2", {1}},
{ "lowlevel.zerocrossingrate.dvar", {1}},
{ "lowlevel.zerocrossingrate.dvar2", {1}},
{ "lowlevel.zerocrossingrate.max", {1}},
{ "lowlevel.zerocrossingrate.mean", {1}},
{ "lowlevel.zerocrossingrate.median", {1}},
{ "lowlevel.zerocrossingrate.min", {1}},
{ "lowlevel.zerocrossingrate.var", {1}},
};
FeatureDef
getFeatureDef(const FeatureName& featureName)
{
auto it {featureDefinitions.find(featureName)};
if (it == std::cend(featureDefinitions))
throw LmsException {"Unhandled requested feature '" + featureName + "'"};
return it->second;
}
FeatureNames
getFeatureNames()
{
FeatureNames res;
std::transform(std::cbegin(featureDefinitions), std::cend(featureDefinitions),
std::inserter(res, std::begin(res)), [](auto itFeature) { return itFeature.first; });
return res;
}
} // namespace Similarity
@@ -0,0 +1,49 @@
/*
* Copyright (C) 2019 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
namespace Similarity {
using FeatureName = std::string;
using FeatureNames = std::unordered_set<FeatureName>;
using FeatureValue = double;
using FeatureValues = std::vector<FeatureValue>;
using FeatureValuesMap = std::unordered_map<FeatureName, FeatureValues>;
struct FeatureDef
{
std::size_t nbDimensions {};
};
FeatureDef getFeatureDef(const FeatureName& featureName);
FeatureNames getFeatureNames();
struct FeatureSettings
{
double weight {};
};
using FeatureSettingsMap = std::unordered_map<FeatureName, FeatureSettings>;
} // namespace Similarity
@@ -0,0 +1,187 @@
/*
* Copyright (C) 2019 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "similarity/features/SimilarityFeaturesScannerAddon.hpp"
#include "AcousticBrainzUtils.hpp"
#include "database/ScanSettings.hpp"
#include "database/Track.hpp"
#include "database/TrackFeatures.hpp"
#include "utils/Logger.hpp"
#include "SimilarityFeaturesCache.hpp"
namespace Similarity {
static
bool
hasAtLeastOneTrackWithFeatures(Database::Session& session)
{
auto transaction {session.createSharedTransaction()};
return !Database::Track::getAllIdsWithFeatures(session, 1).empty();
}
struct TrackInfo
{
Database::IdType id;
std::optional<UUID> mbid;
};
static
std::vector<TrackInfo>
getTracksWithMBIDAndMissingFeatures(Database::Session& dbSession)
{
std::vector<TrackInfo> res;
auto transaction {dbSession.createSharedTransaction()};
auto tracks {Database::Track::getAllWithMBIDAndMissingFeatures(dbSession)};
for (const Database::Track::pointer& track : tracks)
res.push_back({track.id(), track->getMBID()});
return res;
}
FeaturesScannerAddon::FeaturesScannerAddon(Database::Db& db)
: _dbSession {db}
{
std::optional<Similarity::FeaturesCache> cache {Similarity::FeaturesCache::read()};
if (cache)
{
auto searcher {std::make_shared<Similarity::FeaturesSearcher>(_dbSession, *cache, [&]() { return _stopRequested; })};
if (searcher->isValid())
std::atomic_store(&_searcher, searcher);
}
}
std::shared_ptr<Similarity::FeaturesSearcher>
FeaturesScannerAddon::getSearcher()
{
return std::atomic_load(&_searcher);
}
void
FeaturesScannerAddon::requestStop()
{
_stopRequested = true;
}
void
FeaturesScannerAddon::trackUpdated(Database::IdType trackId)
{
auto uniqueTransaction {_dbSession.createUniqueTransaction()};
auto track {Database::Track::getById(_dbSession, trackId)};
if (!track)
return;
track.modify()->setFeatures({});
}
void
FeaturesScannerAddon::preScanComplete()
{
{
auto transaction {_dbSession.createSharedTransaction()};
if (Database::ScanSettings::get(_dbSession)->getSimilarityEngineType() != Database::ScanSettings::SimilarityEngineType::Features)
{
LMS_LOG(DBUPDATER, INFO) << "Do not fetch features since the engine type does not make use of them";
return;
}
}
LMS_LOG(DBUPDATER, DEBUG) << "Getting tracks with missing Features...";
const std::vector<TrackInfo> tracksInfo {getTracksWithMBIDAndMissingFeatures(_dbSession)};
LMS_LOG(DBUPDATER, DEBUG) << "Getting tracks with missing Features DONE (found " << tracksInfo.size() << ")";
if (!tracksInfo.empty())
Similarity::FeaturesCache::invalidate();
for (const TrackInfo& trackInfo : tracksInfo)
{
if (_stopRequested)
return;
if (trackInfo.mbid)
fetchFeatures(trackInfo.id, *trackInfo.mbid);
}
updateSearcher();
}
void
FeaturesScannerAddon::updateSearcher()
{
LMS_LOG(SIMILARITY, INFO) << "Updating searcher...";
if (!hasAtLeastOneTrackWithFeatures(_dbSession))
{
LMS_LOG(DBUPDATER, INFO) << "No track found with features!";
std::atomic_store(&_searcher, std::shared_ptr<FeaturesSearcher>{});
return;
}
Similarity::FeaturesSearcher::TrainSettings trainSettings;
trainSettings.featureSettingsMap = FeaturesSearcher::getDefaultTrainFeatureSettings();
auto searcher {std::make_shared<FeaturesSearcher>(_dbSession, trainSettings, [&]() { return _stopRequested; })};
if (searcher->isValid())
{
std::atomic_store(&_searcher, searcher);
FeaturesCache cache{searcher->toCache()};
cache.write();
LMS_LOG(DBUPDATER, INFO) << "New features similarity searcher instanciated";
}
else
{
LMS_LOG(DBUPDATER, ERROR) << "Cannot set up a valid features similarity searcher!";
std::atomic_store(&_searcher, std::shared_ptr<FeaturesSearcher>{});
}
}
bool
FeaturesScannerAddon::fetchFeatures(Database::IdType trackId, const UUID& MBID)
{
std::map<std::string, double> features;
LMS_LOG(DBUPDATER, DEBUG) << "Fetching low level features for track '" << MBID.getAsString() << "'";
const std::string data {AcousticBrainz::extractLowLevelFeatures(MBID)};
if (data.empty())
{
LMS_LOG(DBUPDATER, ERROR) << "Track " << trackId << ", MBID = '" << MBID.getAsString() << "': cannot extract features using AcousticBrainz";
return false;
}
{
auto uniqueTransaction {_dbSession.createUniqueTransaction()};
Wt::Dbo::ptr<Database::Track> track {Database::Track::getById(_dbSession, trackId)};
if (!track)
return false;
Database::TrackFeatures::create(_dbSession, track, data);
}
return true;
}
} // namespace Similarity
@@ -0,0 +1,487 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "SimilarityFeaturesSearcher.hpp"
#include <random>
#include <unordered_map>
#include "database/Artist.hpp"
#include "database/Release.hpp"
#include "database/Session.hpp"
#include "database/Track.hpp"
#include "database/TrackFeatures.hpp"
#include "som/DataNormalizer.hpp"
#include "utils/Logger.hpp"
namespace Similarity {
const FeatureSettingsMap&
FeaturesSearcher::getDefaultTrainFeatureSettings()
{
static FeatureSettingsMap defaultTrainFeatureSettings
{
{ "lowlevel.spectral_energyband_high.mean", {1}},
{ "lowlevel.spectral_rolloff.median", {1}},
{ "lowlevel.spectral_contrast_valleys.var", {1}},
{ "lowlevel.erbbands.mean", {1}},
{ "lowlevel.gfcc.mean", {1}},
};
return defaultTrainFeatureSettings;
}
static
std::optional<FeatureValuesMap>
getTrackFeatureValues(FeaturesSearcher::FeaturesFetchFunc func, Database::IdType trackId, const std::unordered_set<FeatureName>& featureNames)
{
return func(trackId, featureNames);
}
static
std::optional<FeatureValuesMap>
getTrackFeatureValuesFromDb(Database::Session& session, Database::IdType trackId, const std::unordered_set<FeatureName>& featureNames)
{
auto func = [&](Database::IdType trackId, const std::unordered_set<FeatureName>& featureNames)
{
std::optional<FeatureValuesMap> res;
auto transaction {session.createSharedTransaction()};
Database::Track::pointer track {Database::Track::getById(session, trackId)};
if (!track)
return res;
res = track->getTrackFeatures()->getFeatureValuesMap(featureNames);
if (res->empty())
res.reset();
return res;
};
return getTrackFeatureValues(func, trackId, featureNames);
}
static
std::optional<SOM::InputVector>
convertFeatureValuesMapToInputVector(const FeatureValuesMap& featureValuesMap, std::size_t nbDimensions)
{
std::size_t i {};
std::optional<SOM::InputVector> res {SOM::InputVector {nbDimensions}};
for (const auto& [featureName, values] : featureValuesMap)
{
if (values.size() != getFeatureDef(featureName).nbDimensions)
{
LMS_LOG(SIMILARITY, WARNING) << "Dimension mismatch for feature '" << featureName << "'. Expected " << getFeatureDef(featureName).nbDimensions << ", got " << values.size();
res.reset();
break;
}
for (double val : values)
(*res)[i++] = val;
}
return res;
}
static
SOM::InputVector
getInputVectorWeights(const FeatureSettingsMap& featureSettingsMap, std::size_t nbDimensions)
{
SOM::InputVector weights {nbDimensions};
std::size_t index {};
for (const auto& [featureName, featureSettings] : featureSettingsMap)
{
const std::size_t featureNbDimensions {getFeatureDef(featureName).nbDimensions};
for (std::size_t i {}; i < featureNbDimensions; ++i)
weights[index++] = (1. / featureNbDimensions * featureSettings.weight);
}
assert(index == nbDimensions);
return weights;
}
FeaturesSearcher::FeaturesSearcher(Database::Session& session,
const TrainSettings& trainSettings,
StopRequestedFunction stopRequested)
{
LMS_LOG(SIMILARITY, INFO) << "Constructing features searcher...";
std::unordered_set<FeatureName> featureNames;
std::transform(std::cbegin(trainSettings.featureSettingsMap), std::cend(trainSettings.featureSettingsMap), std::inserter(featureNames, std::begin(featureNames)),
[](const auto& itFeatureSetting) { return itFeatureSetting.first; });
const std::size_t nbDimensions {std::accumulate(std::cbegin(featureNames), std::cend(featureNames), std::size_t {0},
[](std::size_t sum, const FeatureName& featureName) { return sum + getFeatureDef(featureName).nbDimensions; })};
LMS_LOG(SIMILARITY, DEBUG) << "Features dimension = " << nbDimensions;
std::vector<Database::IdType> trackIds;
{
auto transaction {session.createSharedTransaction()};
LMS_LOG(SIMILARITY, DEBUG) << "Getting Tracks with features...";
trackIds = Database::Track::getAllIdsWithFeatures(session);
LMS_LOG(SIMILARITY, DEBUG) << "Getting Tracks with features DONE (found " << trackIds.size() << " tracks)";
}
std::vector<SOM::InputVector> samples;
std::vector<Database::IdType> samplesTrackIds;
samples.reserve(trackIds.size());
samplesTrackIds.reserve(trackIds.size());
LMS_LOG(SIMILARITY, DEBUG) << "Extracting features...";
for (Database::IdType trackId : trackIds)
{
if (stopRequested && stopRequested())
return;
std::optional<FeatureValuesMap> featureValuesMap;
if (_featuresFetchFunc)
featureValuesMap = getTrackFeatureValues(_featuresFetchFunc, trackId, featureNames);
else
featureValuesMap = getTrackFeatureValuesFromDb(session, trackId, featureNames);
if (!featureValuesMap)
continue;
std::optional<SOM::InputVector> inputVector {convertFeatureValuesMapToInputVector(*featureValuesMap, nbDimensions)};
if (!inputVector)
continue;
samples.emplace_back(std::move(*inputVector));
samplesTrackIds.emplace_back(trackId);
}
LMS_LOG(SIMILARITY, DEBUG) << "Extracting features DONE";
if (samples.empty())
{
LMS_LOG(SIMILARITY, INFO) << "Nothing to classify!";
return;
}
LMS_LOG(SIMILARITY, DEBUG) << "Normalizing data...";
SOM::DataNormalizer dataNormalizer {nbDimensions};
dataNormalizer.computeNormalizationFactors(samples);
for (auto& sample : samples)
dataNormalizer.normalizeData(sample);
SOM::Coordinate size {static_cast<SOM::Coordinate>(std::sqrt(samples.size() / trainSettings.sampleCountPerNeuron))};
LMS_LOG(SIMILARITY, INFO) << "Found " << samples.size() << " tracks, constructing a " << size << "*" << size << " network";
SOM::Network network {size, size, nbDimensions};
SOM::InputVector weights {getInputVectorWeights(trainSettings.featureSettingsMap, nbDimensions)};
network.setDataWeights(weights);
auto progressIndicator{[](const auto& iter)
{
LMS_LOG(SIMILARITY, DEBUG) << "Current pass = " << iter.idIteration << " / " << iter.iterationCount;
}};
LMS_LOG(SIMILARITY, DEBUG) << "Training network...";
network.train(samples, trainSettings.iterationCount, progressIndicator, stopRequested);
LMS_LOG(SIMILARITY, DEBUG) << "Training network DONE";
if (stopRequested && stopRequested())
return;
LMS_LOG(SIMILARITY, DEBUG) << "Classifying tracks...";
std::map<Database::IdType, std::set<SOM::Position>> trackPositions;
for (std::size_t i {}; i < samples.size(); ++i)
{
if (stopRequested && stopRequested())
return;
const SOM::Position position {network.getClosestRefVectorPosition(samples[i])};
trackPositions[samplesTrackIds[i]].insert(position);
}
LMS_LOG(SIMILARITY, DEBUG) << "Classifying tracks DONE";
init(session, std::move(network), std::move(trackPositions), stopRequested);
LMS_LOG(SIMILARITY, INFO) << "Successfully constructed features searcher";
}
FeaturesSearcher::FeaturesSearcher(Database::Session& session, FeaturesCache cache, StopRequestedFunction stopRequested)
{
LMS_LOG(SIMILARITY, INFO) << "Constructing features searcher from cache...";
init(session, std::move(cache._network), std::move(cache._trackPositions), stopRequested);
LMS_LOG(SIMILARITY, INFO) << "Successfully constructed features searcher from cache";
}
bool
FeaturesSearcher::isValid() const
{
return _network.get() != nullptr;
}
bool
FeaturesSearcher::isTrackClassified(Database::IdType trackId) const
{
return (_trackPositions.find(trackId) != _trackPositions.end());
}
bool
FeaturesSearcher::isReleaseClassified(Database::IdType releaseId) const
{
return (_releasePositions.find(releaseId) != _releasePositions.end());
}
bool
FeaturesSearcher::isArtistClassified(Database::IdType artistId) const
{
return (_artistPositions.find(artistId) != _artistPositions.end());
}
std::vector<Database::IdType>
FeaturesSearcher::getSimilarTracks(const std::set<Database::IdType>& tracksIds, std::size_t maxCount) const
{
return getSimilarObjects(tracksIds, _tracksMap, _trackPositions, maxCount);
}
std::vector<Database::IdType>
FeaturesSearcher::getSimilarReleases(Database::IdType releaseId, std::size_t maxCount) const
{
return getSimilarObjects({releaseId}, _releasesMap, _releasePositions, maxCount);
}
std::vector<Database::IdType>
FeaturesSearcher::getSimilarArtists(Database::IdType artistId, std::size_t maxCount) const
{
return getSimilarObjects({artistId}, _artistsMap, _artistPositions, maxCount);
}
void
FeaturesSearcher::dump(Database::Session& session, std::ostream& os) const
{
if (!isValid())
{
os << "Invalid searcher" << std::endl;
return;
}
os << "Number of tracks classified: " << _trackPositions.size() << std::endl;
os << "Network size: " << _network->getWidth() << " * " << _network->getHeight() << std::endl;
os << "Ref vectors median distance = " << _networkRefVectorsDistanceMedian << std::endl;
auto transaction {session.createSharedTransaction()};
for (SOM::Coordinate y {}; y < _network->getHeight(); ++y)
{
for (SOM::Coordinate x {}; x < _network->getWidth(); ++x)
{
const auto& trackIds {_tracksMap[{x, y}]};
os << "{" << x << ", " << y << "}";
if (y > 0)
os << " - {" << x << ", " << y - 1 << "}: " << _network->getRefVectorsDistance({x, y}, {x, y - 1});
if (x > 0)
os << " - {" << x - 1 << ", " << y << "}: " << _network->getRefVectorsDistance({x, y}, {x - 1, y});
if (y != _network->getHeight() - 1)
os << " - {" << x << ", " << y + 1 << "}: " << _network->getRefVectorsDistance({x, y}, {x, y + 1});
if (x != _network->getWidth() - 1)
os << " - {" << x + 1 << ", " << y << "}: " << _network->getRefVectorsDistance({x, y}, {x + 1, y});
os << std::endl;
for (Database::IdType trackId : trackIds)
{
auto track {Database::Track::getById(session, trackId)};
if (!track)
continue;
os << "\t";
for (auto artist : track->getArtists())
os << artist->getName() << " - ";
if (track->getRelease())
os << track->getRelease()->getName() << " - ";
os << track->getName() << std::endl;
}
}
os << std::endl;
}
}
FeaturesCache
FeaturesSearcher::toCache() const
{
return FeaturesCache{*_network, _trackPositions};
}
void
FeaturesSearcher::init(Database::Session& session,
SOM::Network network,
std::map<Database::IdType,
std::set<SOM::Position>> tracksPosition,
std::function<bool()> stopRequested)
{
_networkRefVectorsDistanceMedian = network.computeRefVectorsDistanceMedian();
LMS_LOG(SIMILARITY, DEBUG) << "Median distance betweend ref vectors = " << _networkRefVectorsDistanceMedian;
SOM::Coordinate width {network.getWidth()};
SOM::Coordinate height {network.getHeight()};
_artistsMap = SOM::Matrix<std::set<Database::IdType>>{width, height};
_releasesMap = SOM::Matrix<std::set<Database::IdType>>{width, height};
_tracksMap = SOM::Matrix<std::set<Database::IdType>>{width, height};
LMS_LOG(SIMILARITY, DEBUG) << "Constructing maps...";
for (auto itTrackCoord : tracksPosition)
{
if (stopRequested && stopRequested())
return;
auto transaction {session.createSharedTransaction()};
Database::IdType trackId {itTrackCoord.first};
const std::set<SOM::Position>& positionSet {itTrackCoord.second};
const Database::Track::pointer track {Database::Track::getById(session, trackId)};
if (!track)
continue;
for (const SOM::Position& position : positionSet)
{
_tracksMap[position].insert(trackId);
_trackPositions[trackId].insert(position);
if (track->getRelease())
{
_releasePositions[track->getRelease().id()].insert(position);
_releasesMap[position].insert(track->getRelease().id());
}
for (const auto& artist : track->getArtists())
{
_artistPositions[artist.id()].insert(position);
_artistsMap[position].insert(artist.id());
}
}
}
_network = std::make_unique<SOM::Network>(std::move(network));
LMS_LOG(SIMILARITY, DEBUG) << "Constructing maps... DONE";
}
static
std::set<SOM::Position>
getMatchingRefVectorsPosition(const std::set<Database::IdType>& ids, const std::map<Database::IdType, std::set<SOM::Position>>& objectPosition)
{
std::set<SOM::Position> res;
if (ids.empty())
return res;
for (auto id : ids)
{
auto it = objectPosition.find(id);
if (it == objectPosition.end())
continue;
for (const auto& position : it->second)
res.insert(position);
}
return res;
}
static
std::set<Database::IdType>
getObjectsIds(const std::set<SOM::Position>& positionSet, const SOM::Matrix<std::set<Database::IdType>>& objectsMap )
{
std::set<Database::IdType> res;
for (const auto& position : positionSet)
{
for (auto id : objectsMap.get(position))
res.insert(id);
}
return res;
}
std::vector<Database::IdType>
FeaturesSearcher::getSimilarObjects(const std::set<Database::IdType>& ids,
const SOM::Matrix<std::set<Database::IdType>>& objectsMap,
const std::map<Database::IdType, std::set<SOM::Position>>& objectPosition,
std::size_t maxCount) const
{
std::vector<Database::IdType> res;
if (!isValid())
return res;
auto now {std::chrono::system_clock::now()};
std::mt19937 randGenerator{static_cast<std::mt19937::result_type>(std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count())};
std::set<SOM::Position> searchedRefVectorsPosition {getMatchingRefVectorsPosition(ids, objectPosition)};
if (searchedRefVectorsPosition.empty())
return res;
while (1)
{
std::set<Database::IdType> closestObjectIds {getObjectsIds(searchedRefVectorsPosition, objectsMap)};
// Remove objects that are already in input or already reported
for (auto id : ids)
closestObjectIds.erase(id);
for (auto id : res)
closestObjectIds.erase(id);
{
std::vector<Database::IdType> objectIdsToAdd {closestObjectIds.begin(), closestObjectIds.end()};
std::shuffle(objectIdsToAdd.begin(), objectIdsToAdd.end(), randGenerator);
std::copy(objectIdsToAdd.begin(), objectIdsToAdd.end(), std::back_inserter(res));
}
if (res.size() > maxCount)
res.resize(maxCount);
if (res.size() == maxCount)
break;
// If there is not enough objects, try again with closest neighbour until there is too much distance
std::optional<SOM::Position> closestRefVectorPosition {_network->getClosestRefVectorPosition(searchedRefVectorsPosition, _networkRefVectorsDistanceMedian * 0.75)};
if (!closestRefVectorPosition)
break;
searchedRefVectorsPosition.insert(*closestRefVectorPosition);
}
return res;
}
} // ns Similarity
@@ -0,0 +1,110 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <map>
#include <optional>
#include <set>
#include <string>
#include "database/Types.hpp"
#include "som/DataNormalizer.hpp"
#include "som/Network.hpp"
#include "SimilarityFeaturesCache.hpp"
#include "SimilarityFeaturesDefs.hpp"
namespace Database
{
class Session;
}
namespace Similarity {
using FeatureWeight = double;
class FeaturesSearcher
{
public:
using StopRequestedFunction = std::function<bool()>; // return true if stop requested
// Use cache
FeaturesSearcher(Database::Session& session, FeaturesCache cache, StopRequestedFunction stopRequested);
// Use training (may be very slow)
struct TrainSettings
{
std::size_t iterationCount {10};
float sampleCountPerNeuron {4};
FeatureSettingsMap featureSettingsMap;
};
FeaturesSearcher(Database::Session& session, const TrainSettings& trainSettings, StopRequestedFunction stopRequested = {});
static const FeatureSettingsMap& getDefaultTrainFeatureSettings();
bool isValid() const;
bool isTrackClassified(Database::IdType trackId) const;
bool isReleaseClassified(Database::IdType releaseId) const;
bool isArtistClassified(Database::IdType artistId) const;
std::vector<Database::IdType> getSimilarTracks(const std::set<Database::IdType>& tracksId, std::size_t maxCount) const;
std::vector<Database::IdType> getSimilarReleases(Database::IdType releaseId, std::size_t maxCount) const;
std::vector<Database::IdType> getSimilarArtists(Database::IdType artistId, std::size_t maxCount) const;
void dump(Database::Session& session, std::ostream& os) const;
FeaturesCache toCache() const;
using FeaturesFetchFunc = std::function<std::optional<std::unordered_map<std::string, std::vector<double>>>(Database::IdType /*trackId*/, const std::unordered_set<std::string>& /*features*/)>;
// Default is to retrieve the features from the database (may be slow).
// Use this only if you want to train different searchers with the same data
static void setFeaturesFetchFunc(FeaturesFetchFunc func) { _featuresFetchFunc = func; }
private:
using ObjectPositions = std::map<Database::IdType, std::set<SOM::Position>>;
void init(Database::Session& session,
SOM::Network network,
ObjectPositions tracksPosition,
StopRequestedFunction stopRequested);
std::vector<Database::IdType> getSimilarObjects(const std::set<Database::IdType>& ids,
const SOM::Matrix<std::set<Database::IdType>>& objectsMap,
const ObjectPositions& objectPosition,
std::size_t maxCount) const;
std::unique_ptr<SOM::Network> _network;
double _networkRefVectorsDistanceMedian {};
SOM::Matrix<std::set<Database::IdType>> _artistsMap;
ObjectPositions _artistPositions;
SOM::Matrix<std::set<Database::IdType>> _releasesMap;
ObjectPositions _releasePositions;
SOM::Matrix<std::set<Database::IdType>> _tracksMap;
ObjectPositions _trackPositions;
static inline FeaturesFetchFunc _featuresFetchFunc;
};
} // ns Similarity
@@ -0,0 +1,120 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "DataNormalizer.hpp"
#include <algorithm>
#include <numeric>
#include <sstream>
namespace SOM
{
template<typename T>
static
T
variance(const std::vector<T>& vec)
{
std::size_t size {vec.size()};
if (size == 1)
return T {};
const T mean {std::accumulate(vec.begin(), vec.end(), T{}) / size};
return std::accumulate(vec.begin(), vec.end(), T {},
[mean, size] (T accumulator, const T& val)
{
return accumulator + ((val - mean) * (val - mean) / (size - 1));
});
}
DataNormalizer::DataNormalizer(std::size_t inputDimCount)
: _inputDimCount{inputDimCount}
{
}
const DataNormalizer::MinMax&
DataNormalizer::getValue(std::size_t index) const
{
return _minmax[index];
}
void
DataNormalizer::setValue(std::size_t index, const MinMax& minMax)
{
_minmax[index] = minMax;
}
void
DataNormalizer::computeNormalizationFactors(const std::vector<InputVector>& inputVectors)
{
if (inputVectors.empty())
throw Exception("Empty input vectors");
// For each dimension of the input, compute the min/max
_minmax.clear();
_minmax.resize(_inputDimCount);
for (std::size_t dimId {}; dimId < _inputDimCount; ++dimId)
{
std::vector<InputVector::value_type> values;
for (const auto& inputVector: inputVectors)
{
checkSameDimensions(inputVector, _inputDimCount);
values.push_back(inputVector[dimId]);
}
auto result {std::minmax_element(values.begin(), values.end())};
_minmax[dimId] = {*result.first, *result.second};
}
}
InputVector::value_type
DataNormalizer::normalizeValue(InputVector::value_type value, std::size_t dimId) const
{
// clamp
if (value > _minmax[dimId].max)
value = _minmax[dimId].max;
else if (value < _minmax[dimId].min)
value = _minmax[dimId].min;
return (value - _minmax[dimId].min) / (_minmax[dimId].max - _minmax[dimId].min);
}
void
DataNormalizer::normalizeData(InputVector& a) const
{
checkSameDimensions(a, _inputDimCount);
for (std::size_t dimId {}; dimId < _inputDimCount; ++dimId)
{
a[dimId] = normalizeValue(a[dimId], dimId);
}
}
void
DataNormalizer::dump(std::ostream& os) const
{
for (std::size_t i {}; i < _inputDimCount; ++i)
os << "(" << _minmax[i].min << ", " << _minmax[i].max << ")";
}
} // namespace SOM
@@ -0,0 +1,61 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vector>
#include <ostream>
#include "Network.hpp"
namespace SOM
{
class DataNormalizer
{
public:
struct MinMax
{
InputVector::value_type min;
InputVector::value_type max;
};
DataNormalizer(std::size_t inputDimCount);
std::size_t getInputDimCount() const { return _inputDimCount; }
const MinMax& getValue(std::size_t index) const;
void setValue(std::size_t index, const MinMax& minMax);
void computeNormalizationFactors(const std::vector<InputVector>& dataSamples);
void normalizeData(InputVector& data) const;
void dump(std::ostream& os) const;
private:
InputVector::value_type normalizeValue(InputVector::value_type value, std::size_t dimensionId) const;
const std::size_t _inputDimCount;
std::vector<MinMax> _minmax; // Indexed min/max used to normalize data
};
} // namespace SOM
@@ -0,0 +1,194 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vector>
#include <cmath>
namespace SOM
{
class Exception : public LmsException
{
public:
Exception(const std::string& msg) : LmsException(msg) {}
};
class InputVector
{
public:
using value_type = double;
using Norm = double;
using Distance = double;
InputVector(std::size_t nbDimensions, value_type defaultValue = value_type {}) : _values(nbDimensions, defaultValue) {}
bool hasSameDimension(const InputVector& other) const
{
return _values.size() == other._values.size();
}
std::size_t getNbDimensions() const
{
return _values.size();
}
value_type& operator[](std::size_t index)
{
if (index >= getNbDimensions())
throw Exception("Bad range");
return _values[index];
}
value_type operator[](std::size_t index) const
{
if (index >= getNbDimensions())
throw Exception("Bad range");
return _values[index];
}
InputVector& operator+=(const InputVector& other)
{
if (!hasSameDimension(other.getNbDimensions()))
throw Exception {"Not the same dimension count"};
for (std::size_t i {}; i < _values.size(); ++i)
{
_values[i] += other[i];
}
return *this;
}
InputVector& operator-=(const InputVector& other)
{
if (!hasSameDimension(other.getNbDimensions()))
throw Exception {"Not the same dimension count"};
for (std::size_t i {}; i < _values.size(); ++i)
{
_values[i] -= other[i];
}
return *this;
}
InputVector& operator*=(value_type factor)
{
for (std::size_t i {}; i < _values.size(); ++i)
{
_values[i] *= factor;
}
return *this;
}
Norm computeNorm() const
{
Norm res {};
for (value_type val : _values)
res += val * val;
return std::sqrt(res);
}
Distance computeEuclidianSquareDistance(const InputVector& other, const InputVector& weights) const
{
if (!hasSameDimension(other.getNbDimensions())
|| !hasSameDimension(weights.getNbDimensions()))
{
throw Exception {"Not the same dimension count"};
}
Distance res {};
for (std::size_t i {}; i < getNbDimensions(); ++i)
{
const InputVector::value_type diff {_values[i] - other._values[i]};
res += diff * diff * weights._values[i];
}
return res;
}
std::vector<value_type>::iterator begin()
{
return _values.begin();
}
std::vector<value_type>::const_iterator begin() const
{
return _values.cbegin();
}
std::vector<value_type>::const_iterator cbegin() const
{
return _values.cbegin();
}
std::vector<value_type>::iterator end()
{
return _values.end();
}
std::vector<value_type>::const_iterator end() const
{
return _values.cend();
}
std::vector<value_type>::const_iterator cend() const
{
return _values.cend();
}
private:
friend class InputVector operator-(const InputVector& a, const InputVector& b)
{
if (!a.hasSameDimension(b.getNbDimensions()))
throw Exception {"Not the same dimension count"};
InputVector res {a.getNbDimensions()};
for (std::size_t i {}; i < res._values.size(); ++i)
res._values[i] = a._values[i] - b._values[i];
return res;
}
friend std::ostream&
operator<<(std::ostream& os, const InputVector& a)
{
os << "[";
for (value_type val : a._values)
{
os << val << " ";
}
os << "]";
return os;
}
std::vector<value_type> _values;
};
}
@@ -0,0 +1,118 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <algorithm>
#include <cassert>
#include <sstream>
#include <vector>
namespace SOM
{
using Coordinate = unsigned;
using Norm = InputVector::value_type;
struct Position
{
Coordinate x;
Coordinate y;
bool operator<(const Position& other) const
{
if (x == other.x)
return y < other.y;
else
return x < other.x;
}
bool operator==(const Position& other) const
{
return x == other.x && y == other.y;
}
};
template <typename T>
class Matrix
{
public:
Matrix() = default;
Matrix(Coordinate width, Coordinate height)
: _width{width},
_height{height}
{
_values.resize(_width*_height);
}
template<typename... CtArgs>
Matrix(Coordinate width, Coordinate height, CtArgs... args)
: _width{width},
_height{height}
{
_values.resize(_width*_height, T{args...});
}
void clear()
{
std::vector<T> values(_width*_height);
_values.swap(values);
}
Coordinate getHeight() const { return _height; }
Coordinate getWidth() const { return _width; }
T& get(const Position& position)
{
assert(position.x < _width);
assert(position.y < _height);
return _values[position.x + _width*position.y];
}
const T& get(const Position& position) const
{
assert(position.x < _width);
assert(position.y < _height);
return _values[position.x + _width*position.y];
}
T& operator[](const Position& position) { return get(position); }
const T& operator[](const Position& position) const { return get(position); }
template <typename Func>
Position getPositionMinElement(Func func) const
{
assert(!_values.empty());
auto it {std::min_element(_values.begin(), _values.end(), std::move(func))};
auto index {static_cast<Coordinate>(std::distance(_values.begin(), it))};
return {index % _height, index / _height};
}
private:
Coordinate _width {};
Coordinate _height {};
std::vector<T> _values;
};
} // ns SOM
@@ -0,0 +1,336 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#include "Network.hpp"
#include <algorithm>
#include <chrono>
#include <cmath>
#include <random>
#include <sstream>
#include "utils/Logger.hpp"
namespace SOM
{
void
checkSameDimensions(const InputVector& a, const InputVector& b)
{
if (!a.hasSameDimension(b))
throw Exception("Bad data dimension count");
}
void
checkSameDimensions(const InputVector& a, std::size_t inputDimCount)
{
if (a.getNbDimensions() != inputDimCount)
throw Exception("Bad data dimension count");
}
static LearningFactor
defaultLearningFactor(Network::CurrentIteration iteration)
{
static const LearningFactor initialValue{1};
return initialValue * exp(-((iteration.idIteration + 1) / static_cast<LearningFactor>(iteration.iterationCount)));
}
static InputVector::Distance
euclidianSquareDistance(const InputVector& a, const InputVector& b, const InputVector& weights)
{
return a.computeEuclidianSquareDistance(b, weights);
}
static
InputVector::value_type
sigmaFunc(Network::CurrentIteration iteration)
{
constexpr InputVector::value_type sigma0 {1};
return sigma0 * std::exp(- ((iteration.idIteration + 1) / static_cast<InputVector::value_type>(iteration.iterationCount)));
}
static
InputVector::value_type
defaultNeighbourhoodFunc(Norm norm, const Network::CurrentIteration& iteration)
{
InputVector::value_type sigma {sigmaFunc(iteration)};
return exp(-norm / (2 * sigma * sigma));
}
Network::Network(Coordinate width, Coordinate height, std::size_t inputDimCount)
:
_inputDimCount(inputDimCount),
_weights(inputDimCount, static_cast<InputVector::value_type>(1)),
_refVectors(width, height, _inputDimCount),
_distanceFunc(euclidianSquareDistance),
_learningFactorFunc(defaultLearningFactor),
_neighbourhoodFunc(defaultNeighbourhoodFunc)
{
auto now {std::chrono::system_clock::now()};
std::mt19937 randGenerator {static_cast<std::mt19937::result_type>(std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count())};
// init each vector with a random normalized value
std::uniform_real_distribution<InputVector::value_type> dist{0, 1};
for (Coordinate y {}; y < _refVectors.getHeight(); ++y)
{
for (Coordinate x {}; x < _refVectors.getWidth(); ++x)
{
for (InputVector::value_type& val : _refVectors.get({x,y}))
val = dist(randGenerator);
}
}
}
void
Network::setDataWeights(const InputVector& weights)
{
checkSameDimensions(weights, _inputDimCount);
_weights = weights;
}
void
Network::setRefVector(const Position& position, const InputVector& data)
{
checkSameDimensions(data, _inputDimCount);
_refVectors[position] = data;
}
InputVector::Distance
Network::getRefVectorsDistance(const Position& position1, const Position& position2) const
{
return _distanceFunc(_refVectors.get(position1), _refVectors.get(position2), _weights);
}
InputVector::Distance
Network::computeRefVectorsDistanceMean() const
{
std::vector<InputVector::Distance> values;
values.reserve(2 * _refVectors.getHeight()*_refVectors.getWidth() - _refVectors.getWidth() - _refVectors.getHeight());
for (Coordinate y {}; y < _refVectors.getHeight(); ++y)
{
for (Coordinate x {}; x < _refVectors.getWidth(); ++x)
{
if (x != _refVectors.getWidth() - 1)
values.emplace_back(getRefVectorsDistance( {x, y}, {x + 1, y}));
if (y != _refVectors.getHeight() - 1)
values.emplace_back(getRefVectorsDistance( {x, y}, {x, y + 1}));
}
}
return std::accumulate(values.begin(), values.end(), 0.) / values.size();
}
double
Network::computeRefVectorsDistanceMedian() const
{
std::vector<InputVector::Distance> values;
values.reserve(2*_refVectors.getHeight()*_refVectors.getWidth() - _refVectors.getWidth() - _refVectors.getHeight());
for (Coordinate y {}; y < _refVectors.getHeight(); ++y)
{
for (Coordinate x {}; x < _refVectors.getWidth(); ++x)
{
if (x != _refVectors.getWidth() - 1)
values.emplace_back(getRefVectorsDistance( {x, y}, {x + 1, y}));
if (y != _refVectors.getHeight() - 1)
values.emplace_back(getRefVectorsDistance( {x, y}, {x, y + 1}));
}
}
std::sort(values.begin(), values.end());
return values[values.size() > 1 ? values.size()/2 - 1 : 0];
}
void
Network::dump(std::ostream& os) const
{
os << "Width: " << _refVectors.getWidth() << ", Height: " << _refVectors.getHeight() << std::endl;;
for (Coordinate y {}; y < _refVectors.getHeight(); ++y)
{
for (Coordinate x {}; x < _refVectors.getWidth(); ++x)
{
os << _refVectors.get({x, y}) << " ";
}
os << std::endl;
}
os << std::endl;
}
Position
Network::getClosestRefVectorPosition(const InputVector& data) const
{
return _refVectors.getPositionMinElement([&](const auto& a, const auto& b)
{
return (_distanceFunc(a, data, _weights) < _distanceFunc(b, data, _weights));
});
}
std::optional<Position>
Network::getClosestRefVectorPosition(const InputVector& data, InputVector::Distance maxDistance) const
{
std::optional<Position> position {getClosestRefVectorPosition(data)};
if (_distanceFunc(data, _refVectors.get(*position), _weights) > maxDistance)
position.reset();
return position;
}
std::optional<Position>
Network::getClosestRefVectorPosition(const std::set<Position>& refVectorsPosition, InputVector::Distance maxDistance) const
{
std::set<Position> neighboursPosition;
for (const Position& refVectorPosition : refVectorsPosition)
{
if (refVectorPosition.y > 0)
neighboursPosition.insert({ refVectorPosition.x, refVectorPosition.y - 1 });
if (refVectorPosition.y < _refVectors.getHeight() - 1)
neighboursPosition.insert({ refVectorPosition.x, refVectorPosition.y + 1 });
if (refVectorPosition.x > 0)
neighboursPosition.insert({ refVectorPosition.x - 1, refVectorPosition.y });
if (refVectorPosition.x < _refVectors.getWidth() - 1)
neighboursPosition.insert({ refVectorPosition.x + 1, refVectorPosition.y });
}
// remove position that are in the input position
for (const auto& refVectorPosition : refVectorsPosition)
neighboursPosition.erase(refVectorPosition);
if (neighboursPosition.empty())
return std::nullopt;
// Now compute the distance for each neighbour
struct NeighbourInfo
{
Position position;
double distance;
};
std::vector<NeighbourInfo> neighboursInfo;
for (const Position& neighbourPosition : neighboursPosition)
{
auto min = std::min_element(refVectorsPosition.begin(), refVectorsPosition.end(),
[this, neighbourPosition](const auto& a, const auto& b)
{
return (this->getRefVectorsDistance(a, neighbourPosition) < this->getRefVectorsDistance(b, neighbourPosition));
});
InputVector::Distance distance {getRefVectorsDistance(neighbourPosition, *min)};
if (distance > maxDistance)
continue;
neighboursInfo.emplace_back(NeighbourInfo {neighbourPosition, distance});
}
if (neighboursInfo.empty())
return std::nullopt;
auto min {std::min_element(std::cbegin(neighboursInfo), std::cend(neighboursInfo),
[&](const auto& a, const auto& b)
{
return a.distance < b.distance;
})};
return min->position;
}
static Norm
computePositionNorm(const Position& c1, const Position& c2)
{
return std::sqrt((c1.x - c2.x) * (c1.x - c2.x) + (c1.y - c2.y) * (c1.y - c2.y));
}
void
Network::updateRefVectors(const Position& closestRefVectorPosition, const InputVector& input, LearningFactor learningFactor, const CurrentIteration& iteration)
{
for (Coordinate y {}; y < _refVectors.getHeight(); ++y)
{
for (Coordinate x {}; x < _refVectors.getWidth(); ++x)
{
InputVector& refVector {_refVectors.get({x, y})};
const Norm norm {computePositionNorm({x, y}, closestRefVectorPosition)};
InputVector delta {input - refVector};
delta *= (learningFactor * _neighbourhoodFunc(norm, iteration));
refVector += delta;
}
}
}
void
Network::train(const std::vector<InputVector>& inputData, std::size_t nbIterations, ProgressCallback progressCallback, RequestStopCallback requestStopCallback)
{
bool stopRequested {false};
std::vector<const InputVector*> inputDataShuffled;
inputDataShuffled.reserve(inputData.size());
for (const auto& input : inputData)
inputDataShuffled.push_back(&input);
auto now {std::chrono::system_clock::now()};
std::mt19937 randGenerator{static_cast<std::mt19937::result_type>(std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()).count())};
for (std::size_t i {}; i < nbIterations; ++i)
{
CurrentIteration curIter {i, nbIterations};
if (progressCallback)
progressCallback(curIter);
std::shuffle(inputDataShuffled.begin(), inputDataShuffled.end(), randGenerator);
const LearningFactor learningFactor {_learningFactorFunc(curIter)};
for (const InputVector* input : inputDataShuffled)
{
if (requestStopCallback)
stopRequested = requestStopCallback();
if (stopRequested)
return;
updateRefVectors(getClosestRefVectorPosition(*input), *input, learningFactor, curIter);
}
if (stopRequested)
return;
}
}
const InputVector&
Network::getRefVector(const Position& position) const
{
return _refVectors[position];
}
} // namespace SOM
@@ -0,0 +1,110 @@
/*
* Copyright (C) 2018 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vector>
#include <set>
#include <optional>
#include <ostream>
#include <functional>
#include "utils/Exception.hpp"
#include "InputVector.hpp"
#include "Matrix.hpp"
namespace SOM
{
using LearningFactor = InputVector::value_type;
void checkSameDimensions(const InputVector& a, const InputVector& b);
void checkSameDimensions(const InputVector& a, std::size_t inputDimCount);
std::ostream& operator<<(std::ostream& os, const InputVector& a);
class Network
{
public:
// Init a network with random values
Network(Coordinate width, Coordinate height, std::size_t inputDimCount);
Coordinate getWidth() const { return _refVectors.getWidth(); }
Coordinate getHeight() const { return _refVectors.getHeight(); }
std::size_t getInputDimCount() const { return _inputDimCount; }
const InputVector& getDataWeights() const { return _weights; }
// Set weight for each dimension (default is 1 for each weight)
void setDataWeights(const InputVector& weights);
// use this to manually construct a network without training
void setRefVector(const Position& position, const InputVector& data);
// <!> data must be normalized
struct CurrentIteration
{
std::size_t idIteration;
std::size_t iterationCount;
};
using ProgressCallback = std::function<void(const CurrentIteration&)>;
using RequestStopCallback = std::function<bool()>;
void train(const std::vector<InputVector>& dataSamples, std::size_t nbIterations, ProgressCallback = ProgressCallback{}, RequestStopCallback = RequestStopCallback{});
const InputVector& getRefVector(const Position& position) const;
Position getClosestRefVectorPosition(const InputVector& data) const;
std::optional<Position> getClosestRefVectorPosition(const InputVector& data, InputVector::Distance maxDistance) const;
std::optional<Position> getClosestRefVectorPosition(const std::set<Position>& refVectorsPosition, InputVector::Distance maxDistance) const;
InputVector::Distance getRefVectorsDistance(const Position& position1, const Position& position2) const;
InputVector::Distance computeRefVectorsDistanceMean() const;
InputVector::Distance computeRefVectorsDistanceMedian() const;
void dump(std::ostream& os) const;
// For each ref vector, update formula is:
// i is the current iteration
// refVector(i+1) = refVector(i) + LearningFactor(i) * NeighbourhoodFunc(i) * (MatchingRefVector - refVector)
using DistanceFunc = std::function<InputVector::Distance(const InputVector& /* a */, const InputVector& /* b */, const InputVector& /* weights */)>;
void setDistanceFunc(DistanceFunc distanceFunc);
DistanceFunc getDistanceFunc() { return _distanceFunc; }
using LearningFactorFunc = std::function<LearningFactor(const CurrentIteration&)>;
void setLearningFactorFunc(LearningFactorFunc learningFactorFunc);
using NeighbourhoodFunc = std::function<InputVector::value_type(Norm /* norm(Position - CoordMatchingRefVector) */, const CurrentIteration&)>;
void setNeighbourhoodFunc(NeighbourhoodFunc neighbourhoodFunc);
private:
void updateRefVectors(const Position& closestRefVectorPosition, const InputVector& input, LearningFactor learningFactor, const CurrentIteration& iteration);
std::size_t _inputDimCount {};
InputVector _weights; // weight for each dimension
Matrix<InputVector> _refVectors;
DistanceFunc _distanceFunc;
LearningFactorFunc _learningFactorFunc;
NeighbourhoodFunc _neighbourhoodFunc;
};
} // namespace SOM
@@ -0,0 +1,35 @@
/*
* Copyright (C) 2020 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <memory>
namespace Database
{
class Session;
}
namespace Recommendation
{
class Provider;
std::unique_ptr<Provider> createClustersRecommendationProvider();
}
@@ -0,0 +1,40 @@
/*
* Copyright (C) 2020 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <memory>
namespace Database
{
class Session;
}
namespace Scanner
{
class IMediaScanner;
}
namespace Recommendation
{
class Provider;
std::unique_ptr<Provider> createFeaturesRecommendationProvider(Scanner::IMediaScanner& scanner);
}
@@ -0,0 +1,54 @@
/*
* Copyright (C) 2019 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <vector>
#include <unordered_set>
#include "database/Types.hpp"
#include "Provider.hpp"
namespace Database
{
class Session;
}
namespace Recommendation
{
class Provider;
class IEngine
{
public:
virtual ~IEngine() = default;
virtual void clearProviders() = 0;
virtual void addProvider(std::unique_ptr<Provider> provider, unsigned priority) = 0;
// Closest results first
virtual std::vector<Database::IdType> getSimilarTracksFromTrackList(Database::Session& session, Database::IdType tracklistId, std::size_t maxCount) = 0;
virtual std::vector<Database::IdType> getSimilarTracks(Database::Session& session, const std::unordered_set<Database::IdType>& tracksId, std::size_t maxCount) = 0;
virtual std::vector<Database::IdType> getSimilarReleases(Database::Session& session, Database::IdType releaseId, std::size_t maxCount) = 0;
virtual std::vector<Database::IdType> getSimilarArtists(Database::Session& session, Database::IdType artistId, std::size_t maxCount) = 0;
};
std::unique_ptr<IEngine> createEngine();
} // ns Recommendation
@@ -0,0 +1,50 @@
/*
* Copyright (C) 2020 Emeric Poupon
*
* This file is part of LMS.
*
* LMS is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* LMS is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LMS. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <unordered_set>
#include <vector>
#include "database/Types.hpp"
namespace Database
{
class Session;
}
namespace Recommendation
{
class Provider
{
public:
virtual ~Provider() = default;
virtual bool isTrackClassified(Database::IdType trackId) const = 0;
virtual bool isReleaseClassified(Database::IdType releaseId) const = 0;
virtual bool isArtistClassified(Database::IdType artistId) const = 0;
virtual std::vector<Database::IdType> getSimilarTracksFromTrackList(Database::Session& session, Database::IdType tracklistId, std::size_t maxCount) = 0;
virtual std::vector<Database::IdType> getSimilarTracks(Database::Session& session, const std::unordered_set<Database::IdType>& tracksId, std::size_t maxCount) const = 0;
virtual std::vector<Database::IdType> getSimilarReleases(Database::IdType releaseId, std::size_t maxCount) const = 0;
virtual std::vector<Database::IdType> getSimilarArtists(Database::IdType artistId, std::size_t maxCount) const = 0;
};
} // ns Recommendation