def _insert_song_to_graph(self, new_song, star_threshold=0.75, iterstep_threshold=50): next_len = len(self._song_list) if len(self) < iterstep_threshold: iterstep = 1 else: iterstep = round(max(1, math.log(max(next_len, 1)))) # Step 1: Find samples with similar songs (similar to the base step) distances = deque() for song in self._song_list[::iterstep]: if song is not None: distance = Song.distance_compute(song, new_song) distances.append((song, distance)) new_song.distance_add(song, distance) # Step 2: Short refinement step for song, distance in distances: if distance.distance > star_threshold: for neighbor in song.neighbors(): distance = new_song.distance_compute(neighbor) new_song.distance_add(neighbor, distance) return new_song.uid
def rebuild_stupid(self): """(Re)build the graph by calculating the combination of all songs. This is a *very* expensive operation which takes quadratic time and only should be ever used for a small amount of songs where accuracy matters even more thant time. """ for song_a, song_b in combinations(self._song_list, 2): distance = Song.distance_compute(song_a, song_b) Song.distance_add(song_a, song_b, distance)