Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/hl-mapping'
Browse files Browse the repository at this point in the history
  • Loading branch information
alastair committed Jul 1, 2019
2 parents 429601a + cfd439d commit 08f4dc5
Show file tree
Hide file tree
Showing 10 changed files with 1,161 additions and 782 deletions.
92 changes: 92 additions & 0 deletions admin/sql/create_models.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,96 @@ INSERT INTO model (model, model_version, date, status) VALUES ('timbre', 'v2.1_b
INSERT INTO model (model, model_version, date, status) VALUES ('tonal_atonal', 'v2.1_beta1', now(), 'show');
INSERT INTO model (model, model_version, date, status) VALUES ('voice_instrumental', 'v2.1_beta1', now(), 'show');

UPDATE model SET class_mapping = '{
"danceable": "Danceable",
"not_danceable": "Not danceable"
}'::jsonb WHERE model.model = 'danceability';

UPDATE model SET class_mapping = '{
"acoustic": "Acoustic",
"not_acoustic": "Not acoustic"
}'::jsonb WHERE model.model = 'mood_acoustic';

UPDATE model SET class_mapping = '{
"aggressive": "Aggressive",
"not_aggressive": "Not aggressive"
}'::jsonb WHERE model.model = 'mood_aggressive';

UPDATE model SET class_mapping = '{
"electronic": "Electronic",
"not_electronic": "Not electronic"
}'::jsonb WHERE model.model = 'mood_electronic';

UPDATE model SET class_mapping = '{
"happy": "Happy",
"not_happy": "Not happy"
}'::jsonb WHERE model.model = 'mood_happy';

UPDATE model SET class_mapping = '{
"party": "Party",
"not_party": "Not party"
}'::jsonb WHERE model.model = 'mood_party';

UPDATE model SET class_mapping = '{
"relaxed": "Relaxed",
"not_relaxed": "Not relaxed"
}'::jsonb WHERE model.model = 'mood_relaxed';

UPDATE model SET class_mapping = '{
"sad": "Sad",
"not_sad": "Not sad"
}'::jsonb WHERE model.model = 'mood_sad';

UPDATE model SET class_mapping = '{
"Cluster1": "passionate, rousing, confident, boisterous, rowdy",
"Cluster2": "rollicking, cheerful, fun, sweet, amiable/good natured",
"Cluster3": "literate, poignant, wistful, bittersweet, autumnal, brooding",
"Cluster4": "humorous, silly, campy, quirky, whimsical, witty, wry",
"Cluster5": "aggressive, fiery, tense/anxious, intense, volatile, visceral"
}'::jsonb WHERE model.model = 'moods_mirex';

UPDATE model SET class_mapping = '{
"alternative": "Alternative",
"blues": "Blues",
"electronic": "Electronic",
"folkcountry": "Folk/Country",
"funksoulrnb": "Funk/Soul/RnB",
"jazz": "Jazz",
"pop": "Pop",
"raphiphop": "Rap/Hiphop",
"rock": "Rock"
}'::jsonb WHERE model.model = 'genre_dortmund';

UPDATE model SET class_mapping = '{
"ambient": "Ambient",
"dnb": "Drum and Bass",
"house": "House",
"techno": "Techno",
"trance": "Trance"
}'::jsonb WHERE model.model = 'genre_electronic';

UPDATE model SET class_mapping = '{
"cla": "Classical",
"dan": "Dance",
"hip": "Hiphop",
"jaz": "Jazz",
"pop": "Pop",
"rhy": "Rhythm and Blues",
"roc": "Rock",
"spe": "Speech"
}'::jsonb WHERE model.model = 'genre_rosamerica';

UPDATE model SET class_mapping = '{
"blu": "Blues",
"cla": "Classical",
"cou": "Country",
"dis": "Disco",
"hip": "Hiphop",
"jaz": "Jazz",
"met": "Metal",
"pop": "Pop",
"reg": "Reggae",
"roc": "Rock"
}'::jsonb WHERE model.model = 'genre_tzanetakis';

COMMIT;
3 changes: 2 additions & 1 deletion admin/sql/create_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ CREATE TABLE model (
model TEXT NOT NULL,
model_version TEXT NOT NULL,
date TIMESTAMP WITH TIME ZONE DEFAULT NOW(),
status model_status NOT NULL DEFAULT 'hidden'
status model_status NOT NULL DEFAULT 'hidden',
class_mapping JSONB
);

CREATE TABLE statistics (
Expand Down
97 changes: 97 additions & 0 deletions admin/updates/20190606-highlevel-model-mappings.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
BEGIN;

ALTER TABLE model ADD COLUMN class_mapping JSONB;

UPDATE model SET class_mapping = '{
"danceable": "Danceable",
"not_danceable": "Not danceable"
}'::jsonb WHERE model.model = 'danceability';

UPDATE model SET class_mapping = '{
"acoustic": "Acoustic",
"not_acoustic": "Not acoustic"
}'::jsonb WHERE model.model = 'mood_acoustic';

UPDATE model SET class_mapping = '{
"aggressive": "Aggressive",
"not_aggressive": "Not aggressive"
}'::jsonb WHERE model.model = 'mood_aggressive';

UPDATE model SET class_mapping = '{
"electronic": "Electronic",
"not_electronic": "Not electronic"
}'::jsonb WHERE model.model = 'mood_electronic';

UPDATE model SET class_mapping = '{
"happy": "Happy",
"not_happy": "Not happy"
}'::jsonb WHERE model.model = 'mood_happy';

UPDATE model SET class_mapping = '{
"party": "Party",
"not_party": "Not party"
}'::jsonb WHERE model.model = 'mood_party';

UPDATE model SET class_mapping = '{
"relaxed": "Relaxed",
"not_relaxed": "Not relaxed"
}'::jsonb WHERE model.model = 'mood_relaxed';

UPDATE model SET class_mapping = '{
"sad": "Sad",
"not_sad": "Not sad"
}'::jsonb WHERE model.model = 'mood_sad';

UPDATE model SET class_mapping = '{
"Cluster1": "passionate, rousing, confident, boisterous, rowdy",
"Cluster2": "rollicking, cheerful, fun, sweet, amiable/good natured",
"Cluster3": "literate, poignant, wistful, bittersweet, autumnal, brooding",
"Cluster4": "humorous, silly, campy, quirky, whimsical, witty, wry",
"Cluster5": "aggressive, fiery, tense/anxious, intense, volatile, visceral"
}'::jsonb WHERE model.model = 'moods_mirex';

UPDATE model SET class_mapping = '{
"alternative": "Alternative",
"blues": "Blues",
"electronic": "Electronic",
"folkcountry": "Folk/Country",
"funksoulrnb": "Funk/Soul/RnB",
"jazz": "Jazz",
"pop": "Pop",
"raphiphop": "Rap/Hiphop",
"rock": "Rock"
}'::jsonb WHERE model.model = 'genre_dortmund';

UPDATE model SET class_mapping = '{
"ambient": "Ambient",
"dnb": "Drum and Bass",
"house": "House",
"techno": "Techno",
"trance": "Trance"
}'::jsonb WHERE model.model = 'genre_electronic';

UPDATE model SET class_mapping = '{
"cla": "Classical",
"dan": "Dance",
"hip": "Hiphop",
"jaz": "Jazz",
"pop": "Pop",
"rhy": "Rhythm and Blues",
"roc": "Rock",
"spe": "Speech"
}'::jsonb WHERE model.model = 'genre_rosamerica';

UPDATE model SET class_mapping = '{
"blu": "Blues",
"cla": "Classical",
"cou": "Country",
"dis": "Disco",
"hip": "Hiphop",
"jaz": "Jazz",
"met": "Metal",
"pop": "Pop",
"reg": "Reggae",
"roc": "Rock"
}'::jsonb WHERE model.model = 'genre_tzanetakis';

COMMIT;
47 changes: 38 additions & 9 deletions db/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -312,17 +312,15 @@ def set_model_status(model_name, model_version, model_status):
"model_status": model_status})


def get_model(model_name, model_version):
def get_active_models():
with db.engine.begin() as connection:
query = text(
"""SELECT *
FROM model
WHERE model = :model_name
AND model_version = :model_version""")
WHERE status = :model_status""")
result = connection.execute(query,
{"model_name": model_name,
"model_version": model_version})
return result.fetchone()
{"model_status": STATUS_SHOW})
return [dict(row) for row in result.fetchall()]


def _get_model_id(model_name, version):
Expand Down Expand Up @@ -471,31 +469,54 @@ def load_many_low_level(recordings):
return dict(recordings_info)


def load_high_level(mbid, offset=0):
def map_highlevel_class_names(highlevel, mapping):
"""Convert class names from the classifier output to human readable names.
Arguments:
highlevel (dict): highlevel data dict containing shortened keys
mapping (dict): a mapping from class names -> human readable names
Returns:
the highlevel input with the keys of the `all` item, and the `value` item
changed to the values from the provided mapping
"""

new_all = {}
for cl, val in highlevel["all"].items():
new_all[mapping[cl]] = val
highlevel["all"] = new_all
highlevel["value"] = mapping[highlevel["value"]]

return highlevel


def load_high_level(mbid, offset=0, map_classes=False):
"""Load high-level data for a given MBID.
Arguments:
mbid (str): MBID to load
offset (int): submission offset for this MBID, starting from 0
map_classes (bool): if True, map class names to human readable values in the returned data
Raises:
NoDataFoundException: if this mbid doesn't exist or the offset is too high
"""

# in case it's a uuid
mbid = str(mbid).lower()
result = load_many_high_level([(mbid, offset)])
result = load_many_high_level([(mbid, offset)], map_classes)
if not result:
raise db.exceptions.NoDataFoundException

return result[mbid][str(offset)]


def load_many_high_level(recordings):
def load_many_high_level(recordings, map_classes=False):
"""Collect high-level data for multiple recordings.
Args:
recordings: A list of tuples (mbid, offset).
map_classes (bool): if True, map class names to human readable values in the returned data
Returns:
{"mbid-1": {"offset-1": {"metadata-1": metadata, "highlevel-1": highlevel},
Expand Down Expand Up @@ -543,6 +564,7 @@ def load_many_high_level(recordings):
, version.data as version
, ll.gid::text
, ll.submission_offset::text
, m.class_mapping
FROM highlevel_model hlmo
JOIN model m
ON m.id = hlmo.model
Expand All @@ -558,6 +580,10 @@ def load_many_high_level(recordings):
for row in model_result.fetchall():
model = row['model']
data = row['data']
mapping = row['class_mapping']
if map_classes and mapping:
data = map_highlevel_class_names(data, mapping)

data['version'] = row['version']

gid = row['gid']
Expand Down Expand Up @@ -647,6 +673,7 @@ def get_summary_data(mbid, offset=0):
"""Fetches the low-level and high-level features from for the specified MBID.
Args:
mbid: musicbrainz id to get data for
offset: Offset can be specified if you need to get summary for a
different submission. They are ordered by creation time.
Expand Down Expand Up @@ -674,6 +701,8 @@ def get_summary_data(mbid, offset=0):
try:
highlevel = load_high_level(mbid, offset)
summary['highlevel'] = highlevel
models = get_active_models()
summary['models'] = models
except db.exceptions.NoDataFoundException:
pass

Expand Down
Loading

0 comments on commit 08f4dc5

Please sign in to comment.