def linscale(seq, minval=0.0, maxval=1.0):
    """Linearly scales all the values in the sequence to lie between the given values.
    Shifts up to minval and scales by the difference ``maxval-minval``
    If all values are identical, then sets them to `minval`."""
    m, M = min(seq), max(seq)
    def sc(s, m=m, M=M):
        if m==M: return minval
        return minval + ((s-m)/float(M-m))*(maxval-minval)
    seq = [sc(s) for s in seq]
    return seq