<s> from __future__ import division <EOL> """<STR_LIT>""" <EOL> def _rank_dists ( ranks1 , ranks2 ) : <EOL> """<STR_LIT>""" <EOL> ranks1 = dict ( ranks1 ) <EOL> ranks2 = dict ( ranks2 ) <EOL> for k in ranks1 : <EOL> try : <EOL> yield k , ranks1 [ k ] - ranks2 [ k ] <EOL> except KeyError : <EOL> pass <EOL> def spearman_correlation ( ranks1 , ranks2 ) : <EOL> """<STR_LIT>""" <EOL> n = <NUM_LIT:0> <EOL> res = <NUM_LIT:0> <EOL> for k , d in _rank_dists ( ranks1 , ranks2 ) : <EOL> res += d * d <EOL> n += <NUM_LIT:1> <EOL> try : <EOL> return <NUM_LIT:1> - ( <NUM_LIT:6> * res / ( n * ( n * n - <NUM_LIT:1> ) ) ) <EOL> except ZeroDivisionError : <EOL> return <NUM_LIT:0.0> <EOL> def ranks_from_sequence ( seq ) : <EOL> """<STR_LIT>""" <EOL> return ( ( k , i ) for i , k in enumerate ( seq ) ) <EOL> def ranks_from_scores ( scores , rank_gap = <NUM_LIT> ) : <EOL> """<STR_LIT>""" <EOL> prev_score = None <EOL> rank = <NUM_LIT:0> <EOL> for i , ( key , score ) in enumerate ( scores ) : <EOL> try : <EOL> if abs ( score - prev_score ) > rank_gap : <EOL> rank = i <EOL> except TypeError : <EOL> pass <EOL> yield key , rank <EOL> prev_score = score </s>
