Example #1
0
File: library.py Project: pscn/ads
 def _similar_artists(self, artist_nameA, artist_nameB, match, source,
     locked=False):
   if not locked: self.acquire()
   #self._logger.debug(u"%s: [%s]-[%s] %2.2f" % (source, artist_nameA,
   #    artist_nameB, match))
   artistA = ArtistFactory.by_key(artist_nameA)
   artistB = ArtistFactory.by_key(artist_nameB)
   #if not artistA: self._logger.debug(u"similar_artists[%s]: not found" %
   #    artist_nameA)
   #if not artistB: self._logger.debug(u"similar_artists[%s]: not found" %
   #    artist_nameB)
   if artistA and artistB:
     relation = ArtistRelationFactory.get(artistA, artistB)
     old_rating = relation.rating
     relation.rate(0.75 + 0.25 * match)
     self._logger.debug(u"%s [%s]-[%s] m(%2.2f) r(%2.2f|%2.2f)" % (source,
         artistA, artistB, match, relation.rating, old_rating))
     if self._queue_lookup_results:
       if self._lastfm:
         self._lastfm.similar_artists_low(self.similar_artists, artist_nameB,
             self._thres_lastfm_lookup)
       if self._echonest:
         self._echonest.similar_artists_low(self.similar_artists, artist_nameB,
             self._thres_lastfm_lookup)
   if not locked: self.release()
Example #2
0
 def testArtistRef(self):
   # test if 2 calls to the same artist return the same reference
   Factories.clear()
   artist = ArtistFactory.get(self.artist)
   artist.test = self.artist_test
   artist2 = ArtistFactory.get(self.artist)
   self.assertEqual(artist.test, artist2.test)
Example #3
0
  def testArtistClear(self):
    Factories.clear()
    artist = ArtistFactory.get(self.artist)
    artist.test = self.artist_test

    Factories.clear()
    artist = ArtistFactory.get(self.artist)
    self.assertEqual(artist.test, None)
Example #4
0
 def __setstate__(self, dict):
   artistA = ArtistFactory.by_key(dict['artistA_key'])
   del dict['artistA_key']
   artistB = ArtistFactory.by_key(dict['artistB_key'])
   del dict['artistB_key']
   self.__dict__ = dict
   self.artistA = artistA
   self.artistB = artistB
Example #5
0
 def testArtistRelationRef(self):
   # test if 2 calls to the same artist return the same reference
   Factories.clear()
   artistA = ArtistFactory.get(self.artistA)
   artistB = ArtistFactory.get(self.artistB)
   artist_relationA = ArtistRelationFactory.get(artistA, artistB)
   artist_relationA.test = self.artist_relation_test
   artist_relationB = ArtistRelationFactory.get(artistA, artistB)
   self.assertEqual(artist_relationA.test, artist_relationB.test)
Example #6
0
  def testArtistPickle(self):
    Factories.clear()
    artist = ArtistFactory.get(self.artist)
    artist.test = self.artist_test
    state = pickle.dumps(Factories.getstate())

    Factories.clear()
    pickle.loads(state)
    artist = ArtistFactory.get(self.artist)

    self.assertEqual(artist.test, self.artist_test)
Example #7
0
 def testArtistRelation(self):
   Factories.clear()
   artistA = ArtistFactory.get(self.artistA)
   artistA.test = self.artistA_test
   artistB = ArtistFactory.get(self.artistB)
   artistB.test = self.artistB_test
   artist_relation = ArtistRelationFactory.get(artistA, artistB)
   artist_relation.test = self.artist_relation_test
   self.assertEqual(artist_relation.artistA.test, self.artistA_test)
   self.assertEqual(artist_relation.artistB.test, self.artistB_test)
   self.assertEqual(artist_relation.test, self.artist_relation_test)
Example #8
0
  def testArtistRelationPickle(self):
    Factories.clear()
    artistA = ArtistFactory.get(self.artistA)
    artistB = ArtistFactory.get(self.artistB)
    artist_relation = ArtistRelationFactory.get(artistA, artistB)
    artist_relation.test = self.artist_relation_test

    state = pickle.dumps(Factories.getstate())
    Factories.clear()
    pickle.loads(state)

    artistA = ArtistFactory.get(self.artistA)
    artistB = ArtistFactory.get(self.artistB)
    artist_relation = ArtistRelationFactory.get(artistA, artistB)
    artist_relation.test = self.artist_relation_test
    self.assertEqual(artist_relation.test, self.artist_relation_test)
Example #9
0
File: track.py Project: pscn/ads
 def __setstate__(self, dict):
   # Logger.info(u"track dict in: %s" % dict)
   artist = ArtistFactory.by_key(dict['artist_key'])
   del dict['artist_key']
   ratingref = RatingFactory.by_key(dict['ratingref_key'])
   del dict['ratingref_key']
   self.__dict__ = dict
   self.artist = artist
   self.ratingref = ratingref
   # Logger.info(u"track.dict out: %s" % self.__dict__)
   TrackFactory.register(self)
Example #10
0
File: library.py Project: pscn/ads
 def _reset(self, full=False):
   """ clear recent play history + unset last played track """
   self.acquire()
   self._history.clear()
   self._lastplayed_track = None
   self._playing_track = None
   for track in TrackFactory.active_tracks():
     track.ranking = 0.5
     track.relation = 0.5
     track.relation_old = 0.5
     track.relation_cnt = 0
   if full:
     for track in TrackFactory.active_tracks():
       track.lastplayed = 0
       track.laststarted = 0
       track.lastqueued = 0
     for artist in ArtistFactory.active_artists():
       artist.lastplayed = 0
       artist.laststarted = 0
       artist.lastqueued = 0
   self.release()
Example #11
0
 def testArtist(self):
   Factories.clear()
   artist = ArtistFactory.get(self.artist)
   artist.test = self.artist_test
   self.assertEqual(artist.name, self.artist)
   self.assertEqual(artist.test, self.artist_test)
Example #12
0
File: track.py Project: pscn/ads
 def __init__(self, artist_name, track_title):
   self.artist = ArtistFactory.get(artist_name)
   self.title = track_title
   self.ratingref = RatingFactory.get(0.6, 0.1)
   self.rating = self.ratingref.rating
Example #13
0
File: library.py Project: pscn/ads
 def dump_stats(self):
   self._logger.info(u"%d artists, %d tracks %d files" % (
       ArtistFactory.len(), TrackFactory.len(), FileFactory.len()))
Example #14
0
File: library.py Project: pscn/ads
  def _update_ranking(self, locked=False):
    benchmark = Benchmark()
    if not locked: self.acquire()

    seed_track = None
    if self._lastplayed_track:
      seed_track = self._lastplayed_track
    elif self._playing_track:
      seed_track = self._playing_track

    if not self._relation_resetted:
      benchmark.start()
      self._relation_resetted = True
      for track in TrackFactory.active_tracks():
        track.relation_old = track.relation
        track.relation_sum = 0.0
        track.relation_cnt = 0
      for artist in ArtistFactory.active_artists():
        artist.relation_old = artist.relation
        artist.relation_sum = 0.0
        artist.relation_cnt = 0
      self._logger.info(u"update ranking: resetting took %s" % benchmark)

    has_active_tracks = False
    for track in TrackFactory.active_tracks():
      has_active_tracks = True
      break

    benchmark.start()
    # new relation = old relation * decay
    # e.g. for decay = 0.5 (0.75)
    # decfacA = 0.5 * 0.5 = 0.25 (0.75 * 0.5 = 0.375)
    # decfacB = 1.0 - 0.5 = 0.5 (1.0 - 0.75 = 0.25)
    # relation_old=0.75 -> 0.25+0.5*0.75=0.625 (0.375+0.25*0.75=0.5625)
    decfacA = self._relation_decay * 0.5
    decfacB = 1.0 - self._relation_decay
    for track in TrackFactory.active_tracks():
      if (track.relation_old > 0.501) or (track.relation_old < 0.499):
        track.relation = (decfacA + decfacB * track.relation_old)
      else:
        track.relation = 0.5

    for artist in ArtistFactory.active_artists():
      if (artist.relation_old > 0.501) or (artist.relation_old < 0.499):
        artist.relation = (decfacA + decfacB * artist.relation_old)
      else:
        artist.relation = 0.5

    self._logger.info(u"update ranking: set old relation + decay took %s" %
        benchmark)

    if has_active_tracks:
      self._ranking_updated = True
      self.seed(seed_track, True)
      benchmark.start()
      at = []
      tt = []
      for track in TrackFactory.active_tracks():
        """
        so we have:
          ranking   [0-1.0] (old)
          rating    [0-1.0]
          relation  [0-1.0]
          random    [0-1.0]
        and:
          factor min(track_lastplayed/started,artist_lastplayed/started) [0-1.0]
          ? moved to next_file()
        """
        artist = track.artist
        r = random()
        # calculate new ranking
        if track.boost:
          self._logger.info(u"pre boost: %s" % track)
          track.relation = (track.relation + 99.0) / 100.0
          self._logger.info(u"post boost: %s" % track)
        elif track.ban:
          track.relation = 0.0

        # mix with artist relation if we don't have a track relation
        if track.relation_cnt == 0:
          if artist.relation_cnt > 0:
            track.relation = (0.75 * track.relation + 0.25 * artist.relation)
            top_ten(at, u'relation cnt = {} with {} now {} to {}'.format(
                artist.relation_cnt, scale_rating(artist.relation),
                scale_rating(track.relation), artist), track.relation)
        else:
          top_ten(tt, u'relation cnt = {} with {} to {}'.format(
              track.relation_cnt, scale_rating(track.relation), track),
              track.relation)

        track.ranking = (
            self._factor_ranking['rating'] * track.get_rating() +
            self._factor_ranking['relation'] * track.relation +
            self._factor_ranking['random'] * r
            )

      self._logger.info(u"update ranking: took %s" % benchmark)
    self._update_queued = False
    if not locked: self.release()