logo Netzschleuder network catalog, repository, and centrifuge

History of tgbn_genre

The changes to the source code of this entry in the repository, newest first, each with the differences it made to the entry's files. They are also listed on GitLab.

[entries] New entry: tgbn_genre by Tiago de Paula Peixoto c9039e6
diff --git a/entries/tgbn_genre/__init__.py b/entries/tgbn_genre/__init__.py
new file mode 100644
index 0000000..d914f8c
--- /dev/null
+++ b/entries/tgbn_genre/__init__.py
@@ -0,0 +1,77 @@
+#! /usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+# Copyright (C) 2026 Tiago de Paula Peixoto <tiago@skewed.de>
+#
+# This program is free software: you can redistribute it and/or modify it under
+# the terms of the GNU Affero General Public License as published by the Free
+# Software Foundation, either version 3 of the License, or (at your option) any
+# later version.
+#
+# This program 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 Affero General Public License for more
+# details.
+#
+# You should have received a copy of the GNU Affero General Public License along
+# with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+
+
+import pandas
+
+from .. import *
+
+
+title = "Last.fm users and the music genres they listen to (tgbn-genre, TGB, 2005-2009)"
+description = """A temporal bipartite network of 992 users of the music website Last.fm and the music genres of the songs they listened to, from February 2005 to June 2009, from the `tgbn-genre` dataset of the Temporal Graph Benchmark (TGB). TGB built it by matching the songs of the Last.fm listening histories of about 1,000 users (the "Last.fm 1K" data collected by Ò. Celma) with the genre tags of the songs in the Million Song Dataset. Each listen of a song gives a timestamped edge from the user to each genre of the song, weighted by the weight of the genre for the song; TGB kept only the genres with a weight of at least 10% for a song, and that occur at least a thousand times, and cleaned the genre names of typos. A listen may thus give several edges, and repeated edges.
+
+The network has the following properties:
+
+- `is_genre`: whether the node is a genre (otherwise a user).
+- `name`: the identifier of the user (e.g. `user_000871`) or the name of the genre (e.g. `80s`).
+- Edge `time`: the time of the listen, in Unix time.
+- Edge `weight`: the weight of the genre for the song listened to (between 0 and 1)."""
+tags = ['Social', 'Online', 'Preferences', 'Weighted', 'Multigraph', 'Timestamps', 'Metadata']
+url = ['https://tgb.complexdatalab.com/docs/nodeprop/',
+       'https://github.com/shenyangHuang/TGB',
+       'http://millionsongdataset.com/']
+citation = [('S. Huang, F. Poursafaei, J. Danovitch, M. Fey, W. Hu, E. Rossi, J. Leskovec, M. Bronstein, G. Rabusseau, R. Rabbany, "Temporal Graph Benchmark for Machine Learning on Temporal Graphs." Advances in Neural Information Processing Systems 36 (Datasets and Benchmarks Track) (2023)', 'https://doi.org/10.48550/arXiv.2307.01026'),
+            ('Ò. Celma, "Music Recommendation and Discovery in the Long Tail." Springer (2010)', 'https://doi.org/10.1007/978-3-642-13287-2'),
+            ('T. Bertin-Mahieux, D. P. W. Ellis, B. Whitman, P. Lamere, "The Million Song Dataset." Proceedings of the 12th International Society for Music Information Retrieval Conference (ISMIR), 591-596 (2011)', 'https://doi.org/10.7916/D8NZ8J07')]
+icon_hash = None
+upstream_license = ('MIT', None)
+upstream_prefix = 'https://object-arbutus.alliancecan.ca/swift/v1/14c95234f6cd4a21a47deafe20cce2a7/tgb'
+files = [('tgbn-genre.zip:tgbn-genre_edgelist.csv', None, None)]
+
+def fetch_upstream(force=False):
+    return fetch_upstream_files(__name__.split(".")[-1], upstream_prefix, files,
+                                force)
+
+@cache_network()
+@coerce_props(vforce={"name": "string"})
+@annotate()
+def parse(alts=None):
+    global files
+    name = __name__.split(".")[-1]
+    for fname, alt, fmt in files:
+        if alts is not None and alt not in alts:
+            continue
+        # "ts,user_id,genre,weight"
+        with open_upstream_file(name, fname, "rb") as f:
+            df = pandas.read_csv(f, dtype={"ts": "int64", "user_id": "category",
+                                           "genre": "category", "weight": "float64"},
+                                 keep_default_na=False)
+        users = df["user_id"].cat.categories
+        genres = df["genre"].cat.categories
+        N = len(users)
+        g = Graph(directed=True)
+        g.add_vertex(N + len(genres))
+        g.vp.is_genre = g.new_vp("bool")
+        g.vp.is_genre.a[N:] = True
+        g.vp.name = g.new_vp("string", vals=list(users) + list(genres))
+        g.add_edge_list(numpy.column_stack([df["user_id"].cat.codes.values,
+                                            N + df["genre"].cat.codes.values]))
+        g.ep.time = g.new_ep("int64_t", vals=df["ts"].values)
+        g.ep.weight = g.new_ep("double", vals=df["weight"].values)
+        yield alt, g