def testInterp(self): xs = [1, 2, 3] ys = [4, 5, 6] interp = thinkstats.Interpolator(xs, ys) y = interp.Lookup(1) self.assertAlmostEqual(y, 4) y = interp.Lookup(2) self.assertAlmostEqual(y, 5) y = interp.Lookup(3) self.assertAlmostEqual(y, 6) y = interp.Lookup(1.5) self.assertAlmostEqual(y, 4.5) y = interp.Lookup(2.75) self.assertAlmostEqual(y, 5.75) x = interp.Reverse(4) self.assertAlmostEqual(x, 1) x = interp.Reverse(6) self.assertAlmostEqual(x, 3) x = interp.Reverse(4.5) self.assertAlmostEqual(x, 1.5) x = interp.Reverse(5.75) self.assertAlmostEqual(x, 2.75)
def ReadScale(filename='sat_scale.csv', col=2): """Reads a CSV file of SAT scales (maps from raw score to standard score). Args: filename: string filename col: which column to start with (0=Reading, 2=Math, 4=Writing) Returns: list of (raw score, standardize score) pairs """ def ParseRange(s): t = [int(x) for x in s.split('-')] return 1.0 * sum(t) / len(t) fp = open(filename) reader = csv.reader(fp) raws = [] scores = [] for t in reader: try: raw = int(t[col]) raws.append(raw) score = ParseRange(t[col + 1]) scores.append(score) except: pass raws.sort() scores.sort() return thinkstats.Interpolator(raws, scores)